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