ip_tunnel.c 29.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) 2013 Nicira, Inc.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/capability.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/in6.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#include <linux/netfilter_ipv4.h>
#include <linux/etherdevice.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/rculist.h>
S
Sachin Kamat 已提交
29
#include <linux/err.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/protocol.h>
#include <net/ip_tunnels.h>
#include <net/arp.h>
#include <net/checksum.h>
#include <net/dsfield.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/rtnetlink.h>
44
#include <net/udp.h>
45
#include <net/dst_metadata.h>
46

47 48 49 50 51 52
#if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h>
#include <net/ip6_fib.h>
#include <net/ip6_route.h>
#endif

53
static unsigned int ip_tunnel_hash(__be32 key, __be32 remote)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	return hash_32((__force u32)key ^ (__force u32)remote,
			 IP_TNL_HASH_BITS);
}

static bool ip_tunnel_key_match(const struct ip_tunnel_parm *p,
				__be16 flags, __be32 key)
{
	if (p->i_flags & TUNNEL_KEY) {
		if (flags & TUNNEL_KEY)
			return key == p->i_key;
		else
			/* key expected, none present */
			return false;
	} else
		return !(flags & TUNNEL_KEY);
}

/* Fallback tunnel: no source, no destination, no key, no options

   Tunnel hash table:
   We require exact key match i.e. if a key is present in packet
   it will match only tunnel with the same key; if it is not present,
   it will match only keyless tunnel.

   All keysless packets, if not matched configured keyless tunnels
   will match fallback tunnel.
   Given src, dst and key, find appropriate for input tunnel.
*/
struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
				   int link, __be16 flags,
				   __be32 remote, __be32 local,
				   __be32 key)
{
	unsigned int hash;
	struct ip_tunnel *t, *cand = NULL;
	struct hlist_head *head;

92
	hash = ip_tunnel_hash(key, remote);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	head = &itn->tunnels[hash];

	hlist_for_each_entry_rcu(t, head, hash_node) {
		if (local != t->parms.iph.saddr ||
		    remote != t->parms.iph.daddr ||
		    !(t->dev->flags & IFF_UP))
			continue;

		if (!ip_tunnel_key_match(&t->parms, flags, key))
			continue;

		if (t->parms.link == link)
			return t;
		else
			cand = t;
	}

	hlist_for_each_entry_rcu(t, head, hash_node) {
		if (remote != t->parms.iph.daddr ||
D
Dmitry Popov 已提交
112
		    t->parms.iph.saddr != 0 ||
113 114 115 116 117 118 119 120 121 122 123 124
		    !(t->dev->flags & IFF_UP))
			continue;

		if (!ip_tunnel_key_match(&t->parms, flags, key))
			continue;

		if (t->parms.link == link)
			return t;
		else if (!cand)
			cand = t;
	}

125
	hash = ip_tunnel_hash(key, 0);
126 127 128
	head = &itn->tunnels[hash];

	hlist_for_each_entry_rcu(t, head, hash_node) {
D
Dmitry Popov 已提交
129 130 131 132 133
		if ((local != t->parms.iph.saddr || t->parms.iph.daddr != 0) &&
		    (local != t->parms.iph.daddr || !ipv4_is_multicast(local)))
			continue;

		if (!(t->dev->flags & IFF_UP))
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
			continue;

		if (!ip_tunnel_key_match(&t->parms, flags, key))
			continue;

		if (t->parms.link == link)
			return t;
		else if (!cand)
			cand = t;
	}

	if (flags & TUNNEL_NO_KEY)
		goto skip_key_lookup;

	hlist_for_each_entry_rcu(t, head, hash_node) {
		if (t->parms.i_key != key ||
D
Dmitry Popov 已提交
150 151
		    t->parms.iph.saddr != 0 ||
		    t->parms.iph.daddr != 0 ||
152 153 154 155 156 157 158 159 160 161 162 163 164
		    !(t->dev->flags & IFF_UP))
			continue;

		if (t->parms.link == link)
			return t;
		else if (!cand)
			cand = t;
	}

skip_key_lookup:
	if (cand)
		return cand;

165
	t = rcu_dereference(itn->collect_md_tun);
166
	if (t && t->dev->flags & IFF_UP)
167 168
		return t;

169 170 171 172 173 174 175 176 177 178 179 180
	if (itn->fb_tunnel_dev && itn->fb_tunnel_dev->flags & IFF_UP)
		return netdev_priv(itn->fb_tunnel_dev);

	return NULL;
}
EXPORT_SYMBOL_GPL(ip_tunnel_lookup);

static struct hlist_head *ip_bucket(struct ip_tunnel_net *itn,
				    struct ip_tunnel_parm *parms)
{
	unsigned int h;
	__be32 remote;
181
	__be32 i_key = parms->i_key;
182 183 184 185 186 187

	if (parms->iph.daddr && !ipv4_is_multicast(parms->iph.daddr))
		remote = parms->iph.daddr;
	else
		remote = 0;

188 189 190 191
	if (!(parms->i_flags & TUNNEL_KEY) && (parms->i_flags & VTI_ISVTI))
		i_key = 0;

	h = ip_tunnel_hash(i_key, remote);
192 193 194 195 196 197 198
	return &itn->tunnels[h];
}

static void ip_tunnel_add(struct ip_tunnel_net *itn, struct ip_tunnel *t)
{
	struct hlist_head *head = ip_bucket(itn, &t->parms);

199 200
	if (t->collect_md)
		rcu_assign_pointer(itn->collect_md_tun, t);
201 202 203
	hlist_add_head_rcu(&t->hash_node, head);
}

