xref: /linux/net/ipv6/ip6_gre.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
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) < 0)
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 	if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
832 		return -1;
833 
834 	if (!t->parms.collect_md &&
835 	    prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
836 		return -1;
837 
838 	if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
839 					      t->parms.o_flags)))
840 		return -1;
841 
842 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
843 			  &mtu, skb->protocol);
844 	if (err != 0) {
845 		if (err == -EMSGSIZE)
846 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
847 		return -1;
848 	}
849 
850 	return 0;
851 }
852 
853 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
854 {
855 	struct ip6_tnl *t = netdev_priv(dev);
856 	int encap_limit = -1;
857 	struct flowi6 fl6;
858 	__u8 dsfield = 0;
859 	__u32 mtu;
860 	int err;
861 
862 	if (!t->parms.collect_md &&
863 	    prepare_ip6gre_xmit_other(skb, dev, &fl6, &dsfield, &encap_limit))
864 		return -1;
865 
866 	err = gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
867 						t->parms.o_flags));
868 	if (err)
869 		return err;
870 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, skb->protocol);
871 
872 	return err;
873 }
874 
875 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
876 	struct net_device *dev)
877 {
878 	struct ip_tunnel_info *tun_info = NULL;
879 	struct ip6_tnl *t = netdev_priv(dev);
880 	__be16 payload_protocol;
881 	int ret;
882 
883 	if (!pskb_inet_may_pull(skb))
884 		goto tx_err;
885 
886 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
887 		goto tx_err;
888 
889 	if (t->parms.collect_md)
890 		tun_info = skb_tunnel_info_txcheck(skb);
891 
892 	payload_protocol = skb_protocol(skb, true);
893 	switch (payload_protocol) {
894 	case htons(ETH_P_IP):
895 		ret = ip6gre_xmit_ipv4(skb, dev);
896 		break;
897 	case htons(ETH_P_IPV6):
898 		ret = ip6gre_xmit_ipv6(skb, dev);
899 		break;
900 	default:
901 		ret = ip6gre_xmit_other(skb, dev);
902 		break;
903 	}
904 
905 	if (ret < 0)
906 		goto tx_err;
907 
908 	return NETDEV_TX_OK;
909 
910 tx_err:
911 	if (!IS_ERR(tun_info))
912 		DEV_STATS_INC(dev, tx_errors);
913 	DEV_STATS_INC(dev, tx_dropped);
914 	kfree_skb(skb);
915 	return NETDEV_TX_OK;
916 }
917 
918 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
919 					 struct net_device *dev)
920 {
921 	struct ip_tunnel_info *tun_info = NULL;
922 	struct ip6_tnl *t = netdev_priv(dev);
923 	struct dst_entry *dst = skb_dst(skb);
924 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
925 	bool truncate = false;
926 	int encap_limit = -1;
927 	__u8 dsfield = false;
928 	struct flowi6 fl6;
929 	int err = -EINVAL;
930 	__be16 proto;
931 	__u32 mtu;
932 	int nhoff;
933 
934 	if (!pskb_inet_may_pull(skb))
935 		goto tx_err;
936 
937 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
938 		goto tx_err;
939 
940 	if (gre_handle_offloads(skb, false))
941 		goto tx_err;
942 
943 	if (skb->len > dev->mtu + dev->hard_header_len) {
944 		if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
945 			goto tx_err;
946 		truncate = true;
947 	}
948 
949 	nhoff = skb_network_offset(skb);
950 	if (skb->protocol == htons(ETH_P_IP) &&
951 	    (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
952 		truncate = true;
953 
954 	if (skb->protocol == htons(ETH_P_IPV6)) {
955 		int thoff;
956 
957 		if (skb_transport_header_was_set(skb))
958 			thoff = skb_transport_offset(skb);
959 		else
960 			thoff = nhoff + sizeof(struct ipv6hdr);
961 		if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
962 			truncate = true;
963 	}
964 
965 	if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
966 		goto tx_err;
967 
968 	IPCB(skb)->flags = 0;
969 
970 	/* For collect_md mode, derive fl6 from the tunnel key,
971 	 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
972 	 */
973 	if (t->parms.collect_md) {
974 		const struct ip_tunnel_key *key;
975 		struct erspan_metadata *md;
976 		__be32 tun_id;
977 
978 		tun_info = skb_tunnel_info_txcheck(skb);
979 		if (IS_ERR(tun_info) ||
980 		    unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
981 			goto tx_err;
982 
983 		key = &tun_info->key;
984 		memset(&fl6, 0, sizeof(fl6));
985 		fl6.flowi6_proto = IPPROTO_GRE;
986 		fl6.daddr = key->u.ipv6.dst;
987 		fl6.flowlabel = key->label;
988 		fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
989 		fl6.fl6_gre_key = tunnel_id_to_key32(key->tun_id);
990 
991 		dsfield = key->tos;
992 		if (!test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
993 			      tun_info->key.tun_flags))
994 			goto tx_err;
995 		if (tun_info->options_len < sizeof(*md))
996 			goto tx_err;
997 		md = ip_tunnel_info_opts(tun_info);
998 
999 		tun_id = tunnel_id_to_key32(key->tun_id);
1000 		if (md->version == 1) {
1001 			erspan_build_header(skb,
1002 					    ntohl(tun_id),
1003 					    ntohl(md->u.index), truncate,
1004 					    false);
1005 			proto = htons(ETH_P_ERSPAN);
1006 		} else if (md->version == 2) {
1007 			erspan_build_header_v2(skb,
1008 					       ntohl(tun_id),
1009 					       md->u.md2.dir,
1010 					       get_hwid(&md->u.md2),
1011 					       truncate, false);
1012 			proto = htons(ETH_P_ERSPAN2);
1013 		} else {
1014 			goto tx_err;
1015 		}
1016 	} else {
1017 		switch (skb->protocol) {
1018 		case htons(ETH_P_IP):
1019 			memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1020 			prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
1021 						 &dsfield, &encap_limit);
1022 			break;
1023 		case htons(ETH_P_IPV6):
1024 			if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
1025 				goto tx_err;
1026 			if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
1027 						     &dsfield, &encap_limit))
1028 				goto tx_err;
1029 			break;
1030 		default:
1031 			memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1032 			break;
1033 		}
1034 
1035 		if (t->parms.erspan_ver == 1) {
1036 			erspan_build_header(skb, ntohl(t->parms.o_key),
1037 					    t->parms.index,
1038 					    truncate, false);
1039 			proto = htons(ETH_P_ERSPAN);
1040 		} else if (t->parms.erspan_ver == 2) {
1041 			erspan_build_header_v2(skb, ntohl(t->parms.o_key),
1042 					       t->parms.dir,
1043 					       t->parms.hwid,
1044 					       truncate, false);
1045 			proto = htons(ETH_P_ERSPAN2);
1046 		} else {
1047 			goto tx_err;
1048 		}
1049 
1050 		fl6.daddr = t->parms.raddr;
1051 	}
1052 
1053 	/* Push GRE header. */
1054 	__set_bit(IP_TUNNEL_SEQ_BIT, flags);
1055 	gre_build_header(skb, 8, flags, proto, 0,
1056 			 htonl(atomic_fetch_inc(&t->o_seqno)));
1057 
1058 	/* TooBig packet may have updated dst->dev's mtu */
1059 	if (!t->parms.collect_md && dst) {
1060 		mtu = READ_ONCE(dst_dev(dst)->mtu);
1061 		if (dst_mtu(dst) > mtu)
1062 			dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
1063 	}
1064 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1065 			   NEXTHDR_GRE);
1066 	if (err != 0) {
1067 		/* XXX: send ICMP error even if DF is not set. */
1068 		if (err == -EMSGSIZE) {
1069 			if (skb->protocol == htons(ETH_P_IP))
1070 				icmp_ndo_send(skb, ICMP_DEST_UNREACH,
1071 					      ICMP_FRAG_NEEDED, htonl(mtu));
1072 			else
1073 				icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1074 		}
1075 
1076 		goto tx_err;
1077 	}
1078 	return NETDEV_TX_OK;
1079 
1080 tx_err:
1081 	if (!IS_ERR(tun_info))
1082 		DEV_STATS_INC(dev, tx_errors);
1083 	DEV_STATS_INC(dev, tx_dropped);
1084 	kfree_skb(skb);
1085 	return NETDEV_TX_OK;
1086 }
1087 
1088 static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
1089 {
1090 	struct net_device *dev = t->dev;
1091 	struct __ip6_tnl_parm *p = &t->parms;
1092 	struct flowi6 *fl6 = &t->fl.u.ip6;
1093 
1094 	if (dev->type != ARPHRD_ETHER) {
1095 		__dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
1096 		memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1097 	}
1098 
1099 	/* Set up flowi template */
1100 	fl6->saddr = p->laddr;
1101 	fl6->daddr = p->raddr;
1102 	fl6->flowi6_oif = p->link;
1103 	fl6->flowlabel = 0;
1104 	fl6->flowi6_proto = IPPROTO_GRE;
1105 	fl6->fl6_gre_key = t->parms.o_key;
1106 
1107 	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1108 		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1109 	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1110 		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1111 
1112 	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1113 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1114 
1115 	if (p->flags&IP6_TNL_F_CAP_XMIT &&
1116 			p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1117 		dev->flags |= IFF_POINTOPOINT;
1118 	else
1119 		dev->flags &= ~IFF_POINTOPOINT;
1120 }
1121 
1122 static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
1123 					 int t_hlen)
1124 {
1125 	const struct __ip6_tnl_parm *p = &t->parms;
1126 	struct net_device *dev = t->dev;
1127 
1128 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
1129 		int strict = (ipv6_addr_type(&p->raddr) &
1130 			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1131 
1132 		struct rt6_info *rt = rt6_lookup(t->net,
1133 						 &p->raddr, &p->laddr,
1134 						 p->link, NULL, strict);
1135 
1136 		if (!rt)
1137 			return;
1138 
1139 		if (rt->dst.dev) {
1140 			unsigned int headroom;
1141 
1142 			headroom = rt->dst.dev->hard_header_len + t_hlen;
1143 			headroom = ip_tunnel_limit_headroom(headroom);
1144 			dev->needed_headroom = headroom;
1145 
1146 			if (set_mtu) {
1147 				int mtu = rt->dst.dev->mtu - t_hlen;
1148 
1149 				if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1150 					mtu -= 8;
1151 				if (dev->type == ARPHRD_ETHER)
1152 					mtu -= ETH_HLEN;
1153 
1154 				if (mtu < IPV6_MIN_MTU)
1155 					mtu = IPV6_MIN_MTU;
1156 				WRITE_ONCE(dev->mtu, mtu);
1157 			}
1158 		}
1159 		ip6_rt_put(rt);
1160 	}
1161 }
1162 
1163 static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
1164 {
1165 	int t_hlen;
1166 
1167 	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1168 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1169 
1170 	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1171 
1172 	if (tunnel->dev->header_ops && tunnel->dev->type == ARPHRD_IP6GRE)
1173 		tunnel->dev->hard_header_len = t_hlen;
1174 	else
1175 		tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1176 
1177 	return t_hlen;
1178 }
1179 
1180 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1181 {
1182 	ip6gre_tnl_link_config_common(t);
1183 	ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
1184 }
1185 
1186 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
1187 				     const struct __ip6_tnl_parm *p)
1188 {
1189 	t->parms.laddr = p->laddr;
1190 	t->parms.raddr = p->raddr;
1191 	t->parms.flags = p->flags;
1192 	t->parms.hop_limit = p->hop_limit;
1193 	t->parms.encap_limit = p->encap_limit;
1194 	t->parms.flowinfo = p->flowinfo;
1195 	t->parms.link = p->link;
1196 	t->parms.proto = p->proto;
1197 	t->parms.i_key = p->i_key;
1198 	t->parms.o_key = p->o_key;
1199 	ip_tunnel_flags_copy(t->parms.i_flags, p->i_flags);
1200 	ip_tunnel_flags_copy(t->parms.o_flags, p->o_flags);
1201 	t->parms.fwmark = p->fwmark;
1202 	t->parms.erspan_ver = p->erspan_ver;
1203 	t->parms.index = p->index;
1204 	t->parms.dir = p->dir;
1205 	t->parms.hwid = p->hwid;
1206 	dst_cache_reset(&t->dst_cache);
1207 }
1208 
1209 static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
1210 			     int set_mtu)
1211 {
1212 	ip6gre_tnl_copy_tnl_parm(t, p);
1213 	ip6gre_tnl_link_config(t, set_mtu);
1214 	return 0;
1215 }
1216 
1217 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1218 	const struct ip6_tnl_parm2 *u)
1219 {
1220 	p->laddr = u->laddr;
1221 	p->raddr = u->raddr;
1222 	p->flags = u->flags;
1223 	p->hop_limit = u->hop_limit;
1224 	p->encap_limit = u->encap_limit;
1225 	p->flowinfo = u->flowinfo;
1226 	p->link = u->link;
1227 	p->i_key = u->i_key;
1228 	p->o_key = u->o_key;
1229 	gre_flags_to_tnl_flags(p->i_flags, u->i_flags);
1230 	gre_flags_to_tnl_flags(p->o_flags, u->o_flags);
1231 	memcpy(p->name, u->name, sizeof(u->name));
1232 }
1233 
1234 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1235 	const struct __ip6_tnl_parm *p)
1236 {
1237 	u->proto = IPPROTO_GRE;
1238 	u->laddr = p->laddr;
1239 	u->raddr = p->raddr;
1240 	u->flags = p->flags;
1241 	u->hop_limit = p->hop_limit;
1242 	u->encap_limit = p->encap_limit;
1243 	u->flowinfo = p->flowinfo;
1244 	u->link = p->link;
1245 	u->i_key = p->i_key;
1246 	u->o_key = p->o_key;
1247 	u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1248 	u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
1249 	memcpy(u->name, p->name, sizeof(u->name));
1250 }
1251 
1252 static int ip6gre_tunnel_siocdevprivate(struct net_device *dev,
1253 					struct ifreq *ifr, void __user *data,
1254 					int cmd)
1255 {
1256 	struct ip6_tnl *t = netdev_priv(dev);
1257 	struct net *orig_net = dev_net(dev);
1258 	struct __ip6_tnl_parm p1 = {};
1259 	LIST_HEAD(dev_kill_list);
1260 	struct net *net = t->net;
1261 	struct ip6_tnl_parm2 p;
1262 	struct ip6gre_net *ign;
1263 	int err = 0;
1264 
1265 	DEBUG_NET_WARN_ON_ONCE(netdev_need_ops_lock(dev));
1266 
1267 	ign = net_generic(net, ip6gre_net_id);
1268 
1269 	mutex_lock(&ign->tunnels_lock);
1270 
1271 	if (!check_net(net)) {
1272 		err = -EBUSY;
1273 		goto done;
1274 	}
1275 
1276 	switch (cmd) {
1277 	case SIOCGETTUNNEL:
1278 		if (dev == ign->fb_tunnel_dev) {
1279 			if (copy_from_user(&p, data, sizeof(p))) {
1280 				err = -EFAULT;
1281 				break;
1282 			}
1283 			ip6gre_tnl_parm_from_user(&p1, &p);
1284 			t = ip6gre_tunnel_locate(net, &p1, 0);
1285 			if (!t)
1286 				t = netdev_priv(dev);
1287 		}
1288 		memset(&p, 0, sizeof(p));
1289 		ip6gre_tnl_parm_to_user(&p, &t->parms);
1290 		if (copy_to_user(data, &p, sizeof(p)))
1291 			err = -EFAULT;
1292 		break;
1293 
1294 	case SIOCADDTUNNEL:
1295 	case SIOCCHGTUNNEL:
1296 		err = -EPERM;
1297 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1298 			goto done;
1299 
1300 		err = -EFAULT;
1301 		if (copy_from_user(&p, data, sizeof(p)))
1302 			goto done;
1303 
1304 		err = -EINVAL;
1305 		if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1306 			goto done;
1307 
1308 		if (!(p.i_flags&GRE_KEY))
1309 			p.i_key = 0;
1310 		if (!(p.o_flags&GRE_KEY))
1311 			p.o_key = 0;
1312 
1313 		ip6gre_tnl_parm_from_user(&p1, &p);
1314 		t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1315 
1316 		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1317 			if (t && !ip6gre_tunnel_unregistering(t)) {
1318 				if (t->dev != dev) {
1319 					err = -EEXIST;
1320 					break;
1321 				}
1322 			} else {
1323 				t = netdev_priv(dev);
1324 
1325 				if (!ip6gre_tunnel_unregistering(t)) {
1326 					ip6gre_tunnel_unlink(ign, t);
1327 					synchronize_net();
1328 					ip6gre_tnl_change(t, &p1, 1);
1329 					ip6gre_tunnel_link(ign, t);
1330 					netdev_state_change(dev);
1331 				}
1332 			}
1333 		}
1334 
1335 		if (t && !ip6gre_tunnel_unregistering(t)) {
1336 			err = 0;
1337 
1338 			memset(&p, 0, sizeof(p));
1339 			ip6gre_tnl_parm_to_user(&p, &t->parms);
1340 			if (copy_to_user(data, &p, sizeof(p)))
1341 				err = -EFAULT;
1342 		} else {
1343 			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1344 		}
1345 		break;
1346 
1347 	case SIOCDELTUNNEL:
1348 		err = -EPERM;
1349 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1350 			goto done;
1351 
1352 		if (dev == ign->fb_tunnel_dev) {
1353 			err = -EFAULT;
1354 			if (copy_from_user(&p, data, sizeof(p)))
1355 				goto done;
1356 			err = -ENOENT;
1357 			ip6gre_tnl_parm_from_user(&p1, &p);
1358 			t = ip6gre_tunnel_locate(net, &p1, 0);
1359 			if (!t)
1360 				goto done;
1361 			err = -EPERM;
1362 			if (t == netdev_priv(ign->fb_tunnel_dev))
1363 				goto done;
1364 			dev = t->dev;
1365 		}
1366 
1367 		if (!ip6gre_tunnel_unregistering(t))
1368 			__ip6gre_dellink(orig_net, dev, &dev_kill_list);
1369 		err = 0;
1370 		break;
1371 
1372 	default:
1373 		err = -EINVAL;
1374 	}
1375 
1376 done:
1377 	mutex_unlock(&ign->tunnels_lock);
1378 
1379 	unregister_netdevice_many(&dev_kill_list);
1380 	return err;
1381 }
1382 
1383 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1384 			 unsigned short type, const void *daddr,
1385 			 const void *saddr, unsigned int len)
1386 {
1387 	struct ip6_tnl *t = netdev_priv(dev);
1388 	struct ipv6hdr *ipv6h;
1389 	int needed;
1390 	__be16 *p;
1391 
1392 	needed = t->hlen + sizeof(*ipv6h);
1393 	if (skb_headroom(skb) < needed &&
1394 	    pskb_expand_head(skb, HH_DATA_ALIGN(needed - skb_headroom(skb)),
1395 			     0, GFP_ATOMIC))
1396 		return -needed;
1397 
1398 	ipv6h = skb_push(skb, needed);
1399 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1400 						  t->fl.u.ip6.flowlabel,
1401 						  true, &t->fl.u.ip6));
1402 	ipv6h->hop_limit = t->parms.hop_limit;
1403 	ipv6h->nexthdr = NEXTHDR_GRE;
1404 	ipv6h->saddr = t->parms.laddr;
1405 	ipv6h->daddr = t->parms.raddr;
1406 
1407 	p = (__be16 *)(ipv6h + 1);
1408 	p[0] = ip_tunnel_flags_to_be16(t->parms.o_flags);
1409 	p[1] = htons(type);
1410 
1411 	/*
1412 	 *	Set the source hardware address.
1413 	 */
1414 
1415 	if (saddr)
1416 		memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1417 	if (daddr)
1418 		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1419 	if (!ipv6_addr_any(&ipv6h->daddr))
1420 		return t->hlen;
1421 
1422 	return -t->hlen;
1423 }
1424 
1425 static const struct header_ops ip6gre_header_ops = {
1426 	.create	= ip6gre_header,
1427 };
1428 
1429 static const struct net_device_ops ip6gre_netdev_ops = {
1430 	.ndo_init		= ip6gre_tunnel_init,
1431 	.ndo_uninit		= ip6gre_tunnel_uninit,
1432 	.ndo_start_xmit		= ip6gre_tunnel_xmit,
1433 	.ndo_siocdevprivate	= ip6gre_tunnel_siocdevprivate,
1434 	.ndo_change_mtu		= ip6_tnl_change_mtu,
1435 	.ndo_get_iflink		= ip6_tnl_get_iflink,
1436 };
1437 
1438 static void ip6gre_dev_free(struct net_device *dev)
1439 {
1440 	struct ip6_tnl *t = netdev_priv(dev);
1441 
1442 	gro_cells_destroy(&t->gro_cells);
1443 	dst_cache_destroy(&t->dst_cache);
1444 }
1445 
1446 static void ip6gre_tunnel_setup(struct net_device *dev)
1447 {
1448 	dev->netdev_ops = &ip6gre_netdev_ops;
1449 	dev->needs_free_netdev = true;
1450 	dev->priv_destructor = ip6gre_dev_free;
1451 
1452 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1453 	dev->type = ARPHRD_IP6GRE;
1454 
1455 	dev->flags |= IFF_NOARP;
1456 	dev->addr_len = sizeof(struct in6_addr);
1457 	netif_keep_dst(dev);
1458 	/* This perm addr will be used as interface identifier by IPv6 */
1459 	dev->addr_assign_type = NET_ADDR_RANDOM;
1460 	eth_random_addr(dev->perm_addr);
1461 }
1462 
1463 #define GRE6_FEATURES (NETIF_F_SG |		\
1464 		       NETIF_F_FRAGLIST |	\
1465 		       NETIF_F_HIGHDMA |	\
1466 		       NETIF_F_HW_CSUM)
1467 
1468 static void ip6gre_tnl_init_features(struct net_device *dev)
1469 {
1470 	struct ip6_tnl *nt = netdev_priv(dev);
1471 
1472 	dev->features		|= GRE6_FEATURES;
1473 	dev->hw_features	|= GRE6_FEATURES;
1474 
1475 	dev->lltx = true;
1476 
1477 	/* TCP offload with GRE SEQ is not supported, nor can we support 2
1478 	 * levels of outer headers requiring an update.
1479 	 */
1480 	if (test_bit(IP_TUNNEL_SEQ_BIT, nt->parms.o_flags))
1481 		return;
1482 	if (test_bit(IP_TUNNEL_CSUM_BIT, nt->parms.o_flags) &&
1483 	    nt->encap.type != TUNNEL_ENCAP_NONE)
1484 		return;
1485 
1486 	dev->features |= NETIF_F_GSO_SOFTWARE;
1487 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1488 }
1489 
1490 static int ip6gre_tunnel_init_common(struct net_device *dev)
1491 {
1492 	struct ip6_tnl *tunnel;
1493 	int ret;
1494 	int t_hlen;
1495 
1496 	tunnel = netdev_priv(dev);
1497 
1498 	tunnel->dev = dev;
1499 	strscpy(tunnel->parms.name, dev->name);
1500 
1501 	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1502 	if (ret)
1503 		return ret;
1504 
1505 	ret = gro_cells_init(&tunnel->gro_cells, dev);
1506 	if (ret)
1507 		goto cleanup_dst_cache_init;
1508 
1509 	t_hlen = ip6gre_calc_hlen(tunnel);
1510 	dev->mtu = ETH_DATA_LEN - t_hlen;
1511 	if (dev->type == ARPHRD_ETHER)
1512 		dev->mtu -= ETH_HLEN;
1513 	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1514 		dev->mtu -= 8;
1515 
1516 	if (tunnel->parms.collect_md) {
1517 		netif_keep_dst(dev);
1518 	}
1519 	ip6gre_tnl_init_features(dev);
1520 
1521 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1522 	netdev_lockdep_set_classes(dev);
1523 	return 0;
1524 
1525 cleanup_dst_cache_init:
1526 	dst_cache_destroy(&tunnel->dst_cache);
1527 	return ret;
1528 }
1529 
1530 static int ip6gre_tunnel_init(struct net_device *dev)
1531 {
1532 	struct ip6_tnl *tunnel;
1533 	int ret;
1534 
1535 	ret = ip6gre_tunnel_init_common(dev);
1536 	if (ret)
1537 		return ret;
1538 
1539 	tunnel = netdev_priv(dev);
1540 
1541 	if (tunnel->parms.collect_md)
1542 		return 0;
1543 
1544 	__dev_addr_set(dev, &tunnel->parms.laddr, sizeof(struct in6_addr));
1545 	memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1546 
1547 	if (ipv6_addr_any(&tunnel->parms.raddr))
1548 		dev->header_ops = &ip6gre_header_ops;
1549 
1550 	return 0;
1551 }
1552 
1553 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1554 {
1555 	struct ip6_tnl *tunnel = netdev_priv(dev);
1556 
1557 	tunnel->dev = dev;
1558 	tunnel->net = dev_net(dev);
1559 	strscpy(tunnel->parms.name, dev->name);
1560 
1561 	tunnel->hlen		= sizeof(struct ipv6hdr) + 4;
1562 }
1563 
1564 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1565 	.handler     = gre_rcv,
1566 	.err_handler = ip6gre_err,
1567 	.flags       = INET6_PROTO_FINAL,
1568 };
1569 
1570 static void __net_exit ip6gre_exit_rtnl_net(struct net *net,
1571 					    struct list_head *dev_kill_list)
1572 {
1573 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1574 	int prio;
1575 
1576 	WRITE_ONCE(ign->fb_tunnel_dev, NULL);
1577 
1578 	mutex_lock(&ign->tunnels_lock);
1579 
1580 	for (prio = 0; prio < 4; prio++) {
1581 		int h;
1582 
1583 		for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1584 			struct hlist_head *head = &ign->tunnels[prio][h];
1585 			struct hlist_node *tmp;
1586 			struct ip6_tnl *t;
1587 
1588 			hlist_for_each_entry_safe(t, tmp, head, hash_node)
1589 				__ip6gre_dellink(net, t->dev, dev_kill_list);
1590 		}
1591 	}
1592 
1593 	mutex_unlock(&ign->tunnels_lock);
1594 }
1595 
1596 static int __net_init ip6gre_init_net(struct net *net)
1597 {
1598 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1599 	struct net_device *ndev;
1600 	struct ip6_tnl *t;
1601 	int prio, h;
1602 	int err;
1603 
1604 	for (prio = 0; prio < 4; prio++) {
1605 		for (h = 0; h < IP6_GRE_HASH_SIZE; h++)
1606 			INIT_HLIST_HEAD(&ign->tunnels[prio][h]);
1607 	}
1608 
1609 	mutex_init(&ign->tunnels_lock);
1610 
1611 	if (!net_has_fallback_tunnels(net))
1612 		return 0;
1613 	ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1614 			    NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1615 	if (!ndev) {
1616 		err = -ENOMEM;
1617 		goto err_alloc_dev;
1618 	}
1619 	ign->fb_tunnel_dev = ndev;
1620 	dev_net_set(ign->fb_tunnel_dev, net);
1621 	/* FB netdevice is special: we have one, and only one per netns.
1622 	 * Allowing to move it to another netns is clearly unsafe.
1623 	 */
1624 	ign->fb_tunnel_dev->netns_immutable = true;
1625 
1626 	ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1627 	ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1628 
1629 	t = netdev_priv(ign->fb_tunnel_dev);
1630 	hlist_add_head_rcu(&t->hash_node, &ign->tunnels_wc[0]);
1631 
1632 	err = register_netdev(ign->fb_tunnel_dev);
1633 	if (err)
1634 		goto err_reg_dev;
1635 
1636 	return 0;
1637 
1638 err_reg_dev:
1639 	free_netdev(ndev);
1640 err_alloc_dev:
1641 	return err;
1642 }
1643 
1644 static struct pernet_operations ip6gre_net_ops = {
1645 	.init = ip6gre_init_net,
1646 	.exit_rtnl = ip6gre_exit_rtnl_net,
1647 	.id   = &ip6gre_net_id,
1648 	.size = sizeof(struct ip6gre_net),
1649 };
1650 
1651 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1652 				  struct netlink_ext_ack *extack)
1653 {
1654 	__be16 flags;
1655 
1656 	if (!data)
1657 		return 0;
1658 
1659 	flags = 0;
1660 	if (data[IFLA_GRE_IFLAGS])
1661 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1662 	if (data[IFLA_GRE_OFLAGS])
1663 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1664 	if (flags & (GRE_VERSION|GRE_ROUTING))
1665 		return -EINVAL;
1666 
1667 	return 0;
1668 }
1669 
1670 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1671 			       struct netlink_ext_ack *extack)
1672 {
1673 	struct in6_addr daddr;
1674 
1675 	if (tb[IFLA_ADDRESS]) {
1676 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1677 			return -EINVAL;
1678 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1679 			return -EADDRNOTAVAIL;
1680 	}
1681 
1682 	if (!data)
1683 		goto out;
1684 
1685 	if (data[IFLA_GRE_REMOTE]) {
1686 		daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1687 		if (ipv6_addr_any(&daddr))
1688 			return -EINVAL;
1689 	}
1690 
1691 out:
1692 	return ip6gre_tunnel_validate(tb, data, extack);
1693 }
1694 
1695 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1696 				  struct netlink_ext_ack *extack)
1697 {
1698 	__be16 flags = 0;
1699 	int ret, ver = 0;
1700 
1701 	if (!data)
1702 		return 0;
1703 
1704 	ret = ip6gre_tap_validate(tb, data, extack);
1705 	if (ret)
1706 		return ret;
1707 
1708 	/* ERSPAN should only have GRE sequence and key flag */
1709 	if (data[IFLA_GRE_OFLAGS])
1710 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1711 	if (data[IFLA_GRE_IFLAGS])
1712 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1713 	if (!data[IFLA_GRE_COLLECT_METADATA] &&
1714 	    flags != (GRE_SEQ | GRE_KEY))
1715 		return -EINVAL;
1716 
1717 	/* ERSPAN Session ID only has 10-bit. Since we reuse
1718 	 * 32-bit key field as ID, check it's range.
1719 	 */
1720 	if (data[IFLA_GRE_IKEY] &&
1721 	    (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1722 		return -EINVAL;
1723 
1724 	if (data[IFLA_GRE_OKEY] &&
1725 	    (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1726 		return -EINVAL;
1727 
1728 	if (data[IFLA_GRE_ERSPAN_VER]) {
1729 		ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1730 		if (ver != 1 && ver != 2)
1731 			return -EINVAL;
1732 	}
1733 
1734 	if (ver == 1) {
1735 		if (data[IFLA_GRE_ERSPAN_INDEX]) {
1736 			u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1737 
1738 			if (index & ~INDEX_MASK)
1739 				return -EINVAL;
1740 		}
1741 	} else if (ver == 2) {
1742 		if (data[IFLA_GRE_ERSPAN_DIR]) {
1743 			u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1744 
1745 			if (dir & ~(DIR_MASK >> DIR_OFFSET))
1746 				return -EINVAL;
1747 		}
1748 
1749 		if (data[IFLA_GRE_ERSPAN_HWID]) {
1750 			u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1751 
1752 			if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1753 				return -EINVAL;
1754 		}
1755 	}
1756 
1757 	return 0;
1758 }
1759 
1760 static void ip6erspan_set_version(struct nlattr *data[],
1761 				  struct __ip6_tnl_parm *parms)
1762 {
1763 	if (!data)
1764 		return;
1765 
1766 	parms->erspan_ver = 1;
1767 	if (data[IFLA_GRE_ERSPAN_VER])
1768 		parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1769 
1770 	if (parms->erspan_ver == 1) {
1771 		if (data[IFLA_GRE_ERSPAN_INDEX])
1772 			parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1773 	} else if (parms->erspan_ver == 2) {
1774 		if (data[IFLA_GRE_ERSPAN_DIR])
1775 			parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1776 		if (data[IFLA_GRE_ERSPAN_HWID])
1777 			parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1778 	}
1779 }
1780 
1781 static void ip6gre_netlink_parms(struct nlattr *data[],
1782 				struct __ip6_tnl_parm *parms)
1783 {
1784 	memset(parms, 0, sizeof(*parms));
1785 
1786 	if (!data)
1787 		return;
1788 
1789 	if (data[IFLA_GRE_LINK])
1790 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1791 
1792 	if (data[IFLA_GRE_IFLAGS])
1793 		gre_flags_to_tnl_flags(parms->i_flags,
1794 				       nla_get_be16(data[IFLA_GRE_IFLAGS]));
1795 
1796 	if (data[IFLA_GRE_OFLAGS])
1797 		gre_flags_to_tnl_flags(parms->o_flags,
1798 				       nla_get_be16(data[IFLA_GRE_OFLAGS]));
1799 
1800 	if (data[IFLA_GRE_IKEY])
1801 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1802 
1803 	if (data[IFLA_GRE_OKEY])
1804 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1805 
1806 	if (data[IFLA_GRE_LOCAL])
1807 		parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1808 
1809 	if (data[IFLA_GRE_REMOTE])
1810 		parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1811 
1812 	if (data[IFLA_GRE_TTL])
1813 		parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1814 
1815 	if (data[IFLA_GRE_ENCAP_LIMIT])
1816 		parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1817 
1818 	if (data[IFLA_GRE_FLOWINFO])
1819 		parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1820 
1821 	if (data[IFLA_GRE_FLAGS])
1822 		parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1823 
1824 	if (data[IFLA_GRE_FWMARK])
1825 		parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1826 
1827 	if (data[IFLA_GRE_COLLECT_METADATA])
1828 		parms->collect_md = true;
1829 }
1830 
1831 static int ip6gre_tap_init(struct net_device *dev)
1832 {
1833 	int ret;
1834 
1835 	ret = ip6gre_tunnel_init_common(dev);
1836 	if (ret)
1837 		return ret;
1838 
1839 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1840 
1841 	return 0;
1842 }
1843 
1844 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1845 	.ndo_init = ip6gre_tap_init,
1846 	.ndo_uninit = ip6gre_tunnel_uninit,
1847 	.ndo_start_xmit = ip6gre_tunnel_xmit,
1848 	.ndo_set_mac_address = eth_mac_addr,
1849 	.ndo_validate_addr = eth_validate_addr,
1850 	.ndo_change_mtu = ip6_tnl_change_mtu,
1851 	.ndo_get_iflink = ip6_tnl_get_iflink,
1852 };
1853 
1854 static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
1855 {
1856 	int t_hlen;
1857 
1858 	tunnel->tun_hlen = 8;
1859 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1860 		       erspan_hdr_len(tunnel->parms.erspan_ver);
1861 
1862 	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1863 	tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1864 	return t_hlen;
1865 }
1866 
1867 static int ip6erspan_tap_init(struct net_device *dev)
1868 {
1869 	struct ip6_tnl *tunnel;
1870 	int t_hlen;
1871 	int ret;
1872 
1873 	tunnel = netdev_priv(dev);
1874 
1875 	tunnel->dev = dev;
1876 	strscpy(tunnel->parms.name, dev->name);
1877 
1878 	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1879 	if (ret)
1880 		return ret;
1881 
1882 	ret = gro_cells_init(&tunnel->gro_cells, dev);
1883 	if (ret)
1884 		goto cleanup_dst_cache_init;
1885 
1886 	t_hlen = ip6erspan_calc_hlen(tunnel);
1887 	dev->mtu = ETH_DATA_LEN - t_hlen;
1888 	if (dev->type == ARPHRD_ETHER)
1889 		dev->mtu -= ETH_HLEN;
1890 	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1891 		dev->mtu -= 8;
1892 
1893 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1894 	ip6erspan_tnl_link_config(tunnel, 1);
1895 
1896 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1897 	netdev_lockdep_set_classes(dev);
1898 	return 0;
1899 
1900 cleanup_dst_cache_init:
1901 	dst_cache_destroy(&tunnel->dst_cache);
1902 	return ret;
1903 }
1904 
1905 static const struct net_device_ops ip6erspan_netdev_ops = {
1906 	.ndo_init =		ip6erspan_tap_init,
1907 	.ndo_uninit =		ip6erspan_tunnel_uninit,
1908 	.ndo_start_xmit =	ip6erspan_tunnel_xmit,
1909 	.ndo_set_mac_address =	eth_mac_addr,
1910 	.ndo_validate_addr =	eth_validate_addr,
1911 	.ndo_change_mtu =	ip6_tnl_change_mtu,
1912 	.ndo_get_iflink =	ip6_tnl_get_iflink,
1913 };
1914 
1915 static void ip6gre_tap_setup(struct net_device *dev)
1916 {
1917 
1918 	ether_setup(dev);
1919 
1920 	dev->max_mtu = 0;
1921 	dev->netdev_ops = &ip6gre_tap_netdev_ops;
1922 	dev->needs_free_netdev = true;
1923 	dev->priv_destructor = ip6gre_dev_free;
1924 
1925 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1926 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1927 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1928 	netif_keep_dst(dev);
1929 }
1930 
1931 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1932 				       struct ip_tunnel_encap *ipencap)
1933 {
1934 	bool ret = false;
1935 
1936 	memset(ipencap, 0, sizeof(*ipencap));
1937 
1938 	if (!data)
1939 		return ret;
1940 
1941 	if (data[IFLA_GRE_ENCAP_TYPE]) {
1942 		ret = true;
1943 		ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1944 	}
1945 
1946 	if (data[IFLA_GRE_ENCAP_FLAGS]) {
1947 		ret = true;
1948 		ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1949 	}
1950 
1951 	if (data[IFLA_GRE_ENCAP_SPORT]) {
1952 		ret = true;
1953 		ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1954 	}
1955 
1956 	if (data[IFLA_GRE_ENCAP_DPORT]) {
1957 		ret = true;
1958 		ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1959 	}
1960 
1961 	return ret;
1962 }
1963 
1964 static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev,
1965 				 struct nlattr *tb[], struct nlattr *data[],
1966 				 struct netlink_ext_ack *extack)
1967 {
1968 	struct ip6_tnl *nt;
1969 	struct ip_tunnel_encap ipencap;
1970 	int err;
1971 
1972 	nt = netdev_priv(dev);
1973 
1974 	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1975 		int err = ip6_tnl_encap_setup(nt, &ipencap);
1976 
1977 		if (err < 0)
1978 			return err;
1979 	}
1980 
1981 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1982 		eth_hw_addr_random(dev);
1983 
1984 	nt->dev = dev;
1985 	nt->net = link_net;
1986 
1987 	err = register_netdevice(dev);
1988 	if (err)
1989 		goto out;
1990 
1991 	if (tb[IFLA_MTU])
1992 		ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1993 
1994 out:
1995 	return err;
1996 }
1997 
1998 static int ip6gre_newlink(struct net_device *dev,
1999 			  struct rtnl_newlink_params *params,
2000 			  struct netlink_ext_ack *extack)
2001 {
2002 	struct net *net = params->link_net ? : dev_net(dev);
2003 	struct ip6_tnl *nt = netdev_priv(dev);
2004 	struct nlattr **data = params->data;
2005 	struct nlattr **tb = params->tb;
2006 	struct ip6gre_net *ign;
2007 	int err = 0;
2008 
2009 	ip6gre_netlink_parms(data, &nt->parms);
2010 	ign = net_generic(net, ip6gre_net_id);
2011 
2012 	mutex_lock(&ign->tunnels_lock);
2013 
2014 	if (nt->parms.collect_md) {
2015 		if (rtnl_dereference(ign->collect_md_tun))
2016 			err = -EEXIST;
2017 	} else {
2018 		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2019 			err = -EEXIST;
2020 	}
2021 	if (err)
2022 		goto unlock;
2023 
2024 	err = ip6gre_newlink_common(net, dev, tb, data, extack);
2025 	if (!err) {
2026 		ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
2027 		ip6gre_tunnel_link_md(ign, nt);
2028 		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2029 	}
2030 
2031 unlock:
2032 	mutex_unlock(&ign->tunnels_lock);
2033 
2034 	return err;
2035 }
2036 
2037 static struct ip6_tnl *
2038 ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
2039 			 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
2040 			 struct netlink_ext_ack *extack)
2041 {
2042 	struct ip6_tnl *t, *nt = netdev_priv(dev);
2043 	struct net *net = nt->net;
2044 	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2045 	struct ip_tunnel_encap ipencap;
2046 
2047 	if (dev == ign->fb_tunnel_dev)
2048 		return ERR_PTR(-EINVAL);
2049 
2050 	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
2051 		int err = ip6_tnl_encap_setup(nt, &ipencap);
2052 
2053 		if (err < 0)
2054 			return ERR_PTR(err);
2055 	}
2056 
2057 	ip6gre_netlink_parms(data, p_p);
2058 
2059 	t = ip6gre_tunnel_locate(net, p_p, 0);
2060 
2061 	if (t) {
2062 		if (t->dev != dev)
2063 			return ERR_PTR(-EEXIST);
2064 	} else {
2065 		t = nt;
2066 	}
2067 
2068 	if (ip6gre_tunnel_unregistering(t))
2069 		return ERR_PTR(-ENODEV);
2070 
2071 	return t;
2072 }
2073 
2074 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
2075 			     struct nlattr *data[],
2076 			     struct netlink_ext_ack *extack)
2077 {
2078 	struct ip6_tnl *t = netdev_priv(dev);
2079 	struct __ip6_tnl_parm p;
2080 	struct ip6gre_net *ign;
2081 	int err = 0;
2082 
2083 	ign = net_generic(t->net, ip6gre_net_id);
2084 
2085 	if (!rtnl_dev_link_net_capable(dev, t->net))
2086 		return -EPERM;
2087 
2088 	mutex_lock(&ign->tunnels_lock);
2089 
2090 	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2091 	if (IS_ERR(t)) {
2092 		err = PTR_ERR(t);
2093 		goto unlock;
2094 	}
2095 
2096 	ip6gre_tunnel_unlink_md(ign, t);
2097 	ip6gre_tunnel_unlink(ign, t);
2098 	ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
2099 	ip6gre_tunnel_link_md(ign, t);
2100 	ip6gre_tunnel_link(ign, t);
2101 unlock:
2102 	mutex_unlock(&ign->tunnels_lock);
2103 
2104 	return err;
2105 }
2106 
2107 static void __ip6gre_dellink(struct net *net, struct net_device *dev,
2108 			     struct list_head *head)
2109 {
2110 	struct ip6_tnl *t = netdev_priv(dev);
2111 	struct ip6gre_net *ign;
2112 
2113 	ign = net_generic(t->net, ip6gre_net_id);
2114 
2115 	if (dev->rtnl_link_ops == &ip6erspan_tap_ops)
2116 		ip6erspan_tunnel_unlink_md(ign, t);
2117 	else
2118 		ip6gre_tunnel_unlink_md(ign, t);
2119 
2120 	ip6gre_tunnel_unlink(ign, t);
2121 	unregister_netdevice_queue_net(net, dev, head);
2122 }
2123 
2124 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
2125 {
2126 	struct ip6_tnl *t = netdev_priv(dev);
2127 	struct ip6gre_net *ign;
2128 
2129 	ign = net_generic(t->net, ip6gre_net_id);
2130 
2131 	mutex_lock(&ign->tunnels_lock);
2132 
2133 	if (dev != ign->fb_tunnel_dev &&
2134 	    !ip6gre_tunnel_unregistering(t))
2135 		__ip6gre_dellink(dev_net(dev), dev, head);
2136 
2137 	mutex_unlock(&ign->tunnels_lock);
2138 }
2139 
2140 static size_t ip6gre_get_size(const struct net_device *dev)
2141 {
2142 	return
2143 		/* IFLA_GRE_LINK */
2144 		nla_total_size(4) +
2145 		/* IFLA_GRE_IFLAGS */
2146 		nla_total_size(2) +
2147 		/* IFLA_GRE_OFLAGS */
2148 		nla_total_size(2) +
2149 		/* IFLA_GRE_IKEY */
2150 		nla_total_size(4) +
2151 		/* IFLA_GRE_OKEY */
2152 		nla_total_size(4) +
2153 		/* IFLA_GRE_LOCAL */
2154 		nla_total_size(sizeof(struct in6_addr)) +
2155 		/* IFLA_GRE_REMOTE */
2156 		nla_total_size(sizeof(struct in6_addr)) +
2157 		/* IFLA_GRE_TTL */
2158 		nla_total_size(1) +
2159 		/* IFLA_GRE_ENCAP_LIMIT */
2160 		nla_total_size(1) +
2161 		/* IFLA_GRE_FLOWINFO */
2162 		nla_total_size(4) +
2163 		/* IFLA_GRE_FLAGS */
2164 		nla_total_size(4) +
2165 		/* IFLA_GRE_ENCAP_TYPE */
2166 		nla_total_size(2) +
2167 		/* IFLA_GRE_ENCAP_FLAGS */
2168 		nla_total_size(2) +
2169 		/* IFLA_GRE_ENCAP_SPORT */
2170 		nla_total_size(2) +
2171 		/* IFLA_GRE_ENCAP_DPORT */
2172 		nla_total_size(2) +
2173 		/* IFLA_GRE_COLLECT_METADATA */
2174 		nla_total_size(0) +
2175 		/* IFLA_GRE_FWMARK */
2176 		nla_total_size(4) +
2177 		/* IFLA_GRE_ERSPAN_INDEX */
2178 		nla_total_size(4) +
2179 		0;
2180 }
2181 
2182 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
2183 {
2184 	const struct ip6_tnl *t = netdev_priv(dev);
2185 	const struct __ip6_tnl_parm *p = &t->parms;
2186 	IP_TUNNEL_DECLARE_FLAGS(o_flags);
2187 
2188 	ip_tunnel_flags_copy(o_flags, p->o_flags);
2189 
2190 	if (p->erspan_ver == 1 || p->erspan_ver == 2) {
2191 		if (!p->collect_md)
2192 			__set_bit(IP_TUNNEL_KEY_BIT, o_flags);
2193 
2194 		if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2195 			goto nla_put_failure;
2196 
2197 		if (p->erspan_ver == 1) {
2198 			if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2199 				goto nla_put_failure;
2200 		} else {
2201 			if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2202 				goto nla_put_failure;
2203 			if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2204 				goto nla_put_failure;
2205 		}
2206 	}
2207 
2208 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
2209 	    nla_put_be16(skb, IFLA_GRE_IFLAGS,
2210 			 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
2211 	    nla_put_be16(skb, IFLA_GRE_OFLAGS,
2212 			 gre_tnl_flags_to_gre_flags(o_flags)) ||
2213 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
2214 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
2215 	    nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
2216 	    nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
2217 	    nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
2218 	    nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
2219 	    nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
2220 	    nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
2221 	    nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
2222 		goto nla_put_failure;
2223 
2224 	if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2225 			t->encap.type) ||
2226 	    nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2227 			 t->encap.sport) ||
2228 	    nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2229 			 t->encap.dport) ||
2230 	    nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2231 			t->encap.flags))
2232 		goto nla_put_failure;
2233 
2234 	if (p->collect_md) {
2235 		if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2236 			goto nla_put_failure;
2237 	}
2238 
2239 	return 0;
2240 
2241 nla_put_failure:
2242 	return -EMSGSIZE;
2243 }
2244 
2245 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2246 	[IFLA_GRE_LINK]        = { .type = NLA_U32 },
2247 	[IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
2248 	[IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
2249 	[IFLA_GRE_IKEY]        = { .type = NLA_U32 },
2250 	[IFLA_GRE_OKEY]        = { .type = NLA_U32 },
2251 	[IFLA_GRE_LOCAL]       = { .len = sizeof_field(struct ipv6hdr, saddr) },
2252 	[IFLA_GRE_REMOTE]      = { .len = sizeof_field(struct ipv6hdr, daddr) },
2253 	[IFLA_GRE_TTL]         = { .type = NLA_U8 },
2254 	[IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2255 	[IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
2256 	[IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
2257 	[IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
2258 	[IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
2259 	[IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
2260 	[IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
2261 	[IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
2262 	[IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
2263 	[IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
2264 	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
2265 	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
2266 	[IFLA_GRE_ERSPAN_HWID]	= { .type = NLA_U16 },
2267 };
2268 
2269 static void ip6erspan_tap_setup(struct net_device *dev)
2270 {
2271 	ether_setup(dev);
2272 
2273 	dev->max_mtu = 0;
2274 	dev->netdev_ops = &ip6erspan_netdev_ops;
2275 	dev->needs_free_netdev = true;
2276 	dev->priv_destructor = ip6gre_dev_free;
2277 
2278 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2279 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2280 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2281 	netif_keep_dst(dev);
2282 }
2283 
2284 static int ip6erspan_newlink(struct net_device *dev,
2285 			     struct rtnl_newlink_params *params,
2286 			     struct netlink_ext_ack *extack)
2287 {
2288 	struct net *net = params->link_net ? : dev_net(dev);
2289 	struct ip6_tnl *nt = netdev_priv(dev);
2290 	struct nlattr **data = params->data;
2291 	struct nlattr **tb = params->tb;
2292 	struct ip6gre_net *ign;
2293 	int err = 0;
2294 
2295 	ip6gre_netlink_parms(data, &nt->parms);
2296 	ip6erspan_set_version(data, &nt->parms);
2297 	ign = net_generic(net, ip6gre_net_id);
2298 
2299 	mutex_lock(&ign->tunnels_lock);
2300 
2301 	if (nt->parms.collect_md) {
2302 		if (rtnl_dereference(ign->collect_md_tun_erspan))
2303 			err = -EEXIST;
2304 	} else {
2305 		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2306 			err = -EEXIST;
2307 	}
2308 	if (err)
2309 		goto unlock;
2310 
2311 	err = ip6gre_newlink_common(net, dev, tb, data, extack);
2312 	if (!err) {
2313 		ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2314 		ip6erspan_tunnel_link_md(ign, nt);
2315 		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2316 	}
2317 
2318 unlock:
2319 	mutex_unlock(&ign->tunnels_lock);
2320 
2321 	return err;
2322 }
2323 
2324 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
2325 {
2326 	ip6gre_tnl_link_config_common(t);
2327 	ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
2328 }
2329 
2330 static int ip6erspan_tnl_change(struct ip6_tnl *t,
2331 				const struct __ip6_tnl_parm *p, int set_mtu)
2332 {
2333 	ip6gre_tnl_copy_tnl_parm(t, p);
2334 	ip6erspan_tnl_link_config(t, set_mtu);
2335 	return 0;
2336 }
2337 
2338 static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
2339 				struct nlattr *data[],
2340 				struct netlink_ext_ack *extack)
2341 {
2342 	struct ip6_tnl *t = netdev_priv(dev);
2343 	struct __ip6_tnl_parm p;
2344 	struct ip6gre_net *ign;
2345 	int err = 0;
2346 
2347 	if (!rtnl_dev_link_net_capable(dev, t->net))
2348 		return -EPERM;
2349 
2350 	ign = net_generic(t->net, ip6gre_net_id);
2351 
2352 	mutex_lock(&ign->tunnels_lock);
2353 
2354 	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2355 	if (IS_ERR(t)) {
2356 		err = PTR_ERR(t);
2357 		goto unlock;
2358 	}
2359 
2360 	ip6erspan_set_version(data, &p);
2361 	ip6erspan_tunnel_unlink_md(ign, t);
2362 	ip6gre_tunnel_unlink(ign, t);
2363 	ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2364 	ip6erspan_tunnel_link_md(ign, t);
2365 	ip6gre_tunnel_link(ign, t);
2366 unlock:
2367 	mutex_unlock(&ign->tunnels_lock);
2368 
2369 	return err;
2370 }
2371 
2372 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2373 	.kind		= "ip6gre",
2374 	.maxtype	= IFLA_GRE_MAX,
2375 	.policy		= ip6gre_policy,
2376 	.priv_size	= sizeof(struct ip6_tnl),
2377 	.setup		= ip6gre_tunnel_setup,
2378 	.validate	= ip6gre_tunnel_validate,
2379 	.newlink	= ip6gre_newlink,
2380 	.changelink	= ip6gre_changelink,
2381 	.dellink	= ip6gre_dellink,
2382 	.get_size	= ip6gre_get_size,
2383 	.fill_info	= ip6gre_fill_info,
2384 	.get_link_net	= ip6_tnl_get_link_net,
2385 };
2386 
2387 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2388 	.kind		= "ip6gretap",
2389 	.maxtype	= IFLA_GRE_MAX,
2390 	.policy		= ip6gre_policy,
2391 	.priv_size	= sizeof(struct ip6_tnl),
2392 	.setup		= ip6gre_tap_setup,
2393 	.validate	= ip6gre_tap_validate,
2394 	.newlink	= ip6gre_newlink,
2395 	.changelink	= ip6gre_changelink,
2396 	.dellink	= ip6gre_dellink,
2397 	.get_size	= ip6gre_get_size,
2398 	.fill_info	= ip6gre_fill_info,
2399 	.get_link_net	= ip6_tnl_get_link_net,
2400 };
2401 
2402 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2403 	.kind		= "ip6erspan",
2404 	.maxtype	= IFLA_GRE_MAX,
2405 	.policy		= ip6gre_policy,
2406 	.priv_size	= sizeof(struct ip6_tnl),
2407 	.setup		= ip6erspan_tap_setup,
2408 	.validate	= ip6erspan_tap_validate,
2409 	.newlink	= ip6erspan_newlink,
2410 	.changelink	= ip6erspan_changelink,
2411 	.dellink	= ip6gre_dellink,
2412 	.get_size	= ip6gre_get_size,
2413 	.fill_info	= ip6gre_fill_info,
2414 	.get_link_net	= ip6_tnl_get_link_net,
2415 };
2416 
2417 /*
2418  *	And now the modules code and kernel interface.
2419  */
2420 
2421 static int __init ip6gre_init(void)
2422 {
2423 	int err;
2424 
2425 	pr_info("GRE over IPv6 tunneling driver\n");
2426 
2427 	err = register_pernet_device(&ip6gre_net_ops);
2428 	if (err < 0)
2429 		return err;
2430 
2431 	err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2432 	if (err < 0) {
2433 		pr_info("%s: can't add protocol\n", __func__);
2434 		goto add_proto_failed;
2435 	}
2436 
2437 	err = rtnl_link_register(&ip6gre_link_ops);
2438 	if (err < 0)
2439 		goto rtnl_link_failed;
2440 
2441 	err = rtnl_link_register(&ip6gre_tap_ops);
2442 	if (err < 0)
2443 		goto tap_ops_failed;
2444 
2445 	err = rtnl_link_register(&ip6erspan_tap_ops);
2446 	if (err < 0)
2447 		goto erspan_link_failed;
2448 
2449 out:
2450 	return err;
2451 
2452 erspan_link_failed:
2453 	rtnl_link_unregister(&ip6gre_tap_ops);
2454 tap_ops_failed:
2455 	rtnl_link_unregister(&ip6gre_link_ops);
2456 rtnl_link_failed:
2457 	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2458 add_proto_failed:
2459 	unregister_pernet_device(&ip6gre_net_ops);
2460 	goto out;
2461 }
2462 
2463 static void __exit ip6gre_fini(void)
2464 {
2465 	rtnl_link_unregister(&ip6gre_tap_ops);
2466 	rtnl_link_unregister(&ip6gre_link_ops);
2467 	rtnl_link_unregister(&ip6erspan_tap_ops);
2468 	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2469 	unregister_pernet_device(&ip6gre_net_ops);
2470 }
2471 
2472 module_init(ip6gre_init);
2473 module_exit(ip6gre_fini);
2474 MODULE_LICENSE("GPL");
2475 MODULE_AUTHOR("D. Kozlov <xeb@mail.ru>");
2476 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2477 MODULE_ALIAS_RTNL_LINK("ip6gre");
2478 MODULE_ALIAS_RTNL_LINK("ip6gretap");
2479 MODULE_ALIAS_RTNL_LINK("ip6erspan");
2480 MODULE_ALIAS_NETDEV("ip6gre0");
2481