xfrm6_policy.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/compiler.h>
  14. #include <linux/netdevice.h>
  15. #include <net/addrconf.h>
  16. #include <net/xfrm.h>
  17. #include <net/ip.h>
  18. #include <net/ipv6.h>
  19. #include <net/ip6_route.h>
  20. static struct dst_ops xfrm6_dst_ops;
  21. static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
  22. static int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
  23. {
  24. int err = 0;
  25. *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
  26. if (!*dst)
  27. err = -ENETUNREACH;
  28. return err;
  29. }
  30. static struct dst_entry *
  31. __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
  32. {
  33. struct dst_entry *dst;
  34. /* Still not clear if we should set fl->fl6_{src,dst}... */
  35. read_lock_bh(&policy->lock);
  36. for (dst = policy->bundles; dst; dst = dst->next) {
  37. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  38. struct in6_addr fl_dst_prefix, fl_src_prefix;
  39. ipv6_addr_prefix(&fl_dst_prefix,
  40. &fl->fl6_dst,
  41. xdst->u.rt6.rt6i_dst.plen);
  42. ipv6_addr_prefix(&fl_src_prefix,
  43. &fl->fl6_src,
  44. xdst->u.rt6.rt6i_src.plen);
  45. if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
  46. ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
  47. xfrm_bundle_ok(xdst, fl, AF_INET6)) {
  48. dst_clone(dst);
  49. break;
  50. }
  51. }
  52. read_unlock_bh(&policy->lock);
  53. return dst;
  54. }
  55. static inline struct in6_addr*
  56. __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
  57. {
  58. return (x->type->remote_addr) ?
  59. (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
  60. (struct in6_addr*)&x->id.daddr;
  61. }
  62. static inline struct in6_addr*
  63. __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
  64. {
  65. return (x->type->local_addr) ?
  66. (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
  67. (struct in6_addr*)&x->props.saddr;
  68. }
  69. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  70. * all the metrics... Shortly, bundle a bundle.
  71. */
  72. static int
  73. __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  74. struct flowi *fl, struct dst_entry **dst_p)
  75. {
  76. struct dst_entry *dst, *dst_prev;
  77. struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
  78. struct rt6_info *rt = rt0;
  79. struct in6_addr *remote = &fl->fl6_dst;
  80. struct in6_addr *local = &fl->fl6_src;
  81. struct flowi fl_tunnel = {
  82. .nl_u = {
  83. .ip6_u = {
  84. .saddr = *local,
  85. .daddr = *remote
  86. }
  87. }
  88. };
  89. int i;
  90. int err = 0;
  91. int header_len = 0;
  92. int trailer_len = 0;
  93. dst = dst_prev = NULL;
  94. dst_hold(&rt->u.dst);
  95. for (i = 0; i < nx; i++) {
  96. struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
  97. struct xfrm_dst *xdst;
  98. int tunnel = 0;
  99. if (unlikely(dst1 == NULL)) {
  100. err = -ENOBUFS;
  101. dst_release(&rt->u.dst);
  102. goto error;
  103. }
  104. if (!dst)
  105. dst = dst1;
  106. else {
  107. dst_prev->child = dst1;
  108. dst1->flags |= DST_NOHASH;
  109. dst_clone(dst1);
  110. }
  111. xdst = (struct xfrm_dst *)dst1;
  112. xdst->route = &rt->u.dst;
  113. if (rt->rt6i_node)
  114. xdst->route_cookie = rt->rt6i_node->fn_sernum;
  115. dst1->next = dst_prev;
  116. dst_prev = dst1;
  117. if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
  118. remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
  119. local = __xfrm6_bundle_addr_local(xfrm[i], local);
  120. tunnel = 1;
  121. }
  122. header_len += xfrm[i]->props.header_len;
  123. trailer_len += xfrm[i]->props.trailer_len;
  124. if (tunnel) {
  125. ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
  126. ipv6_addr_copy(&fl_tunnel.fl6_src, local);
  127. err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
  128. &fl_tunnel, AF_INET6);
  129. if (err)
  130. goto error;
  131. } else
  132. dst_hold(&rt->u.dst);
  133. }
  134. dst_prev->child = &rt->u.dst;
  135. dst->path = &rt->u.dst;
  136. if (rt->rt6i_node)
  137. ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
  138. *dst_p = dst;
  139. dst = dst_prev;
  140. dst_prev = *dst_p;
  141. i = 0;
  142. for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
  143. struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
  144. dst_prev->xfrm = xfrm[i++];
  145. dst_prev->dev = rt->u.dst.dev;
  146. if (rt->u.dst.dev)
  147. dev_hold(rt->u.dst.dev);
  148. dst_prev->obsolete = -1;
  149. dst_prev->flags |= DST_HOST;
  150. dst_prev->lastuse = jiffies;
  151. dst_prev->header_len = header_len;
  152. dst_prev->trailer_len = trailer_len;
  153. memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
  154. /* Copy neighbour for reachability confirmation */
  155. dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
  156. dst_prev->input = rt->u.dst.input;
  157. dst_prev->output = xfrm6_output;
  158. /* Sheit... I remember I did this right. Apparently,
  159. * it was magically lost, so this code needs audit */
  160. x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
  161. x->u.rt6.rt6i_metric = rt0->rt6i_metric;
  162. x->u.rt6.rt6i_node = rt0->rt6i_node;
  163. x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
  164. memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
  165. x->u.rt6.rt6i_dst = rt0->rt6i_dst;
  166. x->u.rt6.rt6i_src = rt0->rt6i_src;
  167. x->u.rt6.rt6i_idev = rt0->rt6i_idev;
  168. in6_dev_hold(rt0->rt6i_idev);
  169. header_len -= x->u.dst.xfrm->props.header_len;
  170. trailer_len -= x->u.dst.xfrm->props.trailer_len;
  171. }
  172. xfrm_init_pmtu(dst);
  173. return 0;
  174. error:
  175. if (dst)
  176. dst_free(dst);
  177. return err;
  178. }
  179. static inline void
  180. _decode_session6(struct sk_buff *skb, struct flowi *fl)
  181. {
  182. u16 offset = skb->h.raw - skb->nh.raw;
  183. struct ipv6hdr *hdr = skb->nh.ipv6h;
  184. struct ipv6_opt_hdr *exthdr;
  185. u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
  186. memset(fl, 0, sizeof(struct flowi));
  187. ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
  188. ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
  189. while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
  190. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  191. switch (nexthdr) {
  192. case NEXTHDR_ROUTING:
  193. case NEXTHDR_HOP:
  194. case NEXTHDR_DEST:
  195. offset += ipv6_optlen(exthdr);
  196. nexthdr = exthdr->nexthdr;
  197. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  198. break;
  199. case IPPROTO_UDP:
  200. case IPPROTO_TCP:
  201. case IPPROTO_SCTP:
  202. case IPPROTO_DCCP:
  203. if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
  204. u16 *ports = (u16 *)exthdr;
  205. fl->fl_ip_sport = ports[0];
  206. fl->fl_ip_dport = ports[1];
  207. }
  208. fl->proto = nexthdr;
  209. return;
  210. case IPPROTO_ICMPV6:
  211. if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
  212. u8 *icmp = (u8 *)exthdr;
  213. fl->fl_icmp_type = icmp[0];
  214. fl->fl_icmp_code = icmp[1];
  215. }
  216. fl->proto = nexthdr;
  217. return;
  218. /* XXX Why are there these headers? */
  219. case IPPROTO_AH:
  220. case IPPROTO_ESP:
  221. case IPPROTO_COMP:
  222. default:
  223. fl->fl_ipsec_spi = 0;
  224. fl->proto = nexthdr;
  225. return;
  226. };
  227. }
  228. }
  229. static inline int xfrm6_garbage_collect(void)
  230. {
  231. xfrm6_policy_afinfo.garbage_collect();
  232. return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
  233. }
  234. static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
  235. {
  236. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  237. struct dst_entry *path = xdst->route;
  238. path->ops->update_pmtu(path, mtu);
  239. }
  240. static void xfrm6_dst_destroy(struct dst_entry *dst)
  241. {
  242. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  243. if (likely(xdst->u.rt6.rt6i_idev))
  244. in6_dev_put(xdst->u.rt6.rt6i_idev);
  245. xfrm_dst_destroy(xdst);
  246. }
  247. static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  248. int unregister)
  249. {
  250. struct xfrm_dst *xdst;
  251. if (!unregister)
  252. return;
  253. xdst = (struct xfrm_dst *)dst;
  254. if (xdst->u.rt6.rt6i_idev->dev == dev) {
  255. struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
  256. BUG_ON(!loopback_idev);
  257. do {
  258. in6_dev_put(xdst->u.rt6.rt6i_idev);
  259. xdst->u.rt6.rt6i_idev = loopback_idev;
  260. in6_dev_hold(loopback_idev);
  261. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  262. } while (xdst->u.dst.xfrm);
  263. __in6_dev_put(loopback_idev);
  264. }
  265. xfrm_dst_ifdown(dst, dev);
  266. }
  267. static struct dst_ops xfrm6_dst_ops = {
  268. .family = AF_INET6,
  269. .protocol = __constant_htons(ETH_P_IPV6),
  270. .gc = xfrm6_garbage_collect,
  271. .update_pmtu = xfrm6_update_pmtu,
  272. .destroy = xfrm6_dst_destroy,
  273. .ifdown = xfrm6_dst_ifdown,
  274. .gc_thresh = 1024,
  275. .entry_size = sizeof(struct xfrm_dst),
  276. };
  277. static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
  278. .family = AF_INET6,
  279. .dst_ops = &xfrm6_dst_ops,
  280. .dst_lookup = xfrm6_dst_lookup,
  281. .find_bundle = __xfrm6_find_bundle,
  282. .bundle_create = __xfrm6_bundle_create,
  283. .decode_session = _decode_session6,
  284. };
  285. static void __init xfrm6_policy_init(void)
  286. {
  287. xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
  288. }
  289. static void xfrm6_policy_fini(void)
  290. {
  291. xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
  292. }
  293. void __init xfrm6_init(void)
  294. {
  295. xfrm6_policy_init();
  296. xfrm6_state_init();
  297. }
  298. void xfrm6_fini(void)
  299. {
  300. //xfrm6_input_fini();
  301. xfrm6_policy_fini();
  302. xfrm6_state_fini();
  303. }