ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv6 / ip6_tunnel.c
1 /*
2  *      IPv6 over IPv6 tunnel device
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Ville Nuorvala          <vnuorval@tcs.hut.fi>   
7  *
8  *      $Id$
9  *
10  *      Based on:
11  *      linux/net/ipv6/sit.c
12  *
13  *      RFC 2473
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  *
20  */
21
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/sockios.h>
27 #include <linux/if.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/if_tunnel.h>
31 #include <linux/net.h>
32 #include <linux/in6.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/icmpv6.h>
36 #include <linux/init.h>
37 #include <linux/route.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/netfilter_ipv6.h>
40
41 #include <asm/uaccess.h>
42 #include <asm/atomic.h>
43
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/protocol.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/xfrm.h>
51
52 MODULE_AUTHOR("Ville Nuorvala");
53 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
54 MODULE_LICENSE("GPL");
55
56 #define IPV6_TLV_TEL_DST_SIZE 8
57
58 #ifdef IP6_TNL_DEBUG
59 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
60 #else
61 #define IP6_TNL_TRACE(x...) do {;} while(0)
62 #endif
63
64 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
65
66 #define HASH_SIZE  32
67
68 #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
69                      (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
70                     (HASH_SIZE - 1))
71
72 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
73 static int ip6ip6_tnl_dev_init(struct net_device *dev);
74 static void ip6ip6_tnl_dev_setup(struct net_device *dev);
75
76 /* the IPv6 tunnel fallback device */
77 static struct net_device *ip6ip6_fb_tnl_dev;
78
79
80 /* lists for storing tunnels in use */
81 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
82 static struct ip6_tnl *tnls_wc[1];
83 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
84
85 /* lock for the tunnel lists */
86 static rwlock_t ip6ip6_lock = RW_LOCK_UNLOCKED;
87
88 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
89 {
90         struct dst_entry *dst = t->dst_cache;
91
92         if (dst && dst->obsolete && 
93             dst->ops->check(dst, t->dst_cookie) == NULL) {
94                 t->dst_cache = NULL;
95                 return NULL;
96         }
97
98         return dst;
99 }
100
101 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
102 {
103         dst_release(t->dst_cache);
104         t->dst_cache = NULL;
105 }
106
107 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
108 {
109         struct rt6_info *rt = (struct rt6_info *) dst;
110         t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
111         dst_release(t->dst_cache);
112         t->dst_cache = dst;
113 }
114
115 /**
116  * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
117  *   @remote: the address of the tunnel exit-point 
118  *   @local: the address of the tunnel entry-point 
119  *
120  * Return:  
121  *   tunnel matching given end-points if found,
122  *   else fallback tunnel if its device is up, 
123  *   else %NULL
124  **/
125
126 struct ip6_tnl *
127 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
128 {
129         unsigned h0 = HASH(remote);
130         unsigned h1 = HASH(local);
131         struct ip6_tnl *t;
132
133         for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
134                 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
135                     !ipv6_addr_cmp(remote, &t->parms.raddr) &&
136                     (t->dev->flags & IFF_UP))
137                         return t;
138         }
139         if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
140                 return t;
141
142         return NULL;
143 }
144
145 /**
146  * ip6ip6_bucket - get head of list matching given tunnel parameters
147  *   @p: parameters containing tunnel end-points 
148  *
149  * Description:
150  *   ip6ip6_bucket() returns the head of the list matching the 
151  *   &struct in6_addr entries laddr and raddr in @p.
152  *
153  * Return: head of IPv6 tunnel list 
154  **/
155
156 static struct ip6_tnl **
157 ip6ip6_bucket(struct ip6_tnl_parm *p)
158 {
159         struct in6_addr *remote = &p->raddr;
160         struct in6_addr *local = &p->laddr;
161         unsigned h = 0;
162         int prio = 0;
163
164         if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
165                 prio = 1;
166                 h = HASH(remote) ^ HASH(local);
167         }
168         return &tnls[prio][h];
169 }
170
171 /**
172  * ip6ip6_tnl_link - add tunnel to hash table
173  *   @t: tunnel to be added
174  **/
175
176 static void
177 ip6ip6_tnl_link(struct ip6_tnl *t)
178 {
179         struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
180
181         write_lock_bh(&ip6ip6_lock);
182         t->next = *tp;
183         write_unlock_bh(&ip6ip6_lock);
184         *tp = t;
185 }
186
187 /**
188  * ip6ip6_tnl_unlink - remove tunnel from hash table
189  *   @t: tunnel to be removed
190  **/
191
192 static void
193 ip6ip6_tnl_unlink(struct ip6_tnl *t)
194 {
195         struct ip6_tnl **tp;
196
197         for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
198                 if (t == *tp) {
199                         write_lock_bh(&ip6ip6_lock);
200                         *tp = t->next;
201                         write_unlock_bh(&ip6ip6_lock);
202                         break;
203                 }
204         }
205 }
206
207 /**
208  * ip6_tnl_create() - create a new tunnel
209  *   @p: tunnel parameters
210  *   @pt: pointer to new tunnel
211  *
212  * Description:
213  *   Create tunnel matching given parameters.
214  * 
215  * Return: 
216  *   0 on success
217  **/
218
219 static int
220 ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
221 {
222         struct net_device *dev;
223         struct ip6_tnl *t;
224         char name[IFNAMSIZ];
225         int err;
226
227         if (p->name[0]) {
228                 strlcpy(name, p->name, IFNAMSIZ);
229         } else {
230                 int i;
231                 for (i = 1; i < IP6_TNL_MAX; i++) {
232                         sprintf(name, "ip6tnl%d", i);
233                         if (__dev_get_by_name(name) == NULL)
234                                 break;
235                 }
236                 if (i == IP6_TNL_MAX) 
237                         return -ENOBUFS;
238         }
239         dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
240         if (dev == NULL)
241                 return -ENOMEM;
242
243         t = dev->priv;
244         dev->init = ip6ip6_tnl_dev_init;
245         t->parms = *p;
246
247         if ((err = register_netdevice(dev)) < 0) {
248                 free_netdev(dev);
249                 return err;
250         }
251         dev_hold(dev);
252
253         ip6ip6_tnl_link(t);
254         *pt = t;
255         return 0;
256 }
257
258 /**
259  * ip6ip6_tnl_locate - find or create tunnel matching given parameters
260  *   @p: tunnel parameters 
261  *   @create: != 0 if allowed to create new tunnel if no match found
262  *
263  * Description:
264  *   ip6ip6_tnl_locate() first tries to locate an existing tunnel
265  *   based on @parms. If this is unsuccessful, but @create is set a new
266  *   tunnel device is created and registered for use.
267  *
268  * Return:
269  *   0 if tunnel located or created,
270  *   -EINVAL if parameters incorrect,
271  *   -ENODEV if no matching tunnel available
272  **/
273
274 static int
275 ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
276 {
277         struct in6_addr *remote = &p->raddr;
278         struct in6_addr *local = &p->laddr;
279         struct ip6_tnl *t;
280
281         if (p->proto != IPPROTO_IPV6)
282                 return -EINVAL;
283
284         for (t = *ip6ip6_bucket(p); t; t = t->next) {
285                 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
286                     !ipv6_addr_cmp(remote, &t->parms.raddr)) {
287                         *pt = t;
288                         return (create ? -EEXIST : 0);
289                 }
290         }
291         if (!create)
292                 return -ENODEV;
293         
294         return ip6_tnl_create(p, pt);
295 }
296
297 /**
298  * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
299  *   @dev: the device to be destroyed
300  *   
301  * Description:
302  *   ip6ip6_tnl_dev_uninit() removes tunnel from its list
303  **/
304
305 static void
306 ip6ip6_tnl_dev_uninit(struct net_device *dev)
307 {
308         struct ip6_tnl *t = dev->priv;
309
310         if (dev == ip6ip6_fb_tnl_dev) {
311                 write_lock_bh(&ip6ip6_lock);
312                 tnls_wc[0] = NULL;
313                 write_unlock_bh(&ip6ip6_lock);
314         } else {
315                 ip6ip6_tnl_unlink(t);
316         }
317         ip6_tnl_dst_reset(t);
318         dev_put(dev);
319 }
320
321 /**
322  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
323  *   @skb: received socket buffer
324  *
325  * Return: 
326  *   0 if none was found, 
327  *   else index to encapsulation limit
328  **/
329
330 static __u16
331 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
332 {
333         struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
334         __u8 nexthdr = ipv6h->nexthdr;
335         __u16 off = sizeof (*ipv6h);
336
337         while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
338                 __u16 optlen = 0;
339                 struct ipv6_opt_hdr *hdr;
340                 if (raw + off + sizeof (*hdr) > skb->data &&
341                     !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
342                         break;
343
344                 hdr = (struct ipv6_opt_hdr *) (raw + off);
345                 if (nexthdr == NEXTHDR_FRAGMENT) {
346                         struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
347                         if (frag_hdr->frag_off)
348                                 break;
349                         optlen = 8;
350                 } else if (nexthdr == NEXTHDR_AUTH) {
351                         optlen = (hdr->hdrlen + 2) << 2;
352                 } else {
353                         optlen = ipv6_optlen(hdr);
354                 }
355                 if (nexthdr == NEXTHDR_DEST) {
356                         __u16 i = off + 2;
357                         while (1) {
358                                 struct ipv6_tlv_tnl_enc_lim *tel;
359
360                                 /* No more room for encapsulation limit */
361                                 if (i + sizeof (*tel) > off + optlen)
362                                         break;
363
364                                 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
365                                 /* return index of option if found and valid */
366                                 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
367                                     tel->length == 1)
368                                         return i;
369                                 /* else jump to next option */
370                                 if (tel->type)
371                                         i += tel->length + 2;
372                                 else
373                                         i++;
374                         }
375                 }
376                 nexthdr = hdr->nexthdr;
377                 off += optlen;
378         }
379         return 0;
380 }
381
382 /**
383  * ip6ip6_err - tunnel error handler
384  *
385  * Description:
386  *   ip6ip6_err() should handle errors in the tunnel according
387  *   to the specifications in RFC 2473.
388  **/
389
390 void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
391                    int type, int code, int offset, __u32 info)
392 {
393         struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
394         struct ip6_tnl *t;
395         int rel_msg = 0;
396         int rel_type = ICMPV6_DEST_UNREACH;
397         int rel_code = ICMPV6_ADDR_UNREACH;
398         __u32 rel_info = 0;
399         __u16 len;
400
401         /* If the packet doesn't contain the original IPv6 header we are 
402            in trouble since we might need the source address for further 
403            processing of the error. */
404
405         read_lock(&ip6ip6_lock);
406         if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
407                 goto out;
408
409         switch (type) {
410                 __u32 teli;
411                 struct ipv6_tlv_tnl_enc_lim *tel;
412                 __u32 mtu;
413         case ICMPV6_DEST_UNREACH:
414                 if (net_ratelimit())
415                         printk(KERN_WARNING
416                                "%s: Path to destination invalid "
417                                "or inactive!\n", t->parms.name);
418                 rel_msg = 1;
419                 break;
420         case ICMPV6_TIME_EXCEED:
421                 if (code == ICMPV6_EXC_HOPLIMIT) {
422                         if (net_ratelimit())
423                                 printk(KERN_WARNING
424                                        "%s: Too small hop limit or "
425                                        "routing loop in tunnel!\n", 
426                                        t->parms.name);
427                         rel_msg = 1;
428                 }
429                 break;
430         case ICMPV6_PARAMPROB:
431                 /* ignore if parameter problem not caused by a tunnel
432                    encapsulation limit sub-option */
433                 if (code != ICMPV6_HDR_FIELD) {
434                         break;
435                 }
436                 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
437
438                 if (teli && teli == ntohl(info) - 2) {
439                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
440                         if (tel->encap_limit == 0) {
441                                 if (net_ratelimit())
442                                         printk(KERN_WARNING
443                                                "%s: Too small encapsulation "
444                                                "limit or routing loop in "
445                                                "tunnel!\n", t->parms.name);
446                                 rel_msg = 1;
447                         }
448                 }
449                 break;
450         case ICMPV6_PKT_TOOBIG:
451                 mtu = ntohl(info) - offset;
452                 if (mtu < IPV6_MIN_MTU)
453                         mtu = IPV6_MIN_MTU;
454                 t->dev->mtu = mtu;
455
456                 if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
457                         rel_type = ICMPV6_PKT_TOOBIG;
458                         rel_code = 0;
459                         rel_info = mtu;
460                         rel_msg = 1;
461                 }
462                 break;
463         }
464         if (rel_msg &&  pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
465                 struct rt6_info *rt;
466                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
467                 if (!skb2)
468                         goto out;
469
470                 dst_release(skb2->dst);
471                 skb2->dst = NULL;
472                 skb_pull(skb2, offset);
473                 skb2->nh.raw = skb2->data;
474
475                 /* Try to guess incoming interface */
476                 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
477
478                 if (rt && rt->rt6i_dev)
479                         skb2->dev = rt->rt6i_dev;
480
481                 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
482
483                 if (rt)
484                         dst_release(&rt->u.dst);
485
486                 kfree_skb(skb2);
487         }
488 out:
489         read_unlock(&ip6ip6_lock);
490 }
491
492 /**
493  * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
494  *   @skb: received socket buffer
495  *
496  * Return: 0
497  **/
498
499 int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
500 {
501         struct sk_buff *skb = *pskb;
502         struct ipv6hdr *ipv6h;
503         struct ip6_tnl *t;
504
505         if (!pskb_may_pull(skb, sizeof (*ipv6h)))
506                 goto discard;
507
508         ipv6h = skb->nh.ipv6h;
509
510         read_lock(&ip6ip6_lock);
511
512         if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
513                 if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
514                         t->stat.rx_dropped++;
515                         read_unlock(&ip6ip6_lock);
516                         goto discard;
517                 }
518                 secpath_reset(skb);
519                 skb->mac.raw = skb->nh.raw;
520                 skb->nh.raw = skb->data;
521                 skb->protocol = htons(ETH_P_IPV6);
522                 skb->pkt_type = PACKET_HOST;
523                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
524                 skb->dev = t->dev;
525                 dst_release(skb->dst);
526                 skb->dst = NULL;
527                 t->stat.rx_packets++;
528                 t->stat.rx_bytes += skb->len;
529                 netif_rx(skb);
530                 read_unlock(&ip6ip6_lock);
531                 return 0;
532         }
533         read_unlock(&ip6ip6_lock);
534         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
535 discard:
536         kfree_skb(skb);
537         return 0;
538 }
539
540 static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
541 {
542         struct ipv6_tlv_tnl_enc_lim *tel;
543         struct ipv6_txoptions *opt;
544         __u8 *raw;
545
546         int opt_len = sizeof(*opt) + 8;
547
548         if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
549                 return NULL;
550         }
551         memset(opt, 0, opt_len);
552         opt->tot_len = opt_len;
553         opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
554         opt->opt_nflen = 8;
555
556         tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
557         tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
558         tel->length = 1;
559         tel->encap_limit = encap_limit;
560
561         raw = (__u8 *) opt->dst0opt;
562         raw[5] = IPV6_TLV_PADN;
563         raw[6] = 1;
564
565         return opt;
566 }
567
568 /**
569  * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
570  *   @t: the outgoing tunnel device
571  *   @hdr: IPv6 header from the incoming packet 
572  *
573  * Description:
574  *   Avoid trivial tunneling loop by checking that tunnel exit-point 
575  *   doesn't match source of incoming packet.
576  *
577  * Return: 
578  *   1 if conflict,
579  *   0 else
580  **/
581
582 static inline int
583 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
584 {
585         return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);
586 }
587
588 /**
589  * ip6ip6_tnl_xmit - encapsulate packet and send 
590  *   @skb: the outgoing socket buffer
591  *   @dev: the outgoing tunnel device 
592  *
593  * Description:
594  *   Build new header and do some sanity checks on the packet before sending
595  *   it.
596  *
597  * Return: 
598  *   0
599  **/
600
601 int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
602 {
603         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
604         struct net_device_stats *stats = &t->stat;
605         struct ipv6hdr *ipv6h = skb->nh.ipv6h;
606         struct ipv6_txoptions *opt = NULL;
607         int encap_limit = -1;
608         __u16 offset;
609         struct flowi fl;
610         struct dst_entry *dst;
611         struct net_device *tdev;
612         int mtu;
613         int max_headroom = sizeof(struct ipv6hdr);
614         u8 proto;
615         int err;
616         int pkt_len;
617
618         if (t->recursion++) {
619                 stats->collisions++;
620                 goto tx_err;
621         }
622         if (skb->protocol != htons(ETH_P_IPV6) ||
623             !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
624             ip6ip6_tnl_addr_conflict(t, ipv6h)) {
625                 goto tx_err;
626         }
627         if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
628                 struct ipv6_tlv_tnl_enc_lim *tel;
629                 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
630                 if (tel->encap_limit == 0) {
631                         icmpv6_send(skb, ICMPV6_PARAMPROB,
632                                     ICMPV6_HDR_FIELD, offset + 2, skb->dev);
633                         goto tx_err;
634                 }
635                 encap_limit = tel->encap_limit - 1;
636         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
637                 encap_limit = t->parms.encap_limit;
638         }
639         memcpy(&fl, &t->fl, sizeof (fl));
640         proto = fl.proto;
641
642         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
643                 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
644         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
645                 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
646
647         if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
648                 goto tx_err;
649
650         if ((dst = ip6_tnl_dst_check(t)) != NULL)
651                 dst_hold(dst);
652         else
653                 dst = ip6_route_output(NULL, &fl);
654
655         if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
656                 goto tx_err_link_failure;
657
658         tdev = dst->dev;
659
660         if (tdev == dev) {
661                 stats->collisions++;
662                 if (net_ratelimit())
663                         printk(KERN_WARNING 
664                                "%s: Local routing loop detected!\n",
665                                t->parms.name);
666                 goto tx_err_dst_release;
667         }
668         mtu = dst_pmtu(dst) - sizeof (*ipv6h);
669         if (opt) {
670                 max_headroom += 8;
671                 mtu -= 8;
672         }
673         if (mtu < IPV6_MIN_MTU)
674                 mtu = IPV6_MIN_MTU;
675         if (skb->dst && mtu < dst_pmtu(skb->dst)) {
676                 struct rt6_info *rt = (struct rt6_info *) skb->dst;
677                 rt->rt6i_flags |= RTF_MODIFIED;
678                 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
679         }
680         if (skb->len > mtu) {
681                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
682                 goto tx_err_dst_release;
683         }
684
685         /*
686          * Okay, now see if we can stuff it in the buffer as-is.
687          */
688         max_headroom += LL_RESERVED_SPACE(tdev);
689         
690         if (skb_headroom(skb) < max_headroom || 
691             skb_cloned(skb) || skb_shared(skb)) {
692                 struct sk_buff *new_skb;
693                 
694                 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
695                         goto tx_err_dst_release;
696
697                 if (skb->sk)
698                         skb_set_owner_w(new_skb, skb->sk);
699                 kfree_skb(skb);
700                 skb = new_skb;
701         }
702         dst_release(skb->dst);
703         skb->dst = dst_clone(dst);
704
705         skb->h.raw = skb->nh.raw;
706
707         if (opt)
708                 ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
709
710         skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
711         ipv6h = skb->nh.ipv6h;
712         *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
713         ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
714         ipv6h->hop_limit = t->parms.hop_limit;
715         ipv6h->nexthdr = proto;
716         ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
717         ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
718 #ifdef CONFIG_NETFILTER
719         nf_conntrack_put(skb->nfct);
720         skb->nfct = NULL;
721 #ifdef CONFIG_NETFILTER_DEBUG
722         skb->nf_debug = 0;
723 #endif
724 #endif
725         pkt_len = skb->len;
726         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 
727                       skb->dst->dev, dst_output);
728
729         if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
730                 stats->tx_bytes += pkt_len;
731                 stats->tx_packets++;
732         } else {
733                 stats->tx_errors++;
734                 stats->tx_aborted_errors++;
735         }
736         ip6_tnl_dst_store(t, dst);
737
738         if (opt)
739                 kfree(opt);
740
741         t->recursion--;
742         return 0;
743 tx_err_link_failure:
744         stats->tx_carrier_errors++;
745         dst_link_failure(skb);
746 tx_err_dst_release:
747         dst_release(dst);
748         if (opt)
749                 kfree(opt);
750 tx_err:
751         stats->tx_errors++;
752         stats->tx_dropped++;
753         kfree_skb(skb);
754         t->recursion--;
755         return 0;
756 }
757
758 static void ip6_tnl_set_cap(struct ip6_tnl *t)
759 {
760         struct ip6_tnl_parm *p = &t->parms;
761         struct in6_addr *laddr = &p->laddr;
762         struct in6_addr *raddr = &p->raddr;
763         int ltype = ipv6_addr_type(laddr);
764         int rtype = ipv6_addr_type(raddr);
765
766         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
767
768         if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
769             ((ltype|rtype) &
770              (IPV6_ADDR_UNICAST|
771               IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
772               IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
773                 struct net_device *ldev = NULL;
774                 int l_ok = 1;
775                 int r_ok = 1;
776
777                 if (p->link)
778                         ldev = dev_get_by_index(p->link);
779                 
780                 if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
781                         l_ok = 0;
782                 
783                 if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
784                         r_ok = 0;
785                 
786                 if (l_ok && r_ok) {
787                         if (ltype&IPV6_ADDR_UNICAST)
788                                 p->flags |= IP6_TNL_F_CAP_XMIT;
789                         if (rtype&IPV6_ADDR_UNICAST)
790                                 p->flags |= IP6_TNL_F_CAP_RCV;
791                 }
792                 if (ldev)
793                         dev_put(ldev);
794         }
795 }
796
797 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
798 {
799         struct net_device *dev = t->dev;
800         struct ip6_tnl_parm *p = &t->parms;
801         struct flowi *fl = &t->fl;
802
803         memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
804         memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
805
806         /* Set up flowi template */
807         ipv6_addr_copy(&fl->fl6_src, &p->laddr);
808         ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
809         fl->oif = p->link;
810         fl->fl6_flowlabel = 0;
811
812         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
813                 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
814         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
815                 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
816
817         ip6_tnl_set_cap(t);
818
819         if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
820                 dev->flags |= IFF_POINTOPOINT;
821         else
822                 dev->flags &= ~IFF_POINTOPOINT;
823
824         dev->iflink = p->link;
825
826         if (p->flags & IP6_TNL_F_CAP_XMIT) {
827                 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
828                                                  p->link, 0);
829
830                 if (rt == NULL)
831                         return;
832
833                 if (rt->rt6i_dev) {
834                         dev->hard_header_len = rt->rt6i_dev->hard_header_len +
835                                 sizeof (struct ipv6hdr);
836
837                         dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
838
839                         if (dev->mtu < IPV6_MIN_MTU)
840                                 dev->mtu = IPV6_MIN_MTU;
841                 }
842                 dst_release(&rt->u.dst);
843         }
844 }
845
846 /**
847  * ip6ip6_tnl_change - update the tunnel parameters
848  *   @t: tunnel to be changed
849  *   @p: tunnel configuration parameters
850  *   @active: != 0 if tunnel is ready for use
851  *
852  * Description:
853  *   ip6ip6_tnl_change() updates the tunnel parameters
854  **/
855
856 static int
857 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
858 {
859         ipv6_addr_copy(&t->parms.laddr, &p->laddr);
860         ipv6_addr_copy(&t->parms.raddr, &p->raddr);
861         t->parms.flags = p->flags;
862         t->parms.hop_limit = p->hop_limit;
863         t->parms.encap_limit = p->encap_limit;
864         t->parms.flowinfo = p->flowinfo;
865         ip6ip6_tnl_link_config(t);
866         return 0;
867 }
868
869 /**
870  * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace 
871  *   @dev: virtual device associated with tunnel
872  *   @ifr: parameters passed from userspace
873  *   @cmd: command to be performed
874  *
875  * Description:
876  *   ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels 
877  *   from userspace. 
878  *
879  *   The possible commands are the following:
880  *     %SIOCGETTUNNEL: get tunnel parameters for device
881  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
882  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
883  *     %SIOCDELTUNNEL: delete tunnel
884  *
885  *   The fallback device "ip6tnl0", created during module 
886  *   initialization, can be used for creating other tunnel devices.
887  *
888  * Return:
889  *   0 on success,
890  *   %-EFAULT if unable to copy data to or from userspace,
891  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
892  *   %-EINVAL if passed tunnel parameters are invalid,
893  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
894  *   %-ENODEV if attempting to change or delete a nonexisting device
895  **/
896
897 static int
898 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
899 {
900         int err = 0;
901         int create;
902         struct ip6_tnl_parm p;
903         struct ip6_tnl *t = NULL;
904
905         switch (cmd) {
906         case SIOCGETTUNNEL:
907                 if (dev == ip6ip6_fb_tnl_dev) {
908                         if (copy_from_user(&p,
909                                            ifr->ifr_ifru.ifru_data,
910                                            sizeof (p))) {
911                                 err = -EFAULT;
912                                 break;
913                         }
914                         if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
915                                 t = (struct ip6_tnl *) dev->priv;
916                         else if (err)
917                                 break;
918                 } else
919                         t = (struct ip6_tnl *) dev->priv;
920
921                 memcpy(&p, &t->parms, sizeof (p));
922                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
923                         err = -EFAULT;
924                 }
925                 break;
926         case SIOCADDTUNNEL:
927         case SIOCCHGTUNNEL:
928                 err = -EPERM;
929                 create = (cmd == SIOCADDTUNNEL);
930                 if (!capable(CAP_NET_ADMIN))
931                         break;
932                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
933                         err = -EFAULT;
934                         break;
935                 }
936                 if (!create && dev != ip6ip6_fb_tnl_dev) {
937                         t = (struct ip6_tnl *) dev->priv;
938                 }
939                 if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
940                         break;
941                 }
942                 if (cmd == SIOCCHGTUNNEL) {
943                         if (t->dev != dev) {
944                                 err = -EEXIST;
945                                 break;
946                         }
947                         ip6ip6_tnl_unlink(t);
948                         err = ip6ip6_tnl_change(t, &p);
949                         ip6ip6_tnl_link(t);
950                         netdev_state_change(dev);
951                 }
952                 if (copy_to_user(ifr->ifr_ifru.ifru_data,
953                                  &t->parms, sizeof (p))) {
954                         err = -EFAULT;
955                 } else {
956                         err = 0;
957                 }
958                 break;
959         case SIOCDELTUNNEL:
960                 err = -EPERM;
961                 if (!capable(CAP_NET_ADMIN))
962                         break;
963
964                 if (dev == ip6ip6_fb_tnl_dev) {
965                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
966                                            sizeof (p))) {
967                                 err = -EFAULT;
968                                 break;
969                         }
970                         err = ip6ip6_tnl_locate(&p, &t, 0);
971                         if (err)
972                                 break;
973                         if (t == ip6ip6_fb_tnl_dev->priv) {
974                                 err = -EPERM;
975                                 break;
976                         }
977                 } else {
978                         t = (struct ip6_tnl *) dev->priv;
979                 }
980                 err = unregister_netdevice(t->dev);
981                 break;
982         default:
983                 err = -EINVAL;
984         }
985         return err;
986 }
987
988 /**
989  * ip6ip6_tnl_get_stats - return the stats for tunnel device 
990  *   @dev: virtual device associated with tunnel
991  *
992  * Return: stats for device
993  **/
994
995 static struct net_device_stats *
996 ip6ip6_tnl_get_stats(struct net_device *dev)
997 {
998         return &(((struct ip6_tnl *) dev->priv)->stat);
999 }
1000
1001 /**
1002  * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1003  *   @dev: virtual device associated with tunnel
1004  *   @new_mtu: the new mtu
1005  *
1006  * Return:
1007  *   0 on success,
1008  *   %-EINVAL if mtu too small
1009  **/
1010
1011 static int
1012 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1013 {
1014         if (new_mtu < IPV6_MIN_MTU) {
1015                 return -EINVAL;
1016         }
1017         dev->mtu = new_mtu;
1018         return 0;
1019 }
1020
1021 /**
1022  * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1023  *   @dev: virtual device associated with tunnel
1024  *
1025  * Description:
1026  *   Initialize function pointers and device parameters
1027  **/
1028
1029 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1030 {
1031         SET_MODULE_OWNER(dev);
1032         dev->uninit = ip6ip6_tnl_dev_uninit;
1033         dev->destructor = free_netdev;
1034         dev->hard_start_xmit = ip6ip6_tnl_xmit;
1035         dev->get_stats = ip6ip6_tnl_get_stats;
1036         dev->do_ioctl = ip6ip6_tnl_ioctl;
1037         dev->change_mtu = ip6ip6_tnl_change_mtu;
1038
1039         dev->type = ARPHRD_TUNNEL6;
1040         dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1041         dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1042         dev->flags |= IFF_NOARP;
1043         dev->addr_len = sizeof(struct in6_addr);
1044 }
1045
1046
1047 /**
1048  * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1049  *   @dev: virtual device associated with tunnel
1050  **/
1051
1052 static inline void
1053 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1054 {
1055         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1056         t->fl.proto = IPPROTO_IPV6;
1057         t->dev = dev;
1058         strcpy(t->parms.name, dev->name);
1059 }
1060
1061 /**
1062  * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1063  *   @dev: virtual device associated with tunnel
1064  **/
1065
1066 static int
1067 ip6ip6_tnl_dev_init(struct net_device *dev)
1068 {
1069         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1070         ip6ip6_tnl_dev_init_gen(dev);
1071         ip6ip6_tnl_link_config(t);
1072         return 0;
1073 }
1074
1075 /**
1076  * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1077  *   @dev: fallback device
1078  *
1079  * Return: 0
1080  **/
1081
1082 int ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1083 {
1084         struct ip6_tnl *t = dev->priv;
1085         ip6ip6_tnl_dev_init_gen(dev);
1086         dev_hold(dev);
1087         tnls_wc[0] = t;
1088         return 0;
1089 }
1090
1091 static struct inet6_protocol ip6ip6_protocol = {
1092         .handler = ip6ip6_rcv,
1093         .err_handler = ip6ip6_err,
1094         .flags = INET6_PROTO_FINAL
1095 };
1096
1097 /**
1098  * ip6_tunnel_init - register protocol and reserve needed resources
1099  *
1100  * Return: 0 on success
1101  **/
1102
1103 static int __init ip6_tunnel_init(void)
1104 {
1105         int  err;
1106
1107         if ((err = inet6_add_protocol(&ip6ip6_protocol, IPPROTO_IPV6)) < 0) {
1108                 printk(KERN_ERR "Failed to register IPv6 protocol\n");
1109                 return err;
1110         }
1111         ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1112                                          ip6ip6_tnl_dev_setup);
1113
1114         if (!ip6ip6_fb_tnl_dev) {
1115                 err = -ENOMEM;
1116                 goto fail;
1117         }
1118         ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1119
1120         if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1121                 free_netdev(ip6ip6_fb_tnl_dev);
1122                 goto fail;
1123         }
1124         return 0;
1125 fail:
1126         inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
1127         return err;
1128 }
1129
1130 /**
1131  * ip6_tunnel_cleanup - free resources and unregister protocol
1132  **/
1133
1134 static void __exit ip6_tunnel_cleanup(void)
1135 {
1136         unregister_netdev(ip6ip6_fb_tnl_dev);
1137         inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
1138 }
1139
1140 module_init(ip6_tunnel_init);
1141 module_exit(ip6_tunnel_cleanup);