xfrm6_policy.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  23. #include <net/mip6.h>
  24. #endif
  25. static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
  26. static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos,
  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. memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
  35. if (saddr)
  36. memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
  37. dst = ip6_route_output(net, NULL, &fl6);
  38. err = dst->error;
  39. if (dst->error) {
  40. dst_release(dst);
  41. dst = ERR_PTR(err);
  42. }
  43. return dst;
  44. }
  45. static int xfrm6_get_saddr(struct net *net,
  46. xfrm_address_t *saddr, xfrm_address_t *daddr)
  47. {
  48. struct dst_entry *dst;
  49. struct net_device *dev;
  50. dst = xfrm6_dst_lookup(net, 0, NULL, daddr);
  51. if (IS_ERR(dst))
  52. return -EHOSTUNREACH;
  53. dev = ip6_dst_idev(dst)->dev;
  54. ipv6_dev_get_saddr(dev_net(dev), dev,
  55. (struct in6_addr *)&daddr->a6, 0,
  56. (struct in6_addr *)&saddr->a6);
  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 void xfrm6_init_dst(struct net *net, struct xfrm_dst *xdst)
  65. {
  66. struct rt6_info *rt = (struct rt6_info *)xdst;
  67. rt6_init_peer(rt, net->ipv6.peers);
  68. }
  69. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  70. int nfheader_len)
  71. {
  72. if (dst->ops->family == AF_INET6) {
  73. struct rt6_info *rt = (struct rt6_info *)dst;
  74. if (rt->rt6i_node)
  75. path->path_cookie = rt->rt6i_node->fn_sernum;
  76. }
  77. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  78. return 0;
  79. }
  80. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  81. const struct flowi *fl)
  82. {
  83. struct rt6_info *rt = (struct rt6_info *)xdst->route;
  84. xdst->u.dst.dev = dev;
  85. dev_hold(dev);
  86. xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
  87. if (!xdst->u.rt6.rt6i_idev) {
  88. dev_put(dev);
  89. return -ENODEV;
  90. }
  91. rt6_transfer_peer(&xdst->u.rt6, rt);
  92. /* Sheit... I remember I did this right. Apparently,
  93. * it was magically lost, so this code needs audit */
  94. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  95. RTF_LOCAL);
  96. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  97. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  98. if (rt->rt6i_node)
  99. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  100. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  101. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  102. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  103. return 0;
  104. }
  105. static inline void
  106. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  107. {
  108. struct flowi6 *fl6 = &fl->u.ip6;
  109. int onlyproto = 0;
  110. u16 offset = skb_network_header_len(skb);
  111. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  112. struct ipv6_opt_hdr *exthdr;
  113. const unsigned char *nh = skb_network_header(skb);
  114. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  115. int oif = 0;
  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 && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  154. u8 *icmp;
  155. nh = skb_network_header(skb);
  156. icmp = (u8 *)(nh + offset);
  157. fl6->fl6_icmp_type = icmp[0];
  158. fl6->fl6_icmp_code = icmp[1];
  159. }
  160. fl6->flowi6_proto = nexthdr;
  161. return;
  162. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  163. case IPPROTO_MH:
  164. if (!onlyproto && 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. if (rt6_has_peer(&xdst->u.rt6)) {
  211. struct inet_peer *peer = rt6_peer_ptr(&xdst->u.rt6);
  212. inet_putpeer(peer);
  213. }
  214. xfrm_dst_destroy(xdst);
  215. }
  216. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  217. int unregister)
  218. {
  219. struct xfrm_dst *xdst;
  220. if (!unregister)
  221. return;
  222. xdst = (struct xfrm_dst *)dst;
  223. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  224. struct inet6_dev *loopback_idev =
  225. in6_dev_get(dev_net(dev)->loopback_dev);
  226. BUG_ON(!loopback_idev);
  227. do {
  228. in6_dev_put(xdst->u.rt6.rt6i_idev);
  229. xdst->u.rt6.rt6i_idev = loopback_idev;
  230. in6_dev_hold(loopback_idev);
  231. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  232. } while (xdst->u.dst.xfrm);
  233. __in6_dev_put(loopback_idev);
  234. }
  235. xfrm_dst_ifdown(dst, dev);
  236. }
  237. static struct dst_ops xfrm6_dst_ops = {
  238. .family = AF_INET6,
  239. .protocol = cpu_to_be16(ETH_P_IPV6),
  240. .gc = xfrm6_garbage_collect,
  241. .update_pmtu = xfrm6_update_pmtu,
  242. .redirect = xfrm6_redirect,
  243. .cow_metrics = dst_cow_metrics_generic,
  244. .destroy = xfrm6_dst_destroy,
  245. .ifdown = xfrm6_dst_ifdown,
  246. .local_out = __ip6_local_out,
  247. .gc_thresh = 32768,
  248. };
  249. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  250. .family = AF_INET6,
  251. .dst_ops = &xfrm6_dst_ops,
  252. .dst_lookup = xfrm6_dst_lookup,
  253. .get_saddr = xfrm6_get_saddr,
  254. .decode_session = _decode_session6,
  255. .get_tos = xfrm6_get_tos,
  256. .init_dst = xfrm6_init_dst,
  257. .init_path = xfrm6_init_path,
  258. .fill_dst = xfrm6_fill_dst,
  259. .blackhole_route = ip6_blackhole_route,
  260. };
  261. static int __init xfrm6_policy_init(void)
  262. {
  263. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  264. }
  265. static void xfrm6_policy_fini(void)
  266. {
  267. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  268. }
  269. #ifdef CONFIG_SYSCTL
  270. static struct ctl_table xfrm6_policy_table[] = {
  271. {
  272. .procname = "xfrm6_gc_thresh",
  273. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  274. .maxlen = sizeof(int),
  275. .mode = 0644,
  276. .proc_handler = proc_dointvec,
  277. },
  278. { }
  279. };
  280. static int __net_init xfrm6_net_init(struct net *net)
  281. {
  282. struct ctl_table *table;
  283. struct ctl_table_header *hdr;
  284. table = xfrm6_policy_table;
  285. if (!net_eq(net, &init_net)) {
  286. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  287. if (!table)
  288. goto err_alloc;
  289. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  290. }
  291. hdr = register_net_sysctl(net, "net/ipv6", table);
  292. if (!hdr)
  293. goto err_reg;
  294. net->ipv6.sysctl.xfrm6_hdr = hdr;
  295. return 0;
  296. err_reg:
  297. if (!net_eq(net, &init_net))
  298. kfree(table);
  299. err_alloc:
  300. return -ENOMEM;
  301. }
  302. static void __net_exit xfrm6_net_exit(struct net *net)
  303. {
  304. struct ctl_table *table;
  305. if (net->ipv6.sysctl.xfrm6_hdr == NULL)
  306. return;
  307. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  308. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  309. if (!net_eq(net, &init_net))
  310. kfree(table);
  311. }
  312. static struct pernet_operations xfrm6_net_ops = {
  313. .init = xfrm6_net_init,
  314. .exit = xfrm6_net_exit,
  315. };
  316. #endif
  317. int __init xfrm6_init(void)
  318. {
  319. int ret;
  320. dst_entries_init(&xfrm6_dst_ops);
  321. ret = xfrm6_policy_init();
  322. if (ret) {
  323. dst_entries_destroy(&xfrm6_dst_ops);
  324. goto out;
  325. }
  326. ret = xfrm6_state_init();
  327. if (ret)
  328. goto out_policy;
  329. ret = xfrm6_protocol_init();
  330. if (ret)
  331. goto out_state;
  332. #ifdef CONFIG_SYSCTL
  333. register_pernet_subsys(&xfrm6_net_ops);
  334. #endif
  335. out:
  336. return ret;
  337. out_state:
  338. xfrm6_state_fini();
  339. out_policy:
  340. xfrm6_policy_fini();
  341. goto out;
  342. }
  343. void xfrm6_fini(void)
  344. {
  345. #ifdef CONFIG_SYSCTL
  346. unregister_pernet_subsys(&xfrm6_net_ops);
  347. #endif
  348. xfrm6_protocol_fini();
  349. xfrm6_policy_fini();
  350. xfrm6_state_fini();
  351. dst_entries_destroy(&xfrm6_dst_ops);
  352. }