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