ip6_tunnel.c 38.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *	IPv6 tunneling device
L
Linus Torvalds 已提交
3 4 5
 *	Linux INET6 implementation
 *
 *	Authors:
6
 *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
7
 *	Yasuyuki Kozakai	<kozakai@linux-ipv6.org>
L
Linus Torvalds 已提交
8 9
 *
 *      Based on:
10
 *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20
 *
 *      RFC 2473
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 *
 */

21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
23
#include <linux/module.h>
24
#include <linux/capability.h>
L
Linus Torvalds 已提交
25 26 27
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/sockios.h>
28
#include <linux/icmp.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
#include <linux/if.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/if_tunnel.h>
#include <linux/net.h>
#include <linux/in6.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/icmpv6.h>
#include <linux/init.h>
#include <linux/route.h>
#include <linux/rtnetlink.h>
#include <linux/netfilter_ipv6.h>
42
#include <linux/slab.h>
E
Eric Dumazet 已提交
43
#include <linux/hash.h>
L
Linus Torvalds 已提交
44 45

#include <asm/uaccess.h>
A
Arun Sharma 已提交
46
#include <linux/atomic.h>
L
Linus Torvalds 已提交
47

48
#include <net/icmp.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56
#include <net/ip.h>
#include <net/ipv6.h>
#include <net/ip6_route.h>
#include <net/addrconf.h>
#include <net/ip6_tunnel.h>
#include <net/xfrm.h>
#include <net/dsfield.h>
#include <net/inet_ecn.h>
57 58
#include <net/net_namespace.h>
#include <net/netns/generic.h>
L
Linus Torvalds 已提交
59 60

MODULE_AUTHOR("Ville Nuorvala");
61
MODULE_DESCRIPTION("IPv6 tunneling device");
L
Linus Torvalds 已提交
62
MODULE_LICENSE("GPL");
S
stephen hemminger 已提交
63
MODULE_ALIAS_NETDEV("ip6tnl0");
L
Linus Torvalds 已提交
64 65

#ifdef IP6_TNL_DEBUG
66
#define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
L
Linus Torvalds 已提交
67 68 69 70 71
#else
#define IP6_TNL_TRACE(x...) do {;} while(0)
#endif

#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
72
#define IPV6_TCLASS_SHIFT 20
L
Linus Torvalds 已提交
73

E
Eric Dumazet 已提交
74 75
#define HASH_SIZE_SHIFT  5
#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
L
Linus Torvalds 已提交
76

E
Eric Dumazet 已提交
77 78 79 80 81 82
static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
{
	u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);

	return hash_32(hash, HASH_SIZE_SHIFT);
}
L
Linus Torvalds 已提交
83

E
Eric Dumazet 已提交
84
static int ip6_tnl_dev_init(struct net_device *dev);
85
static void ip6_tnl_dev_setup(struct net_device *dev);
L
Linus Torvalds 已提交
86

87
static int ip6_tnl_net_id __read_mostly;
88
struct ip6_tnl_net {
89 90
	/* the IPv6 tunnel fallback device */
	struct net_device *fb_tnl_dev;
91
	/* lists for storing tunnels in use */
E
Eric Dumazet 已提交
92 93 94
	struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
	struct ip6_tnl __rcu *tnls_wc[1];
	struct ip6_tnl __rcu **tnls[2];
95 96
};

E
Eric Dumazet 已提交
97 98 99 100 101 102
/* often modified stats are per cpu, other are shared (netdev->stats) */
struct pcpu_tstats {
	unsigned long	rx_packets;
	unsigned long	rx_bytes;
	unsigned long	tx_packets;
	unsigned long	tx_bytes;
E
Eric Dumazet 已提交
103
} __attribute__((aligned(4*sizeof(unsigned long))));
E
Eric Dumazet 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

static struct net_device_stats *ip6_get_stats(struct net_device *dev)
{
	struct pcpu_tstats sum = { 0 };
	int i;

	for_each_possible_cpu(i) {
		const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);

		sum.rx_packets += tstats->rx_packets;
		sum.rx_bytes   += tstats->rx_bytes;
		sum.tx_packets += tstats->tx_packets;
		sum.tx_bytes   += tstats->tx_bytes;
	}
	dev->stats.rx_packets = sum.rx_packets;
	dev->stats.rx_bytes   = sum.rx_bytes;
	dev->stats.tx_packets = sum.tx_packets;
	dev->stats.tx_bytes   = sum.tx_bytes;
	return &dev->stats;
}

125
/*
E
Eric Dumazet 已提交
126
 * Locking : hash tables are protected by RCU and RTNL
127
 */
L
Linus Torvalds 已提交
128

X
xeb@mail.ru 已提交
129
struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
L
Linus Torvalds 已提交
130 131 132
{
	struct dst_entry *dst = t->dst_cache;

133
	if (dst && dst->obsolete &&
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141
	    dst->ops->check(dst, t->dst_cookie) == NULL) {
		t->dst_cache = NULL;
		dst_release(dst);
		return NULL;
	}

	return dst;
}
X
xeb@mail.ru 已提交
142
EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
L
Linus Torvalds 已提交
143

X
xeb@mail.ru 已提交
144
void ip6_tnl_dst_reset(struct ip6_tnl *t)
L
Linus Torvalds 已提交
145 146 147 148
{
	dst_release(t->dst_cache);
	t->dst_cache = NULL;
}
X
xeb@mail.ru 已提交
149
EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
L
Linus Torvalds 已提交
150

X
xeb@mail.ru 已提交
151
void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
L
Linus Torvalds 已提交
152 153 154 155 156 157
{
	struct rt6_info *rt = (struct rt6_info *) dst;
	t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
	dst_release(t->dst_cache);
	t->dst_cache = dst;
}
X
xeb@mail.ru 已提交
158
EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
L
Linus Torvalds 已提交
159 160

/**
161
 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
162 163
 *   @remote: the address of the tunnel exit-point
 *   @local: the address of the tunnel entry-point
L
Linus Torvalds 已提交
164
 *
165
 * Return:
L
Linus Torvalds 已提交
166
 *   tunnel matching given end-points if found,
167
 *   else fallback tunnel if its device is up,
L
Linus Torvalds 已提交
168 169 170
 *   else %NULL
 **/

171 172 173
#define for_each_ip6_tunnel_rcu(start) \
	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))

L
Linus Torvalds 已提交
174
static struct ip6_tnl *
175
ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
L
Linus Torvalds 已提交
176
{
E
Eric Dumazet 已提交
177
	unsigned int hash = HASH(remote, local);
L
Linus Torvalds 已提交
178
	struct ip6_tnl *t;
179
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
L
Linus Torvalds 已提交
180

E
Eric Dumazet 已提交
181
	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
L
Linus Torvalds 已提交
182 183 184 185 186
		if (ipv6_addr_equal(local, &t->parms.laddr) &&
		    ipv6_addr_equal(remote, &t->parms.raddr) &&
		    (t->dev->flags & IFF_UP))
			return t;
	}
187 188
	t = rcu_dereference(ip6n->tnls_wc[0]);
	if (t && (t->dev->flags & IFF_UP))
L
Linus Torvalds 已提交
189 190 191 192 193 194
		return t;

	return NULL;
}

