x25_route.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifdef KW_TAINT_ANALYSIS
  24. extern void * get_tainted_stuff();
  25. #endif
  26. LIST_HEAD(x25_route_list);
  27. DEFINE_RWLOCK(x25_route_list_lock);
  28. /*
  29. * Add a new route.
  30. */
  31. static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
  32. struct net_device *dev)
  33. {
  34. struct x25_route *rt;
  35. struct list_head *entry;
  36. int rc = -EINVAL;
  37. write_lock_bh(&x25_route_list_lock);
  38. list_for_each(entry, &x25_route_list) {
  39. rt = list_entry(entry, struct x25_route, node);
  40. if (!memcmp(&rt->address, address, sigdigits) &&
  41. rt->sigdigits == sigdigits)
  42. goto out;
  43. }
  44. rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
  45. rc = -ENOMEM;
  46. if (!rt)
  47. goto out;
  48. strcpy(rt->address.x25_addr, "000000000000000");
  49. memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
  50. rt->sigdigits = sigdigits;
  51. rt->dev = dev;
  52. atomic_set(&rt->refcnt, 1);
  53. list_add(&rt->node, &x25_route_list);
  54. rc = 0;
  55. out:
  56. write_unlock_bh(&x25_route_list_lock);
  57. return rc;
  58. }
  59. /**
  60. * __x25_remove_route - remove route from x25_route_list
  61. * @rt - route to remove
  62. *
  63. * Remove route from x25_route_list. If it was there.
  64. * Caller must hold x25_route_list_lock.
  65. */
  66. static void __x25_remove_route(struct x25_route *rt)
  67. {
  68. if (rt->node.next) {
  69. list_del(&rt->node);
  70. x25_route_put(rt);
  71. }
  72. }
  73. static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
  74. struct net_device *dev)
  75. {
  76. struct x25_route *rt;
  77. struct list_head *entry;
  78. int rc = -EINVAL;
  79. write_lock_bh(&x25_route_list_lock);
  80. list_for_each(entry, &x25_route_list) {
  81. rt = list_entry(entry, struct x25_route, node);
  82. if (!memcmp(&rt->address, address, sigdigits) &&
  83. rt->sigdigits == sigdigits && rt->dev == dev) {
  84. __x25_remove_route(rt);
  85. rc = 0;
  86. break;
  87. }
  88. }
  89. write_unlock_bh(&x25_route_list_lock);
  90. return rc;
  91. }
  92. /*
  93. * A device has been removed, remove its routes.
  94. */
  95. void x25_route_device_down(struct net_device *dev)
  96. {
  97. struct x25_route *rt;
  98. struct list_head *entry, *tmp;
  99. write_lock_bh(&x25_route_list_lock);
  100. list_for_each_safe(entry, tmp, &x25_route_list) {
  101. rt = list_entry(entry, struct x25_route, node);
  102. if (rt->dev == dev)
  103. __x25_remove_route(rt);
  104. }
  105. write_unlock_bh(&x25_route_list_lock);
  106. /* Remove any related forwarding */
  107. x25_clear_forward_by_dev(dev);
  108. }
  109. /*
  110. * Check that the device given is a valid X.25 interface that is "up".
  111. */
  112. struct net_device *x25_dev_get(char *devname)
  113. {
  114. struct net_device *dev = dev_get_by_name(&init_net, devname);
  115. if (dev &&
  116. (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
  117. #if IS_ENABLED(CONFIG_LLC)
  118. && dev->type != ARPHRD_ETHER
  119. #endif
  120. ))){
  121. dev_put(dev);
  122. dev = NULL;
  123. }
  124. return dev;
  125. }
  126. /**
  127. * x25_get_route - Find a route given an X.25 address.
  128. * @addr - address to find a route for
  129. *
  130. * Find a route given an X.25 address.
  131. */
  132. struct x25_route *x25_get_route(struct x25_address *addr)
  133. {
  134. struct x25_route *rt, *use = NULL;
  135. struct list_head *entry;
  136. read_lock_bh(&x25_route_list_lock);
  137. list_for_each(entry, &x25_route_list) {
  138. rt = list_entry(entry, struct x25_route, node);
  139. if (!memcmp(&rt->address, addr, rt->sigdigits)) {
  140. if (!use)
  141. use = rt;
  142. else if (rt->sigdigits > use->sigdigits)
  143. use = rt;
  144. }
  145. }
  146. if (use)
  147. x25_route_hold(use);
  148. read_unlock_bh(&x25_route_list_lock);
  149. return use;
  150. }
  151. /*
  152. * Handle the ioctls that control the routing functions.
  153. */
  154. int x25_route_ioctl(unsigned int cmd, void __user *arg_actual)
  155. {
  156. struct x25_route_struct rt;
  157. struct net_device *dev;
  158. int rc = -EINVAL;
  159. #ifdef KW_TAINT_ANALYSIS
  160. void __user *arg = (void __user *)get_tainted_stuff();
  161. #else
  162. void __user *arg = arg_actual;
  163. #endif
  164. if (cmd != SIOCADDRT && cmd != SIOCDELRT)
  165. goto out;
  166. rc = -EFAULT;
  167. if (copy_from_user(&rt, arg, sizeof(rt)))
  168. goto out;
  169. rc = -EINVAL;
  170. if (rt.sigdigits > 15)
  171. goto out;
  172. dev = x25_dev_get(rt.device);
  173. if (!dev)
  174. goto out;
  175. if (cmd == SIOCADDRT)
  176. rc = x25_add_route(&rt.address, rt.sigdigits, dev);
  177. else
  178. rc = x25_del_route(&rt.address, rt.sigdigits, dev);
  179. dev_put(dev);
  180. out:
  181. return rc;
  182. }
  183. /*
  184. * Release all memory associated with X.25 routing structures.
  185. */
  186. void __exit x25_route_free(void)
  187. {
  188. struct x25_route *rt;
  189. struct list_head *entry, *tmp;
  190. write_lock_bh(&x25_route_list_lock);
  191. list_for_each_safe(entry, tmp, &x25_route_list) {
  192. rt = list_entry(entry, struct x25_route, node);
  193. __x25_remove_route(rt);
  194. }
  195. write_unlock_bh(&x25_route_list_lock);
  196. }