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