xfrm6_policy.c 10 KB

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