xfrm6_policy.c 8.0 KB

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