/**
195
 * ip6_tnl_bucket - get head of list matching given tunnel parameters
196
 *   @p: parameters containing tunnel end-points
L
Linus Torvalds 已提交
197 198
 *
 * Description:
199
 *   ip6_tnl_bucket() returns the head of the list matching the
L
Linus Torvalds 已提交
200 201
 *   &struct in6_addr entries laddr and raddr in @p.
 *
202
 * Return: head of IPv6 tunnel list
L
Linus Torvalds 已提交
203 204
 **/

E
Eric Dumazet 已提交
205
static struct ip6_tnl __rcu **
X
xeb@mail.ru 已提交
206
ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
L
Linus Torvalds 已提交
207
{
208 209
	const struct in6_addr *remote = &p->raddr;
	const struct in6_addr *local = &p->laddr;
210
	unsigned int h = 0;
L
Linus Torvalds 已提交
211 212 213 214
	int prio = 0;

	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
		prio = 1;
E
Eric Dumazet 已提交
215
		h = HASH(remote, local);
L
Linus Torvalds 已提交
216
	}
217
	return &ip6n->tnls[prio][h];
L
Linus Torvalds 已提交
218 219 220
}

/**
221
 * ip6_tnl_link - add tunnel to hash table
L
Linus Torvalds 已提交
222 223 224 225
 *   @t: tunnel to be added
 **/

static void
226
ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
L
Linus Torvalds 已提交
227
{
E
Eric Dumazet 已提交
228
	struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
L
Linus Torvalds 已提交
229

230 231
	rcu_assign_pointer(t->next , rtnl_dereference(*tp));
	rcu_assign_pointer(*tp, t);
L
Linus Torvalds 已提交
232 233 234
}

/**
235
 * ip6_tnl_unlink - remove tunnel from hash table
L
Linus Torvalds 已提交
236 237 238 239
 *   @t: tunnel to be removed
 **/

static void
240
ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
L
Linus Torvalds 已提交
241
{
E
Eric Dumazet 已提交
242 243 244 245 246 247 248
	struct ip6_tnl __rcu **tp;
	struct ip6_tnl *iter;

	for (tp = ip6_tnl_bucket(ip6n, &t->parms);
	     (iter = rtnl_dereference(*tp)) != NULL;
	     tp = &iter->next) {
		if (t == iter) {
249
			rcu_assign_pointer(*tp, t->next);
L
Linus Torvalds 已提交
250 251 252 253 254
			break;
		}
	}
}

E
Eric Dumazet 已提交
255 256 257 258 259 260
static void ip6_dev_free(struct net_device *dev)
{
	free_percpu(dev->tstats);
	free_netdev(dev);
}

L
Linus Torvalds 已提交
261
/**
262
 * ip6_tnl_create - create a new tunnel
L
Linus Torvalds 已提交
263 264 265 266 267
 *   @p: tunnel parameters
 *   @pt: pointer to new tunnel
 *
 * Description:
 *   Create tunnel matching given parameters.
268 269
 *
 * Return:
270
 *   created tunnel or NULL
L
Linus Torvalds 已提交
271 272
 **/

X
xeb@mail.ru 已提交
273
static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
L
Linus Torvalds 已提交
274 275 276 277 278
{
	struct net_device *dev;
	struct ip6_tnl *t;
	char name[IFNAMSIZ];
	int err;
279
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
L
Linus Torvalds 已提交
280

281
	if (p->name[0])
L
Linus Torvalds 已提交
282
		strlcpy(name, p->name, IFNAMSIZ);
283 284 285
	else
		sprintf(name, "ip6tnl%%d");

286
	dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
L
Linus Torvalds 已提交
287
	if (dev == NULL)
288
		goto failed;
L
Linus Torvalds 已提交
289

290 291
	dev_net_set(dev, net);

292
	t = netdev_priv(dev);
L
Linus Torvalds 已提交
293
	t->parms = *p;
E
Eric Dumazet 已提交
294 295 296
	err = ip6_tnl_dev_init(dev);
	if (err < 0)
		goto failed_free;
L
Linus Torvalds 已提交
297

298 299 300
	if ((err = register_netdevice(dev)) < 0)
		goto failed_free;

301 302
	strcpy(t->parms.name, dev->name);

L
Linus Torvalds 已提交
303
	dev_hold(dev);
304
	ip6_tnl_link(ip6n, t);
305
	return t;
306 307

failed_free:
E
Eric Dumazet 已提交
308
	ip6_dev_free(dev);
309 310
failed:
	return NULL;
L
Linus Torvalds 已提交
311 312 313
}

/**
314
 * ip6_tnl_locate - find or create tunnel matching given parameters
315
 *   @p: tunnel parameters
L
Linus Torvalds 已提交
316 317 318
 *   @create: != 0 if allowed to create new tunnel if no match found
 *
 * Description:
319
 *   ip6_tnl_locate() first tries to locate an existing tunnel
L
Linus Torvalds 已提交
320 321 322 323
 *   based on @parms. If this is unsuccessful, but @create is set a new
 *   tunnel device is created and registered for use.
 *
 * Return:
324
 *   matching tunnel or NULL
L
Linus Torvalds 已提交
325 326
 **/

327
static struct ip6_tnl *ip6_tnl_locate(struct net *net,
X
xeb@mail.ru 已提交
328
		struct __ip6_tnl_parm *p, int create)
L
Linus Torvalds 已提交
329
{
330 331
	const struct in6_addr *remote = &p->raddr;
	const struct in6_addr *local = &p->laddr;
E
Eric Dumazet 已提交
332
	struct ip6_tnl __rcu **tp;
L
Linus Torvalds 已提交
333
	struct ip6_tnl *t;
334
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
L
Linus Torvalds 已提交
335

E
Eric Dumazet 已提交
336 337 338
	for (tp = ip6_tnl_bucket(ip6n, p);
	     (t = rtnl_dereference(*tp)) != NULL;
	     tp = &t->next) {
L
Linus Torvalds 已提交
339
		if (ipv6_addr_equal(local, &t->parms.laddr) &&
340 341
		    ipv6_addr_equal(remote, &t->parms.raddr))
			return t;
L
Linus Torvalds 已提交
342 343
	}
	if (!create)
344
		return NULL;
345
	return ip6_tnl_create(net, p);
L
Linus Torvalds 已提交
346 347 348
}

/**
349
 * ip6_tnl_dev_uninit - tunnel device uninitializer
L
Linus Torvalds 已提交
350
 *   @dev: the device to be destroyed
351
 *
L
Linus Torvalds 已提交
352
 * Description:
353
 *   ip6_tnl_dev_uninit() removes tunnel from its list
L
Linus Torvalds 已提交
354 355 356
 **/

static void
357
ip6_tnl_dev_uninit(struct net_device *dev)
L
Linus Torvalds 已提交
358
{
359
	struct ip6_tnl *t = netdev_priv(dev);
360 361
	struct net *net = dev_net(dev);
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
L
Linus Torvalds 已提交
362

E
Eric Dumazet 已提交
363
	if (dev == ip6n->fb_tnl_dev)
364
		RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
E
Eric Dumazet 已提交
365
	else
366
		ip6_tnl_unlink(ip6n, t);
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374
	ip6_tnl_dst_reset(t);
	dev_put(dev);
}

/**
 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
 *   @skb: received socket buffer
 *
375 376
 * Return:
 *   0 if none was found,
L
Linus Torvalds 已提交
377 378 379
 *   else index to encapsulation limit
 **/

