xfrm6_policy.c 8.6 KB

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