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