xref: /linux/net/ipv6/ip6_gre.c (revision 72ebc685bca99f466ef35600b5263d1c21c790d9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	GRE over IPv6 protocol decoder.
4  *
5  *	Authors: Dmitry Kozlov (xeb@mail.ru)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/in.h>
19 #include <linux/tcp.h>
20 #include <linux/udp.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <linux/in6.h>
24 #include <linux/inetdevice.h>
25 #include <linux/igmp.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/etherdevice.h>
28 #include <linux/if_ether.h>
29 #include <linux/hash.h>
30 #include <linux/if_tunnel.h>
31 #include <linux/ip6_tunnel.h>
32 
33 #include <net/sock.h>
34 #include <net/ip.h>
35 #include <net/ip_tunnels.h>
36 #include <net/icmp.h>
37 #include <net/protocol.h>
38 #include <net/addrconf.h>
39 #include <net/arp.h>
40 #include <net/checksum.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/xfrm.h>
44 #include <net/net_namespace.h>
45 #include <net/netns/generic.h>
46 #include <net/netdev_lock.h>
47 #include <net/rtnetlink.h>
48 
49 #include <net/ipv6.h>
50 #include <net/ip6_fib.h>
51 #include <net/ip6_route.h>
52 #include <net/ip6_tunnel.h>
53 #include <net/gre.h>
54 #include <net/erspan.h>
55 #include <net/dst_metadata.h>
56 
57 
58 static bool log_ecn_error = true;
59 module_param(log_ecn_error, bool, 0644);
60 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
61 
62 #define IP6_GRE_HASH_SIZE_SHIFT  5
63 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
64 
65 static unsigned int ip6gre_net_id __read_mostly;
66 struct ip6gre_net {
67 	struct hlist_head tunnels[4][IP6_GRE_HASH_SIZE];
68 	struct mutex tunnels_lock;
69 
70 	struct ip6_tnl __rcu *collect_md_tun;
71 	struct ip6_tnl __rcu *collect_md_tun_erspan;
72 	struct net_device *fb_tunnel_dev;
73 };
74 
75 static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
76 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
77 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
78 static int ip6gre_tunnel_init(struct net_device *dev);
79 static void ip6gre_tunnel_setup(struct net_device *dev);
80 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
81 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
82 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
83 static void __ip6gre_dellink(struct net *net, struct net_device *dev,
84 			     struct list_head *head);
85 
86 /* Tunnel hash table */
87 
88 /*
89    4 hash tables:
90 
91    3: (remote,local)
92    2: (remote,*)
93    1: (*,local)
94    0: (*,*)
95 
96    We require exact key match i.e. if a key is present in packet
97    it will match only tunnel with the same key; if it is not present,
98    it will match only keyless tunnel.
99 
100    All keysless packets, if not matched configured keyless tunnels
101    will match fallback tunnel.
102  */
103 
104 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
105 static u32 HASH_ADDR(const struct in6_addr *addr)
106 {
107 	u32 hash = ipv6_addr_hash(addr);
108 
109 	return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
110 }
111 
112 #define tunnels_r_l	tunnels[3]
113 #define tunnels_r	tunnels[2]
114 #define tunnels_l	tunnels[1]
115 #define tunnels_wc	tunnels[0]
116 
117 static bool ip6gre_tunnel_match(struct ip6_tnl *t, int dev_type, int link,
118 				int *cand_score, struct ip6_tnl **ret)
119 {
120 	int score = 0;
121 
122 	if (t->dev->type != ARPHRD_IP6GRE &&
123 	    t->dev->type != dev_type)
124 		return false;
125 
126 	if (t->parms.link != link)
127 		score |= 1;
128 	if (t->dev->type != dev_type)
129 		score |= 2;
130 	if (score == 0) {
131 		*ret = t;
132 		return true;
133 	}
134 
135 	if (score < *cand_score) {
136 		*ret = t;
137 		*cand_score = score;
138 	}
139 	return false;
140 }
141 
142 /* Given src, dst and key, find appropriate for input tunnel. */
143 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
144 		const struct in6_addr *remote, const struct in6_addr *local,
145 		__be32 key, __be16 gre_proto)
146 {
147 	int dev_type = (gre_proto == htons(ETH_P_TEB) ||
148 			gre_proto == htons(ETH_P_ERSPAN) ||
149 			gre_proto == htons(ETH_P_ERSPAN2)) ?
150 		       ARPHRD_ETHER : ARPHRD_IP6GRE;
151 	unsigned int h0 = HASH_ADDR(remote);
152 	unsigned int h1 = HASH_KEY(key);
153 	struct ip6_tnl *t, *cand = NULL;
154 	struct net *net = dev_net(dev);
155 	struct net_device *ndev;
156 	struct hlist_head *head;
157 	int link = dev->ifindex;
158 	struct ip6gre_net *ign;
159 	int cand_score = 4;
160 
161 	ign = net_generic(net, ip6gre_net_id);
162 
163 	head = &ign->tunnels_r_l[h0 ^ h1];
164 	hlist_for_each_entry_rcu(t, head, hash_node) {
165 		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
166 		    !ipv6_addr_equal(remote, &t->parms.raddr) ||
167 		    key != t->parms.i_key ||
168 		    !(t->dev->flags & IFF_UP))
169 			continue;
170 
171 		if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
172 			return cand;
173 	}
174 
175 	head = &ign->tunnels_r[h0 ^ h1];
176 	hlist_for_each_entry_rcu(t, head, hash_node) {
177 		if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
178 		    key != t->parms.i_key ||
179 		    !(t->dev->flags & IFF_UP))
180 			continue;
181 
182 		if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
183 			return cand;
184 	}
185 
186 	head = &ign->tunnels_l[h1];
187 	hlist_for_each_entry_rcu(t, head, hash_node) {
188 		if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
189 			  (!ipv6_addr_equal(local, &t->parms.raddr) ||
190 				 !ipv6_addr_is_multicast(local))) ||
191 		    key != t->parms.i_key ||
192 		    !(t->dev->flags & IFF_UP))
193 			continue;
194 
195 		if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
196 			return cand;
197 	}
198 
199 	head = &ign->tunnels_wc[h1];
200 	hlist_for_each_entry_rcu(t, head, hash_node) {
201 		if (t->parms.i_key != key ||
202 		    !(t->dev->flags & IFF_UP))
203 			continue;
204 
205 		if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
206 			return cand;
207 	}
208 
209 	if (cand)
210 		return cand;
211 
212 	if (gre_proto == htons(ETH_P_ERSPAN) ||
213 	    gre_proto == htons(ETH_P_ERSPAN2))
214 		t = rcu_dereference(ign->collect_md_tun_erspan);
215 	else
216 		t = rcu_dereference(ign->collect_md_tun);
217 
218 	if (t && t->dev->flags & IFF_UP)
219 		return t;
220 
221 	ndev = READ_ONCE(ign->fb_tunnel_dev);
222 	if (ndev && ndev->flags & IFF_UP)
223 		return netdev_priv(ndev);
224 
225 	return NULL;
226 }
227 
228 static struct hlist_head *__ip6gre_bucket(struct ip6gre_net *ign,
229 					  const struct __ip6_tnl_parm *p)
230 {
231 	const struct in6_addr *remote = &p->raddr;
232 	const struct in6_addr *local = &p->laddr;
233 	unsigned int h = HASH_KEY(p->i_key);
234 	int prio = 0;
235 
236 	if (!ipv6_addr_any(local))
237 		prio |= 1;
238 	if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
239 		prio |= 2;
240 		h ^= HASH_ADDR(remote);
241 	}
242 
243 	return &ign->tunnels[prio][h];
244 }
245 
246 static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
247 {
248 	if (t->parms.collect_md)
249 		rcu_assign_pointer(ign->collect_md_tun, t);
250 }
251 
252 static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
253 {
254 	if (t->parms.collect_md)
255 		rcu_assign_pointer(ign->collect_md_tun_erspan, t);
256 }
257 
258 static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
259 {
260 	if (t->parms.collect_md)
261 		rcu_assign_pointer(ign->collect_md_tun, NULL);
262 }
263 
264 static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
265 				       struct ip6_tnl *t)
266 {
267 	if (t->parms.collect_md)
268 		rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
269 }
270 
271 static inline struct hlist_head *ip6gre_bucket(struct ip6gre_net *ign,
272 					       const struct ip6_tnl *t)
273 {
274 	return __ip6gre_bucket(ign, &t->parms);
275 }
276 
277 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
278 {
279 	struct hlist_head *head = ip6gre_bucket(ign, t);
280 
281 	hlist_add_head_rcu(&t->hash_node, head);
282 }
283 
284 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
285 {
286 	hlist_del_init_rcu(&t->hash_node);
287 }
288 
289 static bool ip6gre_tunnel_unregistering(struct ip6_tnl *t)
290 {
291 	return hlist_unhashed(&t->hash_node);
292 }
293 
294 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
295 					   const struct __ip6_tnl_parm *parms,
296 					   int type)
297 {
298 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
299 	const struct in6_addr *remote = &parms->raddr;
300 	const struct in6_addr *local = &parms->laddr;
301 	__be32 key = parms->i_key;
302 	struct hlist_head *head;
303 	int link = parms->link;
304 	struct ip6_tnl *t;
305 
306 	head = __ip6gre_bucket(ign, parms);
307 
308 	hlist_for_each_entry(t, head, hash_node) {
309 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
310 		    ipv6_addr_equal(remote, &t->parms.raddr) &&
311 		    key == t->parms.i_key &&
312 		    link == t->parms.link &&
313 		    type == t->dev->type)
314 			break;
315 	}
316 
317 	return t;
318 }
319 
320 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
321 		const struct __ip6_tnl_parm *parms, int create)
322 {
323 	struct ip6_tnl *t, *nt;
324 	struct net_device *dev;
325 	char name[IFNAMSIZ];
326 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
327 
328 	t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
329 	if (t && create)
330 		return NULL;
331 	if (t || !create)
332 		return t;
333 
334 	if (parms->name[0]) {
335 		if (!dev_valid_name(parms->name))
336 			return NULL;
337 		strscpy(name, parms->name);
338 	} else {
339 		strscpy(name, "ip6gre%d");
340 	}
341 	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
342 			   ip6gre_tunnel_setup);
343 	if (!dev)
344 		return NULL;
345 
346 	dev_net_set(dev, net);
347 
348 	nt = netdev_priv(dev);
349 	nt->parms = *parms;
350 	dev->rtnl_link_ops = &ip6gre_link_ops;
351 
352 	nt->dev = dev;
353 	nt->net = dev_net(dev);
354 
355 	if (register_netdevice(dev) < 0)
356 		goto failed_free;
357 
358 	ip6gre_tnl_link_config(nt, 1);
359 	ip6gre_tunnel_link(ign, nt);
360 	return nt;
361 
362 failed_free:
363 	free_netdev(dev);
364 	return NULL;
365 }
366 
367 static void ip6erspan_tunnel_uninit(struct net_device *dev)
368 {
369 	struct ip6_tnl *t = netdev_priv(dev);
370 
371 	dst_cache_reset(&t->dst_cache);
372 	netdev_put(dev, &t->dev_tracker);
373 }
374 
375 static void ip6gre_tunnel_uninit(struct net_device *dev)
376 {
377 	struct ip6_tnl *t = netdev_priv(dev);
378 
379 	dst_cache_reset(&t->dst_cache);
380 	netdev_put(dev, &t->dev_tracker);
381 }
382 
383 
384 static int ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
385 		       u8 type, u8 code, int offset, __be32 info)
386 {
387 	struct net *net = dev_net(skb->dev);
388 	const struct ipv6hdr *ipv6h;
389 	struct tnl_ptk_info tpi;
390 	struct ip6_tnl *t;
391 
392 	if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IPV6),
393 			     offset) < 0)
394 		return -EINVAL;
395 
396 	ipv6h = (const struct ipv6hdr *)skb->data;
397 	t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
398 				 tpi.key, tpi.proto);
399 	if (!t)
400 		return -ENOENT;
401 
402 	switch (type) {
403 	case ICMPV6_DEST_UNREACH:
404 		net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
405 				    t->parms.name);
406 		if (code != ICMPV6_PORT_UNREACH)
407 			break;
408 		return 0;
409 	case ICMPV6_TIME_EXCEED:
410 		if (code == ICMPV6_EXC_HOPLIMIT) {
411 			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
412 					    t->parms.name);
413 			break;
414 		}
415 		return 0;
416 	case ICMPV6_PARAMPROB: {
417 		struct ipv6_tlv_tnl_enc_lim *tel;
418 		__u32 teli;
419 
420 		teli = 0;
421 		if (code == ICMPV6_HDR_FIELD)
422 			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
423 
424 		if (teli && teli == be32_to_cpu(info) - 2) {
425 			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
426 			if (tel->encap_limit == 0) {
427 				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
428 						    t->parms.name);
429 			}
430 		} else {
431 			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
432 					    t->parms.name);
433 		}
434 		return 0;
435 	}
436 	case ICMPV6_PKT_TOOBIG:
437 		ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
438 		return 0;
439 	case NDISC_REDIRECT:
440 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
441 			     sock_net_uid(net, NULL));
442 		return 0;
443 	}
444 
445 	if (time_before(jiffies, READ_ONCE(t->err_time) + IP6TUNNEL_ERR_TIMEO))
446 		WRITE_ONCE(t->err_count, READ_ONCE(t->err_count) + 1);
447 	else
448 		WRITE_ONCE(t->err_count, 1);
449 	WRITE_ONCE(t->err_time, jiffies);
450 
451 	return 0;
452 }
453 
454 static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
455 {
456 	const struct ipv6hdr *ipv6h;
457 	struct ip6_tnl *tunnel;
458 
459 	ipv6h = ipv6_hdr(skb);
460 	tunnel = ip6gre_tunnel_lookup(skb->dev,
461 				      &ipv6h->saddr, &ipv6h->daddr, tpi->key,
462 				      tpi->proto);
463 	if (tunnel) {
464 		if (tunnel->parms.collect_md) {
465 			IP_TUNNEL_DECLARE_FLAGS(flags);
466 			struct metadata_dst *tun_dst;
467 			__be64 tun_id;
468 
469 			ip_tunnel_flags_copy(flags, tpi->flags);
470 			tun_id = key32_to_tunnel_id(tpi->key);
471 
472 			tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
473 			if (!tun_dst)
474 				return PACKET_REJECT;
475 
476 			ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
477 		} else {
478 			ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
479 		}
480 
481 		return PACKET_RCVD;
482 	}
483 
484 	return PACKET_REJECT;
485 }
486 
487 static int ip6erspan_rcv(struct sk_buff *skb,
488 			 struct tnl_ptk_info *tpi,
489 			 int gre_hdr_len)
490 {
491 	struct erspan_base_hdr *ershdr;
492 	const struct ipv6hdr *ipv6h;
493 	struct erspan_md2 *md2;
494 	struct ip6_tnl *tunnel;
495 	u8 ver;
496 
497 	if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
498 		return PACKET_REJECT;
499 
500 	ipv6h = ipv6_hdr(skb);
501 	ershdr = (struct erspan_base_hdr *)skb->data;
502 	ver = ershdr->ver;
503 
504 	tunnel = ip6gre_tunnel_lookup(skb->dev,
505 				      &ipv6h->saddr, &ipv6h->daddr, tpi->key,
506 				      tpi->proto);
507 	if (tunnel) {
508 		int len = erspan_hdr_len(ver);
509 
510 		if (unlikely(!pskb_may_pull(skb, len)))
511 			return PACKET_REJECT;
512 
513 		if (__iptunnel_pull_header(skb, len,
514 					   htons(ETH_P_TEB),
515 					   false, false))
516 			return PACKET_REJECT;
517 
518 		if (tunnel->parms.collect_md) {
519 			struct erspan_metadata *pkt_md, *md;
520 			IP_TUNNEL_DECLARE_FLAGS(flags);
521 			struct metadata_dst *tun_dst;
522 			struct ip_tunnel_info *info;
523 			unsigned char *gh;
524 			__be64 tun_id;
525 
526 			__set_bit(IP_TUNNEL_KEY_BIT, tpi->flags);
527 			ip_tunnel_flags_copy(flags, tpi->flags);
528 			tun_id = key32_to_tunnel_id(tpi->key);
529 
530 			tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
531 						  sizeof(*md));
532 			if (!tun_dst)
533 				return PACKET_REJECT;
534 
535 			/* MUST set options_len before referencing options */
536 			info = &tun_dst->u.tun_info;
537 			info->options_len = sizeof(*md);
538 
539 			/* skb can be uncloned in __iptunnel_pull_header, so
540 			 * old pkt_md is no longer valid and we need to reset
541 			 * it
542 			 */
543 			gh = skb_network_header(skb) +
544 			     skb_network_header_len(skb);
545 			pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
546 							    sizeof(*ershdr));
547 			md = ip_tunnel_info_opts(info);
548 			md->version = ver;
549 			md2 = &md->u.md2;
550 			memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
551 						       ERSPAN_V2_MDSIZE);
552 			__set_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
553 				  info->key.tun_flags);
554 
555 			ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
556 
557 		} else {
558 			ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
559 		}
560 
561 		return PACKET_RCVD;
562 	}
563 
564 	return PACKET_REJECT;
565 }
566 
567 static int gre_rcv(struct sk_buff *skb)
568 {
569 	struct tnl_ptk_info tpi;
570 	bool csum_err = false;
571 	int hdr_len;
572 
573 	hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
574 	if (hdr_len < 0)
575 		goto drop;
576 
577 	if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
578 		goto drop;
579 
580 	if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
581 		     tpi.proto == htons(ETH_P_ERSPAN2))) {
582 		if (ip6erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
583 			return 0;
584 		goto out;
585 	}
586 
587 	if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
588 		return 0;
589 
590 out:
591 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
592 drop:
593 	dev_core_stats_rx_dropped_inc(skb->dev);
594 	kfree_skb(skb);
595 	return 0;
596 }
597 
598 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
599 {
600 	return iptunnel_handle_offloads(skb,
601 					csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
602 }
603 
604 static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
605 				     struct net_device *dev,
606 				     struct flowi6 *fl6, __u8 *dsfield,
607 				     int *encap_limit)
608 {
609 	const struct iphdr *iph = ip_hdr(skb);
610 	struct ip6_tnl *t = netdev_priv(dev);
611 
612 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
613 		*encap_limit = t->parms.encap_limit;
614 
615 	memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
616 
617 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
618 		*dsfield = ipv4_get_dsfield(iph);
619 	else
620 		*dsfield = ip6_tclass(t->parms.flowinfo);
621 
622 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
623 		fl6->flowi6_mark = skb->mark;
624 	else
625 		fl6->flowi6_mark = t->parms.fwmark;
626 
627 	fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
628 }
629 
630 static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
631 				    struct net_device *dev,
632 				    struct flowi6 *fl6, __u8 *dsfield,
633 				    int *encap_limit)
634 {
635 	struct ipv6hdr *ipv6h;
636 	struct ip6_tnl *t = netdev_priv(dev);
637 	__u16 offset;
638 
639 	offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
640 	/* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
641 	ipv6h = ipv6_hdr(skb);
642 
643 	if (offset > 0) {
644 		struct ipv6_tlv_tnl_enc_lim *tel;
645 
646 		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
647 		if (tel->encap_limit == 0) {
648 			icmpv6_ndo_send(skb, ICMPV6_PARAMPROB,
649 					ICMPV6_HDR_FIELD, offset + 2);
650 			return -1;
651 		}
652 		*encap_limit = tel->encap_limit - 1;
653 	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
654 		*encap_limit = t->parms.encap_limit;
655 	}
656 
657 	memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
658 
659 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
660 		*dsfield = ipv6_get_dsfield(ipv6h);
661 	else
662 		*dsfield = ip6_tclass(t->parms.flowinfo);
663 
664 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
665 		fl6->flowlabel |= ip6_flowlabel(ipv6h);
666 
667 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
668 		fl6->flowi6_mark = skb->mark;
669 	else
670 		fl6->flowi6_mark = t->parms.fwmark;
671 
672 	fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
673 
674 	return 0;
675 }
676 
677 static int prepare_ip6gre_xmit_other(struct sk_buff *skb,
678 				     struct net_device *dev,
679 				     struct flowi6 *fl6, __u8 *dsfield,
680 				     int *encap_limit)
681 {
682 	struct ip6_tnl *t = netdev_priv(dev);
683 
684 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
685 		*encap_limit = t->parms.encap_limit;
686 
687 	memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
688 
689 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
690 		*dsfield = 0;
691 	else
692 		*dsfield = ip6_tclass(t->parms.flowinfo);
693 
694 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
695 		fl6->flowi6_mark = skb->mark;
696 	else
697 		fl6->flowi6_mark = t->parms.fwmark;
698 
699 	fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
700 
701 	return 0;
702 }
703 
704 static struct ip_tunnel_info *skb_tunnel_info_txcheck(struct sk_buff *skb)
705 {
706 	struct ip_tunnel_info *tun_info;
707 
708 	tun_info = skb_tunnel_info(skb);
709 	if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX)))
710 		return ERR_PTR(-EINVAL);
711 
712 	return tun_info;
713 }
714 
715 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
716 			       struct net_device *dev, __u8 dsfield,
717 			       struct flowi6 *fl6, int encap_limit,
718 			       __u32 *pmtu, __be16 proto)
719 {
720 	struct ip6_tnl *tunnel = netdev_priv(dev);
721 	IP_TUNNEL_DECLARE_FLAGS(flags);
722 	__be16 protocol;
723 
724 	if (dev->type == ARPHRD_ETHER)
725 		IPCB(skb)->flags = 0;
726 
727 	if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
728 		fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
729 	else
730 		fl6->daddr = tunnel->parms.raddr;
731 
732 	/* Push GRE header. */
733 	protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
734 
735 	if (tunnel->parms.collect_md) {
736 		struct ip_tunnel_info *tun_info;
737 		const struct ip_tunnel_key *key;
738 		int tun_hlen;
739 
740 		tun_info = skb_tunnel_info_txcheck(skb);
741 		if (IS_ERR(tun_info) ||
742 		    unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
743 			return -EINVAL;
744 
745 		key = &tun_info->key;
746 		memset(fl6, 0, sizeof(*fl6));
747 		fl6->flowi6_proto = IPPROTO_GRE;
748 		fl6->daddr = key->u.ipv6.dst;
749 		fl6->flowlabel = key->label;
750 		fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
751 		fl6->fl6_gre_key = tunnel_id_to_key32(key->tun_id);
752 
753 		dsfield = key->tos;
754 		ip_tunnel_flags_zero(flags);
755 		__set_bit(IP_TUNNEL_CSUM_BIT, flags);
756 		__set_bit(IP_TUNNEL_KEY_BIT, flags);
757 		__set_bit(IP_TUNNEL_SEQ_BIT, flags);
758 		ip_tunnel_flags_and(flags, flags, key->tun_flags);
759 		tun_hlen = gre_calc_hlen(flags);
760 
761 		if (skb_cow_head(skb, dev->needed_headroom ?: tun_hlen + tunnel->encap_hlen))
762 			return -ENOMEM;
763 
764 		gre_build_header(skb, tun_hlen,
765 				 flags, protocol,
766 				 tunnel_id_to_key32(tun_info->key.tun_id),
767 				 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
768 				 htonl(atomic_fetch_inc(&tunnel->o_seqno)) :
769 				 0);
770 
771 	} else {
772 		if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
773 			return -ENOMEM;
774 
775 		ip_tunnel_flags_copy(flags, tunnel->parms.o_flags);
776 
777 		gre_build_header(skb, tunnel->tun_hlen, flags,
778 				 protocol, tunnel->parms.o_key,
779 				 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
780 				 htonl(atomic_fetch_inc(&tunnel->o_seqno)) :
781 				 0);
782 	}
783 
784 	return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
785 			    NEXTHDR_GRE);
786 }
787 
788 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
789 {
790 	struct ip6_tnl *t = netdev_priv(dev);
791 	int encap_limit = -1;
792 	struct flowi6 fl6;
793 	__u8 dsfield = 0;
794 	__u32 mtu;
795 	int err;
796 
797 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
798 
799 	if (!t->parms.collect_md)
800 		prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
801 					 &dsfield, &encap_limit);
802 
803 	err = gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
804 						t->parms.o_flags));
805 	if (err)
806 		return -1;
807 
808 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
809 			  skb->protocol);
810 	if (err != 0) {
811 		/* XXX: send ICMP error even if DF is not set. */
812 		if (err == -EMSGSIZE)
813 			icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
814 				      htonl(mtu));
815 		return -1;
816 	}
817 
818 	return 0;
819 }
820 
821 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
822 {
823 	struct ip6_tnl *t = netdev_priv(dev);
824 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
825 	int encap_limit = -1;
826 	struct flowi6 fl6;
827 	__u8 dsfield = 0;
828 	__u32 mtu;
829 	int err;
830 
831 	/* The outer destination of a collect_md tunnel comes from the
832 	 * metadata, so raddr is not its exit point, and hosts on the link
833 	 * bridged into an L2 one do send from :: (DAD, early MLD).
834 	 */
835 	if (!(t->parms.collect_md && dev->type == ARPHRD_ETHER) &&
836 	    ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
837 		return -1;
838 
839 	if (!t->parms.collect_md &&
840 	    prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
841 		return -1;
842 
843 	if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
844 					      t->parms.o_flags)))
845 		return -1;
846 
847 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
848 			  &mtu, skb->protocol);
849 	if (err != 0) {
850 		if (err == -EMSGSIZE)
851 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
852 		return -1;
853 	}
854 
855 	return 0;
856 }
857 
858 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
859 {
860 	struct ip6_tnl *t = netdev_priv(dev);
861 	int encap_limit = -1;
862 	struct flowi6 fl6;
863 	__u8 dsfield = 0;
864 	__u32 mtu;
865 	int err;
866 
867 	if (!t->parms.collect_md &&
868 	    prepare_ip6gre_xmit_other(skb, dev, &fl6, &dsfield, &encap_limit))
869 		return -1;
870 
871 	err = gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
872 						t->parms.o_flags));
873 	if (err)
874 		return err;
875 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, skb->protocol);
876 
877 	return err;
878 }
879 
880 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
881 	struct net_device *dev)
882 {
883 	struct ip_tunnel_info *tun_info = NULL;
884 	struct ip6_tnl *t = netdev_priv(dev);
885 	__be16 payload_protocol;
886 	int ret;
887 
888 	if (!pskb_inet_may_pull(skb))
889 		goto tx_err;
890 
891 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
892 		goto tx_err;
893 
894 	if (t->parms.collect_md)
895 		tun_info = skb_tunnel_info_txcheck(skb);
896 
897 	payload_protocol = skb_protocol(skb, true);
898 	switch (payload_protocol) {
899 	case htons(ETH_P_IP):
900 		ret = ip6gre_xmit_ipv4(skb, dev);
901 		break;
902 	case htons(ETH_P_IPV6):
903 		ret = ip6gre_xmit_ipv6(skb, dev);
904 		break;
905 	default:
906 		ret = ip6gre_xmit_other(skb, dev);
907 		break;
908 	}
909 
910 	if (ret < 0)
911 		goto tx_err;
912 
913 	return NETDEV_TX_OK;
914 
915 tx_err:
916 	if (!IS_ERR(tun_info))
917 		DEV_STATS_INC(dev, tx_errors);
918 	DEV_STATS_INC(dev, tx_dropped);
919 	kfree_skb(skb);
920 	return NETDEV_TX_OK;
921 }
922 
923 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
924 					 struct net_device *dev)
925 {
926 	struct ip_tunnel_info *tun_info = NULL;
927 	struct ip6_tnl *t = netdev_priv(dev);
928 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
929 	bool truncate = false;
930 	int encap_limit = -1;
931 	__u8 dsfield = false;
932 	struct flowi6 fl6;
933 	int err = -EINVAL;
934 	__be16 proto;
935 	__u32 mtu;
936 	int nhoff;
937 
938 	if (!pskb_inet_may_pull(skb))
939 		goto tx_err;
940 
941 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
942 		goto tx_err;
943 
944 	if (gre_handle_offloads(skb, false))
945 		goto tx_err;
946 
947 	if (skb->len > dev->mtu + dev->hard_header_len) {
948 		if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
949 			goto tx_err;
950 		truncate = true;
951 	}
952 
953 	nhoff = skb_network_offset(skb);
954 	if (skb->protocol == htons(ETH_P_IP) &&
955 	    (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
956 		truncate = true;
957 
958 	if (skb->protocol == htons(ETH_P_IPV6)) {
959 		int thoff;
960 
961 		if (skb_transport_header_was_set(skb))
962 			thoff = skb_transport_offset(skb);
963 		else
964 			thoff = nhoff + sizeof(struct ipv6hdr);
965 		if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
966 			truncate = true;
967 	}
968 
969 	if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
970 		goto tx_err;
971 
972 	IPCB(skb)->flags = 0;
973 
974 	/* For collect_md mode, derive fl6 from the tunnel key,
975 	 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
976 	 */
977 	if (t->parms.collect_md) {
978 		const struct ip_tunnel_key *key;
979 		struct erspan_metadata *md;
980 		__be32 tun_id;
981 
982 		tun_info = skb_tunnel_info_txcheck(skb);
983 		if (IS_ERR(tun_info) ||
984 		    unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
985 			goto tx_err;
986 
987 		key = &tun_info->key;
988 		memset(&fl6, 0, sizeof(fl6));
989 		fl6.flowi6_proto = IPPROTO_GRE;
990 		fl6.daddr = key->u.ipv6.dst;
991 		fl6.flowlabel = key->label;
992 		fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
993 		fl6.fl6_gre_key = tunnel_id_to_key32(key->tun_id);
994 
995 		dsfield = key->tos;
996 		if (!test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
997 			      tun_info->key.tun_flags))
998 			goto tx_err;
999 		if (tun_info->options_len < sizeof(*md))
1000 			goto tx_err;
1001 		md = ip_tunnel_info_opts(tun_info);
1002 
1003 		tun_id = tunnel_id_to_key32(key->tun_id);
1004 		if (md->version == 1) {
1005 			erspan_build_header(skb,
1006 					    ntohl(tun_id),
1007 					    ntohl(md->u.index), truncate,
1008 					    false);
1009 			proto = htons(ETH_P_ERSPAN);
1010 		} else if (md->version == 2) {
1011 			erspan_build_header_v2(skb,
1012 					       ntohl(tun_id),
1013 					       md->u.md2.dir,
1014 					       get_hwid(&md->u.md2),
1015 					       truncate, false);
1016 			proto = htons(ETH_P_ERSPAN2);
1017 		} else {
1018 			goto tx_err;
1019 		}
1020 	} else {
1021 		switch (skb->protocol) {
1022 		case htons(ETH_P_IP):
1023 			memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1024 			prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
1025 						 &dsfield, &encap_limit);
1026 			break;
1027 		case htons(ETH_P_IPV6):
1028 			if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
1029 				goto tx_err;
1030 			if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
1031 						     &dsfield, &encap_limit))
1032 				goto tx_err;
1033 			break;
1034 		default:
1035 			memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1036 			break;
1037 		}
1038 
1039 		if (t->parms.erspan_ver == 1) {
1040 			erspan_build_header(skb, ntohl(t->parms.o_key),
1041 					    t->parms.index,
1042 					    truncate, false);
1043 			proto = htons(ETH_P_ERSPAN);
1044 		} else if (t->parms.erspan_ver == 2) {
1045 			erspan_build_header_v2(skb, ntohl(t->parms.o_key),
1046 					       t->parms.dir,
1047 					       t->parms.hwid,
1048 					       truncate, false);
1049 			proto = htons(ETH_P_ERSPAN2);
1050 		} else {
1051 			goto tx_err;
1052 		}
1053 
1054 		fl6.daddr = t->parms.raddr;
1055 	}
1056 
1057 	/* Push GRE header. */
1058 	__set_bit(IP_TUNNEL_SEQ_BIT, flags);
1059 	gre_build_header(skb, 8, flags, proto, 0,
1060 			 htonl(atomic_fetch_inc(&t->o_seqno)));
1061 
1062 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1063 			   NEXTHDR_GRE);
1064 	if (err != 0) {
1065 		/* XXX: send ICMP error even if DF is not set. */
1066 		if (err == -EMSGSIZE) {
1067 			if (skb->protocol == htons(ETH_P_IP))
1068 				icmp_ndo_send(skb, ICMP_DEST_UNREACH,
1069 					      ICMP_FRAG_NEEDED, htonl(mtu));
1070 			else
1071 				icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1072 		}
1073 
1074 		goto tx_err;
1075 	}
1076 	return NETDEV_TX_OK;
1077 
1078 tx_err:
1079 	if (!IS_ERR(tun_info))
1080 		DEV_STATS_INC(dev, tx_errors);
1081 	DEV_STATS_INC(dev, tx_dropped);
1082 	kfree_skb(skb);
1083 	return NETDEV_TX_OK;
1084 }
1085 
1086 static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
1087 {
1088 	struct net_device *dev = t->dev;
1089 	struct __ip6_tnl_parm *p = &t->parms;
1090 	struct flowi6 *fl6 = &t->fl.u.ip6;
1091 
1092 	if (dev->type != ARPHRD_ETHER) {
1093 		__dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
1094 		memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1095 	}
1096 
1097 	/* Set up flowi template */
1098 	fl6->saddr = p->laddr;
1099 	fl6->daddr = p->raddr;
1100 	fl6->flowi6_oif = p->link;
1101 	fl6->flowlabel = 0;
1102 	fl6->flowi6_proto = IPPROTO_GRE;
1103 	fl6->fl6_gre_key = t->parms.o_key;
1104 
1105 	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1106 		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1107 	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1108 		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1109 
1110 	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1111 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1112 
1113 	if (p->flags&IP6_TNL_F_CAP_XMIT &&
1114 			p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1115 		dev->flags |= IFF_POINTOPOINT;
1116 	else
1117 		dev->flags &= ~IFF_POINTOPOINT;
1118 }
1119 
1120 static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
1121 					 int t_hlen)
1122 {
1123 	const struct __ip6_tnl_parm *p = &t->parms;
1124 	struct net_device *dev = t->dev;
1125 
1126 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
1127 		int strict = (ipv6_addr_type(&p->raddr) &
1128 			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1129 
1130 		struct rt6_info *rt = rt6_lookup(t->net,
1131 						 &p->raddr, &p->laddr,
1132 						 p->link, NULL, strict);
1133 
1134 		if (!rt)
1135 			return;
1136 
1137 		if (rt->dst.dev) {
1138 			unsigned int headroom;
1139 
1140 			headroom = rt->dst.dev->hard_header_len + t_hlen;
1141 			headroom = ip_tunnel_limit_headroom(headroom);
1142 			dev->needed_headroom = headroom;
1143 
1144 			if (set_mtu) {
1145 				int mtu = rt->dst.dev->mtu - t_hlen;
1146 
1147 				if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1148 					mtu -= 8;
1149 				if (dev->type == ARPHRD_ETHER)
1150 					mtu -= ETH_HLEN;
1151 
1152 				if (mtu < IPV6_MIN_MTU)
1153 					mtu = IPV6_MIN_MTU;
1154 				WRITE_ONCE(dev->mtu, mtu);
1155 			}
1156 		}
1157 		ip6_rt_put(rt);
1158 	}
1159 }
1160 
1161 static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
1162 {
1163 	int t_hlen;
1164 
1165 	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1166 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1167 
1168 	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1169 
1170 	if (tunnel->dev->header_ops && tunnel->dev->type == ARPHRD_IP6GRE)
1171 		tunnel->dev->hard_header_len = t_hlen;
1172 	else
1173 		tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1174 
1175 	return t_hlen;
1176 }
1177 
1178 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1179 {
1180 	ip6gre_tnl_link_config_common(t);
1181 	ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
1182 }
1183 
1184 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
1185 				     const struct __ip6_tnl_parm *p)
1186 {
1187 	t->parms.laddr = p->laddr;
1188 	t->parms.raddr = p->raddr;
1189 	t->parms.flags = p->flags;
1190 	t->parms.hop_limit = p->hop_limit;
1191 	t->parms.encap_limit = p->encap_limit;
1192 	t->parms.flowinfo = p->flowinfo;
1193 	t->parms.link = p->link;
1194 	t->parms.proto = p->proto;
1195 	t->parms.i_key = p->i_key;
1196 	t->parms.o_key = p->o_key;
1197 	ip_tunnel_flags_copy(t->parms.i_flags, p->i_flags);
1198 	ip_tunnel_flags_copy(t->parms.o_flags, p->o_flags);
1199 	t->parms.fwmark = p->fwmark;
1200 	t->parms.erspan_ver = p->erspan_ver;
1201 	t->parms.index = p->index;
1202 	t->parms.dir = p->dir;
1203 	t->parms.hwid = p->hwid;
1204 	dst_cache_reset(&t->dst_cache);
1205 }
1206 
1207 static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
1208 			     int set_mtu)
1209 {
1210 	ip6gre_tnl_copy_tnl_parm(t, p);
1211 	ip6gre_tnl_link_config(t, set_mtu);
1212 	return 0;
1213 }
1214 
1215 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1216 	const struct ip6_tnl_parm2 *u)
1217 {
1218 	p->laddr = u->laddr;
1219 	p->raddr = u->raddr;
1220 	p->flags = u->flags;
1221 	p->hop_limit = u->hop_limit;
1222 	p->encap_limit = u->encap_limit;
1223 	p->flowinfo = u->flowinfo;
1224 	p->link = u->link;
1225 	p->i_key = u->i_key;
1226 	p->o_key = u->o_key;
1227 	gre_flags_to_tnl_flags(p->i_flags, u->i_flags);
1228 	gre_flags_to_tnl_flags(p->o_flags, u->o_flags);
1229 	memcpy(p->name, u->name, sizeof(u->name));
1230 }
1231 
1232 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1233 	const struct __ip6_tnl_parm *p)
1234 {
1235 	u->proto = IPPROTO_GRE;
1236 	u->laddr = p->laddr;
1237 	u->raddr = p->raddr;
1238 	u->flags = p->flags;
1239 	u->hop_limit = p->hop_limit;
1240 	u->encap_limit = p->encap_limit;
1241 	u->flowinfo = p->flowinfo;
1242 	u->link = p->link;
1243 	u->i_key = p->i_key;
1244 	u->o_key = p->o_key;
1245 	u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1246 	u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
1247 	memcpy(u->name, p->name, sizeof(u->name));
1248 }
1249 
1250 static int ip6gre_tunnel_siocdevprivate(struct net_device *dev,
1251 					struct ifreq *ifr, void __user *data,
1252 					int cmd)
1253 {
1254 	struct ip6_tnl *t = netdev_priv(dev);
1255 	struct net *orig_net = dev_net(dev);
1256 	struct __ip6_tnl_parm p1 = {};
1257 	LIST_HEAD(dev_kill_list);
1258 	struct net *net = t->net;
1259 	struct ip6_tnl_parm2 p;
1260 	struct ip6gre_net *ign;
1261 	int err = 0;
1262 
1263 	DEBUG_NET_WARN_ON_ONCE(netdev_need_ops_lock(dev));
1264 
1265 	ign = net_generic(net, ip6gre_net_id);
1266 
1267 	mutex_lock(&ign->tunnels_lock);
1268 
1269 	if (!check_net(net)) {
1270 		err = -EBUSY;
1271 		goto done;
1272 	}
1273 
1274 	switch (cmd) {
1275 	case SIOCGETTUNNEL:
1276 		if (dev == ign->fb_tunnel_dev) {
1277 			if (copy_from_user(&p, data, sizeof(p))) {
1278 				err = -EFAULT;
1279 				break;
1280 			}
1281 			ip6gre_tnl_parm_from_user(&p1, &p);
1282 			t = ip6gre_tunnel_locate(net, &p1, 0);
1283 			if (!t)
1284 				t = netdev_priv(dev);
1285 		}
1286 		memset(&p, 0, sizeof(p));
1287 		ip6gre_tnl_parm_to_user(&p, &t->parms);
1288 		if (copy_to_user(data, &p, sizeof(p)))
1289 			err = -EFAULT;
1290 		break;
1291 
1292 	case SIOCADDTUNNEL:
1293 	case SIOCCHGTUNNEL:
1294 		err = -EPERM;
1295 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1296 			goto done;
1297 
1298 		err = -EFAULT;
1299 		if (copy_from_user(&p, data, sizeof(p)))
1300 			goto done;
1301 
1302 		err = -EINVAL;
1303 		if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1304 			goto done;
1305 
1306 		if (!(p.i_flags&GRE_KEY))
1307 			p.i_key = 0;
1308 		if (!(p.o_flags&GRE_KEY))
1309 			p.o_key = 0;
1310 
1311 		ip6gre_tnl_parm_from_user(&p1, &p);
1312 		t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1313 
1314 		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1315 			if (t && !ip6gre_tunnel_unregistering(t)) {
1316 				if (t->dev != dev) {
1317 					err = -EEXIST;
1318 					break;
1319 				}
1320 			} else {
1321 				t = netdev_priv(dev);
1322 
1323 				if (!ip6gre_tunnel_unregistering(t)) {
1324 					ip6gre_tunnel_unlink(ign, t);
1325 					synchronize_net();
1326 					ip6gre_tnl_change(t, &p1, 1);
1327 					ip6gre_tunnel_link(ign, t);
1328 					netdev_state_change(dev);
1329 				}
1330 			}
1331 		}
1332 
1333 		if (t && !ip6gre_tunnel_unregistering(t)) {
1334 			err = 0;
1335 
1336 			memset(&p, 0, sizeof(p));
1337 			ip6gre_tnl_parm_to_user(&p, &t->parms);
1338 			if (copy_to_user(data, &p, sizeof(p)))
1339 				err = -EFAULT;
1340 		} else {
1341 			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1342 		}
1343 		break;
1344 
1345 	case SIOCDELTUNNEL:
1346 		err = -EPERM;
1347 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1348 			goto done;
1349 
1350 		if (dev == ign->fb_tunnel_dev) {
1351 			err = -EFAULT;
1352 			if (copy_from_user(&p, data, sizeof(p)))
1353 				goto done;
1354 			err = -ENOENT;
1355 			ip6gre_tnl_parm_from_user(&p1, &p);
1356 			t = ip6gre_tunnel_locate(net, &p1, 0);
1357 			if (!t)
1358 				goto done;
1359 			err = -EPERM;
1360 			if (t == netdev_priv(ign->fb_tunnel_dev))
1361 				goto done;
1362 			dev = t->dev;
1363 		}
1364 
1365 		if (!ip6gre_tunnel_unregistering(t))
1366 			__ip6gre_dellink(orig_net, dev, &dev_kill_list);
1367 		err = 0;
1368 		break;
1369 
1370 	default:
1371 		err = -EINVAL;
1372 	}
1373 
1374 done:
1375 	mutex_unlock(&ign->tunnels_lock);
1376 
1377 	unregister_netdevice_many(&dev_kill_list);
1378 	return err;
1379 }
1380 
1381 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1382 			 unsigned short type, const void *daddr,
1383 			 const void *saddr, unsigned int len)
1384 {
1385 	struct ip6_tnl *t = netdev_priv(dev);
1386 	struct ipv6hdr *ipv6h;
1387 	int needed;
1388 	__be16 *p;
1389 
1390 	needed = t->hlen + sizeof(*ipv6h);
1391 	if (skb_headroom(skb) < needed &&
1392 	    pskb_expand_head(skb, HH_DATA_ALIGN(needed - skb_headroom(skb)),
1393 			     0, GFP_ATOMIC))
1394 		return -needed;
1395 
1396 	ipv6h = skb_push(skb, needed);
1397 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1398 						  t->fl.u.ip6.flowlabel,
1399 						  true, &t->fl.u.ip6));
1400 	ipv6h->hop_limit = t->parms.hop_limit;
1401 	ipv6h->nexthdr = NEXTHDR_GRE;
1402 	ipv6h->saddr = t->parms.laddr;
1403 	ipv6h->daddr = t->parms.raddr;
1404 
1405 	p = (__be16 *)(ipv6h + 1);
1406 	p[0] = ip_tunnel_flags_to_be16(t->parms.o_flags);
1407 	p[1] = htons(type);
1408 
1409 	/*
1410 	 *	Set the source hardware address.
1411 	 */
1412 
1413 	if (saddr)
1414 		memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1415 	if (daddr)
1416 		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1417 	if (!ipv6_addr_any(&ipv6h->daddr))
1418 		return t->hlen;
1419 
1420 	return -t->hlen;
1421 }
1422 
1423 static const struct header_ops ip6gre_header_ops = {
1424 	.create	= ip6gre_header,
1425 };
1426 
1427 static const struct net_device_ops ip6gre_netdev_ops = {
1428 	.ndo_init		= ip6gre_tunnel_init,
1429 	.ndo_uninit		= ip6gre_tunnel_uninit,
1430 	.ndo_start_xmit		= ip6gre_tunnel_xmit,
1431 	.ndo_siocdevprivate	= ip6gre_tunnel_siocdevprivate,
1432 	.ndo_change_mtu		= ip6_tnl_change_mtu,
1433 	.ndo_get_iflink		= ip6_tnl_get_iflink,
1434 };
1435 
1436 static void ip6gre_dev_free(struct net_device *dev)
1437 {
1438 	struct ip6_tnl *t = netdev_priv(dev);
1439 
1440 	gro_cells_destroy(&t->gro_cells);
1441 	dst_cache_destroy(&t->dst_cache);
1442 }
1443 
1444 static void ip6gre_tunnel_setup(struct net_device *dev)
1445 {
1446 	dev->netdev_ops = &ip6gre_netdev_ops;
1447 	dev->needs_free_netdev = true;
1448 	dev->priv_destructor = ip6gre_dev_free;
1449 
1450 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1451 	dev->type = ARPHRD_IP6GRE;
1452 
1453 	dev->flags |= IFF_NOARP;
1454 	dev->addr_len = sizeof(struct in6_addr);
1455 	netif_keep_dst(dev);
1456 	/* This perm addr will be used as interface identifier by IPv6 */
1457 	dev->addr_assign_type = NET_ADDR_RANDOM;
1458 	eth_random_addr(dev->perm_addr);
1459 }
1460 
1461 #define GRE6_FEATURES (NETIF_F_SG |		\
1462 		       NETIF_F_FRAGLIST |	\
1463 		       NETIF_F_HIGHDMA |	\
1464 		       NETIF_F_HW_CSUM)
1465 
1466 static void ip6gre_tnl_init_features(struct net_device *dev)
1467 {
1468 	struct ip6_tnl *nt = netdev_priv(dev);
1469 
1470 	dev->features		|= GRE6_FEATURES;
1471 	dev->hw_features	|= GRE6_FEATURES;
1472 
1473 	dev->lltx = true;
1474 
1475 	/* TCP offload with GRE SEQ is not supported, nor can we support 2
1476 	 * levels of outer headers requiring an update.
1477 	 */
1478 	if (test_bit(IP_TUNNEL_SEQ_BIT, nt->parms.o_flags))
1479 		return;
1480 	if (test_bit(IP_TUNNEL_CSUM_BIT, nt->parms.o_flags) &&
1481 	    nt->encap.type != TUNNEL_ENCAP_NONE)
1482 		return;
1483 
1484 	dev->features |= NETIF_F_GSO_SOFTWARE;
1485 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1486 }
1487 
1488 static int ip6gre_tunnel_init_common(struct net_device *dev)
1489 {
1490 	struct ip6_tnl *tunnel;
1491 	int ret;
1492 	int t_hlen;
1493 
1494 	tunnel = netdev_priv(dev);
1495 
1496 	tunnel->dev = dev;
1497 	strscpy(tunnel->parms.name, dev->name);
1498 
1499 	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1500 	if (ret)
1501 		return ret;
1502 
1503 	ret = gro_cells_init(&tunnel->gro_cells, dev);
1504 	if (ret)
1505 		goto cleanup_dst_cache_init;
1506 
1507 	t_hlen = ip6gre_calc_hlen(tunnel);
1508 	dev->mtu = ETH_DATA_LEN - t_hlen;
1509 	if (dev->type == ARPHRD_ETHER)
1510 		dev->mtu -= ETH_HLEN;
1511 	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1512 		dev->mtu -= 8;
1513 
1514 	if (tunnel->parms.collect_md) {
1515 		netif_keep_dst(dev);
1516 	}
1517 	ip6gre_tnl_init_features(dev);
1518 
1519 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1520 	netdev_lockdep_set_classes(dev);
1521 	return 0;
1522 
1523 cleanup_dst_cache_init:
1524 	dst_cache_destroy(&tunnel->dst_cache);
1525 	return ret;
1526 }
1527 
1528 static int ip6gre_tunnel_init(struct net_device *dev)
1529 {
1530 	struct ip6_tnl *tunnel;
1531 	int ret;
1532 
1533 	ret = ip6gre_tunnel_init_common(dev);
1534 	if (ret)
1535 		return ret;
1536 
1537 	tunnel = netdev_priv(dev);
1538 
1539 	if (tunnel->parms.collect_md)
1540 		return 0;
1541 
1542 	__dev_addr_set(dev, &tunnel->parms.laddr, sizeof(struct in6_addr));
1543 	memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1544 
1545 	if (ipv6_addr_any(&tunnel->parms.raddr))
1546 		dev->header_ops = &ip6gre_header_ops;
1547 
1548 	return 0;
1549 }
1550 
1551 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1552 {
1553 	struct ip6_tnl *tunnel = netdev_priv(dev);
1554 
1555 	tunnel->dev = dev;
1556 	tunnel->net = dev_net(dev);
1557 	strscpy(tunnel->parms.name, dev->name);
1558 
1559 	tunnel->hlen		= sizeof(struct ipv6hdr) + 4;
1560 }
1561 
1562 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1563 	.handler     = gre_rcv,
1564 	.err_handler = ip6gre_err,
1565 	.flags       = INET6_PROTO_FINAL,
1566 };
1567 
1568 static void __net_exit ip6gre_exit_rtnl_net(struct net *net,
1569 					    struct list_head *dev_kill_list)
1570 {
1571 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1572 	int prio;
1573 
1574 	WRITE_ONCE(ign->fb_tunnel_dev, NULL);
1575 
1576 	mutex_lock(&ign->tunnels_lock);
1577 
1578 	for (prio = 0; prio < 4; prio++) {
1579 		int h;
1580 
1581 		for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1582 			struct hlist_head *head = &ign->tunnels[prio][h];
1583 			struct hlist_node *tmp;
1584 			struct ip6_tnl *t;
1585 
1586 			hlist_for_each_entry_safe(t, tmp, head, hash_node)
1587 				__ip6gre_dellink(net, t->dev, dev_kill_list);
1588 		}
1589 	}
1590 
1591 	mutex_unlock(&ign->tunnels_lock);
1592 }
1593 
1594 static int __net_init ip6gre_init_net(struct net *net)
1595 {
1596 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1597 	struct net_device *ndev;
1598 	struct ip6_tnl *t;
1599 	int prio, h;
1600 	int err;
1601 
1602 	for (prio = 0; prio < 4; prio++) {
1603 		for (h = 0; h < IP6_GRE_HASH_SIZE; h++)
1604 			INIT_HLIST_HEAD(&ign->tunnels[prio][h]);
1605 	}
1606 
1607 	mutex_init(&ign->tunnels_lock);
1608 
1609 	if (!net_has_fallback_tunnels(net))
1610 		return 0;
1611 	ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1612 			    NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1613 	if (!ndev) {
1614 		err = -ENOMEM;
1615 		goto err_alloc_dev;
1616 	}
1617 	ign->fb_tunnel_dev = ndev;
1618 	dev_net_set(ign->fb_tunnel_dev, net);
1619 	/* FB netdevice is special: we have one, and only one per netns.
1620 	 * Allowing to move it to another netns is clearly unsafe.
1621 	 */
1622 	ign->fb_tunnel_dev->netns_immutable = true;
1623 
1624 	ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1625 	ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1626 
1627 	t = netdev_priv(ign->fb_tunnel_dev);
1628 	hlist_add_head_rcu(&t->hash_node, &ign->tunnels_wc[0]);
1629 
1630 	err = register_netdev(ign->fb_tunnel_dev);
1631 	if (err)
1632 		goto err_reg_dev;
1633 
1634 	return 0;
1635 
1636 err_reg_dev:
1637 	free_netdev(ndev);
1638 err_alloc_dev:
1639 	return err;
1640 }
1641 
1642 static struct pernet_operations ip6gre_net_ops = {
1643 	.init = ip6gre_init_net,
1644 	.exit_rtnl = ip6gre_exit_rtnl_net,
1645 	.id   = &ip6gre_net_id,
1646 	.size = sizeof(struct ip6gre_net),
1647 };
1648 
1649 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1650 				  struct netlink_ext_ack *extack)
1651 {
1652 	__be16 flags;
1653 
1654 	if (!data)
1655 		return 0;
1656 
1657 	flags = 0;
1658 	if (data[IFLA_GRE_IFLAGS])
1659 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1660 	if (data[IFLA_GRE_OFLAGS])
1661 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1662 	if (flags & (GRE_VERSION|GRE_ROUTING))
1663 		return -EINVAL;
1664 
1665 	return 0;
1666 }
1667 
1668 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1669 			       struct netlink_ext_ack *extack)
1670 {
1671 	struct in6_addr daddr;
1672 
1673 	if (tb[IFLA_ADDRESS]) {
1674 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1675 			return -EINVAL;
1676 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1677 			return -EADDRNOTAVAIL;
1678 	}
1679 
1680 	if (!data)
1681 		goto out;
1682 
1683 	if (data[IFLA_GRE_REMOTE]) {
1684 		daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1685 		if (ipv6_addr_any(&daddr))
1686 			return -EINVAL;
1687 	}
1688 
1689 out:
1690 	return ip6gre_tunnel_validate(tb, data, extack);
1691 }
1692 
1693 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1694 				  struct netlink_ext_ack *extack)
1695 {
1696 	__be16 flags = 0;
1697 	int ret, ver = 0;
1698 
1699 	if (!data)
1700 		return 0;
1701 
1702 	ret = ip6gre_tap_validate(tb, data, extack);
1703 	if (ret)
1704 		return ret;
1705 
1706 	/* ERSPAN should only have GRE sequence and key flag */
1707 	if (data[IFLA_GRE_OFLAGS])
1708 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1709 	if (data[IFLA_GRE_IFLAGS])
1710 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1711 	if (!data[IFLA_GRE_COLLECT_METADATA] &&
1712 	    flags != (GRE_SEQ | GRE_KEY))
1713 		return -EINVAL;
1714 
1715 	/* ERSPAN Session ID only has 10-bit. Since we reuse
1716 	 * 32-bit key field as ID, check it's range.
1717 	 */
1718 	if (data[IFLA_GRE_IKEY] &&
1719 	    (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1720 		return -EINVAL;
1721 
1722 	if (data[IFLA_GRE_OKEY] &&
1723 	    (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1724 		return -EINVAL;
1725 
1726 	if (data[IFLA_GRE_ERSPAN_VER]) {
1727 		ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1728 		if (ver != 1 && ver != 2)
1729 			return -EINVAL;
1730 	}
1731 
1732 	if (ver == 1) {
1733 		if (data[IFLA_GRE_ERSPAN_INDEX]) {
1734 			u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1735 
1736 			if (index & ~INDEX_MASK)
1737 				return -EINVAL;
1738 		}
1739 	} else if (ver == 2) {
1740 		if (data[IFLA_GRE_ERSPAN_DIR]) {
1741 			u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1742 
1743 			if (dir & ~(DIR_MASK >> DIR_OFFSET))
1744 				return -EINVAL;
1745 		}
1746 
1747 		if (data[IFLA_GRE_ERSPAN_HWID]) {
1748 			u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1749 
1750 			if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1751 				return -EINVAL;
1752 		}
1753 	}
1754 
1755 	return 0;
1756 }
1757 
1758 static void ip6erspan_set_version(struct nlattr *data[],
1759 				  struct __ip6_tnl_parm *parms)
1760 {
1761 	if (!data)
1762 		return;
1763 
1764 	parms->erspan_ver = 1;
1765 	if (data[IFLA_GRE_ERSPAN_VER])
1766 		parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1767 
1768 	if (parms->erspan_ver == 1) {
1769 		if (data[IFLA_GRE_ERSPAN_INDEX])
1770 			parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1771 	} else if (parms->erspan_ver == 2) {
1772 		if (data[IFLA_GRE_ERSPAN_DIR])
1773 			parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1774 		if (data[IFLA_GRE_ERSPAN_HWID])
1775 			parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1776 	}
1777 }
1778 
1779 static void ip6gre_netlink_parms(struct nlattr *data[],
1780 				struct __ip6_tnl_parm *parms)
1781 {
1782 	memset(parms, 0, sizeof(*parms));
1783 
1784 	if (!data)
1785 		return;
1786 
1787 	if (data[IFLA_GRE_LINK])
1788 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1789 
1790 	if (data[IFLA_GRE_IFLAGS])
1791 		gre_flags_to_tnl_flags(parms->i_flags,
1792 				       nla_get_be16(data[IFLA_GRE_IFLAGS]));
1793 
1794 	if (data[IFLA_GRE_OFLAGS])
1795 		gre_flags_to_tnl_flags(parms->o_flags,
1796 				       nla_get_be16(data[IFLA_GRE_OFLAGS]));
1797 
1798 	if (data[IFLA_GRE_IKEY])
1799 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1800 
1801 	if (data[IFLA_GRE_OKEY])
1802 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1803 
1804 	if (data[IFLA_GRE_LOCAL])
1805 		parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1806 
1807 	if (data[IFLA_GRE_REMOTE])
1808 		parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1809 
1810 	if (data[IFLA_GRE_TTL])
1811 		parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1812 
1813 	if (data[IFLA_GRE_ENCAP_LIMIT])
1814 		parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1815 
1816 	if (data[IFLA_GRE_FLOWINFO])
1817 		parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1818 
1819 	if (data[IFLA_GRE_FLAGS])
1820 		parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1821 
1822 	if (data[IFLA_GRE_FWMARK])
1823 		parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1824 
1825 	if (data[IFLA_GRE_COLLECT_METADATA])
1826 		parms->collect_md = true;
1827 }
1828 
1829 static int ip6gre_tap_init(struct net_device *dev)
1830 {
1831 	int ret;
1832 
1833 	ret = ip6gre_tunnel_init_common(dev);
1834 	if (ret)
1835 		return ret;
1836 
1837 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1838 
1839 	return 0;
1840 }
1841 
1842 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1843 	.ndo_init = ip6gre_tap_init,
1844 	.ndo_uninit = ip6gre_tunnel_uninit,
1845 	.ndo_start_xmit = ip6gre_tunnel_xmit,
1846 	.ndo_set_mac_address = eth_mac_addr,
1847 	.ndo_validate_addr = eth_validate_addr,
1848 	.ndo_change_mtu = ip6_tnl_change_mtu,
1849 	.ndo_get_iflink = ip6_tnl_get_iflink,
1850 };
1851 
1852 static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
1853 {
1854 	int t_hlen;
1855 
1856 	tunnel->tun_hlen = 8;
1857 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1858 		       erspan_hdr_len(tunnel->parms.erspan_ver);
1859 
1860 	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1861 	tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1862 	return t_hlen;
1863 }
1864 
1865 static int ip6erspan_tap_init(struct net_device *dev)
1866 {
1867 	struct ip6_tnl *tunnel;
1868 	int t_hlen;
1869 	int ret;
1870 
1871 	tunnel = netdev_priv(dev);
1872 
1873 	tunnel->dev = dev;
1874 	strscpy(tunnel->parms.name, dev->name);
1875 
1876 	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1877 	if (ret)
1878 		return ret;
1879 
1880 	ret = gro_cells_init(&tunnel->gro_cells, dev);
1881 	if (ret)
1882 		goto cleanup_dst_cache_init;
1883 
1884 	t_hlen = ip6erspan_calc_hlen(tunnel);
1885 	dev->mtu = ETH_DATA_LEN - t_hlen;
1886 	if (dev->type == ARPHRD_ETHER)
1887 		dev->mtu -= ETH_HLEN;
1888 	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1889 		dev->mtu -= 8;
1890 
1891 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1892 	ip6erspan_tnl_link_config(tunnel, 1);
1893 
1894 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1895 	netdev_lockdep_set_classes(dev);
1896 	return 0;
1897 
1898 cleanup_dst_cache_init:
1899 	dst_cache_destroy(&tunnel->dst_cache);
1900 	return ret;
1901 }
1902 
1903 static const struct net_device_ops ip6erspan_netdev_ops = {
1904 	.ndo_init =		ip6erspan_tap_init,
1905 	.ndo_uninit =		ip6erspan_tunnel_uninit,
1906 	.ndo_start_xmit =	ip6erspan_tunnel_xmit,
1907 	.ndo_set_mac_address =	eth_mac_addr,
1908 	.ndo_validate_addr =	eth_validate_addr,
1909 	.ndo_change_mtu =	ip6_tnl_change_mtu,
1910 	.ndo_get_iflink =	ip6_tnl_get_iflink,
1911 };
1912 
1913 static void ip6gre_tap_setup(struct net_device *dev)
1914 {
1915 
1916 	ether_setup(dev);
1917 
1918 	dev->max_mtu = 0;
1919 	dev->netdev_ops = &ip6gre_tap_netdev_ops;
1920 	dev->needs_free_netdev = true;
1921 	dev->priv_destructor = ip6gre_dev_free;
1922 
1923 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1924 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1925 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1926 	netif_keep_dst(dev);
1927 }
1928 
1929 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1930 				       struct ip_tunnel_encap *ipencap)
1931 {
1932 	bool ret = false;
1933 
1934 	memset(ipencap, 0, sizeof(*ipencap));
1935 
1936 	if (!data)
1937 		return ret;
1938 
1939 	if (data[IFLA_GRE_ENCAP_TYPE]) {
1940 		ret = true;
1941 		ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1942 	}
1943 
1944 	if (data[IFLA_GRE_ENCAP_FLAGS]) {
1945 		ret = true;
1946 		ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1947 	}
1948 
1949 	if (data[IFLA_GRE_ENCAP_SPORT]) {
1950 		ret = true;
1951 		ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1952 	}
1953 
1954 	if (data[IFLA_GRE_ENCAP_DPORT]) {
1955 		ret = true;
1956 		ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1957 	}
1958 
1959 	return ret;
1960 }
1961 
1962 static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev,
1963 				 struct nlattr *tb[], struct nlattr *data[],
1964 				 struct netlink_ext_ack *extack)
1965 {
1966 	struct ip6_tnl *nt;
1967 	struct ip_tunnel_encap ipencap;
1968 	int err;
1969 
1970 	nt = netdev_priv(dev);
1971 
1972 	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1973 		int err = ip6_tnl_encap_setup(nt, &ipencap);
1974 
1975 		if (err < 0)
1976 			return err;
1977 	}
1978 
1979 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1980 		eth_hw_addr_random(dev);
1981 
1982 	nt->dev = dev;
1983 	nt->net = link_net;
1984 
1985 	err = register_netdevice(dev);
1986 	if (err)
1987 		goto out;
1988 
1989 	if (tb[IFLA_MTU])
1990 		ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1991 
1992 out:
1993 	return err;
1994 }
1995 
1996 static int ip6gre_newlink(struct net_device *dev,
1997 			  struct rtnl_newlink_params *params,
1998 			  struct netlink_ext_ack *extack)
1999 {
2000 	struct net *net = params->link_net ? : dev_net(dev);
2001 	struct ip6_tnl *nt = netdev_priv(dev);
2002 	struct nlattr **data = params->data;
2003 	struct nlattr **tb = params->tb;
2004 	struct ip6gre_net *ign;
2005 	int err = 0;
2006 
2007 	ip6gre_netlink_parms(data, &nt->parms);
2008 	ign = net_generic(net, ip6gre_net_id);
2009 
2010 	mutex_lock(&ign->tunnels_lock);
2011 
2012 	if (nt->parms.collect_md) {
2013 		if (rtnl_dereference(ign->collect_md_tun))
2014 			err = -EEXIST;
2015 	} else {
2016 		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2017 			err = -EEXIST;
2018 	}
2019 	if (err)
2020 		goto unlock;
2021 
2022 	err = ip6gre_newlink_common(net, dev, tb, data, extack);
2023 	if (!err) {
2024 		ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
2025 		ip6gre_tunnel_link_md(ign, nt);
2026 		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2027 	}
2028 
2029 unlock:
2030 	mutex_unlock(&ign->tunnels_lock);
2031 
2032 	return err;
2033 }
2034 
2035 static struct ip6_tnl *
2036 ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
2037 			 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
2038 			 struct netlink_ext_ack *extack)
2039 {
2040 	struct ip6_tnl *t, *nt = netdev_priv(dev);
2041 	struct net *net = nt->net;
2042 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2043 	struct ip_tunnel_encap ipencap;
2044 
2045 	if (dev == ign->fb_tunnel_dev)
2046 		return ERR_PTR(-EINVAL);
2047 
2048 	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
2049 		int err = ip6_tnl_encap_setup(nt, &ipencap);
2050 
2051 		if (err < 0)
2052 			return ERR_PTR(err);
2053 	}
2054 
2055 	ip6gre_netlink_parms(data, p_p);
2056 
2057 	t = ip6gre_tunnel_locate(net, p_p, 0);
2058 
2059 	if (t) {
2060 		if (t->dev != dev)
2061 			return ERR_PTR(-EEXIST);
2062 	} else {
2063 		t = nt;
2064 	}
2065 
2066 	if (ip6gre_tunnel_unregistering(t))
2067 		return ERR_PTR(-ENODEV);
2068 
2069 	return t;
2070 }
2071 
2072 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
2073 			     struct nlattr *data[],
2074 			     struct netlink_ext_ack *extack)
2075 {
2076 	struct ip6_tnl *t = netdev_priv(dev);
2077 	struct __ip6_tnl_parm p;
2078 	struct ip6gre_net *ign;
2079 	int err = 0;
2080 
2081 	ign = net_generic(t->net, ip6gre_net_id);
2082 
2083 	if (!rtnl_dev_link_net_capable(dev, t->net))
2084 		return -EPERM;
2085 
2086 	mutex_lock(&ign->tunnels_lock);
2087 
2088 	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2089 	if (IS_ERR(t)) {
2090 		err = PTR_ERR(t);
2091 		goto unlock;
2092 	}
2093 
2094 	ip6gre_tunnel_unlink_md(ign, t);
2095 	ip6gre_tunnel_unlink(ign, t);
2096 	ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
2097 	ip6gre_tunnel_link_md(ign, t);
2098 	ip6gre_tunnel_link(ign, t);
2099 unlock:
2100 	mutex_unlock(&ign->tunnels_lock);
2101 
2102 	return err;
2103 }
2104 
2105 static void __ip6gre_dellink(struct net *net, struct net_device *dev,
2106 			     struct list_head *head)
2107 {
2108 	struct ip6_tnl *t = netdev_priv(dev);
2109 	struct ip6gre_net *ign;
2110 
2111 	ign = net_generic(t->net, ip6gre_net_id);
2112 
2113 	if (dev->rtnl_link_ops == &ip6erspan_tap_ops)
2114 		ip6erspan_tunnel_unlink_md(ign, t);
2115 	else
2116 		ip6gre_tunnel_unlink_md(ign, t);
2117 
2118 	ip6gre_tunnel_unlink(ign, t);
2119 	unregister_netdevice_queue_net(net, dev, head);
2120 }
2121 
2122 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
2123 {
2124 	struct ip6_tnl *t = netdev_priv(dev);
2125 	struct ip6gre_net *ign;
2126 
2127 	ign = net_generic(t->net, ip6gre_net_id);
2128 
2129 	mutex_lock(&ign->tunnels_lock);
2130 
2131 	if (dev != ign->fb_tunnel_dev &&
2132 	    !ip6gre_tunnel_unregistering(t))
2133 		__ip6gre_dellink(dev_net(dev), dev, head);
2134 
2135 	mutex_unlock(&ign->tunnels_lock);
2136 }
2137 
2138 static size_t ip6gre_get_size(const struct net_device *dev)
2139 {
2140 	return
2141 		/* IFLA_GRE_LINK */
2142 		nla_total_size(4) +
2143 		/* IFLA_GRE_IFLAGS */
2144 		nla_total_size(2) +
2145 		/* IFLA_GRE_OFLAGS */
2146 		nla_total_size(2) +
2147 		/* IFLA_GRE_IKEY */
2148 		nla_total_size(4) +
2149 		/* IFLA_GRE_OKEY */
2150 		nla_total_size(4) +
2151 		/* IFLA_GRE_LOCAL */
2152 		nla_total_size(sizeof(struct in6_addr)) +
2153 		/* IFLA_GRE_REMOTE */
2154 		nla_total_size(sizeof(struct in6_addr)) +
2155 		/* IFLA_GRE_TTL */
2156 		nla_total_size(1) +
2157 		/* IFLA_GRE_ENCAP_LIMIT */
2158 		nla_total_size(1) +
2159 		/* IFLA_GRE_FLOWINFO */
2160 		nla_total_size(4) +
2161 		/* IFLA_GRE_FLAGS */
2162 		nla_total_size(4) +
2163 		/* IFLA_GRE_ENCAP_TYPE */
2164 		nla_total_size(2) +
2165 		/* IFLA_GRE_ENCAP_FLAGS */
2166 		nla_total_size(2) +
2167 		/* IFLA_GRE_ENCAP_SPORT */
2168 		nla_total_size(2) +
2169 		/* IFLA_GRE_ENCAP_DPORT */
2170 		nla_total_size(2) +
2171 		/* IFLA_GRE_COLLECT_METADATA */
2172 		nla_total_size(0) +
2173 		/* IFLA_GRE_FWMARK */
2174 		nla_total_size(4) +
2175 		/* IFLA_GRE_ERSPAN_INDEX */
2176 		nla_total_size(4) +
2177 		0;
2178 }
2179 
2180 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
2181 {
2182 	const struct ip6_tnl *t = netdev_priv(dev);
2183 	const struct __ip6_tnl_parm *p = &t->parms;
2184 	IP_TUNNEL_DECLARE_FLAGS(o_flags);
2185 
2186 	ip_tunnel_flags_copy(o_flags, p->o_flags);
2187 
2188 	if (p->erspan_ver == 1 || p->erspan_ver == 2) {
2189 		if (!p->collect_md)
2190 			__set_bit(IP_TUNNEL_KEY_BIT, o_flags);
2191 
2192 		if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2193 			goto nla_put_failure;
2194 
2195 		if (p->erspan_ver == 1) {
2196 			if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2197 				goto nla_put_failure;
2198 		} else {
2199 			if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2200 				goto nla_put_failure;
2201 			if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2202 				goto nla_put_failure;
2203 		}
2204 	}
2205 
2206 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
2207 	    nla_put_be16(skb, IFLA_GRE_IFLAGS,
2208 			 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
2209 	    nla_put_be16(skb, IFLA_GRE_OFLAGS,
2210 			 gre_tnl_flags_to_gre_flags(o_flags)) ||
2211 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
2212 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
2213 	    nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
2214 	    nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
2215 	    nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
2216 	    nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
2217 	    nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
2218 	    nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
2219 	    nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
2220 		goto nla_put_failure;
2221 
2222 	if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2223 			t->encap.type) ||
2224 	    nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2225 			 t->encap.sport) ||
2226 	    nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2227 			 t->encap.dport) ||
2228 	    nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2229 			t->encap.flags))
2230 		goto nla_put_failure;
2231 
2232 	if (p->collect_md) {
2233 		if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2234 			goto nla_put_failure;
2235 	}
2236 
2237 	return 0;
2238 
2239 nla_put_failure:
2240 	return -EMSGSIZE;
2241 }
2242 
2243 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2244 	[IFLA_GRE_LINK]        = { .type = NLA_U32 },
2245 	[IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
2246 	[IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
2247 	[IFLA_GRE_IKEY]        = { .type = NLA_U32 },
2248 	[IFLA_GRE_OKEY]        = { .type = NLA_U32 },
2249 	[IFLA_GRE_LOCAL]       = { .len = sizeof_field(struct ipv6hdr, saddr) },
2250 	[IFLA_GRE_REMOTE]      = { .len = sizeof_field(struct ipv6hdr, daddr) },
2251 	[IFLA_GRE_TTL]         = { .type = NLA_U8 },
2252 	[IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2253 	[IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
2254 	[IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
2255 	[IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
2256 	[IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
2257 	[IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
2258 	[IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
2259 	[IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
2260 	[IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
2261 	[IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
2262 	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
2263 	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
2264 	[IFLA_GRE_ERSPAN_HWID]	= { .type = NLA_U16 },
2265 };
2266 
2267 static void ip6erspan_tap_setup(struct net_device *dev)
2268 {
2269 	ether_setup(dev);
2270 
2271 	dev->max_mtu = 0;
2272 	dev->netdev_ops = &ip6erspan_netdev_ops;
2273 	dev->needs_free_netdev = true;
2274 	dev->priv_destructor = ip6gre_dev_free;
2275 
2276 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2277 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2278 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2279 	netif_keep_dst(dev);
2280 }
2281 
2282 static int ip6erspan_newlink(struct net_device *dev,
2283 			     struct rtnl_newlink_params *params,
2284 			     struct netlink_ext_ack *extack)
2285 {
2286 	struct net *net = params->link_net ? : dev_net(dev);
2287 	struct ip6_tnl *nt = netdev_priv(dev);
2288 	struct nlattr **data = params->data;
2289 	struct nlattr **tb = params->tb;
2290 	struct ip6gre_net *ign;
2291 	int err = 0;
2292 
2293 	ip6gre_netlink_parms(data, &nt->parms);
2294 	ip6erspan_set_version(data, &nt->parms);
2295 	ign = net_generic(net, ip6gre_net_id);
2296 
2297 	mutex_lock(&ign->tunnels_lock);
2298 
2299 	if (nt->parms.collect_md) {
2300 		if (rtnl_dereference(ign->collect_md_tun_erspan))
2301 			err = -EEXIST;
2302 	} else {
2303 		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2304 			err = -EEXIST;
2305 	}
2306 	if (err)
2307 		goto unlock;
2308 
2309 	err = ip6gre_newlink_common(net, dev, tb, data, extack);
2310 	if (!err) {
2311 		ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2312 		ip6erspan_tunnel_link_md(ign, nt);
2313 		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2314 	}
2315 
2316 unlock:
2317 	mutex_unlock(&ign->tunnels_lock);
2318 
2319 	return err;
2320 }
2321 
2322 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
2323 {
2324 	ip6gre_tnl_link_config_common(t);
2325 	ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
2326 }
2327 
2328 static int ip6erspan_tnl_change(struct ip6_tnl *t,
2329 				const struct __ip6_tnl_parm *p, int set_mtu)
2330 {
2331 	ip6gre_tnl_copy_tnl_parm(t, p);
2332 	ip6erspan_tnl_link_config(t, set_mtu);
2333 	return 0;
2334 }
2335 
2336 static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
2337 				struct nlattr *data[],
2338 				struct netlink_ext_ack *extack)
2339 {
2340 	struct ip6_tnl *t = netdev_priv(dev);
2341 	struct __ip6_tnl_parm p;
2342 	struct ip6gre_net *ign;
2343 	int err = 0;
2344 
2345 	if (!rtnl_dev_link_net_capable(dev, t->net))
2346 		return -EPERM;
2347 
2348 	ign = net_generic(t->net, ip6gre_net_id);
2349 
2350 	mutex_lock(&ign->tunnels_lock);
2351 
2352 	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2353 	if (IS_ERR(t)) {
2354 		err = PTR_ERR(t);
2355 		goto unlock;
2356 	}
2357 
2358 	ip6erspan_set_version(data, &p);
2359 	ip6erspan_tunnel_unlink_md(ign, t);
2360 	ip6gre_tunnel_unlink(ign, t);
2361 	ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2362 	ip6erspan_tunnel_link_md(ign, t);
2363 	ip6gre_tunnel_link(ign, t);
2364 unlock:
2365 	mutex_unlock(&ign->tunnels_lock);
2366 
2367 	return err;
2368 }
2369 
2370 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2371 	.kind		= "ip6gre",
2372 	.maxtype	= IFLA_GRE_MAX,
2373 	.policy		= ip6gre_policy,
2374 	.priv_size	= sizeof(struct ip6_tnl),
2375 	.setup		= ip6gre_tunnel_setup,
2376 	.validate	= ip6gre_tunnel_validate,
2377 	.newlink	= ip6gre_newlink,
2378 	.changelink	= ip6gre_changelink,
2379 	.dellink	= ip6gre_dellink,
2380 	.get_size	= ip6gre_get_size,
2381 	.fill_info	= ip6gre_fill_info,
2382 	.get_link_net	= ip6_tnl_get_link_net,
2383 };
2384 
2385 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2386 	.kind		= "ip6gretap",
2387 	.maxtype	= IFLA_GRE_MAX,
2388 	.policy		= ip6gre_policy,
2389 	.priv_size	= sizeof(struct ip6_tnl),
2390 	.setup		= ip6gre_tap_setup,
2391 	.validate	= ip6gre_tap_validate,
2392 	.newlink	= ip6gre_newlink,
2393 	.changelink	= ip6gre_changelink,
2394 	.dellink	= ip6gre_dellink,
2395 	.get_size	= ip6gre_get_size,
2396 	.fill_info	= ip6gre_fill_info,
2397 	.get_link_net	= ip6_tnl_get_link_net,
2398 };
2399 
2400 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2401 	.kind		= "ip6erspan",
2402 	.maxtype	= IFLA_GRE_MAX,
2403 	.policy		= ip6gre_policy,
2404 	.priv_size	= sizeof(struct ip6_tnl),
2405 	.setup		= ip6erspan_tap_setup,
2406 	.validate	= ip6erspan_tap_validate,
2407 	.newlink	= ip6erspan_newlink,
2408 	.changelink	= ip6erspan_changelink,
2409 	.dellink	= ip6gre_dellink,
2410 	.get_size	= ip6gre_get_size,
2411 	.fill_info	= ip6gre_fill_info,
2412 	.get_link_net	= ip6_tnl_get_link_net,
2413 };
2414 
2415 /*
2416  *	And now the modules code and kernel interface.
2417  */
2418 
2419 static int __init ip6gre_init(void)
2420 {
2421 	int err;
2422 
2423 	pr_info("GRE over IPv6 tunneling driver\n");
2424 
2425 	err = register_pernet_device(&ip6gre_net_ops);
2426 	if (err < 0)
2427 		return err;
2428 
2429 	err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2430 	if (err < 0) {
2431 		pr_info("%s: can't add protocol\n", __func__);
2432 		goto add_proto_failed;
2433 	}
2434 
2435 	err = rtnl_link_register(&ip6gre_link_ops);
2436 	if (err < 0)
2437 		goto rtnl_link_failed;
2438 
2439 	err = rtnl_link_register(&ip6gre_tap_ops);
2440 	if (err < 0)
2441 		goto tap_ops_failed;
2442 
2443 	err = rtnl_link_register(&ip6erspan_tap_ops);
2444 	if (err < 0)
2445 		goto erspan_link_failed;
2446 
2447 out:
2448 	return err;
2449 
2450 erspan_link_failed:
2451 	rtnl_link_unregister(&ip6gre_tap_ops);
2452 tap_ops_failed:
2453 	rtnl_link_unregister(&ip6gre_link_ops);
2454 rtnl_link_failed:
2455 	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2456 add_proto_failed:
2457 	unregister_pernet_device(&ip6gre_net_ops);
2458 	goto out;
2459 }
2460 
2461 static void __exit ip6gre_fini(void)
2462 {
2463 	rtnl_link_unregister(&ip6gre_tap_ops);
2464 	rtnl_link_unregister(&ip6gre_link_ops);
2465 	rtnl_link_unregister(&ip6erspan_tap_ops);
2466 	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2467 	unregister_pernet_device(&ip6gre_net_ops);
2468 }
2469 
2470 module_init(ip6gre_init);
2471 module_exit(ip6gre_fini);
2472 MODULE_LICENSE("GPL");
2473 MODULE_AUTHOR("D. Kozlov <xeb@mail.ru>");
2474 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2475 MODULE_ALIAS_RTNL_LINK("ip6gre");
2476 MODULE_ALIAS_RTNL_LINK("ip6gretap");
2477 MODULE_ALIAS_RTNL_LINK("ip6erspan");
2478 MODULE_ALIAS_NETDEV("ip6gre0");
2479