Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux-2.6] / net / ipv6 / ip6_tunnel.c
1 /*
2  *      IPv6 tunneling device
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Ville Nuorvala          <vnuorval@tcs.hut.fi>
7  *      Yasuyuki Kozakai        <kozakai@linux-ipv6.org>
8  *
9  *      $Id$
10  *
11  *      Based on:
12  *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
13  *
14  *      RFC 2473
15  *
16  *      This program is free software; you can redistribute it and/or
17  *      modify it under the terms of the GNU General Public License
18  *      as published by the Free Software Foundation; either version
19  *      2 of the License, or (at your option) any later version.
20  *
21  */
22
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/if_tunnel.h>
33 #include <linux/net.h>
34 #include <linux/in6.h>
35 #include <linux/netdevice.h>
36 #include <linux/if_arp.h>
37 #include <linux/icmpv6.h>
38 #include <linux/init.h>
39 #include <linux/route.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/netfilter_ipv6.h>
42
43 #include <asm/uaccess.h>
44 #include <asm/atomic.h>
45
46 #include <net/icmp.h>
47 #include <net/ip.h>
48 #include <net/ipv6.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_tunnel.h>
52 #include <net/xfrm.h>
53 #include <net/dsfield.h>
54 #include <net/inet_ecn.h>
55
56 MODULE_AUTHOR("Ville Nuorvala");
57 MODULE_DESCRIPTION("IPv6 tunneling device");
58 MODULE_LICENSE("GPL");
59
60 #define IPV6_TLV_TEL_DST_SIZE 8
61
62 #ifdef IP6_TNL_DEBUG
63 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
64 #else
65 #define IP6_TNL_TRACE(x...) do {;} while(0)
66 #endif
67
68 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
69 #define IPV6_TCLASS_SHIFT 20
70
71 #define HASH_SIZE  32
72
73 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
74                      (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
75                     (HASH_SIZE - 1))
76
77 static int ip6_fb_tnl_dev_init(struct net_device *dev);
78 static int ip6_tnl_dev_init(struct net_device *dev);
79 static void ip6_tnl_dev_setup(struct net_device *dev);
80
81 /* the IPv6 tunnel fallback device */
82 static struct net_device *ip6_fb_tnl_dev;
83
84
85 /* lists for storing tunnels in use */
86 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
87 static struct ip6_tnl *tnls_wc[1];
88 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
89
90 /* lock for the tunnel lists */
91 static DEFINE_RWLOCK(ip6_tnl_lock);
92
93 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
94 {
95         struct dst_entry *dst = t->dst_cache;
96
97         if (dst && dst->obsolete &&
98             dst->ops->check(dst, t->dst_cookie) == NULL) {
99                 t->dst_cache = NULL;
100                 dst_release(dst);
101                 return NULL;
102         }
103
104         return dst;
105 }
106
107 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
108 {
109         dst_release(t->dst_cache);
110         t->dst_cache = NULL;
111 }
112
113 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
114 {
115         struct rt6_info *rt = (struct rt6_info *) dst;
116         t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
117         dst_release(t->dst_cache);
118         t->dst_cache = dst;
119 }
120
121 /**
122  * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
123  *   @remote: the address of the tunnel exit-point
124  *   @local: the address of the tunnel entry-point
125  *
126  * Return:
127  *   tunnel matching given end-points if found,
128  *   else fallback tunnel if its device is up,
129  *   else %NULL
130  **/
131
132 static struct ip6_tnl *
133 ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
134 {
135         unsigned h0 = HASH(remote);
136         unsigned h1 = HASH(local);
137         struct ip6_tnl *t;
138
139         for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
140                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
141                     ipv6_addr_equal(remote, &t->parms.raddr) &&
142                     (t->dev->flags & IFF_UP))
143                         return t;
144         }
145         if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
146                 return t;
147
148         return NULL;
149 }
150
151 /**
152  * ip6_tnl_bucket - get head of list matching given tunnel parameters
153  *   @p: parameters containing tunnel end-points
154  *
155  * Description:
156  *   ip6_tnl_bucket() returns the head of the list matching the
157  *   &struct in6_addr entries laddr and raddr in @p.
158  *
159  * Return: head of IPv6 tunnel list
160  **/
161
162 static struct ip6_tnl **
163 ip6_tnl_bucket(struct ip6_tnl_parm *p)
164 {
165         struct in6_addr *remote = &p->raddr;
166         struct in6_addr *local = &p->laddr;
167         unsigned h = 0;
168         int prio = 0;
169
170         if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
171                 prio = 1;
172                 h = HASH(remote) ^ HASH(local);
173         }
174         return &tnls[prio][h];
175 }
176
177 /**
178  * ip6_tnl_link - add tunnel to hash table
179  *   @t: tunnel to be added
180  **/
181
182 static void
183 ip6_tnl_link(struct ip6_tnl *t)
184 {
185         struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
186
187         t->next = *tp;
188         write_lock_bh(&ip6_tnl_lock);
189         *tp = t;
190         write_unlock_bh(&ip6_tnl_lock);
191 }
192
193 /**
194  * ip6_tnl_unlink - remove tunnel from hash table
195  *   @t: tunnel to be removed
196  **/
197
198 static void
199 ip6_tnl_unlink(struct ip6_tnl *t)
200 {
201         struct ip6_tnl **tp;
202
203         for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
204                 if (t == *tp) {
205                         write_lock_bh(&ip6_tnl_lock);
206                         *tp = t->next;
207                         write_unlock_bh(&ip6_tnl_lock);
208                         break;
209                 }
210         }
211 }
212
213 /**
214  * ip6_tnl_create() - create a new tunnel
215  *   @p: tunnel parameters
216  *   @pt: pointer to new tunnel
217  *
218  * Description:
219  *   Create tunnel matching given parameters.
220  *
221  * Return:
222  *   created tunnel or NULL
223  **/
224
225 static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
226 {
227         struct net_device *dev;
228         struct ip6_tnl *t;
229         char name[IFNAMSIZ];
230         int err;
231
232         if (p->name[0]) {
233                 strlcpy(name, p->name, IFNAMSIZ);
234         } else {
235                 int i;
236                 for (i = 1; i < IP6_TNL_MAX; i++) {
237                         sprintf(name, "ip6tnl%d", i);
238                         if (__dev_get_by_name(name) == NULL)
239                                 break;
240                 }
241                 if (i == IP6_TNL_MAX)
242                         goto failed;
243         }
244         dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
245         if (dev == NULL)
246                 goto failed;
247
248         t = netdev_priv(dev);
249         dev->init = ip6_tnl_dev_init;
250         t->parms = *p;
251
252         if ((err = register_netdevice(dev)) < 0) {
253                 free_netdev(dev);
254                 goto failed;
255         }
256         dev_hold(dev);
257         ip6_tnl_link(t);
258         return t;
259 failed:
260         return NULL;
261 }
262
263 /**
264  * ip6_tnl_locate - find or create tunnel matching given parameters
265  *   @p: tunnel parameters
266  *   @create: != 0 if allowed to create new tunnel if no match found
267  *
268  * Description:
269  *   ip6_tnl_locate() first tries to locate an existing tunnel
270  *   based on @parms. If this is unsuccessful, but @create is set a new
271  *   tunnel device is created and registered for use.
272  *
273  * Return:
274  *   matching tunnel or NULL
275  **/
276
277 static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
278 {
279         struct in6_addr *remote = &p->raddr;
280         struct in6_addr *local = &p->laddr;
281         struct ip6_tnl *t;
282
283         for (t = *ip6_tnl_bucket(p); t; t = t->next) {
284                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
285                     ipv6_addr_equal(remote, &t->parms.raddr))
286                         return t;
287         }
288         if (!create)
289                 return NULL;
290         return ip6_tnl_create(p);
291 }
292
293 /**
294  * ip6_tnl_dev_uninit - tunnel device uninitializer
295  *   @dev: the device to be destroyed
296  *
297  * Description:
298  *   ip6_tnl_dev_uninit() removes tunnel from its list
299  **/
300
301 static void
302 ip6_tnl_dev_uninit(struct net_device *dev)
303 {
304         struct ip6_tnl *t = netdev_priv(dev);
305
306         if (dev == ip6_fb_tnl_dev) {
307                 write_lock_bh(&ip6_tnl_lock);
308                 tnls_wc[0] = NULL;
309                 write_unlock_bh(&ip6_tnl_lock);
310         } else {
311                 ip6_tnl_unlink(t);
312         }
313         ip6_tnl_dst_reset(t);
314         dev_put(dev);
315 }
316
317 /**
318  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
319  *   @skb: received socket buffer
320  *
321  * Return:
322  *   0 if none was found,
323  *   else index to encapsulation limit
324  **/
325
326 static __u16
327 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
328 {
329         struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
330         __u8 nexthdr = ipv6h->nexthdr;
331         __u16 off = sizeof (*ipv6h);
332
333         while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
334                 __u16 optlen = 0;
335                 struct ipv6_opt_hdr *hdr;
336                 if (raw + off + sizeof (*hdr) > skb->data &&
337                     !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
338                         break;
339
340                 hdr = (struct ipv6_opt_hdr *) (raw + off);
341                 if (nexthdr == NEXTHDR_FRAGMENT) {
342                         struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
343                         if (frag_hdr->frag_off)
344                                 break;
345                         optlen = 8;
346                 } else if (nexthdr == NEXTHDR_AUTH) {
347                         optlen = (hdr->hdrlen + 2) << 2;
348                 } else {
349                         optlen = ipv6_optlen(hdr);
350                 }
351                 if (nexthdr == NEXTHDR_DEST) {
352                         __u16 i = off + 2;
353                         while (1) {
354                                 struct ipv6_tlv_tnl_enc_lim *tel;
355
356                                 /* No more room for encapsulation limit */
357                                 if (i + sizeof (*tel) > off + optlen)
358                                         break;
359
360                                 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
361                                 /* return index of option if found and valid */
362                                 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
363                                     tel->length == 1)
364                                         return i;
365                                 /* else jump to next option */
366                                 if (tel->type)
367                                         i += tel->length + 2;
368                                 else
369                                         i++;
370                         }
371                 }
372                 nexthdr = hdr->nexthdr;
373                 off += optlen;
374         }
375         return 0;
376 }
377
378 /**
379  * ip6_tnl_err - tunnel error handler
380  *
381  * Description:
382  *   ip6_tnl_err() should handle errors in the tunnel according
383  *   to the specifications in RFC 2473.
384  **/
385
386 static int
387 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
388             int *type, int *code, int *msg, __be32 *info, int offset)
389 {
390         struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
391         struct ip6_tnl *t;
392         int rel_msg = 0;
393         int rel_type = ICMPV6_DEST_UNREACH;
394         int rel_code = ICMPV6_ADDR_UNREACH;
395         __u32 rel_info = 0;
396         __u16 len;
397         int err = -ENOENT;
398
399         /* If the packet doesn't contain the original IPv6 header we are
400            in trouble since we might need the source address for further
401            processing of the error. */
402
403         read_lock(&ip6_tnl_lock);
404         if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
405                 goto out;
406
407         if (t->parms.proto != ipproto && t->parms.proto != 0)
408                 goto out;
409
410         err = 0;
411
412         switch (*type) {
413                 __u32 teli;
414                 struct ipv6_tlv_tnl_enc_lim *tel;
415                 __u32 mtu;
416         case ICMPV6_DEST_UNREACH:
417                 if (net_ratelimit())
418                         printk(KERN_WARNING
419                                "%s: Path to destination invalid "
420                                "or inactive!\n", t->parms.name);
421                 rel_msg = 1;
422                 break;
423         case ICMPV6_TIME_EXCEED:
424                 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
425                         if (net_ratelimit())
426                                 printk(KERN_WARNING
427                                        "%s: Too small hop limit or "
428                                        "routing loop in tunnel!\n",
429                                        t->parms.name);
430                         rel_msg = 1;
431                 }
432                 break;
433         case ICMPV6_PARAMPROB:
434                 teli = 0;
435                 if ((*code) == ICMPV6_HDR_FIELD)
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                 } else if (net_ratelimit()) {
449                         printk(KERN_WARNING
450                                "%s: Recipient unable to parse tunneled "
451                                "packet!\n ", t->parms.name);
452                 }
453                 break;
454         case ICMPV6_PKT_TOOBIG:
455                 mtu = ntohl(*info) - offset;
456                 if (mtu < IPV6_MIN_MTU)
457                         mtu = IPV6_MIN_MTU;
458                 t->dev->mtu = mtu;
459
460                 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
461                         rel_type = ICMPV6_PKT_TOOBIG;
462                         rel_code = 0;
463                         rel_info = mtu;
464                         rel_msg = 1;
465                 }
466                 break;
467         }
468
469         *type = rel_type;
470         *code = rel_code;
471         *info = rel_info;
472         *msg = rel_msg;
473
474 out:
475         read_unlock(&ip6_tnl_lock);
476         return err;
477 }
478
479 static int
480 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
481            int type, int code, int offset, __u32 info)
482 {
483         int rel_msg = 0;
484         int rel_type = type;
485         int rel_code = code;
486         __u32 rel_info = info;
487         int err;
488         struct sk_buff *skb2;
489         struct iphdr *eiph;
490         struct flowi fl;
491         struct rtable *rt;
492
493         err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
494                           &rel_msg, &rel_info, offset);
495         if (err < 0)
496                 return err;
497
498         if (rel_msg == 0)
499                 return 0;
500
501         switch (rel_type) {
502         case ICMPV6_DEST_UNREACH:
503                 if (rel_code != ICMPV6_ADDR_UNREACH)
504                         return 0;
505                 rel_type = ICMP_DEST_UNREACH;
506                 rel_code = ICMP_HOST_UNREACH;
507                 break;
508         case ICMPV6_PKT_TOOBIG:
509                 if (rel_code != 0)
510                         return 0;
511                 rel_type = ICMP_DEST_UNREACH;
512                 rel_code = ICMP_FRAG_NEEDED;
513                 break;
514         default:
515                 return 0;
516         }
517
518         if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
519                 return 0;
520
521         skb2 = skb_clone(skb, GFP_ATOMIC);
522         if (!skb2)
523                 return 0;
524
525         dst_release(skb2->dst);
526         skb2->dst = NULL;
527         skb_pull(skb2, offset);
528         skb_reset_network_header(skb2);
529         eiph = ip_hdr(skb2);
530
531         /* Try to guess incoming interface */
532         memset(&fl, 0, sizeof(fl));
533         fl.fl4_dst = eiph->saddr;
534         fl.fl4_tos = RT_TOS(eiph->tos);
535         fl.proto = IPPROTO_IPIP;
536         if (ip_route_output_key(&rt, &fl))
537                 goto out;
538
539         skb2->dev = rt->u.dst.dev;
540
541         /* route "incoming" packet */
542         if (rt->rt_flags & RTCF_LOCAL) {
543                 ip_rt_put(rt);
544                 rt = NULL;
545                 fl.fl4_dst = eiph->daddr;
546                 fl.fl4_src = eiph->saddr;
547                 fl.fl4_tos = eiph->tos;
548                 if (ip_route_output_key(&rt, &fl) ||
549                     rt->u.dst.dev->type != ARPHRD_TUNNEL) {
550                         ip_rt_put(rt);
551                         goto out;
552                 }
553         } else {
554                 ip_rt_put(rt);
555                 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
556                                    skb2->dev) ||
557                     skb2->dst->dev->type != ARPHRD_TUNNEL)
558                         goto out;
559         }
560
561         /* change mtu on this route */
562         if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
563                 if (rel_info > dst_mtu(skb2->dst))
564                         goto out;
565
566                 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
567                 rel_info = htonl(rel_info);
568         }
569
570         icmp_send(skb2, rel_type, rel_code, rel_info);
571
572 out:
573         kfree_skb(skb2);
574         return 0;
575 }
576
577 static int
578 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
579            int type, int code, int offset, __u32 info)
580 {
581         int rel_msg = 0;
582         int rel_type = type;
583         int rel_code = code;
584         __u32 rel_info = info;
585         int err;
586
587         err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
588                           &rel_msg, &rel_info, offset);
589         if (err < 0)
590                 return err;
591
592         if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
593                 struct rt6_info *rt;
594                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
595
596                 if (!skb2)
597                         return 0;
598
599                 dst_release(skb2->dst);
600                 skb2->dst = NULL;
601                 skb_pull(skb2, offset);
602                 skb_reset_network_header(skb2);
603
604                 /* Try to guess incoming interface */
605                 rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
606
607                 if (rt && rt->rt6i_dev)
608                         skb2->dev = rt->rt6i_dev;
609
610                 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
611
612                 if (rt)
613                         dst_release(&rt->u.dst);
614
615                 kfree_skb(skb2);
616         }
617
618         return 0;
619 }
620
621 static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
622                                         struct ipv6hdr *ipv6h,
623                                         struct sk_buff *skb)
624 {
625         __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
626
627         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
628                 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
629
630         if (INET_ECN_is_ce(dsfield))
631                 IP_ECN_set_ce(ip_hdr(skb));
632 }
633
634 static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
635                                         struct ipv6hdr *ipv6h,
636                                         struct sk_buff *skb)
637 {
638         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
639                 ipv6_copy_dscp(ipv6h, ipv6_hdr(skb));
640
641         if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
642                 IP6_ECN_set_ce(ipv6_hdr(skb));
643 }
644
645 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
646 {
647         struct ip6_tnl_parm *p = &t->parms;
648         int ret = 0;
649
650         if (p->flags & IP6_TNL_F_CAP_RCV) {
651                 struct net_device *ldev = NULL;
652
653                 if (p->link)
654                         ldev = dev_get_by_index(p->link);
655
656                 if ((ipv6_addr_is_multicast(&p->laddr) ||
657                      likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
658                     likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
659                         ret = 1;
660
661                 if (ldev)
662                         dev_put(ldev);
663         }
664         return ret;
665 }
666
667 /**
668  * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
669  *   @skb: received socket buffer
670  *   @protocol: ethernet protocol ID
671  *   @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
672  *
673  * Return: 0
674  **/
675
676 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
677                        __u8 ipproto,
678                        void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
679                                                     struct ipv6hdr *ipv6h,
680                                                     struct sk_buff *skb))
681 {
682         struct ip6_tnl *t;
683         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
684
685         read_lock(&ip6_tnl_lock);
686
687         if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
688                 if (t->parms.proto != ipproto && t->parms.proto != 0) {
689                         read_unlock(&ip6_tnl_lock);
690                         goto discard;
691                 }
692
693                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
694                         read_unlock(&ip6_tnl_lock);
695                         goto discard;
696                 }
697
698                 if (!ip6_tnl_rcv_ctl(t)) {
699                         t->stat.rx_dropped++;
700                         read_unlock(&ip6_tnl_lock);
701                         goto discard;
702                 }
703                 secpath_reset(skb);
704                 skb->mac_header = skb->network_header;
705                 skb_reset_network_header(skb);
706                 skb->protocol = htons(protocol);
707                 skb->pkt_type = PACKET_HOST;
708                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
709                 skb->dev = t->dev;
710                 dst_release(skb->dst);
711                 skb->dst = NULL;
712                 nf_reset(skb);
713
714                 dscp_ecn_decapsulate(t, ipv6h, skb);
715
716                 t->stat.rx_packets++;
717                 t->stat.rx_bytes += skb->len;
718                 netif_rx(skb);
719                 read_unlock(&ip6_tnl_lock);
720                 return 0;
721         }
722         read_unlock(&ip6_tnl_lock);
723         return 1;
724
725 discard:
726         kfree_skb(skb);
727         return 0;
728 }
729
730 static int ip4ip6_rcv(struct sk_buff *skb)
731 {
732         return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
733                            ip4ip6_dscp_ecn_decapsulate);
734 }
735
736 static int ip6ip6_rcv(struct sk_buff *skb)
737 {
738         return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
739                            ip6ip6_dscp_ecn_decapsulate);
740 }
741
742 struct ipv6_tel_txoption {
743         struct ipv6_txoptions ops;
744         __u8 dst_opt[8];
745 };
746
747 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
748 {
749         memset(opt, 0, sizeof(struct ipv6_tel_txoption));
750
751         opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
752         opt->dst_opt[3] = 1;
753         opt->dst_opt[4] = encap_limit;
754         opt->dst_opt[5] = IPV6_TLV_PADN;
755         opt->dst_opt[6] = 1;
756
757         opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
758         opt->ops.opt_nflen = 8;
759 }
760
761 /**
762  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
763  *   @t: the outgoing tunnel device
764  *   @hdr: IPv6 header from the incoming packet
765  *
766  * Description:
767  *   Avoid trivial tunneling loop by checking that tunnel exit-point
768  *   doesn't match source of incoming packet.
769  *
770  * Return:
771  *   1 if conflict,
772  *   0 else
773  **/
774
775 static inline int
776 ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
777 {
778         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
779 }
780
781 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
782 {
783         struct ip6_tnl_parm *p = &t->parms;
784         int ret = 0;
785
786         if (p->flags & IP6_TNL_F_CAP_XMIT) {
787                 struct net_device *ldev = NULL;
788
789                 if (p->link)
790                         ldev = dev_get_by_index(p->link);
791
792                 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
793                         printk(KERN_WARNING
794                                "%s xmit: Local address not yet configured!\n",
795                                p->name);
796                 else if (!ipv6_addr_is_multicast(&p->raddr) &&
797                          unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
798                         printk(KERN_WARNING
799                                "%s xmit: Routing loop! "
800                                "Remote address found on this node!\n",
801                                p->name);
802                 else
803                         ret = 1;
804                 if (ldev)
805                         dev_put(ldev);
806         }
807         return ret;
808 }
809 /**
810  * ip6_tnl_xmit2 - encapsulate packet and send
811  *   @skb: the outgoing socket buffer
812  *   @dev: the outgoing tunnel device
813  *   @dsfield: dscp code for outer header
814  *   @fl: flow of tunneled packet
815  *   @encap_limit: encapsulation limit
816  *   @pmtu: Path MTU is stored if packet is too big
817  *
818  * Description:
819  *   Build new header and do some sanity checks on the packet before sending
820  *   it.
821  *
822  * Return:
823  *   0 on success
824  *   -1 fail
825  *   %-EMSGSIZE message too big. return mtu in this case.
826  **/
827
828 static int ip6_tnl_xmit2(struct sk_buff *skb,
829                          struct net_device *dev,
830                          __u8 dsfield,
831                          struct flowi *fl,
832                          int encap_limit,
833                          __u32 *pmtu)
834 {
835         struct ip6_tnl *t = netdev_priv(dev);
836         struct net_device_stats *stats = &t->stat;
837         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
838         struct ipv6_tel_txoption opt;
839         struct dst_entry *dst;
840         struct net_device *tdev;
841         int mtu;
842         int max_headroom = sizeof(struct ipv6hdr);
843         u8 proto;
844         int err = -1;
845         int pkt_len;
846
847         if ((dst = ip6_tnl_dst_check(t)) != NULL)
848                 dst_hold(dst);
849         else {
850                 dst = ip6_route_output(NULL, fl);
851
852                 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
853                         goto tx_err_link_failure;
854         }
855
856         tdev = dst->dev;
857
858         if (tdev == dev) {
859                 stats->collisions++;
860                 if (net_ratelimit())
861                         printk(KERN_WARNING
862                                "%s: Local routing loop detected!\n",
863                                t->parms.name);
864                 goto tx_err_dst_release;
865         }
866         mtu = dst_mtu(dst) - sizeof (*ipv6h);
867         if (encap_limit >= 0) {
868                 max_headroom += 8;
869                 mtu -= 8;
870         }
871         if (mtu < IPV6_MIN_MTU)
872                 mtu = IPV6_MIN_MTU;
873         if (skb->dst)
874                 skb->dst->ops->update_pmtu(skb->dst, mtu);
875         if (skb->len > mtu) {
876                 *pmtu = mtu;
877                 err = -EMSGSIZE;
878                 goto tx_err_dst_release;
879         }
880
881         /*
882          * Okay, now see if we can stuff it in the buffer as-is.
883          */
884         max_headroom += LL_RESERVED_SPACE(tdev);
885
886         if (skb_headroom(skb) < max_headroom ||
887             skb_cloned(skb) || skb_shared(skb)) {
888                 struct sk_buff *new_skb;
889
890                 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
891                         goto tx_err_dst_release;
892
893                 if (skb->sk)
894                         skb_set_owner_w(new_skb, skb->sk);
895                 kfree_skb(skb);
896                 skb = new_skb;
897         }
898         dst_release(skb->dst);
899         skb->dst = dst_clone(dst);
900
901         skb->transport_header = skb->network_header;
902
903         proto = fl->proto;
904         if (encap_limit >= 0) {
905                 init_tel_txopt(&opt, encap_limit);
906                 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
907         }
908         skb_push(skb, sizeof(struct ipv6hdr));
909         skb_reset_network_header(skb);
910         ipv6h = ipv6_hdr(skb);
911         *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
912         dsfield = INET_ECN_encapsulate(0, dsfield);
913         ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
914         ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
915         ipv6h->hop_limit = t->parms.hop_limit;
916         ipv6h->nexthdr = proto;
917         ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
918         ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
919         nf_reset(skb);
920         pkt_len = skb->len;
921         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
922                       skb->dst->dev, dst_output);
923
924         if (net_xmit_eval(err) == 0) {
925                 stats->tx_bytes += pkt_len;
926                 stats->tx_packets++;
927         } else {
928                 stats->tx_errors++;
929                 stats->tx_aborted_errors++;
930         }
931         ip6_tnl_dst_store(t, dst);
932         return 0;
933 tx_err_link_failure:
934         stats->tx_carrier_errors++;
935         dst_link_failure(skb);
936 tx_err_dst_release:
937         dst_release(dst);
938         return err;
939 }
940
941 static inline int
942 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
943 {
944         struct ip6_tnl *t = netdev_priv(dev);
945         struct iphdr  *iph = ip_hdr(skb);
946         int encap_limit = -1;
947         struct flowi fl;
948         __u8 dsfield;
949         __u32 mtu;
950         int err;
951
952         if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
953             !ip6_tnl_xmit_ctl(t))
954                 return -1;
955
956         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
957                 encap_limit = t->parms.encap_limit;
958
959         memcpy(&fl, &t->fl, sizeof (fl));
960         fl.proto = IPPROTO_IPIP;
961
962         dsfield = ipv4_get_dsfield(iph);
963
964         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
965                 fl.fl6_flowlabel |= ntohl(((__u32)iph->tos << IPV6_TCLASS_SHIFT)
966                                           & IPV6_TCLASS_MASK);
967
968         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
969         if (err != 0) {
970                 /* XXX: send ICMP error even if DF is not set. */
971                 if (err == -EMSGSIZE)
972                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
973                                   htonl(mtu));
974                 return -1;
975         }
976
977         return 0;
978 }
979
980 static inline int
981 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
982 {
983         struct ip6_tnl *t = netdev_priv(dev);
984         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
985         int encap_limit = -1;
986         __u16 offset;
987         struct flowi fl;
988         __u8 dsfield;
989         __u32 mtu;
990         int err;
991
992         if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
993             !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
994                 return -1;
995
996         offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
997         if (offset > 0) {
998                 struct ipv6_tlv_tnl_enc_lim *tel;
999                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1000                 if (tel->encap_limit == 0) {
1001                         icmpv6_send(skb, ICMPV6_PARAMPROB,
1002                                     ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1003                         return -1;
1004                 }
1005                 encap_limit = tel->encap_limit - 1;
1006         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1007                 encap_limit = t->parms.encap_limit;
1008
1009         memcpy(&fl, &t->fl, sizeof (fl));
1010         fl.proto = IPPROTO_IPV6;
1011
1012         dsfield = ipv6_get_dsfield(ipv6h);
1013         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1014                 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1015         if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1016                 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1017
1018         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1019         if (err != 0) {
1020                 if (err == -EMSGSIZE)
1021                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1022                 return -1;
1023         }
1024
1025         return 0;
1026 }
1027
1028 static int
1029 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1030 {
1031         struct ip6_tnl *t = netdev_priv(dev);
1032         struct net_device_stats *stats = &t->stat;
1033         int ret;
1034
1035         if (t->recursion++) {
1036                 t->stat.collisions++;
1037                 goto tx_err;
1038         }
1039
1040         switch (skb->protocol) {
1041         case __constant_htons(ETH_P_IP):
1042                 ret = ip4ip6_tnl_xmit(skb, dev);
1043                 break;
1044         case __constant_htons(ETH_P_IPV6):
1045                 ret = ip6ip6_tnl_xmit(skb, dev);
1046                 break;
1047         default:
1048                 goto tx_err;
1049         }
1050
1051         if (ret < 0)
1052                 goto tx_err;
1053
1054         t->recursion--;
1055         return 0;
1056
1057 tx_err:
1058         stats->tx_errors++;
1059         stats->tx_dropped++;
1060         kfree_skb(skb);
1061         t->recursion--;
1062         return 0;
1063 }
1064
1065 static void ip6_tnl_set_cap(struct ip6_tnl *t)
1066 {
1067         struct ip6_tnl_parm *p = &t->parms;
1068         int ltype = ipv6_addr_type(&p->laddr);
1069         int rtype = ipv6_addr_type(&p->raddr);
1070
1071         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1072
1073         if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1074             rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1075             !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
1076             (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
1077                 if (ltype&IPV6_ADDR_UNICAST)
1078                         p->flags |= IP6_TNL_F_CAP_XMIT;
1079                 if (rtype&IPV6_ADDR_UNICAST)
1080                         p->flags |= IP6_TNL_F_CAP_RCV;
1081         }
1082 }
1083
1084 static void ip6_tnl_link_config(struct ip6_tnl *t)
1085 {
1086         struct net_device *dev = t->dev;
1087         struct ip6_tnl_parm *p = &t->parms;
1088         struct flowi *fl = &t->fl;
1089
1090         memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1091         memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1092
1093         /* Set up flowi template */
1094         ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1095         ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1096         fl->oif = p->link;
1097         fl->fl6_flowlabel = 0;
1098
1099         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1100                 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1101         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1102                 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1103
1104         ip6_tnl_set_cap(t);
1105
1106         if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1107                 dev->flags |= IFF_POINTOPOINT;
1108         else
1109                 dev->flags &= ~IFF_POINTOPOINT;
1110
1111         dev->iflink = p->link;
1112
1113         if (p->flags & IP6_TNL_F_CAP_XMIT) {
1114                 int strict = (ipv6_addr_type(&p->raddr) &
1115                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1116
1117                 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
1118                                                  p->link, strict);
1119
1120                 if (rt == NULL)
1121                         return;
1122
1123                 if (rt->rt6i_dev) {
1124                         dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1125                                 sizeof (struct ipv6hdr);
1126
1127                         dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1128
1129                         if (dev->mtu < IPV6_MIN_MTU)
1130                                 dev->mtu = IPV6_MIN_MTU;
1131                 }
1132                 dst_release(&rt->u.dst);
1133         }
1134 }
1135
1136 /**
1137  * ip6_tnl_change - update the tunnel parameters
1138  *   @t: tunnel to be changed
1139  *   @p: tunnel configuration parameters
1140  *   @active: != 0 if tunnel is ready for use
1141  *
1142  * Description:
1143  *   ip6_tnl_change() updates the tunnel parameters
1144  **/
1145
1146 static int
1147 ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
1148 {
1149         ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1150         ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1151         t->parms.flags = p->flags;
1152         t->parms.hop_limit = p->hop_limit;
1153         t->parms.encap_limit = p->encap_limit;
1154         t->parms.flowinfo = p->flowinfo;
1155         t->parms.link = p->link;
1156         t->parms.proto = p->proto;
1157         ip6_tnl_dst_reset(t);
1158         ip6_tnl_link_config(t);
1159         return 0;
1160 }
1161
1162 /**
1163  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1164  *   @dev: virtual device associated with tunnel
1165  *   @ifr: parameters passed from userspace
1166  *   @cmd: command to be performed
1167  *
1168  * Description:
1169  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1170  *   from userspace.
1171  *
1172  *   The possible commands are the following:
1173  *     %SIOCGETTUNNEL: get tunnel parameters for device
1174  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1175  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
1176  *     %SIOCDELTUNNEL: delete tunnel
1177  *
1178  *   The fallback device "ip6tnl0", created during module
1179  *   initialization, can be used for creating other tunnel devices.
1180  *
1181  * Return:
1182  *   0 on success,
1183  *   %-EFAULT if unable to copy data to or from userspace,
1184  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
1185  *   %-EINVAL if passed tunnel parameters are invalid,
1186  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
1187  *   %-ENODEV if attempting to change or delete a nonexisting device
1188  **/
1189
1190 static int
1191 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1192 {
1193         int err = 0;
1194         struct ip6_tnl_parm p;
1195         struct ip6_tnl *t = NULL;
1196
1197         switch (cmd) {
1198         case SIOCGETTUNNEL:
1199                 if (dev == ip6_fb_tnl_dev) {
1200                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
1201                                 err = -EFAULT;
1202                                 break;
1203                         }
1204                         t = ip6_tnl_locate(&p, 0);
1205                 }
1206                 if (t == NULL)
1207                         t = netdev_priv(dev);
1208                 memcpy(&p, &t->parms, sizeof (p));
1209                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1210                         err = -EFAULT;
1211                 }
1212                 break;
1213         case SIOCADDTUNNEL:
1214         case SIOCCHGTUNNEL:
1215                 err = -EPERM;
1216                 if (!capable(CAP_NET_ADMIN))
1217                         break;
1218                 err = -EFAULT;
1219                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1220                         break;
1221                 err = -EINVAL;
1222                 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1223                     p.proto != 0)
1224                         break;
1225                 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1226                 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1227                         if (t != NULL) {
1228                                 if (t->dev != dev) {
1229                                         err = -EEXIST;
1230                                         break;
1231                                 }
1232                         } else
1233                                 t = netdev_priv(dev);
1234
1235                         ip6_tnl_unlink(t);
1236                         err = ip6_tnl_change(t, &p);
1237                         ip6_tnl_link(t);
1238                         netdev_state_change(dev);
1239                 }
1240                 if (t) {
1241                         err = 0;
1242                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1243                                 err = -EFAULT;
1244
1245                 } else
1246                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1247                 break;
1248         case SIOCDELTUNNEL:
1249                 err = -EPERM;
1250                 if (!capable(CAP_NET_ADMIN))
1251                         break;
1252
1253                 if (dev == ip6_fb_tnl_dev) {
1254                         err = -EFAULT;
1255                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1256                                 break;
1257                         err = -ENOENT;
1258                         if ((t = ip6_tnl_locate(&p, 0)) == NULL)
1259                                 break;
1260                         err = -EPERM;
1261                         if (t->dev == ip6_fb_tnl_dev)
1262                                 break;
1263                         dev = t->dev;
1264                 }
1265                 err = 0;
1266                 unregister_netdevice(dev);
1267                 break;
1268         default:
1269                 err = -EINVAL;
1270         }
1271         return err;
1272 }
1273
1274 /**
1275  * ip6_tnl_get_stats - return the stats for tunnel device
1276  *   @dev: virtual device associated with tunnel
1277  *
1278  * Return: stats for device
1279  **/
1280
1281 static struct net_device_stats *
1282 ip6_tnl_get_stats(struct net_device *dev)
1283 {
1284         return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1285 }
1286
1287 /**
1288  * ip6_tnl_change_mtu - change mtu manually for tunnel device
1289  *   @dev: virtual device associated with tunnel
1290  *   @new_mtu: the new mtu
1291  *
1292  * Return:
1293  *   0 on success,
1294  *   %-EINVAL if mtu too small
1295  **/
1296
1297 static int
1298 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1299 {
1300         if (new_mtu < IPV6_MIN_MTU) {
1301                 return -EINVAL;
1302         }
1303         dev->mtu = new_mtu;
1304         return 0;
1305 }
1306
1307 /**
1308  * ip6_tnl_dev_setup - setup virtual tunnel device
1309  *   @dev: virtual device associated with tunnel
1310  *
1311  * Description:
1312  *   Initialize function pointers and device parameters
1313  **/
1314
1315 static void ip6_tnl_dev_setup(struct net_device *dev)
1316 {
1317         SET_MODULE_OWNER(dev);
1318         dev->uninit = ip6_tnl_dev_uninit;
1319         dev->destructor = free_netdev;
1320         dev->hard_start_xmit = ip6_tnl_xmit;
1321         dev->get_stats = ip6_tnl_get_stats;
1322         dev->do_ioctl = ip6_tnl_ioctl;
1323         dev->change_mtu = ip6_tnl_change_mtu;
1324
1325         dev->type = ARPHRD_TUNNEL6;
1326         dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1327         dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1328         dev->flags |= IFF_NOARP;
1329         dev->addr_len = sizeof(struct in6_addr);
1330 }
1331
1332
1333 /**
1334  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1335  *   @dev: virtual device associated with tunnel
1336  **/
1337
1338 static inline void
1339 ip6_tnl_dev_init_gen(struct net_device *dev)
1340 {
1341         struct ip6_tnl *t = netdev_priv(dev);
1342         t->dev = dev;
1343         strcpy(t->parms.name, dev->name);
1344 }
1345
1346 /**
1347  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1348  *   @dev: virtual device associated with tunnel
1349  **/
1350
1351 static int
1352 ip6_tnl_dev_init(struct net_device *dev)
1353 {
1354         struct ip6_tnl *t = netdev_priv(dev);
1355         ip6_tnl_dev_init_gen(dev);
1356         ip6_tnl_link_config(t);
1357         return 0;
1358 }
1359
1360 /**
1361  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1362  *   @dev: fallback device
1363  *
1364  * Return: 0
1365  **/
1366
1367 static int
1368 ip6_fb_tnl_dev_init(struct net_device *dev)
1369 {
1370         struct ip6_tnl *t = netdev_priv(dev);
1371         ip6_tnl_dev_init_gen(dev);
1372         t->parms.proto = IPPROTO_IPV6;
1373         dev_hold(dev);
1374         tnls_wc[0] = t;
1375         return 0;
1376 }
1377
1378 static struct xfrm6_tunnel ip4ip6_handler = {
1379         .handler        = ip4ip6_rcv,
1380         .err_handler    = ip4ip6_err,
1381         .priority       =       1,
1382 };
1383
1384 static struct xfrm6_tunnel ip6ip6_handler = {
1385         .handler        = ip6ip6_rcv,
1386         .err_handler    = ip6ip6_err,
1387         .priority       =       1,
1388 };
1389
1390 /**
1391  * ip6_tunnel_init - register protocol and reserve needed resources
1392  *
1393  * Return: 0 on success
1394  **/
1395
1396 static int __init ip6_tunnel_init(void)
1397 {
1398         int  err;
1399
1400         if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
1401                 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
1402                 err = -EAGAIN;
1403                 goto out;
1404         }
1405
1406         if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
1407                 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
1408                 err = -EAGAIN;
1409                 goto unreg_ip4ip6;
1410         }
1411         ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1412                                       ip6_tnl_dev_setup);
1413
1414         if (!ip6_fb_tnl_dev) {
1415                 err = -ENOMEM;
1416                 goto fail;
1417         }
1418         ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
1419
1420         if ((err = register_netdev(ip6_fb_tnl_dev))) {
1421                 free_netdev(ip6_fb_tnl_dev);
1422                 goto fail;
1423         }
1424         return 0;
1425 fail:
1426         xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1427 unreg_ip4ip6:
1428         xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1429 out:
1430         return err;
1431 }
1432
1433 static void __exit ip6_tnl_destroy_tunnels(void)
1434 {
1435         int h;
1436         struct ip6_tnl *t;
1437
1438         for (h = 0; h < HASH_SIZE; h++) {
1439                 while ((t = tnls_r_l[h]) != NULL)
1440                         unregister_netdevice(t->dev);
1441         }
1442
1443         t = tnls_wc[0];
1444         unregister_netdevice(t->dev);
1445 }
1446
1447 /**
1448  * ip6_tunnel_cleanup - free resources and unregister protocol
1449  **/
1450
1451 static void __exit ip6_tunnel_cleanup(void)
1452 {
1453         if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1454                 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
1455
1456         if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1457                 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
1458
1459         rtnl_lock();
1460         ip6_tnl_destroy_tunnels();
1461         rtnl_unlock();
1462 }
1463
1464 module_init(ip6_tunnel_init);
1465 module_exit(ip6_tunnel_cleanup);