xfrm6_policy.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 IS_ENABLED(CONFIG_IPV6_MIP6)
  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 void xfrm6_init_dst(struct net *net, struct xfrm_dst *xdst)
  65. {
  66. struct rt6_info *rt = (struct rt6_info *)xdst;
  67. rt6_init_peer(rt, net->ipv6.peers);
  68. }
  69. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  70. int nfheader_len)
  71. {
  72. if (dst->ops->family == AF_INET6) {
  73. struct rt6_info *rt = (struct rt6_info*)dst;
  74. if (rt->rt6i_node)
  75. path->path_cookie = rt->rt6i_node->fn_sernum;
  76. }
  77. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  78. return 0;
  79. }
  80. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  81. const struct flowi *fl)
  82. {
  83. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  84. xdst->u.dst.dev = dev;
  85. dev_hold(dev);
  86. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  87. if (!xdst->u.rt6.rt6i_idev) {
  88. dev_put(dev);
  89. return -ENODEV;
  90. }
  91. rt6_transfer_peer(&xdst->u.rt6, rt);
  92. /* Sheit... I remember I did this right. Apparently,
  93. * it was magically lost, so this code needs audit */
  94. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  95. RTF_LOCAL);
  96. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  97. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  98. if (rt->rt6i_node)
  99. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  100. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  101. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  102. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  103. return 0;
  104. }
  105. static inline void
  106. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  107. {
  108. struct flowi6 *fl6 = &fl->u.ip6;
  109. int onlyproto = 0;
  110. u16 offset = skb_network_header_len(skb);
  111. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  112. struct ipv6_opt_hdr *exthdr;
  113. const unsigned char *nh = skb_network_header(skb);
  114. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  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 IS_ENABLED(CONFIG_IPV6_MIP6)
  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, struct sock *sk,
  182. struct sk_buff *skb, u32 mtu)
  183. {
  184. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  185. struct dst_entry *path = xdst->route;
  186. path->ops->update_pmtu(path, sk, skb, mtu);
  187. }
  188. static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
  189. struct sk_buff *skb)
  190. {
  191. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  192. struct dst_entry *path = xdst->route;
  193. path->ops->redirect(path, sk, skb);
  194. }
  195. static void xfrm6_dst_destroy(struct dst_entry *dst)
  196. {
  197. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  198. if (likely(xdst->u.rt6.rt6i_idev))
  199. in6_dev_put(xdst->u.rt6.rt6i_idev);
  200. dst_destroy_metrics_generic(dst);
  201. if (rt6_has_peer(&xdst->u.rt6)) {
  202. struct inet_peer *peer = rt6_peer_ptr(&xdst->u.rt6);
  203. inet_putpeer(peer);
  204. }
  205. xfrm_dst_destroy(xdst);
  206. }
  207. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  208. int unregister)
  209. {
  210. struct xfrm_dst *xdst;
  211. if (!unregister)
  212. return;
  213. xdst = (struct xfrm_dst *)dst;
  214. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  215. struct inet6_dev *loopback_idev =
  216. in6_dev_get(dev_net(dev)->loopback_dev);
  217. BUG_ON(!loopback_idev);
  218. do {
  219. in6_dev_put(xdst->u.rt6.rt6i_idev);
  220. xdst->u.rt6.rt6i_idev = loopback_idev;
  221. in6_dev_hold(loopback_idev);
  222. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  223. } while (xdst->u.dst.xfrm);
  224. __in6_dev_put(loopback_idev);
  225. }
  226. xfrm_dst_ifdown(dst, dev);
  227. }
  228. static struct dst_ops xfrm6_dst_ops = {
  229. .family = AF_INET6,
  230. .protocol = cpu_to_be16(ETH_P_IPV6),
  231. .gc = xfrm6_garbage_collect,
  232. .update_pmtu = xfrm6_update_pmtu,
  233. .redirect = xfrm6_redirect,
  234. .cow_metrics = dst_cow_metrics_generic,
  235. .destroy = xfrm6_dst_destroy,
  236. .ifdown = xfrm6_dst_ifdown,
  237. .local_out = __ip6_local_out,
  238. .gc_thresh = 1024,
  239. };
  240. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  241. .family = AF_INET6,
  242. .dst_ops = &xfrm6_dst_ops,
  243. .dst_lookup = xfrm6_dst_lookup,
  244. .get_saddr = xfrm6_get_saddr,
  245. .decode_session = _decode_session6,
  246. .get_tos = xfrm6_get_tos,
  247. .init_dst = xfrm6_init_dst,
  248. .init_path = xfrm6_init_path,
  249. .fill_dst = xfrm6_fill_dst,
  250. .blackhole_route = ip6_blackhole_route,
  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 int __net_init xfrm6_net_init(struct net *net)
  272. {
  273. struct ctl_table *table;
  274. struct ctl_table_header *hdr;
  275. table = xfrm6_policy_table;
  276. if (!net_eq(net, &init_net)) {
  277. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  278. if (!table)
  279. goto err_alloc;
  280. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  281. }
  282. hdr = register_net_sysctl(net, "net/ipv6", table);
  283. if (!hdr)
  284. goto err_reg;
  285. net->ipv6.sysctl.xfrm6_hdr = hdr;
  286. return 0;
  287. err_reg:
  288. if (!net_eq(net, &init_net))
  289. kfree(table);
  290. err_alloc:
  291. return -ENOMEM;
  292. }
  293. static void __net_exit xfrm6_net_exit(struct net *net)
  294. {
  295. struct ctl_table *table;
  296. if (net->ipv6.sysctl.xfrm6_hdr == NULL)
  297. return;
  298. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  299. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  300. if (!net_eq(net, &init_net))
  301. kfree(table);
  302. }
  303. static struct pernet_operations xfrm6_net_ops = {
  304. .init = xfrm6_net_init,
  305. .exit = xfrm6_net_exit,
  306. };
  307. #endif
  308. int __init xfrm6_init(void)
  309. {
  310. int ret;
  311. dst_entries_init(&xfrm6_dst_ops);
  312. ret = xfrm6_policy_init();
  313. if (ret) {
  314. dst_entries_destroy(&xfrm6_dst_ops);
  315. goto out;
  316. }
  317. ret = xfrm6_state_init();
  318. if (ret)
  319. goto out_policy;
  320. #ifdef CONFIG_SYSCTL
  321. register_pernet_subsys(&xfrm6_net_ops);
  322. #endif
  323. out:
  324. return ret;
  325. out_policy:
  326. xfrm6_policy_fini();
  327. goto out;
  328. }
  329. void xfrm6_fini(void)
  330. {
  331. #ifdef CONFIG_SYSCTL
  332. unregister_pernet_subsys(&xfrm6_net_ops);
  333. #endif
  334. xfrm6_policy_fini();
  335. xfrm6_state_fini();
  336. dst_entries_destroy(&xfrm6_dst_ops);
  337. }