204
static void ip_tunnel_del(struct ip_tunnel_net *itn, struct ip_tunnel *t)
205
{
206 207
	if (t->collect_md)
		rcu_assign_pointer(itn->collect_md_tun, NULL);
208 209 210 211 212 213 214 215 216 217
	hlist_del_init_rcu(&t->hash_node);
}

static struct ip_tunnel *ip_tunnel_find(struct ip_tunnel_net *itn,
					struct ip_tunnel_parm *parms,
					int type)
{
	__be32 remote = parms->iph.daddr;
	__be32 local = parms->iph.saddr;
	__be32 key = parms->i_key;
218
	__be16 flags = parms->i_flags;
219 220 221 222 223 224 225 226
	int link = parms->link;
	struct ip_tunnel *t = NULL;
	struct hlist_head *head = ip_bucket(itn, parms);

	hlist_for_each_entry_rcu(t, head, hash_node) {
		if (local == t->parms.iph.saddr &&
		    remote == t->parms.iph.daddr &&
		    link == t->parms.link &&
227 228
		    type == t->dev->type &&
		    ip_tunnel_key_match(&t->parms, flags, key))
229 230 231 232 233 234 235 236 237 238 239 240 241 242
			break;
	}
	return t;
}

static struct net_device *__ip_tunnel_create(struct net *net,
					     const struct rtnl_link_ops *ops,
					     struct ip_tunnel_parm *parms)
{
	int err;
	struct ip_tunnel *tunnel;
	struct net_device *dev;
	char name[IFNAMSIZ];

243 244 245 246
	err = -E2BIG;
	if (parms->name[0]) {
		if (!dev_valid_name(parms->name))
			goto failed;
247
		strlcpy(name, parms->name, IFNAMSIZ);
248 249
	} else {
		if (strlen(ops->kind) > (IFNAMSIZ - 3))
250
			goto failed;
251 252
		strcpy(name, ops->kind);
		strcat(name, "%d");
253 254 255
	}

	ASSERT_RTNL();
256
	dev = alloc_netdev(ops->priv_size, name, NET_NAME_UNKNOWN, ops->setup);
257 258 259 260 261 262 263 264 265 266
	if (!dev) {
		err = -ENOMEM;
		goto failed;
	}
	dev_net_set(dev, net);

	dev->rtnl_link_ops = ops;

	tunnel = netdev_priv(dev);
	tunnel->parms = *parms;
N
Nicolas Dichtel 已提交
267
	tunnel->net = net;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

	err = register_netdevice(dev);
	if (err)
		goto failed_free;

	return dev;

failed_free:
	free_netdev(dev);
failed:
	return ERR_PTR(err);
}

static int ip_tunnel_bind_dev(struct net_device *dev)
{
	struct net_device *tdev = NULL;
	struct ip_tunnel *tunnel = netdev_priv(dev);
	const struct iphdr *iph;
	int hlen = LL_MAX_HEADER;
	int mtu = ETH_DATA_LEN;
	int t_hlen = tunnel->hlen + sizeof(struct iphdr);

	iph = &tunnel->parms.iph;

	/* Guess output device to choose reasonable mtu and needed_headroom */
	if (iph->daddr) {
		struct flowi4 fl4;
		struct rtable *rt;

297 298 299
		ip_tunnel_init_flow(&fl4, iph->protocol, iph->daddr,
				    iph->saddr, tunnel->parms.o_key,
				    RT_TOS(iph->tos), tunnel->parms.link,
300
				    tunnel->fwmark, 0);
T
Tom Herbert 已提交
301 302
		rt = ip_route_output_key(tunnel->net, &fl4);

303 304 305 306 307 308
		if (!IS_ERR(rt)) {
			tdev = rt->dst.dev;
			ip_rt_put(rt);
		}
		if (dev->type != ARPHRD_ETHER)
			dev->flags |= IFF_POINTOPOINT;
309 310

		dst_cache_reset(&tunnel->dst_cache);
311 312 313
	}

	if (!tdev && tunnel->parms.link)
N
Nicolas Dichtel 已提交
314
		tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
315 316 317

	if (tdev) {
		hlen = tdev->hard_header_len + tdev->needed_headroom;
318
		mtu = min(tdev->mtu, IP_MAX_MTU);
319 320 321 322 323
	}

	dev->needed_headroom = t_hlen + hlen;
	mtu -= (dev->hard_header_len + t_hlen);

324 325
	if (mtu < IPV4_MIN_MTU)
		mtu = IPV4_MIN_MTU;
326 327 328 329 330 331 332 333

	return mtu;
}

static struct ip_tunnel *ip_tunnel_create(struct net *net,
					  struct ip_tunnel_net *itn,
					  struct ip_tunnel_parm *parms)
{
334
	struct ip_tunnel *nt;
335
	struct net_device *dev;
336
	int t_hlen;
337 338
	int mtu;
	int err;
339

340
	dev = __ip_tunnel_create(net, itn->rtnl_link_ops, parms);
341
	if (IS_ERR(dev))
342
		return ERR_CAST(dev);
343

344 345 346 347
	mtu = ip_tunnel_bind_dev(dev);
	err = dev_set_mtu(dev, mtu);
	if (err)
		goto err_dev_set_mtu;
348 349

