xfrm6_policy.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  25. #include <net/mip6.h>
  26. #endif
  27. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
  28. const xfrm_address_t *saddr,
  29. const xfrm_address_t *daddr,
  30. u32 mark)
  31. {
  32. struct flowi6 fl6;
  33. struct dst_entry *dst;
  34. int err;
  35. memset(&fl6, 0, sizeof(fl6));
  36. fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
  37. fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
  38. fl6.flowi6_mark = mark;
  39. memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
  40. if (saddr)
  41. memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
  42. dst = ip6_route_output(net, NULL, &fl6);
  43. err = dst->error;
  44. if (dst->error) {
  45. dst_release(dst);
  46. dst = ERR_PTR(err);
  47. }
  48. return dst;
  49. }
  50. static int xfrm6_get_saddr(struct net *net, int oif,
  51. xfrm_address_t *saddr, xfrm_address_t *daddr,
  52. u32 mark)
  53. {
  54. struct dst_entry *dst;
  55. struct net_device *dev;
  56. dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr, mark);
  57. if (IS_ERR(dst))
  58. return -EHOSTUNREACH;
  59. dev = ip6_dst_idev(dst)->dev;
  60. ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
  61. dst_release(dst);
  62. return 0;
  63. }
  64. static int xfrm6_get_tos(const struct flowi *fl)
  65. {
  66. return 0;
  67. }
  68. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  69. int nfheader_len)
  70. {
  71. if (dst->ops->family == AF_INET6) {
  72. struct rt6_info *rt = (struct rt6_info *)dst;
  73. path->path_cookie = rt6_get_cookie(rt);
  74. }
  75. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  76. return 0;
  77. }
  78. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  79. const struct flowi *fl)
  80. {
  81. struct rt6_info *rt = (struct rt6_info *)xdst->route;
  82. xdst->u.dst.dev = dev;
  83. dev_hold(dev);
  84. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  85. if (!xdst->u.rt6.rt6i_idev) {
  86. dev_put(dev);
  87. return -ENODEV;
  88. }
  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->route_cookie = rt6_get_cookie(rt);
  94. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  95. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  96. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  97. INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
  98. rt6_uncached_list_add(&xdst->u.rt6);
  99. atomic_inc(&dev_net(dev)->ipv6.rt6_stats->fib_rt_uncache);
  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. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  108. u32 offset = sizeof(*hdr);
  109. struct ipv6_opt_hdr *exthdr;
  110. const unsigned char *nh = skb_network_header(skb);
  111. u16 nhoff = IP6CB(skb)->nhoff;
  112. int oif = 0;
  113. u8 nexthdr;
  114. if (!nhoff)
  115. nhoff = offsetof(struct ipv6hdr, nexthdr);
  116. nexthdr = nh[nhoff];
  117. if (skb_dst(skb))
  118. oif = skb_dst(skb)->dev->ifindex;
  119. memset(fl6, 0, sizeof(struct flowi6));
  120. fl6->flowi6_mark = skb->mark;
  121. fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
  122. fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
  123. fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
  124. while (nh + offset + sizeof(*exthdr) < skb->data ||
  125. pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
  126. nh = skb_network_header(skb);
  127. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  128. switch (nexthdr) {
  129. case NEXTHDR_FRAGMENT:
  130. onlyproto = 1;
  131. /* fall through */
  132. case NEXTHDR_ROUTING:
  133. case NEXTHDR_HOP:
  134. case NEXTHDR_DEST:
  135. offset += ipv6_optlen(exthdr);
  136. nexthdr = exthdr->nexthdr;
  137. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  138. break;
  139. case IPPROTO_UDP:
  140. case IPPROTO_UDPLITE:
  141. case IPPROTO_TCP:
  142. case IPPROTO_SCTP:
  143. case IPPROTO_DCCP:
  144. if (!onlyproto && (nh + offset + 4 < skb->data ||
  145. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  146. __be16 *ports;
  147. nh = skb_network_header(skb);
  148. ports = (__be16 *)(nh + offset);
  149. fl6->fl6_sport = ports[!!reverse];
  150. fl6->fl6_dport = ports[!reverse];
  151. }
  152. fl6->flowi6_proto = nexthdr;
  153. return;
  154. case IPPROTO_ICMPV6:
  155. if (!onlyproto && (nh + offset + 2 < skb->data ||
  156. pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
  157. u8 *icmp;
  158. nh = skb_network_header(skb);
  159. icmp = (u8 *)(nh + offset);
  160. fl6->fl6_icmp_type = icmp[0];
  161. fl6->fl6_icmp_code = icmp[1];
  162. }
  163. fl6->flowi6_proto = nexthdr;
  164. return;
  165. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  166. case IPPROTO_MH:
  167. offset += ipv6_optlen(exthdr);
  168. if (!onlyproto && (nh + offset + 3 < skb->data ||
  169. pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
  170. struct ip6_mh *mh;
  171. nh = skb_network_header(skb);
  172. mh = (struct ip6_mh *)(nh + offset);
  173. fl6->fl6_mh_type = mh->ip6mh_type;
  174. }
  175. fl6->flowi6_proto = nexthdr;
  176. return;
  177. #endif
  178. /* XXX Why are there these headers? */
  179. case IPPROTO_AH:
  180. case IPPROTO_ESP:
  181. case IPPROTO_COMP:
  182. default:
  183. fl6->fl6_ipsec_spi = 0;
  184. fl6->flowi6_proto = nexthdr;
  185. return;
  186. }
  187. }
  188. }
  189. static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
  190. struct sk_buff *skb, u32 mtu)
  191. {
  192. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  193. struct dst_entry *path = xdst->route;
  194. path->ops->update_pmtu(path, sk, skb, mtu);
  195. }
  196. static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
  197. struct sk_buff *skb)
  198. {
  199. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  200. struct dst_entry *path = xdst->route;
  201. path->ops->redirect(path, sk, skb);
  202. }
  203. static void xfrm6_dst_destroy(struct dst_entry *dst)
  204. {
  205. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  206. if (likely(xdst->u.rt6.rt6i_idev))
  207. in6_dev_put(xdst->u.rt6.rt6i_idev);
  208. dst_destroy_metrics_generic(dst);
  209. if (xdst->u.rt6.rt6i_uncached_list)
  210. rt6_uncached_list_del(&xdst->u.rt6);
  211. xfrm_dst_destroy(xdst);
  212. }
  213. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  214. int unregister)
  215. {
  216. struct xfrm_dst *xdst;
  217. if (!unregister)
  218. return;
  219. xdst = (struct xfrm_dst *)dst;
  220. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  221. struct inet6_dev *loopback_idev =
  222. in6_dev_get(dev_net(dev)->loopback_dev);
  223. BUG_ON(!loopback_idev);
  224. do {
  225. in6_dev_put(xdst->u.rt6.rt6i_idev);
  226. xdst->u.rt6.rt6i_idev = loopback_idev;
  227. in6_dev_hold(loopback_idev);
  228. xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
  229. } while (xdst->u.dst.xfrm);
  230. __in6_dev_put(loopback_idev);
  231. }
  232. xfrm_dst_ifdown(dst, dev);
  233. }
  234. static struct dst_ops xfrm6_dst_ops_template = {
  235. .family = AF_INET6,
  236. .update_pmtu = xfrm6_update_pmtu,
  237. .redirect = xfrm6_redirect,
  238. .cow_metrics = dst_cow_metrics_generic,
  239. .destroy = xfrm6_dst_destroy,
  240. .ifdown = xfrm6_dst_ifdown,
  241. .local_out = __ip6_local_out,
  242. .gc_thresh = 32768,
  243. };
  244. static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  245. .dst_ops = &xfrm6_dst_ops_template,
  246. .dst_lookup = xfrm6_dst_lookup,
  247. .get_saddr = xfrm6_get_saddr,
  248. .decode_session = _decode_session6,
  249. .get_tos = xfrm6_get_tos,
  250. .init_path = xfrm6_init_path,
  251. .fill_dst = xfrm6_fill_dst,
  252. .blackhole_route = ip6_blackhole_route,
  253. };
  254. static int __init xfrm6_policy_init(void)
  255. {
  256. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
  257. }
  258. static void xfrm6_policy_fini(void)
  259. {
  260. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  261. }
  262. #ifdef CONFIG_SYSCTL
  263. static struct ctl_table xfrm6_policy_table[] = {
  264. {
  265. .procname = "xfrm6_gc_thresh",
  266. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  267. .maxlen = sizeof(int),
  268. .mode = 0644,
  269. .proc_handler = proc_dointvec,
  270. },
  271. { }
  272. };
  273. static int __net_init xfrm6_net_sysctl_init(struct net *net)
  274. {
  275. struct ctl_table *table;
  276. struct ctl_table_header *hdr;
  277. table = xfrm6_policy_table;
  278. if (!net_eq(net, &init_net)) {
  279. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  280. if (!table)
  281. goto err_alloc;
  282. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  283. }
  284. hdr = register_net_sysctl(net, "net/ipv6", table);
  285. if (!hdr)
  286. goto err_reg;
  287. net->ipv6.sysctl.xfrm6_hdr = hdr;
  288. return 0;
  289. err_reg:
  290. if (!net_eq(net, &init_net))
  291. kfree(table);
  292. err_alloc:
  293. return -ENOMEM;
  294. }
  295. static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
  296. {
  297. struct ctl_table *table;
  298. if (!net->ipv6.sysctl.xfrm6_hdr)
  299. return;
  300. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  301. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  302. if (!net_eq(net, &init_net))
  303. kfree(table);
  304. }
  305. #else /* CONFIG_SYSCTL */
  306. static inline int xfrm6_net_sysctl_init(struct net *net)
  307. {
  308. return 0;
  309. }
  310. static inline void xfrm6_net_sysctl_exit(struct net *net)
  311. {
  312. }
  313. #endif
  314. static int __net_init xfrm6_net_init(struct net *net)
  315. {
  316. int ret;
  317. memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
  318. sizeof(xfrm6_dst_ops_template));
  319. ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
  320. if (ret)
  321. return ret;
  322. ret = xfrm6_net_sysctl_init(net);
  323. if (ret)
  324. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  325. return ret;
  326. }
  327. static void __net_exit xfrm6_net_exit(struct net *net)
  328. {
  329. xfrm6_net_sysctl_exit(net);
  330. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  331. }
  332. static struct pernet_operations xfrm6_net_ops = {
  333. .init = xfrm6_net_init,
  334. .exit = xfrm6_net_exit,
  335. };
  336. int __init xfrm6_init(void)
  337. {
  338. int ret;
  339. ret = xfrm6_policy_init();
  340. if (ret)
  341. goto out;
  342. ret = xfrm6_state_init();
  343. if (ret)
  344. goto out_policy;
  345. ret = xfrm6_protocol_init();
  346. if (ret)
  347. goto out_state;
  348. register_pernet_subsys(&xfrm6_net_ops);
  349. out:
  350. return ret;
  351. out_state:
  352. xfrm6_state_fini();
  353. out_policy:
  354. xfrm6_policy_fini();
  355. goto out;
  356. }
  357. void xfrm6_fini(void)
  358. {
  359. unregister_pernet_subsys(&xfrm6_net_ops);
  360. xfrm6_protocol_fini();
  361. xfrm6_policy_fini();
  362. xfrm6_state_fini();
  363. }