ipx_route.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Implements the IPX routing routines.
  3. * Code moved from af_ipx.c.
  4. *
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2003
  6. *
  7. * See net/ipx/ChangeLog.
  8. */
  9. #include <linux/list.h>
  10. #include <linux/route.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <net/ipx.h>
  14. #include <net/sock.h>
  15. LIST_HEAD(ipx_routes);
  16. DEFINE_RWLOCK(ipx_routes_lock);
  17. extern struct ipx_interface *ipx_internal_net;
  18. extern struct ipx_interface *ipxitf_find_using_net(__be32 net);
  19. extern int ipxitf_demux_socket(struct ipx_interface *intrfc,
  20. struct sk_buff *skb, int copy);
  21. extern int ipxitf_demux_socket(struct ipx_interface *intrfc,
  22. struct sk_buff *skb, int copy);
  23. struct ipx_route *ipxrtr_lookup(__be32 net)
  24. {
  25. struct ipx_route *r;
  26. read_lock_bh(&ipx_routes_lock);
  27. list_for_each_entry(r, &ipx_routes, node)
  28. if (r->ir_net == net) {
  29. ipxrtr_hold(r);
  30. goto unlock;
  31. }
  32. r = NULL;
  33. unlock:
  34. read_unlock_bh(&ipx_routes_lock);
  35. return r;
  36. }
  37. /*
  38. * Caller must hold a reference to intrfc
  39. */
  40. int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
  41. unsigned char *node)
  42. {
  43. struct ipx_route *rt;
  44. int rc;
  45. /* Get a route structure; either existing or create */
  46. rt = ipxrtr_lookup(network);
  47. if (!rt) {
  48. rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
  49. rc = -EAGAIN;
  50. if (!rt)
  51. goto out;
  52. atomic_set(&rt->refcnt, 1);
  53. ipxrtr_hold(rt);
  54. write_lock_bh(&ipx_routes_lock);
  55. list_add(&rt->node, &ipx_routes);
  56. write_unlock_bh(&ipx_routes_lock);
  57. } else {
  58. rc = -EEXIST;
  59. if (intrfc == ipx_internal_net)
  60. goto out_put;
  61. }
  62. rt->ir_net = network;
  63. rt->ir_intrfc = intrfc;
  64. if (!node) {
  65. memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
  66. rt->ir_routed = 0;
  67. } else {
  68. memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
  69. rt->ir_routed = 1;
  70. }
  71. rc = 0;
  72. out_put:
  73. ipxrtr_put(rt);
  74. out:
  75. return rc;
  76. }
  77. void ipxrtr_del_routes(struct ipx_interface *intrfc)
  78. {
  79. struct ipx_route *r, *tmp;
  80. write_lock_bh(&ipx_routes_lock);
  81. list_for_each_entry_safe(r, tmp, &ipx_routes, node)
  82. if (r->ir_intrfc == intrfc) {
  83. list_del(&r->node);
  84. ipxrtr_put(r);
  85. }
  86. write_unlock_bh(&ipx_routes_lock);
  87. }
  88. static int ipxrtr_create(struct ipx_route_definition *rd)
  89. {
  90. struct ipx_interface *intrfc;
  91. int rc = -ENETUNREACH;
  92. /* Find the appropriate interface */
  93. intrfc = ipxitf_find_using_net(rd->ipx_router_network);
  94. if (!intrfc)
  95. goto out;
  96. rc = ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
  97. ipxitf_put(intrfc);
  98. out:
  99. return rc;
  100. }
  101. static int ipxrtr_delete(__be32 net)
  102. {
  103. struct ipx_route *r, *tmp;
  104. int rc;
  105. write_lock_bh(&ipx_routes_lock);
  106. list_for_each_entry_safe(r, tmp, &ipx_routes, node)
  107. if (r->ir_net == net) {
  108. /* Directly connected; can't lose route */
  109. rc = -EPERM;
  110. if (!r->ir_routed)
  111. goto out;
  112. list_del(&r->node);
  113. ipxrtr_put(r);
  114. rc = 0;
  115. goto out;
  116. }
  117. rc = -ENOENT;
  118. out:
  119. write_unlock_bh(&ipx_routes_lock);
  120. return rc;
  121. }
  122. /*
  123. * The skb has to be unshared, we'll end up calling ipxitf_send, that'll
  124. * modify the packet
  125. */
  126. int ipxrtr_route_skb(struct sk_buff *skb)
  127. {
  128. struct ipxhdr *ipx = ipx_hdr(skb);
  129. struct ipx_route *r = ipxrtr_lookup(IPX_SKB_CB(skb)->ipx_dest_net);
  130. if (!r) { /* no known route */
  131. kfree_skb(skb);
  132. return 0;
  133. }
  134. ipxitf_hold(r->ir_intrfc);
  135. ipxitf_send(r->ir_intrfc, skb, r->ir_routed ?
  136. r->ir_router_node : ipx->ipx_dest.node);
  137. ipxitf_put(r->ir_intrfc);
  138. ipxrtr_put(r);
  139. return 0;
  140. }
  141. /*
  142. * Route an outgoing frame from a socket.
  143. */
  144. int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
  145. struct msghdr *msg, size_t len, int noblock)
  146. {
  147. struct sk_buff *skb;
  148. struct ipx_sock *ipxs = ipx_sk(sk);
  149. struct ipx_interface *intrfc;
  150. struct ipxhdr *ipx;
  151. size_t size;
  152. int ipx_offset;
  153. struct ipx_route *rt = NULL;
  154. int rc;
  155. /* Find the appropriate interface on which to send packet */
  156. if (!usipx->sipx_network && ipx_primary_net) {
  157. usipx->sipx_network = ipx_primary_net->if_netnum;
  158. intrfc = ipx_primary_net;
  159. } else {
  160. rt = ipxrtr_lookup(usipx->sipx_network);
  161. rc = -ENETUNREACH;
  162. if (!rt)
  163. goto out;
  164. intrfc = rt->ir_intrfc;
  165. }
  166. ipxitf_hold(intrfc);
  167. ipx_offset = intrfc->if_ipx_offset;
  168. size = sizeof(struct ipxhdr) + len + ipx_offset;
  169. skb = sock_alloc_send_skb(sk, size, noblock, &rc);
  170. if (!skb)
  171. goto out_put;
  172. skb_reserve(skb, ipx_offset);
  173. skb->sk = sk;
  174. /* Fill in IPX header */
  175. skb_reset_network_header(skb);
  176. skb_reset_transport_header(skb);
  177. skb_put(skb, sizeof(struct ipxhdr));
  178. ipx = ipx_hdr(skb);
  179. ipx->ipx_pktsize = htons(len + sizeof(struct ipxhdr));
  180. IPX_SKB_CB(skb)->ipx_tctrl = 0;
  181. ipx->ipx_type = usipx->sipx_type;
  182. IPX_SKB_CB(skb)->last_hop.index = -1;
  183. #ifdef CONFIG_IPX_INTERN
  184. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  185. memcpy(ipx->ipx_source.node, ipxs->node, IPX_NODE_LEN);
  186. #else
  187. rc = ntohs(ipxs->port);
  188. if (rc == 0x453 || rc == 0x452) {
  189. /* RIP/SAP special handling for mars_nwe */
  190. IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
  191. memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
  192. } else {
  193. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  194. memcpy(ipx->ipx_source.node, ipxs->intrfc->if_node,
  195. IPX_NODE_LEN);
  196. }
  197. #endif /* CONFIG_IPX_INTERN */
  198. ipx->ipx_source.sock = ipxs->port;
  199. IPX_SKB_CB(skb)->ipx_dest_net = usipx->sipx_network;
  200. memcpy(ipx->ipx_dest.node, usipx->sipx_node, IPX_NODE_LEN);
  201. ipx->ipx_dest.sock = usipx->sipx_port;
  202. rc = memcpy_from_msg(skb_put(skb, len), msg, len);
  203. if (rc) {
  204. kfree_skb(skb);
  205. goto out_put;
  206. }
  207. /* Apply checksum. Not allowed on 802.3 links. */
  208. if (sk->sk_no_check_tx ||
  209. intrfc->if_dlink_type == htons(IPX_FRAME_8023))
  210. ipx->ipx_checksum = htons(0xFFFF);
  211. else
  212. ipx->ipx_checksum = ipx_cksum(ipx, len + sizeof(struct ipxhdr));
  213. rc = ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
  214. rt->ir_router_node : ipx->ipx_dest.node);
  215. out_put:
  216. ipxitf_put(intrfc);
  217. if (rt)
  218. ipxrtr_put(rt);
  219. out:
  220. return rc;
  221. }
  222. /*
  223. * We use a normal struct rtentry for route handling
  224. */
  225. int ipxrtr_ioctl(unsigned int cmd, void __user *arg)
  226. {
  227. struct rtentry rt; /* Use these to behave like 'other' stacks */
  228. struct sockaddr_ipx *sg, *st;
  229. int rc = -EFAULT;
  230. if (copy_from_user(&rt, arg, sizeof(rt)))
  231. goto out;
  232. sg = (struct sockaddr_ipx *)&rt.rt_gateway;
  233. st = (struct sockaddr_ipx *)&rt.rt_dst;
  234. rc = -EINVAL;
  235. if (!(rt.rt_flags & RTF_GATEWAY) || /* Direct routes are fixed */
  236. sg->sipx_family != AF_IPX ||
  237. st->sipx_family != AF_IPX)
  238. goto out;
  239. switch (cmd) {
  240. case SIOCDELRT:
  241. rc = ipxrtr_delete(st->sipx_network);
  242. break;
  243. case SIOCADDRT: {
  244. struct ipx_route_definition f;
  245. f.ipx_network = st->sipx_network;
  246. f.ipx_router_network = sg->sipx_network;
  247. memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
  248. rc = ipxrtr_create(&f);
  249. break;
  250. }
  251. }
  252. out:
  253. return rc;
  254. }