	nt = netdev_priv(dev);
350 351
	t_hlen = nt->hlen + sizeof(struct iphdr);
	dev->min_mtu = ETH_MIN_MTU;
352
	dev->max_mtu = IP_MAX_MTU - dev->hard_header_len - t_hlen;
353 354
	ip_tunnel_add(itn, nt);
	return nt;
355 356 357 358

err_dev_set_mtu:
	unregister_netdevice(dev);
	return ERR_PTR(err);
359 360 361
}

int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
362 363
		  const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
		  bool log_ecn_error)
364
{
365
	struct pcpu_sw_netstats *tstats;
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	const struct iphdr *iph = ip_hdr(skb);
	int err;

#ifdef CONFIG_NET_IPGRE_BROADCAST
	if (ipv4_is_multicast(iph->daddr)) {
		tunnel->dev->stats.multicast++;
		skb->pkt_type = PACKET_BROADCAST;
	}
#endif

	if ((!(tpi->flags&TUNNEL_CSUM) &&  (tunnel->parms.i_flags&TUNNEL_CSUM)) ||
	     ((tpi->flags&TUNNEL_CSUM) && !(tunnel->parms.i_flags&TUNNEL_CSUM))) {
		tunnel->dev->stats.rx_crc_errors++;
		tunnel->dev->stats.rx_errors++;
		goto drop;
	}

	if (tunnel->parms.i_flags&TUNNEL_SEQ) {
		if (!(tpi->flags&TUNNEL_SEQ) ||
		    (tunnel->i_seqno && (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
			tunnel->dev->stats.rx_fifo_errors++;
			tunnel->dev->stats.rx_errors++;
			goto drop;
		}
		tunnel->i_seqno = ntohl(tpi->seq) + 1;
	}

393 394
	skb_reset_network_header(skb);

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	err = IP_ECN_decapsulate(iph, skb);
	if (unlikely(err)) {
		if (log_ecn_error)
			net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
					&iph->saddr, iph->tos);
		if (err > 1) {
			++tunnel->dev->stats.rx_frame_errors;
			++tunnel->dev->stats.rx_errors;
			goto drop;
		}
	}

	tstats = this_cpu_ptr(tunnel->dev->tstats);
	u64_stats_update_begin(&tstats->syncp);
	tstats->rx_packets++;
	tstats->rx_bytes += skb->len;
	u64_stats_update_end(&tstats->syncp);

413 414
	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));

415 416 417 418 419 420
	if (tunnel->dev->type == ARPHRD_ETHER) {
		skb->protocol = eth_type_trans(skb, tunnel->dev);
		skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
	} else {
		skb->dev = tunnel->dev;
	}
421

422 423 424
	if (tun_dst)
		skb_dst_set(skb, (struct dst_entry *)tun_dst);

425 426 427 428
	gro_cells_receive(&tunnel->gro_cells, skb);
	return 0;

drop:
429 430
	if (tun_dst)
		dst_release((struct dst_entry *)tun_dst);
431 432 433 434 435
	kfree_skb(skb);
	return 0;
}
EXPORT_SYMBOL_GPL(ip_tunnel_rcv);

436 437 438
int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *ops,
			    unsigned int num)
{
439 440 441
	if (num >= MAX_IPTUN_ENCAP_OPS)
		return -ERANGE;

442 443 444
	return !cmpxchg((const struct ip_tunnel_encap_ops **)
			&iptun_encaps[num],
			NULL, ops) ? 0 : -1;
445
}
446 447 448 449 450 451 452
EXPORT_SYMBOL(ip_tunnel_encap_add_ops);

int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *ops,
			    unsigned int num)
{
	int ret;

453 454 455
	if (num >= MAX_IPTUN_ENCAP_OPS)
		return -ERANGE;

456 457 458 459 460 461 462 463 464
	ret = (cmpxchg((const struct ip_tunnel_encap_ops **)
		       &iptun_encaps[num],
		       ops, NULL) == ops) ? 0 : -1;

	synchronize_net();

	return ret;
}
EXPORT_SYMBOL(ip_tunnel_encap_del_ops);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

int ip_tunnel_encap_setup(struct ip_tunnel *t,
			  struct ip_tunnel_encap *ipencap)
{
	int hlen;

	memset(&t->encap, 0, sizeof(t->encap));

	hlen = ip_encap_hlen(ipencap);
	if (hlen < 0)
		return hlen;

	t->encap.type = ipencap->type;
	t->encap.sport = ipencap->sport;
	t->encap.dport = ipencap->dport;
	t->encap.flags = ipencap->flags;

	t->encap_hlen = hlen;
	t->hlen = t->encap_hlen + t->tun_hlen;

	return 0;
}
EXPORT_SYMBOL_GPL(ip_tunnel_encap_setup);

489
static int tnl_update_pmtu(struct net_device *dev, struct sk_buff *skb,
490
			    struct rtable *rt, __be16 df,
491 492
			    const struct iphdr *inner_iph,
			    int tunnel_hlen, __be32 dst, bool md)
493 494
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
495
	int pkt_size;
496 497
	int mtu;

498 499 500
	tunnel_hlen = md ? tunnel_hlen : tunnel->hlen;
	pkt_size = skb->len - tunnel_hlen - dev->hard_header_len;

501 502
	if (df)
		mtu = dst_mtu(&rt->dst) - dev->hard_header_len
503
					- sizeof(struct iphdr) - tunnel_hlen;
504
	else
505
		mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
506

