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