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