xref: /linux/net/ipv4/ip_tunnel.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
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 
614 	use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
615 	if (use_cache)
616 		rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl4.saddr);
617 	if (!rt) {
618 		rt = ip_route_output_key(tunnel->net, &fl4);
619 		if (IS_ERR(rt)) {
620 			DEV_STATS_INC(dev, tx_carrier_errors);
621 			goto tx_error;
622 		}
623 		if (use_cache)
624 			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
625 					  fl4.saddr);
626 	}
627 	if (rt->dst.dev == dev) {
628 		ip_rt_put(rt);
629 		DEV_STATS_INC(dev, collisions);
630 		goto tx_error;
631 	}
632 
633 	if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags))
634 		df = htons(IP_DF);
635 	if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, tunnel_hlen,
636 			    key->u.ipv4.dst, true)) {
637 		ip_rt_put(rt);
638 		goto tx_error;
639 	}
640 
641 	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
642 	ttl = key->ttl;
643 	if (ttl == 0) {
644 		if (skb->protocol == htons(ETH_P_IP))
645 			ttl = inner_iph->ttl;
646 		else if (skb->protocol == htons(ETH_P_IPV6))
647 			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
648 		else
649 			ttl = ip4_dst_hoplimit(&rt->dst);
650 	}
651 
652 	headroom += LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len;
653 	if (skb_cow_head(skb, headroom)) {
654 		ip_rt_put(rt);
655 		goto tx_dropped;
656 	}
657 
658 	ip_tunnel_adj_headroom(dev, headroom);
659 
660 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, proto, tos, ttl,
661 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
662 	return;
663 tx_error:
664 	DEV_STATS_INC(dev, tx_errors);
665 	goto kfree;
666 tx_dropped:
667 	DEV_STATS_INC(dev, tx_dropped);
668 kfree:
669 	kfree_skb(skb);
670 }
671 EXPORT_SYMBOL_GPL(ip_md_tunnel_xmit);
672 
673 void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
674 		    const struct iphdr *tnl_params, u8 protocol)
675 {
676 	struct ip_tunnel *tunnel = netdev_priv(dev);
677 	struct ip_tunnel_info *tun_info = NULL;
678 	const struct iphdr *inner_iph;
679 	unsigned int max_headroom;	/* The extra header space needed */
680 	struct rtable *rt = NULL;		/* Route to the other host */
681 	__be16 payload_protocol;
682 	bool use_cache = false;
683 	struct flowi4 fl4;
684 	bool md = false;
685 	bool connected;
686 	int err_count;
687 	u8 tos, ttl;
688 	__be32 dst;
689 	__be16 df;
690 
691 	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
692 	connected = (tunnel->parms.iph.daddr != 0);
693 	payload_protocol = skb_protocol(skb, true);
694 
695 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
696 
697 	dst = tnl_params->daddr;
698 	if (dst == 0) {
699 		/* NBMA tunnel */
700 
701 		if (!skb_dst(skb)) {
702 			DEV_STATS_INC(dev, tx_fifo_errors);
703 			goto tx_error;
704 		}
705 
706 		tun_info = skb_tunnel_info(skb);
707 		if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX) &&
708 		    ip_tunnel_info_af(tun_info) == AF_INET &&
709 		    tun_info->key.u.ipv4.dst) {
710 			dst = tun_info->key.u.ipv4.dst;
711 			md = true;
712 			connected = true;
713 		} else if (payload_protocol == htons(ETH_P_IP)) {
714 			rt = skb_rtable(skb);
715 			dst = rt_nexthop(rt, inner_iph->daddr);
716 		}
717 #if IS_ENABLED(CONFIG_IPV6)
718 		else if (payload_protocol == htons(ETH_P_IPV6)) {
719 			const struct in6_addr *addr6;
720 			struct neighbour *neigh;
721 			bool do_tx_error_icmp;
722 			int addr_type;
723 
724 			neigh = dst_neigh_lookup(skb_dst(skb),
725 						 &ipv6_hdr(skb)->daddr);
726 			if (!neigh)
727 				goto tx_error;
728 
729 			addr6 = (const struct in6_addr *)&neigh->primary_key;
730 			addr_type = ipv6_addr_type(addr6);
731 
732 			if (addr_type == IPV6_ADDR_ANY) {
733 				addr6 = &ipv6_hdr(skb)->daddr;
734 				addr_type = ipv6_addr_type(addr6);
735 			}
736 
737 			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
738 				do_tx_error_icmp = true;
739 			else {
740 				do_tx_error_icmp = false;
741 				dst = addr6->s6_addr32[3];
742 			}
743 			neigh_release(neigh);
744 			if (do_tx_error_icmp)
745 				goto tx_error_icmp;
746 		}
747 #endif
748 		else
749 			goto tx_error;
750 
751 		if (!md)
752 			connected = false;
753 	}
754 
755 	tos = tnl_params->tos;
756 	if (tos & 0x1) {
757 		tos &= ~0x1;
758 		if (payload_protocol == htons(ETH_P_IP)) {
759 			tos = inner_iph->tos;
760 			connected = false;
761 		} else if (payload_protocol == htons(ETH_P_IPV6)) {
762 			tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
763 			connected = false;
764 		}
765 	}
766 
767 	ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr,
768 			    tunnel->parms.o_key, tos & INET_DSCP_MASK,
769 			    tunnel->net, READ_ONCE(tunnel->parms.link),
770 			    tunnel->fwmark, skb_get_hash(skb), 0);
771 
772 	if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0)
773 		goto tx_error;
774 
775 	if (connected && md) {
776 		use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
777 		if (use_cache)
778 			rt = dst_cache_get_ip4(&tun_info->dst_cache,
779 					       &fl4.saddr);
780 	} else {
781 		rt = connected ? dst_cache_get_ip4(&tunnel->dst_cache,
782 						&fl4.saddr) : NULL;
783 	}
784 
785 	if (!rt) {
786 		rt = ip_route_output_key(tunnel->net, &fl4);
787 
788 		if (IS_ERR(rt)) {
789 			DEV_STATS_INC(dev, tx_carrier_errors);
790 			goto tx_error;
791 		}
792 		if (use_cache)
793 			dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
794 					  fl4.saddr);
795 		else if (!md && connected)
796 			dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst,
797 					  fl4.saddr);
798 	}
799 
800 	if (rt->dst.dev == dev) {
801 		ip_rt_put(rt);
802 		DEV_STATS_INC(dev, collisions);
803 		goto tx_error;
804 	}
805 
806 	df = tnl_params->frag_off;
807 	if (payload_protocol == htons(ETH_P_IP) && !tunnel->ignore_df)
808 		df |= (inner_iph->frag_off & htons(IP_DF));
809 
810 	if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, 0, 0, false)) {
811 		ip_rt_put(rt);
812 		goto tx_error;
813 	}
814 
815 	err_count = READ_ONCE(tunnel->err_count);
816 	if (err_count > 0) {
817 		if (time_before(jiffies,
818 				READ_ONCE(tunnel->err_time) + IPTUNNEL_ERR_TIMEO)) {
819 			WRITE_ONCE(tunnel->err_count, err_count - 1);
820 
821 			dst_link_failure(skb);
822 		} else {
823 			WRITE_ONCE(tunnel->err_count, 0);
824 		}
825 	}
826 
827 	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
828 	ttl = tnl_params->ttl;
829 	if (ttl == 0) {
830 		if (payload_protocol == htons(ETH_P_IP))
831 			ttl = inner_iph->ttl;
832 #if IS_ENABLED(CONFIG_IPV6)
833 		else if (payload_protocol == htons(ETH_P_IPV6))
834 			ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
835 #endif
836 		else
837 			ttl = ip4_dst_hoplimit(&rt->dst);
838 	}
839 
840 	max_headroom = LL_RESERVED_SPACE(rt->dst.dev) + sizeof(struct iphdr)
841 			+ rt->dst.header_len + ip_encap_hlen(&tunnel->encap);
842 
843 	if (skb_cow_head(skb, max_headroom)) {
844 		ip_rt_put(rt);
845 		DEV_STATS_INC(dev, tx_dropped);
846 		kfree_skb(skb);
847 		return;
848 	}
849 
850 	ip_tunnel_adj_headroom(dev, max_headroom);
851 
852 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
853 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
854 	return;
855 
856 #if IS_ENABLED(CONFIG_IPV6)
857 tx_error_icmp:
858 	dst_link_failure(skb);
859 #endif
860 tx_error:
861 	DEV_STATS_INC(dev, tx_errors);
862 	kfree_skb(skb);
863 }
864 EXPORT_SYMBOL_GPL(ip_tunnel_xmit);
865 
866 static void ip_tunnel_update(struct ip_tunnel_net *itn,
867 			     struct ip_tunnel *t,
868 			     struct net_device *dev,
869 			     struct ip_tunnel_parm_kern *p,
870 			     bool set_mtu,
871 			     __u32 fwmark)
872 {
873 	ip_tunnel_del(itn, t);
874 	t->parms.iph.saddr = p->iph.saddr;
875 	t->parms.iph.daddr = p->iph.daddr;
876 	t->parms.i_key = p->i_key;
877 	t->parms.o_key = p->o_key;
878 	if (dev->type != ARPHRD_ETHER) {
879 		__dev_addr_set(dev, &p->iph.saddr, 4);
880 		memcpy(dev->broadcast, &p->iph.daddr, 4);
881 	}
882 	ip_tunnel_add(itn, t);
883 
884 	t->parms.iph.ttl = p->iph.ttl;
885 	t->parms.iph.tos = p->iph.tos;
886 	t->parms.iph.frag_off = p->iph.frag_off;
887 
888 	if (t->parms.link != p->link || t->fwmark != fwmark) {
889 		int mtu;
890 
891 		WRITE_ONCE(t->parms.link, p->link);
892 		t->fwmark = fwmark;
893 		mtu = ip_tunnel_bind_dev(dev);
894 		if (set_mtu)
895 			WRITE_ONCE(dev->mtu, mtu);
896 	}
897 	dst_cache_reset(&t->dst_cache);
898 	netdev_state_change(dev);
899 }
900 
901 static void __ip_tunnel_dellink(struct net *net, struct net_device *dev,
902 				struct list_head *head)
903 {
904 	struct ip_tunnel *tunnel = netdev_priv(dev);
905 	struct ip_tunnel_net *itn;
906 
907 	itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
908 	ip_tunnel_del(itn, tunnel);
909 	unregister_netdevice_queue_net(net, dev, head);
910 }
911 
912 int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p,
913 		  int cmd)
914 {
915 	struct ip_tunnel *t = netdev_priv(dev);
916 	struct net *orig_net = dev_net(dev);
917 	struct ip_tunnel_net *itn;
918 	LIST_HEAD(dev_kill_list);
919 	struct net *net = t->net;
920 	int err = 0;
921 
922 	DEBUG_NET_WARN_ON_ONCE(netdev_need_ops_lock(dev));
923 
924 	itn = net_generic(net, t->ip_tnl_net_id);
925 
926 	mutex_lock(&itn->tunnels_lock);
927 
928 	if (!check_net(net)) {
929 		err = -EBUSY;
930 		goto done;
931 	}
932 
933 	switch (cmd) {
934 	case SIOCGETTUNNEL:
935 		if (dev == itn->fb_tunnel_dev) {
936 			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
937 			if (!t)
938 				t = netdev_priv(dev);
939 		}
940 		memcpy(p, &t->parms, sizeof(*p));
941 		break;
942 
943 	case SIOCADDTUNNEL:
944 	case SIOCCHGTUNNEL:
945 		err = -EPERM;
946 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
947 			goto done;
948 		if (p->iph.ttl)
949 			p->iph.frag_off |= htons(IP_DF);
950 		if (!test_bit(IP_TUNNEL_VTI_BIT, p->i_flags)) {
951 			if (!test_bit(IP_TUNNEL_KEY_BIT, p->i_flags))
952 				p->i_key = 0;
953 			if (!test_bit(IP_TUNNEL_KEY_BIT, p->o_flags))
954 				p->o_key = 0;
955 		}
956 
957 		t = ip_tunnel_find(itn, p, itn->type);
958 
959 		if (cmd == SIOCADDTUNNEL) {
960 			if (!t) {
961 				t = ip_tunnel_create(net, itn, p);
962 				err = PTR_ERR_OR_ZERO(t);
963 				break;
964 			}
965 
966 			err = -EEXIST;
967 			break;
968 		}
969 		if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
970 			if (t) {
971 				if (t->dev != dev) {
972 					err = -EEXIST;
973 					break;
974 				}
975 			} else {
976 				unsigned int nflags = 0;
977 
978 				if (ipv4_is_multicast(p->iph.daddr))
979 					nflags = IFF_BROADCAST;
980 				else if (p->iph.daddr)
981 					nflags = IFF_POINTOPOINT;
982 
983 				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
984 					err = -EINVAL;
985 					break;
986 				}
987 
988 				t = netdev_priv(dev);
989 			}
990 		}
991 
992 		if (t && !ip_tunnel_unregistering(t)) {
993 			err = 0;
994 			ip_tunnel_update(itn, t, dev, p, true, 0);
995 		} else {
996 			err = -ENOENT;
997 		}
998 		break;
999 
1000 	case SIOCDELTUNNEL:
1001 		err = -EPERM;
1002 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1003 			goto done;
1004 
1005 		if (dev == itn->fb_tunnel_dev) {
1006 			err = -ENOENT;
1007 			t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
1008 			if (!t)
1009 				goto done;
1010 			err = -EPERM;
1011 			if (t == netdev_priv(itn->fb_tunnel_dev))
1012 				goto done;
1013 			dev = t->dev;
1014 		}
1015 
1016 		if (!ip_tunnel_unregistering(t))
1017 			__ip_tunnel_dellink(orig_net, dev, &dev_kill_list);
1018 
1019 		err = 0;
1020 		break;
1021 
1022 	default:
1023 		err = -EINVAL;
1024 	}
1025 
1026 done:
1027 	mutex_unlock(&itn->tunnels_lock);
1028 
1029 	unregister_netdevice_many(&dev_kill_list);
1030 
1031 	return err;
1032 }
1033 EXPORT_SYMBOL_GPL(ip_tunnel_ctl);
1034 
1035 bool ip_tunnel_parm_from_user(struct ip_tunnel_parm_kern *kp,
1036 			      const void __user *data)
1037 {
1038 	struct ip_tunnel_parm p;
1039 
1040 	if (copy_from_user(&p, data, sizeof(p)))
1041 		return false;
1042 
1043 	strscpy(kp->name, p.name);
1044 	kp->link = p.link;
1045 	ip_tunnel_flags_from_be16(kp->i_flags, p.i_flags);
1046 	ip_tunnel_flags_from_be16(kp->o_flags, p.o_flags);
1047 	kp->i_key = p.i_key;
1048 	kp->o_key = p.o_key;
1049 	memcpy(&kp->iph, &p.iph, min(sizeof(kp->iph), sizeof(p.iph)));
1050 
1051 	return true;
1052 }
1053 EXPORT_SYMBOL_GPL(ip_tunnel_parm_from_user);
1054 
1055 bool ip_tunnel_parm_to_user(void __user *data, struct ip_tunnel_parm_kern *kp)
1056 {
1057 	struct ip_tunnel_parm p;
1058 
1059 	if (!ip_tunnel_flags_is_be16_compat(kp->i_flags) ||
1060 	    !ip_tunnel_flags_is_be16_compat(kp->o_flags))
1061 		return false;
1062 
1063 	memset(&p, 0, sizeof(p));
1064 
1065 	strscpy(p.name, kp->name);
1066 	p.link = kp->link;
1067 	p.i_flags = ip_tunnel_flags_to_be16(kp->i_flags);
1068 	p.o_flags = ip_tunnel_flags_to_be16(kp->o_flags);
1069 	p.i_key = kp->i_key;
1070 	p.o_key = kp->o_key;
1071 	memcpy(&p.iph, &kp->iph, min(sizeof(p.iph), sizeof(kp->iph)));
1072 
1073 	return !copy_to_user(data, &p, sizeof(p));
1074 }
1075 EXPORT_SYMBOL_GPL(ip_tunnel_parm_to_user);
1076 
1077 int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
1078 			     void __user *data, int cmd)
1079 {
1080 	struct ip_tunnel_parm_kern p;
1081 	int err;
1082 
1083 	if (!ip_tunnel_parm_from_user(&p, data))
1084 		return -EFAULT;
1085 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, cmd);
1086 	if (!err && !ip_tunnel_parm_to_user(data, &p))
1087 		return -EFAULT;
1088 	return err;
1089 }
1090 EXPORT_SYMBOL_GPL(ip_tunnel_siocdevprivate);
1091 
1092 int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1093 {
1094 	struct ip_tunnel *tunnel = netdev_priv(dev);
1095 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1096 	int max_mtu = IP_MAX_MTU - t_hlen;
1097 
1098 	if (dev->type == ARPHRD_ETHER)
1099 		max_mtu -= dev->hard_header_len;
1100 
1101 	if (new_mtu < ETH_MIN_MTU || new_mtu > max_mtu)
1102 		return -EINVAL;
1103 
1104 	WRITE_ONCE(dev->mtu, new_mtu);
1105 	return 0;
1106 }
1107 EXPORT_SYMBOL_GPL(ip_tunnel_change_mtu);
1108 
1109 static void ip_tunnel_dev_free(struct net_device *dev)
1110 {
1111 	struct ip_tunnel *tunnel = netdev_priv(dev);
1112 
1113 	gro_cells_destroy(&tunnel->gro_cells);
1114 	dst_cache_destroy(&tunnel->dst_cache);
1115 }
1116 
1117 void ip_tunnel_dellink(struct net_device *dev, struct list_head *head)
1118 {
1119 	struct ip_tunnel *tunnel = netdev_priv(dev);
1120 	struct ip_tunnel_net *itn;
1121 
1122 	itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
1123 
1124 	if (itn->fb_tunnel_dev != dev) {
1125 		mutex_lock(&itn->tunnels_lock);
1126 		if (!ip_tunnel_unregistering(tunnel))
1127 			__ip_tunnel_dellink(dev_net(dev), dev, head);
1128 		mutex_unlock(&itn->tunnels_lock);
1129 	}
1130 }
1131 EXPORT_SYMBOL_GPL(ip_tunnel_dellink);
1132 
1133 struct net *ip_tunnel_get_link_net(const struct net_device *dev)
1134 {
1135 	struct ip_tunnel *tunnel = netdev_priv(dev);
1136 
1137 	return READ_ONCE(tunnel->net);
1138 }
1139 EXPORT_SYMBOL(ip_tunnel_get_link_net);
1140 
1141 int ip_tunnel_get_iflink(const struct net_device *dev)
1142 {
1143 	const struct ip_tunnel *tunnel = netdev_priv(dev);
1144 
1145 	return READ_ONCE(tunnel->parms.link);
1146 }
1147 EXPORT_SYMBOL(ip_tunnel_get_iflink);
1148 
1149 int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
1150 				  struct rtnl_link_ops *ops, char *devname)
1151 {
1152 	struct ip_tunnel_net *itn = net_generic(net, ip_tnl_net_id);
1153 	struct ip_tunnel_parm_kern parms;
1154 	unsigned int i;
1155 
1156 	itn->rtnl_link_ops = ops;
1157 	for (i = 0; i < IP_TNL_HASH_SIZE; i++)
1158 		INIT_HLIST_HEAD(&itn->tunnels[i]);
1159 
1160 	mutex_init(&itn->tunnels_lock);
1161 
1162 	if (!ops || !net_has_fallback_tunnels(net)) {
1163 		struct ip_tunnel_net *it_init_net;
1164 
1165 		it_init_net = net_generic(&init_net, ip_tnl_net_id);
1166 		itn->type = it_init_net->type;
1167 		itn->fb_tunnel_dev = NULL;
1168 		return 0;
1169 	}
1170 
1171 	memset(&parms, 0, sizeof(parms));
1172 	if (devname)
1173 		strscpy(parms.name, devname, IFNAMSIZ);
1174 
1175 	rtnl_lock();
1176 	itn->fb_tunnel_dev = __ip_tunnel_create(net, ops, &parms);
1177 	/* FB netdevice is special: we have one, and only one per netns.
1178 	 * Allowing to move it to another netns is clearly unsafe.
1179 	 */
1180 	if (!IS_ERR(itn->fb_tunnel_dev)) {
1181 		itn->fb_tunnel_dev->netns_immutable = true;
1182 		itn->fb_tunnel_dev->mtu = ip_tunnel_bind_dev(itn->fb_tunnel_dev);
1183 		ip_tunnel_add(itn, netdev_priv(itn->fb_tunnel_dev));
1184 		itn->type = itn->fb_tunnel_dev->type;
1185 	}
1186 	rtnl_unlock();
1187 
1188 	return PTR_ERR_OR_ZERO(itn->fb_tunnel_dev);
1189 }
1190 EXPORT_SYMBOL_GPL(ip_tunnel_init_net);
1191 
1192 void ip_tunnel_delete_net(struct net *net, unsigned int id,
1193 			  struct list_head *head)
1194 {
1195 	struct ip_tunnel_net *itn = net_generic(net, id);
1196 	int h;
1197 
1198 	ASSERT_RTNL_NET(net);
1199 
1200 	mutex_lock(&itn->tunnels_lock);
1201 
1202 	WRITE_ONCE(itn->fb_tunnel_dev, NULL);
1203 
1204 	for (h = 0; h < IP_TNL_HASH_SIZE; h++) {
1205 		struct hlist_head *thead = &itn->tunnels[h];
1206 		struct hlist_node *n;
1207 		struct ip_tunnel *t;
1208 
1209 		hlist_for_each_entry_safe(t, n, thead, hash_node)
1210 			__ip_tunnel_dellink(net, t->dev, head);
1211 	}
1212 
1213 	mutex_unlock(&itn->tunnels_lock);
1214 }
1215 EXPORT_SYMBOL_GPL(ip_tunnel_delete_net);
1216 
1217 int ip_tunnel_newlink(struct net *net, struct net_device *dev,
1218 		      struct nlattr *tb[], struct ip_tunnel_parm_kern *p,
1219 		      __u32 fwmark)
1220 {
1221 	struct ip_tunnel_net *itn;
1222 	struct ip_tunnel *nt;
1223 	int err = 0;
1224 	int mtu;
1225 
1226 	nt = netdev_priv(dev);
1227 	itn = net_generic(net, nt->ip_tnl_net_id);
1228 
1229 	mutex_lock(&itn->tunnels_lock);
1230 
1231 	if (nt->collect_md) {
1232 		if (rtnl_dereference(itn->collect_md_tun))
1233 			err = -EEXIST;
1234 	} else {
1235 		if (ip_tunnel_find(itn, p, dev->type))
1236 			err = -EEXIST;
1237 	}
1238 	if (err)
1239 		goto out;
1240 
1241 	nt->net = net;
1242 	nt->parms = *p;
1243 	nt->fwmark = fwmark;
1244 	err = register_netdevice(dev);
1245 	if (err)
1246 		goto err_register_netdevice;
1247 
1248 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1249 		eth_hw_addr_random(dev);
1250 
1251 	mtu = ip_tunnel_bind_dev(dev);
1252 	if (tb[IFLA_MTU]) {
1253 		unsigned int max = IP_MAX_MTU - (nt->hlen + sizeof(struct iphdr));
1254 
1255 		if (dev->type == ARPHRD_ETHER)
1256 			max -= dev->hard_header_len;
1257 
1258 		mtu = clamp(dev->mtu, (unsigned int)ETH_MIN_MTU, max);
1259 	}
1260 
1261 	err = dev_set_mtu(dev, mtu);
1262 	if (err)
1263 		goto err_dev_set_mtu;
1264 
1265 	ip_tunnel_add(itn, nt);
1266 out:
1267 	mutex_unlock(&itn->tunnels_lock);
1268 
1269 	return err;
1270 
1271 err_dev_set_mtu:
1272 	unregister_netdevice(dev);
1273 err_register_netdevice:
1274 	goto out;
1275 }
1276 EXPORT_SYMBOL_GPL(ip_tunnel_newlink);
1277 
1278 int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
1279 			 struct ip_tunnel_parm_kern *p, __u32 fwmark)
1280 {
1281 	struct ip_tunnel *tunnel = netdev_priv(dev);
1282 	struct net *net = tunnel->net;
1283 	struct ip_tunnel_net *itn;
1284 	struct ip_tunnel *t;
1285 	int err = 0;
1286 
1287 	itn = net_generic(net, tunnel->ip_tnl_net_id);
1288 
1289 	if (dev == itn->fb_tunnel_dev)
1290 		return -EINVAL;
1291 
1292 	mutex_lock(&itn->tunnels_lock);
1293 
1294 	t = ip_tunnel_find(itn, p, dev->type);
1295 
1296 	if (t) {
1297 		if (t->dev != dev) {
1298 			err = -EEXIST;
1299 			goto out;
1300 		}
1301 	} else {
1302 		t = tunnel;
1303 
1304 		if (dev->type != ARPHRD_ETHER) {
1305 			unsigned int nflags = 0;
1306 
1307 			if (ipv4_is_multicast(p->iph.daddr))
1308 				nflags = IFF_BROADCAST;
1309 			else if (p->iph.daddr)
1310 				nflags = IFF_POINTOPOINT;
1311 
1312 			if ((dev->flags ^ nflags) &
1313 			    (IFF_POINTOPOINT | IFF_BROADCAST)) {
1314 				err = -EINVAL;
1315 				goto out;
1316 			}
1317 		}
1318 	}
1319 
1320 	if (ip_tunnel_unregistering(t)) {
1321 		err = -ENODEV;
1322 		goto out;
1323 	}
1324 
1325 	ip_tunnel_update(itn, t, dev, p, !tb[IFLA_MTU], fwmark);
1326 out:
1327 	mutex_unlock(&itn->tunnels_lock);
1328 
1329 	return err;
1330 }
1331 EXPORT_SYMBOL_GPL(ip_tunnel_changelink);
1332 
1333 int __ip_tunnel_init(struct net_device *dev)
1334 {
1335 	struct ip_tunnel *tunnel = netdev_priv(dev);
1336 	struct iphdr *iph = &tunnel->parms.iph;
1337 	int err;
1338 
1339 	dev->needs_free_netdev = true;
1340 	dev->priv_destructor = ip_tunnel_dev_free;
1341 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1342 
1343 	err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1344 	if (err)
1345 		return err;
1346 
1347 	err = gro_cells_init(&tunnel->gro_cells, dev);
1348 	if (err) {
1349 		dst_cache_destroy(&tunnel->dst_cache);
1350 		return err;
1351 	}
1352 
1353 	tunnel->dev = dev;
1354 	strscpy(tunnel->parms.name, dev->name);
1355 	iph->version		= 4;
1356 	iph->ihl		= 5;
1357 
1358 	if (tunnel->collect_md)
1359 		netif_keep_dst(dev);
1360 	return 0;
1361 }
1362 EXPORT_SYMBOL_GPL(__ip_tunnel_init);
1363 
1364 void ip_tunnel_uninit(struct net_device *dev)
1365 {
1366 	struct ip_tunnel *tunnel = netdev_priv(dev);
1367 
1368 	dst_cache_reset(&tunnel->dst_cache);
1369 }
1370 EXPORT_SYMBOL_GPL(ip_tunnel_uninit);
1371 
1372 /* Do least required initialization, rest of init is done in tunnel_init call */
1373 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id)
1374 {
1375 	struct ip_tunnel *tunnel = netdev_priv(dev);
1376 	tunnel->ip_tnl_net_id = net_id;
1377 }
1378 EXPORT_SYMBOL_GPL(ip_tunnel_setup);
1379 
1380 MODULE_DESCRIPTION("IPv4 tunnel implementation library");
1381 MODULE_LICENSE("GPL");
1382