ipx_route.c 6.8 KB

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