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