PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipx/ipx_route.c

http://github.com/CyanogenMod/cm-kernel
C | 294 lines | 228 code | 38 blank | 28 comment | 38 complexity | b62ffb565c782a847004911e52a45f41 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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_reset_network_header(skb);
  179. skb_reset_transport_header(skb);
  180. skb_put(skb, sizeof(struct ipxhdr));
  181. ipx = ipx_hdr(skb);
  182. ipx->ipx_pktsize = htons(len + sizeof(struct ipxhdr));
  183. IPX_SKB_CB(skb)->ipx_tctrl = 0;
  184. ipx->ipx_type = usipx->sipx_type;
  185. IPX_SKB_CB(skb)->last_hop.index = -1;
  186. #ifdef CONFIG_IPX_INTERN
  187. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  188. memcpy(ipx->ipx_source.node, ipxs->node, IPX_NODE_LEN);
  189. #else
  190. rc = ntohs(ipxs->port);
  191. if (rc == 0x453 || rc == 0x452) {
  192. /* RIP/SAP special handling for mars_nwe */
  193. IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
  194. memcpy(ipx->ipx_source.node, intrfc->if_node, IPX_NODE_LEN);
  195. } else {
  196. IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
  197. memcpy(ipx->ipx_source.node, ipxs->intrfc->if_node,
  198. IPX_NODE_LEN);
  199. }
  200. #endif /* CONFIG_IPX_INTERN */
  201. ipx->ipx_source.sock = ipxs->port;
  202. IPX_SKB_CB(skb)->ipx_dest_net = usipx->sipx_network;
  203. memcpy(ipx->ipx_dest.node, usipx->sipx_node, IPX_NODE_LEN);
  204. ipx->ipx_dest.sock = usipx->sipx_port;
  205. rc = memcpy_fromiovec(skb_put(skb, len), iov, len);
  206. if (rc) {
  207. kfree_skb(skb);
  208. goto out_put;
  209. }
  210. /* Apply checksum. Not allowed on 802.3 links. */
  211. if (sk->sk_no_check || intrfc->if_dlink_type == htons(IPX_FRAME_8023))
  212. ipx->ipx_checksum = htons(0xFFFF);
  213. else
  214. ipx->ipx_checksum = ipx_cksum(ipx, len + sizeof(struct ipxhdr));
  215. rc = ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
  216. rt->ir_router_node : ipx->ipx_dest.node);
  217. out_put:
  218. ipxitf_put(intrfc);
  219. if (rt)
  220. ipxrtr_put(rt);
  221. out:
  222. return rc;
  223. }
  224. /*
  225. * We use a normal struct rtentry for route handling
  226. */
  227. int ipxrtr_ioctl(unsigned int cmd, void __user *arg)
  228. {
  229. struct rtentry rt; /* Use these to behave like 'other' stacks */
  230. struct sockaddr_ipx *sg, *st;
  231. int rc = -EFAULT;
  232. if (copy_from_user(&rt, arg, sizeof(rt)))
  233. goto out;
  234. sg = (struct sockaddr_ipx *)&rt.rt_gateway;
  235. st = (struct sockaddr_ipx *)&rt.rt_dst;
  236. rc = -EINVAL;
  237. if (!(rt.rt_flags & RTF_GATEWAY) || /* Direct routes are fixed */
  238. sg->sipx_family != AF_IPX ||
  239. st->sipx_family != AF_IPX)
  240. goto out;
  241. switch (cmd) {
  242. case SIOCDELRT:
  243. rc = ipxrtr_delete(st->sipx_network);
  244. break;
  245. case SIOCADDRT: {
  246. struct ipx_route_definition f;
  247. f.ipx_network = st->sipx_network;
  248. f.ipx_router_network = sg->sipx_network;
  249. memcpy(f.ipx_router_node, sg->sipx_node, IPX_NODE_LEN);
  250. rc = ipxrtr_create(&f);
  251. break;
  252. }
  253. }
  254. out:
  255. return rc;
  256. }