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