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