2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012-2013 Vyatta 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/errno.h>
16 #include <linux/slab.h>
17 #include <linux/udp.h>
18 #include <linux/igmp.h>
19 #include <linux/if_ether.h>
20 #include <linux/ethtool.h>
22 #include <net/ndisc.h>
25 #include <net/rtnetlink.h>
26 #include <net/inet_ecn.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
29 #include <net/vxlan.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_tunnel.h>
33 #include <net/ip6_checksum.h>
36 #define VXLAN_VERSION "0.1"
38 #define PORT_HASH_BITS 8
39 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
40 #define FDB_AGE_DEFAULT 300 /* 5 min */
41 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43 /* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
45 * for compatibility with early adopters.
47 static unsigned short vxlan_port __read_mostly
= 8472;
48 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
49 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
51 static bool log_ecn_error
= true;
52 module_param(log_ecn_error
, bool, 0644);
53 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
55 static int vxlan_net_id
;
56 static struct rtnl_link_ops vxlan_link_ops
;
58 static const u8 all_zeros_mac
[ETH_ALEN
+ 2];
60 static int vxlan_sock_add(struct vxlan_dev
*vxlan
);
62 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
);
64 /* per-network namespace private data for this module */
66 struct list_head vxlan_list
;
67 struct hlist_head sock_list
[PORT_HASH_SIZE
];
71 /* Forwarding table entry */
73 struct hlist_node hlist
; /* linked list of entries */
75 unsigned long updated
; /* jiffies */
77 struct list_head remotes
;
78 u8 eth_addr
[ETH_ALEN
];
79 u16 state
; /* see ndm_state */
80 u8 flags
; /* see ndm_flags */
83 /* salt for hash table */
84 static u32 vxlan_salt __read_mostly
;
86 static inline bool vxlan_collect_metadata(struct vxlan_sock
*vs
)
88 return vs
->flags
& VXLAN_F_COLLECT_METADATA
||
89 ip_tunnel_collect_metadata();
92 #if IS_ENABLED(CONFIG_IPV6)
94 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
96 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
98 if (a
->sa
.sa_family
== AF_INET6
)
99 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
101 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
104 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
106 if (ipa
->sa
.sa_family
== AF_INET6
)
107 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
109 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
112 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
114 if (ipa
->sa
.sa_family
== AF_INET6
)
115 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
117 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
120 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
122 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
123 ip
->sin6
.sin6_addr
= nla_get_in6_addr(nla
);
124 ip
->sa
.sa_family
= AF_INET6
;
126 } else if (nla_len(nla
) >= sizeof(__be32
)) {
127 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
128 ip
->sa
.sa_family
= AF_INET
;
131 return -EAFNOSUPPORT
;
135 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
136 const union vxlan_addr
*ip
)
138 if (ip
->sa
.sa_family
== AF_INET6
)
139 return nla_put_in6_addr(skb
, attr
, &ip
->sin6
.sin6_addr
);
141 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
144 #else /* !CONFIG_IPV6 */
147 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
149 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
152 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
154 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
157 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
159 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
162 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
164 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
165 return -EAFNOSUPPORT
;
166 } else if (nla_len(nla
) >= sizeof(__be32
)) {
167 ip
->sin
.sin_addr
.s_addr
= nla_get_in_addr(nla
);
168 ip
->sa
.sa_family
= AF_INET
;
171 return -EAFNOSUPPORT
;
175 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
176 const union vxlan_addr
*ip
)
178 return nla_put_in_addr(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
182 /* Virtual Network hash table head */
183 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, __be32 vni
)
185 return &vs
->vni_list
[hash_32((__force u32
)vni
, VNI_HASH_BITS
)];
188 /* Socket hash table head */
189 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
191 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
193 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
196 /* First remote destination for a forwarding entry.
197 * Guaranteed to be non-NULL because remotes are never deleted.
199 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
201 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
204 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
206 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
209 /* Find VXLAN socket based on network namespace, address family and UDP port
210 * and enabled unshareable flags.
212 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, sa_family_t family
,
213 __be16 port
, u32 flags
)
215 struct vxlan_sock
*vs
;
217 flags
&= VXLAN_F_RCV_FLAGS
;
219 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
220 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
221 vxlan_get_sk_family(vs
) == family
&&
228 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, __be32 vni
)
230 struct vxlan_dev_node
*node
;
232 /* For flow based devices, map all packets to VNI 0 */
233 if (vs
->flags
& VXLAN_F_COLLECT_METADATA
)
236 hlist_for_each_entry_rcu(node
, vni_head(vs
, vni
), hlist
) {
237 if (node
->vxlan
->default_dst
.remote_vni
== vni
)
244 /* Look up VNI in a per net namespace table */
245 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, __be32 vni
,
246 sa_family_t family
, __be16 port
,
249 struct vxlan_sock
*vs
;
251 vs
= vxlan_find_sock(net
, family
, port
, flags
);
255 return vxlan_vs_find_vni(vs
, vni
);
258 /* Fill in neighbour message in skbuff. */
259 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
260 const struct vxlan_fdb
*fdb
,
261 u32 portid
, u32 seq
, int type
, unsigned int flags
,
262 const struct vxlan_rdst
*rdst
)
264 unsigned long now
= jiffies
;
265 struct nda_cacheinfo ci
;
266 struct nlmsghdr
*nlh
;
268 bool send_ip
, send_eth
;
270 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
274 ndm
= nlmsg_data(nlh
);
275 memset(ndm
, 0, sizeof(*ndm
));
277 send_eth
= send_ip
= true;
279 if (type
== RTM_GETNEIGH
) {
280 ndm
->ndm_family
= AF_INET
;
281 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
282 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
284 ndm
->ndm_family
= AF_BRIDGE
;
285 ndm
->ndm_state
= fdb
->state
;
286 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
287 ndm
->ndm_flags
= fdb
->flags
;
288 ndm
->ndm_type
= RTN_UNICAST
;
290 if (!net_eq(dev_net(vxlan
->dev
), vxlan
->net
) &&
291 nla_put_s32(skb
, NDA_LINK_NETNSID
,
292 peernet2id(dev_net(vxlan
->dev
), vxlan
->net
)))
293 goto nla_put_failure
;
295 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
296 goto nla_put_failure
;
298 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
299 goto nla_put_failure
;
301 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->cfg
.dst_port
&&
302 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
303 goto nla_put_failure
;
304 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
305 nla_put_u32(skb
, NDA_VNI
, be32_to_cpu(rdst
->remote_vni
)))
306 goto nla_put_failure
;
307 if (rdst
->remote_ifindex
&&
308 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
309 goto nla_put_failure
;
311 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
312 ci
.ndm_confirmed
= 0;
313 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
316 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
317 goto nla_put_failure
;
323 nlmsg_cancel(skb
, nlh
);
327 static inline size_t vxlan_nlmsg_size(void)
329 return NLMSG_ALIGN(sizeof(struct ndmsg
))
330 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
331 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
332 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
333 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
334 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
335 + nla_total_size(sizeof(__s32
)) /* NDA_LINK_NETNSID */
336 + nla_total_size(sizeof(struct nda_cacheinfo
));
339 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
340 struct vxlan_rdst
*rd
, int type
)
342 struct net
*net
= dev_net(vxlan
->dev
);
346 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
350 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
352 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
353 WARN_ON(err
== -EMSGSIZE
);
358 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
362 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
365 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
367 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
368 struct vxlan_fdb f
= {
371 struct vxlan_rdst remote
= {
372 .remote_ip
= *ipa
, /* goes to NDA_DST */
373 .remote_vni
= cpu_to_be32(VXLAN_N_VID
),
376 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
379 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
381 struct vxlan_fdb f
= {
384 struct vxlan_rdst remote
= { };
386 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
388 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
391 /* Hash Ethernet address */
392 static u32
eth_hash(const unsigned char *addr
)
394 u64 value
= get_unaligned((u64
*)addr
);
396 /* only want 6 bytes */
402 return hash_64(value
, FDB_HASH_BITS
);
405 /* Hash chain to use given mac address */
406 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
409 return &vxlan
->fdb_head
[eth_hash(mac
)];
412 /* Look up Ethernet address in forwarding table */
413 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
416 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
419 hlist_for_each_entry_rcu(f
, head
, hlist
) {
420 if (ether_addr_equal(mac
, f
->eth_addr
))
427 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
432 f
= __vxlan_find_mac(vxlan
, mac
);
439 /* caller should hold vxlan->hash_lock */
440 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
441 union vxlan_addr
*ip
, __be16 port
,
442 __be32 vni
, __u32 ifindex
)
444 struct vxlan_rdst
*rd
;
446 list_for_each_entry(rd
, &f
->remotes
, list
) {
447 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
448 rd
->remote_port
== port
&&
449 rd
->remote_vni
== vni
&&
450 rd
->remote_ifindex
== ifindex
)
457 /* Replace destination of unicast mac */
458 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
459 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
462 struct vxlan_rdst
*rd
;
464 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
468 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
472 dst_cache_reset(&rd
->dst_cache
);
474 rd
->remote_port
= port
;
475 rd
->remote_vni
= vni
;
476 rd
->remote_ifindex
= ifindex
;
480 /* Add/update destinations for multicast */
481 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
482 union vxlan_addr
*ip
, __be16 port
, __be32 vni
,
483 __u32 ifindex
, struct vxlan_rdst
**rdp
)
485 struct vxlan_rdst
*rd
;
487 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
491 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
495 if (dst_cache_init(&rd
->dst_cache
, GFP_ATOMIC
)) {
501 rd
->remote_port
= port
;
502 rd
->remote_vni
= vni
;
503 rd
->remote_ifindex
= ifindex
;
505 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
511 static struct vxlanhdr
*vxlan_gro_remcsum(struct sk_buff
*skb
,
513 struct vxlanhdr
*vh
, size_t hdrlen
,
515 struct gro_remcsum
*grc
,
518 size_t start
, offset
;
520 if (skb
->remcsum_offload
)
523 if (!NAPI_GRO_CB(skb
)->csum_valid
)
526 start
= vxlan_rco_start(vni_field
);
527 offset
= start
+ vxlan_rco_offset(vni_field
);
529 vh
= skb_gro_remcsum_process(skb
, (void *)vh
, off
, hdrlen
,
530 start
, offset
, grc
, nopartial
);
532 skb
->remcsum_offload
= 1;
537 static struct sk_buff
**vxlan_gro_receive(struct sock
*sk
,
538 struct sk_buff
**head
,
541 struct sk_buff
*p
, **pp
= NULL
;
542 struct vxlanhdr
*vh
, *vh2
;
543 unsigned int hlen
, off_vx
;
545 struct vxlan_sock
*vs
= rcu_dereference_sk_user_data(sk
);
547 struct gro_remcsum grc
;
549 skb_gro_remcsum_init(&grc
);
551 off_vx
= skb_gro_offset(skb
);
552 hlen
= off_vx
+ sizeof(*vh
);
553 vh
= skb_gro_header_fast(skb
, off_vx
);
554 if (skb_gro_header_hard(skb
, hlen
)) {
555 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
560 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
562 flags
= vh
->vx_flags
;
564 if ((flags
& VXLAN_HF_RCO
) && (vs
->flags
& VXLAN_F_REMCSUM_RX
)) {
565 vh
= vxlan_gro_remcsum(skb
, off_vx
, vh
, sizeof(struct vxlanhdr
),
568 VXLAN_F_REMCSUM_NOPARTIAL
));
574 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
576 for (p
= *head
; p
; p
= p
->next
) {
577 if (!NAPI_GRO_CB(p
)->same_flow
)
580 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
581 if (vh
->vx_flags
!= vh2
->vx_flags
||
582 vh
->vx_vni
!= vh2
->vx_vni
) {
583 NAPI_GRO_CB(p
)->same_flow
= 0;
588 pp
= call_gro_receive(eth_gro_receive
, head
, skb
);
592 skb_gro_remcsum_cleanup(skb
, &grc
);
593 NAPI_GRO_CB(skb
)->flush
|= flush
;
598 static int vxlan_gro_complete(struct sock
*sk
, struct sk_buff
*skb
, int nhoff
)
600 /* Sets 'skb->inner_mac_header' since we are always called with
601 * 'skb->encapsulation' set.
603 return eth_gro_complete(skb
, nhoff
+ sizeof(struct vxlanhdr
));
606 /* Add new entry to forwarding table -- assumes lock held */
607 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
608 const u8
*mac
, union vxlan_addr
*ip
,
609 __u16 state
, __u16 flags
,
610 __be16 port
, __be32 vni
, __u32 ifindex
,
613 struct vxlan_rdst
*rd
= NULL
;
618 f
= __vxlan_find_mac(vxlan
, mac
);
620 if (flags
& NLM_F_EXCL
) {
621 netdev_dbg(vxlan
->dev
,
622 "lost race to create %pM\n", mac
);
625 if (f
->state
!= state
) {
627 f
->updated
= jiffies
;
630 if (f
->flags
!= ndm_flags
) {
631 f
->flags
= ndm_flags
;
632 f
->updated
= jiffies
;
635 if ((flags
& NLM_F_REPLACE
)) {
636 /* Only change unicasts */
637 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
638 is_zero_ether_addr(f
->eth_addr
))) {
639 notify
|= vxlan_fdb_replace(f
, ip
, port
, vni
,
644 if ((flags
& NLM_F_APPEND
) &&
645 (is_multicast_ether_addr(f
->eth_addr
) ||
646 is_zero_ether_addr(f
->eth_addr
))) {
647 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
654 if (!(flags
& NLM_F_CREATE
))
657 if (vxlan
->cfg
.addrmax
&&
658 vxlan
->addrcnt
>= vxlan
->cfg
.addrmax
)
661 /* Disallow replace to add a multicast entry */
662 if ((flags
& NLM_F_REPLACE
) &&
663 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
666 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
667 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
673 f
->flags
= ndm_flags
;
674 f
->updated
= f
->used
= jiffies
;
675 INIT_LIST_HEAD(&f
->remotes
);
676 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
678 rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
685 hlist_add_head_rcu(&f
->hlist
,
686 vxlan_fdb_head(vxlan
, mac
));
691 rd
= first_remote_rtnl(f
);
692 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
698 static void vxlan_fdb_free(struct rcu_head
*head
)
700 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
701 struct vxlan_rdst
*rd
, *nd
;
703 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
) {
704 dst_cache_destroy(&rd
->dst_cache
);
710 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
712 netdev_dbg(vxlan
->dev
,
713 "delete %pM\n", f
->eth_addr
);
716 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
718 hlist_del_rcu(&f
->hlist
);
719 call_rcu(&f
->rcu
, vxlan_fdb_free
);
722 static void vxlan_dst_free(struct rcu_head
*head
)
724 struct vxlan_rdst
*rd
= container_of(head
, struct vxlan_rdst
, rcu
);
726 dst_cache_destroy(&rd
->dst_cache
);
730 static void vxlan_fdb_dst_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
,
731 struct vxlan_rdst
*rd
)
733 list_del_rcu(&rd
->list
);
734 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
735 call_rcu(&rd
->rcu
, vxlan_dst_free
);
738 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
739 union vxlan_addr
*ip
, __be16
*port
, __be32
*vni
,
742 struct net
*net
= dev_net(vxlan
->dev
);
746 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
750 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
751 if (remote
->sa
.sa_family
== AF_INET
) {
752 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
753 ip
->sa
.sa_family
= AF_INET
;
754 #if IS_ENABLED(CONFIG_IPV6)
756 ip
->sin6
.sin6_addr
= in6addr_any
;
757 ip
->sa
.sa_family
= AF_INET6
;
763 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
765 *port
= nla_get_be16(tb
[NDA_PORT
]);
767 *port
= vxlan
->cfg
.dst_port
;
771 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
773 *vni
= cpu_to_be32(nla_get_u32(tb
[NDA_VNI
]));
775 *vni
= vxlan
->default_dst
.remote_vni
;
778 if (tb
[NDA_IFINDEX
]) {
779 struct net_device
*tdev
;
781 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
783 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
784 tdev
= __dev_get_by_index(net
, *ifindex
);
786 return -EADDRNOTAVAIL
;
794 /* Add static entry (via netlink) */
795 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
796 struct net_device
*dev
,
797 const unsigned char *addr
, u16 vid
, u16 flags
)
799 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
800 /* struct net *net = dev_net(vxlan->dev); */
807 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
808 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
813 if (tb
[NDA_DST
] == NULL
)
816 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
820 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
821 return -EAFNOSUPPORT
;
823 spin_lock_bh(&vxlan
->hash_lock
);
824 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
825 port
, vni
, ifindex
, ndm
->ndm_flags
);
826 spin_unlock_bh(&vxlan
->hash_lock
);
831 /* Delete entry (via netlink) */
832 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
833 struct net_device
*dev
,
834 const unsigned char *addr
, u16 vid
)
836 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
838 struct vxlan_rdst
*rd
= NULL
;
845 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
851 spin_lock_bh(&vxlan
->hash_lock
);
852 f
= vxlan_find_mac(vxlan
, addr
);
856 if (!vxlan_addr_any(&ip
)) {
857 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
864 /* remove a destination if it's not the only one on the list,
865 * otherwise destroy the fdb entry
867 if (rd
&& !list_is_singular(&f
->remotes
)) {
868 vxlan_fdb_dst_destroy(vxlan
, f
, rd
);
872 vxlan_fdb_destroy(vxlan
, f
);
875 spin_unlock_bh(&vxlan
->hash_lock
);
880 /* Dump forwarding table */
881 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
882 struct net_device
*dev
,
883 struct net_device
*filter_dev
, int *idx
)
885 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
889 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
892 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
893 struct vxlan_rdst
*rd
;
895 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
896 if (*idx
< cb
->args
[2])
899 err
= vxlan_fdb_info(skb
, vxlan
, f
,
900 NETLINK_CB(cb
->skb
).portid
,
915 /* Watch incoming packets to learn mapping between Ethernet address
916 * and Tunnel endpoint.
917 * Return true if packet is bogus and should be dropped.
919 static bool vxlan_snoop(struct net_device
*dev
,
920 union vxlan_addr
*src_ip
, const u8
*src_mac
)
922 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
925 f
= vxlan_find_mac(vxlan
, src_mac
);
927 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
929 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
932 /* Don't migrate static entries, drop packets */
933 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
938 "%pM migrated from %pIS to %pIS\n",
939 src_mac
, &rdst
->remote_ip
.sa
, &src_ip
->sa
);
941 rdst
->remote_ip
= *src_ip
;
942 f
->updated
= jiffies
;
943 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
945 /* learned new entry */
946 spin_lock(&vxlan
->hash_lock
);
948 /* close off race between vxlan_flush and incoming packets */
949 if (netif_running(dev
))
950 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
952 NLM_F_EXCL
|NLM_F_CREATE
,
954 vxlan
->default_dst
.remote_vni
,
956 spin_unlock(&vxlan
->hash_lock
);
962 /* See if multicast group is already in use by other ID */
963 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
965 struct vxlan_dev
*vxlan
;
966 struct vxlan_sock
*sock4
;
967 #if IS_ENABLED(CONFIG_IPV6)
968 struct vxlan_sock
*sock6
;
970 unsigned short family
= dev
->default_dst
.remote_ip
.sa
.sa_family
;
972 sock4
= rtnl_dereference(dev
->vn4_sock
);
974 /* The vxlan_sock is only used by dev, leaving group has
975 * no effect on other vxlan devices.
977 if (family
== AF_INET
&& sock4
&& atomic_read(&sock4
->refcnt
) == 1)
979 #if IS_ENABLED(CONFIG_IPV6)
980 sock6
= rtnl_dereference(dev
->vn6_sock
);
981 if (family
== AF_INET6
&& sock6
&& atomic_read(&sock6
->refcnt
) == 1)
985 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
986 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
989 if (family
== AF_INET
&&
990 rtnl_dereference(vxlan
->vn4_sock
) != sock4
)
992 #if IS_ENABLED(CONFIG_IPV6)
993 if (family
== AF_INET6
&&
994 rtnl_dereference(vxlan
->vn6_sock
) != sock6
)
998 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
999 &dev
->default_dst
.remote_ip
))
1002 if (vxlan
->default_dst
.remote_ifindex
!=
1003 dev
->default_dst
.remote_ifindex
)
1012 static bool __vxlan_sock_release_prep(struct vxlan_sock
*vs
)
1014 struct vxlan_net
*vn
;
1018 if (!atomic_dec_and_test(&vs
->refcnt
))
1021 vn
= net_generic(sock_net(vs
->sock
->sk
), vxlan_net_id
);
1022 spin_lock(&vn
->sock_lock
);
1023 hlist_del_rcu(&vs
->hlist
);
1024 udp_tunnel_notify_del_rx_port(vs
->sock
,
1025 (vs
->flags
& VXLAN_F_GPE
) ?
1026 UDP_TUNNEL_TYPE_VXLAN_GPE
:
1027 UDP_TUNNEL_TYPE_VXLAN
);
1028 spin_unlock(&vn
->sock_lock
);
1033 static void vxlan_sock_release(struct vxlan_dev
*vxlan
)
1035 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1036 #if IS_ENABLED(CONFIG_IPV6)
1037 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1039 rcu_assign_pointer(vxlan
->vn6_sock
, NULL
);
1042 rcu_assign_pointer(vxlan
->vn4_sock
, NULL
);
1045 vxlan_vs_del_dev(vxlan
);
1047 if (__vxlan_sock_release_prep(sock4
)) {
1048 udp_tunnel_sock_release(sock4
->sock
);
1052 #if IS_ENABLED(CONFIG_IPV6)
1053 if (__vxlan_sock_release_prep(sock6
)) {
1054 udp_tunnel_sock_release(sock6
->sock
);
1060 /* Update multicast group membership when first VNI on
1061 * multicast address is brought up
1063 static int vxlan_igmp_join(struct vxlan_dev
*vxlan
)
1066 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1067 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1070 if (ip
->sa
.sa_family
== AF_INET
) {
1071 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1072 struct ip_mreqn mreq
= {
1073 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1074 .imr_ifindex
= ifindex
,
1077 sk
= sock4
->sock
->sk
;
1079 ret
= ip_mc_join_group(sk
, &mreq
);
1081 #if IS_ENABLED(CONFIG_IPV6)
1083 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1085 sk
= sock6
->sock
->sk
;
1087 ret
= ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1088 &ip
->sin6
.sin6_addr
);
1096 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1097 static int vxlan_igmp_leave(struct vxlan_dev
*vxlan
)
1100 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1101 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1104 if (ip
->sa
.sa_family
== AF_INET
) {
1105 struct vxlan_sock
*sock4
= rtnl_dereference(vxlan
->vn4_sock
);
1106 struct ip_mreqn mreq
= {
1107 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1108 .imr_ifindex
= ifindex
,
1111 sk
= sock4
->sock
->sk
;
1113 ret
= ip_mc_leave_group(sk
, &mreq
);
1115 #if IS_ENABLED(CONFIG_IPV6)
1117 struct vxlan_sock
*sock6
= rtnl_dereference(vxlan
->vn6_sock
);
1119 sk
= sock6
->sock
->sk
;
1121 ret
= ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1122 &ip
->sin6
.sin6_addr
);
1130 static bool vxlan_remcsum(struct vxlanhdr
*unparsed
,
1131 struct sk_buff
*skb
, u32 vxflags
)
1133 size_t start
, offset
;
1135 if (!(unparsed
->vx_flags
& VXLAN_HF_RCO
) || skb
->remcsum_offload
)
1138 start
= vxlan_rco_start(unparsed
->vx_vni
);
1139 offset
= start
+ vxlan_rco_offset(unparsed
->vx_vni
);
1141 if (!pskb_may_pull(skb
, offset
+ sizeof(u16
)))
1144 skb_remcsum_process(skb
, (void *)(vxlan_hdr(skb
) + 1), start
, offset
,
1145 !!(vxflags
& VXLAN_F_REMCSUM_NOPARTIAL
));
1147 unparsed
->vx_flags
&= ~VXLAN_HF_RCO
;
1148 unparsed
->vx_vni
&= VXLAN_VNI_MASK
;
1152 static void vxlan_parse_gbp_hdr(struct vxlanhdr
*unparsed
,
1153 struct sk_buff
*skb
, u32 vxflags
,
1154 struct vxlan_metadata
*md
)
1156 struct vxlanhdr_gbp
*gbp
= (struct vxlanhdr_gbp
*)unparsed
;
1157 struct metadata_dst
*tun_dst
;
1159 if (!(unparsed
->vx_flags
& VXLAN_HF_GBP
))
1162 md
->gbp
= ntohs(gbp
->policy_id
);
1164 tun_dst
= (struct metadata_dst
*)skb_dst(skb
);
1166 tun_dst
->u
.tun_info
.key
.tun_flags
|= TUNNEL_VXLAN_OPT
;
1167 tun_dst
->u
.tun_info
.options_len
= sizeof(*md
);
1169 if (gbp
->dont_learn
)
1170 md
->gbp
|= VXLAN_GBP_DONT_LEARN
;
1172 if (gbp
->policy_applied
)
1173 md
->gbp
|= VXLAN_GBP_POLICY_APPLIED
;
1175 /* In flow-based mode, GBP is carried in dst_metadata */
1176 if (!(vxflags
& VXLAN_F_COLLECT_METADATA
))
1177 skb
->mark
= md
->gbp
;
1179 unparsed
->vx_flags
&= ~VXLAN_GBP_USED_BITS
;
1182 static bool vxlan_parse_gpe_hdr(struct vxlanhdr
*unparsed
,
1184 struct sk_buff
*skb
, u32 vxflags
)
1186 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)unparsed
;
1188 /* Need to have Next Protocol set for interfaces in GPE mode. */
1189 if (!gpe
->np_applied
)
1191 /* "The initial version is 0. If a receiver does not support the
1192 * version indicated it MUST drop the packet.
1194 if (gpe
->version
!= 0)
1196 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1197 * processing MUST occur." However, we don't implement OAM
1198 * processing, thus drop the packet.
1203 switch (gpe
->next_protocol
) {
1204 case VXLAN_GPE_NP_IPV4
:
1205 *protocol
= htons(ETH_P_IP
);
1207 case VXLAN_GPE_NP_IPV6
:
1208 *protocol
= htons(ETH_P_IPV6
);
1210 case VXLAN_GPE_NP_ETHERNET
:
1211 *protocol
= htons(ETH_P_TEB
);
1217 unparsed
->vx_flags
&= ~VXLAN_GPE_USED_BITS
;
1221 static bool vxlan_set_mac(struct vxlan_dev
*vxlan
,
1222 struct vxlan_sock
*vs
,
1223 struct sk_buff
*skb
)
1225 union vxlan_addr saddr
;
1227 skb_reset_mac_header(skb
);
1228 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1229 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1231 /* Ignore packet loops (and multicast echo) */
1232 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1235 /* Get address from the outer IP header */
1236 if (vxlan_get_sk_family(vs
) == AF_INET
) {
1237 saddr
.sin
.sin_addr
.s_addr
= ip_hdr(skb
)->saddr
;
1238 saddr
.sa
.sa_family
= AF_INET
;
1239 #if IS_ENABLED(CONFIG_IPV6)
1241 saddr
.sin6
.sin6_addr
= ipv6_hdr(skb
)->saddr
;
1242 saddr
.sa
.sa_family
= AF_INET6
;
1246 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1247 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1253 static bool vxlan_ecn_decapsulate(struct vxlan_sock
*vs
, void *oiph
,
1254 struct sk_buff
*skb
)
1258 if (vxlan_get_sk_family(vs
) == AF_INET
)
1259 err
= IP_ECN_decapsulate(oiph
, skb
);
1260 #if IS_ENABLED(CONFIG_IPV6)
1262 err
= IP6_ECN_decapsulate(oiph
, skb
);
1265 if (unlikely(err
) && log_ecn_error
) {
1266 if (vxlan_get_sk_family(vs
) == AF_INET
)
1267 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1268 &((struct iphdr
*)oiph
)->saddr
,
1269 ((struct iphdr
*)oiph
)->tos
);
1271 net_info_ratelimited("non-ECT from %pI6\n",
1272 &((struct ipv6hdr
*)oiph
)->saddr
);
1277 /* Callback from net/ipv4/udp.c to receive packets */
1278 static int vxlan_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1280 struct pcpu_sw_netstats
*stats
;
1281 struct vxlan_dev
*vxlan
;
1282 struct vxlan_sock
*vs
;
1283 struct vxlanhdr unparsed
;
1284 struct vxlan_metadata _md
;
1285 struct vxlan_metadata
*md
= &_md
;
1286 __be16 protocol
= htons(ETH_P_TEB
);
1287 bool raw_proto
= false;
1290 /* Need UDP and VXLAN header to be present */
1291 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1294 unparsed
= *vxlan_hdr(skb
);
1295 /* VNI flag always required to be set */
1296 if (!(unparsed
.vx_flags
& VXLAN_HF_VNI
)) {
1297 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1298 ntohl(vxlan_hdr(skb
)->vx_flags
),
1299 ntohl(vxlan_hdr(skb
)->vx_vni
));
1300 /* Return non vxlan pkt */
1303 unparsed
.vx_flags
&= ~VXLAN_HF_VNI
;
1304 unparsed
.vx_vni
&= ~VXLAN_VNI_MASK
;
1306 vs
= rcu_dereference_sk_user_data(sk
);
1310 vxlan
= vxlan_vs_find_vni(vs
, vxlan_vni(vxlan_hdr(skb
)->vx_vni
));
1314 /* For backwards compatibility, only allow reserved fields to be
1315 * used by VXLAN extensions if explicitly requested.
1317 if (vs
->flags
& VXLAN_F_GPE
) {
1318 if (!vxlan_parse_gpe_hdr(&unparsed
, &protocol
, skb
, vs
->flags
))
1323 if (__iptunnel_pull_header(skb
, VXLAN_HLEN
, protocol
, raw_proto
,
1324 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
))))
1327 if (vxlan_collect_metadata(vs
)) {
1328 __be32 vni
= vxlan_vni(vxlan_hdr(skb
)->vx_vni
);
1329 struct metadata_dst
*tun_dst
;
1331 tun_dst
= udp_tun_rx_dst(skb
, vxlan_get_sk_family(vs
), TUNNEL_KEY
,
1332 key32_to_tunnel_id(vni
), sizeof(*md
));
1337 md
= ip_tunnel_info_opts(&tun_dst
->u
.tun_info
);
1339 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
1341 memset(md
, 0, sizeof(*md
));
1344 if (vs
->flags
& VXLAN_F_REMCSUM_RX
)
1345 if (!vxlan_remcsum(&unparsed
, skb
, vs
->flags
))
1347 if (vs
->flags
& VXLAN_F_GBP
)
1348 vxlan_parse_gbp_hdr(&unparsed
, skb
, vs
->flags
, md
);
1349 /* Note that GBP and GPE can never be active together. This is
1350 * ensured in vxlan_dev_configure.
1353 if (unparsed
.vx_flags
|| unparsed
.vx_vni
) {
1354 /* If there are any unprocessed flags remaining treat
1355 * this as a malformed packet. This behavior diverges from
1356 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1357 * in reserved fields are to be ignored. The approach here
1358 * maintains compatibility with previous stack code, and also
1359 * is more robust and provides a little more security in
1360 * adding extensions to VXLAN.
1366 if (!vxlan_set_mac(vxlan
, vs
, skb
))
1369 skb_reset_mac_header(skb
);
1370 skb
->dev
= vxlan
->dev
;
1371 skb
->pkt_type
= PACKET_HOST
;
1374 oiph
= skb_network_header(skb
);
1375 skb_reset_network_header(skb
);
1377 if (!vxlan_ecn_decapsulate(vs
, oiph
, skb
)) {
1378 ++vxlan
->dev
->stats
.rx_frame_errors
;
1379 ++vxlan
->dev
->stats
.rx_errors
;
1385 if (unlikely(!(vxlan
->dev
->flags
& IFF_UP
))) {
1387 atomic_long_inc(&vxlan
->dev
->rx_dropped
);
1391 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1392 u64_stats_update_begin(&stats
->syncp
);
1393 stats
->rx_packets
++;
1394 stats
->rx_bytes
+= skb
->len
;
1395 u64_stats_update_end(&stats
->syncp
);
1397 gro_cells_receive(&vxlan
->gro_cells
, skb
);
1404 /* Consume bad packet */
1409 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1411 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1412 struct arphdr
*parp
;
1415 struct neighbour
*n
;
1417 if (dev
->flags
& IFF_NOARP
)
1420 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1421 dev
->stats
.tx_dropped
++;
1424 parp
= arp_hdr(skb
);
1426 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1427 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1428 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1429 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1430 parp
->ar_hln
!= dev
->addr_len
||
1433 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1435 arpptr
+= dev
->addr_len
; /* sha */
1436 memcpy(&sip
, arpptr
, sizeof(sip
));
1437 arpptr
+= sizeof(sip
);
1438 arpptr
+= dev
->addr_len
; /* tha */
1439 memcpy(&tip
, arpptr
, sizeof(tip
));
1441 if (ipv4_is_loopback(tip
) ||
1442 ipv4_is_multicast(tip
))
1445 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1448 struct vxlan_fdb
*f
;
1449 struct sk_buff
*reply
;
1451 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1456 f
= vxlan_find_mac(vxlan
, n
->ha
);
1457 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1458 /* bridge-local neighbor */
1463 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1471 skb_reset_mac_header(reply
);
1472 __skb_pull(reply
, skb_network_offset(reply
));
1473 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1474 reply
->pkt_type
= PACKET_HOST
;
1476 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1477 dev
->stats
.rx_dropped
++;
1478 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1479 union vxlan_addr ipa
= {
1480 .sin
.sin_addr
.s_addr
= tip
,
1481 .sin
.sin_family
= AF_INET
,
1484 vxlan_ip_miss(dev
, &ipa
);
1488 return NETDEV_TX_OK
;
1491 #if IS_ENABLED(CONFIG_IPV6)
1492 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1493 struct neighbour
*n
, bool isrouter
)
1495 struct net_device
*dev
= request
->dev
;
1496 struct sk_buff
*reply
;
1497 struct nd_msg
*ns
, *na
;
1498 struct ipv6hdr
*pip6
;
1500 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1507 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1508 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1509 reply
= alloc_skb(len
, GFP_ATOMIC
);
1513 reply
->protocol
= htons(ETH_P_IPV6
);
1515 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1516 skb_push(reply
, sizeof(struct ethhdr
));
1517 skb_reset_mac_header(reply
);
1519 ns
= (struct nd_msg
*)skb_transport_header(request
);
1521 daddr
= eth_hdr(request
)->h_source
;
1522 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1523 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1524 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1525 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1530 /* Ethernet header */
1531 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1532 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1533 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1534 reply
->protocol
= htons(ETH_P_IPV6
);
1536 skb_pull(reply
, sizeof(struct ethhdr
));
1537 skb_reset_network_header(reply
);
1538 skb_put(reply
, sizeof(struct ipv6hdr
));
1542 pip6
= ipv6_hdr(reply
);
1543 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1545 pip6
->priority
= ipv6_hdr(request
)->priority
;
1546 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1547 pip6
->hop_limit
= 255;
1548 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1549 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1551 skb_pull(reply
, sizeof(struct ipv6hdr
));
1552 skb_reset_transport_header(reply
);
1554 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1556 /* Neighbor Advertisement */
1557 memset(na
, 0, sizeof(*na
)+na_olen
);
1558 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1559 na
->icmph
.icmp6_router
= isrouter
;
1560 na
->icmph
.icmp6_override
= 1;
1561 na
->icmph
.icmp6_solicited
= 1;
1562 na
->target
= ns
->target
;
1563 ether_addr_copy(&na
->opt
[2], n
->ha
);
1564 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1565 na
->opt
[1] = na_olen
>> 3;
1567 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1568 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1569 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1571 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1573 skb_push(reply
, sizeof(struct ipv6hdr
));
1575 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1580 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1582 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1584 const struct ipv6hdr
*iphdr
;
1585 const struct in6_addr
*saddr
, *daddr
;
1586 struct neighbour
*n
;
1587 struct inet6_dev
*in6_dev
;
1589 in6_dev
= __in6_dev_get(dev
);
1593 iphdr
= ipv6_hdr(skb
);
1594 saddr
= &iphdr
->saddr
;
1595 daddr
= &iphdr
->daddr
;
1597 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1598 if (msg
->icmph
.icmp6_code
!= 0 ||
1599 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1602 if (ipv6_addr_loopback(daddr
) ||
1603 ipv6_addr_is_multicast(&msg
->target
))
1606 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1609 struct vxlan_fdb
*f
;
1610 struct sk_buff
*reply
;
1612 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1617 f
= vxlan_find_mac(vxlan
, n
->ha
);
1618 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1619 /* bridge-local neighbor */
1624 reply
= vxlan_na_create(skb
, n
,
1625 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1632 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1633 dev
->stats
.rx_dropped
++;
1635 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1636 union vxlan_addr ipa
= {
1637 .sin6
.sin6_addr
= msg
->target
,
1638 .sin6
.sin6_family
= AF_INET6
,
1641 vxlan_ip_miss(dev
, &ipa
);
1646 return NETDEV_TX_OK
;
1650 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1652 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1653 struct neighbour
*n
;
1655 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1659 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1664 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1667 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1668 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1669 union vxlan_addr ipa
= {
1670 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1671 .sin
.sin_family
= AF_INET
,
1674 vxlan_ip_miss(dev
, &ipa
);
1680 #if IS_ENABLED(CONFIG_IPV6)
1683 struct ipv6hdr
*pip6
;
1685 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1687 pip6
= ipv6_hdr(skb
);
1688 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1689 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1690 union vxlan_addr ipa
= {
1691 .sin6
.sin6_addr
= pip6
->daddr
,
1692 .sin6
.sin6_family
= AF_INET6
,
1695 vxlan_ip_miss(dev
, &ipa
);
1709 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1711 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1713 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1722 static void vxlan_build_gbp_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1723 struct vxlan_metadata
*md
)
1725 struct vxlanhdr_gbp
*gbp
;
1730 gbp
= (struct vxlanhdr_gbp
*)vxh
;
1731 vxh
->vx_flags
|= VXLAN_HF_GBP
;
1733 if (md
->gbp
& VXLAN_GBP_DONT_LEARN
)
1734 gbp
->dont_learn
= 1;
1736 if (md
->gbp
& VXLAN_GBP_POLICY_APPLIED
)
1737 gbp
->policy_applied
= 1;
1739 gbp
->policy_id
= htons(md
->gbp
& VXLAN_GBP_ID_MASK
);
1742 static int vxlan_build_gpe_hdr(struct vxlanhdr
*vxh
, u32 vxflags
,
1745 struct vxlanhdr_gpe
*gpe
= (struct vxlanhdr_gpe
*)vxh
;
1747 gpe
->np_applied
= 1;
1750 case htons(ETH_P_IP
):
1751 gpe
->next_protocol
= VXLAN_GPE_NP_IPV4
;
1753 case htons(ETH_P_IPV6
):
1754 gpe
->next_protocol
= VXLAN_GPE_NP_IPV6
;
1756 case htons(ETH_P_TEB
):
1757 gpe
->next_protocol
= VXLAN_GPE_NP_ETHERNET
;
1760 return -EPFNOSUPPORT
;
1763 static int vxlan_build_skb(struct sk_buff
*skb
, struct dst_entry
*dst
,
1764 int iphdr_len
, __be32 vni
,
1765 struct vxlan_metadata
*md
, u32 vxflags
,
1768 struct vxlanhdr
*vxh
;
1771 int type
= udp_sum
? SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
1772 __be16 inner_protocol
= htons(ETH_P_TEB
);
1774 if ((vxflags
& VXLAN_F_REMCSUM_TX
) &&
1775 skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1776 int csum_start
= skb_checksum_start_offset(skb
);
1778 if (csum_start
<= VXLAN_MAX_REMCSUM_START
&&
1779 !(csum_start
& VXLAN_RCO_SHIFT_MASK
) &&
1780 (skb
->csum_offset
== offsetof(struct udphdr
, check
) ||
1781 skb
->csum_offset
== offsetof(struct tcphdr
, check
)))
1782 type
|= SKB_GSO_TUNNEL_REMCSUM
;
1785 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1786 + VXLAN_HLEN
+ iphdr_len
1787 + (skb_vlan_tag_present(skb
) ? VLAN_HLEN
: 0);
1789 /* Need space for new headers (invalidates iph ptr) */
1790 err
= skb_cow_head(skb
, min_headroom
);
1794 skb
= vlan_hwaccel_push_inside(skb
);
1798 err
= iptunnel_handle_offloads(skb
, type
);
1802 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1803 vxh
->vx_flags
= VXLAN_HF_VNI
;
1804 vxh
->vx_vni
= vxlan_vni_field(vni
);
1806 if (type
& SKB_GSO_TUNNEL_REMCSUM
) {
1809 start
= skb_checksum_start_offset(skb
) - sizeof(struct vxlanhdr
);
1810 vxh
->vx_vni
|= vxlan_compute_rco(start
, skb
->csum_offset
);
1811 vxh
->vx_flags
|= VXLAN_HF_RCO
;
1813 if (!skb_is_gso(skb
)) {
1814 skb
->ip_summed
= CHECKSUM_NONE
;
1815 skb
->encapsulation
= 0;
1819 if (vxflags
& VXLAN_F_GBP
)
1820 vxlan_build_gbp_hdr(vxh
, vxflags
, md
);
1821 if (vxflags
& VXLAN_F_GPE
) {
1822 err
= vxlan_build_gpe_hdr(vxh
, vxflags
, skb
->protocol
);
1825 inner_protocol
= skb
->protocol
;
1828 skb_set_inner_protocol(skb
, inner_protocol
);
1836 static struct rtable
*vxlan_get_route(struct vxlan_dev
*vxlan
,
1837 struct sk_buff
*skb
, int oif
, u8 tos
,
1838 __be32 daddr
, __be32
*saddr
,
1839 struct dst_cache
*dst_cache
,
1840 const struct ip_tunnel_info
*info
)
1842 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1843 struct rtable
*rt
= NULL
;
1849 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
1854 memset(&fl4
, 0, sizeof(fl4
));
1855 fl4
.flowi4_oif
= oif
;
1856 fl4
.flowi4_tos
= RT_TOS(tos
);
1857 fl4
.flowi4_mark
= skb
->mark
;
1858 fl4
.flowi4_proto
= IPPROTO_UDP
;
1862 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1866 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
1871 #if IS_ENABLED(CONFIG_IPV6)
1872 static struct dst_entry
*vxlan6_get_route(struct vxlan_dev
*vxlan
,
1873 struct sk_buff
*skb
, int oif
, u8 tos
,
1875 const struct in6_addr
*daddr
,
1876 struct in6_addr
*saddr
,
1877 struct dst_cache
*dst_cache
,
1878 const struct ip_tunnel_info
*info
)
1880 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
1881 bool use_cache
= ip_tunnel_dst_cache_usable(skb
, info
);
1882 struct dst_entry
*ndst
;
1887 return ERR_PTR(-EIO
);
1892 ndst
= dst_cache_get_ip6(dst_cache
, saddr
);
1897 memset(&fl6
, 0, sizeof(fl6
));
1898 fl6
.flowi6_oif
= oif
;
1901 fl6
.flowlabel
= ip6_make_flowinfo(RT_TOS(tos
), label
);
1902 fl6
.flowi6_mark
= skb
->mark
;
1903 fl6
.flowi6_proto
= IPPROTO_UDP
;
1905 err
= ipv6_stub
->ipv6_dst_lookup(vxlan
->net
,
1909 return ERR_PTR(err
);
1913 dst_cache_set_ip6(dst_cache
, ndst
, saddr
);
1918 /* Bypass encapsulation if the destination is local */
1919 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1920 struct vxlan_dev
*dst_vxlan
)
1922 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1923 union vxlan_addr loopback
;
1924 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1925 struct net_device
*dev
;
1928 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1929 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1930 skb
->pkt_type
= PACKET_HOST
;
1931 skb
->encapsulation
= 0;
1932 skb
->dev
= dst_vxlan
->dev
;
1933 __skb_pull(skb
, skb_network_offset(skb
));
1935 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1936 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1937 loopback
.sa
.sa_family
= AF_INET
;
1938 #if IS_ENABLED(CONFIG_IPV6)
1940 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1941 loopback
.sa
.sa_family
= AF_INET6
;
1947 if (unlikely(!(dev
->flags
& IFF_UP
))) {
1952 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1953 vxlan_snoop(dev
, &loopback
, eth_hdr(skb
)->h_source
);
1955 u64_stats_update_begin(&tx_stats
->syncp
);
1956 tx_stats
->tx_packets
++;
1957 tx_stats
->tx_bytes
+= len
;
1958 u64_stats_update_end(&tx_stats
->syncp
);
1960 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1961 u64_stats_update_begin(&rx_stats
->syncp
);
1962 rx_stats
->rx_packets
++;
1963 rx_stats
->rx_bytes
+= len
;
1964 u64_stats_update_end(&rx_stats
->syncp
);
1967 dev
->stats
.rx_dropped
++;
1972 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1973 struct vxlan_rdst
*rdst
, bool did_rsc
)
1975 struct dst_cache
*dst_cache
;
1976 struct ip_tunnel_info
*info
;
1977 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1979 struct rtable
*rt
= NULL
;
1980 const struct iphdr
*old_iph
;
1981 union vxlan_addr
*dst
;
1982 union vxlan_addr remote_ip
, local_ip
;
1983 struct vxlan_metadata _md
;
1984 struct vxlan_metadata
*md
= &_md
;
1985 __be16 src_port
= 0, dst_port
;
1990 u32 flags
= vxlan
->flags
;
1991 bool udp_sum
= false;
1992 bool xnet
= !net_eq(vxlan
->net
, dev_net(vxlan
->dev
));
1994 info
= skb_tunnel_info(skb
);
1998 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->cfg
.dst_port
;
1999 vni
= rdst
->remote_vni
;
2000 dst
= &rdst
->remote_ip
;
2001 local_ip
= vxlan
->cfg
.saddr
;
2002 dst_cache
= &rdst
->dst_cache
;
2005 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2009 dst_port
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2010 vni
= tunnel_id_to_key32(info
->key
.tun_id
);
2011 remote_ip
.sa
.sa_family
= ip_tunnel_info_af(info
);
2012 if (remote_ip
.sa
.sa_family
== AF_INET
) {
2013 remote_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.dst
;
2014 local_ip
.sin
.sin_addr
.s_addr
= info
->key
.u
.ipv4
.src
;
2016 remote_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.dst
;
2017 local_ip
.sin6
.sin6_addr
= info
->key
.u
.ipv6
.src
;
2020 dst_cache
= &info
->dst_cache
;
2023 if (vxlan_addr_any(dst
)) {
2025 /* short-circuited back to local bridge */
2026 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
2032 old_iph
= ip_hdr(skb
);
2034 ttl
= vxlan
->cfg
.ttl
;
2035 if (!ttl
&& vxlan_addr_multicast(dst
))
2038 tos
= vxlan
->cfg
.tos
;
2040 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
2042 label
= vxlan
->cfg
.label
;
2043 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2044 vxlan
->cfg
.port_max
, true);
2047 ttl
= info
->key
.ttl
;
2048 tos
= info
->key
.tos
;
2049 label
= info
->key
.label
;
2050 udp_sum
= !!(info
->key
.tun_flags
& TUNNEL_CSUM
);
2052 if (info
->options_len
)
2053 md
= ip_tunnel_info_opts(info
);
2055 md
->gbp
= skb
->mark
;
2058 if (dst
->sa
.sa_family
== AF_INET
) {
2059 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2063 sk
= sock4
->sock
->sk
;
2065 rt
= vxlan_get_route(vxlan
, skb
,
2066 rdst
? rdst
->remote_ifindex
: 0, tos
,
2067 dst
->sin
.sin_addr
.s_addr
,
2068 &local_ip
.sin
.sin_addr
.s_addr
,
2071 netdev_dbg(dev
, "no route to %pI4\n",
2072 &dst
->sin
.sin_addr
.s_addr
);
2073 dev
->stats
.tx_carrier_errors
++;
2077 if (rt
->dst
.dev
== dev
) {
2078 netdev_dbg(dev
, "circular route to %pI4\n",
2079 &dst
->sin
.sin_addr
.s_addr
);
2080 dev
->stats
.collisions
++;
2084 /* Bypass encapsulation if the destination is local */
2085 if (!info
&& rt
->rt_flags
& RTCF_LOCAL
&&
2086 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2087 struct vxlan_dev
*dst_vxlan
;
2090 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
2091 dst
->sa
.sa_family
, dst_port
,
2095 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
2100 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM_TX
);
2101 else if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
)
2104 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2105 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
2106 err
= vxlan_build_skb(skb
, &rt
->dst
, sizeof(struct iphdr
),
2107 vni
, md
, flags
, udp_sum
);
2111 udp_tunnel_xmit_skb(rt
, sk
, skb
, local_ip
.sin
.sin_addr
.s_addr
,
2112 dst
->sin
.sin_addr
.s_addr
, tos
, ttl
, df
,
2113 src_port
, dst_port
, xnet
, !udp_sum
);
2114 #if IS_ENABLED(CONFIG_IPV6)
2116 struct vxlan_sock
*sock6
= rcu_dereference(vxlan
->vn6_sock
);
2117 struct dst_entry
*ndst
;
2122 sk
= sock6
->sock
->sk
;
2124 ndst
= vxlan6_get_route(vxlan
, skb
,
2125 rdst
? rdst
->remote_ifindex
: 0, tos
,
2126 label
, &dst
->sin6
.sin6_addr
,
2127 &local_ip
.sin6
.sin6_addr
,
2130 netdev_dbg(dev
, "no route to %pI6\n",
2131 &dst
->sin6
.sin6_addr
);
2132 dev
->stats
.tx_carrier_errors
++;
2136 if (ndst
->dev
== dev
) {
2137 netdev_dbg(dev
, "circular route to %pI6\n",
2138 &dst
->sin6
.sin6_addr
);
2140 dev
->stats
.collisions
++;
2144 /* Bypass encapsulation if the destination is local */
2145 rt6i_flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
2146 if (!info
&& rt6i_flags
& RTF_LOCAL
&&
2147 !(rt6i_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
2148 struct vxlan_dev
*dst_vxlan
;
2151 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
2152 dst
->sa
.sa_family
, dst_port
,
2156 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
2161 udp_sum
= !(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2163 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
2164 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
2165 skb_scrub_packet(skb
, xnet
);
2166 err
= vxlan_build_skb(skb
, ndst
, sizeof(struct ipv6hdr
),
2167 vni
, md
, flags
, udp_sum
);
2170 dev
->stats
.tx_errors
++;
2173 udp_tunnel6_xmit_skb(ndst
, sk
, skb
, dev
,
2174 &local_ip
.sin6
.sin6_addr
,
2175 &dst
->sin6
.sin6_addr
, tos
, ttl
,
2176 label
, src_port
, dst_port
, !udp_sum
);
2184 dev
->stats
.tx_dropped
++;
2188 /* skb is already freed. */
2193 dev
->stats
.tx_errors
++;
2199 /* Transmit local packets over Vxlan
2201 * Outer IP header inherits ECN and DF from inner header.
2202 * Outer UDP destination is the VXLAN assigned port.
2203 * source port is based on hash of flow
2205 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2207 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2208 const struct ip_tunnel_info
*info
;
2210 bool did_rsc
= false;
2211 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
2212 struct vxlan_fdb
*f
;
2214 info
= skb_tunnel_info(skb
);
2216 skb_reset_mac_header(skb
);
2218 if (vxlan
->flags
& VXLAN_F_COLLECT_METADATA
) {
2219 if (info
&& info
->mode
& IP_TUNNEL_INFO_TX
)
2220 vxlan_xmit_one(skb
, dev
, NULL
, false);
2223 return NETDEV_TX_OK
;
2226 if (vxlan
->flags
& VXLAN_F_PROXY
) {
2228 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
2229 return arp_reduce(dev
, skb
);
2230 #if IS_ENABLED(CONFIG_IPV6)
2231 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
2232 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
2233 + sizeof(struct nd_msg
)) &&
2234 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
2237 msg
= (struct nd_msg
*)skb_transport_header(skb
);
2238 if (msg
->icmph
.icmp6_code
== 0 &&
2239 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
2240 return neigh_reduce(dev
, skb
);
2246 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
2249 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
2250 (ntohs(eth
->h_proto
) == ETH_P_IP
||
2251 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
2252 did_rsc
= route_shortcircuit(dev
, skb
);
2254 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
2258 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
2260 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
2261 !is_multicast_ether_addr(eth
->h_dest
))
2262 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
2264 dev
->stats
.tx_dropped
++;
2266 return NETDEV_TX_OK
;
2270 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
2271 struct sk_buff
*skb1
;
2277 skb1
= skb_clone(skb
, GFP_ATOMIC
);
2279 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
2283 vxlan_xmit_one(skb
, dev
, fdst
, did_rsc
);
2286 return NETDEV_TX_OK
;
2289 /* Walk the forwarding table and purge stale entries */
2290 static void vxlan_cleanup(unsigned long arg
)
2292 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
2293 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
2296 if (!netif_running(vxlan
->dev
))
2299 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2300 struct hlist_node
*p
, *n
;
2302 spin_lock_bh(&vxlan
->hash_lock
);
2303 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2305 = container_of(p
, struct vxlan_fdb
, hlist
);
2306 unsigned long timeout
;
2308 if (f
->state
& (NUD_PERMANENT
| NUD_NOARP
))
2311 timeout
= f
->used
+ vxlan
->cfg
.age_interval
* HZ
;
2312 if (time_before_eq(timeout
, jiffies
)) {
2313 netdev_dbg(vxlan
->dev
,
2314 "garbage collect %pM\n",
2316 f
->state
= NUD_STALE
;
2317 vxlan_fdb_destroy(vxlan
, f
);
2318 } else if (time_before(timeout
, next_timer
))
2319 next_timer
= timeout
;
2321 spin_unlock_bh(&vxlan
->hash_lock
);
2324 mod_timer(&vxlan
->age_timer
, next_timer
);
2327 static void vxlan_vs_del_dev(struct vxlan_dev
*vxlan
)
2329 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2331 spin_lock(&vn
->sock_lock
);
2332 hlist_del_init_rcu(&vxlan
->hlist4
.hlist
);
2333 #if IS_ENABLED(CONFIG_IPV6)
2334 hlist_del_init_rcu(&vxlan
->hlist6
.hlist
);
2336 spin_unlock(&vn
->sock_lock
);
2339 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
,
2340 struct vxlan_dev_node
*node
)
2342 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2343 __be32 vni
= vxlan
->default_dst
.remote_vni
;
2345 node
->vxlan
= vxlan
;
2346 spin_lock(&vn
->sock_lock
);
2347 hlist_add_head_rcu(&node
->hlist
, vni_head(vs
, vni
));
2348 spin_unlock(&vn
->sock_lock
);
2351 /* Setup stats when device is created */
2352 static int vxlan_init(struct net_device
*dev
)
2354 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2361 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
2363 struct vxlan_fdb
*f
;
2365 spin_lock_bh(&vxlan
->hash_lock
);
2366 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
2368 vxlan_fdb_destroy(vxlan
, f
);
2369 spin_unlock_bh(&vxlan
->hash_lock
);
2372 static void vxlan_uninit(struct net_device
*dev
)
2374 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2376 gro_cells_destroy(&vxlan
->gro_cells
);
2378 vxlan_fdb_delete_default(vxlan
);
2380 free_percpu(dev
->tstats
);
2383 /* Start ageing timer and join group when device is brought up */
2384 static int vxlan_open(struct net_device
*dev
)
2386 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2389 ret
= vxlan_sock_add(vxlan
);
2393 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2394 ret
= vxlan_igmp_join(vxlan
);
2395 if (ret
== -EADDRINUSE
)
2398 vxlan_sock_release(vxlan
);
2403 if (vxlan
->cfg
.age_interval
)
2404 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2409 /* Purge the forwarding table */
2410 static void vxlan_flush(struct vxlan_dev
*vxlan
)
2414 spin_lock_bh(&vxlan
->hash_lock
);
2415 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2416 struct hlist_node
*p
, *n
;
2417 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2419 = container_of(p
, struct vxlan_fdb
, hlist
);
2420 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2421 if (!is_zero_ether_addr(f
->eth_addr
))
2422 vxlan_fdb_destroy(vxlan
, f
);
2425 spin_unlock_bh(&vxlan
->hash_lock
);
2428 /* Cleanup timer and forwarding table on shutdown */
2429 static int vxlan_stop(struct net_device
*dev
)
2431 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2432 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2435 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2436 !vxlan_group_used(vn
, vxlan
))
2437 ret
= vxlan_igmp_leave(vxlan
);
2439 del_timer_sync(&vxlan
->age_timer
);
2442 vxlan_sock_release(vxlan
);
2447 /* Stub, nothing needs to be done. */
2448 static void vxlan_set_multicast_list(struct net_device
*dev
)
2452 static int __vxlan_change_mtu(struct net_device
*dev
,
2453 struct net_device
*lowerdev
,
2454 struct vxlan_rdst
*dst
, int new_mtu
, bool strict
)
2456 int max_mtu
= IP_MAX_MTU
;
2459 max_mtu
= lowerdev
->mtu
;
2461 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
)
2462 max_mtu
-= VXLAN6_HEADROOM
;
2464 max_mtu
-= VXLAN_HEADROOM
;
2469 if (new_mtu
> max_mtu
) {
2480 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2482 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2483 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2484 struct net_device
*lowerdev
= __dev_get_by_index(vxlan
->net
,
2485 dst
->remote_ifindex
);
2486 return __vxlan_change_mtu(dev
, lowerdev
, dst
, new_mtu
, true);
2489 static int vxlan_fill_metadata_dst(struct net_device
*dev
, struct sk_buff
*skb
)
2491 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2492 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
2493 __be16 sport
, dport
;
2495 sport
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->cfg
.port_min
,
2496 vxlan
->cfg
.port_max
, true);
2497 dport
= info
->key
.tp_dst
? : vxlan
->cfg
.dst_port
;
2499 if (ip_tunnel_info_af(info
) == AF_INET
) {
2500 struct vxlan_sock
*sock4
= rcu_dereference(vxlan
->vn4_sock
);
2505 rt
= vxlan_get_route(vxlan
, skb
, 0, info
->key
.tos
,
2506 info
->key
.u
.ipv4
.dst
,
2507 &info
->key
.u
.ipv4
.src
,
2508 &info
->dst_cache
, info
);
2513 #if IS_ENABLED(CONFIG_IPV6)
2514 struct dst_entry
*ndst
;
2516 ndst
= vxlan6_get_route(vxlan
, skb
, 0, info
->key
.tos
,
2517 info
->key
.label
, &info
->key
.u
.ipv6
.dst
,
2518 &info
->key
.u
.ipv6
.src
,
2519 &info
->dst_cache
, info
);
2521 return PTR_ERR(ndst
);
2523 #else /* !CONFIG_IPV6 */
2524 return -EPFNOSUPPORT
;
2527 info
->key
.tp_src
= sport
;
2528 info
->key
.tp_dst
= dport
;
2532 static const struct net_device_ops vxlan_netdev_ether_ops
= {
2533 .ndo_init
= vxlan_init
,
2534 .ndo_uninit
= vxlan_uninit
,
2535 .ndo_open
= vxlan_open
,
2536 .ndo_stop
= vxlan_stop
,
2537 .ndo_start_xmit
= vxlan_xmit
,
2538 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2539 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2540 .ndo_change_mtu
= vxlan_change_mtu
,
2541 .ndo_validate_addr
= eth_validate_addr
,
2542 .ndo_set_mac_address
= eth_mac_addr
,
2543 .ndo_fdb_add
= vxlan_fdb_add
,
2544 .ndo_fdb_del
= vxlan_fdb_delete
,
2545 .ndo_fdb_dump
= vxlan_fdb_dump
,
2546 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2549 static const struct net_device_ops vxlan_netdev_raw_ops
= {
2550 .ndo_init
= vxlan_init
,
2551 .ndo_uninit
= vxlan_uninit
,
2552 .ndo_open
= vxlan_open
,
2553 .ndo_stop
= vxlan_stop
,
2554 .ndo_start_xmit
= vxlan_xmit
,
2555 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2556 .ndo_change_mtu
= vxlan_change_mtu
,
2557 .ndo_fill_metadata_dst
= vxlan_fill_metadata_dst
,
2560 /* Info for udev, that this is a virtual tunnel endpoint */
2561 static struct device_type vxlan_type
= {
2565 /* Calls the ndo_udp_tunnel_add of the caller in order to
2566 * supply the listening VXLAN udp ports. Callers are expected
2567 * to implement the ndo_udp_tunnel_add.
2569 static void vxlan_push_rx_ports(struct net_device
*dev
)
2571 struct vxlan_sock
*vs
;
2572 struct net
*net
= dev_net(dev
);
2573 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2576 spin_lock(&vn
->sock_lock
);
2577 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2578 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
)
2579 udp_tunnel_push_rx_port(dev
, vs
->sock
,
2580 (vs
->flags
& VXLAN_F_GPE
) ?
2581 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2582 UDP_TUNNEL_TYPE_VXLAN
);
2584 spin_unlock(&vn
->sock_lock
);
2587 /* Initialize the device structure. */
2588 static void vxlan_setup(struct net_device
*dev
)
2590 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2593 eth_hw_addr_random(dev
);
2596 dev
->destructor
= free_netdev
;
2597 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2599 dev
->features
|= NETIF_F_LLTX
;
2600 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2601 dev
->features
|= NETIF_F_RXCSUM
;
2602 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2604 dev
->vlan_features
= dev
->features
;
2605 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2606 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2607 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2608 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2609 netif_keep_dst(dev
);
2610 dev
->priv_flags
|= IFF_NO_QUEUE
;
2612 INIT_LIST_HEAD(&vxlan
->next
);
2613 spin_lock_init(&vxlan
->hash_lock
);
2615 init_timer_deferrable(&vxlan
->age_timer
);
2616 vxlan
->age_timer
.function
= vxlan_cleanup
;
2617 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2619 vxlan
->cfg
.dst_port
= htons(vxlan_port
);
2623 gro_cells_init(&vxlan
->gro_cells
, dev
);
2625 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2626 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2629 static void vxlan_ether_setup(struct net_device
*dev
)
2631 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2632 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2633 dev
->netdev_ops
= &vxlan_netdev_ether_ops
;
2636 static void vxlan_raw_setup(struct net_device
*dev
)
2638 dev
->header_ops
= NULL
;
2639 dev
->type
= ARPHRD_NONE
;
2640 dev
->hard_header_len
= 0;
2642 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
2643 dev
->netdev_ops
= &vxlan_netdev_raw_ops
;
2646 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2647 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2648 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2649 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2650 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2651 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2652 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2653 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2654 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2655 [IFLA_VXLAN_LABEL
] = { .type
= NLA_U32
},
2656 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2657 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2658 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2659 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2660 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2661 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2662 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2663 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2664 [IFLA_VXLAN_COLLECT_METADATA
] = { .type
= NLA_U8
},
2665 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2666 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2667 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2668 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2669 [IFLA_VXLAN_REMCSUM_TX
] = { .type
= NLA_U8
},
2670 [IFLA_VXLAN_REMCSUM_RX
] = { .type
= NLA_U8
},
2671 [IFLA_VXLAN_GBP
] = { .type
= NLA_FLAG
, },
2672 [IFLA_VXLAN_GPE
] = { .type
= NLA_FLAG
, },
2673 [IFLA_VXLAN_REMCSUM_NOPARTIAL
] = { .type
= NLA_FLAG
},
2676 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2678 if (tb
[IFLA_ADDRESS
]) {
2679 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2680 pr_debug("invalid link address (not ethernet)\n");
2684 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2685 pr_debug("invalid all zero ethernet address\n");
2686 return -EADDRNOTAVAIL
;
2693 if (data
[IFLA_VXLAN_ID
]) {
2694 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2695 if (id
>= VXLAN_N_VID
)
2699 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2700 const struct ifla_vxlan_port_range
*p
2701 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2703 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2704 pr_debug("port range %u .. %u not valid\n",
2705 ntohs(p
->low
), ntohs(p
->high
));
2713 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2714 struct ethtool_drvinfo
*drvinfo
)
2716 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2717 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2720 static const struct ethtool_ops vxlan_ethtool_ops
= {
2721 .get_drvinfo
= vxlan_get_drvinfo
,
2722 .get_link
= ethtool_op_get_link
,
2725 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2726 __be16 port
, u32 flags
)
2728 struct socket
*sock
;
2729 struct udp_port_cfg udp_conf
;
2732 memset(&udp_conf
, 0, sizeof(udp_conf
));
2735 udp_conf
.family
= AF_INET6
;
2736 udp_conf
.use_udp6_rx_checksums
=
2737 !(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2738 udp_conf
.ipv6_v6only
= 1;
2740 udp_conf
.family
= AF_INET
;
2743 udp_conf
.local_udp_port
= port
;
2745 /* Open UDP socket */
2746 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2748 return ERR_PTR(err
);
2753 /* Create new listen socket if needed */
2754 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, bool ipv6
,
2755 __be16 port
, u32 flags
)
2757 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2758 struct vxlan_sock
*vs
;
2759 struct socket
*sock
;
2761 struct udp_tunnel_sock_cfg tunnel_cfg
;
2763 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2765 return ERR_PTR(-ENOMEM
);
2767 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2768 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2770 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2772 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port
),
2775 return ERR_CAST(sock
);
2779 atomic_set(&vs
->refcnt
, 1);
2780 vs
->flags
= (flags
& VXLAN_F_RCV_FLAGS
);
2782 spin_lock(&vn
->sock_lock
);
2783 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2784 udp_tunnel_notify_add_rx_port(sock
,
2785 (vs
->flags
& VXLAN_F_GPE
) ?
2786 UDP_TUNNEL_TYPE_VXLAN_GPE
:
2787 UDP_TUNNEL_TYPE_VXLAN
);
2788 spin_unlock(&vn
->sock_lock
);
2790 /* Mark socket as an encapsulation socket. */
2791 memset(&tunnel_cfg
, 0, sizeof(tunnel_cfg
));
2792 tunnel_cfg
.sk_user_data
= vs
;
2793 tunnel_cfg
.encap_type
= 1;
2794 tunnel_cfg
.encap_rcv
= vxlan_rcv
;
2795 tunnel_cfg
.encap_destroy
= NULL
;
2796 tunnel_cfg
.gro_receive
= vxlan_gro_receive
;
2797 tunnel_cfg
.gro_complete
= vxlan_gro_complete
;
2799 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2804 static int __vxlan_sock_add(struct vxlan_dev
*vxlan
, bool ipv6
)
2806 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2807 struct vxlan_sock
*vs
= NULL
;
2808 struct vxlan_dev_node
*node
;
2810 if (!vxlan
->cfg
.no_share
) {
2811 spin_lock(&vn
->sock_lock
);
2812 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2813 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2814 if (vs
&& !atomic_add_unless(&vs
->refcnt
, 1, 0)) {
2815 spin_unlock(&vn
->sock_lock
);
2818 spin_unlock(&vn
->sock_lock
);
2821 vs
= vxlan_socket_create(vxlan
->net
, ipv6
,
2822 vxlan
->cfg
.dst_port
, vxlan
->flags
);
2825 #if IS_ENABLED(CONFIG_IPV6)
2827 rcu_assign_pointer(vxlan
->vn6_sock
, vs
);
2828 node
= &vxlan
->hlist6
;
2832 rcu_assign_pointer(vxlan
->vn4_sock
, vs
);
2833 node
= &vxlan
->hlist4
;
2835 vxlan_vs_add_dev(vs
, vxlan
, node
);
2839 static int vxlan_sock_add(struct vxlan_dev
*vxlan
)
2841 bool metadata
= vxlan
->flags
& VXLAN_F_COLLECT_METADATA
;
2842 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
|| metadata
;
2843 bool ipv4
= !ipv6
|| metadata
;
2846 RCU_INIT_POINTER(vxlan
->vn4_sock
, NULL
);
2847 #if IS_ENABLED(CONFIG_IPV6)
2848 RCU_INIT_POINTER(vxlan
->vn6_sock
, NULL
);
2850 ret
= __vxlan_sock_add(vxlan
, true);
2851 if (ret
< 0 && ret
!= -EAFNOSUPPORT
)
2856 ret
= __vxlan_sock_add(vxlan
, false);
2858 vxlan_sock_release(vxlan
);
2862 static int vxlan_dev_configure(struct net
*src_net
, struct net_device
*dev
,
2863 struct vxlan_config
*conf
)
2865 struct vxlan_net
*vn
= net_generic(src_net
, vxlan_net_id
);
2866 struct vxlan_dev
*vxlan
= netdev_priv(dev
), *tmp
;
2867 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2868 unsigned short needed_headroom
= ETH_HLEN
;
2870 bool use_ipv6
= false;
2871 __be16 default_port
= vxlan
->cfg
.dst_port
;
2872 struct net_device
*lowerdev
= NULL
;
2874 if (conf
->flags
& VXLAN_F_GPE
) {
2875 /* For now, allow GPE only together with COLLECT_METADATA.
2876 * This can be relaxed later; in such case, the other side
2877 * of the PtP link will have to be provided.
2879 if ((conf
->flags
& ~VXLAN_F_ALLOWED_GPE
) ||
2880 !(conf
->flags
& VXLAN_F_COLLECT_METADATA
)) {
2881 pr_info("unsupported combination of extensions\n");
2885 vxlan_raw_setup(dev
);
2887 vxlan_ether_setup(dev
);
2890 vxlan
->net
= src_net
;
2892 dst
->remote_vni
= conf
->vni
;
2894 memcpy(&dst
->remote_ip
, &conf
->remote_ip
, sizeof(conf
->remote_ip
));
2896 /* Unless IPv6 is explicitly requested, assume IPv4 */
2897 if (!dst
->remote_ip
.sa
.sa_family
)
2898 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2900 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
||
2901 vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) {
2902 if (!IS_ENABLED(CONFIG_IPV6
))
2903 return -EPFNOSUPPORT
;
2905 vxlan
->flags
|= VXLAN_F_IPV6
;
2908 if (conf
->label
&& !use_ipv6
) {
2909 pr_info("label only supported in use with IPv6\n");
2913 if (conf
->remote_ifindex
) {
2914 lowerdev
= __dev_get_by_index(src_net
, conf
->remote_ifindex
);
2915 dst
->remote_ifindex
= conf
->remote_ifindex
;
2918 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2922 #if IS_ENABLED(CONFIG_IPV6)
2924 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2925 if (idev
&& idev
->cnf
.disable_ipv6
) {
2926 pr_info("IPv6 is disabled via sysctl\n");
2933 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2935 needed_headroom
= lowerdev
->hard_header_len
;
2936 } else if (vxlan_addr_multicast(&dst
->remote_ip
)) {
2937 pr_info("multicast destination requires interface to be specified\n");
2942 dev
->gso_max_size
= lowerdev
->gso_max_size
;
2943 dev
->gso_max_segs
= lowerdev
->gso_max_segs
;
2947 err
= __vxlan_change_mtu(dev
, lowerdev
, dst
, conf
->mtu
, false);
2952 if (use_ipv6
|| conf
->flags
& VXLAN_F_COLLECT_METADATA
)
2953 needed_headroom
+= VXLAN6_HEADROOM
;
2955 needed_headroom
+= VXLAN_HEADROOM
;
2956 dev
->needed_headroom
= needed_headroom
;
2958 memcpy(&vxlan
->cfg
, conf
, sizeof(*conf
));
2959 if (!vxlan
->cfg
.dst_port
) {
2960 if (conf
->flags
& VXLAN_F_GPE
)
2961 vxlan
->cfg
.dst_port
= htons(4790); /* IANA VXLAN-GPE port */
2963 vxlan
->cfg
.dst_port
= default_port
;
2965 vxlan
->flags
|= conf
->flags
;
2967 if (!vxlan
->cfg
.age_interval
)
2968 vxlan
->cfg
.age_interval
= FDB_AGE_DEFAULT
;
2970 list_for_each_entry(tmp
, &vn
->vxlan_list
, next
) {
2971 if (tmp
->cfg
.vni
== conf
->vni
&&
2972 (tmp
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
||
2973 tmp
->cfg
.saddr
.sa
.sa_family
== AF_INET6
) == use_ipv6
&&
2974 tmp
->cfg
.dst_port
== vxlan
->cfg
.dst_port
&&
2975 (tmp
->flags
& VXLAN_F_RCV_FLAGS
) ==
2976 (vxlan
->flags
& VXLAN_F_RCV_FLAGS
)) {
2977 pr_info("duplicate VNI %u\n", be32_to_cpu(conf
->vni
));
2982 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
2984 /* create an fdb entry for a valid default destination */
2985 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2986 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2987 &vxlan
->default_dst
.remote_ip
,
2988 NUD_REACHABLE
|NUD_PERMANENT
,
2989 NLM_F_EXCL
|NLM_F_CREATE
,
2990 vxlan
->cfg
.dst_port
,
2991 vxlan
->default_dst
.remote_vni
,
2992 vxlan
->default_dst
.remote_ifindex
,
2998 err
= register_netdevice(dev
);
3000 vxlan_fdb_delete_default(vxlan
);
3004 list_add(&vxlan
->next
, &vn
->vxlan_list
);
3009 static int vxlan_newlink(struct net
*src_net
, struct net_device
*dev
,
3010 struct nlattr
*tb
[], struct nlattr
*data
[])
3012 struct vxlan_config conf
;
3014 memset(&conf
, 0, sizeof(conf
));
3016 if (data
[IFLA_VXLAN_ID
])
3017 conf
.vni
= cpu_to_be32(nla_get_u32(data
[IFLA_VXLAN_ID
]));
3019 if (data
[IFLA_VXLAN_GROUP
]) {
3020 conf
.remote_ip
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_GROUP
]);
3021 } else if (data
[IFLA_VXLAN_GROUP6
]) {
3022 if (!IS_ENABLED(CONFIG_IPV6
))
3023 return -EPFNOSUPPORT
;
3025 conf
.remote_ip
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_GROUP6
]);
3026 conf
.remote_ip
.sa
.sa_family
= AF_INET6
;
3029 if (data
[IFLA_VXLAN_LOCAL
]) {
3030 conf
.saddr
.sin
.sin_addr
.s_addr
= nla_get_in_addr(data
[IFLA_VXLAN_LOCAL
]);
3031 conf
.saddr
.sa
.sa_family
= AF_INET
;
3032 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
3033 if (!IS_ENABLED(CONFIG_IPV6
))
3034 return -EPFNOSUPPORT
;
3036 /* TODO: respect scope id */
3037 conf
.saddr
.sin6
.sin6_addr
= nla_get_in6_addr(data
[IFLA_VXLAN_LOCAL6
]);
3038 conf
.saddr
.sa
.sa_family
= AF_INET6
;
3041 if (data
[IFLA_VXLAN_LINK
])
3042 conf
.remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]);
3044 if (data
[IFLA_VXLAN_TOS
])
3045 conf
.tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
3047 if (data
[IFLA_VXLAN_TTL
])
3048 conf
.ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
3050 if (data
[IFLA_VXLAN_LABEL
])
3051 conf
.label
= nla_get_be32(data
[IFLA_VXLAN_LABEL
]) &
3052 IPV6_FLOWLABEL_MASK
;
3054 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
3055 conf
.flags
|= VXLAN_F_LEARN
;
3057 if (data
[IFLA_VXLAN_AGEING
])
3058 conf
.age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
3060 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
3061 conf
.flags
|= VXLAN_F_PROXY
;
3063 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
3064 conf
.flags
|= VXLAN_F_RSC
;
3066 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
3067 conf
.flags
|= VXLAN_F_L2MISS
;
3069 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
3070 conf
.flags
|= VXLAN_F_L3MISS
;
3072 if (data
[IFLA_VXLAN_LIMIT
])
3073 conf
.addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
3075 if (data
[IFLA_VXLAN_COLLECT_METADATA
] &&
3076 nla_get_u8(data
[IFLA_VXLAN_COLLECT_METADATA
]))
3077 conf
.flags
|= VXLAN_F_COLLECT_METADATA
;
3079 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
3080 const struct ifla_vxlan_port_range
*p
3081 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
3082 conf
.port_min
= ntohs(p
->low
);
3083 conf
.port_max
= ntohs(p
->high
);
3086 if (data
[IFLA_VXLAN_PORT
])
3087 conf
.dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
3089 if (data
[IFLA_VXLAN_UDP_CSUM
] &&
3090 !nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
3091 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM_TX
;
3093 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
3094 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
3095 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
3097 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
3098 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
3099 conf
.flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
3101 if (data
[IFLA_VXLAN_REMCSUM_TX
] &&
3102 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_TX
]))
3103 conf
.flags
|= VXLAN_F_REMCSUM_TX
;
3105 if (data
[IFLA_VXLAN_REMCSUM_RX
] &&
3106 nla_get_u8(data
[IFLA_VXLAN_REMCSUM_RX
]))
3107 conf
.flags
|= VXLAN_F_REMCSUM_RX
;
3109 if (data
[IFLA_VXLAN_GBP
])
3110 conf
.flags
|= VXLAN_F_GBP
;
3112 if (data
[IFLA_VXLAN_GPE
])
3113 conf
.flags
|= VXLAN_F_GPE
;
3115 if (data
[IFLA_VXLAN_REMCSUM_NOPARTIAL
])
3116 conf
.flags
|= VXLAN_F_REMCSUM_NOPARTIAL
;
3119 conf
.mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3121 return vxlan_dev_configure(src_net
, dev
, &conf
);
3124 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
3126 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3128 list_del(&vxlan
->next
);
3129 unregister_netdevice_queue(dev
, head
);
3132 static size_t vxlan_get_size(const struct net_device
*dev
)
3135 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
3136 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
3137 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
3138 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
3139 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
3140 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
3141 nla_total_size(sizeof(__be32
)) + /* IFLA_VXLAN_LABEL */
3142 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
3143 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
3144 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
3145 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
3146 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
3147 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_COLLECT_METADATA */
3148 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
3149 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
3150 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
3151 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
3152 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
3153 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3154 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3155 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_TX */
3156 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_REMCSUM_RX */
3160 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
3162 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3163 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3164 struct ifla_vxlan_port_range ports
= {
3165 .low
= htons(vxlan
->cfg
.port_min
),
3166 .high
= htons(vxlan
->cfg
.port_max
),
3169 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, be32_to_cpu(dst
->remote_vni
)))
3170 goto nla_put_failure
;
3172 if (!vxlan_addr_any(&dst
->remote_ip
)) {
3173 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
3174 if (nla_put_in_addr(skb
, IFLA_VXLAN_GROUP
,
3175 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
3176 goto nla_put_failure
;
3177 #if IS_ENABLED(CONFIG_IPV6)
3179 if (nla_put_in6_addr(skb
, IFLA_VXLAN_GROUP6
,
3180 &dst
->remote_ip
.sin6
.sin6_addr
))
3181 goto nla_put_failure
;
3186 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
3187 goto nla_put_failure
;
3189 if (!vxlan_addr_any(&vxlan
->cfg
.saddr
)) {
3190 if (vxlan
->cfg
.saddr
.sa
.sa_family
== AF_INET
) {
3191 if (nla_put_in_addr(skb
, IFLA_VXLAN_LOCAL
,
3192 vxlan
->cfg
.saddr
.sin
.sin_addr
.s_addr
))
3193 goto nla_put_failure
;
3194 #if IS_ENABLED(CONFIG_IPV6)
3196 if (nla_put_in6_addr(skb
, IFLA_VXLAN_LOCAL6
,
3197 &vxlan
->cfg
.saddr
.sin6
.sin6_addr
))
3198 goto nla_put_failure
;
3203 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->cfg
.ttl
) ||
3204 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->cfg
.tos
) ||
3205 nla_put_be32(skb
, IFLA_VXLAN_LABEL
, vxlan
->cfg
.label
) ||
3206 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
3207 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
3208 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
3209 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
3210 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
3211 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
3212 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
3213 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
3214 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
3215 nla_put_u8(skb
, IFLA_VXLAN_COLLECT_METADATA
,
3216 !!(vxlan
->flags
& VXLAN_F_COLLECT_METADATA
)) ||
3217 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->cfg
.age_interval
) ||
3218 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->cfg
.addrmax
) ||
3219 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->cfg
.dst_port
) ||
3220 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
3221 !(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM_TX
)) ||
3222 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
3223 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
3224 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
3225 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)) ||
3226 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_TX
,
3227 !!(vxlan
->flags
& VXLAN_F_REMCSUM_TX
)) ||
3228 nla_put_u8(skb
, IFLA_VXLAN_REMCSUM_RX
,
3229 !!(vxlan
->flags
& VXLAN_F_REMCSUM_RX
)))
3230 goto nla_put_failure
;
3232 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
3233 goto nla_put_failure
;
3235 if (vxlan
->flags
& VXLAN_F_GBP
&&
3236 nla_put_flag(skb
, IFLA_VXLAN_GBP
))
3237 goto nla_put_failure
;
3239 if (vxlan
->flags
& VXLAN_F_GPE
&&
3240 nla_put_flag(skb
, IFLA_VXLAN_GPE
))
3241 goto nla_put_failure
;
3243 if (vxlan
->flags
& VXLAN_F_REMCSUM_NOPARTIAL
&&
3244 nla_put_flag(skb
, IFLA_VXLAN_REMCSUM_NOPARTIAL
))
3245 goto nla_put_failure
;
3253 static struct net
*vxlan_get_link_net(const struct net_device
*dev
)
3255 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
3260 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
3262 .maxtype
= IFLA_VXLAN_MAX
,
3263 .policy
= vxlan_policy
,
3264 .priv_size
= sizeof(struct vxlan_dev
),
3265 .setup
= vxlan_setup
,
3266 .validate
= vxlan_validate
,
3267 .newlink
= vxlan_newlink
,
3268 .dellink
= vxlan_dellink
,
3269 .get_size
= vxlan_get_size
,
3270 .fill_info
= vxlan_fill_info
,
3271 .get_link_net
= vxlan_get_link_net
,
3274 struct net_device
*vxlan_dev_create(struct net
*net
, const char *name
,
3275 u8 name_assign_type
,
3276 struct vxlan_config
*conf
)
3278 struct nlattr
*tb
[IFLA_MAX
+ 1];
3279 struct net_device
*dev
;
3282 memset(&tb
, 0, sizeof(tb
));
3284 dev
= rtnl_create_link(net
, name
, name_assign_type
,
3285 &vxlan_link_ops
, tb
);
3289 err
= vxlan_dev_configure(net
, dev
, conf
);
3292 return ERR_PTR(err
);
3295 err
= rtnl_configure_link(dev
, NULL
);
3297 LIST_HEAD(list_kill
);
3299 vxlan_dellink(dev
, &list_kill
);
3300 unregister_netdevice_many(&list_kill
);
3301 return ERR_PTR(err
);
3306 EXPORT_SYMBOL_GPL(vxlan_dev_create
);
3308 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
3309 struct net_device
*dev
)
3311 struct vxlan_dev
*vxlan
, *next
;
3312 LIST_HEAD(list_kill
);
3314 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3315 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
3317 /* In case we created vxlan device with carrier
3318 * and we loose the carrier due to module unload
3319 * we also need to remove vxlan device. In other
3320 * cases, it's not necessary and remote_ifindex
3321 * is 0 here, so no matches.
3323 if (dst
->remote_ifindex
== dev
->ifindex
)
3324 vxlan_dellink(vxlan
->dev
, &list_kill
);
3327 unregister_netdevice_many(&list_kill
);
3330 static int vxlan_netdevice_event(struct notifier_block
*unused
,
3331 unsigned long event
, void *ptr
)
3333 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3334 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
3336 if (event
== NETDEV_UNREGISTER
)
3337 vxlan_handle_lowerdev_unregister(vn
, dev
);
3338 else if (event
== NETDEV_UDP_TUNNEL_PUSH_INFO
)
3339 vxlan_push_rx_ports(dev
);
3344 static struct notifier_block vxlan_notifier_block __read_mostly
= {
3345 .notifier_call
= vxlan_netdevice_event
,
3348 static __net_init
int vxlan_init_net(struct net
*net
)
3350 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3353 INIT_LIST_HEAD(&vn
->vxlan_list
);
3354 spin_lock_init(&vn
->sock_lock
);
3356 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
3357 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
3362 static void __net_exit
vxlan_exit_net(struct net
*net
)
3364 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
3365 struct vxlan_dev
*vxlan
, *next
;
3366 struct net_device
*dev
, *aux
;
3370 for_each_netdev_safe(net
, dev
, aux
)
3371 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
3372 unregister_netdevice_queue(dev
, &list
);
3374 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
3375 /* If vxlan->dev is in the same netns, it has already been added
3376 * to the list by the previous loop.
3378 if (!net_eq(dev_net(vxlan
->dev
), net
))
3379 unregister_netdevice_queue(vxlan
->dev
, &list
);
3382 unregister_netdevice_many(&list
);
3386 static struct pernet_operations vxlan_net_ops
= {
3387 .init
= vxlan_init_net
,
3388 .exit
= vxlan_exit_net
,
3389 .id
= &vxlan_net_id
,
3390 .size
= sizeof(struct vxlan_net
),
3393 static int __init
vxlan_init_module(void)
3397 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
3399 rc
= register_pernet_subsys(&vxlan_net_ops
);
3403 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
3407 rc
= rtnl_link_register(&vxlan_link_ops
);
3413 unregister_netdevice_notifier(&vxlan_notifier_block
);
3415 unregister_pernet_subsys(&vxlan_net_ops
);
3419 late_initcall(vxlan_init_module
);
3421 static void __exit
vxlan_cleanup_module(void)
3423 rtnl_link_unregister(&vxlan_link_ops
);
3424 unregister_netdevice_notifier(&vxlan_notifier_block
);
3425 unregister_pernet_subsys(&vxlan_net_ops
);
3426 /* rcu_barrier() is called by netns */
3428 module_exit(vxlan_cleanup_module
);
3430 MODULE_LICENSE("GPL");
3431 MODULE_VERSION(VXLAN_VERSION
);
3432 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3433 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3434 MODULE_ALIAS_RTNL_LINK("vxlan");