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))
40 /* per-network namespace private data for this module */
42 struct list_head geneve_list
;
43 struct list_head sock_list
;
46 static unsigned int geneve_net_id
;
48 struct geneve_dev_node
{
49 struct hlist_node hlist
;
50 struct geneve_dev
*geneve
;
53 /* Pseudo network device */
55 struct geneve_dev_node hlist4
; /* vni hash table for IPv4 socket */
56 #if IS_ENABLED(CONFIG_IPV6)
57 struct geneve_dev_node hlist6
; /* vni hash table for IPv6 socket */
59 struct net
*net
; /* netns for packet i/o */
60 struct net_device
*dev
; /* netdev for geneve tunnel */
61 struct ip_tunnel_info info
;
62 struct geneve_sock __rcu
*sock4
; /* IPv4 socket used for geneve tunnel */
63 #if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_sock __rcu
*sock6
; /* IPv6 socket used for geneve tunnel */
66 struct list_head next
; /* geneve's per namespace list */
67 struct gro_cells gro_cells
;
69 bool use_udp6_rx_checksums
;
74 struct list_head list
;
78 struct hlist_head vni_list
[VNI_HASH_SIZE
];
81 static inline __u32
geneve_net_vni_hash(u8 vni
[3])
85 vnid
= (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
86 return hash_32(vnid
, VNI_HASH_BITS
);
89 static __be64
vni_to_tunnel_id(const __u8
*vni
)
92 return (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
94 return (__force __be64
)(((__force u64
)vni
[0] << 40) |
95 ((__force u64
)vni
[1] << 48) |
96 ((__force u64
)vni
[2] << 56));
100 /* Convert 64 bit tunnel ID to 24 bit VNI. */
101 static void tunnel_id_to_vni(__be64 tun_id
, __u8
*vni
)
104 vni
[0] = (__force __u8
)(tun_id
>> 16);
105 vni
[1] = (__force __u8
)(tun_id
>> 8);
106 vni
[2] = (__force __u8
)tun_id
;
108 vni
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
109 vni
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
110 vni
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
114 static bool eq_tun_id_and_vni(u8
*tun_id
, u8
*vni
)
116 return !memcmp(vni
, &tun_id
[5], 3);
119 static sa_family_t
geneve_get_sk_family(struct geneve_sock
*gs
)
121 return gs
->sock
->sk
->sk_family
;
124 static struct geneve_dev
*geneve_lookup(struct geneve_sock
*gs
,
125 __be32 addr
, u8 vni
[])
127 struct hlist_head
*vni_list_head
;
128 struct geneve_dev_node
*node
;
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(node
, vni_list_head
, hlist
) {
135 if (eq_tun_id_and_vni((u8
*)&node
->geneve
->info
.key
.tun_id
, vni
) &&
136 addr
== node
->geneve
->info
.key
.u
.ipv4
.dst
)
142 #if IS_ENABLED(CONFIG_IPV6)
143 static struct geneve_dev
*geneve6_lookup(struct geneve_sock
*gs
,
144 struct in6_addr addr6
, u8 vni
[])
146 struct hlist_head
*vni_list_head
;
147 struct geneve_dev_node
*node
;
150 /* Find the device for this VNI */
151 hash
= geneve_net_vni_hash(vni
);
152 vni_list_head
= &gs
->vni_list
[hash
];
153 hlist_for_each_entry_rcu(node
, vni_list_head
, hlist
) {
154 if (eq_tun_id_and_vni((u8
*)&node
->geneve
->info
.key
.tun_id
, vni
) &&
155 ipv6_addr_equal(&addr6
, &node
->geneve
->info
.key
.u
.ipv6
.dst
))
162 static inline struct genevehdr
*geneve_hdr(const struct sk_buff
*skb
)
164 return (struct genevehdr
*)(udp_hdr(skb
) + 1);
167 static struct geneve_dev
*geneve_lookup_skb(struct geneve_sock
*gs
,
170 static u8 zero_vni
[3];
173 if (geneve_get_sk_family(gs
) == AF_INET
) {
177 iph
= ip_hdr(skb
); /* outer IP header... */
179 if (gs
->collect_md
) {
183 vni
= geneve_hdr(skb
)->vni
;
187 return geneve_lookup(gs
, addr
, vni
);
188 #if IS_ENABLED(CONFIG_IPV6)
189 } else if (geneve_get_sk_family(gs
) == AF_INET6
) {
190 static struct in6_addr zero_addr6
;
191 struct ipv6hdr
*ip6h
;
192 struct in6_addr addr6
;
194 ip6h
= ipv6_hdr(skb
); /* outer IPv6 header... */
196 if (gs
->collect_md
) {
200 vni
= geneve_hdr(skb
)->vni
;
204 return geneve6_lookup(gs
, addr6
, vni
);
210 /* geneve receive/decap routine */
211 static void geneve_rx(struct geneve_dev
*geneve
, struct geneve_sock
*gs
,
214 struct genevehdr
*gnvh
= geneve_hdr(skb
);
215 struct metadata_dst
*tun_dst
= NULL
;
216 struct pcpu_sw_netstats
*stats
;
221 if (ip_tunnel_collect_metadata() || gs
->collect_md
) {
224 flags
= TUNNEL_KEY
| TUNNEL_GENEVE_OPT
|
225 (gnvh
->oam
? TUNNEL_OAM
: 0) |
226 (gnvh
->critical
? TUNNEL_CRIT_OPT
: 0);
228 tun_dst
= udp_tun_rx_dst(skb
, geneve_get_sk_family(gs
), flags
,
229 vni_to_tunnel_id(gnvh
->vni
),
232 geneve
->dev
->stats
.rx_dropped
++;
235 /* Update tunnel dst according to Geneve options. */
236 ip_tunnel_info_opts_set(&tun_dst
->u
.tun_info
,
237 gnvh
->options
, gnvh
->opt_len
* 4);
239 /* Drop packets w/ critical options,
240 * since we don't support any...
242 if (gnvh
->critical
) {
243 geneve
->dev
->stats
.rx_frame_errors
++;
244 geneve
->dev
->stats
.rx_errors
++;
249 skb_reset_mac_header(skb
);
250 skb
->protocol
= eth_type_trans(skb
, geneve
->dev
);
251 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
254 skb_dst_set(skb
, &tun_dst
->dst
);
256 /* Ignore packet loops (and multicast echo) */
257 if (ether_addr_equal(eth_hdr(skb
)->h_source
, geneve
->dev
->dev_addr
)) {
258 geneve
->dev
->stats
.rx_errors
++;
262 oiph
= skb_network_header(skb
);
263 skb_reset_network_header(skb
);
265 if (geneve_get_sk_family(gs
) == AF_INET
)
266 err
= IP_ECN_decapsulate(oiph
, skb
);
267 #if IS_ENABLED(CONFIG_IPV6)
269 err
= IP6_ECN_decapsulate(oiph
, skb
);
274 if (geneve_get_sk_family(gs
) == AF_INET
)
275 net_info_ratelimited("non-ECT from %pI4 "
277 &((struct iphdr
*)oiph
)->saddr
,
278 ((struct iphdr
*)oiph
)->tos
);
279 #if IS_ENABLED(CONFIG_IPV6)
281 net_info_ratelimited("non-ECT from %pI6\n",
282 &((struct ipv6hdr
*)oiph
)->saddr
);
286 ++geneve
->dev
->stats
.rx_frame_errors
;
287 ++geneve
->dev
->stats
.rx_errors
;
293 err
= gro_cells_receive(&geneve
->gro_cells
, skb
);
294 if (likely(err
== NET_RX_SUCCESS
)) {
295 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
296 u64_stats_update_begin(&stats
->syncp
);
298 stats
->rx_bytes
+= len
;
299 u64_stats_update_end(&stats
->syncp
);
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
->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
->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 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
387 __be16 port
, bool ipv6_rx_csum
)
390 struct udp_port_cfg udp_conf
;
393 memset(&udp_conf
, 0, sizeof(udp_conf
));
396 udp_conf
.family
= AF_INET6
;
397 udp_conf
.ipv6_v6only
= 1;
398 udp_conf
.use_udp6_rx_checksums
= ipv6_rx_csum
;
400 udp_conf
.family
= AF_INET
;
401 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
404 udp_conf
.local_udp_port
= port
;
406 /* Open UDP socket */
407 err
= udp_sock_create(net
, &udp_conf
, &sock
);
414 static int geneve_hlen(struct genevehdr
*gh
)
416 return sizeof(*gh
) + gh
->opt_len
* 4;
419 static struct sk_buff
**geneve_gro_receive(struct sock
*sk
,
420 struct sk_buff
**head
,
423 struct sk_buff
*p
, **pp
= NULL
;
424 struct genevehdr
*gh
, *gh2
;
425 unsigned int hlen
, gh_len
, off_gnv
;
426 const struct packet_offload
*ptype
;
430 off_gnv
= skb_gro_offset(skb
);
431 hlen
= off_gnv
+ sizeof(*gh
);
432 gh
= skb_gro_header_fast(skb
, off_gnv
);
433 if (skb_gro_header_hard(skb
, hlen
)) {
434 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
439 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
441 gh_len
= geneve_hlen(gh
);
443 hlen
= off_gnv
+ gh_len
;
444 if (skb_gro_header_hard(skb
, hlen
)) {
445 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
450 for (p
= *head
; p
; p
= p
->next
) {
451 if (!NAPI_GRO_CB(p
)->same_flow
)
454 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
455 if (gh
->opt_len
!= gh2
->opt_len
||
456 memcmp(gh
, gh2
, gh_len
)) {
457 NAPI_GRO_CB(p
)->same_flow
= 0;
462 type
= gh
->proto_type
;
465 ptype
= gro_find_receive_by_type(type
);
469 skb_gro_pull(skb
, gh_len
);
470 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
471 pp
= call_gro_receive(ptype
->callbacks
.gro_receive
, head
, skb
);
477 NAPI_GRO_CB(skb
)->flush
|= flush
;
482 static int geneve_gro_complete(struct sock
*sk
, struct sk_buff
*skb
,
485 struct genevehdr
*gh
;
486 struct packet_offload
*ptype
;
491 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
492 gh_len
= geneve_hlen(gh
);
493 type
= gh
->proto_type
;
496 ptype
= gro_find_complete_by_type(type
);
498 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
502 skb_set_inner_mac_header(skb
, nhoff
+ gh_len
);
507 /* Create new listen socket if needed */
508 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
509 bool ipv6
, bool ipv6_rx_csum
)
511 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
512 struct geneve_sock
*gs
;
514 struct udp_tunnel_sock_cfg tunnel_cfg
;
517 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
519 return ERR_PTR(-ENOMEM
);
521 sock
= geneve_create_sock(net
, ipv6
, port
, ipv6_rx_csum
);
524 return ERR_CAST(sock
);
529 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
530 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
532 /* Initialize the geneve udp offloads structure */
533 udp_tunnel_notify_add_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
535 /* Mark socket as an encapsulation socket */
536 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
537 tunnel_cfg
.sk_user_data
= gs
;
538 tunnel_cfg
.encap_type
= 1;
539 tunnel_cfg
.gro_receive
= geneve_gro_receive
;
540 tunnel_cfg
.gro_complete
= geneve_gro_complete
;
541 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
542 tunnel_cfg
.encap_destroy
= NULL
;
543 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
544 list_add(&gs
->list
, &gn
->sock_list
);
548 static void __geneve_sock_release(struct geneve_sock
*gs
)
550 if (!gs
|| --gs
->refcnt
)
554 udp_tunnel_notify_del_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
555 udp_tunnel_sock_release(gs
->sock
);
559 static void geneve_sock_release(struct geneve_dev
*geneve
)
561 struct geneve_sock
*gs4
= rtnl_dereference(geneve
->sock4
);
562 #if IS_ENABLED(CONFIG_IPV6)
563 struct geneve_sock
*gs6
= rtnl_dereference(geneve
->sock6
);
565 rcu_assign_pointer(geneve
->sock6
, NULL
);
568 rcu_assign_pointer(geneve
->sock4
, NULL
);
571 __geneve_sock_release(gs4
);
572 #if IS_ENABLED(CONFIG_IPV6)
573 __geneve_sock_release(gs6
);
577 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
581 struct geneve_sock
*gs
;
583 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
584 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
585 geneve_get_sk_family(gs
) == family
) {
592 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
594 struct net
*net
= geneve
->net
;
595 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
596 struct geneve_dev_node
*node
;
597 struct geneve_sock
*gs
;
601 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->info
.key
.tp_dst
);
607 gs
= geneve_socket_create(net
, geneve
->info
.key
.tp_dst
, ipv6
,
608 geneve
->use_udp6_rx_checksums
);
613 gs
->collect_md
= geneve
->collect_md
;
614 #if IS_ENABLED(CONFIG_IPV6)
616 rcu_assign_pointer(geneve
->sock6
, gs
);
617 node
= &geneve
->hlist6
;
621 rcu_assign_pointer(geneve
->sock4
, gs
);
622 node
= &geneve
->hlist4
;
624 node
->geneve
= geneve
;
626 tunnel_id_to_vni(geneve
->info
.key
.tun_id
, vni
);
627 hash
= geneve_net_vni_hash(vni
);
628 hlist_add_head_rcu(&node
->hlist
, &gs
->vni_list
[hash
]);
632 static int geneve_open(struct net_device
*dev
)
634 struct geneve_dev
*geneve
= netdev_priv(dev
);
635 bool ipv6
= !!(geneve
->info
.mode
& IP_TUNNEL_INFO_IPV6
);
636 bool metadata
= geneve
->collect_md
;
639 #if IS_ENABLED(CONFIG_IPV6)
640 if (ipv6
|| metadata
)
641 ret
= geneve_sock_add(geneve
, true);
643 if (!ret
&& (!ipv6
|| metadata
))
644 ret
= geneve_sock_add(geneve
, false);
646 geneve_sock_release(geneve
);
651 static int geneve_stop(struct net_device
*dev
)
653 struct geneve_dev
*geneve
= netdev_priv(dev
);
655 hlist_del_init_rcu(&geneve
->hlist4
.hlist
);
656 #if IS_ENABLED(CONFIG_IPV6)
657 hlist_del_init_rcu(&geneve
->hlist6
.hlist
);
659 geneve_sock_release(geneve
);
663 static void geneve_build_header(struct genevehdr
*geneveh
,
664 const struct ip_tunnel_info
*info
)
666 geneveh
->ver
= GENEVE_VER
;
667 geneveh
->opt_len
= info
->options_len
/ 4;
668 geneveh
->oam
= !!(info
->key
.tun_flags
& TUNNEL_OAM
);
669 geneveh
->critical
= !!(info
->key
.tun_flags
& TUNNEL_CRIT_OPT
);
671 tunnel_id_to_vni(info
->key
.tun_id
, geneveh
->vni
);
672 geneveh
->proto_type
= htons(ETH_P_TEB
);
675 ip_tunnel_info_opts_get(geneveh
->options
, info
);
678 static int geneve_build_skb(struct dst_entry
*dst
, struct sk_buff
*skb
,
679 const struct ip_tunnel_info
*info
,
680 bool xnet
, int ip_hdr_len
)
682 bool udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
683 struct genevehdr
*gnvh
;
687 skb_reset_mac_header(skb
);
688 skb_scrub_packet(skb
, xnet
);
690 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
+
691 GENEVE_BASE_HLEN
+ info
->options_len
+ ip_hdr_len
;
692 err
= skb_cow_head(skb
, min_headroom
);
696 err
= udp_tunnel_handle_offloads(skb
, udp_sum
);
700 gnvh
= __skb_push(skb
, sizeof(*gnvh
) + info
->options_len
);
701 geneve_build_header(gnvh
, info
);
702 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
710 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
711 struct net_device
*dev
,
712 struct geneve_sock
*gs4
,
714 const struct ip_tunnel_info
*info
)
716 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
717 struct geneve_dev
*geneve
= netdev_priv(dev
);
718 struct dst_cache
*dst_cache
;
719 struct rtable
*rt
= NULL
;
723 return ERR_PTR(-EIO
);
725 memset(fl4
, 0, sizeof(*fl4
));
726 fl4
->flowi4_mark
= skb
->mark
;
727 fl4
->flowi4_proto
= IPPROTO_UDP
;
728 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
729 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
732 if ((tos
== 1) && !geneve
->collect_md
) {
733 tos
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
736 fl4
->flowi4_tos
= RT_TOS(tos
);
738 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
740 rt
= dst_cache_get_ip4(dst_cache
, &fl4
->saddr
);
744 rt
= ip_route_output_key(geneve
->net
, fl4
);
746 netdev_dbg(dev
, "no route to %pI4\n", &fl4
->daddr
);
747 return ERR_PTR(-ENETUNREACH
);
749 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
750 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
->daddr
);
752 return ERR_PTR(-ELOOP
);
755 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
->saddr
);
759 #if IS_ENABLED(CONFIG_IPV6)
760 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
761 struct net_device
*dev
,
762 struct geneve_sock
*gs6
,
764 const struct ip_tunnel_info
*info
)
766 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
767 struct geneve_dev
*geneve
= netdev_priv(dev
);
768 struct dst_entry
*dst
= NULL
;
769 struct dst_cache
*dst_cache
;
773 return ERR_PTR(-EIO
);
775 memset(fl6
, 0, sizeof(*fl6
));
776 fl6
->flowi6_mark
= skb
->mark
;
777 fl6
->flowi6_proto
= IPPROTO_UDP
;
778 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
779 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
780 prio
= info
->key
.tos
;
781 if ((prio
== 1) && !geneve
->collect_md
) {
782 prio
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
786 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(prio
),
788 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
790 dst
= dst_cache_get_ip6(dst_cache
, &fl6
->saddr
);
794 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
795 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
796 return ERR_PTR(-ENETUNREACH
);
798 if (dst
->dev
== dev
) { /* is this necessary? */
799 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
801 return ERR_PTR(-ELOOP
);
805 dst_cache_set_ip6(dst_cache
, dst
, &fl6
->saddr
);
810 static int geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
811 struct geneve_dev
*geneve
,
812 const struct ip_tunnel_info
*info
)
814 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
815 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
816 const struct ip_tunnel_key
*key
= &info
->key
;
824 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
829 int mtu
= dst_mtu(&rt
->dst
) - sizeof(struct iphdr
) -
830 GENEVE_BASE_HLEN
- info
->options_len
- 14;
832 skb_dst_update_pmtu(skb
, mtu
);
835 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
836 if (geneve
->collect_md
) {
837 tos
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
840 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, ip_hdr(skb
), skb
);
841 ttl
= key
->ttl
? : ip4_dst_hoplimit(&rt
->dst
);
843 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
845 err
= geneve_build_skb(&rt
->dst
, skb
, info
, xnet
, sizeof(struct iphdr
));
849 udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
850 tos
, ttl
, df
, sport
, geneve
->info
.key
.tp_dst
,
851 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
852 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
856 #if IS_ENABLED(CONFIG_IPV6)
857 static int geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
858 struct geneve_dev
*geneve
,
859 const struct ip_tunnel_info
*info
)
861 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
862 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
863 const struct ip_tunnel_key
*key
= &info
->key
;
864 struct dst_entry
*dst
= NULL
;
870 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
875 int mtu
= dst_mtu(dst
) - sizeof(struct ipv6hdr
) -
876 GENEVE_BASE_HLEN
- info
->options_len
- 14;
878 skb_dst_update_pmtu(skb
, mtu
);
881 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
882 if (geneve
->collect_md
) {
883 prio
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
886 prio
= ip_tunnel_ecn_encap(ip6_tclass(fl6
.flowlabel
),
888 ttl
= key
->ttl
? : ip6_dst_hoplimit(dst
);
890 err
= geneve_build_skb(dst
, skb
, info
, xnet
, sizeof(struct ipv6hdr
));
894 udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
895 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
,
896 info
->key
.label
, sport
, geneve
->info
.key
.tp_dst
,
897 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
902 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
904 struct geneve_dev
*geneve
= netdev_priv(dev
);
905 struct ip_tunnel_info
*info
= NULL
;
908 if (geneve
->collect_md
) {
909 info
= skb_tunnel_info(skb
);
910 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
912 netdev_dbg(dev
, "no tunnel metadata\n");
916 info
= &geneve
->info
;
920 #if IS_ENABLED(CONFIG_IPV6)
921 if (info
->mode
& IP_TUNNEL_INFO_IPV6
)
922 err
= geneve6_xmit_skb(skb
, dev
, geneve
, info
);
925 err
= geneve_xmit_skb(skb
, dev
, geneve
, info
);
934 dev
->stats
.collisions
++;
935 else if (err
== -ENETUNREACH
)
936 dev
->stats
.tx_carrier_errors
++;
938 dev
->stats
.tx_errors
++;
942 static int geneve_change_mtu(struct net_device
*dev
, int new_mtu
)
944 /* Only possible if called internally, ndo_change_mtu path's new_mtu
945 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
947 if (new_mtu
> dev
->max_mtu
)
948 new_mtu
= dev
->max_mtu
;
954 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
956 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
957 struct geneve_dev
*geneve
= netdev_priv(dev
);
959 if (ip_tunnel_info_af(info
) == AF_INET
) {
962 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
964 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
969 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
970 #if IS_ENABLED(CONFIG_IPV6)
971 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
972 struct dst_entry
*dst
;
974 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
976 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
981 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
987 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
989 info
->key
.tp_dst
= geneve
->info
.key
.tp_dst
;
993 static const struct net_device_ops geneve_netdev_ops
= {
994 .ndo_init
= geneve_init
,
995 .ndo_uninit
= geneve_uninit
,
996 .ndo_open
= geneve_open
,
997 .ndo_stop
= geneve_stop
,
998 .ndo_start_xmit
= geneve_xmit
,
999 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1000 .ndo_change_mtu
= geneve_change_mtu
,
1001 .ndo_validate_addr
= eth_validate_addr
,
1002 .ndo_set_mac_address
= eth_mac_addr
,
1003 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1006 static void geneve_get_drvinfo(struct net_device
*dev
,
1007 struct ethtool_drvinfo
*drvinfo
)
1009 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1010 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1013 static const struct ethtool_ops geneve_ethtool_ops
= {
1014 .get_drvinfo
= geneve_get_drvinfo
,
1015 .get_link
= ethtool_op_get_link
,
1018 /* Info for udev, that this is a virtual tunnel endpoint */
1019 static struct device_type geneve_type
= {
1023 /* Calls the ndo_udp_tunnel_add of the caller in order to
1024 * supply the listening GENEVE udp ports. Callers are expected
1025 * to implement the ndo_udp_tunnel_add.
1027 static void geneve_offload_rx_ports(struct net_device
*dev
, bool push
)
1029 struct net
*net
= dev_net(dev
);
1030 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1031 struct geneve_sock
*gs
;
1034 list_for_each_entry_rcu(gs
, &gn
->sock_list
, list
) {
1036 udp_tunnel_push_rx_port(dev
, gs
->sock
,
1037 UDP_TUNNEL_TYPE_GENEVE
);
1039 udp_tunnel_drop_rx_port(dev
, gs
->sock
,
1040 UDP_TUNNEL_TYPE_GENEVE
);
1046 /* Initialize the device structure. */
1047 static void geneve_setup(struct net_device
*dev
)
1051 dev
->netdev_ops
= &geneve_netdev_ops
;
1052 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1053 dev
->needs_free_netdev
= true;
1055 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1057 dev
->features
|= NETIF_F_LLTX
;
1058 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1059 dev
->features
|= NETIF_F_RXCSUM
;
1060 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1062 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1063 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1065 /* MTU range: 68 - (something less than 65535) */
1066 dev
->min_mtu
= ETH_MIN_MTU
;
1067 /* The max_mtu calculation does not take account of GENEVE
1068 * options, to avoid excluding potentially valid
1069 * configurations. This will be further reduced by IPvX hdr size.
1071 dev
->max_mtu
= IP_MAX_MTU
- GENEVE_BASE_HLEN
- dev
->hard_header_len
;
1073 netif_keep_dst(dev
);
1074 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1075 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1076 eth_hw_addr_random(dev
);
1079 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1080 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1081 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1082 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1083 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1084 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1085 [IFLA_GENEVE_LABEL
] = { .type
= NLA_U32
},
1086 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1087 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1088 [IFLA_GENEVE_UDP_CSUM
] = { .type
= NLA_U8
},
1089 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
1090 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
1093 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1094 struct netlink_ext_ack
*extack
)
1096 if (tb
[IFLA_ADDRESS
]) {
1097 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1098 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1099 "Provided link layer address is not Ethernet");
1103 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1104 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1105 "Provided Ethernet address is not unicast");
1106 return -EADDRNOTAVAIL
;
1111 NL_SET_ERR_MSG(extack
,
1112 "Not enough attributes provided to perform the operation");
1116 if (data
[IFLA_GENEVE_ID
]) {
1117 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1119 if (vni
>= GENEVE_N_VID
) {
1120 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_ID
],
1121 "Geneve ID must be lower than 16777216");
1129 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1130 const struct ip_tunnel_info
*info
,
1131 bool *tun_on_same_port
,
1132 bool *tun_collect_md
)
1134 struct geneve_dev
*geneve
, *t
= NULL
;
1136 *tun_on_same_port
= false;
1137 *tun_collect_md
= false;
1138 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1139 if (info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
) {
1140 *tun_collect_md
= geneve
->collect_md
;
1141 *tun_on_same_port
= true;
1143 if (info
->key
.tun_id
== geneve
->info
.key
.tun_id
&&
1144 info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
&&
1145 !memcmp(&info
->key
.u
, &geneve
->info
.key
.u
, sizeof(info
->key
.u
)))
1151 static bool is_tnl_info_zero(const struct ip_tunnel_info
*info
)
1153 return !(info
->key
.tun_id
|| info
->key
.tun_flags
|| info
->key
.tos
||
1154 info
->key
.ttl
|| info
->key
.label
|| info
->key
.tp_src
||
1155 memchr_inv(&info
->key
.u
, 0, sizeof(info
->key
.u
)));
1158 static bool geneve_dst_addr_equal(struct ip_tunnel_info
*a
,
1159 struct ip_tunnel_info
*b
)
1161 if (ip_tunnel_info_af(a
) == AF_INET
)
1162 return a
->key
.u
.ipv4
.dst
== b
->key
.u
.ipv4
.dst
;
1164 return ipv6_addr_equal(&a
->key
.u
.ipv6
.dst
, &b
->key
.u
.ipv6
.dst
);
1167 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1168 struct netlink_ext_ack
*extack
,
1169 const struct ip_tunnel_info
*info
,
1170 bool metadata
, bool ipv6_rx_csum
)
1172 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1173 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1174 bool tun_collect_md
, tun_on_same_port
;
1177 if (metadata
&& !is_tnl_info_zero(info
)) {
1178 NL_SET_ERR_MSG(extack
,
1179 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
1186 t
= geneve_find_dev(gn
, info
, &tun_on_same_port
, &tun_collect_md
);
1190 /* make enough headroom for basic scenario */
1191 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1192 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1193 encap_len
+= sizeof(struct iphdr
);
1194 dev
->max_mtu
-= sizeof(struct iphdr
);
1196 encap_len
+= sizeof(struct ipv6hdr
);
1197 dev
->max_mtu
-= sizeof(struct ipv6hdr
);
1199 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1202 if (tun_on_same_port
) {
1203 NL_SET_ERR_MSG(extack
,
1204 "There can be only one externally controlled device on a destination port");
1208 if (tun_collect_md
) {
1209 NL_SET_ERR_MSG(extack
,
1210 "There already exists an externally controlled device on this destination port");
1215 dst_cache_reset(&geneve
->info
.dst_cache
);
1216 geneve
->info
= *info
;
1217 geneve
->collect_md
= metadata
;
1218 geneve
->use_udp6_rx_checksums
= ipv6_rx_csum
;
1220 err
= register_netdevice(dev
);
1224 list_add(&geneve
->next
, &gn
->geneve_list
);
1228 static void init_tnl_info(struct ip_tunnel_info
*info
, __u16 dst_port
)
1230 memset(info
, 0, sizeof(*info
));
1231 info
->key
.tp_dst
= htons(dst_port
);
1234 static int geneve_nl2info(struct nlattr
*tb
[], struct nlattr
*data
[],
1235 struct netlink_ext_ack
*extack
,
1236 struct ip_tunnel_info
*info
, bool *metadata
,
1237 bool *use_udp6_rx_checksums
, bool changelink
)
1241 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
]) {
1242 NL_SET_ERR_MSG(extack
,
1243 "Cannot specify both IPv4 and IPv6 Remote addresses");
1247 if (data
[IFLA_GENEVE_REMOTE
]) {
1248 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET6
)) {
1249 attrtype
= IFLA_GENEVE_REMOTE
;
1253 info
->key
.u
.ipv4
.dst
=
1254 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1256 if (IN_MULTICAST(ntohl(info
->key
.u
.ipv4
.dst
))) {
1257 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE
],
1258 "Remote IPv4 address cannot be Multicast");
1263 if (data
[IFLA_GENEVE_REMOTE6
]) {
1264 #if IS_ENABLED(CONFIG_IPV6)
1265 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET
)) {
1266 attrtype
= IFLA_GENEVE_REMOTE6
;
1270 info
->mode
= IP_TUNNEL_INFO_IPV6
;
1271 info
->key
.u
.ipv6
.dst
=
1272 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1274 if (ipv6_addr_type(&info
->key
.u
.ipv6
.dst
) &
1275 IPV6_ADDR_LINKLOCAL
) {
1276 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1277 "Remote IPv6 address cannot be link-local");
1280 if (ipv6_addr_is_multicast(&info
->key
.u
.ipv6
.dst
)) {
1281 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1282 "Remote IPv6 address cannot be Multicast");
1285 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1286 *use_udp6_rx_checksums
= true;
1288 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1289 "IPv6 support not enabled in the kernel");
1290 return -EPFNOSUPPORT
;
1294 if (data
[IFLA_GENEVE_ID
]) {
1299 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1300 tvni
[0] = (vni
& 0x00ff0000) >> 16;
1301 tvni
[1] = (vni
& 0x0000ff00) >> 8;
1302 tvni
[2] = vni
& 0x000000ff;
1304 tunid
= vni_to_tunnel_id(tvni
);
1305 if (changelink
&& (tunid
!= info
->key
.tun_id
)) {
1306 attrtype
= IFLA_GENEVE_ID
;
1309 info
->key
.tun_id
= tunid
;
1312 if (data
[IFLA_GENEVE_TTL
])
1313 info
->key
.ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1315 if (data
[IFLA_GENEVE_TOS
])
1316 info
->key
.tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1318 if (data
[IFLA_GENEVE_LABEL
]) {
1319 info
->key
.label
= nla_get_be32(data
[IFLA_GENEVE_LABEL
]) &
1320 IPV6_FLOWLABEL_MASK
;
1321 if (info
->key
.label
&& (!(info
->mode
& IP_TUNNEL_INFO_IPV6
))) {
1322 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_LABEL
],
1323 "Label attribute only applies for IPv6 Geneve devices");
1328 if (data
[IFLA_GENEVE_PORT
]) {
1330 attrtype
= IFLA_GENEVE_PORT
;
1333 info
->key
.tp_dst
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1336 if (data
[IFLA_GENEVE_COLLECT_METADATA
]) {
1338 attrtype
= IFLA_GENEVE_COLLECT_METADATA
;
1344 if (data
[IFLA_GENEVE_UDP_CSUM
]) {
1346 attrtype
= IFLA_GENEVE_UDP_CSUM
;
1349 if (nla_get_u8(data
[IFLA_GENEVE_UDP_CSUM
]))
1350 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1353 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]) {
1354 #if IS_ENABLED(CONFIG_IPV6)
1356 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_TX
;
1359 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
1360 info
->key
.tun_flags
&= ~TUNNEL_CSUM
;
1362 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
],
1363 "IPv6 support not enabled in the kernel");
1364 return -EPFNOSUPPORT
;
1368 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]) {
1369 #if IS_ENABLED(CONFIG_IPV6)
1371 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_RX
;
1374 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
1375 *use_udp6_rx_checksums
= false;
1377 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
],
1378 "IPv6 support not enabled in the kernel");
1379 return -EPFNOSUPPORT
;
1385 NL_SET_ERR_MSG_ATTR(extack
, data
[attrtype
],
1386 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1390 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1391 struct nlattr
*tb
[], struct nlattr
*data
[],
1392 struct netlink_ext_ack
*extack
)
1394 bool use_udp6_rx_checksums
= false;
1395 struct ip_tunnel_info info
;
1396 bool metadata
= false;
1399 init_tnl_info(&info
, GENEVE_UDP_PORT
);
1400 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1401 &use_udp6_rx_checksums
, false);
1405 return geneve_configure(net
, dev
, extack
, &info
, metadata
,
1406 use_udp6_rx_checksums
);
1409 /* Quiesces the geneve device data path for both TX and RX.
1411 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1412 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1413 * to complete for the existing set of in-flight packets to be transmitted,
1414 * then we would have quiesced the transmit data path. All the future packets
1415 * will get dropped until we unquiesce the data path.
1417 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1418 * if we set that to NULL under RCU and wait for synchronize_net() to
1419 * complete, then we would have quiesced the receive data path.
1421 static void geneve_quiesce(struct geneve_dev
*geneve
, struct geneve_sock
**gs4
,
1422 struct geneve_sock
**gs6
)
1424 *gs4
= rtnl_dereference(geneve
->sock4
);
1425 rcu_assign_pointer(geneve
->sock4
, NULL
);
1427 rcu_assign_sk_user_data((*gs4
)->sock
->sk
, NULL
);
1428 #if IS_ENABLED(CONFIG_IPV6)
1429 *gs6
= rtnl_dereference(geneve
->sock6
);
1430 rcu_assign_pointer(geneve
->sock6
, NULL
);
1432 rcu_assign_sk_user_data((*gs6
)->sock
->sk
, NULL
);
1439 /* Resumes the geneve device data path for both TX and RX. */
1440 static void geneve_unquiesce(struct geneve_dev
*geneve
, struct geneve_sock
*gs4
,
1441 struct geneve_sock __maybe_unused
*gs6
)
1443 rcu_assign_pointer(geneve
->sock4
, gs4
);
1445 rcu_assign_sk_user_data(gs4
->sock
->sk
, gs4
);
1446 #if IS_ENABLED(CONFIG_IPV6)
1447 rcu_assign_pointer(geneve
->sock6
, gs6
);
1449 rcu_assign_sk_user_data(gs6
->sock
->sk
, gs6
);
1454 static int geneve_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1455 struct nlattr
*data
[],
1456 struct netlink_ext_ack
*extack
)
1458 struct geneve_dev
*geneve
= netdev_priv(dev
);
1459 struct geneve_sock
*gs4
, *gs6
;
1460 struct ip_tunnel_info info
;
1462 bool use_udp6_rx_checksums
;
1465 /* If the geneve device is configured for metadata (or externally
1466 * controlled, for example, OVS), then nothing can be changed.
1468 if (geneve
->collect_md
)
1471 /* Start with the existing info. */
1472 memcpy(&info
, &geneve
->info
, sizeof(info
));
1473 metadata
= geneve
->collect_md
;
1474 use_udp6_rx_checksums
= geneve
->use_udp6_rx_checksums
;
1475 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1476 &use_udp6_rx_checksums
, true);
1480 if (!geneve_dst_addr_equal(&geneve
->info
, &info
))
1481 dst_cache_reset(&info
.dst_cache
);
1483 geneve_quiesce(geneve
, &gs4
, &gs6
);
1484 geneve
->info
= info
;
1485 geneve
->collect_md
= metadata
;
1486 geneve
->use_udp6_rx_checksums
= use_udp6_rx_checksums
;
1487 geneve_unquiesce(geneve
, gs4
, gs6
);
1492 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1494 struct geneve_dev
*geneve
= netdev_priv(dev
);
1496 list_del(&geneve
->next
);
1497 unregister_netdevice_queue(dev
, head
);
1500 static size_t geneve_get_size(const struct net_device
*dev
)
1502 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1503 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1504 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1505 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1506 nla_total_size(sizeof(__be32
)) + /* IFLA_GENEVE_LABEL */
1507 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1508 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1509 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_CSUM */
1510 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1511 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1515 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1517 struct geneve_dev
*geneve
= netdev_priv(dev
);
1518 struct ip_tunnel_info
*info
= &geneve
->info
;
1519 bool metadata
= geneve
->collect_md
;
1523 tunnel_id_to_vni(info
->key
.tun_id
, tmp_vni
);
1524 vni
= (tmp_vni
[0] << 16) | (tmp_vni
[1] << 8) | tmp_vni
[2];
1525 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1526 goto nla_put_failure
;
1528 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1529 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1530 info
->key
.u
.ipv4
.dst
))
1531 goto nla_put_failure
;
1532 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_CSUM
,
1533 !!(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1534 goto nla_put_failure
;
1536 #if IS_ENABLED(CONFIG_IPV6)
1537 } else if (!metadata
) {
1538 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1539 &info
->key
.u
.ipv6
.dst
))
1540 goto nla_put_failure
;
1541 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
1542 !(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1543 goto nla_put_failure
;
1547 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, info
->key
.ttl
) ||
1548 nla_put_u8(skb
, IFLA_GENEVE_TOS
, info
->key
.tos
) ||
1549 nla_put_be32(skb
, IFLA_GENEVE_LABEL
, info
->key
.label
))
1550 goto nla_put_failure
;
1552 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, info
->key
.tp_dst
))
1553 goto nla_put_failure
;
1555 if (metadata
&& nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1556 goto nla_put_failure
;
1558 #if IS_ENABLED(CONFIG_IPV6)
1559 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
1560 !geneve
->use_udp6_rx_checksums
))
1561 goto nla_put_failure
;
1570 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1572 .maxtype
= IFLA_GENEVE_MAX
,
1573 .policy
= geneve_policy
,
1574 .priv_size
= sizeof(struct geneve_dev
),
1575 .setup
= geneve_setup
,
1576 .validate
= geneve_validate
,
1577 .newlink
= geneve_newlink
,
1578 .changelink
= geneve_changelink
,
1579 .dellink
= geneve_dellink
,
1580 .get_size
= geneve_get_size
,
1581 .fill_info
= geneve_fill_info
,
1584 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1585 u8 name_assign_type
, u16 dst_port
)
1587 struct nlattr
*tb
[IFLA_MAX
+ 1];
1588 struct ip_tunnel_info info
;
1589 struct net_device
*dev
;
1590 LIST_HEAD(list_kill
);
1593 memset(tb
, 0, sizeof(tb
));
1594 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1595 &geneve_link_ops
, tb
);
1599 init_tnl_info(&info
, dst_port
);
1600 err
= geneve_configure(net
, dev
, NULL
, &info
, true, true);
1603 return ERR_PTR(err
);
1606 /* openvswitch users expect packet sizes to be unrestricted,
1607 * so set the largest MTU we can.
1609 err
= geneve_change_mtu(dev
, IP_MAX_MTU
);
1613 err
= rtnl_configure_link(dev
, NULL
);
1619 geneve_dellink(dev
, &list_kill
);
1620 unregister_netdevice_many(&list_kill
);
1621 return ERR_PTR(err
);
1623 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1625 static int geneve_netdevice_event(struct notifier_block
*unused
,
1626 unsigned long event
, void *ptr
)
1628 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1630 if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
||
1631 event
== NETDEV_UDP_TUNNEL_DROP_INFO
) {
1632 geneve_offload_rx_ports(dev
, event
== NETDEV_UDP_TUNNEL_PUSH_INFO
);
1633 } else if (event
== NETDEV_UNREGISTER
) {
1634 geneve_offload_rx_ports(dev
, false);
1635 } else if (event
== NETDEV_REGISTER
) {
1636 geneve_offload_rx_ports(dev
, true);
1642 static struct notifier_block geneve_notifier_block __read_mostly
= {
1643 .notifier_call
= geneve_netdevice_event
,
1646 static __net_init
int geneve_init_net(struct net
*net
)
1648 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1650 INIT_LIST_HEAD(&gn
->geneve_list
);
1651 INIT_LIST_HEAD(&gn
->sock_list
);
1655 static void geneve_destroy_tunnels(struct net
*net
, struct list_head
*head
)
1657 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1658 struct geneve_dev
*geneve
, *next
;
1659 struct net_device
*dev
, *aux
;
1661 /* gather any geneve devices that were moved into this ns */
1662 for_each_netdev_safe(net
, dev
, aux
)
1663 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1664 unregister_netdevice_queue(dev
, head
);
1666 /* now gather any other geneve devices that were created in this ns */
1667 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1668 /* If geneve->dev is in the same netns, it was already added
1669 * to the list by the previous loop.
1671 if (!net_eq(dev_net(geneve
->dev
), net
))
1672 unregister_netdevice_queue(geneve
->dev
, head
);
1675 WARN_ON_ONCE(!list_empty(&gn
->sock_list
));
1678 static void __net_exit
geneve_exit_batch_net(struct list_head
*net_list
)
1684 list_for_each_entry(net
, net_list
, exit_list
)
1685 geneve_destroy_tunnels(net
, &list
);
1687 /* unregister the devices gathered above */
1688 unregister_netdevice_many(&list
);
1692 static struct pernet_operations geneve_net_ops
= {
1693 .init
= geneve_init_net
,
1694 .exit_batch
= geneve_exit_batch_net
,
1695 .id
= &geneve_net_id
,
1696 .size
= sizeof(struct geneve_net
),
1699 static int __init
geneve_init_module(void)
1703 rc
= register_pernet_subsys(&geneve_net_ops
);
1707 rc
= register_netdevice_notifier(&geneve_notifier_block
);
1711 rc
= rtnl_link_register(&geneve_link_ops
);
1717 unregister_netdevice_notifier(&geneve_notifier_block
);
1719 unregister_pernet_subsys(&geneve_net_ops
);
1723 late_initcall(geneve_init_module
);
1725 static void __exit
geneve_cleanup_module(void)
1727 rtnl_link_unregister(&geneve_link_ops
);
1728 unregister_netdevice_notifier(&geneve_notifier_block
);
1729 unregister_pernet_subsys(&geneve_net_ops
);
1731 module_exit(geneve_cleanup_module
);
1733 MODULE_LICENSE("GPL");
1734 MODULE_VERSION(GENEVE_NETDEV_VER
);
1735 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1736 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1737 MODULE_ALIAS_RTNL_LINK("geneve");