x25_route.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Jonathan Naylor Started coding.
  18. */
  19. #include <linux/if_arp.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <net/x25.h>
  23. LIST_HEAD(x25_route_list);
  24. DEFINE_RWLOCK(x25_route_list_lock);
  25. /*
  26. * Add a new route.
  27. */
  28. static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
  29. struct net_device *dev)
  30. {
  31. struct x25_route *rt;
  32. struct list_head *entry;
  33. int rc = -EINVAL;
  34. write_lock_bh(&x25_route_list_lock);
  35. list_for_each(entry, &x25_route_list) {
  36. rt = list_entry(entry, struct x25_route, node);
  37. if (!memcmp(&rt->address, address, sigdigits) &&
  38. rt->sigdigits == sigdigits)
  39. goto out;
  40. }
  41. rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
  42. rc = -ENOMEM;
  43. if (!rt)
  44. goto out;
  45. strcpy(rt->address.x25_addr, "000000000000000");
  46. memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
  47. rt->sigdigits = sigdigits;
  48. rt->dev = dev;
  49. atomic_set(&rt->refcnt, 1);
  50. list_add(&rt->node, &x25_route_list);
  51. rc = 0;
  52. out:
  53. write_unlock_bh(&x25_route_list_lock);
  54. return rc;
  55. }
  56. /**
  57. * __x25_remove_route - remove route from x25_route_list
  58. * @rt: route to remove
  59. *
  60. * Remove route from x25_route_list. If it was there.
  61. * Caller must hold x25_route_list_lock.
  62. */
  63. static void __x25_remove_route(struct x25_route *rt)
  64. {
  65. if (rt->node.next) {
  66. list_del(&rt->node);
  67. x25_route_put(rt);
  68. }
  69. }
  70. static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
  71. struct net_device *dev)
  72. {
  73. struct x25_route *rt;
  74. struct list_head *entry;
  75. int rc = -EINVAL;
  76. write_lock_bh(&x25_route_list_lock);
  77. list_for_each(entry, &x25_route_list) {
  78. rt = list_entry(entry, struct x25_route, node);
  79. if (!memcmp(&rt->address, address, sigdigits) &&
  80. rt->sigdigits == sigdigits && rt->dev == dev) {
  81. __x25_remove_route(rt);
  82. rc = 0;
  83. break;
  84. }
  85. }
  86. write_unlock_bh(&x25_route_list_lock);
  87. return rc;
  88. }
  89. /*
  90. * A device has been removed, remove its routes.
  91. */
  92. void x25_route_device_down(struct net_device *dev)
  93. {
  94. struct x25_route *rt;
  95. struct list_head *entry, *tmp;
  96. write_lock_bh(&x25_route_list_lock);
  97. list_for_each_safe(entry, tmp, &x25_route_list) {
  98. rt = list_entry(entry, struct x25_route, node);
  99. if (rt->dev == dev)
  100. __x25_remove_route(rt);
  101. }
  102. write_unlock_bh(&x25_route_list_lock);
  103. /* Remove any related forwarding */
  104. x25_clear_forward_by_dev(dev);
  105. }
  106. /*
  107. * Check that the device given is a valid X.25 interface that is "up".
  108. */
  109. struct net_device *x25_dev_get(char *devname)
  110. {
  111. struct net_device *dev = dev_get_by_name(&init_net, devname);
  112. if (dev &&
  113. (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
  114. #if IS_ENABLED(CONFIG_LLC)
  115. && dev->type != ARPHRD_ETHER
  116. #endif
  117. ))){
  118. dev_put(dev);
  119. dev = NULL;
  120. }
  121. return dev;
  122. }
  123. /**
  124. * x25_get_route - Find a route given an X.25 address.
  125. * @addr - address to find a route for
  126. *
  127. * Find a route given an X.25 address.
  128. */
  129. struct x25_route *x25_get_route(struct x25_address *addr)
  130. {
  131. struct x25_route *rt, *use = NULL;
  132. struct list_head *entry;
  133. read_lock_bh(&x25_route_list_lock);
  134. list_for_each(entry, &x25_route_list) {
  135. rt = list_entry(entry, struct x25_route, node);
  136. if (!memcmp(&rt->address, addr, rt->sigdigits)) {
  137. if (!use)
  138. use = rt;
  139. else if (rt->sigdigits > use->sigdigits)
  140. use = rt;
  141. }
  142. }
  143. if (use)
  144. x25_route_hold(use);
  145. read_unlock_bh(&x25_route_list_lock);
  146. return use;
  147. }
  148. /*
  149. * Handle the ioctls that control the routing functions.
  150. */
  151. int x25_route_ioctl(unsigned int cmd, void __user *arg)
  152. {
  153. struct x25_route_struct rt;
  154. struct net_device *dev;
  155. int rc = -EINVAL;
  156. if (cmd != SIOCADDRT && cmd != SIOCDELRT)
  157. goto out;
  158. rc = -EFAULT;
  159. if (copy_from_user(&rt, arg, sizeof(rt)))
  160. goto out;
  161. rc = -EINVAL;
  162. if (rt.sigdigits > 15)
  163. goto out;
  164. dev = x25_dev_get(rt.device);
  165. if (!dev)
  166. goto out;
  167. if (cmd == SIOCADDRT)
  168. rc = x25_add_route(&rt.address, rt.sigdigits, dev);
  169. else
  170. rc = x25_del_route(&rt.address, rt.sigdigits, dev);
  171. dev_put(dev);
  172. out:
  173. return rc;
  174. }
  175. /*
  176. * Release all memory associated with X.25 routing structures.
  177. */
  178. void __exit x25_route_free(void)
  179. {
  180. struct x25_route *rt;
  181. struct list_head *entry, *tmp;
  182. write_lock_bh(&x25_route_list_lock);
  183. list_for_each_safe(entry, tmp, &x25_route_list) {
  184. rt = list_entry(entry, struct x25_route, node);
  185. __x25_remove_route(rt);
  186. }
  187. write_unlock_bh(&x25_route_list_lock);
  188. }