X
xeb@mail.ru 已提交
380
__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
L
Linus Torvalds 已提交
381
{
382
	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	__u8 nexthdr = ipv6h->nexthdr;
	__u16 off = sizeof (*ipv6h);

	while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
		__u16 optlen = 0;
		struct ipv6_opt_hdr *hdr;
		if (raw + off + sizeof (*hdr) > skb->data &&
		    !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
			break;

		hdr = (struct ipv6_opt_hdr *) (raw + off);
		if (nexthdr == NEXTHDR_FRAGMENT) {
			struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
			if (frag_hdr->frag_off)
				break;
			optlen = 8;
		} else if (nexthdr == NEXTHDR_AUTH) {
			optlen = (hdr->hdrlen + 2) << 2;
		} else {
			optlen = ipv6_optlen(hdr);
		}
		if (nexthdr == NEXTHDR_DEST) {
			__u16 i = off + 2;
			while (1) {
				struct ipv6_tlv_tnl_enc_lim *tel;

				/* No more room for encapsulation limit */
				if (i + sizeof (*tel) > off + optlen)
					break;

				tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
				/* return index of option if found and valid */
				if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
				    tel->length == 1)
					return i;
				/* else jump to next option */
				if (tel->type)
					i += tel->length + 2;
				else
					i++;
			}
		}
		nexthdr = hdr->nexthdr;
		off += optlen;
	}
	return 0;
}
X
xeb@mail.ru 已提交
430
EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
L
Linus Torvalds 已提交
431 432

/**
433
 * ip6_tnl_err - tunnel error handler
L
Linus Torvalds 已提交
434 435
 *
 * Description:
436
 *   ip6_tnl_err() should handle errors in the tunnel according
L
Linus Torvalds 已提交
437 438 439
 *   to the specifications in RFC 2473.
 **/

H
Herbert Xu 已提交
440
static int
441
ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
442
	    u8 *type, u8 *code, int *msg, __u32 *info, int offset)
L
Linus Torvalds 已提交
443
{
444
	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
L
Linus Torvalds 已提交
445 446
	struct ip6_tnl *t;
	int rel_msg = 0;
447 448
	u8 rel_type = ICMPV6_DEST_UNREACH;
	u8 rel_code = ICMPV6_ADDR_UNREACH;
L
Linus Torvalds 已提交
449 450
	__u32 rel_info = 0;
	__u16 len;
H
Herbert Xu 已提交
451
	int err = -ENOENT;
L
Linus Torvalds 已提交
452

453 454
	/* If the packet doesn't contain the original IPv6 header we are
	   in trouble since we might need the source address for further
L
Linus Torvalds 已提交
455 456
	   processing of the error. */

457
	rcu_read_lock();
458
	if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
459
					&ipv6h->saddr)) == NULL)
L
Linus Torvalds 已提交
460 461
		goto out;

462 463 464
	if (t->parms.proto != ipproto && t->parms.proto != 0)
		goto out;

H
Herbert Xu 已提交
465 466
	err = 0;

467
	switch (*type) {
L
Linus Torvalds 已提交
468 469 470 471
		__u32 teli;
		struct ipv6_tlv_tnl_enc_lim *tel;
		__u32 mtu;
	case ICMPV6_DEST_UNREACH:
472 473
		net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
				     t->parms.name);
L
Linus Torvalds 已提交
474 475 476
		rel_msg = 1;
		break;
	case ICMPV6_TIME_EXCEED:
477
		if ((*code) == ICMPV6_EXC_HOPLIMIT) {
478 479
			net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
					     t->parms.name);
L
Linus Torvalds 已提交
480 481 482 483
			rel_msg = 1;
		}
		break;
	case ICMPV6_PARAMPROB:
484
		teli = 0;
485
		if ((*code) == ICMPV6_HDR_FIELD)
X
xeb@mail.ru 已提交
486
			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
L
Linus Torvalds 已提交
487

A
Al Viro 已提交
488
		if (teli && teli == *info - 2) {
L
Linus Torvalds 已提交
489 490
			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
			if (tel->encap_limit == 0) {
491 492
				net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
						     t->parms.name);
L
Linus Torvalds 已提交
493 494
				rel_msg = 1;
			}
495 496 497
		} else {
			net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
					     t->parms.name);
L
Linus Torvalds 已提交
498 499 500
		}
		break;
	case ICMPV6_PKT_TOOBIG:
A
Al Viro 已提交
501
		mtu = *info - offset;
L
Linus Torvalds 已提交
502 503 504 505
		if (mtu < IPV6_MIN_MTU)
			mtu = IPV6_MIN_MTU;
		t->dev->mtu = mtu;

A
Al Viro 已提交
506
		if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
L
Linus Torvalds 已提交
507 508 509 510 511 512 513
			rel_type = ICMPV6_PKT_TOOBIG;
			rel_code = 0;
			rel_info = mtu;
			rel_msg = 1;
		}
		break;
	}
514 515 516 517 518 519 520

	*type = rel_type;
	*code = rel_code;
	*info = rel_info;
	*msg = rel_msg;

out:
521
	rcu_read_unlock();
522 523 524
	return err;
}

525 526
static int
ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
527
	   u8 type, u8 code, int offset, __be32 info)
528 529
{
	int rel_msg = 0;
530 531
	u8 rel_type = type;
	u8 rel_code = code;
A
Al Viro 已提交
532
	__u32 rel_info = ntohl(info);
533 534
	int err;
	struct sk_buff *skb2;
535
	const struct iphdr *eiph;
536
	struct rtable *rt;
537
	struct flowi4 fl4;
538

539 540
	err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
			  &rel_msg, &rel_info, offset);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	if (err < 0)
		return err;

	if (rel_msg == 0)
		return 0;

	switch (rel_type) {
	case ICMPV6_DEST_UNREACH:
		if (rel_code != ICMPV6_ADDR_UNREACH)
			return 0;
		rel_type = ICMP_DEST_UNREACH;
		rel_code = ICMP_HOST_UNREACH;
		break;
	case ICMPV6_PKT_TOOBIG:
		if (rel_code != 0)
			return 0;
		rel_type = ICMP_DEST_UNREACH;
		rel_code = ICMP_FRAG_NEEDED;
		break;
560 561 562
	case NDISC_REDIRECT:
		rel_type = ICMP_REDIRECT;
		rel_code = ICMP_REDIR_HOST;
563 564 565 566 567 568 569 570 571 572 573
	default:
		return 0;
	}

	if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
		return 0;

	skb2 = skb_clone(skb, GFP_ATOMIC);
	if (!skb2)
		return 0;

E
Eric Dumazet 已提交
574 575
	skb_dst_drop(skb2);

576
	skb_pull(skb2, offset);
577
	skb_reset_network_header(skb2);
578
	eiph = ip_hdr(skb2);
579 580

	/* Try to guess incoming interface */
581
	rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
582 583 584
				   eiph->saddr, 0,
				   0, 0,
				   IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
585
	if (IS_ERR(rt))
586 587
		goto out;

588
	skb2->dev = rt->dst.dev;
589 590 591 592 593

	/* route "incoming" packet */
	if (rt->rt_flags & RTCF_LOCAL) {
		ip_rt_put(rt);
		rt = NULL;
594
		rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
595 596 597 598
					   eiph->daddr, eiph->saddr,
					   0, 0,
					   IPPROTO_IPIP,
					   RT_TOS(eiph->tos), 0);
599
		if (IS_ERR(rt) ||
600
		    rt->dst.dev->type != ARPHRD_TUNNEL) {
601 602
			if (!IS_ERR(rt))
				ip_rt_put(rt);
603 604
			goto out;
		}
