xfrm6_policy.c 7.5 KB

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