xfrm6_policy.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 int xfrm6_get_tos(struct flowi *fl)
  60. {
  61. return 0;
  62. }
  63. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  64. int nfheader_len)
  65. {
  66. if (dst->ops->family == AF_INET6) {
  67. struct rt6_info *rt = (struct rt6_info*)dst;
  68. if (rt->rt6i_node)
  69. path->path_cookie = rt->rt6i_node->fn_sernum;
  70. }
  71. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  72. return 0;
  73. }
  74. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  75. struct flowi *fl)
  76. {
  77. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  78. xdst->u.dst.dev = dev;
  79. dev_hold(dev);
  80. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  81. if (!xdst->u.rt6.rt6i_idev)
  82. return -ENODEV;
  83. /* Sheit... I remember I did this right. Apparently,
  84. * it was magically lost, so this code needs audit */
  85. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  86. RTF_LOCAL);
  87. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  88. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  89. if (rt->rt6i_node)
  90. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  91. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  92. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  93. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  94. return 0;
  95. }
  96. static inline void
  97. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  98. {
  99. int onlyproto = 0;
  100. u16 offset = skb_network_header_len(skb);
  101. struct ipv6hdr *hdr = ipv6_hdr(skb);
  102. struct ipv6_opt_hdr *exthdr;
  103. const unsigned char *nh = skb_network_header(skb);
  104. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  105. memset(fl, 0, sizeof(struct flowi));
  106. fl->mark = skb->mark;
  107. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  108. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  109. while (nh + offset + 1 < skb->data ||
  110. pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  111. nh = skb_network_header(skb);
  112. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  113. switch (nexthdr) {
  114. case NEXTHDR_FRAGMENT:
  115. onlyproto = 1;
  116. case NEXTHDR_ROUTING:
  117. case NEXTHDR_HOP:
  118. case NEXTHDR_DEST:
  119. offset += ipv6_optlen(exthdr);
  120. nexthdr = exthdr->nexthdr;
  121. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  122. break;
  123. case IPPROTO_UDP:
  124. case IPPROTO_UDPLITE:
  125. case IPPROTO_TCP:
  126. case IPPROTO_SCTP:
  127. case IPPROTO_DCCP:
  128. if (!onlyproto && (nh + offset + 4 < skb->data ||
  129. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  130. __be16 *ports = (__be16 *)exthdr;
  131. fl->fl_ip_sport = ports[!!reverse];
  132. fl->fl_ip_dport = ports[!reverse];
  133. }
  134. fl->proto = nexthdr;
  135. return;
  136. case IPPROTO_ICMPV6:
  137. if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  138. u8 *icmp = (u8 *)exthdr;
  139. fl->fl_icmp_type = icmp[0];
  140. fl->fl_icmp_code = icmp[1];
  141. }
  142. fl->proto = nexthdr;
  143. return;
  144. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  145. case IPPROTO_MH:
  146. if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  147. struct ip6_mh *mh;
  148. mh = (struct ip6_mh *)exthdr;
  149. fl->fl_mh_type = mh->ip6mh_type;
  150. }
  151. fl->proto = nexthdr;
  152. return;
  153. #endif
  154. /* XXX Why are there these headers? */
  155. case IPPROTO_AH:
  156. case IPPROTO_ESP:
  157. case IPPROTO_COMP:
  158. default:
  159. fl->fl_ipsec_spi = 0;
  160. fl->proto = nexthdr;
  161. return;
  162. }
  163. }
  164. }
  165. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  166. {
  167. struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
  168. xfrm6_policy_afinfo.garbage_collect(net);
  169. return (atomic_read(&ops->entries) > ops->gc_thresh * 2);
  170. }
  171. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  172. {
  173. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  174. struct dst_entry *path = xdst->route;
  175. path->ops->update_pmtu(path, mtu);
  176. }
  177. static void xfrm6_dst_destroy(struct dst_entry *dst)
  178. {
  179. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  180. if (likely(xdst->u.rt6.rt6i_idev))
  181. in6_dev_put(xdst->u.rt6.rt6i_idev);
  182. xfrm_dst_destroy(xdst);
  183. }
  184. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  185. int unregister)
  186. {
  187. struct xfrm_dst *xdst;
  188. if (!unregister)
  189. return;
  190. xdst = (struct xfrm_dst *)dst;
  191. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  192. struct inet6_dev *loopback_idev =
  193. in6_dev_get(dev_net(dev)->loopback_dev);
  194. BUG_ON(!loopback_idev);
  195. do {
  196. in6_dev_put(xdst->u.rt6.rt6i_idev);
  197. xdst->u.rt6.rt6i_idev = loopback_idev;
  198. in6_dev_hold(loopback_idev);
  199. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  200. } while (xdst->u.dst.xfrm);
  201. __in6_dev_put(loopback_idev);
  202. }
  203. xfrm_dst_ifdown(dst, dev);
  204. }
  205. static struct dst_ops xfrm6_dst_ops = {
  206. .family = AF_INET6,
  207. .protocol = cpu_to_be16(ETH_P_IPV6),
  208. .gc = xfrm6_garbage_collect,
  209. .update_pmtu = xfrm6_update_pmtu,
  210. .destroy = xfrm6_dst_destroy,
  211. .ifdown = xfrm6_dst_ifdown,
  212. .local_out = __ip6_local_out,
  213. .gc_thresh = 1024,
  214. .entries = ATOMIC_INIT(0),
  215. };
  216. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  217. .family = AF_INET6,
  218. .dst_ops = &xfrm6_dst_ops,
  219. .dst_lookup = xfrm6_dst_lookup,
  220. .get_saddr = xfrm6_get_saddr,
  221. .decode_session = _decode_session6,
  222. .get_tos = xfrm6_get_tos,
  223. .init_path = xfrm6_init_path,
  224. .fill_dst = xfrm6_fill_dst,
  225. };
  226. static int __init xfrm6_policy_init(void)
  227. {
  228. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  229. }
  230. static void xfrm6_policy_fini(void)
  231. {
  232. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  233. }
  234. #ifdef CONFIG_SYSCTL
  235. static struct ctl_table xfrm6_policy_table[] = {
  236. {
  237. .procname = "xfrm6_gc_thresh",
  238. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  239. .maxlen = sizeof(int),
  240. .mode = 0644,
  241. .proc_handler = proc_dointvec,
  242. },
  243. { }
  244. };
  245. static struct ctl_table_header *sysctl_hdr;
  246. #endif
  247. int __init xfrm6_init(void)
  248. {
  249. int ret;
  250. unsigned int gc_thresh;
  251. /*
  252. * We need a good default value for the xfrm6 gc threshold.
  253. * In ipv4 we set it to the route hash table size * 8, which
  254. * is half the size of the maximaum route cache for ipv4. It
  255. * would be good to do the same thing for v6, except the table is
  256. * constructed differently here. Here each table for a net namespace
  257. * can have FIB_TABLE_HASHSZ entries, so lets go with the same
  258. * computation that we used for ipv4 here. Also, lets keep the initial
  259. * gc_thresh to a minimum of 1024, since, the ipv6 route cache defaults
  260. * to that as a minimum as well
  261. */
  262. gc_thresh = FIB6_TABLE_HASHSZ * 8;
  263. xfrm6_dst_ops.gc_thresh = (gc_thresh < 1024) ? 1024 : gc_thresh;
  264. ret = xfrm6_policy_init();
  265. if (ret)
  266. goto out;
  267. ret = xfrm6_state_init();
  268. if (ret)
  269. goto out_policy;
  270. #ifdef CONFIG_SYSCTL
  271. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path,
  272. xfrm6_policy_table);
  273. #endif
  274. out:
  275. return ret;
  276. out_policy:
  277. xfrm6_policy_fini();
  278. goto out;
  279. }
  280. void xfrm6_fini(void)
  281. {
  282. #ifdef CONFIG_SYSCTL
  283. if (sysctl_hdr)
  284. unregister_net_sysctl_table(sysctl_hdr);
  285. #endif
  286. //xfrm6_input_fini();
  287. xfrm6_policy_fini();
  288. xfrm6_state_fini();
  289. }