2 * GENEVE: Generic Network Virtualization Encapsulation
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/hash.h>
18 #include <net/dst_metadata.h>
19 #include <net/gro_cells.h>
20 #include <net/rtnetlink.h>
21 #include <net/geneve.h>
22 #include <net/protocol.h>
24 #define GENEVE_NETDEV_VER "0.6"
26 #define GENEVE_UDP_PORT 6081
28 #define GENEVE_N_VID (1u << 24)
29 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
31 #define VNI_HASH_BITS 10
32 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
34 static bool log_ecn_error = true;
35 module_param(log_ecn_error, bool, 0644);
36 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
39 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
41 /* per-network namespace private data for this module */
43 struct list_head geneve_list;
44 struct list_head sock_list;
47 static int geneve_net_id;
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
55 static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
57 /* Pseudo network device */
59 struct hlist_node hlist; /* vni hash table */
60 struct net *net; /* netns for packet i/o */
61 struct net_device *dev; /* netdev for geneve tunnel */
62 struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */
63 #if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */
66 u8 vni[3]; /* virtual network ID for tunnel */
67 u8 ttl; /* TTL override */
68 u8 tos; /* TOS override */
69 union geneve_addr remote; /* IP address for link partner */
70 struct list_head next; /* geneve's per namespace list */
73 struct gro_cells gro_cells;
78 struct list_head list;
82 struct udp_offload udp_offloads;
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 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
106 __be32 addr, u8 vni[])
108 struct hlist_head *vni_list_head;
109 struct geneve_dev *geneve;
112 /* Find the device for this VNI */
113 hash = geneve_net_vni_hash(vni);
114 vni_list_head = &gs->vni_list[hash];
115 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
116 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
117 addr == geneve->remote.sin.sin_addr.s_addr)
123 #if IS_ENABLED(CONFIG_IPV6)
124 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
125 struct in6_addr addr6, u8 vni[])
127 struct hlist_head *vni_list_head;
128 struct geneve_dev *geneve;
131 /* Find the device for this VNI */
132 hash = geneve_net_vni_hash(vni);
133 vni_list_head = &gs->vni_list[hash];
134 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
135 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
136 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
143 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
145 return (struct genevehdr *)(udp_hdr(skb) + 1);
148 /* geneve receive/decap routine */
149 static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
151 struct genevehdr *gnvh = geneve_hdr(skb);
152 struct metadata_dst *tun_dst = NULL;
153 struct geneve_dev *geneve = NULL;
154 struct pcpu_sw_netstats *stats;
155 struct iphdr *iph = NULL;
157 static u8 zero_vni[3];
160 sa_family_t sa_family;
161 #if IS_ENABLED(CONFIG_IPV6)
162 struct ipv6hdr *ip6h = NULL;
163 struct in6_addr addr6;
164 static struct in6_addr zero_addr6;
167 sa_family = gs->sock->sk->sk_family;
169 if (sa_family == AF_INET) {
170 iph = ip_hdr(skb); /* outer IP header... */
172 if (gs->collect_md) {
181 geneve = geneve_lookup(gs, addr, vni);
182 #if IS_ENABLED(CONFIG_IPV6)
183 } else if (sa_family == AF_INET6) {
184 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
186 if (gs->collect_md) {
195 geneve = geneve6_lookup(gs, addr6, vni);
201 if (ip_tunnel_collect_metadata() || gs->collect_md) {
204 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
205 (gnvh->oam ? TUNNEL_OAM : 0) |
206 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
208 tun_dst = udp_tun_rx_dst(skb, sa_family, flags,
209 vni_to_tunnel_id(gnvh->vni),
213 /* Update tunnel dst according to Geneve options. */
214 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
215 gnvh->options, gnvh->opt_len * 4);
217 /* Drop packets w/ critical options,
218 * since we don't support any...
224 skb_reset_mac_header(skb);
225 skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
226 skb->protocol = eth_type_trans(skb, geneve->dev);
227 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
230 skb_dst_set(skb, &tun_dst->dst);
232 /* Ignore packet loops (and multicast echo) */
233 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
236 skb_reset_network_header(skb);
239 err = IP_ECN_decapsulate(iph, skb);
240 #if IS_ENABLED(CONFIG_IPV6)
242 err = IP6_ECN_decapsulate(ip6h, skb);
248 net_info_ratelimited("non-ECT from %pI4 "
250 &iph->saddr, iph->tos);
251 #if IS_ENABLED(CONFIG_IPV6)
253 net_info_ratelimited("non-ECT from %pI6\n",
258 ++geneve->dev->stats.rx_frame_errors;
259 ++geneve->dev->stats.rx_errors;
264 stats = this_cpu_ptr(geneve->dev->tstats);
265 u64_stats_update_begin(&stats->syncp);
267 stats->rx_bytes += skb->len;
268 u64_stats_update_end(&stats->syncp);
270 gro_cells_receive(&geneve->gro_cells, skb);
273 /* Consume bad packet */
277 /* Setup stats when device is created */
278 static int geneve_init(struct net_device *dev)
280 struct geneve_dev *geneve = netdev_priv(dev);
283 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
287 err = gro_cells_init(&geneve->gro_cells, dev);
289 free_percpu(dev->tstats);
296 static void geneve_uninit(struct net_device *dev)
298 struct geneve_dev *geneve = netdev_priv(dev);
300 gro_cells_destroy(&geneve->gro_cells);
301 free_percpu(dev->tstats);
304 /* Callback from net/ipv4/udp.c to receive packets */
305 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
307 struct genevehdr *geneveh;
308 struct geneve_sock *gs;
311 /* Need Geneve and inner Ethernet header to be present */
312 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
315 /* Return packets with reserved bits set */
316 geneveh = geneve_hdr(skb);
317 if (unlikely(geneveh->ver != GENEVE_VER))
320 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
323 opts_len = geneveh->opt_len * 4;
324 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
328 gs = rcu_dereference_sk_user_data(sk);
336 /* Consume bad packet */
341 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
345 struct udp_port_cfg udp_conf;
348 memset(&udp_conf, 0, sizeof(udp_conf));
351 udp_conf.family = AF_INET6;
352 udp_conf.ipv6_v6only = 1;
354 udp_conf.family = AF_INET;
355 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
358 udp_conf.local_udp_port = port;
360 /* Open UDP socket */
361 err = udp_sock_create(net, &udp_conf, &sock);
368 static void geneve_notify_add_rx_port(struct geneve_sock *gs)
370 struct sock *sk = gs->sock->sk;
371 sa_family_t sa_family = sk->sk_family;
374 if (sa_family == AF_INET) {
375 err = udp_add_offload(&gs->udp_offloads);
377 pr_warn("geneve: udp_add_offload failed with status %d\n",
382 static int geneve_hlen(struct genevehdr *gh)
384 return sizeof(*gh) + gh->opt_len * 4;
387 static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
389 struct udp_offload *uoff)
391 struct sk_buff *p, **pp = NULL;
392 struct genevehdr *gh, *gh2;
393 unsigned int hlen, gh_len, off_gnv;
394 const struct packet_offload *ptype;
398 off_gnv = skb_gro_offset(skb);
399 hlen = off_gnv + sizeof(*gh);
400 gh = skb_gro_header_fast(skb, off_gnv);
401 if (skb_gro_header_hard(skb, hlen)) {
402 gh = skb_gro_header_slow(skb, hlen, off_gnv);
407 if (gh->ver != GENEVE_VER || gh->oam)
409 gh_len = geneve_hlen(gh);
411 hlen = off_gnv + gh_len;
412 if (skb_gro_header_hard(skb, hlen)) {
413 gh = skb_gro_header_slow(skb, hlen, off_gnv);
420 for (p = *head; p; p = p->next) {
421 if (!NAPI_GRO_CB(p)->same_flow)
424 gh2 = (struct genevehdr *)(p->data + off_gnv);
425 if (gh->opt_len != gh2->opt_len ||
426 memcmp(gh, gh2, gh_len)) {
427 NAPI_GRO_CB(p)->same_flow = 0;
432 type = gh->proto_type;
435 ptype = gro_find_receive_by_type(type);
441 skb_gro_pull(skb, gh_len);
442 skb_gro_postpull_rcsum(skb, gh, gh_len);
443 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
448 NAPI_GRO_CB(skb)->flush |= flush;
453 static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
454 struct udp_offload *uoff)
456 struct genevehdr *gh;
457 struct packet_offload *ptype;
462 udp_tunnel_gro_complete(skb, nhoff);
464 gh = (struct genevehdr *)(skb->data + nhoff);
465 gh_len = geneve_hlen(gh);
466 type = gh->proto_type;
469 ptype = gro_find_complete_by_type(type);
471 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
477 /* Create new listen socket if needed */
478 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
481 struct geneve_net *gn = net_generic(net, geneve_net_id);
482 struct geneve_sock *gs;
484 struct udp_tunnel_sock_cfg tunnel_cfg;
487 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
489 return ERR_PTR(-ENOMEM);
491 sock = geneve_create_sock(net, ipv6, port);
494 return ERR_CAST(sock);
499 for (h = 0; h < VNI_HASH_SIZE; ++h)
500 INIT_HLIST_HEAD(&gs->vni_list[h]);
502 /* Initialize the geneve udp offloads structure */
503 gs->udp_offloads.port = port;
504 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
505 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
506 geneve_notify_add_rx_port(gs);
508 /* Mark socket as an encapsulation socket */
509 tunnel_cfg.sk_user_data = gs;
510 tunnel_cfg.encap_type = 1;
511 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
512 tunnel_cfg.encap_destroy = NULL;
513 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
514 list_add(&gs->list, &gn->sock_list);
518 static void geneve_notify_del_rx_port(struct geneve_sock *gs)
520 struct sock *sk = gs->sock->sk;
521 sa_family_t sa_family = sk->sk_family;
523 if (sa_family == AF_INET)
524 udp_del_offload(&gs->udp_offloads);
527 static void __geneve_sock_release(struct geneve_sock *gs)
529 if (!gs || --gs->refcnt)
533 geneve_notify_del_rx_port(gs);
534 udp_tunnel_sock_release(gs->sock);
538 static void geneve_sock_release(struct geneve_dev *geneve)
540 __geneve_sock_release(geneve->sock4);
541 #if IS_ENABLED(CONFIG_IPV6)
542 __geneve_sock_release(geneve->sock6);
546 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
550 struct geneve_sock *gs;
552 list_for_each_entry(gs, &gn->sock_list, list) {
553 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
554 inet_sk(gs->sock->sk)->sk.sk_family == family) {
561 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
563 struct net *net = geneve->net;
564 struct geneve_net *gn = net_generic(net, geneve_net_id);
565 struct geneve_sock *gs;
568 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
574 gs = geneve_socket_create(net, geneve->dst_port, ipv6);
579 gs->collect_md = geneve->collect_md;
580 #if IS_ENABLED(CONFIG_IPV6)
587 hash = geneve_net_vni_hash(geneve->vni);
588 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
592 static int geneve_open(struct net_device *dev)
594 struct geneve_dev *geneve = netdev_priv(dev);
595 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
596 bool metadata = geneve->collect_md;
599 geneve->sock4 = NULL;
600 #if IS_ENABLED(CONFIG_IPV6)
601 geneve->sock6 = NULL;
602 if (ipv6 || metadata)
603 ret = geneve_sock_add(geneve, true);
605 if (!ret && (!ipv6 || metadata))
606 ret = geneve_sock_add(geneve, false);
608 geneve_sock_release(geneve);
613 static int geneve_stop(struct net_device *dev)
615 struct geneve_dev *geneve = netdev_priv(dev);
617 if (!hlist_unhashed(&geneve->hlist))
618 hlist_del_rcu(&geneve->hlist);
619 geneve_sock_release(geneve);
623 static void geneve_build_header(struct genevehdr *geneveh,
624 __be16 tun_flags, u8 vni[3],
625 u8 options_len, u8 *options)
627 geneveh->ver = GENEVE_VER;
628 geneveh->opt_len = options_len / 4;
629 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
630 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
632 memcpy(geneveh->vni, vni, 3);
633 geneveh->proto_type = htons(ETH_P_TEB);
636 memcpy(geneveh->options, options, options_len);
639 static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
640 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
641 bool csum, bool xnet)
643 struct genevehdr *gnvh;
647 skb_scrub_packet(skb, xnet);
649 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
650 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
651 err = skb_cow_head(skb, min_headroom);
657 skb = udp_tunnel_handle_offloads(skb, csum);
663 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
664 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
666 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
674 #if IS_ENABLED(CONFIG_IPV6)
675 static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
676 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
677 bool csum, bool xnet)
679 struct genevehdr *gnvh;
683 skb_scrub_packet(skb, xnet);
685 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
686 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
687 err = skb_cow_head(skb, min_headroom);
693 skb = udp_tunnel_handle_offloads(skb, csum);
699 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
700 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
702 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
711 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
712 struct net_device *dev,
714 struct ip_tunnel_info *info)
716 struct geneve_dev *geneve = netdev_priv(dev);
717 struct rtable *rt = NULL;
720 memset(fl4, 0, sizeof(*fl4));
721 fl4->flowi4_mark = skb->mark;
722 fl4->flowi4_proto = IPPROTO_UDP;
725 fl4->daddr = info->key.u.ipv4.dst;
726 fl4->saddr = info->key.u.ipv4.src;
727 fl4->flowi4_tos = RT_TOS(info->key.tos);
731 const struct iphdr *iip = ip_hdr(skb);
733 tos = ip_tunnel_get_dsfield(iip, skb);
736 fl4->flowi4_tos = RT_TOS(tos);
737 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
740 rt = ip_route_output_key(geneve->net, fl4);
742 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
743 return ERR_PTR(-ENETUNREACH);
745 if (rt->dst.dev == dev) { /* is this necessary? */
746 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
748 return ERR_PTR(-ELOOP);
753 #if IS_ENABLED(CONFIG_IPV6)
754 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
755 struct net_device *dev,
757 struct ip_tunnel_info *info)
759 struct geneve_dev *geneve = netdev_priv(dev);
760 struct geneve_sock *gs6 = geneve->sock6;
761 struct dst_entry *dst = NULL;
764 memset(fl6, 0, sizeof(*fl6));
765 fl6->flowi6_mark = skb->mark;
766 fl6->flowi6_proto = IPPROTO_UDP;
769 fl6->daddr = info->key.u.ipv6.dst;
770 fl6->saddr = info->key.u.ipv6.src;
771 fl6->flowi6_tos = RT_TOS(info->key.tos);
775 const struct iphdr *iip = ip_hdr(skb);
777 prio = ip_tunnel_get_dsfield(iip, skb);
780 fl6->flowi6_tos = RT_TOS(prio);
781 fl6->daddr = geneve->remote.sin6.sin6_addr;
784 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
785 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
786 return ERR_PTR(-ENETUNREACH);
788 if (dst->dev == dev) { /* is this necessary? */
789 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
791 return ERR_PTR(-ELOOP);
798 /* Convert 64 bit tunnel ID to 24 bit VNI. */
799 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
802 vni[0] = (__force __u8)(tun_id >> 16);
803 vni[1] = (__force __u8)(tun_id >> 8);
804 vni[2] = (__force __u8)tun_id;
806 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
807 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
808 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
812 static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
813 struct ip_tunnel_info *info)
815 struct geneve_dev *geneve = netdev_priv(dev);
816 struct geneve_sock *gs4 = geneve->sock4;
817 struct rtable *rt = NULL;
818 const struct iphdr *iip; /* interior IP header */
825 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
827 if (geneve->collect_md) {
828 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
829 netdev_dbg(dev, "no tunnel metadata\n");
832 if (info && ip_tunnel_info_af(info) != AF_INET)
836 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
842 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
843 skb_reset_mac_header(skb);
848 const struct ip_tunnel_key *key = &info->key;
852 tunnel_id_to_vni(key->tun_id, vni);
853 if (key->tun_flags & TUNNEL_GENEVE_OPT)
854 opts = ip_tunnel_info_opts(info);
856 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
857 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
858 info->options_len, opts, udp_csum, xnet);
862 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
864 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
867 err = geneve_build_skb(rt, skb, 0, geneve->vni,
868 0, NULL, udp_csum, xnet);
872 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
874 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
876 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
879 err = udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
880 tos, ttl, df, sport, geneve->dst_port,
881 !net_eq(geneve->net, dev_net(geneve->dev)),
884 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
891 dev->stats.collisions++;
892 else if (err == -ENETUNREACH)
893 dev->stats.tx_carrier_errors++;
895 dev->stats.tx_errors++;
899 #if IS_ENABLED(CONFIG_IPV6)
900 static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
901 struct ip_tunnel_info *info)
903 struct geneve_dev *geneve = netdev_priv(dev);
904 struct geneve_sock *gs6 = geneve->sock6;
905 struct dst_entry *dst = NULL;
906 const struct iphdr *iip; /* interior IP header */
912 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
914 if (geneve->collect_md) {
915 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
916 netdev_dbg(dev, "no tunnel metadata\n");
921 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
927 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
928 skb_reset_mac_header(skb);
933 const struct ip_tunnel_key *key = &info->key;
937 tunnel_id_to_vni(key->tun_id, vni);
938 if (key->tun_flags & TUNNEL_GENEVE_OPT)
939 opts = ip_tunnel_info_opts(info);
941 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
942 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
943 info->options_len, opts,
948 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
952 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
953 0, NULL, udp_csum, xnet);
957 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
959 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
961 ttl = ttl ? : ip6_dst_hoplimit(dst);
963 err = udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
964 &fl6.saddr, &fl6.daddr, prio, ttl,
965 sport, geneve->dst_port, !udp_csum);
972 dev->stats.collisions++;
973 else if (err == -ENETUNREACH)
974 dev->stats.tx_carrier_errors++;
976 dev->stats.tx_errors++;
981 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
983 struct geneve_dev *geneve = netdev_priv(dev);
984 struct ip_tunnel_info *info = NULL;
986 if (geneve->collect_md)
987 info = skb_tunnel_info(skb);
989 #if IS_ENABLED(CONFIG_IPV6)
990 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
991 (!info && geneve->remote.sa.sa_family == AF_INET6))
992 return geneve6_xmit_skb(skb, dev, info);
994 return geneve_xmit_skb(skb, dev, info);
997 static int geneve_change_mtu(struct net_device *dev, int new_mtu)
999 /* GENEVE overhead is not fixed, so we can't enforce a more
1002 if (new_mtu < 68 || new_mtu > IP_MAX_MTU)
1008 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1010 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1011 struct geneve_dev *geneve = netdev_priv(dev);
1014 #if IS_ENABLED(CONFIG_IPV6)
1015 struct dst_entry *dst;
1019 if (ip_tunnel_info_af(info) == AF_INET) {
1020 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1025 info->key.u.ipv4.src = fl4.saddr;
1026 #if IS_ENABLED(CONFIG_IPV6)
1027 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1028 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1030 return PTR_ERR(dst);
1033 info->key.u.ipv6.src = fl6.saddr;
1039 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1040 1, USHRT_MAX, true);
1041 info->key.tp_dst = geneve->dst_port;
1045 static const struct net_device_ops geneve_netdev_ops = {
1046 .ndo_init = geneve_init,
1047 .ndo_uninit = geneve_uninit,
1048 .ndo_open = geneve_open,
1049 .ndo_stop = geneve_stop,
1050 .ndo_start_xmit = geneve_xmit,
1051 .ndo_get_stats64 = ip_tunnel_get_stats64,
1052 .ndo_change_mtu = geneve_change_mtu,
1053 .ndo_validate_addr = eth_validate_addr,
1054 .ndo_set_mac_address = eth_mac_addr,
1055 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
1058 static void geneve_get_drvinfo(struct net_device *dev,
1059 struct ethtool_drvinfo *drvinfo)
1061 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1062 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1065 static const struct ethtool_ops geneve_ethtool_ops = {
1066 .get_drvinfo = geneve_get_drvinfo,
1067 .get_link = ethtool_op_get_link,
1070 /* Info for udev, that this is a virtual tunnel endpoint */
1071 static struct device_type geneve_type = {
1075 /* Initialize the device structure. */
1076 static void geneve_setup(struct net_device *dev)
1080 dev->netdev_ops = &geneve_netdev_ops;
1081 dev->ethtool_ops = &geneve_ethtool_ops;
1082 dev->destructor = free_netdev;
1084 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1086 dev->features |= NETIF_F_LLTX;
1087 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1088 dev->features |= NETIF_F_RXCSUM;
1089 dev->features |= NETIF_F_GSO_SOFTWARE;
1091 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1092 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1094 netif_keep_dst(dev);
1095 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1096 eth_hw_addr_random(dev);
1099 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1100 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1101 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1102 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
1103 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
1104 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
1105 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
1106 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
1109 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1111 if (tb[IFLA_ADDRESS]) {
1112 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1115 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1116 return -EADDRNOTAVAIL;
1122 if (data[IFLA_GENEVE_ID]) {
1123 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1125 if (vni >= GENEVE_VID_MASK)
1132 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1134 union geneve_addr *remote,
1136 bool *tun_on_same_port,
1137 bool *tun_collect_md)
1139 struct geneve_dev *geneve, *t;
1141 *tun_on_same_port = false;
1142 *tun_collect_md = false;
1144 list_for_each_entry(geneve, &gn->geneve_list, next) {
1145 if (geneve->dst_port == dst_port) {
1146 *tun_collect_md = geneve->collect_md;
1147 *tun_on_same_port = true;
1149 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
1150 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
1151 dst_port == geneve->dst_port)
1157 static int geneve_configure(struct net *net, struct net_device *dev,
1158 union geneve_addr *remote,
1159 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
1162 struct geneve_net *gn = net_generic(net, geneve_net_id);
1163 struct geneve_dev *t, *geneve = netdev_priv(dev);
1164 bool tun_collect_md, tun_on_same_port;
1170 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1176 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1177 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1178 geneve->vni[2] = vni & 0x000000ff;
1180 if ((remote->sa.sa_family == AF_INET &&
1181 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1182 (remote->sa.sa_family == AF_INET6 &&
1183 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
1185 geneve->remote = *remote;
1189 geneve->dst_port = dst_port;
1190 geneve->collect_md = metadata;
1192 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
1193 &tun_on_same_port, &tun_collect_md);
1197 /* make enough headroom for basic scenario */
1198 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1199 if (remote->sa.sa_family == AF_INET)
1200 encap_len += sizeof(struct iphdr);
1202 encap_len += sizeof(struct ipv6hdr);
1203 dev->needed_headroom = encap_len + ETH_HLEN;
1206 if (tun_on_same_port)
1213 err = register_netdevice(dev);
1217 list_add(&geneve->next, &gn->geneve_list);
1221 static int geneve_newlink(struct net *net, struct net_device *dev,
1222 struct nlattr *tb[], struct nlattr *data[])
1224 __be16 dst_port = htons(GENEVE_UDP_PORT);
1225 __u8 ttl = 0, tos = 0;
1226 bool metadata = false;
1227 union geneve_addr remote = geneve_remote_unspec;
1230 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1233 if (data[IFLA_GENEVE_REMOTE]) {
1234 remote.sa.sa_family = AF_INET;
1235 remote.sin.sin_addr.s_addr =
1236 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1239 if (data[IFLA_GENEVE_REMOTE6]) {
1240 if (!IS_ENABLED(CONFIG_IPV6))
1241 return -EPFNOSUPPORT;
1243 remote.sa.sa_family = AF_INET6;
1244 remote.sin6.sin6_addr =
1245 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1247 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1248 IPV6_ADDR_LINKLOCAL) {
1249 netdev_dbg(dev, "link-local remote is unsupported\n");
1254 if (data[IFLA_GENEVE_ID])
1255 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1257 if (data[IFLA_GENEVE_TTL])
1258 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1260 if (data[IFLA_GENEVE_TOS])
1261 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1263 if (data[IFLA_GENEVE_PORT])
1264 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
1266 if (data[IFLA_GENEVE_COLLECT_METADATA])
1269 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
1273 static void geneve_dellink(struct net_device *dev, struct list_head *head)
1275 struct geneve_dev *geneve = netdev_priv(dev);
1277 list_del(&geneve->next);
1278 unregister_netdevice_queue(dev, head);
1281 static size_t geneve_get_size(const struct net_device *dev)
1283 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
1284 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
1285 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
1286 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
1287 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
1288 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1292 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1294 struct geneve_dev *geneve = netdev_priv(dev);
1297 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1298 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1299 goto nla_put_failure;
1301 if (geneve->remote.sa.sa_family == AF_INET) {
1302 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1303 geneve->remote.sin.sin_addr.s_addr))
1304 goto nla_put_failure;
1305 #if IS_ENABLED(CONFIG_IPV6)
1307 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1308 &geneve->remote.sin6.sin6_addr))
1309 goto nla_put_failure;
1313 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1314 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
1315 goto nla_put_failure;
1317 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
1318 goto nla_put_failure;
1320 if (geneve->collect_md) {
1321 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1322 goto nla_put_failure;
1331 static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1333 .maxtype = IFLA_GENEVE_MAX,
1334 .policy = geneve_policy,
1335 .priv_size = sizeof(struct geneve_dev),
1336 .setup = geneve_setup,
1337 .validate = geneve_validate,
1338 .newlink = geneve_newlink,
1339 .dellink = geneve_dellink,
1340 .get_size = geneve_get_size,
1341 .fill_info = geneve_fill_info,
1344 struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1345 u8 name_assign_type, u16 dst_port)
1347 struct nlattr *tb[IFLA_MAX + 1];
1348 struct net_device *dev;
1351 memset(tb, 0, sizeof(tb));
1352 dev = rtnl_create_link(net, name, name_assign_type,
1353 &geneve_link_ops, tb);
1357 err = geneve_configure(net, dev, &geneve_remote_unspec,
1358 0, 0, 0, htons(dst_port), true);
1362 /* openvswitch users expect packet sizes to be unrestricted,
1363 * so set the largest MTU we can.
1365 err = geneve_change_mtu(dev, IP_MAX_MTU);
1373 return ERR_PTR(err);
1375 EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1377 static __net_init int geneve_init_net(struct net *net)
1379 struct geneve_net *gn = net_generic(net, geneve_net_id);
1381 INIT_LIST_HEAD(&gn->geneve_list);
1382 INIT_LIST_HEAD(&gn->sock_list);
1386 static void __net_exit geneve_exit_net(struct net *net)
1388 struct geneve_net *gn = net_generic(net, geneve_net_id);
1389 struct geneve_dev *geneve, *next;
1390 struct net_device *dev, *aux;
1395 /* gather any geneve devices that were moved into this ns */
1396 for_each_netdev_safe(net, dev, aux)
1397 if (dev->rtnl_link_ops == &geneve_link_ops)
1398 unregister_netdevice_queue(dev, &list);
1400 /* now gather any other geneve devices that were created in this ns */
1401 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1402 /* If geneve->dev is in the same netns, it was already added
1403 * to the list by the previous loop.
1405 if (!net_eq(dev_net(geneve->dev), net))
1406 unregister_netdevice_queue(geneve->dev, &list);
1409 /* unregister the devices gathered above */
1410 unregister_netdevice_many(&list);
1414 static struct pernet_operations geneve_net_ops = {
1415 .init = geneve_init_net,
1416 .exit = geneve_exit_net,
1417 .id = &geneve_net_id,
1418 .size = sizeof(struct geneve_net),
1421 static int __init geneve_init_module(void)
1425 rc = register_pernet_subsys(&geneve_net_ops);
1429 rc = rtnl_link_register(&geneve_link_ops);
1435 unregister_pernet_subsys(&geneve_net_ops);
1439 late_initcall(geneve_init_module);
1441 static void __exit geneve_cleanup_module(void)
1443 rtnl_link_unregister(&geneve_link_ops);
1444 unregister_pernet_subsys(&geneve_net_ops);
1446 module_exit(geneve_cleanup_module);
1448 MODULE_LICENSE("GPL");
1449 MODULE_VERSION(GENEVE_NETDEV_VER);
1450 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1451 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1452 MODULE_ALIAS_RTNL_LINK("geneve");