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 */
71 __be32 label
; /* IPv6 flowlabel override */
74 struct gro_cells gro_cells
;
76 struct dst_cache dst_cache
;
79 /* Geneve device flags */
80 #define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
81 #define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
82 #define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
86 struct list_head list
;
90 struct udp_offload udp_offloads
;
91 struct hlist_head vni_list
[VNI_HASH_SIZE
];
95 static inline __u32
geneve_net_vni_hash(u8 vni
[3])
99 vnid
= (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
100 return hash_32(vnid
, VNI_HASH_BITS
);
103 static __be64
vni_to_tunnel_id(const __u8
*vni
)
106 return (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
108 return (__force __be64
)(((__force u64
)vni
[0] << 40) |
109 ((__force u64
)vni
[1] << 48) |
110 ((__force u64
)vni
[2] << 56));
114 static sa_family_t
geneve_get_sk_family(struct geneve_sock
*gs
)
116 return gs
->sock
->sk
->sk_family
;
119 static struct geneve_dev
*geneve_lookup(struct geneve_sock
*gs
,
120 __be32 addr
, u8 vni
[])
122 struct hlist_head
*vni_list_head
;
123 struct geneve_dev
*geneve
;
126 /* Find the device for this VNI */
127 hash
= geneve_net_vni_hash(vni
);
128 vni_list_head
= &gs
->vni_list
[hash
];
129 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
130 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
131 addr
== geneve
->remote
.sin
.sin_addr
.s_addr
)
137 #if IS_ENABLED(CONFIG_IPV6)
138 static struct geneve_dev
*geneve6_lookup(struct geneve_sock
*gs
,
139 struct in6_addr addr6
, u8 vni
[])
141 struct hlist_head
*vni_list_head
;
142 struct geneve_dev
*geneve
;
145 /* Find the device for this VNI */
146 hash
= geneve_net_vni_hash(vni
);
147 vni_list_head
= &gs
->vni_list
[hash
];
148 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
149 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
150 ipv6_addr_equal(&addr6
, &geneve
->remote
.sin6
.sin6_addr
))
157 static inline struct genevehdr
*geneve_hdr(const struct sk_buff
*skb
)
159 return (struct genevehdr
*)(udp_hdr(skb
) + 1);
162 static struct geneve_dev
*geneve_lookup_skb(struct geneve_sock
*gs
,
167 static u8 zero_vni
[3];
168 #if IS_ENABLED(CONFIG_IPV6)
169 static struct in6_addr zero_addr6
;
172 if (geneve_get_sk_family(gs
) == AF_INET
) {
175 iph
= ip_hdr(skb
); /* outer IP header... */
177 if (gs
->collect_md
) {
181 vni
= geneve_hdr(skb
)->vni
;
185 return geneve_lookup(gs
, addr
, vni
);
186 #if IS_ENABLED(CONFIG_IPV6)
187 } else if (geneve_get_sk_family(gs
) == AF_INET6
) {
188 struct ipv6hdr
*ip6h
;
189 struct in6_addr addr6
;
191 ip6h
= ipv6_hdr(skb
); /* outer IPv6 header... */
193 if (gs
->collect_md
) {
197 vni
= geneve_hdr(skb
)->vni
;
201 return geneve6_lookup(gs
, addr6
, vni
);
207 /* geneve receive/decap routine */
208 static void geneve_rx(struct geneve_dev
*geneve
, struct geneve_sock
*gs
,
211 struct genevehdr
*gnvh
= geneve_hdr(skb
);
212 struct metadata_dst
*tun_dst
= NULL
;
213 struct pcpu_sw_netstats
*stats
;
217 if (ip_tunnel_collect_metadata() || gs
->collect_md
) {
220 flags
= TUNNEL_KEY
| TUNNEL_GENEVE_OPT
|
221 (gnvh
->oam
? TUNNEL_OAM
: 0) |
222 (gnvh
->critical
? TUNNEL_CRIT_OPT
: 0);
224 tun_dst
= udp_tun_rx_dst(skb
, geneve_get_sk_family(gs
), flags
,
225 vni_to_tunnel_id(gnvh
->vni
),
229 /* Update tunnel dst according to Geneve options. */
230 ip_tunnel_info_opts_set(&tun_dst
->u
.tun_info
,
231 gnvh
->options
, gnvh
->opt_len
* 4);
233 /* Drop packets w/ critical options,
234 * since we don't support any...
240 skb_reset_mac_header(skb
);
241 skb
->protocol
= eth_type_trans(skb
, geneve
->dev
);
242 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
245 skb_dst_set(skb
, &tun_dst
->dst
);
247 /* Ignore packet loops (and multicast echo) */
248 if (ether_addr_equal(eth_hdr(skb
)->h_source
, geneve
->dev
->dev_addr
))
251 oiph
= skb_network_header(skb
);
252 skb_reset_network_header(skb
);
254 if (geneve_get_sk_family(gs
) == AF_INET
)
255 err
= IP_ECN_decapsulate(oiph
, skb
);
256 #if IS_ENABLED(CONFIG_IPV6)
258 err
= IP6_ECN_decapsulate(oiph
, skb
);
263 if (geneve_get_sk_family(gs
) == AF_INET
)
264 net_info_ratelimited("non-ECT from %pI4 "
266 &((struct iphdr
*)oiph
)->saddr
,
267 ((struct iphdr
*)oiph
)->tos
);
268 #if IS_ENABLED(CONFIG_IPV6)
270 net_info_ratelimited("non-ECT from %pI6\n",
271 &((struct ipv6hdr
*)oiph
)->saddr
);
275 ++geneve
->dev
->stats
.rx_frame_errors
;
276 ++geneve
->dev
->stats
.rx_errors
;
281 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
282 u64_stats_update_begin(&stats
->syncp
);
284 stats
->rx_bytes
+= skb
->len
;
285 u64_stats_update_end(&stats
->syncp
);
287 gro_cells_receive(&geneve
->gro_cells
, skb
);
290 /* Consume bad packet */
294 /* Setup stats when device is created */
295 static int geneve_init(struct net_device
*dev
)
297 struct geneve_dev
*geneve
= netdev_priv(dev
);
300 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
304 err
= gro_cells_init(&geneve
->gro_cells
, dev
);
306 free_percpu(dev
->tstats
);
310 err
= dst_cache_init(&geneve
->dst_cache
, GFP_KERNEL
);
312 free_percpu(dev
->tstats
);
313 gro_cells_destroy(&geneve
->gro_cells
);
320 static void geneve_uninit(struct net_device
*dev
)
322 struct geneve_dev
*geneve
= netdev_priv(dev
);
324 dst_cache_destroy(&geneve
->dst_cache
);
325 gro_cells_destroy(&geneve
->gro_cells
);
326 free_percpu(dev
->tstats
);
329 /* Callback from net/ipv4/udp.c to receive packets */
330 static int geneve_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
332 struct genevehdr
*geneveh
;
333 struct geneve_dev
*geneve
;
334 struct geneve_sock
*gs
;
337 /* Need Geneve and inner Ethernet header to be present */
338 if (unlikely(!pskb_may_pull(skb
, GENEVE_BASE_HLEN
)))
341 /* Return packets with reserved bits set */
342 geneveh
= geneve_hdr(skb
);
343 if (unlikely(geneveh
->ver
!= GENEVE_VER
))
346 if (unlikely(geneveh
->proto_type
!= htons(ETH_P_TEB
)))
349 gs
= rcu_dereference_sk_user_data(sk
);
353 geneve
= geneve_lookup_skb(gs
, skb
);
357 opts_len
= geneveh
->opt_len
* 4;
358 if (iptunnel_pull_header(skb
, GENEVE_BASE_HLEN
+ opts_len
,
360 !net_eq(geneve
->net
, dev_net(geneve
->dev
))))
363 geneve_rx(geneve
, gs
, skb
);
367 /* Consume bad packet */
372 /* Let the UDP layer deal with the skb */
376 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
377 __be16 port
, u32 flags
)
380 struct udp_port_cfg udp_conf
;
383 memset(&udp_conf
, 0, sizeof(udp_conf
));
386 udp_conf
.family
= AF_INET6
;
387 udp_conf
.ipv6_v6only
= 1;
388 udp_conf
.use_udp6_rx_checksums
=
389 !(flags
& GENEVE_F_UDP_ZERO_CSUM6_RX
);
391 udp_conf
.family
= AF_INET
;
392 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
395 udp_conf
.local_udp_port
= port
;
397 /* Open UDP socket */
398 err
= udp_sock_create(net
, &udp_conf
, &sock
);
405 static void geneve_notify_add_rx_port(struct geneve_sock
*gs
)
407 struct net_device
*dev
;
408 struct sock
*sk
= gs
->sock
->sk
;
409 struct net
*net
= sock_net(sk
);
410 sa_family_t sa_family
= geneve_get_sk_family(gs
);
411 __be16 port
= inet_sk(sk
)->inet_sport
;
414 if (sa_family
== AF_INET
) {
415 err
= udp_add_offload(sock_net(sk
), &gs
->udp_offloads
);
417 pr_warn("geneve: udp_add_offload failed with status %d\n",
422 for_each_netdev_rcu(net
, dev
) {
423 if (dev
->netdev_ops
->ndo_add_geneve_port
)
424 dev
->netdev_ops
->ndo_add_geneve_port(dev
, sa_family
,
430 static int geneve_hlen(struct genevehdr
*gh
)
432 return sizeof(*gh
) + gh
->opt_len
* 4;
435 static struct sk_buff
**geneve_gro_receive(struct sk_buff
**head
,
437 struct udp_offload
*uoff
)
439 struct sk_buff
*p
, **pp
= NULL
;
440 struct genevehdr
*gh
, *gh2
;
441 unsigned int hlen
, gh_len
, off_gnv
;
442 const struct packet_offload
*ptype
;
446 off_gnv
= skb_gro_offset(skb
);
447 hlen
= off_gnv
+ sizeof(*gh
);
448 gh
= skb_gro_header_fast(skb
, off_gnv
);
449 if (skb_gro_header_hard(skb
, hlen
)) {
450 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
455 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
457 gh_len
= geneve_hlen(gh
);
459 hlen
= off_gnv
+ gh_len
;
460 if (skb_gro_header_hard(skb
, hlen
)) {
461 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
466 for (p
= *head
; p
; p
= p
->next
) {
467 if (!NAPI_GRO_CB(p
)->same_flow
)
470 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
471 if (gh
->opt_len
!= gh2
->opt_len
||
472 memcmp(gh
, gh2
, gh_len
)) {
473 NAPI_GRO_CB(p
)->same_flow
= 0;
478 type
= gh
->proto_type
;
481 ptype
= gro_find_receive_by_type(type
);
485 skb_gro_pull(skb
, gh_len
);
486 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
487 pp
= ptype
->callbacks
.gro_receive(head
, skb
);
493 NAPI_GRO_CB(skb
)->flush
|= flush
;
498 static int geneve_gro_complete(struct sk_buff
*skb
, int nhoff
,
499 struct udp_offload
*uoff
)
501 struct genevehdr
*gh
;
502 struct packet_offload
*ptype
;
507 udp_tunnel_gro_complete(skb
, nhoff
);
509 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
510 gh_len
= geneve_hlen(gh
);
511 type
= gh
->proto_type
;
514 ptype
= gro_find_complete_by_type(type
);
516 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
522 /* Create new listen socket if needed */
523 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
524 bool ipv6
, u32 flags
)
526 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
527 struct geneve_sock
*gs
;
529 struct udp_tunnel_sock_cfg tunnel_cfg
;
532 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
534 return ERR_PTR(-ENOMEM
);
536 sock
= geneve_create_sock(net
, ipv6
, port
, flags
);
539 return ERR_CAST(sock
);
544 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
545 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
547 /* Initialize the geneve udp offloads structure */
548 gs
->udp_offloads
.port
= port
;
549 gs
->udp_offloads
.callbacks
.gro_receive
= geneve_gro_receive
;
550 gs
->udp_offloads
.callbacks
.gro_complete
= geneve_gro_complete
;
551 geneve_notify_add_rx_port(gs
);
553 /* Mark socket as an encapsulation socket */
554 tunnel_cfg
.sk_user_data
= gs
;
555 tunnel_cfg
.encap_type
= 1;
556 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
557 tunnel_cfg
.encap_destroy
= NULL
;
558 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
559 list_add(&gs
->list
, &gn
->sock_list
);
563 static void geneve_notify_del_rx_port(struct geneve_sock
*gs
)
565 struct net_device
*dev
;
566 struct sock
*sk
= gs
->sock
->sk
;
567 struct net
*net
= sock_net(sk
);
568 sa_family_t sa_family
= geneve_get_sk_family(gs
);
569 __be16 port
= inet_sk(sk
)->inet_sport
;
572 for_each_netdev_rcu(net
, dev
) {
573 if (dev
->netdev_ops
->ndo_del_geneve_port
)
574 dev
->netdev_ops
->ndo_del_geneve_port(dev
, sa_family
,
580 if (sa_family
== AF_INET
)
581 udp_del_offload(&gs
->udp_offloads
);
584 static void __geneve_sock_release(struct geneve_sock
*gs
)
586 if (!gs
|| --gs
->refcnt
)
590 geneve_notify_del_rx_port(gs
);
591 udp_tunnel_sock_release(gs
->sock
);
595 static void geneve_sock_release(struct geneve_dev
*geneve
)
597 __geneve_sock_release(geneve
->sock4
);
598 #if IS_ENABLED(CONFIG_IPV6)
599 __geneve_sock_release(geneve
->sock6
);
603 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
607 struct geneve_sock
*gs
;
609 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
610 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
611 geneve_get_sk_family(gs
) == family
) {
618 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
620 struct net
*net
= geneve
->net
;
621 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
622 struct geneve_sock
*gs
;
625 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->dst_port
);
631 gs
= geneve_socket_create(net
, geneve
->dst_port
, ipv6
, geneve
->flags
);
636 gs
->collect_md
= geneve
->collect_md
;
637 gs
->flags
= geneve
->flags
;
638 #if IS_ENABLED(CONFIG_IPV6)
645 hash
= geneve_net_vni_hash(geneve
->vni
);
646 hlist_add_head_rcu(&geneve
->hlist
, &gs
->vni_list
[hash
]);
650 static int geneve_open(struct net_device
*dev
)
652 struct geneve_dev
*geneve
= netdev_priv(dev
);
653 bool ipv6
= geneve
->remote
.sa
.sa_family
== AF_INET6
;
654 bool metadata
= geneve
->collect_md
;
657 geneve
->sock4
= NULL
;
658 #if IS_ENABLED(CONFIG_IPV6)
659 geneve
->sock6
= NULL
;
660 if (ipv6
|| metadata
)
661 ret
= geneve_sock_add(geneve
, true);
663 if (!ret
&& (!ipv6
|| metadata
))
664 ret
= geneve_sock_add(geneve
, false);
666 geneve_sock_release(geneve
);
671 static int geneve_stop(struct net_device
*dev
)
673 struct geneve_dev
*geneve
= netdev_priv(dev
);
675 if (!hlist_unhashed(&geneve
->hlist
))
676 hlist_del_rcu(&geneve
->hlist
);
677 geneve_sock_release(geneve
);
681 static void geneve_build_header(struct genevehdr
*geneveh
,
682 __be16 tun_flags
, u8 vni
[3],
683 u8 options_len
, u8
*options
)
685 geneveh
->ver
= GENEVE_VER
;
686 geneveh
->opt_len
= options_len
/ 4;
687 geneveh
->oam
= !!(tun_flags
& TUNNEL_OAM
);
688 geneveh
->critical
= !!(tun_flags
& TUNNEL_CRIT_OPT
);
690 memcpy(geneveh
->vni
, vni
, 3);
691 geneveh
->proto_type
= htons(ETH_P_TEB
);
694 memcpy(geneveh
->options
, options
, options_len
);
697 static int geneve_build_skb(struct rtable
*rt
, struct sk_buff
*skb
,
698 __be16 tun_flags
, u8 vni
[3], u8 opt_len
, u8
*opt
,
699 u32 flags
, bool xnet
)
701 struct genevehdr
*gnvh
;
704 bool udp_sum
= !(flags
& GENEVE_F_UDP_ZERO_CSUM_TX
);
706 skb_scrub_packet(skb
, xnet
);
708 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
709 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct iphdr
);
710 err
= skb_cow_head(skb
, min_headroom
);
716 skb
= udp_tunnel_handle_offloads(skb
, udp_sum
);
722 gnvh
= (struct genevehdr
*)__skb_push(skb
, sizeof(*gnvh
) + opt_len
);
723 geneve_build_header(gnvh
, tun_flags
, vni
, opt_len
, opt
);
725 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
733 #if IS_ENABLED(CONFIG_IPV6)
734 static int geneve6_build_skb(struct dst_entry
*dst
, struct sk_buff
*skb
,
735 __be16 tun_flags
, u8 vni
[3], u8 opt_len
, u8
*opt
,
736 u32 flags
, bool xnet
)
738 struct genevehdr
*gnvh
;
741 bool udp_sum
= !(flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
);
743 skb_scrub_packet(skb
, xnet
);
745 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
746 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct ipv6hdr
);
747 err
= skb_cow_head(skb
, min_headroom
);
753 skb
= udp_tunnel_handle_offloads(skb
, udp_sum
);
759 gnvh
= (struct genevehdr
*)__skb_push(skb
, sizeof(*gnvh
) + opt_len
);
760 geneve_build_header(gnvh
, tun_flags
, vni
, opt_len
, opt
);
762 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
771 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
772 struct net_device
*dev
,
774 struct ip_tunnel_info
*info
)
776 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
777 struct geneve_dev
*geneve
= netdev_priv(dev
);
778 struct dst_cache
*dst_cache
;
779 struct rtable
*rt
= NULL
;
782 memset(fl4
, 0, sizeof(*fl4
));
783 fl4
->flowi4_mark
= skb
->mark
;
784 fl4
->flowi4_proto
= IPPROTO_UDP
;
787 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
788 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
789 fl4
->flowi4_tos
= RT_TOS(info
->key
.tos
);
790 dst_cache
= &info
->dst_cache
;
794 const struct iphdr
*iip
= ip_hdr(skb
);
796 tos
= ip_tunnel_get_dsfield(iip
, skb
);
800 fl4
->flowi4_tos
= RT_TOS(tos
);
801 fl4
->daddr
= geneve
->remote
.sin
.sin_addr
.s_addr
;
802 dst_cache
= &geneve
->dst_cache
;
806 rt
= dst_cache_get_ip4(dst_cache
, &fl4
->saddr
);
811 rt
= ip_route_output_key(geneve
->net
, fl4
);
813 netdev_dbg(dev
, "no route to %pI4\n", &fl4
->daddr
);
814 return ERR_PTR(-ENETUNREACH
);
816 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
817 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
->daddr
);
819 return ERR_PTR(-ELOOP
);
822 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
->saddr
);
826 #if IS_ENABLED(CONFIG_IPV6)
827 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
828 struct net_device
*dev
,
830 struct ip_tunnel_info
*info
)
832 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
833 struct geneve_dev
*geneve
= netdev_priv(dev
);
834 struct geneve_sock
*gs6
= geneve
->sock6
;
835 struct dst_entry
*dst
= NULL
;
836 struct dst_cache
*dst_cache
;
839 memset(fl6
, 0, sizeof(*fl6
));
840 fl6
->flowi6_mark
= skb
->mark
;
841 fl6
->flowi6_proto
= IPPROTO_UDP
;
844 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
845 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
846 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(info
->key
.tos
),
848 dst_cache
= &info
->dst_cache
;
852 const struct iphdr
*iip
= ip_hdr(skb
);
854 prio
= ip_tunnel_get_dsfield(iip
, skb
);
858 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(prio
),
860 fl6
->daddr
= geneve
->remote
.sin6
.sin6_addr
;
861 dst_cache
= &geneve
->dst_cache
;
865 dst
= dst_cache_get_ip6(dst_cache
, &fl6
->saddr
);
870 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
871 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
872 return ERR_PTR(-ENETUNREACH
);
874 if (dst
->dev
== dev
) { /* is this necessary? */
875 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
877 return ERR_PTR(-ELOOP
);
881 dst_cache_set_ip6(dst_cache
, dst
, &fl6
->saddr
);
886 /* Convert 64 bit tunnel ID to 24 bit VNI. */
887 static void tunnel_id_to_vni(__be64 tun_id
, __u8
*vni
)
890 vni
[0] = (__force __u8
)(tun_id
>> 16);
891 vni
[1] = (__force __u8
)(tun_id
>> 8);
892 vni
[2] = (__force __u8
)tun_id
;
894 vni
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
895 vni
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
896 vni
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
900 static netdev_tx_t
geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
901 struct ip_tunnel_info
*info
)
903 struct geneve_dev
*geneve
= netdev_priv(dev
);
904 struct geneve_sock
*gs4
= geneve
->sock4
;
905 struct rtable
*rt
= NULL
;
906 const struct iphdr
*iip
; /* interior IP header */
912 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
913 u32 flags
= geneve
->flags
;
915 if (geneve
->collect_md
) {
916 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
917 netdev_dbg(dev
, "no tunnel metadata\n");
920 if (info
&& ip_tunnel_info_af(info
) != AF_INET
)
924 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
930 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
931 skb_reset_mac_header(skb
);
936 const struct ip_tunnel_key
*key
= &info
->key
;
940 tunnel_id_to_vni(key
->tun_id
, vni
);
941 if (info
->options_len
)
942 opts
= ip_tunnel_info_opts(info
);
944 if (key
->tun_flags
& TUNNEL_CSUM
)
945 flags
&= ~GENEVE_F_UDP_ZERO_CSUM_TX
;
947 flags
|= GENEVE_F_UDP_ZERO_CSUM_TX
;
949 err
= geneve_build_skb(rt
, skb
, key
->tun_flags
, vni
,
950 info
->options_len
, opts
, flags
, xnet
);
954 tos
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
956 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
958 err
= geneve_build_skb(rt
, skb
, 0, geneve
->vni
,
959 0, NULL
, flags
, xnet
);
963 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, iip
, skb
);
965 if (!ttl
&& IN_MULTICAST(ntohl(fl4
.daddr
)))
967 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
970 udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
971 tos
, ttl
, df
, sport
, geneve
->dst_port
,
972 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
973 !!(flags
& GENEVE_F_UDP_ZERO_CSUM_TX
));
981 dev
->stats
.collisions
++;
982 else if (err
== -ENETUNREACH
)
983 dev
->stats
.tx_carrier_errors
++;
985 dev
->stats
.tx_errors
++;
989 #if IS_ENABLED(CONFIG_IPV6)
990 static netdev_tx_t
geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
991 struct ip_tunnel_info
*info
)
993 struct geneve_dev
*geneve
= netdev_priv(dev
);
994 struct geneve_sock
*gs6
= geneve
->sock6
;
995 struct dst_entry
*dst
= NULL
;
996 const struct iphdr
*iip
; /* interior IP header */
1002 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
1003 u32 flags
= geneve
->flags
;
1005 if (geneve
->collect_md
) {
1006 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
1007 netdev_dbg(dev
, "no tunnel metadata\n");
1012 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
1018 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
1019 skb_reset_mac_header(skb
);
1024 const struct ip_tunnel_key
*key
= &info
->key
;
1028 tunnel_id_to_vni(key
->tun_id
, vni
);
1029 if (info
->options_len
)
1030 opts
= ip_tunnel_info_opts(info
);
1032 if (key
->tun_flags
& TUNNEL_CSUM
)
1033 flags
&= ~GENEVE_F_UDP_ZERO_CSUM6_TX
;
1035 flags
|= GENEVE_F_UDP_ZERO_CSUM6_TX
;
1037 err
= geneve6_build_skb(dst
, skb
, key
->tun_flags
, vni
,
1038 info
->options_len
, opts
,
1043 prio
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
1045 label
= info
->key
.label
;
1047 err
= geneve6_build_skb(dst
, skb
, 0, geneve
->vni
,
1048 0, NULL
, flags
, xnet
);
1052 prio
= ip_tunnel_ecn_encap(ip6_tclass(fl6
.flowlabel
),
1055 if (!ttl
&& ipv6_addr_is_multicast(&fl6
.daddr
))
1057 ttl
= ttl
? : ip6_dst_hoplimit(dst
);
1058 label
= geneve
->label
;
1061 udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
1062 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
, label
,
1063 sport
, geneve
->dst_port
,
1064 !!(flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
));
1065 return NETDEV_TX_OK
;
1071 dev
->stats
.collisions
++;
1072 else if (err
== -ENETUNREACH
)
1073 dev
->stats
.tx_carrier_errors
++;
1075 dev
->stats
.tx_errors
++;
1076 return NETDEV_TX_OK
;
1080 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1082 struct geneve_dev
*geneve
= netdev_priv(dev
);
1083 struct ip_tunnel_info
*info
= NULL
;
1085 if (geneve
->collect_md
)
1086 info
= skb_tunnel_info(skb
);
1088 #if IS_ENABLED(CONFIG_IPV6)
1089 if ((info
&& ip_tunnel_info_af(info
) == AF_INET6
) ||
1090 (!info
&& geneve
->remote
.sa
.sa_family
== AF_INET6
))
1091 return geneve6_xmit_skb(skb
, dev
, info
);
1093 return geneve_xmit_skb(skb
, dev
, info
);
1096 static int __geneve_change_mtu(struct net_device
*dev
, int new_mtu
, bool strict
)
1098 /* The max_mtu calculation does not take account of GENEVE
1099 * options, to avoid excluding potentially valid
1102 int max_mtu
= IP_MAX_MTU
- GENEVE_BASE_HLEN
- sizeof(struct iphdr
)
1103 - dev
->hard_header_len
;
1108 if (new_mtu
> max_mtu
) {
1119 static int geneve_change_mtu(struct net_device
*dev
, int new_mtu
)
1121 return __geneve_change_mtu(dev
, new_mtu
, true);
1124 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
1126 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
1127 struct geneve_dev
*geneve
= netdev_priv(dev
);
1130 #if IS_ENABLED(CONFIG_IPV6)
1131 struct dst_entry
*dst
;
1135 if (ip_tunnel_info_af(info
) == AF_INET
) {
1136 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
1141 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
1142 #if IS_ENABLED(CONFIG_IPV6)
1143 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
1144 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
1146 return PTR_ERR(dst
);
1149 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
1155 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
1156 1, USHRT_MAX
, true);
1157 info
->key
.tp_dst
= geneve
->dst_port
;
1161 static const struct net_device_ops geneve_netdev_ops
= {
1162 .ndo_init
= geneve_init
,
1163 .ndo_uninit
= geneve_uninit
,
1164 .ndo_open
= geneve_open
,
1165 .ndo_stop
= geneve_stop
,
1166 .ndo_start_xmit
= geneve_xmit
,
1167 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1168 .ndo_change_mtu
= geneve_change_mtu
,
1169 .ndo_validate_addr
= eth_validate_addr
,
1170 .ndo_set_mac_address
= eth_mac_addr
,
1171 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1174 static void geneve_get_drvinfo(struct net_device
*dev
,
1175 struct ethtool_drvinfo
*drvinfo
)
1177 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1178 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1181 static const struct ethtool_ops geneve_ethtool_ops
= {
1182 .get_drvinfo
= geneve_get_drvinfo
,
1183 .get_link
= ethtool_op_get_link
,
1186 /* Info for udev, that this is a virtual tunnel endpoint */
1187 static struct device_type geneve_type
= {
1191 /* Calls the ndo_add_geneve_port of the caller in order to
1192 * supply the listening GENEVE udp ports. Callers are expected
1193 * to implement the ndo_add_geneve_port.
1195 void geneve_get_rx_port(struct net_device
*dev
)
1197 struct net
*net
= dev_net(dev
);
1198 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1199 struct geneve_sock
*gs
;
1200 sa_family_t sa_family
;
1205 list_for_each_entry_rcu(gs
, &gn
->sock_list
, list
) {
1207 sa_family
= sk
->sk_family
;
1208 port
= inet_sk(sk
)->inet_sport
;
1209 dev
->netdev_ops
->ndo_add_geneve_port(dev
, sa_family
, port
);
1213 EXPORT_SYMBOL_GPL(geneve_get_rx_port
);
1215 /* Initialize the device structure. */
1216 static void geneve_setup(struct net_device
*dev
)
1220 dev
->netdev_ops
= &geneve_netdev_ops
;
1221 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1222 dev
->destructor
= free_netdev
;
1224 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1226 dev
->features
|= NETIF_F_LLTX
;
1227 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1228 dev
->features
|= NETIF_F_RXCSUM
;
1229 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1231 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1232 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1234 netif_keep_dst(dev
);
1235 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1236 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1237 eth_hw_addr_random(dev
);
1240 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1241 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1242 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1243 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1244 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1245 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1246 [IFLA_GENEVE_LABEL
] = { .type
= NLA_U32
},
1247 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1248 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1249 [IFLA_GENEVE_UDP_CSUM
] = { .type
= NLA_U8
},
1250 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
1251 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
1254 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1256 if (tb
[IFLA_ADDRESS
]) {
1257 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1260 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1261 return -EADDRNOTAVAIL
;
1267 if (data
[IFLA_GENEVE_ID
]) {
1268 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1270 if (vni
>= GENEVE_VID_MASK
)
1277 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1279 union geneve_addr
*remote
,
1281 bool *tun_on_same_port
,
1282 bool *tun_collect_md
)
1284 struct geneve_dev
*geneve
, *t
;
1286 *tun_on_same_port
= false;
1287 *tun_collect_md
= false;
1289 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1290 if (geneve
->dst_port
== dst_port
) {
1291 *tun_collect_md
= geneve
->collect_md
;
1292 *tun_on_same_port
= true;
1294 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
1295 !memcmp(remote
, &geneve
->remote
, sizeof(geneve
->remote
)) &&
1296 dst_port
== geneve
->dst_port
)
1302 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1303 union geneve_addr
*remote
,
1304 __u32 vni
, __u8 ttl
, __u8 tos
, __be32 label
,
1305 __be16 dst_port
, bool metadata
, u32 flags
)
1307 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1308 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1309 bool tun_collect_md
, tun_on_same_port
;
1315 (remote
->sa
.sa_family
!= AF_UNSPEC
|| vni
|| tos
|| ttl
|| label
))
1321 geneve
->vni
[0] = (vni
& 0x00ff0000) >> 16;
1322 geneve
->vni
[1] = (vni
& 0x0000ff00) >> 8;
1323 geneve
->vni
[2] = vni
& 0x000000ff;
1325 if ((remote
->sa
.sa_family
== AF_INET
&&
1326 IN_MULTICAST(ntohl(remote
->sin
.sin_addr
.s_addr
))) ||
1327 (remote
->sa
.sa_family
== AF_INET6
&&
1328 ipv6_addr_is_multicast(&remote
->sin6
.sin6_addr
)))
1330 if (label
&& remote
->sa
.sa_family
!= AF_INET6
)
1333 geneve
->remote
= *remote
;
1337 geneve
->label
= label
;
1338 geneve
->dst_port
= dst_port
;
1339 geneve
->collect_md
= metadata
;
1340 geneve
->flags
= flags
;
1342 t
= geneve_find_dev(gn
, dst_port
, remote
, geneve
->vni
,
1343 &tun_on_same_port
, &tun_collect_md
);
1347 /* make enough headroom for basic scenario */
1348 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1349 if (remote
->sa
.sa_family
== AF_INET
)
1350 encap_len
+= sizeof(struct iphdr
);
1352 encap_len
+= sizeof(struct ipv6hdr
);
1353 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1356 if (tun_on_same_port
)
1363 dst_cache_reset(&geneve
->dst_cache
);
1365 err
= register_netdevice(dev
);
1369 list_add(&geneve
->next
, &gn
->geneve_list
);
1373 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1374 struct nlattr
*tb
[], struct nlattr
*data
[])
1376 __be16 dst_port
= htons(GENEVE_UDP_PORT
);
1377 __u8 ttl
= 0, tos
= 0;
1378 bool metadata
= false;
1379 union geneve_addr remote
= geneve_remote_unspec
;
1384 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
])
1387 if (data
[IFLA_GENEVE_REMOTE
]) {
1388 remote
.sa
.sa_family
= AF_INET
;
1389 remote
.sin
.sin_addr
.s_addr
=
1390 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1393 if (data
[IFLA_GENEVE_REMOTE6
]) {
1394 if (!IS_ENABLED(CONFIG_IPV6
))
1395 return -EPFNOSUPPORT
;
1397 remote
.sa
.sa_family
= AF_INET6
;
1398 remote
.sin6
.sin6_addr
=
1399 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1401 if (ipv6_addr_type(&remote
.sin6
.sin6_addr
) &
1402 IPV6_ADDR_LINKLOCAL
) {
1403 netdev_dbg(dev
, "link-local remote is unsupported\n");
1408 if (data
[IFLA_GENEVE_ID
])
1409 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1411 if (data
[IFLA_GENEVE_TTL
])
1412 ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1414 if (data
[IFLA_GENEVE_TOS
])
1415 tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1417 if (data
[IFLA_GENEVE_LABEL
])
1418 label
= nla_get_be32(data
[IFLA_GENEVE_LABEL
]) &
1419 IPV6_FLOWLABEL_MASK
;
1421 if (data
[IFLA_GENEVE_PORT
])
1422 dst_port
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1424 if (data
[IFLA_GENEVE_COLLECT_METADATA
])
1427 if (data
[IFLA_GENEVE_UDP_CSUM
] &&
1428 !nla_get_u8(data
[IFLA_GENEVE_UDP_CSUM
]))
1429 flags
|= GENEVE_F_UDP_ZERO_CSUM_TX
;
1431 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] &&
1432 nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
1433 flags
|= GENEVE_F_UDP_ZERO_CSUM6_TX
;
1435 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] &&
1436 nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
1437 flags
|= GENEVE_F_UDP_ZERO_CSUM6_RX
;
1439 return geneve_configure(net
, dev
, &remote
, vni
, ttl
, tos
, label
,
1440 dst_port
, metadata
, flags
);
1443 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1445 struct geneve_dev
*geneve
= netdev_priv(dev
);
1447 list_del(&geneve
->next
);
1448 unregister_netdevice_queue(dev
, head
);
1451 static size_t geneve_get_size(const struct net_device
*dev
)
1453 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1454 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1455 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1456 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1457 nla_total_size(sizeof(__be32
)) + /* IFLA_GENEVE_LABEL */
1458 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1459 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1460 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_CSUM */
1461 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1462 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1466 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1468 struct geneve_dev
*geneve
= netdev_priv(dev
);
1471 vni
= (geneve
->vni
[0] << 16) | (geneve
->vni
[1] << 8) | geneve
->vni
[2];
1472 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1473 goto nla_put_failure
;
1475 if (geneve
->remote
.sa
.sa_family
== AF_INET
) {
1476 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1477 geneve
->remote
.sin
.sin_addr
.s_addr
))
1478 goto nla_put_failure
;
1479 #if IS_ENABLED(CONFIG_IPV6)
1481 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1482 &geneve
->remote
.sin6
.sin6_addr
))
1483 goto nla_put_failure
;
1487 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, geneve
->ttl
) ||
1488 nla_put_u8(skb
, IFLA_GENEVE_TOS
, geneve
->tos
) ||
1489 nla_put_be32(skb
, IFLA_GENEVE_LABEL
, geneve
->label
))
1490 goto nla_put_failure
;
1492 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, geneve
->dst_port
))
1493 goto nla_put_failure
;
1495 if (geneve
->collect_md
) {
1496 if (nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1497 goto nla_put_failure
;
1500 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_CSUM
,
1501 !(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM_TX
)) ||
1502 nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
1503 !!(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
)) ||
1504 nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
1505 !!(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM6_RX
)))
1506 goto nla_put_failure
;
1514 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1516 .maxtype
= IFLA_GENEVE_MAX
,
1517 .policy
= geneve_policy
,
1518 .priv_size
= sizeof(struct geneve_dev
),
1519 .setup
= geneve_setup
,
1520 .validate
= geneve_validate
,
1521 .newlink
= geneve_newlink
,
1522 .dellink
= geneve_dellink
,
1523 .get_size
= geneve_get_size
,
1524 .fill_info
= geneve_fill_info
,
1527 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1528 u8 name_assign_type
, u16 dst_port
)
1530 struct nlattr
*tb
[IFLA_MAX
+ 1];
1531 struct net_device
*dev
;
1534 memset(tb
, 0, sizeof(tb
));
1535 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1536 &geneve_link_ops
, tb
);
1540 err
= geneve_configure(net
, dev
, &geneve_remote_unspec
,
1541 0, 0, 0, 0, htons(dst_port
), true,
1542 GENEVE_F_UDP_ZERO_CSUM6_RX
);
1546 /* openvswitch users expect packet sizes to be unrestricted,
1547 * so set the largest MTU we can.
1549 err
= __geneve_change_mtu(dev
, IP_MAX_MTU
, false);
1557 return ERR_PTR(err
);
1559 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1561 static __net_init
int geneve_init_net(struct net
*net
)
1563 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1565 INIT_LIST_HEAD(&gn
->geneve_list
);
1566 INIT_LIST_HEAD(&gn
->sock_list
);
1570 static void __net_exit
geneve_exit_net(struct net
*net
)
1572 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1573 struct geneve_dev
*geneve
, *next
;
1574 struct net_device
*dev
, *aux
;
1579 /* gather any geneve devices that were moved into this ns */
1580 for_each_netdev_safe(net
, dev
, aux
)
1581 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1582 unregister_netdevice_queue(dev
, &list
);
1584 /* now gather any other geneve devices that were created in this ns */
1585 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1586 /* If geneve->dev is in the same netns, it was already added
1587 * to the list by the previous loop.
1589 if (!net_eq(dev_net(geneve
->dev
), net
))
1590 unregister_netdevice_queue(geneve
->dev
, &list
);
1593 /* unregister the devices gathered above */
1594 unregister_netdevice_many(&list
);
1598 static struct pernet_operations geneve_net_ops
= {
1599 .init
= geneve_init_net
,
1600 .exit
= geneve_exit_net
,
1601 .id
= &geneve_net_id
,
1602 .size
= sizeof(struct geneve_net
),
1605 static int __init
geneve_init_module(void)
1609 rc
= register_pernet_subsys(&geneve_net_ops
);
1613 rc
= rtnl_link_register(&geneve_link_ops
);
1619 unregister_pernet_subsys(&geneve_net_ops
);
1623 late_initcall(geneve_init_module
);
1625 static void __exit
geneve_cleanup_module(void)
1627 rtnl_link_unregister(&geneve_link_ops
);
1628 unregister_pernet_subsys(&geneve_net_ops
);
1630 module_exit(geneve_cleanup_module
);
1632 MODULE_LICENSE("GPL");
1633 MODULE_VERSION(GENEVE_NETDEV_VER
);
1634 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1635 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1636 MODULE_ALIAS_RTNL_LINK("geneve");