xfrm6_policy.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include <net/l3mdev.h>
  23. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  24. #include <net/mip6.h>
  25. #endif
  26. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
  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. fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
  35. fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
  36. memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
  37. if (saddr)
  38. memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
  39. dst = ip6_route_output(net, NULL, &fl6);
  40. err = dst->error;
  41. if (dst->error) {
  42. dst_release(dst);
  43. dst = ERR_PTR(err);
  44. }
  45. return dst;
  46. }
  47. static int xfrm6_get_saddr(struct net *net, int oif,
  48. xfrm_address_t *saddr, xfrm_address_t *daddr)
  49. {
  50. struct dst_entry *dst;
  51. struct net_device *dev;
  52. dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr);
  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_get_tos(const struct flowi *fl)
  61. {
  62. return 0;
  63. }
  64. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  65. int nfheader_len)
  66. {
  67. if (dst->ops->family == AF_INET6) {
  68. struct rt6_info *rt = (struct rt6_info *)dst;
  69. path->path_cookie = rt6_get_cookie(rt);
  70. }
  71. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  72. return 0;
  73. }
  74. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  75. const struct flowi *fl)
  76. {
  77. struct rt6_info *rt = (struct rt6_info *)xdst->route;
  78. xdst->u.dst.dev = dev;
  79. dev_hold(dev);
  80. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  81. if (!xdst->u.rt6.rt6i_idev) {
  82. dev_put(dev);
  83. return -ENODEV;
  84. }
  85. /* Sheit... I remember I did this right. Apparently,
  86. * it was magically lost, so this code needs audit */
  87. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  88. RTF_LOCAL);
  89. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  90. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  91. xdst->route_cookie = rt6_get_cookie(rt);
  92. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  93. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  94. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  95. return 0;
  96. }
  97. static inline void
  98. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  99. {
  100. struct flowi6 *fl6 = &fl->u.ip6;
  101. int onlyproto = 0;
  102. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  103. u16 offset = sizeof(*hdr);
  104. struct ipv6_opt_hdr *exthdr;
  105. const unsigned char *nh = skb_network_header(skb);
  106. u16 nhoff = IP6CB(skb)->nhoff;
  107. int oif = 0;
  108. u8 nexthdr;
  109. if (!nhoff)
  110. nhoff = offsetof(struct ipv6hdr, nexthdr);
  111. nexthdr = nh[nhoff];
  112. if (skb_dst(skb))
  113. oif = skb_dst(skb)->dev->ifindex;
  114. memset(fl6, 0, sizeof(struct flowi6));
  115. fl6->flowi6_mark = skb->mark;
  116. fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
  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;
  141. nh = skb_network_header(skb);
  142. ports = (__be16 *)(nh + offset);
  143. fl6->fl6_sport = ports[!!reverse];
  144. fl6->fl6_dport = ports[!reverse];
  145. }
  146. fl6->flowi6_proto = nexthdr;
  147. return;
  148. case IPPROTO_ICMPV6:
  149. if (!onlyproto && (nh + offset + 2 < skb->data ||
  150. pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
  151. u8 *icmp;
  152. nh = skb_network_header(skb);
  153. icmp = (u8 *)(nh + offset);
  154. fl6->fl6_icmp_type = icmp[0];
  155. fl6->fl6_icmp_code = icmp[1];
  156. }
  157. fl6->flowi6_proto = nexthdr;
  158. return;
  159. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  160. case IPPROTO_MH:
  161. offset += ipv6_optlen(exthdr);
  162. if (!onlyproto && (nh + offset + 3 < skb->data ||
  163. pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
  164. struct ip6_mh *mh;
  165. nh = skb_network_header(skb);
  166. mh = (struct ip6_mh *)(nh + offset);
  167. fl6->fl6_mh_type = mh->ip6mh_type;
  168. }
  169. fl6->flowi6_proto = nexthdr;
  170. return;
  171. #endif
  172. /* XXX Why are there these headers? */
  173. case IPPROTO_AH:
  174. case IPPROTO_ESP:
  175. case IPPROTO_COMP:
  176. default:
  177. fl6->fl6_ipsec_spi = 0;
  178. fl6->flowi6_proto = nexthdr;
  179. return;
  180. }
  181. }
  182. }
  183. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  184. {
  185. struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
  186. xfrm_garbage_collect_deferred(net);
  187. return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
  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. xfrm_dst_destroy(xdst);
  210. }
  211. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  212. int unregister)
  213. {
  214. struct xfrm_dst *xdst;
  215. if (!unregister)
  216. return;
  217. xdst = (struct xfrm_dst *)dst;
  218. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  219. struct inet6_dev *loopback_idev =
  220. in6_dev_get(dev_net(dev)->loopback_dev);
  221. BUG_ON(!loopback_idev);
  222. do {
  223. in6_dev_put(xdst->u.rt6.rt6i_idev);
  224. xdst->u.rt6.rt6i_idev = loopback_idev;
  225. in6_dev_hold(loopback_idev);
  226. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  227. } while (xdst->u.dst.xfrm);
  228. __in6_dev_put(loopback_idev);
  229. }
  230. xfrm_dst_ifdown(dst, dev);
  231. }
  232. static struct dst_ops xfrm6_dst_ops_template = {
  233. .family = AF_INET6,
  234. .gc = xfrm6_garbage_collect,
  235. .update_pmtu = xfrm6_update_pmtu,
  236. .redirect = xfrm6_redirect,
  237. .cow_metrics = dst_cow_metrics_generic,
  238. .destroy = xfrm6_dst_destroy,
  239. .ifdown = xfrm6_dst_ifdown,
  240. .local_out = __ip6_local_out,
  241. .gc_thresh = INT_MAX,
  242. };
  243. static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  244. .dst_ops = &xfrm6_dst_ops_template,
  245. .dst_lookup = xfrm6_dst_lookup,
  246. .get_saddr = xfrm6_get_saddr,
  247. .decode_session = _decode_session6,
  248. .get_tos = xfrm6_get_tos,
  249. .init_path = xfrm6_init_path,
  250. .fill_dst = xfrm6_fill_dst,
  251. .blackhole_route = ip6_blackhole_route,
  252. };
  253. static int __init xfrm6_policy_init(void)
  254. {
  255. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
  256. }
  257. static void xfrm6_policy_fini(void)
  258. {
  259. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  260. }
  261. #ifdef CONFIG_SYSCTL
  262. static struct ctl_table xfrm6_policy_table[] = {
  263. {
  264. .procname = "xfrm6_gc_thresh",
  265. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  266. .maxlen = sizeof(int),
  267. .mode = 0644,
  268. .proc_handler = proc_dointvec,
  269. },
  270. { }
  271. };
  272. static int __net_init xfrm6_net_sysctl_init(struct net *net)
  273. {
  274. struct ctl_table *table;
  275. struct ctl_table_header *hdr;
  276. table = xfrm6_policy_table;
  277. if (!net_eq(net, &init_net)) {
  278. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  279. if (!table)
  280. goto err_alloc;
  281. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  282. }
  283. hdr = register_net_sysctl(net, "net/ipv6", table);
  284. if (!hdr)
  285. goto err_reg;
  286. net->ipv6.sysctl.xfrm6_hdr = hdr;
  287. return 0;
  288. err_reg:
  289. if (!net_eq(net, &init_net))
  290. kfree(table);
  291. err_alloc:
  292. return -ENOMEM;
  293. }
  294. static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
  295. {
  296. struct ctl_table *table;
  297. if (!net->ipv6.sysctl.xfrm6_hdr)
  298. return;
  299. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  300. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  301. if (!net_eq(net, &init_net))
  302. kfree(table);
  303. }
  304. #else /* CONFIG_SYSCTL */
  305. static inline int xfrm6_net_sysctl_init(struct net *net)
  306. {
  307. return 0;
  308. }
  309. static inline void xfrm6_net_sysctl_exit(struct net *net)
  310. {
  311. }
  312. #endif
  313. static int __net_init xfrm6_net_init(struct net *net)
  314. {
  315. int ret;
  316. memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
  317. sizeof(xfrm6_dst_ops_template));
  318. ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
  319. if (ret)
  320. return ret;
  321. ret = xfrm6_net_sysctl_init(net);
  322. if (ret)
  323. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  324. return ret;
  325. }
  326. static void __net_exit xfrm6_net_exit(struct net *net)
  327. {
  328. xfrm6_net_sysctl_exit(net);
  329. dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
  330. }
  331. static struct pernet_operations xfrm6_net_ops = {
  332. .init = xfrm6_net_init,
  333. .exit = xfrm6_net_exit,
  334. };
  335. int __init xfrm6_init(void)
  336. {
  337. int ret;
  338. ret = xfrm6_policy_init();
  339. if (ret)
  340. goto out;
  341. ret = xfrm6_state_init();
  342. if (ret)
  343. goto out_policy;
  344. ret = xfrm6_protocol_init();
  345. if (ret)
  346. goto out_state;
  347. register_pernet_subsys(&xfrm6_net_ops);
  348. out:
  349. return ret;
  350. out_state:
  351. xfrm6_state_fini();
  352. out_policy:
  353. xfrm6_policy_fini();
  354. goto out;
  355. }
  356. void xfrm6_fini(void)
  357. {
  358. unregister_pernet_subsys(&xfrm6_net_ops);
  359. xfrm6_protocol_fini();
  360. xfrm6_policy_fini();
  361. xfrm6_state_fini();
  362. }