VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 static 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 static void 
391 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
392            int type, int code, int offset, __u32 info)
393 {
394         struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
395         struct ip6_tnl *t;
396         int rel_msg = 0;
397         int rel_type = ICMPV6_DEST_UNREACH;
398         int rel_code = ICMPV6_ADDR_UNREACH;
399         __u32 rel_info = 0;
400         __u16 len;
401
402         /* If the packet doesn't contain the original IPv6 header we are 
403            in trouble since we might need the source address for further 
404            processing of the error. */
405
406         read_lock(&ip6ip6_lock);
407         if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
408                 goto out;
409
410         switch (type) {
411                 __u32 teli;
412                 struct ipv6_tlv_tnl_enc_lim *tel;
413                 __u32 mtu;
414         case ICMPV6_DEST_UNREACH:
415                 if (net_ratelimit())
416                         printk(KERN_WARNING
417                                "%s: Path to destination invalid "
418                                "or inactive!\n", t->parms.name);
419                 rel_msg = 1;
420                 break;
421         case ICMPV6_TIME_EXCEED:
422                 if (code == ICMPV6_EXC_HOPLIMIT) {
423                         if (net_ratelimit())
424                                 printk(KERN_WARNING
425                                        "%s: Too small hop limit or "
426                                        "routing loop in tunnel!\n", 
427                                        t->parms.name);
428                         rel_msg = 1;
429                 }
430                 break;
431         case ICMPV6_PARAMPROB:
432                 /* ignore if parameter problem not caused by a tunnel
433                    encapsulation limit sub-option */
434                 if (code != ICMPV6_HDR_FIELD) {
435                         break;
436                 }
437                 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
438
439                 if (teli && teli == ntohl(info) - 2) {
440                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
441                         if (tel->encap_limit == 0) {
442                                 if (net_ratelimit())
443                                         printk(KERN_WARNING
444                                                "%s: Too small encapsulation "
445                                                "limit or routing loop in "
446                                                "tunnel!\n", t->parms.name);
447                                 rel_msg = 1;
448                         }
449                 }
450                 break;
451         case ICMPV6_PKT_TOOBIG:
452                 mtu = ntohl(info) - offset;
453                 if (mtu < IPV6_MIN_MTU)
454                         mtu = IPV6_MIN_MTU;
455                 t->dev->mtu = mtu;
456
457                 if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
458                         rel_type = ICMPV6_PKT_TOOBIG;
459                         rel_code = 0;
460                         rel_info = mtu;
461                         rel_msg = 1;
462                 }
463                 break;
464         }
465         if (rel_msg &&  pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
466                 struct rt6_info *rt;
467                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
468                 if (!skb2)
469                         goto out;
470
471                 dst_release(skb2->dst);
472                 skb2->dst = NULL;
473                 skb_pull(skb2, offset);
474                 skb2->nh.raw = skb2->data;
475
476                 /* Try to guess incoming interface */
477                 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
478
479                 if (rt && rt->rt6i_dev)
480                         skb2->dev = rt->rt6i_dev;
481
482                 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
483
484                 if (rt)
485                         dst_release(&rt->u.dst);
486
487                 kfree_skb(skb2);
488         }
489 out:
490         read_unlock(&ip6ip6_lock);
491 }
492
493 /**
494  * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
495  *   @skb: received socket buffer
496  *
497  * Return: 0
498  **/
499
500 static int 
501 ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
502 {
503         struct sk_buff *skb = *pskb;
504         struct ipv6hdr *ipv6h;
505         struct ip6_tnl *t;
506
507         if (!pskb_may_pull(skb, sizeof (*ipv6h)))
508                 goto discard;
509
510         ipv6h = skb->nh.ipv6h;
511
512         read_lock(&ip6ip6_lock);
513
514         if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
515                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
516                         kfree_skb(skb);
517                         return 0;
518                 }
519
520                 if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
521                         t->stat.rx_dropped++;
522                         read_unlock(&ip6ip6_lock);
523                         goto discard;
524                 }
525                 secpath_reset(skb);
526                 skb->mac.raw = skb->nh.raw;
527                 skb->nh.raw = skb->data;
528                 skb->protocol = htons(ETH_P_IPV6);
529                 skb->pkt_type = PACKET_HOST;
530                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
531                 skb->dev = t->dev;
532                 dst_release(skb->dst);
533                 skb->dst = NULL;
534                 t->stat.rx_packets++;
535                 t->stat.rx_bytes += skb->len;
536                 netif_rx(skb);
537                 read_unlock(&ip6ip6_lock);
538                 return 0;
539         }
540         read_unlock(&ip6ip6_lock);
541         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
542 discard:
543         return 1;
544 }
545
546 static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
547 {
548         struct ipv6_tlv_tnl_enc_lim *tel;
549         struct ipv6_txoptions *opt;
550         __u8 *raw;
551
552         int opt_len = sizeof(*opt) + 8;
553
554         if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
555                 return NULL;
556         }
557         memset(opt, 0, opt_len);
558         opt->tot_len = opt_len;
559         opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
560         opt->opt_nflen = 8;
561
562         tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
563         tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
564         tel->length = 1;
565         tel->encap_limit = encap_limit;
566
567         raw = (__u8 *) opt->dst0opt;
568         raw[5] = IPV6_TLV_PADN;
569         raw[6] = 1;
570
571         return opt;
572 }
573
574 /**
575  * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
576  *   @t: the outgoing tunnel device
577  *   @hdr: IPv6 header from the incoming packet 
578  *
579  * Description:
580  *   Avoid trivial tunneling loop by checking that tunnel exit-point 
581  *   doesn't match source of incoming packet.
582  *
583  * Return: 
584  *   1 if conflict,
585  *   0 else
586  **/
587
588 static inline int
589 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
590 {
591         return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);
592 }
593
594 /**
595  * ip6ip6_tnl_xmit - encapsulate packet and send 
596  *   @skb: the outgoing socket buffer
597  *   @dev: the outgoing tunnel device 
598  *
599  * Description:
600  *   Build new header and do some sanity checks on the packet before sending
601  *   it.
602  *
603  * Return: 
604  *   0
605  **/
606
607 static int 
608 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
609 {
610         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
611         struct net_device_stats *stats = &t->stat;
612         struct ipv6hdr *ipv6h = skb->nh.ipv6h;
613         struct ipv6_txoptions *opt = NULL;
614         int encap_limit = -1;
615         __u16 offset;
616         struct flowi fl;
617         struct dst_entry *dst;
618         struct net_device *tdev;
619         int mtu;
620         int max_headroom = sizeof(struct ipv6hdr);
621         u8 proto;
622         int err;
623         int pkt_len;
624
625         if (t->recursion++) {
626                 stats->collisions++;
627                 goto tx_err;
628         }
629         if (skb->protocol != htons(ETH_P_IPV6) ||
630             !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
631             ip6ip6_tnl_addr_conflict(t, ipv6h)) {
632                 goto tx_err;
633         }
634         if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
635                 struct ipv6_tlv_tnl_enc_lim *tel;
636                 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
637                 if (tel->encap_limit == 0) {
638                         icmpv6_send(skb, ICMPV6_PARAMPROB,
639                                     ICMPV6_HDR_FIELD, offset + 2, skb->dev);
640                         goto tx_err;
641                 }
642                 encap_limit = tel->encap_limit - 1;
643         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
644                 encap_limit = t->parms.encap_limit;
645         }
646         memcpy(&fl, &t->fl, sizeof (fl));
647         proto = fl.proto;
648
649         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
650                 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
651         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
652                 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
653
654         if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
655                 goto tx_err;
656
657         if ((dst = ip6_tnl_dst_check(t)) != NULL)
658                 dst_hold(dst);
659         else
660                 dst = ip6_route_output(NULL, &fl);
661
662         if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
663                 goto tx_err_link_failure;
664
665         tdev = dst->dev;
666
667         if (tdev == dev) {
668                 stats->collisions++;
669                 if (net_ratelimit())
670                         printk(KERN_WARNING 
671                                "%s: Local routing loop detected!\n",
672                                t->parms.name);
673                 goto tx_err_dst_release;
674         }
675         mtu = dst_pmtu(dst) - sizeof (*ipv6h);
676         if (opt) {
677                 max_headroom += 8;
678                 mtu -= 8;
679         }
680         if (mtu < IPV6_MIN_MTU)
681                 mtu = IPV6_MIN_MTU;
682         if (skb->dst && mtu < dst_pmtu(skb->dst)) {
683                 struct rt6_info *rt = (struct rt6_info *) skb->dst;
684                 rt->rt6i_flags |= RTF_MODIFIED;
685                 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
686         }
687         if (skb->len > mtu) {
688                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
689                 goto tx_err_dst_release;
690         }
691
692         /*
693          * Okay, now see if we can stuff it in the buffer as-is.
694          */
695         max_headroom += LL_RESERVED_SPACE(tdev);
696         
697         if (skb_headroom(skb) < max_headroom || 
698             skb_cloned(skb) || skb_shared(skb)) {
699                 struct sk_buff *new_skb;
700                 
701                 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
702                         goto tx_err_dst_release;
703
704                 if (skb->sk)
705                         skb_set_owner_w(new_skb, skb->sk);
706                 kfree_skb(skb);
707                 skb = new_skb;
708         }
709         dst_release(skb->dst);
710         skb->dst = dst_clone(dst);
711
712         skb->h.raw = skb->nh.raw;
713
714         if (opt)
715                 ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
716
717         skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
718         ipv6h = skb->nh.ipv6h;
719         *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
720         ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
721         ipv6h->hop_limit = t->parms.hop_limit;
722         ipv6h->nexthdr = proto;
723         ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
724         ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
725         nf_reset(skb);
726         pkt_len = skb->len;
727         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 
728                       skb->dst->dev, dst_output);
729
730         if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
731                 stats->tx_bytes += pkt_len;
732                 stats->tx_packets++;
733         } else {
734                 stats->tx_errors++;
735                 stats->tx_aborted_errors++;
736         }
737         ip6_tnl_dst_store(t, dst);
738
739         if (opt)
740                 kfree(opt);
741
742         t->recursion--;
743         return 0;
744 tx_err_link_failure:
745         stats->tx_carrier_errors++;
746         dst_link_failure(skb);
747 tx_err_dst_release:
748         dst_release(dst);
749         if (opt)
750                 kfree(opt);
751 tx_err:
752         stats->tx_errors++;
753         stats->tx_dropped++;
754         kfree_skb(skb);
755         t->recursion--;
756         return 0;
757 }
758
759 static void ip6_tnl_set_cap(struct ip6_tnl *t)
760 {
761         struct ip6_tnl_parm *p = &t->parms;
762         struct in6_addr *laddr = &p->laddr;
763         struct in6_addr *raddr = &p->raddr;
764         int ltype = ipv6_addr_type(laddr);
765         int rtype = ipv6_addr_type(raddr);
766
767         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
768
769         if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
770             ((ltype|rtype) &
771              (IPV6_ADDR_UNICAST|
772               IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
773               IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
774                 struct net_device *ldev = NULL;
775                 int l_ok = 1;
776                 int r_ok = 1;
777
778                 if (p->link)
779                         ldev = dev_get_by_index(p->link);
780                 
781                 if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
782                         l_ok = 0;
783                 
784                 if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
785                         r_ok = 0;
786                 
787                 if (l_ok && r_ok) {
788                         if (ltype&IPV6_ADDR_UNICAST)
789                                 p->flags |= IP6_TNL_F_CAP_XMIT;
790                         if (rtype&IPV6_ADDR_UNICAST)
791                                 p->flags |= IP6_TNL_F_CAP_RCV;
792                 }
793                 if (ldev)
794                         dev_put(ldev);
795         }
796 }
797
798 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
799 {
800         struct net_device *dev = t->dev;
801         struct ip6_tnl_parm *p = &t->parms;
802         struct flowi *fl = &t->fl;
803
804         memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
805         memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
806
807         /* Set up flowi template */
808         ipv6_addr_copy(&fl->fl6_src, &p->laddr);
809         ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
810         fl->oif = p->link;
811         fl->fl6_flowlabel = 0;
812
813         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
814                 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
815         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
816                 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
817
818         ip6_tnl_set_cap(t);
819
820         if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
821                 dev->flags |= IFF_POINTOPOINT;
822         else
823                 dev->flags &= ~IFF_POINTOPOINT;
824
825         dev->iflink = p->link;
826
827         if (p->flags & IP6_TNL_F_CAP_XMIT) {
828                 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
829                                                  p->link, 0);
830
831                 if (rt == NULL)
832                         return;
833
834                 if (rt->rt6i_dev) {
835                         dev->hard_header_len = rt->rt6i_dev->hard_header_len +
836                                 sizeof (struct ipv6hdr);
837
838                         dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
839
840                         if (dev->mtu < IPV6_MIN_MTU)
841                                 dev->mtu = IPV6_MIN_MTU;
842                 }
843                 dst_release(&rt->u.dst);
844         }
845 }
846
847 /**
848  * ip6ip6_tnl_change - update the tunnel parameters
849  *   @t: tunnel to be changed
850  *   @p: tunnel configuration parameters
851  *   @active: != 0 if tunnel is ready for use
852  *
853  * Description:
854  *   ip6ip6_tnl_change() updates the tunnel parameters
855  **/
856
857 static int
858 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
859 {
860         ipv6_addr_copy(&t->parms.laddr, &p->laddr);
861         ipv6_addr_copy(&t->parms.raddr, &p->raddr);
862         t->parms.flags = p->flags;
863         t->parms.hop_limit = p->hop_limit;
864         t->parms.encap_limit = p->encap_limit;
865         t->parms.flowinfo = p->flowinfo;
866         ip6ip6_tnl_link_config(t);
867         return 0;
868 }
869
870 /**
871  * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace 
872  *   @dev: virtual device associated with tunnel
873  *   @ifr: parameters passed from userspace
874  *   @cmd: command to be performed
875  *
876  * Description:
877  *   ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels 
878  *   from userspace. 
879  *
880  *   The possible commands are the following:
881  *     %SIOCGETTUNNEL: get tunnel parameters for device
882  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
883  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
884  *     %SIOCDELTUNNEL: delete tunnel
885  *
886  *   The fallback device "ip6tnl0", created during module 
887  *   initialization, can be used for creating other tunnel devices.
888  *
889  * Return:
890  *   0 on success,
891  *   %-EFAULT if unable to copy data to or from userspace,
892  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
893  *   %-EINVAL if passed tunnel parameters are invalid,
894  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
895  *   %-ENODEV if attempting to change or delete a nonexisting device
896  **/
897
898 static int
899 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
900 {
901         int err = 0;
902         int create;
903         struct ip6_tnl_parm p;
904         struct ip6_tnl *t = NULL;
905
906         switch (cmd) {
907         case SIOCGETTUNNEL:
908                 if (dev == ip6ip6_fb_tnl_dev) {
909                         if (copy_from_user(&p,
910                                            ifr->ifr_ifru.ifru_data,
911                                            sizeof (p))) {
912                                 err = -EFAULT;
913                                 break;
914                         }
915                         if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
916                                 t = (struct ip6_tnl *) dev->priv;
917                         else if (err)
918                                 break;
919                 } else
920                         t = (struct ip6_tnl *) dev->priv;
921
922                 memcpy(&p, &t->parms, sizeof (p));
923                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
924                         err = -EFAULT;
925                 }
926                 break;
927         case SIOCADDTUNNEL:
928         case SIOCCHGTUNNEL:
929                 err = -EPERM;
930                 create = (cmd == SIOCADDTUNNEL);
931                 if (!capable(CAP_NET_ADMIN))
932                         break;
933                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
934                         err = -EFAULT;
935                         break;
936                 }
937                 if (!create && dev != ip6ip6_fb_tnl_dev) {
938                         t = (struct ip6_tnl *) dev->priv;
939                 }
940                 if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
941                         break;
942                 }
943                 if (cmd == SIOCCHGTUNNEL) {
944                         if (t->dev != dev) {
945                                 err = -EEXIST;
946                                 break;
947                         }
948                         ip6ip6_tnl_unlink(t);
949                         err = ip6ip6_tnl_change(t, &p);
950                         ip6ip6_tnl_link(t);
951                         netdev_state_change(dev);
952                 }
953                 if (copy_to_user(ifr->ifr_ifru.ifru_data,
954                                  &t->parms, sizeof (p))) {
955                         err = -EFAULT;
956                 } else {
957                         err = 0;
958                 }
959                 break;
960         case SIOCDELTUNNEL:
961                 err = -EPERM;
962                 if (!capable(CAP_NET_ADMIN))
963                         break;
964
965                 if (dev == ip6ip6_fb_tnl_dev) {
966                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
967                                            sizeof (p))) {
968                                 err = -EFAULT;
969                                 break;
970                         }
971                         err = ip6ip6_tnl_locate(&p, &t, 0);
972                         if (err)
973                                 break;
974                         if (t == ip6ip6_fb_tnl_dev->priv) {
975                                 err = -EPERM;
976                                 break;
977                         }
978                 } else {
979                         t = (struct ip6_tnl *) dev->priv;
980                 }
981                 err = unregister_netdevice(t->dev);
982                 break;
983         default:
984                 err = -EINVAL;
985         }
986         return err;
987 }
988
989 /**
990  * ip6ip6_tnl_get_stats - return the stats for tunnel device 
991  *   @dev: virtual device associated with tunnel
992  *
993  * Return: stats for device
994  **/
995
996 static struct net_device_stats *
997 ip6ip6_tnl_get_stats(struct net_device *dev)
998 {
999         return &(((struct ip6_tnl *) dev->priv)->stat);
1000 }
1001
1002 /**
1003  * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1004  *   @dev: virtual device associated with tunnel
1005  *   @new_mtu: the new mtu
1006  *
1007  * Return:
1008  *   0 on success,
1009  *   %-EINVAL if mtu too small
1010  **/
1011
1012 static int
1013 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1014 {
1015         if (new_mtu < IPV6_MIN_MTU) {
1016                 return -EINVAL;
1017         }
1018         dev->mtu = new_mtu;
1019         return 0;
1020 }
1021
1022 /**
1023  * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1024  *   @dev: virtual device associated with tunnel
1025  *
1026  * Description:
1027  *   Initialize function pointers and device parameters
1028  **/
1029
1030 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1031 {
1032         SET_MODULE_OWNER(dev);
1033         dev->uninit = ip6ip6_tnl_dev_uninit;
1034         dev->destructor = free_netdev;
1035         dev->hard_start_xmit = ip6ip6_tnl_xmit;
1036         dev->get_stats = ip6ip6_tnl_get_stats;
1037         dev->do_ioctl = ip6ip6_tnl_ioctl;
1038         dev->change_mtu = ip6ip6_tnl_change_mtu;
1039
1040         dev->type = ARPHRD_TUNNEL6;
1041         dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1042         dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1043         dev->flags |= IFF_NOARP;
1044         dev->addr_len = sizeof(struct in6_addr);
1045 }
1046
1047
1048 /**
1049  * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1050  *   @dev: virtual device associated with tunnel
1051  **/
1052
1053 static inline void
1054 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1055 {
1056         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1057         t->fl.proto = IPPROTO_IPV6;
1058         t->dev = dev;
1059         strcpy(t->parms.name, dev->name);
1060 }
1061
1062 /**
1063  * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1064  *   @dev: virtual device associated with tunnel
1065  **/
1066
1067 static int
1068 ip6ip6_tnl_dev_init(struct net_device *dev)
1069 {
1070         struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1071         ip6ip6_tnl_dev_init_gen(dev);
1072         ip6ip6_tnl_link_config(t);
1073         return 0;
1074 }
1075
1076 /**
1077  * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1078  *   @dev: fallback device
1079  *
1080  * Return: 0
1081  **/
1082
1083 static int 
1084 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1085 {
1086         struct ip6_tnl *t = dev->priv;
1087         ip6ip6_tnl_dev_init_gen(dev);
1088         dev_hold(dev);
1089         tnls_wc[0] = t;
1090         return 0;
1091 }
1092
1093 static struct xfrm6_tunnel ip6ip6_handler = {
1094         .handler = ip6ip6_rcv,
1095         .err_handler = ip6ip6_err,
1096 };
1097
1098 /**
1099  * ip6_tunnel_init - register protocol and reserve needed resources
1100  *
1101  * Return: 0 on success
1102  **/
1103
1104 static int __init ip6_tunnel_init(void)
1105 {
1106         int  err;
1107
1108         if (xfrm6_tunnel_register(&ip6ip6_handler) < 0) {
1109                 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1110                 return -EAGAIN;
1111         }
1112         ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1113                                          ip6ip6_tnl_dev_setup);
1114
1115         if (!ip6ip6_fb_tnl_dev) {
1116                 err = -ENOMEM;
1117                 goto fail;
1118         }
1119         ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1120
1121         if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1122                 free_netdev(ip6ip6_fb_tnl_dev);
1123                 goto fail;
1124         }
1125         return 0;
1126 fail:
1127         xfrm6_tunnel_deregister(&ip6ip6_handler);
1128         return err;
1129 }
1130
1131 /**
1132  * ip6_tunnel_cleanup - free resources and unregister protocol
1133  **/
1134
1135 static void __exit ip6_tunnel_cleanup(void)
1136 {
1137         if (xfrm6_tunnel_deregister(&ip6ip6_handler) < 0)
1138                 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1139
1140         unregister_netdev(ip6ip6_fb_tnl_dev);
1141 }
1142
1143 module_init(ip6_tunnel_init);
1144 module_exit(ip6_tunnel_cleanup);