xfrm6_policy.c 9.0 KB

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