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