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