605
		skb_dst_set(skb2, &rt->dst);
606 607 608 609
	} else {
		ip_rt_put(rt);
		if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
				   skb2->dev) ||
E
Eric Dumazet 已提交
610
		    skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
611 612 613 614 615
			goto out;
	}

	/* change mtu on this route */
	if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
E
Eric Dumazet 已提交
616
		if (rel_info > dst_mtu(skb_dst(skb2)))
617 618
			goto out;

619
		skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
620
	}
621
	if (rel_type == ICMP_REDIRECT)
622
		skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
623

A
Al Viro 已提交
624
	icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
625 626 627 628 629 630

out:
	kfree_skb(skb2);
	return 0;
}

631 632
static int
ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
633
	   u8 type, u8 code, int offset, __be32 info)
634 635
{
	int rel_msg = 0;
636 637
	u8 rel_type = type;
	u8 rel_code = code;
A
Al Viro 已提交
638
	__u32 rel_info = ntohl(info);
639 640
	int err;

641 642
	err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
			  &rel_msg, &rel_info, offset);
643 644 645 646
	if (err < 0)
		return err;

	if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
L
Linus Torvalds 已提交
647 648
		struct rt6_info *rt;
		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
649

L
Linus Torvalds 已提交
650
		if (!skb2)
651
			return 0;
L
Linus Torvalds 已提交
652

E
Eric Dumazet 已提交
653
		skb_dst_drop(skb2);
L
Linus Torvalds 已提交
654
		skb_pull(skb2, offset);
655
		skb_reset_network_header(skb2);
L
Linus Torvalds 已提交
656 657

		/* Try to guess incoming interface */
658 659
		rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
				NULL, 0, 0);
L
Linus Torvalds 已提交
660

661 662
		if (rt && rt->dst.dev)
			skb2->dev = rt->dst.dev;
L
Linus Torvalds 已提交
663

664
		icmpv6_send(skb2, rel_type, rel_code, rel_info);
L
Linus Torvalds 已提交
665

A
Amerigo Wang 已提交
666
		ip6_rt_put(rt);
L
Linus Torvalds 已提交
667 668 669

		kfree_skb(skb2);
	}
670 671

	return 0;
L
Linus Torvalds 已提交
672 673
}

674 675
static void ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
					const struct ipv6hdr *ipv6h,
676 677 678 679 680
					struct sk_buff *skb)
{
	__u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;

	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
681
		ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
682 683

	if (INET_ECN_is_ce(dsfield))
684
		IP_ECN_set_ce(ip_hdr(skb));
685 686
}

687 688
static void ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
					const struct ipv6hdr *ipv6h,
689
					struct sk_buff *skb)
L
Linus Torvalds 已提交
690
{
691
	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
692
		ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
L
Linus Torvalds 已提交
693

694
	if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
695
		IP6_ECN_set_ce(ipv6_hdr(skb));
L
Linus Torvalds 已提交
696
}
697

X
xeb@mail.ru 已提交
698
__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
699 700 701
			     const struct in6_addr *laddr,
			     const struct in6_addr *raddr)
{
X
xeb@mail.ru 已提交
702
	struct __ip6_tnl_parm *p = &t->parms;
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	int ltype = ipv6_addr_type(laddr);
	int rtype = ipv6_addr_type(raddr);
	__u32 flags = 0;

	if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
		flags = IP6_TNL_F_CAP_PER_PACKET;
	} else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
		   rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
		   !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
		   (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
		if (ltype&IPV6_ADDR_UNICAST)
			flags |= IP6_TNL_F_CAP_XMIT;
		if (rtype&IPV6_ADDR_UNICAST)
			flags |= IP6_TNL_F_CAP_RCV;
	}
	return flags;
}
X
xeb@mail.ru 已提交
720
EXPORT_SYMBOL(ip6_tnl_get_cap);
721

E
Eric Dumazet 已提交
722
/* called with rcu_read_lock() */
X
xeb@mail.ru 已提交
723
int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
724 725
				  const struct in6_addr *laddr,
				  const struct in6_addr *raddr)
726
{
X
xeb@mail.ru 已提交
727
	struct __ip6_tnl_parm *p = &t->parms;
728
	int ret = 0;
729
	struct net *net = dev_net(t->dev);
730

731 732 733
	if ((p->flags & IP6_TNL_F_CAP_RCV) ||
	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
734
		struct net_device *ldev = NULL;
735 736

		if (p->link)
E
Eric Dumazet 已提交
737
			ldev = dev_get_by_index_rcu(net, p->link);
738

739 740 741
		if ((ipv6_addr_is_multicast(laddr) ||
		     likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
		    likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
742 743 744 745
			ret = 1;
	}
	return ret;
}
X
xeb@mail.ru 已提交
746
EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
L
Linus Torvalds 已提交
747 748

/**
749
 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
L
Linus Torvalds 已提交
750
 *   @skb: received socket buffer
751 752
 *   @protocol: ethernet protocol ID
 *   @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
L
Linus Torvalds 已提交
753 754 755 756
 *
 * Return: 0
 **/

757
static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
758
		       __u8 ipproto,
759 760
		       void (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
						    const struct ipv6hdr *ipv6h,
761
						    struct sk_buff *skb))
L
Linus Torvalds 已提交
762 763
{
	struct ip6_tnl *t;
764
	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
L
Linus Torvalds 已提交
765

766
	rcu_read_lock();
L
Linus Torvalds 已提交
767

768
	if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
769
					&ipv6h->daddr)) != NULL) {
E
Eric Dumazet 已提交
770 771
		struct pcpu_tstats *tstats;

772
		if (t->parms.proto != ipproto && t->parms.proto != 0) {
773
			rcu_read_unlock();
774 775 776
			goto discard;
		}

L
Linus Torvalds 已提交
777
		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
778
			rcu_read_unlock();
779
			goto discard;
L
Linus Torvalds 已提交
780 781
		}

782
		if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
783
			t->dev->stats.rx_dropped++;
784
			rcu_read_unlock();
L
Linus Torvalds 已提交
785 786 787
			goto discard;
		}
		secpath_reset(skb);
788
		skb->mac_header = skb->network_header;
789
		skb_reset_network_header(skb);
790
		skb->protocol = htons(protocol);
L
Linus Torvalds 已提交
791 792
		skb->pkt_type = PACKET_HOST;
		memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
793

E
Eric Dumazet 已提交
794 795 796 797 798
		tstats = this_cpu_ptr(t->dev->tstats);
		tstats->rx_packets++;
		tstats->rx_bytes += skb->len;

		__skb_tunnel_rx(skb, t->dev);
799

800
		dscp_ecn_decapsulate(t, ipv6h, skb);
E
Eric Dumazet 已提交
801

802
		netif_rx(skb);
E
Eric Dumazet 已提交
803

804
		rcu_read_unlock();
L
Linus Torvalds 已提交
805 806
		return 0;
	}
807
	rcu_read_unlock();
L
Linus Torvalds 已提交
808
	return 1;
809 810 811 812

discard:
	kfree_skb(skb);
	return 0;
L
Linus Torvalds 已提交
813 814
}

815 816
static int ip4ip6_rcv(struct sk_buff *skb)
{
817 818
	return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
			   ip4ip6_dscp_ecn_decapsulate);
819 820
}

