xfrm6_policy.c 7.2 KB

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