xfrm6_policy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xfrm6_policy.c: based on xfrm4_policy.c
  4. *
  5. * Authors:
  6. * Mitsuru KANDA @USAGI
  7. * Kazunori MIYAZAWA @USAGI
  8. * Kunihiro Ishiguro <[email protected]>
  9. * IPv6 support
  10. * YOSHIFUJI Hideaki
  11. * Split up af-specific portion
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netdevice.h>
  17. #include <net/addrconf.h>
  18. #include <net/dst.h>
  19. #include <net/xfrm.h>
  20. #include <net/ip.h>
  21. #include <net/ipv6.h>
  22. #include <net/ip6_route.h>
  23. #include <net/l3mdev.h>
  24. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
  25. const xfrm_address_t *saddr,
  26. const xfrm_address_t *daddr,
  27. u32 mark)
  28. {
  29. struct flowi6 fl6;
  30. struct dst_entry *dst;
  31. int err;
  32. memset(&fl6, 0, sizeof(fl6));
  33. fl6.flowi6_l3mdev = l3mdev_master_ifindex_by_index(net, oif);
  34. fl6.flowi6_mark = mark;
  35. memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
  36. if (saddr)
  37. memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
  38. dst = ip6_route_output(net, NULL, &fl6);
  39. err = dst->error;
  40. if (dst->error) {
  41. dst_release(dst);
  42. dst = ERR_PTR(err);
  43. }
  44. return dst;
  45. }
  46. static int xfrm6_get_saddr(struct net *net, int oif,
  47. xfrm_address_t *saddr, xfrm_address_t *daddr,
  48. u32 mark)
  49. {
  50. struct dst_entry *dst;
  51. struct net_device *dev;
  52. dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr, mark);
  53. if (IS_ERR(dst))
  54. return -EHOSTUNREACH;
  55. dev = ip6_dst_idev(dst)->dev;
  56. ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
  57. dst_release(dst);
  58. return 0;
  59. }
  60. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  61. const struct flowi *fl)
  62. {
  63. struct rt6_info *rt = (struct rt6_info *)xdst->route;
  64. xdst->u.dst.dev = dev;
  65. netdev_hold(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
  66. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  67. if (!xdst->u.rt6.rt6i_idev) {
  68. netdev_put(dev, &xdst->u.dst.dev_tracker);
  69. return -ENODEV;
  70. }
  71. /* Sheit... I remember I did this right. Apparently,
  72. * it was magically lost, so this code needs audit */
  73. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  74. RTF_LOCAL);
  75. xdst->route_cookie = rt6_get_cookie(rt);
  76. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  77. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  78. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  79. INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
  80. rt6_uncached_list_add(&xdst->u.rt6);
  81. return 0;
  82. }
  83. static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
  84. struct sk_buff *skb, u32 mtu,
  85. bool confirm_neigh)
  86. {
  87. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  88. struct dst_entry *path = xdst->route;
  89. path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
  90. }
  91. static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
  92. struct sk_buff *skb)
  93. {
  94. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  95. struct dst_entry *path = xdst->route;
  96. path->ops->redirect(path, sk, skb);
  97. }
  98. static void xfrm6_dst_destroy(struct dst_entry *dst)
  99. {
  100. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  101. dst_destroy_metrics_generic(dst);
  102. if (xdst->u.rt6.rt6i_uncached_list)
  103. rt6_uncached_list_del(&xdst->u.rt6);
  104. if (likely(xdst->u.rt6.rt6i_idev))
  105. in6_dev_put(xdst->u.rt6.rt6i_idev);
  106. xfrm_dst_destroy(xdst);
  107. }
  108. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  109. int unregister)
  110. {
  111. struct xfrm_dst *xdst;
  112. if (!unregister)
  113. return;
  114. xdst = (struct xfrm_dst *)dst;
  115. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  116. struct inet6_dev *loopback_idev =
  117. in6_dev_get(dev_net(dev)->loopback_dev);
  118. do {
  119. in6_dev_put(xdst->u.rt6.rt6i_idev);
  120. xdst->u.rt6.rt6i_idev = loopback_idev;
  121. in6_dev_hold(loopback_idev);
  122. xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
  123. } while (xdst->u.dst.xfrm);
  124. __in6_dev_put(loopback_idev);
  125. }
  126. xfrm_dst_ifdown(dst, dev);
  127. }
  128. static struct dst_ops xfrm6_dst_ops_template = {
  129. .family = AF_INET6,
  130. .update_pmtu = xfrm6_update_pmtu,
  131. .redirect = xfrm6_redirect,
  132. .cow_metrics = dst_cow_metrics_generic,
  133. .destroy = xfrm6_dst_destroy,
  134. .ifdown = xfrm6_dst_ifdown,
  135. .local_out = __ip6_local_out,
  136. .gc_thresh = 32768,
  137. };
  138. static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  139. .dst_ops = &xfrm6_dst_ops_template,
  140. .dst_lookup = xfrm6_dst_lookup,
  141. .get_saddr = xfrm6_get_saddr,
  142. .fill_dst = xfrm6_fill_dst,
  143. .blackhole_route = ip6_blackhole_route,
  144. };
  145. static int __init xfrm6_policy_init(void)
  146. {
  147. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
  148. }
  149. static void xfrm6_policy_fini(void)
  150. {
  151. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  152. }
  153. #ifdef CONFIG_SYSCTL
  154. static struct ctl_table xfrm6_policy_table[] = {
  155. {
  156. .procname = "xfrm6_gc_thresh",
  157. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  158. .maxlen = sizeof(int),
  159. .mode = 0644,
  160. .proc_handler = proc_dointvec,
  161. },
  162. { }
  163. };
  164. static int __net_init xfrm6_net_sysctl_init(struct net *net)
  165. {
  166. struct ctl_table *table;
  167. struct ctl_table_header *hdr;
  168. table = xfrm6_policy_table;
  169. if (!net_eq(net, &init_net)) {
  170. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  171. if (!table)
  172. goto err_alloc;
  173. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  174. }
  175. hdr = register_net_sysctl(net, "net/ipv6", table);
  176. if (!hdr)
  177. goto err_reg;
  178. net->ipv6.sysctl.xfrm6_hdr = hdr;
  179. return 0;
  180. err_reg:
  181. if (!net_eq(net, &init_net))
  182. kfree(table);
  183. err_alloc:
  184. return -ENOMEM;
  185. }
  186. static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
  187. {
  188. struct ctl_table *table;
  189. if (!net->ipv6.sysctl.xfrm6_hdr)
  190. return;
  191. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  192. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  193. if (!net_eq(net, &init_net))
  194. kfree(table);
  195. }
  196. #else /* CONFIG_SYSCTL */
  197. static inline int xfrm6_net_sysctl_init(struct net *net)
  198. {
  199. return 0;
  200. }
  201. static inline void xfrm6_net_sysctl_exit(struct net *net)
  202. {
  203. }
  204. #endif
  205. static int __net_init xfrm6_net_init(struct net *net)
  206. {
  207. int ret;
  208. memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
  209. sizeof(xfrm6_dst_ops_template));
  210. ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
  211. if (ret)
  212. return ret;
  213. ret = xfrm6_net_sysctl_init(net);
  214. if (ret)
  215. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  216. return ret;
  217. }
  218. static void __net_exit xfrm6_net_exit(struct net *net)
  219. {
  220. xfrm6_net_sysctl_exit(net);
  221. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  222. }
  223. static struct pernet_operations xfrm6_net_ops = {
  224. .init = xfrm6_net_init,
  225. .exit = xfrm6_net_exit,
  226. };
  227. int __init xfrm6_init(void)
  228. {
  229. int ret;
  230. ret = xfrm6_policy_init();
  231. if (ret)
  232. goto out;
  233. ret = xfrm6_state_init();
  234. if (ret)
  235. goto out_policy;
  236. ret = xfrm6_protocol_init();
  237. if (ret)
  238. goto out_state;
  239. ret = register_pernet_subsys(&xfrm6_net_ops);
  240. if (ret)
  241. goto out_protocol;
  242. out:
  243. return ret;
  244. out_protocol:
  245. xfrm6_protocol_fini();
  246. out_state:
  247. xfrm6_state_fini();
  248. out_policy:
  249. xfrm6_policy_fini();
  250. goto out;
  251. }
  252. void xfrm6_fini(void)
  253. {
  254. unregister_pernet_subsys(&xfrm6_net_ops);
  255. xfrm6_protocol_fini();
  256. xfrm6_policy_fini();
  257. xfrm6_state_fini();
  258. }