2 * GENEVE: Generic Network Virtualization Encapsulation
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/hash.h>
18 #include <net/dst_metadata.h>
19 #include <net/gro_cells.h>
20 #include <net/rtnetlink.h>
21 #include <net/geneve.h>
22 #include <net/protocol.h>
24 #define GENEVE_NETDEV_VER "0.6"
26 #define GENEVE_UDP_PORT 6081
28 #define GENEVE_N_VID (1u << 24)
29 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
31 #define VNI_HASH_BITS 10
32 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
34 static bool log_ecn_error
= true;
35 module_param(log_ecn_error
, bool, 0644);
36 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
39 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
41 /* per-network namespace private data for this module */
43 struct list_head geneve_list
;
44 struct list_head sock_list
;
47 static int geneve_net_id
;
50 struct sockaddr_in sin
;
51 struct sockaddr_in6 sin6
;
55 static union geneve_addr geneve_remote_unspec
= { .sa
.sa_family
= AF_UNSPEC
, };
57 /* Pseudo network device */
59 struct hlist_node hlist
; /* vni hash table */
60 struct net
*net
; /* netns for packet i/o */
61 struct net_device
*dev
; /* netdev for geneve tunnel */
62 struct geneve_sock
*sock4
; /* IPv4 socket used for geneve tunnel */
63 #if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_sock
*sock6
; /* IPv6 socket used for geneve tunnel */
66 u8 vni
[3]; /* virtual network ID for tunnel */
67 u8 ttl
; /* TTL override */
68 u8 tos
; /* TOS override */
69 union geneve_addr remote
; /* IP address for link partner */
70 struct list_head next
; /* geneve's per namespace list */
73 struct gro_cells gro_cells
;
78 struct list_head list
;
82 struct udp_offload udp_offloads
;
83 struct hlist_head vni_list
[VNI_HASH_SIZE
];
86 static inline __u32
geneve_net_vni_hash(u8 vni
[3])
90 vnid
= (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
91 return hash_32(vnid
, VNI_HASH_BITS
);
94 static __be64
vni_to_tunnel_id(const __u8
*vni
)
97 return (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
99 return (__force __be64
)(((__force u64
)vni
[0] << 40) |
100 ((__force u64
)vni
[1] << 48) |
101 ((__force u64
)vni
[2] << 56));
105 static struct geneve_dev
*geneve_lookup(struct geneve_sock
*gs
,
106 __be32 addr
, u8 vni
[])
108 struct hlist_head
*vni_list_head
;
109 struct geneve_dev
*geneve
;
112 /* Find the device for this VNI */
113 hash
= geneve_net_vni_hash(vni
);
114 vni_list_head
= &gs
->vni_list
[hash
];
115 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
116 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
117 addr
== geneve
->remote
.sin
.sin_addr
.s_addr
)
123 #if IS_ENABLED(CONFIG_IPV6)
124 static struct geneve_dev
*geneve6_lookup(struct geneve_sock
*gs
,
125 struct in6_addr addr6
, u8 vni
[])
127 struct hlist_head
*vni_list_head
;
128 struct geneve_dev
*geneve
;
131 /* Find the device for this VNI */
132 hash
= geneve_net_vni_hash(vni
);
133 vni_list_head
= &gs
->vni_list
[hash
];
134 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
135 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
136 ipv6_addr_equal(&addr6
, &geneve
->remote
.sin6
.sin6_addr
))
143 static inline struct genevehdr
*geneve_hdr(const struct sk_buff
*skb
)
145 return (struct genevehdr
*)(udp_hdr(skb
) + 1);
148 /* geneve receive/decap routine */
149 static void geneve_rx(struct geneve_sock
*gs
, struct sk_buff
*skb
)
151 struct genevehdr
*gnvh
= geneve_hdr(skb
);
152 struct metadata_dst
*tun_dst
= NULL
;
153 struct geneve_dev
*geneve
= NULL
;
154 struct pcpu_sw_netstats
*stats
;
155 struct iphdr
*iph
= NULL
;
157 static u8 zero_vni
[3];
160 sa_family_t sa_family
;
161 #if IS_ENABLED(CONFIG_IPV6)
162 struct ipv6hdr
*ip6h
= NULL
;
163 struct in6_addr addr6
;
164 static struct in6_addr zero_addr6
;
167 sa_family
= gs
->sock
->sk
->sk_family
;
169 if (sa_family
== AF_INET
) {
170 iph
= ip_hdr(skb
); /* outer IP header... */
172 if (gs
->collect_md
) {
181 geneve
= geneve_lookup(gs
, addr
, vni
);
182 #if IS_ENABLED(CONFIG_IPV6)
183 } else if (sa_family
== AF_INET6
) {
184 ip6h
= ipv6_hdr(skb
); /* outer IPv6 header... */
186 if (gs
->collect_md
) {
195 geneve
= geneve6_lookup(gs
, addr6
, vni
);
201 if (ip_tunnel_collect_metadata() || gs
->collect_md
) {
204 flags
= TUNNEL_KEY
| TUNNEL_GENEVE_OPT
|
205 (gnvh
->oam
? TUNNEL_OAM
: 0) |
206 (gnvh
->critical
? TUNNEL_CRIT_OPT
: 0);
208 tun_dst
= udp_tun_rx_dst(skb
, sa_family
, flags
,
209 vni_to_tunnel_id(gnvh
->vni
),
213 /* Update tunnel dst according to Geneve options. */
214 ip_tunnel_info_opts_set(&tun_dst
->u
.tun_info
,
215 gnvh
->options
, gnvh
->opt_len
* 4);
217 /* Drop packets w/ critical options,
218 * since we don't support any...
224 skb_reset_mac_header(skb
);
225 skb_scrub_packet(skb
, !net_eq(geneve
->net
, dev_net(geneve
->dev
)));
226 skb
->protocol
= eth_type_trans(skb
, geneve
->dev
);
227 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
230 skb_dst_set(skb
, &tun_dst
->dst
);
232 /* Ignore packet loops (and multicast echo) */
233 if (ether_addr_equal(eth_hdr(skb
)->h_source
, geneve
->dev
->dev_addr
))
236 skb_reset_network_header(skb
);
239 err
= IP_ECN_decapsulate(iph
, skb
);
240 #if IS_ENABLED(CONFIG_IPV6)
242 err
= IP6_ECN_decapsulate(ip6h
, skb
);
248 net_info_ratelimited("non-ECT from %pI4 "
250 &iph
->saddr
, iph
->tos
);
251 #if IS_ENABLED(CONFIG_IPV6)
253 net_info_ratelimited("non-ECT from %pI6\n",
258 ++geneve
->dev
->stats
.rx_frame_errors
;
259 ++geneve
->dev
->stats
.rx_errors
;
264 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
265 u64_stats_update_begin(&stats
->syncp
);
267 stats
->rx_bytes
+= skb
->len
;
268 u64_stats_update_end(&stats
->syncp
);
270 gro_cells_receive(&geneve
->gro_cells
, skb
);
273 /* Consume bad packet */
277 /* Setup stats when device is created */
278 static int geneve_init(struct net_device
*dev
)
280 struct geneve_dev
*geneve
= netdev_priv(dev
);
283 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
287 err
= gro_cells_init(&geneve
->gro_cells
, dev
);
289 free_percpu(dev
->tstats
);
296 static void geneve_uninit(struct net_device
*dev
)
298 struct geneve_dev
*geneve
= netdev_priv(dev
);
300 gro_cells_destroy(&geneve
->gro_cells
);
301 free_percpu(dev
->tstats
);
304 /* Callback from net/ipv4/udp.c to receive packets */
305 static int geneve_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
307 struct genevehdr
*geneveh
;
308 struct geneve_sock
*gs
;
311 /* Need Geneve and inner Ethernet header to be present */
312 if (unlikely(!pskb_may_pull(skb
, GENEVE_BASE_HLEN
)))
315 /* Return packets with reserved bits set */
316 geneveh
= geneve_hdr(skb
);
317 if (unlikely(geneveh
->ver
!= GENEVE_VER
))
320 if (unlikely(geneveh
->proto_type
!= htons(ETH_P_TEB
)))
323 opts_len
= geneveh
->opt_len
* 4;
324 if (iptunnel_pull_header(skb
, GENEVE_BASE_HLEN
+ opts_len
,
328 gs
= rcu_dereference_sk_user_data(sk
);
336 /* Consume bad packet */
341 /* Let the UDP layer deal with the skb */
345 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
349 struct udp_port_cfg udp_conf
;
352 memset(&udp_conf
, 0, sizeof(udp_conf
));
355 udp_conf
.family
= AF_INET6
;
356 udp_conf
.ipv6_v6only
= 1;
358 udp_conf
.family
= AF_INET
;
359 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
362 udp_conf
.local_udp_port
= port
;
364 /* Open UDP socket */
365 err
= udp_sock_create(net
, &udp_conf
, &sock
);
372 static void geneve_notify_add_rx_port(struct geneve_sock
*gs
)
374 struct sock
*sk
= gs
->sock
->sk
;
375 sa_family_t sa_family
= sk
->sk_family
;
378 if (sa_family
== AF_INET
) {
379 err
= udp_add_offload(&gs
->udp_offloads
);
381 pr_warn("geneve: udp_add_offload failed with status %d\n",
386 static int geneve_hlen(struct genevehdr
*gh
)
388 return sizeof(*gh
) + gh
->opt_len
* 4;
391 static struct sk_buff
**geneve_gro_receive(struct sk_buff
**head
,
393 struct udp_offload
*uoff
)
395 struct sk_buff
*p
, **pp
= NULL
;
396 struct genevehdr
*gh
, *gh2
;
397 unsigned int hlen
, gh_len
, off_gnv
;
398 const struct packet_offload
*ptype
;
402 off_gnv
= skb_gro_offset(skb
);
403 hlen
= off_gnv
+ sizeof(*gh
);
404 gh
= skb_gro_header_fast(skb
, off_gnv
);
405 if (skb_gro_header_hard(skb
, hlen
)) {
406 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
411 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
413 gh_len
= geneve_hlen(gh
);
415 hlen
= off_gnv
+ gh_len
;
416 if (skb_gro_header_hard(skb
, hlen
)) {
417 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
424 for (p
= *head
; p
; p
= p
->next
) {
425 if (!NAPI_GRO_CB(p
)->same_flow
)
428 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
429 if (gh
->opt_len
!= gh2
->opt_len
||
430 memcmp(gh
, gh2
, gh_len
)) {
431 NAPI_GRO_CB(p
)->same_flow
= 0;
436 type
= gh
->proto_type
;
439 ptype
= gro_find_receive_by_type(type
);
445 skb_gro_pull(skb
, gh_len
);
446 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
447 pp
= ptype
->callbacks
.gro_receive(head
, skb
);
452 NAPI_GRO_CB(skb
)->flush
|= flush
;
457 static int geneve_gro_complete(struct sk_buff
*skb
, int nhoff
,
458 struct udp_offload
*uoff
)
460 struct genevehdr
*gh
;
461 struct packet_offload
*ptype
;
466 udp_tunnel_gro_complete(skb
, nhoff
);
468 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
469 gh_len
= geneve_hlen(gh
);
470 type
= gh
->proto_type
;
473 ptype
= gro_find_complete_by_type(type
);
475 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
481 /* Create new listen socket if needed */
482 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
485 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
486 struct geneve_sock
*gs
;
488 struct udp_tunnel_sock_cfg tunnel_cfg
;
491 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
493 return ERR_PTR(-ENOMEM
);
495 sock
= geneve_create_sock(net
, ipv6
, port
);
498 return ERR_CAST(sock
);
503 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
504 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
506 /* Initialize the geneve udp offloads structure */
507 gs
->udp_offloads
.port
= port
;
508 gs
->udp_offloads
.callbacks
.gro_receive
= geneve_gro_receive
;
509 gs
->udp_offloads
.callbacks
.gro_complete
= geneve_gro_complete
;
510 geneve_notify_add_rx_port(gs
);
512 /* Mark socket as an encapsulation socket */
513 tunnel_cfg
.sk_user_data
= gs
;
514 tunnel_cfg
.encap_type
= 1;
515 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
516 tunnel_cfg
.encap_destroy
= NULL
;
517 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
518 list_add(&gs
->list
, &gn
->sock_list
);
522 static void geneve_notify_del_rx_port(struct geneve_sock
*gs
)
524 struct sock
*sk
= gs
->sock
->sk
;
525 sa_family_t sa_family
= sk
->sk_family
;
527 if (sa_family
== AF_INET
)
528 udp_del_offload(&gs
->udp_offloads
);
531 static void __geneve_sock_release(struct geneve_sock
*gs
)
533 if (!gs
|| --gs
->refcnt
)
537 geneve_notify_del_rx_port(gs
);
538 udp_tunnel_sock_release(gs
->sock
);
542 static void geneve_sock_release(struct geneve_dev
*geneve
)
544 __geneve_sock_release(geneve
->sock4
);
545 #if IS_ENABLED(CONFIG_IPV6)
546 __geneve_sock_release(geneve
->sock6
);
550 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
554 struct geneve_sock
*gs
;
556 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
557 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
558 inet_sk(gs
->sock
->sk
)->sk
.sk_family
== family
) {
565 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
567 struct net
*net
= geneve
->net
;
568 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
569 struct geneve_sock
*gs
;
572 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->dst_port
);
578 gs
= geneve_socket_create(net
, geneve
->dst_port
, ipv6
);
583 gs
->collect_md
= geneve
->collect_md
;
584 #if IS_ENABLED(CONFIG_IPV6)
591 hash
= geneve_net_vni_hash(geneve
->vni
);
592 hlist_add_head_rcu(&geneve
->hlist
, &gs
->vni_list
[hash
]);
596 static int geneve_open(struct net_device
*dev
)
598 struct geneve_dev
*geneve
= netdev_priv(dev
);
599 bool ipv6
= geneve
->remote
.sa
.sa_family
== AF_INET6
;
600 bool metadata
= geneve
->collect_md
;
603 geneve
->sock4
= NULL
;
604 #if IS_ENABLED(CONFIG_IPV6)
605 geneve
->sock6
= NULL
;
606 if (ipv6
|| metadata
)
607 ret
= geneve_sock_add(geneve
, true);
609 if (!ret
&& (!ipv6
|| metadata
))
610 ret
= geneve_sock_add(geneve
, false);
612 geneve_sock_release(geneve
);
617 static int geneve_stop(struct net_device
*dev
)
619 struct geneve_dev
*geneve
= netdev_priv(dev
);
621 if (!hlist_unhashed(&geneve
->hlist
))
622 hlist_del_rcu(&geneve
->hlist
);
623 geneve_sock_release(geneve
);
627 static void geneve_build_header(struct genevehdr
*geneveh
,
628 __be16 tun_flags
, u8 vni
[3],
629 u8 options_len
, u8
*options
)
631 geneveh
->ver
= GENEVE_VER
;
632 geneveh
->opt_len
= options_len
/ 4;
633 geneveh
->oam
= !!(tun_flags
& TUNNEL_OAM
);
634 geneveh
->critical
= !!(tun_flags
& TUNNEL_CRIT_OPT
);
636 memcpy(geneveh
->vni
, vni
, 3);
637 geneveh
->proto_type
= htons(ETH_P_TEB
);
640 memcpy(geneveh
->options
, options
, options_len
);
643 static int geneve_build_skb(struct rtable
*rt
, struct sk_buff
*skb
,
644 __be16 tun_flags
, u8 vni
[3], u8 opt_len
, u8
*opt
,
645 bool csum
, bool xnet
)
647 struct genevehdr
*gnvh
;
651 skb_scrub_packet(skb
, xnet
);
653 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
654 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct iphdr
);
655 err
= skb_cow_head(skb
, min_headroom
);
661 skb
= udp_tunnel_handle_offloads(skb
, csum
);
667 gnvh
= (struct genevehdr
*)__skb_push(skb
, sizeof(*gnvh
) + opt_len
);
668 geneve_build_header(gnvh
, tun_flags
, vni
, opt_len
, opt
);
670 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
678 #if IS_ENABLED(CONFIG_IPV6)
679 static int geneve6_build_skb(struct dst_entry
*dst
, struct sk_buff
*skb
,
680 __be16 tun_flags
, u8 vni
[3], u8 opt_len
, u8
*opt
,
681 bool csum
, bool xnet
)
683 struct genevehdr
*gnvh
;
687 skb_scrub_packet(skb
, xnet
);
689 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
690 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct ipv6hdr
);
691 err
= skb_cow_head(skb
, min_headroom
);
697 skb
= udp_tunnel_handle_offloads(skb
, csum
);
703 gnvh
= (struct genevehdr
*)__skb_push(skb
, sizeof(*gnvh
) + opt_len
);
704 geneve_build_header(gnvh
, tun_flags
, vni
, opt_len
, opt
);
706 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
715 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
716 struct net_device
*dev
,
718 struct ip_tunnel_info
*info
)
720 struct geneve_dev
*geneve
= netdev_priv(dev
);
721 struct rtable
*rt
= NULL
;
724 memset(fl4
, 0, sizeof(*fl4
));
725 fl4
->flowi4_mark
= skb
->mark
;
726 fl4
->flowi4_proto
= IPPROTO_UDP
;
729 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
730 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
731 fl4
->flowi4_tos
= RT_TOS(info
->key
.tos
);
735 const struct iphdr
*iip
= ip_hdr(skb
);
737 tos
= ip_tunnel_get_dsfield(iip
, skb
);
740 fl4
->flowi4_tos
= RT_TOS(tos
);
741 fl4
->daddr
= geneve
->remote
.sin
.sin_addr
.s_addr
;
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
);
757 #if IS_ENABLED(CONFIG_IPV6)
758 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
759 struct net_device
*dev
,
761 struct ip_tunnel_info
*info
)
763 struct geneve_dev
*geneve
= netdev_priv(dev
);
764 struct geneve_sock
*gs6
= geneve
->sock6
;
765 struct dst_entry
*dst
= NULL
;
768 memset(fl6
, 0, sizeof(*fl6
));
769 fl6
->flowi6_mark
= skb
->mark
;
770 fl6
->flowi6_proto
= IPPROTO_UDP
;
773 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
774 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
775 fl6
->flowi6_tos
= RT_TOS(info
->key
.tos
);
779 const struct iphdr
*iip
= ip_hdr(skb
);
781 prio
= ip_tunnel_get_dsfield(iip
, skb
);
784 fl6
->flowi6_tos
= RT_TOS(prio
);
785 fl6
->daddr
= geneve
->remote
.sin6
.sin6_addr
;
788 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
789 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
790 return ERR_PTR(-ENETUNREACH
);
792 if (dst
->dev
== dev
) { /* is this necessary? */
793 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
795 return ERR_PTR(-ELOOP
);
802 /* Convert 64 bit tunnel ID to 24 bit VNI. */
803 static void tunnel_id_to_vni(__be64 tun_id
, __u8
*vni
)
806 vni
[0] = (__force __u8
)(tun_id
>> 16);
807 vni
[1] = (__force __u8
)(tun_id
>> 8);
808 vni
[2] = (__force __u8
)tun_id
;
810 vni
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
811 vni
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
812 vni
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
816 static netdev_tx_t
geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
817 struct ip_tunnel_info
*info
)
819 struct geneve_dev
*geneve
= netdev_priv(dev
);
820 struct geneve_sock
*gs4
= geneve
->sock4
;
821 struct rtable
*rt
= NULL
;
822 const struct iphdr
*iip
; /* interior IP header */
829 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
831 if (geneve
->collect_md
) {
832 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
833 netdev_dbg(dev
, "no tunnel metadata\n");
836 if (info
&& ip_tunnel_info_af(info
) != AF_INET
)
840 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
846 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
847 skb_reset_mac_header(skb
);
852 const struct ip_tunnel_key
*key
= &info
->key
;
856 tunnel_id_to_vni(key
->tun_id
, vni
);
857 if (key
->tun_flags
& TUNNEL_GENEVE_OPT
)
858 opts
= ip_tunnel_info_opts(info
);
860 udp_csum
= !!(key
->tun_flags
& TUNNEL_CSUM
);
861 err
= geneve_build_skb(rt
, skb
, key
->tun_flags
, vni
,
862 info
->options_len
, opts
, udp_csum
, xnet
);
866 tos
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
868 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
871 err
= geneve_build_skb(rt
, skb
, 0, geneve
->vni
,
872 0, NULL
, udp_csum
, xnet
);
876 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, iip
, skb
);
878 if (!ttl
&& IN_MULTICAST(ntohl(fl4
.daddr
)))
880 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
883 err
= udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
884 tos
, ttl
, df
, sport
, geneve
->dst_port
,
885 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
888 iptunnel_xmit_stats(err
, &dev
->stats
, dev
->tstats
);
895 dev
->stats
.collisions
++;
896 else if (err
== -ENETUNREACH
)
897 dev
->stats
.tx_carrier_errors
++;
899 dev
->stats
.tx_errors
++;
903 #if IS_ENABLED(CONFIG_IPV6)
904 static netdev_tx_t
geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
905 struct ip_tunnel_info
*info
)
907 struct geneve_dev
*geneve
= netdev_priv(dev
);
908 struct geneve_sock
*gs6
= geneve
->sock6
;
909 struct dst_entry
*dst
= NULL
;
910 const struct iphdr
*iip
; /* interior IP header */
916 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
918 if (geneve
->collect_md
) {
919 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
920 netdev_dbg(dev
, "no tunnel metadata\n");
925 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
931 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
932 skb_reset_mac_header(skb
);
937 const struct ip_tunnel_key
*key
= &info
->key
;
941 tunnel_id_to_vni(key
->tun_id
, vni
);
942 if (key
->tun_flags
& TUNNEL_GENEVE_OPT
)
943 opts
= ip_tunnel_info_opts(info
);
945 udp_csum
= !!(key
->tun_flags
& TUNNEL_CSUM
);
946 err
= geneve6_build_skb(dst
, skb
, key
->tun_flags
, vni
,
947 info
->options_len
, opts
,
952 prio
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
956 err
= geneve6_build_skb(dst
, skb
, 0, geneve
->vni
,
957 0, NULL
, udp_csum
, xnet
);
961 prio
= ip_tunnel_ecn_encap(fl6
.flowi6_tos
, iip
, skb
);
963 if (!ttl
&& ipv6_addr_is_multicast(&fl6
.daddr
))
965 ttl
= ttl
? : ip6_dst_hoplimit(dst
);
967 err
= udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
968 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
,
969 sport
, geneve
->dst_port
, !udp_csum
);
976 dev
->stats
.collisions
++;
977 else if (err
== -ENETUNREACH
)
978 dev
->stats
.tx_carrier_errors
++;
980 dev
->stats
.tx_errors
++;
985 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
987 struct geneve_dev
*geneve
= netdev_priv(dev
);
988 struct ip_tunnel_info
*info
= NULL
;
990 if (geneve
->collect_md
)
991 info
= skb_tunnel_info(skb
);
993 #if IS_ENABLED(CONFIG_IPV6)
994 if ((info
&& ip_tunnel_info_af(info
) == AF_INET6
) ||
995 (!info
&& geneve
->remote
.sa
.sa_family
== AF_INET6
))
996 return geneve6_xmit_skb(skb
, dev
, info
);
998 return geneve_xmit_skb(skb
, dev
, info
);
1001 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
1003 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
1004 struct geneve_dev
*geneve
= netdev_priv(dev
);
1007 #if IS_ENABLED(CONFIG_IPV6)
1008 struct dst_entry
*dst
;
1012 if (ip_tunnel_info_af(info
) == AF_INET
) {
1013 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
1018 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
1019 #if IS_ENABLED(CONFIG_IPV6)
1020 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
1021 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
1023 return PTR_ERR(dst
);
1026 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
1032 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
1033 1, USHRT_MAX
, true);
1034 info
->key
.tp_dst
= geneve
->dst_port
;
1038 static const struct net_device_ops geneve_netdev_ops
= {
1039 .ndo_init
= geneve_init
,
1040 .ndo_uninit
= geneve_uninit
,
1041 .ndo_open
= geneve_open
,
1042 .ndo_stop
= geneve_stop
,
1043 .ndo_start_xmit
= geneve_xmit
,
1044 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1045 .ndo_change_mtu
= eth_change_mtu
,
1046 .ndo_validate_addr
= eth_validate_addr
,
1047 .ndo_set_mac_address
= eth_mac_addr
,
1048 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1051 static void geneve_get_drvinfo(struct net_device
*dev
,
1052 struct ethtool_drvinfo
*drvinfo
)
1054 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1055 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1058 static const struct ethtool_ops geneve_ethtool_ops
= {
1059 .get_drvinfo
= geneve_get_drvinfo
,
1060 .get_link
= ethtool_op_get_link
,
1063 /* Info for udev, that this is a virtual tunnel endpoint */
1064 static struct device_type geneve_type
= {
1068 /* Initialize the device structure. */
1069 static void geneve_setup(struct net_device
*dev
)
1073 dev
->netdev_ops
= &geneve_netdev_ops
;
1074 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1075 dev
->destructor
= free_netdev
;
1077 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1079 dev
->features
|= NETIF_F_LLTX
;
1080 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1081 dev
->features
|= NETIF_F_RXCSUM
;
1082 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1084 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1085 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1087 netif_keep_dst(dev
);
1088 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1089 eth_hw_addr_random(dev
);
1092 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1093 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1094 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1095 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1096 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1097 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1098 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1099 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1102 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1104 if (tb
[IFLA_ADDRESS
]) {
1105 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1108 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1109 return -EADDRNOTAVAIL
;
1115 if (data
[IFLA_GENEVE_ID
]) {
1116 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1118 if (vni
>= GENEVE_VID_MASK
)
1125 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1127 union geneve_addr
*remote
,
1129 bool *tun_on_same_port
,
1130 bool *tun_collect_md
)
1132 struct geneve_dev
*geneve
, *t
;
1134 *tun_on_same_port
= false;
1135 *tun_collect_md
= false;
1137 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1138 if (geneve
->dst_port
== dst_port
) {
1139 *tun_collect_md
= geneve
->collect_md
;
1140 *tun_on_same_port
= true;
1142 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
1143 !memcmp(remote
, &geneve
->remote
, sizeof(geneve
->remote
)) &&
1144 dst_port
== geneve
->dst_port
)
1150 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1151 union geneve_addr
*remote
,
1152 __u32 vni
, __u8 ttl
, __u8 tos
, __be16 dst_port
,
1155 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1156 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1157 bool tun_collect_md
, tun_on_same_port
;
1163 (remote
->sa
.sa_family
!= AF_UNSPEC
|| vni
|| tos
|| ttl
))
1169 geneve
->vni
[0] = (vni
& 0x00ff0000) >> 16;
1170 geneve
->vni
[1] = (vni
& 0x0000ff00) >> 8;
1171 geneve
->vni
[2] = vni
& 0x000000ff;
1173 if ((remote
->sa
.sa_family
== AF_INET
&&
1174 IN_MULTICAST(ntohl(remote
->sin
.sin_addr
.s_addr
))) ||
1175 (remote
->sa
.sa_family
== AF_INET6
&&
1176 ipv6_addr_is_multicast(&remote
->sin6
.sin6_addr
)))
1178 geneve
->remote
= *remote
;
1182 geneve
->dst_port
= dst_port
;
1183 geneve
->collect_md
= metadata
;
1185 t
= geneve_find_dev(gn
, dst_port
, remote
, geneve
->vni
,
1186 &tun_on_same_port
, &tun_collect_md
);
1190 /* make enough headroom for basic scenario */
1191 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1192 if (remote
->sa
.sa_family
== AF_INET
)
1193 encap_len
+= sizeof(struct iphdr
);
1195 encap_len
+= sizeof(struct ipv6hdr
);
1196 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1199 if (tun_on_same_port
)
1206 err
= register_netdevice(dev
);
1210 list_add(&geneve
->next
, &gn
->geneve_list
);
1214 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1215 struct nlattr
*tb
[], struct nlattr
*data
[])
1217 __be16 dst_port
= htons(GENEVE_UDP_PORT
);
1218 __u8 ttl
= 0, tos
= 0;
1219 bool metadata
= false;
1220 union geneve_addr remote
= geneve_remote_unspec
;
1223 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
])
1226 if (data
[IFLA_GENEVE_REMOTE
]) {
1227 remote
.sa
.sa_family
= AF_INET
;
1228 remote
.sin
.sin_addr
.s_addr
=
1229 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1232 if (data
[IFLA_GENEVE_REMOTE6
]) {
1233 if (!IS_ENABLED(CONFIG_IPV6
))
1234 return -EPFNOSUPPORT
;
1236 remote
.sa
.sa_family
= AF_INET6
;
1237 remote
.sin6
.sin6_addr
=
1238 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1240 if (ipv6_addr_type(&remote
.sin6
.sin6_addr
) &
1241 IPV6_ADDR_LINKLOCAL
) {
1242 netdev_dbg(dev
, "link-local remote is unsupported\n");
1247 if (data
[IFLA_GENEVE_ID
])
1248 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1250 if (data
[IFLA_GENEVE_TTL
])
1251 ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1253 if (data
[IFLA_GENEVE_TOS
])
1254 tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1256 if (data
[IFLA_GENEVE_PORT
])
1257 dst_port
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1259 if (data
[IFLA_GENEVE_COLLECT_METADATA
])
1262 return geneve_configure(net
, dev
, &remote
, vni
, ttl
, tos
, dst_port
,
1266 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1268 struct geneve_dev
*geneve
= netdev_priv(dev
);
1270 list_del(&geneve
->next
);
1271 unregister_netdevice_queue(dev
, head
);
1274 static size_t geneve_get_size(const struct net_device
*dev
)
1276 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1277 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1278 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1279 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1280 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1281 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1285 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1287 struct geneve_dev
*geneve
= netdev_priv(dev
);
1290 vni
= (geneve
->vni
[0] << 16) | (geneve
->vni
[1] << 8) | geneve
->vni
[2];
1291 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1292 goto nla_put_failure
;
1294 if (geneve
->remote
.sa
.sa_family
== AF_INET
) {
1295 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1296 geneve
->remote
.sin
.sin_addr
.s_addr
))
1297 goto nla_put_failure
;
1298 #if IS_ENABLED(CONFIG_IPV6)
1300 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1301 &geneve
->remote
.sin6
.sin6_addr
))
1302 goto nla_put_failure
;
1306 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, geneve
->ttl
) ||
1307 nla_put_u8(skb
, IFLA_GENEVE_TOS
, geneve
->tos
))
1308 goto nla_put_failure
;
1310 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, geneve
->dst_port
))
1311 goto nla_put_failure
;
1313 if (geneve
->collect_md
) {
1314 if (nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1315 goto nla_put_failure
;
1324 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1326 .maxtype
= IFLA_GENEVE_MAX
,
1327 .policy
= geneve_policy
,
1328 .priv_size
= sizeof(struct geneve_dev
),
1329 .setup
= geneve_setup
,
1330 .validate
= geneve_validate
,
1331 .newlink
= geneve_newlink
,
1332 .dellink
= geneve_dellink
,
1333 .get_size
= geneve_get_size
,
1334 .fill_info
= geneve_fill_info
,
1337 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1338 u8 name_assign_type
, u16 dst_port
)
1340 struct nlattr
*tb
[IFLA_MAX
+ 1];
1341 struct net_device
*dev
;
1344 memset(tb
, 0, sizeof(tb
));
1345 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1346 &geneve_link_ops
, tb
);
1350 err
= geneve_configure(net
, dev
, &geneve_remote_unspec
,
1351 0, 0, 0, htons(dst_port
), true);
1354 return ERR_PTR(err
);
1358 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1360 static __net_init
int geneve_init_net(struct net
*net
)
1362 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1364 INIT_LIST_HEAD(&gn
->geneve_list
);
1365 INIT_LIST_HEAD(&gn
->sock_list
);
1369 static void __net_exit
geneve_exit_net(struct net
*net
)
1371 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1372 struct geneve_dev
*geneve
, *next
;
1373 struct net_device
*dev
, *aux
;
1378 /* gather any geneve devices that were moved into this ns */
1379 for_each_netdev_safe(net
, dev
, aux
)
1380 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1381 unregister_netdevice_queue(dev
, &list
);
1383 /* now gather any other geneve devices that were created in this ns */
1384 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1385 /* If geneve->dev is in the same netns, it was already added
1386 * to the list by the previous loop.
1388 if (!net_eq(dev_net(geneve
->dev
), net
))
1389 unregister_netdevice_queue(geneve
->dev
, &list
);
1392 /* unregister the devices gathered above */
1393 unregister_netdevice_many(&list
);
1397 static struct pernet_operations geneve_net_ops
= {
1398 .init
= geneve_init_net
,
1399 .exit
= geneve_exit_net
,
1400 .id
= &geneve_net_id
,
1401 .size
= sizeof(struct geneve_net
),
1404 static int __init
geneve_init_module(void)
1408 rc
= register_pernet_subsys(&geneve_net_ops
);
1412 rc
= rtnl_link_register(&geneve_link_ops
);
1418 unregister_pernet_subsys(&geneve_net_ops
);
1422 late_initcall(geneve_init_module
);
1424 static void __exit
geneve_cleanup_module(void)
1426 rtnl_link_unregister(&geneve_link_ops
);
1427 unregister_pernet_subsys(&geneve_net_ops
);
1429 module_exit(geneve_cleanup_module
);
1431 MODULE_LICENSE("GPL");
1432 MODULE_VERSION(GENEVE_NETDEV_VER
);
1433 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1434 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1435 MODULE_ALIAS_RTNL_LINK("geneve");