821 822
static int ip6ip6_rcv(struct sk_buff *skb)
{
823 824
	return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
			   ip6ip6_dscp_ecn_decapsulate);
825 826
}

827 828 829 830
struct ipv6_tel_txoption {
	struct ipv6_txoptions ops;
	__u8 dst_opt[8];
};
L
Linus Torvalds 已提交
831

832 833 834
static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
{
	memset(opt, 0, sizeof(struct ipv6_tel_txoption));
L
Linus Torvalds 已提交
835

836 837 838 839 840
	opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
	opt->dst_opt[3] = 1;
	opt->dst_opt[4] = encap_limit;
	opt->dst_opt[5] = IPV6_TLV_PADN;
	opt->dst_opt[6] = 1;
L
Linus Torvalds 已提交
841

842 843
	opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
	opt->ops.opt_nflen = 8;
L
Linus Torvalds 已提交
844 845 846
}

/**
847
 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
L
Linus Torvalds 已提交
848
 *   @t: the outgoing tunnel device
849
 *   @hdr: IPv6 header from the incoming packet
L
Linus Torvalds 已提交
850 851
 *
 * Description:
852
 *   Avoid trivial tunneling loop by checking that tunnel exit-point
L
Linus Torvalds 已提交
853 854
 *   doesn't match source of incoming packet.
 *
855
 * Return:
L
Linus Torvalds 已提交
856 857 858 859
 *   1 if conflict,
 *   0 else
 **/

E
Eric Dumazet 已提交
860
static inline bool
861
ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
L
Linus Torvalds 已提交
862 863 864 865
{
	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
}

X
xeb@mail.ru 已提交
866
int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
867
{
X
xeb@mail.ru 已提交
868
	struct __ip6_tnl_parm *p = &t->parms;
869
	int ret = 0;
870
	struct net *net = dev_net(t->dev);
871

872
	if (p->flags & IP6_TNL_F_CAP_XMIT) {
873 874
		struct net_device *ldev = NULL;

E
Eric Dumazet 已提交
875
		rcu_read_lock();
876
		if (p->link)
E
Eric Dumazet 已提交
877
			ldev = dev_get_by_index_rcu(net, p->link);
878

879
		if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
880 881
			pr_warn("%s xmit: Local address not yet configured!\n",
				p->name);
882
		else if (!ipv6_addr_is_multicast(&p->raddr) &&
883
			 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
884 885
			pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
				p->name);
886 887
		else
			ret = 1;
E
Eric Dumazet 已提交
888
		rcu_read_unlock();
889 890 891
	}
	return ret;
}
X
xeb@mail.ru 已提交
892 893
EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);

L
Linus Torvalds 已提交
894
/**
895
 * ip6_tnl_xmit2 - encapsulate packet and send
L
Linus Torvalds 已提交
896
 *   @skb: the outgoing socket buffer
897
 *   @dev: the outgoing tunnel device
898 899 900 901
 *   @dsfield: dscp code for outer header
 *   @fl: flow of tunneled packet
 *   @encap_limit: encapsulation limit
 *   @pmtu: Path MTU is stored if packet is too big
L
Linus Torvalds 已提交
902 903 904 905 906
 *
 * Description:
 *   Build new header and do some sanity checks on the packet before sending
 *   it.
 *
907
 * Return:
908
 *   0 on success
909 910
 *   -1 fail
 *   %-EMSGSIZE message too big. return mtu in this case.
L
Linus Torvalds 已提交
911 912
 **/

913 914 915
static int ip6_tnl_xmit2(struct sk_buff *skb,
			 struct net_device *dev,
			 __u8 dsfield,
916
			 struct flowi6 *fl6,
917 918
			 int encap_limit,
			 __u32 *pmtu)
L
Linus Torvalds 已提交
919
{
A
Alexey Dobriyan 已提交
920
	struct net *net = dev_net(dev);
921
	struct ip6_tnl *t = netdev_priv(dev);
922
	struct net_device_stats *stats = &t->dev->stats;
923
	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
924
	struct ipv6_tel_txoption opt;
925
	struct dst_entry *dst = NULL, *ndst = NULL;
L
Linus Torvalds 已提交
926 927
	struct net_device *tdev;
	int mtu;
928
	unsigned int max_headroom = sizeof(struct ipv6hdr);
L
Linus Torvalds 已提交
929
	u8 proto;
930
	int err = -1;
L
Linus Torvalds 已提交
931 932
	int pkt_len;

933 934
	if (!fl6->flowi6_mark)
		dst = ip6_tnl_dst_check(t);
935 936
	if (!dst) {
		ndst = ip6_route_output(net, NULL, fl6);
L
Linus Torvalds 已提交
937

938
		if (ndst->error)
939
			goto tx_err_link_failure;
940 941 942 943
		ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
		if (IS_ERR(ndst)) {
			err = PTR_ERR(ndst);
			ndst = NULL;
944 945
			goto tx_err_link_failure;
		}
946
		dst = ndst;
947
	}
L
Linus Torvalds 已提交
948 949 950 951 952

	tdev = dst->dev;

	if (tdev == dev) {
		stats->collisions++;
953 954
		net_warn_ratelimited("%s: Local routing loop detected!\n",
				     t->parms.name);
L
Linus Torvalds 已提交
955 956 957
		goto tx_err_dst_release;
	}
	mtu = dst_mtu(dst) - sizeof (*ipv6h);
958
	if (encap_limit >= 0) {
L
Linus Torvalds 已提交
959 960 961 962 963
		max_headroom += 8;
		mtu -= 8;
	}
	if (mtu < IPV6_MIN_MTU)
		mtu = IPV6_MIN_MTU;
E
Eric Dumazet 已提交
964
	if (skb_dst(skb))
965
		skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
L
Linus Torvalds 已提交
966
	if (skb->len > mtu) {
967 968
		*pmtu = mtu;
		err = -EMSGSIZE;
L
Linus Torvalds 已提交
969 970 971 972 973 974 975
		goto tx_err_dst_release;
	}

	/*
	 * Okay, now see if we can stuff it in the buffer as-is.
	 */
	max_headroom += LL_RESERVED_SPACE(tdev);
976

977 978
	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
L
Linus Torvalds 已提交
979
		struct sk_buff *new_skb;
980

L
Linus Torvalds 已提交
981 982 983 984 985
		if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
			goto tx_err_dst_release;

		if (skb->sk)
			skb_set_owner_w(new_skb, skb->sk);
986
		consume_skb(skb);
L
Linus Torvalds 已提交
987 988
		skb = new_skb;
	}
E
Eric Dumazet 已提交
989
	skb_dst_drop(skb);
990 991 992 993 994 995
	if (fl6->flowi6_mark) {
		skb_dst_set(skb, dst);
		ndst = NULL;
	} else {
		skb_dst_set_noref(skb, dst);
	}
996
	skb->transport_header = skb->network_header;
L
Linus Torvalds 已提交
997

998
	proto = fl6->flowi6_proto;
999 1000 1001 1002
	if (encap_limit >= 0) {
		init_tel_txopt(&opt, encap_limit);
		ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
	}
1003 1004
	skb_push(skb, sizeof(struct ipv6hdr));
	skb_reset_network_header(skb);
1005
	ipv6h = ipv6_hdr(skb);
