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