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