1006
	*(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
L
Linus Torvalds 已提交
1007 1008 1009 1010
	dsfield = INET_ECN_encapsulate(0, dsfield);
	ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
	ipv6h->hop_limit = t->parms.hop_limit;
	ipv6h->nexthdr = proto;
A
Alexey Dobriyan 已提交
1011 1012
	ipv6h->saddr = fl6->saddr;
	ipv6h->daddr = fl6->daddr;
L
Linus Torvalds 已提交
1013 1014
	nf_reset(skb);
	pkt_len = skb->len;
H
Herbert Xu 已提交
1015
	err = ip6_local_out(skb);
L
Linus Torvalds 已提交
1016

1017
	if (net_xmit_eval(err) == 0) {
E
Eric Dumazet 已提交
1018 1019 1020 1021
		struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);

		tstats->tx_bytes += pkt_len;
		tstats->tx_packets++;
L
Linus Torvalds 已提交
1022 1023 1024 1025
	} else {
		stats->tx_errors++;
		stats->tx_aborted_errors++;
	}
1026 1027
	if (ndst)
		ip6_tnl_dst_store(t, ndst);
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032
	return 0;
tx_err_link_failure:
	stats->tx_carrier_errors++;
	dst_link_failure(skb);
tx_err_dst_release:
1033
	dst_release(ndst);
1034 1035 1036
	return err;
}

1037 1038 1039 1040
static inline int
ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
1041
	const struct iphdr  *iph = ip_hdr(skb);
1042
	int encap_limit = -1;
1043
	struct flowi6 fl6;
1044 1045 1046 1047
	__u8 dsfield;
	__u32 mtu;
	int err;

1048 1049
	if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
	    !ip6_tnl_xmit_ctl(t))
1050 1051 1052 1053 1054
		return -1;

	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		encap_limit = t->parms.encap_limit;

1055 1056
	memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
	fl6.flowi6_proto = IPPROTO_IPIP;
1057 1058 1059

	dsfield = ipv4_get_dsfield(iph);

1060
	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1061
		fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
A
Al Viro 已提交
1062
					  & IPV6_TCLASS_MASK;
1063 1064
	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
		fl6.flowi6_mark = skb->mark;
1065

1066
	err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
	if (err != 0) {
		/* XXX: send ICMP error even if DF is not set. */
		if (err == -EMSGSIZE)
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
				  htonl(mtu));
		return -1;
	}

	return 0;
}

1078 1079 1080 1081
static inline int
ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
1082
	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1083 1084
	int encap_limit = -1;
	__u16 offset;
1085
	struct flowi6 fl6;
1086 1087 1088 1089
	__u8 dsfield;
	__u32 mtu;
	int err;

1090 1091
	if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
	    !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
1092 1093
		return -1;

X
xeb@mail.ru 已提交
1094
	offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1095
	if (offset > 0) {
1096
		struct ipv6_tlv_tnl_enc_lim *tel;
1097
		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1098 1099
		if (tel->encap_limit == 0) {
			icmpv6_send(skb, ICMPV6_PARAMPROB,
1100
				    ICMPV6_HDR_FIELD, offset + 2);
1101 1102 1103 1104 1105 1106
			return -1;
		}
		encap_limit = tel->encap_limit - 1;
	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		encap_limit = t->parms.encap_limit;

1107 1108
	memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
	fl6.flowi6_proto = IPPROTO_IPV6;
1109 1110

	dsfield = ipv6_get_dsfield(ipv6h);
1111
	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1112
		fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1113
	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1114
		fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1115 1116
	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
		fl6.flowi6_mark = skb->mark;
1117

1118
	err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1119 1120
	if (err != 0) {
		if (err == -EMSGSIZE)
1121
			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1122 1123 1124 1125 1126 1127
		return -1;
	}

	return 0;
}

1128
static netdev_tx_t
1129 1130 1131
ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
1132
	struct net_device_stats *stats = &t->dev->stats;
1133 1134 1135
	int ret;

	switch (skb->protocol) {
1136
	case htons(ETH_P_IP):
1137 1138
		ret = ip4ip6_tnl_xmit(skb, dev);
		break;
1139
	case htons(ETH_P_IPV6):
1140 1141 1142 1143 1144 1145 1146 1147 1148
		ret = ip6ip6_tnl_xmit(skb, dev);
		break;
	default:
		goto tx_err;
	}

	if (ret < 0)
		goto tx_err;

1149
	return NETDEV_TX_OK;
1150

L
Linus Torvalds 已提交
1151 1152 1153 1154
tx_err:
	stats->tx_errors++;
	stats->tx_dropped++;
	kfree_skb(skb);
1155
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1156 1157
}

1158
static void ip6_tnl_link_config(struct ip6_tnl *t)
L
Linus Torvalds 已提交
1159 1160
{
	struct net_device *dev = t->dev;
X
xeb@mail.ru 已提交
1161
	struct __ip6_tnl_parm *p = &t->parms;
1162
	struct flowi6 *fl6 = &t->fl.u.ip6;
L
Linus Torvalds 已提交
1163

1164 1165
	memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
	memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
L
Linus Torvalds 已提交
1166 1167

	/* Set up flowi template */
A
Alexey Dobriyan 已提交
1168 1169
	fl6->saddr = p->laddr;
	fl6->daddr = p->raddr;
1170 1171
	fl6->flowi6_oif = p->link;
	fl6->flowlabel = 0;
L
Linus Torvalds 已提交
1172 1173

	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1174
		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
L
Linus Torvalds 已提交
1175
	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1176
		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
L
Linus Torvalds 已提交
1177

1178 1179
	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188

	if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
		dev->flags |= IFF_POINTOPOINT;
	else
		dev->flags &= ~IFF_POINTOPOINT;

	dev->iflink = p->link;

	if (p->flags & IP6_TNL_F_CAP_XMIT) {
1189 1190 1191
		int strict = (ipv6_addr_type(&p->raddr) &
			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));

1192 1193
		struct rt6_info *rt = rt6_lookup(dev_net(dev),
						 &p->raddr, &p->laddr,
1194
						 p->link, strict);
L
Linus Torvalds 已提交
1195 1196 1197 1198

		if (rt == NULL)
			return;

1199 1200
		if (rt->dst.dev) {
			dev->hard_header_len = rt->dst.dev->hard_header_len +
L
Linus Torvalds 已提交
1201 1202
				sizeof (struct ipv6hdr);

1203
			dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
1204 1205
			if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
				dev->mtu-=8;
L
Linus Torvalds 已提交
1206 1207 1208 1209

			if (dev->mtu < IPV6_MIN_MTU)
				dev->mtu = IPV6_MIN_MTU;
		}
A
Amerigo Wang 已提交
1210
		ip6_rt_put(rt);
L
Linus Torvalds 已提交
1211 1212 1213 1214
	}
}

/**
1215
 * ip6_tnl_change - update the tunnel parameters
L
Linus Torvalds 已提交
1216 1217 1218 1219
 *   @t: tunnel to be changed
 *   @p: tunnel configuration parameters
 *
 * Description:
1220
 *   ip6_tnl_change() updates the tunnel parameters
L
Linus Torvalds 已提交
1221 1222 1223
 **/

static int
X
xeb@mail.ru 已提交
1224
ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
L
Linus Torvalds 已提交
1225
{
A
Alexey Dobriyan 已提交
1226 1227
	t->parms.laddr = p->laddr;
	t->parms.raddr = p->raddr;
L
Linus Torvalds 已提交
1228 1229 1230 1231
	t->parms.flags = p->flags;
	t->parms.hop_limit = p->hop_limit;
	t->parms.encap_limit = p->encap_limit;
	t->parms.flowinfo = p->flowinfo;
1232
	t->parms.link = p->link;
1233
	t->parms.proto = p->proto;
1234
	ip6_tnl_dst_reset(t);
1235
	ip6_tnl_link_config(t);
L
Linus Torvalds 已提交
1236 1237 1238
	return 0;
}

