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