xfrm6_policy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. u16 offset = skb_network_header_len(skb);
  125. struct ipv6hdr *hdr = ipv6_hdr(skb);
  126. struct ipv6_opt_hdr *exthdr;
  127. const unsigned char *nh = skb_network_header(skb);
  128. u8 nexthdr = nh[IP6CB(skb)->nhoff];
  129. memset(fl, 0, sizeof(struct flowi));
  130. ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
  131. ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
  132. while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
  133. nh = skb_network_header(skb);
  134. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  135. switch (nexthdr) {
  136. case NEXTHDR_ROUTING:
  137. case NEXTHDR_HOP:
  138. case NEXTHDR_DEST:
  139. offset += ipv6_optlen(exthdr);
  140. nexthdr = exthdr->nexthdr;
  141. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  142. break;
  143. case IPPROTO_UDP:
  144. case IPPROTO_UDPLITE:
  145. case IPPROTO_TCP:
  146. case IPPROTO_SCTP:
  147. case IPPROTO_DCCP:
  148. if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
  149. __be16 *ports = (__be16 *)exthdr;
  150. fl->fl_ip_sport = ports[!!reverse];
  151. fl->fl_ip_dport = ports[!reverse];
  152. }
  153. fl->proto = nexthdr;
  154. return;
  155. case IPPROTO_ICMPV6:
  156. if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
  157. u8 *icmp = (u8 *)exthdr;
  158. fl->fl_icmp_type = icmp[0];
  159. fl->fl_icmp_code = icmp[1];
  160. }
  161. fl->proto = nexthdr;
  162. return;
  163. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  164. case IPPROTO_MH:
  165. if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
  166. struct ip6_mh *mh;
  167. mh = (struct ip6_mh *)exthdr;
  168. fl->fl_mh_type = mh->ip6mh_type;
  169. }
  170. fl->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. fl->fl_ipsec_spi = 0;
  179. fl->proto = nexthdr;
  180. return;
  181. }
  182. }
  183. }
  184. static inline int xfrm6_garbage_collect(struct dst_ops *ops)
  185. {
  186. xfrm6_policy_afinfo.garbage_collect();
  187. return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
  188. }
  189. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  190. {
  191. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  192. struct dst_entry *path = xdst->route;
  193. path->ops->update_pmtu(path, mtu);
  194. }
  195. static void xfrm6_dst_destroy(struct dst_entry *dst)
  196. {
  197. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  198. if (likely(xdst->u.rt6.rt6i_idev))
  199. in6_dev_put(xdst->u.rt6.rt6i_idev);
  200. xfrm_dst_destroy(xdst);
  201. }
  202. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  203. int unregister)
  204. {
  205. struct xfrm_dst *xdst;
  206. if (!unregister)
  207. return;
  208. xdst = (struct xfrm_dst *)dst;
  209. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  210. struct inet6_dev *loopback_idev =
  211. in6_dev_get(dev_net(dev)->loopback_dev);
  212. BUG_ON(!loopback_idev);
  213. do {
  214. in6_dev_put(xdst->u.rt6.rt6i_idev);
  215. xdst->u.rt6.rt6i_idev = loopback_idev;
  216. in6_dev_hold(loopback_idev);
  217. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  218. } while (xdst->u.dst.xfrm);
  219. __in6_dev_put(loopback_idev);
  220. }
  221. xfrm_dst_ifdown(dst, dev);
  222. }
  223. static struct dst_ops xfrm6_dst_ops = {
  224. .family = AF_INET6,
  225. .protocol = __constant_htons(ETH_P_IPV6),
  226. .gc = xfrm6_garbage_collect,
  227. .update_pmtu = xfrm6_update_pmtu,
  228. .destroy = xfrm6_dst_destroy,
  229. .ifdown = xfrm6_dst_ifdown,
  230. .local_out = __ip6_local_out,
  231. .gc_thresh = 1024,
  232. .entry_size = sizeof(struct xfrm_dst),
  233. .entries = ATOMIC_INIT(0),
  234. };
  235. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  236. .family = AF_INET6,
  237. .dst_ops = &xfrm6_dst_ops,
  238. .dst_lookup = xfrm6_dst_lookup,
  239. .get_saddr = xfrm6_get_saddr,
  240. .find_bundle = __xfrm6_find_bundle,
  241. .decode_session = _decode_session6,
  242. .get_tos = xfrm6_get_tos,
  243. .init_path = xfrm6_init_path,
  244. .fill_dst = xfrm6_fill_dst,
  245. };
  246. static int __init xfrm6_policy_init(void)
  247. {
  248. return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  249. }
  250. static void xfrm6_policy_fini(void)
  251. {
  252. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  253. }
  254. int __init xfrm6_init(void)
  255. {
  256. int ret;
  257. ret = xfrm6_policy_init();
  258. if (ret)
  259. goto out;
  260. ret = xfrm6_state_init();
  261. if (ret)
  262. goto out_policy;
  263. out:
  264. return ret;
  265. out_policy:
  266. xfrm6_policy_fini();
  267. goto out;
  268. }
  269. void xfrm6_fini(void)
  270. {
  271. //xfrm6_input_fini();
  272. xfrm6_policy_fini();
  273. xfrm6_state_fini();
  274. }