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