507 508
	if (skb_valid_dst(skb))
		skb_dst_update_pmtu(skb, mtu);
509 510 511

	if (skb->protocol == htons(ETH_P_IP)) {
		if (!skb_is_gso(skb) &&
512 513
		    (inner_iph->frag_off & htons(IP_DF)) &&
		    mtu < pkt_size) {
514 515 516 517 518 519 520
			memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
			return -E2BIG;
		}
	}
#if IS_ENABLED(CONFIG_IPV6)
	else if (skb->protocol == htons(ETH_P_IPV6)) {
521
		struct rt6_info *rt6;
522 523
		__be32 daddr;

524 525
		rt6 = skb_valid_dst(skb) ? (struct rt6_info *)skb_dst(skb) :
					   NULL;
526
		daddr = md ? dst : tunnel->parms.iph.daddr;
527 528 529

		if (rt6 && mtu < dst_mtu(skb_dst(skb)) &&
			   mtu >= IPV6_MIN_MTU) {
530
			if ((daddr && !ipv4_is_multicast(daddr)) ||
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			    rt6->rt6i_dst.plen == 128) {
				rt6->rt6i_flags |= RTF_MODIFIED;
				dst_metric_set(skb_dst(skb), RTAX_MTU, mtu);
			}
		}

		if (!skb_is_gso(skb) && mtu >= IPV6_MIN_MTU &&
					mtu < pkt_size) {
			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
			return -E2BIG;
		}
	}
#endif
	return 0;
}

547 548
void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
		       u8 proto, int tunnel_hlen)
549 550 551 552 553 554
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
	u32 headroom = sizeof(struct iphdr);
	struct ip_tunnel_info *tun_info;
	const struct ip_tunnel_key *key;
	const struct iphdr *inner_iph;
555
	struct rtable *rt = NULL;
556 557 558
	struct flowi4 fl4;
	__be16 df = 0;
	u8 tos, ttl;
559
	bool use_cache;
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

	tun_info = skb_tunnel_info(skb);
	if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
		     ip_tunnel_info_af(tun_info) != AF_INET))
		goto tx_error;
	key = &tun_info->key;
	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
	tos = key->tos;
	if (tos == 1) {
		if (skb->protocol == htons(ETH_P_IP))
			tos = inner_iph->tos;
		else if (skb->protocol == htons(ETH_P_IPV6))
			tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
	}
575 576
	ip_tunnel_init_flow(&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src,
			    tunnel_id_to_key32(key->tun_id), RT_TOS(tos),
577
			    0, skb->mark, skb_get_hash(skb));
578 579
	if (tunnel->encap.type != TUNNEL_ENCAP_NONE)
		goto tx_error;
580 581 582 583 584 585 586 587 588 589 590 591 592

	use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
	if (use_cache)
		rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl4.saddr);
	if (!rt) {
		rt = ip_route_output_key(tunnel->net, &fl4);
		if (IS_ERR(rt)) {
			dev->stats.tx_carrier_errors++;
			goto tx_error;
		}
		if (use_cache)
			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
					  fl4.saddr);
593 594 595 596 597 598
	}
	if (rt->dst.dev == dev) {
		ip_rt_put(rt);
		dev->stats.collisions++;
		goto tx_error;
	}
599 600 601 602 603 604 605 606 607

	if (key->tun_flags & TUNNEL_DONT_FRAGMENT)
		df = htons(IP_DF);
	if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, tunnel_hlen,
			    key->u.ipv4.dst, true)) {
		ip_rt_put(rt);
		goto tx_error;
	}

608 609 610 611 612 613 614 615 616 617
	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
	ttl = key->ttl;
	if (ttl == 0) {
		if (skb->protocol == htons(ETH_P_IP))
			ttl = inner_iph->ttl;
		else if (skb->protocol == htons(ETH_P_IPV6))
			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
		else
			ttl = ip4_dst_hoplimit(&rt->dst);
	}
618 619

	if (!df && skb->protocol == htons(ETH_P_IP))
620
		df = inner_iph->frag_off & htons(IP_DF);
621

622 623 624 625 626 627 628 629
	headroom += LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len;
	if (headroom > dev->needed_headroom)
		dev->needed_headroom = headroom;

	if (skb_cow_head(skb, dev->needed_headroom)) {
		ip_rt_put(rt);
		goto tx_dropped;
	}
630 631
	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, proto, tos, ttl,
		      df, !net_eq(tunnel->net, dev_net(dev)));
632 633 634 635 636 637 638 639 640 641 642
	return;
tx_error:
	dev->stats.tx_errors++;
	goto kfree;
tx_dropped:
	dev->stats.tx_dropped++;
kfree:
	kfree_skb(skb);
}
EXPORT_SYMBOL_GPL(ip_md_tunnel_xmit);

643
void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
644
		    const struct iphdr *tnl_params, u8 protocol)
645 646
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
647
	struct ip_tunnel_info *tun_info = NULL;
648 649
	const struct iphdr *inner_iph;
	unsigned int max_headroom;	/* The extra header space needed */
650 651 652 653
	struct rtable *rt = NULL;		/* Route to the other host */
	bool use_cache = false;
	struct flowi4 fl4;
	bool md = false;
654
	bool connected;
655 656 657
	u8 tos, ttl;
	__be32 dst;
	__be16 df;
658 659

	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
660
	connected = (tunnel->parms.iph.daddr != 0);
661

662 663
	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));

