xref: /linux/net/ipv4/ip_tunnel.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2013 Nicira, Inc.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/capability.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/in.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19 #include <linux/if_arp.h>
20 #include <linux/init.h>
21 #include <linux/in6.h>
22 #include <linux/inetdevice.h>
23 #include <linux/igmp.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/rculist.h>
29 #include <linux/err.h>
30 
31 #include <net/sock.h>
32 #include <net/ip.h>
33 #include <net/icmp.h>
34 #include <net/protocol.h>
35 #include <net/ip_tunnels.h>
36 #include <net/arp.h>
37 #include <net/checksum.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/xfrm.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/netdev_lock.h>
44 #include <net/rtnetlink.h>
45 #include <net/udp.h>
46 #include <net/dst_metadata.h>
47 #include <net/inet_dscp.h>
48 
49 #if IS_ENABLED(CONFIG_IPV6)
50 #include <net/ipv6.h>
51 #include <net/ip6_fib.h>
52 #include <net/ip6_route.h>
53 #endif
54 
55 static unsigned int ip_tunnel_hash(__be32 key, __be32 remote)
56 {
57 	return hash_32((__force u32)key ^ (__force u32)remote,
58 			 IP_TNL_HASH_BITS);
59 }
60 
61 static bool ip_tunnel_key_match(const struct ip_tunnel_parm_kern *p,
62 				const unsigned long *flags, __be32 key)
63 {
64 	if (!test_bit(IP_TUNNEL_KEY_BIT, flags))
65 		return !test_bit(IP_TUNNEL_KEY_BIT, p->i_flags);
66 
67 	return test_bit(IP_TUNNEL_KEY_BIT, p->i_flags) && p->i_key == key;
68 }
69 
70 /* Fallback tunnel: no source, no destination, no key, no options
71 
72    Tunnel hash table:
73    We require exact key match i.e. if a key is present in packet
74    it will match only tunnel with the same key; if it is not present,
75    it will match only keyless tunnel.
76 
77    All keysless packets, if not matched configured keyless tunnels
78    will match fallback tunnel.
79    Given src, dst and key, find appropriate for input tunnel.
80 */
81 struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
82 				   int link, const unsigned long *flags,
83 				   __be32 remote, __be32 local,
84 				   __be32 key)
85 {
86 	struct ip_tunnel *t, *cand = NULL;
87 	struct hlist_head *head;
88 	struct net_device *ndev;
89 	unsigned int hash;
90 
91 	hash = ip_tunnel_hash(key, remote);
92 	head = &itn->tunnels[hash];
93 
94 	hlist_for_each_entry_rcu(t, head, hash_node) {
95 		if (local != t->parms.iph.saddr ||
96 		    remote != t->parms.iph.daddr ||
97 		    !(t->dev->flags & IFF_UP))
98 			continue;
99 
100 		if (!ip_tunnel_key_match(&t->parms, flags, key))
101 			continue;
102 
103 		if (READ_ONCE(t->parms.link) == link)
104 			return t;
105 		cand = t;
106 	}
107 
108 	hlist_for_each_entry_rcu(t, head, hash_node) {
109 		if (remote != t->parms.iph.daddr ||
110 		    t->parms.iph.saddr != 0 ||
111 		    !(t->dev->flags & IFF_UP))
112 			continue;
113 
114 		if (!ip_tunnel_key_match(&t->parms, flags, key))
115 			continue;
116 
117 		if (READ_ONCE(t->parms.link) == link)
118 			return t;
119 		if (!cand)
120 			cand = t;
121 	}
122 
123 	hash = ip_tunnel_hash(key, 0);
124 	head = &itn->tunnels[hash];
125 
126 	hlist_for_each_entry_rcu(t, head, hash_node) {
127 		if ((local != t->parms.iph.saddr || t->parms.iph.daddr != 0) &&
128 		    (local != t->parms.iph.daddr || !ipv4_is_multicast(local)))
129 			continue;
130 
131 		if (!(t->dev->flags & IFF_UP))
132 			continue;
133 
134 		if (!ip_tunnel_key_match(&t->parms, flags, key))
135 			continue;
136 
137 		if (READ_ONCE(t->parms.link) == link)
138 			return t;
139 		if (!cand)
140 			cand = t;
141 	}
142 
143 	hlist_for_each_entry_rcu(t, head, hash_node) {
144 		if ((!test_bit(IP_TUNNEL_NO_KEY_BIT, flags) &&
145 		     t->parms.i_key != key) ||
146 		    t->parms.iph.saddr != 0 ||
147 		    t->parms.iph.daddr != 0 ||
148 		    !(t->dev->flags & IFF_UP))
149 			continue;
150 
151 		if (READ_ONCE(t->parms.link) == link)
152 			return t;
153 		if (!cand)
154 			cand = t;
155 	}
156 
157 	if (cand)
158 		return cand;
159 
160 	t = rcu_dereference(itn->collect_md_tun);
161 	if (t && t->dev->flags & IFF_UP)
162 		return t;
163 
164 	ndev = READ_ONCE(itn->fb_tunnel_dev);
165 	if (ndev && ndev->flags & IFF_UP)
166 		return netdev_priv(ndev);
167 
168 	return NULL;
169 }
170 EXPORT_SYMBOL_GPL(ip_tunnel_lookup);
171 
172 static struct hlist_head *ip_bucket(struct ip_tunnel_net *itn,
173 				    struct ip_tunnel_parm_kern *parms)
174 {
175 	unsigned int h;
176 	__be32 remote;
177 	__be32 i_key = parms->i_key;
178 
179 	if (parms->iph.daddr && !ipv4_is_multicast(parms->iph.daddr))
180 		remote = parms->iph.daddr;
181 	else
182 		remote = 0;
183 
184 	if (!test_bit(IP_TUNNEL_KEY_BIT, parms->i_flags) &&
185 	    test_bit(IP_TUNNEL_VTI_BIT, parms->i_flags))
186 		i_key = 0;
187 
188 	h = ip_tunnel_hash(i_key, remote);
189 	return &itn->tunnels[h];
190 }
191 
192 static void ip_tunnel_add(struct ip_tunnel_net *itn, struct ip_tunnel *t)
193 {
194 	struct hlist_head *head = ip_bucket(itn, &t->parms);
195 
196 	if (t->collect_md)
197 		rcu_assign_pointer(itn->collect_md_tun, t);
198 	hlist_add_head_rcu(&t->hash_node, head);
199 }
200 
201 static void ip_tunnel_del(struct ip_tunnel_net *itn, struct ip_tunnel *t)
202 {
203 	if (t->collect_md)
204 		rcu_assign_pointer(itn->collect_md_tun, NULL);
205 	hlist_del_init_rcu(&t->hash_node);
206 }
207 
208 static bool ip_tunnel_unregistering(struct ip_tunnel *t)
209 {
210 	return hlist_unhashed(&t->hash_node);
211 }
212 
213 static struct ip_tunnel *ip_tunnel_find(struct ip_tunnel_net *itn,
214 					struct ip_tunnel_parm_kern *parms,
215 					int type)
216 {
217 	__be32 remote = parms->iph.daddr;
218 	__be32 local = parms->iph.saddr;
219 	IP_TUNNEL_DECLARE_FLAGS(flags);
220 	__be32 key = parms->i_key;
221 	int link = parms->link;
222 	struct ip_tunnel *t = NULL;
223 	struct hlist_head *head = ip_bucket(itn, parms);
224 
225 	ip_tunnel_flags_copy(flags, parms->i_flags);
226 
227 	hlist_for_each_entry_rcu(t, head, hash_node,
228 				 lockdep_is_held(&itn->tunnels_lock)) {
229 		if (local == t->parms.iph.saddr &&
230 		    remote == t->parms.iph.daddr &&
231 		    link == READ_ONCE(t->parms.link) &&
232 		    type == t->dev->type &&
233 		    ip_tunnel_key_match(&t->parms, flags, key))
234 			break;
235 	}
236 	return t;
237 }
238 
239 static struct net_device *__ip_tunnel_create(struct net *net,
240 					     const struct rtnl_link_ops *ops,
241 					     struct ip_tunnel_parm_kern *parms)
242 {
243 	int err;
244 	struct ip_tunnel *tunnel;
245 	struct net_device *dev;
246 	char name[IFNAMSIZ];
247 
248 	err = -E2BIG;
249 	if (parms->name[0]) {
250 		if (!dev_valid_name(parms->name))
251 			goto failed;
252 		strscpy(name, parms->name);
253 	} else {
254 		if (strlen(ops->kind) > (IFNAMSIZ - 3))
255 			goto failed;
256 		strscpy(name, ops->kind);
257 		strcat(name, "%d");
258 	}
259 
260 	ASSERT_RTNL();
261 	dev = alloc_netdev(ops->priv_size, name, NET_NAME_UNKNOWN, ops->setup);
262 	if (!dev) {
263 		err = -ENOMEM;
264 		goto failed;
265 	}
266 	dev_net_set(dev, net);
267 
268 	dev->rtnl_link_ops = ops;
269 
270 	tunnel = netdev_priv(dev);
271 	tunnel->parms = *parms;
272 	tunnel->net = net;
273 
274 	err = register_netdevice(dev);
275 	if (err)
276 		goto failed_free;
277 
278 	return dev;
279 
280 failed_free:
281 	free_netdev(dev);
282 failed:
283 	return ERR_PTR(err);
284 }
285 
286 static int ip_tunnel_bind_dev(struct net_device *dev)
287 {
288 	struct net_device *tdev = NULL;
289 	struct ip_tunnel *tunnel = netdev_priv(dev);
290 	const struct iphdr *iph;
291 	int hlen = LL_MAX_HEADER;
292 	int mtu = ETH_DATA_LEN;
293 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
294 
295 	iph = &tunnel->parms.iph;
296 
297 	/* Guess output device to choose reasonable mtu and needed_headroom */
298 	if (iph->daddr) {
299 		struct flowi4 fl4;
300 		struct rtable *rt;
301 
302 		ip_tunnel_init_flow(&fl4, iph->protocol, iph->daddr,
303 				    iph->saddr, tunnel->parms.o_key,
304 				    iph->tos & INET_DSCP_MASK, tunnel->net,
305 				    tunnel->parms.link, tunnel->fwmark, 0, 0);
306 		rt = ip_route_output_key(tunnel->net, &fl4);
307 
308 		if (!IS_ERR(rt)) {
309 			tdev = rt->dst.dev;
310 			ip_rt_put(rt);
311 		}
312 		if (dev->type != ARPHRD_ETHER)
313 			dev->flags |= IFF_POINTOPOINT;
314 
315 		dst_cache_reset(&tunnel->dst_cache);
316 	}
317 
318 	if (!tdev && tunnel->parms.link)
319 		tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
320 
321 	if (tdev) {
322 		hlen = tdev->hard_header_len + tdev->needed_headroom;
323 		mtu = min(tdev->mtu, IP_MAX_MTU);
324 	}
325 
326 	dev->needed_headroom = ip_tunnel_limit_headroom(t_hlen + hlen);
327 	mtu -= t_hlen + (dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0);
328 
329 	if (mtu < IPV4_MIN_MTU)
330 		mtu = IPV4_MIN_MTU;
331 
332 	return mtu;
333 }
334 
335 static struct ip_tunnel *ip_tunnel_create(struct net *net,
336 					  struct ip_tunnel_net *itn,
337 					  struct ip_tunnel_parm_kern *parms)
338 {
339 	struct ip_tunnel *nt;
340 	struct net_device *dev;
341 	int t_hlen;
342 	int mtu;
343 	int err;
344 
345 	dev = __ip_tunnel_create(net, itn->rtnl_link_ops, parms);
346 	if (IS_ERR(dev))
347 		return ERR_CAST(dev);
348 
349 	mtu = ip_tunnel_bind_dev(dev);
350 	err = dev_set_mtu(dev, mtu);
351 	if (err)
352 		goto err_dev_set_mtu;
353 
354 	nt = netdev_priv(dev);
355 	t_hlen = nt->hlen + sizeof(struct iphdr);
356 	dev->min_mtu = ETH_MIN_MTU;
357 	dev->max_mtu = IP_MAX_MTU - t_hlen;
358 	if (dev->type == ARPHRD_ETHER)
359 		dev->max_mtu -= dev->hard_header_len;
360 
361 	ip_tunnel_add(itn, nt);
362 	return nt;
363 
364 err_dev_set_mtu:
365 	unregister_netdevice(dev);
366 	return ERR_PTR(err);
367 }
368 
369 void ip_tunnel_md_udp_encap(struct sk_buff *skb, struct ip_tunnel_info *info)
370 {
371 	const struct iphdr *iph = ip_hdr(skb);
372 	const struct udphdr *udph;
373 
374 	if (iph->protocol != IPPROTO_UDP)
375 		return;
376 
377 	udph = (struct udphdr *)((__u8 *)iph + (iph->ihl << 2));
378 	info->encap.sport = udph->source;
379 	info->encap.dport = udph->dest;
380 }
381 EXPORT_SYMBOL(ip_tunnel_md_udp_encap);
382 
383 int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
384 		  const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
385 		  bool log_ecn_error)
386 {
387 	const struct iphdr *iph = ip_hdr(skb);
388 	int nh, err;
389 
390 #ifdef CONFIG_NET_IPGRE_BROADCAST
391 	if (ipv4_is_multicast(iph->daddr)) {
392 		DEV_STATS_INC(tunnel->dev, multicast);
393 		skb->pkt_type = PACKET_BROADCAST;
394 	}
395 #endif
396 
397 	if (test_bit(IP_TUNNEL_CSUM_BIT, tunnel->parms.i_flags) !=
398 	    test_bit(IP_TUNNEL_CSUM_BIT, tpi->flags)) {
399 		DEV_STATS_INC(tunnel->dev, rx_crc_errors);
400 		DEV_STATS_INC(tunnel->dev, rx_errors);
401 		goto drop;
402 	}
403 
404 	if (test_bit(IP_TUNNEL_SEQ_BIT, tunnel->parms.i_flags)) {
405 		if (!test_bit(IP_TUNNEL_SEQ_BIT, tpi->flags) ||
406 		    (tunnel->i_seqno && (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
407 			DEV_STATS_INC(tunnel->dev, rx_fifo_errors);
408 			DEV_STATS_INC(tunnel->dev, rx_errors);
409 			goto drop;
410 		}
411 		tunnel->i_seqno = ntohl(tpi->seq) + 1;
412 	}
413 
414 	/* Save offset of outer header relative to skb->head,
415 	 * because we are going to reset the network header to the inner header
416 	 * and might change skb->head.
417 	 */
418 	nh = skb_network_header(skb) - skb->head;
419 
420 	skb_set_network_header(skb, (tunnel->dev->type == ARPHRD_ETHER) ? ETH_HLEN : 0);
421 
422 	if (!pskb_inet_may_pull(skb)) {
423 		DEV_STATS_INC(tunnel->dev, rx_length_errors);
424 		DEV_STATS_INC(tunnel->dev, rx_errors);
425 		goto drop;
426 	}
427 	iph = (struct iphdr *)(skb->head + nh);
428 
429 	err = IP_ECN_decapsulate(iph, skb);
430 	if (unlikely(err)) {
431 		if (log_ecn_error)
432 			net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
433 					&iph->saddr, iph->tos);
434 		if (err > 1) {
435 			DEV_STATS_INC(tunnel->dev, rx_frame_errors);
436 			DEV_STATS_INC(tunnel->dev, rx_errors);
437 			goto drop;
438 		}
439 	}
440 
441 	dev_sw_netstats_rx_add(tunnel->dev, skb->len);
442 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
443 
444 	if (tunnel->dev->type == ARPHRD_ETHER) {
445 		skb->protocol = eth_type_trans(skb, tunnel->dev);
446 		skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
447 	} else {
448 		skb->dev = tunnel->dev;
449 	}
450 
451 	if (tun_dst)
452 		skb_dst_set(skb, (struct dst_entry *)tun_dst);
453 
454 	gro_cells_receive(&tunnel->gro_cells, skb);
455 	return 0;
456 
457 drop:
458 	if (tun_dst)
459 		dst_release((struct dst_entry *)tun_dst);
460 	kfree_skb(skb);
461 	return 0;
462 }
463 EXPORT_SYMBOL_GPL(ip_tunnel_rcv);
464 
465 int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *ops,
466 			    unsigned int num)
467 {
468 	if (num >= MAX_IPTUN_ENCAP_OPS)
469 		return -ERANGE;
470 
471 	return !cmpxchg((const struct ip_tunnel_encap_ops **)
472 			&iptun_encaps[num],
473 			NULL, ops) ? 0 : -1;
474 }
475 EXPORT_SYMBOL(ip_tunnel_encap_add_ops);
476 
477 int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *ops,
478 			    unsigned int num)
479 {
480 	int ret;
481 
482 	if (num >= MAX_IPTUN_ENCAP_OPS)
483 		return -ERANGE;
484 
485 	ret = (cmpxchg((const struct ip_tunnel_encap_ops **)
486 		       &iptun_encaps[num],
487 		       ops, NULL) == ops) ? 0 : -1;
488 
489 	synchronize_net();
490 
491 	return ret;
492 }
493 EXPORT_SYMBOL(ip_tunnel_encap_del_ops);
494 
495 int ip_tunnel_encap_setup(struct ip_tunnel *t,
496 			  struct ip_tunnel_encap *ipencap)
497 {
498 	int hlen;
499 
500 	hlen = ip_encap_hlen(ipencap);
501 	if (hlen < 0)
502 		return hlen;
503 
504 	WRITE_ONCE(t->encap.type, ipencap->type);
505 	WRITE_ONCE(t->encap.sport, ipencap->sport);
506 	WRITE_ONCE(t->encap.dport, ipencap->dport);
507 	WRITE_ONCE(t->encap.flags, ipencap->flags);
508 
509 	WRITE_ONCE(t->encap_hlen, hlen);
510 	WRITE_ONCE(t->hlen, hlen + t->tun_hlen);
511 
512 	return 0;
513 }
514 EXPORT_SYMBOL_GPL(ip_tunnel_encap_setup);
515 
516 static int tnl_update_pmtu(struct net_device *dev, struct sk_buff *skb,
517 			    struct rtable *rt, __be16 df,
518 			    const struct iphdr *inner_iph,
519 			    int tunnel_hlen, __be32 dst, bool md)
520 {
521 	struct ip_tunnel *tunnel = netdev_priv(dev);
522 	int pkt_size;
523 	int mtu;
524 
525 	tunnel_hlen = md ? tunnel_hlen : tunnel->hlen;
526 	pkt_size = skb->len - tunnel_hlen;
527 	pkt_size -= dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0;
528 
529 	if (df) {
530 		mtu = dst_mtu(&rt->dst) - (sizeof(struct iphdr) + tunnel_hlen);
531 		mtu -= dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0;
532 	} else {
533 		mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
534 	}
535 
536 	if (skb_valid_dst(skb))
537 		skb_dst_update_pmtu_no_confirm(skb, mtu);
538 
539 	if (skb->protocol == htons(ETH_P_IP)) {
540 		if (!skb_is_gso(skb) &&
541 		    (inner_iph->frag_off & htons(IP_DF)) &&
542 		    mtu < pkt_size) {
543 			icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
544 			return -E2BIG;
545 		}
546 	}
547 #if IS_ENABLED(CONFIG_IPV6)
548 	else if (skb->protocol == htons(ETH_P_IPV6)) {
549 		struct rt6_info *rt6;
550 		__be32 daddr;
551 
552 		rt6 = skb_valid_dst(skb) ? dst_rt6_info(skb_dst(skb)) :
553 					   NULL;
554 		daddr = md ? dst : tunnel->parms.iph.daddr;
555 
556 		if (rt6 && mtu < dst_mtu(skb_dst(skb)) &&
557 			   mtu >= IPV6_MIN_MTU) {
558 			if ((daddr && !ipv4_is_multicast(daddr)) ||
559 			    rt6->rt6i_dst.plen == 128) {
560 				rt6->rt6i_flags |= RTF_MODIFIED;
561 				dst_metric_set(skb_dst(skb), RTAX_MTU, mtu);
562 			}
563 		}
564 
565 		if (!skb_is_gso(skb) && mtu >= IPV6_MIN_MTU &&
566 					mtu < pkt_size) {
567 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
568 			return -E2BIG;
569 		}
570 	}
571 #endif
572 	return 0;
573 }
574 
575 void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
576 		       u8 proto, int tunnel_hlen)
577 {
578 	struct ip_tunnel *tunnel = netdev_priv(dev);
579 	u32 headroom = sizeof(struct iphdr);
580 	struct ip_tunnel_info *tun_info;
581 	const struct ip_tunnel_key *key;
582 	const struct iphdr *inner_iph;
583 	struct rtable *rt = NULL;
584 	struct flowi4 fl4;
585 	__be16 df = 0;
586 	u8 tos, ttl;
587 	bool use_cache;
588 
589 	tun_info = skb_tunnel_info(skb);
590 	if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
591 		     ip_tunnel_info_af(tun_info) != AF_INET))
592 		goto tx_error;
593 	key = &tun_info->key;
594 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
595 	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
596 	tos = key->tos;
597 	if (tos == 1) {
598 		if (skb->protocol == htons(ETH_P_IP))
599 			tos = inner_iph->tos;
600 		else if (skb->protocol == htons(ETH_P_IPV6))
601 			tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
602 	}
603 	ip_tunnel_init_flow(&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src,
604 			    tunnel_id_to_key32(key->tun_id),
605 			    tos & INET_DSCP_MASK, tunnel->net, 0, skb->mark,
606 			    skb_get_hash(skb), key->flow_flags);
607 
608 	if (!tunnel_hlen)
609 		tunnel_hlen = ip_encap_hlen(&tun_info->encap);
610 
611 	if (ip_tunnel_encap(skb, &tun_info->encap, &proto, &fl4) < 0)
612 		goto tx_error;
613 	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
614 
615 	use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
616 	if (use_cache)
617 		rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl4.saddr);
618 	if (!rt) {
619 		rt = ip_route_output_key(tunnel->net, &fl4);
620 		if (IS_ERR(rt)) {
621 			DEV_STATS_INC(dev, tx_carrier_errors);
622 			goto tx_error;
623 		}
624 		if (use_cache)
625 			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
626 					  fl4.saddr);
627 	}
628 	if (rt->dst.dev == dev) {
629 		ip_rt_put(rt);
630 		DEV_STATS_INC(dev, collisions);
631 		goto tx_error;
632 	}
633 
634 	if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags))
635 		df = htons(IP_DF);
636 	if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, tunnel_hlen,
637 			    key->u.ipv4.dst, true)) {
638 		ip_rt_put(rt);
639 		goto tx_error;
640 	}
641 
642 	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
643 	ttl = key->ttl;
644 	if (ttl == 0) {
645 		if (skb->protocol == htons(ETH_P_IP))
646 			ttl = inner_iph->ttl;
647 		else if (skb->protocol == htons(ETH_P_IPV6))
648 			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
649 		else
650 			ttl = ip4_dst_hoplimit(&rt->dst);
651 	}
652 
653 	headroom += LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len;
654 	if (skb_cow_head(skb, headroom)) {
655 		ip_rt_put(rt);
656 		goto tx_dropped;
657 	}
658 
659 	ip_tunnel_adj_headroom(dev, headroom);
660 
661 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, proto, tos, ttl,
662 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
663 	return;
664 tx_error:
665 	DEV_STATS_INC(dev, tx_errors);
666 	goto kfree;
667 tx_dropped:
668 	DEV_STATS_INC(dev, tx_dropped);
669 kfree:
670 	kfree_skb(skb);
671 }
672 EXPORT_SYMBOL_GPL(ip_md_tunnel_xmit);
673 
674 void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
675 		    const struct iphdr *tnl_params, u8 protocol)
676 {
677 	struct ip_tunnel *tunnel = netdev_priv(dev);
678 	struct ip_tunnel_info *tun_info = NULL;
679 	const struct iphdr *inner_iph;
680 	unsigned int max_headroom;	/* The extra header space needed */
681 	struct rtable *rt = NULL;		/* Route to the other host */
682 	__be16 payload_protocol;
683 	bool use_cache = false;
684 	struct flowi4 fl4;
685 	bool md = false;
686 	bool connected;
687 	int err_count;
688 	u8 tos, ttl;
689 	__be32 dst;
690 	__be16 df;
691 
692 	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
693 	connected = (tunnel->parms.iph.daddr != 0);
694 	payload_protocol = skb_protocol(skb, true);
695 
696 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
697 
698 	dst = tnl_params->daddr;
699 	if (dst == 0) {
700 		/* NBMA tunnel */
701 
702 		if (!skb_dst(skb)) {
703 			DEV_STATS_INC(dev, tx_fifo_errors);
704 			goto tx_error;
705 		}
706 
707 		tun_info = skb_tunnel_info(skb);
708 		if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX) &&
709 		    ip_tunnel_info_af(tun_info) == AF_INET &&
710 		    tun_info->key.u.ipv4.dst) {
711 			dst = tun_info->key.u.ipv4.dst;
712 			md = true;
713 			connected = true;
714 		} else if (payload_protocol == htons(ETH_P_IP)) {
715 			rt = skb_rtable(skb);
716 			dst = rt_nexthop(rt, inner_iph->daddr);
717 		}
718 #if IS_ENABLED(CONFIG_IPV6)
719 		else if (payload_protocol == htons(ETH_P_IPV6)) {
720 			const struct in6_addr *addr6;
721 			struct neighbour *neigh;
722 			bool do_tx_error_icmp;
723 			int addr_type;
724 
725 			neigh = dst_neigh_lookup(skb_dst(skb),
726 						 &ipv6_hdr(skb)->daddr);
727 			if (!neigh)
728 				goto tx_error;
729 
730 			addr6 = (const struct in6_addr *)&neigh->primary_key;
731 			addr_type = ipv6_addr_type(addr6);
732 
733 			if (addr_type == IPV6_ADDR_ANY) {
734 				addr6 = &ipv6_hdr(skb)->daddr;
735 				addr_type = ipv6_addr_type(addr6);
736 			}
737 
738 			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
739 				do_tx_error_icmp = true;
740 			else {
741 				do_tx_error_icmp = false;
742 				dst = addr6->s6_addr32[3];
743 			}
744 			neigh_release(neigh);
745 			if (do_tx_error_icmp)
746 				goto tx_error_icmp;
747 		}
748 #endif
749 		else
750 			goto tx_error;
751 
752 		if (!md)
753 			connected = false;
754 	}
755 
756 	tos = tnl_params->tos;
757 	df = tnl_params->frag_off;
758 	ttl = tnl_params->ttl;
759 	if (tos & 0x1) {
760 		tos &= ~0x1;
761 		if (payload_protocol == htons(ETH_P_IP)) {
762 			tos = inner_iph->tos;
763 			connected = false;
764 		} else if (payload_protocol == htons(ETH_P_IPV6)) {
765 			tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
766 			connected = false;
767 		}
768 	}
769 
770 	ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr,
771 			    tunnel->parms.o_key, tos & INET_DSCP_MASK,
772 			    tunnel->net, READ_ONCE(tunnel->parms.link),
773 			    tunnel->fwmark, skb_get_hash(skb), 0);
774 
775 	if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0)
776 		goto tx_error;
777 	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
778 
779 	if (connected && md) {
780 		use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
781 		if (use_cache)
782 			rt = dst_cache_get_ip4(&tun_info->dst_cache,
783 					       &fl4.saddr);
784 	} else {
785 		rt = connected ? dst_cache_get_ip4(&tunnel->dst_cache,
786 						&fl4.saddr) : NULL;
787 	}
788 
789 	if (!rt) {
790 		rt = ip_route_output_key(tunnel->net, &fl4);
791 
792 		if (IS_ERR(rt)) {
793 			DEV_STATS_INC(dev, tx_carrier_errors);
794 			goto tx_error;
795 		}
796 		if (use_cache)
797 			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
798 					  fl4.saddr);
799 		else if (!md && connected)
800 			dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst,
801 					  fl4.saddr);
802 	}
803 
804 	if (rt->dst.dev == dev) {
805 		ip_rt_put(rt);
806 		DEV_STATS_INC(dev, collisions);
807 		goto tx_error;
808 	}
809 
810 	if (payload_protocol == htons(ETH_P_IP) && !tunnel->ignore_df)
811 		df |= (inner_iph->frag_off & htons(IP_DF));
812 
813 	if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, 0, 0, false)) {
814 		ip_rt_put(rt);
815 		goto tx_error;
816 	}
817 
818 	err_count = READ_ONCE(tunnel->err_count);
819 	if (err_count > 0) {
820 		if (time_before(jiffies,
821 				READ_ONCE(tunnel->err_time) + IPTUNNEL_ERR_TIMEO)) {
822 			WRITE_ONCE(tunnel->err_count, err_count - 1);
823 
824 			dst_link_failure(skb);
825 		} else {
826 			WRITE_ONCE(tunnel->err_count, 0);
827 		}
828 	}
829 
830 	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
831 	if (ttl == 0) {
832 		if (payload_protocol == htons(ETH_P_IP))
833 			ttl = inner_iph->ttl;
834 #if IS_ENABLED(CONFIG_IPV6)
835 		else if (payload_protocol == htons(ETH_P_IPV6))
836 			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
837 #endif
838 		else
839 			ttl = ip4_dst_hoplimit(&rt->dst);
840 	}
841 
842 	max_headroom = LL_RESERVED_SPACE(rt->dst.dev) + sizeof(struct iphdr)
843 			+ rt->dst.header_len + ip_encap_hlen(&tunnel->encap);
844 
845 	if (skb_cow_head(skb, max_headroom)) {
846 		ip_rt_put(rt);
847 		DEV_STATS_INC(dev, tx_dropped);
848 		kfree_skb(skb);
849 		return;
850 	}
851 
852 	ip_tunnel_adj_headroom(dev, max_headroom);
853 
854 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
855 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
856 	return;
857 
858 #if IS_ENABLED(CONFIG_IPV6)
859 tx_error_icmp:
860 	dst_link_failure(skb);
861 #endif
862 tx_error:
863 	DEV_STATS_INC(dev, tx_errors);
864 	kfree_skb(skb);
865 }
866 EXPORT_SYMBOL_GPL(ip_tunnel_xmit);
867 
868 static void ip_tunnel_update(struct ip_tunnel_net *itn,
869 			     struct ip_tunnel *t,
870 			     struct net_device *dev,
871 			     struct ip_tunnel_parm_kern *p,
872 			     bool set_mtu,
873 			     __u32 fwmark)
874 {
875 	ip_tunnel_del(itn, t);
876 	t->parms.iph.saddr = p->iph.saddr;
877 	t->parms.iph.daddr = p->iph.daddr;
878 	t->parms.i_key = p->i_key;
879 	t->parms.o_key = p->o_key;
880 	if (dev->type != ARPHRD_ETHER) {
881 		__dev_addr_set(dev, &p->iph.saddr, 4);
882 		memcpy(dev->broadcast, &p->iph.daddr, 4);
883 	}
884 	ip_tunnel_add(itn, t);
885 
886 	t->parms.iph.ttl = p->iph.ttl;
887 	t->parms.iph.tos = p->iph.tos;
888 	t->parms.iph.frag_off = p->iph.frag_off;
889 
890 	if (t->parms.link != p->link || t->fwmark != fwmark) {
891 		int mtu;
892 
893 		WRITE_ONCE(t->parms.link, p->link);
894 		t->fwmark = fwmark;
895 		mtu = ip_tunnel_bind_dev(dev);
896 		if (set_mtu)
897 			WRITE_ONCE(dev->mtu, mtu);
898 	}
899 	dst_cache_reset(&t->dst_cache);
900 	netdev_state_change(dev);
901 }
902 
903 static void __ip_tunnel_dellink(struct net *net, struct net_device *dev,
904 				struct list_head *head)
905 {
906 	struct ip_tunnel *tunnel = netdev_priv(dev);
907 	struct ip_tunnel_net *itn;
908 
909 	itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
910 	ip_tunnel_del(itn, tunnel);
911 	unregister_netdevice_queue_net(net, dev, head);
912 }
913 
914 int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p,
915 		  int cmd)
916 {
917 	struct ip_tunnel *t = netdev_priv(dev);
918 	struct net *orig_net = dev_net(dev);
919 	struct ip_tunnel_net *itn;
920 	LIST_HEAD(dev_kill_list);
921 	struct net *net = t->net;
922 	int err = 0;
923 
924 	DEBUG_NET_WARN_ON_ONCE(netdev_need_ops_lock(dev));
925 
926 	itn = net_generic(net, t->ip_tnl_net_id);
927 
928 	mutex_lock(&itn->tunnels_lock);
929 
930 	if (!check_net(net)) {
931 		err = -EBUSY;
932 		goto done;
933 	}
934 
935 	switch (cmd) {
936 	case SIOCGETTUNNEL:
937 		if (dev == itn->fb_tunnel_dev) {
938 			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
939 			if (!t)
940 				t = netdev_priv(dev);
941 		}
942 		memcpy(p, &t->parms, sizeof(*p));
943 		break;
944 
945 	case SIOCADDTUNNEL:
946 	case SIOCCHGTUNNEL:
947 		err = -EPERM;
948 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
949 			goto done;
950 		if (p->iph.ttl)
951 			p->iph.frag_off |= htons(IP_DF);
952 		if (!test_bit(IP_TUNNEL_VTI_BIT, p->i_flags)) {
953 			if (!test_bit(IP_TUNNEL_KEY_BIT, p->i_flags))
954 				p->i_key = 0;
955 			if (!test_bit(IP_TUNNEL_KEY_BIT, p->o_flags))
956 				p->o_key = 0;
957 		}
958 
959 		t = ip_tunnel_find(itn, p, itn->type);
960 
961 		if (cmd == SIOCADDTUNNEL) {
962 			if (!t) {
963 				t = ip_tunnel_create(net, itn, p);
964 				err = PTR_ERR_OR_ZERO(t);
965 				break;
966 			}
967 
968 			err = -EEXIST;
969 			break;
970 		}
971 		if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
972 			if (t) {
973 				if (t->dev != dev) {
974 					err = -EEXIST;
975 					break;
976 				}
977 			} else {
978 				unsigned int nflags = 0;
979 
980 				if (ipv4_is_multicast(p->iph.daddr))
981 					nflags = IFF_BROADCAST;
982 				else if (p->iph.daddr)
983 					nflags = IFF_POINTOPOINT;
984 
985 				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
986 					err = -EINVAL;
987 					break;
988 				}
989 
990 				t = netdev_priv(dev);
991 			}
992 		}
993 
994 		if (t && !ip_tunnel_unregistering(t)) {
995 			err = 0;
996 			ip_tunnel_update(itn, t, dev, p, true, 0);
997 		} else {
998 			err = -ENOENT;
999 		}
1000 		break;
1001 
1002 	case SIOCDELTUNNEL:
1003 		err = -EPERM;
1004 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1005 			goto done;
1006 
1007 		if (dev == itn->fb_tunnel_dev) {
1008 			err = -ENOENT;
1009 			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
1010 			if (!t)
1011 				goto done;
1012 			err = -EPERM;
1013 			if (t == netdev_priv(itn->fb_tunnel_dev))
1014 				goto done;
1015 			dev = t->dev;
1016 		}
1017 
1018 		if (!ip_tunnel_unregistering(t))
1019 			__ip_tunnel_dellink(orig_net, dev, &dev_kill_list);
1020 
1021 		err = 0;
1022 		break;
1023 
1024 	default:
1025 		err = -EINVAL;
1026 	}
1027 
1028 done:
1029 	mutex_unlock(&itn->tunnels_lock);
1030 
1031 	unregister_netdevice_many(&dev_kill_list);
1032 
1033 	return err;
1034 }
1035 EXPORT_SYMBOL_GPL(ip_tunnel_ctl);
1036 
1037 bool ip_tunnel_parm_from_user(struct ip_tunnel_parm_kern *kp,
1038 			      const void __user *data)
1039 {
1040 	struct ip_tunnel_parm p;
1041 
1042 	if (copy_from_user(&p, data, sizeof(p)))
1043 		return false;
1044 
1045 	strscpy(kp->name, p.name);
1046 	kp->link = p.link;
1047 	ip_tunnel_flags_from_be16(kp->i_flags, p.i_flags);
1048 	ip_tunnel_flags_from_be16(kp->o_flags, p.o_flags);
1049 	kp->i_key = p.i_key;
1050 	kp->o_key = p.o_key;
1051 	memcpy(&kp->iph, &p.iph, min(sizeof(kp->iph), sizeof(p.iph)));
1052 
1053 	return true;
1054 }
1055 EXPORT_SYMBOL_GPL(ip_tunnel_parm_from_user);
1056 
1057 bool ip_tunnel_parm_to_user(void __user *data, struct ip_tunnel_parm_kern *kp)
1058 {
1059 	struct ip_tunnel_parm p;
1060 
1061 	if (!ip_tunnel_flags_is_be16_compat(kp->i_flags) ||
1062 	    !ip_tunnel_flags_is_be16_compat(kp->o_flags))
1063 		return false;
1064 
1065 	memset(&p, 0, sizeof(p));
1066 
1067 	strscpy(p.name, kp->name);
1068 	p.link = kp->link;
1069 	p.i_flags = ip_tunnel_flags_to_be16(kp->i_flags);
1070 	p.o_flags = ip_tunnel_flags_to_be16(kp->o_flags);
1071 	p.i_key = kp->i_key;
1072 	p.o_key = kp->o_key;
1073 	memcpy(&p.iph, &kp->iph, min(sizeof(p.iph), sizeof(kp->iph)));
1074 
1075 	return !copy_to_user(data, &p, sizeof(p));
1076 }
1077 EXPORT_SYMBOL_GPL(ip_tunnel_parm_to_user);
1078 
1079 int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
1080 			     void __user *data, int cmd)
1081 {
1082 	struct ip_tunnel_parm_kern p;
1083 	int err;
1084 
1085 	if (!ip_tunnel_parm_from_user(&p, data))
1086 		return -EFAULT;
1087 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, cmd);
1088 	if (!err && !ip_tunnel_parm_to_user(data, &p))
1089 		return -EFAULT;
1090 	return err;
1091 }
1092 EXPORT_SYMBOL_GPL(ip_tunnel_siocdevprivate);
1093 
1094 int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1095 {
1096 	struct ip_tunnel *tunnel = netdev_priv(dev);
1097 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1098 	int max_mtu = IP_MAX_MTU - t_hlen;
1099 
1100 	if (dev->type == ARPHRD_ETHER)
1101 		max_mtu -= dev->hard_header_len;
1102 
1103 	if (new_mtu < ETH_MIN_MTU || new_mtu > max_mtu)
1104 		return -EINVAL;
1105 
1106 	WRITE_ONCE(dev->mtu, new_mtu);
1107 	return 0;
1108 }
1109 EXPORT_SYMBOL_GPL(ip_tunnel_change_mtu);
1110 
1111 static void ip_tunnel_dev_free(struct net_device *dev)
1112 {
1113 	struct ip_tunnel *tunnel = netdev_priv(dev);
1114 
1115 	gro_cells_destroy(&tunnel->gro_cells);
1116 	dst_cache_destroy(&tunnel->dst_cache);
1117 }
1118 
1119 void ip_tunnel_dellink(struct net_device *dev, struct list_head *head)
1120 {
1121 	struct ip_tunnel *tunnel = netdev_priv(dev);
1122 	struct ip_tunnel_net *itn;
1123 
1124 	itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
1125 
1126 	if (itn->fb_tunnel_dev != dev) {
1127 		mutex_lock(&itn->tunnels_lock);
1128 		if (!ip_tunnel_unregistering(tunnel))
1129 			__ip_tunnel_dellink(dev_net(dev), dev, head);
1130 		mutex_unlock(&itn->tunnels_lock);
1131 	}
1132 }
1133 EXPORT_SYMBOL_GPL(ip_tunnel_dellink);
1134 
1135 struct net *ip_tunnel_get_link_net(const struct net_device *dev)
1136 {
1137 	struct ip_tunnel *tunnel = netdev_priv(dev);
1138 
1139 	return READ_ONCE(tunnel->net);
1140 }
1141 EXPORT_SYMBOL(ip_tunnel_get_link_net);
1142 
1143 int ip_tunnel_get_iflink(const struct net_device *dev)
1144 {
1145 	const struct ip_tunnel *tunnel = netdev_priv(dev);
1146 
1147 	return READ_ONCE(tunnel->parms.link);
1148 }
1149 EXPORT_SYMBOL(ip_tunnel_get_iflink);
1150 
1151 int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
1152 				  struct rtnl_link_ops *ops, char *devname)
1153 {
1154 	struct ip_tunnel_net *itn = net_generic(net, ip_tnl_net_id);
1155 	struct ip_tunnel_parm_kern parms;
1156 	unsigned int i;
1157 
1158 	itn->rtnl_link_ops = ops;
1159 	for (i = 0; i < IP_TNL_HASH_SIZE; i++)
1160 		INIT_HLIST_HEAD(&itn->tunnels[i]);
1161 
1162 	mutex_init(&itn->tunnels_lock);
1163 
1164 	if (!ops || !net_has_fallback_tunnels(net)) {
1165 		struct ip_tunnel_net *it_init_net;
1166 
1167 		it_init_net = net_generic(&init_net, ip_tnl_net_id);
1168 		itn->type = it_init_net->type;
1169 		itn->fb_tunnel_dev = NULL;
1170 		return 0;
1171 	}
1172 
1173 	memset(&parms, 0, sizeof(parms));
1174 	if (devname)
1175 		strscpy(parms.name, devname, IFNAMSIZ);
1176 
1177 	rtnl_lock();
1178 	itn->fb_tunnel_dev = __ip_tunnel_create(net, ops, &parms);
1179 	/* FB netdevice is special: we have one, and only one per netns.
1180 	 * Allowing to move it to another netns is clearly unsafe.
1181 	 */
1182 	if (!IS_ERR(itn->fb_tunnel_dev)) {
1183 		itn->fb_tunnel_dev->netns_immutable = true;
1184 		itn->fb_tunnel_dev->mtu = ip_tunnel_bind_dev(itn->fb_tunnel_dev);
1185 		ip_tunnel_add(itn, netdev_priv(itn->fb_tunnel_dev));
1186 		itn->type = itn->fb_tunnel_dev->type;
1187 	}
1188 	rtnl_unlock();
1189 
1190 	return PTR_ERR_OR_ZERO(itn->fb_tunnel_dev);
1191 }
1192 EXPORT_SYMBOL_GPL(ip_tunnel_init_net);
1193 
1194 void ip_tunnel_delete_net(struct net *net, unsigned int id,
1195 			  struct list_head *head)
1196 {
1197 	struct ip_tunnel_net *itn = net_generic(net, id);
1198 	int h;
1199 
1200 	ASSERT_RTNL_NET(net);
1201 
1202 	mutex_lock(&itn->tunnels_lock);
1203 
1204 	WRITE_ONCE(itn->fb_tunnel_dev, NULL);
1205 
1206 	for (h = 0; h < IP_TNL_HASH_SIZE; h++) {
1207 		struct hlist_head *thead = &itn->tunnels[h];
1208 		struct hlist_node *n;
1209 		struct ip_tunnel *t;
1210 
1211 		hlist_for_each_entry_safe(t, n, thead, hash_node)
1212 			__ip_tunnel_dellink(net, t->dev, head);
1213 	}
1214 
1215 	mutex_unlock(&itn->tunnels_lock);
1216 }
1217 EXPORT_SYMBOL_GPL(ip_tunnel_delete_net);
1218 
1219 int ip_tunnel_newlink(struct net *net, struct net_device *dev,
1220 		      struct nlattr *tb[], struct ip_tunnel_parm_kern *p,
1221 		      __u32 fwmark)
1222 {
1223 	struct ip_tunnel_net *itn;
1224 	struct ip_tunnel *nt;
1225 	int err = 0;
1226 	int mtu;
1227 
1228 	nt = netdev_priv(dev);
1229 	itn = net_generic(net, nt->ip_tnl_net_id);
1230 
1231 	mutex_lock(&itn->tunnels_lock);
1232 
1233 	if (nt->collect_md) {
1234 		if (rtnl_dereference(itn->collect_md_tun))
1235 			err = -EEXIST;
1236 	} else {
1237 		if (ip_tunnel_find(itn, p, dev->type))
1238 			err = -EEXIST;
1239 	}
1240 	if (err)
1241 		goto out;
1242 
1243 	nt->net = net;
1244 	nt->parms = *p;
1245 	nt->fwmark = fwmark;
1246 	err = register_netdevice(dev);
1247 	if (err)
1248 		goto err_register_netdevice;
1249 
1250 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1251 		eth_hw_addr_random(dev);
1252 
1253 	mtu = ip_tunnel_bind_dev(dev);
1254 	if (tb[IFLA_MTU]) {
1255 		unsigned int max = IP_MAX_MTU - (nt->hlen + sizeof(struct iphdr));
1256 
1257 		if (dev->type == ARPHRD_ETHER)
1258 			max -= dev->hard_header_len;
1259 
1260 		mtu = clamp(dev->mtu, (unsigned int)ETH_MIN_MTU, max);
1261 	}
1262 
1263 	err = dev_set_mtu(dev, mtu);
1264 	if (err)
1265 		goto err_dev_set_mtu;
1266 
1267 	ip_tunnel_add(itn, nt);
1268 out:
1269 	mutex_unlock(&itn->tunnels_lock);
1270 
1271 	return err;
1272 
1273 err_dev_set_mtu:
1274 	unregister_netdevice(dev);
1275 err_register_netdevice:
1276 	goto out;
1277 }
1278 EXPORT_SYMBOL_GPL(ip_tunnel_newlink);
1279 
1280 int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
1281 			 struct ip_tunnel_parm_kern *p, __u32 fwmark)
1282 {
1283 	struct ip_tunnel *tunnel = netdev_priv(dev);
1284 	struct net *net = tunnel->net;
1285 	struct ip_tunnel_net *itn;
1286 	struct ip_tunnel *t;
1287 	int err = 0;
1288 
1289 	itn = net_generic(net, tunnel->ip_tnl_net_id);
1290 
1291 	if (dev == itn->fb_tunnel_dev)
1292 		return -EINVAL;
1293 
1294 	mutex_lock(&itn->tunnels_lock);
1295 
1296 	t = ip_tunnel_find(itn, p, dev->type);
1297 
1298 	if (t) {
1299 		if (t->dev != dev) {
1300 			err = -EEXIST;
1301 			goto out;
1302 		}
1303 	} else {
1304 		t = tunnel;
1305 
1306 		if (dev->type != ARPHRD_ETHER) {
1307 			unsigned int nflags = 0;
1308 
1309 			if (ipv4_is_multicast(p->iph.daddr))
1310 				nflags = IFF_BROADCAST;
1311 			else if (p->iph.daddr)
1312 				nflags = IFF_POINTOPOINT;
1313 
1314 			if ((dev->flags ^ nflags) &
1315 			    (IFF_POINTOPOINT | IFF_BROADCAST)) {
1316 				err = -EINVAL;
1317 				goto out;
1318 			}
1319 		}
1320 	}
1321 
1322 	if (ip_tunnel_unregistering(t)) {
1323 		err = -ENODEV;
1324 		goto out;
1325 	}
1326 
1327 	ip_tunnel_update(itn, t, dev, p, !tb[IFLA_MTU], fwmark);
1328 out:
1329 	mutex_unlock(&itn->tunnels_lock);
1330 
1331 	return err;
1332 }
1333 EXPORT_SYMBOL_GPL(ip_tunnel_changelink);
1334 
1335 int __ip_tunnel_init(struct net_device *dev)
1336 {
1337 	struct ip_tunnel *tunnel = netdev_priv(dev);
1338 	struct iphdr *iph = &tunnel->parms.iph;
1339 	int err;
1340 
1341 	dev->needs_free_netdev = true;
1342 	dev->priv_destructor = ip_tunnel_dev_free;
1343 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1344 
1345 	err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1346 	if (err)
1347 		return err;
1348 
1349 	err = gro_cells_init(&tunnel->gro_cells, dev);
1350 	if (err) {
1351 		dst_cache_destroy(&tunnel->dst_cache);
1352 		return err;
1353 	}
1354 
1355 	tunnel->dev = dev;
1356 	strscpy(tunnel->parms.name, dev->name);
1357 	iph->version		= 4;
1358 	iph->ihl		= 5;
1359 
1360 	if (tunnel->collect_md)
1361 		netif_keep_dst(dev);
1362 	return 0;
1363 }
1364 EXPORT_SYMBOL_GPL(__ip_tunnel_init);
1365 
1366 void ip_tunnel_uninit(struct net_device *dev)
1367 {
1368 	struct ip_tunnel *tunnel = netdev_priv(dev);
1369 
1370 	dst_cache_reset(&tunnel->dst_cache);
1371 }
1372 EXPORT_SYMBOL_GPL(ip_tunnel_uninit);
1373 
1374 /* Do least required initialization, rest of init is done in tunnel_init call */
1375 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id)
1376 {
1377 	struct ip_tunnel *tunnel = netdev_priv(dev);
1378 	tunnel->ip_tnl_net_id = net_id;
1379 }
1380 EXPORT_SYMBOL_GPL(ip_tunnel_setup);
1381 
1382 MODULE_DESCRIPTION("IPv4 tunnel implementation library");
1383 MODULE_LICENSE("GPL");
1384