2 * GENEVE: Generic Network Virtualization Encapsulation
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/etherdevice.h>
16 #include <linux/hash.h>
17 #include <net/dst_metadata.h>
18 #include <net/gro_cells.h>
19 #include <net/rtnetlink.h>
20 #include <net/geneve.h>
21 #include <net/protocol.h>
23 #define GENEVE_NETDEV_VER "0.6"
25 #define GENEVE_UDP_PORT 6081
27 #define GENEVE_N_VID (1u << 24)
28 #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
30 #define VNI_HASH_BITS 10
31 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
33 static bool log_ecn_error
= true;
34 module_param(log_ecn_error
, bool, 0644);
35 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
38 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
40 /* per-network namespace private data for this module */
42 struct list_head geneve_list
;
43 struct list_head sock_list
;
46 static int geneve_net_id
;
49 struct sockaddr_in sin
;
50 struct sockaddr_in6 sin6
;
54 static union geneve_addr geneve_remote_unspec
= { .sa
.sa_family
= AF_UNSPEC
, };
56 /* Pseudo network device */
58 struct hlist_node hlist
; /* vni hash table */
59 struct net
*net
; /* netns for packet i/o */
60 struct net_device
*dev
; /* netdev for geneve tunnel */
61 struct geneve_sock
*sock4
; /* IPv4 socket used for geneve tunnel */
62 #if IS_ENABLED(CONFIG_IPV6)
63 struct geneve_sock
*sock6
; /* IPv6 socket used for geneve tunnel */
65 u8 vni
[3]; /* virtual network ID for tunnel */
66 u8 ttl
; /* TTL override */
67 u8 tos
; /* TOS override */
68 union geneve_addr remote
; /* IP address for link partner */
69 struct list_head next
; /* geneve's per namespace list */
70 __be32 label
; /* IPv6 flowlabel override */
73 struct gro_cells gro_cells
;
75 struct dst_cache dst_cache
;
78 /* Geneve device flags */
79 #define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
80 #define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
81 #define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
85 struct list_head list
;
89 struct hlist_head vni_list
[VNI_HASH_SIZE
];
93 static inline __u32
geneve_net_vni_hash(u8 vni
[3])
97 vnid
= (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
98 return hash_32(vnid
, VNI_HASH_BITS
);
101 static __be64
vni_to_tunnel_id(const __u8
*vni
)
104 return (vni
[0] << 16) | (vni
[1] << 8) | vni
[2];
106 return (__force __be64
)(((__force u64
)vni
[0] << 40) |
107 ((__force u64
)vni
[1] << 48) |
108 ((__force u64
)vni
[2] << 56));
112 static sa_family_t
geneve_get_sk_family(struct geneve_sock
*gs
)
114 return gs
->sock
->sk
->sk_family
;
117 static struct geneve_dev
*geneve_lookup(struct geneve_sock
*gs
,
118 __be32 addr
, u8 vni
[])
120 struct hlist_head
*vni_list_head
;
121 struct geneve_dev
*geneve
;
124 /* Find the device for this VNI */
125 hash
= geneve_net_vni_hash(vni
);
126 vni_list_head
= &gs
->vni_list
[hash
];
127 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
128 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
129 addr
== geneve
->remote
.sin
.sin_addr
.s_addr
)
135 #if IS_ENABLED(CONFIG_IPV6)
136 static struct geneve_dev
*geneve6_lookup(struct geneve_sock
*gs
,
137 struct in6_addr addr6
, u8 vni
[])
139 struct hlist_head
*vni_list_head
;
140 struct geneve_dev
*geneve
;
143 /* Find the device for this VNI */
144 hash
= geneve_net_vni_hash(vni
);
145 vni_list_head
= &gs
->vni_list
[hash
];
146 hlist_for_each_entry_rcu(geneve
, vni_list_head
, hlist
) {
147 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
148 ipv6_addr_equal(&addr6
, &geneve
->remote
.sin6
.sin6_addr
))
155 static inline struct genevehdr
*geneve_hdr(const struct sk_buff
*skb
)
157 return (struct genevehdr
*)(udp_hdr(skb
) + 1);
160 static struct geneve_dev
*geneve_lookup_skb(struct geneve_sock
*gs
,
165 static u8 zero_vni
[3];
166 #if IS_ENABLED(CONFIG_IPV6)
167 static struct in6_addr zero_addr6
;
170 if (geneve_get_sk_family(gs
) == AF_INET
) {
173 iph
= ip_hdr(skb
); /* outer IP header... */
175 if (gs
->collect_md
) {
179 vni
= geneve_hdr(skb
)->vni
;
183 return geneve_lookup(gs
, addr
, vni
);
184 #if IS_ENABLED(CONFIG_IPV6)
185 } else if (geneve_get_sk_family(gs
) == AF_INET6
) {
186 struct ipv6hdr
*ip6h
;
187 struct in6_addr addr6
;
189 ip6h
= ipv6_hdr(skb
); /* outer IPv6 header... */
191 if (gs
->collect_md
) {
195 vni
= geneve_hdr(skb
)->vni
;
199 return geneve6_lookup(gs
, addr6
, vni
);
205 /* geneve receive/decap routine */
206 static void geneve_rx(struct geneve_dev
*geneve
, struct geneve_sock
*gs
,
209 struct genevehdr
*gnvh
= geneve_hdr(skb
);
210 struct metadata_dst
*tun_dst
= NULL
;
211 struct pcpu_sw_netstats
*stats
;
215 if (ip_tunnel_collect_metadata() || gs
->collect_md
) {
218 flags
= TUNNEL_KEY
| TUNNEL_GENEVE_OPT
|
219 (gnvh
->oam
? TUNNEL_OAM
: 0) |
220 (gnvh
->critical
? TUNNEL_CRIT_OPT
: 0);
222 tun_dst
= udp_tun_rx_dst(skb
, geneve_get_sk_family(gs
), flags
,
223 vni_to_tunnel_id(gnvh
->vni
),
227 /* Update tunnel dst according to Geneve options. */
228 ip_tunnel_info_opts_set(&tun_dst
->u
.tun_info
,
229 gnvh
->options
, gnvh
->opt_len
* 4);
231 /* Drop packets w/ critical options,
232 * since we don't support any...
238 skb_reset_mac_header(skb
);
239 skb
->protocol
= eth_type_trans(skb
, geneve
->dev
);
240 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
243 skb_dst_set(skb
, &tun_dst
->dst
);
245 /* Ignore packet loops (and multicast echo) */
246 if (ether_addr_equal(eth_hdr(skb
)->h_source
, geneve
->dev
->dev_addr
))
249 oiph
= skb_network_header(skb
);
250 skb_reset_network_header(skb
);
252 if (geneve_get_sk_family(gs
) == AF_INET
)
253 err
= IP_ECN_decapsulate(oiph
, skb
);
254 #if IS_ENABLED(CONFIG_IPV6)
256 err
= IP6_ECN_decapsulate(oiph
, skb
);
261 if (geneve_get_sk_family(gs
) == AF_INET
)
262 net_info_ratelimited("non-ECT from %pI4 "
264 &((struct iphdr
*)oiph
)->saddr
,
265 ((struct iphdr
*)oiph
)->tos
);
266 #if IS_ENABLED(CONFIG_IPV6)
268 net_info_ratelimited("non-ECT from %pI6\n",
269 &((struct ipv6hdr
*)oiph
)->saddr
);
273 ++geneve
->dev
->stats
.rx_frame_errors
;
274 ++geneve
->dev
->stats
.rx_errors
;
279 stats
= this_cpu_ptr(geneve
->dev
->tstats
);
280 u64_stats_update_begin(&stats
->syncp
);
282 stats
->rx_bytes
+= skb
->len
;
283 u64_stats_update_end(&stats
->syncp
);
285 gro_cells_receive(&geneve
->gro_cells
, skb
);
288 /* Consume bad packet */
292 /* Setup stats when device is created */
293 static int geneve_init(struct net_device
*dev
)
295 struct geneve_dev
*geneve
= netdev_priv(dev
);
298 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
302 err
= gro_cells_init(&geneve
->gro_cells
, dev
);
304 free_percpu(dev
->tstats
);
308 err
= dst_cache_init(&geneve
->dst_cache
, GFP_KERNEL
);
310 free_percpu(dev
->tstats
);
311 gro_cells_destroy(&geneve
->gro_cells
);
318 static void geneve_uninit(struct net_device
*dev
)
320 struct geneve_dev
*geneve
= netdev_priv(dev
);
322 dst_cache_destroy(&geneve
->dst_cache
);
323 gro_cells_destroy(&geneve
->gro_cells
);
324 free_percpu(dev
->tstats
);
327 /* Callback from net/ipv4/udp.c to receive packets */
328 static int geneve_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
330 struct genevehdr
*geneveh
;
331 struct geneve_dev
*geneve
;
332 struct geneve_sock
*gs
;
335 /* Need Geneve and inner Ethernet header to be present */
336 if (unlikely(!pskb_may_pull(skb
, GENEVE_BASE_HLEN
)))
339 /* Return packets with reserved bits set */
340 geneveh
= geneve_hdr(skb
);
341 if (unlikely(geneveh
->ver
!= GENEVE_VER
))
344 if (unlikely(geneveh
->proto_type
!= htons(ETH_P_TEB
)))
347 gs
= rcu_dereference_sk_user_data(sk
);
351 geneve
= geneve_lookup_skb(gs
, skb
);
355 opts_len
= geneveh
->opt_len
* 4;
356 if (iptunnel_pull_header(skb
, GENEVE_BASE_HLEN
+ opts_len
,
358 !net_eq(geneve
->net
, dev_net(geneve
->dev
))))
361 geneve_rx(geneve
, gs
, skb
);
365 /* Consume bad packet */
370 static struct socket
*geneve_create_sock(struct net
*net
, bool ipv6
,
371 __be16 port
, u32 flags
)
374 struct udp_port_cfg udp_conf
;
377 memset(&udp_conf
, 0, sizeof(udp_conf
));
380 udp_conf
.family
= AF_INET6
;
381 udp_conf
.ipv6_v6only
= 1;
382 udp_conf
.use_udp6_rx_checksums
=
383 !(flags
& GENEVE_F_UDP_ZERO_CSUM6_RX
);
385 udp_conf
.family
= AF_INET
;
386 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
389 udp_conf
.local_udp_port
= port
;
391 /* Open UDP socket */
392 err
= udp_sock_create(net
, &udp_conf
, &sock
);
399 static int geneve_hlen(struct genevehdr
*gh
)
401 return sizeof(*gh
) + gh
->opt_len
* 4;
404 static struct sk_buff
**geneve_gro_receive(struct sock
*sk
,
405 struct sk_buff
**head
,
408 struct sk_buff
*p
, **pp
= NULL
;
409 struct genevehdr
*gh
, *gh2
;
410 unsigned int hlen
, gh_len
, off_gnv
;
411 const struct packet_offload
*ptype
;
415 off_gnv
= skb_gro_offset(skb
);
416 hlen
= off_gnv
+ sizeof(*gh
);
417 gh
= skb_gro_header_fast(skb
, off_gnv
);
418 if (skb_gro_header_hard(skb
, hlen
)) {
419 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
424 if (gh
->ver
!= GENEVE_VER
|| gh
->oam
)
426 gh_len
= geneve_hlen(gh
);
428 hlen
= off_gnv
+ gh_len
;
429 if (skb_gro_header_hard(skb
, hlen
)) {
430 gh
= skb_gro_header_slow(skb
, hlen
, off_gnv
);
435 for (p
= *head
; p
; p
= p
->next
) {
436 if (!NAPI_GRO_CB(p
)->same_flow
)
439 gh2
= (struct genevehdr
*)(p
->data
+ off_gnv
);
440 if (gh
->opt_len
!= gh2
->opt_len
||
441 memcmp(gh
, gh2
, gh_len
)) {
442 NAPI_GRO_CB(p
)->same_flow
= 0;
447 type
= gh
->proto_type
;
450 ptype
= gro_find_receive_by_type(type
);
454 skb_gro_pull(skb
, gh_len
);
455 skb_gro_postpull_rcsum(skb
, gh
, gh_len
);
456 pp
= ptype
->callbacks
.gro_receive(head
, skb
);
462 NAPI_GRO_CB(skb
)->flush
|= flush
;
467 static int geneve_gro_complete(struct sock
*sk
, struct sk_buff
*skb
,
470 struct genevehdr
*gh
;
471 struct packet_offload
*ptype
;
476 gh
= (struct genevehdr
*)(skb
->data
+ nhoff
);
477 gh_len
= geneve_hlen(gh
);
478 type
= gh
->proto_type
;
481 ptype
= gro_find_complete_by_type(type
);
483 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ gh_len
);
487 skb_set_inner_mac_header(skb
, nhoff
+ gh_len
);
492 /* Create new listen socket if needed */
493 static struct geneve_sock
*geneve_socket_create(struct net
*net
, __be16 port
,
494 bool ipv6
, u32 flags
)
496 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
497 struct geneve_sock
*gs
;
499 struct udp_tunnel_sock_cfg tunnel_cfg
;
502 gs
= kzalloc(sizeof(*gs
), GFP_KERNEL
);
504 return ERR_PTR(-ENOMEM
);
506 sock
= geneve_create_sock(net
, ipv6
, port
, flags
);
509 return ERR_CAST(sock
);
514 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
515 INIT_HLIST_HEAD(&gs
->vni_list
[h
]);
517 /* Initialize the geneve udp offloads structure */
518 udp_tunnel_notify_add_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
520 /* Mark socket as an encapsulation socket */
521 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
522 tunnel_cfg
.sk_user_data
= gs
;
523 tunnel_cfg
.encap_type
= 1;
524 tunnel_cfg
.gro_receive
= geneve_gro_receive
;
525 tunnel_cfg
.gro_complete
= geneve_gro_complete
;
526 tunnel_cfg
.encap_rcv
= geneve_udp_encap_recv
;
527 tunnel_cfg
.encap_destroy
= NULL
;
528 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
529 list_add(&gs
->list
, &gn
->sock_list
);
533 static void __geneve_sock_release(struct geneve_sock
*gs
)
535 if (!gs
|| --gs
->refcnt
)
539 udp_tunnel_notify_del_rx_port(gs
->sock
, UDP_TUNNEL_TYPE_GENEVE
);
540 udp_tunnel_sock_release(gs
->sock
);
544 static void geneve_sock_release(struct geneve_dev
*geneve
)
546 __geneve_sock_release(geneve
->sock4
);
547 #if IS_ENABLED(CONFIG_IPV6)
548 __geneve_sock_release(geneve
->sock6
);
552 static struct geneve_sock
*geneve_find_sock(struct geneve_net
*gn
,
556 struct geneve_sock
*gs
;
558 list_for_each_entry(gs
, &gn
->sock_list
, list
) {
559 if (inet_sk(gs
->sock
->sk
)->inet_sport
== dst_port
&&
560 geneve_get_sk_family(gs
) == family
) {
567 static int geneve_sock_add(struct geneve_dev
*geneve
, bool ipv6
)
569 struct net
*net
= geneve
->net
;
570 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
571 struct geneve_sock
*gs
;
574 gs
= geneve_find_sock(gn
, ipv6
? AF_INET6
: AF_INET
, geneve
->dst_port
);
580 gs
= geneve_socket_create(net
, geneve
->dst_port
, ipv6
, geneve
->flags
);
585 gs
->collect_md
= geneve
->collect_md
;
586 gs
->flags
= geneve
->flags
;
587 #if IS_ENABLED(CONFIG_IPV6)
594 hash
= geneve_net_vni_hash(geneve
->vni
);
595 hlist_add_head_rcu(&geneve
->hlist
, &gs
->vni_list
[hash
]);
599 static int geneve_open(struct net_device
*dev
)
601 struct geneve_dev
*geneve
= netdev_priv(dev
);
602 bool ipv6
= geneve
->remote
.sa
.sa_family
== AF_INET6
;
603 bool metadata
= geneve
->collect_md
;
606 geneve
->sock4
= NULL
;
607 #if IS_ENABLED(CONFIG_IPV6)
608 geneve
->sock6
= NULL
;
609 if (ipv6
|| metadata
)
610 ret
= geneve_sock_add(geneve
, true);
612 if (!ret
&& (!ipv6
|| metadata
))
613 ret
= geneve_sock_add(geneve
, false);
615 geneve_sock_release(geneve
);
620 static int geneve_stop(struct net_device
*dev
)
622 struct geneve_dev
*geneve
= netdev_priv(dev
);
624 if (!hlist_unhashed(&geneve
->hlist
))
625 hlist_del_rcu(&geneve
->hlist
);
626 geneve_sock_release(geneve
);
630 static void geneve_build_header(struct genevehdr
*geneveh
,
631 __be16 tun_flags
, u8 vni
[3],
632 u8 options_len
, u8
*options
)
634 geneveh
->ver
= GENEVE_VER
;
635 geneveh
->opt_len
= options_len
/ 4;
636 geneveh
->oam
= !!(tun_flags
& TUNNEL_OAM
);
637 geneveh
->critical
= !!(tun_flags
& TUNNEL_CRIT_OPT
);
639 memcpy(geneveh
->vni
, vni
, 3);
640 geneveh
->proto_type
= htons(ETH_P_TEB
);
643 memcpy(geneveh
->options
, options
, options_len
);
646 static int geneve_build_skb(struct rtable
*rt
, struct sk_buff
*skb
,
647 __be16 tun_flags
, u8 vni
[3], u8 opt_len
, u8
*opt
,
648 u32 flags
, bool xnet
)
650 struct genevehdr
*gnvh
;
653 bool udp_sum
= !(flags
& GENEVE_F_UDP_ZERO_CSUM_TX
);
655 skb_scrub_packet(skb
, xnet
);
657 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
658 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct iphdr
);
659 err
= skb_cow_head(skb
, min_headroom
);
663 err
= udp_tunnel_handle_offloads(skb
, udp_sum
);
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 u32 flags
, bool xnet
)
683 struct genevehdr
*gnvh
;
686 bool udp_sum
= !(flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
);
688 skb_scrub_packet(skb
, xnet
);
690 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
691 + GENEVE_BASE_HLEN
+ opt_len
+ sizeof(struct ipv6hdr
);
692 err
= skb_cow_head(skb
, min_headroom
);
696 err
= udp_tunnel_handle_offloads(skb
, udp_sum
);
700 gnvh
= (struct genevehdr
*)__skb_push(skb
, sizeof(*gnvh
) + opt_len
);
701 geneve_build_header(gnvh
, tun_flags
, vni
, opt_len
, opt
);
703 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
712 static struct rtable
*geneve_get_v4_rt(struct sk_buff
*skb
,
713 struct net_device
*dev
,
715 struct ip_tunnel_info
*info
)
717 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
718 struct geneve_dev
*geneve
= netdev_priv(dev
);
719 struct dst_cache
*dst_cache
;
720 struct rtable
*rt
= NULL
;
723 memset(fl4
, 0, sizeof(*fl4
));
724 fl4
->flowi4_mark
= skb
->mark
;
725 fl4
->flowi4_proto
= IPPROTO_UDP
;
728 fl4
->daddr
= info
->key
.u
.ipv4
.dst
;
729 fl4
->saddr
= info
->key
.u
.ipv4
.src
;
730 fl4
->flowi4_tos
= RT_TOS(info
->key
.tos
);
731 dst_cache
= &info
->dst_cache
;
735 const struct iphdr
*iip
= ip_hdr(skb
);
737 tos
= ip_tunnel_get_dsfield(iip
, skb
);
741 fl4
->flowi4_tos
= RT_TOS(tos
);
742 fl4
->daddr
= geneve
->remote
.sin
.sin_addr
.s_addr
;
743 dst_cache
= &geneve
->dst_cache
;
747 rt
= dst_cache_get_ip4(dst_cache
, &fl4
->saddr
);
752 rt
= ip_route_output_key(geneve
->net
, fl4
);
754 netdev_dbg(dev
, "no route to %pI4\n", &fl4
->daddr
);
755 return ERR_PTR(-ENETUNREACH
);
757 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
758 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
->daddr
);
760 return ERR_PTR(-ELOOP
);
763 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
->saddr
);
767 #if IS_ENABLED(CONFIG_IPV6)
768 static struct dst_entry
*geneve_get_v6_dst(struct sk_buff
*skb
,
769 struct net_device
*dev
,
771 struct ip_tunnel_info
*info
)
773 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
774 struct geneve_dev
*geneve
= netdev_priv(dev
);
775 struct geneve_sock
*gs6
= geneve
->sock6
;
776 struct dst_entry
*dst
= NULL
;
777 struct dst_cache
*dst_cache
;
780 memset(fl6
, 0, sizeof(*fl6
));
781 fl6
->flowi6_mark
= skb
->mark
;
782 fl6
->flowi6_proto
= IPPROTO_UDP
;
785 fl6
->daddr
= info
->key
.u
.ipv6
.dst
;
786 fl6
->saddr
= info
->key
.u
.ipv6
.src
;
787 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(info
->key
.tos
),
789 dst_cache
= &info
->dst_cache
;
793 const struct iphdr
*iip
= ip_hdr(skb
);
795 prio
= ip_tunnel_get_dsfield(iip
, skb
);
799 fl6
->flowlabel
= ip6_make_flowinfo(RT_TOS(prio
),
801 fl6
->daddr
= geneve
->remote
.sin6
.sin6_addr
;
802 dst_cache
= &geneve
->dst_cache
;
806 dst
= dst_cache_get_ip6(dst_cache
, &fl6
->saddr
);
811 if (ipv6_stub
->ipv6_dst_lookup(geneve
->net
, gs6
->sock
->sk
, &dst
, fl6
)) {
812 netdev_dbg(dev
, "no route to %pI6\n", &fl6
->daddr
);
813 return ERR_PTR(-ENETUNREACH
);
815 if (dst
->dev
== dev
) { /* is this necessary? */
816 netdev_dbg(dev
, "circular route to %pI6\n", &fl6
->daddr
);
818 return ERR_PTR(-ELOOP
);
822 dst_cache_set_ip6(dst_cache
, dst
, &fl6
->saddr
);
827 /* Convert 64 bit tunnel ID to 24 bit VNI. */
828 static void tunnel_id_to_vni(__be64 tun_id
, __u8
*vni
)
831 vni
[0] = (__force __u8
)(tun_id
>> 16);
832 vni
[1] = (__force __u8
)(tun_id
>> 8);
833 vni
[2] = (__force __u8
)tun_id
;
835 vni
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
836 vni
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
837 vni
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
841 static netdev_tx_t
geneve_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
842 struct ip_tunnel_info
*info
)
844 struct geneve_dev
*geneve
= netdev_priv(dev
);
845 struct geneve_sock
*gs4
= geneve
->sock4
;
846 struct rtable
*rt
= NULL
;
847 const struct iphdr
*iip
; /* interior IP header */
853 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
854 u32 flags
= geneve
->flags
;
856 if (geneve
->collect_md
) {
857 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
858 netdev_dbg(dev
, "no tunnel metadata\n");
861 if (info
&& ip_tunnel_info_af(info
) != AF_INET
)
865 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
871 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
872 skb_reset_mac_header(skb
);
877 const struct ip_tunnel_key
*key
= &info
->key
;
881 tunnel_id_to_vni(key
->tun_id
, vni
);
882 if (info
->options_len
)
883 opts
= ip_tunnel_info_opts(info
);
885 if (key
->tun_flags
& TUNNEL_CSUM
)
886 flags
&= ~GENEVE_F_UDP_ZERO_CSUM_TX
;
888 flags
|= GENEVE_F_UDP_ZERO_CSUM_TX
;
890 err
= geneve_build_skb(rt
, skb
, key
->tun_flags
, vni
,
891 info
->options_len
, opts
, flags
, xnet
);
895 tos
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
897 df
= key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
899 err
= geneve_build_skb(rt
, skb
, 0, geneve
->vni
,
900 0, NULL
, flags
, xnet
);
904 tos
= ip_tunnel_ecn_encap(fl4
.flowi4_tos
, iip
, skb
);
906 if (!ttl
&& IN_MULTICAST(ntohl(fl4
.daddr
)))
908 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
911 udp_tunnel_xmit_skb(rt
, gs4
->sock
->sk
, skb
, fl4
.saddr
, fl4
.daddr
,
912 tos
, ttl
, df
, sport
, geneve
->dst_port
,
913 !net_eq(geneve
->net
, dev_net(geneve
->dev
)),
914 !!(flags
& GENEVE_F_UDP_ZERO_CSUM_TX
));
922 dev
->stats
.collisions
++;
923 else if (err
== -ENETUNREACH
)
924 dev
->stats
.tx_carrier_errors
++;
926 dev
->stats
.tx_errors
++;
930 #if IS_ENABLED(CONFIG_IPV6)
931 static netdev_tx_t
geneve6_xmit_skb(struct sk_buff
*skb
, struct net_device
*dev
,
932 struct ip_tunnel_info
*info
)
934 struct geneve_dev
*geneve
= netdev_priv(dev
);
935 struct geneve_sock
*gs6
= geneve
->sock6
;
936 struct dst_entry
*dst
= NULL
;
937 const struct iphdr
*iip
; /* interior IP header */
943 bool xnet
= !net_eq(geneve
->net
, dev_net(geneve
->dev
));
944 u32 flags
= geneve
->flags
;
946 if (geneve
->collect_md
) {
947 if (unlikely(!info
|| !(info
->mode
& IP_TUNNEL_INFO_TX
))) {
948 netdev_dbg(dev
, "no tunnel metadata\n");
953 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
959 sport
= udp_flow_src_port(geneve
->net
, skb
, 1, USHRT_MAX
, true);
960 skb_reset_mac_header(skb
);
965 const struct ip_tunnel_key
*key
= &info
->key
;
969 tunnel_id_to_vni(key
->tun_id
, vni
);
970 if (info
->options_len
)
971 opts
= ip_tunnel_info_opts(info
);
973 if (key
->tun_flags
& TUNNEL_CSUM
)
974 flags
&= ~GENEVE_F_UDP_ZERO_CSUM6_TX
;
976 flags
|= GENEVE_F_UDP_ZERO_CSUM6_TX
;
978 err
= geneve6_build_skb(dst
, skb
, key
->tun_flags
, vni
,
979 info
->options_len
, opts
,
984 prio
= ip_tunnel_ecn_encap(key
->tos
, iip
, skb
);
986 label
= info
->key
.label
;
988 err
= geneve6_build_skb(dst
, skb
, 0, geneve
->vni
,
989 0, NULL
, flags
, xnet
);
993 prio
= ip_tunnel_ecn_encap(ip6_tclass(fl6
.flowlabel
),
996 if (!ttl
&& ipv6_addr_is_multicast(&fl6
.daddr
))
998 ttl
= ttl
? : ip6_dst_hoplimit(dst
);
999 label
= geneve
->label
;
1002 udp_tunnel6_xmit_skb(dst
, gs6
->sock
->sk
, skb
, dev
,
1003 &fl6
.saddr
, &fl6
.daddr
, prio
, ttl
, label
,
1004 sport
, geneve
->dst_port
,
1005 !!(flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
));
1006 return NETDEV_TX_OK
;
1012 dev
->stats
.collisions
++;
1013 else if (err
== -ENETUNREACH
)
1014 dev
->stats
.tx_carrier_errors
++;
1016 dev
->stats
.tx_errors
++;
1017 return NETDEV_TX_OK
;
1021 static netdev_tx_t
geneve_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1023 struct geneve_dev
*geneve
= netdev_priv(dev
);
1024 struct ip_tunnel_info
*info
= NULL
;
1026 if (geneve
->collect_md
)
1027 info
= skb_tunnel_info(skb
);
1029 #if IS_ENABLED(CONFIG_IPV6)
1030 if ((info
&& ip_tunnel_info_af(info
) == AF_INET6
) ||
1031 (!info
&& geneve
->remote
.sa
.sa_family
== AF_INET6
))
1032 return geneve6_xmit_skb(skb
, dev
, info
);
1034 return geneve_xmit_skb(skb
, dev
, info
);
1037 static int __geneve_change_mtu(struct net_device
*dev
, int new_mtu
, bool strict
)
1039 struct geneve_dev
*geneve
= netdev_priv(dev
);
1040 /* The max_mtu calculation does not take account of GENEVE
1041 * options, to avoid excluding potentially valid
1044 int max_mtu
= IP_MAX_MTU
- GENEVE_BASE_HLEN
- dev
->hard_header_len
;
1046 if (geneve
->remote
.sa
.sa_family
== AF_INET6
)
1047 max_mtu
-= sizeof(struct ipv6hdr
);
1049 max_mtu
-= sizeof(struct iphdr
);
1054 if (new_mtu
> max_mtu
) {
1065 static int geneve_change_mtu(struct net_device
*dev
, int new_mtu
)
1067 return __geneve_change_mtu(dev
, new_mtu
, true);
1070 static int geneve_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
1072 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
1073 struct geneve_dev
*geneve
= netdev_priv(dev
);
1076 #if IS_ENABLED(CONFIG_IPV6)
1077 struct dst_entry
*dst
;
1081 if (ip_tunnel_info_af(info
) == AF_INET
) {
1082 rt
= geneve_get_v4_rt(skb
, dev
, &fl4
, info
);
1087 info
->key
.u
.ipv4
.src
= fl4
.saddr
;
1088 #if IS_ENABLED(CONFIG_IPV6)
1089 } else if (ip_tunnel_info_af(info
) == AF_INET6
) {
1090 dst
= geneve_get_v6_dst(skb
, dev
, &fl6
, info
);
1092 return PTR_ERR(dst
);
1095 info
->key
.u
.ipv6
.src
= fl6
.saddr
;
1101 info
->key
.tp_src
= udp_flow_src_port(geneve
->net
, skb
,
1102 1, USHRT_MAX
, true);
1103 info
->key
.tp_dst
= geneve
->dst_port
;
1107 static const struct net_device_ops geneve_netdev_ops
= {
1108 .ndo_init
= geneve_init
,
1109 .ndo_uninit
= geneve_uninit
,
1110 .ndo_open
= geneve_open
,
1111 .ndo_stop
= geneve_stop
,
1112 .ndo_start_xmit
= geneve_xmit
,
1113 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1114 .ndo_change_mtu
= geneve_change_mtu
,
1115 .ndo_validate_addr
= eth_validate_addr
,
1116 .ndo_set_mac_address
= eth_mac_addr
,
1117 .ndo_fill_metadata_dst
= geneve_fill_metadata_dst
,
1120 static void geneve_get_drvinfo(struct net_device
*dev
,
1121 struct ethtool_drvinfo
*drvinfo
)
1123 strlcpy(drvinfo
->version
, GENEVE_NETDEV_VER
, sizeof(drvinfo
->version
));
1124 strlcpy(drvinfo
->driver
, "geneve", sizeof(drvinfo
->driver
));
1127 static const struct ethtool_ops geneve_ethtool_ops
= {
1128 .get_drvinfo
= geneve_get_drvinfo
,
1129 .get_link
= ethtool_op_get_link
,
1132 /* Info for udev, that this is a virtual tunnel endpoint */
1133 static struct device_type geneve_type
= {
1137 /* Calls the ndo_udp_tunnel_add of the caller in order to
1138 * supply the listening GENEVE udp ports. Callers are expected
1139 * to implement the ndo_udp_tunnel_add.
1141 static void geneve_push_rx_ports(struct net_device
*dev
)
1143 struct net
*net
= dev_net(dev
);
1144 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1145 struct geneve_sock
*gs
;
1148 list_for_each_entry_rcu(gs
, &gn
->sock_list
, list
)
1149 udp_tunnel_push_rx_port(dev
, gs
->sock
,
1150 UDP_TUNNEL_TYPE_GENEVE
);
1154 /* Initialize the device structure. */
1155 static void geneve_setup(struct net_device
*dev
)
1159 dev
->netdev_ops
= &geneve_netdev_ops
;
1160 dev
->ethtool_ops
= &geneve_ethtool_ops
;
1161 dev
->destructor
= free_netdev
;
1163 SET_NETDEV_DEVTYPE(dev
, &geneve_type
);
1165 dev
->features
|= NETIF_F_LLTX
;
1166 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1167 dev
->features
|= NETIF_F_RXCSUM
;
1168 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1170 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1171 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1173 netif_keep_dst(dev
);
1174 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1175 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
| IFF_NO_QUEUE
;
1176 eth_hw_addr_random(dev
);
1179 static const struct nla_policy geneve_policy
[IFLA_GENEVE_MAX
+ 1] = {
1180 [IFLA_GENEVE_ID
] = { .type
= NLA_U32
},
1181 [IFLA_GENEVE_REMOTE
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1182 [IFLA_GENEVE_REMOTE6
] = { .len
= sizeof(struct in6_addr
) },
1183 [IFLA_GENEVE_TTL
] = { .type
= NLA_U8
},
1184 [IFLA_GENEVE_TOS
] = { .type
= NLA_U8
},
1185 [IFLA_GENEVE_LABEL
] = { .type
= NLA_U32
},
1186 [IFLA_GENEVE_PORT
] = { .type
= NLA_U16
},
1187 [IFLA_GENEVE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
1188 [IFLA_GENEVE_UDP_CSUM
] = { .type
= NLA_U8
},
1189 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
1190 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
1193 static int geneve_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1195 if (tb
[IFLA_ADDRESS
]) {
1196 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1199 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1200 return -EADDRNOTAVAIL
;
1206 if (data
[IFLA_GENEVE_ID
]) {
1207 __u32 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1209 if (vni
>= GENEVE_VID_MASK
)
1216 static struct geneve_dev
*geneve_find_dev(struct geneve_net
*gn
,
1218 union geneve_addr
*remote
,
1220 bool *tun_on_same_port
,
1221 bool *tun_collect_md
)
1223 struct geneve_dev
*geneve
, *t
;
1225 *tun_on_same_port
= false;
1226 *tun_collect_md
= false;
1228 list_for_each_entry(geneve
, &gn
->geneve_list
, next
) {
1229 if (geneve
->dst_port
== dst_port
) {
1230 *tun_collect_md
= geneve
->collect_md
;
1231 *tun_on_same_port
= true;
1233 if (!memcmp(vni
, geneve
->vni
, sizeof(geneve
->vni
)) &&
1234 !memcmp(remote
, &geneve
->remote
, sizeof(geneve
->remote
)) &&
1235 dst_port
== geneve
->dst_port
)
1241 static int geneve_configure(struct net
*net
, struct net_device
*dev
,
1242 union geneve_addr
*remote
,
1243 __u32 vni
, __u8 ttl
, __u8 tos
, __be32 label
,
1244 __be16 dst_port
, bool metadata
, u32 flags
)
1246 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1247 struct geneve_dev
*t
, *geneve
= netdev_priv(dev
);
1248 bool tun_collect_md
, tun_on_same_port
;
1254 (remote
->sa
.sa_family
!= AF_UNSPEC
|| vni
|| tos
|| ttl
|| label
))
1260 geneve
->vni
[0] = (vni
& 0x00ff0000) >> 16;
1261 geneve
->vni
[1] = (vni
& 0x0000ff00) >> 8;
1262 geneve
->vni
[2] = vni
& 0x000000ff;
1264 if ((remote
->sa
.sa_family
== AF_INET
&&
1265 IN_MULTICAST(ntohl(remote
->sin
.sin_addr
.s_addr
))) ||
1266 (remote
->sa
.sa_family
== AF_INET6
&&
1267 ipv6_addr_is_multicast(&remote
->sin6
.sin6_addr
)))
1269 if (label
&& remote
->sa
.sa_family
!= AF_INET6
)
1272 geneve
->remote
= *remote
;
1276 geneve
->label
= label
;
1277 geneve
->dst_port
= dst_port
;
1278 geneve
->collect_md
= metadata
;
1279 geneve
->flags
= flags
;
1281 t
= geneve_find_dev(gn
, dst_port
, remote
, geneve
->vni
,
1282 &tun_on_same_port
, &tun_collect_md
);
1286 /* make enough headroom for basic scenario */
1287 encap_len
= GENEVE_BASE_HLEN
+ ETH_HLEN
;
1288 if (remote
->sa
.sa_family
== AF_INET
)
1289 encap_len
+= sizeof(struct iphdr
);
1291 encap_len
+= sizeof(struct ipv6hdr
);
1292 dev
->needed_headroom
= encap_len
+ ETH_HLEN
;
1295 if (tun_on_same_port
)
1302 dst_cache_reset(&geneve
->dst_cache
);
1304 err
= register_netdevice(dev
);
1308 list_add(&geneve
->next
, &gn
->geneve_list
);
1312 static int geneve_newlink(struct net
*net
, struct net_device
*dev
,
1313 struct nlattr
*tb
[], struct nlattr
*data
[])
1315 __be16 dst_port
= htons(GENEVE_UDP_PORT
);
1316 __u8 ttl
= 0, tos
= 0;
1317 bool metadata
= false;
1318 union geneve_addr remote
= geneve_remote_unspec
;
1323 if (data
[IFLA_GENEVE_REMOTE
] && data
[IFLA_GENEVE_REMOTE6
])
1326 if (data
[IFLA_GENEVE_REMOTE
]) {
1327 remote
.sa
.sa_family
= AF_INET
;
1328 remote
.sin
.sin_addr
.s_addr
=
1329 nla_get_in_addr(data
[IFLA_GENEVE_REMOTE
]);
1332 if (data
[IFLA_GENEVE_REMOTE6
]) {
1333 if (!IS_ENABLED(CONFIG_IPV6
))
1334 return -EPFNOSUPPORT
;
1336 remote
.sa
.sa_family
= AF_INET6
;
1337 remote
.sin6
.sin6_addr
=
1338 nla_get_in6_addr(data
[IFLA_GENEVE_REMOTE6
]);
1340 if (ipv6_addr_type(&remote
.sin6
.sin6_addr
) &
1341 IPV6_ADDR_LINKLOCAL
) {
1342 netdev_dbg(dev
, "link-local remote is unsupported\n");
1347 if (data
[IFLA_GENEVE_ID
])
1348 vni
= nla_get_u32(data
[IFLA_GENEVE_ID
]);
1350 if (data
[IFLA_GENEVE_TTL
])
1351 ttl
= nla_get_u8(data
[IFLA_GENEVE_TTL
]);
1353 if (data
[IFLA_GENEVE_TOS
])
1354 tos
= nla_get_u8(data
[IFLA_GENEVE_TOS
]);
1356 if (data
[IFLA_GENEVE_LABEL
])
1357 label
= nla_get_be32(data
[IFLA_GENEVE_LABEL
]) &
1358 IPV6_FLOWLABEL_MASK
;
1360 if (data
[IFLA_GENEVE_PORT
])
1361 dst_port
= nla_get_be16(data
[IFLA_GENEVE_PORT
]);
1363 if (data
[IFLA_GENEVE_COLLECT_METADATA
])
1366 if (data
[IFLA_GENEVE_UDP_CSUM
] &&
1367 !nla_get_u8(data
[IFLA_GENEVE_UDP_CSUM
]))
1368 flags
|= GENEVE_F_UDP_ZERO_CSUM_TX
;
1370 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
] &&
1371 nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
1372 flags
|= GENEVE_F_UDP_ZERO_CSUM6_TX
;
1374 if (data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
] &&
1375 nla_get_u8(data
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
1376 flags
|= GENEVE_F_UDP_ZERO_CSUM6_RX
;
1378 return geneve_configure(net
, dev
, &remote
, vni
, ttl
, tos
, label
,
1379 dst_port
, metadata
, flags
);
1382 static void geneve_dellink(struct net_device
*dev
, struct list_head
*head
)
1384 struct geneve_dev
*geneve
= netdev_priv(dev
);
1386 list_del(&geneve
->next
);
1387 unregister_netdevice_queue(dev
, head
);
1390 static size_t geneve_get_size(const struct net_device
*dev
)
1392 return nla_total_size(sizeof(__u32
)) + /* IFLA_GENEVE_ID */
1393 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_GENEVE_REMOTE{6} */
1394 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TTL */
1395 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_TOS */
1396 nla_total_size(sizeof(__be32
)) + /* IFLA_GENEVE_LABEL */
1397 nla_total_size(sizeof(__be16
)) + /* IFLA_GENEVE_PORT */
1398 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
1399 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_CSUM */
1400 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1401 nla_total_size(sizeof(__u8
)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
1405 static int geneve_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1407 struct geneve_dev
*geneve
= netdev_priv(dev
);
1410 vni
= (geneve
->vni
[0] << 16) | (geneve
->vni
[1] << 8) | geneve
->vni
[2];
1411 if (nla_put_u32(skb
, IFLA_GENEVE_ID
, vni
))
1412 goto nla_put_failure
;
1414 if (geneve
->remote
.sa
.sa_family
== AF_INET
) {
1415 if (nla_put_in_addr(skb
, IFLA_GENEVE_REMOTE
,
1416 geneve
->remote
.sin
.sin_addr
.s_addr
))
1417 goto nla_put_failure
;
1418 #if IS_ENABLED(CONFIG_IPV6)
1420 if (nla_put_in6_addr(skb
, IFLA_GENEVE_REMOTE6
,
1421 &geneve
->remote
.sin6
.sin6_addr
))
1422 goto nla_put_failure
;
1426 if (nla_put_u8(skb
, IFLA_GENEVE_TTL
, geneve
->ttl
) ||
1427 nla_put_u8(skb
, IFLA_GENEVE_TOS
, geneve
->tos
) ||
1428 nla_put_be32(skb
, IFLA_GENEVE_LABEL
, geneve
->label
))
1429 goto nla_put_failure
;
1431 if (nla_put_be16(skb
, IFLA_GENEVE_PORT
, geneve
->dst_port
))
1432 goto nla_put_failure
;
1434 if (geneve
->collect_md
) {
1435 if (nla_put_flag(skb
, IFLA_GENEVE_COLLECT_METADATA
))
1436 goto nla_put_failure
;
1439 if (nla_put_u8(skb
, IFLA_GENEVE_UDP_CSUM
,
1440 !(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM_TX
)) ||
1441 nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
1442 !!(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM6_TX
)) ||
1443 nla_put_u8(skb
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
1444 !!(geneve
->flags
& GENEVE_F_UDP_ZERO_CSUM6_RX
)))
1445 goto nla_put_failure
;
1453 static struct rtnl_link_ops geneve_link_ops __read_mostly
= {
1455 .maxtype
= IFLA_GENEVE_MAX
,
1456 .policy
= geneve_policy
,
1457 .priv_size
= sizeof(struct geneve_dev
),
1458 .setup
= geneve_setup
,
1459 .validate
= geneve_validate
,
1460 .newlink
= geneve_newlink
,
1461 .dellink
= geneve_dellink
,
1462 .get_size
= geneve_get_size
,
1463 .fill_info
= geneve_fill_info
,
1466 struct net_device
*geneve_dev_create_fb(struct net
*net
, const char *name
,
1467 u8 name_assign_type
, u16 dst_port
)
1469 struct nlattr
*tb
[IFLA_MAX
+ 1];
1470 struct net_device
*dev
;
1471 LIST_HEAD(list_kill
);
1474 memset(tb
, 0, sizeof(tb
));
1475 dev
= rtnl_create_link(net
, name
, name_assign_type
,
1476 &geneve_link_ops
, tb
);
1480 err
= geneve_configure(net
, dev
, &geneve_remote_unspec
,
1481 0, 0, 0, 0, htons(dst_port
), true,
1482 GENEVE_F_UDP_ZERO_CSUM6_RX
);
1485 return ERR_PTR(err
);
1488 /* openvswitch users expect packet sizes to be unrestricted,
1489 * so set the largest MTU we can.
1491 err
= __geneve_change_mtu(dev
, IP_MAX_MTU
, false);
1495 err
= rtnl_configure_link(dev
, NULL
);
1502 geneve_dellink(dev
, &list_kill
);
1503 unregister_netdevice_many(&list_kill
);
1504 return ERR_PTR(err
);
1506 EXPORT_SYMBOL_GPL(geneve_dev_create_fb
);
1508 static int geneve_netdevice_event(struct notifier_block
*unused
,
1509 unsigned long event
, void *ptr
)
1511 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1513 if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
1514 geneve_push_rx_ports(dev
);
1519 static struct notifier_block geneve_notifier_block __read_mostly
= {
1520 .notifier_call
= geneve_netdevice_event
,
1523 static __net_init
int geneve_init_net(struct net
*net
)
1525 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1527 INIT_LIST_HEAD(&gn
->geneve_list
);
1528 INIT_LIST_HEAD(&gn
->sock_list
);
1532 static void __net_exit
geneve_exit_net(struct net
*net
)
1534 struct geneve_net
*gn
= net_generic(net
, geneve_net_id
);
1535 struct geneve_dev
*geneve
, *next
;
1536 struct net_device
*dev
, *aux
;
1541 /* gather any geneve devices that were moved into this ns */
1542 for_each_netdev_safe(net
, dev
, aux
)
1543 if (dev
->rtnl_link_ops
== &geneve_link_ops
)
1544 unregister_netdevice_queue(dev
, &list
);
1546 /* now gather any other geneve devices that were created in this ns */
1547 list_for_each_entry_safe(geneve
, next
, &gn
->geneve_list
, next
) {
1548 /* If geneve->dev is in the same netns, it was already added
1549 * to the list by the previous loop.
1551 if (!net_eq(dev_net(geneve
->dev
), net
))
1552 unregister_netdevice_queue(geneve
->dev
, &list
);
1555 /* unregister the devices gathered above */
1556 unregister_netdevice_many(&list
);
1560 static struct pernet_operations geneve_net_ops
= {
1561 .init
= geneve_init_net
,
1562 .exit
= geneve_exit_net
,
1563 .id
= &geneve_net_id
,
1564 .size
= sizeof(struct geneve_net
),
1567 static int __init
geneve_init_module(void)
1571 rc
= register_pernet_subsys(&geneve_net_ops
);
1575 rc
= register_netdevice_notifier(&geneve_notifier_block
);
1579 rc
= rtnl_link_register(&geneve_link_ops
);
1586 unregister_netdevice_notifier(&geneve_notifier_block
);
1588 unregister_pernet_subsys(&geneve_net_ops
);
1592 late_initcall(geneve_init_module
);
1594 static void __exit
geneve_cleanup_module(void)
1596 rtnl_link_unregister(&geneve_link_ops
);
1597 unregister_netdevice_notifier(&geneve_notifier_block
);
1598 unregister_pernet_subsys(&geneve_net_ops
);
1600 module_exit(geneve_cleanup_module
);
1602 MODULE_LICENSE("GPL");
1603 MODULE_VERSION(GENEVE_NETDEV_VER
);
1604 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1605 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1606 MODULE_ALIAS_RTNL_LINK("geneve");