X
xeb@mail.ru 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
static void
ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
{
	p->laddr = u->laddr;
	p->raddr = u->raddr;
	p->flags = u->flags;
	p->hop_limit = u->hop_limit;
	p->encap_limit = u->encap_limit;
	p->flowinfo = u->flowinfo;
	p->link = u->link;
	p->proto = u->proto;
	memcpy(p->name, u->name, sizeof(u->name));
}

static void
ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
{
	u->laddr = p->laddr;
	u->raddr = p->raddr;
	u->flags = p->flags;
	u->hop_limit = p->hop_limit;
	u->encap_limit = p->encap_limit;
	u->flowinfo = p->flowinfo;
	u->link = p->link;
	u->proto = p->proto;
	memcpy(u->name, p->name, sizeof(u->name));
}

L
Linus Torvalds 已提交
1267
/**
1268
 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273
 *   @dev: virtual device associated with tunnel
 *   @ifr: parameters passed from userspace
 *   @cmd: command to be performed
 *
 * Description:
1274
 *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1275
 *   from userspace.
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281 1282
 *
 *   The possible commands are the following:
 *     %SIOCGETTUNNEL: get tunnel parameters for device
 *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
 *     %SIOCCHGTUNNEL: change tunnel parameters to those given
 *     %SIOCDELTUNNEL: delete tunnel
 *
1283
 *   The fallback device "ip6tnl0", created during module
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
 *   initialization, can be used for creating other tunnel devices.
 *
 * Return:
 *   0 on success,
 *   %-EFAULT if unable to copy data to or from userspace,
 *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
 *   %-EINVAL if passed tunnel parameters are invalid,
 *   %-EEXIST if changing a tunnel's parameters would cause a conflict
 *   %-ENODEV if attempting to change or delete a nonexisting device
 **/

static int
1296
ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
L
Linus Torvalds 已提交
1297 1298 1299
{
	int err = 0;
	struct ip6_tnl_parm p;
X
xeb@mail.ru 已提交
1300
	struct __ip6_tnl_parm p1;
L
Linus Torvalds 已提交
1301
	struct ip6_tnl *t = NULL;
1302 1303
	struct net *net = dev_net(dev);
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
L
Linus Torvalds 已提交
1304 1305 1306

	switch (cmd) {
	case SIOCGETTUNNEL:
1307
		if (dev == ip6n->fb_tnl_dev) {
1308
			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
L
Linus Torvalds 已提交
1309 1310 1311
				err = -EFAULT;
				break;
			}
X
xeb@mail.ru 已提交
1312 1313
			ip6_tnl_parm_from_user(&p1, &p);
			t = ip6_tnl_locate(net, &p1, 0);
1314 1315
		} else {
			memset(&p, 0, sizeof(p));
1316 1317
		}
		if (t == NULL)
1318
			t = netdev_priv(dev);
X
xeb@mail.ru 已提交
1319
		ip6_tnl_parm_to_user(&p, &t->parms);
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328
		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
			err = -EFAULT;
		}
		break;
	case SIOCADDTUNNEL:
	case SIOCCHGTUNNEL:
		err = -EPERM;
		if (!capable(CAP_NET_ADMIN))
			break;
1329 1330
		err = -EFAULT;
		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
L
Linus Torvalds 已提交
1331
			break;
1332
		err = -EINVAL;
1333 1334
		if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
		    p.proto != 0)
L
Linus Torvalds 已提交
1335
			break;
X
xeb@mail.ru 已提交
1336 1337
		ip6_tnl_parm_from_user(&p1, &p);
		t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1338
		if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1339 1340 1341 1342 1343 1344 1345 1346
			if (t != NULL) {
				if (t->dev != dev) {
					err = -EEXIST;
					break;
				}
			} else
				t = netdev_priv(dev);

1347
			ip6_tnl_unlink(ip6n, t);
1348
			synchronize_net();
X
xeb@mail.ru 已提交
1349
			err = ip6_tnl_change(t, &p1);
1350
			ip6_tnl_link(ip6n, t);
L
Linus Torvalds 已提交
1351 1352
			netdev_state_change(dev);
		}
1353
		if (t) {
L
Linus Torvalds 已提交
1354
			err = 0;
X
xeb@mail.ru 已提交
1355 1356
			ip6_tnl_parm_to_user(&p, &t->parms);
			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1357 1358 1359 1360
				err = -EFAULT;

		} else
			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365 1366
		break;
	case SIOCDELTUNNEL:
		err = -EPERM;
		if (!capable(CAP_NET_ADMIN))
			break;

1367
		if (dev == ip6n->fb_tnl_dev) {
1368 1369
			err = -EFAULT;
			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
L
Linus Torvalds 已提交
1370
				break;
1371
			err = -ENOENT;
X
xeb@mail.ru 已提交
1372 1373 1374
			ip6_tnl_parm_from_user(&p1, &p);
			t = ip6_tnl_locate(net, &p1, 0);
			if (t == NULL)
L
Linus Torvalds 已提交
1375
				break;
1376
			err = -EPERM;
1377
			if (t->dev == ip6n->fb_tnl_dev)
L
Linus Torvalds 已提交
1378
				break;
1379
			dev = t->dev;
L
Linus Torvalds 已提交
1380
		}
1381 1382
		err = 0;
		unregister_netdevice(dev);
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389 1390
		break;
	default:
		err = -EINVAL;
	}
	return err;
}

/**
1391
 * ip6_tnl_change_mtu - change mtu manually for tunnel device
L
Linus Torvalds 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400
 *   @dev: virtual device associated with tunnel
 *   @new_mtu: the new mtu
 *
 * Return:
 *   0 on success,
 *   %-EINVAL if mtu too small
 **/

static int
1401
ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409
{
	if (new_mtu < IPV6_MIN_MTU) {
		return -EINVAL;
	}
	dev->mtu = new_mtu;
	return 0;
}

1410 1411

static const struct net_device_ops ip6_tnl_netdev_ops = {
E
Eric Dumazet 已提交
1412
	.ndo_uninit	= ip6_tnl_dev_uninit,
1413
	.ndo_start_xmit = ip6_tnl_xmit,
E
Eric Dumazet 已提交
1414
	.ndo_do_ioctl	= ip6_tnl_ioctl,
1415
	.ndo_change_mtu = ip6_tnl_change_mtu,
E
Eric Dumazet 已提交
1416
	.ndo_get_stats	= ip6_get_stats,
1417 1418
};

E
Eric Dumazet 已提交
1419

L
Linus Torvalds 已提交
1420
/**
1421
 * ip6_tnl_dev_setup - setup virtual tunnel device
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426 1427
 *   @dev: virtual device associated with tunnel
 *
 * Description:
 *   Initialize function pointers and device parameters
 **/

