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