Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[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  * Fred Templin <fred.l.templin@boeing.com>:    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 #include <net/net_namespace.h>
56 #include <net/netns/generic.h>
57
58 /*
59    This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
60
61    For comments look at net/ipv4/ip_gre.c --ANK
62  */
63
64 #define HASH_SIZE  16
65 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
66
67 static int ipip6_fb_tunnel_init(struct net_device *dev);
68 static int ipip6_tunnel_init(struct net_device *dev);
69 static void ipip6_tunnel_setup(struct net_device *dev);
70
71 static int sit_net_id;
72 struct sit_net {
73         struct ip_tunnel *tunnels_r_l[HASH_SIZE];
74         struct ip_tunnel *tunnels_r[HASH_SIZE];
75         struct ip_tunnel *tunnels_l[HASH_SIZE];
76         struct ip_tunnel *tunnels_wc[1];
77         struct ip_tunnel **tunnels[4];
78
79         struct net_device *fb_tunnel_dev;
80 };
81
82 static DEFINE_RWLOCK(ipip6_lock);
83
84 static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
85                 __be32 remote, __be32 local)
86 {
87         unsigned h0 = HASH(remote);
88         unsigned h1 = HASH(local);
89         struct ip_tunnel *t;
90         struct sit_net *sitn = net_generic(net, sit_net_id);
91
92         for (t = sitn->tunnels_r_l[h0^h1]; t; t = t->next) {
93                 if (local == t->parms.iph.saddr &&
94                     remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
95                         return t;
96         }
97         for (t = sitn->tunnels_r[h0]; t; t = t->next) {
98                 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
99                         return t;
100         }
101         for (t = sitn->tunnels_l[h1]; t; t = t->next) {
102                 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
103                         return t;
104         }
105         if ((t = sitn->tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
106                 return t;
107         return NULL;
108 }
109
110 static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
111                 struct ip_tunnel_parm *parms)
112 {
113         __be32 remote = parms->iph.daddr;
114         __be32 local = parms->iph.saddr;
115         unsigned h = 0;
116         int prio = 0;
117
118         if (remote) {
119                 prio |= 2;
120                 h ^= HASH(remote);
121         }
122         if (local) {
123                 prio |= 1;
124                 h ^= HASH(local);
125         }
126         return &sitn->tunnels[prio][h];
127 }
128
129 static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
130                 struct ip_tunnel *t)
131 {
132         return __ipip6_bucket(sitn, &t->parms);
133 }
134
135 static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
136 {
137         struct ip_tunnel **tp;
138
139         for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
140                 if (t == *tp) {
141                         write_lock_bh(&ipip6_lock);
142                         *tp = t->next;
143                         write_unlock_bh(&ipip6_lock);
144                         break;
145                 }
146         }
147 }
148
149 static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
150 {
151         struct ip_tunnel **tp = ipip6_bucket(sitn, t);
152
153         t->next = *tp;
154         write_lock_bh(&ipip6_lock);
155         *tp = t;
156         write_unlock_bh(&ipip6_lock);
157 }
158
159 static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
160                 struct ip_tunnel_parm *parms, int create)
161 {
162         __be32 remote = parms->iph.daddr;
163         __be32 local = parms->iph.saddr;
164         struct ip_tunnel *t, **tp, *nt;
165         struct net_device *dev;
166         char name[IFNAMSIZ];
167         struct sit_net *sitn = net_generic(net, sit_net_id);
168
169         for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
170                 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
171                         return t;
172         }
173         if (!create)
174                 goto failed;
175
176         if (parms->name[0])
177                 strlcpy(name, parms->name, IFNAMSIZ);
178         else
179                 sprintf(name, "sit%%d");
180
181         dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
182         if (dev == NULL)
183                 return NULL;
184
185         dev_net_set(dev, net);
186
187         if (strchr(name, '%')) {
188                 if (dev_alloc_name(dev, name) < 0)
189                         goto failed_free;
190         }
191
192         nt = netdev_priv(dev);
193         dev->init = ipip6_tunnel_init;
194         nt->parms = *parms;
195
196         if (parms->i_flags & SIT_ISATAP)
197                 dev->priv_flags |= IFF_ISATAP;
198
199         if (register_netdevice(dev) < 0)
200                 goto failed_free;
201
202         dev_hold(dev);
203
204         ipip6_tunnel_link(sitn, nt);
205         return nt;
206
207 failed_free:
208         free_netdev(dev);
209 failed:
210         return NULL;
211 }
212
213 static struct ip_tunnel_prl_entry *
214 __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
215 {
216         struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
217
218         for (p = t->prl; p; p = p->next)
219                 if (p->addr == addr)
220                         break;
221         return p;
222
223 }
224
225 static int ipip6_tunnel_get_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
226 {
227         struct ip_tunnel_prl *kp;
228         struct ip_tunnel_prl_entry *prl;
229         unsigned int cmax, c = 0, ca, len;
230         int ret = 0;
231
232         cmax = a->datalen / sizeof(*a);
233         if (cmax > 1 && a->addr != htonl(INADDR_ANY))
234                 cmax = 1;
235
236         /* For simple GET or for root users,
237          * we try harder to allocate.
238          */
239         kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
240                 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
241                 NULL;
242
243         read_lock(&ipip6_lock);
244
245         ca = t->prl_count < cmax ? t->prl_count : cmax;
246
247         if (!kp) {
248                 /* We don't try hard to allocate much memory for
249                  * non-root users.
250                  * For root users, retry allocating enough memory for
251                  * the answer.
252                  */
253                 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
254                 if (!kp) {
255                         ret = -ENOMEM;
256                         goto out;
257                 }
258         }
259
260         c = 0;
261         for (prl = t->prl; prl; prl = prl->next) {
262                 if (c > cmax)
263                         break;
264                 if (a->addr != htonl(INADDR_ANY) && prl->addr != a->addr)
265                         continue;
266                 kp[c].addr = prl->addr;
267                 kp[c].flags = prl->flags;
268                 c++;
269                 if (a->addr != htonl(INADDR_ANY))
270                         break;
271         }
272 out:
273         read_unlock(&ipip6_lock);
274
275         len = sizeof(*kp) * c;
276         ret = len ? copy_to_user(a->data, kp, len) : 0;
277
278         kfree(kp);
279         if (ret)
280                 return -EFAULT;
281
282         a->datalen = len;
283         return 0;
284 }
285
286 static int
287 ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
288 {
289         struct ip_tunnel_prl_entry *p;
290         int err = 0;
291
292         if (a->addr == htonl(INADDR_ANY))
293                 return -EINVAL;
294
295         write_lock(&ipip6_lock);
296
297         for (p = t->prl; p; p = p->next) {
298                 if (p->addr == a->addr) {
299                         if (chg)
300                                 goto update;
301                         err = -EEXIST;
302                         goto out;
303                 }
304         }
305
306         if (chg) {
307                 err = -ENXIO;
308                 goto out;
309         }
310
311         p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
312         if (!p) {
313                 err = -ENOBUFS;
314                 goto out;
315         }
316
317         p->next = t->prl;
318         t->prl = p;
319         t->prl_count++;
320 update:
321         p->addr = a->addr;
322         p->flags = a->flags;
323 out:
324         write_unlock(&ipip6_lock);
325         return err;
326 }
327
328 static int
329 ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
330 {
331         struct ip_tunnel_prl_entry *x, **p;
332         int err = 0;
333
334         write_lock(&ipip6_lock);
335
336         if (a && a->addr != htonl(INADDR_ANY)) {
337                 for (p = &t->prl; *p; p = &(*p)->next) {
338                         if ((*p)->addr == a->addr) {
339                                 x = *p;
340                                 *p = x->next;
341                                 kfree(x);
342                                 t->prl_count--;
343                                 goto out;
344                         }
345                 }
346                 err = -ENXIO;
347         } else {
348                 while (t->prl) {
349                         x = t->prl;
350                         t->prl = t->prl->next;
351                         kfree(x);
352                         t->prl_count--;
353                 }
354         }
355 out:
356         write_unlock(&ipip6_lock);
357         return 0;
358 }
359
360 static int
361 isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
362 {
363         struct ip_tunnel_prl_entry *p;
364         int ok = 1;
365
366         read_lock(&ipip6_lock);
367         p = __ipip6_tunnel_locate_prl(t, iph->saddr);
368         if (p) {
369                 if (p->flags & PRL_DEFAULT)
370                         skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
371                 else
372                         skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
373         } else {
374                 struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
375                 if (ipv6_addr_is_isatap(addr6) &&
376                     (addr6->s6_addr32[3] == iph->saddr) &&
377                     ipv6_chk_prefix(addr6, t->dev))
378                         skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
379                 else
380                         ok = 0;
381         }
382         read_unlock(&ipip6_lock);
383         return ok;
384 }
385
386 static void ipip6_tunnel_uninit(struct net_device *dev)
387 {
388         struct net *net = dev_net(dev);
389         struct sit_net *sitn = net_generic(net, sit_net_id);
390
391         if (dev == sitn->fb_tunnel_dev) {
392                 write_lock_bh(&ipip6_lock);
393                 sitn->tunnels_wc[0] = NULL;
394                 write_unlock_bh(&ipip6_lock);
395                 dev_put(dev);
396         } else {
397                 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
398                 ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
399                 dev_put(dev);
400         }
401 }
402
403
404 static int ipip6_err(struct sk_buff *skb, u32 info)
405 {
406 #ifndef I_WISH_WORLD_WERE_PERFECT
407
408 /* It is not :-( All the routers (except for Linux) return only
409    8 bytes of packet payload. It means, that precise relaying of
410    ICMP in the real Internet is absolutely infeasible.
411  */
412         struct iphdr *iph = (struct iphdr*)skb->data;
413         const int type = icmp_hdr(skb)->type;
414         const int code = icmp_hdr(skb)->code;
415         struct ip_tunnel *t;
416         int err;
417
418         switch (type) {
419         default:
420         case ICMP_PARAMETERPROB:
421                 return 0;
422
423         case ICMP_DEST_UNREACH:
424                 switch (code) {
425                 case ICMP_SR_FAILED:
426                 case ICMP_PORT_UNREACH:
427                         /* Impossible event. */
428                         return 0;
429                 case ICMP_FRAG_NEEDED:
430                         /* Soft state for pmtu is maintained by IP core. */
431                         return 0;
432                 default:
433                         /* All others are translated to HOST_UNREACH.
434                            rfc2003 contains "deep thoughts" about NET_UNREACH,
435                            I believe they are just ether pollution. --ANK
436                          */
437                         break;
438                 }
439                 break;
440         case ICMP_TIME_EXCEEDED:
441                 if (code != ICMP_EXC_TTL)
442                         return 0;
443                 break;
444         }
445
446         err = -ENOENT;
447
448         read_lock(&ipip6_lock);
449         t = ipip6_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
450         if (t == NULL || t->parms.iph.daddr == 0)
451                 goto out;
452
453         err = 0;
454         if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
455                 goto out;
456
457         if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
458                 t->err_count++;
459         else
460                 t->err_count = 1;
461         t->err_time = jiffies;
462 out:
463         read_unlock(&ipip6_lock);
464         return err;
465 #else
466         struct iphdr *iph = (struct iphdr*)dp;
467         int hlen = iph->ihl<<2;
468         struct ipv6hdr *iph6;
469         const int type = icmp_hdr(skb)->type;
470         const int code = icmp_hdr(skb)->code;
471         int rel_type = 0;
472         int rel_code = 0;
473         int rel_info = 0;
474         struct sk_buff *skb2;
475         struct rt6_info *rt6i;
476
477         if (len < hlen + sizeof(struct ipv6hdr))
478                 return;
479         iph6 = (struct ipv6hdr*)(dp + hlen);
480
481         switch (type) {
482         default:
483                 return;
484         case ICMP_PARAMETERPROB:
485                 if (icmp_hdr(skb)->un.gateway < hlen)
486                         return;
487
488                 /* So... This guy found something strange INSIDE encapsulated
489                    packet. Well, he is fool, but what can we do ?
490                  */
491                 rel_type = ICMPV6_PARAMPROB;
492                 rel_info = icmp_hdr(skb)->un.gateway - hlen;
493                 break;
494
495         case ICMP_DEST_UNREACH:
496                 switch (code) {
497                 case ICMP_SR_FAILED:
498                 case ICMP_PORT_UNREACH:
499                         /* Impossible event. */
500                         return;
501                 case ICMP_FRAG_NEEDED:
502                         /* Too complicated case ... */
503                         return;
504                 default:
505                         /* All others are translated to HOST_UNREACH.
506                            rfc2003 contains "deep thoughts" about NET_UNREACH,
507                            I believe, it is just ether pollution. --ANK
508                          */
509                         rel_type = ICMPV6_DEST_UNREACH;
510                         rel_code = ICMPV6_ADDR_UNREACH;
511                         break;
512                 }
513                 break;
514         case ICMP_TIME_EXCEEDED:
515                 if (code != ICMP_EXC_TTL)
516                         return;
517                 rel_type = ICMPV6_TIME_EXCEED;
518                 rel_code = ICMPV6_EXC_HOPLIMIT;
519                 break;
520         }
521
522         /* Prepare fake skb to feed it to icmpv6_send */
523         skb2 = skb_clone(skb, GFP_ATOMIC);
524         if (skb2 == NULL)
525                 return 0;
526         dst_release(skb2->dst);
527         skb2->dst = NULL;
528         skb_pull(skb2, skb->data - (u8*)iph6);
529         skb_reset_network_header(skb2);
530
531         /* Try to guess incoming interface */
532         rt6i = rt6_lookup(dev_net(skb->dev), &iph6->saddr, NULL, NULL, 0);
533         if (rt6i && rt6i->rt6i_dev) {
534                 skb2->dev = rt6i->rt6i_dev;
535
536                 rt6i = rt6_lookup(dev_net(skb->dev),
537                                 &iph6->daddr, &iph6->saddr, NULL, 0);
538
539                 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
540                         struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
541                         if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
542                                 rel_type = ICMPV6_DEST_UNREACH;
543                                 rel_code = ICMPV6_ADDR_UNREACH;
544                         }
545                         icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
546                 }
547         }
548         kfree_skb(skb2);
549         return 0;
550 #endif
551 }
552
553 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
554 {
555         if (INET_ECN_is_ce(iph->tos))
556                 IP6_ECN_set_ce(ipv6_hdr(skb));
557 }
558
559 static int ipip6_rcv(struct sk_buff *skb)
560 {
561         struct iphdr *iph;
562         struct ip_tunnel *tunnel;
563
564         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
565                 goto out;
566
567         iph = ip_hdr(skb);
568
569         read_lock(&ipip6_lock);
570         if ((tunnel = ipip6_tunnel_lookup(dev_net(skb->dev),
571                                         iph->saddr, iph->daddr)) != NULL) {
572                 secpath_reset(skb);
573                 skb->mac_header = skb->network_header;
574                 skb_reset_network_header(skb);
575                 IPCB(skb)->flags = 0;
576                 skb->protocol = htons(ETH_P_IPV6);
577                 skb->pkt_type = PACKET_HOST;
578
579                 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
580                     !isatap_chksrc(skb, iph, tunnel)) {
581                         tunnel->stat.rx_errors++;
582                         read_unlock(&ipip6_lock);
583                         kfree_skb(skb);
584                         return 0;
585                 }
586                 tunnel->stat.rx_packets++;
587                 tunnel->stat.rx_bytes += skb->len;
588                 skb->dev = tunnel->dev;
589                 dst_release(skb->dst);
590                 skb->dst = NULL;
591                 nf_reset(skb);
592                 ipip6_ecn_decapsulate(iph, skb);
593                 netif_rx(skb);
594                 read_unlock(&ipip6_lock);
595                 return 0;
596         }
597
598         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
599         read_unlock(&ipip6_lock);
600 out:
601         kfree_skb(skb);
602         return 0;
603 }
604
605 /* Returns the embedded IPv4 address if the IPv6 address
606    comes from 6to4 (RFC 3056) addr space */
607
608 static inline __be32 try_6to4(struct in6_addr *v6dst)
609 {
610         __be32 dst = 0;
611
612         if (v6dst->s6_addr16[0] == htons(0x2002)) {
613                 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
614                 memcpy(&dst, &v6dst->s6_addr16[1], 4);
615         }
616         return dst;
617 }
618
619 /*
620  *      This function assumes it is being called from dev_queue_xmit()
621  *      and that skb is filled properly by that function.
622  */
623
624 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
625 {
626         struct ip_tunnel *tunnel = netdev_priv(dev);
627         struct net_device_stats *stats = &tunnel->stat;
628         struct iphdr  *tiph = &tunnel->parms.iph;
629         struct ipv6hdr *iph6 = ipv6_hdr(skb);
630         u8     tos = tunnel->parms.iph.tos;
631         struct rtable *rt;                      /* Route to the other host */
632         struct net_device *tdev;                        /* Device to other host */
633         struct iphdr  *iph;                     /* Our new IP header */
634         unsigned int max_headroom;              /* The extra header space needed */
635         __be32 dst = tiph->daddr;
636         int    mtu;
637         struct in6_addr *addr6;
638         int addr_type;
639
640         if (tunnel->recursion++) {
641                 tunnel->stat.collisions++;
642                 goto tx_error;
643         }
644
645         if (skb->protocol != htons(ETH_P_IPV6))
646                 goto tx_error;
647
648         /* ISATAP (RFC4214) - must come before 6to4 */
649         if (dev->priv_flags & IFF_ISATAP) {
650                 struct neighbour *neigh = NULL;
651
652                 if (skb->dst)
653                         neigh = skb->dst->neighbour;
654
655                 if (neigh == NULL) {
656                         if (net_ratelimit())
657                                 printk(KERN_DEBUG "sit: nexthop == NULL\n");
658                         goto tx_error;
659                 }
660
661                 addr6 = (struct in6_addr*)&neigh->primary_key;
662                 addr_type = ipv6_addr_type(addr6);
663
664                 if ((addr_type & IPV6_ADDR_UNICAST) &&
665                      ipv6_addr_is_isatap(addr6))
666                         dst = addr6->s6_addr32[3];
667                 else
668                         goto tx_error;
669         }
670
671         if (!dst)
672                 dst = try_6to4(&iph6->daddr);
673
674         if (!dst) {
675                 struct neighbour *neigh = NULL;
676
677                 if (skb->dst)
678                         neigh = skb->dst->neighbour;
679
680                 if (neigh == NULL) {
681                         if (net_ratelimit())
682                                 printk(KERN_DEBUG "sit: nexthop == NULL\n");
683                         goto tx_error;
684                 }
685
686                 addr6 = (struct in6_addr*)&neigh->primary_key;
687                 addr_type = ipv6_addr_type(addr6);
688
689                 if (addr_type == IPV6_ADDR_ANY) {
690                         addr6 = &ipv6_hdr(skb)->daddr;
691                         addr_type = ipv6_addr_type(addr6);
692                 }
693
694                 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
695                         goto tx_error_icmp;
696
697                 dst = addr6->s6_addr32[3];
698         }
699
700         {
701                 struct flowi fl = { .nl_u = { .ip4_u =
702                                               { .daddr = dst,
703                                                 .saddr = tiph->saddr,
704                                                 .tos = RT_TOS(tos) } },
705                                     .oif = tunnel->parms.link,
706                                     .proto = IPPROTO_IPV6 };
707                 if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
708                         tunnel->stat.tx_carrier_errors++;
709                         goto tx_error_icmp;
710                 }
711         }
712         if (rt->rt_type != RTN_UNICAST) {
713                 ip_rt_put(rt);
714                 tunnel->stat.tx_carrier_errors++;
715                 goto tx_error_icmp;
716         }
717         tdev = rt->u.dst.dev;
718
719         if (tdev == dev) {
720                 ip_rt_put(rt);
721                 tunnel->stat.collisions++;
722                 goto tx_error;
723         }
724
725         if (tiph->frag_off)
726                 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
727         else
728                 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
729
730         if (mtu < 68) {
731                 tunnel->stat.collisions++;
732                 ip_rt_put(rt);
733                 goto tx_error;
734         }
735         if (mtu < IPV6_MIN_MTU)
736                 mtu = IPV6_MIN_MTU;
737         if (tunnel->parms.iph.daddr && skb->dst)
738                 skb->dst->ops->update_pmtu(skb->dst, mtu);
739
740         if (skb->len > mtu) {
741                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
742                 ip_rt_put(rt);
743                 goto tx_error;
744         }
745
746         if (tunnel->err_count > 0) {
747                 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
748                         tunnel->err_count--;
749                         dst_link_failure(skb);
750                 } else
751                         tunnel->err_count = 0;
752         }
753
754         /*
755          * Okay, now see if we can stuff it in the buffer as-is.
756          */
757         max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
758
759         if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
760             (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
761                 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
762                 if (!new_skb) {
763                         ip_rt_put(rt);
764                         stats->tx_dropped++;
765                         dev_kfree_skb(skb);
766                         tunnel->recursion--;
767                         return 0;
768                 }
769                 if (skb->sk)
770                         skb_set_owner_w(new_skb, skb->sk);
771                 dev_kfree_skb(skb);
772                 skb = new_skb;
773                 iph6 = ipv6_hdr(skb);
774         }
775
776         skb->transport_header = skb->network_header;
777         skb_push(skb, sizeof(struct iphdr));
778         skb_reset_network_header(skb);
779         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
780         IPCB(skb)->flags = 0;
781         dst_release(skb->dst);
782         skb->dst = &rt->u.dst;
783
784         /*
785          *      Push down and install the IPIP header.
786          */
787
788         iph                     =       ip_hdr(skb);
789         iph->version            =       4;
790         iph->ihl                =       sizeof(struct iphdr)>>2;
791         if (mtu > IPV6_MIN_MTU)
792                 iph->frag_off   =       htons(IP_DF);
793         else
794                 iph->frag_off   =       0;
795
796         iph->protocol           =       IPPROTO_IPV6;
797         iph->tos                =       INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
798         iph->daddr              =       rt->rt_dst;
799         iph->saddr              =       rt->rt_src;
800
801         if ((iph->ttl = tiph->ttl) == 0)
802                 iph->ttl        =       iph6->hop_limit;
803
804         nf_reset(skb);
805
806         IPTUNNEL_XMIT();
807         tunnel->recursion--;
808         return 0;
809
810 tx_error_icmp:
811         dst_link_failure(skb);
812 tx_error:
813         stats->tx_errors++;
814         dev_kfree_skb(skb);
815         tunnel->recursion--;
816         return 0;
817 }
818
819 static void ipip6_tunnel_bind_dev(struct net_device *dev)
820 {
821         struct net_device *tdev = NULL;
822         struct ip_tunnel *tunnel;
823         struct iphdr *iph;
824
825         tunnel = netdev_priv(dev);
826         iph = &tunnel->parms.iph;
827
828         if (iph->daddr) {
829                 struct flowi fl = { .nl_u = { .ip4_u =
830                                               { .daddr = iph->daddr,
831                                                 .saddr = iph->saddr,
832                                                 .tos = RT_TOS(iph->tos) } },
833                                     .oif = tunnel->parms.link,
834                                     .proto = IPPROTO_IPV6 };
835                 struct rtable *rt;
836                 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
837                         tdev = rt->u.dst.dev;
838                         ip_rt_put(rt);
839                 }
840                 dev->flags |= IFF_POINTOPOINT;
841         }
842
843         if (!tdev && tunnel->parms.link)
844                 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
845
846         if (tdev) {
847                 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
848                 dev->mtu = tdev->mtu - sizeof(struct iphdr);
849                 if (dev->mtu < IPV6_MIN_MTU)
850                         dev->mtu = IPV6_MIN_MTU;
851         }
852         dev->iflink = tunnel->parms.link;
853 }
854
855 static int
856 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
857 {
858         int err = 0;
859         struct ip_tunnel_parm p;
860         struct ip_tunnel_prl prl;
861         struct ip_tunnel *t;
862         struct net *net = dev_net(dev);
863         struct sit_net *sitn = net_generic(net, sit_net_id);
864
865         switch (cmd) {
866         case SIOCGETTUNNEL:
867                 t = NULL;
868                 if (dev == sitn->fb_tunnel_dev) {
869                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
870                                 err = -EFAULT;
871                                 break;
872                         }
873                         t = ipip6_tunnel_locate(net, &p, 0);
874                 }
875                 if (t == NULL)
876                         t = netdev_priv(dev);
877                 memcpy(&p, &t->parms, sizeof(p));
878                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
879                         err = -EFAULT;
880                 break;
881
882         case SIOCADDTUNNEL:
883         case SIOCCHGTUNNEL:
884                 err = -EPERM;
885                 if (!capable(CAP_NET_ADMIN))
886                         goto done;
887
888                 err = -EFAULT;
889                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
890                         goto done;
891
892                 err = -EINVAL;
893                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
894                     p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
895                         goto done;
896                 if (p.iph.ttl)
897                         p.iph.frag_off |= htons(IP_DF);
898
899                 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
900
901                 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
902                         if (t != NULL) {
903                                 if (t->dev != dev) {
904                                         err = -EEXIST;
905                                         break;
906                                 }
907                         } else {
908                                 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
909                                     (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
910                                         err = -EINVAL;
911                                         break;
912                                 }
913                                 t = netdev_priv(dev);
914                                 ipip6_tunnel_unlink(sitn, t);
915                                 t->parms.iph.saddr = p.iph.saddr;
916                                 t->parms.iph.daddr = p.iph.daddr;
917                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
918                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
919                                 ipip6_tunnel_link(sitn, t);
920                                 netdev_state_change(dev);
921                         }
922                 }
923
924                 if (t) {
925                         err = 0;
926                         if (cmd == SIOCCHGTUNNEL) {
927                                 t->parms.iph.ttl = p.iph.ttl;
928                                 t->parms.iph.tos = p.iph.tos;
929                                 if (t->parms.link != p.link) {
930                                         t->parms.link = p.link;
931                                         ipip6_tunnel_bind_dev(dev);
932                                         netdev_state_change(dev);
933                                 }
934                         }
935                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
936                                 err = -EFAULT;
937                 } else
938                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
939                 break;
940
941         case SIOCDELTUNNEL:
942                 err = -EPERM;
943                 if (!capable(CAP_NET_ADMIN))
944                         goto done;
945
946                 if (dev == sitn->fb_tunnel_dev) {
947                         err = -EFAULT;
948                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
949                                 goto done;
950                         err = -ENOENT;
951                         if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
952                                 goto done;
953                         err = -EPERM;
954                         if (t == netdev_priv(sitn->fb_tunnel_dev))
955                                 goto done;
956                         dev = t->dev;
957                 }
958                 unregister_netdevice(dev);
959                 err = 0;
960                 break;
961
962         case SIOCGETPRL:
963         case SIOCADDPRL:
964         case SIOCDELPRL:
965         case SIOCCHGPRL:
966                 err = -EPERM;
967                 if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN))
968                         goto done;
969                 err = -EINVAL;
970                 if (dev == sitn->fb_tunnel_dev)
971                         goto done;
972                 err = -EFAULT;
973                 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
974                         goto done;
975                 err = -ENOENT;
976                 if (!(t = netdev_priv(dev)))
977                         goto done;
978
979                 switch (cmd) {
980                 case SIOCGETPRL:
981                         err = ipip6_tunnel_get_prl(t, &prl);
982                         if (!err && copy_to_user(ifr->ifr_ifru.ifru_data,
983                                                  &prl, sizeof(prl)))
984                                 err = -EFAULT;
985                         break;
986                 case SIOCDELPRL:
987                         err = ipip6_tunnel_del_prl(t, &prl);
988                         break;
989                 case SIOCADDPRL:
990                 case SIOCCHGPRL:
991                         err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
992                         break;
993                 }
994                 if (cmd != SIOCGETPRL)
995                         netdev_state_change(dev);
996                 break;
997
998         default:
999                 err = -EINVAL;
1000         }
1001
1002 done:
1003         return err;
1004 }
1005
1006 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
1007 {
1008         return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
1009 }
1010
1011 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1012 {
1013         if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
1014                 return -EINVAL;
1015         dev->mtu = new_mtu;
1016         return 0;
1017 }
1018
1019 static void ipip6_tunnel_setup(struct net_device *dev)
1020 {
1021         dev->uninit             = ipip6_tunnel_uninit;
1022         dev->destructor         = free_netdev;
1023         dev->hard_start_xmit    = ipip6_tunnel_xmit;
1024         dev->get_stats          = ipip6_tunnel_get_stats;
1025         dev->do_ioctl           = ipip6_tunnel_ioctl;
1026         dev->change_mtu         = ipip6_tunnel_change_mtu;
1027
1028         dev->type               = ARPHRD_SIT;
1029         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
1030         dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr);
1031         dev->flags              = IFF_NOARP;
1032         dev->iflink             = 0;
1033         dev->addr_len           = 4;
1034         dev->features           |= NETIF_F_NETNS_LOCAL;
1035 }
1036
1037 static int ipip6_tunnel_init(struct net_device *dev)
1038 {
1039         struct ip_tunnel *tunnel;
1040
1041         tunnel = netdev_priv(dev);
1042
1043         tunnel->dev = dev;
1044         strcpy(tunnel->parms.name, dev->name);
1045
1046         memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1047         memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1048
1049         ipip6_tunnel_bind_dev(dev);
1050
1051         return 0;
1052 }
1053
1054 static int ipip6_fb_tunnel_init(struct net_device *dev)
1055 {
1056         struct ip_tunnel *tunnel = netdev_priv(dev);
1057         struct iphdr *iph = &tunnel->parms.iph;
1058         struct net *net = dev_net(dev);
1059         struct sit_net *sitn = net_generic(net, sit_net_id);
1060
1061         tunnel->dev = dev;
1062         strcpy(tunnel->parms.name, dev->name);
1063
1064         iph->version            = 4;
1065         iph->protocol           = IPPROTO_IPV6;
1066         iph->ihl                = 5;
1067         iph->ttl                = 64;
1068
1069         dev_hold(dev);
1070         sitn->tunnels_wc[0]     = tunnel;
1071         return 0;
1072 }
1073
1074 static struct xfrm_tunnel sit_handler = {
1075         .handler        =       ipip6_rcv,
1076         .err_handler    =       ipip6_err,
1077         .priority       =       1,
1078 };
1079
1080 static void sit_destroy_tunnels(struct sit_net *sitn)
1081 {
1082         int prio;
1083
1084         for (prio = 1; prio < 4; prio++) {
1085                 int h;
1086                 for (h = 0; h < HASH_SIZE; h++) {
1087                         struct ip_tunnel *t;
1088                         while ((t = sitn->tunnels[prio][h]) != NULL)
1089                                 unregister_netdevice(t->dev);
1090                 }
1091         }
1092 }
1093
1094 static int sit_init_net(struct net *net)
1095 {
1096         int err;
1097         struct sit_net *sitn;
1098
1099         err = -ENOMEM;
1100         sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
1101         if (sitn == NULL)
1102                 goto err_alloc;
1103
1104         err = net_assign_generic(net, sit_net_id, sitn);
1105         if (err < 0)
1106                 goto err_assign;
1107
1108         sitn->tunnels[0] = sitn->tunnels_wc;
1109         sitn->tunnels[1] = sitn->tunnels_l;
1110         sitn->tunnels[2] = sitn->tunnels_r;
1111         sitn->tunnels[3] = sitn->tunnels_r_l;
1112
1113         sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1114                                            ipip6_tunnel_setup);
1115         if (!sitn->fb_tunnel_dev) {
1116                 err = -ENOMEM;
1117                 goto err_alloc_dev;
1118         }
1119
1120         sitn->fb_tunnel_dev->init = ipip6_fb_tunnel_init;
1121         dev_net_set(sitn->fb_tunnel_dev, net);
1122
1123         if ((err = register_netdev(sitn->fb_tunnel_dev)))
1124                 goto err_reg_dev;
1125
1126         return 0;
1127
1128 err_reg_dev:
1129         free_netdev(sitn->fb_tunnel_dev);
1130 err_alloc_dev:
1131         /* nothing */
1132 err_assign:
1133         kfree(sitn);
1134 err_alloc:
1135         return err;
1136 }
1137
1138 static void sit_exit_net(struct net *net)
1139 {
1140         struct sit_net *sitn;
1141
1142         sitn = net_generic(net, sit_net_id);
1143         rtnl_lock();
1144         sit_destroy_tunnels(sitn);
1145         unregister_netdevice(sitn->fb_tunnel_dev);
1146         rtnl_unlock();
1147         kfree(sitn);
1148 }
1149
1150 static struct pernet_operations sit_net_ops = {
1151         .init = sit_init_net,
1152         .exit = sit_exit_net,
1153 };
1154
1155 static void __exit sit_cleanup(void)
1156 {
1157         xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1158
1159         unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
1160 }
1161
1162 static int __init sit_init(void)
1163 {
1164         int err;
1165
1166         printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1167
1168         if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
1169                 printk(KERN_INFO "sit init: Can't add protocol\n");
1170                 return -EAGAIN;
1171         }
1172
1173         err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
1174         if (err < 0)
1175                 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1176
1177         return err;
1178 }
1179
1180 module_init(sit_init);
1181 module_exit(sit_cleanup);
1182 MODULE_LICENSE("GPL");
1183 MODULE_ALIAS("sit0");