664 665 666 667
	dst = tnl_params->daddr;
	if (dst == 0) {
		/* NBMA tunnel */

668
		if (!skb_dst(skb)) {
669 670 671 672
			dev->stats.tx_fifo_errors++;
			goto tx_error;
		}

673 674 675
		tun_info = skb_tunnel_info(skb);
		if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX) &&
		    ip_tunnel_info_af(tun_info) == AF_INET &&
676
		    tun_info->key.u.ipv4.dst) {
677
			dst = tun_info->key.u.ipv4.dst;
678 679 680
			md = true;
			connected = true;
		}
681
		else if (skb->protocol == htons(ETH_P_IP)) {
682 683 684 685 686 687 688 689 690 691 692 693
			rt = skb_rtable(skb);
			dst = rt_nexthop(rt, inner_iph->daddr);
		}
#if IS_ENABLED(CONFIG_IPV6)
		else if (skb->protocol == htons(ETH_P_IPV6)) {
			const struct in6_addr *addr6;
			struct neighbour *neigh;
			bool do_tx_error_icmp;
			int addr_type;

			neigh = dst_neigh_lookup(skb_dst(skb),
						 &ipv6_hdr(skb)->daddr);
694
			if (!neigh)
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
				goto tx_error;

			addr6 = (const struct in6_addr *)&neigh->primary_key;
			addr_type = ipv6_addr_type(addr6);

			if (addr_type == IPV6_ADDR_ANY) {
				addr6 = &ipv6_hdr(skb)->daddr;
				addr_type = ipv6_addr_type(addr6);
			}

			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
				do_tx_error_icmp = true;
			else {
				do_tx_error_icmp = false;
				dst = addr6->s6_addr32[3];
			}
			neigh_release(neigh);
			if (do_tx_error_icmp)
				goto tx_error_icmp;
		}
#endif
		else
			goto tx_error;
T
Tom Herbert 已提交
718

719 720
		if (!md)
			connected = false;
721 722 723 724 725
	}

	tos = tnl_params->tos;
	if (tos & 0x1) {
		tos &= ~0x1;
T
Tom Herbert 已提交
726
		if (skb->protocol == htons(ETH_P_IP)) {
727
			tos = inner_iph->tos;
T
Tom Herbert 已提交
728 729
			connected = false;
		} else if (skb->protocol == htons(ETH_P_IPV6)) {
730
			tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
T
Tom Herbert 已提交
731 732
			connected = false;
		}
733 734
	}

735 736
	ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr,
			    tunnel->parms.o_key, RT_TOS(tos), tunnel->parms.link,
737
			    tunnel->fwmark, skb_get_hash(skb));
T
Tom Herbert 已提交
738

739 740 741
	if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0)
		goto tx_error;

742 743 744 745 746 747 748 749 750
	if (connected && md) {
		use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
		if (use_cache)
			rt = dst_cache_get_ip4(&tun_info->dst_cache,
					       &fl4.saddr);
	} else {
		rt = connected ? dst_cache_get_ip4(&tunnel->dst_cache,
						&fl4.saddr) : NULL;
	}
T
Tom Herbert 已提交
751 752 753 754 755 756 757 758

	if (!rt) {
		rt = ip_route_output_key(tunnel->net, &fl4);

		if (IS_ERR(rt)) {
			dev->stats.tx_carrier_errors++;
			goto tx_error;
		}
759 760 761 762
		if (use_cache)
			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
					  fl4.saddr);
		else if (!md && connected)
763 764
			dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst,
					  fl4.saddr);
765
	}
T
Tom Herbert 已提交
766

767
	if (rt->dst.dev == dev) {
768 769 770 771 772
		ip_rt_put(rt);
		dev->stats.collisions++;
		goto tx_error;
	}

773 774
	if (tnl_update_pmtu(dev, skb, rt, tnl_params->frag_off, inner_iph,
			    0, 0, false)) {
775 776
		ip_rt_put(rt);
		goto tx_error;
777 778 779 780 781 782 783 784 785 786 787 788
	}

	if (tunnel->err_count > 0) {
		if (time_before(jiffies,
				tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
			tunnel->err_count--;

			dst_link_failure(skb);
		} else
			tunnel->err_count = 0;
	}

789
	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
790 791 792 793 794 795 796 797 798 799 800 801
	ttl = tnl_params->ttl;
	if (ttl == 0) {
		if (skb->protocol == htons(ETH_P_IP))
			ttl = inner_iph->ttl;
#if IS_ENABLED(CONFIG_IPV6)
		else if (skb->protocol == htons(ETH_P_IPV6))
			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
#endif
		else
			ttl = ip4_dst_hoplimit(&rt->dst);
	}

802
	df = tnl_params->frag_off;
803
	if (skb->protocol == htons(ETH_P_IP) && !tunnel->ignore_df)
804 805
		df |= (inner_iph->frag_off&htons(IP_DF));

806
	max_headroom = LL_RESERVED_SPACE(rt->dst.dev) + sizeof(struct iphdr)
807
			+ rt->dst.header_len + ip_encap_hlen(&tunnel->encap);
808
	if (max_headroom > dev->needed_headroom)
809
		dev->needed_headroom = max_headroom;
810 811

	if (skb_cow_head(skb, dev->needed_headroom)) {
812
		ip_rt_put(rt);
813
		dev->stats.tx_dropped++;
E
Eric Dumazet 已提交
814
		kfree_skb(skb);
815
		return;
816 817
	}

818 819
	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
		      df, !net_eq(tunnel->net, dev_net(dev)));
