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