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