xfrm6_policy.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * xfrm6_policy.c: based on xfrm4_policy.c
  3. *
  4. * Authors:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <[email protected]>
  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. #include <net/l3mdev.h>
  23. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  24. #include <net/mip6.h>
  25. #endif
  26. static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
  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->u.rt6.rt6i_metric = rt->rt6i_metric;
  94. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  95. xdst->route_cookie = rt6_get_cookie(rt);
  96. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  97. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  98. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  99. return 0;
  100. }
  101. static inline void
  102. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  103. {
  104. struct flowi6 *fl6 = &fl->u.ip6;
  105. int onlyproto = 0;
  106. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  107. u32 offset = sizeof(*hdr);
  108. struct ipv6_opt_hdr *exthdr;
  109. const unsigned char *nh = skb_network_header(skb);
  110. u16 nhoff = IP6CB(skb)->nhoff;
  111. int oif = 0;
  112. u8 nexthdr;
  113. if (!nhoff)
  114. nhoff = offsetof(struct ipv6hdr, nexthdr);
  115. nexthdr = nh[nhoff];
  116. if (skb_dst(skb))
  117. oif = skb_dst(skb)->dev->ifindex;
  118. memset(fl6, 0, sizeof(struct flowi6));
  119. fl6->flowi6_mark = skb->mark;
  120. fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
  121. fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
  122. fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
  123. while (nh + offset + 1 < skb->data ||
  124. pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  125. nh = skb_network_header(skb);
  126. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  127. switch (nexthdr) {
  128. case NEXTHDR_FRAGMENT:
  129. onlyproto = 1;
  130. case NEXTHDR_ROUTING:
  131. case NEXTHDR_HOP:
  132. case NEXTHDR_DEST:
  133. offset += ipv6_optlen(exthdr);
  134. nexthdr = exthdr->nexthdr;
  135. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  136. break;
  137. case IPPROTO_UDP:
  138. case IPPROTO_UDPLITE:
  139. case IPPROTO_TCP:
  140. case IPPROTO_SCTP:
  141. case IPPROTO_DCCP:
  142. if (!onlyproto && (nh + offset + 4 < skb->data ||
  143. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  144. __be16 *ports;
  145. nh = skb_network_header(skb);
  146. ports = (__be16 *)(nh + offset);
  147. fl6->fl6_sport = ports[!!reverse];
  148. fl6->fl6_dport = ports[!reverse];
  149. }
  150. fl6->flowi6_proto = nexthdr;
  151. return;
  152. case IPPROTO_ICMPV6:
  153. if (!onlyproto && (nh + offset + 2 < skb->data ||
  154. pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
  155. u8 *icmp;
  156. nh = skb_network_header(skb);
  157. icmp = (u8 *)(nh + offset);
  158. fl6->fl6_icmp_type = icmp[0];
  159. fl6->fl6_icmp_code = icmp[1];
  160. }
  161. fl6->flowi6_proto = nexthdr;
  162. return;
  163. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  164. case IPPROTO_MH:
  165. offset += ipv6_optlen(exthdr);
  166. if (!onlyproto && (nh + offset + 3 < skb->data ||
  167. pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
  168. struct ip6_mh *mh;
  169. nh = skb_network_header(skb);
  170. mh = (struct ip6_mh *)(nh + offset);
  171. fl6->fl6_mh_type = mh->ip6mh_type;
  172. }
  173. fl6->flowi6_proto = nexthdr;
  174. return;
  175. #endif
  176. /* XXX Why are there these headers? */
  177. case IPPROTO_AH:
  178. case IPPROTO_ESP:
  179. case IPPROTO_COMP:
  180. default:
  181. fl6->fl6_ipsec_spi = 0;
  182. fl6->flowi6_proto = nexthdr;
  183. return;
  184. }
  185. }
  186. }
  187. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  188. {
  189. struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
  190. xfrm6_policy_afinfo.garbage_collect(net);
  191. return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
  192. }
  193. static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
  194. struct sk_buff *skb, u32 mtu)
  195. {
  196. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  197. struct dst_entry *path = xdst->route;
  198. path->ops->update_pmtu(path, sk, skb, mtu);
  199. }
  200. static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
  201. struct sk_buff *skb)
  202. {
  203. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  204. struct dst_entry *path = xdst->route;
  205. path->ops->redirect(path, sk, skb);
  206. }
  207. static void xfrm6_dst_destroy(struct dst_entry *dst)
  208. {
  209. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  210. if (likely(xdst->u.rt6.rt6i_idev))
  211. in6_dev_put(xdst->u.rt6.rt6i_idev);
  212. dst_destroy_metrics_generic(dst);
  213. xfrm_dst_destroy(xdst);
  214. }
  215. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  216. int unregister)
  217. {
  218. struct xfrm_dst *xdst;
  219. if (!unregister)
  220. return;
  221. xdst = (struct xfrm_dst *)dst;
  222. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  223. struct inet6_dev *loopback_idev =
  224. in6_dev_get(dev_net(dev)->loopback_dev);
  225. BUG_ON(!loopback_idev);
  226. do {
  227. in6_dev_put(xdst->u.rt6.rt6i_idev);
  228. xdst->u.rt6.rt6i_idev = loopback_idev;
  229. in6_dev_hold(loopback_idev);
  230. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  231. } while (xdst->u.dst.xfrm);
  232. __in6_dev_put(loopback_idev);
  233. }
  234. xfrm_dst_ifdown(dst, dev);
  235. }
  236. static struct dst_ops xfrm6_dst_ops_template = {
  237. .family = AF_INET6,
  238. .gc = xfrm6_garbage_collect,
  239. .update_pmtu = xfrm6_update_pmtu,
  240. .redirect = xfrm6_redirect,
  241. .cow_metrics = dst_cow_metrics_generic,
  242. .destroy = xfrm6_dst_destroy,
  243. .ifdown = xfrm6_dst_ifdown,
  244. .local_out = __ip6_local_out,
  245. .gc_thresh = INT_MAX,
  246. };
  247. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  248. .family = AF_INET6,
  249. .dst_ops = &xfrm6_dst_ops_template,
  250. .dst_lookup = xfrm6_dst_lookup,
  251. .get_saddr = xfrm6_get_saddr,
  252. .decode_session = _decode_session6,
  253. .get_tos = xfrm6_get_tos,
  254. .init_path = xfrm6_init_path,
  255. .fill_dst = xfrm6_fill_dst,
  256. .blackhole_route = ip6_blackhole_route,
  257. };
  258. static int __init xfrm6_policy_init(void)
  259. {
  260. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  261. }
  262. static void xfrm6_policy_fini(void)
  263. {
  264. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  265. }
  266. #ifdef CONFIG_SYSCTL
  267. static struct ctl_table xfrm6_policy_table[] = {
  268. {
  269. .procname = "xfrm6_gc_thresh",
  270. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  271. .maxlen = sizeof(int),
  272. .mode = 0644,
  273. .proc_handler = proc_dointvec,
  274. },
  275. { }
  276. };
  277. static int __net_init xfrm6_net_sysctl_init(struct net *net)
  278. {
  279. struct ctl_table *table;
  280. struct ctl_table_header *hdr;
  281. table = xfrm6_policy_table;
  282. if (!net_eq(net, &init_net)) {
  283. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  284. if (!table)
  285. goto err_alloc;
  286. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  287. }
  288. hdr = register_net_sysctl(net, "net/ipv6", table);
  289. if (!hdr)
  290. goto err_reg;
  291. net->ipv6.sysctl.xfrm6_hdr = hdr;
  292. return 0;
  293. err_reg:
  294. if (!net_eq(net, &init_net))
  295. kfree(table);
  296. err_alloc:
  297. return -ENOMEM;
  298. }
  299. static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
  300. {
  301. struct ctl_table *table;
  302. if (!net->ipv6.sysctl.xfrm6_hdr)
  303. return;
  304. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  305. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  306. if (!net_eq(net, &init_net))
  307. kfree(table);
  308. }
  309. #else /* CONFIG_SYSCTL */
  310. static inline int xfrm6_net_sysctl_init(struct net *net)
  311. {
  312. return 0;
  313. }
  314. static inline void xfrm6_net_sysctl_exit(struct net *net)
  315. {
  316. }
  317. #endif
  318. static int __net_init xfrm6_net_init(struct net *net)
  319. {
  320. int ret;
  321. memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
  322. sizeof(xfrm6_dst_ops_template));
  323. ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
  324. if (ret)
  325. return ret;
  326. ret = xfrm6_net_sysctl_init(net);
  327. if (ret)
  328. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  329. return ret;
  330. }
  331. static void __net_exit xfrm6_net_exit(struct net *net)
  332. {
  333. xfrm6_net_sysctl_exit(net);
  334. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  335. }
  336. static struct pernet_operations xfrm6_net_ops = {
  337. .init = xfrm6_net_init,
  338. .exit = xfrm6_net_exit,
  339. };
  340. int __init xfrm6_init(void)
  341. {
  342. int ret;
  343. ret = xfrm6_policy_init();
  344. if (ret)
  345. goto out;
  346. ret = xfrm6_state_init();
  347. if (ret)
  348. goto out_policy;
  349. ret = xfrm6_protocol_init();
  350. if (ret)
  351. goto out_state;
  352. register_pernet_subsys(&xfrm6_net_ops);
  353. out:
  354. return ret;
  355. out_state:
  356. xfrm6_state_fini();
  357. out_policy:
  358. xfrm6_policy_fini();
  359. goto out;
  360. }
  361. void xfrm6_fini(void)
  362. {
  363. unregister_pernet_subsys(&xfrm6_net_ops);
  364. xfrm6_protocol_fini();
  365. xfrm6_policy_fini();
  366. xfrm6_state_fini();
  367. }