xfrm4_policy.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * xfrm4_policy.c
  3. *
  4. * Changes:
  5. * Kazunori MIYAZAWA @USAGI
  6. * YOSHIFUJI Hideaki @USAGI
  7. * Split up af-specific portion
  8. *
  9. */
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/inetdevice.h>
  13. #include <net/dst.h>
  14. #include <net/xfrm.h>
  15. #include <net/ip.h>
  16. static struct dst_ops xfrm4_dst_ops;
  17. static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
  18. static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
  19. xfrm_address_t *saddr,
  20. xfrm_address_t *daddr)
  21. {
  22. struct flowi fl = {
  23. .nl_u = {
  24. .ip4_u = {
  25. .tos = tos,
  26. .daddr = daddr->a4,
  27. },
  28. },
  29. };
  30. struct dst_entry *dst;
  31. struct rtable *rt;
  32. int err;
  33. if (saddr)
  34. fl.fl4_src = saddr->a4;
  35. err = __ip_route_output_key(net, &rt, &fl);
  36. dst = &rt->u.dst;
  37. if (err)
  38. dst = ERR_PTR(err);
  39. return dst;
  40. }
  41. static int xfrm4_get_saddr(struct net *net,
  42. xfrm_address_t *saddr, xfrm_address_t *daddr)
  43. {
  44. struct dst_entry *dst;
  45. struct rtable *rt;
  46. dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
  47. if (IS_ERR(dst))
  48. return -EHOSTUNREACH;
  49. rt = (struct rtable *)dst;
  50. saddr->a4 = rt->rt_src;
  51. dst_release(dst);
  52. return 0;
  53. }
  54. static struct dst_entry *
  55. __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  56. {
  57. struct dst_entry *dst;
  58. read_lock_bh(&policy->lock);
  59. for (dst = policy->bundles; dst; dst = dst->next) {
  60. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  61. if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
  62. xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
  63. xdst->u.rt.fl.fl4_src == fl->fl4_src &&
  64. xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
  65. xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
  66. dst_clone(dst);
  67. break;
  68. }
  69. }
  70. read_unlock_bh(&policy->lock);
  71. return dst;
  72. }
  73. static int xfrm4_get_tos(struct flowi *fl)
  74. {
  75. return fl->fl4_tos;
  76. }
  77. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  78. int nfheader_len)
  79. {
  80. return 0;
  81. }
  82. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  83. {
  84. struct rtable *rt = (struct rtable *)xdst->route;
  85. xdst->u.rt.fl = rt->fl;
  86. xdst->u.dst.dev = dev;
  87. dev_hold(dev);
  88. xdst->u.rt.idev = in_dev_get(dev);
  89. if (!xdst->u.rt.idev)
  90. return -ENODEV;
  91. xdst->u.rt.peer = rt->peer;
  92. if (rt->peer)
  93. atomic_inc(&rt->peer->refcnt);
  94. /* Sheit... I remember I did this right. Apparently,
  95. * it was magically lost, so this code needs audit */
  96. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  97. RTCF_LOCAL);
  98. xdst->u.rt.rt_type = rt->rt_type;
  99. xdst->u.rt.rt_src = rt->rt_src;
  100. xdst->u.rt.rt_dst = rt->rt_dst;
  101. xdst->u.rt.rt_gateway = rt->rt_gateway;
  102. xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
  103. return 0;
  104. }
  105. static void
  106. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  107. {
  108. struct iphdr *iph = ip_hdr(skb);
  109. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  110. memset(fl, 0, sizeof(struct flowi));
  111. if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
  112. switch (iph->protocol) {
  113. case IPPROTO_UDP:
  114. case IPPROTO_UDPLITE:
  115. case IPPROTO_TCP:
  116. case IPPROTO_SCTP:
  117. case IPPROTO_DCCP:
  118. if (xprth + 4 < skb->data ||
  119. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  120. __be16 *ports = (__be16 *)xprth;
  121. fl->fl_ip_sport = ports[!!reverse];
  122. fl->fl_ip_dport = ports[!reverse];
  123. }
  124. break;
  125. case IPPROTO_ICMP:
  126. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  127. u8 *icmp = xprth;
  128. fl->fl_icmp_type = icmp[0];
  129. fl->fl_icmp_code = icmp[1];
  130. }
  131. break;
  132. case IPPROTO_ESP:
  133. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  134. __be32 *ehdr = (__be32 *)xprth;
  135. fl->fl_ipsec_spi = ehdr[0];
  136. }
  137. break;
  138. case IPPROTO_AH:
  139. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  140. __be32 *ah_hdr = (__be32*)xprth;
  141. fl->fl_ipsec_spi = ah_hdr[1];
  142. }
  143. break;
  144. case IPPROTO_COMP:
  145. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  146. __be16 *ipcomp_hdr = (__be16 *)xprth;
  147. fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  148. }
  149. break;
  150. default:
  151. fl->fl_ipsec_spi = 0;
  152. break;
  153. }
  154. }
  155. fl->proto = iph->protocol;
  156. fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
  157. fl->fl4_src = reverse ? iph->daddr : iph->saddr;
  158. fl->fl4_tos = iph->tos;
  159. }
  160. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  161. {
  162. xfrm4_policy_afinfo.garbage_collect(&init_net);
  163. return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
  164. }
  165. static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
  166. {
  167. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  168. struct dst_entry *path = xdst->route;
  169. path->ops->update_pmtu(path, mtu);
  170. }
  171. static void xfrm4_dst_destroy(struct dst_entry *dst)
  172. {
  173. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  174. if (likely(xdst->u.rt.idev))
  175. in_dev_put(xdst->u.rt.idev);
  176. if (likely(xdst->u.rt.peer))
  177. inet_putpeer(xdst->u.rt.peer);
  178. xfrm_dst_destroy(xdst);
  179. }
  180. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  181. int unregister)
  182. {
  183. struct xfrm_dst *xdst;
  184. if (!unregister)
  185. return;
  186. xdst = (struct xfrm_dst *)dst;
  187. if (xdst->u.rt.idev->dev == dev) {
  188. struct in_device *loopback_idev =
  189. in_dev_get(dev_net(dev)->loopback_dev);
  190. BUG_ON(!loopback_idev);
  191. do {
  192. in_dev_put(xdst->u.rt.idev);
  193. xdst->u.rt.idev = loopback_idev;
  194. in_dev_hold(loopback_idev);
  195. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  196. } while (xdst->u.dst.xfrm);
  197. __in_dev_put(loopback_idev);
  198. }
  199. xfrm_dst_ifdown(dst, dev);
  200. }
  201. static struct dst_ops xfrm4_dst_ops = {
  202. .family = AF_INET,
  203. .protocol = cpu_to_be16(ETH_P_IP),
  204. .gc = xfrm4_garbage_collect,
  205. .update_pmtu = xfrm4_update_pmtu,
  206. .destroy = xfrm4_dst_destroy,
  207. .ifdown = xfrm4_dst_ifdown,
  208. .local_out = __ip_local_out,
  209. .gc_thresh = 1024,
  210. .entries = ATOMIC_INIT(0),
  211. };
  212. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  213. .family = AF_INET,
  214. .dst_ops = &xfrm4_dst_ops,
  215. .dst_lookup = xfrm4_dst_lookup,
  216. .get_saddr = xfrm4_get_saddr,
  217. .find_bundle = __xfrm4_find_bundle,
  218. .decode_session = _decode_session4,
  219. .get_tos = xfrm4_get_tos,
  220. .init_path = xfrm4_init_path,
  221. .fill_dst = xfrm4_fill_dst,
  222. };
  223. static void __init xfrm4_policy_init(void)
  224. {
  225. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  226. }
  227. static void __exit xfrm4_policy_fini(void)
  228. {
  229. xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
  230. }
  231. void __init xfrm4_init(void)
  232. {
  233. xfrm4_state_init();
  234. xfrm4_policy_init();
  235. }