| 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: | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | *	Pedro Roque		<roque@di.fc.ul.pt> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 19 | * Fred L. Templin <fltemplin@acm.org>:		isatap support | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 23 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> | 
|  | 25 | #include <linux/types.h> | 
|  | 26 | #include <linux/socket.h> | 
|  | 27 | #include <linux/sockios.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 36 | #include <linux/if_ether.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | #include <net/sock.h> | 
|  | 39 | #include <net/snmp.h> | 
|  | 40 |  | 
|  | 41 | #include <net/ipv6.h> | 
|  | 42 | #include <net/protocol.h> | 
|  | 43 | #include <net/transp_v6.h> | 
|  | 44 | #include <net/ip6_fib.h> | 
|  | 45 | #include <net/ip6_route.h> | 
|  | 46 | #include <net/ndisc.h> | 
|  | 47 | #include <net/addrconf.h> | 
|  | 48 | #include <net/ip.h> | 
|  | 49 | #include <net/udp.h> | 
|  | 50 | #include <net/icmp.h> | 
|  | 51 | #include <net/ipip.h> | 
|  | 52 | #include <net/inet_ecn.h> | 
|  | 53 | #include <net/xfrm.h> | 
|  | 54 | #include <net/dsfield.h> | 
|  | 55 |  | 
|  | 56 | /* | 
|  | 57 | This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c | 
|  | 58 |  | 
|  | 59 | For comments look at net/ipv4/ip_gre.c --ANK | 
|  | 60 | */ | 
|  | 61 |  | 
|  | 62 | #define HASH_SIZE  16 | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 63 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | static int ipip6_fb_tunnel_init(struct net_device *dev); | 
|  | 66 | static int ipip6_tunnel_init(struct net_device *dev); | 
|  | 67 | static void ipip6_tunnel_setup(struct net_device *dev); | 
|  | 68 |  | 
|  | 69 | static struct net_device *ipip6_fb_tunnel_dev; | 
|  | 70 |  | 
|  | 71 | static struct ip_tunnel *tunnels_r_l[HASH_SIZE]; | 
|  | 72 | static struct ip_tunnel *tunnels_r[HASH_SIZE]; | 
|  | 73 | static struct ip_tunnel *tunnels_l[HASH_SIZE]; | 
|  | 74 | static struct ip_tunnel *tunnels_wc[1]; | 
|  | 75 | static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l }; | 
|  | 76 |  | 
|  | 77 | static DEFINE_RWLOCK(ipip6_lock); | 
|  | 78 |  | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 79 | static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { | 
|  | 81 | unsigned h0 = HASH(remote); | 
|  | 82 | unsigned h1 = HASH(local); | 
|  | 83 | struct ip_tunnel *t; | 
|  | 84 |  | 
|  | 85 | for (t = tunnels_r_l[h0^h1]; t; t = t->next) { | 
|  | 86 | if (local == t->parms.iph.saddr && | 
|  | 87 | remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | 
|  | 88 | return t; | 
|  | 89 | } | 
|  | 90 | for (t = tunnels_r[h0]; t; t = t->next) { | 
|  | 91 | if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | 
|  | 92 | return t; | 
|  | 93 | } | 
|  | 94 | for (t = tunnels_l[h1]; t; t = t->next) { | 
|  | 95 | if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP)) | 
|  | 96 | return t; | 
|  | 97 | } | 
|  | 98 | if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP)) | 
|  | 99 | return t; | 
|  | 100 | return NULL; | 
|  | 101 | } | 
|  | 102 |  | 
| YOSHIFUJI Hideaki | 420fe23 | 2007-04-24 20:44:47 +0900 | [diff] [blame] | 103 | static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { | 
| YOSHIFUJI Hideaki | 420fe23 | 2007-04-24 20:44:47 +0900 | [diff] [blame] | 105 | __be32 remote = parms->iph.daddr; | 
|  | 106 | __be32 local = parms->iph.saddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | unsigned h = 0; | 
|  | 108 | int prio = 0; | 
|  | 109 |  | 
|  | 110 | if (remote) { | 
|  | 111 | prio |= 2; | 
|  | 112 | h ^= HASH(remote); | 
|  | 113 | } | 
|  | 114 | if (local) { | 
|  | 115 | prio |= 1; | 
|  | 116 | h ^= HASH(local); | 
|  | 117 | } | 
|  | 118 | return &tunnels[prio][h]; | 
|  | 119 | } | 
|  | 120 |  | 
| YOSHIFUJI Hideaki | 420fe23 | 2007-04-24 20:44:47 +0900 | [diff] [blame] | 121 | static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t) | 
|  | 122 | { | 
|  | 123 | return __ipip6_bucket(&t->parms); | 
|  | 124 | } | 
|  | 125 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | static void ipip6_tunnel_unlink(struct ip_tunnel *t) | 
|  | 127 | { | 
|  | 128 | struct ip_tunnel **tp; | 
|  | 129 |  | 
|  | 130 | for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) { | 
|  | 131 | if (t == *tp) { | 
|  | 132 | write_lock_bh(&ipip6_lock); | 
|  | 133 | *tp = t->next; | 
|  | 134 | write_unlock_bh(&ipip6_lock); | 
|  | 135 | break; | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | static void ipip6_tunnel_link(struct ip_tunnel *t) | 
|  | 141 | { | 
|  | 142 | struct ip_tunnel **tp = ipip6_bucket(t); | 
|  | 143 |  | 
|  | 144 | t->next = *tp; | 
|  | 145 | write_lock_bh(&ipip6_lock); | 
|  | 146 | *tp = t; | 
|  | 147 | write_unlock_bh(&ipip6_lock); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create) | 
|  | 151 | { | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 152 | __be32 remote = parms->iph.daddr; | 
|  | 153 | __be32 local = parms->iph.saddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | struct ip_tunnel *t, **tp, *nt; | 
|  | 155 | struct net_device *dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | char name[IFNAMSIZ]; | 
|  | 157 |  | 
| YOSHIFUJI Hideaki | 420fe23 | 2007-04-24 20:44:47 +0900 | [diff] [blame] | 158 | for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) | 
|  | 160 | return t; | 
|  | 161 | } | 
|  | 162 | if (!create) | 
|  | 163 | goto failed; | 
|  | 164 |  | 
|  | 165 | if (parms->name[0]) | 
|  | 166 | strlcpy(name, parms->name, IFNAMSIZ); | 
|  | 167 | else { | 
|  | 168 | int i; | 
|  | 169 | for (i=1; i<100; i++) { | 
|  | 170 | sprintf(name, "sit%d", i); | 
| Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 171 | if (__dev_get_by_name(&init_net, name) == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | break; | 
|  | 173 | } | 
|  | 174 | if (i==100) | 
|  | 175 | goto failed; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup); | 
|  | 179 | if (dev == NULL) | 
|  | 180 | return NULL; | 
|  | 181 |  | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 182 | nt = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | dev->init = ipip6_tunnel_init; | 
|  | 184 | nt->parms = *parms; | 
|  | 185 |  | 
| Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 186 | if (parms->i_flags & SIT_ISATAP) | 
|  | 187 | dev->priv_flags |= IFF_ISATAP; | 
|  | 188 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (register_netdevice(dev) < 0) { | 
|  | 190 | free_netdev(dev); | 
|  | 191 | goto failed; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | dev_hold(dev); | 
|  | 195 |  | 
|  | 196 | ipip6_tunnel_link(nt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return nt; | 
|  | 198 |  | 
|  | 199 | failed: | 
|  | 200 | return NULL; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | static void ipip6_tunnel_uninit(struct net_device *dev) | 
|  | 204 | { | 
|  | 205 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 206 | write_lock_bh(&ipip6_lock); | 
|  | 207 | tunnels_wc[0] = NULL; | 
|  | 208 | write_unlock_bh(&ipip6_lock); | 
|  | 209 | dev_put(dev); | 
|  | 210 | } else { | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 211 | ipip6_tunnel_unlink(netdev_priv(dev)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | dev_put(dev); | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 |  | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 217 | static int ipip6_err(struct sk_buff *skb, u32 info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { | 
|  | 219 | #ifndef I_WISH_WORLD_WERE_PERFECT | 
|  | 220 |  | 
|  | 221 | /* It is not :-( All the routers (except for Linux) return only | 
|  | 222 | 8 bytes of packet payload. It means, that precise relaying of | 
|  | 223 | ICMP in the real Internet is absolutely infeasible. | 
|  | 224 | */ | 
|  | 225 | struct iphdr *iph = (struct iphdr*)skb->data; | 
| Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 226 | const int type = icmp_hdr(skb)->type; | 
|  | 227 | const int code = icmp_hdr(skb)->code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | struct ip_tunnel *t; | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 229 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | switch (type) { | 
|  | 232 | default: | 
|  | 233 | case ICMP_PARAMETERPROB: | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 234 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | case ICMP_DEST_UNREACH: | 
|  | 237 | switch (code) { | 
|  | 238 | case ICMP_SR_FAILED: | 
|  | 239 | case ICMP_PORT_UNREACH: | 
|  | 240 | /* Impossible event. */ | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 241 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | case ICMP_FRAG_NEEDED: | 
|  | 243 | /* Soft state for pmtu is maintained by IP core. */ | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 244 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | default: | 
|  | 246 | /* All others are translated to HOST_UNREACH. | 
|  | 247 | rfc2003 contains "deep thoughts" about NET_UNREACH, | 
|  | 248 | I believe they are just ether pollution. --ANK | 
|  | 249 | */ | 
|  | 250 | break; | 
|  | 251 | } | 
|  | 252 | break; | 
|  | 253 | case ICMP_TIME_EXCEEDED: | 
|  | 254 | if (code != ICMP_EXC_TTL) | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 255 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | break; | 
|  | 257 | } | 
|  | 258 |  | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 259 | err = -ENOENT; | 
|  | 260 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | read_lock(&ipip6_lock); | 
|  | 262 | t = ipip6_tunnel_lookup(iph->daddr, iph->saddr); | 
|  | 263 | if (t == NULL || t->parms.iph.daddr == 0) | 
|  | 264 | goto out; | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 265 |  | 
|  | 266 | err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) | 
|  | 268 | goto out; | 
|  | 269 |  | 
|  | 270 | if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO) | 
|  | 271 | t->err_count++; | 
|  | 272 | else | 
|  | 273 | t->err_count = 1; | 
|  | 274 | t->err_time = jiffies; | 
|  | 275 | out: | 
|  | 276 | read_unlock(&ipip6_lock); | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 277 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | #else | 
|  | 279 | struct iphdr *iph = (struct iphdr*)dp; | 
|  | 280 | int hlen = iph->ihl<<2; | 
|  | 281 | struct ipv6hdr *iph6; | 
| Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 282 | const int type = icmp_hdr(skb)->type; | 
|  | 283 | const int code = icmp_hdr(skb)->code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int rel_type = 0; | 
|  | 285 | int rel_code = 0; | 
|  | 286 | int rel_info = 0; | 
|  | 287 | struct sk_buff *skb2; | 
|  | 288 | struct rt6_info *rt6i; | 
|  | 289 |  | 
|  | 290 | if (len < hlen + sizeof(struct ipv6hdr)) | 
|  | 291 | return; | 
|  | 292 | iph6 = (struct ipv6hdr*)(dp + hlen); | 
|  | 293 |  | 
|  | 294 | switch (type) { | 
|  | 295 | default: | 
|  | 296 | return; | 
|  | 297 | case ICMP_PARAMETERPROB: | 
| Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 298 | if (icmp_hdr(skb)->un.gateway < hlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return; | 
|  | 300 |  | 
|  | 301 | /* So... This guy found something strange INSIDE encapsulated | 
|  | 302 | packet. Well, he is fool, but what can we do ? | 
|  | 303 | */ | 
|  | 304 | rel_type = ICMPV6_PARAMPROB; | 
| Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 305 | rel_info = icmp_hdr(skb)->un.gateway - hlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | break; | 
|  | 307 |  | 
|  | 308 | case ICMP_DEST_UNREACH: | 
|  | 309 | switch (code) { | 
|  | 310 | case ICMP_SR_FAILED: | 
|  | 311 | case ICMP_PORT_UNREACH: | 
|  | 312 | /* Impossible event. */ | 
|  | 313 | return; | 
|  | 314 | case ICMP_FRAG_NEEDED: | 
|  | 315 | /* Too complicated case ... */ | 
|  | 316 | return; | 
|  | 317 | default: | 
|  | 318 | /* All others are translated to HOST_UNREACH. | 
|  | 319 | rfc2003 contains "deep thoughts" about NET_UNREACH, | 
|  | 320 | I believe, it is just ether pollution. --ANK | 
|  | 321 | */ | 
|  | 322 | rel_type = ICMPV6_DEST_UNREACH; | 
|  | 323 | rel_code = ICMPV6_ADDR_UNREACH; | 
|  | 324 | break; | 
|  | 325 | } | 
|  | 326 | break; | 
|  | 327 | case ICMP_TIME_EXCEEDED: | 
|  | 328 | if (code != ICMP_EXC_TTL) | 
|  | 329 | return; | 
|  | 330 | rel_type = ICMPV6_TIME_EXCEED; | 
|  | 331 | rel_code = ICMPV6_EXC_HOPLIMIT; | 
|  | 332 | break; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | /* Prepare fake skb to feed it to icmpv6_send */ | 
|  | 336 | skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 337 | if (skb2 == NULL) | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 338 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | dst_release(skb2->dst); | 
|  | 340 | skb2->dst = NULL; | 
|  | 341 | skb_pull(skb2, skb->data - (u8*)iph6); | 
| Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 342 | skb_reset_network_header(skb2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 |  | 
|  | 344 | /* Try to guess incoming interface */ | 
|  | 345 | rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0); | 
|  | 346 | if (rt6i && rt6i->rt6i_dev) { | 
|  | 347 | skb2->dev = rt6i->rt6i_dev; | 
|  | 348 |  | 
|  | 349 | rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0); | 
|  | 350 |  | 
|  | 351 | if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) { | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 352 | struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) { | 
|  | 354 | rel_type = ICMPV6_DEST_UNREACH; | 
|  | 355 | rel_code = ICMPV6_ADDR_UNREACH; | 
|  | 356 | } | 
|  | 357 | icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); | 
|  | 358 | } | 
|  | 359 | } | 
|  | 360 | kfree_skb(skb2); | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 361 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | #endif | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb) | 
|  | 366 | { | 
|  | 367 | if (INET_ECN_is_ce(iph->tos)) | 
| Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 368 | IP6_ECN_set_ce(ipv6_hdr(skb)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } | 
|  | 370 |  | 
| Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 371 | /* ISATAP (RFC4214) - check source address */ | 
|  | 372 | static int | 
|  | 373 | isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev) | 
|  | 374 | { | 
|  | 375 | struct neighbour *neigh; | 
|  | 376 | struct dst_entry *dst; | 
|  | 377 | struct rt6_info *rt; | 
|  | 378 | struct flowi fl; | 
|  | 379 | struct in6_addr *addr6; | 
|  | 380 | struct in6_addr rtr; | 
|  | 381 | struct ipv6hdr *iph6; | 
|  | 382 | int ok = 0; | 
|  | 383 |  | 
|  | 384 | /* from onlink default router */ | 
|  | 385 | ipv6_addr_set(&rtr,  htonl(0xFE800000), 0, 0, 0); | 
|  | 386 | ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr); | 
|  | 387 | if ((rt = rt6_get_dflt_router(&rtr, dev))) { | 
|  | 388 | dst_release(&rt->u.dst); | 
|  | 389 | return 1; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | iph6 = ipv6_hdr(skb); | 
|  | 393 | memset(&fl, 0, sizeof(fl)); | 
|  | 394 | fl.proto = iph6->nexthdr; | 
|  | 395 | ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr); | 
|  | 396 | fl.oif = dev->ifindex; | 
|  | 397 | security_skb_classify_flow(skb, &fl); | 
|  | 398 |  | 
|  | 399 | dst = ip6_route_output(NULL, &fl); | 
|  | 400 | if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) { | 
|  | 401 |  | 
|  | 402 | addr6 = (struct in6_addr*)&neigh->primary_key; | 
|  | 403 |  | 
|  | 404 | /* from correct previous hop */ | 
|  | 405 | if (ipv6_addr_is_isatap(addr6) && | 
|  | 406 | (addr6->s6_addr32[3] == iph->saddr)) | 
|  | 407 | ok = 1; | 
|  | 408 | } | 
|  | 409 | dst_release(dst); | 
|  | 410 | return ok; | 
|  | 411 | } | 
|  | 412 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | static int ipip6_rcv(struct sk_buff *skb) | 
|  | 414 | { | 
|  | 415 | struct iphdr *iph; | 
|  | 416 | struct ip_tunnel *tunnel; | 
|  | 417 |  | 
|  | 418 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | 
|  | 419 | goto out; | 
|  | 420 |  | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 421 | iph = ip_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 |  | 
|  | 423 | read_lock(&ipip6_lock); | 
|  | 424 | if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) { | 
|  | 425 | secpath_reset(skb); | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 426 | skb->mac_header = skb->network_header; | 
| Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 427 | skb_reset_network_header(skb); | 
| Patrick McHardy | 8cdfab8 | 2006-01-06 23:04:01 -0800 | [diff] [blame] | 428 | IPCB(skb)->flags = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | skb->protocol = htons(ETH_P_IPV6); | 
|  | 430 | skb->pkt_type = PACKET_HOST; | 
| Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 431 |  | 
|  | 432 | if ((tunnel->dev->priv_flags & IFF_ISATAP) && | 
|  | 433 | !isatap_srcok(skb, iph, tunnel->dev)) { | 
|  | 434 | tunnel->stat.rx_errors++; | 
|  | 435 | read_unlock(&ipip6_lock); | 
|  | 436 | kfree_skb(skb); | 
|  | 437 | return 0; | 
|  | 438 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | tunnel->stat.rx_packets++; | 
|  | 440 | tunnel->stat.rx_bytes += skb->len; | 
|  | 441 | skb->dev = tunnel->dev; | 
|  | 442 | dst_release(skb->dst); | 
|  | 443 | skb->dst = NULL; | 
|  | 444 | nf_reset(skb); | 
|  | 445 | ipip6_ecn_decapsulate(iph, skb); | 
|  | 446 | netif_rx(skb); | 
|  | 447 | read_unlock(&ipip6_lock); | 
|  | 448 | return 0; | 
|  | 449 | } | 
|  | 450 |  | 
| Herbert Xu | 45af08b | 2006-04-05 22:31:19 -0700 | [diff] [blame] | 451 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | kfree_skb(skb); | 
|  | 453 | read_unlock(&ipip6_lock); | 
|  | 454 | out: | 
|  | 455 | return 0; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | /* Returns the embedded IPv4 address if the IPv6 address | 
|  | 459 | comes from 6to4 (RFC 3056) addr space */ | 
|  | 460 |  | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 461 | static inline __be32 try_6to4(struct in6_addr *v6dst) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 463 | __be32 dst = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 |  | 
|  | 465 | if (v6dst->s6_addr16[0] == htons(0x2002)) { | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 466 | /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | memcpy(&dst, &v6dst->s6_addr16[1], 4); | 
|  | 468 | } | 
|  | 469 | return dst; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | /* | 
|  | 473 | *	This function assumes it is being called from dev_queue_xmit() | 
|  | 474 | *	and that skb is filled properly by that function. | 
|  | 475 | */ | 
|  | 476 |  | 
|  | 477 | static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) | 
|  | 478 | { | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 479 | struct ip_tunnel *tunnel = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct net_device_stats *stats = &tunnel->stat; | 
|  | 481 | struct iphdr  *tiph = &tunnel->parms.iph; | 
| Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 482 | struct ipv6hdr *iph6 = ipv6_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | u8     tos = tunnel->parms.iph.tos; | 
|  | 484 | struct rtable *rt;     			/* Route to the other host */ | 
|  | 485 | struct net_device *tdev;			/* Device to other host */ | 
|  | 486 | struct iphdr  *iph;			/* Our new IP header */ | 
| Chuck Lever | c2636b4 | 2007-10-23 21:07:32 -0700 | [diff] [blame] | 487 | unsigned int max_headroom;		/* The extra header space needed */ | 
| Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 488 | __be32 dst = tiph->daddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | int    mtu; | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 490 | struct in6_addr *addr6; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | int addr_type; | 
|  | 492 |  | 
|  | 493 | if (tunnel->recursion++) { | 
|  | 494 | tunnel->stat.collisions++; | 
|  | 495 | goto tx_error; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | if (skb->protocol != htons(ETH_P_IPV6)) | 
|  | 499 | goto tx_error; | 
|  | 500 |  | 
| Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 501 | /* ISATAP (RFC4214) - must come before 6to4 */ | 
|  | 502 | if (dev->priv_flags & IFF_ISATAP) { | 
|  | 503 | struct neighbour *neigh = NULL; | 
|  | 504 |  | 
|  | 505 | if (skb->dst) | 
|  | 506 | neigh = skb->dst->neighbour; | 
|  | 507 |  | 
|  | 508 | if (neigh == NULL) { | 
|  | 509 | if (net_ratelimit()) | 
|  | 510 | printk(KERN_DEBUG "sit: nexthop == NULL\n"); | 
|  | 511 | goto tx_error; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | addr6 = (struct in6_addr*)&neigh->primary_key; | 
|  | 515 | addr_type = ipv6_addr_type(addr6); | 
|  | 516 |  | 
|  | 517 | if ((addr_type & IPV6_ADDR_UNICAST) && | 
|  | 518 | ipv6_addr_is_isatap(addr6)) | 
|  | 519 | dst = addr6->s6_addr32[3]; | 
|  | 520 | else | 
|  | 521 | goto tx_error; | 
|  | 522 | } | 
|  | 523 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (!dst) | 
|  | 525 | dst = try_6to4(&iph6->daddr); | 
|  | 526 |  | 
|  | 527 | if (!dst) { | 
|  | 528 | struct neighbour *neigh = NULL; | 
|  | 529 |  | 
|  | 530 | if (skb->dst) | 
|  | 531 | neigh = skb->dst->neighbour; | 
|  | 532 |  | 
|  | 533 | if (neigh == NULL) { | 
|  | 534 | if (net_ratelimit()) | 
|  | 535 | printk(KERN_DEBUG "sit: nexthop == NULL\n"); | 
|  | 536 | goto tx_error; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | addr6 = (struct in6_addr*)&neigh->primary_key; | 
|  | 540 | addr_type = ipv6_addr_type(addr6); | 
|  | 541 |  | 
|  | 542 | if (addr_type == IPV6_ADDR_ANY) { | 
| Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 543 | addr6 = &ipv6_hdr(skb)->daddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | addr_type = ipv6_addr_type(addr6); | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | if ((addr_type & IPV6_ADDR_COMPATv4) == 0) | 
|  | 548 | goto tx_error_icmp; | 
|  | 549 |  | 
|  | 550 | dst = addr6->s6_addr32[3]; | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 | { | 
|  | 554 | struct flowi fl = { .nl_u = { .ip4_u = | 
|  | 555 | { .daddr = dst, | 
|  | 556 | .saddr = tiph->saddr, | 
|  | 557 | .tos = RT_TOS(tos) } }, | 
|  | 558 | .oif = tunnel->parms.link, | 
|  | 559 | .proto = IPPROTO_IPV6 }; | 
| Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 560 | if (ip_route_output_key(&init_net, &rt, &fl)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | tunnel->stat.tx_carrier_errors++; | 
|  | 562 | goto tx_error_icmp; | 
|  | 563 | } | 
|  | 564 | } | 
|  | 565 | if (rt->rt_type != RTN_UNICAST) { | 
|  | 566 | ip_rt_put(rt); | 
|  | 567 | tunnel->stat.tx_carrier_errors++; | 
|  | 568 | goto tx_error_icmp; | 
|  | 569 | } | 
|  | 570 | tdev = rt->u.dst.dev; | 
|  | 571 |  | 
|  | 572 | if (tdev == dev) { | 
|  | 573 | ip_rt_put(rt); | 
|  | 574 | tunnel->stat.collisions++; | 
|  | 575 | goto tx_error; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | if (tiph->frag_off) | 
|  | 579 | mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr); | 
|  | 580 | else | 
|  | 581 | mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu; | 
|  | 582 |  | 
|  | 583 | if (mtu < 68) { | 
|  | 584 | tunnel->stat.collisions++; | 
|  | 585 | ip_rt_put(rt); | 
|  | 586 | goto tx_error; | 
|  | 587 | } | 
|  | 588 | if (mtu < IPV6_MIN_MTU) | 
|  | 589 | mtu = IPV6_MIN_MTU; | 
|  | 590 | if (tunnel->parms.iph.daddr && skb->dst) | 
|  | 591 | skb->dst->ops->update_pmtu(skb->dst, mtu); | 
|  | 592 |  | 
|  | 593 | if (skb->len > mtu) { | 
|  | 594 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); | 
|  | 595 | ip_rt_put(rt); | 
|  | 596 | goto tx_error; | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | if (tunnel->err_count > 0) { | 
|  | 600 | if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) { | 
|  | 601 | tunnel->err_count--; | 
|  | 602 | dst_link_failure(skb); | 
|  | 603 | } else | 
|  | 604 | tunnel->err_count = 0; | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 | /* | 
|  | 608 | * Okay, now see if we can stuff it in the buffer as-is. | 
|  | 609 | */ | 
|  | 610 | max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr); | 
|  | 611 |  | 
| Patrick McHardy | cfbba49 | 2007-07-09 15:33:40 -0700 | [diff] [blame] | 612 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || | 
|  | 613 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); | 
|  | 615 | if (!new_skb) { | 
|  | 616 | ip_rt_put(rt); | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 617 | stats->tx_dropped++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | dev_kfree_skb(skb); | 
|  | 619 | tunnel->recursion--; | 
|  | 620 | return 0; | 
|  | 621 | } | 
|  | 622 | if (skb->sk) | 
|  | 623 | skb_set_owner_w(new_skb, skb->sk); | 
|  | 624 | dev_kfree_skb(skb); | 
|  | 625 | skb = new_skb; | 
| Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 626 | iph6 = ipv6_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } | 
|  | 628 |  | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 629 | skb->transport_header = skb->network_header; | 
| Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 630 | skb_push(skb, sizeof(struct iphdr)); | 
|  | 631 | skb_reset_network_header(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | 
| Patrick McHardy | 8cdfab8 | 2006-01-06 23:04:01 -0800 | [diff] [blame] | 633 | IPCB(skb)->flags = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | dst_release(skb->dst); | 
|  | 635 | skb->dst = &rt->u.dst; | 
|  | 636 |  | 
|  | 637 | /* | 
|  | 638 | *	Push down and install the IPIP header. | 
|  | 639 | */ | 
|  | 640 |  | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 641 | iph 			=	ip_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | iph->version		=	4; | 
|  | 643 | iph->ihl		=	sizeof(struct iphdr)>>2; | 
|  | 644 | if (mtu > IPV6_MIN_MTU) | 
|  | 645 | iph->frag_off	=	htons(IP_DF); | 
|  | 646 | else | 
|  | 647 | iph->frag_off	=	0; | 
|  | 648 |  | 
|  | 649 | iph->protocol		=	IPPROTO_IPV6; | 
|  | 650 | iph->tos		=	INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); | 
|  | 651 | iph->daddr		=	rt->rt_dst; | 
|  | 652 | iph->saddr		=	rt->rt_src; | 
|  | 653 |  | 
|  | 654 | if ((iph->ttl = tiph->ttl) == 0) | 
|  | 655 | iph->ttl	=	iph6->hop_limit; | 
|  | 656 |  | 
|  | 657 | nf_reset(skb); | 
|  | 658 |  | 
|  | 659 | IPTUNNEL_XMIT(); | 
|  | 660 | tunnel->recursion--; | 
|  | 661 | return 0; | 
|  | 662 |  | 
|  | 663 | tx_error_icmp: | 
|  | 664 | dst_link_failure(skb); | 
|  | 665 | tx_error: | 
|  | 666 | stats->tx_errors++; | 
|  | 667 | dev_kfree_skb(skb); | 
|  | 668 | tunnel->recursion--; | 
|  | 669 | return 0; | 
|  | 670 | } | 
|  | 671 |  | 
| Michal Schmidt | 8a4a50f | 2007-12-13 09:47:00 -0800 | [diff] [blame] | 672 | static void ipip6_tunnel_bind_dev(struct net_device *dev) | 
|  | 673 | { | 
|  | 674 | struct net_device *tdev = NULL; | 
|  | 675 | struct ip_tunnel *tunnel; | 
|  | 676 | struct iphdr *iph; | 
|  | 677 |  | 
|  | 678 | tunnel = netdev_priv(dev); | 
|  | 679 | iph = &tunnel->parms.iph; | 
|  | 680 |  | 
|  | 681 | if (iph->daddr) { | 
|  | 682 | struct flowi fl = { .nl_u = { .ip4_u = | 
|  | 683 | { .daddr = iph->daddr, | 
|  | 684 | .saddr = iph->saddr, | 
|  | 685 | .tos = RT_TOS(iph->tos) } }, | 
|  | 686 | .oif = tunnel->parms.link, | 
|  | 687 | .proto = IPPROTO_IPV6 }; | 
|  | 688 | struct rtable *rt; | 
| Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 689 | if (!ip_route_output_key(&init_net, &rt, &fl)) { | 
| Michal Schmidt | 8a4a50f | 2007-12-13 09:47:00 -0800 | [diff] [blame] | 690 | tdev = rt->u.dst.dev; | 
|  | 691 | ip_rt_put(rt); | 
|  | 692 | } | 
|  | 693 | dev->flags |= IFF_POINTOPOINT; | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | if (!tdev && tunnel->parms.link) | 
|  | 697 | tdev = __dev_get_by_index(&init_net, tunnel->parms.link); | 
|  | 698 |  | 
|  | 699 | if (tdev) { | 
|  | 700 | dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr); | 
|  | 701 | dev->mtu = tdev->mtu - sizeof(struct iphdr); | 
|  | 702 | if (dev->mtu < IPV6_MIN_MTU) | 
|  | 703 | dev->mtu = IPV6_MIN_MTU; | 
|  | 704 | } | 
|  | 705 | dev->iflink = tunnel->parms.link; | 
|  | 706 | } | 
|  | 707 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | static int | 
|  | 709 | ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | 
|  | 710 | { | 
|  | 711 | int err = 0; | 
|  | 712 | struct ip_tunnel_parm p; | 
|  | 713 | struct ip_tunnel *t; | 
|  | 714 |  | 
|  | 715 | switch (cmd) { | 
|  | 716 | case SIOCGETTUNNEL: | 
|  | 717 | t = NULL; | 
|  | 718 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 719 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | 
|  | 720 | err = -EFAULT; | 
|  | 721 | break; | 
|  | 722 | } | 
|  | 723 | t = ipip6_tunnel_locate(&p, 0); | 
|  | 724 | } | 
|  | 725 | if (t == NULL) | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 726 | t = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | memcpy(&p, &t->parms, sizeof(p)); | 
|  | 728 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | 
|  | 729 | err = -EFAULT; | 
|  | 730 | break; | 
|  | 731 |  | 
|  | 732 | case SIOCADDTUNNEL: | 
|  | 733 | case SIOCCHGTUNNEL: | 
|  | 734 | err = -EPERM; | 
|  | 735 | if (!capable(CAP_NET_ADMIN)) | 
|  | 736 | goto done; | 
|  | 737 |  | 
|  | 738 | err = -EFAULT; | 
|  | 739 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | 
|  | 740 | goto done; | 
|  | 741 |  | 
|  | 742 | err = -EINVAL; | 
|  | 743 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 || | 
|  | 744 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF))) | 
|  | 745 | goto done; | 
|  | 746 | if (p.iph.ttl) | 
|  | 747 | p.iph.frag_off |= htons(IP_DF); | 
|  | 748 |  | 
|  | 749 | t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL); | 
|  | 750 |  | 
|  | 751 | if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | 
|  | 752 | if (t != NULL) { | 
|  | 753 | if (t->dev != dev) { | 
|  | 754 | err = -EEXIST; | 
|  | 755 | break; | 
|  | 756 | } | 
|  | 757 | } else { | 
|  | 758 | if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) || | 
|  | 759 | (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) { | 
|  | 760 | err = -EINVAL; | 
|  | 761 | break; | 
|  | 762 | } | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 763 | t = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | ipip6_tunnel_unlink(t); | 
|  | 765 | t->parms.iph.saddr = p.iph.saddr; | 
|  | 766 | t->parms.iph.daddr = p.iph.daddr; | 
|  | 767 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | 
|  | 768 | memcpy(dev->broadcast, &p.iph.daddr, 4); | 
|  | 769 | ipip6_tunnel_link(t); | 
|  | 770 | netdev_state_change(dev); | 
|  | 771 | } | 
|  | 772 | } | 
|  | 773 |  | 
|  | 774 | if (t) { | 
|  | 775 | err = 0; | 
|  | 776 | if (cmd == SIOCCHGTUNNEL) { | 
|  | 777 | t->parms.iph.ttl = p.iph.ttl; | 
|  | 778 | t->parms.iph.tos = p.iph.tos; | 
| Michal Schmidt | 8a4a50f | 2007-12-13 09:47:00 -0800 | [diff] [blame] | 779 | if (t->parms.link != p.link) { | 
|  | 780 | t->parms.link = p.link; | 
|  | 781 | ipip6_tunnel_bind_dev(dev); | 
|  | 782 | netdev_state_change(dev); | 
|  | 783 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } | 
|  | 785 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p))) | 
|  | 786 | err = -EFAULT; | 
|  | 787 | } else | 
|  | 788 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | 
|  | 789 | break; | 
|  | 790 |  | 
|  | 791 | case SIOCDELTUNNEL: | 
|  | 792 | err = -EPERM; | 
|  | 793 | if (!capable(CAP_NET_ADMIN)) | 
|  | 794 | goto done; | 
|  | 795 |  | 
|  | 796 | if (dev == ipip6_fb_tunnel_dev) { | 
|  | 797 | err = -EFAULT; | 
|  | 798 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | 
|  | 799 | goto done; | 
|  | 800 | err = -ENOENT; | 
|  | 801 | if ((t = ipip6_tunnel_locate(&p, 0)) == NULL) | 
|  | 802 | goto done; | 
|  | 803 | err = -EPERM; | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 804 | if (t == netdev_priv(ipip6_fb_tunnel_dev)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | goto done; | 
|  | 806 | dev = t->dev; | 
|  | 807 | } | 
| Stephen Hemminger | 22f8cde | 2007-02-07 00:09:58 -0800 | [diff] [blame] | 808 | unregister_netdevice(dev); | 
|  | 809 | err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | break; | 
|  | 811 |  | 
|  | 812 | default: | 
|  | 813 | err = -EINVAL; | 
|  | 814 | } | 
|  | 815 |  | 
|  | 816 | done: | 
|  | 817 | return err; | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev) | 
|  | 821 | { | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 822 | return &(((struct ip_tunnel*)netdev_priv(dev))->stat); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } | 
|  | 824 |  | 
|  | 825 | static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu) | 
|  | 826 | { | 
|  | 827 | if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr)) | 
|  | 828 | return -EINVAL; | 
|  | 829 | dev->mtu = new_mtu; | 
|  | 830 | return 0; | 
|  | 831 | } | 
|  | 832 |  | 
|  | 833 | static void ipip6_tunnel_setup(struct net_device *dev) | 
|  | 834 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | dev->uninit		= ipip6_tunnel_uninit; | 
|  | 836 | dev->destructor 	= free_netdev; | 
|  | 837 | dev->hard_start_xmit	= ipip6_tunnel_xmit; | 
|  | 838 | dev->get_stats		= ipip6_tunnel_get_stats; | 
|  | 839 | dev->do_ioctl		= ipip6_tunnel_ioctl; | 
|  | 840 | dev->change_mtu		= ipip6_tunnel_change_mtu; | 
|  | 841 |  | 
|  | 842 | dev->type		= ARPHRD_SIT; | 
|  | 843 | dev->hard_header_len 	= LL_MAX_HEADER + sizeof(struct iphdr); | 
| Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 844 | dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | dev->flags		= IFF_NOARP; | 
|  | 846 | dev->iflink		= 0; | 
|  | 847 | dev->addr_len		= 4; | 
|  | 848 | } | 
|  | 849 |  | 
|  | 850 | static int ipip6_tunnel_init(struct net_device *dev) | 
|  | 851 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | struct ip_tunnel *tunnel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 |  | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 854 | tunnel = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 |  | 
|  | 856 | tunnel->dev = dev; | 
|  | 857 | strcpy(tunnel->parms.name, dev->name); | 
|  | 858 |  | 
|  | 859 | memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); | 
|  | 860 | memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); | 
|  | 861 |  | 
| Michal Schmidt | 8a4a50f | 2007-12-13 09:47:00 -0800 | [diff] [blame] | 862 | ipip6_tunnel_bind_dev(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 |  | 
|  | 864 | return 0; | 
|  | 865 | } | 
|  | 866 |  | 
| Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 867 | static int __init ipip6_fb_tunnel_init(struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { | 
| Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 869 | struct ip_tunnel *tunnel = netdev_priv(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | struct iphdr *iph = &tunnel->parms.iph; | 
|  | 871 |  | 
|  | 872 | tunnel->dev = dev; | 
|  | 873 | strcpy(tunnel->parms.name, dev->name); | 
|  | 874 |  | 
|  | 875 | iph->version		= 4; | 
|  | 876 | iph->protocol		= IPPROTO_IPV6; | 
|  | 877 | iph->ihl		= 5; | 
|  | 878 | iph->ttl		= 64; | 
|  | 879 |  | 
|  | 880 | dev_hold(dev); | 
|  | 881 | tunnels_wc[0]		= tunnel; | 
|  | 882 | return 0; | 
|  | 883 | } | 
|  | 884 |  | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 885 | static struct xfrm_tunnel sit_handler = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | .handler	=	ipip6_rcv, | 
|  | 887 | .err_handler	=	ipip6_err, | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 888 | .priority	=	1, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | }; | 
|  | 890 |  | 
| Alexey Kuznetsov | db44575 | 2005-07-30 17:46:44 -0700 | [diff] [blame] | 891 | static void __exit sit_destroy_tunnels(void) | 
|  | 892 | { | 
|  | 893 | int prio; | 
|  | 894 |  | 
|  | 895 | for (prio = 1; prio < 4; prio++) { | 
|  | 896 | int h; | 
|  | 897 | for (h = 0; h < HASH_SIZE; h++) { | 
|  | 898 | struct ip_tunnel *t; | 
|  | 899 | while ((t = tunnels[prio][h]) != NULL) | 
|  | 900 | unregister_netdevice(t->dev); | 
|  | 901 | } | 
|  | 902 | } | 
|  | 903 | } | 
|  | 904 |  | 
| Adrian Bunk | 89c8945 | 2006-11-20 16:56:48 -0800 | [diff] [blame] | 905 | static void __exit sit_cleanup(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 907 | xfrm4_tunnel_deregister(&sit_handler, AF_INET6); | 
| Alexey Kuznetsov | db44575 | 2005-07-30 17:46:44 -0700 | [diff] [blame] | 908 |  | 
|  | 909 | rtnl_lock(); | 
|  | 910 | sit_destroy_tunnels(); | 
|  | 911 | unregister_netdevice(ipip6_fb_tunnel_dev); | 
|  | 912 | rtnl_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } | 
|  | 914 |  | 
| Adrian Bunk | 89c8945 | 2006-11-20 16:56:48 -0800 | [diff] [blame] | 915 | static int __init sit_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { | 
|  | 917 | int err; | 
|  | 918 |  | 
|  | 919 | printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n"); | 
|  | 920 |  | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 921 | if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | printk(KERN_INFO "sit init: Can't add protocol\n"); | 
|  | 923 | return -EAGAIN; | 
|  | 924 | } | 
|  | 925 |  | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 926 | ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | ipip6_tunnel_setup); | 
|  | 928 | if (!ipip6_fb_tunnel_dev) { | 
|  | 929 | err = -ENOMEM; | 
|  | 930 | goto err1; | 
|  | 931 | } | 
|  | 932 |  | 
|  | 933 | ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init; | 
|  | 934 |  | 
|  | 935 | if ((err =  register_netdev(ipip6_fb_tunnel_dev))) | 
|  | 936 | goto err2; | 
|  | 937 |  | 
|  | 938 | out: | 
|  | 939 | return err; | 
|  | 940 | err2: | 
|  | 941 | free_netdev(ipip6_fb_tunnel_dev); | 
|  | 942 | err1: | 
| Kazunori MIYAZAWA | c73cb5a | 2007-02-13 12:55:25 -0800 | [diff] [blame] | 943 | xfrm4_tunnel_deregister(&sit_handler, AF_INET6); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | goto out; | 
|  | 945 | } | 
| Joerg Roedel | 989e5b9 | 2006-10-10 14:47:44 -0700 | [diff] [blame] | 946 |  | 
|  | 947 | module_init(sit_init); | 
|  | 948 | module_exit(sit_cleanup); | 
| Jan Dittmer | 39c8508 | 2006-10-13 15:05:53 -0700 | [diff] [blame] | 949 | MODULE_LICENSE("GPL"); | 
| Patrick McHardy | daccff0 | 2006-11-05 15:47:04 -0800 | [diff] [blame] | 950 | MODULE_ALIAS("sit0"); |