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/etherdevice.h>
16 #include <linux/hash.h>
17 #include <net/dst_metadata.h>
18 #include <net/gro_cells.h>
19 #include <net/rtnetlink.h>
20 #include <net/geneve.h>
21 #include <net/protocol.h>
23 #define GENEVE_NETDEV_VER "0.6"
25 #define GENEVE_UDP_PORT 6081
27 #define GENEVE_N_VID (1u << 24)
28 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
30 #define VNI_HASH_BITS 10
31 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
33 static bool log_ecn_error
= true;
34 module_param(log_ecn_error
, bool, 0644);
35 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
38 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
39 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
40 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
42 /* per-network namespace private data for this module */
44 struct list_head geneve_list
;
45 struct list_head sock_list
;
48 static unsigned int geneve_net_id
;
50 struct geneve_dev_node
{
51 struct hlist_node hlist
;
52 struct geneve_dev
*geneve
;
55 /* Pseudo network device */
57 struct geneve_dev_node hlist4
; /* vni hash table for IPv4 socket */
58 #if IS_ENABLED(CONFIG_IPV6)
59 struct geneve_dev_node hlist6
; /* vni hash table for IPv6 socket */
61 struct net
*net
; /* netns for packet i/o */
62 struct net_device
*dev
; /* netdev for geneve tunnel */
63 struct ip_tunnel_info info
;
64 struct geneve_sock __rcu
*sock4
; /* IPv4 socket used for geneve tunnel */
65 #if IS_ENABLED(CONFIG_IPV6)
66 struct geneve_sock __rcu
*sock6
; /* IPv6 socket used for geneve tunnel */
68 struct list_head next
; /* geneve's per namespace list */
69 struct gro_cells gro_cells
;
71 bool use_udp6_rx_checksums
;
77 struct list_head list
;
81 struct hlist_head vni_list
[VNI_HASH_SIZE
];
84 static inline __u32
geneve_net_vni_hash(u8 vni
[3])
88 vnid
= (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
89 return hash_32(vnid
, VNI_HASH_BITS
);
92 static __be64
vni_to_tunnel_id(const __u8
*vni
)
95 return (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
97 return (__force __be64
)(((__force u64
)vni
[0] << 40) |
98 ((__force u64
)vni
[1] << 48) |
99 ((__force u64
)vni
[2] << 56));
103 /* Convert 64 bit tunnel ID to 24 bit VNI. */
104 static void tunnel_id_to_vni(__be64 tun_id
, __u8
*vni
)
107 vni
[0] = (__force __u8
)(tun_id
>> 16);
108 vni
[1] = (__force __u8
)(tun_id
>> 8);
109 vni
[2] = (__force __u8
)tun_id
;
111 vni
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
112 vni
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
113 vni
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
117 static bool eq_tun_id_and_vni(u8
*tun_id
, u8
*vni
)
119 return !memcmp(vni
, &tun_id
[5], 3);
122 static sa_family_t
geneve_get_sk_family(struct geneve_sock
*gs
)
124 return gs
->sock
->sk
->sk_family
;
127 static struct geneve_dev
*geneve_lookup(struct geneve_sock
*gs
,
128 __be32 addr
, u8 vni
[])
130 struct hlist_head
*vni_list_head
;
131 struct geneve_dev_node
*node
;
134 /* Find the device for this VNI */
135 hash
= geneve_net_vni_hash(vni
);
136 vni_list_head
= &gs
->vni_list
[hash
];
137 hlist_for_each_entry_rcu(node
, vni_list_head
, hlist
) {
138 if (eq_tun_id_and_vni((u8
*)&node
->geneve
->info
.key
.tun_id
, vni
) &&
139 addr
== node
->geneve
->info
.key
.u
.ipv4
.dst
)
145 #if IS_ENABLED(CONFIG_IPV6)
146 static struct geneve_dev
*geneve6_lookup(struct geneve_sock
*gs
,
147 struct in6_addr addr6
, u8 vni
[])
149 struct hlist_head
*vni_list_head
;
150 struct geneve_dev_node
*node
;
153 /* Find the device for this VNI */
154 hash
= geneve_net_vni_hash(vni
);
155 vni_list_head
= &gs
->vni_list
[hash
];
156 hlist_for_each_entry_rcu(node
, vni_list_head
, hlist
) {
157 if (eq_tun_id_and_vni((u8
*)&node
->geneve
->info
.key
.tun_id
, vni
) &&
158 ipv6_addr_equal(&addr6
, &node
->geneve
->info
.key
.u
.ipv6
.dst
))
165 static inline struct genevehdr
*geneve_hdr(const struct sk_buff
*skb
)
167 return (struct genevehdr
*)(udp_hdr(skb
) + 1);
170 static struct geneve_dev
*geneve_lookup_skb(struct geneve_sock
*gs
,
173 static u8 zero_vni
[3];
176 if (geneve_get_sk_family(gs
) == AF_INET
) {
180 iph
= ip_hdr(skb
); /* outer IP header... */
182 if (gs
->collect_md
) {
186 vni
= geneve_hdr(skb
)->vni
;
190 return geneve_lookup(gs
, addr
, vni
);
191 #if IS_ENABLED(CONFIG_IPV6)
192 } else if (geneve_get_sk_family(gs
) == AF_INET6
) {
193 static struct in6_addr zero_addr6
;
194 struct ipv6hdr
*ip6h
;
195 struct in6_addr addr6
;
197 ip6h
= ipv6_hdr(skb
); /* outer IPv6 header... */
199 if (gs
->collect_md
) {
203 vni
= geneve_hdr(skb
)->vni
;
207 return geneve6_lookup(gs
, addr6
, vni
);
213 /* geneve receive/decap routine */
214 static void geneve_rx(struct geneve_dev
*geneve
, struct geneve_sock
*gs
,
217 struct genevehdr
*gnvh
= geneve_hdr(skb
);
218 struct metadata_dst
*tun_dst
= NULL
;
219 struct pcpu_sw_netstats
*stats
;
224 if (ip_tunnel_collect_metadata() || gs
->collect_md
) {
227 flags
= TUNNEL_KEY
| TUNNEL_GENEVE_OPT
|
228 (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 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
300 u64_stats_update_begin(&stats
->syncp
);
302 stats
->rx_bytes
+= len
;
303 u64_stats_update_end(&stats
->syncp
);
307 /* Consume bad packet */
311 /* Setup stats when device is created */
312 static int geneve_init(struct net_device
*dev
)
314 struct geneve_dev
*geneve
= netdev_priv(dev
);
317 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
321 err
= gro_cells_init(&geneve
->gro_cells
, dev
);
323 free_percpu(dev
->tstats
);
327 err
= dst_cache_init(&geneve
->info
.dst_cache
, GFP_KERNEL
);
329 free_percpu(dev
->tstats
);
330 gro_cells_destroy(&geneve
->gro_cells
);
336 static void geneve_uninit(struct net_device
*dev
)
338 struct geneve_dev
*geneve
= netdev_priv(dev
);
340 dst_cache_destroy(&geneve
->info
.dst_cache
);
341 gro_cells_destroy(&geneve
->gro_cells
);
342 free_percpu(dev
->tstats
);
345 /* Callback from net/ipv4/udp.c to receive packets */
346 static int geneve_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
348 struct genevehdr
*geneveh
;
349 struct geneve_dev
*geneve
;
350 struct geneve_sock
*gs
;
353 /* Need UDP and Geneve header to be present */
354 if (unlikely(!pskb_may_pull(skb
, GENEVE_BASE_HLEN
)))
357 /* Return packets with reserved bits set */
358 geneveh
= geneve_hdr(skb
);
359 if (unlikely(geneveh
->ver
!= GENEVE_VER
))
362 if (unlikely(geneveh
->proto_type
!= htons(ETH_P_TEB
)))
365 gs
= rcu_dereference_sk_user_data(sk
);
369 geneve
= geneve_lookup_skb(gs
, skb
);
373 opts_len
= geneveh
->opt_len
* 4;
374 if (iptunnel_pull_header(skb
, GENEVE_BASE_HLEN
+ opts_len
,
376 !net_eq(geneve
->net
, dev_net(geneve
->dev
)))) {
377 geneve
->dev
->stats
.rx_dropped
++;
381 geneve_rx(geneve
, gs
, skb
);
385 /* Consume bad packet */
390 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
391 __be16 port
, bool ipv6_rx_csum
)
394 struct udp_port_cfg udp_conf
;
397 memset(&udp_conf
, 0, sizeof(udp_conf
));
400 udp_conf
.family
= AF_INET6
;
401 udp_conf
.ipv6_v6only
= 1;
402 udp_conf
.use_udp6_rx_checksums
= ipv6_rx_csum
;
404 udp_conf
.family
= AF_INET
;
405 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
408 udp_conf
.local_udp_port
= port
;
410 /* Open UDP socket */
411 err
= udp_sock_create(net
, &udp_conf
, &sock
);
418 static int geneve_hlen(struct genevehdr
*gh
)
420 return sizeof(*gh
) + gh
->opt_len
* 4;
423 static struct sk_buff
*geneve_gro_receive(struct sock
*sk
,
424 struct list_head
*head
,
427 struct sk_buff
*pp
= NULL
;
429 struct genevehdr
*gh
, *gh2
;
430 unsigned int hlen
, gh_len
, off_gnv
;
431 const struct packet_offload
*ptype
;
435 off_gnv
= skb_gro_offset(skb
);
436 hlen
= off_gnv
+ sizeof(*gh
);
437 gh
= skb_gro_header_fast(skb
, off_gnv
);
438 if (skb_gro_header_hard(skb
, hlen
)) {
439 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
444 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
446 gh_len
= geneve_hlen(gh
);
448 hlen
= off_gnv
+ gh_len
;
449 if (skb_gro_header_hard(skb
, hlen
)) {
450 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
455 list_for_each_entry(p
, head
, list
) {
456 if (!NAPI_GRO_CB(p
)->same_flow
)
459 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
460 if (gh
->opt_len
!= gh2
->opt_len
||
461 memcmp(gh
, gh2
, gh_len
)) {
462 NAPI_GRO_CB(p
)->same_flow
= 0;
467 type
= gh
->proto_type
;
470 ptype
= gro_find_receive_by_type(type
);
474 skb_gro_pull(skb
, gh_len
);
475 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
476 pp
= call_gro_receive(ptype
->callbacks
.gro_receive
, head
, skb
);
482 skb_gro_flush_final(skb
, pp
, flush
);
487 static int geneve_gro_complete(struct sock
*sk
, struct sk_buff
*skb
,
490 struct genevehdr
*gh
;
491 struct packet_offload
*ptype
;
496 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
497 gh_len
= geneve_hlen(gh
);
498 type
= gh
->proto_type
;
501 ptype
= gro_find_complete_by_type(type
);
503 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
507 skb_set_inner_mac_header(skb
, nhoff
+ gh_len
);
512 /* Create new listen socket if needed */
513 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
514 bool ipv6
, bool ipv6_rx_csum
)
516 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
517 struct geneve_sock
*gs
;
519 struct udp_tunnel_sock_cfg tunnel_cfg
;
522 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
524 return ERR_PTR(-ENOMEM
);
526 sock
= geneve_create_sock(net
, ipv6
, port
, ipv6_rx_csum
);
529 return ERR_CAST(sock
);
534 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
535 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
537 /* Initialize the geneve udp offloads structure */
538 udp_tunnel_notify_add_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
540 /* Mark socket as an encapsulation socket */
541 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
542 tunnel_cfg
.sk_user_data
= gs
;
543 tunnel_cfg
.encap_type
= 1;
544 tunnel_cfg
.gro_receive
= geneve_gro_receive
;
545 tunnel_cfg
.gro_complete
= geneve_gro_complete
;
546 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
547 tunnel_cfg
.encap_destroy
= NULL
;
548 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
549 list_add(&gs
->list
, &gn
->sock_list
);
553 static void __geneve_sock_release(struct geneve_sock
*gs
)
555 if (!gs
|| --gs
->refcnt
)
559 udp_tunnel_notify_del_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
560 udp_tunnel_sock_release(gs
->sock
);
564 static void geneve_sock_release(struct geneve_dev
*geneve
)
566 struct geneve_sock
*gs4
= rtnl_dereference(geneve
->sock4
);
567 #if IS_ENABLED(CONFIG_IPV6)
568 struct geneve_sock
*gs6
= rtnl_dereference(geneve
->sock6
);
570 rcu_assign_pointer(geneve
->sock6
, NULL
);
573 rcu_assign_pointer(geneve
->sock4
, NULL
);
576 __geneve_sock_release(gs4
);
577 #if IS_ENABLED(CONFIG_IPV6)
578 __geneve_sock_release(gs6
);
582 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
586 struct geneve_sock
*gs
;
588 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
589 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
590 geneve_get_sk_family(gs
) == family
) {
597 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
599 struct net
*net
= geneve
->net
;
600 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
601 struct geneve_dev_node
*node
;
602 struct geneve_sock
*gs
;
606 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->info
.key
.tp_dst
);
612 gs
= geneve_socket_create(net
, geneve
->info
.key
.tp_dst
, ipv6
,
613 geneve
->use_udp6_rx_checksums
);
618 gs
->collect_md
= geneve
->collect_md
;
619 #if IS_ENABLED(CONFIG_IPV6)
621 rcu_assign_pointer(geneve
->sock6
, gs
);
622 node
= &geneve
->hlist6
;
626 rcu_assign_pointer(geneve
->sock4
, gs
);
627 node
= &geneve
->hlist4
;
629 node
->geneve
= geneve
;
631 tunnel_id_to_vni(geneve
->info
.key
.tun_id
, vni
);
632 hash
= geneve_net_vni_hash(vni
);
633 hlist_add_head_rcu(&node
->hlist
, &gs
->vni_list
[hash
]);
637 static int geneve_open(struct net_device
*dev
)
639 struct geneve_dev
*geneve
= netdev_priv(dev
);
640 bool ipv6
= !!(geneve
->info
.mode
& IP_TUNNEL_INFO_IPV6
);
641 bool metadata
= geneve
->collect_md
;
644 #if IS_ENABLED(CONFIG_IPV6)
645 if (ipv6
|| metadata
)
646 ret
= geneve_sock_add(geneve
, true);
648 if (!ret
&& (!ipv6
|| metadata
))
649 ret
= geneve_sock_add(geneve
, false);
651 geneve_sock_release(geneve
);
656 static int geneve_stop(struct net_device
*dev
)
658 struct geneve_dev
*geneve
= netdev_priv(dev
);
660 hlist_del_init_rcu(&geneve
->hlist4
.hlist
);
661 #if IS_ENABLED(CONFIG_IPV6)
662 hlist_del_init_rcu(&geneve
->hlist6
.hlist
);
664 geneve_sock_release(geneve
);
668 static void geneve_build_header(struct genevehdr
*geneveh
,
669 const struct ip_tunnel_info
*info
)
671 geneveh
->ver
= GENEVE_VER
;
672 geneveh
->opt_len
= info
->options_len
/ 4;
673 geneveh
->oam
= !!(info
->key
.tun_flags
& TUNNEL_OAM
);
674 geneveh
->critical
= !!(info
->key
.tun_flags
& TUNNEL_CRIT_OPT
);
676 tunnel_id_to_vni(info
->key
.tun_id
, geneveh
->vni
);
677 geneveh
->proto_type
= htons(ETH_P_TEB
);
680 if (info
->key
.tun_flags
& TUNNEL_GENEVE_OPT
)
681 ip_tunnel_info_opts_get(geneveh
->options
, info
);
684 static int geneve_build_skb(struct dst_entry
*dst
, struct sk_buff
*skb
,
685 const struct ip_tunnel_info
*info
,
686 bool xnet
, int ip_hdr_len
)
688 bool udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
689 struct genevehdr
*gnvh
;
693 skb_reset_mac_header(skb
);
694 skb_scrub_packet(skb
, xnet
);
696 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
+
697 GENEVE_BASE_HLEN
+ info
->options_len
+ ip_hdr_len
;
698 err
= skb_cow_head(skb
, min_headroom
);
702 err
= udp_tunnel_handle_offloads(skb
, udp_sum
);
706 gnvh
= __skb_push(skb
, sizeof(*gnvh
) + info
->options_len
);
707 geneve_build_header(gnvh
, info
);
708 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
716 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
717 struct net_device
*dev
,
718 struct geneve_sock
*gs4
,
720 const struct ip_tunnel_info
*info
)
722 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
723 struct geneve_dev
*geneve
= netdev_priv(dev
);
724 struct dst_cache
*dst_cache
;
725 struct rtable
*rt
= NULL
;
729 return ERR_PTR(-EIO
);
731 memset(fl4
, 0, sizeof(*fl4
));
732 fl4
->flowi4_mark
= skb
->mark
;
733 fl4
->flowi4_proto
= IPPROTO_UDP
;
734 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
735 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
738 if ((tos
== 1) && !geneve
->collect_md
) {
739 tos
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
742 fl4
->flowi4_tos
= RT_TOS(tos
);
744 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
746 rt
= dst_cache_get_ip4(dst_cache
, &fl4
->saddr
);
750 rt
= ip_route_output_key(geneve
->net
, fl4
);
752 netdev_dbg(dev
, "no route to %pI4\n", &fl4
->daddr
);
753 return ERR_PTR(-ENETUNREACH
);
755 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
756 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
->daddr
);
758 return ERR_PTR(-ELOOP
);
761 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
->saddr
);
765 #if IS_ENABLED(CONFIG_IPV6)
766 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
767 struct net_device
*dev
,
768 struct geneve_sock
*gs6
,
770 const struct ip_tunnel_info
*info
)
772 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
773 struct geneve_dev
*geneve
= netdev_priv(dev
);
774 struct dst_entry
*dst
= NULL
;
775 struct dst_cache
*dst_cache
;
779 return ERR_PTR(-EIO
);
781 memset(fl6
, 0, sizeof(*fl6
));
782 fl6
->flowi6_mark
= skb
->mark
;
783 fl6
->flowi6_proto
= IPPROTO_UDP
;
784 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
785 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
786 prio
= info
->key
.tos
;
787 if ((prio
== 1) && !geneve
->collect_md
) {
788 prio
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
792 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(prio
),
794 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
796 dst
= dst_cache_get_ip6(dst_cache
, &fl6
->saddr
);
800 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
801 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
802 return ERR_PTR(-ENETUNREACH
);
804 if (dst
->dev
== dev
) { /* is this necessary? */
805 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
807 return ERR_PTR(-ELOOP
);
811 dst_cache_set_ip6(dst_cache
, dst
, &fl6
->saddr
);
816 static int geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
817 struct geneve_dev
*geneve
,
818 const struct ip_tunnel_info
*info
)
820 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
821 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
822 const struct ip_tunnel_key
*key
= &info
->key
;
830 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
834 skb_tunnel_check_pmtu(skb
, &rt
->dst
,
835 GENEVE_IPV4_HLEN
+ info
->options_len
);
837 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
838 if (geneve
->collect_md
) {
839 tos
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
842 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, ip_hdr(skb
), skb
);
843 if (geneve
->ttl_inherit
)
844 ttl
= ip_tunnel_get_ttl(ip_hdr(skb
), skb
);
847 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
849 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
851 err
= geneve_build_skb(&rt
->dst
, skb
, info
, xnet
, sizeof(struct iphdr
));
855 udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
856 tos
, ttl
, df
, sport
, geneve
->info
.key
.tp_dst
,
857 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
858 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
862 #if IS_ENABLED(CONFIG_IPV6)
863 static int geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
864 struct geneve_dev
*geneve
,
865 const struct ip_tunnel_info
*info
)
867 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
868 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
869 const struct ip_tunnel_key
*key
= &info
->key
;
870 struct dst_entry
*dst
= NULL
;
876 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
880 skb_tunnel_check_pmtu(skb
, dst
, GENEVE_IPV6_HLEN
+ info
->options_len
);
882 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
883 if (geneve
->collect_md
) {
884 prio
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
887 prio
= ip_tunnel_ecn_encap(ip6_tclass(fl6
.flowlabel
),
889 if (geneve
->ttl_inherit
)
890 ttl
= ip_tunnel_get_ttl(ip_hdr(skb
), skb
);
893 ttl
= ttl
? : ip6_dst_hoplimit(dst
);
895 err
= geneve_build_skb(dst
, skb
, info
, xnet
, sizeof(struct ipv6hdr
));
899 udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
900 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
,
901 info
->key
.label
, sport
, geneve
->info
.key
.tp_dst
,
902 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
907 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
909 struct geneve_dev
*geneve
= netdev_priv(dev
);
910 struct ip_tunnel_info
*info
= NULL
;
913 if (geneve
->collect_md
) {
914 info
= skb_tunnel_info(skb
);
915 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
917 netdev_dbg(dev
, "no tunnel metadata\n");
921 info
= &geneve
->info
;
925 #if IS_ENABLED(CONFIG_IPV6)
926 if (info
->mode
& IP_TUNNEL_INFO_IPV6
)
927 err
= geneve6_xmit_skb(skb
, dev
, geneve
, info
);
930 err
= geneve_xmit_skb(skb
, dev
, geneve
, info
);
939 dev
->stats
.collisions
++;
940 else if (err
== -ENETUNREACH
)
941 dev
->stats
.tx_carrier_errors
++;
943 dev
->stats
.tx_errors
++;
947 static int geneve_change_mtu(struct net_device
*dev
, int new_mtu
)
949 if (new_mtu
> dev
->max_mtu
)
950 new_mtu
= dev
->max_mtu
;
951 else if (new_mtu
< dev
->min_mtu
)
952 new_mtu
= dev
->min_mtu
;
958 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
960 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
961 struct geneve_dev
*geneve
= netdev_priv(dev
);
963 if (ip_tunnel_info_af(info
) == AF_INET
) {
966 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
968 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
973 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
974 #if IS_ENABLED(CONFIG_IPV6)
975 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
976 struct dst_entry
*dst
;
978 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
980 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
985 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
991 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
993 info
->key
.tp_dst
= geneve
->info
.key
.tp_dst
;
997 static const struct net_device_ops geneve_netdev_ops
= {
998 .ndo_init
= geneve_init
,
999 .ndo_uninit
= geneve_uninit
,
1000 .ndo_open
= geneve_open
,
1001 .ndo_stop
= geneve_stop
,
1002 .ndo_start_xmit
= geneve_xmit
,
1003 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1004 .ndo_change_mtu
= geneve_change_mtu
,
1005 .ndo_validate_addr
= eth_validate_addr
,
1006 .ndo_set_mac_address
= eth_mac_addr
,
1007 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1010 static void geneve_get_drvinfo(struct net_device
*dev
,
1011 struct ethtool_drvinfo
*drvinfo
)
1013 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1014 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1017 static const struct ethtool_ops geneve_ethtool_ops
= {
1018 .get_drvinfo
= geneve_get_drvinfo
,
1019 .get_link
= ethtool_op_get_link
,
1022 /* Info for udev, that this is a virtual tunnel endpoint */
1023 static struct device_type geneve_type
= {
1027 /* Calls the ndo_udp_tunnel_add of the caller in order to
1028 * supply the listening GENEVE udp ports. Callers are expected
1029 * to implement the ndo_udp_tunnel_add.
1031 static void geneve_offload_rx_ports(struct net_device
*dev
, bool push
)
1033 struct net
*net
= dev_net(dev
);
1034 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1035 struct geneve_sock
*gs
;
1038 list_for_each_entry_rcu(gs
, &gn
->sock_list
, list
) {
1040 udp_tunnel_push_rx_port(dev
, gs
->sock
,
1041 UDP_TUNNEL_TYPE_GENEVE
);
1043 udp_tunnel_drop_rx_port(dev
, gs
->sock
,
1044 UDP_TUNNEL_TYPE_GENEVE
);
1050 /* Initialize the device structure. */
1051 static void geneve_setup(struct net_device
*dev
)
1055 dev
->netdev_ops
= &geneve_netdev_ops
;
1056 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1057 dev
->needs_free_netdev
= true;
1059 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1061 dev
->features
|= NETIF_F_LLTX
;
1062 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1063 dev
->features
|= NETIF_F_RXCSUM
;
1064 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1066 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1067 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1069 /* MTU range: 68 - (something less than 65535) */
1070 dev
->min_mtu
= ETH_MIN_MTU
;
1071 /* The max_mtu calculation does not take account of GENEVE
1072 * options, to avoid excluding potentially valid
1073 * configurations. This will be further reduced by IPvX hdr size.
1075 dev
->max_mtu
= IP_MAX_MTU
- GENEVE_BASE_HLEN
- dev
->hard_header_len
;
1077 netif_keep_dst(dev
);
1078 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1079 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1080 eth_hw_addr_random(dev
);
1083 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1084 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1085 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1086 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1087 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1088 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1089 [IFLA_GENEVE_LABEL
] = { .type
= NLA_U32
},
1090 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1091 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1092 [IFLA_GENEVE_UDP_CSUM
] = { .type
= NLA_U8
},
1093 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
1094 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
1095 [IFLA_GENEVE_TTL_INHERIT
] = { .type
= NLA_U8
},
1098 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1099 struct netlink_ext_ack
*extack
)
1101 if (tb
[IFLA_ADDRESS
]) {
1102 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1103 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1104 "Provided link layer address is not Ethernet");
1108 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1109 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1110 "Provided Ethernet address is not unicast");
1111 return -EADDRNOTAVAIL
;
1116 NL_SET_ERR_MSG(extack
,
1117 "Not enough attributes provided to perform the operation");
1121 if (data
[IFLA_GENEVE_ID
]) {
1122 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1124 if (vni
>= GENEVE_N_VID
) {
1125 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_ID
],
1126 "Geneve ID must be lower than 16777216");
1134 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1135 const struct ip_tunnel_info
*info
,
1136 bool *tun_on_same_port
,
1137 bool *tun_collect_md
)
1139 struct geneve_dev
*geneve
, *t
= NULL
;
1141 *tun_on_same_port
= false;
1142 *tun_collect_md
= false;
1143 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1144 if (info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
) {
1145 *tun_collect_md
= geneve
->collect_md
;
1146 *tun_on_same_port
= true;
1148 if (info
->key
.tun_id
== geneve
->info
.key
.tun_id
&&
1149 info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
&&
1150 !memcmp(&info
->key
.u
, &geneve
->info
.key
.u
, sizeof(info
->key
.u
)))
1156 static bool is_tnl_info_zero(const struct ip_tunnel_info
*info
)
1158 return !(info
->key
.tun_id
|| info
->key
.tun_flags
|| info
->key
.tos
||
1159 info
->key
.ttl
|| info
->key
.label
|| info
->key
.tp_src
||
1160 memchr_inv(&info
->key
.u
, 0, sizeof(info
->key
.u
)));
1163 static bool geneve_dst_addr_equal(struct ip_tunnel_info
*a
,
1164 struct ip_tunnel_info
*b
)
1166 if (ip_tunnel_info_af(a
) == AF_INET
)
1167 return a
->key
.u
.ipv4
.dst
== b
->key
.u
.ipv4
.dst
;
1169 return ipv6_addr_equal(&a
->key
.u
.ipv6
.dst
, &b
->key
.u
.ipv6
.dst
);
1172 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1173 struct netlink_ext_ack
*extack
,
1174 const struct ip_tunnel_info
*info
,
1175 bool metadata
, bool ipv6_rx_csum
,
1178 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1179 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1180 bool tun_collect_md
, tun_on_same_port
;
1183 if (metadata
&& !is_tnl_info_zero(info
)) {
1184 NL_SET_ERR_MSG(extack
,
1185 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
1192 t
= geneve_find_dev(gn
, info
, &tun_on_same_port
, &tun_collect_md
);
1196 /* make enough headroom for basic scenario */
1197 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1198 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1199 encap_len
+= sizeof(struct iphdr
);
1200 dev
->max_mtu
-= sizeof(struct iphdr
);
1202 encap_len
+= sizeof(struct ipv6hdr
);
1203 dev
->max_mtu
-= sizeof(struct ipv6hdr
);
1205 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1208 if (tun_on_same_port
) {
1209 NL_SET_ERR_MSG(extack
,
1210 "There can be only one externally controlled device on a destination port");
1214 if (tun_collect_md
) {
1215 NL_SET_ERR_MSG(extack
,
1216 "There already exists an externally controlled device on this destination port");
1221 dst_cache_reset(&geneve
->info
.dst_cache
);
1222 geneve
->info
= *info
;
1223 geneve
->collect_md
= metadata
;
1224 geneve
->use_udp6_rx_checksums
= ipv6_rx_csum
;
1225 geneve
->ttl_inherit
= ttl_inherit
;
1227 err
= register_netdevice(dev
);
1231 list_add(&geneve
->next
, &gn
->geneve_list
);
1235 static void init_tnl_info(struct ip_tunnel_info
*info
, __u16 dst_port
)
1237 memset(info
, 0, sizeof(*info
));
1238 info
->key
.tp_dst
= htons(dst_port
);
1241 static int geneve_nl2info(struct nlattr
*tb
[], struct nlattr
*data
[],
1242 struct netlink_ext_ack
*extack
,
1243 struct ip_tunnel_info
*info
, bool *metadata
,
1244 bool *use_udp6_rx_checksums
, bool *ttl_inherit
,
1249 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
]) {
1250 NL_SET_ERR_MSG(extack
,
1251 "Cannot specify both IPv4 and IPv6 Remote addresses");
1255 if (data
[IFLA_GENEVE_REMOTE
]) {
1256 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET6
)) {
1257 attrtype
= IFLA_GENEVE_REMOTE
;
1261 info
->key
.u
.ipv4
.dst
=
1262 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1264 if (IN_MULTICAST(ntohl(info
->key
.u
.ipv4
.dst
))) {
1265 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE
],
1266 "Remote IPv4 address cannot be Multicast");
1271 if (data
[IFLA_GENEVE_REMOTE6
]) {
1272 #if IS_ENABLED(CONFIG_IPV6)
1273 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET
)) {
1274 attrtype
= IFLA_GENEVE_REMOTE6
;
1278 info
->mode
= IP_TUNNEL_INFO_IPV6
;
1279 info
->key
.u
.ipv6
.dst
=
1280 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1282 if (ipv6_addr_type(&info
->key
.u
.ipv6
.dst
) &
1283 IPV6_ADDR_LINKLOCAL
) {
1284 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1285 "Remote IPv6 address cannot be link-local");
1288 if (ipv6_addr_is_multicast(&info
->key
.u
.ipv6
.dst
)) {
1289 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1290 "Remote IPv6 address cannot be Multicast");
1293 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1294 *use_udp6_rx_checksums
= true;
1296 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1297 "IPv6 support not enabled in the kernel");
1298 return -EPFNOSUPPORT
;
1302 if (data
[IFLA_GENEVE_ID
]) {
1307 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1308 tvni
[0] = (vni
& 0x00ff0000) >> 16;
1309 tvni
[1] = (vni
& 0x0000ff00) >> 8;
1310 tvni
[2] = vni
& 0x000000ff;
1312 tunid
= vni_to_tunnel_id(tvni
);
1313 if (changelink
&& (tunid
!= info
->key
.tun_id
)) {
1314 attrtype
= IFLA_GENEVE_ID
;
1317 info
->key
.tun_id
= tunid
;
1320 if (data
[IFLA_GENEVE_TTL_INHERIT
]) {
1321 if (nla_get_u8(data
[IFLA_GENEVE_TTL_INHERIT
]))
1322 *ttl_inherit
= true;
1324 *ttl_inherit
= false;
1325 } else if (data
[IFLA_GENEVE_TTL
]) {
1326 info
->key
.ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1327 *ttl_inherit
= false;
1330 if (data
[IFLA_GENEVE_TOS
])
1331 info
->key
.tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1333 if (data
[IFLA_GENEVE_LABEL
]) {
1334 info
->key
.label
= nla_get_be32(data
[IFLA_GENEVE_LABEL
]) &
1335 IPV6_FLOWLABEL_MASK
;
1336 if (info
->key
.label
&& (!(info
->mode
& IP_TUNNEL_INFO_IPV6
))) {
1337 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_LABEL
],
1338 "Label attribute only applies for IPv6 Geneve devices");
1343 if (data
[IFLA_GENEVE_PORT
]) {
1345 attrtype
= IFLA_GENEVE_PORT
;
1348 info
->key
.tp_dst
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1351 if (data
[IFLA_GENEVE_COLLECT_METADATA
]) {
1353 attrtype
= IFLA_GENEVE_COLLECT_METADATA
;
1359 if (data
[IFLA_GENEVE_UDP_CSUM
]) {
1361 attrtype
= IFLA_GENEVE_UDP_CSUM
;
1364 if (nla_get_u8(data
[IFLA_GENEVE_UDP_CSUM
]))
1365 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1368 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]) {
1369 #if IS_ENABLED(CONFIG_IPV6)
1371 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_TX
;
1374 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
1375 info
->key
.tun_flags
&= ~TUNNEL_CSUM
;
1377 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
],
1378 "IPv6 support not enabled in the kernel");
1379 return -EPFNOSUPPORT
;
1383 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]) {
1384 #if IS_ENABLED(CONFIG_IPV6)
1386 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_RX
;
1389 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
1390 *use_udp6_rx_checksums
= false;
1392 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
],
1393 "IPv6 support not enabled in the kernel");
1394 return -EPFNOSUPPORT
;
1400 NL_SET_ERR_MSG_ATTR(extack
, data
[attrtype
],
1401 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1405 static void geneve_link_config(struct net_device
*dev
,
1406 struct ip_tunnel_info
*info
, struct nlattr
*tb
[])
1408 struct geneve_dev
*geneve
= netdev_priv(dev
);
1412 geneve_change_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1416 switch (ip_tunnel_info_af(info
)) {
1418 struct flowi4 fl4
= { .daddr
= info
->key
.u
.ipv4
.dst
};
1419 struct rtable
*rt
= ip_route_output_key(geneve
->net
, &fl4
);
1421 if (!IS_ERR(rt
) && rt
->dst
.dev
) {
1422 ldev_mtu
= rt
->dst
.dev
->mtu
- GENEVE_IPV4_HLEN
;
1427 #if IS_ENABLED(CONFIG_IPV6)
1429 struct rt6_info
*rt
= rt6_lookup(geneve
->net
,
1430 &info
->key
.u
.ipv6
.dst
, NULL
, 0,
1433 if (rt
&& rt
->dst
.dev
)
1434 ldev_mtu
= rt
->dst
.dev
->mtu
- GENEVE_IPV6_HLEN
;
1444 geneve_change_mtu(dev
, ldev_mtu
- info
->options_len
);
1447 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1448 struct nlattr
*tb
[], struct nlattr
*data
[],
1449 struct netlink_ext_ack
*extack
)
1451 bool use_udp6_rx_checksums
= false;
1452 struct ip_tunnel_info info
;
1453 bool ttl_inherit
= false;
1454 bool metadata
= false;
1457 init_tnl_info(&info
, GENEVE_UDP_PORT
);
1458 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1459 &use_udp6_rx_checksums
, &ttl_inherit
, false);
1463 err
= geneve_configure(net
, dev
, extack
, &info
, metadata
,
1464 use_udp6_rx_checksums
, ttl_inherit
);
1468 geneve_link_config(dev
, &info
, tb
);
1473 /* Quiesces the geneve device data path for both TX and RX.
1475 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1476 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1477 * to complete for the existing set of in-flight packets to be transmitted,
1478 * then we would have quiesced the transmit data path. All the future packets
1479 * will get dropped until we unquiesce the data path.
1481 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1482 * if we set that to NULL under RCU and wait for synchronize_net() to
1483 * complete, then we would have quiesced the receive data path.
1485 static void geneve_quiesce(struct geneve_dev
*geneve
, struct geneve_sock
**gs4
,
1486 struct geneve_sock
**gs6
)
1488 *gs4
= rtnl_dereference(geneve
->sock4
);
1489 rcu_assign_pointer(geneve
->sock4
, NULL
);
1491 rcu_assign_sk_user_data((*gs4
)->sock
->sk
, NULL
);
1492 #if IS_ENABLED(CONFIG_IPV6)
1493 *gs6
= rtnl_dereference(geneve
->sock6
);
1494 rcu_assign_pointer(geneve
->sock6
, NULL
);
1496 rcu_assign_sk_user_data((*gs6
)->sock
->sk
, NULL
);
1503 /* Resumes the geneve device data path for both TX and RX. */
1504 static void geneve_unquiesce(struct geneve_dev
*geneve
, struct geneve_sock
*gs4
,
1505 struct geneve_sock __maybe_unused
*gs6
)
1507 rcu_assign_pointer(geneve
->sock4
, gs4
);
1509 rcu_assign_sk_user_data(gs4
->sock
->sk
, gs4
);
1510 #if IS_ENABLED(CONFIG_IPV6)
1511 rcu_assign_pointer(geneve
->sock6
, gs6
);
1513 rcu_assign_sk_user_data(gs6
->sock
->sk
, gs6
);
1518 static int geneve_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1519 struct nlattr
*data
[],
1520 struct netlink_ext_ack
*extack
)
1522 struct geneve_dev
*geneve
= netdev_priv(dev
);
1523 struct geneve_sock
*gs4
, *gs6
;
1524 struct ip_tunnel_info info
;
1526 bool use_udp6_rx_checksums
;
1530 /* If the geneve device is configured for metadata (or externally
1531 * controlled, for example, OVS), then nothing can be changed.
1533 if (geneve
->collect_md
)
1536 /* Start with the existing info. */
1537 memcpy(&info
, &geneve
->info
, sizeof(info
));
1538 metadata
= geneve
->collect_md
;
1539 use_udp6_rx_checksums
= geneve
->use_udp6_rx_checksums
;
1540 ttl_inherit
= geneve
->ttl_inherit
;
1541 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1542 &use_udp6_rx_checksums
, &ttl_inherit
, true);
1546 if (!geneve_dst_addr_equal(&geneve
->info
, &info
)) {
1547 dst_cache_reset(&info
.dst_cache
);
1548 geneve_link_config(dev
, &info
, tb
);
1551 geneve_quiesce(geneve
, &gs4
, &gs6
);
1552 geneve
->info
= info
;
1553 geneve
->collect_md
= metadata
;
1554 geneve
->use_udp6_rx_checksums
= use_udp6_rx_checksums
;
1555 geneve
->ttl_inherit
= ttl_inherit
;
1556 geneve_unquiesce(geneve
, gs4
, gs6
);
1561 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1563 struct geneve_dev
*geneve
= netdev_priv(dev
);
1565 list_del(&geneve
->next
);
1566 unregister_netdevice_queue(dev
, head
);
1569 static size_t geneve_get_size(const struct net_device
*dev
)
1571 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1572 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1573 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1574 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1575 nla_total_size(sizeof(__be32
)) + /* IFLA_GENEVE_LABEL */
1576 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1577 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1578 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_CSUM */
1579 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1580 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1581 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL_INHERIT */
1585 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1587 struct geneve_dev
*geneve
= netdev_priv(dev
);
1588 struct ip_tunnel_info
*info
= &geneve
->info
;
1589 bool ttl_inherit
= geneve
->ttl_inherit
;
1590 bool metadata
= geneve
->collect_md
;
1594 tunnel_id_to_vni(info
->key
.tun_id
, tmp_vni
);
1595 vni
= (tmp_vni
[0] << 16) | (tmp_vni
[1] << 8) | tmp_vni
[2];
1596 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1597 goto nla_put_failure
;
1599 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1600 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1601 info
->key
.u
.ipv4
.dst
))
1602 goto nla_put_failure
;
1603 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_CSUM
,
1604 !!(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1605 goto nla_put_failure
;
1607 #if IS_ENABLED(CONFIG_IPV6)
1608 } else if (!metadata
) {
1609 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1610 &info
->key
.u
.ipv6
.dst
))
1611 goto nla_put_failure
;
1612 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
1613 !(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1614 goto nla_put_failure
;
1618 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, info
->key
.ttl
) ||
1619 nla_put_u8(skb
, IFLA_GENEVE_TOS
, info
->key
.tos
) ||
1620 nla_put_be32(skb
, IFLA_GENEVE_LABEL
, info
->key
.label
))
1621 goto nla_put_failure
;
1623 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, info
->key
.tp_dst
))
1624 goto nla_put_failure
;
1626 if (metadata
&& nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1627 goto nla_put_failure
;
1629 #if IS_ENABLED(CONFIG_IPV6)
1630 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
1631 !geneve
->use_udp6_rx_checksums
))
1632 goto nla_put_failure
;
1635 if (nla_put_u8(skb
, IFLA_GENEVE_TTL_INHERIT
, ttl_inherit
))
1636 goto nla_put_failure
;
1644 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1646 .maxtype
= IFLA_GENEVE_MAX
,
1647 .policy
= geneve_policy
,
1648 .priv_size
= sizeof(struct geneve_dev
),
1649 .setup
= geneve_setup
,
1650 .validate
= geneve_validate
,
1651 .newlink
= geneve_newlink
,
1652 .changelink
= geneve_changelink
,
1653 .dellink
= geneve_dellink
,
1654 .get_size
= geneve_get_size
,
1655 .fill_info
= geneve_fill_info
,
1658 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1659 u8 name_assign_type
, u16 dst_port
)
1661 struct nlattr
*tb
[IFLA_MAX
+ 1];
1662 struct ip_tunnel_info info
;
1663 struct net_device
*dev
;
1664 LIST_HEAD(list_kill
);
1667 memset(tb
, 0, sizeof(tb
));
1668 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1669 &geneve_link_ops
, tb
);
1673 init_tnl_info(&info
, dst_port
);
1674 err
= geneve_configure(net
, dev
, NULL
, &info
, true, true, false);
1677 return ERR_PTR(err
);
1680 /* openvswitch users expect packet sizes to be unrestricted,
1681 * so set the largest MTU we can.
1683 err
= geneve_change_mtu(dev
, IP_MAX_MTU
);
1687 err
= rtnl_configure_link(dev
, NULL
);
1693 geneve_dellink(dev
, &list_kill
);
1694 unregister_netdevice_many(&list_kill
);
1695 return ERR_PTR(err
);
1697 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1699 static int geneve_netdevice_event(struct notifier_block
*unused
,
1700 unsigned long event
, void *ptr
)
1702 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1704 if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
||
1705 event
== NETDEV_UDP_TUNNEL_DROP_INFO
) {
1706 geneve_offload_rx_ports(dev
, event
== NETDEV_UDP_TUNNEL_PUSH_INFO
);
1707 } else if (event
== NETDEV_UNREGISTER
) {
1708 geneve_offload_rx_ports(dev
, false);
1709 } else if (event
== NETDEV_REGISTER
) {
1710 geneve_offload_rx_ports(dev
, true);
1716 static struct notifier_block geneve_notifier_block __read_mostly
= {
1717 .notifier_call
= geneve_netdevice_event
,
1720 static __net_init
int geneve_init_net(struct net
*net
)
1722 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1724 INIT_LIST_HEAD(&gn
->geneve_list
);
1725 INIT_LIST_HEAD(&gn
->sock_list
);
1729 static void geneve_destroy_tunnels(struct net
*net
, struct list_head
*head
)
1731 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1732 struct geneve_dev
*geneve
, *next
;
1733 struct net_device
*dev
, *aux
;
1735 /* gather any geneve devices that were moved into this ns */
1736 for_each_netdev_safe(net
, dev
, aux
)
1737 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1738 unregister_netdevice_queue(dev
, head
);
1740 /* now gather any other geneve devices that were created in this ns */
1741 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1742 /* If geneve->dev is in the same netns, it was already added
1743 * to the list by the previous loop.
1745 if (!net_eq(dev_net(geneve
->dev
), net
))
1746 unregister_netdevice_queue(geneve
->dev
, head
);
1749 WARN_ON_ONCE(!list_empty(&gn
->sock_list
));
1752 static void __net_exit
geneve_exit_batch_net(struct list_head
*net_list
)
1758 list_for_each_entry(net
, net_list
, exit_list
)
1759 geneve_destroy_tunnels(net
, &list
);
1761 /* unregister the devices gathered above */
1762 unregister_netdevice_many(&list
);
1766 static struct pernet_operations geneve_net_ops
= {
1767 .init
= geneve_init_net
,
1768 .exit_batch
= geneve_exit_batch_net
,
1769 .id
= &geneve_net_id
,
1770 .size
= sizeof(struct geneve_net
),
1773 static int __init
geneve_init_module(void)
1777 rc
= register_pernet_subsys(&geneve_net_ops
);
1781 rc
= register_netdevice_notifier(&geneve_notifier_block
);
1785 rc
= rtnl_link_register(&geneve_link_ops
);
1791 unregister_netdevice_notifier(&geneve_notifier_block
);
1793 unregister_pernet_subsys(&geneve_net_ops
);
1797 late_initcall(geneve_init_module
);
1799 static void __exit
geneve_cleanup_module(void)
1801 rtnl_link_unregister(&geneve_link_ops
);
1802 unregister_netdevice_notifier(&geneve_notifier_block
);
1803 unregister_pernet_subsys(&geneve_net_ops
);
1805 module_exit(geneve_cleanup_module
);
1807 MODULE_LICENSE("GPL");
1808 MODULE_VERSION(GENEVE_NETDEV_VER
);
1809 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1810 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1811 MODULE_ALIAS_RTNL_LINK("geneve");