xfrm6_policy.c 9.7 KB

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