ipx_route.c 6.7 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/spinlock.h>
  12. #include <net/ipx.h>
  13. #include <net/sock.h>
  14. LIST_HEAD(ipx_routes);
  15. DEFINE_RWLOCK(ipx_routes_lock);
  16. extern struct ipx_interface *ipx_internal_net;
  17. extern __be16 ipx_cksum(struct ipxhdr *packet, int length);
  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. extern int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb,
  24. char *node);
  25. extern struct ipx_interface *ipxitf_find_using_net(__be32 net);
  26. struct ipx_route *ipxrtr_lookup(__be32 net)
  27. {
  28. struct ipx_route *r;
  29. read_lock_bh(&ipx_routes_lock);
  30. list_for_each_entry(r, &ipx_routes, node)
  31. if (r->ir_net == net) {
  32. ipxrtr_hold(r);
  33. goto unlock;
  34. }
  35. r = NULL;
  36. unlock:
  37. read_unlock_bh(&ipx_routes_lock);
  38. return r;
  39. }
  40. /*
  41. * Caller must hold a reference to intrfc
  42. */
  43. int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
  44. unsigned char *node)
  45. {
  46. struct ipx_route *rt;
  47. int rc;
  48. /* Get a route structure; either existing or create */
  49. rt = ipxrtr_lookup(network);
  50. if (!rt) {
  51. rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
  52. rc = -EAGAIN;
  53. if (!rt)
  54. goto out;
  55. atomic_set(&rt->refcnt, 1);
  56. ipxrtr_hold(rt);
  57. write_lock_bh(&ipx_routes_lock);
  58. list_add(&rt->node, &ipx_routes);
  59. write_unlock_bh(&ipx_routes_lock);
  60. } else {
  61. rc = -EEXIST;
  62. if (intrfc == ipx_internal_net)
  63. goto out_put;
  64. }
  65. rt->ir_net = network;
  66. rt->ir_intrfc = intrfc;
  67. if (!node) {
  68. memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
  69. rt->ir_routed = 0;
  70. } else {
  71. memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
  72. rt->ir_routed = 1;
  73. }
  74. rc = 0;
  75. out_put:
  76. ipxrtr_put(rt);
  77. out:
  78. return rc;
  79. }
  80. void ipxrtr_del_routes(struct ipx_interface *intrfc)
  81. {
  82. struct ipx_route *r, *tmp;
  83. write_lock_bh(&ipx_routes_lock);
  84. list_for_each_entry_safe(r, tmp, &ipx_routes, node)
  85. if (r->ir_intrfc == intrfc) {
  86. list_del(&r->node);
  87. ipxrtr_put(r);
  88. }
  89. write_unlock_bh(&ipx_routes_lock);
  90. }
  91. static int ipxrtr_create(struct ipx_route_definition *rd)
  92. {
  93. struct ipx_interface *intrfc;
  94. int rc = -ENETUNREACH;
  95. /* Find the appropriate interface */
  96. intrfc = ipxitf_find_using_net(rd->ipx_router_network);
  97. if (!intrfc)
  98. goto out;
  99. rc = ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
  100. ipxitf_put(intrfc);
  101. out:
  102. return rc;
  103. }
  104. static int ipxrtr_delete(__be32 net)
  105. {
  106. struct ipx_route *r, *tmp;
  107. int rc;
  108. write_lock_bh(&ipx_routes_lock);
  109. list_for_each_entry_safe(r, tmp, &ipx_routes, node)
  110. if (r->ir_net == net) {
  111. /* Directly connected; can't lose route */
  112. rc = -EPERM;
  113. if (!r->ir_routed)
  114. goto out;
  115. list_del(&r->node);
  116. ipxrtr_put(r);
  117. rc = 0;
  118. goto out;
  119. }
  120. rc = -ENOENT;
  121. out:
  122. write_unlock_bh(&ipx_routes_lock);
  123. return rc;
  124. }
  125. /*
  126. * The skb has to be unshared, we'll end up calling ipxitf_send, that'll
  127. * modify the packet
  128. */
  129. int ipxrtr_route_skb(struct sk_buff *skb)
  130. {
  131. struct ipxhdr *ipx = ipx_hdr(skb);
  132. struct ipx_route *r = ipxrtr_lookup(IPX_SKB_CB(skb)->ipx_dest_net);
  133. if (!r) { /* no known route */
  134. kfree_skb(skb);
  135. return 0;
  136. }
  137. ipxitf_hold(r->ir_intrfc);
  138. ipxitf_send(r->ir_intrfc, skb, r->ir_routed ?
  139. r->ir_router_node : ipx->ipx_dest.node);
  140. ipxitf_put(r->ir_intrfc);
  141. ipxrtr_put(r);
  142. return 0;
  143. }
  144. /*
  145. * Route an outgoing frame from a socket.
  146. */
  147. int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
  148. struct iovec *iov, size_t len, int noblock)
  149. {
  150. struct sk_buff *skb;
  151. struct ipx_sock *ipxs = ipx_sk(sk);
  152. struct ipx_interface *intrfc;
  153. struct ipxhdr *ipx;
  154. size_t size;
  155. int ipx_offset;
  156. struct ipx_route *rt = NULL;
  157. int rc;
  158. /* Find the appropriate interface on which to send packet */
  159. if (!usipx->sipx_network && ipx_primary_net) {
  160. usipx->sipx_network = ipx_primary_net->if_netnum;
  161. intrfc = ipx_primary_net;
  162. } else {
  163. rt = ipxrtr_lookup(usipx->sipx_network);
  164. rc = -ENETUNREACH;
  165. if (!rt)
  166. goto out;
  167. intrfc = rt->ir_intrfc;
  168. }
  169. ipxitf_hold(intrfc);
  170. ipx_offset = intrfc->if_ipx_offset;
  171. size = sizeof(struct ipxhdr) + len + ipx_offset;
  172. skb = sock_alloc_send_skb(sk, size, noblock, &rc);
  173. if (!skb)
  174. goto out_put;
  175. skb_reserve(skb, ipx_offset);
  176. skb->sk = sk;
  177. /* Fill in IPX header */
  178. skb->h.raw = skb->nh.raw = skb_put(skb, sizeof(struct ipxhdr));
  179. ipx = ipx_hdr(skb);
  180. ipx->ipx_pktsize = htons(len + sizeof(struct ipxhdr));
  181. IPX_SKB_CB(skb)->ipx_tctrl = 0;
  182. ipx->ipx_type = usipx->sipx_type;
  183. IPX_SKB_CB(skb)->last_hop.index = -1;
  184. #ifdef CONFIG_IPX_INTERN
  185. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  186. memcpy(ipx->ipx_source.node, ipxs->node, IPX_NODE_LEN);
  187. #else
  188. rc = ntohs(ipxs->port);
  189. if (rc == 0x453 || rc == 0x452) {
  190. /* RIP/SAP special handling for mars_nwe */
  191. IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
  192. memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
  193. } else {
  194. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  195. memcpy(ipx->ipx_source.node, ipxs->intrfc->if_node,
  196. IPX_NODE_LEN);
  197. }
  198. #endif /* CONFIG_IPX_INTERN */
  199. ipx->ipx_source.sock = ipxs->port;
  200. IPX_SKB_CB(skb)->ipx_dest_net = usipx->sipx_network;
  201. memcpy(ipx->ipx_dest.node, usipx->sipx_node, IPX_NODE_LEN);
  202. ipx->ipx_dest.sock = usipx->sipx_port;
  203. rc = memcpy_fromiovec(skb_put(skb, len), iov, len);
  204. if (rc) {
  205. kfree_skb(skb);
  206. goto out_put;
  207. }
  208. /* Apply checksum. Not allowed on 802.3 links. */
  209. if (sk->sk_no_check || 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. }