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