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