[IPV6] IP6TUNNEL: Split out generic routine in ip6ip6_xmit().
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / net / ipv6 / ip6_tunnel.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
4 *
5 * Authors:
1ab1457c 6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
1da177e4
LT
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
1da177e4 22#include <linux/module.h>
4fc268d2 23#include <linux/capability.h>
1da177e4
LT
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>
1da177e4
LT
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
53MODULE_AUTHOR("Ville Nuorvala");
54MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
55MODULE_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
e69a4adc 69#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
1ab1457c
YH
70 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
71 (HASH_SIZE - 1))
1da177e4
LT
72
73static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
74static int ip6ip6_tnl_dev_init(struct net_device *dev);
75static void ip6ip6_tnl_dev_setup(struct net_device *dev);
76
77/* the IPv6 tunnel fallback device */
78static struct net_device *ip6ip6_fb_tnl_dev;
79
80
81/* lists for storing tunnels in use */
82static struct ip6_tnl *tnls_r_l[HASH_SIZE];
83static struct ip6_tnl *tnls_wc[1];
84static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
85
86/* lock for the tunnel lists */
87static DEFINE_RWLOCK(ip6ip6_lock);
88
89static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
90{
91 struct dst_entry *dst = t->dst_cache;
92
1ab1457c 93 if (dst && dst->obsolete &&
1da177e4
LT
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
103static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
104{
105 dst_release(t->dst_cache);
106 t->dst_cache = NULL;
107}
108
109static 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
1ab1457c
YH
119 * @remote: the address of the tunnel exit-point
120 * @local: the address of the tunnel entry-point
1da177e4 121 *
1ab1457c 122 * Return:
1da177e4 123 * tunnel matching given end-points if found,
1ab1457c 124 * else fallback tunnel if its device is up,
1da177e4
LT
125 * else %NULL
126 **/
127
128static struct ip6_tnl *
129ip6ip6_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
1ab1457c 149 * @p: parameters containing tunnel end-points
1da177e4
LT
150 *
151 * Description:
1ab1457c 152 * ip6ip6_bucket() returns the head of the list matching the
1da177e4
LT
153 * &struct in6_addr entries laddr and raddr in @p.
154 *
1ab1457c 155 * Return: head of IPv6 tunnel list
1da177e4
LT
156 **/
157
158static struct ip6_tnl **
159ip6ip6_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
178static void
179ip6ip6_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
194static void
195ip6ip6_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.
1ab1457c
YH
216 *
217 * Return:
567131a7 218 * created tunnel or NULL
1da177e4
LT
219 **/
220
567131a7 221static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
1da177e4
LT
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 }
1ab1457c 237 if (i == IP6_TNL_MAX)
567131a7 238 goto failed;
1da177e4
LT
239 }
240 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
241 if (dev == NULL)
567131a7 242 goto failed;
1da177e4 243
2941a486 244 t = netdev_priv(dev);
1da177e4
LT
245 dev->init = ip6ip6_tnl_dev_init;
246 t->parms = *p;
247
248 if ((err = register_netdevice(dev)) < 0) {
249 free_netdev(dev);
567131a7 250 goto failed;
1da177e4
LT
251 }
252 dev_hold(dev);
1da177e4 253 ip6ip6_tnl_link(t);
567131a7
VN
254 return t;
255failed:
256 return NULL;
1da177e4
LT
257}
258
259/**
260 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
1ab1457c 261 * @p: tunnel parameters
1da177e4
LT
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:
567131a7 270 * matching tunnel or NULL
1da177e4
LT
271 **/
272
567131a7 273static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
1da177e4
LT
274{
275 struct in6_addr *remote = &p->raddr;
276 struct in6_addr *local = &p->laddr;
277 struct ip6_tnl *t;
278
1da177e4
LT
279 for (t = *ip6ip6_bucket(p); t; t = t->next) {
280 if (ipv6_addr_equal(local, &t->parms.laddr) &&
567131a7
VN
281 ipv6_addr_equal(remote, &t->parms.raddr))
282 return t;
1da177e4
LT
283 }
284 if (!create)
567131a7
VN
285 return NULL;
286 return ip6_tnl_create(p);
1da177e4
LT
287}
288
289/**
290 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
291 * @dev: the device to be destroyed
1ab1457c 292 *
1da177e4
LT
293 * Description:
294 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
295 **/
296
297static void
298ip6ip6_tnl_dev_uninit(struct net_device *dev)
299{
2941a486 300 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
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 *
1ab1457c
YH
317 * Return:
318 * 0 if none was found,
1da177e4
LT
319 * else index to encapsulation limit
320 **/
321
322static __u16
323parse_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/**
e490d1d8 375 * ip6_tnl_err - tunnel error handler
1da177e4
LT
376 *
377 * Description:
e490d1d8 378 * ip6_tnl_err() should handle errors in the tunnel according
1da177e4
LT
379 * to the specifications in RFC 2473.
380 **/
381
d2acc347 382static int
e490d1d8
YK
383ip6_tnl_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
384 int *type, int *code, int *msg, __be32 *info, int offset)
1da177e4
LT
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;
d2acc347 393 int err = -ENOENT;
1da177e4 394
1ab1457c
YH
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
1da177e4
LT
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
d2acc347
HX
403 err = 0;
404
e490d1d8 405 switch (*type) {
1da177e4
LT
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:
e490d1d8 417 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
1da177e4
LT
418 if (net_ratelimit())
419 printk(KERN_WARNING
420 "%s: Too small hop limit or "
1ab1457c 421 "routing loop in tunnel!\n",
1da177e4
LT
422 t->parms.name);
423 rel_msg = 1;
424 }
425 break;
426 case ICMPV6_PARAMPROB:
107a5fe6 427 teli = 0;
e490d1d8 428 if ((*code) == ICMPV6_HDR_FIELD)
107a5fe6 429 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
1da177e4 430
e490d1d8 431 if (teli && teli == ntohl(*info) - 2) {
1da177e4
LT
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 }
107a5fe6
VN
441 } else if (net_ratelimit()) {
442 printk(KERN_WARNING
443 "%s: Recipient unable to parse tunneled "
444 "packet!\n ", t->parms.name);
1da177e4
LT
445 }
446 break;
447 case ICMPV6_PKT_TOOBIG:
e490d1d8 448 mtu = ntohl(*info) - offset;
1da177e4
LT
449 if (mtu < IPV6_MIN_MTU)
450 mtu = IPV6_MIN_MTU;
451 t->dev->mtu = mtu;
452
cc6cdac0 453 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
1da177e4
LT
454 rel_type = ICMPV6_PKT_TOOBIG;
455 rel_code = 0;
456 rel_info = mtu;
457 rel_msg = 1;
458 }
459 break;
460 }
e490d1d8
YK
461
462 *type = rel_type;
463 *code = rel_code;
464 *info = rel_info;
465 *msg = rel_msg;
466
467out:
468 read_unlock(&ip6ip6_lock);
469 return err;
470}
471
472static int
473ip6ip6_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))) {
1da177e4
LT
488 struct rt6_info *rt;
489 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
305d4b3c 490
1da177e4 491 if (!skb2)
e490d1d8 492 return 0;
1da177e4
LT
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 }
e490d1d8
YK
512
513 return 0;
1da177e4
LT
514}
515
8359925b
YK
516static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
517 struct ipv6hdr *ipv6h,
518 struct sk_buff *skb)
1da177e4 519{
8359925b
YK
520 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
521 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
1da177e4 522
8359925b
YK
523 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
524 IP6_ECN_set_ce(skb->nh.ipv6h);
1da177e4 525}
8359925b 526
09c6bbf0
VN
527static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
528{
529 struct ip6_tnl_parm *p = &t->parms;
530 int ret = 0;
531
532 if (p->flags & IP6_TNL_F_CAP_RCV) {
1ab1457c 533 struct net_device *ldev = NULL;
09c6bbf0
VN
534
535 if (p->link)
536 ldev = dev_get_by_index(p->link);
537
538 if ((ipv6_addr_is_multicast(&p->laddr) ||
539 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
540 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
541 ret = 1;
542
543 if (ldev)
544 dev_put(ldev);
545 }
546 return ret;
547}
1da177e4
LT
548
549/**
550 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
551 * @skb: received socket buffer
8359925b
YK
552 * @protocol: ethernet protocol ID
553 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
1da177e4
LT
554 *
555 * Return: 0
556 **/
557
8359925b
YK
558static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
559 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
560 struct ipv6hdr *ipv6h,
561 struct sk_buff *skb))
1da177e4 562{
1da177e4
LT
563 struct ipv6hdr *ipv6h;
564 struct ip6_tnl *t;
565
1da177e4
LT
566 ipv6h = skb->nh.ipv6h;
567
568 read_lock(&ip6ip6_lock);
569
570 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
571 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
9f0ede52 572 read_unlock(&ip6ip6_lock);
50fba2aa 573 goto discard;
1da177e4
LT
574 }
575
09c6bbf0 576 if (!ip6_tnl_rcv_ctl(t)) {
1da177e4
LT
577 t->stat.rx_dropped++;
578 read_unlock(&ip6ip6_lock);
579 goto discard;
580 }
581 secpath_reset(skb);
582 skb->mac.raw = skb->nh.raw;
583 skb->nh.raw = skb->data;
8359925b 584 skb->protocol = htons(protocol);
1da177e4
LT
585 skb->pkt_type = PACKET_HOST;
586 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
587 skb->dev = t->dev;
588 dst_release(skb->dst);
589 skb->dst = NULL;
53ab61c6 590 nf_reset(skb);
8359925b
YK
591
592 dscp_ecn_decapsulate(t, ipv6h, skb);
593
1da177e4
LT
594 t->stat.rx_packets++;
595 t->stat.rx_bytes += skb->len;
596 netif_rx(skb);
597 read_unlock(&ip6ip6_lock);
598 return 0;
599 }
600 read_unlock(&ip6ip6_lock);
1da177e4 601 return 1;
50fba2aa
HX
602
603discard:
604 kfree_skb(skb);
605 return 0;
1da177e4
LT
606}
607
8359925b
YK
608static int ip6ip6_rcv(struct sk_buff *skb)
609{
610 return ip6_tnl_rcv(skb, ETH_P_IPV6, ip6ip6_dscp_ecn_decapsulate);
611}
612
6fb32dde
VN
613struct ipv6_tel_txoption {
614 struct ipv6_txoptions ops;
615 __u8 dst_opt[8];
616};
1da177e4 617
6fb32dde
VN
618static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
619{
620 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
1da177e4 621
6fb32dde
VN
622 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
623 opt->dst_opt[3] = 1;
624 opt->dst_opt[4] = encap_limit;
625 opt->dst_opt[5] = IPV6_TLV_PADN;
626 opt->dst_opt[6] = 1;
1da177e4 627
6fb32dde
VN
628 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
629 opt->ops.opt_nflen = 8;
1da177e4
LT
630}
631
632/**
633 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
634 * @t: the outgoing tunnel device
1ab1457c 635 * @hdr: IPv6 header from the incoming packet
1da177e4
LT
636 *
637 * Description:
1ab1457c 638 * Avoid trivial tunneling loop by checking that tunnel exit-point
1da177e4
LT
639 * doesn't match source of incoming packet.
640 *
1ab1457c 641 * Return:
1da177e4
LT
642 * 1 if conflict,
643 * 0 else
644 **/
645
646static inline int
647ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
648{
649 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
650}
651
09c6bbf0
VN
652static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
653{
654 struct ip6_tnl_parm *p = &t->parms;
655 int ret = 0;
656
1ab1457c 657 if (p->flags & IP6_TNL_F_CAP_XMIT) {
09c6bbf0
VN
658 struct net_device *ldev = NULL;
659
660 if (p->link)
661 ldev = dev_get_by_index(p->link);
662
663 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
664 printk(KERN_WARNING
665 "%s xmit: Local address not yet configured!\n",
666 p->name);
667 else if (!ipv6_addr_is_multicast(&p->raddr) &&
668 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
669 printk(KERN_WARNING
670 "%s xmit: Routing loop! "
671 "Remote address found on this node!\n",
672 p->name);
673 else
674 ret = 1;
675 if (ldev)
676 dev_put(ldev);
677 }
678 return ret;
679}
1da177e4 680/**
61ec2aec 681 * ip6_tnl_xmit2 - encapsulate packet and send
1da177e4 682 * @skb: the outgoing socket buffer
1ab1457c 683 * @dev: the outgoing tunnel device
61ec2aec
YK
684 * @dsfield: dscp code for outer header
685 * @fl: flow of tunneled packet
686 * @encap_limit: encapsulation limit
687 * @pmtu: Path MTU is stored if packet is too big
1da177e4
LT
688 *
689 * Description:
690 * Build new header and do some sanity checks on the packet before sending
691 * it.
692 *
1ab1457c 693 * Return:
1da177e4 694 * 0
61ec2aec
YK
695 * -1 fail
696 * %-EMSGSIZE message too big. return mtu in this case.
1da177e4
LT
697 **/
698
61ec2aec
YK
699static int ip6_tnl_xmit2(struct sk_buff *skb,
700 struct net_device *dev,
701 __u8 dsfield,
702 struct flowi *fl,
703 int encap_limit,
704 __u32 *pmtu)
1da177e4 705{
2941a486 706 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
707 struct net_device_stats *stats = &t->stat;
708 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
6fb32dde 709 struct ipv6_tel_txoption opt;
1da177e4
LT
710 struct dst_entry *dst;
711 struct net_device *tdev;
712 int mtu;
713 int max_headroom = sizeof(struct ipv6hdr);
714 u8 proto;
61ec2aec 715 int err = -1;
1da177e4 716 int pkt_len;
1da177e4 717
1da177e4
LT
718 if ((dst = ip6_tnl_dst_check(t)) != NULL)
719 dst_hold(dst);
a57ebc90 720 else {
61ec2aec 721 dst = ip6_route_output(NULL, fl);
1da177e4 722
61ec2aec 723 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
a57ebc90
PM
724 goto tx_err_link_failure;
725 }
1da177e4
LT
726
727 tdev = dst->dev;
728
729 if (tdev == dev) {
730 stats->collisions++;
731 if (net_ratelimit())
1ab1457c 732 printk(KERN_WARNING
1da177e4
LT
733 "%s: Local routing loop detected!\n",
734 t->parms.name);
735 goto tx_err_dst_release;
736 }
737 mtu = dst_mtu(dst) - sizeof (*ipv6h);
6fb32dde 738 if (encap_limit >= 0) {
1da177e4
LT
739 max_headroom += 8;
740 mtu -= 8;
741 }
742 if (mtu < IPV6_MIN_MTU)
743 mtu = IPV6_MIN_MTU;
26892058
YK
744 if (skb->dst)
745 skb->dst->ops->update_pmtu(skb->dst, mtu);
1da177e4 746 if (skb->len > mtu) {
61ec2aec
YK
747 *pmtu = mtu;
748 err = -EMSGSIZE;
1da177e4
LT
749 goto tx_err_dst_release;
750 }
751
752 /*
753 * Okay, now see if we can stuff it in the buffer as-is.
754 */
755 max_headroom += LL_RESERVED_SPACE(tdev);
1ab1457c
YH
756
757 if (skb_headroom(skb) < max_headroom ||
1da177e4
LT
758 skb_cloned(skb) || skb_shared(skb)) {
759 struct sk_buff *new_skb;
1ab1457c 760
1da177e4
LT
761 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
762 goto tx_err_dst_release;
763
764 if (skb->sk)
765 skb_set_owner_w(new_skb, skb->sk);
766 kfree_skb(skb);
767 skb = new_skb;
768 }
769 dst_release(skb->dst);
770 skb->dst = dst_clone(dst);
771
772 skb->h.raw = skb->nh.raw;
773
61ec2aec 774 proto = fl->proto;
6fb32dde
VN
775 if (encap_limit >= 0) {
776 init_tel_txopt(&opt, encap_limit);
777 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
778 }
1da177e4
LT
779 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
780 ipv6h = skb->nh.ipv6h;
61ec2aec 781 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
1da177e4
LT
782 dsfield = INET_ECN_encapsulate(0, dsfield);
783 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
784 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
785 ipv6h->hop_limit = t->parms.hop_limit;
786 ipv6h->nexthdr = proto;
61ec2aec
YK
787 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
788 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
1da177e4
LT
789 nf_reset(skb);
790 pkt_len = skb->len;
1ab1457c 791 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
1da177e4
LT
792 skb->dst->dev, dst_output);
793
b9df3cb8 794 if (net_xmit_eval(err) == 0) {
1da177e4
LT
795 stats->tx_bytes += pkt_len;
796 stats->tx_packets++;
797 } else {
798 stats->tx_errors++;
799 stats->tx_aborted_errors++;
800 }
801 ip6_tnl_dst_store(t, dst);
1da177e4
LT
802 return 0;
803tx_err_link_failure:
804 stats->tx_carrier_errors++;
805 dst_link_failure(skb);
806tx_err_dst_release:
807 dst_release(dst);
61ec2aec
YK
808 return err;
809}
810
811static inline int
812ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
813{
814 struct ip6_tnl *t = netdev_priv(dev);
815 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
816 int encap_limit = -1;
817 __u16 offset;
818 struct flowi fl;
819 __u8 dsfield;
820 __u32 mtu;
821 int err;
822
823 if (!ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
824 return -1;
825
826 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
827 struct ipv6_tlv_tnl_enc_lim *tel;
828 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
829 if (tel->encap_limit == 0) {
830 icmpv6_send(skb, ICMPV6_PARAMPROB,
831 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
832 return -1;
833 }
834 encap_limit = tel->encap_limit - 1;
835 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
836 encap_limit = t->parms.encap_limit;
837
838 memcpy(&fl, &t->fl, sizeof (fl));
839 fl.proto = IPPROTO_IPV6;
840
841 dsfield = ipv6_get_dsfield(ipv6h);
842 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
843 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
844 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
845 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
846
847 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
848 if (err != 0) {
849 if (err == -EMSGSIZE)
850 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
851 return -1;
852 }
853
854 return 0;
855}
856
857static int
858ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
859{
860 struct ip6_tnl *t = netdev_priv(dev);
861 struct net_device_stats *stats = &t->stat;
862 int ret;
863
864 if (t->recursion++) {
865 t->stat.collisions++;
866 goto tx_err;
867 }
868
869 switch (skb->protocol) {
870 case __constant_htons(ETH_P_IPV6):
871 ret = ip6ip6_tnl_xmit(skb, dev);
872 break;
873 default:
874 goto tx_err;
875 }
876
877 if (ret < 0)
878 goto tx_err;
879
880 t->recursion--;
881 return 0;
882
1da177e4
LT
883tx_err:
884 stats->tx_errors++;
885 stats->tx_dropped++;
886 kfree_skb(skb);
887 t->recursion--;
888 return 0;
889}
890
891static void ip6_tnl_set_cap(struct ip6_tnl *t)
892{
893 struct ip6_tnl_parm *p = &t->parms;
09c6bbf0
VN
894 int ltype = ipv6_addr_type(&p->laddr);
895 int rtype = ipv6_addr_type(&p->raddr);
1da177e4
LT
896
897 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
898
09c6bbf0
VN
899 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
900 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
901 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
305d4b3c 902 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
09c6bbf0
VN
903 if (ltype&IPV6_ADDR_UNICAST)
904 p->flags |= IP6_TNL_F_CAP_XMIT;
905 if (rtype&IPV6_ADDR_UNICAST)
906 p->flags |= IP6_TNL_F_CAP_RCV;
1da177e4
LT
907 }
908}
909
910static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
911{
912 struct net_device *dev = t->dev;
913 struct ip6_tnl_parm *p = &t->parms;
914 struct flowi *fl = &t->fl;
915
916 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
917 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
918
919 /* Set up flowi template */
920 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
921 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
922 fl->oif = p->link;
923 fl->fl6_flowlabel = 0;
924
925 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
926 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
927 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
928 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
929
930 ip6_tnl_set_cap(t);
931
932 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
933 dev->flags |= IFF_POINTOPOINT;
934 else
935 dev->flags &= ~IFF_POINTOPOINT;
936
937 dev->iflink = p->link;
938
939 if (p->flags & IP6_TNL_F_CAP_XMIT) {
305d4b3c
VN
940 int strict = (ipv6_addr_type(&p->raddr) &
941 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
942
1da177e4 943 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
305d4b3c 944 p->link, strict);
1da177e4
LT
945
946 if (rt == NULL)
947 return;
948
949 if (rt->rt6i_dev) {
950 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
951 sizeof (struct ipv6hdr);
952
953 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
954
955 if (dev->mtu < IPV6_MIN_MTU)
956 dev->mtu = IPV6_MIN_MTU;
957 }
958 dst_release(&rt->u.dst);
959 }
960}
961
962/**
963 * ip6ip6_tnl_change - update the tunnel parameters
964 * @t: tunnel to be changed
965 * @p: tunnel configuration parameters
966 * @active: != 0 if tunnel is ready for use
967 *
968 * Description:
969 * ip6ip6_tnl_change() updates the tunnel parameters
970 **/
971
972static int
973ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
974{
975 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
976 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
977 t->parms.flags = p->flags;
978 t->parms.hop_limit = p->hop_limit;
979 t->parms.encap_limit = p->encap_limit;
980 t->parms.flowinfo = p->flowinfo;
8181b8c1 981 t->parms.link = p->link;
0c088890 982 ip6_tnl_dst_reset(t);
1da177e4
LT
983 ip6ip6_tnl_link_config(t);
984 return 0;
985}
986
987/**
1ab1457c 988 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1da177e4
LT
989 * @dev: virtual device associated with tunnel
990 * @ifr: parameters passed from userspace
991 * @cmd: command to be performed
992 *
993 * Description:
1ab1457c
YH
994 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
995 * from userspace.
1da177e4
LT
996 *
997 * The possible commands are the following:
998 * %SIOCGETTUNNEL: get tunnel parameters for device
999 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1000 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1001 * %SIOCDELTUNNEL: delete tunnel
1002 *
1ab1457c 1003 * The fallback device "ip6tnl0", created during module
1da177e4
LT
1004 * initialization, can be used for creating other tunnel devices.
1005 *
1006 * Return:
1007 * 0 on success,
1008 * %-EFAULT if unable to copy data to or from userspace,
1009 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1010 * %-EINVAL if passed tunnel parameters are invalid,
1011 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1012 * %-ENODEV if attempting to change or delete a nonexisting device
1013 **/
1014
1015static int
1016ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1017{
1018 int err = 0;
1da177e4
LT
1019 struct ip6_tnl_parm p;
1020 struct ip6_tnl *t = NULL;
1021
1022 switch (cmd) {
1023 case SIOCGETTUNNEL:
1024 if (dev == ip6ip6_fb_tnl_dev) {
567131a7 1025 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
1da177e4
LT
1026 err = -EFAULT;
1027 break;
1028 }
567131a7
VN
1029 t = ip6ip6_tnl_locate(&p, 0);
1030 }
1031 if (t == NULL)
2941a486 1032 t = netdev_priv(dev);
1da177e4
LT
1033 memcpy(&p, &t->parms, sizeof (p));
1034 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1035 err = -EFAULT;
1036 }
1037 break;
1038 case SIOCADDTUNNEL:
1039 case SIOCCHGTUNNEL:
1040 err = -EPERM;
1da177e4
LT
1041 if (!capable(CAP_NET_ADMIN))
1042 break;
567131a7
VN
1043 err = -EFAULT;
1044 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1da177e4 1045 break;
567131a7
VN
1046 err = -EINVAL;
1047 if (p.proto != IPPROTO_IPV6)
1da177e4 1048 break;
567131a7
VN
1049 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1050 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1051 if (t != NULL) {
1052 if (t->dev != dev) {
1053 err = -EEXIST;
1054 break;
1055 }
1056 } else
1057 t = netdev_priv(dev);
1058
1da177e4
LT
1059 ip6ip6_tnl_unlink(t);
1060 err = ip6ip6_tnl_change(t, &p);
1061 ip6ip6_tnl_link(t);
1062 netdev_state_change(dev);
1063 }
567131a7 1064 if (t) {
1da177e4 1065 err = 0;
567131a7
VN
1066 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1067 err = -EFAULT;
1068
1069 } else
1070 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1da177e4
LT
1071 break;
1072 case SIOCDELTUNNEL:
1073 err = -EPERM;
1074 if (!capable(CAP_NET_ADMIN))
1075 break;
1076
1077 if (dev == ip6ip6_fb_tnl_dev) {
567131a7
VN
1078 err = -EFAULT;
1079 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1da177e4 1080 break;
567131a7
VN
1081 err = -ENOENT;
1082 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
1da177e4 1083 break;
567131a7
VN
1084 err = -EPERM;
1085 if (t->dev == ip6ip6_fb_tnl_dev)
1da177e4 1086 break;
567131a7 1087 dev = t->dev;
1da177e4 1088 }
22f8cde5
SH
1089 err = 0;
1090 unregister_netdevice(dev);
1da177e4
LT
1091 break;
1092 default:
1093 err = -EINVAL;
1094 }
1095 return err;
1096}
1097
1098/**
1ab1457c 1099 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1da177e4
LT
1100 * @dev: virtual device associated with tunnel
1101 *
1102 * Return: stats for device
1103 **/
1104
1105static struct net_device_stats *
1106ip6ip6_tnl_get_stats(struct net_device *dev)
1107{
2941a486 1108 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1da177e4
LT
1109}
1110
1111/**
1112 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1113 * @dev: virtual device associated with tunnel
1114 * @new_mtu: the new mtu
1115 *
1116 * Return:
1117 * 0 on success,
1118 * %-EINVAL if mtu too small
1119 **/
1120
1121static int
1122ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1123{
1124 if (new_mtu < IPV6_MIN_MTU) {
1125 return -EINVAL;
1126 }
1127 dev->mtu = new_mtu;
1128 return 0;
1129}
1130
1131/**
1132 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1133 * @dev: virtual device associated with tunnel
1134 *
1135 * Description:
1136 * Initialize function pointers and device parameters
1137 **/
1138
1139static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1140{
1141 SET_MODULE_OWNER(dev);
1142 dev->uninit = ip6ip6_tnl_dev_uninit;
1143 dev->destructor = free_netdev;
61ec2aec 1144 dev->hard_start_xmit = ip6_tnl_xmit;
1da177e4
LT
1145 dev->get_stats = ip6ip6_tnl_get_stats;
1146 dev->do_ioctl = ip6ip6_tnl_ioctl;
1147 dev->change_mtu = ip6ip6_tnl_change_mtu;
1148
1149 dev->type = ARPHRD_TUNNEL6;
1150 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1151 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1152 dev->flags |= IFF_NOARP;
1153 dev->addr_len = sizeof(struct in6_addr);
1154}
1155
1156
1157/**
1158 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1159 * @dev: virtual device associated with tunnel
1160 **/
1161
1162static inline void
1163ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1164{
2941a486 1165 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
1166 t->fl.proto = IPPROTO_IPV6;
1167 t->dev = dev;
1168 strcpy(t->parms.name, dev->name);
1169}
1170
1171/**
1172 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1173 * @dev: virtual device associated with tunnel
1174 **/
1175
1176static int
1177ip6ip6_tnl_dev_init(struct net_device *dev)
1178{
2941a486 1179 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
1180 ip6ip6_tnl_dev_init_gen(dev);
1181 ip6ip6_tnl_link_config(t);
1182 return 0;
1183}
1184
1185/**
1186 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1187 * @dev: fallback device
1188 *
1189 * Return: 0
1190 **/
1191
1ab1457c 1192static int
1da177e4
LT
1193ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1194{
2941a486 1195 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
1196 ip6ip6_tnl_dev_init_gen(dev);
1197 dev_hold(dev);
1198 tnls_wc[0] = t;
1199 return 0;
1200}
1201
1202static struct xfrm6_tunnel ip6ip6_handler = {
0303770d
PM
1203 .handler = ip6ip6_rcv,
1204 .err_handler = ip6ip6_err,
d2acc347 1205 .priority = 1,
1da177e4
LT
1206};
1207
1208/**
1209 * ip6_tunnel_init - register protocol and reserve needed resources
1210 *
1211 * Return: 0 on success
1212 **/
1213
1214static int __init ip6_tunnel_init(void)
1215{
1216 int err;
1217
73d605d1 1218 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
1da177e4
LT
1219 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1220 return -EAGAIN;
1221 }
1222 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1223 ip6ip6_tnl_dev_setup);
1224
1225 if (!ip6ip6_fb_tnl_dev) {
1226 err = -ENOMEM;
1227 goto fail;
1228 }
1229 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1230
1231 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1232 free_netdev(ip6ip6_fb_tnl_dev);
1233 goto fail;
1234 }
1235 return 0;
1236fail:
73d605d1 1237 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1da177e4
LT
1238 return err;
1239}
1240
b3fdd9f1
YK
1241static void __exit ip6ip6_destroy_tunnels(void)
1242{
1243 int h;
1244 struct ip6_tnl *t;
1245
1246 for (h = 0; h < HASH_SIZE; h++) {
1247 while ((t = tnls_r_l[h]) != NULL)
1248 unregister_netdevice(t->dev);
1249 }
1250
1251 t = tnls_wc[0];
1252 unregister_netdevice(t->dev);
1253}
1254
1da177e4
LT
1255/**
1256 * ip6_tunnel_cleanup - free resources and unregister protocol
1257 **/
1258
1259static void __exit ip6_tunnel_cleanup(void)
1260{
73d605d1 1261 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1da177e4
LT
1262 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1263
b3fdd9f1
YK
1264 rtnl_lock();
1265 ip6ip6_destroy_tunnels();
1266 rtnl_unlock();
1da177e4
LT
1267}
1268
1269module_init(ip6_tunnel_init);
1270module_exit(ip6_tunnel_cleanup);