xfrm6_policy.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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, &daddr->in6, 0, &saddr->in6);
  55. dst_release(dst);
  56. return 0;
  57. }
  58. static int xfrm6_get_tos(const struct flowi *fl)
  59. {
  60. return 0;
  61. }
  62. static void xfrm6_init_dst(struct net *net, struct xfrm_dst *xdst)
  63. {
  64. struct rt6_info *rt = (struct rt6_info *)xdst;
  65. rt6_init_peer(rt, net->ipv6.peers);
  66. }
  67. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  68. int nfheader_len)
  69. {
  70. if (dst->ops->family == AF_INET6) {
  71. struct rt6_info *rt = (struct rt6_info *)dst;
  72. if (rt->rt6i_node)
  73. path->path_cookie = rt->rt6i_node->fn_sernum;
  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. 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. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  109. u16 offset = sizeof(*hdr);
  110. struct ipv6_opt_hdr *exthdr;
  111. const unsigned char *nh = skb_network_header(skb);
  112. u16 nhoff = IP6CB(skb)->nhoff;
  113. int oif = 0;
  114. u8 nexthdr;
  115. if (!nhoff)
  116. nhoff = offsetof(struct ipv6hdr, nexthdr);
  117. nexthdr = nh[nhoff];
  118. if (skb_dst(skb))
  119. oif = skb_dst(skb)->dev->ifindex;
  120. memset(fl6, 0, sizeof(struct flowi6));
  121. fl6->flowi6_mark = skb->mark;
  122. fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
  123. fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
  124. fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
  125. while (nh + offset + 1 < skb->data ||
  126. pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  127. nh = skb_network_header(skb);
  128. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  129. switch (nexthdr) {
  130. case NEXTHDR_FRAGMENT:
  131. onlyproto = 1;
  132. case NEXTHDR_ROUTING:
  133. case NEXTHDR_HOP:
  134. case NEXTHDR_DEST:
  135. offset += ipv6_optlen(exthdr);
  136. nexthdr = exthdr->nexthdr;
  137. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  138. break;
  139. case IPPROTO_UDP:
  140. case IPPROTO_UDPLITE:
  141. case IPPROTO_TCP:
  142. case IPPROTO_SCTP:
  143. case IPPROTO_DCCP:
  144. if (!onlyproto && (nh + offset + 4 < skb->data ||
  145. pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
  146. __be16 *ports;
  147. nh = skb_network_header(skb);
  148. ports = (__be16 *)(nh + offset);
  149. fl6->fl6_sport = ports[!!reverse];
  150. fl6->fl6_dport = ports[!reverse];
  151. }
  152. fl6->flowi6_proto = nexthdr;
  153. return;
  154. case IPPROTO_ICMPV6:
  155. if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  156. u8 *icmp;
  157. nh = skb_network_header(skb);
  158. icmp = (u8 *)(nh + offset);
  159. fl6->fl6_icmp_type = icmp[0];
  160. fl6->fl6_icmp_code = icmp[1];
  161. }
  162. fl6->flowi6_proto = nexthdr;
  163. return;
  164. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  165. case IPPROTO_MH:
  166. offset += ipv6_optlen(exthdr);
  167. if (!onlyproto && 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. if (rt6_has_peer(&xdst->u.rt6)) {
  214. struct inet_peer *peer = rt6_peer_ptr(&xdst->u.rt6);
  215. inet_putpeer(peer);
  216. }
  217. xfrm_dst_destroy(xdst);
  218. }
  219. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  220. int unregister)
  221. {
  222. struct xfrm_dst *xdst;
  223. if (!unregister)
  224. return;
  225. xdst = (struct xfrm_dst *)dst;
  226. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  227. struct inet6_dev *loopback_idev =
  228. in6_dev_get(dev_net(dev)->loopback_dev);
  229. BUG_ON(!loopback_idev);
  230. do {
  231. in6_dev_put(xdst->u.rt6.rt6i_idev);
  232. xdst->u.rt6.rt6i_idev = loopback_idev;
  233. in6_dev_hold(loopback_idev);
  234. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  235. } while (xdst->u.dst.xfrm);
  236. __in6_dev_put(loopback_idev);
  237. }
  238. xfrm_dst_ifdown(dst, dev);
  239. }
  240. static struct dst_ops xfrm6_dst_ops = {
  241. .family = AF_INET6,
  242. .gc = xfrm6_garbage_collect,
  243. .update_pmtu = xfrm6_update_pmtu,
  244. .redirect = xfrm6_redirect,
  245. .cow_metrics = dst_cow_metrics_generic,
  246. .destroy = xfrm6_dst_destroy,
  247. .ifdown = xfrm6_dst_ifdown,
  248. .local_out = __ip6_local_out,
  249. .gc_thresh = 32768,
  250. };
  251. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  252. .family = AF_INET6,
  253. .dst_ops = &xfrm6_dst_ops,
  254. .dst_lookup = xfrm6_dst_lookup,
  255. .get_saddr = xfrm6_get_saddr,
  256. .decode_session = _decode_session6,
  257. .get_tos = xfrm6_get_tos,
  258. .init_dst = xfrm6_init_dst,
  259. .init_path = xfrm6_init_path,
  260. .fill_dst = xfrm6_fill_dst,
  261. .blackhole_route = ip6_blackhole_route,
  262. };
  263. static int __init xfrm6_policy_init(void)
  264. {
  265. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  266. }
  267. static void xfrm6_policy_fini(void)
  268. {
  269. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  270. }
  271. #ifdef CONFIG_SYSCTL
  272. static struct ctl_table xfrm6_policy_table[] = {
  273. {
  274. .procname = "xfrm6_gc_thresh",
  275. .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
  276. .maxlen = sizeof(int),
  277. .mode = 0644,
  278. .proc_handler = proc_dointvec,
  279. },
  280. { }
  281. };
  282. static int __net_init xfrm6_net_init(struct net *net)
  283. {
  284. struct ctl_table *table;
  285. struct ctl_table_header *hdr;
  286. table = xfrm6_policy_table;
  287. if (!net_eq(net, &init_net)) {
  288. table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
  289. if (!table)
  290. goto err_alloc;
  291. table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
  292. }
  293. hdr = register_net_sysctl(net, "net/ipv6", table);
  294. if (!hdr)
  295. goto err_reg;
  296. net->ipv6.sysctl.xfrm6_hdr = hdr;
  297. return 0;
  298. err_reg:
  299. if (!net_eq(net, &init_net))
  300. kfree(table);
  301. err_alloc:
  302. return -ENOMEM;
  303. }
  304. static void __net_exit xfrm6_net_exit(struct net *net)
  305. {
  306. struct ctl_table *table;
  307. if (!net->ipv6.sysctl.xfrm6_hdr)
  308. return;
  309. table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
  310. unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
  311. if (!net_eq(net, &init_net))
  312. kfree(table);
  313. }
  314. static struct pernet_operations xfrm6_net_ops = {
  315. .init = xfrm6_net_init,
  316. .exit = xfrm6_net_exit,
  317. };
  318. #endif
  319. int __init xfrm6_init(void)
  320. {
  321. int ret;
  322. dst_entries_init(&xfrm6_dst_ops);
  323. ret = xfrm6_policy_init();
  324. if (ret) {
  325. dst_entries_destroy(&xfrm6_dst_ops);
  326. goto out;
  327. }
  328. ret = xfrm6_state_init();
  329. if (ret)
  330. goto out_policy;
  331. ret = xfrm6_protocol_init();
  332. if (ret)
  333. goto out_state;
  334. #ifdef CONFIG_SYSCTL
  335. register_pernet_subsys(&xfrm6_net_ops);
  336. #endif
  337. out:
  338. return ret;
  339. out_state:
  340. xfrm6_state_fini();
  341. out_policy:
  342. xfrm6_policy_fini();
  343. goto out;
  344. }
  345. void xfrm6_fini(void)
  346. {
  347. #ifdef CONFIG_SYSCTL
  348. unregister_pernet_subsys(&xfrm6_net_ops);
  349. #endif
  350. xfrm6_protocol_fini();
  351. xfrm6_policy_fini();
  352. xfrm6_state_fini();
  353. dst_entries_destroy(&xfrm6_dst_ops);
  354. }