xref: /openbmc/linux/drivers/net/geneve.c (revision 35267cea)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GENEVE: Generic Network Virtualization Encapsulation
4  *
5  * Copyright (c) 2015 Red Hat, Inc.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/ethtool.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/etherdevice.h>
14 #include <linux/hash.h>
15 #include <net/ipv6_stubs.h>
16 #include <net/dst_metadata.h>
17 #include <net/gro_cells.h>
18 #include <net/rtnetlink.h>
19 #include <net/geneve.h>
20 #include <net/protocol.h>
21 
22 #define GENEVE_NETDEV_VER	"0.6"
23 
24 #define GENEVE_N_VID		(1u << 24)
25 #define GENEVE_VID_MASK		(GENEVE_N_VID - 1)
26 
27 #define VNI_HASH_BITS		10
28 #define VNI_HASH_SIZE		(1<<VNI_HASH_BITS)
29 
30 static bool log_ecn_error = true;
31 module_param(log_ecn_error, bool, 0644);
32 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
33 
34 #define GENEVE_VER 0
35 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
36 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
37 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
38 
39 /* per-network namespace private data for this module */
40 struct geneve_net {
41 	struct list_head	geneve_list;
42 	struct list_head	sock_list;
43 };
44 
45 static unsigned int geneve_net_id;
46 
47 struct geneve_dev_node {
48 	struct hlist_node hlist;
49 	struct geneve_dev *geneve;
50 };
51 
52 struct geneve_config {
53 	struct ip_tunnel_info	info;
54 	bool			collect_md;
55 	bool			use_udp6_rx_checksums;
56 	bool			ttl_inherit;
57 	enum ifla_geneve_df	df;
58 };
59 
60 /* Pseudo network device */
61 struct geneve_dev {
62 	struct geneve_dev_node hlist4;	/* vni hash table for IPv4 socket */
63 #if IS_ENABLED(CONFIG_IPV6)
64 	struct geneve_dev_node hlist6;	/* vni hash table for IPv6 socket */
65 #endif
66 	struct net	   *net;	/* netns for packet i/o */
67 	struct net_device  *dev;	/* netdev for geneve tunnel */
68 	struct geneve_sock __rcu *sock4;	/* IPv4 socket used for geneve tunnel */
69 #if IS_ENABLED(CONFIG_IPV6)
70 	struct geneve_sock __rcu *sock6;	/* IPv6 socket used for geneve tunnel */
71 #endif
72 	struct list_head   next;	/* geneve's per namespace list */
73 	struct gro_cells   gro_cells;
74 	struct geneve_config cfg;
75 };
76 
77 struct geneve_sock {
78 	bool			collect_md;
79 	struct list_head	list;
80 	struct socket		*sock;
81 	struct rcu_head		rcu;
82 	int			refcnt;
83 	struct hlist_head	vni_list[VNI_HASH_SIZE];
84 };
85 
86 static inline __u32 geneve_net_vni_hash(u8 vni[3])
87 {
88 	__u32 vnid;
89 
90 	vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
91 	return hash_32(vnid, VNI_HASH_BITS);
92 }
93 
94 static __be64 vni_to_tunnel_id(const __u8 *vni)
95 {
96 #ifdef __BIG_ENDIAN
97 	return (vni[0] << 16) | (vni[1] << 8) | vni[2];
98 #else
99 	return (__force __be64)(((__force u64)vni[0] << 40) |
100 				((__force u64)vni[1] << 48) |
101 				((__force u64)vni[2] << 56));
102 #endif
103 }
104 
105 /* Convert 64 bit tunnel ID to 24 bit VNI. */
106 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
107 {
108 #ifdef __BIG_ENDIAN
109 	vni[0] = (__force __u8)(tun_id >> 16);
110 	vni[1] = (__force __u8)(tun_id >> 8);
111 	vni[2] = (__force __u8)tun_id;
112 #else
113 	vni[0] = (__force __u8)((__force u64)tun_id >> 40);
114 	vni[1] = (__force __u8)((__force u64)tun_id >> 48);
115 	vni[2] = (__force __u8)((__force u64)tun_id >> 56);
116 #endif
117 }
118 
119 static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
120 {
121 	return !memcmp(vni, &tun_id[5], 3);
122 }
123 
124 static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
125 {
126 	return gs->sock->sk->sk_family;
127 }
128 
129 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
130 					__be32 addr, u8 vni[])
131 {
132 	struct hlist_head *vni_list_head;
133 	struct geneve_dev_node *node;
134 	__u32 hash;
135 
136 	/* Find the device for this VNI */
137 	hash = geneve_net_vni_hash(vni);
138 	vni_list_head = &gs->vni_list[hash];
139 	hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
140 		if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
141 		    addr == node->geneve->cfg.info.key.u.ipv4.dst)
142 			return node->geneve;
143 	}
144 	return NULL;
145 }
146 
147 #if IS_ENABLED(CONFIG_IPV6)
148 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
149 					 struct in6_addr addr6, u8 vni[])
150 {
151 	struct hlist_head *vni_list_head;
152 	struct geneve_dev_node *node;
153 	__u32 hash;
154 
155 	/* Find the device for this VNI */
156 	hash = geneve_net_vni_hash(vni);
157 	vni_list_head = &gs->vni_list[hash];
158 	hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
159 		if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
160 		    ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
161 			return node->geneve;
162 	}
163 	return NULL;
164 }
165 #endif
166 
167 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
168 {
169 	return (struct genevehdr *)(udp_hdr(skb) + 1);
170 }
171 
172 static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
173 					    struct sk_buff *skb)
174 {
175 	static u8 zero_vni[3];
176 	u8 *vni;
177 
178 	if (geneve_get_sk_family(gs) == AF_INET) {
179 		struct iphdr *iph;
180 		__be32 addr;
181 
182 		iph = ip_hdr(skb); /* outer IP header... */
183 
184 		if (gs->collect_md) {
185 			vni = zero_vni;
186 			addr = 0;
187 		} else {
188 			vni = geneve_hdr(skb)->vni;
189 			addr = iph->saddr;
190 		}
191 
192 		return geneve_lookup(gs, addr, vni);
193 #if IS_ENABLED(CONFIG_IPV6)
194 	} else if (geneve_get_sk_family(gs) == AF_INET6) {
195 		static struct in6_addr zero_addr6;
196 		struct ipv6hdr *ip6h;
197 		struct in6_addr addr6;
198 
199 		ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
200 
201 		if (gs->collect_md) {
202 			vni = zero_vni;
203 			addr6 = zero_addr6;
204 		} else {
205 			vni = geneve_hdr(skb)->vni;
206 			addr6 = ip6h->saddr;
207 		}
208 
209 		return geneve6_lookup(gs, addr6, vni);
210 #endif
211 	}
212 	return NULL;
213 }
214 
215 /* geneve receive/decap routine */
216 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
217 		      struct sk_buff *skb)
218 {
219 	struct genevehdr *gnvh = geneve_hdr(skb);
220 	struct metadata_dst *tun_dst = NULL;
221 	unsigned int len;
222 	int err = 0;
223 	void *oiph;
224 
225 	if (ip_tunnel_collect_metadata() || gs->collect_md) {
226 		__be16 flags;
227 
228 		flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
229 			(gnvh->critical ? TUNNEL_CRIT_OPT : 0);
230 
231 		tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
232 					 vni_to_tunnel_id(gnvh->vni),
233 					 gnvh->opt_len * 4);
234 		if (!tun_dst) {
235 			geneve->dev->stats.rx_dropped++;
236 			goto drop;
237 		}
238 		/* Update tunnel dst according to Geneve options. */
239 		ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
240 					gnvh->options, gnvh->opt_len * 4,
241 					TUNNEL_GENEVE_OPT);
242 	} else {
243 		/* Drop packets w/ critical options,
244 		 * since we don't support any...
245 		 */
246 		if (gnvh->critical) {
247 			geneve->dev->stats.rx_frame_errors++;
248 			geneve->dev->stats.rx_errors++;
249 			goto drop;
250 		}
251 	}
252 
253 	skb_reset_mac_header(skb);
254 	skb->protocol = eth_type_trans(skb, geneve->dev);
255 	skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
256 
257 	if (tun_dst)
258 		skb_dst_set(skb, &tun_dst->dst);
259 
260 	/* Ignore packet loops (and multicast echo) */
261 	if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
262 		geneve->dev->stats.rx_errors++;
263 		goto drop;
264 	}
265 
266 	oiph = skb_network_header(skb);
267 	skb_reset_network_header(skb);
268 
269 	if (geneve_get_sk_family(gs) == AF_INET)
270 		err = IP_ECN_decapsulate(oiph, skb);
271 #if IS_ENABLED(CONFIG_IPV6)
272 	else
273 		err = IP6_ECN_decapsulate(oiph, skb);
274 #endif
275 
276 	if (unlikely(err)) {
277 		if (log_ecn_error) {
278 			if (geneve_get_sk_family(gs) == AF_INET)
279 				net_info_ratelimited("non-ECT from %pI4 "
280 						     "with TOS=%#x\n",
281 						     &((struct iphdr *)oiph)->saddr,
282 						     ((struct iphdr *)oiph)->tos);
283 #if IS_ENABLED(CONFIG_IPV6)
284 			else
285 				net_info_ratelimited("non-ECT from %pI6\n",
286 						     &((struct ipv6hdr *)oiph)->saddr);
287 #endif
288 		}
289 		if (err > 1) {
290 			++geneve->dev->stats.rx_frame_errors;
291 			++geneve->dev->stats.rx_errors;
292 			goto drop;
293 		}
294 	}
295 
296 	len = skb->len;
297 	err = gro_cells_receive(&geneve->gro_cells, skb);
298 	if (likely(err == NET_RX_SUCCESS))
299 		dev_sw_netstats_rx_add(geneve->dev, len);
300 
301 	return;
302 drop:
303 	/* Consume bad packet */
304 	kfree_skb(skb);
305 }
306 
307 /* Setup stats when device is created */
308 static int geneve_init(struct net_device *dev)
309 {
310 	struct geneve_dev *geneve = netdev_priv(dev);
311 	int err;
312 
313 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
314 	if (!dev->tstats)
315 		return -ENOMEM;
316 
317 	err = gro_cells_init(&geneve->gro_cells, dev);
318 	if (err) {
319 		free_percpu(dev->tstats);
320 		return err;
321 	}
322 
323 	err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
324 	if (err) {
325 		free_percpu(dev->tstats);
326 		gro_cells_destroy(&geneve->gro_cells);
327 		return err;
328 	}
329 	return 0;
330 }
331 
332 static void geneve_uninit(struct net_device *dev)
333 {
334 	struct geneve_dev *geneve = netdev_priv(dev);
335 
336 	dst_cache_destroy(&geneve->cfg.info.dst_cache);
337 	gro_cells_destroy(&geneve->gro_cells);
338 	free_percpu(dev->tstats);
339 }
340 
341 /* Callback from net/ipv4/udp.c to receive packets */
342 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
343 {
344 	struct genevehdr *geneveh;
345 	struct geneve_dev *geneve;
346 	struct geneve_sock *gs;
347 	int opts_len;
348 
349 	/* Need UDP and Geneve header to be present */
350 	if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
351 		goto drop;
352 
353 	/* Return packets with reserved bits set */
354 	geneveh = geneve_hdr(skb);
355 	if (unlikely(geneveh->ver != GENEVE_VER))
356 		goto drop;
357 
358 	if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
359 		goto drop;
360 
361 	gs = rcu_dereference_sk_user_data(sk);
362 	if (!gs)
363 		goto drop;
364 
365 	geneve = geneve_lookup_skb(gs, skb);
366 	if (!geneve)
367 		goto drop;
368 
369 	opts_len = geneveh->opt_len * 4;
370 	if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
371 				 htons(ETH_P_TEB),
372 				 !net_eq(geneve->net, dev_net(geneve->dev)))) {
373 		geneve->dev->stats.rx_dropped++;
374 		goto drop;
375 	}
376 
377 	geneve_rx(geneve, gs, skb);
378 	return 0;
379 
380 drop:
381 	/* Consume bad packet */
382 	kfree_skb(skb);
383 	return 0;
384 }
385 
386 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
387 static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
388 {
389 	struct genevehdr *geneveh;
390 	struct geneve_sock *gs;
391 	u8 zero_vni[3] = { 0 };
392 	u8 *vni = zero_vni;
393 
394 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
395 		return -EINVAL;
396 
397 	geneveh = geneve_hdr(skb);
398 	if (geneveh->ver != GENEVE_VER)
399 		return -EINVAL;
400 
401 	if (geneveh->proto_type != htons(ETH_P_TEB))
402 		return -EINVAL;
403 
404 	gs = rcu_dereference_sk_user_data(sk);
405 	if (!gs)
406 		return -ENOENT;
407 
408 	if (geneve_get_sk_family(gs) == AF_INET) {
409 		struct iphdr *iph = ip_hdr(skb);
410 		__be32 addr4 = 0;
411 
412 		if (!gs->collect_md) {
413 			vni = geneve_hdr(skb)->vni;
414 			addr4 = iph->daddr;
415 		}
416 
417 		return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
418 	}
419 
420 #if IS_ENABLED(CONFIG_IPV6)
421 	if (geneve_get_sk_family(gs) == AF_INET6) {
422 		struct ipv6hdr *ip6h = ipv6_hdr(skb);
423 		struct in6_addr addr6;
424 
425 		memset(&addr6, 0, sizeof(struct in6_addr));
426 
427 		if (!gs->collect_md) {
428 			vni = geneve_hdr(skb)->vni;
429 			addr6 = ip6h->daddr;
430 		}
431 
432 		return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
433 	}
434 #endif
435 
436 	return -EPFNOSUPPORT;
437 }
438 
439 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
440 					 __be16 port, bool ipv6_rx_csum)
441 {
442 	struct socket *sock;
443 	struct udp_port_cfg udp_conf;
444 	int err;
445 
446 	memset(&udp_conf, 0, sizeof(udp_conf));
447 
448 	if (ipv6) {
449 		udp_conf.family = AF_INET6;
450 		udp_conf.ipv6_v6only = 1;
451 		udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
452 	} else {
453 		udp_conf.family = AF_INET;
454 		udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
455 	}
456 
457 	udp_conf.local_udp_port = port;
458 
459 	/* Open UDP socket */
460 	err = udp_sock_create(net, &udp_conf, &sock);
461 	if (err < 0)
462 		return ERR_PTR(err);
463 
464 	udp_allow_gso(sock->sk);
465 	return sock;
466 }
467 
468 static int geneve_hlen(struct genevehdr *gh)
469 {
470 	return sizeof(*gh) + gh->opt_len * 4;
471 }
472 
473 static struct sk_buff *geneve_gro_receive(struct sock *sk,
474 					  struct list_head *head,
475 					  struct sk_buff *skb)
476 {
477 	struct sk_buff *pp = NULL;
478 	struct sk_buff *p;
479 	struct genevehdr *gh, *gh2;
480 	unsigned int hlen, gh_len, off_gnv;
481 	const struct packet_offload *ptype;
482 	__be16 type;
483 	int flush = 1;
484 
485 	off_gnv = skb_gro_offset(skb);
486 	hlen = off_gnv + sizeof(*gh);
487 	gh = skb_gro_header_fast(skb, off_gnv);
488 	if (skb_gro_header_hard(skb, hlen)) {
489 		gh = skb_gro_header_slow(skb, hlen, off_gnv);
490 		if (unlikely(!gh))
491 			goto out;
492 	}
493 
494 	if (gh->ver != GENEVE_VER || gh->oam)
495 		goto out;
496 	gh_len = geneve_hlen(gh);
497 
498 	hlen = off_gnv + gh_len;
499 	if (skb_gro_header_hard(skb, hlen)) {
500 		gh = skb_gro_header_slow(skb, hlen, off_gnv);
501 		if (unlikely(!gh))
502 			goto out;
503 	}
504 
505 	list_for_each_entry(p, head, list) {
506 		if (!NAPI_GRO_CB(p)->same_flow)
507 			continue;
508 
509 		gh2 = (struct genevehdr *)(p->data + off_gnv);
510 		if (gh->opt_len != gh2->opt_len ||
511 		    memcmp(gh, gh2, gh_len)) {
512 			NAPI_GRO_CB(p)->same_flow = 0;
513 			continue;
514 		}
515 	}
516 
517 	type = gh->proto_type;
518 
519 	rcu_read_lock();
520 	ptype = gro_find_receive_by_type(type);
521 	if (!ptype)
522 		goto out_unlock;
523 
524 	skb_gro_pull(skb, gh_len);
525 	skb_gro_postpull_rcsum(skb, gh, gh_len);
526 	pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
527 	flush = 0;
528 
529 out_unlock:
530 	rcu_read_unlock();
531 out:
532 	skb_gro_flush_final(skb, pp, flush);
533 
534 	return pp;
535 }
536 
537 static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
538 			       int nhoff)
539 {
540 	struct genevehdr *gh;
541 	struct packet_offload *ptype;
542 	__be16 type;
543 	int gh_len;
544 	int err = -ENOSYS;
545 
546 	gh = (struct genevehdr *)(skb->data + nhoff);
547 	gh_len = geneve_hlen(gh);
548 	type = gh->proto_type;
549 
550 	rcu_read_lock();
551 	ptype = gro_find_complete_by_type(type);
552 	if (ptype)
553 		err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
554 
555 	rcu_read_unlock();
556 
557 	skb_set_inner_mac_header(skb, nhoff + gh_len);
558 
559 	return err;
560 }
561 
562 /* Create new listen socket if needed */
563 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
564 						bool ipv6, bool ipv6_rx_csum)
565 {
566 	struct geneve_net *gn = net_generic(net, geneve_net_id);
567 	struct geneve_sock *gs;
568 	struct socket *sock;
569 	struct udp_tunnel_sock_cfg tunnel_cfg;
570 	int h;
571 
572 	gs = kzalloc(sizeof(*gs), GFP_KERNEL);
573 	if (!gs)
574 		return ERR_PTR(-ENOMEM);
575 
576 	sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
577 	if (IS_ERR(sock)) {
578 		kfree(gs);
579 		return ERR_CAST(sock);
580 	}
581 
582 	gs->sock = sock;
583 	gs->refcnt = 1;
584 	for (h = 0; h < VNI_HASH_SIZE; ++h)
585 		INIT_HLIST_HEAD(&gs->vni_list[h]);
586 
587 	/* Initialize the geneve udp offloads structure */
588 	udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
589 
590 	/* Mark socket as an encapsulation socket */
591 	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
592 	tunnel_cfg.sk_user_data = gs;
593 	tunnel_cfg.encap_type = 1;
594 	tunnel_cfg.gro_receive = geneve_gro_receive;
595 	tunnel_cfg.gro_complete = geneve_gro_complete;
596 	tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
597 	tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
598 	tunnel_cfg.encap_destroy = NULL;
599 	setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
600 	list_add(&gs->list, &gn->sock_list);
601 	return gs;
602 }
603 
604 static void __geneve_sock_release(struct geneve_sock *gs)
605 {
606 	if (!gs || --gs->refcnt)
607 		return;
608 
609 	list_del(&gs->list);
610 	udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
611 	udp_tunnel_sock_release(gs->sock);
612 	kfree_rcu(gs, rcu);
613 }
614 
615 static void geneve_sock_release(struct geneve_dev *geneve)
616 {
617 	struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
618 #if IS_ENABLED(CONFIG_IPV6)
619 	struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
620 
621 	rcu_assign_pointer(geneve->sock6, NULL);
622 #endif
623 
624 	rcu_assign_pointer(geneve->sock4, NULL);
625 	synchronize_net();
626 
627 	__geneve_sock_release(gs4);
628 #if IS_ENABLED(CONFIG_IPV6)
629 	__geneve_sock_release(gs6);
630 #endif
631 }
632 
633 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
634 					    sa_family_t family,
635 					    __be16 dst_port)
636 {
637 	struct geneve_sock *gs;
638 
639 	list_for_each_entry(gs, &gn->sock_list, list) {
640 		if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
641 		    geneve_get_sk_family(gs) == family) {
642 			return gs;
643 		}
644 	}
645 	return NULL;
646 }
647 
648 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
649 {
650 	struct net *net = geneve->net;
651 	struct geneve_net *gn = net_generic(net, geneve_net_id);
652 	struct geneve_dev_node *node;
653 	struct geneve_sock *gs;
654 	__u8 vni[3];
655 	__u32 hash;
656 
657 	gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
658 	if (gs) {
659 		gs->refcnt++;
660 		goto out;
661 	}
662 
663 	gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
664 				  geneve->cfg.use_udp6_rx_checksums);
665 	if (IS_ERR(gs))
666 		return PTR_ERR(gs);
667 
668 out:
669 	gs->collect_md = geneve->cfg.collect_md;
670 #if IS_ENABLED(CONFIG_IPV6)
671 	if (ipv6) {
672 		rcu_assign_pointer(geneve->sock6, gs);
673 		node = &geneve->hlist6;
674 	} else
675 #endif
676 	{
677 		rcu_assign_pointer(geneve->sock4, gs);
678 		node = &geneve->hlist4;
679 	}
680 	node->geneve = geneve;
681 
682 	tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
683 	hash = geneve_net_vni_hash(vni);
684 	hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
685 	return 0;
686 }
687 
688 static int geneve_open(struct net_device *dev)
689 {
690 	struct geneve_dev *geneve = netdev_priv(dev);
691 	bool metadata = geneve->cfg.collect_md;
692 	bool ipv4, ipv6;
693 	int ret = 0;
694 
695 	ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
696 	ipv4 = !ipv6 || metadata;
697 #if IS_ENABLED(CONFIG_IPV6)
698 	if (ipv6) {
699 		ret = geneve_sock_add(geneve, true);
700 		if (ret < 0 && ret != -EAFNOSUPPORT)
701 			ipv4 = false;
702 	}
703 #endif
704 	if (ipv4)
705 		ret = geneve_sock_add(geneve, false);
706 	if (ret < 0)
707 		geneve_sock_release(geneve);
708 
709 	return ret;
710 }
711 
712 static int geneve_stop(struct net_device *dev)
713 {
714 	struct geneve_dev *geneve = netdev_priv(dev);
715 
716 	hlist_del_init_rcu(&geneve->hlist4.hlist);
717 #if IS_ENABLED(CONFIG_IPV6)
718 	hlist_del_init_rcu(&geneve->hlist6.hlist);
719 #endif
720 	geneve_sock_release(geneve);
721 	return 0;
722 }
723 
724 static void geneve_build_header(struct genevehdr *geneveh,
725 				const struct ip_tunnel_info *info)
726 {
727 	geneveh->ver = GENEVE_VER;
728 	geneveh->opt_len = info->options_len / 4;
729 	geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
730 	geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
731 	geneveh->rsvd1 = 0;
732 	tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
733 	geneveh->proto_type = htons(ETH_P_TEB);
734 	geneveh->rsvd2 = 0;
735 
736 	if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
737 		ip_tunnel_info_opts_get(geneveh->options, info);
738 }
739 
740 static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
741 			    const struct ip_tunnel_info *info,
742 			    bool xnet, int ip_hdr_len)
743 {
744 	bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
745 	struct genevehdr *gnvh;
746 	int min_headroom;
747 	int err;
748 
749 	skb_reset_mac_header(skb);
750 	skb_scrub_packet(skb, xnet);
751 
752 	min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
753 		       GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
754 	err = skb_cow_head(skb, min_headroom);
755 	if (unlikely(err))
756 		goto free_dst;
757 
758 	err = udp_tunnel_handle_offloads(skb, udp_sum);
759 	if (err)
760 		goto free_dst;
761 
762 	gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
763 	geneve_build_header(gnvh, info);
764 	skb_set_inner_protocol(skb, htons(ETH_P_TEB));
765 	return 0;
766 
767 free_dst:
768 	dst_release(dst);
769 	return err;
770 }
771 
772 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
773 				       struct net_device *dev,
774 				       struct geneve_sock *gs4,
775 				       struct flowi4 *fl4,
776 				       const struct ip_tunnel_info *info,
777 				       __be16 dport, __be16 sport)
778 {
779 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
780 	struct geneve_dev *geneve = netdev_priv(dev);
781 	struct dst_cache *dst_cache;
782 	struct rtable *rt = NULL;
783 	__u8 tos;
784 
785 	if (!gs4)
786 		return ERR_PTR(-EIO);
787 
788 	memset(fl4, 0, sizeof(*fl4));
789 	fl4->flowi4_mark = skb->mark;
790 	fl4->flowi4_proto = IPPROTO_UDP;
791 	fl4->daddr = info->key.u.ipv4.dst;
792 	fl4->saddr = info->key.u.ipv4.src;
793 	fl4->fl4_dport = dport;
794 	fl4->fl4_sport = sport;
795 
796 	tos = info->key.tos;
797 	if ((tos == 1) && !geneve->cfg.collect_md) {
798 		tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
799 		use_cache = false;
800 	}
801 	fl4->flowi4_tos = RT_TOS(tos);
802 
803 	dst_cache = (struct dst_cache *)&info->dst_cache;
804 	if (use_cache) {
805 		rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
806 		if (rt)
807 			return rt;
808 	}
809 	rt = ip_route_output_key(geneve->net, fl4);
810 	if (IS_ERR(rt)) {
811 		netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
812 		return ERR_PTR(-ENETUNREACH);
813 	}
814 	if (rt->dst.dev == dev) { /* is this necessary? */
815 		netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
816 		ip_rt_put(rt);
817 		return ERR_PTR(-ELOOP);
818 	}
819 	if (use_cache)
820 		dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
821 	return rt;
822 }
823 
824 #if IS_ENABLED(CONFIG_IPV6)
825 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
826 					   struct net_device *dev,
827 					   struct geneve_sock *gs6,
828 					   struct flowi6 *fl6,
829 					   const struct ip_tunnel_info *info,
830 					   __be16 dport, __be16 sport)
831 {
832 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
833 	struct geneve_dev *geneve = netdev_priv(dev);
834 	struct dst_entry *dst = NULL;
835 	struct dst_cache *dst_cache;
836 	__u8 prio;
837 
838 	if (!gs6)
839 		return ERR_PTR(-EIO);
840 
841 	memset(fl6, 0, sizeof(*fl6));
842 	fl6->flowi6_mark = skb->mark;
843 	fl6->flowi6_proto = IPPROTO_UDP;
844 	fl6->daddr = info->key.u.ipv6.dst;
845 	fl6->saddr = info->key.u.ipv6.src;
846 	fl6->fl6_dport = dport;
847 	fl6->fl6_sport = sport;
848 
849 	prio = info->key.tos;
850 	if ((prio == 1) && !geneve->cfg.collect_md) {
851 		prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
852 		use_cache = false;
853 	}
854 
855 	fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
856 					   info->key.label);
857 	dst_cache = (struct dst_cache *)&info->dst_cache;
858 	if (use_cache) {
859 		dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
860 		if (dst)
861 			return dst;
862 	}
863 	dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
864 					      NULL);
865 	if (IS_ERR(dst)) {
866 		netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
867 		return ERR_PTR(-ENETUNREACH);
868 	}
869 	if (dst->dev == dev) { /* is this necessary? */
870 		netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
871 		dst_release(dst);
872 		return ERR_PTR(-ELOOP);
873 	}
874 
875 	if (use_cache)
876 		dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
877 	return dst;
878 }
879 #endif
880 
881 static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
882 			   struct geneve_dev *geneve,
883 			   const struct ip_tunnel_info *info)
884 {
885 	bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
886 	struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
887 	const struct ip_tunnel_key *key = &info->key;
888 	struct rtable *rt;
889 	struct flowi4 fl4;
890 	__u8 tos, ttl;
891 	__be16 df = 0;
892 	__be16 sport;
893 	int err;
894 
895 	if (!pskb_inet_may_pull(skb))
896 		return -EINVAL;
897 
898 	sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
899 	rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
900 			      geneve->cfg.info.key.tp_dst, sport);
901 	if (IS_ERR(rt))
902 		return PTR_ERR(rt);
903 
904 	err = skb_tunnel_check_pmtu(skb, &rt->dst,
905 				    GENEVE_IPV4_HLEN + info->options_len,
906 				    netif_is_any_bridge_port(dev));
907 	if (err < 0) {
908 		dst_release(&rt->dst);
909 		return err;
910 	} else if (err) {
911 		struct ip_tunnel_info *info;
912 
913 		info = skb_tunnel_info(skb);
914 		if (info) {
915 			struct ip_tunnel_info *unclone;
916 
917 			unclone = skb_tunnel_info_unclone(skb);
918 			if (unlikely(!unclone)) {
919 				dst_release(&rt->dst);
920 				return -ENOMEM;
921 			}
922 
923 			unclone->key.u.ipv4.dst = fl4.saddr;
924 			unclone->key.u.ipv4.src = fl4.daddr;
925 		}
926 
927 		if (!pskb_may_pull(skb, ETH_HLEN)) {
928 			dst_release(&rt->dst);
929 			return -EINVAL;
930 		}
931 
932 		skb->protocol = eth_type_trans(skb, geneve->dev);
933 		netif_rx(skb);
934 		dst_release(&rt->dst);
935 		return -EMSGSIZE;
936 	}
937 
938 	if (geneve->cfg.collect_md) {
939 		tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
940 		ttl = key->ttl;
941 
942 		df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
943 	} else {
944 		tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
945 		if (geneve->cfg.ttl_inherit)
946 			ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
947 		else
948 			ttl = key->ttl;
949 		ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
950 
951 		if (geneve->cfg.df == GENEVE_DF_SET) {
952 			df = htons(IP_DF);
953 		} else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
954 			struct ethhdr *eth = eth_hdr(skb);
955 
956 			if (ntohs(eth->h_proto) == ETH_P_IPV6) {
957 				df = htons(IP_DF);
958 			} else if (ntohs(eth->h_proto) == ETH_P_IP) {
959 				struct iphdr *iph = ip_hdr(skb);
960 
961 				if (iph->frag_off & htons(IP_DF))
962 					df = htons(IP_DF);
963 			}
964 		}
965 	}
966 
967 	err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
968 	if (unlikely(err))
969 		return err;
970 
971 	udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
972 			    tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
973 			    !net_eq(geneve->net, dev_net(geneve->dev)),
974 			    !(info->key.tun_flags & TUNNEL_CSUM));
975 	return 0;
976 }
977 
978 #if IS_ENABLED(CONFIG_IPV6)
979 static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
980 			    struct geneve_dev *geneve,
981 			    const struct ip_tunnel_info *info)
982 {
983 	bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
984 	struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
985 	const struct ip_tunnel_key *key = &info->key;
986 	struct dst_entry *dst = NULL;
987 	struct flowi6 fl6;
988 	__u8 prio, ttl;
989 	__be16 sport;
990 	int err;
991 
992 	if (!pskb_inet_may_pull(skb))
993 		return -EINVAL;
994 
995 	sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
996 	dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
997 				geneve->cfg.info.key.tp_dst, sport);
998 	if (IS_ERR(dst))
999 		return PTR_ERR(dst);
1000 
1001 	err = skb_tunnel_check_pmtu(skb, dst,
1002 				    GENEVE_IPV6_HLEN + info->options_len,
1003 				    netif_is_any_bridge_port(dev));
1004 	if (err < 0) {
1005 		dst_release(dst);
1006 		return err;
1007 	} else if (err) {
1008 		struct ip_tunnel_info *info = skb_tunnel_info(skb);
1009 
1010 		if (info) {
1011 			struct ip_tunnel_info *unclone;
1012 
1013 			unclone = skb_tunnel_info_unclone(skb);
1014 			if (unlikely(!unclone)) {
1015 				dst_release(dst);
1016 				return -ENOMEM;
1017 			}
1018 
1019 			unclone->key.u.ipv6.dst = fl6.saddr;
1020 			unclone->key.u.ipv6.src = fl6.daddr;
1021 		}
1022 
1023 		if (!pskb_may_pull(skb, ETH_HLEN)) {
1024 			dst_release(dst);
1025 			return -EINVAL;
1026 		}
1027 
1028 		skb->protocol = eth_type_trans(skb, geneve->dev);
1029 		netif_rx(skb);
1030 		dst_release(dst);
1031 		return -EMSGSIZE;
1032 	}
1033 
1034 	if (geneve->cfg.collect_md) {
1035 		prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
1036 		ttl = key->ttl;
1037 	} else {
1038 		prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
1039 					   ip_hdr(skb), skb);
1040 		if (geneve->cfg.ttl_inherit)
1041 			ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1042 		else
1043 			ttl = key->ttl;
1044 		ttl = ttl ? : ip6_dst_hoplimit(dst);
1045 	}
1046 	err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
1047 	if (unlikely(err))
1048 		return err;
1049 
1050 	udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
1051 			     &fl6.saddr, &fl6.daddr, prio, ttl,
1052 			     info->key.label, sport, geneve->cfg.info.key.tp_dst,
1053 			     !(info->key.tun_flags & TUNNEL_CSUM));
1054 	return 0;
1055 }
1056 #endif
1057 
1058 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1059 {
1060 	struct geneve_dev *geneve = netdev_priv(dev);
1061 	struct ip_tunnel_info *info = NULL;
1062 	int err;
1063 
1064 	if (geneve->cfg.collect_md) {
1065 		info = skb_tunnel_info(skb);
1066 		if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
1067 			netdev_dbg(dev, "no tunnel metadata\n");
1068 			dev_kfree_skb(skb);
1069 			dev->stats.tx_dropped++;
1070 			return NETDEV_TX_OK;
1071 		}
1072 	} else {
1073 		info = &geneve->cfg.info;
1074 	}
1075 
1076 	rcu_read_lock();
1077 #if IS_ENABLED(CONFIG_IPV6)
1078 	if (info->mode & IP_TUNNEL_INFO_IPV6)
1079 		err = geneve6_xmit_skb(skb, dev, geneve, info);
1080 	else
1081 #endif
1082 		err = geneve_xmit_skb(skb, dev, geneve, info);
1083 	rcu_read_unlock();
1084 
1085 	if (likely(!err))
1086 		return NETDEV_TX_OK;
1087 
1088 	if (err != -EMSGSIZE)
1089 		dev_kfree_skb(skb);
1090 
1091 	if (err == -ELOOP)
1092 		dev->stats.collisions++;
1093 	else if (err == -ENETUNREACH)
1094 		dev->stats.tx_carrier_errors++;
1095 
1096 	dev->stats.tx_errors++;
1097 	return NETDEV_TX_OK;
1098 }
1099 
1100 static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1101 {
1102 	if (new_mtu > dev->max_mtu)
1103 		new_mtu = dev->max_mtu;
1104 	else if (new_mtu < dev->min_mtu)
1105 		new_mtu = dev->min_mtu;
1106 
1107 	dev->mtu = new_mtu;
1108 	return 0;
1109 }
1110 
1111 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1112 {
1113 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
1114 	struct geneve_dev *geneve = netdev_priv(dev);
1115 	__be16 sport;
1116 
1117 	if (ip_tunnel_info_af(info) == AF_INET) {
1118 		struct rtable *rt;
1119 		struct flowi4 fl4;
1120 
1121 		struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1122 		sport = udp_flow_src_port(geneve->net, skb,
1123 					  1, USHRT_MAX, true);
1124 
1125 		rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1126 				      geneve->cfg.info.key.tp_dst, sport);
1127 		if (IS_ERR(rt))
1128 			return PTR_ERR(rt);
1129 
1130 		ip_rt_put(rt);
1131 		info->key.u.ipv4.src = fl4.saddr;
1132 #if IS_ENABLED(CONFIG_IPV6)
1133 	} else if (ip_tunnel_info_af(info) == AF_INET6) {
1134 		struct dst_entry *dst;
1135 		struct flowi6 fl6;
1136 
1137 		struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1138 		sport = udp_flow_src_port(geneve->net, skb,
1139 					  1, USHRT_MAX, true);
1140 
1141 		dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1142 					geneve->cfg.info.key.tp_dst, sport);
1143 		if (IS_ERR(dst))
1144 			return PTR_ERR(dst);
1145 
1146 		dst_release(dst);
1147 		info->key.u.ipv6.src = fl6.saddr;
1148 #endif
1149 	} else {
1150 		return -EINVAL;
1151 	}
1152 
1153 	info->key.tp_src = sport;
1154 	info->key.tp_dst = geneve->cfg.info.key.tp_dst;
1155 	return 0;
1156 }
1157 
1158 static const struct net_device_ops geneve_netdev_ops = {
1159 	.ndo_init		= geneve_init,
1160 	.ndo_uninit		= geneve_uninit,
1161 	.ndo_open		= geneve_open,
1162 	.ndo_stop		= geneve_stop,
1163 	.ndo_start_xmit		= geneve_xmit,
1164 	.ndo_get_stats64	= dev_get_tstats64,
1165 	.ndo_change_mtu		= geneve_change_mtu,
1166 	.ndo_validate_addr	= eth_validate_addr,
1167 	.ndo_set_mac_address	= eth_mac_addr,
1168 	.ndo_fill_metadata_dst	= geneve_fill_metadata_dst,
1169 };
1170 
1171 static void geneve_get_drvinfo(struct net_device *dev,
1172 			       struct ethtool_drvinfo *drvinfo)
1173 {
1174 	strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1175 	strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1176 }
1177 
1178 static const struct ethtool_ops geneve_ethtool_ops = {
1179 	.get_drvinfo	= geneve_get_drvinfo,
1180 	.get_link	= ethtool_op_get_link,
1181 };
1182 
1183 /* Info for udev, that this is a virtual tunnel endpoint */
1184 static struct device_type geneve_type = {
1185 	.name = "geneve",
1186 };
1187 
1188 /* Calls the ndo_udp_tunnel_add of the caller in order to
1189  * supply the listening GENEVE udp ports. Callers are expected
1190  * to implement the ndo_udp_tunnel_add.
1191  */
1192 static void geneve_offload_rx_ports(struct net_device *dev, bool push)
1193 {
1194 	struct net *net = dev_net(dev);
1195 	struct geneve_net *gn = net_generic(net, geneve_net_id);
1196 	struct geneve_sock *gs;
1197 
1198 	rcu_read_lock();
1199 	list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1200 		if (push) {
1201 			udp_tunnel_push_rx_port(dev, gs->sock,
1202 						UDP_TUNNEL_TYPE_GENEVE);
1203 		} else {
1204 			udp_tunnel_drop_rx_port(dev, gs->sock,
1205 						UDP_TUNNEL_TYPE_GENEVE);
1206 		}
1207 	}
1208 	rcu_read_unlock();
1209 }
1210 
1211 /* Initialize the device structure. */
1212 static void geneve_setup(struct net_device *dev)
1213 {
1214 	ether_setup(dev);
1215 
1216 	dev->netdev_ops = &geneve_netdev_ops;
1217 	dev->ethtool_ops = &geneve_ethtool_ops;
1218 	dev->needs_free_netdev = true;
1219 
1220 	SET_NETDEV_DEVTYPE(dev, &geneve_type);
1221 
1222 	dev->features    |= NETIF_F_LLTX;
1223 	dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1224 	dev->features    |= NETIF_F_RXCSUM;
1225 	dev->features    |= NETIF_F_GSO_SOFTWARE;
1226 
1227 	dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1228 	dev->hw_features |= NETIF_F_RXCSUM;
1229 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1230 
1231 	/* MTU range: 68 - (something less than 65535) */
1232 	dev->min_mtu = ETH_MIN_MTU;
1233 	/* The max_mtu calculation does not take account of GENEVE
1234 	 * options, to avoid excluding potentially valid
1235 	 * configurations. This will be further reduced by IPvX hdr size.
1236 	 */
1237 	dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1238 
1239 	netif_keep_dst(dev);
1240 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1241 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1242 	eth_hw_addr_random(dev);
1243 }
1244 
1245 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1246 	[IFLA_GENEVE_ID]		= { .type = NLA_U32 },
1247 	[IFLA_GENEVE_REMOTE]		= { .len = sizeof_field(struct iphdr, daddr) },
1248 	[IFLA_GENEVE_REMOTE6]		= { .len = sizeof(struct in6_addr) },
1249 	[IFLA_GENEVE_TTL]		= { .type = NLA_U8 },
1250 	[IFLA_GENEVE_TOS]		= { .type = NLA_U8 },
1251 	[IFLA_GENEVE_LABEL]		= { .type = NLA_U32 },
1252 	[IFLA_GENEVE_PORT]		= { .type = NLA_U16 },
1253 	[IFLA_GENEVE_COLLECT_METADATA]	= { .type = NLA_FLAG },
1254 	[IFLA_GENEVE_UDP_CSUM]		= { .type = NLA_U8 },
1255 	[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]	= { .type = NLA_U8 },
1256 	[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]	= { .type = NLA_U8 },
1257 	[IFLA_GENEVE_TTL_INHERIT]	= { .type = NLA_U8 },
1258 	[IFLA_GENEVE_DF]		= { .type = NLA_U8 },
1259 };
1260 
1261 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1262 			   struct netlink_ext_ack *extack)
1263 {
1264 	if (tb[IFLA_ADDRESS]) {
1265 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1266 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1267 					    "Provided link layer address is not Ethernet");
1268 			return -EINVAL;
1269 		}
1270 
1271 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1272 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1273 					    "Provided Ethernet address is not unicast");
1274 			return -EADDRNOTAVAIL;
1275 		}
1276 	}
1277 
1278 	if (!data) {
1279 		NL_SET_ERR_MSG(extack,
1280 			       "Not enough attributes provided to perform the operation");
1281 		return -EINVAL;
1282 	}
1283 
1284 	if (data[IFLA_GENEVE_ID]) {
1285 		__u32 vni =  nla_get_u32(data[IFLA_GENEVE_ID]);
1286 
1287 		if (vni >= GENEVE_N_VID) {
1288 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1289 					    "Geneve ID must be lower than 16777216");
1290 			return -ERANGE;
1291 		}
1292 	}
1293 
1294 	if (data[IFLA_GENEVE_DF]) {
1295 		enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1296 
1297 		if (df < 0 || df > GENEVE_DF_MAX) {
1298 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
1299 					    "Invalid DF attribute");
1300 			return -EINVAL;
1301 		}
1302 	}
1303 
1304 	return 0;
1305 }
1306 
1307 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1308 					  const struct ip_tunnel_info *info,
1309 					  bool *tun_on_same_port,
1310 					  bool *tun_collect_md)
1311 {
1312 	struct geneve_dev *geneve, *t = NULL;
1313 
1314 	*tun_on_same_port = false;
1315 	*tun_collect_md = false;
1316 	list_for_each_entry(geneve, &gn->geneve_list, next) {
1317 		if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1318 			*tun_collect_md = geneve->cfg.collect_md;
1319 			*tun_on_same_port = true;
1320 		}
1321 		if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1322 		    info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1323 		    !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
1324 			t = geneve;
1325 	}
1326 	return t;
1327 }
1328 
1329 static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1330 {
1331 	return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1332 		 info->key.ttl || info->key.label || info->key.tp_src ||
1333 		 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
1334 }
1335 
1336 static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1337 				  struct ip_tunnel_info *b)
1338 {
1339 	if (ip_tunnel_info_af(a) == AF_INET)
1340 		return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1341 	else
1342 		return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1343 }
1344 
1345 static int geneve_configure(struct net *net, struct net_device *dev,
1346 			    struct netlink_ext_ack *extack,
1347 			    const struct geneve_config *cfg)
1348 {
1349 	struct geneve_net *gn = net_generic(net, geneve_net_id);
1350 	struct geneve_dev *t, *geneve = netdev_priv(dev);
1351 	const struct ip_tunnel_info *info = &cfg->info;
1352 	bool tun_collect_md, tun_on_same_port;
1353 	int err, encap_len;
1354 
1355 	if (cfg->collect_md && !is_tnl_info_zero(info)) {
1356 		NL_SET_ERR_MSG(extack,
1357 			       "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
1358 		return -EINVAL;
1359 	}
1360 
1361 	geneve->net = net;
1362 	geneve->dev = dev;
1363 
1364 	t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
1365 	if (t)
1366 		return -EBUSY;
1367 
1368 	/* make enough headroom for basic scenario */
1369 	encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1370 	if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
1371 		encap_len += sizeof(struct iphdr);
1372 		dev->max_mtu -= sizeof(struct iphdr);
1373 	} else {
1374 		encap_len += sizeof(struct ipv6hdr);
1375 		dev->max_mtu -= sizeof(struct ipv6hdr);
1376 	}
1377 	dev->needed_headroom = encap_len + ETH_HLEN;
1378 
1379 	if (cfg->collect_md) {
1380 		if (tun_on_same_port) {
1381 			NL_SET_ERR_MSG(extack,
1382 				       "There can be only one externally controlled device on a destination port");
1383 			return -EPERM;
1384 		}
1385 	} else {
1386 		if (tun_collect_md) {
1387 			NL_SET_ERR_MSG(extack,
1388 				       "There already exists an externally controlled device on this destination port");
1389 			return -EPERM;
1390 		}
1391 	}
1392 
1393 	dst_cache_reset(&geneve->cfg.info.dst_cache);
1394 	memcpy(&geneve->cfg, cfg, sizeof(*cfg));
1395 
1396 	err = register_netdevice(dev);
1397 	if (err)
1398 		return err;
1399 
1400 	list_add(&geneve->next, &gn->geneve_list);
1401 	return 0;
1402 }
1403 
1404 static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1405 {
1406 	memset(info, 0, sizeof(*info));
1407 	info->key.tp_dst = htons(dst_port);
1408 }
1409 
1410 static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1411 			  struct netlink_ext_ack *extack,
1412 			  struct geneve_config *cfg, bool changelink)
1413 {
1414 	struct ip_tunnel_info *info = &cfg->info;
1415 	int attrtype;
1416 
1417 	if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1418 		NL_SET_ERR_MSG(extack,
1419 			       "Cannot specify both IPv4 and IPv6 Remote addresses");
1420 		return -EINVAL;
1421 	}
1422 
1423 	if (data[IFLA_GENEVE_REMOTE]) {
1424 		if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1425 			attrtype = IFLA_GENEVE_REMOTE;
1426 			goto change_notsup;
1427 		}
1428 
1429 		info->key.u.ipv4.dst =
1430 			nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1431 
1432 		if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
1433 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1434 					    "Remote IPv4 address cannot be Multicast");
1435 			return -EINVAL;
1436 		}
1437 	}
1438 
1439 	if (data[IFLA_GENEVE_REMOTE6]) {
1440 #if IS_ENABLED(CONFIG_IPV6)
1441 		if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1442 			attrtype = IFLA_GENEVE_REMOTE6;
1443 			goto change_notsup;
1444 		}
1445 
1446 		info->mode = IP_TUNNEL_INFO_IPV6;
1447 		info->key.u.ipv6.dst =
1448 			nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1449 
1450 		if (ipv6_addr_type(&info->key.u.ipv6.dst) &
1451 		    IPV6_ADDR_LINKLOCAL) {
1452 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1453 					    "Remote IPv6 address cannot be link-local");
1454 			return -EINVAL;
1455 		}
1456 		if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
1457 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1458 					    "Remote IPv6 address cannot be Multicast");
1459 			return -EINVAL;
1460 		}
1461 		info->key.tun_flags |= TUNNEL_CSUM;
1462 		cfg->use_udp6_rx_checksums = true;
1463 #else
1464 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1465 				    "IPv6 support not enabled in the kernel");
1466 		return -EPFNOSUPPORT;
1467 #endif
1468 	}
1469 
1470 	if (data[IFLA_GENEVE_ID]) {
1471 		__u32 vni;
1472 		__u8 tvni[3];
1473 		__be64 tunid;
1474 
1475 		vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1476 		tvni[0] = (vni & 0x00ff0000) >> 16;
1477 		tvni[1] = (vni & 0x0000ff00) >> 8;
1478 		tvni[2] =  vni & 0x000000ff;
1479 
1480 		tunid = vni_to_tunnel_id(tvni);
1481 		if (changelink && (tunid != info->key.tun_id)) {
1482 			attrtype = IFLA_GENEVE_ID;
1483 			goto change_notsup;
1484 		}
1485 		info->key.tun_id = tunid;
1486 	}
1487 
1488 	if (data[IFLA_GENEVE_TTL_INHERIT]) {
1489 		if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1490 			cfg->ttl_inherit = true;
1491 		else
1492 			cfg->ttl_inherit = false;
1493 	} else if (data[IFLA_GENEVE_TTL]) {
1494 		info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1495 		cfg->ttl_inherit = false;
1496 	}
1497 
1498 	if (data[IFLA_GENEVE_TOS])
1499 		info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1500 
1501 	if (data[IFLA_GENEVE_DF])
1502 		cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
1503 
1504 	if (data[IFLA_GENEVE_LABEL]) {
1505 		info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1506 				  IPV6_FLOWLABEL_MASK;
1507 		if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1508 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1509 					    "Label attribute only applies for IPv6 Geneve devices");
1510 			return -EINVAL;
1511 		}
1512 	}
1513 
1514 	if (data[IFLA_GENEVE_PORT]) {
1515 		if (changelink) {
1516 			attrtype = IFLA_GENEVE_PORT;
1517 			goto change_notsup;
1518 		}
1519 		info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1520 	}
1521 
1522 	if (data[IFLA_GENEVE_COLLECT_METADATA]) {
1523 		if (changelink) {
1524 			attrtype = IFLA_GENEVE_COLLECT_METADATA;
1525 			goto change_notsup;
1526 		}
1527 		cfg->collect_md = true;
1528 	}
1529 
1530 	if (data[IFLA_GENEVE_UDP_CSUM]) {
1531 		if (changelink) {
1532 			attrtype = IFLA_GENEVE_UDP_CSUM;
1533 			goto change_notsup;
1534 		}
1535 		if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1536 			info->key.tun_flags |= TUNNEL_CSUM;
1537 	}
1538 
1539 	if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
1540 #if IS_ENABLED(CONFIG_IPV6)
1541 		if (changelink) {
1542 			attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1543 			goto change_notsup;
1544 		}
1545 		if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1546 			info->key.tun_flags &= ~TUNNEL_CSUM;
1547 #else
1548 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1549 				    "IPv6 support not enabled in the kernel");
1550 		return -EPFNOSUPPORT;
1551 #endif
1552 	}
1553 
1554 	if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
1555 #if IS_ENABLED(CONFIG_IPV6)
1556 		if (changelink) {
1557 			attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1558 			goto change_notsup;
1559 		}
1560 		if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1561 			cfg->use_udp6_rx_checksums = false;
1562 #else
1563 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1564 				    "IPv6 support not enabled in the kernel");
1565 		return -EPFNOSUPPORT;
1566 #endif
1567 	}
1568 
1569 	return 0;
1570 change_notsup:
1571 	NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1572 			    "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1573 	return -EOPNOTSUPP;
1574 }
1575 
1576 static void geneve_link_config(struct net_device *dev,
1577 			       struct ip_tunnel_info *info, struct nlattr *tb[])
1578 {
1579 	struct geneve_dev *geneve = netdev_priv(dev);
1580 	int ldev_mtu = 0;
1581 
1582 	if (tb[IFLA_MTU]) {
1583 		geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1584 		return;
1585 	}
1586 
1587 	switch (ip_tunnel_info_af(info)) {
1588 	case AF_INET: {
1589 		struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1590 		struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1591 
1592 		if (!IS_ERR(rt) && rt->dst.dev) {
1593 			ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1594 			ip_rt_put(rt);
1595 		}
1596 		break;
1597 	}
1598 #if IS_ENABLED(CONFIG_IPV6)
1599 	case AF_INET6: {
1600 		struct rt6_info *rt;
1601 
1602 		if (!__in6_dev_get(dev))
1603 			break;
1604 
1605 		rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1606 				NULL, 0);
1607 
1608 		if (rt && rt->dst.dev)
1609 			ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1610 		ip6_rt_put(rt);
1611 		break;
1612 	}
1613 #endif
1614 	}
1615 
1616 	if (ldev_mtu <= 0)
1617 		return;
1618 
1619 	geneve_change_mtu(dev, ldev_mtu - info->options_len);
1620 }
1621 
1622 static int geneve_newlink(struct net *net, struct net_device *dev,
1623 			  struct nlattr *tb[], struct nlattr *data[],
1624 			  struct netlink_ext_ack *extack)
1625 {
1626 	struct geneve_config cfg = {
1627 		.df = GENEVE_DF_UNSET,
1628 		.use_udp6_rx_checksums = false,
1629 		.ttl_inherit = false,
1630 		.collect_md = false,
1631 	};
1632 	int err;
1633 
1634 	init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1635 	err = geneve_nl2info(tb, data, extack, &cfg, false);
1636 	if (err)
1637 		return err;
1638 
1639 	err = geneve_configure(net, dev, extack, &cfg);
1640 	if (err)
1641 		return err;
1642 
1643 	geneve_link_config(dev, &cfg.info, tb);
1644 
1645 	return 0;
1646 }
1647 
1648 /* Quiesces the geneve device data path for both TX and RX.
1649  *
1650  * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1651  * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1652  * to complete for the existing set of in-flight packets to be transmitted,
1653  * then we would have quiesced the transmit data path. All the future packets
1654  * will get dropped until we unquiesce the data path.
1655  *
1656  * On receive geneve dereference the geneve_sock stashed in the socket. So,
1657  * if we set that to NULL under RCU and wait for synchronize_net() to
1658  * complete, then we would have quiesced the receive data path.
1659  */
1660 static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1661 			   struct geneve_sock **gs6)
1662 {
1663 	*gs4 = rtnl_dereference(geneve->sock4);
1664 	rcu_assign_pointer(geneve->sock4, NULL);
1665 	if (*gs4)
1666 		rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1667 #if IS_ENABLED(CONFIG_IPV6)
1668 	*gs6 = rtnl_dereference(geneve->sock6);
1669 	rcu_assign_pointer(geneve->sock6, NULL);
1670 	if (*gs6)
1671 		rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1672 #else
1673 	*gs6 = NULL;
1674 #endif
1675 	synchronize_net();
1676 }
1677 
1678 /* Resumes the geneve device data path for both TX and RX. */
1679 static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1680 			     struct geneve_sock __maybe_unused *gs6)
1681 {
1682 	rcu_assign_pointer(geneve->sock4, gs4);
1683 	if (gs4)
1684 		rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 	rcu_assign_pointer(geneve->sock6, gs6);
1687 	if (gs6)
1688 		rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1689 #endif
1690 	synchronize_net();
1691 }
1692 
1693 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1694 			     struct nlattr *data[],
1695 			     struct netlink_ext_ack *extack)
1696 {
1697 	struct geneve_dev *geneve = netdev_priv(dev);
1698 	struct geneve_sock *gs4, *gs6;
1699 	struct geneve_config cfg;
1700 	int err;
1701 
1702 	/* If the geneve device is configured for metadata (or externally
1703 	 * controlled, for example, OVS), then nothing can be changed.
1704 	 */
1705 	if (geneve->cfg.collect_md)
1706 		return -EOPNOTSUPP;
1707 
1708 	/* Start with the existing info. */
1709 	memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1710 	err = geneve_nl2info(tb, data, extack, &cfg, true);
1711 	if (err)
1712 		return err;
1713 
1714 	if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1715 		dst_cache_reset(&cfg.info.dst_cache);
1716 		geneve_link_config(dev, &cfg.info, tb);
1717 	}
1718 
1719 	geneve_quiesce(geneve, &gs4, &gs6);
1720 	memcpy(&geneve->cfg, &cfg, sizeof(cfg));
1721 	geneve_unquiesce(geneve, gs4, gs6);
1722 
1723 	return 0;
1724 }
1725 
1726 static void geneve_dellink(struct net_device *dev, struct list_head *head)
1727 {
1728 	struct geneve_dev *geneve = netdev_priv(dev);
1729 
1730 	list_del(&geneve->next);
1731 	unregister_netdevice_queue(dev, head);
1732 }
1733 
1734 static size_t geneve_get_size(const struct net_device *dev)
1735 {
1736 	return nla_total_size(sizeof(__u32)) +	/* IFLA_GENEVE_ID */
1737 		nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
1738 		nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TTL */
1739 		nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TOS */
1740 		nla_total_size(sizeof(__u8)) +	/* IFLA_GENEVE_DF */
1741 		nla_total_size(sizeof(__be32)) +  /* IFLA_GENEVE_LABEL */
1742 		nla_total_size(sizeof(__be16)) +  /* IFLA_GENEVE_PORT */
1743 		nla_total_size(0) +	 /* IFLA_GENEVE_COLLECT_METADATA */
1744 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1745 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1746 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1747 		nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
1748 		0;
1749 }
1750 
1751 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1752 {
1753 	struct geneve_dev *geneve = netdev_priv(dev);
1754 	struct ip_tunnel_info *info = &geneve->cfg.info;
1755 	bool ttl_inherit = geneve->cfg.ttl_inherit;
1756 	bool metadata = geneve->cfg.collect_md;
1757 	__u8 tmp_vni[3];
1758 	__u32 vni;
1759 
1760 	tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1761 	vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
1762 	if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1763 		goto nla_put_failure;
1764 
1765 	if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
1766 		if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1767 				    info->key.u.ipv4.dst))
1768 			goto nla_put_failure;
1769 		if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1770 			       !!(info->key.tun_flags & TUNNEL_CSUM)))
1771 			goto nla_put_failure;
1772 
1773 #if IS_ENABLED(CONFIG_IPV6)
1774 	} else if (!metadata) {
1775 		if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1776 				     &info->key.u.ipv6.dst))
1777 			goto nla_put_failure;
1778 		if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1779 			       !(info->key.tun_flags & TUNNEL_CSUM)))
1780 			goto nla_put_failure;
1781 #endif
1782 	}
1783 
1784 	if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1785 	    nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1786 	    nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
1787 		goto nla_put_failure;
1788 
1789 	if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
1790 		goto nla_put_failure;
1791 
1792 	if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
1793 		goto nla_put_failure;
1794 
1795 	if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1796 		goto nla_put_failure;
1797 
1798 #if IS_ENABLED(CONFIG_IPV6)
1799 	if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1800 		       !geneve->cfg.use_udp6_rx_checksums))
1801 		goto nla_put_failure;
1802 #endif
1803 
1804 	if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1805 		goto nla_put_failure;
1806 
1807 	return 0;
1808 
1809 nla_put_failure:
1810 	return -EMSGSIZE;
1811 }
1812 
1813 static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1814 	.kind		= "geneve",
1815 	.maxtype	= IFLA_GENEVE_MAX,
1816 	.policy		= geneve_policy,
1817 	.priv_size	= sizeof(struct geneve_dev),
1818 	.setup		= geneve_setup,
1819 	.validate	= geneve_validate,
1820 	.newlink	= geneve_newlink,
1821 	.changelink	= geneve_changelink,
1822 	.dellink	= geneve_dellink,
1823 	.get_size	= geneve_get_size,
1824 	.fill_info	= geneve_fill_info,
1825 };
1826 
1827 struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1828 					u8 name_assign_type, u16 dst_port)
1829 {
1830 	struct nlattr *tb[IFLA_MAX + 1];
1831 	struct net_device *dev;
1832 	LIST_HEAD(list_kill);
1833 	int err;
1834 	struct geneve_config cfg = {
1835 		.df = GENEVE_DF_UNSET,
1836 		.use_udp6_rx_checksums = true,
1837 		.ttl_inherit = false,
1838 		.collect_md = true,
1839 	};
1840 
1841 	memset(tb, 0, sizeof(tb));
1842 	dev = rtnl_create_link(net, name, name_assign_type,
1843 			       &geneve_link_ops, tb, NULL);
1844 	if (IS_ERR(dev))
1845 		return dev;
1846 
1847 	init_tnl_info(&cfg.info, dst_port);
1848 	err = geneve_configure(net, dev, NULL, &cfg);
1849 	if (err) {
1850 		free_netdev(dev);
1851 		return ERR_PTR(err);
1852 	}
1853 
1854 	/* openvswitch users expect packet sizes to be unrestricted,
1855 	 * so set the largest MTU we can.
1856 	 */
1857 	err = geneve_change_mtu(dev, IP_MAX_MTU);
1858 	if (err)
1859 		goto err;
1860 
1861 	err = rtnl_configure_link(dev, NULL);
1862 	if (err < 0)
1863 		goto err;
1864 
1865 	return dev;
1866 err:
1867 	geneve_dellink(dev, &list_kill);
1868 	unregister_netdevice_many(&list_kill);
1869 	return ERR_PTR(err);
1870 }
1871 EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1872 
1873 static int geneve_netdevice_event(struct notifier_block *unused,
1874 				  unsigned long event, void *ptr)
1875 {
1876 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1877 
1878 	if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1879 		geneve_offload_rx_ports(dev, true);
1880 	else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1881 		geneve_offload_rx_ports(dev, false);
1882 
1883 	return NOTIFY_DONE;
1884 }
1885 
1886 static struct notifier_block geneve_notifier_block __read_mostly = {
1887 	.notifier_call = geneve_netdevice_event,
1888 };
1889 
1890 static __net_init int geneve_init_net(struct net *net)
1891 {
1892 	struct geneve_net *gn = net_generic(net, geneve_net_id);
1893 
1894 	INIT_LIST_HEAD(&gn->geneve_list);
1895 	INIT_LIST_HEAD(&gn->sock_list);
1896 	return 0;
1897 }
1898 
1899 static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
1900 {
1901 	struct geneve_net *gn = net_generic(net, geneve_net_id);
1902 	struct geneve_dev *geneve, *next;
1903 	struct net_device *dev, *aux;
1904 
1905 	/* gather any geneve devices that were moved into this ns */
1906 	for_each_netdev_safe(net, dev, aux)
1907 		if (dev->rtnl_link_ops == &geneve_link_ops)
1908 			unregister_netdevice_queue(dev, head);
1909 
1910 	/* now gather any other geneve devices that were created in this ns */
1911 	list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1912 		/* If geneve->dev is in the same netns, it was already added
1913 		 * to the list by the previous loop.
1914 		 */
1915 		if (!net_eq(dev_net(geneve->dev), net))
1916 			unregister_netdevice_queue(geneve->dev, head);
1917 	}
1918 }
1919 
1920 static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1921 {
1922 	struct net *net;
1923 	LIST_HEAD(list);
1924 
1925 	rtnl_lock();
1926 	list_for_each_entry(net, net_list, exit_list)
1927 		geneve_destroy_tunnels(net, &list);
1928 
1929 	/* unregister the devices gathered above */
1930 	unregister_netdevice_many(&list);
1931 	rtnl_unlock();
1932 
1933 	list_for_each_entry(net, net_list, exit_list) {
1934 		const struct geneve_net *gn = net_generic(net, geneve_net_id);
1935 
1936 		WARN_ON_ONCE(!list_empty(&gn->sock_list));
1937 	}
1938 }
1939 
1940 static struct pernet_operations geneve_net_ops = {
1941 	.init = geneve_init_net,
1942 	.exit_batch = geneve_exit_batch_net,
1943 	.id   = &geneve_net_id,
1944 	.size = sizeof(struct geneve_net),
1945 };
1946 
1947 static int __init geneve_init_module(void)
1948 {
1949 	int rc;
1950 
1951 	rc = register_pernet_subsys(&geneve_net_ops);
1952 	if (rc)
1953 		goto out1;
1954 
1955 	rc = register_netdevice_notifier(&geneve_notifier_block);
1956 	if (rc)
1957 		goto out2;
1958 
1959 	rc = rtnl_link_register(&geneve_link_ops);
1960 	if (rc)
1961 		goto out3;
1962 
1963 	return 0;
1964 out3:
1965 	unregister_netdevice_notifier(&geneve_notifier_block);
1966 out2:
1967 	unregister_pernet_subsys(&geneve_net_ops);
1968 out1:
1969 	return rc;
1970 }
1971 late_initcall(geneve_init_module);
1972 
1973 static void __exit geneve_cleanup_module(void)
1974 {
1975 	rtnl_link_unregister(&geneve_link_ops);
1976 	unregister_netdevice_notifier(&geneve_notifier_block);
1977 	unregister_pernet_subsys(&geneve_net_ops);
1978 }
1979 module_exit(geneve_cleanup_module);
1980 
1981 MODULE_LICENSE("GPL");
1982 MODULE_VERSION(GENEVE_NETDEV_VER);
1983 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1984 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1985 MODULE_ALIAS_RTNL_LINK("geneve");
1986