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>
20 #include <net/protocol.h>
22 #define GENEVE_NETDEV_VER "0.6"
24 #define GENEVE_N_VID (1u << 24)
25 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
27 #define VNI_HASH_BITS 10
28 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
30 static bool log_ecn_error = true;
31 module_param(log_ecn_error, bool, 0644);
32 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
35 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
36 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
37 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
39 /* per-network namespace private data for this module */
41 struct list_head geneve_list;
42 struct list_head sock_list;
45 static unsigned int geneve_net_id;
47 struct geneve_dev_node {
48 struct hlist_node hlist;
49 struct geneve_dev *geneve;
52 struct geneve_config {
53 struct ip_tunnel_info info;
55 bool use_udp6_rx_checksums;
57 enum ifla_geneve_df df;
60 /* Pseudo network device */
62 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
63 #if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
66 struct net *net; /* netns for packet i/o */
67 struct net_device *dev; /* netdev for geneve tunnel */
68 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
69 #if IS_ENABLED(CONFIG_IPV6)
70 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
72 struct list_head next; /* geneve's per namespace list */
73 struct gro_cells gro_cells;
74 struct geneve_config cfg;
79 struct list_head list;
83 struct hlist_head vni_list[VNI_HASH_SIZE];
86 static inline __u32 geneve_net_vni_hash(u8 vni[3])
90 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
91 return hash_32(vnid, VNI_HASH_BITS);
94 static __be64 vni_to_tunnel_id(const __u8 *vni)
97 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
99 return (__force __be64)(((__force u64)vni[0] << 40) |
100 ((__force u64)vni[1] << 48) |
101 ((__force u64)vni[2] << 56));
105 /* Convert 64 bit tunnel ID to 24 bit VNI. */
106 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
109 vni[0] = (__force __u8)(tun_id >> 16);
110 vni[1] = (__force __u8)(tun_id >> 8);
111 vni[2] = (__force __u8)tun_id;
113 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
114 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
115 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
119 static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
121 return !memcmp(vni, &tun_id[5], 3);
124 static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
126 return gs->sock->sk->sk_family;
129 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
130 __be32 addr, u8 vni[])
132 struct hlist_head *vni_list_head;
133 struct geneve_dev_node *node;
136 /* Find the device for this VNI */
137 hash = geneve_net_vni_hash(vni);
138 vni_list_head = &gs->vni_list[hash];
139 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
140 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
141 addr == node->geneve->cfg.info.key.u.ipv4.dst)
147 #if IS_ENABLED(CONFIG_IPV6)
148 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
149 struct in6_addr addr6, u8 vni[])
151 struct hlist_head *vni_list_head;
152 struct geneve_dev_node *node;
155 /* Find the device for this VNI */
156 hash = geneve_net_vni_hash(vni);
157 vni_list_head = &gs->vni_list[hash];
158 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
159 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
160 ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
167 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
169 return (struct genevehdr *)(udp_hdr(skb) + 1);
172 static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
175 static u8 zero_vni[3];
178 if (geneve_get_sk_family(gs) == AF_INET) {
182 iph = ip_hdr(skb); /* outer IP header... */
184 if (gs->collect_md) {
188 vni = geneve_hdr(skb)->vni;
192 return geneve_lookup(gs, addr, vni);
193 #if IS_ENABLED(CONFIG_IPV6)
194 } else if (geneve_get_sk_family(gs) == AF_INET6) {
195 static struct in6_addr zero_addr6;
196 struct ipv6hdr *ip6h;
197 struct in6_addr addr6;
199 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
201 if (gs->collect_md) {
205 vni = geneve_hdr(skb)->vni;
209 return geneve6_lookup(gs, addr6, vni);
215 /* geneve receive/decap routine */
216 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
219 struct genevehdr *gnvh = geneve_hdr(skb);
220 struct metadata_dst *tun_dst = NULL;
225 if (ip_tunnel_collect_metadata() || gs->collect_md) {
228 flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
229 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
231 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
232 vni_to_tunnel_id(gnvh->vni),
235 geneve->dev->stats.rx_dropped++;
238 /* Update tunnel dst according to Geneve options. */
239 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
240 gnvh->options, gnvh->opt_len * 4,
243 /* Drop packets w/ critical options,
244 * since we don't support any...
246 if (gnvh->critical) {
247 geneve->dev->stats.rx_frame_errors++;
248 geneve->dev->stats.rx_errors++;
253 skb_reset_mac_header(skb);
254 skb->protocol = eth_type_trans(skb, geneve->dev);
255 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
258 skb_dst_set(skb, &tun_dst->dst);
260 /* Ignore packet loops (and multicast echo) */
261 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
262 geneve->dev->stats.rx_errors++;
266 oiph = skb_network_header(skb);
267 skb_reset_network_header(skb);
269 if (geneve_get_sk_family(gs) == AF_INET)
270 err = IP_ECN_decapsulate(oiph, skb);
271 #if IS_ENABLED(CONFIG_IPV6)
273 err = IP6_ECN_decapsulate(oiph, skb);
278 if (geneve_get_sk_family(gs) == AF_INET)
279 net_info_ratelimited("non-ECT from %pI4 "
281 &((struct iphdr *)oiph)->saddr,
282 ((struct iphdr *)oiph)->tos);
283 #if IS_ENABLED(CONFIG_IPV6)
285 net_info_ratelimited("non-ECT from %pI6\n",
286 &((struct ipv6hdr *)oiph)->saddr);
290 ++geneve->dev->stats.rx_frame_errors;
291 ++geneve->dev->stats.rx_errors;
297 err = gro_cells_receive(&geneve->gro_cells, skb);
298 if (likely(err == NET_RX_SUCCESS))
299 dev_sw_netstats_rx_add(geneve->dev, len);
303 /* Consume bad packet */
307 /* Setup stats when device is created */
308 static int geneve_init(struct net_device *dev)
310 struct geneve_dev *geneve = netdev_priv(dev);
313 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
317 err = gro_cells_init(&geneve->gro_cells, dev);
319 free_percpu(dev->tstats);
323 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
325 free_percpu(dev->tstats);
326 gro_cells_destroy(&geneve->gro_cells);
332 static void geneve_uninit(struct net_device *dev)
334 struct geneve_dev *geneve = netdev_priv(dev);
336 dst_cache_destroy(&geneve->cfg.info.dst_cache);
337 gro_cells_destroy(&geneve->gro_cells);
338 free_percpu(dev->tstats);
341 /* Callback from net/ipv4/udp.c to receive packets */
342 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
344 struct genevehdr *geneveh;
345 struct geneve_dev *geneve;
346 struct geneve_sock *gs;
349 /* Need UDP and Geneve header to be present */
350 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
353 /* Return packets with reserved bits set */
354 geneveh = geneve_hdr(skb);
355 if (unlikely(geneveh->ver != GENEVE_VER))
358 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
361 gs = rcu_dereference_sk_user_data(sk);
365 geneve = geneve_lookup_skb(gs, skb);
369 opts_len = geneveh->opt_len * 4;
370 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
372 !net_eq(geneve->net, dev_net(geneve->dev)))) {
373 geneve->dev->stats.rx_dropped++;
377 geneve_rx(geneve, gs, skb);
381 /* Consume bad packet */
386 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
387 static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
389 struct genevehdr *geneveh;
390 struct geneve_sock *gs;
391 u8 zero_vni[3] = { 0 };
394 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
397 geneveh = geneve_hdr(skb);
398 if (geneveh->ver != GENEVE_VER)
401 if (geneveh->proto_type != htons(ETH_P_TEB))
404 gs = rcu_dereference_sk_user_data(sk);
408 if (geneve_get_sk_family(gs) == AF_INET) {
409 struct iphdr *iph = ip_hdr(skb);
412 if (!gs->collect_md) {
413 vni = geneve_hdr(skb)->vni;
417 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
420 #if IS_ENABLED(CONFIG_IPV6)
421 if (geneve_get_sk_family(gs) == AF_INET6) {
422 struct ipv6hdr *ip6h = ipv6_hdr(skb);
423 struct in6_addr addr6;
425 memset(&addr6, 0, sizeof(struct in6_addr));
427 if (!gs->collect_md) {
428 vni = geneve_hdr(skb)->vni;
432 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
436 return -EPFNOSUPPORT;
439 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
440 __be16 port, bool ipv6_rx_csum)
443 struct udp_port_cfg udp_conf;
446 memset(&udp_conf, 0, sizeof(udp_conf));
449 udp_conf.family = AF_INET6;
450 udp_conf.ipv6_v6only = 1;
451 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
453 udp_conf.family = AF_INET;
454 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
457 udp_conf.local_udp_port = port;
459 /* Open UDP socket */
460 err = udp_sock_create(net, &udp_conf, &sock);
464 udp_allow_gso(sock->sk);
468 static int geneve_hlen(struct genevehdr *gh)
470 return sizeof(*gh) + gh->opt_len * 4;
473 static struct sk_buff *geneve_gro_receive(struct sock *sk,
474 struct list_head *head,
477 struct sk_buff *pp = NULL;
479 struct genevehdr *gh, *gh2;
480 unsigned int hlen, gh_len, off_gnv;
481 const struct packet_offload *ptype;
485 off_gnv = skb_gro_offset(skb);
486 hlen = off_gnv + sizeof(*gh);
487 gh = skb_gro_header_fast(skb, off_gnv);
488 if (skb_gro_header_hard(skb, hlen)) {
489 gh = skb_gro_header_slow(skb, hlen, off_gnv);
494 if (gh->ver != GENEVE_VER || gh->oam)
496 gh_len = geneve_hlen(gh);
498 hlen = off_gnv + gh_len;
499 if (skb_gro_header_hard(skb, hlen)) {
500 gh = skb_gro_header_slow(skb, hlen, off_gnv);
505 list_for_each_entry(p, head, list) {
506 if (!NAPI_GRO_CB(p)->same_flow)
509 gh2 = (struct genevehdr *)(p->data + off_gnv);
510 if (gh->opt_len != gh2->opt_len ||
511 memcmp(gh, gh2, gh_len)) {
512 NAPI_GRO_CB(p)->same_flow = 0;
517 type = gh->proto_type;
520 ptype = gro_find_receive_by_type(type);
524 skb_gro_pull(skb, gh_len);
525 skb_gro_postpull_rcsum(skb, gh, gh_len);
526 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
532 skb_gro_flush_final(skb, pp, flush);
537 static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
540 struct genevehdr *gh;
541 struct packet_offload *ptype;
546 gh = (struct genevehdr *)(skb->data + nhoff);
547 gh_len = geneve_hlen(gh);
548 type = gh->proto_type;
551 ptype = gro_find_complete_by_type(type);
553 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
557 skb_set_inner_mac_header(skb, nhoff + gh_len);
562 /* Create new listen socket if needed */
563 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
564 bool ipv6, bool ipv6_rx_csum)
566 struct geneve_net *gn = net_generic(net, geneve_net_id);
567 struct geneve_sock *gs;
569 struct udp_tunnel_sock_cfg tunnel_cfg;
572 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
574 return ERR_PTR(-ENOMEM);
576 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
579 return ERR_CAST(sock);
584 for (h = 0; h < VNI_HASH_SIZE; ++h)
585 INIT_HLIST_HEAD(&gs->vni_list[h]);
587 /* Initialize the geneve udp offloads structure */
588 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
590 /* Mark socket as an encapsulation socket */
591 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
592 tunnel_cfg.sk_user_data = gs;
593 tunnel_cfg.encap_type = 1;
594 tunnel_cfg.gro_receive = geneve_gro_receive;
595 tunnel_cfg.gro_complete = geneve_gro_complete;
596 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
597 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
598 tunnel_cfg.encap_destroy = NULL;
599 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
600 list_add(&gs->list, &gn->sock_list);
604 static void __geneve_sock_release(struct geneve_sock *gs)
606 if (!gs || --gs->refcnt)
610 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
611 udp_tunnel_sock_release(gs->sock);
615 static void geneve_sock_release(struct geneve_dev *geneve)
617 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
618 #if IS_ENABLED(CONFIG_IPV6)
619 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
621 rcu_assign_pointer(geneve->sock6, NULL);
624 rcu_assign_pointer(geneve->sock4, NULL);
627 __geneve_sock_release(gs4);
628 #if IS_ENABLED(CONFIG_IPV6)
629 __geneve_sock_release(gs6);
633 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
637 struct geneve_sock *gs;
639 list_for_each_entry(gs, &gn->sock_list, list) {
640 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
641 geneve_get_sk_family(gs) == family) {
648 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
650 struct net *net = geneve->net;
651 struct geneve_net *gn = net_generic(net, geneve_net_id);
652 struct geneve_dev_node *node;
653 struct geneve_sock *gs;
657 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
663 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
664 geneve->cfg.use_udp6_rx_checksums);
669 gs->collect_md = geneve->cfg.collect_md;
670 #if IS_ENABLED(CONFIG_IPV6)
672 rcu_assign_pointer(geneve->sock6, gs);
673 node = &geneve->hlist6;
677 rcu_assign_pointer(geneve->sock4, gs);
678 node = &geneve->hlist4;
680 node->geneve = geneve;
682 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
683 hash = geneve_net_vni_hash(vni);
684 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
688 static int geneve_open(struct net_device *dev)
690 struct geneve_dev *geneve = netdev_priv(dev);
691 bool metadata = geneve->cfg.collect_md;
695 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
696 ipv4 = !ipv6 || metadata;
697 #if IS_ENABLED(CONFIG_IPV6)
699 ret = geneve_sock_add(geneve, true);
700 if (ret < 0 && ret != -EAFNOSUPPORT)
705 ret = geneve_sock_add(geneve, false);
707 geneve_sock_release(geneve);
712 static int geneve_stop(struct net_device *dev)
714 struct geneve_dev *geneve = netdev_priv(dev);
716 hlist_del_init_rcu(&geneve->hlist4.hlist);
717 #if IS_ENABLED(CONFIG_IPV6)
718 hlist_del_init_rcu(&geneve->hlist6.hlist);
720 geneve_sock_release(geneve);
724 static void geneve_build_header(struct genevehdr *geneveh,
725 const struct ip_tunnel_info *info)
727 geneveh->ver = GENEVE_VER;
728 geneveh->opt_len = info->options_len / 4;
729 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
730 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
732 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
733 geneveh->proto_type = htons(ETH_P_TEB);
736 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
737 ip_tunnel_info_opts_get(geneveh->options, info);
740 static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
741 const struct ip_tunnel_info *info,
742 bool xnet, int ip_hdr_len)
744 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
745 struct genevehdr *gnvh;
749 skb_reset_mac_header(skb);
750 skb_scrub_packet(skb, xnet);
752 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
753 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
754 err = skb_cow_head(skb, min_headroom);
758 err = udp_tunnel_handle_offloads(skb, udp_sum);
762 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
763 geneve_build_header(gnvh, info);
764 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
772 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
773 struct net_device *dev,
774 struct geneve_sock *gs4,
776 const struct ip_tunnel_info *info,
777 __be16 dport, __be16 sport)
779 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
780 struct geneve_dev *geneve = netdev_priv(dev);
781 struct dst_cache *dst_cache;
782 struct rtable *rt = NULL;
786 return ERR_PTR(-EIO);
788 memset(fl4, 0, sizeof(*fl4));
789 fl4->flowi4_mark = skb->mark;
790 fl4->flowi4_proto = IPPROTO_UDP;
791 fl4->daddr = info->key.u.ipv4.dst;
792 fl4->saddr = info->key.u.ipv4.src;
793 fl4->fl4_dport = dport;
794 fl4->fl4_sport = sport;
797 if ((tos == 1) && !geneve->cfg.collect_md) {
798 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
801 fl4->flowi4_tos = RT_TOS(tos);
803 dst_cache = (struct dst_cache *)&info->dst_cache;
805 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
809 rt = ip_route_output_key(geneve->net, fl4);
811 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
812 return ERR_PTR(-ENETUNREACH);
814 if (rt->dst.dev == dev) { /* is this necessary? */
815 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
817 return ERR_PTR(-ELOOP);
820 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
824 #if IS_ENABLED(CONFIG_IPV6)
825 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
826 struct net_device *dev,
827 struct geneve_sock *gs6,
829 const struct ip_tunnel_info *info,
830 __be16 dport, __be16 sport)
832 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
833 struct geneve_dev *geneve = netdev_priv(dev);
834 struct dst_entry *dst = NULL;
835 struct dst_cache *dst_cache;
839 return ERR_PTR(-EIO);
841 memset(fl6, 0, sizeof(*fl6));
842 fl6->flowi6_mark = skb->mark;
843 fl6->flowi6_proto = IPPROTO_UDP;
844 fl6->daddr = info->key.u.ipv6.dst;
845 fl6->saddr = info->key.u.ipv6.src;
846 fl6->fl6_dport = dport;
847 fl6->fl6_sport = sport;
849 prio = info->key.tos;
850 if ((prio == 1) && !geneve->cfg.collect_md) {
851 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
855 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
857 dst_cache = (struct dst_cache *)&info->dst_cache;
859 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
863 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
866 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
867 return ERR_PTR(-ENETUNREACH);
869 if (dst->dev == dev) { /* is this necessary? */
870 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
872 return ERR_PTR(-ELOOP);
876 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
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)
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;
895 if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
898 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
899 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
900 geneve->cfg.info.key.tp_dst, sport);
904 err = skb_tunnel_check_pmtu(skb, &rt->dst,
905 GENEVE_IPV4_HLEN + info->options_len,
906 netif_is_any_bridge_port(dev));
908 dst_release(&rt->dst);
911 struct ip_tunnel_info *info;
913 info = skb_tunnel_info(skb);
915 struct ip_tunnel_info *unclone;
917 unclone = skb_tunnel_info_unclone(skb);
918 if (unlikely(!unclone)) {
919 dst_release(&rt->dst);
923 unclone->key.u.ipv4.dst = fl4.saddr;
924 unclone->key.u.ipv4.src = fl4.daddr;
927 if (!pskb_may_pull(skb, ETH_HLEN)) {
928 dst_release(&rt->dst);
932 skb->protocol = eth_type_trans(skb, geneve->dev);
934 dst_release(&rt->dst);
938 if (geneve->cfg.collect_md) {
939 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
942 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
944 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
945 if (geneve->cfg.ttl_inherit)
946 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
949 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
951 if (geneve->cfg.df == GENEVE_DF_SET) {
953 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
954 struct ethhdr *eth = eth_hdr(skb);
956 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
958 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
959 struct iphdr *iph = ip_hdr(skb);
961 if (iph->frag_off & htons(IP_DF))
967 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
971 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
972 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
973 !net_eq(geneve->net, dev_net(geneve->dev)),
974 !(info->key.tun_flags & TUNNEL_CSUM));
978 #if IS_ENABLED(CONFIG_IPV6)
979 static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
980 struct geneve_dev *geneve,
981 const struct ip_tunnel_info *info)
983 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
984 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
985 const struct ip_tunnel_key *key = &info->key;
986 struct dst_entry *dst = NULL;
992 if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
995 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
996 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
997 geneve->cfg.info.key.tp_dst, sport);
1001 err = skb_tunnel_check_pmtu(skb, dst,
1002 GENEVE_IPV6_HLEN + info->options_len,
1003 netif_is_any_bridge_port(dev));
1008 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1011 struct ip_tunnel_info *unclone;
1013 unclone = skb_tunnel_info_unclone(skb);
1014 if (unlikely(!unclone)) {
1019 unclone->key.u.ipv6.dst = fl6.saddr;
1020 unclone->key.u.ipv6.src = fl6.daddr;
1023 if (!pskb_may_pull(skb, ETH_HLEN)) {
1028 skb->protocol = eth_type_trans(skb, geneve->dev);
1034 if (geneve->cfg.collect_md) {
1035 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
1038 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
1040 if (geneve->cfg.ttl_inherit)
1041 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1044 ttl = ttl ? : ip6_dst_hoplimit(dst);
1046 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
1050 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
1051 &fl6.saddr, &fl6.daddr, prio, ttl,
1052 info->key.label, sport, geneve->cfg.info.key.tp_dst,
1053 !(info->key.tun_flags & TUNNEL_CSUM));
1058 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1060 struct geneve_dev *geneve = netdev_priv(dev);
1061 struct ip_tunnel_info *info = NULL;
1064 if (geneve->cfg.collect_md) {
1065 info = skb_tunnel_info(skb);
1066 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
1067 netdev_dbg(dev, "no tunnel metadata\n");
1069 dev->stats.tx_dropped++;
1070 return NETDEV_TX_OK;
1073 info = &geneve->cfg.info;
1077 #if IS_ENABLED(CONFIG_IPV6)
1078 if (info->mode & IP_TUNNEL_INFO_IPV6)
1079 err = geneve6_xmit_skb(skb, dev, geneve, info);
1082 err = geneve_xmit_skb(skb, dev, geneve, info);
1086 return NETDEV_TX_OK;
1088 if (err != -EMSGSIZE)
1092 dev->stats.collisions++;
1093 else if (err == -ENETUNREACH)
1094 dev->stats.tx_carrier_errors++;
1096 dev->stats.tx_errors++;
1097 return NETDEV_TX_OK;
1100 static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1102 if (new_mtu > dev->max_mtu)
1103 new_mtu = dev->max_mtu;
1104 else if (new_mtu < dev->min_mtu)
1105 new_mtu = dev->min_mtu;
1111 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1113 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1114 struct geneve_dev *geneve = netdev_priv(dev);
1117 if (ip_tunnel_info_af(info) == AF_INET) {
1121 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1122 sport = udp_flow_src_port(geneve->net, skb,
1123 1, USHRT_MAX, true);
1125 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1126 geneve->cfg.info.key.tp_dst, sport);
1131 info->key.u.ipv4.src = fl4.saddr;
1132 #if IS_ENABLED(CONFIG_IPV6)
1133 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1134 struct dst_entry *dst;
1137 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1138 sport = udp_flow_src_port(geneve->net, skb,
1139 1, USHRT_MAX, true);
1141 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1142 geneve->cfg.info.key.tp_dst, sport);
1144 return PTR_ERR(dst);
1147 info->key.u.ipv6.src = fl6.saddr;
1153 info->key.tp_src = sport;
1154 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
1158 static const struct net_device_ops geneve_netdev_ops = {
1159 .ndo_init = geneve_init,
1160 .ndo_uninit = geneve_uninit,
1161 .ndo_open = geneve_open,
1162 .ndo_stop = geneve_stop,
1163 .ndo_start_xmit = geneve_xmit,
1164 .ndo_get_stats64 = dev_get_tstats64,
1165 .ndo_change_mtu = geneve_change_mtu,
1166 .ndo_validate_addr = eth_validate_addr,
1167 .ndo_set_mac_address = eth_mac_addr,
1168 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
1171 static void geneve_get_drvinfo(struct net_device *dev,
1172 struct ethtool_drvinfo *drvinfo)
1174 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1175 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1178 static const struct ethtool_ops geneve_ethtool_ops = {
1179 .get_drvinfo = geneve_get_drvinfo,
1180 .get_link = ethtool_op_get_link,
1183 /* Info for udev, that this is a virtual tunnel endpoint */
1184 static struct device_type geneve_type = {
1188 /* Calls the ndo_udp_tunnel_add of the caller in order to
1189 * supply the listening GENEVE udp ports. Callers are expected
1190 * to implement the ndo_udp_tunnel_add.
1192 static void geneve_offload_rx_ports(struct net_device *dev, bool push)
1194 struct net *net = dev_net(dev);
1195 struct geneve_net *gn = net_generic(net, geneve_net_id);
1196 struct geneve_sock *gs;
1199 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1201 udp_tunnel_push_rx_port(dev, gs->sock,
1202 UDP_TUNNEL_TYPE_GENEVE);
1204 udp_tunnel_drop_rx_port(dev, gs->sock,
1205 UDP_TUNNEL_TYPE_GENEVE);
1211 /* Initialize the device structure. */
1212 static void geneve_setup(struct net_device *dev)
1216 dev->netdev_ops = &geneve_netdev_ops;
1217 dev->ethtool_ops = &geneve_ethtool_ops;
1218 dev->needs_free_netdev = true;
1220 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1222 dev->features |= NETIF_F_LLTX;
1223 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1224 dev->features |= NETIF_F_RXCSUM;
1225 dev->features |= NETIF_F_GSO_SOFTWARE;
1227 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1228 dev->hw_features |= NETIF_F_RXCSUM;
1229 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
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.
1237 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
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);
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 },
1261 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1262 struct netlink_ext_ack *extack)
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");
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;
1279 NL_SET_ERR_MSG(extack,
1280 "Not enough attributes provided to perform the operation");
1284 if (data[IFLA_GENEVE_ID]) {
1285 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
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");
1294 if (data[IFLA_GENEVE_DF]) {
1295 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1297 if (df < 0 || df > GENEVE_DF_MAX) {
1298 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
1299 "Invalid DF attribute");
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)
1312 struct geneve_dev *geneve, *t = NULL;
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;
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)))
1329 static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
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)));
1336 static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1337 struct ip_tunnel_info *b)
1339 if (ip_tunnel_info_af(a) == AF_INET)
1340 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1342 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1345 static int geneve_configure(struct net *net, struct net_device *dev,
1346 struct netlink_ext_ack *extack,
1347 const struct geneve_config *cfg)
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;
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");
1364 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
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);
1374 encap_len += sizeof(struct ipv6hdr);
1375 dev->max_mtu -= sizeof(struct ipv6hdr);
1377 dev->needed_headroom = encap_len + ETH_HLEN;
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");
1386 if (tun_collect_md) {
1387 NL_SET_ERR_MSG(extack,
1388 "There already exists an externally controlled device on this destination port");
1393 dst_cache_reset(&geneve->cfg.info.dst_cache);
1394 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
1396 err = register_netdevice(dev);
1400 list_add(&geneve->next, &gn->geneve_list);
1404 static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1406 memset(info, 0, sizeof(*info));
1407 info->key.tp_dst = htons(dst_port);
1410 static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1411 struct netlink_ext_ack *extack,
1412 struct geneve_config *cfg, bool changelink)
1414 struct ip_tunnel_info *info = &cfg->info;
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");
1423 if (data[IFLA_GENEVE_REMOTE]) {
1424 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1425 attrtype = IFLA_GENEVE_REMOTE;
1429 info->key.u.ipv4.dst =
1430 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
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");
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;
1446 info->mode = IP_TUNNEL_INFO_IPV6;
1447 info->key.u.ipv6.dst =
1448 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
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");
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");
1461 info->key.tun_flags |= TUNNEL_CSUM;
1462 cfg->use_udp6_rx_checksums = true;
1464 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1465 "IPv6 support not enabled in the kernel");
1466 return -EPFNOSUPPORT;
1470 if (data[IFLA_GENEVE_ID]) {
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;
1480 tunid = vni_to_tunnel_id(tvni);
1481 if (changelink && (tunid != info->key.tun_id)) {
1482 attrtype = IFLA_GENEVE_ID;
1485 info->key.tun_id = tunid;
1488 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1489 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1490 cfg->ttl_inherit = true;
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;
1498 if (data[IFLA_GENEVE_TOS])
1499 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1501 if (data[IFLA_GENEVE_DF])
1502 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
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");
1514 if (data[IFLA_GENEVE_PORT]) {
1516 attrtype = IFLA_GENEVE_PORT;
1519 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1522 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
1524 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1527 cfg->collect_md = true;
1530 if (data[IFLA_GENEVE_UDP_CSUM]) {
1532 attrtype = IFLA_GENEVE_UDP_CSUM;
1535 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1536 info->key.tun_flags |= TUNNEL_CSUM;
1539 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
1540 #if IS_ENABLED(CONFIG_IPV6)
1542 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1545 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1546 info->key.tun_flags &= ~TUNNEL_CSUM;
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;
1554 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
1555 #if IS_ENABLED(CONFIG_IPV6)
1557 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1560 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1561 cfg->use_udp6_rx_checksums = false;
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;
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");
1576 static void geneve_link_config(struct net_device *dev,
1577 struct ip_tunnel_info *info, struct nlattr *tb[])
1579 struct geneve_dev *geneve = netdev_priv(dev);
1583 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1587 switch (ip_tunnel_info_af(info)) {
1589 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1590 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1592 if (!IS_ERR(rt) && rt->dst.dev) {
1593 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1598 #if IS_ENABLED(CONFIG_IPV6)
1600 struct rt6_info *rt;
1602 if (!__in6_dev_get(dev))
1605 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1608 if (rt && rt->dst.dev)
1609 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1619 geneve_change_mtu(dev, ldev_mtu - info->options_len);
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)
1626 struct geneve_config cfg = {
1627 .df = GENEVE_DF_UNSET,
1628 .use_udp6_rx_checksums = false,
1629 .ttl_inherit = false,
1630 .collect_md = false,
1634 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1635 err = geneve_nl2info(tb, data, extack, &cfg, false);
1639 err = geneve_configure(net, dev, extack, &cfg);
1643 geneve_link_config(dev, &cfg.info, tb);
1648 /* Quiesces the geneve device data path for both TX and RX.
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.
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.
1660 static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1661 struct geneve_sock **gs6)
1663 *gs4 = rtnl_dereference(geneve->sock4);
1664 rcu_assign_pointer(geneve->sock4, NULL);
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);
1671 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1678 /* Resumes the geneve device data path for both TX and RX. */
1679 static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1680 struct geneve_sock __maybe_unused *gs6)
1682 rcu_assign_pointer(geneve->sock4, gs4);
1684 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 rcu_assign_pointer(geneve->sock6, gs6);
1688 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1693 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1694 struct nlattr *data[],
1695 struct netlink_ext_ack *extack)
1697 struct geneve_dev *geneve = netdev_priv(dev);
1698 struct geneve_sock *gs4, *gs6;
1699 struct geneve_config cfg;
1702 /* If the geneve device is configured for metadata (or externally
1703 * controlled, for example, OVS), then nothing can be changed.
1705 if (geneve->cfg.collect_md)
1708 /* Start with the existing info. */
1709 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1710 err = geneve_nl2info(tb, data, extack, &cfg, true);
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);
1719 geneve_quiesce(geneve, &gs4, &gs6);
1720 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
1721 geneve_unquiesce(geneve, gs4, gs6);
1726 static void geneve_dellink(struct net_device *dev, struct list_head *head)
1728 struct geneve_dev *geneve = netdev_priv(dev);
1730 list_del(&geneve->next);
1731 unregister_netdevice_queue(dev, head);
1734 static size_t geneve_get_size(const struct net_device *dev)
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 */
1751 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
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;
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;
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;
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;
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;
1789 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
1790 goto nla_put_failure;
1792 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
1793 goto nla_put_failure;
1795 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1796 goto nla_put_failure;
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;
1804 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1805 goto nla_put_failure;
1813 static struct rtnl_link_ops geneve_link_ops __read_mostly = {
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,
1827 struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1828 u8 name_assign_type, u16 dst_port)
1830 struct nlattr *tb[IFLA_MAX + 1];
1831 struct net_device *dev;
1832 LIST_HEAD(list_kill);
1834 struct geneve_config cfg = {
1835 .df = GENEVE_DF_UNSET,
1836 .use_udp6_rx_checksums = true,
1837 .ttl_inherit = false,
1841 memset(tb, 0, sizeof(tb));
1842 dev = rtnl_create_link(net, name, name_assign_type,
1843 &geneve_link_ops, tb, NULL);
1847 init_tnl_info(&cfg.info, dst_port);
1848 err = geneve_configure(net, dev, NULL, &cfg);
1851 return ERR_PTR(err);
1854 /* openvswitch users expect packet sizes to be unrestricted,
1855 * so set the largest MTU we can.
1857 err = geneve_change_mtu(dev, IP_MAX_MTU);
1861 err = rtnl_configure_link(dev, NULL);
1867 geneve_dellink(dev, &list_kill);
1868 unregister_netdevice_many(&list_kill);
1869 return ERR_PTR(err);
1871 EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1873 static int geneve_netdevice_event(struct notifier_block *unused,
1874 unsigned long event, void *ptr)
1876 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1878 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1879 geneve_offload_rx_ports(dev, true);
1880 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1881 geneve_offload_rx_ports(dev, false);
1886 static struct notifier_block geneve_notifier_block __read_mostly = {
1887 .notifier_call = geneve_netdevice_event,
1890 static __net_init int geneve_init_net(struct net *net)
1892 struct geneve_net *gn = net_generic(net, geneve_net_id);
1894 INIT_LIST_HEAD(&gn->geneve_list);
1895 INIT_LIST_HEAD(&gn->sock_list);
1899 static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
1901 struct geneve_net *gn = net_generic(net, geneve_net_id);
1902 struct geneve_dev *geneve, *next;
1903 struct net_device *dev, *aux;
1905 /* gather any geneve devices that were moved into this ns */
1906 for_each_netdev_safe(net, dev, aux)
1907 if (dev->rtnl_link_ops == &geneve_link_ops)
1908 unregister_netdevice_queue(dev, head);
1910 /* now gather any other geneve devices that were created in this ns */
1911 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1912 /* If geneve->dev is in the same netns, it was already added
1913 * to the list by the previous loop.
1915 if (!net_eq(dev_net(geneve->dev), net))
1916 unregister_netdevice_queue(geneve->dev, head);
1920 static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1926 list_for_each_entry(net, net_list, exit_list)
1927 geneve_destroy_tunnels(net, &list);
1929 /* unregister the devices gathered above */
1930 unregister_netdevice_many(&list);
1933 list_for_each_entry(net, net_list, exit_list) {
1934 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1936 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1940 static struct pernet_operations geneve_net_ops = {
1941 .init = geneve_init_net,
1942 .exit_batch = geneve_exit_batch_net,
1943 .id = &geneve_net_id,
1944 .size = sizeof(struct geneve_net),
1947 static int __init geneve_init_module(void)
1951 rc = register_pernet_subsys(&geneve_net_ops);
1955 rc = register_netdevice_notifier(&geneve_notifier_block);
1959 rc = rtnl_link_register(&geneve_link_ops);
1965 unregister_netdevice_notifier(&geneve_notifier_block);
1967 unregister_pernet_subsys(&geneve_net_ops);
1971 late_initcall(geneve_init_module);
1973 static void __exit geneve_cleanup_module(void)
1975 rtnl_link_unregister(&geneve_link_ops);
1976 unregister_netdevice_notifier(&geneve_notifier_block);
1977 unregister_pernet_subsys(&geneve_net_ops);
1979 module_exit(geneve_cleanup_module);
1981 MODULE_LICENSE("GPL");
1982 MODULE_VERSION(GENEVE_NETDEV_VER);
1983 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1984 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1985 MODULE_ALIAS_RTNL_LINK("geneve");