blob: 08a483a8de5083dda156e24b64c6f1805cf980f6 [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 *
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
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -040019 * Fred Templin <fred.l.templin@boeing.com>: isatap support
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080023#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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 Katterjohn46f25df2006-01-05 16:35:42 -080036#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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 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
65static int ipip6_fb_tunnel_init(struct net_device *dev);
66static int ipip6_tunnel_init(struct net_device *dev);
67static void ipip6_tunnel_setup(struct net_device *dev);
68
69static struct net_device *ipip6_fb_tunnel_dev;
70
71static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
72static struct ip_tunnel *tunnels_r[HASH_SIZE];
73static struct ip_tunnel *tunnels_l[HASH_SIZE];
74static struct ip_tunnel *tunnels_wc[1];
75static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
76
77static DEFINE_RWLOCK(ipip6_lock);
78
Al Viroe69a4ad2006-11-14 20:56:00 -080079static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
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 Hideaki420fe232007-04-24 20:44:47 +0900103static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900105 __be32 remote = parms->iph.daddr;
106 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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 Hideaki420fe232007-04-24 20:44:47 +0900121static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
122{
123 return __ipip6_bucket(&t->parms);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static 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
140static 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
150static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
151{
Al Viroe69a4ad2006-11-14 20:56:00 -0800152 __be32 remote = parms->iph.daddr;
153 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct ip_tunnel *t, **tp, *nt;
155 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 char name[IFNAMSIZ];
157
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900158 for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800167 else
168 sprintf(name, "sit%%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
171 if (dev == NULL)
172 return NULL;
173
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800174 if (strchr(name, '%')) {
175 if (dev_alloc_name(dev, name) < 0)
176 goto failed_free;
177 }
178
Patrick McHardy2941a482006-01-08 22:05:26 -0800179 nt = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dev->init = ipip6_tunnel_init;
181 nt->parms = *parms;
182
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100183 if (parms->i_flags & SIT_ISATAP)
184 dev->priv_flags |= IFF_ISATAP;
185
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800186 if (register_netdevice(dev) < 0)
187 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 dev_hold(dev);
190
191 ipip6_tunnel_link(nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return nt;
193
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800194failed_free:
195 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196failed:
197 return NULL;
198}
199
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400200static struct ip_tunnel_prl_entry *
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900201__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400202{
203 struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
204
205 for (p = t->prl; p; p = p->next)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900206 if (p->addr == addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400207 break;
208 return p;
209
210}
211
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900212static int ipip6_tunnel_get_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
213{
214 struct ip_tunnel_prl *kp;
215 struct ip_tunnel_prl_entry *prl;
216 unsigned int cmax, c = 0, ca, len;
217 int ret = 0;
218
219 cmax = a->datalen / sizeof(*a);
220 if (cmax > 1 && a->addr != htonl(INADDR_ANY))
221 cmax = 1;
222
223 /* For simple GET or for root users,
224 * we try harder to allocate.
225 */
226 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
227 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
228 NULL;
229
230 read_lock(&ipip6_lock);
231
232 ca = t->prl_count < cmax ? t->prl_count : cmax;
233
234 if (!kp) {
235 /* We don't try hard to allocate much memory for
236 * non-root users.
237 * For root users, retry allocating enough memory for
238 * the answer.
239 */
240 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
241 if (!kp) {
242 ret = -ENOMEM;
243 goto out;
244 }
245 }
246
247 c = 0;
248 for (prl = t->prl; prl; prl = prl->next) {
249 if (c > cmax)
250 break;
251 if (a->addr != htonl(INADDR_ANY) && prl->addr != a->addr)
252 continue;
253 kp[c].addr = prl->addr;
254 kp[c].flags = prl->flags;
255 c++;
256 if (a->addr != htonl(INADDR_ANY))
257 break;
258 }
259out:
260 read_unlock(&ipip6_lock);
261
262 len = sizeof(*kp) * c;
263 ret = len ? copy_to_user(a->data, kp, len) : 0;
264
265 kfree(kp);
266 if (ret)
267 return -EFAULT;
268
269 a->datalen = len;
270 return 0;
271}
272
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400273static int
274ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
275{
276 struct ip_tunnel_prl_entry *p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900277 int err = 0;
278
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900279 if (a->addr == htonl(INADDR_ANY))
280 return -EINVAL;
281
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900282 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400283
284 for (p = t->prl; p; p = p->next) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900285 if (p->addr == a->addr) {
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900286 if (chg)
287 goto update;
288 err = -EEXIST;
289 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400290 }
291 }
292
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900293 if (chg) {
294 err = -ENXIO;
295 goto out;
296 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400297
298 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900299 if (!p) {
300 err = -ENOBUFS;
301 goto out;
302 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400303
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400304 p->next = t->prl;
305 t->prl = p;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900306 t->prl_count++;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900307update:
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900308 p->addr = a->addr;
309 p->flags = a->flags;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900310out:
311 write_unlock(&ipip6_lock);
312 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400313}
314
315static int
316ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
317{
318 struct ip_tunnel_prl_entry *x, **p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900319 int err = 0;
320
321 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400322
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900323 if (a && a->addr != htonl(INADDR_ANY)) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400324 for (p = &t->prl; *p; p = &(*p)->next) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900325 if ((*p)->addr == a->addr) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400326 x = *p;
327 *p = x->next;
328 kfree(x);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900329 t->prl_count--;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900330 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400331 }
332 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900333 err = -ENXIO;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400334 } else {
335 while (t->prl) {
336 x = t->prl;
337 t->prl = t->prl->next;
338 kfree(x);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900339 t->prl_count--;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400340 }
341 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900342out:
343 write_unlock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400344 return 0;
345}
346
347/* copied directly from anycast.c */
348static int
349ipip6_onlink(struct in6_addr *addr, struct net_device *dev)
350{
351 struct inet6_dev *idev;
352 struct inet6_ifaddr *ifa;
353 int onlink;
354
355 onlink = 0;
356 rcu_read_lock();
357 idev = __in6_dev_get(dev);
358 if (idev) {
359 read_lock_bh(&idev->lock);
360 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
361 onlink = ipv6_prefix_equal(addr, &ifa->addr,
362 ifa->prefix_len);
363 if (onlink)
364 break;
365 }
366 read_unlock_bh(&idev->lock);
367 }
368 rcu_read_unlock();
369 return onlink;
370}
371
372static int
373isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
374{
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900375 struct ip_tunnel_prl_entry *p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400376 int ok = 1;
377
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900378 read_lock(&ipip6_lock);
379 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400380 if (p) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900381 if (p->flags & PRL_DEFAULT)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400382 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
383 else
384 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
385 } else {
386 struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
387 if (ipv6_addr_is_isatap(addr6) &&
388 (addr6->s6_addr32[3] == iph->saddr) &&
389 ipip6_onlink(addr6, t->dev))
390 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
391 else
392 ok = 0;
393 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900394 read_unlock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400395 return ok;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static void ipip6_tunnel_uninit(struct net_device *dev)
399{
400 if (dev == ipip6_fb_tunnel_dev) {
401 write_lock_bh(&ipip6_lock);
402 tunnels_wc[0] = NULL;
403 write_unlock_bh(&ipip6_lock);
404 dev_put(dev);
405 } else {
Patrick McHardy2941a482006-01-08 22:05:26 -0800406 ipip6_tunnel_unlink(netdev_priv(dev));
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400407 ipip6_tunnel_del_prl(netdev_priv(dev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev_put(dev);
409 }
410}
411
412
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800413static int ipip6_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415#ifndef I_WISH_WORLD_WERE_PERFECT
416
417/* It is not :-( All the routers (except for Linux) return only
418 8 bytes of packet payload. It means, that precise relaying of
419 ICMP in the real Internet is absolutely infeasible.
420 */
421 struct iphdr *iph = (struct iphdr*)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300422 const int type = icmp_hdr(skb)->type;
423 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct ip_tunnel *t;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800425 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 switch (type) {
428 default:
429 case ICMP_PARAMETERPROB:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 case ICMP_DEST_UNREACH:
433 switch (code) {
434 case ICMP_SR_FAILED:
435 case ICMP_PORT_UNREACH:
436 /* Impossible event. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 case ICMP_FRAG_NEEDED:
439 /* Soft state for pmtu is maintained by IP core. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 default:
442 /* All others are translated to HOST_UNREACH.
443 rfc2003 contains "deep thoughts" about NET_UNREACH,
444 I believe they are just ether pollution. --ANK
445 */
446 break;
447 }
448 break;
449 case ICMP_TIME_EXCEEDED:
450 if (code != ICMP_EXC_TTL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 }
454
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800455 err = -ENOENT;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 read_lock(&ipip6_lock);
458 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
459 if (t == NULL || t->parms.iph.daddr == 0)
460 goto out;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800461
462 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
464 goto out;
465
466 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
467 t->err_count++;
468 else
469 t->err_count = 1;
470 t->err_time = jiffies;
471out:
472 read_unlock(&ipip6_lock);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#else
475 struct iphdr *iph = (struct iphdr*)dp;
476 int hlen = iph->ihl<<2;
477 struct ipv6hdr *iph6;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300478 const int type = icmp_hdr(skb)->type;
479 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int rel_type = 0;
481 int rel_code = 0;
482 int rel_info = 0;
483 struct sk_buff *skb2;
484 struct rt6_info *rt6i;
485
486 if (len < hlen + sizeof(struct ipv6hdr))
487 return;
488 iph6 = (struct ipv6hdr*)(dp + hlen);
489
490 switch (type) {
491 default:
492 return;
493 case ICMP_PARAMETERPROB:
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300494 if (icmp_hdr(skb)->un.gateway < hlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return;
496
497 /* So... This guy found something strange INSIDE encapsulated
498 packet. Well, he is fool, but what can we do ?
499 */
500 rel_type = ICMPV6_PARAMPROB;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300501 rel_info = icmp_hdr(skb)->un.gateway - hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503
504 case ICMP_DEST_UNREACH:
505 switch (code) {
506 case ICMP_SR_FAILED:
507 case ICMP_PORT_UNREACH:
508 /* Impossible event. */
509 return;
510 case ICMP_FRAG_NEEDED:
511 /* Too complicated case ... */
512 return;
513 default:
514 /* All others are translated to HOST_UNREACH.
515 rfc2003 contains "deep thoughts" about NET_UNREACH,
516 I believe, it is just ether pollution. --ANK
517 */
518 rel_type = ICMPV6_DEST_UNREACH;
519 rel_code = ICMPV6_ADDR_UNREACH;
520 break;
521 }
522 break;
523 case ICMP_TIME_EXCEEDED:
524 if (code != ICMP_EXC_TTL)
525 return;
526 rel_type = ICMPV6_TIME_EXCEED;
527 rel_code = ICMPV6_EXC_HOPLIMIT;
528 break;
529 }
530
531 /* Prepare fake skb to feed it to icmpv6_send */
532 skb2 = skb_clone(skb, GFP_ATOMIC);
533 if (skb2 == NULL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dst_release(skb2->dst);
536 skb2->dst = NULL;
537 skb_pull(skb2, skb->data - (u8*)iph6);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700538 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Try to guess incoming interface */
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800541 rt6i = rt6_lookup(&init_net, &iph6->saddr, NULL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (rt6i && rt6i->rt6i_dev) {
543 skb2->dev = rt6i->rt6i_dev;
544
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800545 rt6i = rt6_lookup(&init_net, &iph6->daddr, &iph6->saddr, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
Patrick McHardy2941a482006-01-08 22:05:26 -0800548 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
550 rel_type = ICMPV6_DEST_UNREACH;
551 rel_code = ICMPV6_ADDR_UNREACH;
552 }
553 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
554 }
555 }
556 kfree_skb(skb2);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
559}
560
561static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
562{
563 if (INET_ECN_is_ce(iph->tos))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700564 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567static int ipip6_rcv(struct sk_buff *skb)
568{
569 struct iphdr *iph;
570 struct ip_tunnel *tunnel;
571
572 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
573 goto out;
574
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700575 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 read_lock(&ipip6_lock);
578 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
579 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700580 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700581 skb_reset_network_header(skb);
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800582 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 skb->protocol = htons(ETH_P_IPV6);
584 skb->pkt_type = PACKET_HOST;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100585
586 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400587 !isatap_chksrc(skb, iph, tunnel)) {
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100588 tunnel->stat.rx_errors++;
589 read_unlock(&ipip6_lock);
590 kfree_skb(skb);
591 return 0;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 tunnel->stat.rx_packets++;
594 tunnel->stat.rx_bytes += skb->len;
595 skb->dev = tunnel->dev;
596 dst_release(skb->dst);
597 skb->dst = NULL;
598 nf_reset(skb);
599 ipip6_ecn_decapsulate(iph, skb);
600 netif_rx(skb);
601 read_unlock(&ipip6_lock);
602 return 0;
603 }
604
Herbert Xu45af08b2006-04-05 22:31:19 -0700605 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 kfree_skb(skb);
607 read_unlock(&ipip6_lock);
608out:
609 return 0;
610}
611
612/* Returns the embedded IPv4 address if the IPv6 address
613 comes from 6to4 (RFC 3056) addr space */
614
Al Viroe69a4ad2006-11-14 20:56:00 -0800615static inline __be32 try_6to4(struct in6_addr *v6dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Al Viroe69a4ad2006-11-14 20:56:00 -0800617 __be32 dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (v6dst->s6_addr16[0] == htons(0x2002)) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900620 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 memcpy(&dst, &v6dst->s6_addr16[1], 4);
622 }
623 return dst;
624}
625
626/*
627 * This function assumes it is being called from dev_queue_xmit()
628 * and that skb is filled properly by that function.
629 */
630
631static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
632{
Patrick McHardy2941a482006-01-08 22:05:26 -0800633 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct net_device_stats *stats = &tunnel->stat;
635 struct iphdr *tiph = &tunnel->parms.iph;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700636 struct ipv6hdr *iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 u8 tos = tunnel->parms.iph.tos;
638 struct rtable *rt; /* Route to the other host */
639 struct net_device *tdev; /* Device to other host */
640 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700641 unsigned int max_headroom; /* The extra header space needed */
Al Viroe69a4ad2006-11-14 20:56:00 -0800642 __be32 dst = tiph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644 struct in6_addr *addr6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int addr_type;
646
647 if (tunnel->recursion++) {
648 tunnel->stat.collisions++;
649 goto tx_error;
650 }
651
652 if (skb->protocol != htons(ETH_P_IPV6))
653 goto tx_error;
654
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100655 /* ISATAP (RFC4214) - must come before 6to4 */
656 if (dev->priv_flags & IFF_ISATAP) {
657 struct neighbour *neigh = NULL;
658
659 if (skb->dst)
660 neigh = skb->dst->neighbour;
661
662 if (neigh == NULL) {
663 if (net_ratelimit())
664 printk(KERN_DEBUG "sit: nexthop == NULL\n");
665 goto tx_error;
666 }
667
668 addr6 = (struct in6_addr*)&neigh->primary_key;
669 addr_type = ipv6_addr_type(addr6);
670
671 if ((addr_type & IPV6_ADDR_UNICAST) &&
672 ipv6_addr_is_isatap(addr6))
673 dst = addr6->s6_addr32[3];
674 else
675 goto tx_error;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!dst)
679 dst = try_6to4(&iph6->daddr);
680
681 if (!dst) {
682 struct neighbour *neigh = NULL;
683
684 if (skb->dst)
685 neigh = skb->dst->neighbour;
686
687 if (neigh == NULL) {
688 if (net_ratelimit())
689 printk(KERN_DEBUG "sit: nexthop == NULL\n");
690 goto tx_error;
691 }
692
693 addr6 = (struct in6_addr*)&neigh->primary_key;
694 addr_type = ipv6_addr_type(addr6);
695
696 if (addr_type == IPV6_ADDR_ANY) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700697 addr6 = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 addr_type = ipv6_addr_type(addr6);
699 }
700
701 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
702 goto tx_error_icmp;
703
704 dst = addr6->s6_addr32[3];
705 }
706
707 {
708 struct flowi fl = { .nl_u = { .ip4_u =
709 { .daddr = dst,
710 .saddr = tiph->saddr,
711 .tos = RT_TOS(tos) } },
712 .oif = tunnel->parms.link,
713 .proto = IPPROTO_IPV6 };
Denis V. Lunevf2063512008-01-22 22:07:34 -0800714 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 tunnel->stat.tx_carrier_errors++;
716 goto tx_error_icmp;
717 }
718 }
719 if (rt->rt_type != RTN_UNICAST) {
720 ip_rt_put(rt);
721 tunnel->stat.tx_carrier_errors++;
722 goto tx_error_icmp;
723 }
724 tdev = rt->u.dst.dev;
725
726 if (tdev == dev) {
727 ip_rt_put(rt);
728 tunnel->stat.collisions++;
729 goto tx_error;
730 }
731
732 if (tiph->frag_off)
733 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
734 else
735 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
736
737 if (mtu < 68) {
738 tunnel->stat.collisions++;
739 ip_rt_put(rt);
740 goto tx_error;
741 }
742 if (mtu < IPV6_MIN_MTU)
743 mtu = IPV6_MIN_MTU;
744 if (tunnel->parms.iph.daddr && skb->dst)
745 skb->dst->ops->update_pmtu(skb->dst, mtu);
746
747 if (skb->len > mtu) {
748 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
749 ip_rt_put(rt);
750 goto tx_error;
751 }
752
753 if (tunnel->err_count > 0) {
754 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
755 tunnel->err_count--;
756 dst_link_failure(skb);
757 } else
758 tunnel->err_count = 0;
759 }
760
761 /*
762 * Okay, now see if we can stuff it in the buffer as-is.
763 */
764 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
765
Patrick McHardycfbba492007-07-09 15:33:40 -0700766 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
767 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
769 if (!new_skb) {
770 ip_rt_put(rt);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900771 stats->tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dev_kfree_skb(skb);
773 tunnel->recursion--;
774 return 0;
775 }
776 if (skb->sk)
777 skb_set_owner_w(new_skb, skb->sk);
778 dev_kfree_skb(skb);
779 skb = new_skb;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700780 iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700783 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700784 skb_push(skb, sizeof(struct iphdr));
785 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800787 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dst_release(skb->dst);
789 skb->dst = &rt->u.dst;
790
791 /*
792 * Push down and install the IPIP header.
793 */
794
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700795 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 iph->version = 4;
797 iph->ihl = sizeof(struct iphdr)>>2;
798 if (mtu > IPV6_MIN_MTU)
799 iph->frag_off = htons(IP_DF);
800 else
801 iph->frag_off = 0;
802
803 iph->protocol = IPPROTO_IPV6;
804 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
805 iph->daddr = rt->rt_dst;
806 iph->saddr = rt->rt_src;
807
808 if ((iph->ttl = tiph->ttl) == 0)
809 iph->ttl = iph6->hop_limit;
810
811 nf_reset(skb);
812
813 IPTUNNEL_XMIT();
814 tunnel->recursion--;
815 return 0;
816
817tx_error_icmp:
818 dst_link_failure(skb);
819tx_error:
820 stats->tx_errors++;
821 dev_kfree_skb(skb);
822 tunnel->recursion--;
823 return 0;
824}
825
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800826static void ipip6_tunnel_bind_dev(struct net_device *dev)
827{
828 struct net_device *tdev = NULL;
829 struct ip_tunnel *tunnel;
830 struct iphdr *iph;
831
832 tunnel = netdev_priv(dev);
833 iph = &tunnel->parms.iph;
834
835 if (iph->daddr) {
836 struct flowi fl = { .nl_u = { .ip4_u =
837 { .daddr = iph->daddr,
838 .saddr = iph->saddr,
839 .tos = RT_TOS(iph->tos) } },
840 .oif = tunnel->parms.link,
841 .proto = IPPROTO_IPV6 };
842 struct rtable *rt;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800843 if (!ip_route_output_key(&init_net, &rt, &fl)) {
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800844 tdev = rt->u.dst.dev;
845 ip_rt_put(rt);
846 }
847 dev->flags |= IFF_POINTOPOINT;
848 }
849
850 if (!tdev && tunnel->parms.link)
851 tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
852
853 if (tdev) {
854 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
855 dev->mtu = tdev->mtu - sizeof(struct iphdr);
856 if (dev->mtu < IPV6_MIN_MTU)
857 dev->mtu = IPV6_MIN_MTU;
858 }
859 dev->iflink = tunnel->parms.link;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static int
863ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
864{
865 int err = 0;
866 struct ip_tunnel_parm p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400867 struct ip_tunnel_prl prl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct ip_tunnel *t;
869
870 switch (cmd) {
871 case SIOCGETTUNNEL:
872 t = NULL;
873 if (dev == ipip6_fb_tunnel_dev) {
874 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
875 err = -EFAULT;
876 break;
877 }
878 t = ipip6_tunnel_locate(&p, 0);
879 }
880 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800881 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 memcpy(&p, &t->parms, sizeof(p));
883 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
884 err = -EFAULT;
885 break;
886
887 case SIOCADDTUNNEL:
888 case SIOCCHGTUNNEL:
889 err = -EPERM;
890 if (!capable(CAP_NET_ADMIN))
891 goto done;
892
893 err = -EFAULT;
894 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
895 goto done;
896
897 err = -EINVAL;
898 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
899 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
900 goto done;
901 if (p.iph.ttl)
902 p.iph.frag_off |= htons(IP_DF);
903
904 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
905
906 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
907 if (t != NULL) {
908 if (t->dev != dev) {
909 err = -EEXIST;
910 break;
911 }
912 } else {
913 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
914 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
915 err = -EINVAL;
916 break;
917 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800918 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ipip6_tunnel_unlink(t);
920 t->parms.iph.saddr = p.iph.saddr;
921 t->parms.iph.daddr = p.iph.daddr;
922 memcpy(dev->dev_addr, &p.iph.saddr, 4);
923 memcpy(dev->broadcast, &p.iph.daddr, 4);
924 ipip6_tunnel_link(t);
925 netdev_state_change(dev);
926 }
927 }
928
929 if (t) {
930 err = 0;
931 if (cmd == SIOCCHGTUNNEL) {
932 t->parms.iph.ttl = p.iph.ttl;
933 t->parms.iph.tos = p.iph.tos;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800934 if (t->parms.link != p.link) {
935 t->parms.link = p.link;
936 ipip6_tunnel_bind_dev(dev);
937 netdev_state_change(dev);
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
941 err = -EFAULT;
942 } else
943 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
944 break;
945
946 case SIOCDELTUNNEL:
947 err = -EPERM;
948 if (!capable(CAP_NET_ADMIN))
949 goto done;
950
951 if (dev == ipip6_fb_tunnel_dev) {
952 err = -EFAULT;
953 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
954 goto done;
955 err = -ENOENT;
956 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
957 goto done;
958 err = -EPERM;
Patrick McHardy2941a482006-01-08 22:05:26 -0800959 if (t == netdev_priv(ipip6_fb_tunnel_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 goto done;
961 dev = t->dev;
962 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -0800963 unregister_netdevice(dev);
964 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900967 case SIOCGETPRL:
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400968 case SIOCADDPRL:
969 case SIOCDELPRL:
970 case SIOCCHGPRL:
971 err = -EPERM;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900972 if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN))
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400973 goto done;
974 err = -EINVAL;
975 if (dev == ipip6_fb_tunnel_dev)
976 goto done;
977 err = -EFAULT;
978 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
979 goto done;
980 err = -ENOENT;
981 if (!(t = netdev_priv(dev)))
982 goto done;
983
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900984 switch (cmd) {
985 case SIOCGETPRL:
986 err = ipip6_tunnel_get_prl(t, &prl);
987 if (!err && copy_to_user(ifr->ifr_ifru.ifru_data,
988 &prl, sizeof(prl)))
989 err = -EFAULT;
990 break;
991 case SIOCDELPRL:
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400992 err = ipip6_tunnel_del_prl(t, &prl);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900993 break;
994 case SIOCADDPRL:
995 case SIOCCHGPRL:
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400996 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900997 break;
998 }
999 if (cmd != SIOCGETPRL)
1000 netdev_state_change(dev);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001001 break;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 default:
1004 err = -EINVAL;
1005 }
1006
1007done:
1008 return err;
1009}
1010
1011static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
1012{
Patrick McHardy2941a482006-01-08 22:05:26 -08001013 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1017{
1018 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
1019 return -EINVAL;
1020 dev->mtu = new_mtu;
1021 return 0;
1022}
1023
1024static void ipip6_tunnel_setup(struct net_device *dev)
1025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 dev->uninit = ipip6_tunnel_uninit;
1027 dev->destructor = free_netdev;
1028 dev->hard_start_xmit = ipip6_tunnel_xmit;
1029 dev->get_stats = ipip6_tunnel_get_stats;
1030 dev->do_ioctl = ipip6_tunnel_ioctl;
1031 dev->change_mtu = ipip6_tunnel_change_mtu;
1032
1033 dev->type = ARPHRD_SIT;
1034 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
Kris Katterjohn46f25df2006-01-05 16:35:42 -08001035 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 dev->flags = IFF_NOARP;
1037 dev->iflink = 0;
1038 dev->addr_len = 4;
1039}
1040
1041static int ipip6_tunnel_init(struct net_device *dev)
1042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Patrick McHardy2941a482006-01-08 22:05:26 -08001045 tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 tunnel->dev = dev;
1048 strcpy(tunnel->parms.name, dev->name);
1049
1050 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1051 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1052
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001053 ipip6_tunnel_bind_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 return 0;
1056}
1057
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001058static int __init ipip6_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Patrick McHardy2941a482006-01-08 22:05:26 -08001060 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct iphdr *iph = &tunnel->parms.iph;
1062
1063 tunnel->dev = dev;
1064 strcpy(tunnel->parms.name, dev->name);
1065
1066 iph->version = 4;
1067 iph->protocol = IPPROTO_IPV6;
1068 iph->ihl = 5;
1069 iph->ttl = 64;
1070
1071 dev_hold(dev);
1072 tunnels_wc[0] = tunnel;
1073 return 0;
1074}
1075
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001076static struct xfrm_tunnel sit_handler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 .handler = ipip6_rcv,
1078 .err_handler = ipip6_err,
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001079 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080};
1081
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001082static void __exit sit_destroy_tunnels(void)
1083{
1084 int prio;
1085
1086 for (prio = 1; prio < 4; prio++) {
1087 int h;
1088 for (h = 0; h < HASH_SIZE; h++) {
1089 struct ip_tunnel *t;
1090 while ((t = tunnels[prio][h]) != NULL)
1091 unregister_netdevice(t->dev);
1092 }
1093 }
1094}
1095
Adrian Bunk89c89452006-11-20 16:56:48 -08001096static void __exit sit_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001098 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001099
1100 rtnl_lock();
1101 sit_destroy_tunnels();
1102 unregister_netdevice(ipip6_fb_tunnel_dev);
1103 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Adrian Bunk89c89452006-11-20 16:56:48 -08001106static int __init sit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 int err;
1109
1110 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1111
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001112 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 printk(KERN_INFO "sit init: Can't add protocol\n");
1114 return -EAGAIN;
1115 }
1116
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001117 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ipip6_tunnel_setup);
1119 if (!ipip6_fb_tunnel_dev) {
1120 err = -ENOMEM;
1121 goto err1;
1122 }
1123
1124 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
1125
1126 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
1127 goto err2;
1128
1129 out:
1130 return err;
1131 err2:
1132 free_netdev(ipip6_fb_tunnel_dev);
1133 err1:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001134 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto out;
1136}
Joerg Roedel989e5b92006-10-10 14:47:44 -07001137
1138module_init(sit_init);
1139module_exit(sit_cleanup);
Jan Dittmer39c85082006-10-13 15:05:53 -07001140MODULE_LICENSE("GPL");
Patrick McHardydaccff02006-11-05 15:47:04 -08001141MODULE_ALIAS("sit0");