820 821 822 823 824 825 826 827
	return;

#if IS_ENABLED(CONFIG_IPV6)
tx_error_icmp:
	dst_link_failure(skb);
#endif
tx_error:
	dev->stats.tx_errors++;
E
Eric Dumazet 已提交
828
	kfree_skb(skb);
829 830 831 832 833 834 835
}
EXPORT_SYMBOL_GPL(ip_tunnel_xmit);

static void ip_tunnel_update(struct ip_tunnel_net *itn,
			     struct ip_tunnel *t,
			     struct net_device *dev,
			     struct ip_tunnel_parm *p,
836 837
			     bool set_mtu,
			     __u32 fwmark)
838
{
839
	ip_tunnel_del(itn, t);
840 841 842 843 844 845 846 847 848 849 850 851 852 853
	t->parms.iph.saddr = p->iph.saddr;
	t->parms.iph.daddr = p->iph.daddr;
	t->parms.i_key = p->i_key;
	t->parms.o_key = p->o_key;
	if (dev->type != ARPHRD_ETHER) {
		memcpy(dev->dev_addr, &p->iph.saddr, 4);
		memcpy(dev->broadcast, &p->iph.daddr, 4);
	}
	ip_tunnel_add(itn, t);

	t->parms.iph.ttl = p->iph.ttl;
	t->parms.iph.tos = p->iph.tos;
	t->parms.iph.frag_off = p->iph.frag_off;

854
	if (t->parms.link != p->link || t->fwmark != fwmark) {
855 856 857
		int mtu;

		t->parms.link = p->link;
858
		t->fwmark = fwmark;
859 860 861 862
		mtu = ip_tunnel_bind_dev(dev);
		if (set_mtu)
			dev->mtu = mtu;
	}
863
	dst_cache_reset(&t->dst_cache);
864 865 866 867 868 869
	netdev_state_change(dev);
}

int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
{
	int err = 0;
870 871 872
	struct ip_tunnel *t = netdev_priv(dev);
	struct net *net = t->net;
	struct ip_tunnel_net *itn = net_generic(net, t->ip_tnl_net_id);
873 874 875

	switch (cmd) {
	case SIOCGETTUNNEL:
876
		if (dev == itn->fb_tunnel_dev) {
877
			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
878
			if (!t)
879 880
				t = netdev_priv(dev);
		}
881 882 883 884 885 886 887 888 889 890
		memcpy(p, &t->parms, sizeof(*p));
		break;

	case SIOCADDTUNNEL:
	case SIOCCHGTUNNEL:
		err = -EPERM;
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
			goto done;
		if (p->iph.ttl)
			p->iph.frag_off |= htons(IP_DF);
891 892 893 894 895 896
		if (!(p->i_flags & VTI_ISVTI)) {
			if (!(p->i_flags & TUNNEL_KEY))
				p->i_key = 0;
			if (!(p->o_flags & TUNNEL_KEY))
				p->o_key = 0;
		}
897

898
		t = ip_tunnel_find(itn, p, itn->type);
899

900 901 902 903 904 905 906 907
		if (cmd == SIOCADDTUNNEL) {
			if (!t) {
				t = ip_tunnel_create(net, itn, p);
				err = PTR_ERR_OR_ZERO(t);
				break;
			}

			err = -EEXIST;
D
Duan Jiong 已提交
908
			break;
909
		}
910
		if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
911
			if (t) {
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
				if (t->dev != dev) {
					err = -EEXIST;
					break;
				}
			} else {
				unsigned int nflags = 0;

				if (ipv4_is_multicast(p->iph.daddr))
					nflags = IFF_BROADCAST;
				else if (p->iph.daddr)
					nflags = IFF_POINTOPOINT;

				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
					err = -EINVAL;
					break;
				}

				t = netdev_priv(dev);
			}
		}

		if (t) {
			err = 0;
935
			ip_tunnel_update(itn, t, dev, p, true, 0);
936 937 938
		} else {
			err = -ENOENT;
		}
939 940 941 942 943 944 945 946 947 948
		break;

	case SIOCDELTUNNEL:
		err = -EPERM;
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
			goto done;

		if (dev == itn->fb_tunnel_dev) {
			err = -ENOENT;
			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
949
			if (!t)
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
				goto done;
			err = -EPERM;
			if (t == netdev_priv(itn->fb_tunnel_dev))
				goto done;
			dev = t->dev;
		}
		unregister_netdevice(dev);
		err = 0;
		break;

	default:
		err = -EINVAL;
	}

done:
	return err;
}
EXPORT_SYMBOL_GPL(ip_tunnel_ioctl);

969
int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict)
970 971 972
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
973
	int max_mtu = IP_MAX_MTU - dev->hard_header_len - t_hlen;
974

975
	if (new_mtu < ETH_MIN_MTU)
976
		return -EINVAL;
977 978 979 980 981 982 983 984

	if (new_mtu > max_mtu) {
		if (strict)
			return -EINVAL;

		new_mtu = max_mtu;
	}

985 986 987
	dev->mtu = new_mtu;
	return 0;
}
988 989 990 991 992 993
EXPORT_SYMBOL_GPL(__ip_tunnel_change_mtu);

int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
{
	return __ip_tunnel_change_mtu(dev, new_mtu, true);
}
994 995 996 997 998 999 1000
EXPORT_SYMBOL_GPL(ip_tunnel_change_mtu);

