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