xfrm6_policy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 xfrm_policy_afinfo xfrm6_policy_afinfo;
  26. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos,
  27. xfrm_address_t *saddr,
  28. xfrm_address_t *daddr)
  29. {
  30. struct flowi fl = {};
  31. struct dst_entry *dst;
  32. int err;
  33. memcpy(&fl.fl6_dst, daddr, sizeof(fl.fl6_dst));
  34. if (saddr)
  35. memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
  36. dst = ip6_route_output(net, NULL, &fl);
  37. err = dst->error;
  38. if (dst->error) {
  39. dst_release(dst);
  40. dst = ERR_PTR(err);
  41. }
  42. return dst;
  43. }
  44. static int xfrm6_get_saddr(struct net *net,
  45. xfrm_address_t *saddr, xfrm_address_t *daddr)
  46. {
  47. struct dst_entry *dst;
  48. struct net_device *dev;
  49. dst = xfrm6_dst_lookup(net, 0, NULL, daddr);
  50. if (IS_ERR(dst))
  51. return -EHOSTUNREACH;
  52. dev = ip6_dst_idev(dst)->dev;
  53. ipv6_dev_get_saddr(dev_net(dev), dev,
  54. (struct in6_addr *)&daddr->a6, 0,
  55. (struct in6_addr *)&saddr->a6);
  56. dst_release(dst);
  57. return 0;
  58. }
  59. static struct dst_entry *
  60. __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  61. {
  62. struct dst_entry *dst;
  63. /* Still not clear if we should set fl->fl6_{src,dst}... */
  64. read_lock_bh(&policy->lock);
  65. for (dst = policy->bundles; dst; dst = dst->next) {
  66. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  67. struct in6_addr fl_dst_prefix, fl_src_prefix;
  68. ipv6_addr_prefix(&fl_dst_prefix,
  69. &fl->fl6_dst,
  70. xdst->u.rt6.rt6i_dst.plen);
  71. ipv6_addr_prefix(&fl_src_prefix,
  72. &fl->fl6_src,
  73. xdst->u.rt6.rt6i_src.plen);
  74. if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
  75. ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
  76. xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
  77. (xdst->u.rt6.rt6i_dst.plen != 128 ||
  78. xdst->u.rt6.rt6i_src.plen != 128))) {
  79. dst_clone(dst);
  80. break;
  81. }
  82. }
  83. read_unlock_bh(&policy->lock);
  84. return dst;
  85. }
  86. static int xfrm6_get_tos(struct flowi *fl)
  87. {
  88. return 0;
  89. }
  90. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  91. int nfheader_len)
  92. {
  93. if (dst->ops->family == AF_INET6) {
  94. struct rt6_info *rt = (struct rt6_info*)dst;
  95. if (rt->rt6i_node)
  96. path->path_cookie = rt->rt6i_node->fn_sernum;
  97. }
  98. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  99. return 0;
  100. }
  101. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  102. {
  103. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  104. xdst->u.dst.dev = dev;
  105. dev_hold(dev);
  106. xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
  107. if (!xdst->u.rt6.rt6i_idev)
  108. return -ENODEV;
  109. /* Sheit... I remember I did this right. Apparently,
  110. * it was magically lost, so this code needs audit */
  111. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  112. RTF_LOCAL);
  113. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  114. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  115. if (rt->rt6i_node)
  116. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  117. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  118. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  119. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  120. return 0;
  121. }
  122. static inline void
  123. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  124. {
  125. int onlyproto = 0;
  126. u16 offset = skb_network_header_len(skb);
  127. struct ipv6hdr *hdr = ipv6_hdr(skb);
  128. struct ipv6_opt_hdr *exthdr;
  129. const unsigned char *nh = skb_network_header(skb);
  130. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  131. memset(fl, 0, sizeof(struct flowi));
  132. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  133. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  134. while (nh + offset + 1 < skb->data ||
  135. pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  136. nh = skb_network_header(skb);
  137. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  138. switch (nexthdr) {
  139. case NEXTHDR_FRAGMENT:
  140. onlyproto = 1;
  141. case NEXTHDR_ROUTING:
  142. case NEXTHDR_HOP:
  143. case NEXTHDR_DEST:
  144. offset += ipv6_optlen(exthdr);
  145. nexthdr = exthdr->nexthdr;
  146. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  147. break;
  148. case IPPROTO_UDP:
  149. case IPPROTO_UDPLITE:
  150. case IPPROTO_TCP:
  151. case IPPROTO_SCTP:
  152. case IPPROTO_DCCP:
  153. if (!onlyproto && (nh + offset + 4 < skb->data ||
  154. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  155. __be16 *ports = (__be16 *)exthdr;
  156. fl->fl_ip_sport = ports[!!reverse];
  157. fl->fl_ip_dport = ports[!reverse];
  158. }
  159. fl->proto = nexthdr;
  160. return;
  161. case IPPROTO_ICMPV6:
  162. if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  163. u8 *icmp = (u8 *)exthdr;
  164. fl->fl_icmp_type = icmp[0];
  165. fl->fl_icmp_code = icmp[1];
  166. }
  167. fl->proto = nexthdr;
  168. return;
  169. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  170. case IPPROTO_MH:
  171. if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  172. struct ip6_mh *mh;
  173. mh = (struct ip6_mh *)exthdr;
  174. fl->fl_mh_type = mh->ip6mh_type;
  175. }
  176. fl->proto = nexthdr;
  177. return;
  178. #endif
  179. /* XXX Why are there these headers? */
  180. case IPPROTO_AH:
  181. case IPPROTO_ESP:
  182. case IPPROTO_COMP:
  183. default:
  184. fl->fl_ipsec_spi = 0;
  185. fl->proto = nexthdr;
  186. return;
  187. }
  188. }
  189. }
  190. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  191. {
  192. struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
  193. xfrm6_policy_afinfo.garbage_collect(net);
  194. return (atomic_read(&ops->entries) > 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. #ifdef CONFIG_SYSCTL
  261. static struct ctl_table xfrm6_policy_table[] = {
  262. {
  263. .procname = "xfrm6_gc_thresh",
  264. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  265. .maxlen = sizeof(int),
  266. .mode = 0644,
  267. .proc_handler = proc_dointvec,
  268. },
  269. { }
  270. };
  271. static struct ctl_table_header *sysctl_hdr;
  272. #endif
  273. int __init xfrm6_init(void)
  274. {
  275. int ret;
  276. unsigned int gc_thresh;
  277. /*
  278. * We need a good default value for the xfrm6 gc threshold.
  279. * In ipv4 we set it to the route hash table size * 8, which
  280. * is half the size of the maximaum route cache for ipv4. It
  281. * would be good to do the same thing for v6, except the table is
  282. * constructed differently here. Here each table for a net namespace
  283. * can have FIB_TABLE_HASHSZ entries, so lets go with the same
  284. * computation that we used for ipv4 here. Also, lets keep the initial
  285. * gc_thresh to a minimum of 1024, since, the ipv6 route cache defaults
  286. * to that as a minimum as well
  287. */
  288. gc_thresh = FIB6_TABLE_HASHSZ * 8;
  289. xfrm6_dst_ops.gc_thresh = (gc_thresh < 1024) ? 1024 : gc_thresh;
  290. ret = xfrm6_policy_init();
  291. if (ret)
  292. goto out;
  293. ret = xfrm6_state_init();
  294. if (ret)
  295. goto out_policy;
  296. #ifdef CONFIG_SYSCTL
  297. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path,
  298. xfrm6_policy_table);
  299. #endif
  300. out:
  301. return ret;
  302. out_policy:
  303. xfrm6_policy_fini();
  304. goto out;
  305. }
  306. void xfrm6_fini(void)
  307. {
  308. #ifdef CONFIG_SYSCTL
  309. if (sysctl_hdr)
  310. unregister_net_sysctl_table(sysctl_hdr);
  311. #endif
  312. //xfrm6_input_fini();
  313. xfrm6_policy_fini();
  314. xfrm6_state_fini();
  315. }