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/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
31 #include <net/ndisc.h>
33 #include <net/ip_tunnels.h>
36 #include <net/udp_tunnel.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/vxlan.h>
44 #include <net/protocol.h>
45 #include <net/udp_tunnel.h>
46 #if IS_ENABLED(CONFIG_IPV6)
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
53 #define VXLAN_VERSION "0.1"
55 #define PORT_HASH_BITS 8
56 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
57 #define VNI_HASH_BITS 10
58 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59 #define FDB_HASH_BITS 8
60 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61 #define FDB_AGE_DEFAULT 300 /* 5 min */
62 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
64 #define VXLAN_N_VID (1u << 24)
65 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
66 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
68 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
70 /* VXLAN protocol header */
76 /* UDP port for VXLAN traffic.
77 * The IANA assigned port is 4789, but the Linux default is 8472
78 * for compatibility with early adopters.
80 static unsigned short vxlan_port __read_mostly
= 8472;
81 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
82 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
84 static bool log_ecn_error
= true;
85 module_param(log_ecn_error
, bool, 0644);
86 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
88 static int vxlan_net_id
;
90 static const u8 all_zeros_mac
[ETH_ALEN
];
92 /* per-network namespace private data for this module */
94 struct list_head vxlan_list
;
95 struct hlist_head sock_list
[PORT_HASH_SIZE
];
100 struct sockaddr_in sin
;
101 struct sockaddr_in6 sin6
;
106 union vxlan_addr remote_ip
;
110 struct list_head list
;
114 /* Forwarding table entry */
116 struct hlist_node hlist
; /* linked list of entries */
118 unsigned long updated
; /* jiffies */
120 struct list_head remotes
;
121 u16 state
; /* see ndm_state */
122 u8 flags
; /* see ndm_flags */
123 u8 eth_addr
[ETH_ALEN
];
126 /* Pseudo network device */
128 struct hlist_node hlist
; /* vni hash table */
129 struct list_head next
; /* vxlan's per namespace list */
130 struct vxlan_sock
*vn_sock
; /* listening socket */
131 struct net_device
*dev
;
132 struct net
*net
; /* netns for packet i/o */
133 struct vxlan_rdst default_dst
; /* default destination */
134 union vxlan_addr saddr
; /* source address */
136 __u16 port_min
; /* source port range */
138 __u8 tos
; /* TOS override */
140 u32 flags
; /* VXLAN_F_* in vxlan.h */
142 struct work_struct sock_work
;
143 struct work_struct igmp_join
;
144 struct work_struct igmp_leave
;
146 unsigned long age_interval
;
147 struct timer_list age_timer
;
148 spinlock_t hash_lock
;
149 unsigned int addrcnt
;
150 unsigned int addrmax
;
152 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
155 /* salt for hash table */
156 static u32 vxlan_salt __read_mostly
;
157 static struct workqueue_struct
*vxlan_wq
;
159 static void vxlan_sock_work(struct work_struct
*work
);
161 #if IS_ENABLED(CONFIG_IPV6)
163 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
165 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
167 if (a
->sa
.sa_family
== AF_INET6
)
168 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
170 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
173 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
175 if (ipa
->sa
.sa_family
== AF_INET6
)
176 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
178 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
181 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
183 if (ipa
->sa
.sa_family
== AF_INET6
)
184 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
186 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
189 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
191 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
192 nla_memcpy(&ip
->sin6
.sin6_addr
, nla
, sizeof(struct in6_addr
));
193 ip
->sa
.sa_family
= AF_INET6
;
195 } else if (nla_len(nla
) >= sizeof(__be32
)) {
196 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
197 ip
->sa
.sa_family
= AF_INET
;
200 return -EAFNOSUPPORT
;
204 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
205 const union vxlan_addr
*ip
)
207 if (ip
->sa
.sa_family
== AF_INET6
)
208 return nla_put(skb
, attr
, sizeof(struct in6_addr
), &ip
->sin6
.sin6_addr
);
210 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
213 #else /* !CONFIG_IPV6 */
216 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
218 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
221 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
223 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
226 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
228 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
231 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
233 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
234 return -EAFNOSUPPORT
;
235 } else if (nla_len(nla
) >= sizeof(__be32
)) {
236 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
237 ip
->sa
.sa_family
= AF_INET
;
240 return -EAFNOSUPPORT
;
244 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
245 const union vxlan_addr
*ip
)
247 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
251 /* Virtual Network hash table head */
252 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, u32 id
)
254 return &vs
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
257 /* Socket hash table head */
258 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
260 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
262 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
265 /* First remote destination for a forwarding entry.
266 * Guaranteed to be non-NULL because remotes are never deleted.
268 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
270 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
273 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
275 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
278 /* Find VXLAN socket based on network namespace, address family and UDP port */
279 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
,
280 sa_family_t family
, __be16 port
)
282 struct vxlan_sock
*vs
;
284 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
285 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
&&
286 inet_sk(vs
->sock
->sk
)->sk
.sk_family
== family
)
292 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, u32 id
)
294 struct vxlan_dev
*vxlan
;
296 hlist_for_each_entry_rcu(vxlan
, vni_head(vs
, id
), hlist
) {
297 if (vxlan
->default_dst
.remote_vni
== id
)
304 /* Look up VNI in a per net namespace table */
305 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
,
306 sa_family_t family
, __be16 port
)
308 struct vxlan_sock
*vs
;
310 vs
= vxlan_find_sock(net
, family
, port
);
314 return vxlan_vs_find_vni(vs
, id
);
317 /* Fill in neighbour message in skbuff. */
318 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
319 const struct vxlan_fdb
*fdb
,
320 u32 portid
, u32 seq
, int type
, unsigned int flags
,
321 const struct vxlan_rdst
*rdst
)
323 unsigned long now
= jiffies
;
324 struct nda_cacheinfo ci
;
325 struct nlmsghdr
*nlh
;
327 bool send_ip
, send_eth
;
329 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
333 ndm
= nlmsg_data(nlh
);
334 memset(ndm
, 0, sizeof(*ndm
));
336 send_eth
= send_ip
= true;
338 if (type
== RTM_GETNEIGH
) {
339 ndm
->ndm_family
= AF_INET
;
340 send_ip
= !vxlan_addr_any(&rdst
->remote_ip
);
341 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
343 ndm
->ndm_family
= AF_BRIDGE
;
344 ndm
->ndm_state
= fdb
->state
;
345 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
346 ndm
->ndm_flags
= fdb
->flags
;
347 ndm
->ndm_type
= RTN_UNICAST
;
349 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
350 goto nla_put_failure
;
352 if (send_ip
&& vxlan_nla_put_addr(skb
, NDA_DST
, &rdst
->remote_ip
))
353 goto nla_put_failure
;
355 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan
->dst_port
&&
356 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
357 goto nla_put_failure
;
358 if (rdst
->remote_vni
!= vxlan
->default_dst
.remote_vni
&&
359 nla_put_u32(skb
, NDA_VNI
, rdst
->remote_vni
))
360 goto nla_put_failure
;
361 if (rdst
->remote_ifindex
&&
362 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
363 goto nla_put_failure
;
365 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
366 ci
.ndm_confirmed
= 0;
367 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
370 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
371 goto nla_put_failure
;
373 return nlmsg_end(skb
, nlh
);
376 nlmsg_cancel(skb
, nlh
);
380 static inline size_t vxlan_nlmsg_size(void)
382 return NLMSG_ALIGN(sizeof(struct ndmsg
))
383 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
384 + nla_total_size(sizeof(struct in6_addr
)) /* NDA_DST */
385 + nla_total_size(sizeof(__be16
)) /* NDA_PORT */
386 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
388 + nla_total_size(sizeof(struct nda_cacheinfo
));
391 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*fdb
,
392 struct vxlan_rdst
*rd
, int type
)
394 struct net
*net
= dev_net(vxlan
->dev
);
398 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
402 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, rd
);
404 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
405 WARN_ON(err
== -EMSGSIZE
);
410 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
414 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
417 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
419 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
420 struct vxlan_fdb f
= {
423 struct vxlan_rdst remote
= {
424 .remote_ip
= *ipa
, /* goes to NDA_DST */
425 .remote_vni
= VXLAN_N_VID
,
428 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
431 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
433 struct vxlan_fdb f
= {
436 struct vxlan_rdst remote
= { };
438 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
440 vxlan_fdb_notify(vxlan
, &f
, &remote
, RTM_GETNEIGH
);
443 /* Hash Ethernet address */
444 static u32
eth_hash(const unsigned char *addr
)
446 u64 value
= get_unaligned((u64
*)addr
);
448 /* only want 6 bytes */
454 return hash_64(value
, FDB_HASH_BITS
);
457 /* Hash chain to use given mac address */
458 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
461 return &vxlan
->fdb_head
[eth_hash(mac
)];
464 /* Look up Ethernet address in forwarding table */
465 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
468 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
471 hlist_for_each_entry_rcu(f
, head
, hlist
) {
472 if (ether_addr_equal(mac
, f
->eth_addr
))
479 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
484 f
= __vxlan_find_mac(vxlan
, mac
);
491 /* caller should hold vxlan->hash_lock */
492 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
493 union vxlan_addr
*ip
, __be16 port
,
494 __u32 vni
, __u32 ifindex
)
496 struct vxlan_rdst
*rd
;
498 list_for_each_entry(rd
, &f
->remotes
, list
) {
499 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
500 rd
->remote_port
== port
&&
501 rd
->remote_vni
== vni
&&
502 rd
->remote_ifindex
== ifindex
)
509 /* Replace destination of unicast mac */
510 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
511 union vxlan_addr
*ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
513 struct vxlan_rdst
*rd
;
515 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
519 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
523 rd
->remote_port
= port
;
524 rd
->remote_vni
= vni
;
525 rd
->remote_ifindex
= ifindex
;
529 /* Add/update destinations for multicast */
530 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
531 union vxlan_addr
*ip
, __be16 port
, __u32 vni
,
532 __u32 ifindex
, struct vxlan_rdst
**rdp
)
534 struct vxlan_rdst
*rd
;
536 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
540 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
544 rd
->remote_port
= port
;
545 rd
->remote_vni
= vni
;
546 rd
->remote_ifindex
= ifindex
;
548 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
554 static struct sk_buff
**vxlan_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
)
556 struct sk_buff
*p
, **pp
= NULL
;
557 struct vxlanhdr
*vh
, *vh2
;
558 struct ethhdr
*eh
, *eh2
;
559 unsigned int hlen
, off_vx
, off_eth
;
560 const struct packet_offload
*ptype
;
564 off_vx
= skb_gro_offset(skb
);
565 hlen
= off_vx
+ sizeof(*vh
);
566 vh
= skb_gro_header_fast(skb
, off_vx
);
567 if (skb_gro_header_hard(skb
, hlen
)) {
568 vh
= skb_gro_header_slow(skb
, hlen
, off_vx
);
572 skb_gro_pull(skb
, sizeof(struct vxlanhdr
)); /* pull vxlan header */
573 skb_gro_postpull_rcsum(skb
, vh
, sizeof(struct vxlanhdr
));
575 off_eth
= skb_gro_offset(skb
);
576 hlen
= off_eth
+ sizeof(*eh
);
577 eh
= skb_gro_header_fast(skb
, off_eth
);
578 if (skb_gro_header_hard(skb
, hlen
)) {
579 eh
= skb_gro_header_slow(skb
, hlen
, off_eth
);
586 for (p
= *head
; p
; p
= p
->next
) {
587 if (!NAPI_GRO_CB(p
)->same_flow
)
590 vh2
= (struct vxlanhdr
*)(p
->data
+ off_vx
);
591 eh2
= (struct ethhdr
*)(p
->data
+ off_eth
);
592 if (vh
->vx_vni
!= vh2
->vx_vni
|| compare_ether_header(eh
, eh2
)) {
593 NAPI_GRO_CB(p
)->same_flow
= 0;
601 ptype
= gro_find_receive_by_type(type
);
607 skb_gro_pull(skb
, sizeof(*eh
)); /* pull inner eth header */
608 skb_gro_postpull_rcsum(skb
, eh
, sizeof(*eh
));
609 pp
= ptype
->callbacks
.gro_receive(head
, skb
);
614 NAPI_GRO_CB(skb
)->flush
|= flush
;
619 static int vxlan_gro_complete(struct sk_buff
*skb
, int nhoff
)
622 struct packet_offload
*ptype
;
624 int vxlan_len
= sizeof(struct vxlanhdr
) + sizeof(struct ethhdr
);
627 udp_tunnel_gro_complete(skb
, nhoff
);
629 eh
= (struct ethhdr
*)(skb
->data
+ nhoff
+ sizeof(struct vxlanhdr
));
633 ptype
= gro_find_complete_by_type(type
);
635 err
= ptype
->callbacks
.gro_complete(skb
, nhoff
+ vxlan_len
);
641 /* Notify netdevs that UDP port started listening */
642 static void vxlan_notify_add_rx_port(struct vxlan_sock
*vs
)
644 struct net_device
*dev
;
645 struct sock
*sk
= vs
->sock
->sk
;
646 struct net
*net
= sock_net(sk
);
647 sa_family_t sa_family
= sk
->sk_family
;
648 __be16 port
= inet_sk(sk
)->inet_sport
;
651 if (sa_family
== AF_INET
) {
652 err
= udp_add_offload(&vs
->udp_offloads
);
654 pr_warn("vxlan: udp_add_offload failed with status %d\n", err
);
658 for_each_netdev_rcu(net
, dev
) {
659 if (dev
->netdev_ops
->ndo_add_vxlan_port
)
660 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
666 /* Notify netdevs that UDP port is no more listening */
667 static void vxlan_notify_del_rx_port(struct vxlan_sock
*vs
)
669 struct net_device
*dev
;
670 struct sock
*sk
= vs
->sock
->sk
;
671 struct net
*net
= sock_net(sk
);
672 sa_family_t sa_family
= sk
->sk_family
;
673 __be16 port
= inet_sk(sk
)->inet_sport
;
676 for_each_netdev_rcu(net
, dev
) {
677 if (dev
->netdev_ops
->ndo_del_vxlan_port
)
678 dev
->netdev_ops
->ndo_del_vxlan_port(dev
, sa_family
,
683 if (sa_family
== AF_INET
)
684 udp_del_offload(&vs
->udp_offloads
);
687 /* Add new entry to forwarding table -- assumes lock held */
688 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
689 const u8
*mac
, union vxlan_addr
*ip
,
690 __u16 state
, __u16 flags
,
691 __be16 port
, __u32 vni
, __u32 ifindex
,
694 struct vxlan_rdst
*rd
= NULL
;
698 f
= __vxlan_find_mac(vxlan
, mac
);
700 if (flags
& NLM_F_EXCL
) {
701 netdev_dbg(vxlan
->dev
,
702 "lost race to create %pM\n", mac
);
705 if (f
->state
!= state
) {
707 f
->updated
= jiffies
;
710 if (f
->flags
!= ndm_flags
) {
711 f
->flags
= ndm_flags
;
712 f
->updated
= jiffies
;
715 if ((flags
& NLM_F_REPLACE
)) {
716 /* Only change unicasts */
717 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
718 is_zero_ether_addr(f
->eth_addr
))) {
719 int rc
= vxlan_fdb_replace(f
, ip
, port
, vni
,
728 if ((flags
& NLM_F_APPEND
) &&
729 (is_multicast_ether_addr(f
->eth_addr
) ||
730 is_zero_ether_addr(f
->eth_addr
))) {
731 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
,
739 if (!(flags
& NLM_F_CREATE
))
742 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
745 /* Disallow replace to add a multicast entry */
746 if ((flags
& NLM_F_REPLACE
) &&
747 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
750 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
751 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
757 f
->flags
= ndm_flags
;
758 f
->updated
= f
->used
= jiffies
;
759 INIT_LIST_HEAD(&f
->remotes
);
760 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
762 vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
, &rd
);
765 hlist_add_head_rcu(&f
->hlist
,
766 vxlan_fdb_head(vxlan
, mac
));
771 rd
= first_remote_rtnl(f
);
772 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_NEWNEIGH
);
778 static void vxlan_fdb_free(struct rcu_head
*head
)
780 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
781 struct vxlan_rdst
*rd
, *nd
;
783 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
)
788 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
790 netdev_dbg(vxlan
->dev
,
791 "delete %pM\n", f
->eth_addr
);
794 vxlan_fdb_notify(vxlan
, f
, first_remote_rtnl(f
), RTM_DELNEIGH
);
796 hlist_del_rcu(&f
->hlist
);
797 call_rcu(&f
->rcu
, vxlan_fdb_free
);
800 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
801 union vxlan_addr
*ip
, __be16
*port
, u32
*vni
, u32
*ifindex
)
803 struct net
*net
= dev_net(vxlan
->dev
);
807 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
811 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
812 if (remote
->sa
.sa_family
== AF_INET
) {
813 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
814 ip
->sa
.sa_family
= AF_INET
;
815 #if IS_ENABLED(CONFIG_IPV6)
817 ip
->sin6
.sin6_addr
= in6addr_any
;
818 ip
->sa
.sa_family
= AF_INET6
;
824 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
826 *port
= nla_get_be16(tb
[NDA_PORT
]);
828 *port
= vxlan
->dst_port
;
832 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
834 *vni
= nla_get_u32(tb
[NDA_VNI
]);
836 *vni
= vxlan
->default_dst
.remote_vni
;
839 if (tb
[NDA_IFINDEX
]) {
840 struct net_device
*tdev
;
842 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
844 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
845 tdev
= __dev_get_by_index(net
, *ifindex
);
847 return -EADDRNOTAVAIL
;
855 /* Add static entry (via netlink) */
856 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
857 struct net_device
*dev
,
858 const unsigned char *addr
, u16 flags
)
860 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
861 /* struct net *net = dev_net(vxlan->dev); */
867 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
868 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
873 if (tb
[NDA_DST
] == NULL
)
876 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
880 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
!= ip
.sa
.sa_family
)
881 return -EAFNOSUPPORT
;
883 spin_lock_bh(&vxlan
->hash_lock
);
884 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
885 port
, vni
, ifindex
, ndm
->ndm_flags
);
886 spin_unlock_bh(&vxlan
->hash_lock
);
891 /* Delete entry (via netlink) */
892 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
893 struct net_device
*dev
,
894 const unsigned char *addr
)
896 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
898 struct vxlan_rdst
*rd
= NULL
;
904 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
910 spin_lock_bh(&vxlan
->hash_lock
);
911 f
= vxlan_find_mac(vxlan
, addr
);
915 if (!vxlan_addr_any(&ip
)) {
916 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
923 /* remove a destination if it's not the only one on the list,
924 * otherwise destroy the fdb entry
926 if (rd
&& !list_is_singular(&f
->remotes
)) {
927 list_del_rcu(&rd
->list
);
928 vxlan_fdb_notify(vxlan
, f
, rd
, RTM_DELNEIGH
);
933 vxlan_fdb_destroy(vxlan
, f
);
936 spin_unlock_bh(&vxlan
->hash_lock
);
941 /* Dump forwarding table */
942 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
943 struct net_device
*dev
,
944 struct net_device
*filter_dev
, int idx
)
946 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
949 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
953 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
954 struct vxlan_rdst
*rd
;
956 if (idx
< cb
->args
[0])
959 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
960 err
= vxlan_fdb_info(skb
, vxlan
, f
,
961 NETLINK_CB(cb
->skb
).portid
,
976 /* Watch incoming packets to learn mapping between Ethernet address
977 * and Tunnel endpoint.
978 * Return true if packet is bogus and should be droppped.
980 static bool vxlan_snoop(struct net_device
*dev
,
981 union vxlan_addr
*src_ip
, const u8
*src_mac
)
983 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
986 f
= vxlan_find_mac(vxlan
, src_mac
);
988 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
990 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
993 /* Don't migrate static entries, drop packets */
994 if (f
->state
& NUD_NOARP
)
999 "%pM migrated from %pIS to %pIS\n",
1000 src_mac
, &rdst
->remote_ip
, &src_ip
);
1002 rdst
->remote_ip
= *src_ip
;
1003 f
->updated
= jiffies
;
1004 vxlan_fdb_notify(vxlan
, f
, rdst
, RTM_NEWNEIGH
);
1006 /* learned new entry */
1007 spin_lock(&vxlan
->hash_lock
);
1009 /* close off race between vxlan_flush and incoming packets */
1010 if (netif_running(dev
))
1011 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
1013 NLM_F_EXCL
|NLM_F_CREATE
,
1015 vxlan
->default_dst
.remote_vni
,
1017 spin_unlock(&vxlan
->hash_lock
);
1023 /* See if multicast group is already in use by other ID */
1024 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
1026 struct vxlan_dev
*vxlan
;
1028 /* The vxlan_sock is only used by dev, leaving group has
1029 * no effect on other vxlan devices.
1031 if (atomic_read(&dev
->vn_sock
->refcnt
) == 1)
1034 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
1035 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
1038 if (vxlan
->vn_sock
!= dev
->vn_sock
)
1041 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
1042 &dev
->default_dst
.remote_ip
))
1045 if (vxlan
->default_dst
.remote_ifindex
!=
1046 dev
->default_dst
.remote_ifindex
)
1055 static void vxlan_sock_hold(struct vxlan_sock
*vs
)
1057 atomic_inc(&vs
->refcnt
);
1060 void vxlan_sock_release(struct vxlan_sock
*vs
)
1062 struct sock
*sk
= vs
->sock
->sk
;
1063 struct net
*net
= sock_net(sk
);
1064 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1066 if (!atomic_dec_and_test(&vs
->refcnt
))
1069 spin_lock(&vn
->sock_lock
);
1070 hlist_del_rcu(&vs
->hlist
);
1071 vxlan_notify_del_rx_port(vs
);
1072 spin_unlock(&vn
->sock_lock
);
1074 queue_work(vxlan_wq
, &vs
->del_work
);
1076 EXPORT_SYMBOL_GPL(vxlan_sock_release
);
1078 /* Callback to update multicast group membership when first VNI on
1079 * multicast asddress is brought up
1080 * Done as workqueue because ip_mc_join_group acquires RTNL.
1082 static void vxlan_igmp_join(struct work_struct
*work
)
1084 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_join
);
1085 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1086 struct sock
*sk
= vs
->sock
->sk
;
1087 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1088 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1091 if (ip
->sa
.sa_family
== AF_INET
) {
1092 struct ip_mreqn mreq
= {
1093 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1094 .imr_ifindex
= ifindex
,
1097 ip_mc_join_group(sk
, &mreq
);
1098 #if IS_ENABLED(CONFIG_IPV6)
1100 ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
1101 &ip
->sin6
.sin6_addr
);
1106 vxlan_sock_release(vs
);
1107 dev_put(vxlan
->dev
);
1110 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1111 static void vxlan_igmp_leave(struct work_struct
*work
)
1113 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_leave
);
1114 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1115 struct sock
*sk
= vs
->sock
->sk
;
1116 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1117 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1120 if (ip
->sa
.sa_family
== AF_INET
) {
1121 struct ip_mreqn mreq
= {
1122 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1123 .imr_ifindex
= ifindex
,
1126 ip_mc_leave_group(sk
, &mreq
);
1127 #if IS_ENABLED(CONFIG_IPV6)
1129 ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1130 &ip
->sin6
.sin6_addr
);
1136 vxlan_sock_release(vs
);
1137 dev_put(vxlan
->dev
);
1140 /* Callback from net/ipv4/udp.c to receive packets */
1141 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1143 struct vxlan_sock
*vs
;
1144 struct vxlanhdr
*vxh
;
1146 /* Need Vxlan and inner Ethernet header to be present */
1147 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1150 /* Return packets with reserved bits set */
1151 vxh
= (struct vxlanhdr
*)(udp_hdr(skb
) + 1);
1152 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
1153 (vxh
->vx_vni
& htonl(0xff))) {
1154 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1155 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
1159 if (iptunnel_pull_header(skb
, VXLAN_HLEN
, htons(ETH_P_TEB
)))
1162 vs
= rcu_dereference_sk_user_data(sk
);
1166 vs
->rcv(vs
, skb
, vxh
->vx_vni
);
1170 /* Consume bad packet */
1175 /* Return non vxlan pkt */
1179 static void vxlan_rcv(struct vxlan_sock
*vs
,
1180 struct sk_buff
*skb
, __be32 vx_vni
)
1182 struct iphdr
*oip
= NULL
;
1183 struct ipv6hdr
*oip6
= NULL
;
1184 struct vxlan_dev
*vxlan
;
1185 struct pcpu_sw_netstats
*stats
;
1186 union vxlan_addr saddr
;
1189 union vxlan_addr
*remote_ip
;
1191 vni
= ntohl(vx_vni
) >> 8;
1192 /* Is this VNI defined? */
1193 vxlan
= vxlan_vs_find_vni(vs
, vni
);
1197 remote_ip
= &vxlan
->default_dst
.remote_ip
;
1198 skb_reset_mac_header(skb
);
1199 skb_scrub_packet(skb
, !net_eq(vxlan
->net
, dev_net(vxlan
->dev
)));
1200 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1201 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
1203 /* Ignore packet loops (and multicast echo) */
1204 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1207 /* Re-examine inner Ethernet packet */
1208 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1210 saddr
.sin
.sin_addr
.s_addr
= oip
->saddr
;
1211 saddr
.sa
.sa_family
= AF_INET
;
1212 #if IS_ENABLED(CONFIG_IPV6)
1214 oip6
= ipv6_hdr(skb
);
1215 saddr
.sin6
.sin6_addr
= oip6
->saddr
;
1216 saddr
.sa
.sa_family
= AF_INET6
;
1220 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1221 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1224 skb_reset_network_header(skb
);
1227 err
= IP6_ECN_decapsulate(oip6
, skb
);
1229 err
= IP_ECN_decapsulate(oip
, skb
);
1231 if (unlikely(err
)) {
1232 if (log_ecn_error
) {
1234 net_info_ratelimited("non-ECT from %pI6\n",
1237 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1238 &oip
->saddr
, oip
->tos
);
1241 ++vxlan
->dev
->stats
.rx_frame_errors
;
1242 ++vxlan
->dev
->stats
.rx_errors
;
1247 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1248 u64_stats_update_begin(&stats
->syncp
);
1249 stats
->rx_packets
++;
1250 stats
->rx_bytes
+= skb
->len
;
1251 u64_stats_update_end(&stats
->syncp
);
1257 /* Consume bad packet */
1261 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1263 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1264 struct arphdr
*parp
;
1267 struct neighbour
*n
;
1269 if (dev
->flags
& IFF_NOARP
)
1272 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1273 dev
->stats
.tx_dropped
++;
1276 parp
= arp_hdr(skb
);
1278 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1279 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1280 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1281 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1282 parp
->ar_hln
!= dev
->addr_len
||
1285 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1287 arpptr
+= dev
->addr_len
; /* sha */
1288 memcpy(&sip
, arpptr
, sizeof(sip
));
1289 arpptr
+= sizeof(sip
);
1290 arpptr
+= dev
->addr_len
; /* tha */
1291 memcpy(&tip
, arpptr
, sizeof(tip
));
1293 if (ipv4_is_loopback(tip
) ||
1294 ipv4_is_multicast(tip
))
1297 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1300 struct vxlan_fdb
*f
;
1301 struct sk_buff
*reply
;
1303 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1308 f
= vxlan_find_mac(vxlan
, n
->ha
);
1309 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1310 /* bridge-local neighbor */
1315 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1323 skb_reset_mac_header(reply
);
1324 __skb_pull(reply
, skb_network_offset(reply
));
1325 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1326 reply
->pkt_type
= PACKET_HOST
;
1328 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1329 dev
->stats
.rx_dropped
++;
1330 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1331 union vxlan_addr ipa
= {
1332 .sin
.sin_addr
.s_addr
= tip
,
1333 .sin
.sin_family
= AF_INET
,
1336 vxlan_ip_miss(dev
, &ipa
);
1340 return NETDEV_TX_OK
;
1343 #if IS_ENABLED(CONFIG_IPV6)
1344 static struct sk_buff
*vxlan_na_create(struct sk_buff
*request
,
1345 struct neighbour
*n
, bool isrouter
)
1347 struct net_device
*dev
= request
->dev
;
1348 struct sk_buff
*reply
;
1349 struct nd_msg
*ns
, *na
;
1350 struct ipv6hdr
*pip6
;
1352 int na_olen
= 8; /* opt hdr + ETH_ALEN for target */
1359 len
= LL_RESERVED_SPACE(dev
) + sizeof(struct ipv6hdr
) +
1360 sizeof(*na
) + na_olen
+ dev
->needed_tailroom
;
1361 reply
= alloc_skb(len
, GFP_ATOMIC
);
1365 reply
->protocol
= htons(ETH_P_IPV6
);
1367 skb_reserve(reply
, LL_RESERVED_SPACE(request
->dev
));
1368 skb_push(reply
, sizeof(struct ethhdr
));
1369 skb_set_mac_header(reply
, 0);
1371 ns
= (struct nd_msg
*)skb_transport_header(request
);
1373 daddr
= eth_hdr(request
)->h_source
;
1374 ns_olen
= request
->len
- skb_transport_offset(request
) - sizeof(*ns
);
1375 for (i
= 0; i
< ns_olen
-1; i
+= (ns
->opt
[i
+1]<<3)) {
1376 if (ns
->opt
[i
] == ND_OPT_SOURCE_LL_ADDR
) {
1377 daddr
= ns
->opt
+ i
+ sizeof(struct nd_opt_hdr
);
1382 /* Ethernet header */
1383 ether_addr_copy(eth_hdr(reply
)->h_dest
, daddr
);
1384 ether_addr_copy(eth_hdr(reply
)->h_source
, n
->ha
);
1385 eth_hdr(reply
)->h_proto
= htons(ETH_P_IPV6
);
1386 reply
->protocol
= htons(ETH_P_IPV6
);
1388 skb_pull(reply
, sizeof(struct ethhdr
));
1389 skb_set_network_header(reply
, 0);
1390 skb_put(reply
, sizeof(struct ipv6hdr
));
1394 pip6
= ipv6_hdr(reply
);
1395 memset(pip6
, 0, sizeof(struct ipv6hdr
));
1397 pip6
->priority
= ipv6_hdr(request
)->priority
;
1398 pip6
->nexthdr
= IPPROTO_ICMPV6
;
1399 pip6
->hop_limit
= 255;
1400 pip6
->daddr
= ipv6_hdr(request
)->saddr
;
1401 pip6
->saddr
= *(struct in6_addr
*)n
->primary_key
;
1403 skb_pull(reply
, sizeof(struct ipv6hdr
));
1404 skb_set_transport_header(reply
, 0);
1406 na
= (struct nd_msg
*)skb_put(reply
, sizeof(*na
) + na_olen
);
1408 /* Neighbor Advertisement */
1409 memset(na
, 0, sizeof(*na
)+na_olen
);
1410 na
->icmph
.icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
1411 na
->icmph
.icmp6_router
= isrouter
;
1412 na
->icmph
.icmp6_override
= 1;
1413 na
->icmph
.icmp6_solicited
= 1;
1414 na
->target
= ns
->target
;
1415 ether_addr_copy(&na
->opt
[2], n
->ha
);
1416 na
->opt
[0] = ND_OPT_TARGET_LL_ADDR
;
1417 na
->opt
[1] = na_olen
>> 3;
1419 na
->icmph
.icmp6_cksum
= csum_ipv6_magic(&pip6
->saddr
,
1420 &pip6
->daddr
, sizeof(*na
)+na_olen
, IPPROTO_ICMPV6
,
1421 csum_partial(na
, sizeof(*na
)+na_olen
, 0));
1423 pip6
->payload_len
= htons(sizeof(*na
)+na_olen
);
1425 skb_push(reply
, sizeof(struct ipv6hdr
));
1427 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1432 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1434 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1436 const struct ipv6hdr
*iphdr
;
1437 const struct in6_addr
*saddr
, *daddr
;
1438 struct neighbour
*n
;
1439 struct inet6_dev
*in6_dev
;
1441 in6_dev
= __in6_dev_get(dev
);
1445 iphdr
= ipv6_hdr(skb
);
1446 saddr
= &iphdr
->saddr
;
1447 daddr
= &iphdr
->daddr
;
1449 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1450 if (msg
->icmph
.icmp6_code
!= 0 ||
1451 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1454 if (ipv6_addr_loopback(daddr
) ||
1455 ipv6_addr_is_multicast(&msg
->target
))
1458 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &msg
->target
, dev
);
1461 struct vxlan_fdb
*f
;
1462 struct sk_buff
*reply
;
1464 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1469 f
= vxlan_find_mac(vxlan
, n
->ha
);
1470 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1471 /* bridge-local neighbor */
1476 reply
= vxlan_na_create(skb
, n
,
1477 !!(f
? f
->flags
& NTF_ROUTER
: 0));
1484 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1485 dev
->stats
.rx_dropped
++;
1487 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1488 union vxlan_addr ipa
= {
1489 .sin6
.sin6_addr
= msg
->target
,
1490 .sin6
.sin6_family
= AF_INET6
,
1493 vxlan_ip_miss(dev
, &ipa
);
1498 return NETDEV_TX_OK
;
1502 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1504 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1505 struct neighbour
*n
;
1507 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1511 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1516 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1519 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1520 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1521 union vxlan_addr ipa
= {
1522 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1523 .sin
.sin_family
= AF_INET
,
1526 vxlan_ip_miss(dev
, &ipa
);
1532 #if IS_ENABLED(CONFIG_IPV6)
1535 struct ipv6hdr
*pip6
;
1537 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1539 pip6
= ipv6_hdr(skb
);
1540 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1541 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1542 union vxlan_addr ipa
= {
1543 .sin6
.sin6_addr
= pip6
->daddr
,
1544 .sin6
.sin6_family
= AF_INET6
,
1547 vxlan_ip_miss(dev
, &ipa
);
1561 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1563 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1565 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1574 #if IS_ENABLED(CONFIG_IPV6)
1575 static int vxlan6_xmit_skb(struct vxlan_sock
*vs
,
1576 struct dst_entry
*dst
, struct sk_buff
*skb
,
1577 struct net_device
*dev
, struct in6_addr
*saddr
,
1578 struct in6_addr
*daddr
, __u8 prio
, __u8 ttl
,
1579 __be16 src_port
, __be16 dst_port
, __be32 vni
,
1582 struct vxlanhdr
*vxh
;
1585 bool udp_sum
= !udp_get_no_check6_tx(vs
->sock
->sk
);
1587 skb
= udp_tunnel_handle_offloads(skb
, udp_sum
);
1591 skb_scrub_packet(skb
, xnet
);
1593 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1594 + VXLAN_HLEN
+ sizeof(struct ipv6hdr
)
1595 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1597 /* Need space for new headers (invalidates iph ptr) */
1598 err
= skb_cow_head(skb
, min_headroom
);
1602 if (vlan_tx_tag_present(skb
)) {
1603 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
1604 vlan_tx_tag_get(skb
));
1611 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1612 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1615 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
1617 udp_tunnel6_xmit_skb(vs
->sock
, dst
, skb
, dev
, saddr
, daddr
, prio
,
1618 ttl
, src_port
, dst_port
);
1623 int vxlan_xmit_skb(struct vxlan_sock
*vs
,
1624 struct rtable
*rt
, struct sk_buff
*skb
,
1625 __be32 src
, __be32 dst
, __u8 tos
, __u8 ttl
, __be16 df
,
1626 __be16 src_port
, __be16 dst_port
, __be32 vni
, bool xnet
)
1628 struct vxlanhdr
*vxh
;
1631 bool udp_sum
= !vs
->sock
->sk
->sk_no_check_tx
;
1633 skb
= udp_tunnel_handle_offloads(skb
, udp_sum
);
1637 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
1638 + VXLAN_HLEN
+ sizeof(struct iphdr
)
1639 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1641 /* Need space for new headers (invalidates iph ptr) */
1642 err
= skb_cow_head(skb
, min_headroom
);
1646 if (vlan_tx_tag_present(skb
)) {
1647 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
1648 vlan_tx_tag_get(skb
));
1655 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1656 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1659 skb_set_inner_protocol(skb
, htons(ETH_P_TEB
));
1661 return udp_tunnel_xmit_skb(vs
->sock
, rt
, skb
, src
, dst
, tos
,
1662 ttl
, df
, src_port
, dst_port
, xnet
);
1664 EXPORT_SYMBOL_GPL(vxlan_xmit_skb
);
1666 /* Bypass encapsulation if the destination is local */
1667 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1668 struct vxlan_dev
*dst_vxlan
)
1670 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1671 union vxlan_addr loopback
;
1672 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1673 struct net_device
*dev
= skb
->dev
;
1676 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1677 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1678 skb
->pkt_type
= PACKET_HOST
;
1679 skb
->encapsulation
= 0;
1680 skb
->dev
= dst_vxlan
->dev
;
1681 __skb_pull(skb
, skb_network_offset(skb
));
1683 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1684 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1685 loopback
.sa
.sa_family
= AF_INET
;
1686 #if IS_ENABLED(CONFIG_IPV6)
1688 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1689 loopback
.sa
.sa_family
= AF_INET6
;
1693 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1694 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
);
1696 u64_stats_update_begin(&tx_stats
->syncp
);
1697 tx_stats
->tx_packets
++;
1698 tx_stats
->tx_bytes
+= len
;
1699 u64_stats_update_end(&tx_stats
->syncp
);
1701 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1702 u64_stats_update_begin(&rx_stats
->syncp
);
1703 rx_stats
->rx_packets
++;
1704 rx_stats
->rx_bytes
+= len
;
1705 u64_stats_update_end(&rx_stats
->syncp
);
1707 dev
->stats
.rx_dropped
++;
1711 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1712 struct vxlan_rdst
*rdst
, bool did_rsc
)
1714 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1715 struct rtable
*rt
= NULL
;
1716 const struct iphdr
*old_iph
;
1718 union vxlan_addr
*dst
;
1719 __be16 src_port
= 0, dst_port
;
1725 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->dst_port
;
1726 vni
= rdst
->remote_vni
;
1727 dst
= &rdst
->remote_ip
;
1729 if (vxlan_addr_any(dst
)) {
1731 /* short-circuited back to local bridge */
1732 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
1738 old_iph
= ip_hdr(skb
);
1741 if (!ttl
&& vxlan_addr_multicast(dst
))
1746 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1748 src_port
= udp_flow_src_port(dev_net(dev
), skb
, vxlan
->port_min
,
1749 vxlan
->port_max
, true);
1751 if (dst
->sa
.sa_family
== AF_INET
) {
1752 memset(&fl4
, 0, sizeof(fl4
));
1753 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1754 fl4
.flowi4_tos
= RT_TOS(tos
);
1755 fl4
.daddr
= dst
->sin
.sin_addr
.s_addr
;
1756 fl4
.saddr
= vxlan
->saddr
.sin
.sin_addr
.s_addr
;
1758 rt
= ip_route_output_key(vxlan
->net
, &fl4
);
1760 netdev_dbg(dev
, "no route to %pI4\n",
1761 &dst
->sin
.sin_addr
.s_addr
);
1762 dev
->stats
.tx_carrier_errors
++;
1766 if (rt
->dst
.dev
== dev
) {
1767 netdev_dbg(dev
, "circular route to %pI4\n",
1768 &dst
->sin
.sin_addr
.s_addr
);
1769 dev
->stats
.collisions
++;
1773 /* Bypass encapsulation if the destination is local */
1774 if (rt
->rt_flags
& RTCF_LOCAL
&&
1775 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1776 struct vxlan_dev
*dst_vxlan
;
1779 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
1780 dst
->sa
.sa_family
, dst_port
);
1783 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1787 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
1788 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1790 err
= vxlan_xmit_skb(vxlan
->vn_sock
, rt
, skb
,
1791 fl4
.saddr
, dst
->sin
.sin_addr
.s_addr
,
1792 tos
, ttl
, df
, src_port
, dst_port
,
1794 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
)));
1798 iptunnel_xmit_stats(err
, &dev
->stats
, dev
->tstats
);
1799 #if IS_ENABLED(CONFIG_IPV6)
1801 struct sock
*sk
= vxlan
->vn_sock
->sock
->sk
;
1802 struct dst_entry
*ndst
;
1806 memset(&fl6
, 0, sizeof(fl6
));
1807 fl6
.flowi6_oif
= rdst
->remote_ifindex
;
1808 fl6
.daddr
= dst
->sin6
.sin6_addr
;
1809 fl6
.saddr
= vxlan
->saddr
.sin6
.sin6_addr
;
1810 fl6
.flowi6_proto
= IPPROTO_UDP
;
1812 if (ipv6_stub
->ipv6_dst_lookup(sk
, &ndst
, &fl6
)) {
1813 netdev_dbg(dev
, "no route to %pI6\n",
1814 &dst
->sin6
.sin6_addr
);
1815 dev
->stats
.tx_carrier_errors
++;
1819 if (ndst
->dev
== dev
) {
1820 netdev_dbg(dev
, "circular route to %pI6\n",
1821 &dst
->sin6
.sin6_addr
);
1823 dev
->stats
.collisions
++;
1827 /* Bypass encapsulation if the destination is local */
1828 flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
1829 if (flags
& RTF_LOCAL
&&
1830 !(flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1831 struct vxlan_dev
*dst_vxlan
;
1834 dst_vxlan
= vxlan_find_vni(vxlan
->net
, vni
,
1835 dst
->sa
.sa_family
, dst_port
);
1838 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1842 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
1844 err
= vxlan6_xmit_skb(vxlan
->vn_sock
, ndst
, skb
,
1845 dev
, &fl6
.saddr
, &fl6
.daddr
, 0, ttl
,
1846 src_port
, dst_port
, htonl(vni
<< 8),
1847 !net_eq(vxlan
->net
, dev_net(vxlan
->dev
)));
1854 dev
->stats
.tx_dropped
++;
1860 dev
->stats
.tx_errors
++;
1865 /* Transmit local packets over Vxlan
1867 * Outer IP header inherits ECN and DF from inner header.
1868 * Outer UDP destination is the VXLAN assigned port.
1869 * source port is based on hash of flow
1871 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1873 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1875 bool did_rsc
= false;
1876 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
1877 struct vxlan_fdb
*f
;
1879 skb_reset_mac_header(skb
);
1882 if ((vxlan
->flags
& VXLAN_F_PROXY
)) {
1883 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
1884 return arp_reduce(dev
, skb
);
1885 #if IS_ENABLED(CONFIG_IPV6)
1886 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
1887 pskb_may_pull(skb
, sizeof(struct ipv6hdr
)
1888 + sizeof(struct nd_msg
)) &&
1889 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
1892 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1893 if (msg
->icmph
.icmp6_code
== 0 &&
1894 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
1895 return neigh_reduce(dev
, skb
);
1901 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1904 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
1905 (ntohs(eth
->h_proto
) == ETH_P_IP
||
1906 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
1907 did_rsc
= route_shortcircuit(dev
, skb
);
1909 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1913 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
1915 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
1916 !is_multicast_ether_addr(eth
->h_dest
))
1917 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1919 dev
->stats
.tx_dropped
++;
1921 return NETDEV_TX_OK
;
1925 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
1926 struct sk_buff
*skb1
;
1932 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1934 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1938 vxlan_xmit_one(skb
, dev
, fdst
, did_rsc
);
1941 return NETDEV_TX_OK
;
1944 /* Walk the forwarding table and purge stale entries */
1945 static void vxlan_cleanup(unsigned long arg
)
1947 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1948 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1951 if (!netif_running(vxlan
->dev
))
1954 spin_lock_bh(&vxlan
->hash_lock
);
1955 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1956 struct hlist_node
*p
, *n
;
1957 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1959 = container_of(p
, struct vxlan_fdb
, hlist
);
1960 unsigned long timeout
;
1962 if (f
->state
& NUD_PERMANENT
)
1965 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1966 if (time_before_eq(timeout
, jiffies
)) {
1967 netdev_dbg(vxlan
->dev
,
1968 "garbage collect %pM\n",
1970 f
->state
= NUD_STALE
;
1971 vxlan_fdb_destroy(vxlan
, f
);
1972 } else if (time_before(timeout
, next_timer
))
1973 next_timer
= timeout
;
1976 spin_unlock_bh(&vxlan
->hash_lock
);
1978 mod_timer(&vxlan
->age_timer
, next_timer
);
1981 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
1983 __u32 vni
= vxlan
->default_dst
.remote_vni
;
1985 vxlan
->vn_sock
= vs
;
1986 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
1989 /* Setup stats when device is created */
1990 static int vxlan_init(struct net_device
*dev
)
1992 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1993 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
1994 struct vxlan_sock
*vs
;
1995 bool ipv6
= vxlan
->flags
& VXLAN_F_IPV6
;
1997 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
2001 spin_lock(&vn
->sock_lock
);
2002 vs
= vxlan_find_sock(vxlan
->net
, ipv6
? AF_INET6
: AF_INET
,
2005 /* If we have a socket with same port already, reuse it */
2006 atomic_inc(&vs
->refcnt
);
2007 vxlan_vs_add_dev(vs
, vxlan
);
2009 /* otherwise make new socket outside of RTNL */
2011 queue_work(vxlan_wq
, &vxlan
->sock_work
);
2013 spin_unlock(&vn
->sock_lock
);
2018 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
2020 struct vxlan_fdb
*f
;
2022 spin_lock_bh(&vxlan
->hash_lock
);
2023 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
2025 vxlan_fdb_destroy(vxlan
, f
);
2026 spin_unlock_bh(&vxlan
->hash_lock
);
2029 static void vxlan_uninit(struct net_device
*dev
)
2031 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2032 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2034 vxlan_fdb_delete_default(vxlan
);
2037 vxlan_sock_release(vs
);
2038 free_percpu(dev
->tstats
);
2041 /* Start ageing timer and join group when device is brought up */
2042 static int vxlan_open(struct net_device
*dev
)
2044 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2045 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2047 /* socket hasn't been created */
2051 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
2052 vxlan_sock_hold(vs
);
2054 queue_work(vxlan_wq
, &vxlan
->igmp_join
);
2057 if (vxlan
->age_interval
)
2058 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
2063 /* Purge the forwarding table */
2064 static void vxlan_flush(struct vxlan_dev
*vxlan
)
2068 spin_lock_bh(&vxlan
->hash_lock
);
2069 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
2070 struct hlist_node
*p
, *n
;
2071 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
2073 = container_of(p
, struct vxlan_fdb
, hlist
);
2074 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2075 if (!is_zero_ether_addr(f
->eth_addr
))
2076 vxlan_fdb_destroy(vxlan
, f
);
2079 spin_unlock_bh(&vxlan
->hash_lock
);
2082 /* Cleanup timer and forwarding table on shutdown */
2083 static int vxlan_stop(struct net_device
*dev
)
2085 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2086 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2087 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
2089 if (vs
&& vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
2090 !vxlan_group_used(vn
, vxlan
)) {
2091 vxlan_sock_hold(vs
);
2093 queue_work(vxlan_wq
, &vxlan
->igmp_leave
);
2096 del_timer_sync(&vxlan
->age_timer
);
2103 /* Stub, nothing needs to be done. */
2104 static void vxlan_set_multicast_list(struct net_device
*dev
)
2108 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2110 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2111 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2112 struct net_device
*lowerdev
;
2115 lowerdev
= __dev_get_by_index(vxlan
->net
, dst
->remote_ifindex
);
2116 if (lowerdev
== NULL
)
2117 return eth_change_mtu(dev
, new_mtu
);
2119 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
)
2120 max_mtu
= lowerdev
->mtu
- VXLAN6_HEADROOM
;
2122 max_mtu
= lowerdev
->mtu
- VXLAN_HEADROOM
;
2124 if (new_mtu
< 68 || new_mtu
> max_mtu
)
2131 static const struct net_device_ops vxlan_netdev_ops
= {
2132 .ndo_init
= vxlan_init
,
2133 .ndo_uninit
= vxlan_uninit
,
2134 .ndo_open
= vxlan_open
,
2135 .ndo_stop
= vxlan_stop
,
2136 .ndo_start_xmit
= vxlan_xmit
,
2137 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2138 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2139 .ndo_change_mtu
= vxlan_change_mtu
,
2140 .ndo_validate_addr
= eth_validate_addr
,
2141 .ndo_set_mac_address
= eth_mac_addr
,
2142 .ndo_fdb_add
= vxlan_fdb_add
,
2143 .ndo_fdb_del
= vxlan_fdb_delete
,
2144 .ndo_fdb_dump
= vxlan_fdb_dump
,
2147 /* Info for udev, that this is a virtual tunnel endpoint */
2148 static struct device_type vxlan_type
= {
2152 /* Calls the ndo_add_vxlan_port of the caller in order to
2153 * supply the listening VXLAN udp ports. Callers are expected
2154 * to implement the ndo_add_vxlan_port.
2156 void vxlan_get_rx_port(struct net_device
*dev
)
2158 struct vxlan_sock
*vs
;
2159 struct net
*net
= dev_net(dev
);
2160 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2161 sa_family_t sa_family
;
2165 spin_lock(&vn
->sock_lock
);
2166 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2167 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2168 port
= inet_sk(vs
->sock
->sk
)->inet_sport
;
2169 sa_family
= vs
->sock
->sk
->sk_family
;
2170 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
2174 spin_unlock(&vn
->sock_lock
);
2176 EXPORT_SYMBOL_GPL(vxlan_get_rx_port
);
2178 /* Initialize the device structure. */
2179 static void vxlan_setup(struct net_device
*dev
)
2181 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2184 eth_hw_addr_random(dev
);
2186 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
)
2187 dev
->needed_headroom
= ETH_HLEN
+ VXLAN6_HEADROOM
;
2189 dev
->needed_headroom
= ETH_HLEN
+ VXLAN_HEADROOM
;
2191 dev
->netdev_ops
= &vxlan_netdev_ops
;
2192 dev
->destructor
= free_netdev
;
2193 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2195 dev
->tx_queue_len
= 0;
2196 dev
->features
|= NETIF_F_LLTX
;
2197 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2198 dev
->features
|= NETIF_F_RXCSUM
;
2199 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2201 dev
->vlan_features
= dev
->features
;
2202 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2203 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2204 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2205 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2206 netif_keep_dst(dev
);
2207 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2209 INIT_LIST_HEAD(&vxlan
->next
);
2210 spin_lock_init(&vxlan
->hash_lock
);
2211 INIT_WORK(&vxlan
->igmp_join
, vxlan_igmp_join
);
2212 INIT_WORK(&vxlan
->igmp_leave
, vxlan_igmp_leave
);
2213 INIT_WORK(&vxlan
->sock_work
, vxlan_sock_work
);
2215 init_timer_deferrable(&vxlan
->age_timer
);
2216 vxlan
->age_timer
.function
= vxlan_cleanup
;
2217 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2219 vxlan
->dst_port
= htons(vxlan_port
);
2223 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2224 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2227 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2228 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2229 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2230 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2231 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2232 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2233 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2234 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2235 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2236 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2237 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2238 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2239 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2240 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2241 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2242 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2243 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2244 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2245 [IFLA_VXLAN_UDP_CSUM
] = { .type
= NLA_U8
},
2246 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] = { .type
= NLA_U8
},
2247 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] = { .type
= NLA_U8
},
2250 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2252 if (tb
[IFLA_ADDRESS
]) {
2253 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2254 pr_debug("invalid link address (not ethernet)\n");
2258 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2259 pr_debug("invalid all zero ethernet address\n");
2260 return -EADDRNOTAVAIL
;
2267 if (data
[IFLA_VXLAN_ID
]) {
2268 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2269 if (id
>= VXLAN_VID_MASK
)
2273 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2274 const struct ifla_vxlan_port_range
*p
2275 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2277 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2278 pr_debug("port range %u .. %u not valid\n",
2279 ntohs(p
->low
), ntohs(p
->high
));
2287 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2288 struct ethtool_drvinfo
*drvinfo
)
2290 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2291 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2294 static const struct ethtool_ops vxlan_ethtool_ops
= {
2295 .get_drvinfo
= vxlan_get_drvinfo
,
2296 .get_link
= ethtool_op_get_link
,
2299 static void vxlan_del_work(struct work_struct
*work
)
2301 struct vxlan_sock
*vs
= container_of(work
, struct vxlan_sock
, del_work
);
2302 udp_tunnel_sock_release(vs
->sock
);
2306 static struct socket
*vxlan_create_sock(struct net
*net
, bool ipv6
,
2307 __be16 port
, u32 flags
)
2309 struct socket
*sock
;
2310 struct udp_port_cfg udp_conf
;
2313 memset(&udp_conf
, 0, sizeof(udp_conf
));
2316 udp_conf
.family
= AF_INET6
;
2317 udp_conf
.use_udp6_tx_checksums
=
2318 !!(flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
);
2319 udp_conf
.use_udp6_rx_checksums
=
2320 !!(flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
);
2322 udp_conf
.family
= AF_INET
;
2323 udp_conf
.local_ip
.s_addr
= INADDR_ANY
;
2324 udp_conf
.use_udp_checksums
=
2325 !!(flags
& VXLAN_F_UDP_CSUM
);
2328 udp_conf
.local_udp_port
= port
;
2330 /* Open UDP socket */
2331 err
= udp_sock_create(net
, &udp_conf
, &sock
);
2333 return ERR_PTR(err
);
2338 /* Create new listen socket if needed */
2339 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, __be16 port
,
2340 vxlan_rcv_t
*rcv
, void *data
,
2343 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2344 struct vxlan_sock
*vs
;
2345 struct socket
*sock
;
2347 bool ipv6
= !!(flags
& VXLAN_F_IPV6
);
2348 struct udp_tunnel_sock_cfg tunnel_cfg
;
2350 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
);
2352 return ERR_PTR(-ENOMEM
);
2354 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2355 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2357 INIT_WORK(&vs
->del_work
, vxlan_del_work
);
2359 sock
= vxlan_create_sock(net
, ipv6
, port
, flags
);
2362 return ERR_CAST(sock
);
2366 atomic_set(&vs
->refcnt
, 1);
2370 /* Initialize the vxlan udp offloads structure */
2371 vs
->udp_offloads
.port
= port
;
2372 vs
->udp_offloads
.callbacks
.gro_receive
= vxlan_gro_receive
;
2373 vs
->udp_offloads
.callbacks
.gro_complete
= vxlan_gro_complete
;
2375 spin_lock(&vn
->sock_lock
);
2376 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2377 vxlan_notify_add_rx_port(vs
);
2378 spin_unlock(&vn
->sock_lock
);
2380 /* Mark socket as an encapsulation socket. */
2381 tunnel_cfg
.sk_user_data
= vs
;
2382 tunnel_cfg
.encap_type
= 1;
2383 tunnel_cfg
.encap_rcv
= vxlan_udp_encap_recv
;
2384 tunnel_cfg
.encap_destroy
= NULL
;
2386 setup_udp_tunnel_sock(net
, sock
, &tunnel_cfg
);
2391 struct vxlan_sock
*vxlan_sock_add(struct net
*net
, __be16 port
,
2392 vxlan_rcv_t
*rcv
, void *data
,
2393 bool no_share
, u32 flags
)
2395 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2396 struct vxlan_sock
*vs
;
2397 bool ipv6
= flags
& VXLAN_F_IPV6
;
2399 vs
= vxlan_socket_create(net
, port
, rcv
, data
, flags
);
2403 if (no_share
) /* Return error if sharing is not allowed. */
2406 spin_lock(&vn
->sock_lock
);
2407 vs
= vxlan_find_sock(net
, ipv6
? AF_INET6
: AF_INET
, port
);
2410 atomic_inc(&vs
->refcnt
);
2412 vs
= ERR_PTR(-EBUSY
);
2414 spin_unlock(&vn
->sock_lock
);
2417 vs
= ERR_PTR(-EINVAL
);
2421 EXPORT_SYMBOL_GPL(vxlan_sock_add
);
2423 /* Scheduled at device creation to bind to a socket */
2424 static void vxlan_sock_work(struct work_struct
*work
)
2426 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, sock_work
);
2427 struct net
*net
= vxlan
->net
;
2428 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2429 __be16 port
= vxlan
->dst_port
;
2430 struct vxlan_sock
*nvs
;
2432 nvs
= vxlan_sock_add(net
, port
, vxlan_rcv
, NULL
, false, vxlan
->flags
);
2433 spin_lock(&vn
->sock_lock
);
2435 vxlan_vs_add_dev(nvs
, vxlan
);
2436 spin_unlock(&vn
->sock_lock
);
2438 dev_put(vxlan
->dev
);
2441 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
2442 struct nlattr
*tb
[], struct nlattr
*data
[])
2444 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2445 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2446 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2449 bool use_ipv6
= false;
2451 if (!data
[IFLA_VXLAN_ID
])
2454 vxlan
->net
= dev_net(dev
);
2456 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2457 dst
->remote_vni
= vni
;
2459 /* Unless IPv6 is explicitly requested, assume IPv4 */
2460 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2461 if (data
[IFLA_VXLAN_GROUP
]) {
2462 dst
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
2463 } else if (data
[IFLA_VXLAN_GROUP6
]) {
2464 if (!IS_ENABLED(CONFIG_IPV6
))
2465 return -EPFNOSUPPORT
;
2467 nla_memcpy(&dst
->remote_ip
.sin6
.sin6_addr
, data
[IFLA_VXLAN_GROUP6
],
2468 sizeof(struct in6_addr
));
2469 dst
->remote_ip
.sa
.sa_family
= AF_INET6
;
2473 if (data
[IFLA_VXLAN_LOCAL
]) {
2474 vxlan
->saddr
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
2475 vxlan
->saddr
.sa
.sa_family
= AF_INET
;
2476 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
2477 if (!IS_ENABLED(CONFIG_IPV6
))
2478 return -EPFNOSUPPORT
;
2480 /* TODO: respect scope id */
2481 nla_memcpy(&vxlan
->saddr
.sin6
.sin6_addr
, data
[IFLA_VXLAN_LOCAL6
],
2482 sizeof(struct in6_addr
));
2483 vxlan
->saddr
.sa
.sa_family
= AF_INET6
;
2487 if (data
[IFLA_VXLAN_LINK
] &&
2488 (dst
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
2489 struct net_device
*lowerdev
2490 = __dev_get_by_index(net
, dst
->remote_ifindex
);
2493 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2497 #if IS_ENABLED(CONFIG_IPV6)
2499 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2500 if (idev
&& idev
->cnf
.disable_ipv6
) {
2501 pr_info("IPv6 is disabled via sysctl\n");
2504 vxlan
->flags
|= VXLAN_F_IPV6
;
2509 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2511 dev
->needed_headroom
= lowerdev
->hard_header_len
+
2512 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2513 } else if (use_ipv6
)
2514 vxlan
->flags
|= VXLAN_F_IPV6
;
2516 if (data
[IFLA_VXLAN_TOS
])
2517 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
2519 if (data
[IFLA_VXLAN_TTL
])
2520 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
2522 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
2523 vxlan
->flags
|= VXLAN_F_LEARN
;
2525 if (data
[IFLA_VXLAN_AGEING
])
2526 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
2528 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
2530 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
2531 vxlan
->flags
|= VXLAN_F_PROXY
;
2533 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
2534 vxlan
->flags
|= VXLAN_F_RSC
;
2536 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
2537 vxlan
->flags
|= VXLAN_F_L2MISS
;
2539 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
2540 vxlan
->flags
|= VXLAN_F_L3MISS
;
2542 if (data
[IFLA_VXLAN_LIMIT
])
2543 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
2545 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2546 const struct ifla_vxlan_port_range
*p
2547 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2548 vxlan
->port_min
= ntohs(p
->low
);
2549 vxlan
->port_max
= ntohs(p
->high
);
2552 if (data
[IFLA_VXLAN_PORT
])
2553 vxlan
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
2555 if (data
[IFLA_VXLAN_UDP_CSUM
] && nla_get_u8(data
[IFLA_VXLAN_UDP_CSUM
]))
2556 vxlan
->flags
|= VXLAN_F_UDP_CSUM
;
2558 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
2559 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
2560 vxlan
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_TX
;
2562 if (data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
2563 nla_get_u8(data
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
2564 vxlan
->flags
|= VXLAN_F_UDP_ZERO_CSUM6_RX
;
2566 if (vxlan_find_vni(net
, vni
, use_ipv6
? AF_INET6
: AF_INET
,
2568 pr_info("duplicate VNI %u\n", vni
);
2572 dev
->ethtool_ops
= &vxlan_ethtool_ops
;
2574 /* create an fdb entry for a valid default destination */
2575 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2576 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2577 &vxlan
->default_dst
.remote_ip
,
2578 NUD_REACHABLE
|NUD_PERMANENT
,
2579 NLM_F_EXCL
|NLM_F_CREATE
,
2581 vxlan
->default_dst
.remote_vni
,
2582 vxlan
->default_dst
.remote_ifindex
,
2588 err
= register_netdevice(dev
);
2590 vxlan_fdb_delete_default(vxlan
);
2594 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2599 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
2601 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2602 struct vxlan_net
*vn
= net_generic(vxlan
->net
, vxlan_net_id
);
2604 spin_lock(&vn
->sock_lock
);
2605 if (!hlist_unhashed(&vxlan
->hlist
))
2606 hlist_del_rcu(&vxlan
->hlist
);
2607 spin_unlock(&vn
->sock_lock
);
2609 list_del(&vxlan
->next
);
2610 unregister_netdevice_queue(dev
, head
);
2613 static size_t vxlan_get_size(const struct net_device
*dev
)
2616 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
2617 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
2618 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
2619 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
2620 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
2621 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
2622 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
2623 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
2624 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
2625 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
2626 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
2627 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
2628 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
2629 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
2630 nla_total_size(sizeof(__be16
)) + /* IFLA_VXLAN_PORT */
2631 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_CSUM */
2632 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2633 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2637 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2639 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2640 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2641 struct ifla_vxlan_port_range ports
= {
2642 .low
= htons(vxlan
->port_min
),
2643 .high
= htons(vxlan
->port_max
),
2646 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, dst
->remote_vni
))
2647 goto nla_put_failure
;
2649 if (!vxlan_addr_any(&dst
->remote_ip
)) {
2650 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
2651 if (nla_put_be32(skb
, IFLA_VXLAN_GROUP
,
2652 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
2653 goto nla_put_failure
;
2654 #if IS_ENABLED(CONFIG_IPV6)
2656 if (nla_put(skb
, IFLA_VXLAN_GROUP6
, sizeof(struct in6_addr
),
2657 &dst
->remote_ip
.sin6
.sin6_addr
))
2658 goto nla_put_failure
;
2663 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
2664 goto nla_put_failure
;
2666 if (!vxlan_addr_any(&vxlan
->saddr
)) {
2667 if (vxlan
->saddr
.sa
.sa_family
== AF_INET
) {
2668 if (nla_put_be32(skb
, IFLA_VXLAN_LOCAL
,
2669 vxlan
->saddr
.sin
.sin_addr
.s_addr
))
2670 goto nla_put_failure
;
2671 #if IS_ENABLED(CONFIG_IPV6)
2673 if (nla_put(skb
, IFLA_VXLAN_LOCAL6
, sizeof(struct in6_addr
),
2674 &vxlan
->saddr
.sin6
.sin6_addr
))
2675 goto nla_put_failure
;
2680 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
2681 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
2682 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
2683 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
2684 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
2685 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
2686 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
2687 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
2688 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
2689 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
2690 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
2691 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
2692 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
) ||
2693 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->dst_port
) ||
2694 nla_put_u8(skb
, IFLA_VXLAN_UDP_CSUM
,
2695 !!(vxlan
->flags
& VXLAN_F_UDP_CSUM
)) ||
2696 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
,
2697 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_TX
)) ||
2698 nla_put_u8(skb
, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
,
2699 !!(vxlan
->flags
& VXLAN_F_UDP_ZERO_CSUM6_RX
)))
2700 goto nla_put_failure
;
2702 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
2703 goto nla_put_failure
;
2711 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
2713 .maxtype
= IFLA_VXLAN_MAX
,
2714 .policy
= vxlan_policy
,
2715 .priv_size
= sizeof(struct vxlan_dev
),
2716 .setup
= vxlan_setup
,
2717 .validate
= vxlan_validate
,
2718 .newlink
= vxlan_newlink
,
2719 .dellink
= vxlan_dellink
,
2720 .get_size
= vxlan_get_size
,
2721 .fill_info
= vxlan_fill_info
,
2724 static void vxlan_handle_lowerdev_unregister(struct vxlan_net
*vn
,
2725 struct net_device
*dev
)
2727 struct vxlan_dev
*vxlan
, *next
;
2728 LIST_HEAD(list_kill
);
2730 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
2731 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2733 /* In case we created vxlan device with carrier
2734 * and we loose the carrier due to module unload
2735 * we also need to remove vxlan device. In other
2736 * cases, it's not necessary and remote_ifindex
2737 * is 0 here, so no matches.
2739 if (dst
->remote_ifindex
== dev
->ifindex
)
2740 vxlan_dellink(vxlan
->dev
, &list_kill
);
2743 unregister_netdevice_many(&list_kill
);
2746 static int vxlan_lowerdev_event(struct notifier_block
*unused
,
2747 unsigned long event
, void *ptr
)
2749 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2750 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2752 if (event
== NETDEV_UNREGISTER
)
2753 vxlan_handle_lowerdev_unregister(vn
, dev
);
2758 static struct notifier_block vxlan_notifier_block __read_mostly
= {
2759 .notifier_call
= vxlan_lowerdev_event
,
2762 static __net_init
int vxlan_init_net(struct net
*net
)
2764 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2767 INIT_LIST_HEAD(&vn
->vxlan_list
);
2768 spin_lock_init(&vn
->sock_lock
);
2770 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
2771 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
2776 static void __net_exit
vxlan_exit_net(struct net
*net
)
2778 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2779 struct vxlan_dev
*vxlan
, *next
;
2780 struct net_device
*dev
, *aux
;
2784 for_each_netdev_safe(net
, dev
, aux
)
2785 if (dev
->rtnl_link_ops
== &vxlan_link_ops
)
2786 unregister_netdevice_queue(dev
, &list
);
2788 list_for_each_entry_safe(vxlan
, next
, &vn
->vxlan_list
, next
) {
2789 /* If vxlan->dev is in the same netns, it has already been added
2790 * to the list by the previous loop.
2792 if (!net_eq(dev_net(vxlan
->dev
), net
))
2793 unregister_netdevice_queue(dev
, &list
);
2796 unregister_netdevice_many(&list
);
2800 static struct pernet_operations vxlan_net_ops
= {
2801 .init
= vxlan_init_net
,
2802 .exit
= vxlan_exit_net
,
2803 .id
= &vxlan_net_id
,
2804 .size
= sizeof(struct vxlan_net
),
2807 static int __init
vxlan_init_module(void)
2811 vxlan_wq
= alloc_workqueue("vxlan", 0, 0);
2815 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
2817 rc
= register_pernet_subsys(&vxlan_net_ops
);
2821 rc
= register_netdevice_notifier(&vxlan_notifier_block
);
2825 rc
= rtnl_link_register(&vxlan_link_ops
);
2831 unregister_netdevice_notifier(&vxlan_notifier_block
);
2833 unregister_pernet_subsys(&vxlan_net_ops
);
2835 destroy_workqueue(vxlan_wq
);
2838 late_initcall(vxlan_init_module
);
2840 static void __exit
vxlan_cleanup_module(void)
2842 rtnl_link_unregister(&vxlan_link_ops
);
2843 unregister_netdevice_notifier(&vxlan_notifier_block
);
2844 destroy_workqueue(vxlan_wq
);
2845 unregister_pernet_subsys(&vxlan_net_ops
);
2846 /* rcu_barrier() is called by netns */
2848 module_exit(vxlan_cleanup_module
);
2850 MODULE_LICENSE("GPL");
2851 MODULE_VERSION(VXLAN_VERSION
);
2852 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2853 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
2854 MODULE_ALIAS_RTNL_LINK("vxlan");