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