static void ip_tunnel_dev_free(struct net_device *dev)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);

	gro_cells_destroy(&tunnel->gro_cells);
1001
	dst_cache_destroy(&tunnel->dst_cache);
1002 1003 1004 1005 1006 1007 1008 1009
	free_percpu(dev->tstats);
}

void ip_tunnel_dellink(struct net_device *dev, struct list_head *head)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
	struct ip_tunnel_net *itn;

N
Nicolas Dichtel 已提交
1010
	itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
1011 1012

	if (itn->fb_tunnel_dev != dev) {
1013
		ip_tunnel_del(itn, netdev_priv(dev));
1014 1015 1016 1017 1018
		unregister_netdevice_queue(dev, head);
	}
}
EXPORT_SYMBOL_GPL(ip_tunnel_dellink);

1019 1020 1021 1022 1023 1024 1025 1026
struct net *ip_tunnel_get_link_net(const struct net_device *dev)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);

	return tunnel->net;
}
EXPORT_SYMBOL(ip_tunnel_get_link_net);

1027 1028 1029 1030 1031 1032 1033 1034
int ip_tunnel_get_iflink(const struct net_device *dev)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);

	return tunnel->parms.link;
}
EXPORT_SYMBOL(ip_tunnel_get_iflink);

1035
int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
1036 1037 1038 1039
				  struct rtnl_link_ops *ops, char *devname)
{
	struct ip_tunnel_net *itn = net_generic(net, ip_tnl_net_id);
	struct ip_tunnel_parm parms;
1040
	unsigned int i;
1041

1042
	itn->rtnl_link_ops = ops;
1043 1044
	for (i = 0; i < IP_TNL_HASH_SIZE; i++)
		INIT_HLIST_HEAD(&itn->tunnels[i]);
1045

1046 1047 1048 1049 1050
	if (!ops || !net_has_fallback_tunnels(net)) {
		struct ip_tunnel_net *it_init_net;

		it_init_net = net_generic(&init_net, ip_tnl_net_id);
		itn->type = it_init_net->type;
1051 1052 1053
		itn->fb_tunnel_dev = NULL;
		return 0;
	}
1054

1055 1056 1057 1058 1059 1060
	memset(&parms, 0, sizeof(parms));
	if (devname)
		strlcpy(parms.name, devname, IFNAMSIZ);

	rtnl_lock();
	itn->fb_tunnel_dev = __ip_tunnel_create(net, ops, &parms);
1061 1062 1063
	/* FB netdevice is special: we have one, and only one per netns.
	 * Allowing to move it to another netns is clearly unsafe.
	 */
1064
	if (!IS_ERR(itn->fb_tunnel_dev)) {
1065
		itn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1066
		itn->fb_tunnel_dev->mtu = ip_tunnel_bind_dev(itn->fb_tunnel_dev);
1067
		ip_tunnel_add(itn, netdev_priv(itn->fb_tunnel_dev));
1068
		itn->type = itn->fb_tunnel_dev->type;
1069
	}
1070
	rtnl_unlock();
1071

S
Sachin Kamat 已提交
1072
	return PTR_ERR_OR_ZERO(itn->fb_tunnel_dev);
1073 1074 1075
}
EXPORT_SYMBOL_GPL(ip_tunnel_init_net);

1076 1077
static void ip_tunnel_destroy(struct net *net, struct ip_tunnel_net *itn,
			      struct list_head *head,
N
Nicolas Dichtel 已提交
1078
			      struct rtnl_link_ops *ops)
1079
{
N
Nicolas Dichtel 已提交
1080
	struct net_device *dev, *aux;
1081 1082
	int h;

N
Nicolas Dichtel 已提交
1083 1084 1085 1086
	for_each_netdev_safe(net, dev, aux)
		if (dev->rtnl_link_ops == ops)
			unregister_netdevice_queue(dev, head);

1087 1088 1089 1090 1091 1092
	for (h = 0; h < IP_TNL_HASH_SIZE; h++) {
		struct ip_tunnel *t;
		struct hlist_node *n;
		struct hlist_head *thead = &itn->tunnels[h];

		hlist_for_each_entry_safe(t, n, thead, hash_node)
N
Nicolas Dichtel 已提交
1093 1094 1095 1096 1097
			/* If dev is in the same netns, it has already
			 * been added to the list by the previous loop.
			 */
			if (!net_eq(dev_net(t->dev), net))
				unregister_netdevice_queue(t->dev, head);
1098 1099 1100
	}
}

1101 1102
void ip_tunnel_delete_nets(struct list_head *net_list, unsigned int id,
			   struct rtnl_link_ops *ops)
1103
{
1104 1105
	struct ip_tunnel_net *itn;
	struct net *net;
1106 1107 1108
	LIST_HEAD(list);

	rtnl_lock();
1109 1110
	list_for_each_entry(net, net_list, exit_list) {
		itn = net_generic(net, id);
1111
		ip_tunnel_destroy(net, itn, &list, ops);
1112
	}
1113 1114 1115
	unregister_netdevice_many(&list);
	rtnl_unlock();
}
1116
EXPORT_SYMBOL_GPL(ip_tunnel_delete_nets);
1117 1118

int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
1119
		      struct ip_tunnel_parm *p, __u32 fwmark)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
{
	struct ip_tunnel *nt;
	struct net *net = dev_net(dev);
	struct ip_tunnel_net *itn;
	int mtu;
	int err;

