blob: 510d31f3cb9689e35eff467667764671f68686a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Changes:
15 * Roger Venning <r.venning@telstra.com>: 6to4 support
16 * Nate Thompson <nate@thebog.net>: 6to4 support
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -040017 * Fred Templin <fred.l.templin@boeing.com>: isatap support
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/socket.h>
25#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/net.h>
27#include <linux/in6.h>
28#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/icmp.h>
31#include <asm/uaccess.h>
32#include <linux/init.h>
33#include <linux/netfilter_ipv4.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080034#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/sock.h>
37#include <net/snmp.h>
38
39#include <net/ipv6.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/ip6_fib.h>
43#include <net/ip6_route.h>
44#include <net/ndisc.h>
45#include <net/addrconf.h>
46#include <net/ip.h>
47#include <net/udp.h>
48#include <net/icmp.h>
49#include <net/ipip.h>
50#include <net/inet_ecn.h>
51#include <net/xfrm.h>
52#include <net/dsfield.h>
Pavel Emelyanov8190d902008-04-16 01:15:17 -070053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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 Viroe69a4ad2006-11-14 20:56:00 -080063#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Stephen Hemminger1326c3d2008-11-20 20:33:56 -080065static void ipip6_fb_tunnel_init(struct net_device *dev);
66static void ipip6_tunnel_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void ipip6_tunnel_setup(struct net_device *dev);
68
Pavel Emelyanov8190d902008-04-16 01:15:17 -070069static int sit_net_id;
70struct sit_net {
Pavel Emelyanov29182172008-04-16 01:16:38 -070071 struct ip_tunnel *tunnels_r_l[HASH_SIZE];
72 struct ip_tunnel *tunnels_r[HASH_SIZE];
73 struct ip_tunnel *tunnels_l[HASH_SIZE];
74 struct ip_tunnel *tunnels_wc[1];
75 struct ip_tunnel **tunnels[4];
76
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -070077 struct net_device *fb_tunnel_dev;
Pavel Emelyanov8190d902008-04-16 01:15:17 -070078};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static DEFINE_RWLOCK(ipip6_lock);
81
Pavel Emelyanovca8def12008-04-16 01:15:39 -070082static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +000083 struct net_device *dev, __be32 remote, __be32 local)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 unsigned h0 = HASH(remote);
86 unsigned h1 = HASH(local);
87 struct ip_tunnel *t;
Pavel Emelyanov29182172008-04-16 01:16:38 -070088 struct sit_net *sitn = net_generic(net, sit_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Pavel Emelyanov29182172008-04-16 01:16:38 -070090 for (t = sitn->tunnels_r_l[h0^h1]; t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (local == t->parms.iph.saddr &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +000092 remote == t->parms.iph.daddr &&
93 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
94 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return t;
96 }
Pavel Emelyanov29182172008-04-16 01:16:38 -070097 for (t = sitn->tunnels_r[h0]; t; t = t->next) {
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +000098 if (remote == t->parms.iph.daddr &&
99 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
100 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return t;
102 }
Pavel Emelyanov29182172008-04-16 01:16:38 -0700103 for (t = sitn->tunnels_l[h1]; t; t = t->next) {
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000104 if (local == t->parms.iph.saddr &&
105 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
106 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return t;
108 }
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000109 t = sitn->tunnels_wc[0];
110 if ((t != NULL) && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return t;
112 return NULL;
113}
114
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700115static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
116 struct ip_tunnel_parm *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900118 __be32 remote = parms->iph.daddr;
119 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned h = 0;
121 int prio = 0;
122
123 if (remote) {
124 prio |= 2;
125 h ^= HASH(remote);
126 }
127 if (local) {
128 prio |= 1;
129 h ^= HASH(local);
130 }
Pavel Emelyanov29182172008-04-16 01:16:38 -0700131 return &sitn->tunnels[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700134static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
135 struct ip_tunnel *t)
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900136{
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700137 return __ipip6_bucket(sitn, &t->parms);
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900138}
139
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700140static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct ip_tunnel **tp;
143
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700144 for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (t == *tp) {
146 write_lock_bh(&ipip6_lock);
147 *tp = t->next;
148 write_unlock_bh(&ipip6_lock);
149 break;
150 }
151 }
152}
153
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700154static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700156 struct ip_tunnel **tp = ipip6_bucket(sitn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 t->next = *tp;
159 write_lock_bh(&ipip6_lock);
160 *tp = t;
161 write_unlock_bh(&ipip6_lock);
162}
163
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +0000164static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000165{
166#ifdef CONFIG_IPV6_SIT_6RD
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +0000167 struct ip_tunnel *t = netdev_priv(dev);
168
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000169 if (t->dev == sitn->fb_tunnel_dev) {
170 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
171 t->ip6rd.relay_prefix = 0;
172 t->ip6rd.prefixlen = 16;
173 t->ip6rd.relay_prefixlen = 0;
174 } else {
175 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
176 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
177 }
178#endif
179}
180
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700181static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
182 struct ip_tunnel_parm *parms, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Al Viroe69a4ad2006-11-14 20:56:00 -0800184 __be32 remote = parms->iph.daddr;
185 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct ip_tunnel *t, **tp, *nt;
187 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 char name[IFNAMSIZ];
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700189 struct sit_net *sitn = net_generic(net, sit_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700191 for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
Sascha Hlusiak8db99e52009-05-19 12:56:48 +0000192 if (local == t->parms.iph.saddr &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000193 remote == t->parms.iph.daddr &&
194 parms->link == t->parms.link) {
Sascha Hlusiak8db99e52009-05-19 12:56:48 +0000195 if (create)
196 return NULL;
197 else
198 return t;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 if (!create)
202 goto failed;
203
204 if (parms->name[0])
205 strlcpy(name, parms->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800206 else
207 sprintf(name, "sit%%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
210 if (dev == NULL)
211 return NULL;
212
Pavel Emelyanov7a971462008-04-16 01:17:18 -0700213 dev_net_set(dev, net);
214
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800215 if (strchr(name, '%')) {
216 if (dev_alloc_name(dev, name) < 0)
217 goto failed_free;
218 }
219
Patrick McHardy2941a482006-01-08 22:05:26 -0800220 nt = netdev_priv(dev);
Stephen Hemminger1326c3d2008-11-20 20:33:56 -0800221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 nt->parms = *parms;
Bjørn Mork1b1d8f72009-03-18 18:56:54 -0700223 ipip6_tunnel_init(dev);
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +0000224 ipip6_tunnel_clone_6rd(dev, sitn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100226 if (parms->i_flags & SIT_ISATAP)
227 dev->priv_flags |= IFF_ISATAP;
228
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800229 if (register_netdevice(dev) < 0)
230 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 dev_hold(dev);
233
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700234 ipip6_tunnel_link(sitn, nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return nt;
236
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800237failed_free:
238 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239failed:
240 return NULL;
241}
242
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400243static struct ip_tunnel_prl_entry *
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900244__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400245{
246 struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
247
248 for (p = t->prl; p; p = p->next)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900249 if (p->addr == addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400250 break;
251 return p;
252
253}
254
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700255static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
256 struct ip_tunnel_prl __user *a)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900257{
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700258 struct ip_tunnel_prl kprl, *kp;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900259 struct ip_tunnel_prl_entry *prl;
260 unsigned int cmax, c = 0, ca, len;
261 int ret = 0;
262
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700263 if (copy_from_user(&kprl, a, sizeof(kprl)))
264 return -EFAULT;
265 cmax = kprl.datalen / sizeof(kprl);
266 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900267 cmax = 1;
268
269 /* For simple GET or for root users,
270 * we try harder to allocate.
271 */
272 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
273 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
274 NULL;
275
276 read_lock(&ipip6_lock);
277
278 ca = t->prl_count < cmax ? t->prl_count : cmax;
279
280 if (!kp) {
281 /* We don't try hard to allocate much memory for
282 * non-root users.
283 * For root users, retry allocating enough memory for
284 * the answer.
285 */
286 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
287 if (!kp) {
288 ret = -ENOMEM;
289 goto out;
290 }
291 }
292
293 c = 0;
294 for (prl = t->prl; prl; prl = prl->next) {
Sascha Hlusiak298bf122009-09-29 11:27:05 +0000295 if (c >= cmax)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900296 break;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700297 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900298 continue;
299 kp[c].addr = prl->addr;
300 kp[c].flags = prl->flags;
301 c++;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700302 if (kprl.addr != htonl(INADDR_ANY))
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900303 break;
304 }
305out:
306 read_unlock(&ipip6_lock);
307
308 len = sizeof(*kp) * c;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700309 ret = 0;
310 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
311 ret = -EFAULT;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900312
313 kfree(kp);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900314
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700315 return ret;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900316}
317
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400318static int
319ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
320{
321 struct ip_tunnel_prl_entry *p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900322 int err = 0;
323
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900324 if (a->addr == htonl(INADDR_ANY))
325 return -EINVAL;
326
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900327 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400328
329 for (p = t->prl; p; p = p->next) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900330 if (p->addr == a->addr) {
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900331 if (chg)
332 goto update;
333 err = -EEXIST;
334 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400335 }
336 }
337
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900338 if (chg) {
339 err = -ENXIO;
340 goto out;
341 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400342
343 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900344 if (!p) {
345 err = -ENOBUFS;
346 goto out;
347 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400348
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400349 p->next = t->prl;
350 t->prl = p;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900351 t->prl_count++;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900352update:
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900353 p->addr = a->addr;
354 p->flags = a->flags;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900355out:
356 write_unlock(&ipip6_lock);
357 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400358}
359
360static int
361ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
362{
363 struct ip_tunnel_prl_entry *x, **p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900364 int err = 0;
365
366 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400367
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900368 if (a && a->addr != htonl(INADDR_ANY)) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400369 for (p = &t->prl; *p; p = &(*p)->next) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900370 if ((*p)->addr == a->addr) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400371 x = *p;
372 *p = x->next;
373 kfree(x);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900374 t->prl_count--;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900375 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400376 }
377 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900378 err = -ENXIO;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400379 } else {
380 while (t->prl) {
381 x = t->prl;
382 t->prl = t->prl->next;
383 kfree(x);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900384 t->prl_count--;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400385 }
386 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900387out:
388 write_unlock(&ipip6_lock);
Sascha Hlusiak4b27960172009-05-19 12:56:50 +0000389 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400390}
391
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400392static int
393isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
394{
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900395 struct ip_tunnel_prl_entry *p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400396 int ok = 1;
397
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900398 read_lock(&ipip6_lock);
399 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400400 if (p) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900401 if (p->flags & PRL_DEFAULT)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400402 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
403 else
404 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
405 } else {
406 struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
407 if (ipv6_addr_is_isatap(addr6) &&
408 (addr6->s6_addr32[3] == iph->saddr) &&
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -0400409 ipv6_chk_prefix(addr6, t->dev))
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400410 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
411 else
412 ok = 0;
413 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900414 read_unlock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400415 return ok;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void ipip6_tunnel_uninit(struct net_device *dev)
419{
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700420 struct net *net = dev_net(dev);
421 struct sit_net *sitn = net_generic(net, sit_net_id);
422
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700423 if (dev == sitn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 write_lock_bh(&ipip6_lock);
Pavel Emelyanov29182172008-04-16 01:16:38 -0700425 sitn->tunnels_wc[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 write_unlock_bh(&ipip6_lock);
427 dev_put(dev);
428 } else {
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700429 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
YOSHIFUJI Hideaki02e10b92008-04-10 15:41:27 +0900430 ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dev_put(dev);
432 }
433}
434
435
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800436static int ipip6_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Rami Rosen071f92d2008-05-21 17:47:54 -0700439/* All the routers (except for Linux) return only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 8 bytes of packet payload. It means, that precise relaying of
441 ICMP in the real Internet is absolutely infeasible.
442 */
443 struct iphdr *iph = (struct iphdr*)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300444 const int type = icmp_hdr(skb)->type;
445 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct ip_tunnel *t;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800447 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 switch (type) {
450 default:
451 case ICMP_PARAMETERPROB:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 case ICMP_DEST_UNREACH:
455 switch (code) {
456 case ICMP_SR_FAILED:
457 case ICMP_PORT_UNREACH:
458 /* Impossible event. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case ICMP_FRAG_NEEDED:
461 /* Soft state for pmtu is maintained by IP core. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 default:
464 /* All others are translated to HOST_UNREACH.
465 rfc2003 contains "deep thoughts" about NET_UNREACH,
466 I believe they are just ether pollution. --ANK
467 */
468 break;
469 }
470 break;
471 case ICMP_TIME_EXCEEDED:
472 if (code != ICMP_EXC_TTL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 }
476
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800477 err = -ENOENT;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 read_lock(&ipip6_lock);
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000480 t = ipip6_tunnel_lookup(dev_net(skb->dev),
481 skb->dev,
482 iph->daddr,
483 iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (t == NULL || t->parms.iph.daddr == 0)
485 goto out;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800486
487 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
489 goto out;
490
Wei Yongjunbb800872009-02-24 23:37:19 -0800491 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 t->err_count++;
493 else
494 t->err_count = 1;
495 t->err_time = jiffies;
496out:
497 read_unlock(&ipip6_lock);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
502{
503 if (INET_ECN_is_ce(iph->tos))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700504 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static int ipip6_rcv(struct sk_buff *skb)
508{
509 struct iphdr *iph;
510 struct ip_tunnel *tunnel;
511
512 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
513 goto out;
514
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700515 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 read_lock(&ipip6_lock);
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000518 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
519 iph->saddr, iph->daddr);
520 if (tunnel != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700522 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700523 skb_reset_network_header(skb);
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800524 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 skb->protocol = htons(ETH_P_IPV6);
526 skb->pkt_type = PACKET_HOST;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100527
528 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400529 !isatap_chksrc(skb, iph, tunnel)) {
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700530 tunnel->dev->stats.rx_errors++;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100531 read_unlock(&ipip6_lock);
532 kfree_skb(skb);
533 return 0;
534 }
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700535 tunnel->dev->stats.rx_packets++;
536 tunnel->dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 skb->dev = tunnel->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000538 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 nf_reset(skb);
540 ipip6_ecn_decapsulate(iph, skb);
541 netif_rx(skb);
542 read_unlock(&ipip6_lock);
543 return 0;
544 }
545
Herbert Xu45af08b2006-04-05 22:31:19 -0700546 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 read_unlock(&ipip6_lock);
548out:
David S. Miller36ca34c2008-05-08 23:40:26 -0700549 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000553/*
554 * Returns the embedded IPv4 address if the IPv6 address
555 * comes from 6rd / 6to4 (RFC 3056) addr space.
556 */
557static inline
558__be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Al Viroe69a4ad2006-11-14 20:56:00 -0800560 __be32 dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000562#ifdef CONFIG_IPV6_SIT_6RD
563 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
564 tunnel->ip6rd.prefixlen)) {
565 unsigned pbw0, pbi0;
566 int pbi1;
567 u32 d;
568
569 pbw0 = tunnel->ip6rd.prefixlen >> 5;
570 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
571
YOSHIFUJI Hideaki / 吉藤英明e7db38c2009-10-11 03:44:45 +0000572 d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000573 tunnel->ip6rd.relay_prefixlen;
574
575 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
576 if (pbi1 > 0)
YOSHIFUJI Hideaki / 吉藤英明e7db38c2009-10-11 03:44:45 +0000577 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000578 (32 - pbi1);
579
580 dst = tunnel->ip6rd.relay_prefix | htonl(d);
581 }
582#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (v6dst->s6_addr16[0] == htons(0x2002)) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900584 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 memcpy(&dst, &v6dst->s6_addr16[1], 4);
586 }
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return dst;
589}
590
591/*
592 * This function assumes it is being called from dev_queue_xmit()
593 * and that skb is filled properly by that function.
594 */
595
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000596static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
597 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Patrick McHardy2941a482006-01-08 22:05:26 -0800599 struct ip_tunnel *tunnel = netdev_priv(dev);
Eric Dumazet0bfbedb2009-10-05 00:11:22 -0700600 struct net_device_stats *stats = &dev->stats;
601 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct iphdr *tiph = &tunnel->parms.iph;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700603 struct ipv6hdr *iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 u8 tos = tunnel->parms.iph.tos;
605 struct rtable *rt; /* Route to the other host */
606 struct net_device *tdev; /* Device to other host */
607 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700608 unsigned int max_headroom; /* The extra header space needed */
Al Viroe69a4ad2006-11-14 20:56:00 -0800609 __be32 dst = tiph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900611 struct in6_addr *addr6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int addr_type;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (skb->protocol != htons(ETH_P_IPV6))
615 goto tx_error;
616
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100617 /* ISATAP (RFC4214) - must come before 6to4 */
618 if (dev->priv_flags & IFF_ISATAP) {
619 struct neighbour *neigh = NULL;
620
Eric Dumazetadf30902009-06-02 05:19:30 +0000621 if (skb_dst(skb))
622 neigh = skb_dst(skb)->neighbour;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100623
624 if (neigh == NULL) {
625 if (net_ratelimit())
626 printk(KERN_DEBUG "sit: nexthop == NULL\n");
627 goto tx_error;
628 }
629
630 addr6 = (struct in6_addr*)&neigh->primary_key;
631 addr_type = ipv6_addr_type(addr6);
632
633 if ((addr_type & IPV6_ADDR_UNICAST) &&
634 ipv6_addr_is_isatap(addr6))
635 dst = addr6->s6_addr32[3];
636 else
637 goto tx_error;
638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!dst)
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000641 dst = try_6rd(&iph6->daddr, tunnel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (!dst) {
644 struct neighbour *neigh = NULL;
645
Eric Dumazetadf30902009-06-02 05:19:30 +0000646 if (skb_dst(skb))
647 neigh = skb_dst(skb)->neighbour;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (neigh == NULL) {
650 if (net_ratelimit())
651 printk(KERN_DEBUG "sit: nexthop == NULL\n");
652 goto tx_error;
653 }
654
655 addr6 = (struct in6_addr*)&neigh->primary_key;
656 addr_type = ipv6_addr_type(addr6);
657
658 if (addr_type == IPV6_ADDR_ANY) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700659 addr6 = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 addr_type = ipv6_addr_type(addr6);
661 }
662
663 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
664 goto tx_error_icmp;
665
666 dst = addr6->s6_addr32[3];
667 }
668
669 {
670 struct flowi fl = { .nl_u = { .ip4_u =
671 { .daddr = dst,
672 .saddr = tiph->saddr,
673 .tos = RT_TOS(tos) } },
674 .oif = tunnel->parms.link,
675 .proto = IPPROTO_IPV6 };
Pavel Emelyanov907a08c2008-04-16 01:16:58 -0700676 if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700677 stats->tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto tx_error_icmp;
679 }
680 }
681 if (rt->rt_type != RTN_UNICAST) {
682 ip_rt_put(rt);
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700683 stats->tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto tx_error_icmp;
685 }
686 tdev = rt->u.dst.dev;
687
688 if (tdev == dev) {
689 ip_rt_put(rt);
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700690 stats->collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto tx_error;
692 }
693
694 if (tiph->frag_off)
695 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
696 else
Eric Dumazetadf30902009-06-02 05:19:30 +0000697 mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (mtu < 68) {
Pavel Emelyanov4eecc102008-05-21 14:15:46 -0700700 stats->collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ip_rt_put(rt);
702 goto tx_error;
703 }
704 if (mtu < IPV6_MIN_MTU)
705 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +0000706 if (tunnel->parms.iph.daddr && skb_dst(skb))
707 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (skb->len > mtu) {
710 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
711 ip_rt_put(rt);
712 goto tx_error;
713 }
714
715 if (tunnel->err_count > 0) {
Wei Yongjunbb800872009-02-24 23:37:19 -0800716 if (time_before(jiffies,
717 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 tunnel->err_count--;
719 dst_link_failure(skb);
720 } else
721 tunnel->err_count = 0;
722 }
723
724 /*
725 * Okay, now see if we can stuff it in the buffer as-is.
726 */
727 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
728
Patrick McHardycfbba492007-07-09 15:33:40 -0700729 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
730 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
732 if (!new_skb) {
733 ip_rt_put(rt);
Eric Dumazet0bfbedb2009-10-05 00:11:22 -0700734 txq->tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000736 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 if (skb->sk)
739 skb_set_owner_w(new_skb, skb->sk);
740 dev_kfree_skb(skb);
741 skb = new_skb;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700742 iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700745 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700746 skb_push(skb, sizeof(struct iphdr));
747 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800749 IPCB(skb)->flags = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000750 skb_dst_drop(skb);
751 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * Push down and install the IPIP header.
755 */
756
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700757 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 iph->version = 4;
759 iph->ihl = sizeof(struct iphdr)>>2;
760 if (mtu > IPV6_MIN_MTU)
Sascha Hlusiak8945a802009-08-28 23:53:53 -0700761 iph->frag_off = tiph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else
763 iph->frag_off = 0;
764
765 iph->protocol = IPPROTO_IPV6;
766 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
767 iph->daddr = rt->rt_dst;
768 iph->saddr = rt->rt_src;
769
770 if ((iph->ttl = tiph->ttl) == 0)
771 iph->ttl = iph6->hop_limit;
772
773 nf_reset(skb);
774
775 IPTUNNEL_XMIT();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000776 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778tx_error_icmp:
779 dst_link_failure(skb);
780tx_error:
781 stats->tx_errors++;
782 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000783 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800786static void ipip6_tunnel_bind_dev(struct net_device *dev)
787{
788 struct net_device *tdev = NULL;
789 struct ip_tunnel *tunnel;
790 struct iphdr *iph;
791
792 tunnel = netdev_priv(dev);
793 iph = &tunnel->parms.iph;
794
795 if (iph->daddr) {
796 struct flowi fl = { .nl_u = { .ip4_u =
797 { .daddr = iph->daddr,
798 .saddr = iph->saddr,
799 .tos = RT_TOS(iph->tos) } },
800 .oif = tunnel->parms.link,
801 .proto = IPPROTO_IPV6 };
802 struct rtable *rt;
Pavel Emelyanov907a08c2008-04-16 01:16:58 -0700803 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800804 tdev = rt->u.dst.dev;
805 ip_rt_put(rt);
806 }
807 dev->flags |= IFF_POINTOPOINT;
808 }
809
810 if (!tdev && tunnel->parms.link)
Pavel Emelyanov907a08c2008-04-16 01:16:58 -0700811 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800812
813 if (tdev) {
814 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
815 dev->mtu = tdev->mtu - sizeof(struct iphdr);
816 if (dev->mtu < IPV6_MIN_MTU)
817 dev->mtu = IPV6_MIN_MTU;
818 }
819 dev->iflink = tunnel->parms.link;
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static int
823ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
824{
825 int err = 0;
826 struct ip_tunnel_parm p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400827 struct ip_tunnel_prl prl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct ip_tunnel *t;
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700829 struct net *net = dev_net(dev);
830 struct sit_net *sitn = net_generic(net, sit_net_id);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000831#ifdef CONFIG_IPV6_SIT_6RD
832 struct ip_tunnel_6rd ip6rd;
833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 switch (cmd) {
836 case SIOCGETTUNNEL:
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000837#ifdef CONFIG_IPV6_SIT_6RD
838 case SIOCGET6RD:
839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 t = NULL;
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700841 if (dev == sitn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
843 err = -EFAULT;
844 break;
845 }
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700846 t = ipip6_tunnel_locate(net, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800849 t = netdev_priv(dev);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000850
851 err = -EFAULT;
852 if (cmd == SIOCGETTUNNEL) {
853 memcpy(&p, &t->parms, sizeof(p));
854 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
855 sizeof(p)))
856 goto done;
857#ifdef CONFIG_IPV6_SIT_6RD
858 } else {
859 ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
860 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
861 ip6rd.prefixlen = t->ip6rd.prefixlen;
862 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
863 if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
864 sizeof(ip6rd)))
865 goto done;
866#endif
867 }
868 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
870
871 case SIOCADDTUNNEL:
872 case SIOCCHGTUNNEL:
873 err = -EPERM;
874 if (!capable(CAP_NET_ADMIN))
875 goto done;
876
877 err = -EFAULT;
878 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
879 goto done;
880
881 err = -EINVAL;
882 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
883 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
884 goto done;
885 if (p.iph.ttl)
886 p.iph.frag_off |= htons(IP_DF);
887
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700888 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700890 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (t != NULL) {
892 if (t->dev != dev) {
893 err = -EEXIST;
894 break;
895 }
896 } else {
897 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
898 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
899 err = -EINVAL;
900 break;
901 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800902 t = netdev_priv(dev);
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700903 ipip6_tunnel_unlink(sitn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 t->parms.iph.saddr = p.iph.saddr;
905 t->parms.iph.daddr = p.iph.daddr;
906 memcpy(dev->dev_addr, &p.iph.saddr, 4);
907 memcpy(dev->broadcast, &p.iph.daddr, 4);
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700908 ipip6_tunnel_link(sitn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 netdev_state_change(dev);
910 }
911 }
912
913 if (t) {
914 err = 0;
915 if (cmd == SIOCCHGTUNNEL) {
916 t->parms.iph.ttl = p.iph.ttl;
917 t->parms.iph.tos = p.iph.tos;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800918 if (t->parms.link != p.link) {
919 t->parms.link = p.link;
920 ipip6_tunnel_bind_dev(dev);
921 netdev_state_change(dev);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
925 err = -EFAULT;
926 } else
927 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
928 break;
929
930 case SIOCDELTUNNEL:
931 err = -EPERM;
932 if (!capable(CAP_NET_ADMIN))
933 goto done;
934
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700935 if (dev == sitn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 err = -EFAULT;
937 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
938 goto done;
939 err = -ENOENT;
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700940 if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto done;
942 err = -EPERM;
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700943 if (t == netdev_priv(sitn->fb_tunnel_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto done;
945 dev = t->dev;
946 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -0800947 unregister_netdevice(dev);
948 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900951 case SIOCGETPRL:
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700952 err = -EINVAL;
953 if (dev == sitn->fb_tunnel_dev)
954 goto done;
955 err = -ENOENT;
956 if (!(t = netdev_priv(dev)))
957 goto done;
958 err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
959 break;
960
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400961 case SIOCADDPRL:
962 case SIOCDELPRL:
963 case SIOCCHGPRL:
964 err = -EPERM;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700965 if (!capable(CAP_NET_ADMIN))
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400966 goto done;
967 err = -EINVAL;
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700968 if (dev == sitn->fb_tunnel_dev)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400969 goto done;
970 err = -EFAULT;
971 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
972 goto done;
973 err = -ENOENT;
974 if (!(t = netdev_priv(dev)))
975 goto done;
976
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900977 switch (cmd) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900978 case SIOCDELPRL:
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400979 err = ipip6_tunnel_del_prl(t, &prl);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900980 break;
981 case SIOCADDPRL:
982 case SIOCCHGPRL:
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400983 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900984 break;
985 }
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700986 netdev_state_change(dev);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400987 break;
988
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000989#ifdef CONFIG_IPV6_SIT_6RD
990 case SIOCADD6RD:
991 case SIOCCHG6RD:
992 case SIOCDEL6RD:
993 err = -EPERM;
994 if (!capable(CAP_NET_ADMIN))
995 goto done;
996
997 err = -EFAULT;
998 if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
999 sizeof(ip6rd)))
1000 goto done;
1001
1002 t = netdev_priv(dev);
1003
1004 if (cmd != SIOCDEL6RD) {
1005 struct in6_addr prefix;
1006 __be32 relay_prefix;
1007
1008 err = -EINVAL;
1009 if (ip6rd.relay_prefixlen > 32 ||
1010 ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
1011 goto done;
1012
1013 ipv6_addr_prefix(&prefix, &ip6rd.prefix,
1014 ip6rd.prefixlen);
1015 if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
1016 goto done;
YOSHIFUJI Hideaki / 吉藤英明91b2a3f2009-10-11 03:45:13 +00001017 if (ip6rd.relay_prefixlen)
1018 relay_prefix = ip6rd.relay_prefix &
1019 htonl(0xffffffffUL <<
1020 (32 - ip6rd.relay_prefixlen));
1021 else
1022 relay_prefix = 0;
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +00001023 if (relay_prefix != ip6rd.relay_prefix)
1024 goto done;
1025
1026 ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
1027 t->ip6rd.relay_prefix = relay_prefix;
1028 t->ip6rd.prefixlen = ip6rd.prefixlen;
1029 t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
1030 } else
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +00001031 ipip6_tunnel_clone_6rd(dev, sitn);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +00001032
1033 err = 0;
1034 break;
1035#endif
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 default:
1038 err = -EINVAL;
1039 }
1040
1041done:
1042 return err;
1043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1046{
1047 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
1048 return -EINVAL;
1049 dev->mtu = new_mtu;
1050 return 0;
1051}
1052
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001053static const struct net_device_ops ipip6_netdev_ops = {
1054 .ndo_uninit = ipip6_tunnel_uninit,
1055 .ndo_start_xmit = ipip6_tunnel_xmit,
1056 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1057 .ndo_change_mtu = ipip6_tunnel_change_mtu,
1058};
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static void ipip6_tunnel_setup(struct net_device *dev)
1061{
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001062 dev->netdev_ops = &ipip6_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 dev->type = ARPHRD_SIT;
1066 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
Kris Katterjohn46f25df2006-01-05 16:35:42 -08001067 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 dev->flags = IFF_NOARP;
Sascha Hlusiakf2ba0252009-07-11 20:30:52 -07001069 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 dev->iflink = 0;
1071 dev->addr_len = 4;
Pavel Emelyanov7a971462008-04-16 01:17:18 -07001072 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001075static void ipip6_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001077 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 tunnel->dev = dev;
1080 strcpy(tunnel->parms.name, dev->name);
1081
1082 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1083 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1084
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001085 ipip6_tunnel_bind_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001088static void ipip6_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Patrick McHardy2941a482006-01-08 22:05:26 -08001090 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 struct iphdr *iph = &tunnel->parms.iph;
Pavel Emelyanov29182172008-04-16 01:16:38 -07001092 struct net *net = dev_net(dev);
1093 struct sit_net *sitn = net_generic(net, sit_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 tunnel->dev = dev;
1096 strcpy(tunnel->parms.name, dev->name);
1097
1098 iph->version = 4;
1099 iph->protocol = IPPROTO_IPV6;
1100 iph->ihl = 5;
1101 iph->ttl = 64;
1102
1103 dev_hold(dev);
Pavel Emelyanov29182172008-04-16 01:16:38 -07001104 sitn->tunnels_wc[0] = tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001107static struct xfrm_tunnel sit_handler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 .handler = ipip6_rcv,
1109 .err_handler = ipip6_err,
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001110 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111};
1112
Pavel Emelyanov29182172008-04-16 01:16:38 -07001113static void sit_destroy_tunnels(struct sit_net *sitn)
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001114{
1115 int prio;
1116
1117 for (prio = 1; prio < 4; prio++) {
1118 int h;
1119 for (h = 0; h < HASH_SIZE; h++) {
1120 struct ip_tunnel *t;
Pavel Emelyanov29182172008-04-16 01:16:38 -07001121 while ((t = sitn->tunnels[prio][h]) != NULL)
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001122 unregister_netdevice(t->dev);
1123 }
1124 }
1125}
1126
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001127static int sit_init_net(struct net *net)
1128{
1129 int err;
1130 struct sit_net *sitn;
1131
1132 err = -ENOMEM;
Pavel Emelyanov29182172008-04-16 01:16:38 -07001133 sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001134 if (sitn == NULL)
1135 goto err_alloc;
1136
1137 err = net_assign_generic(net, sit_net_id, sitn);
1138 if (err < 0)
1139 goto err_assign;
1140
Pavel Emelyanov29182172008-04-16 01:16:38 -07001141 sitn->tunnels[0] = sitn->tunnels_wc;
1142 sitn->tunnels[1] = sitn->tunnels_l;
1143 sitn->tunnels[2] = sitn->tunnels_r;
1144 sitn->tunnels[3] = sitn->tunnels_r_l;
1145
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001146 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1147 ipip6_tunnel_setup);
1148 if (!sitn->fb_tunnel_dev) {
1149 err = -ENOMEM;
1150 goto err_alloc_dev;
1151 }
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001152 dev_net_set(sitn->fb_tunnel_dev, net);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001153
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001154 ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +00001155 ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001156
1157 if ((err = register_netdev(sitn->fb_tunnel_dev)))
1158 goto err_reg_dev;
1159
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001160 return 0;
1161
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001162err_reg_dev:
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001163 dev_put(sitn->fb_tunnel_dev);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001164 free_netdev(sitn->fb_tunnel_dev);
1165err_alloc_dev:
1166 /* nothing */
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001167err_assign:
1168 kfree(sitn);
1169err_alloc:
1170 return err;
1171}
1172
1173static void sit_exit_net(struct net *net)
1174{
1175 struct sit_net *sitn;
1176
1177 sitn = net_generic(net, sit_net_id);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001178 rtnl_lock();
Pavel Emelyanov29182172008-04-16 01:16:38 -07001179 sit_destroy_tunnels(sitn);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001180 unregister_netdevice(sitn->fb_tunnel_dev);
1181 rtnl_unlock();
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001182 kfree(sitn);
1183}
1184
1185static struct pernet_operations sit_net_ops = {
1186 .init = sit_init_net,
1187 .exit = sit_exit_net,
1188};
1189
Adrian Bunk89c89452006-11-20 16:56:48 -08001190static void __exit sit_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001192 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001193
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001194 unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Adrian Bunk89c89452006-11-20 16:56:48 -08001197static int __init sit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 int err;
1200
1201 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1202
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001203 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 printk(KERN_INFO "sit init: Can't add protocol\n");
1205 return -EAGAIN;
1206 }
1207
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001208 err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
1209 if (err < 0)
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001210 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
Joerg Roedel989e5b92006-10-10 14:47:44 -07001214
1215module_init(sit_init);
1216module_exit(sit_cleanup);
Jan Dittmer39c85082006-10-13 15:05:53 -07001217MODULE_LICENSE("GPL");
Patrick McHardydaccff02006-11-05 15:47:04 -08001218MODULE_ALIAS("sit0");