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