xfrm6_policy.c 7.0 KB

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