xfrm6_policy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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(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. dst = xfrm6_dst_lookup(0, NULL, daddr);
  48. if (IS_ERR(dst))
  49. return -EHOSTUNREACH;
  50. ipv6_get_saddr(dst, (struct in6_addr *)&daddr->a6,
  51. (struct in6_addr *)&saddr->a6);
  52. dst_release(dst);
  53. return 0;
  54. }
  55. static struct dst_entry *
  56. __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  57. {
  58. struct dst_entry *dst;
  59. /* Still not clear if we should set fl->fl6_{src,dst}... */
  60. read_lock_bh(&policy->lock);
  61. for (dst = policy->bundles; dst; dst = dst->next) {
  62. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  63. struct in6_addr fl_dst_prefix, fl_src_prefix;
  64. ipv6_addr_prefix(&fl_dst_prefix,
  65. &fl->fl6_dst,
  66. xdst->u.rt6.rt6i_dst.plen);
  67. ipv6_addr_prefix(&fl_src_prefix,
  68. &fl->fl6_src,
  69. xdst->u.rt6.rt6i_src.plen);
  70. if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
  71. ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
  72. xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
  73. (xdst->u.rt6.rt6i_dst.plen != 128 ||
  74. xdst->u.rt6.rt6i_src.plen != 128))) {
  75. dst_clone(dst);
  76. break;
  77. }
  78. }
  79. read_unlock_bh(&policy->lock);
  80. return dst;
  81. }
  82. static int xfrm6_get_tos(struct flowi *fl)
  83. {
  84. return 0;
  85. }
  86. static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  87. int nfheader_len)
  88. {
  89. if (dst->ops->family == AF_INET6) {
  90. struct rt6_info *rt = (struct rt6_info*)dst;
  91. if (rt->rt6i_node)
  92. path->path_cookie = rt->rt6i_node->fn_sernum;
  93. }
  94. path->u.rt6.rt6i_nfheader_len = nfheader_len;
  95. return 0;
  96. }
  97. static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  98. {
  99. struct rt6_info *rt = (struct rt6_info*)xdst->route;
  100. xdst->u.dst.dev = dev;
  101. dev_hold(dev);
  102. xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
  103. if (!xdst->u.rt6.rt6i_idev)
  104. return -ENODEV;
  105. /* Sheit... I remember I did this right. Apparently,
  106. * it was magically lost, so this code needs audit */
  107. xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
  108. RTF_LOCAL);
  109. xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
  110. xdst->u.rt6.rt6i_node = rt->rt6i_node;
  111. if (rt->rt6i_node)
  112. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  113. xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
  114. xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
  115. xdst->u.rt6.rt6i_src = rt->rt6i_src;
  116. return 0;
  117. }
  118. static inline void
  119. _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
  120. {
  121. u16 offset = skb_network_header_len(skb);
  122. struct ipv6hdr *hdr = ipv6_hdr(skb);
  123. struct ipv6_opt_hdr *exthdr;
  124. const unsigned char *nh = skb_network_header(skb);
  125. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  126. memset(fl, 0, sizeof(struct flowi));
  127. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  128. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  129. while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  130. nh = skb_network_header(skb);
  131. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  132. switch (nexthdr) {
  133. case NEXTHDR_ROUTING:
  134. case NEXTHDR_HOP:
  135. case NEXTHDR_DEST:
  136. offset += ipv6_optlen(exthdr);
  137. nexthdr = exthdr->nexthdr;
  138. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  139. break;
  140. case IPPROTO_UDP:
  141. case IPPROTO_UDPLITE:
  142. case IPPROTO_TCP:
  143. case IPPROTO_SCTP:
  144. case IPPROTO_DCCP:
  145. if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
  146. __be16 *ports = (__be16 *)exthdr;
  147. fl->fl_ip_sport = ports[!!reverse];
  148. fl->fl_ip_dport = ports[!reverse];
  149. }
  150. fl->proto = nexthdr;
  151. return;
  152. case IPPROTO_ICMPV6:
  153. if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  154. u8 *icmp = (u8 *)exthdr;
  155. fl->fl_icmp_type = icmp[0];
  156. fl->fl_icmp_code = icmp[1];
  157. }
  158. fl->proto = nexthdr;
  159. return;
  160. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  161. case IPPROTO_MH:
  162. if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  163. struct ip6_mh *mh;
  164. mh = (struct ip6_mh *)exthdr;
  165. fl->fl_mh_type = mh->ip6mh_type;
  166. }
  167. fl->proto = nexthdr;
  168. return;
  169. #endif
  170. /* XXX Why are there these headers? */
  171. case IPPROTO_AH:
  172. case IPPROTO_ESP:
  173. case IPPROTO_COMP:
  174. default:
  175. fl->fl_ipsec_spi = 0;
  176. fl->proto = nexthdr;
  177. return;
  178. }
  179. }
  180. }
  181. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  182. {
  183. xfrm6_policy_afinfo.garbage_collect();
  184. return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
  185. }
  186. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  187. {
  188. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  189. struct dst_entry *path = xdst->route;
  190. path->ops->update_pmtu(path, mtu);
  191. }
  192. static void xfrm6_dst_destroy(struct dst_entry *dst)
  193. {
  194. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  195. if (likely(xdst->u.rt6.rt6i_idev))
  196. in6_dev_put(xdst->u.rt6.rt6i_idev);
  197. xfrm_dst_destroy(xdst);
  198. }
  199. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  200. int unregister)
  201. {
  202. struct xfrm_dst *xdst;
  203. if (!unregister)
  204. return;
  205. xdst = (struct xfrm_dst *)dst;
  206. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  207. struct inet6_dev *loopback_idev =
  208. in6_dev_get(dev->nd_net->loopback_dev);
  209. BUG_ON(!loopback_idev);
  210. do {
  211. in6_dev_put(xdst->u.rt6.rt6i_idev);
  212. xdst->u.rt6.rt6i_idev = loopback_idev;
  213. in6_dev_hold(loopback_idev);
  214. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  215. } while (xdst->u.dst.xfrm);
  216. __in6_dev_put(loopback_idev);
  217. }
  218. xfrm_dst_ifdown(dst, dev);
  219. }
  220. static struct dst_ops xfrm6_dst_ops = {
  221. .family = AF_INET6,
  222. .protocol = __constant_htons(ETH_P_IPV6),
  223. .gc = xfrm6_garbage_collect,
  224. .update_pmtu = xfrm6_update_pmtu,
  225. .destroy = xfrm6_dst_destroy,
  226. .ifdown = xfrm6_dst_ifdown,
  227. .local_out = __ip6_local_out,
  228. .gc_thresh = 1024,
  229. .entry_size = sizeof(struct xfrm_dst),
  230. .entries = ATOMIC_INIT(0),
  231. };
  232. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  233. .family = AF_INET6,
  234. .dst_ops = &xfrm6_dst_ops,
  235. .dst_lookup = xfrm6_dst_lookup,
  236. .get_saddr = xfrm6_get_saddr,
  237. .find_bundle = __xfrm6_find_bundle,
  238. .decode_session = _decode_session6,
  239. .get_tos = xfrm6_get_tos,
  240. .init_path = xfrm6_init_path,
  241. .fill_dst = xfrm6_fill_dst,
  242. };
  243. static int __init xfrm6_policy_init(void)
  244. {
  245. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  246. }
  247. static void xfrm6_policy_fini(void)
  248. {
  249. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  250. }
  251. int __init xfrm6_init(void)
  252. {
  253. int ret;
  254. ret = xfrm6_policy_init();
  255. if (ret)
  256. goto out;
  257. ret = xfrm6_state_init();
  258. if (ret)
  259. goto out_policy;
  260. out:
  261. return ret;
  262. out_policy:
  263. xfrm6_policy_fini();
  264. goto out;
  265. }
  266. void xfrm6_fini(void)
  267. {
  268. //xfrm6_input_fini();
  269. xfrm6_policy_fini();
  270. xfrm6_state_fini();
  271. }