1 // SPDX-License-Identifier: GPL-2.0-only
3 * GENEVE: Generic Network Virtualization Encapsulation
5 * Copyright (c) 2015 Red Hat, Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/hash.h>
14 #include <net/ipv6_stubs.h>
15 #include <net/dst_metadata.h>
16 #include <net/gro_cells.h>
17 #include <net/rtnetlink.h>
18 #include <net/geneve.h>
19 #include <net/protocol.h>
21 #define GENEVE_NETDEV_VER "0.6"
23 #define GENEVE_N_VID (1u << 24)
24 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
26 #define VNI_HASH_BITS 10
27 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
29 static bool log_ecn_error
= true;
30 module_param(log_ecn_error
, bool, 0644);
31 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
34 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
35 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
36 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
38 /* per-network namespace private data for this module */
40 struct list_head geneve_list
;
41 struct list_head sock_list
;
44 static unsigned int geneve_net_id
;
46 struct geneve_dev_node
{
47 struct hlist_node hlist
;
48 struct geneve_dev
*geneve
;
51 /* Pseudo network device */
53 struct geneve_dev_node hlist4
; /* vni hash table for IPv4 socket */
54 #if IS_ENABLED(CONFIG_IPV6)
55 struct geneve_dev_node hlist6
; /* vni hash table for IPv6 socket */
57 struct net
*net
; /* netns for packet i/o */
58 struct net_device
*dev
; /* netdev for geneve tunnel */
59 struct ip_tunnel_info info
;
60 struct geneve_sock __rcu
*sock4
; /* IPv4 socket used for geneve tunnel */
61 #if IS_ENABLED(CONFIG_IPV6)
62 struct geneve_sock __rcu
*sock6
; /* IPv6 socket used for geneve tunnel */
64 struct list_head next
; /* geneve's per namespace list */
65 struct gro_cells gro_cells
;
67 bool use_udp6_rx_checksums
;
69 enum ifla_geneve_df df
;
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,
240 /* Drop packets w/ critical options,
241 * since we don't support any...
243 if (gnvh
->critical
) {
244 geneve
->dev
->stats
.rx_frame_errors
++;
245 geneve
->dev
->stats
.rx_errors
++;
250 skb_reset_mac_header(skb
);
251 skb
->protocol
= eth_type_trans(skb
, geneve
->dev
);
252 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
255 skb_dst_set(skb
, &tun_dst
->dst
);
257 /* Ignore packet loops (and multicast echo) */
258 if (ether_addr_equal(eth_hdr(skb
)->h_source
, geneve
->dev
->dev_addr
)) {
259 geneve
->dev
->stats
.rx_errors
++;
263 oiph
= skb_network_header(skb
);
264 skb_reset_network_header(skb
);
266 if (geneve_get_sk_family(gs
) == AF_INET
)
267 err
= IP_ECN_decapsulate(oiph
, skb
);
268 #if IS_ENABLED(CONFIG_IPV6)
270 err
= IP6_ECN_decapsulate(oiph
, skb
);
275 if (geneve_get_sk_family(gs
) == AF_INET
)
276 net_info_ratelimited("non-ECT from %pI4 "
278 &((struct iphdr
*)oiph
)->saddr
,
279 ((struct iphdr
*)oiph
)->tos
);
280 #if IS_ENABLED(CONFIG_IPV6)
282 net_info_ratelimited("non-ECT from %pI6\n",
283 &((struct ipv6hdr
*)oiph
)->saddr
);
287 ++geneve
->dev
->stats
.rx_frame_errors
;
288 ++geneve
->dev
->stats
.rx_errors
;
294 err
= gro_cells_receive(&geneve
->gro_cells
, skb
);
295 if (likely(err
== NET_RX_SUCCESS
)) {
296 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
297 u64_stats_update_begin(&stats
->syncp
);
299 stats
->rx_bytes
+= len
;
300 u64_stats_update_end(&stats
->syncp
);
304 /* Consume bad packet */
308 /* Setup stats when device is created */
309 static int geneve_init(struct net_device
*dev
)
311 struct geneve_dev
*geneve
= netdev_priv(dev
);
314 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
318 err
= gro_cells_init(&geneve
->gro_cells
, dev
);
320 free_percpu(dev
->tstats
);
324 err
= dst_cache_init(&geneve
->info
.dst_cache
, GFP_KERNEL
);
326 free_percpu(dev
->tstats
);
327 gro_cells_destroy(&geneve
->gro_cells
);
333 static void geneve_uninit(struct net_device
*dev
)
335 struct geneve_dev
*geneve
= netdev_priv(dev
);
337 dst_cache_destroy(&geneve
->info
.dst_cache
);
338 gro_cells_destroy(&geneve
->gro_cells
);
339 free_percpu(dev
->tstats
);
342 /* Callback from net/ipv4/udp.c to receive packets */
343 static int geneve_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
345 struct genevehdr
*geneveh
;
346 struct geneve_dev
*geneve
;
347 struct geneve_sock
*gs
;
350 /* Need UDP and Geneve header to be present */
351 if (unlikely(!pskb_may_pull(skb
, GENEVE_BASE_HLEN
)))
354 /* Return packets with reserved bits set */
355 geneveh
= geneve_hdr(skb
);
356 if (unlikely(geneveh
->ver
!= GENEVE_VER
))
359 if (unlikely(geneveh
->proto_type
!= htons(ETH_P_TEB
)))
362 gs
= rcu_dereference_sk_user_data(sk
);
366 geneve
= geneve_lookup_skb(gs
, skb
);
370 opts_len
= geneveh
->opt_len
* 4;
371 if (iptunnel_pull_header(skb
, GENEVE_BASE_HLEN
+ opts_len
,
373 !net_eq(geneve
->net
, dev_net(geneve
->dev
)))) {
374 geneve
->dev
->stats
.rx_dropped
++;
378 geneve_rx(geneve
, gs
, skb
);
382 /* Consume bad packet */
387 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
388 static int geneve_udp_encap_err_lookup(struct sock
*sk
, struct sk_buff
*skb
)
390 struct genevehdr
*geneveh
;
391 struct geneve_sock
*gs
;
392 u8 zero_vni
[3] = { 0 };
395 if (!pskb_may_pull(skb
, skb_transport_offset(skb
) + GENEVE_BASE_HLEN
))
398 geneveh
= geneve_hdr(skb
);
399 if (geneveh
->ver
!= GENEVE_VER
)
402 if (geneveh
->proto_type
!= htons(ETH_P_TEB
))
405 gs
= rcu_dereference_sk_user_data(sk
);
409 if (geneve_get_sk_family(gs
) == AF_INET
) {
410 struct iphdr
*iph
= ip_hdr(skb
);
413 if (!gs
->collect_md
) {
414 vni
= geneve_hdr(skb
)->vni
;
418 return geneve_lookup(gs
, addr4
, vni
) ? 0 : -ENOENT
;
421 #if IS_ENABLED(CONFIG_IPV6)
422 if (geneve_get_sk_family(gs
) == AF_INET6
) {
423 struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
424 struct in6_addr addr6
;
426 memset(&addr6
, 0, sizeof(struct in6_addr
));
428 if (!gs
->collect_md
) {
429 vni
= geneve_hdr(skb
)->vni
;
433 return geneve6_lookup(gs
, addr6
, vni
) ? 0 : -ENOENT
;
437 return -EPFNOSUPPORT
;
440 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
441 __be16 port
, bool ipv6_rx_csum
)
444 struct udp_port_cfg udp_conf
;
447 memset(&udp_conf
, 0, sizeof(udp_conf
));
450 udp_conf
.family
= AF_INET6
;
451 udp_conf
.ipv6_v6only
= 1;
452 udp_conf
.use_udp6_rx_checksums
= ipv6_rx_csum
;
454 udp_conf
.family
= AF_INET
;
455 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
458 udp_conf
.local_udp_port
= port
;
460 /* Open UDP socket */
461 err
= udp_sock_create(net
, &udp_conf
, &sock
);
468 static int geneve_hlen(struct genevehdr
*gh
)
470 return sizeof(*gh
) + gh
->opt_len
* 4;
473 static struct sk_buff
*geneve_gro_receive(struct sock
*sk
,
474 struct list_head
*head
,
477 struct sk_buff
*pp
= NULL
;
479 struct genevehdr
*gh
, *gh2
;
480 unsigned int hlen
, gh_len
, off_gnv
;
481 const struct packet_offload
*ptype
;
485 off_gnv
= skb_gro_offset(skb
);
486 hlen
= off_gnv
+ sizeof(*gh
);
487 gh
= skb_gro_header_fast(skb
, off_gnv
);
488 if (skb_gro_header_hard(skb
, hlen
)) {
489 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
494 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
496 gh_len
= geneve_hlen(gh
);
498 hlen
= off_gnv
+ gh_len
;
499 if (skb_gro_header_hard(skb
, hlen
)) {
500 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
505 list_for_each_entry(p
, head
, list
) {
506 if (!NAPI_GRO_CB(p
)->same_flow
)
509 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
510 if (gh
->opt_len
!= gh2
->opt_len
||
511 memcmp(gh
, gh2
, gh_len
)) {
512 NAPI_GRO_CB(p
)->same_flow
= 0;
517 type
= gh
->proto_type
;
520 ptype
= gro_find_receive_by_type(type
);
524 skb_gro_pull(skb
, gh_len
);
525 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
526 pp
= call_gro_receive(ptype
->callbacks
.gro_receive
, head
, skb
);
532 skb_gro_flush_final(skb
, pp
, flush
);
537 static int geneve_gro_complete(struct sock
*sk
, struct sk_buff
*skb
,
540 struct genevehdr
*gh
;
541 struct packet_offload
*ptype
;
546 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
547 gh_len
= geneve_hlen(gh
);
548 type
= gh
->proto_type
;
551 ptype
= gro_find_complete_by_type(type
);
553 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
557 skb_set_inner_mac_header(skb
, nhoff
+ gh_len
);
562 /* Create new listen socket if needed */
563 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
564 bool ipv6
, bool ipv6_rx_csum
)
566 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
567 struct geneve_sock
*gs
;
569 struct udp_tunnel_sock_cfg tunnel_cfg
;
572 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
574 return ERR_PTR(-ENOMEM
);
576 sock
= geneve_create_sock(net
, ipv6
, port
, ipv6_rx_csum
);
579 return ERR_CAST(sock
);
584 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
585 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
587 /* Initialize the geneve udp offloads structure */
588 udp_tunnel_notify_add_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
590 /* Mark socket as an encapsulation socket */
591 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
592 tunnel_cfg
.sk_user_data
= gs
;
593 tunnel_cfg
.encap_type
= 1;
594 tunnel_cfg
.gro_receive
= geneve_gro_receive
;
595 tunnel_cfg
.gro_complete
= geneve_gro_complete
;
596 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
597 tunnel_cfg
.encap_err_lookup
= geneve_udp_encap_err_lookup
;
598 tunnel_cfg
.encap_destroy
= NULL
;
599 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
600 list_add(&gs
->list
, &gn
->sock_list
);
604 static void __geneve_sock_release(struct geneve_sock
*gs
)
606 if (!gs
|| --gs
->refcnt
)
610 udp_tunnel_notify_del_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
611 udp_tunnel_sock_release(gs
->sock
);
615 static void geneve_sock_release(struct geneve_dev
*geneve
)
617 struct geneve_sock
*gs4
= rtnl_dereference(geneve
->sock4
);
618 #if IS_ENABLED(CONFIG_IPV6)
619 struct geneve_sock
*gs6
= rtnl_dereference(geneve
->sock6
);
621 rcu_assign_pointer(geneve
->sock6
, NULL
);
624 rcu_assign_pointer(geneve
->sock4
, NULL
);
627 __geneve_sock_release(gs4
);
628 #if IS_ENABLED(CONFIG_IPV6)
629 __geneve_sock_release(gs6
);
633 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
637 struct geneve_sock
*gs
;
639 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
640 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
641 geneve_get_sk_family(gs
) == family
) {
648 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
650 struct net
*net
= geneve
->net
;
651 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
652 struct geneve_dev_node
*node
;
653 struct geneve_sock
*gs
;
657 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->info
.key
.tp_dst
);
663 gs
= geneve_socket_create(net
, geneve
->info
.key
.tp_dst
, ipv6
,
664 geneve
->use_udp6_rx_checksums
);
669 gs
->collect_md
= geneve
->collect_md
;
670 #if IS_ENABLED(CONFIG_IPV6)
672 rcu_assign_pointer(geneve
->sock6
, gs
);
673 node
= &geneve
->hlist6
;
677 rcu_assign_pointer(geneve
->sock4
, gs
);
678 node
= &geneve
->hlist4
;
680 node
->geneve
= geneve
;
682 tunnel_id_to_vni(geneve
->info
.key
.tun_id
, vni
);
683 hash
= geneve_net_vni_hash(vni
);
684 hlist_add_head_rcu(&node
->hlist
, &gs
->vni_list
[hash
]);
688 static int geneve_open(struct net_device
*dev
)
690 struct geneve_dev
*geneve
= netdev_priv(dev
);
691 bool metadata
= geneve
->collect_md
;
695 ipv6
= geneve
->info
.mode
& IP_TUNNEL_INFO_IPV6
|| metadata
;
696 ipv4
= !ipv6
|| metadata
;
697 #if IS_ENABLED(CONFIG_IPV6)
699 ret
= geneve_sock_add(geneve
, true);
700 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
705 ret
= geneve_sock_add(geneve
, false);
707 geneve_sock_release(geneve
);
712 static int geneve_stop(struct net_device
*dev
)
714 struct geneve_dev
*geneve
= netdev_priv(dev
);
716 hlist_del_init_rcu(&geneve
->hlist4
.hlist
);
717 #if IS_ENABLED(CONFIG_IPV6)
718 hlist_del_init_rcu(&geneve
->hlist6
.hlist
);
720 geneve_sock_release(geneve
);
724 static void geneve_build_header(struct genevehdr
*geneveh
,
725 const struct ip_tunnel_info
*info
)
727 geneveh
->ver
= GENEVE_VER
;
728 geneveh
->opt_len
= info
->options_len
/ 4;
729 geneveh
->oam
= !!(info
->key
.tun_flags
& TUNNEL_OAM
);
730 geneveh
->critical
= !!(info
->key
.tun_flags
& TUNNEL_CRIT_OPT
);
732 tunnel_id_to_vni(info
->key
.tun_id
, geneveh
->vni
);
733 geneveh
->proto_type
= htons(ETH_P_TEB
);
736 if (info
->key
.tun_flags
& TUNNEL_GENEVE_OPT
)
737 ip_tunnel_info_opts_get(geneveh
->options
, info
);
740 static int geneve_build_skb(struct dst_entry
*dst
, struct sk_buff
*skb
,
741 const struct ip_tunnel_info
*info
,
742 bool xnet
, int ip_hdr_len
)
744 bool udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
745 struct genevehdr
*gnvh
;
749 skb_reset_mac_header(skb
);
750 skb_scrub_packet(skb
, xnet
);
752 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
+
753 GENEVE_BASE_HLEN
+ info
->options_len
+ ip_hdr_len
;
754 err
= skb_cow_head(skb
, min_headroom
);
758 err
= udp_tunnel_handle_offloads(skb
, udp_sum
);
762 gnvh
= __skb_push(skb
, sizeof(*gnvh
) + info
->options_len
);
763 geneve_build_header(gnvh
, info
);
764 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
772 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
773 struct net_device
*dev
,
774 struct geneve_sock
*gs4
,
776 const struct ip_tunnel_info
*info
)
778 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
779 struct geneve_dev
*geneve
= netdev_priv(dev
);
780 struct dst_cache
*dst_cache
;
781 struct rtable
*rt
= NULL
;
785 return ERR_PTR(-EIO
);
787 memset(fl4
, 0, sizeof(*fl4
));
788 fl4
->flowi4_mark
= skb
->mark
;
789 fl4
->flowi4_proto
= IPPROTO_UDP
;
790 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
791 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
794 if ((tos
== 1) && !geneve
->collect_md
) {
795 tos
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
798 fl4
->flowi4_tos
= RT_TOS(tos
);
800 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
802 rt
= dst_cache_get_ip4(dst_cache
, &fl4
->saddr
);
806 rt
= ip_route_output_key(geneve
->net
, fl4
);
808 netdev_dbg(dev
, "no route to %pI4\n", &fl4
->daddr
);
809 return ERR_PTR(-ENETUNREACH
);
811 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
812 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
->daddr
);
814 return ERR_PTR(-ELOOP
);
817 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
->saddr
);
821 #if IS_ENABLED(CONFIG_IPV6)
822 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
823 struct net_device
*dev
,
824 struct geneve_sock
*gs6
,
826 const struct ip_tunnel_info
*info
)
828 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
829 struct geneve_dev
*geneve
= netdev_priv(dev
);
830 struct dst_entry
*dst
= NULL
;
831 struct dst_cache
*dst_cache
;
835 return ERR_PTR(-EIO
);
837 memset(fl6
, 0, sizeof(*fl6
));
838 fl6
->flowi6_mark
= skb
->mark
;
839 fl6
->flowi6_proto
= IPPROTO_UDP
;
840 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
841 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
842 prio
= info
->key
.tos
;
843 if ((prio
== 1) && !geneve
->collect_md
) {
844 prio
= ip_tunnel_get_dsfield(ip_hdr(skb
), skb
);
848 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(prio
),
850 dst_cache
= (struct dst_cache
*)&info
->dst_cache
;
852 dst
= dst_cache_get_ip6(dst_cache
, &fl6
->saddr
);
856 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
857 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
858 return ERR_PTR(-ENETUNREACH
);
860 if (dst
->dev
== dev
) { /* is this necessary? */
861 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
863 return ERR_PTR(-ELOOP
);
867 dst_cache_set_ip6(dst_cache
, dst
, &fl6
->saddr
);
872 static int geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
873 struct geneve_dev
*geneve
,
874 const struct ip_tunnel_info
*info
)
876 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
877 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
878 const struct ip_tunnel_key
*key
= &info
->key
;
886 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
890 skb_tunnel_check_pmtu(skb
, &rt
->dst
,
891 GENEVE_IPV4_HLEN
+ info
->options_len
);
893 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
894 if (geneve
->collect_md
) {
895 tos
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
898 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
900 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, ip_hdr(skb
), skb
);
901 if (geneve
->ttl_inherit
)
902 ttl
= ip_tunnel_get_ttl(ip_hdr(skb
), skb
);
905 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
907 if (geneve
->df
== GENEVE_DF_SET
) {
909 } else if (geneve
->df
== GENEVE_DF_INHERIT
) {
910 struct ethhdr
*eth
= eth_hdr(skb
);
912 if (ntohs(eth
->h_proto
) == ETH_P_IPV6
) {
914 } else if (ntohs(eth
->h_proto
) == ETH_P_IP
) {
915 struct iphdr
*iph
= ip_hdr(skb
);
917 if (iph
->frag_off
& htons(IP_DF
))
923 err
= geneve_build_skb(&rt
->dst
, skb
, info
, xnet
, sizeof(struct iphdr
));
927 udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
928 tos
, ttl
, df
, sport
, geneve
->info
.key
.tp_dst
,
929 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
930 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
934 #if IS_ENABLED(CONFIG_IPV6)
935 static int geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
936 struct geneve_dev
*geneve
,
937 const struct ip_tunnel_info
*info
)
939 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
940 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
941 const struct ip_tunnel_key
*key
= &info
->key
;
942 struct dst_entry
*dst
= NULL
;
948 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
952 skb_tunnel_check_pmtu(skb
, dst
, GENEVE_IPV6_HLEN
+ info
->options_len
);
954 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
955 if (geneve
->collect_md
) {
956 prio
= ip_tunnel_ecn_encap(key
->tos
, ip_hdr(skb
), skb
);
959 prio
= ip_tunnel_ecn_encap(ip6_tclass(fl6
.flowlabel
),
961 if (geneve
->ttl_inherit
)
962 ttl
= ip_tunnel_get_ttl(ip_hdr(skb
), skb
);
965 ttl
= ttl
? : ip6_dst_hoplimit(dst
);
967 err
= geneve_build_skb(dst
, skb
, info
, xnet
, sizeof(struct ipv6hdr
));
971 udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
972 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
,
973 info
->key
.label
, sport
, geneve
->info
.key
.tp_dst
,
974 !(info
->key
.tun_flags
& TUNNEL_CSUM
));
979 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
981 struct geneve_dev
*geneve
= netdev_priv(dev
);
982 struct ip_tunnel_info
*info
= NULL
;
985 if (geneve
->collect_md
) {
986 info
= skb_tunnel_info(skb
);
987 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
989 netdev_dbg(dev
, "no tunnel metadata\n");
993 info
= &geneve
->info
;
997 #if IS_ENABLED(CONFIG_IPV6)
998 if (info
->mode
& IP_TUNNEL_INFO_IPV6
)
999 err
= geneve6_xmit_skb(skb
, dev
, geneve
, info
);
1002 err
= geneve_xmit_skb(skb
, dev
, geneve
, info
);
1006 return NETDEV_TX_OK
;
1011 dev
->stats
.collisions
++;
1012 else if (err
== -ENETUNREACH
)
1013 dev
->stats
.tx_carrier_errors
++;
1015 dev
->stats
.tx_errors
++;
1016 return NETDEV_TX_OK
;
1019 static int geneve_change_mtu(struct net_device
*dev
, int new_mtu
)
1021 if (new_mtu
> dev
->max_mtu
)
1022 new_mtu
= dev
->max_mtu
;
1023 else if (new_mtu
< dev
->min_mtu
)
1024 new_mtu
= dev
->min_mtu
;
1030 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
1032 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
1033 struct geneve_dev
*geneve
= netdev_priv(dev
);
1035 if (ip_tunnel_info_af(info
) == AF_INET
) {
1038 struct geneve_sock
*gs4
= rcu_dereference(geneve
->sock4
);
1040 rt
= geneve_get_v4_rt(skb
, dev
, gs4
, &fl4
, info
);
1045 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
1046 #if IS_ENABLED(CONFIG_IPV6)
1047 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
1048 struct dst_entry
*dst
;
1050 struct geneve_sock
*gs6
= rcu_dereference(geneve
->sock6
);
1052 dst
= geneve_get_v6_dst(skb
, dev
, gs6
, &fl6
, info
);
1054 return PTR_ERR(dst
);
1057 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
1063 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
1064 1, USHRT_MAX
, true);
1065 info
->key
.tp_dst
= geneve
->info
.key
.tp_dst
;
1069 static const struct net_device_ops geneve_netdev_ops
= {
1070 .ndo_init
= geneve_init
,
1071 .ndo_uninit
= geneve_uninit
,
1072 .ndo_open
= geneve_open
,
1073 .ndo_stop
= geneve_stop
,
1074 .ndo_start_xmit
= geneve_xmit
,
1075 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1076 .ndo_change_mtu
= geneve_change_mtu
,
1077 .ndo_validate_addr
= eth_validate_addr
,
1078 .ndo_set_mac_address
= eth_mac_addr
,
1079 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1082 static void geneve_get_drvinfo(struct net_device
*dev
,
1083 struct ethtool_drvinfo
*drvinfo
)
1085 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1086 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1089 static const struct ethtool_ops geneve_ethtool_ops
= {
1090 .get_drvinfo
= geneve_get_drvinfo
,
1091 .get_link
= ethtool_op_get_link
,
1094 /* Info for udev, that this is a virtual tunnel endpoint */
1095 static struct device_type geneve_type
= {
1099 /* Calls the ndo_udp_tunnel_add of the caller in order to
1100 * supply the listening GENEVE udp ports. Callers are expected
1101 * to implement the ndo_udp_tunnel_add.
1103 static void geneve_offload_rx_ports(struct net_device
*dev
, bool push
)
1105 struct net
*net
= dev_net(dev
);
1106 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1107 struct geneve_sock
*gs
;
1110 list_for_each_entry_rcu(gs
, &gn
->sock_list
, list
) {
1112 udp_tunnel_push_rx_port(dev
, gs
->sock
,
1113 UDP_TUNNEL_TYPE_GENEVE
);
1115 udp_tunnel_drop_rx_port(dev
, gs
->sock
,
1116 UDP_TUNNEL_TYPE_GENEVE
);
1122 /* Initialize the device structure. */
1123 static void geneve_setup(struct net_device
*dev
)
1127 dev
->netdev_ops
= &geneve_netdev_ops
;
1128 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1129 dev
->needs_free_netdev
= true;
1131 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1133 dev
->features
|= NETIF_F_LLTX
;
1134 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1135 dev
->features
|= NETIF_F_RXCSUM
;
1136 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1138 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1139 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1141 /* MTU range: 68 - (something less than 65535) */
1142 dev
->min_mtu
= ETH_MIN_MTU
;
1143 /* The max_mtu calculation does not take account of GENEVE
1144 * options, to avoid excluding potentially valid
1145 * configurations. This will be further reduced by IPvX hdr size.
1147 dev
->max_mtu
= IP_MAX_MTU
- GENEVE_BASE_HLEN
- dev
->hard_header_len
;
1149 netif_keep_dst(dev
);
1150 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1151 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1152 eth_hw_addr_random(dev
);
1155 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1156 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1157 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1158 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1159 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1160 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1161 [IFLA_GENEVE_LABEL
] = { .type
= NLA_U32
},
1162 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1163 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1164 [IFLA_GENEVE_UDP_CSUM
] = { .type
= NLA_U8
},
1165 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
1166 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
1167 [IFLA_GENEVE_TTL_INHERIT
] = { .type
= NLA_U8
},
1168 [IFLA_GENEVE_DF
] = { .type
= NLA_U8
},
1171 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1172 struct netlink_ext_ack
*extack
)
1174 if (tb
[IFLA_ADDRESS
]) {
1175 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1176 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1177 "Provided link layer address is not Ethernet");
1181 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1182 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_ADDRESS
],
1183 "Provided Ethernet address is not unicast");
1184 return -EADDRNOTAVAIL
;
1189 NL_SET_ERR_MSG(extack
,
1190 "Not enough attributes provided to perform the operation");
1194 if (data
[IFLA_GENEVE_ID
]) {
1195 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1197 if (vni
>= GENEVE_N_VID
) {
1198 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_ID
],
1199 "Geneve ID must be lower than 16777216");
1204 if (data
[IFLA_GENEVE_DF
]) {
1205 enum ifla_geneve_df df
= nla_get_u8(data
[IFLA_GENEVE_DF
]);
1207 if (df
< 0 || df
> GENEVE_DF_MAX
) {
1208 NL_SET_ERR_MSG_ATTR(extack
, tb
[IFLA_GENEVE_DF
],
1209 "Invalid DF attribute");
1217 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1218 const struct ip_tunnel_info
*info
,
1219 bool *tun_on_same_port
,
1220 bool *tun_collect_md
)
1222 struct geneve_dev
*geneve
, *t
= NULL
;
1224 *tun_on_same_port
= false;
1225 *tun_collect_md
= false;
1226 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1227 if (info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
) {
1228 *tun_collect_md
= geneve
->collect_md
;
1229 *tun_on_same_port
= true;
1231 if (info
->key
.tun_id
== geneve
->info
.key
.tun_id
&&
1232 info
->key
.tp_dst
== geneve
->info
.key
.tp_dst
&&
1233 !memcmp(&info
->key
.u
, &geneve
->info
.key
.u
, sizeof(info
->key
.u
)))
1239 static bool is_tnl_info_zero(const struct ip_tunnel_info
*info
)
1241 return !(info
->key
.tun_id
|| info
->key
.tun_flags
|| info
->key
.tos
||
1242 info
->key
.ttl
|| info
->key
.label
|| info
->key
.tp_src
||
1243 memchr_inv(&info
->key
.u
, 0, sizeof(info
->key
.u
)));
1246 static bool geneve_dst_addr_equal(struct ip_tunnel_info
*a
,
1247 struct ip_tunnel_info
*b
)
1249 if (ip_tunnel_info_af(a
) == AF_INET
)
1250 return a
->key
.u
.ipv4
.dst
== b
->key
.u
.ipv4
.dst
;
1252 return ipv6_addr_equal(&a
->key
.u
.ipv6
.dst
, &b
->key
.u
.ipv6
.dst
);
1255 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1256 struct netlink_ext_ack
*extack
,
1257 const struct ip_tunnel_info
*info
,
1258 bool metadata
, bool ipv6_rx_csum
,
1259 bool ttl_inherit
, enum ifla_geneve_df df
)
1261 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1262 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1263 bool tun_collect_md
, tun_on_same_port
;
1266 if (metadata
&& !is_tnl_info_zero(info
)) {
1267 NL_SET_ERR_MSG(extack
,
1268 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
1275 t
= geneve_find_dev(gn
, info
, &tun_on_same_port
, &tun_collect_md
);
1279 /* make enough headroom for basic scenario */
1280 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1281 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1282 encap_len
+= sizeof(struct iphdr
);
1283 dev
->max_mtu
-= sizeof(struct iphdr
);
1285 encap_len
+= sizeof(struct ipv6hdr
);
1286 dev
->max_mtu
-= sizeof(struct ipv6hdr
);
1288 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1291 if (tun_on_same_port
) {
1292 NL_SET_ERR_MSG(extack
,
1293 "There can be only one externally controlled device on a destination port");
1297 if (tun_collect_md
) {
1298 NL_SET_ERR_MSG(extack
,
1299 "There already exists an externally controlled device on this destination port");
1304 dst_cache_reset(&geneve
->info
.dst_cache
);
1305 geneve
->info
= *info
;
1306 geneve
->collect_md
= metadata
;
1307 geneve
->use_udp6_rx_checksums
= ipv6_rx_csum
;
1308 geneve
->ttl_inherit
= ttl_inherit
;
1311 err
= register_netdevice(dev
);
1315 list_add(&geneve
->next
, &gn
->geneve_list
);
1319 static void init_tnl_info(struct ip_tunnel_info
*info
, __u16 dst_port
)
1321 memset(info
, 0, sizeof(*info
));
1322 info
->key
.tp_dst
= htons(dst_port
);
1325 static int geneve_nl2info(struct nlattr
*tb
[], struct nlattr
*data
[],
1326 struct netlink_ext_ack
*extack
,
1327 struct ip_tunnel_info
*info
, bool *metadata
,
1328 bool *use_udp6_rx_checksums
, bool *ttl_inherit
,
1329 enum ifla_geneve_df
*df
, bool changelink
)
1333 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
]) {
1334 NL_SET_ERR_MSG(extack
,
1335 "Cannot specify both IPv4 and IPv6 Remote addresses");
1339 if (data
[IFLA_GENEVE_REMOTE
]) {
1340 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET6
)) {
1341 attrtype
= IFLA_GENEVE_REMOTE
;
1345 info
->key
.u
.ipv4
.dst
=
1346 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1348 if (IN_MULTICAST(ntohl(info
->key
.u
.ipv4
.dst
))) {
1349 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE
],
1350 "Remote IPv4 address cannot be Multicast");
1355 if (data
[IFLA_GENEVE_REMOTE6
]) {
1356 #if IS_ENABLED(CONFIG_IPV6)
1357 if (changelink
&& (ip_tunnel_info_af(info
) == AF_INET
)) {
1358 attrtype
= IFLA_GENEVE_REMOTE6
;
1362 info
->mode
= IP_TUNNEL_INFO_IPV6
;
1363 info
->key
.u
.ipv6
.dst
=
1364 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1366 if (ipv6_addr_type(&info
->key
.u
.ipv6
.dst
) &
1367 IPV6_ADDR_LINKLOCAL
) {
1368 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1369 "Remote IPv6 address cannot be link-local");
1372 if (ipv6_addr_is_multicast(&info
->key
.u
.ipv6
.dst
)) {
1373 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1374 "Remote IPv6 address cannot be Multicast");
1377 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1378 *use_udp6_rx_checksums
= true;
1380 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_REMOTE6
],
1381 "IPv6 support not enabled in the kernel");
1382 return -EPFNOSUPPORT
;
1386 if (data
[IFLA_GENEVE_ID
]) {
1391 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1392 tvni
[0] = (vni
& 0x00ff0000) >> 16;
1393 tvni
[1] = (vni
& 0x0000ff00) >> 8;
1394 tvni
[2] = vni
& 0x000000ff;
1396 tunid
= vni_to_tunnel_id(tvni
);
1397 if (changelink
&& (tunid
!= info
->key
.tun_id
)) {
1398 attrtype
= IFLA_GENEVE_ID
;
1401 info
->key
.tun_id
= tunid
;
1404 if (data
[IFLA_GENEVE_TTL_INHERIT
]) {
1405 if (nla_get_u8(data
[IFLA_GENEVE_TTL_INHERIT
]))
1406 *ttl_inherit
= true;
1408 *ttl_inherit
= false;
1409 } else if (data
[IFLA_GENEVE_TTL
]) {
1410 info
->key
.ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1411 *ttl_inherit
= false;
1414 if (data
[IFLA_GENEVE_TOS
])
1415 info
->key
.tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1417 if (data
[IFLA_GENEVE_DF
])
1418 *df
= nla_get_u8(data
[IFLA_GENEVE_DF
]);
1420 if (data
[IFLA_GENEVE_LABEL
]) {
1421 info
->key
.label
= nla_get_be32(data
[IFLA_GENEVE_LABEL
]) &
1422 IPV6_FLOWLABEL_MASK
;
1423 if (info
->key
.label
&& (!(info
->mode
& IP_TUNNEL_INFO_IPV6
))) {
1424 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_LABEL
],
1425 "Label attribute only applies for IPv6 Geneve devices");
1430 if (data
[IFLA_GENEVE_PORT
]) {
1432 attrtype
= IFLA_GENEVE_PORT
;
1435 info
->key
.tp_dst
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1438 if (data
[IFLA_GENEVE_COLLECT_METADATA
]) {
1440 attrtype
= IFLA_GENEVE_COLLECT_METADATA
;
1446 if (data
[IFLA_GENEVE_UDP_CSUM
]) {
1448 attrtype
= IFLA_GENEVE_UDP_CSUM
;
1451 if (nla_get_u8(data
[IFLA_GENEVE_UDP_CSUM
]))
1452 info
->key
.tun_flags
|= TUNNEL_CSUM
;
1455 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]) {
1456 #if IS_ENABLED(CONFIG_IPV6)
1458 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_TX
;
1461 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
1462 info
->key
.tun_flags
&= ~TUNNEL_CSUM
;
1464 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
],
1465 "IPv6 support not enabled in the kernel");
1466 return -EPFNOSUPPORT
;
1470 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]) {
1471 #if IS_ENABLED(CONFIG_IPV6)
1473 attrtype
= IFLA_GENEVE_UDP_ZERO_CSUM6_RX
;
1476 if (nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
1477 *use_udp6_rx_checksums
= false;
1479 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
],
1480 "IPv6 support not enabled in the kernel");
1481 return -EPFNOSUPPORT
;
1487 NL_SET_ERR_MSG_ATTR(extack
, data
[attrtype
],
1488 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1492 static void geneve_link_config(struct net_device
*dev
,
1493 struct ip_tunnel_info
*info
, struct nlattr
*tb
[])
1495 struct geneve_dev
*geneve
= netdev_priv(dev
);
1499 geneve_change_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1503 switch (ip_tunnel_info_af(info
)) {
1505 struct flowi4 fl4
= { .daddr
= info
->key
.u
.ipv4
.dst
};
1506 struct rtable
*rt
= ip_route_output_key(geneve
->net
, &fl4
);
1508 if (!IS_ERR(rt
) && rt
->dst
.dev
) {
1509 ldev_mtu
= rt
->dst
.dev
->mtu
- GENEVE_IPV4_HLEN
;
1514 #if IS_ENABLED(CONFIG_IPV6)
1516 struct rt6_info
*rt
;
1518 if (!__in6_dev_get(dev
))
1521 rt
= rt6_lookup(geneve
->net
, &info
->key
.u
.ipv6
.dst
, NULL
, 0,
1524 if (rt
&& rt
->dst
.dev
)
1525 ldev_mtu
= rt
->dst
.dev
->mtu
- GENEVE_IPV6_HLEN
;
1535 geneve_change_mtu(dev
, ldev_mtu
- info
->options_len
);
1538 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1539 struct nlattr
*tb
[], struct nlattr
*data
[],
1540 struct netlink_ext_ack
*extack
)
1542 enum ifla_geneve_df df
= GENEVE_DF_UNSET
;
1543 bool use_udp6_rx_checksums
= false;
1544 struct ip_tunnel_info info
;
1545 bool ttl_inherit
= false;
1546 bool metadata
= false;
1549 init_tnl_info(&info
, GENEVE_UDP_PORT
);
1550 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1551 &use_udp6_rx_checksums
, &ttl_inherit
, &df
, false);
1555 err
= geneve_configure(net
, dev
, extack
, &info
, metadata
,
1556 use_udp6_rx_checksums
, ttl_inherit
, df
);
1560 geneve_link_config(dev
, &info
, tb
);
1565 /* Quiesces the geneve device data path for both TX and RX.
1567 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1568 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1569 * to complete for the existing set of in-flight packets to be transmitted,
1570 * then we would have quiesced the transmit data path. All the future packets
1571 * will get dropped until we unquiesce the data path.
1573 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1574 * if we set that to NULL under RCU and wait for synchronize_net() to
1575 * complete, then we would have quiesced the receive data path.
1577 static void geneve_quiesce(struct geneve_dev
*geneve
, struct geneve_sock
**gs4
,
1578 struct geneve_sock
**gs6
)
1580 *gs4
= rtnl_dereference(geneve
->sock4
);
1581 rcu_assign_pointer(geneve
->sock4
, NULL
);
1583 rcu_assign_sk_user_data((*gs4
)->sock
->sk
, NULL
);
1584 #if IS_ENABLED(CONFIG_IPV6)
1585 *gs6
= rtnl_dereference(geneve
->sock6
);
1586 rcu_assign_pointer(geneve
->sock6
, NULL
);
1588 rcu_assign_sk_user_data((*gs6
)->sock
->sk
, NULL
);
1595 /* Resumes the geneve device data path for both TX and RX. */
1596 static void geneve_unquiesce(struct geneve_dev
*geneve
, struct geneve_sock
*gs4
,
1597 struct geneve_sock __maybe_unused
*gs6
)
1599 rcu_assign_pointer(geneve
->sock4
, gs4
);
1601 rcu_assign_sk_user_data(gs4
->sock
->sk
, gs4
);
1602 #if IS_ENABLED(CONFIG_IPV6)
1603 rcu_assign_pointer(geneve
->sock6
, gs6
);
1605 rcu_assign_sk_user_data(gs6
->sock
->sk
, gs6
);
1610 static int geneve_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1611 struct nlattr
*data
[],
1612 struct netlink_ext_ack
*extack
)
1614 struct geneve_dev
*geneve
= netdev_priv(dev
);
1615 struct geneve_sock
*gs4
, *gs6
;
1616 struct ip_tunnel_info info
;
1618 bool use_udp6_rx_checksums
;
1619 enum ifla_geneve_df df
;
1623 /* If the geneve device is configured for metadata (or externally
1624 * controlled, for example, OVS), then nothing can be changed.
1626 if (geneve
->collect_md
)
1629 /* Start with the existing info. */
1630 memcpy(&info
, &geneve
->info
, sizeof(info
));
1631 metadata
= geneve
->collect_md
;
1632 use_udp6_rx_checksums
= geneve
->use_udp6_rx_checksums
;
1633 ttl_inherit
= geneve
->ttl_inherit
;
1634 err
= geneve_nl2info(tb
, data
, extack
, &info
, &metadata
,
1635 &use_udp6_rx_checksums
, &ttl_inherit
, &df
, true);
1639 if (!geneve_dst_addr_equal(&geneve
->info
, &info
)) {
1640 dst_cache_reset(&info
.dst_cache
);
1641 geneve_link_config(dev
, &info
, tb
);
1644 geneve_quiesce(geneve
, &gs4
, &gs6
);
1645 geneve
->info
= info
;
1646 geneve
->collect_md
= metadata
;
1647 geneve
->use_udp6_rx_checksums
= use_udp6_rx_checksums
;
1648 geneve
->ttl_inherit
= ttl_inherit
;
1649 geneve_unquiesce(geneve
, gs4
, gs6
);
1654 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1656 struct geneve_dev
*geneve
= netdev_priv(dev
);
1658 list_del(&geneve
->next
);
1659 unregister_netdevice_queue(dev
, head
);
1662 static size_t geneve_get_size(const struct net_device
*dev
)
1664 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1665 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1666 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1667 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1668 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_DF */
1669 nla_total_size(sizeof(__be32
)) + /* IFLA_GENEVE_LABEL */
1670 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1671 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1672 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_CSUM */
1673 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1674 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1675 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL_INHERIT */
1679 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1681 struct geneve_dev
*geneve
= netdev_priv(dev
);
1682 struct ip_tunnel_info
*info
= &geneve
->info
;
1683 bool ttl_inherit
= geneve
->ttl_inherit
;
1684 bool metadata
= geneve
->collect_md
;
1688 tunnel_id_to_vni(info
->key
.tun_id
, tmp_vni
);
1689 vni
= (tmp_vni
[0] << 16) | (tmp_vni
[1] << 8) | tmp_vni
[2];
1690 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1691 goto nla_put_failure
;
1693 if (!metadata
&& ip_tunnel_info_af(info
) == AF_INET
) {
1694 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1695 info
->key
.u
.ipv4
.dst
))
1696 goto nla_put_failure
;
1697 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_CSUM
,
1698 !!(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1699 goto nla_put_failure
;
1701 #if IS_ENABLED(CONFIG_IPV6)
1702 } else if (!metadata
) {
1703 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1704 &info
->key
.u
.ipv6
.dst
))
1705 goto nla_put_failure
;
1706 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
1707 !(info
->key
.tun_flags
& TUNNEL_CSUM
)))
1708 goto nla_put_failure
;
1712 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, info
->key
.ttl
) ||
1713 nla_put_u8(skb
, IFLA_GENEVE_TOS
, info
->key
.tos
) ||
1714 nla_put_be32(skb
, IFLA_GENEVE_LABEL
, info
->key
.label
))
1715 goto nla_put_failure
;
1717 if (nla_put_u8(skb
, IFLA_GENEVE_DF
, geneve
->df
))
1718 goto nla_put_failure
;
1720 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, info
->key
.tp_dst
))
1721 goto nla_put_failure
;
1723 if (metadata
&& nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1724 goto nla_put_failure
;
1726 #if IS_ENABLED(CONFIG_IPV6)
1727 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
1728 !geneve
->use_udp6_rx_checksums
))
1729 goto nla_put_failure
;
1732 if (nla_put_u8(skb
, IFLA_GENEVE_TTL_INHERIT
, ttl_inherit
))
1733 goto nla_put_failure
;
1741 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1743 .maxtype
= IFLA_GENEVE_MAX
,
1744 .policy
= geneve_policy
,
1745 .priv_size
= sizeof(struct geneve_dev
),
1746 .setup
= geneve_setup
,
1747 .validate
= geneve_validate
,
1748 .newlink
= geneve_newlink
,
1749 .changelink
= geneve_changelink
,
1750 .dellink
= geneve_dellink
,
1751 .get_size
= geneve_get_size
,
1752 .fill_info
= geneve_fill_info
,
1755 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1756 u8 name_assign_type
, u16 dst_port
)
1758 struct nlattr
*tb
[IFLA_MAX
+ 1];
1759 struct ip_tunnel_info info
;
1760 struct net_device
*dev
;
1761 LIST_HEAD(list_kill
);
1764 memset(tb
, 0, sizeof(tb
));
1765 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1766 &geneve_link_ops
, tb
, NULL
);
1770 init_tnl_info(&info
, dst_port
);
1771 err
= geneve_configure(net
, dev
, NULL
, &info
,
1772 true, true, false, GENEVE_DF_UNSET
);
1775 return ERR_PTR(err
);
1778 /* openvswitch users expect packet sizes to be unrestricted,
1779 * so set the largest MTU we can.
1781 err
= geneve_change_mtu(dev
, IP_MAX_MTU
);
1785 err
= rtnl_configure_link(dev
, NULL
);
1791 geneve_dellink(dev
, &list_kill
);
1792 unregister_netdevice_many(&list_kill
);
1793 return ERR_PTR(err
);
1795 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1797 static int geneve_netdevice_event(struct notifier_block
*unused
,
1798 unsigned long event
, void *ptr
)
1800 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1802 if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
||
1803 event
== NETDEV_UDP_TUNNEL_DROP_INFO
) {
1804 geneve_offload_rx_ports(dev
, event
== NETDEV_UDP_TUNNEL_PUSH_INFO
);
1805 } else if (event
== NETDEV_UNREGISTER
) {
1806 geneve_offload_rx_ports(dev
, false);
1807 } else if (event
== NETDEV_REGISTER
) {
1808 geneve_offload_rx_ports(dev
, true);
1814 static struct notifier_block geneve_notifier_block __read_mostly
= {
1815 .notifier_call
= geneve_netdevice_event
,
1818 static __net_init
int geneve_init_net(struct net
*net
)
1820 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1822 INIT_LIST_HEAD(&gn
->geneve_list
);
1823 INIT_LIST_HEAD(&gn
->sock_list
);
1827 static void geneve_destroy_tunnels(struct net
*net
, struct list_head
*head
)
1829 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1830 struct geneve_dev
*geneve
, *next
;
1831 struct net_device
*dev
, *aux
;
1833 /* gather any geneve devices that were moved into this ns */
1834 for_each_netdev_safe(net
, dev
, aux
)
1835 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1836 unregister_netdevice_queue(dev
, head
);
1838 /* now gather any other geneve devices that were created in this ns */
1839 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1840 /* If geneve->dev is in the same netns, it was already added
1841 * to the list by the previous loop.
1843 if (!net_eq(dev_net(geneve
->dev
), net
))
1844 unregister_netdevice_queue(geneve
->dev
, head
);
1847 WARN_ON_ONCE(!list_empty(&gn
->sock_list
));
1850 static void __net_exit
geneve_exit_batch_net(struct list_head
*net_list
)
1856 list_for_each_entry(net
, net_list
, exit_list
)
1857 geneve_destroy_tunnels(net
, &list
);
1859 /* unregister the devices gathered above */
1860 unregister_netdevice_many(&list
);
1864 static struct pernet_operations geneve_net_ops
= {
1865 .init
= geneve_init_net
,
1866 .exit_batch
= geneve_exit_batch_net
,
1867 .id
= &geneve_net_id
,
1868 .size
= sizeof(struct geneve_net
),
1871 static int __init
geneve_init_module(void)
1875 rc
= register_pernet_subsys(&geneve_net_ops
);
1879 rc
= register_netdevice_notifier(&geneve_notifier_block
);
1883 rc
= rtnl_link_register(&geneve_link_ops
);
1889 unregister_netdevice_notifier(&geneve_notifier_block
);
1891 unregister_pernet_subsys(&geneve_net_ops
);
1895 late_initcall(geneve_init_module
);
1897 static void __exit
geneve_cleanup_module(void)
1899 rtnl_link_unregister(&geneve_link_ops
);
1900 unregister_netdevice_notifier(&geneve_notifier_block
);
1901 unregister_pernet_subsys(&geneve_net_ops
);
1903 module_exit(geneve_cleanup_module
);
1905 MODULE_LICENSE("GPL");
1906 MODULE_VERSION(GENEVE_NETDEV_VER
);
1907 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1908 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1909 MODULE_ALIAS_RTNL_LINK("geneve");