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