xfrm6_policy.c 8.6 KB

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