1 // SPDX-License-Identifier: GPL-2.0-only
3 * GENEVE: Generic Network Virtualization Encapsulation
5 * Copyright (c) 2015 Red Hat, Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/ethtool.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/etherdevice.h>
14 #include <linux/hash.h>
15 #include <net/ipv6_stubs.h>
16 #include <net/dst_metadata.h>
17 #include <net/gro_cells.h>
18 #include <net/rtnetlink.h>
19 #include <net/geneve.h>
21 #include <net/protocol.h>
23 #define GENEVE_NETDEV_VER "0.6"
25 #define GENEVE_N_VID (1u << 24)
26 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
28 #define VNI_HASH_BITS 10
29 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
31 static bool log_ecn_error = true;
32 module_param(log_ecn_error, bool, 0644);
33 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
36 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
37 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
38 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
40 /* per-network namespace private data for this module */
42 struct list_head geneve_list;
43 struct list_head sock_list;
46 static unsigned int geneve_net_id;
48 struct geneve_dev_node {
49 struct hlist_node hlist;
50 struct geneve_dev *geneve;
53 struct geneve_config {
54 struct ip_tunnel_info info;
56 bool use_udp6_rx_checksums;
58 enum ifla_geneve_df df;
59 bool inner_proto_inherit;
62 /* Pseudo network device */
64 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
65 #if IS_ENABLED(CONFIG_IPV6)
66 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
68 struct net *net; /* netns for packet i/o */
69 struct net_device *dev; /* netdev for geneve tunnel */
70 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
71 #if IS_ENABLED(CONFIG_IPV6)
72 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
74 struct list_head next; /* geneve's per namespace list */
75 struct gro_cells gro_cells;
76 struct geneve_config cfg;
81 struct list_head list;
85 struct hlist_head vni_list[VNI_HASH_SIZE];
88 static inline __u32 geneve_net_vni_hash(u8 vni[3])
92 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
93 return hash_32(vnid, VNI_HASH_BITS);
96 static __be64 vni_to_tunnel_id(const __u8 *vni)
99 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
101 return (__force __be64)(((__force u64)vni[0] << 40) |
102 ((__force u64)vni[1] << 48) |
103 ((__force u64)vni[2] << 56));
107 /* Convert 64 bit tunnel ID to 24 bit VNI. */
108 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
111 vni[0] = (__force __u8)(tun_id >> 16);
112 vni[1] = (__force __u8)(tun_id >> 8);
113 vni[2] = (__force __u8)tun_id;
115 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
116 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
117 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
121 static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
123 return !memcmp(vni, &tun_id[5], 3);
126 static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
128 return gs->sock->sk->sk_family;
131 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
132 __be32 addr, u8 vni[])
134 struct hlist_head *vni_list_head;
135 struct geneve_dev_node *node;
138 /* Find the device for this VNI */
139 hash = geneve_net_vni_hash(vni);
140 vni_list_head = &gs->vni_list[hash];
141 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
142 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
143 addr == node->geneve->cfg.info.key.u.ipv4.dst)
149 #if IS_ENABLED(CONFIG_IPV6)
150 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
151 struct in6_addr addr6, u8 vni[])
153 struct hlist_head *vni_list_head;
154 struct geneve_dev_node *node;
157 /* Find the device for this VNI */
158 hash = geneve_net_vni_hash(vni);
159 vni_list_head = &gs->vni_list[hash];
160 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
161 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
162 ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
169 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
171 return (struct genevehdr *)(udp_hdr(skb) + 1);
174 static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
177 static u8 zero_vni[3];
180 if (geneve_get_sk_family(gs) == AF_INET) {
184 iph = ip_hdr(skb); /* outer IP header... */
186 if (gs->collect_md) {
190 vni = geneve_hdr(skb)->vni;
194 return geneve_lookup(gs, addr, vni);
195 #if IS_ENABLED(CONFIG_IPV6)
196 } else if (geneve_get_sk_family(gs) == AF_INET6) {
197 static struct in6_addr zero_addr6;
198 struct ipv6hdr *ip6h;
199 struct in6_addr addr6;
201 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
203 if (gs->collect_md) {
207 vni = geneve_hdr(skb)->vni;
211 return geneve6_lookup(gs, addr6, vni);
217 /* geneve receive/decap routine */
218 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
221 struct genevehdr *gnvh = geneve_hdr(skb);
222 struct metadata_dst *tun_dst = NULL;
227 if (ip_tunnel_collect_metadata() || gs->collect_md) {
230 flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
231 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
233 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
234 vni_to_tunnel_id(gnvh->vni),
237 geneve->dev->stats.rx_dropped++;
240 /* Update tunnel dst according to Geneve options. */
241 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
242 gnvh->options, gnvh->opt_len * 4,
245 /* Drop packets w/ critical options,
246 * since we don't support any...
248 if (gnvh->critical) {
249 geneve->dev->stats.rx_frame_errors++;
250 geneve->dev->stats.rx_errors++;
256 skb_dst_set(skb, &tun_dst->dst);
258 if (gnvh->proto_type == htons(ETH_P_TEB)) {
259 skb_reset_mac_header(skb);
260 skb->protocol = eth_type_trans(skb, geneve->dev);
261 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
263 /* Ignore packet loops (and multicast echo) */
264 if (ether_addr_equal(eth_hdr(skb)->h_source,
265 geneve->dev->dev_addr)) {
266 geneve->dev->stats.rx_errors++;
270 skb_reset_mac_header(skb);
271 skb->dev = geneve->dev;
272 skb->pkt_type = PACKET_HOST;
275 oiph = skb_network_header(skb);
276 skb_reset_network_header(skb);
278 if (geneve_get_sk_family(gs) == AF_INET)
279 err = IP_ECN_decapsulate(oiph, skb);
280 #if IS_ENABLED(CONFIG_IPV6)
282 err = IP6_ECN_decapsulate(oiph, skb);
287 if (geneve_get_sk_family(gs) == AF_INET)
288 net_info_ratelimited("non-ECT from %pI4 "
290 &((struct iphdr *)oiph)->saddr,
291 ((struct iphdr *)oiph)->tos);
292 #if IS_ENABLED(CONFIG_IPV6)
294 net_info_ratelimited("non-ECT from %pI6\n",
295 &((struct ipv6hdr *)oiph)->saddr);
299 ++geneve->dev->stats.rx_frame_errors;
300 ++geneve->dev->stats.rx_errors;
306 err = gro_cells_receive(&geneve->gro_cells, skb);
307 if (likely(err == NET_RX_SUCCESS))
308 dev_sw_netstats_rx_add(geneve->dev, len);
312 /* Consume bad packet */
316 /* Setup stats when device is created */
317 static int geneve_init(struct net_device *dev)
319 struct geneve_dev *geneve = netdev_priv(dev);
322 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
326 err = gro_cells_init(&geneve->gro_cells, dev);
328 free_percpu(dev->tstats);
332 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
334 free_percpu(dev->tstats);
335 gro_cells_destroy(&geneve->gro_cells);
341 static void geneve_uninit(struct net_device *dev)
343 struct geneve_dev *geneve = netdev_priv(dev);
345 dst_cache_destroy(&geneve->cfg.info.dst_cache);
346 gro_cells_destroy(&geneve->gro_cells);
347 free_percpu(dev->tstats);
350 /* Callback from net/ipv4/udp.c to receive packets */
351 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
353 struct genevehdr *geneveh;
354 struct geneve_dev *geneve;
355 struct geneve_sock *gs;
359 /* Need UDP and Geneve header to be present */
360 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
363 /* Return packets with reserved bits set */
364 geneveh = geneve_hdr(skb);
365 if (unlikely(geneveh->ver != GENEVE_VER))
368 gs = rcu_dereference_sk_user_data(sk);
372 geneve = geneve_lookup_skb(gs, skb);
376 inner_proto = geneveh->proto_type;
378 if (unlikely((!geneve->cfg.inner_proto_inherit &&
379 inner_proto != htons(ETH_P_TEB)))) {
380 geneve->dev->stats.rx_dropped++;
384 opts_len = geneveh->opt_len * 4;
385 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, inner_proto,
386 !net_eq(geneve->net, dev_net(geneve->dev)))) {
387 geneve->dev->stats.rx_dropped++;
391 geneve_rx(geneve, gs, skb);
395 /* Consume bad packet */
400 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
401 static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
403 struct genevehdr *geneveh;
404 struct geneve_sock *gs;
405 u8 zero_vni[3] = { 0 };
408 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
411 geneveh = geneve_hdr(skb);
412 if (geneveh->ver != GENEVE_VER)
415 if (geneveh->proto_type != htons(ETH_P_TEB))
418 gs = rcu_dereference_sk_user_data(sk);
422 if (geneve_get_sk_family(gs) == AF_INET) {
423 struct iphdr *iph = ip_hdr(skb);
426 if (!gs->collect_md) {
427 vni = geneve_hdr(skb)->vni;
431 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
434 #if IS_ENABLED(CONFIG_IPV6)
435 if (geneve_get_sk_family(gs) == AF_INET6) {
436 struct ipv6hdr *ip6h = ipv6_hdr(skb);
437 struct in6_addr addr6;
439 memset(&addr6, 0, sizeof(struct in6_addr));
441 if (!gs->collect_md) {
442 vni = geneve_hdr(skb)->vni;
446 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
450 return -EPFNOSUPPORT;
453 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
454 __be16 port, bool ipv6_rx_csum)
457 struct udp_port_cfg udp_conf;
460 memset(&udp_conf, 0, sizeof(udp_conf));
463 udp_conf.family = AF_INET6;
464 udp_conf.ipv6_v6only = 1;
465 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
467 udp_conf.family = AF_INET;
468 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
471 udp_conf.local_udp_port = port;
473 /* Open UDP socket */
474 err = udp_sock_create(net, &udp_conf, &sock);
478 udp_allow_gso(sock->sk);
482 static int geneve_hlen(struct genevehdr *gh)
484 return sizeof(*gh) + gh->opt_len * 4;
487 static struct sk_buff *geneve_gro_receive(struct sock *sk,
488 struct list_head *head,
491 struct sk_buff *pp = NULL;
493 struct genevehdr *gh, *gh2;
494 unsigned int hlen, gh_len, off_gnv;
495 const struct packet_offload *ptype;
499 off_gnv = skb_gro_offset(skb);
500 hlen = off_gnv + sizeof(*gh);
501 gh = skb_gro_header(skb, hlen, off_gnv);
505 if (gh->ver != GENEVE_VER || gh->oam)
507 gh_len = geneve_hlen(gh);
509 hlen = off_gnv + gh_len;
510 if (skb_gro_header_hard(skb, hlen)) {
511 gh = skb_gro_header_slow(skb, hlen, off_gnv);
516 list_for_each_entry(p, head, list) {
517 if (!NAPI_GRO_CB(p)->same_flow)
520 gh2 = (struct genevehdr *)(p->data + off_gnv);
521 if (gh->opt_len != gh2->opt_len ||
522 memcmp(gh, gh2, gh_len)) {
523 NAPI_GRO_CB(p)->same_flow = 0;
528 skb_gro_pull(skb, gh_len);
529 skb_gro_postpull_rcsum(skb, gh, gh_len);
530 type = gh->proto_type;
531 if (likely(type == htons(ETH_P_TEB)))
532 return call_gro_receive(eth_gro_receive, head, skb);
534 ptype = gro_find_receive_by_type(type);
538 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
542 skb_gro_flush_final(skb, pp, flush);
547 static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
550 struct genevehdr *gh;
551 struct packet_offload *ptype;
556 gh = (struct genevehdr *)(skb->data + nhoff);
557 gh_len = geneve_hlen(gh);
558 type = gh->proto_type;
560 /* since skb->encapsulation is set, eth_gro_complete() sets the inner mac header */
561 if (likely(type == htons(ETH_P_TEB)))
562 return eth_gro_complete(skb, nhoff + gh_len);
564 ptype = gro_find_complete_by_type(type);
566 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
568 skb_set_inner_mac_header(skb, nhoff + gh_len);
573 /* Create new listen socket if needed */
574 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
575 bool ipv6, bool ipv6_rx_csum)
577 struct geneve_net *gn = net_generic(net, geneve_net_id);
578 struct geneve_sock *gs;
580 struct udp_tunnel_sock_cfg tunnel_cfg;
583 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
585 return ERR_PTR(-ENOMEM);
587 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
590 return ERR_CAST(sock);
595 for (h = 0; h < VNI_HASH_SIZE; ++h)
596 INIT_HLIST_HEAD(&gs->vni_list[h]);
598 /* Initialize the geneve udp offloads structure */
599 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
601 /* Mark socket as an encapsulation socket */
602 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
603 tunnel_cfg.sk_user_data = gs;
604 tunnel_cfg.encap_type = 1;
605 tunnel_cfg.gro_receive = geneve_gro_receive;
606 tunnel_cfg.gro_complete = geneve_gro_complete;
607 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
608 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
609 tunnel_cfg.encap_destroy = NULL;
610 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
611 list_add(&gs->list, &gn->sock_list);
615 static void __geneve_sock_release(struct geneve_sock *gs)
617 if (!gs || --gs->refcnt)
621 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
622 udp_tunnel_sock_release(gs->sock);
626 static void geneve_sock_release(struct geneve_dev *geneve)
628 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
629 #if IS_ENABLED(CONFIG_IPV6)
630 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
632 rcu_assign_pointer(geneve->sock6, NULL);
635 rcu_assign_pointer(geneve->sock4, NULL);
638 __geneve_sock_release(gs4);
639 #if IS_ENABLED(CONFIG_IPV6)
640 __geneve_sock_release(gs6);
644 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
648 struct geneve_sock *gs;
650 list_for_each_entry(gs, &gn->sock_list, list) {
651 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
652 geneve_get_sk_family(gs) == family) {
659 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
661 struct net *net = geneve->net;
662 struct geneve_net *gn = net_generic(net, geneve_net_id);
663 struct geneve_dev_node *node;
664 struct geneve_sock *gs;
668 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
674 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
675 geneve->cfg.use_udp6_rx_checksums);
680 gs->collect_md = geneve->cfg.collect_md;
681 #if IS_ENABLED(CONFIG_IPV6)
683 rcu_assign_pointer(geneve->sock6, gs);
684 node = &geneve->hlist6;
688 rcu_assign_pointer(geneve->sock4, gs);
689 node = &geneve->hlist4;
691 node->geneve = geneve;
693 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
694 hash = geneve_net_vni_hash(vni);
695 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
699 static int geneve_open(struct net_device *dev)
701 struct geneve_dev *geneve = netdev_priv(dev);
702 bool metadata = geneve->cfg.collect_md;
706 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
707 ipv4 = !ipv6 || metadata;
708 #if IS_ENABLED(CONFIG_IPV6)
710 ret = geneve_sock_add(geneve, true);
711 if (ret < 0 && ret != -EAFNOSUPPORT)
716 ret = geneve_sock_add(geneve, false);
718 geneve_sock_release(geneve);
723 static int geneve_stop(struct net_device *dev)
725 struct geneve_dev *geneve = netdev_priv(dev);
727 hlist_del_init_rcu(&geneve->hlist4.hlist);
728 #if IS_ENABLED(CONFIG_IPV6)
729 hlist_del_init_rcu(&geneve->hlist6.hlist);
731 geneve_sock_release(geneve);
735 static void geneve_build_header(struct genevehdr *geneveh,
736 const struct ip_tunnel_info *info,
739 geneveh->ver = GENEVE_VER;
740 geneveh->opt_len = info->options_len / 4;
741 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
742 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
744 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
745 geneveh->proto_type = inner_proto;
748 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
749 ip_tunnel_info_opts_get(geneveh->options, info);
752 static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
753 const struct ip_tunnel_info *info,
754 bool xnet, int ip_hdr_len,
755 bool inner_proto_inherit)
757 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
758 struct genevehdr *gnvh;
763 skb_reset_mac_header(skb);
764 skb_scrub_packet(skb, xnet);
766 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
767 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
768 err = skb_cow_head(skb, min_headroom);
772 err = udp_tunnel_handle_offloads(skb, udp_sum);
776 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
777 inner_proto = inner_proto_inherit ? skb->protocol : htons(ETH_P_TEB);
778 geneve_build_header(gnvh, info, inner_proto);
779 skb_set_inner_protocol(skb, inner_proto);
787 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
788 struct net_device *dev,
789 struct geneve_sock *gs4,
791 const struct ip_tunnel_info *info,
792 __be16 dport, __be16 sport,
795 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
796 struct geneve_dev *geneve = netdev_priv(dev);
797 struct dst_cache *dst_cache;
798 struct rtable *rt = NULL;
802 return ERR_PTR(-EIO);
804 memset(fl4, 0, sizeof(*fl4));
805 fl4->flowi4_mark = skb->mark;
806 fl4->flowi4_proto = IPPROTO_UDP;
807 fl4->daddr = info->key.u.ipv4.dst;
808 fl4->saddr = info->key.u.ipv4.src;
809 fl4->fl4_dport = dport;
810 fl4->fl4_sport = sport;
811 fl4->flowi4_flags = info->key.flow_flags;
814 if ((tos == 1) && !geneve->cfg.collect_md) {
815 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
818 fl4->flowi4_tos = RT_TOS(tos);
822 dst_cache = (struct dst_cache *)&info->dst_cache;
824 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
828 rt = ip_route_output_key(geneve->net, fl4);
830 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
831 return ERR_PTR(-ENETUNREACH);
833 if (rt->dst.dev == dev) { /* is this necessary? */
834 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
836 return ERR_PTR(-ELOOP);
839 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
843 #if IS_ENABLED(CONFIG_IPV6)
844 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
845 struct net_device *dev,
846 struct geneve_sock *gs6,
848 const struct ip_tunnel_info *info,
849 __be16 dport, __be16 sport)
851 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
852 struct geneve_dev *geneve = netdev_priv(dev);
853 struct dst_entry *dst = NULL;
854 struct dst_cache *dst_cache;
858 return ERR_PTR(-EIO);
860 memset(fl6, 0, sizeof(*fl6));
861 fl6->flowi6_mark = skb->mark;
862 fl6->flowi6_proto = IPPROTO_UDP;
863 fl6->daddr = info->key.u.ipv6.dst;
864 fl6->saddr = info->key.u.ipv6.src;
865 fl6->fl6_dport = dport;
866 fl6->fl6_sport = sport;
868 prio = info->key.tos;
869 if ((prio == 1) && !geneve->cfg.collect_md) {
870 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
874 fl6->flowlabel = ip6_make_flowinfo(prio, info->key.label);
875 dst_cache = (struct dst_cache *)&info->dst_cache;
877 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
881 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
884 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
885 return ERR_PTR(-ENETUNREACH);
887 if (dst->dev == dev) { /* is this necessary? */
888 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
890 return ERR_PTR(-ELOOP);
894 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
899 static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
900 struct geneve_dev *geneve,
901 const struct ip_tunnel_info *info)
903 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
904 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
905 const struct ip_tunnel_key *key = &info->key;
914 if (!pskb_inet_may_pull(skb))
917 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
918 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
919 geneve->cfg.info.key.tp_dst, sport, &full_tos);
923 err = skb_tunnel_check_pmtu(skb, &rt->dst,
924 GENEVE_IPV4_HLEN + info->options_len,
925 netif_is_any_bridge_port(dev));
927 dst_release(&rt->dst);
930 struct ip_tunnel_info *info;
932 info = skb_tunnel_info(skb);
934 struct ip_tunnel_info *unclone;
936 unclone = skb_tunnel_info_unclone(skb);
937 if (unlikely(!unclone)) {
938 dst_release(&rt->dst);
942 unclone->key.u.ipv4.dst = fl4.saddr;
943 unclone->key.u.ipv4.src = fl4.daddr;
946 if (!pskb_may_pull(skb, ETH_HLEN)) {
947 dst_release(&rt->dst);
951 skb->protocol = eth_type_trans(skb, geneve->dev);
953 dst_release(&rt->dst);
957 if (geneve->cfg.collect_md) {
958 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
961 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
963 tos = ip_tunnel_ecn_encap(full_tos, ip_hdr(skb), skb);
964 if (geneve->cfg.ttl_inherit)
965 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
968 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
970 if (geneve->cfg.df == GENEVE_DF_SET) {
972 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
973 struct ethhdr *eth = eth_hdr(skb);
975 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
977 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
978 struct iphdr *iph = ip_hdr(skb);
980 if (iph->frag_off & htons(IP_DF))
986 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr),
987 geneve->cfg.inner_proto_inherit);
991 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
992 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
993 !net_eq(geneve->net, dev_net(geneve->dev)),
994 !(info->key.tun_flags & TUNNEL_CSUM));
998 #if IS_ENABLED(CONFIG_IPV6)
999 static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
1000 struct geneve_dev *geneve,
1001 const struct ip_tunnel_info *info)
1003 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
1004 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1005 const struct ip_tunnel_key *key = &info->key;
1006 struct dst_entry *dst = NULL;
1012 if (!pskb_inet_may_pull(skb))
1015 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
1016 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1017 geneve->cfg.info.key.tp_dst, sport);
1019 return PTR_ERR(dst);
1021 err = skb_tunnel_check_pmtu(skb, dst,
1022 GENEVE_IPV6_HLEN + info->options_len,
1023 netif_is_any_bridge_port(dev));
1028 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1031 struct ip_tunnel_info *unclone;
1033 unclone = skb_tunnel_info_unclone(skb);
1034 if (unlikely(!unclone)) {
1039 unclone->key.u.ipv6.dst = fl6.saddr;
1040 unclone->key.u.ipv6.src = fl6.daddr;
1043 if (!pskb_may_pull(skb, ETH_HLEN)) {
1048 skb->protocol = eth_type_trans(skb, geneve->dev);
1054 if (geneve->cfg.collect_md) {
1055 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
1058 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
1060 if (geneve->cfg.ttl_inherit)
1061 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1064 ttl = ttl ? : ip6_dst_hoplimit(dst);
1066 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr),
1067 geneve->cfg.inner_proto_inherit);
1071 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
1072 &fl6.saddr, &fl6.daddr, prio, ttl,
1073 info->key.label, sport, geneve->cfg.info.key.tp_dst,
1074 !(info->key.tun_flags & TUNNEL_CSUM));
1079 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1081 struct geneve_dev *geneve = netdev_priv(dev);
1082 struct ip_tunnel_info *info = NULL;
1085 if (geneve->cfg.collect_md) {
1086 info = skb_tunnel_info(skb);
1087 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
1088 netdev_dbg(dev, "no tunnel metadata\n");
1090 dev->stats.tx_dropped++;
1091 return NETDEV_TX_OK;
1094 info = &geneve->cfg.info;
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 if (info->mode & IP_TUNNEL_INFO_IPV6)
1100 err = geneve6_xmit_skb(skb, dev, geneve, info);
1103 err = geneve_xmit_skb(skb, dev, geneve, info);
1107 return NETDEV_TX_OK;
1109 if (err != -EMSGSIZE)
1113 dev->stats.collisions++;
1114 else if (err == -ENETUNREACH)
1115 dev->stats.tx_carrier_errors++;
1117 dev->stats.tx_errors++;
1118 return NETDEV_TX_OK;
1121 static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1123 if (new_mtu > dev->max_mtu)
1124 new_mtu = dev->max_mtu;
1125 else if (new_mtu < dev->min_mtu)
1126 new_mtu = dev->min_mtu;
1132 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1134 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1135 struct geneve_dev *geneve = netdev_priv(dev);
1138 if (ip_tunnel_info_af(info) == AF_INET) {
1142 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1143 sport = udp_flow_src_port(geneve->net, skb,
1144 1, USHRT_MAX, true);
1146 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1147 geneve->cfg.info.key.tp_dst, sport, NULL);
1152 info->key.u.ipv4.src = fl4.saddr;
1153 #if IS_ENABLED(CONFIG_IPV6)
1154 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1155 struct dst_entry *dst;
1158 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1159 sport = udp_flow_src_port(geneve->net, skb,
1160 1, USHRT_MAX, true);
1162 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1163 geneve->cfg.info.key.tp_dst, sport);
1165 return PTR_ERR(dst);
1168 info->key.u.ipv6.src = fl6.saddr;
1174 info->key.tp_src = sport;
1175 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
1179 static const struct net_device_ops geneve_netdev_ops = {
1180 .ndo_init = geneve_init,
1181 .ndo_uninit = geneve_uninit,
1182 .ndo_open = geneve_open,
1183 .ndo_stop = geneve_stop,
1184 .ndo_start_xmit = geneve_xmit,
1185 .ndo_get_stats64 = dev_get_tstats64,
1186 .ndo_change_mtu = geneve_change_mtu,
1187 .ndo_validate_addr = eth_validate_addr,
1188 .ndo_set_mac_address = eth_mac_addr,
1189 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
1192 static void geneve_get_drvinfo(struct net_device *dev,
1193 struct ethtool_drvinfo *drvinfo)
1195 strscpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1196 strscpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1199 static const struct ethtool_ops geneve_ethtool_ops = {
1200 .get_drvinfo = geneve_get_drvinfo,
1201 .get_link = ethtool_op_get_link,
1204 /* Info for udev, that this is a virtual tunnel endpoint */
1205 static struct device_type geneve_type = {
1209 /* Calls the ndo_udp_tunnel_add of the caller in order to
1210 * supply the listening GENEVE udp ports. Callers are expected
1211 * to implement the ndo_udp_tunnel_add.
1213 static void geneve_offload_rx_ports(struct net_device *dev, bool push)
1215 struct net *net = dev_net(dev);
1216 struct geneve_net *gn = net_generic(net, geneve_net_id);
1217 struct geneve_sock *gs;
1220 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1222 udp_tunnel_push_rx_port(dev, gs->sock,
1223 UDP_TUNNEL_TYPE_GENEVE);
1225 udp_tunnel_drop_rx_port(dev, gs->sock,
1226 UDP_TUNNEL_TYPE_GENEVE);
1232 /* Initialize the device structure. */
1233 static void geneve_setup(struct net_device *dev)
1237 dev->netdev_ops = &geneve_netdev_ops;
1238 dev->ethtool_ops = &geneve_ethtool_ops;
1239 dev->needs_free_netdev = true;
1241 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1243 dev->features |= NETIF_F_LLTX;
1244 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1245 dev->features |= NETIF_F_RXCSUM;
1246 dev->features |= NETIF_F_GSO_SOFTWARE;
1248 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1249 dev->hw_features |= NETIF_F_RXCSUM;
1250 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1252 /* MTU range: 68 - (something less than 65535) */
1253 dev->min_mtu = ETH_MIN_MTU;
1254 /* The max_mtu calculation does not take account of GENEVE
1255 * options, to avoid excluding potentially valid
1256 * configurations. This will be further reduced by IPvX hdr size.
1258 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1260 netif_keep_dst(dev);
1261 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1262 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1263 eth_hw_addr_random(dev);
1266 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1267 [IFLA_GENEVE_UNSPEC] = { .strict_start_type = IFLA_GENEVE_INNER_PROTO_INHERIT },
1268 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1269 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
1270 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
1271 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
1272 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
1273 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
1274 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
1275 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
1276 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1277 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1278 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
1279 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
1280 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
1281 [IFLA_GENEVE_INNER_PROTO_INHERIT] = { .type = NLA_FLAG },
1284 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1285 struct netlink_ext_ack *extack)
1287 if (tb[IFLA_ADDRESS]) {
1288 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1289 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1290 "Provided link layer address is not Ethernet");
1294 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1295 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1296 "Provided Ethernet address is not unicast");
1297 return -EADDRNOTAVAIL;
1302 NL_SET_ERR_MSG(extack,
1303 "Not enough attributes provided to perform the operation");
1307 if (data[IFLA_GENEVE_ID]) {
1308 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1310 if (vni >= GENEVE_N_VID) {
1311 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1312 "Geneve ID must be lower than 16777216");
1317 if (data[IFLA_GENEVE_DF]) {
1318 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1320 if (df < 0 || df > GENEVE_DF_MAX) {
1321 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
1322 "Invalid DF attribute");
1330 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1331 const struct ip_tunnel_info *info,
1332 bool *tun_on_same_port,
1333 bool *tun_collect_md)
1335 struct geneve_dev *geneve, *t = NULL;
1337 *tun_on_same_port = false;
1338 *tun_collect_md = false;
1339 list_for_each_entry(geneve, &gn->geneve_list, next) {
1340 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1341 *tun_collect_md = geneve->cfg.collect_md;
1342 *tun_on_same_port = true;
1344 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1345 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1346 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
1352 static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1354 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1355 info->key.ttl || info->key.label || info->key.tp_src ||
1356 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
1359 static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1360 struct ip_tunnel_info *b)
1362 if (ip_tunnel_info_af(a) == AF_INET)
1363 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1365 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1368 static int geneve_configure(struct net *net, struct net_device *dev,
1369 struct netlink_ext_ack *extack,
1370 const struct geneve_config *cfg)
1372 struct geneve_net *gn = net_generic(net, geneve_net_id);
1373 struct geneve_dev *t, *geneve = netdev_priv(dev);
1374 const struct ip_tunnel_info *info = &cfg->info;
1375 bool tun_collect_md, tun_on_same_port;
1378 if (cfg->collect_md && !is_tnl_info_zero(info)) {
1379 NL_SET_ERR_MSG(extack,
1380 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
1387 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
1391 /* make enough headroom for basic scenario */
1392 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1393 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
1394 encap_len += sizeof(struct iphdr);
1395 dev->max_mtu -= sizeof(struct iphdr);
1397 encap_len += sizeof(struct ipv6hdr);
1398 dev->max_mtu -= sizeof(struct ipv6hdr);
1400 dev->needed_headroom = encap_len + ETH_HLEN;
1402 if (cfg->collect_md) {
1403 if (tun_on_same_port) {
1404 NL_SET_ERR_MSG(extack,
1405 "There can be only one externally controlled device on a destination port");
1409 if (tun_collect_md) {
1410 NL_SET_ERR_MSG(extack,
1411 "There already exists an externally controlled device on this destination port");
1416 dst_cache_reset(&geneve->cfg.info.dst_cache);
1417 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
1419 if (geneve->cfg.inner_proto_inherit) {
1420 dev->header_ops = NULL;
1421 dev->type = ARPHRD_NONE;
1422 dev->hard_header_len = 0;
1424 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1427 err = register_netdevice(dev);
1431 list_add(&geneve->next, &gn->geneve_list);
1435 static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1437 memset(info, 0, sizeof(*info));
1438 info->key.tp_dst = htons(dst_port);
1441 static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1442 struct netlink_ext_ack *extack,
1443 struct geneve_config *cfg, bool changelink)
1445 struct ip_tunnel_info *info = &cfg->info;
1448 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1449 NL_SET_ERR_MSG(extack,
1450 "Cannot specify both IPv4 and IPv6 Remote addresses");
1454 if (data[IFLA_GENEVE_REMOTE]) {
1455 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1456 attrtype = IFLA_GENEVE_REMOTE;
1460 info->key.u.ipv4.dst =
1461 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1463 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
1464 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1465 "Remote IPv4 address cannot be Multicast");
1470 if (data[IFLA_GENEVE_REMOTE6]) {
1471 #if IS_ENABLED(CONFIG_IPV6)
1472 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1473 attrtype = IFLA_GENEVE_REMOTE6;
1477 info->mode = IP_TUNNEL_INFO_IPV6;
1478 info->key.u.ipv6.dst =
1479 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1481 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
1482 IPV6_ADDR_LINKLOCAL) {
1483 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1484 "Remote IPv6 address cannot be link-local");
1487 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
1488 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1489 "Remote IPv6 address cannot be Multicast");
1492 info->key.tun_flags |= TUNNEL_CSUM;
1493 cfg->use_udp6_rx_checksums = true;
1495 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1496 "IPv6 support not enabled in the kernel");
1497 return -EPFNOSUPPORT;
1501 if (data[IFLA_GENEVE_ID]) {
1506 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1507 tvni[0] = (vni & 0x00ff0000) >> 16;
1508 tvni[1] = (vni & 0x0000ff00) >> 8;
1509 tvni[2] = vni & 0x000000ff;
1511 tunid = vni_to_tunnel_id(tvni);
1512 if (changelink && (tunid != info->key.tun_id)) {
1513 attrtype = IFLA_GENEVE_ID;
1516 info->key.tun_id = tunid;
1519 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1520 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1521 cfg->ttl_inherit = true;
1523 cfg->ttl_inherit = false;
1524 } else if (data[IFLA_GENEVE_TTL]) {
1525 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1526 cfg->ttl_inherit = false;
1529 if (data[IFLA_GENEVE_TOS])
1530 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1532 if (data[IFLA_GENEVE_DF])
1533 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
1535 if (data[IFLA_GENEVE_LABEL]) {
1536 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1537 IPV6_FLOWLABEL_MASK;
1538 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1539 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1540 "Label attribute only applies for IPv6 Geneve devices");
1545 if (data[IFLA_GENEVE_PORT]) {
1547 attrtype = IFLA_GENEVE_PORT;
1550 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1553 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
1555 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1558 cfg->collect_md = true;
1561 if (data[IFLA_GENEVE_UDP_CSUM]) {
1563 attrtype = IFLA_GENEVE_UDP_CSUM;
1566 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1567 info->key.tun_flags |= TUNNEL_CSUM;
1570 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
1571 #if IS_ENABLED(CONFIG_IPV6)
1573 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1576 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1577 info->key.tun_flags &= ~TUNNEL_CSUM;
1579 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1580 "IPv6 support not enabled in the kernel");
1581 return -EPFNOSUPPORT;
1585 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
1586 #if IS_ENABLED(CONFIG_IPV6)
1588 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1591 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1592 cfg->use_udp6_rx_checksums = false;
1594 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1595 "IPv6 support not enabled in the kernel");
1596 return -EPFNOSUPPORT;
1600 if (data[IFLA_GENEVE_INNER_PROTO_INHERIT]) {
1602 attrtype = IFLA_GENEVE_INNER_PROTO_INHERIT;
1605 cfg->inner_proto_inherit = true;
1610 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1611 "Changing VNI, Port, endpoint IP address family, external, inner_proto_inherit, and UDP checksum attributes are not supported");
1615 static void geneve_link_config(struct net_device *dev,
1616 struct ip_tunnel_info *info, struct nlattr *tb[])
1618 struct geneve_dev *geneve = netdev_priv(dev);
1622 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1626 switch (ip_tunnel_info_af(info)) {
1628 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1629 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1631 if (!IS_ERR(rt) && rt->dst.dev) {
1632 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1637 #if IS_ENABLED(CONFIG_IPV6)
1639 struct rt6_info *rt;
1641 if (!__in6_dev_get(dev))
1644 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1647 if (rt && rt->dst.dev)
1648 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1658 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1661 static int geneve_newlink(struct net *net, struct net_device *dev,
1662 struct nlattr *tb[], struct nlattr *data[],
1663 struct netlink_ext_ack *extack)
1665 struct geneve_config cfg = {
1666 .df = GENEVE_DF_UNSET,
1667 .use_udp6_rx_checksums = false,
1668 .ttl_inherit = false,
1669 .collect_md = false,
1673 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1674 err = geneve_nl2info(tb, data, extack, &cfg, false);
1678 err = geneve_configure(net, dev, extack, &cfg);
1682 geneve_link_config(dev, &cfg.info, tb);
1687 /* Quiesces the geneve device data path for both TX and RX.
1689 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1690 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1691 * to complete for the existing set of in-flight packets to be transmitted,
1692 * then we would have quiesced the transmit data path. All the future packets
1693 * will get dropped until we unquiesce the data path.
1695 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1696 * if we set that to NULL under RCU and wait for synchronize_net() to
1697 * complete, then we would have quiesced the receive data path.
1699 static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1700 struct geneve_sock **gs6)
1702 *gs4 = rtnl_dereference(geneve->sock4);
1703 rcu_assign_pointer(geneve->sock4, NULL);
1705 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1706 #if IS_ENABLED(CONFIG_IPV6)
1707 *gs6 = rtnl_dereference(geneve->sock6);
1708 rcu_assign_pointer(geneve->sock6, NULL);
1710 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1717 /* Resumes the geneve device data path for both TX and RX. */
1718 static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1719 struct geneve_sock __maybe_unused *gs6)
1721 rcu_assign_pointer(geneve->sock4, gs4);
1723 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1724 #if IS_ENABLED(CONFIG_IPV6)
1725 rcu_assign_pointer(geneve->sock6, gs6);
1727 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1732 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1733 struct nlattr *data[],
1734 struct netlink_ext_ack *extack)
1736 struct geneve_dev *geneve = netdev_priv(dev);
1737 struct geneve_sock *gs4, *gs6;
1738 struct geneve_config cfg;
1741 /* If the geneve device is configured for metadata (or externally
1742 * controlled, for example, OVS), then nothing can be changed.
1744 if (geneve->cfg.collect_md)
1747 /* Start with the existing info. */
1748 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1749 err = geneve_nl2info(tb, data, extack, &cfg, true);
1753 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1754 dst_cache_reset(&cfg.info.dst_cache);
1755 geneve_link_config(dev, &cfg.info, tb);
1758 geneve_quiesce(geneve, &gs4, &gs6);
1759 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
1760 geneve_unquiesce(geneve, gs4, gs6);
1765 static void geneve_dellink(struct net_device *dev, struct list_head *head)
1767 struct geneve_dev *geneve = netdev_priv(dev);
1769 list_del(&geneve->next);
1770 unregister_netdevice_queue(dev, head);
1773 static size_t geneve_get_size(const struct net_device *dev)
1775 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
1776 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
1777 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
1778 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
1779 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
1780 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
1781 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
1782 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1783 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1784 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1785 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1786 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
1787 nla_total_size(0) + /* IFLA_GENEVE_INNER_PROTO_INHERIT */
1791 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1793 struct geneve_dev *geneve = netdev_priv(dev);
1794 struct ip_tunnel_info *info = &geneve->cfg.info;
1795 bool ttl_inherit = geneve->cfg.ttl_inherit;
1796 bool metadata = geneve->cfg.collect_md;
1800 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1801 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
1802 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1803 goto nla_put_failure;
1805 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
1806 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1807 info->key.u.ipv4.dst))
1808 goto nla_put_failure;
1809 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1810 !!(info->key.tun_flags & TUNNEL_CSUM)))
1811 goto nla_put_failure;
1813 #if IS_ENABLED(CONFIG_IPV6)
1814 } else if (!metadata) {
1815 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1816 &info->key.u.ipv6.dst))
1817 goto nla_put_failure;
1818 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1819 !(info->key.tun_flags & TUNNEL_CSUM)))
1820 goto nla_put_failure;
1824 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1825 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1826 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
1827 goto nla_put_failure;
1829 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
1830 goto nla_put_failure;
1832 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
1833 goto nla_put_failure;
1835 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1836 goto nla_put_failure;
1838 #if IS_ENABLED(CONFIG_IPV6)
1839 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1840 !geneve->cfg.use_udp6_rx_checksums))
1841 goto nla_put_failure;
1844 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1845 goto nla_put_failure;
1847 if (geneve->cfg.inner_proto_inherit &&
1848 nla_put_flag(skb, IFLA_GENEVE_INNER_PROTO_INHERIT))
1849 goto nla_put_failure;
1857 static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1859 .maxtype = IFLA_GENEVE_MAX,
1860 .policy = geneve_policy,
1861 .priv_size = sizeof(struct geneve_dev),
1862 .setup = geneve_setup,
1863 .validate = geneve_validate,
1864 .newlink = geneve_newlink,
1865 .changelink = geneve_changelink,
1866 .dellink = geneve_dellink,
1867 .get_size = geneve_get_size,
1868 .fill_info = geneve_fill_info,
1871 struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1872 u8 name_assign_type, u16 dst_port)
1874 struct nlattr *tb[IFLA_MAX + 1];
1875 struct net_device *dev;
1876 LIST_HEAD(list_kill);
1878 struct geneve_config cfg = {
1879 .df = GENEVE_DF_UNSET,
1880 .use_udp6_rx_checksums = true,
1881 .ttl_inherit = false,
1885 memset(tb, 0, sizeof(tb));
1886 dev = rtnl_create_link(net, name, name_assign_type,
1887 &geneve_link_ops, tb, NULL);
1891 init_tnl_info(&cfg.info, dst_port);
1892 err = geneve_configure(net, dev, NULL, &cfg);
1895 return ERR_PTR(err);
1898 /* openvswitch users expect packet sizes to be unrestricted,
1899 * so set the largest MTU we can.
1901 err = geneve_change_mtu(dev, IP_MAX_MTU);
1905 err = rtnl_configure_link(dev, NULL, 0, NULL);
1911 geneve_dellink(dev, &list_kill);
1912 unregister_netdevice_many(&list_kill);
1913 return ERR_PTR(err);
1915 EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1917 static int geneve_netdevice_event(struct notifier_block *unused,
1918 unsigned long event, void *ptr)
1920 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1922 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1923 geneve_offload_rx_ports(dev, true);
1924 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1925 geneve_offload_rx_ports(dev, false);
1930 static struct notifier_block geneve_notifier_block __read_mostly = {
1931 .notifier_call = geneve_netdevice_event,
1934 static __net_init int geneve_init_net(struct net *net)
1936 struct geneve_net *gn = net_generic(net, geneve_net_id);
1938 INIT_LIST_HEAD(&gn->geneve_list);
1939 INIT_LIST_HEAD(&gn->sock_list);
1943 static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
1945 struct geneve_net *gn = net_generic(net, geneve_net_id);
1946 struct geneve_dev *geneve, *next;
1947 struct net_device *dev, *aux;
1949 /* gather any geneve devices that were moved into this ns */
1950 for_each_netdev_safe(net, dev, aux)
1951 if (dev->rtnl_link_ops == &geneve_link_ops)
1952 unregister_netdevice_queue(dev, head);
1954 /* now gather any other geneve devices that were created in this ns */
1955 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1956 /* If geneve->dev is in the same netns, it was already added
1957 * to the list by the previous loop.
1959 if (!net_eq(dev_net(geneve->dev), net))
1960 unregister_netdevice_queue(geneve->dev, head);
1964 static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1970 list_for_each_entry(net, net_list, exit_list)
1971 geneve_destroy_tunnels(net, &list);
1973 /* unregister the devices gathered above */
1974 unregister_netdevice_many(&list);
1977 list_for_each_entry(net, net_list, exit_list) {
1978 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1980 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1984 static struct pernet_operations geneve_net_ops = {
1985 .init = geneve_init_net,
1986 .exit_batch = geneve_exit_batch_net,
1987 .id = &geneve_net_id,
1988 .size = sizeof(struct geneve_net),
1991 static int __init geneve_init_module(void)
1995 rc = register_pernet_subsys(&geneve_net_ops);
1999 rc = register_netdevice_notifier(&geneve_notifier_block);
2003 rc = rtnl_link_register(&geneve_link_ops);
2009 unregister_netdevice_notifier(&geneve_notifier_block);
2011 unregister_pernet_subsys(&geneve_net_ops);
2015 late_initcall(geneve_init_module);
2017 static void __exit geneve_cleanup_module(void)
2019 rtnl_link_unregister(&geneve_link_ops);
2020 unregister_netdevice_notifier(&geneve_notifier_block);
2021 unregister_pernet_subsys(&geneve_net_ops);
2023 module_exit(geneve_cleanup_module);
2025 MODULE_LICENSE("GPL");
2026 MODULE_VERSION(GENEVE_NETDEV_VER);
2027 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
2028 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
2029 MODULE_ALIAS_RTNL_LINK("geneve");