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