1428
static void ip6_tnl_dev_setup(struct net_device *dev)
L
Linus Torvalds 已提交
1429
{
1430 1431
	struct ip6_tnl *t;

1432
	dev->netdev_ops = &ip6_tnl_netdev_ops;
E
Eric Dumazet 已提交
1433
	dev->destructor = ip6_dev_free;
L
Linus Torvalds 已提交
1434 1435 1436 1437

	dev->type = ARPHRD_TUNNEL6;
	dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
	dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1438 1439 1440
	t = netdev_priv(dev);
	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		dev->mtu-=8;
L
Linus Torvalds 已提交
1441 1442
	dev->flags |= IFF_NOARP;
	dev->addr_len = sizeof(struct in6_addr);
1443
	dev->features |= NETIF_F_NETNS_LOCAL;
1444
	dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
L
Linus Torvalds 已提交
1445 1446 1447 1448
}


/**
1449
 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
L
Linus Torvalds 已提交
1450 1451 1452
 *   @dev: virtual device associated with tunnel
 **/

E
Eric Dumazet 已提交
1453
static inline int
1454
ip6_tnl_dev_init_gen(struct net_device *dev)
L
Linus Torvalds 已提交
1455
{
1456
	struct ip6_tnl *t = netdev_priv(dev);
E
Eric Dumazet 已提交
1457

L
Linus Torvalds 已提交
1458
	t->dev = dev;
E
Eric Dumazet 已提交
1459 1460 1461 1462
	dev->tstats = alloc_percpu(struct pcpu_tstats);
	if (!dev->tstats)
		return -ENOMEM;
	return 0;
L
Linus Torvalds 已提交
1463 1464 1465
}

/**
1466
 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
L
Linus Torvalds 已提交
1467 1468 1469
 *   @dev: virtual device associated with tunnel
 **/

E
Eric Dumazet 已提交
1470
static int ip6_tnl_dev_init(struct net_device *dev)
L
Linus Torvalds 已提交
1471
{
1472
	struct ip6_tnl *t = netdev_priv(dev);
E
Eric Dumazet 已提交
1473 1474 1475 1476
	int err = ip6_tnl_dev_init_gen(dev);

	if (err)
		return err;
1477
	ip6_tnl_link_config(t);
E
Eric Dumazet 已提交
1478
	return 0;
L
Linus Torvalds 已提交
1479 1480 1481
}

/**
1482
 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487
 *   @dev: fallback device
 *
 * Return: 0
 **/

E
Eric Dumazet 已提交
1488
static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
L
Linus Torvalds 已提交
1489
{
1490
	struct ip6_tnl *t = netdev_priv(dev);
1491 1492
	struct net *net = dev_net(dev);
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
E
Eric Dumazet 已提交
1493 1494 1495 1496
	int err = ip6_tnl_dev_init_gen(dev);

	if (err)
		return err;
1497

1498
	t->parms.proto = IPPROTO_IPV6;
L
Linus Torvalds 已提交
1499
	dev_hold(dev);
1500 1501 1502

	ip6_tnl_link_config(t);

1503
	rcu_assign_pointer(ip6n->tnls_wc[0], t);
E
Eric Dumazet 已提交
1504
	return 0;
L
Linus Torvalds 已提交
1505 1506
}

1507
static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1508 1509 1510 1511 1512
	.handler	= ip4ip6_rcv,
	.err_handler	= ip4ip6_err,
	.priority	=	1,
};

1513
static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1514 1515
	.handler	= ip6ip6_rcv,
	.err_handler	= ip6ip6_err,
H
Herbert Xu 已提交
1516
	.priority	=	1,
L
Linus Torvalds 已提交
1517 1518
};

1519
static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
1520 1521 1522
{
	int h;
	struct ip6_tnl *t;
1523
	LIST_HEAD(list);
1524 1525

	for (h = 0; h < HASH_SIZE; h++) {
E
Eric Dumazet 已提交
1526
		t = rtnl_dereference(ip6n->tnls_r_l[h]);
1527 1528
		while (t != NULL) {
			unregister_netdevice_queue(t->dev, &list);
E
Eric Dumazet 已提交
1529
			t = rtnl_dereference(t->next);
1530
		}
1531 1532
	}

E
Eric Dumazet 已提交
1533
	t = rtnl_dereference(ip6n->tnls_wc[0]);
1534 1535
	unregister_netdevice_queue(t->dev, &list);
	unregister_netdevice_many(&list);
1536 1537
}

1538
static int __net_init ip6_tnl_init_net(struct net *net)
1539
{
1540
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1541
	struct ip6_tnl *t = NULL;
1542 1543
	int err;

1544 1545 1546
	ip6n->tnls[0] = ip6n->tnls_wc;
	ip6n->tnls[1] = ip6n->tnls_r_l;

1547 1548 1549 1550 1551 1552
	err = -ENOMEM;
	ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
				      ip6_tnl_dev_setup);

	if (!ip6n->fb_tnl_dev)
		goto err_alloc_dev;
1553
	dev_net_set(ip6n->fb_tnl_dev, net);
1554

E
Eric Dumazet 已提交
1555 1556 1557
	err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
	if (err < 0)
		goto err_register;
1558 1559 1560 1561

	err = register_netdev(ip6n->fb_tnl_dev);
	if (err < 0)
		goto err_register;
1562 1563 1564 1565

	t = netdev_priv(ip6n->fb_tnl_dev);

	strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1566 1567
	return 0;

1568
err_register:
E
Eric Dumazet 已提交
1569
	ip6_dev_free(ip6n->fb_tnl_dev);
1570
err_alloc_dev:
1571 1572 1573
	return err;
}

1574
static void __net_exit ip6_tnl_exit_net(struct net *net)
1575
{
1576
	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1577

1578 1579 1580
	rtnl_lock();
	ip6_tnl_destroy_tunnels(ip6n);
	rtnl_unlock();
1581 1582 1583 1584 1585
}

static struct pernet_operations ip6_tnl_net_ops = {
	.init = ip6_tnl_init_net,
	.exit = ip6_tnl_exit_net,
1586 1587
	.id   = &ip6_tnl_net_id,
	.size = sizeof(struct ip6_tnl_net),
1588 1589
};

L
Linus Torvalds 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
/**
 * ip6_tunnel_init - register protocol and reserve needed resources
 *
 * Return: 0 on success
 **/

static int __init ip6_tunnel_init(void)
{
	int  err;

1600 1601 1602 1603 1604 1605
	err = register_pernet_device(&ip6_tnl_net_ops);
	if (err < 0)
		goto out_pernet;

	err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
	if (err < 0) {
1606
		pr_err("%s: can't register ip4ip6\n", __func__);
1607
		goto out_ip4ip6;
1608 1609
	}

1610 1611
	err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
	if (err < 0) {
1612
		pr_err("%s: can't register ip6ip6\n", __func__);
1613
		goto out_ip6ip6;
L
Linus Torvalds 已提交
1614
	}
1615

L
Linus Torvalds 已提交
1616
	return 0;
1617 1618

out_ip6ip6:
1619
	xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1620 1621 1622
out_ip4ip6:
	unregister_pernet_device(&ip6_tnl_net_ops);
out_pernet:
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631
	return err;
}

/**
 * ip6_tunnel_cleanup - free resources and unregister protocol
 **/

static void __exit ip6_tunnel_cleanup(void)
{
1632
	if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1633
		pr_info("%s: can't deregister ip4ip6\n", __func__);
1634

1635
	if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1636
		pr_info("%s: can't deregister ip6ip6\n", __func__);
L
Linus Torvalds 已提交
1637

1638
	unregister_pernet_device(&ip6_tnl_net_ops);
L
Linus Torvalds 已提交
1639 1640 1641 1642
}

module_init(ip6_tunnel_init);
module_exit(ip6_tunnel_cleanup);