	nt = netdev_priv(dev);
	itn = net_generic(net, nt->ip_tnl_net_id);

1130 1131 1132 1133 1134 1135 1136
	if (nt->collect_md) {
		if (rtnl_dereference(itn->collect_md_tun))
			return -EEXIST;
	} else {
		if (ip_tunnel_find(itn, p, dev->type))
			return -EEXIST;
	}
1137

N
Nicolas Dichtel 已提交
1138
	nt->net = net;
1139
	nt->parms = *p;
1140
	nt->fwmark = fwmark;
1141 1142
	err = register_netdevice(dev);
	if (err)
1143
		goto err_register_netdevice;
1144 1145 1146 1147 1148

	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
		eth_hw_addr_random(dev);

	mtu = ip_tunnel_bind_dev(dev);
1149
	if (tb[IFLA_MTU]) {
1150
		unsigned int max = IP_MAX_MTU - dev->hard_header_len - nt->hlen;
1151

1152 1153
		mtu = clamp(dev->mtu, (unsigned int)ETH_MIN_MTU,
			    (unsigned int)(max - sizeof(struct iphdr)));
1154
	}
1155

1156 1157 1158
	err = dev_set_mtu(dev, mtu);
	if (err)
		goto err_dev_set_mtu;
1159 1160

	ip_tunnel_add(itn, nt);
1161 1162 1163 1164 1165
	return 0;

err_dev_set_mtu:
	unregister_netdevice(dev);
err_register_netdevice:
1166 1167 1168 1169 1170
	return err;
}
EXPORT_SYMBOL_GPL(ip_tunnel_newlink);

int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
1171
			 struct ip_tunnel_parm *p, __u32 fwmark)
1172
{
N
Nicolas Dichtel 已提交
1173
	struct ip_tunnel *t;
1174
	struct ip_tunnel *tunnel = netdev_priv(dev);
N
Nicolas Dichtel 已提交
1175
	struct net *net = tunnel->net;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	struct ip_tunnel_net *itn = net_generic(net, tunnel->ip_tnl_net_id);

	if (dev == itn->fb_tunnel_dev)
		return -EINVAL;

	t = ip_tunnel_find(itn, p, dev->type);

	if (t) {
		if (t->dev != dev)
			return -EEXIST;
	} else {
N
Nicolas Dichtel 已提交
1187
		t = tunnel;
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

		if (dev->type != ARPHRD_ETHER) {
			unsigned int nflags = 0;

			if (ipv4_is_multicast(p->iph.daddr))
				nflags = IFF_BROADCAST;
			else if (p->iph.daddr)
				nflags = IFF_POINTOPOINT;

			if ((dev->flags ^ nflags) &
			    (IFF_POINTOPOINT | IFF_BROADCAST))
				return -EINVAL;
		}
	}

1203
	ip_tunnel_update(itn, t, dev, p, !tb[IFLA_MTU], fwmark);
1204 1205 1206 1207 1208 1209 1210 1211
	return 0;
}
EXPORT_SYMBOL_GPL(ip_tunnel_changelink);

int ip_tunnel_init(struct net_device *dev)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
	struct iphdr *iph = &tunnel->parms.iph;
1212
	int err;
1213

1214 1215
	dev->needs_free_netdev = true;
	dev->priv_destructor = ip_tunnel_dev_free;
1216
	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1217 1218 1219
	if (!dev->tstats)
		return -ENOMEM;

1220 1221
	err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
	if (err) {
1222
		free_percpu(dev->tstats);
1223
		return err;
1224 1225
	}

1226 1227
	err = gro_cells_init(&tunnel->gro_cells, dev);
	if (err) {
1228
		dst_cache_destroy(&tunnel->dst_cache);
1229 1230 1231 1232 1233
		free_percpu(dev->tstats);
		return err;
	}

	tunnel->dev = dev;
N
Nicolas Dichtel 已提交
1234
	tunnel->net = dev_net(dev);
1235 1236 1237 1238
	strcpy(tunnel->parms.name, dev->name);
	iph->version		= 4;
	iph->ihl		= 5;

1239 1240 1241 1242
	if (tunnel->collect_md) {
		dev->features |= NETIF_F_NETNS_LOCAL;
		netif_keep_dst(dev);
	}
1243 1244 1245 1246 1247 1248 1249
	return 0;
}
EXPORT_SYMBOL_GPL(ip_tunnel_init);

void ip_tunnel_uninit(struct net_device *dev)
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
N
Nicolas Dichtel 已提交
1250
	struct net *net = tunnel->net;
1251 1252 1253 1254 1255
	struct ip_tunnel_net *itn;

	itn = net_generic(net, tunnel->ip_tnl_net_id);
	/* fb_tunnel_dev will be unregisted in net-exit call. */
	if (itn->fb_tunnel_dev != dev)
1256
		ip_tunnel_del(itn, netdev_priv(dev));
T
Tom Herbert 已提交
1257

1258
	dst_cache_reset(&tunnel->dst_cache);
1259 1260 1261 1262
}
EXPORT_SYMBOL_GPL(ip_tunnel_uninit);

/* Do least required initialization, rest of init is done in tunnel_init call */
1263
void ip_tunnel_setup(struct net_device *dev, unsigned int net_id)
1264 1265 1266 1267 1268 1269 1270
{
	struct ip_tunnel *tunnel = netdev_priv(dev);
	tunnel->ip_tnl_net_id = net_id;
}
EXPORT_SYMBOL_GPL(ip_tunnel_setup);

MODULE_LICENSE("GPL");