| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT) | 
|  | 3 | *	Linux INET6 implementation | 
|  | 4 | * | 
|  | 5 | *	Authors: | 
|  | 6 | *	Pedro Roque		<roque@di.fc.ul.pt> | 
|  | 7 | *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru> | 
|  | 8 | * | 
|  | 9 | *	$Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $ | 
|  | 10 | * | 
|  | 11 | *	This program is free software; you can redistribute it and/or | 
|  | 12 | *      modify it under the terms of the GNU General Public License | 
|  | 13 | *      as published by the Free Software Foundation; either version | 
|  | 14 | *      2 of the License, or (at your option) any later version. | 
|  | 15 | * | 
|  | 16 | *	Changes: | 
|  | 17 | * Roger Venning <r.venning@telstra.com>:	6to4 support | 
|  | 18 | * Nate Thompson <nate@thebog.net>:		6to4 support | 
|  | 19 | */ | 
|  | 20 |  | 
|  | 21 | #include <linux/config.h> | 
|  | 22 | #include <linux/module.h> | 
|  | 23 | #include <linux/errno.h> | 
|  | 24 | #include <linux/types.h> | 
|  | 25 | #include <linux/socket.h> | 
|  | 26 | #include <linux/sockios.h> | 
|  | 27 | #include <linux/sched.h> | 
|  | 28 | #include <linux/net.h> | 
|  | 29 | #include <linux/in6.h> | 
|  | 30 | #include <linux/netdevice.h> | 
|  | 31 | #include <linux/if_arp.h> | 
|  | 32 | #include <linux/icmp.h> | 
|  | 33 | #include <asm/uaccess.h> | 
|  | 34 | #include <linux/init.h> | 
|  | 35 | #include <linux/netfilter_ipv4.h> | 
|  | 36 |  | 
|  | 37 | #include <net/sock.h> | 
|  | 38 | #include <net/snmp.h> | 
|  | 39 |  | 
|  | 40 | #include <net/ipv6.h> | 
|  | 41 | #include <net/protocol.h> | 
|  | 42 | #include <net/transp_v6.h> | 
|  | 43 | #include <net/ip6_fib.h> | 
|  | 44 | #include <net/ip6_route.h> | 
|  | 45 | #include <net/ndisc.h> | 
|  | 46 | #include <net/addrconf.h> | 
|  | 47 | #include <net/ip.h> | 
|  | 48 | #include <net/udp.h> | 
|  | 49 | #include <net/icmp.h> | 
|  | 50 | #include <net/ipip.h> | 
|  | 51 | #include <net/inet_ecn.h> | 
|  | 52 | #include <net/xfrm.h> | 
|  | 53 | #include <net/dsfield.h> | 
|  | 54 |  | 
|  | 55 | /* | 
|  | 56 | This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c | 
|  | 57 |  | 
|  | 58 | For comments look at net/ipv4/ip_gre.c --ANK | 
|  | 59 | */ | 
|  | 60 |  | 
|  | 61 | #define HASH_SIZE  16 | 
|  | 62 | #define HASH(addr) ((addr^(addr>>4))&0xF) | 
|  | 63 |  | 
|  | 64 | static int ipip6_fb_tunnel_init(struct net_device *dev); | 
|  | 65 | static int ipip6_tunnel_init(struct net_device *dev); | 
|  | 66 | static void ipip6_tunnel_setup(struct net_device *dev); | 
|  | 67 |  | 
|  | 68 | static struct net_device *ipip6_fb_tunnel_dev; | 
|  | 69 |  | 
|  | 70 | static struct ip_tunnel *tunnels_r_l[HASH_SIZE]; | 
|  | 71 | static struct ip_tunnel *tunnels_r[HASH_SIZE]; | 
|  | 72 | static struct ip_tunnel *tunnels_l[HASH_SIZE]; | 
|  | 73 | static struct ip_tunnel *tunnels_wc[1]; | 
|  | 74 | static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l }; | 
|  | 75 |  | 
|  | 76 | static DEFINE_RWLOCK(ipip6_lock); | 
|  | 77 |  | 
|  | 78 | static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local) | 
|  | 79 | { | 
|  | 80 | unsigned h0 = HASH(remote); | 
|  | 81 | unsigned h1 = HASH(local); | 
|  | 82 | struct ip_tunnel *t; | 
|  | 83 |  | 
|  | 84 | for (t = tunnels_r_l[h0^h1]; t; t = t->next) { | 
|  | 85 | if (local == t->parms.iph.saddr && | 
|  | 86 | remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | 
|  | 87 | return t; | 
|  | 88 | } | 
|  | 89 | for (t = tunnels_r[h0]; t; t = t->next) { | 
|  | 90 | if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | 
|  | 91 | return t; | 
|  | 92 | } | 
|  | 93 | for (t = tunnels_l[h1]; t; t = t->next) { | 
|  | 94 | if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP)) | 
|  | 95 | return t; | 
|  | 96 | } | 
|  | 97 | if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP)) | 
|  | 98 | return t; | 
|  | 99 | return NULL; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t) | 
|  | 103 | { | 
|  | 104 | u32 remote = t->parms.iph.daddr; | 
|  | 105 | u32 local = t->parms.iph.saddr; | 
|  | 106 | unsigned h = 0; | 
|  | 107 | int prio = 0; | 
|  | 108 |  | 
|  | 109 | if (remote) { | 
|  | 110 | prio |= 2; | 
|  | 111 | h ^= HASH(remote); | 
|  | 112 | } | 
|  | 113 | if (local) { | 
|  | 114 | prio |= 1; | 
|  | 115 | h ^= HASH(local); | 
|  | 116 | } | 
|  | 117 | return &tunnels[prio][h]; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | static void ipip6_tunnel_unlink(struct ip_tunnel *t) | 
|  | 121 | { | 
|  | 122 | struct ip_tunnel **tp; | 
|  | 123 |  | 
|  | 124 | for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) { | 
|  | 125 | if (t == *tp) { | 
|  | 126 | write_lock_bh(&ipip6_lock); | 
|  | 127 | *tp = t->next; | 
|  | 128 | write_unlock_bh(&ipip6_lock); | 
|  | 129 | break; | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | static void ipip6_tunnel_link(struct ip_tunnel *t) | 
|  | 135 | { | 
|  | 136 | struct ip_tunnel **tp = ipip6_bucket(t); | 
|  | 137 |  | 
|  | 138 | t->next = *tp; | 
|  | 139 | write_lock_bh(&ipip6_lock); | 
|  | 140 | *tp = t; | 
|  | 141 | write_unlock_bh(&ipip6_lock); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create) | 
|  | 145 | { | 
|  | 146 | u32 remote = parms->iph.daddr; | 
|  | 147 | u32 local = parms->iph.saddr; | 
|  | 148 | struct ip_tunnel *t, **tp, *nt; | 
|  | 149 | struct net_device *dev; | 
|  | 150 | unsigned h = 0; | 
|  | 151 | int prio = 0; | 
|  | 152 | char name[IFNAMSIZ]; | 
|  | 153 |  | 
|  | 154 | if (remote) { | 
|  | 155 | prio |= 2; | 
|  | 156 | h ^= HASH(remote); | 
|  | 157 | } | 
|  | 158 | if (local) { | 
|  | 159 | prio |= 1; | 
|  | 160 | h ^= HASH(local); | 
|  | 161 | } | 
|  | 162 | for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) { | 
|  | 163 | if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) | 
|  | 164 | return t; | 
|  | 165 | } | 
|  | 166 | if (!create) | 
|  | 167 | goto failed; | 
|  | 168 |  | 
|  | 169 | if (parms->name[0]) | 
|  | 170 | strlcpy(name, parms->name, IFNAMSIZ); | 
|  | 171 | else { | 
|  | 172 | int i; | 
|  | 173 | for (i=1; i<100; i++) { | 
|  | 174 | sprintf(name, "sit%d", i); | 
|  | 175 | if (__dev_get_by_name(name) == NULL) | 
|  | 176 | break; | 
|  | 177 | } | 
|  | 178 | if (i==100) | 
|  | 179 | goto failed; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup); | 
|  | 183 | if (dev == NULL) | 
|  | 184 | return NULL; | 
|  | 185 |  | 
|  | 186 | nt = dev->priv; | 
|  | 187 | dev->init = ipip6_tunnel_init; | 
|  | 188 | nt->parms = *parms; | 
|  | 189 |  | 
|  | 190 | if (register_netdevice(dev) < 0) { | 
|  | 191 | free_netdev(dev); | 
|  | 192 | goto failed; | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | dev_hold(dev); | 
|  | 196 |  | 
|  | 197 | ipip6_tunnel_link(nt); | 
|  | 198 | /* Do not decrement MOD_USE_COUNT here. */ | 
|  | 199 | return nt; | 
|  | 200 |  | 
|  | 201 | failed: | 
|  | 202 | return NULL; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | static void ipip6_tunnel_uninit(struct net_device *dev) | 
|  | 206 | { | 
|  | 207 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 208 | write_lock_bh(&ipip6_lock); | 
|  | 209 | tunnels_wc[0] = NULL; | 
|  | 210 | write_unlock_bh(&ipip6_lock); | 
|  | 211 | dev_put(dev); | 
|  | 212 | } else { | 
|  | 213 | ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv); | 
|  | 214 | dev_put(dev); | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 |  | 
|  | 219 | static void ipip6_err(struct sk_buff *skb, u32 info) | 
|  | 220 | { | 
|  | 221 | #ifndef I_WISH_WORLD_WERE_PERFECT | 
|  | 222 |  | 
|  | 223 | /* It is not :-( All the routers (except for Linux) return only | 
|  | 224 | 8 bytes of packet payload. It means, that precise relaying of | 
|  | 225 | ICMP in the real Internet is absolutely infeasible. | 
|  | 226 | */ | 
|  | 227 | struct iphdr *iph = (struct iphdr*)skb->data; | 
|  | 228 | int type = skb->h.icmph->type; | 
|  | 229 | int code = skb->h.icmph->code; | 
|  | 230 | struct ip_tunnel *t; | 
|  | 231 |  | 
|  | 232 | switch (type) { | 
|  | 233 | default: | 
|  | 234 | case ICMP_PARAMETERPROB: | 
|  | 235 | return; | 
|  | 236 |  | 
|  | 237 | case ICMP_DEST_UNREACH: | 
|  | 238 | switch (code) { | 
|  | 239 | case ICMP_SR_FAILED: | 
|  | 240 | case ICMP_PORT_UNREACH: | 
|  | 241 | /* Impossible event. */ | 
|  | 242 | return; | 
|  | 243 | case ICMP_FRAG_NEEDED: | 
|  | 244 | /* Soft state for pmtu is maintained by IP core. */ | 
|  | 245 | return; | 
|  | 246 | default: | 
|  | 247 | /* All others are translated to HOST_UNREACH. | 
|  | 248 | rfc2003 contains "deep thoughts" about NET_UNREACH, | 
|  | 249 | I believe they are just ether pollution. --ANK | 
|  | 250 | */ | 
|  | 251 | break; | 
|  | 252 | } | 
|  | 253 | break; | 
|  | 254 | case ICMP_TIME_EXCEEDED: | 
|  | 255 | if (code != ICMP_EXC_TTL) | 
|  | 256 | return; | 
|  | 257 | break; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | read_lock(&ipip6_lock); | 
|  | 261 | t = ipip6_tunnel_lookup(iph->daddr, iph->saddr); | 
|  | 262 | if (t == NULL || t->parms.iph.daddr == 0) | 
|  | 263 | goto out; | 
|  | 264 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) | 
|  | 265 | goto out; | 
|  | 266 |  | 
|  | 267 | if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO) | 
|  | 268 | t->err_count++; | 
|  | 269 | else | 
|  | 270 | t->err_count = 1; | 
|  | 271 | t->err_time = jiffies; | 
|  | 272 | out: | 
|  | 273 | read_unlock(&ipip6_lock); | 
|  | 274 | return; | 
|  | 275 | #else | 
|  | 276 | struct iphdr *iph = (struct iphdr*)dp; | 
|  | 277 | int hlen = iph->ihl<<2; | 
|  | 278 | struct ipv6hdr *iph6; | 
|  | 279 | int type = skb->h.icmph->type; | 
|  | 280 | int code = skb->h.icmph->code; | 
|  | 281 | int rel_type = 0; | 
|  | 282 | int rel_code = 0; | 
|  | 283 | int rel_info = 0; | 
|  | 284 | struct sk_buff *skb2; | 
|  | 285 | struct rt6_info *rt6i; | 
|  | 286 |  | 
|  | 287 | if (len < hlen + sizeof(struct ipv6hdr)) | 
|  | 288 | return; | 
|  | 289 | iph6 = (struct ipv6hdr*)(dp + hlen); | 
|  | 290 |  | 
|  | 291 | switch (type) { | 
|  | 292 | default: | 
|  | 293 | return; | 
|  | 294 | case ICMP_PARAMETERPROB: | 
|  | 295 | if (skb->h.icmph->un.gateway < hlen) | 
|  | 296 | return; | 
|  | 297 |  | 
|  | 298 | /* So... This guy found something strange INSIDE encapsulated | 
|  | 299 | packet. Well, he is fool, but what can we do ? | 
|  | 300 | */ | 
|  | 301 | rel_type = ICMPV6_PARAMPROB; | 
|  | 302 | rel_info = skb->h.icmph->un.gateway - hlen; | 
|  | 303 | break; | 
|  | 304 |  | 
|  | 305 | case ICMP_DEST_UNREACH: | 
|  | 306 | switch (code) { | 
|  | 307 | case ICMP_SR_FAILED: | 
|  | 308 | case ICMP_PORT_UNREACH: | 
|  | 309 | /* Impossible event. */ | 
|  | 310 | return; | 
|  | 311 | case ICMP_FRAG_NEEDED: | 
|  | 312 | /* Too complicated case ... */ | 
|  | 313 | return; | 
|  | 314 | default: | 
|  | 315 | /* All others are translated to HOST_UNREACH. | 
|  | 316 | rfc2003 contains "deep thoughts" about NET_UNREACH, | 
|  | 317 | I believe, it is just ether pollution. --ANK | 
|  | 318 | */ | 
|  | 319 | rel_type = ICMPV6_DEST_UNREACH; | 
|  | 320 | rel_code = ICMPV6_ADDR_UNREACH; | 
|  | 321 | break; | 
|  | 322 | } | 
|  | 323 | break; | 
|  | 324 | case ICMP_TIME_EXCEEDED: | 
|  | 325 | if (code != ICMP_EXC_TTL) | 
|  | 326 | return; | 
|  | 327 | rel_type = ICMPV6_TIME_EXCEED; | 
|  | 328 | rel_code = ICMPV6_EXC_HOPLIMIT; | 
|  | 329 | break; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | /* Prepare fake skb to feed it to icmpv6_send */ | 
|  | 333 | skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 334 | if (skb2 == NULL) | 
|  | 335 | return; | 
|  | 336 | dst_release(skb2->dst); | 
|  | 337 | skb2->dst = NULL; | 
|  | 338 | skb_pull(skb2, skb->data - (u8*)iph6); | 
|  | 339 | skb2->nh.raw = skb2->data; | 
|  | 340 |  | 
|  | 341 | /* Try to guess incoming interface */ | 
|  | 342 | rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0); | 
|  | 343 | if (rt6i && rt6i->rt6i_dev) { | 
|  | 344 | skb2->dev = rt6i->rt6i_dev; | 
|  | 345 |  | 
|  | 346 | rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0); | 
|  | 347 |  | 
|  | 348 | if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) { | 
|  | 349 | struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv; | 
|  | 350 | if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) { | 
|  | 351 | rel_type = ICMPV6_DEST_UNREACH; | 
|  | 352 | rel_code = ICMPV6_ADDR_UNREACH; | 
|  | 353 | } | 
|  | 354 | icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 | kfree_skb(skb2); | 
|  | 358 | return; | 
|  | 359 | #endif | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb) | 
|  | 363 | { | 
|  | 364 | if (INET_ECN_is_ce(iph->tos)) | 
|  | 365 | IP6_ECN_set_ce(skb->nh.ipv6h); | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | static int ipip6_rcv(struct sk_buff *skb) | 
|  | 369 | { | 
|  | 370 | struct iphdr *iph; | 
|  | 371 | struct ip_tunnel *tunnel; | 
|  | 372 |  | 
|  | 373 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | 
|  | 374 | goto out; | 
|  | 375 |  | 
|  | 376 | iph = skb->nh.iph; | 
|  | 377 |  | 
|  | 378 | read_lock(&ipip6_lock); | 
|  | 379 | if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) { | 
|  | 380 | secpath_reset(skb); | 
|  | 381 | skb->mac.raw = skb->nh.raw; | 
|  | 382 | skb->nh.raw = skb->data; | 
|  | 383 | memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); | 
|  | 384 | skb->protocol = htons(ETH_P_IPV6); | 
|  | 385 | skb->pkt_type = PACKET_HOST; | 
|  | 386 | tunnel->stat.rx_packets++; | 
|  | 387 | tunnel->stat.rx_bytes += skb->len; | 
|  | 388 | skb->dev = tunnel->dev; | 
|  | 389 | dst_release(skb->dst); | 
|  | 390 | skb->dst = NULL; | 
|  | 391 | nf_reset(skb); | 
|  | 392 | ipip6_ecn_decapsulate(iph, skb); | 
|  | 393 | netif_rx(skb); | 
|  | 394 | read_unlock(&ipip6_lock); | 
|  | 395 | return 0; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0); | 
|  | 399 | kfree_skb(skb); | 
|  | 400 | read_unlock(&ipip6_lock); | 
|  | 401 | out: | 
|  | 402 | return 0; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | /* Returns the embedded IPv4 address if the IPv6 address | 
|  | 406 | comes from 6to4 (RFC 3056) addr space */ | 
|  | 407 |  | 
|  | 408 | static inline u32 try_6to4(struct in6_addr *v6dst) | 
|  | 409 | { | 
|  | 410 | u32 dst = 0; | 
|  | 411 |  | 
|  | 412 | if (v6dst->s6_addr16[0] == htons(0x2002)) { | 
|  | 413 | /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */ | 
|  | 414 | memcpy(&dst, &v6dst->s6_addr16[1], 4); | 
|  | 415 | } | 
|  | 416 | return dst; | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | /* | 
|  | 420 | *	This function assumes it is being called from dev_queue_xmit() | 
|  | 421 | *	and that skb is filled properly by that function. | 
|  | 422 | */ | 
|  | 423 |  | 
|  | 424 | static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) | 
|  | 425 | { | 
|  | 426 | struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv; | 
|  | 427 | struct net_device_stats *stats = &tunnel->stat; | 
|  | 428 | struct iphdr  *tiph = &tunnel->parms.iph; | 
|  | 429 | struct ipv6hdr *iph6 = skb->nh.ipv6h; | 
|  | 430 | u8     tos = tunnel->parms.iph.tos; | 
|  | 431 | struct rtable *rt;     			/* Route to the other host */ | 
|  | 432 | struct net_device *tdev;			/* Device to other host */ | 
|  | 433 | struct iphdr  *iph;			/* Our new IP header */ | 
|  | 434 | int    max_headroom;			/* The extra header space needed */ | 
|  | 435 | u32    dst = tiph->daddr; | 
|  | 436 | int    mtu; | 
|  | 437 | struct in6_addr *addr6; | 
|  | 438 | int addr_type; | 
|  | 439 |  | 
|  | 440 | if (tunnel->recursion++) { | 
|  | 441 | tunnel->stat.collisions++; | 
|  | 442 | goto tx_error; | 
|  | 443 | } | 
|  | 444 |  | 
|  | 445 | if (skb->protocol != htons(ETH_P_IPV6)) | 
|  | 446 | goto tx_error; | 
|  | 447 |  | 
|  | 448 | if (!dst) | 
|  | 449 | dst = try_6to4(&iph6->daddr); | 
|  | 450 |  | 
|  | 451 | if (!dst) { | 
|  | 452 | struct neighbour *neigh = NULL; | 
|  | 453 |  | 
|  | 454 | if (skb->dst) | 
|  | 455 | neigh = skb->dst->neighbour; | 
|  | 456 |  | 
|  | 457 | if (neigh == NULL) { | 
|  | 458 | if (net_ratelimit()) | 
|  | 459 | printk(KERN_DEBUG "sit: nexthop == NULL\n"); | 
|  | 460 | goto tx_error; | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | addr6 = (struct in6_addr*)&neigh->primary_key; | 
|  | 464 | addr_type = ipv6_addr_type(addr6); | 
|  | 465 |  | 
|  | 466 | if (addr_type == IPV6_ADDR_ANY) { | 
|  | 467 | addr6 = &skb->nh.ipv6h->daddr; | 
|  | 468 | addr_type = ipv6_addr_type(addr6); | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | if ((addr_type & IPV6_ADDR_COMPATv4) == 0) | 
|  | 472 | goto tx_error_icmp; | 
|  | 473 |  | 
|  | 474 | dst = addr6->s6_addr32[3]; | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | { | 
|  | 478 | struct flowi fl = { .nl_u = { .ip4_u = | 
|  | 479 | { .daddr = dst, | 
|  | 480 | .saddr = tiph->saddr, | 
|  | 481 | .tos = RT_TOS(tos) } }, | 
|  | 482 | .oif = tunnel->parms.link, | 
|  | 483 | .proto = IPPROTO_IPV6 }; | 
|  | 484 | if (ip_route_output_key(&rt, &fl)) { | 
|  | 485 | tunnel->stat.tx_carrier_errors++; | 
|  | 486 | goto tx_error_icmp; | 
|  | 487 | } | 
|  | 488 | } | 
|  | 489 | if (rt->rt_type != RTN_UNICAST) { | 
|  | 490 | ip_rt_put(rt); | 
|  | 491 | tunnel->stat.tx_carrier_errors++; | 
|  | 492 | goto tx_error_icmp; | 
|  | 493 | } | 
|  | 494 | tdev = rt->u.dst.dev; | 
|  | 495 |  | 
|  | 496 | if (tdev == dev) { | 
|  | 497 | ip_rt_put(rt); | 
|  | 498 | tunnel->stat.collisions++; | 
|  | 499 | goto tx_error; | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | if (tiph->frag_off) | 
|  | 503 | mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr); | 
|  | 504 | else | 
|  | 505 | mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu; | 
|  | 506 |  | 
|  | 507 | if (mtu < 68) { | 
|  | 508 | tunnel->stat.collisions++; | 
|  | 509 | ip_rt_put(rt); | 
|  | 510 | goto tx_error; | 
|  | 511 | } | 
|  | 512 | if (mtu < IPV6_MIN_MTU) | 
|  | 513 | mtu = IPV6_MIN_MTU; | 
|  | 514 | if (tunnel->parms.iph.daddr && skb->dst) | 
|  | 515 | skb->dst->ops->update_pmtu(skb->dst, mtu); | 
|  | 516 |  | 
|  | 517 | if (skb->len > mtu) { | 
|  | 518 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); | 
|  | 519 | ip_rt_put(rt); | 
|  | 520 | goto tx_error; | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | if (tunnel->err_count > 0) { | 
|  | 524 | if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) { | 
|  | 525 | tunnel->err_count--; | 
|  | 526 | dst_link_failure(skb); | 
|  | 527 | } else | 
|  | 528 | tunnel->err_count = 0; | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | /* | 
|  | 532 | * Okay, now see if we can stuff it in the buffer as-is. | 
|  | 533 | */ | 
|  | 534 | max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr); | 
|  | 535 |  | 
|  | 536 | if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) { | 
|  | 537 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); | 
|  | 538 | if (!new_skb) { | 
|  | 539 | ip_rt_put(rt); | 
|  | 540 | stats->tx_dropped++; | 
|  | 541 | dev_kfree_skb(skb); | 
|  | 542 | tunnel->recursion--; | 
|  | 543 | return 0; | 
|  | 544 | } | 
|  | 545 | if (skb->sk) | 
|  | 546 | skb_set_owner_w(new_skb, skb->sk); | 
|  | 547 | dev_kfree_skb(skb); | 
|  | 548 | skb = new_skb; | 
|  | 549 | iph6 = skb->nh.ipv6h; | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | skb->h.raw = skb->nh.raw; | 
|  | 553 | skb->nh.raw = skb_push(skb, sizeof(struct iphdr)); | 
|  | 554 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | 
|  | 555 | dst_release(skb->dst); | 
|  | 556 | skb->dst = &rt->u.dst; | 
|  | 557 |  | 
|  | 558 | /* | 
|  | 559 | *	Push down and install the IPIP header. | 
|  | 560 | */ | 
|  | 561 |  | 
|  | 562 | iph 			=	skb->nh.iph; | 
|  | 563 | iph->version		=	4; | 
|  | 564 | iph->ihl		=	sizeof(struct iphdr)>>2; | 
|  | 565 | if (mtu > IPV6_MIN_MTU) | 
|  | 566 | iph->frag_off	=	htons(IP_DF); | 
|  | 567 | else | 
|  | 568 | iph->frag_off	=	0; | 
|  | 569 |  | 
|  | 570 | iph->protocol		=	IPPROTO_IPV6; | 
|  | 571 | iph->tos		=	INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); | 
|  | 572 | iph->daddr		=	rt->rt_dst; | 
|  | 573 | iph->saddr		=	rt->rt_src; | 
|  | 574 |  | 
|  | 575 | if ((iph->ttl = tiph->ttl) == 0) | 
|  | 576 | iph->ttl	=	iph6->hop_limit; | 
|  | 577 |  | 
|  | 578 | nf_reset(skb); | 
|  | 579 |  | 
|  | 580 | IPTUNNEL_XMIT(); | 
|  | 581 | tunnel->recursion--; | 
|  | 582 | return 0; | 
|  | 583 |  | 
|  | 584 | tx_error_icmp: | 
|  | 585 | dst_link_failure(skb); | 
|  | 586 | tx_error: | 
|  | 587 | stats->tx_errors++; | 
|  | 588 | dev_kfree_skb(skb); | 
|  | 589 | tunnel->recursion--; | 
|  | 590 | return 0; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | static int | 
|  | 594 | ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | 
|  | 595 | { | 
|  | 596 | int err = 0; | 
|  | 597 | struct ip_tunnel_parm p; | 
|  | 598 | struct ip_tunnel *t; | 
|  | 599 |  | 
|  | 600 | switch (cmd) { | 
|  | 601 | case SIOCGETTUNNEL: | 
|  | 602 | t = NULL; | 
|  | 603 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 604 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | 
|  | 605 | err = -EFAULT; | 
|  | 606 | break; | 
|  | 607 | } | 
|  | 608 | t = ipip6_tunnel_locate(&p, 0); | 
|  | 609 | } | 
|  | 610 | if (t == NULL) | 
|  | 611 | t = (struct ip_tunnel*)dev->priv; | 
|  | 612 | memcpy(&p, &t->parms, sizeof(p)); | 
|  | 613 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | 
|  | 614 | err = -EFAULT; | 
|  | 615 | break; | 
|  | 616 |  | 
|  | 617 | case SIOCADDTUNNEL: | 
|  | 618 | case SIOCCHGTUNNEL: | 
|  | 619 | err = -EPERM; | 
|  | 620 | if (!capable(CAP_NET_ADMIN)) | 
|  | 621 | goto done; | 
|  | 622 |  | 
|  | 623 | err = -EFAULT; | 
|  | 624 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | 
|  | 625 | goto done; | 
|  | 626 |  | 
|  | 627 | err = -EINVAL; | 
|  | 628 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 || | 
|  | 629 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF))) | 
|  | 630 | goto done; | 
|  | 631 | if (p.iph.ttl) | 
|  | 632 | p.iph.frag_off |= htons(IP_DF); | 
|  | 633 |  | 
|  | 634 | t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL); | 
|  | 635 |  | 
|  | 636 | if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | 
|  | 637 | if (t != NULL) { | 
|  | 638 | if (t->dev != dev) { | 
|  | 639 | err = -EEXIST; | 
|  | 640 | break; | 
|  | 641 | } | 
|  | 642 | } else { | 
|  | 643 | if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) || | 
|  | 644 | (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) { | 
|  | 645 | err = -EINVAL; | 
|  | 646 | break; | 
|  | 647 | } | 
|  | 648 | t = (struct ip_tunnel*)dev->priv; | 
|  | 649 | ipip6_tunnel_unlink(t); | 
|  | 650 | t->parms.iph.saddr = p.iph.saddr; | 
|  | 651 | t->parms.iph.daddr = p.iph.daddr; | 
|  | 652 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | 
|  | 653 | memcpy(dev->broadcast, &p.iph.daddr, 4); | 
|  | 654 | ipip6_tunnel_link(t); | 
|  | 655 | netdev_state_change(dev); | 
|  | 656 | } | 
|  | 657 | } | 
|  | 658 |  | 
|  | 659 | if (t) { | 
|  | 660 | err = 0; | 
|  | 661 | if (cmd == SIOCCHGTUNNEL) { | 
|  | 662 | t->parms.iph.ttl = p.iph.ttl; | 
|  | 663 | t->parms.iph.tos = p.iph.tos; | 
|  | 664 | } | 
|  | 665 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p))) | 
|  | 666 | err = -EFAULT; | 
|  | 667 | } else | 
|  | 668 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | 
|  | 669 | break; | 
|  | 670 |  | 
|  | 671 | case SIOCDELTUNNEL: | 
|  | 672 | err = -EPERM; | 
|  | 673 | if (!capable(CAP_NET_ADMIN)) | 
|  | 674 | goto done; | 
|  | 675 |  | 
|  | 676 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 677 | err = -EFAULT; | 
|  | 678 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | 
|  | 679 | goto done; | 
|  | 680 | err = -ENOENT; | 
|  | 681 | if ((t = ipip6_tunnel_locate(&p, 0)) == NULL) | 
|  | 682 | goto done; | 
|  | 683 | err = -EPERM; | 
|  | 684 | if (t == ipip6_fb_tunnel_dev->priv) | 
|  | 685 | goto done; | 
|  | 686 | dev = t->dev; | 
|  | 687 | } | 
|  | 688 | err = unregister_netdevice(dev); | 
|  | 689 | break; | 
|  | 690 |  | 
|  | 691 | default: | 
|  | 692 | err = -EINVAL; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | done: | 
|  | 696 | return err; | 
|  | 697 | } | 
|  | 698 |  | 
|  | 699 | static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev) | 
|  | 700 | { | 
|  | 701 | return &(((struct ip_tunnel*)dev->priv)->stat); | 
|  | 702 | } | 
|  | 703 |  | 
|  | 704 | static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu) | 
|  | 705 | { | 
|  | 706 | if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr)) | 
|  | 707 | return -EINVAL; | 
|  | 708 | dev->mtu = new_mtu; | 
|  | 709 | return 0; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | static void ipip6_tunnel_setup(struct net_device *dev) | 
|  | 713 | { | 
|  | 714 | SET_MODULE_OWNER(dev); | 
|  | 715 | dev->uninit		= ipip6_tunnel_uninit; | 
|  | 716 | dev->destructor 	= free_netdev; | 
|  | 717 | dev->hard_start_xmit	= ipip6_tunnel_xmit; | 
|  | 718 | dev->get_stats		= ipip6_tunnel_get_stats; | 
|  | 719 | dev->do_ioctl		= ipip6_tunnel_ioctl; | 
|  | 720 | dev->change_mtu		= ipip6_tunnel_change_mtu; | 
|  | 721 |  | 
|  | 722 | dev->type		= ARPHRD_SIT; | 
|  | 723 | dev->hard_header_len 	= LL_MAX_HEADER + sizeof(struct iphdr); | 
|  | 724 | dev->mtu		= 1500 - sizeof(struct iphdr); | 
|  | 725 | dev->flags		= IFF_NOARP; | 
|  | 726 | dev->iflink		= 0; | 
|  | 727 | dev->addr_len		= 4; | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | static int ipip6_tunnel_init(struct net_device *dev) | 
|  | 731 | { | 
|  | 732 | struct net_device *tdev = NULL; | 
|  | 733 | struct ip_tunnel *tunnel; | 
|  | 734 | struct iphdr *iph; | 
|  | 735 |  | 
|  | 736 | tunnel = (struct ip_tunnel*)dev->priv; | 
|  | 737 | iph = &tunnel->parms.iph; | 
|  | 738 |  | 
|  | 739 | tunnel->dev = dev; | 
|  | 740 | strcpy(tunnel->parms.name, dev->name); | 
|  | 741 |  | 
|  | 742 | memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); | 
|  | 743 | memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); | 
|  | 744 |  | 
|  | 745 | if (iph->daddr) { | 
|  | 746 | struct flowi fl = { .nl_u = { .ip4_u = | 
|  | 747 | { .daddr = iph->daddr, | 
|  | 748 | .saddr = iph->saddr, | 
|  | 749 | .tos = RT_TOS(iph->tos) } }, | 
|  | 750 | .oif = tunnel->parms.link, | 
|  | 751 | .proto = IPPROTO_IPV6 }; | 
|  | 752 | struct rtable *rt; | 
|  | 753 | if (!ip_route_output_key(&rt, &fl)) { | 
|  | 754 | tdev = rt->u.dst.dev; | 
|  | 755 | ip_rt_put(rt); | 
|  | 756 | } | 
|  | 757 | dev->flags |= IFF_POINTOPOINT; | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | if (!tdev && tunnel->parms.link) | 
|  | 761 | tdev = __dev_get_by_index(tunnel->parms.link); | 
|  | 762 |  | 
|  | 763 | if (tdev) { | 
|  | 764 | dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr); | 
|  | 765 | dev->mtu = tdev->mtu - sizeof(struct iphdr); | 
|  | 766 | if (dev->mtu < IPV6_MIN_MTU) | 
|  | 767 | dev->mtu = IPV6_MIN_MTU; | 
|  | 768 | } | 
|  | 769 | dev->iflink = tunnel->parms.link; | 
|  | 770 |  | 
|  | 771 | return 0; | 
|  | 772 | } | 
|  | 773 |  | 
|  | 774 | int __init ipip6_fb_tunnel_init(struct net_device *dev) | 
|  | 775 | { | 
|  | 776 | struct ip_tunnel *tunnel = dev->priv; | 
|  | 777 | struct iphdr *iph = &tunnel->parms.iph; | 
|  | 778 |  | 
|  | 779 | tunnel->dev = dev; | 
|  | 780 | strcpy(tunnel->parms.name, dev->name); | 
|  | 781 |  | 
|  | 782 | iph->version		= 4; | 
|  | 783 | iph->protocol		= IPPROTO_IPV6; | 
|  | 784 | iph->ihl		= 5; | 
|  | 785 | iph->ttl		= 64; | 
|  | 786 |  | 
|  | 787 | dev_hold(dev); | 
|  | 788 | tunnels_wc[0]		= tunnel; | 
|  | 789 | return 0; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | static struct net_protocol sit_protocol = { | 
|  | 793 | .handler	=	ipip6_rcv, | 
|  | 794 | .err_handler	=	ipip6_err, | 
|  | 795 | }; | 
|  | 796 |  | 
|  | 797 | void __exit sit_cleanup(void) | 
|  | 798 | { | 
|  | 799 | inet_del_protocol(&sit_protocol, IPPROTO_IPV6); | 
|  | 800 | unregister_netdev(ipip6_fb_tunnel_dev); | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | int __init sit_init(void) | 
|  | 804 | { | 
|  | 805 | int err; | 
|  | 806 |  | 
|  | 807 | printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n"); | 
|  | 808 |  | 
|  | 809 | if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) { | 
|  | 810 | printk(KERN_INFO "sit init: Can't add protocol\n"); | 
|  | 811 | return -EAGAIN; | 
|  | 812 | } | 
|  | 813 |  | 
|  | 814 | ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", | 
|  | 815 | ipip6_tunnel_setup); | 
|  | 816 | if (!ipip6_fb_tunnel_dev) { | 
|  | 817 | err = -ENOMEM; | 
|  | 818 | goto err1; | 
|  | 819 | } | 
|  | 820 |  | 
|  | 821 | ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init; | 
|  | 822 |  | 
|  | 823 | if ((err =  register_netdev(ipip6_fb_tunnel_dev))) | 
|  | 824 | goto err2; | 
|  | 825 |  | 
|  | 826 | out: | 
|  | 827 | return err; | 
|  | 828 | err2: | 
|  | 829 | free_netdev(ipip6_fb_tunnel_dev); | 
|  | 830 | err1: | 
|  | 831 | inet_del_protocol(&sit_protocol, IPPROTO_IPV6); | 
|  | 832 | goto out; | 
|  | 833 | } |