xfrm6_policy.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * xfrm6_policy.c: based on xfrm4_policy.c
  3. *
  4. * Authors:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * YOSHIFUJI Hideaki
  10. * Split up af-specific portion
  11. *
  12. */
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <net/addrconf.h>
  17. #include <net/dst.h>
  18. #include <net/xfrm.h>
  19. #include <net/ip.h>
  20. #include <net/ipv6.h>
  21. #include <net/ip6_route.h>
  22. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  23. #include <net/mip6.h>
  24. #endif
  25. static struct dst_ops xfrm6_dst_ops;
  26. static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
  27. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos,
  28. xfrm_address_t *saddr,
  29. xfrm_address_t *daddr)
  30. {
  31. struct flowi fl = {};
  32. struct dst_entry *dst;
  33. int err;
  34. memcpy(&fl.fl6_dst, daddr, sizeof(fl.fl6_dst));
  35. if (saddr)
  36. memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
  37. dst = ip6_route_output(net, NULL, &fl);
  38. err = dst->error;
  39. if (dst->error) {
  40. dst_release(dst);
  41. dst = ERR_PTR(err);
  42. }
  43. return dst;
  44. }
  45. static int xfrm6_get_saddr(struct net *net,
  46. xfrm_address_t *saddr, xfrm_address_t *daddr)
  47. {
  48. struct dst_entry *dst;
  49. struct net_device *dev;
  50. dst = xfrm6_dst_lookup(net, 0, NULL, daddr);
  51. if (IS_ERR(dst))
  52. return -EHOSTUNREACH;
  53. dev = ip6_dst_idev(dst)->dev;
  54. ipv6_dev_get_saddr(dev_net(dev), dev,
  55. (struct in6_addr *)&daddr->a6, 0,
  56. (struct in6_addr *)&saddr->a6);
  57. dst_release(dst);
  58. return 0;
  59. }
  60. static struct dst_entry *
  61. __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  62. {
  63. struct dst_entry *dst;
  64. /* Still not clear if we should set fl->fl6_{src,dst}... */
  65. read_lock_bh(&policy->lock);
  66. for (dst = policy->bundles; dst; dst = dst->next) {
  67. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  68. struct in6_addr fl_dst_prefix, fl_src_prefix;
  69. ipv6_addr_prefix(&fl_dst_prefix,
  70. &fl->fl6_dst,
  71. xdst->u.rt6.rt6i_dst.plen);
  72. ipv6_addr_prefix(&fl_src_prefix,
  73. &fl->fl6_src,
  74. xdst->u.rt6.rt6i_src.plen);
  75. if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
  76. ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
  77. xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
  78. (xdst->u.rt6.rt6i_dst.plen != 128 ||
  79. xdst->u.rt6.rt6i_src.plen != 128))) {
  80. dst_clone(dst);
  81. break;
  82. }
  83. }
  84. read_unlock_bh(&policy->lock);
  85. return dst;
  86. }
  87. static int xfrm6_get_tos(struct flowi *fl)
  88. {
  89. return 0;
  90. }
  91. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  92. int nfheader_len)
  93. {
  94. if (dst->ops->family == AF_INET6) {
  95. struct rt6_info *rt = (struct rt6_info*)dst;
  96. if (rt->rt6i_node)
  97. path->path_cookie = rt->rt6i_node->fn_sernum;
  98. }
  99. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  100. return 0;
  101. }
  102. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  103. {
  104. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  105. xdst->u.dst.dev = dev;
  106. dev_hold(dev);
  107. xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
  108. if (!xdst->u.rt6.rt6i_idev)
  109. return -ENODEV;
  110. /* Sheit... I remember I did this right. Apparently,
  111. * it was magically lost, so this code needs audit */
  112. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  113. RTF_LOCAL);
  114. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  115. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  116. if (rt->rt6i_node)
  117. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  118. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  119. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  120. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  121. return 0;
  122. }
  123. static inline void
  124. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  125. {
  126. int onlyproto = 0;
  127. u16 offset = skb_network_header_len(skb);
  128. struct ipv6hdr *hdr = ipv6_hdr(skb);
  129. struct ipv6_opt_hdr *exthdr;
  130. const unsigned char *nh = skb_network_header(skb);
  131. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  132. memset(fl, 0, sizeof(struct flowi));
  133. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  134. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  135. while (nh + offset + 1 < skb->data ||
  136. pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  137. nh = skb_network_header(skb);
  138. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  139. switch (nexthdr) {
  140. case NEXTHDR_FRAGMENT:
  141. onlyproto = 1;
  142. case NEXTHDR_ROUTING:
  143. case NEXTHDR_HOP:
  144. case NEXTHDR_DEST:
  145. offset += ipv6_optlen(exthdr);
  146. nexthdr = exthdr->nexthdr;
  147. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  148. break;
  149. case IPPROTO_UDP:
  150. case IPPROTO_UDPLITE:
  151. case IPPROTO_TCP:
  152. case IPPROTO_SCTP:
  153. case IPPROTO_DCCP:
  154. if (!onlyproto && (nh + offset + 4 < skb->data ||
  155. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  156. __be16 *ports = (__be16 *)exthdr;
  157. fl->fl_ip_sport = ports[!!reverse];
  158. fl->fl_ip_dport = ports[!reverse];
  159. }
  160. fl->proto = nexthdr;
  161. return;
  162. case IPPROTO_ICMPV6:
  163. if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  164. u8 *icmp = (u8 *)exthdr;
  165. fl->fl_icmp_type = icmp[0];
  166. fl->fl_icmp_code = icmp[1];
  167. }
  168. fl->proto = nexthdr;
  169. return;
  170. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  171. case IPPROTO_MH:
  172. if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  173. struct ip6_mh *mh;
  174. mh = (struct ip6_mh *)exthdr;
  175. fl->fl_mh_type = mh->ip6mh_type;
  176. }
  177. fl->proto = nexthdr;
  178. return;
  179. #endif
  180. /* XXX Why are there these headers? */
  181. case IPPROTO_AH:
  182. case IPPROTO_ESP:
  183. case IPPROTO_COMP:
  184. default:
  185. fl->fl_ipsec_spi = 0;
  186. fl->proto = nexthdr;
  187. return;
  188. }
  189. }
  190. }
  191. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  192. {
  193. xfrm6_policy_afinfo.garbage_collect(&init_net);
  194. return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
  195. }
  196. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  197. {
  198. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  199. struct dst_entry *path = xdst->route;
  200. path->ops->update_pmtu(path, mtu);
  201. }
  202. static void xfrm6_dst_destroy(struct dst_entry *dst)
  203. {
  204. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  205. if (likely(xdst->u.rt6.rt6i_idev))
  206. in6_dev_put(xdst->u.rt6.rt6i_idev);
  207. xfrm_dst_destroy(xdst);
  208. }
  209. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  210. int unregister)
  211. {
  212. struct xfrm_dst *xdst;
  213. if (!unregister)
  214. return;
  215. xdst = (struct xfrm_dst *)dst;
  216. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  217. struct inet6_dev *loopback_idev =
  218. in6_dev_get(dev_net(dev)->loopback_dev);
  219. BUG_ON(!loopback_idev);
  220. do {
  221. in6_dev_put(xdst->u.rt6.rt6i_idev);
  222. xdst->u.rt6.rt6i_idev = loopback_idev;
  223. in6_dev_hold(loopback_idev);
  224. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  225. } while (xdst->u.dst.xfrm);
  226. __in6_dev_put(loopback_idev);
  227. }
  228. xfrm_dst_ifdown(dst, dev);
  229. }
  230. static struct dst_ops xfrm6_dst_ops = {
  231. .family = AF_INET6,
  232. .protocol = cpu_to_be16(ETH_P_IPV6),
  233. .gc = xfrm6_garbage_collect,
  234. .update_pmtu = xfrm6_update_pmtu,
  235. .destroy = xfrm6_dst_destroy,
  236. .ifdown = xfrm6_dst_ifdown,
  237. .local_out = __ip6_local_out,
  238. .gc_thresh = 1024,
  239. .entries = ATOMIC_INIT(0),
  240. };
  241. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  242. .family = AF_INET6,
  243. .dst_ops = &xfrm6_dst_ops,
  244. .dst_lookup = xfrm6_dst_lookup,
  245. .get_saddr = xfrm6_get_saddr,
  246. .find_bundle = __xfrm6_find_bundle,
  247. .decode_session = _decode_session6,
  248. .get_tos = xfrm6_get_tos,
  249. .init_path = xfrm6_init_path,
  250. .fill_dst = xfrm6_fill_dst,
  251. };
  252. static int __init xfrm6_policy_init(void)
  253. {
  254. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  255. }
  256. static void xfrm6_policy_fini(void)
  257. {
  258. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  259. }
  260. int __init xfrm6_init(void)
  261. {
  262. int ret;
  263. ret = xfrm6_policy_init();
  264. if (ret)
  265. goto out;
  266. ret = xfrm6_state_init();
  267. if (ret)
  268. goto out_policy;
  269. out:
  270. return ret;
  271. out_policy:
  272. xfrm6_policy_fini();
  273. goto out;
  274. }
  275. void xfrm6_fini(void)
  276. {
  277. //xfrm6_input_fini();
  278. xfrm6_policy_fini();
  279. xfrm6_state_fini();
  280. }