blob: 84c1ed246afbe1d8eff4393092e3433289b2423b [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)
206 if (p->entry.addr == addr)
207 break;
208 return p;
209
210}
211
212static int
213ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
214{
215 struct ip_tunnel_prl_entry *p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900216 int err = 0;
217
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900218 if (a->addr == htonl(INADDR_ANY))
219 return -EINVAL;
220
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900221 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400222
223 for (p = t->prl; p; p = p->next) {
224 if (p->entry.addr == a->addr) {
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900225 if (chg)
226 goto update;
227 err = -EEXIST;
228 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400229 }
230 }
231
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900232 if (chg) {
233 err = -ENXIO;
234 goto out;
235 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400236
237 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900238 if (!p) {
239 err = -ENOBUFS;
240 goto out;
241 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400242
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400243 p->next = t->prl;
244 t->prl = p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900245update:
246 p->entry = *a;
247out:
248 write_unlock(&ipip6_lock);
249 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400250}
251
252static int
253ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
254{
255 struct ip_tunnel_prl_entry *x, **p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900256 int err = 0;
257
258 write_lock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400259
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900260 if (a && a->addr != htonl(INADDR_ANY)) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400261 for (p = &t->prl; *p; p = &(*p)->next) {
262 if ((*p)->entry.addr == a->addr) {
263 x = *p;
264 *p = x->next;
265 kfree(x);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900266 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400267 }
268 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900269 err = -ENXIO;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400270 } else {
271 while (t->prl) {
272 x = t->prl;
273 t->prl = t->prl->next;
274 kfree(x);
275 }
276 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900277out:
278 write_unlock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400279 return 0;
280}
281
282/* copied directly from anycast.c */
283static int
284ipip6_onlink(struct in6_addr *addr, struct net_device *dev)
285{
286 struct inet6_dev *idev;
287 struct inet6_ifaddr *ifa;
288 int onlink;
289
290 onlink = 0;
291 rcu_read_lock();
292 idev = __in6_dev_get(dev);
293 if (idev) {
294 read_lock_bh(&idev->lock);
295 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
296 onlink = ipv6_prefix_equal(addr, &ifa->addr,
297 ifa->prefix_len);
298 if (onlink)
299 break;
300 }
301 read_unlock_bh(&idev->lock);
302 }
303 rcu_read_unlock();
304 return onlink;
305}
306
307static int
308isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
309{
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900310 struct ip_tunnel_prl_entry *p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400311 int ok = 1;
312
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900313 read_lock(&ipip6_lock);
314 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400315 if (p) {
316 if (p->entry.flags & PRL_DEFAULT)
317 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
318 else
319 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
320 } else {
321 struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
322 if (ipv6_addr_is_isatap(addr6) &&
323 (addr6->s6_addr32[3] == iph->saddr) &&
324 ipip6_onlink(addr6, t->dev))
325 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
326 else
327 ok = 0;
328 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900329 read_unlock(&ipip6_lock);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400330 return ok;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void ipip6_tunnel_uninit(struct net_device *dev)
334{
335 if (dev == ipip6_fb_tunnel_dev) {
336 write_lock_bh(&ipip6_lock);
337 tunnels_wc[0] = NULL;
338 write_unlock_bh(&ipip6_lock);
339 dev_put(dev);
340 } else {
Patrick McHardy2941a482006-01-08 22:05:26 -0800341 ipip6_tunnel_unlink(netdev_priv(dev));
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400342 ipip6_tunnel_del_prl(netdev_priv(dev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 dev_put(dev);
344 }
345}
346
347
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800348static int ipip6_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350#ifndef I_WISH_WORLD_WERE_PERFECT
351
352/* It is not :-( All the routers (except for Linux) return only
353 8 bytes of packet payload. It means, that precise relaying of
354 ICMP in the real Internet is absolutely infeasible.
355 */
356 struct iphdr *iph = (struct iphdr*)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300357 const int type = icmp_hdr(skb)->type;
358 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct ip_tunnel *t;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 switch (type) {
363 default:
364 case ICMP_PARAMETERPROB:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 case ICMP_DEST_UNREACH:
368 switch (code) {
369 case ICMP_SR_FAILED:
370 case ICMP_PORT_UNREACH:
371 /* Impossible event. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 case ICMP_FRAG_NEEDED:
374 /* Soft state for pmtu is maintained by IP core. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 default:
377 /* All others are translated to HOST_UNREACH.
378 rfc2003 contains "deep thoughts" about NET_UNREACH,
379 I believe they are just ether pollution. --ANK
380 */
381 break;
382 }
383 break;
384 case ICMP_TIME_EXCEEDED:
385 if (code != ICMP_EXC_TTL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 }
389
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800390 err = -ENOENT;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 read_lock(&ipip6_lock);
393 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
394 if (t == NULL || t->parms.iph.daddr == 0)
395 goto out;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800396
397 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
399 goto out;
400
401 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
402 t->err_count++;
403 else
404 t->err_count = 1;
405 t->err_time = jiffies;
406out:
407 read_unlock(&ipip6_lock);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#else
410 struct iphdr *iph = (struct iphdr*)dp;
411 int hlen = iph->ihl<<2;
412 struct ipv6hdr *iph6;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300413 const int type = icmp_hdr(skb)->type;
414 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int rel_type = 0;
416 int rel_code = 0;
417 int rel_info = 0;
418 struct sk_buff *skb2;
419 struct rt6_info *rt6i;
420
421 if (len < hlen + sizeof(struct ipv6hdr))
422 return;
423 iph6 = (struct ipv6hdr*)(dp + hlen);
424
425 switch (type) {
426 default:
427 return;
428 case ICMP_PARAMETERPROB:
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300429 if (icmp_hdr(skb)->un.gateway < hlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return;
431
432 /* So... This guy found something strange INSIDE encapsulated
433 packet. Well, he is fool, but what can we do ?
434 */
435 rel_type = ICMPV6_PARAMPROB;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300436 rel_info = icmp_hdr(skb)->un.gateway - hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438
439 case ICMP_DEST_UNREACH:
440 switch (code) {
441 case ICMP_SR_FAILED:
442 case ICMP_PORT_UNREACH:
443 /* Impossible event. */
444 return;
445 case ICMP_FRAG_NEEDED:
446 /* Too complicated case ... */
447 return;
448 default:
449 /* All others are translated to HOST_UNREACH.
450 rfc2003 contains "deep thoughts" about NET_UNREACH,
451 I believe, it is just ether pollution. --ANK
452 */
453 rel_type = ICMPV6_DEST_UNREACH;
454 rel_code = ICMPV6_ADDR_UNREACH;
455 break;
456 }
457 break;
458 case ICMP_TIME_EXCEEDED:
459 if (code != ICMP_EXC_TTL)
460 return;
461 rel_type = ICMPV6_TIME_EXCEED;
462 rel_code = ICMPV6_EXC_HOPLIMIT;
463 break;
464 }
465
466 /* Prepare fake skb to feed it to icmpv6_send */
467 skb2 = skb_clone(skb, GFP_ATOMIC);
468 if (skb2 == NULL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dst_release(skb2->dst);
471 skb2->dst = NULL;
472 skb_pull(skb2, skb->data - (u8*)iph6);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700473 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Try to guess incoming interface */
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800476 rt6i = rt6_lookup(&init_net, &iph6->saddr, NULL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (rt6i && rt6i->rt6i_dev) {
478 skb2->dev = rt6i->rt6i_dev;
479
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800480 rt6i = rt6_lookup(&init_net, &iph6->daddr, &iph6->saddr, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
Patrick McHardy2941a482006-01-08 22:05:26 -0800483 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
485 rel_type = ICMPV6_DEST_UNREACH;
486 rel_code = ICMPV6_ADDR_UNREACH;
487 }
488 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
489 }
490 }
491 kfree_skb(skb2);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif
494}
495
496static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
497{
498 if (INET_ECN_is_ce(iph->tos))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700499 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static int ipip6_rcv(struct sk_buff *skb)
503{
504 struct iphdr *iph;
505 struct ip_tunnel *tunnel;
506
507 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
508 goto out;
509
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700510 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 read_lock(&ipip6_lock);
513 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
514 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700515 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700516 skb_reset_network_header(skb);
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800517 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 skb->protocol = htons(ETH_P_IPV6);
519 skb->pkt_type = PACKET_HOST;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100520
521 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400522 !isatap_chksrc(skb, iph, tunnel)) {
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100523 tunnel->stat.rx_errors++;
524 read_unlock(&ipip6_lock);
525 kfree_skb(skb);
526 return 0;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 tunnel->stat.rx_packets++;
529 tunnel->stat.rx_bytes += skb->len;
530 skb->dev = tunnel->dev;
531 dst_release(skb->dst);
532 skb->dst = NULL;
533 nf_reset(skb);
534 ipip6_ecn_decapsulate(iph, skb);
535 netif_rx(skb);
536 read_unlock(&ipip6_lock);
537 return 0;
538 }
539
Herbert Xu45af08b2006-04-05 22:31:19 -0700540 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree_skb(skb);
542 read_unlock(&ipip6_lock);
543out:
544 return 0;
545}
546
547/* Returns the embedded IPv4 address if the IPv6 address
548 comes from 6to4 (RFC 3056) addr space */
549
Al Viroe69a4ad2006-11-14 20:56:00 -0800550static inline __be32 try_6to4(struct in6_addr *v6dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Al Viroe69a4ad2006-11-14 20:56:00 -0800552 __be32 dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (v6dst->s6_addr16[0] == htons(0x2002)) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900555 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 memcpy(&dst, &v6dst->s6_addr16[1], 4);
557 }
558 return dst;
559}
560
561/*
562 * This function assumes it is being called from dev_queue_xmit()
563 * and that skb is filled properly by that function.
564 */
565
566static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
567{
Patrick McHardy2941a482006-01-08 22:05:26 -0800568 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct net_device_stats *stats = &tunnel->stat;
570 struct iphdr *tiph = &tunnel->parms.iph;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700571 struct ipv6hdr *iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 u8 tos = tunnel->parms.iph.tos;
573 struct rtable *rt; /* Route to the other host */
574 struct net_device *tdev; /* Device to other host */
575 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700576 unsigned int max_headroom; /* The extra header space needed */
Al Viroe69a4ad2006-11-14 20:56:00 -0800577 __be32 dst = tiph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900579 struct in6_addr *addr6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int addr_type;
581
582 if (tunnel->recursion++) {
583 tunnel->stat.collisions++;
584 goto tx_error;
585 }
586
587 if (skb->protocol != htons(ETH_P_IPV6))
588 goto tx_error;
589
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100590 /* ISATAP (RFC4214) - must come before 6to4 */
591 if (dev->priv_flags & IFF_ISATAP) {
592 struct neighbour *neigh = NULL;
593
594 if (skb->dst)
595 neigh = skb->dst->neighbour;
596
597 if (neigh == NULL) {
598 if (net_ratelimit())
599 printk(KERN_DEBUG "sit: nexthop == NULL\n");
600 goto tx_error;
601 }
602
603 addr6 = (struct in6_addr*)&neigh->primary_key;
604 addr_type = ipv6_addr_type(addr6);
605
606 if ((addr_type & IPV6_ADDR_UNICAST) &&
607 ipv6_addr_is_isatap(addr6))
608 dst = addr6->s6_addr32[3];
609 else
610 goto tx_error;
611 }
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!dst)
614 dst = try_6to4(&iph6->daddr);
615
616 if (!dst) {
617 struct neighbour *neigh = NULL;
618
619 if (skb->dst)
620 neigh = skb->dst->neighbour;
621
622 if (neigh == NULL) {
623 if (net_ratelimit())
624 printk(KERN_DEBUG "sit: nexthop == NULL\n");
625 goto tx_error;
626 }
627
628 addr6 = (struct in6_addr*)&neigh->primary_key;
629 addr_type = ipv6_addr_type(addr6);
630
631 if (addr_type == IPV6_ADDR_ANY) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700632 addr6 = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 addr_type = ipv6_addr_type(addr6);
634 }
635
636 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
637 goto tx_error_icmp;
638
639 dst = addr6->s6_addr32[3];
640 }
641
642 {
643 struct flowi fl = { .nl_u = { .ip4_u =
644 { .daddr = dst,
645 .saddr = tiph->saddr,
646 .tos = RT_TOS(tos) } },
647 .oif = tunnel->parms.link,
648 .proto = IPPROTO_IPV6 };
Denis V. Lunevf2063512008-01-22 22:07:34 -0800649 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 tunnel->stat.tx_carrier_errors++;
651 goto tx_error_icmp;
652 }
653 }
654 if (rt->rt_type != RTN_UNICAST) {
655 ip_rt_put(rt);
656 tunnel->stat.tx_carrier_errors++;
657 goto tx_error_icmp;
658 }
659 tdev = rt->u.dst.dev;
660
661 if (tdev == dev) {
662 ip_rt_put(rt);
663 tunnel->stat.collisions++;
664 goto tx_error;
665 }
666
667 if (tiph->frag_off)
668 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
669 else
670 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
671
672 if (mtu < 68) {
673 tunnel->stat.collisions++;
674 ip_rt_put(rt);
675 goto tx_error;
676 }
677 if (mtu < IPV6_MIN_MTU)
678 mtu = IPV6_MIN_MTU;
679 if (tunnel->parms.iph.daddr && skb->dst)
680 skb->dst->ops->update_pmtu(skb->dst, mtu);
681
682 if (skb->len > mtu) {
683 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
684 ip_rt_put(rt);
685 goto tx_error;
686 }
687
688 if (tunnel->err_count > 0) {
689 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
690 tunnel->err_count--;
691 dst_link_failure(skb);
692 } else
693 tunnel->err_count = 0;
694 }
695
696 /*
697 * Okay, now see if we can stuff it in the buffer as-is.
698 */
699 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
700
Patrick McHardycfbba492007-07-09 15:33:40 -0700701 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
702 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
704 if (!new_skb) {
705 ip_rt_put(rt);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900706 stats->tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dev_kfree_skb(skb);
708 tunnel->recursion--;
709 return 0;
710 }
711 if (skb->sk)
712 skb_set_owner_w(new_skb, skb->sk);
713 dev_kfree_skb(skb);
714 skb = new_skb;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700715 iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700718 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700719 skb_push(skb, sizeof(struct iphdr));
720 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800722 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dst_release(skb->dst);
724 skb->dst = &rt->u.dst;
725
726 /*
727 * Push down and install the IPIP header.
728 */
729
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700730 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 iph->version = 4;
732 iph->ihl = sizeof(struct iphdr)>>2;
733 if (mtu > IPV6_MIN_MTU)
734 iph->frag_off = htons(IP_DF);
735 else
736 iph->frag_off = 0;
737
738 iph->protocol = IPPROTO_IPV6;
739 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
740 iph->daddr = rt->rt_dst;
741 iph->saddr = rt->rt_src;
742
743 if ((iph->ttl = tiph->ttl) == 0)
744 iph->ttl = iph6->hop_limit;
745
746 nf_reset(skb);
747
748 IPTUNNEL_XMIT();
749 tunnel->recursion--;
750 return 0;
751
752tx_error_icmp:
753 dst_link_failure(skb);
754tx_error:
755 stats->tx_errors++;
756 dev_kfree_skb(skb);
757 tunnel->recursion--;
758 return 0;
759}
760
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800761static void ipip6_tunnel_bind_dev(struct net_device *dev)
762{
763 struct net_device *tdev = NULL;
764 struct ip_tunnel *tunnel;
765 struct iphdr *iph;
766
767 tunnel = netdev_priv(dev);
768 iph = &tunnel->parms.iph;
769
770 if (iph->daddr) {
771 struct flowi fl = { .nl_u = { .ip4_u =
772 { .daddr = iph->daddr,
773 .saddr = iph->saddr,
774 .tos = RT_TOS(iph->tos) } },
775 .oif = tunnel->parms.link,
776 .proto = IPPROTO_IPV6 };
777 struct rtable *rt;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800778 if (!ip_route_output_key(&init_net, &rt, &fl)) {
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800779 tdev = rt->u.dst.dev;
780 ip_rt_put(rt);
781 }
782 dev->flags |= IFF_POINTOPOINT;
783 }
784
785 if (!tdev && tunnel->parms.link)
786 tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
787
788 if (tdev) {
789 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
790 dev->mtu = tdev->mtu - sizeof(struct iphdr);
791 if (dev->mtu < IPV6_MIN_MTU)
792 dev->mtu = IPV6_MIN_MTU;
793 }
794 dev->iflink = tunnel->parms.link;
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797static int
798ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
799{
800 int err = 0;
801 struct ip_tunnel_parm p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400802 struct ip_tunnel_prl prl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct ip_tunnel *t;
804
805 switch (cmd) {
806 case SIOCGETTUNNEL:
807 t = NULL;
808 if (dev == ipip6_fb_tunnel_dev) {
809 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
810 err = -EFAULT;
811 break;
812 }
813 t = ipip6_tunnel_locate(&p, 0);
814 }
815 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800816 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 memcpy(&p, &t->parms, sizeof(p));
818 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
819 err = -EFAULT;
820 break;
821
822 case SIOCADDTUNNEL:
823 case SIOCCHGTUNNEL:
824 err = -EPERM;
825 if (!capable(CAP_NET_ADMIN))
826 goto done;
827
828 err = -EFAULT;
829 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
830 goto done;
831
832 err = -EINVAL;
833 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
834 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
835 goto done;
836 if (p.iph.ttl)
837 p.iph.frag_off |= htons(IP_DF);
838
839 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
840
841 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
842 if (t != NULL) {
843 if (t->dev != dev) {
844 err = -EEXIST;
845 break;
846 }
847 } else {
848 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
849 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
850 err = -EINVAL;
851 break;
852 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800853 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ipip6_tunnel_unlink(t);
855 t->parms.iph.saddr = p.iph.saddr;
856 t->parms.iph.daddr = p.iph.daddr;
857 memcpy(dev->dev_addr, &p.iph.saddr, 4);
858 memcpy(dev->broadcast, &p.iph.daddr, 4);
859 ipip6_tunnel_link(t);
860 netdev_state_change(dev);
861 }
862 }
863
864 if (t) {
865 err = 0;
866 if (cmd == SIOCCHGTUNNEL) {
867 t->parms.iph.ttl = p.iph.ttl;
868 t->parms.iph.tos = p.iph.tos;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800869 if (t->parms.link != p.link) {
870 t->parms.link = p.link;
871 ipip6_tunnel_bind_dev(dev);
872 netdev_state_change(dev);
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
876 err = -EFAULT;
877 } else
878 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
879 break;
880
881 case SIOCDELTUNNEL:
882 err = -EPERM;
883 if (!capable(CAP_NET_ADMIN))
884 goto done;
885
886 if (dev == ipip6_fb_tunnel_dev) {
887 err = -EFAULT;
888 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
889 goto done;
890 err = -ENOENT;
891 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
892 goto done;
893 err = -EPERM;
Patrick McHardy2941a482006-01-08 22:05:26 -0800894 if (t == netdev_priv(ipip6_fb_tunnel_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto done;
896 dev = t->dev;
897 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -0800898 unregister_netdevice(dev);
899 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400902 case SIOCADDPRL:
903 case SIOCDELPRL:
904 case SIOCCHGPRL:
905 err = -EPERM;
906 if (!capable(CAP_NET_ADMIN))
907 goto done;
908 err = -EINVAL;
909 if (dev == ipip6_fb_tunnel_dev)
910 goto done;
911 err = -EFAULT;
912 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
913 goto done;
914 err = -ENOENT;
915 if (!(t = netdev_priv(dev)))
916 goto done;
917
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400918 if (cmd == SIOCDELPRL)
919 err = ipip6_tunnel_del_prl(t, &prl);
920 else
921 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400922 netdev_state_change(dev);
923 break;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 default:
926 err = -EINVAL;
927 }
928
929done:
930 return err;
931}
932
933static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
934{
Patrick McHardy2941a482006-01-08 22:05:26 -0800935 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
939{
940 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
941 return -EINVAL;
942 dev->mtu = new_mtu;
943 return 0;
944}
945
946static void ipip6_tunnel_setup(struct net_device *dev)
947{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dev->uninit = ipip6_tunnel_uninit;
949 dev->destructor = free_netdev;
950 dev->hard_start_xmit = ipip6_tunnel_xmit;
951 dev->get_stats = ipip6_tunnel_get_stats;
952 dev->do_ioctl = ipip6_tunnel_ioctl;
953 dev->change_mtu = ipip6_tunnel_change_mtu;
954
955 dev->type = ARPHRD_SIT;
956 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800957 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 dev->flags = IFF_NOARP;
959 dev->iflink = 0;
960 dev->addr_len = 4;
961}
962
963static int ipip6_tunnel_init(struct net_device *dev)
964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Patrick McHardy2941a482006-01-08 22:05:26 -0800967 tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 tunnel->dev = dev;
970 strcpy(tunnel->parms.name, dev->name);
971
972 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
973 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
974
Michal Schmidt8a4a50f2007-12-13 09:47:00 -0800975 ipip6_tunnel_bind_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 return 0;
978}
979
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300980static int __init ipip6_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Patrick McHardy2941a482006-01-08 22:05:26 -0800982 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct iphdr *iph = &tunnel->parms.iph;
984
985 tunnel->dev = dev;
986 strcpy(tunnel->parms.name, dev->name);
987
988 iph->version = 4;
989 iph->protocol = IPPROTO_IPV6;
990 iph->ihl = 5;
991 iph->ttl = 64;
992
993 dev_hold(dev);
994 tunnels_wc[0] = tunnel;
995 return 0;
996}
997
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800998static struct xfrm_tunnel sit_handler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .handler = ipip6_rcv,
1000 .err_handler = ipip6_err,
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001001 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002};
1003
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001004static void __exit sit_destroy_tunnels(void)
1005{
1006 int prio;
1007
1008 for (prio = 1; prio < 4; prio++) {
1009 int h;
1010 for (h = 0; h < HASH_SIZE; h++) {
1011 struct ip_tunnel *t;
1012 while ((t = tunnels[prio][h]) != NULL)
1013 unregister_netdevice(t->dev);
1014 }
1015 }
1016}
1017
Adrian Bunk89c89452006-11-20 16:56:48 -08001018static void __exit sit_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001020 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001021
1022 rtnl_lock();
1023 sit_destroy_tunnels();
1024 unregister_netdevice(ipip6_fb_tunnel_dev);
1025 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Adrian Bunk89c89452006-11-20 16:56:48 -08001028static int __init sit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 int err;
1031
1032 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1033
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001034 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 printk(KERN_INFO "sit init: Can't add protocol\n");
1036 return -EAGAIN;
1037 }
1038
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001039 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ipip6_tunnel_setup);
1041 if (!ipip6_fb_tunnel_dev) {
1042 err = -ENOMEM;
1043 goto err1;
1044 }
1045
1046 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
1047
1048 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
1049 goto err2;
1050
1051 out:
1052 return err;
1053 err2:
1054 free_netdev(ipip6_fb_tunnel_dev);
1055 err1:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001056 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out;
1058}
Joerg Roedel989e5b92006-10-10 14:47:44 -07001059
1060module_init(sit_init);
1061module_exit(sit_cleanup);
Jan Dittmer39c85082006-10-13 15:05:53 -07001062MODULE_LICENSE("GPL");
Patrick McHardydaccff02006-11-05 15:47:04 -08001063MODULE_ALIAS("sit0");