xfrm6_policy.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  23. #include <net/mip6.h>
  24. #endif
  25. static struct dst_ops xfrm6_dst_ops;
  26. static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
  27. static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
  28. xfrm_address_t *daddr)
  29. {
  30. struct flowi fl = {};
  31. struct dst_entry *dst;
  32. int err;
  33. memcpy(&fl.fl6_dst, daddr, sizeof(fl.fl6_dst));
  34. if (saddr)
  35. memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
  36. dst = ip6_route_output(&init_net, NULL, &fl);
  37. err = dst->error;
  38. if (dst->error) {
  39. dst_release(dst);
  40. dst = ERR_PTR(err);
  41. }
  42. return dst;
  43. }
  44. static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
  45. {
  46. struct dst_entry *dst;
  47. struct net_device *dev;
  48. dst = xfrm6_dst_lookup(0, NULL, daddr);
  49. if (IS_ERR(dst))
  50. return -EHOSTUNREACH;
  51. dev = ip6_dst_idev(dst)->dev;
  52. ipv6_dev_get_saddr(dev_net(dev), dev,
  53. (struct in6_addr *)&daddr->a6, 0,
  54. (struct in6_addr *)&saddr->a6);
  55. dst_release(dst);
  56. return 0;
  57. }
  58. static struct dst_entry *
  59. __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  60. {
  61. struct dst_entry *dst;
  62. /* Still not clear if we should set fl->fl6_{src,dst}... */
  63. read_lock_bh(&policy->lock);
  64. for (dst = policy->bundles; dst; dst = dst->next) {
  65. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  66. struct in6_addr fl_dst_prefix, fl_src_prefix;
  67. ipv6_addr_prefix(&fl_dst_prefix,
  68. &fl->fl6_dst,
  69. xdst->u.rt6.rt6i_dst.plen);
  70. ipv6_addr_prefix(&fl_src_prefix,
  71. &fl->fl6_src,
  72. xdst->u.rt6.rt6i_src.plen);
  73. if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
  74. ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
  75. xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
  76. (xdst->u.rt6.rt6i_dst.plen != 128 ||
  77. xdst->u.rt6.rt6i_src.plen != 128))) {
  78. dst_clone(dst);
  79. break;
  80. }
  81. }
  82. read_unlock_bh(&policy->lock);
  83. return dst;
  84. }
  85. static int xfrm6_get_tos(struct flowi *fl)
  86. {
  87. return 0;
  88. }
  89. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  90. int nfheader_len)
  91. {
  92. if (dst->ops->family == AF_INET6) {
  93. struct rt6_info *rt = (struct rt6_info*)dst;
  94. if (rt->rt6i_node)
  95. path->path_cookie = rt->rt6i_node->fn_sernum;
  96. }
  97. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  98. return 0;
  99. }
  100. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  101. {
  102. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  103. xdst->u.dst.dev = dev;
  104. dev_hold(dev);
  105. xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
  106. if (!xdst->u.rt6.rt6i_idev)
  107. return -ENODEV;
  108. /* Sheit... I remember I did this right. Apparently,
  109. * it was magically lost, so this code needs audit */
  110. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  111. RTF_LOCAL);
  112. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  113. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  114. if (rt->rt6i_node)
  115. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  116. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  117. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  118. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  119. return 0;
  120. }
  121. static inline void
  122. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  123. {
  124. int onlyproto = 0;
  125. u16 offset = skb_network_header_len(skb);
  126. struct ipv6hdr *hdr = ipv6_hdr(skb);
  127. struct ipv6_opt_hdr *exthdr;
  128. const unsigned char *nh = skb_network_header(skb);
  129. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  130. memset(fl, 0, sizeof(struct flowi));
  131. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  132. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  133. while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  134. nh = skb_network_header(skb);
  135. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  136. switch (nexthdr) {
  137. case NEXTHDR_FRAGMENT:
  138. onlyproto = 1;
  139. case NEXTHDR_ROUTING:
  140. case NEXTHDR_HOP:
  141. case NEXTHDR_DEST:
  142. offset += ipv6_optlen(exthdr);
  143. nexthdr = exthdr->nexthdr;
  144. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  145. break;
  146. case IPPROTO_UDP:
  147. case IPPROTO_UDPLITE:
  148. case IPPROTO_TCP:
  149. case IPPROTO_SCTP:
  150. case IPPROTO_DCCP:
  151. if (!onlyproto && pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
  152. __be16 *ports = (__be16 *)exthdr;
  153. fl->fl_ip_sport = ports[!!reverse];
  154. fl->fl_ip_dport = ports[!reverse];
  155. }
  156. fl->proto = nexthdr;
  157. return;
  158. case IPPROTO_ICMPV6:
  159. if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  160. u8 *icmp = (u8 *)exthdr;
  161. fl->fl_icmp_type = icmp[0];
  162. fl->fl_icmp_code = icmp[1];
  163. }
  164. fl->proto = nexthdr;
  165. return;
  166. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  167. case IPPROTO_MH:
  168. if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  169. struct ip6_mh *mh;
  170. mh = (struct ip6_mh *)exthdr;
  171. fl->fl_mh_type = mh->ip6mh_type;
  172. }
  173. fl->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. fl->fl_ipsec_spi = 0;
  182. fl->proto = nexthdr;
  183. return;
  184. }
  185. }
  186. }
  187. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  188. {
  189. xfrm6_policy_afinfo.garbage_collect();
  190. return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
  191. }
  192. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  193. {
  194. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  195. struct dst_entry *path = xdst->route;
  196. path->ops->update_pmtu(path, mtu);
  197. }
  198. static void xfrm6_dst_destroy(struct dst_entry *dst)
  199. {
  200. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  201. if (likely(xdst->u.rt6.rt6i_idev))
  202. in6_dev_put(xdst->u.rt6.rt6i_idev);
  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 = __constant_htons(ETH_P_IPV6),
  229. .gc = xfrm6_garbage_collect,
  230. .update_pmtu = xfrm6_update_pmtu,
  231. .destroy = xfrm6_dst_destroy,
  232. .ifdown = xfrm6_dst_ifdown,
  233. .local_out = __ip6_local_out,
  234. .gc_thresh = 1024,
  235. .entries = ATOMIC_INIT(0),
  236. };
  237. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  238. .family = AF_INET6,
  239. .dst_ops = &xfrm6_dst_ops,
  240. .dst_lookup = xfrm6_dst_lookup,
  241. .get_saddr = xfrm6_get_saddr,
  242. .find_bundle = __xfrm6_find_bundle,
  243. .decode_session = _decode_session6,
  244. .get_tos = xfrm6_get_tos,
  245. .init_path = xfrm6_init_path,
  246. .fill_dst = xfrm6_fill_dst,
  247. };
  248. static int __init xfrm6_policy_init(void)
  249. {
  250. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  251. }
  252. static void xfrm6_policy_fini(void)
  253. {
  254. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  255. }
  256. int __init xfrm6_init(void)
  257. {
  258. int ret;
  259. ret = xfrm6_policy_init();
  260. if (ret)
  261. goto out;
  262. ret = xfrm6_state_init();
  263. if (ret)
  264. goto out_policy;
  265. out:
  266. return ret;
  267. out_policy:
  268. xfrm6_policy_fini();
  269. goto out;
  270. }
  271. void xfrm6_fini(void)
  272. {
  273. //xfrm6_input_fini();
  274. xfrm6_policy_fini();
  275. xfrm6_state_fini();
  276. }