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/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
43 #if IS_ENABLED(CONFIG_IPV6)
45 #include <net/addrconf.h>
46 #include <net/ip6_tunnel.h>
47 #include <net/ip6_checksum.h>
50 #define VXLAN_VERSION "0.1"
52 #define PORT_HASH_BITS 8
53 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
54 #define VNI_HASH_BITS 10
55 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56 #define FDB_HASH_BITS 8
57 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
61 #define VXLAN_N_VID (1u << 24)
62 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
63 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
65 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
67 /* VXLAN protocol header */
73 /* UDP port for VXLAN traffic.
74 * The IANA assigned port is 4789, but the Linux default is 8472
75 * for compatibility with early adopters.
77 static unsigned short vxlan_port __read_mostly
= 8472;
78 module_param_named(udp_port
, vxlan_port
, ushort
, 0444);
79 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
81 static bool log_ecn_error
= true;
82 module_param(log_ecn_error
, bool, 0644);
83 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
85 static int vxlan_net_id
;
87 static const u8 all_zeros_mac
[ETH_ALEN
];
89 /* per-network namespace private data for this module */
91 struct list_head vxlan_list
;
92 struct hlist_head sock_list
[PORT_HASH_SIZE
];
97 struct sockaddr_in sin
;
98 struct sockaddr_in6 sin6
;
103 union vxlan_addr remote_ip
;
107 struct list_head list
;
111 /* Forwarding table entry */
113 struct hlist_node hlist
; /* linked list of entries */
115 unsigned long updated
; /* jiffies */
117 struct list_head remotes
;
118 u16 state
; /* see ndm_state */
119 u8 flags
; /* see ndm_flags */
120 u8 eth_addr
[ETH_ALEN
];
123 /* Pseudo network device */
125 struct hlist_node hlist
; /* vni hash table */
126 struct list_head next
; /* vxlan's per namespace list */
127 struct vxlan_sock
*vn_sock
; /* listening socket */
128 struct net_device
*dev
;
129 struct vxlan_rdst default_dst
; /* default destination */
130 union vxlan_addr saddr
; /* source address */
132 __u16 port_min
; /* source port range */
134 __u8 tos
; /* TOS override */
136 u32 flags
; /* VXLAN_F_* below */
138 struct work_struct sock_work
;
139 struct work_struct igmp_join
;
140 struct work_struct igmp_leave
;
142 unsigned long age_interval
;
143 struct timer_list age_timer
;
144 spinlock_t hash_lock
;
145 unsigned int addrcnt
;
146 unsigned int addrmax
;
148 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
151 #define VXLAN_F_LEARN 0x01
152 #define VXLAN_F_PROXY 0x02
153 #define VXLAN_F_RSC 0x04
154 #define VXLAN_F_L2MISS 0x08
155 #define VXLAN_F_L3MISS 0x10
156 #define VXLAN_F_IPV6 0x20 /* internal flag */
158 /* salt for hash table */
159 static u32 vxlan_salt __read_mostly
;
160 static struct workqueue_struct
*vxlan_wq
;
162 static void vxlan_sock_work(struct work_struct
*work
);
164 #if IS_ENABLED(CONFIG_IPV6)
166 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
168 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
170 if (a
->sa
.sa_family
== AF_INET6
)
171 return ipv6_addr_equal(&a
->sin6
.sin6_addr
, &b
->sin6
.sin6_addr
);
173 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
176 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
178 if (ipa
->sa
.sa_family
== AF_INET6
)
179 return ipv6_addr_any(&ipa
->sin6
.sin6_addr
);
181 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
184 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
186 if (ipa
->sa
.sa_family
== AF_INET6
)
187 return ipv6_addr_is_multicast(&ipa
->sin6
.sin6_addr
);
189 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
192 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
194 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
195 nla_memcpy(&ip
->sin6
.sin6_addr
, nla
, sizeof(struct in6_addr
));
196 ip
->sa
.sa_family
= AF_INET6
;
198 } else if (nla_len(nla
) >= sizeof(__be32
)) {
199 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
200 ip
->sa
.sa_family
= AF_INET
;
203 return -EAFNOSUPPORT
;
207 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
208 const union vxlan_addr
*ip
)
210 if (ip
->sa
.sa_family
== AF_INET6
)
211 return nla_put(skb
, attr
, sizeof(struct in6_addr
), &ip
->sin6
.sin6_addr
);
213 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
216 #else /* !CONFIG_IPV6 */
219 bool vxlan_addr_equal(const union vxlan_addr
*a
, const union vxlan_addr
*b
)
221 return a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
;
224 static inline bool vxlan_addr_any(const union vxlan_addr
*ipa
)
226 return ipa
->sin
.sin_addr
.s_addr
== htonl(INADDR_ANY
);
229 static inline bool vxlan_addr_multicast(const union vxlan_addr
*ipa
)
231 return IN_MULTICAST(ntohl(ipa
->sin
.sin_addr
.s_addr
));
234 static int vxlan_nla_get_addr(union vxlan_addr
*ip
, struct nlattr
*nla
)
236 if (nla_len(nla
) >= sizeof(struct in6_addr
)) {
237 return -EAFNOSUPPORT
;
238 } else if (nla_len(nla
) >= sizeof(__be32
)) {
239 ip
->sin
.sin_addr
.s_addr
= nla_get_be32(nla
);
240 ip
->sa
.sa_family
= AF_INET
;
243 return -EAFNOSUPPORT
;
247 static int vxlan_nla_put_addr(struct sk_buff
*skb
, int attr
,
248 const union vxlan_addr
*ip
)
250 return nla_put_be32(skb
, attr
, ip
->sin
.sin_addr
.s_addr
);
254 /* Virtual Network hash table head */
255 static inline struct hlist_head
*vni_head(struct vxlan_sock
*vs
, u32 id
)
257 return &vs
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
260 /* Socket hash table head */
261 static inline struct hlist_head
*vs_head(struct net
*net
, __be16 port
)
263 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
265 return &vn
->sock_list
[hash_32(ntohs(port
), PORT_HASH_BITS
)];
268 /* First remote destination for a forwarding entry.
269 * Guaranteed to be non-NULL because remotes are never deleted.
271 static inline struct vxlan_rdst
*first_remote_rcu(struct vxlan_fdb
*fdb
)
273 return list_entry_rcu(fdb
->remotes
.next
, struct vxlan_rdst
, list
);
276 static inline struct vxlan_rdst
*first_remote_rtnl(struct vxlan_fdb
*fdb
)
278 return list_first_entry(&fdb
->remotes
, struct vxlan_rdst
, list
);
281 /* Find VXLAN socket based on network namespace and UDP port */
282 static struct vxlan_sock
*vxlan_find_sock(struct net
*net
, __be16 port
)
284 struct vxlan_sock
*vs
;
286 hlist_for_each_entry_rcu(vs
, vs_head(net
, port
), hlist
) {
287 if (inet_sk(vs
->sock
->sk
)->inet_sport
== port
)
293 static struct vxlan_dev
*vxlan_vs_find_vni(struct vxlan_sock
*vs
, u32 id
)
295 struct vxlan_dev
*vxlan
;
297 hlist_for_each_entry_rcu(vxlan
, vni_head(vs
, id
), hlist
) {
298 if (vxlan
->default_dst
.remote_vni
== id
)
305 /* Look up VNI in a per net namespace table */
306 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
, __be16 port
)
308 struct vxlan_sock
*vs
;
310 vs
= vxlan_find_sock(net
, 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
= NDA_DST
;
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
,
392 struct vxlan_fdb
*fdb
, 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,
403 first_remote_rtnl(fdb
));
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err
== -EMSGSIZE
);
411 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
415 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
418 static void vxlan_ip_miss(struct net_device
*dev
, union vxlan_addr
*ipa
)
420 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
421 struct vxlan_fdb f
= {
424 struct vxlan_rdst remote
= {
425 .remote_ip
= *ipa
, /* goes to NDA_DST */
426 .remote_vni
= VXLAN_N_VID
,
429 INIT_LIST_HEAD(&f
.remotes
);
430 list_add_rcu(&remote
.list
, &f
.remotes
);
432 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
435 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
437 struct vxlan_fdb f
= {
441 INIT_LIST_HEAD(&f
.remotes
);
442 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
444 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
447 /* Hash Ethernet address */
448 static u32
eth_hash(const unsigned char *addr
)
450 u64 value
= get_unaligned((u64
*)addr
);
452 /* only want 6 bytes */
458 return hash_64(value
, FDB_HASH_BITS
);
461 /* Hash chain to use given mac address */
462 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
465 return &vxlan
->fdb_head
[eth_hash(mac
)];
468 /* Look up Ethernet address in forwarding table */
469 static struct vxlan_fdb
*__vxlan_find_mac(struct vxlan_dev
*vxlan
,
473 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
476 hlist_for_each_entry_rcu(f
, head
, hlist
) {
477 if (ether_addr_equal(mac
, f
->eth_addr
))
484 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
489 f
= __vxlan_find_mac(vxlan
, mac
);
496 /* caller should hold vxlan->hash_lock */
497 static struct vxlan_rdst
*vxlan_fdb_find_rdst(struct vxlan_fdb
*f
,
498 union vxlan_addr
*ip
, __be16 port
,
499 __u32 vni
, __u32 ifindex
)
501 struct vxlan_rdst
*rd
;
503 list_for_each_entry(rd
, &f
->remotes
, list
) {
504 if (vxlan_addr_equal(&rd
->remote_ip
, ip
) &&
505 rd
->remote_port
== port
&&
506 rd
->remote_vni
== vni
&&
507 rd
->remote_ifindex
== ifindex
)
514 /* Replace destination of unicast mac */
515 static int vxlan_fdb_replace(struct vxlan_fdb
*f
,
516 union vxlan_addr
*ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
518 struct vxlan_rdst
*rd
;
520 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
524 rd
= list_first_entry_or_null(&f
->remotes
, struct vxlan_rdst
, list
);
528 rd
->remote_port
= port
;
529 rd
->remote_vni
= vni
;
530 rd
->remote_ifindex
= ifindex
;
534 /* Add/update destinations for multicast */
535 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
536 union vxlan_addr
*ip
, __be16 port
, __u32 vni
, __u32 ifindex
)
538 struct vxlan_rdst
*rd
;
540 rd
= vxlan_fdb_find_rdst(f
, ip
, port
, vni
, ifindex
);
544 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
548 rd
->remote_port
= port
;
549 rd
->remote_vni
= vni
;
550 rd
->remote_ifindex
= ifindex
;
552 list_add_tail_rcu(&rd
->list
, &f
->remotes
);
557 /* Notify netdevs that UDP port started listening */
558 static void vxlan_notify_add_rx_port(struct sock
*sk
)
560 struct net_device
*dev
;
561 struct net
*net
= sock_net(sk
);
562 sa_family_t sa_family
= sk
->sk_family
;
563 __be16 port
= inet_sk(sk
)->inet_sport
;
566 for_each_netdev_rcu(net
, dev
) {
567 if (dev
->netdev_ops
->ndo_add_vxlan_port
)
568 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
574 /* Notify netdevs that UDP port is no more listening */
575 static void vxlan_notify_del_rx_port(struct sock
*sk
)
577 struct net_device
*dev
;
578 struct net
*net
= sock_net(sk
);
579 sa_family_t sa_family
= sk
->sk_family
;
580 __be16 port
= inet_sk(sk
)->inet_sport
;
583 for_each_netdev_rcu(net
, dev
) {
584 if (dev
->netdev_ops
->ndo_del_vxlan_port
)
585 dev
->netdev_ops
->ndo_del_vxlan_port(dev
, sa_family
,
591 /* Add new entry to forwarding table -- assumes lock held */
592 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
593 const u8
*mac
, union vxlan_addr
*ip
,
594 __u16 state
, __u16 flags
,
595 __be16 port
, __u32 vni
, __u32 ifindex
,
601 f
= __vxlan_find_mac(vxlan
, mac
);
603 if (flags
& NLM_F_EXCL
) {
604 netdev_dbg(vxlan
->dev
,
605 "lost race to create %pM\n", mac
);
608 if (f
->state
!= state
) {
610 f
->updated
= jiffies
;
613 if (f
->flags
!= ndm_flags
) {
614 f
->flags
= ndm_flags
;
615 f
->updated
= jiffies
;
618 if ((flags
& NLM_F_REPLACE
)) {
619 /* Only change unicasts */
620 if (!(is_multicast_ether_addr(f
->eth_addr
) ||
621 is_zero_ether_addr(f
->eth_addr
))) {
622 int rc
= vxlan_fdb_replace(f
, ip
, port
, vni
,
631 if ((flags
& NLM_F_APPEND
) &&
632 (is_multicast_ether_addr(f
->eth_addr
) ||
633 is_zero_ether_addr(f
->eth_addr
))) {
634 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
641 if (!(flags
& NLM_F_CREATE
))
644 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
647 /* Disallow replace to add a multicast entry */
648 if ((flags
& NLM_F_REPLACE
) &&
649 (is_multicast_ether_addr(mac
) || is_zero_ether_addr(mac
)))
652 netdev_dbg(vxlan
->dev
, "add %pM -> %pIS\n", mac
, ip
);
653 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
659 f
->flags
= ndm_flags
;
660 f
->updated
= f
->used
= jiffies
;
661 INIT_LIST_HEAD(&f
->remotes
);
662 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
664 vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
667 hlist_add_head_rcu(&f
->hlist
,
668 vxlan_fdb_head(vxlan
, mac
));
672 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
677 static void vxlan_fdb_free(struct rcu_head
*head
)
679 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
680 struct vxlan_rdst
*rd
, *nd
;
682 list_for_each_entry_safe(rd
, nd
, &f
->remotes
, list
)
687 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
689 netdev_dbg(vxlan
->dev
,
690 "delete %pM\n", f
->eth_addr
);
693 vxlan_fdb_notify(vxlan
, f
, RTM_DELNEIGH
);
695 hlist_del_rcu(&f
->hlist
);
696 call_rcu(&f
->rcu
, vxlan_fdb_free
);
699 static int vxlan_fdb_parse(struct nlattr
*tb
[], struct vxlan_dev
*vxlan
,
700 union vxlan_addr
*ip
, __be16
*port
, u32
*vni
, u32
*ifindex
)
702 struct net
*net
= dev_net(vxlan
->dev
);
706 err
= vxlan_nla_get_addr(ip
, tb
[NDA_DST
]);
710 union vxlan_addr
*remote
= &vxlan
->default_dst
.remote_ip
;
711 if (remote
->sa
.sa_family
== AF_INET
) {
712 ip
->sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
713 ip
->sa
.sa_family
= AF_INET
;
714 #if IS_ENABLED(CONFIG_IPV6)
716 ip
->sin6
.sin6_addr
= in6addr_any
;
717 ip
->sa
.sa_family
= AF_INET6
;
723 if (nla_len(tb
[NDA_PORT
]) != sizeof(__be16
))
725 *port
= nla_get_be16(tb
[NDA_PORT
]);
727 *port
= vxlan
->dst_port
;
731 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
733 *vni
= nla_get_u32(tb
[NDA_VNI
]);
735 *vni
= vxlan
->default_dst
.remote_vni
;
738 if (tb
[NDA_IFINDEX
]) {
739 struct net_device
*tdev
;
741 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
743 *ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
744 tdev
= dev_get_by_index(net
, *ifindex
);
746 return -EADDRNOTAVAIL
;
755 /* Add static entry (via netlink) */
756 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
757 struct net_device
*dev
,
758 const unsigned char *addr
, u16 flags
)
760 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
761 /* struct net *net = dev_net(vxlan->dev); */
767 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
768 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
773 if (tb
[NDA_DST
] == NULL
)
776 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
780 spin_lock_bh(&vxlan
->hash_lock
);
781 err
= vxlan_fdb_create(vxlan
, addr
, &ip
, ndm
->ndm_state
, flags
,
782 port
, vni
, ifindex
, ndm
->ndm_flags
);
783 spin_unlock_bh(&vxlan
->hash_lock
);
788 /* Delete entry (via netlink) */
789 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
790 struct net_device
*dev
,
791 const unsigned char *addr
)
793 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
795 struct vxlan_rdst
*rd
= NULL
;
801 err
= vxlan_fdb_parse(tb
, vxlan
, &ip
, &port
, &vni
, &ifindex
);
807 spin_lock_bh(&vxlan
->hash_lock
);
808 f
= vxlan_find_mac(vxlan
, addr
);
812 if (!vxlan_addr_any(&ip
)) {
813 rd
= vxlan_fdb_find_rdst(f
, &ip
, port
, vni
, ifindex
);
820 /* remove a destination if it's not the only one on the list,
821 * otherwise destroy the fdb entry
823 if (rd
&& !list_is_singular(&f
->remotes
)) {
824 list_del_rcu(&rd
->list
);
829 vxlan_fdb_destroy(vxlan
, f
);
832 spin_unlock_bh(&vxlan
->hash_lock
);
837 /* Dump forwarding table */
838 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
839 struct net_device
*dev
, int idx
)
841 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
844 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
848 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
849 struct vxlan_rdst
*rd
;
851 if (idx
< cb
->args
[0])
854 list_for_each_entry_rcu(rd
, &f
->remotes
, list
) {
855 err
= vxlan_fdb_info(skb
, vxlan
, f
,
856 NETLINK_CB(cb
->skb
).portid
,
871 /* Watch incoming packets to learn mapping between Ethernet address
872 * and Tunnel endpoint.
873 * Return true if packet is bogus and should be droppped.
875 static bool vxlan_snoop(struct net_device
*dev
,
876 union vxlan_addr
*src_ip
, const u8
*src_mac
)
878 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
881 f
= vxlan_find_mac(vxlan
, src_mac
);
883 struct vxlan_rdst
*rdst
= first_remote_rcu(f
);
885 if (likely(vxlan_addr_equal(&rdst
->remote_ip
, src_ip
)))
888 /* Don't migrate static entries, drop packets */
889 if (f
->state
& NUD_NOARP
)
894 "%pM migrated from %pIS to %pIS\n",
895 src_mac
, &rdst
->remote_ip
, &src_ip
);
897 rdst
->remote_ip
= *src_ip
;
898 f
->updated
= jiffies
;
899 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
901 /* learned new entry */
902 spin_lock(&vxlan
->hash_lock
);
904 /* close off race between vxlan_flush and incoming packets */
905 if (netif_running(dev
))
906 vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
908 NLM_F_EXCL
|NLM_F_CREATE
,
910 vxlan
->default_dst
.remote_vni
,
912 spin_unlock(&vxlan
->hash_lock
);
918 /* See if multicast group is already in use by other ID */
919 static bool vxlan_group_used(struct vxlan_net
*vn
, struct vxlan_dev
*dev
)
921 struct vxlan_dev
*vxlan
;
923 /* The vxlan_sock is only used by dev, leaving group has
924 * no effect on other vxlan devices.
926 if (atomic_read(&dev
->vn_sock
->refcnt
) == 1)
929 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
) {
930 if (!netif_running(vxlan
->dev
) || vxlan
== dev
)
933 if (vxlan
->vn_sock
!= dev
->vn_sock
)
936 if (!vxlan_addr_equal(&vxlan
->default_dst
.remote_ip
,
937 &dev
->default_dst
.remote_ip
))
940 if (vxlan
->default_dst
.remote_ifindex
!=
941 dev
->default_dst
.remote_ifindex
)
950 static void vxlan_sock_hold(struct vxlan_sock
*vs
)
952 atomic_inc(&vs
->refcnt
);
955 void vxlan_sock_release(struct vxlan_sock
*vs
)
957 struct sock
*sk
= vs
->sock
->sk
;
958 struct net
*net
= sock_net(sk
);
959 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
961 if (!atomic_dec_and_test(&vs
->refcnt
))
964 spin_lock(&vn
->sock_lock
);
965 hlist_del_rcu(&vs
->hlist
);
966 rcu_assign_sk_user_data(vs
->sock
->sk
, NULL
);
967 vxlan_notify_del_rx_port(sk
);
968 spin_unlock(&vn
->sock_lock
);
970 queue_work(vxlan_wq
, &vs
->del_work
);
972 EXPORT_SYMBOL_GPL(vxlan_sock_release
);
974 /* Callback to update multicast group membership when first VNI on
975 * multicast asddress is brought up
976 * Done as workqueue because ip_mc_join_group acquires RTNL.
978 static void vxlan_igmp_join(struct work_struct
*work
)
980 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_join
);
981 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
982 struct sock
*sk
= vs
->sock
->sk
;
983 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
984 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
987 if (ip
->sa
.sa_family
== AF_INET
) {
988 struct ip_mreqn mreq
= {
989 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
990 .imr_ifindex
= ifindex
,
993 ip_mc_join_group(sk
, &mreq
);
994 #if IS_ENABLED(CONFIG_IPV6)
996 ipv6_stub
->ipv6_sock_mc_join(sk
, ifindex
,
997 &ip
->sin6
.sin6_addr
);
1002 vxlan_sock_release(vs
);
1003 dev_put(vxlan
->dev
);
1006 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1007 static void vxlan_igmp_leave(struct work_struct
*work
)
1009 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, igmp_leave
);
1010 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1011 struct sock
*sk
= vs
->sock
->sk
;
1012 union vxlan_addr
*ip
= &vxlan
->default_dst
.remote_ip
;
1013 int ifindex
= vxlan
->default_dst
.remote_ifindex
;
1016 if (ip
->sa
.sa_family
== AF_INET
) {
1017 struct ip_mreqn mreq
= {
1018 .imr_multiaddr
.s_addr
= ip
->sin
.sin_addr
.s_addr
,
1019 .imr_ifindex
= ifindex
,
1022 ip_mc_leave_group(sk
, &mreq
);
1023 #if IS_ENABLED(CONFIG_IPV6)
1025 ipv6_stub
->ipv6_sock_mc_drop(sk
, ifindex
,
1026 &ip
->sin6
.sin6_addr
);
1032 vxlan_sock_release(vs
);
1033 dev_put(vxlan
->dev
);
1036 /* Callback from net/ipv4/udp.c to receive packets */
1037 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1039 struct vxlan_sock
*vs
;
1040 struct vxlanhdr
*vxh
;
1043 /* Need Vxlan and inner Ethernet header to be present */
1044 if (!pskb_may_pull(skb
, VXLAN_HLEN
))
1047 /* Return packets with reserved bits set */
1048 vxh
= (struct vxlanhdr
*)(udp_hdr(skb
) + 1);
1049 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
1050 (vxh
->vx_vni
& htonl(0xff))) {
1051 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
1052 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
1056 if (iptunnel_pull_header(skb
, VXLAN_HLEN
, htons(ETH_P_TEB
)))
1059 port
= inet_sk(sk
)->inet_sport
;
1061 vs
= rcu_dereference_sk_user_data(sk
);
1065 vs
->rcv(vs
, skb
, vxh
->vx_vni
);
1069 /* Consume bad packet */
1074 /* Return non vxlan pkt */
1078 static void vxlan_rcv(struct vxlan_sock
*vs
,
1079 struct sk_buff
*skb
, __be32 vx_vni
)
1081 struct iphdr
*oip
= NULL
;
1082 struct ipv6hdr
*oip6
= NULL
;
1083 struct vxlan_dev
*vxlan
;
1084 struct pcpu_sw_netstats
*stats
;
1085 union vxlan_addr saddr
;
1088 union vxlan_addr
*remote_ip
;
1090 vni
= ntohl(vx_vni
) >> 8;
1091 /* Is this VNI defined? */
1092 vxlan
= vxlan_vs_find_vni(vs
, vni
);
1096 remote_ip
= &vxlan
->default_dst
.remote_ip
;
1097 skb_reset_mac_header(skb
);
1098 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
1100 /* Ignore packet loops (and multicast echo) */
1101 if (ether_addr_equal(eth_hdr(skb
)->h_source
, vxlan
->dev
->dev_addr
))
1104 /* Re-examine inner Ethernet packet */
1105 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1107 saddr
.sin
.sin_addr
.s_addr
= oip
->saddr
;
1108 saddr
.sa
.sa_family
= AF_INET
;
1109 #if IS_ENABLED(CONFIG_IPV6)
1111 oip6
= ipv6_hdr(skb
);
1112 saddr
.sin6
.sin6_addr
= oip6
->saddr
;
1113 saddr
.sa
.sa_family
= AF_INET6
;
1117 if ((vxlan
->flags
& VXLAN_F_LEARN
) &&
1118 vxlan_snoop(skb
->dev
, &saddr
, eth_hdr(skb
)->h_source
))
1121 skb_reset_network_header(skb
);
1123 /* If the NIC driver gave us an encapsulated packet with
1124 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1125 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1126 * for us. Otherwise force the upper layers to verify it.
1128 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
|| !skb
->encapsulation
||
1129 !(vxlan
->dev
->features
& NETIF_F_RXCSUM
))
1130 skb
->ip_summed
= CHECKSUM_NONE
;
1132 skb
->encapsulation
= 0;
1135 err
= IP6_ECN_decapsulate(oip6
, skb
);
1137 err
= IP_ECN_decapsulate(oip
, skb
);
1139 if (unlikely(err
)) {
1140 if (log_ecn_error
) {
1142 net_info_ratelimited("non-ECT from %pI6\n",
1145 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1146 &oip
->saddr
, oip
->tos
);
1149 ++vxlan
->dev
->stats
.rx_frame_errors
;
1150 ++vxlan
->dev
->stats
.rx_errors
;
1155 stats
= this_cpu_ptr(vxlan
->dev
->tstats
);
1156 u64_stats_update_begin(&stats
->syncp
);
1157 stats
->rx_packets
++;
1158 stats
->rx_bytes
+= skb
->len
;
1159 u64_stats_update_end(&stats
->syncp
);
1165 /* Consume bad packet */
1169 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1171 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1172 struct arphdr
*parp
;
1175 struct neighbour
*n
;
1177 if (dev
->flags
& IFF_NOARP
)
1180 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
1181 dev
->stats
.tx_dropped
++;
1184 parp
= arp_hdr(skb
);
1186 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
1187 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
1188 parp
->ar_pro
!= htons(ETH_P_IP
) ||
1189 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
1190 parp
->ar_hln
!= dev
->addr_len
||
1193 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
1195 arpptr
+= dev
->addr_len
; /* sha */
1196 memcpy(&sip
, arpptr
, sizeof(sip
));
1197 arpptr
+= sizeof(sip
);
1198 arpptr
+= dev
->addr_len
; /* tha */
1199 memcpy(&tip
, arpptr
, sizeof(tip
));
1201 if (ipv4_is_loopback(tip
) ||
1202 ipv4_is_multicast(tip
))
1205 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
1208 struct vxlan_fdb
*f
;
1209 struct sk_buff
*reply
;
1211 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1216 f
= vxlan_find_mac(vxlan
, n
->ha
);
1217 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1218 /* bridge-local neighbor */
1223 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
1228 skb_reset_mac_header(reply
);
1229 __skb_pull(reply
, skb_network_offset(reply
));
1230 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
1231 reply
->pkt_type
= PACKET_HOST
;
1233 if (netif_rx_ni(reply
) == NET_RX_DROP
)
1234 dev
->stats
.rx_dropped
++;
1235 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1236 union vxlan_addr ipa
= {
1237 .sin
.sin_addr
.s_addr
= tip
,
1238 .sa
.sa_family
= AF_INET
,
1241 vxlan_ip_miss(dev
, &ipa
);
1245 return NETDEV_TX_OK
;
1248 #if IS_ENABLED(CONFIG_IPV6)
1249 static int neigh_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
1251 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1252 struct neighbour
*n
;
1253 union vxlan_addr ipa
;
1254 const struct ipv6hdr
*iphdr
;
1255 const struct in6_addr
*saddr
, *daddr
;
1257 struct inet6_dev
*in6_dev
= NULL
;
1259 in6_dev
= __in6_dev_get(dev
);
1263 if (!pskb_may_pull(skb
, skb
->len
))
1266 iphdr
= ipv6_hdr(skb
);
1267 saddr
= &iphdr
->saddr
;
1268 daddr
= &iphdr
->daddr
;
1270 if (ipv6_addr_loopback(daddr
) ||
1271 ipv6_addr_is_multicast(daddr
))
1274 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1275 if (msg
->icmph
.icmp6_code
!= 0 ||
1276 msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
1279 n
= neigh_lookup(ipv6_stub
->nd_tbl
, daddr
, dev
);
1282 struct vxlan_fdb
*f
;
1284 if (!(n
->nud_state
& NUD_CONNECTED
)) {
1289 f
= vxlan_find_mac(vxlan
, n
->ha
);
1290 if (f
&& vxlan_addr_any(&(first_remote_rcu(f
)->remote_ip
))) {
1291 /* bridge-local neighbor */
1296 ipv6_stub
->ndisc_send_na(dev
, n
, saddr
, &msg
->target
,
1297 !!in6_dev
->cnf
.forwarding
,
1298 true, false, false);
1300 } else if (vxlan
->flags
& VXLAN_F_L3MISS
) {
1301 ipa
.sin6
.sin6_addr
= *daddr
;
1302 ipa
.sa
.sa_family
= AF_INET6
;
1303 vxlan_ip_miss(dev
, &ipa
);
1308 return NETDEV_TX_OK
;
1312 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
1314 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1315 struct neighbour
*n
;
1317 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
1321 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1326 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1329 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
1330 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1331 union vxlan_addr ipa
= {
1332 .sin
.sin_addr
.s_addr
= pip
->daddr
,
1333 .sa
.sa_family
= AF_INET
,
1336 vxlan_ip_miss(dev
, &ipa
);
1342 #if IS_ENABLED(CONFIG_IPV6)
1345 struct ipv6hdr
*pip6
;
1347 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
1349 pip6
= ipv6_hdr(skb
);
1350 n
= neigh_lookup(ipv6_stub
->nd_tbl
, &pip6
->daddr
, dev
);
1351 if (!n
&& (vxlan
->flags
& VXLAN_F_L3MISS
)) {
1352 union vxlan_addr ipa
= {
1353 .sin6
.sin6_addr
= pip6
->daddr
,
1354 .sa
.sa_family
= AF_INET6
,
1357 vxlan_ip_miss(dev
, &ipa
);
1371 diff
= !ether_addr_equal(eth_hdr(skb
)->h_dest
, n
->ha
);
1373 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
1375 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
1384 /* Compute source port for outgoing packet
1385 * first choice to use L4 flow hash since it will spread
1386 * better and maybe available from hardware
1387 * secondary choice is to use jhash on the Ethernet header
1389 __be16
vxlan_src_port(__u16 port_min
, __u16 port_max
, struct sk_buff
*skb
)
1391 unsigned int range
= (port_max
- port_min
) + 1;
1394 hash
= skb_get_hash(skb
);
1396 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
1397 (__force u32
) skb
->protocol
);
1399 return htons((((u64
) hash
* range
) >> 32) + port_min
);
1401 EXPORT_SYMBOL_GPL(vxlan_src_port
);
1403 static int handle_offloads(struct sk_buff
*skb
)
1405 if (skb_is_gso(skb
)) {
1406 int err
= skb_unclone(skb
, GFP_ATOMIC
);
1410 skb_shinfo(skb
)->gso_type
|= SKB_GSO_UDP_TUNNEL
;
1411 } else if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
1412 skb
->ip_summed
= CHECKSUM_NONE
;
1417 #if IS_ENABLED(CONFIG_IPV6)
1418 static int vxlan6_xmit_skb(struct vxlan_sock
*vs
,
1419 struct dst_entry
*dst
, struct sk_buff
*skb
,
1420 struct net_device
*dev
, struct in6_addr
*saddr
,
1421 struct in6_addr
*daddr
, __u8 prio
, __u8 ttl
,
1422 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1424 struct ipv6hdr
*ip6h
;
1425 struct vxlanhdr
*vxh
;
1430 if (!skb
->encapsulation
) {
1431 skb_reset_inner_headers(skb
);
1432 skb
->encapsulation
= 1;
1435 skb_scrub_packet(skb
, false);
1437 min_headroom
= LL_RESERVED_SPACE(dst
->dev
) + dst
->header_len
1438 + VXLAN_HLEN
+ sizeof(struct ipv6hdr
)
1439 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1441 /* Need space for new headers (invalidates iph ptr) */
1442 err
= skb_cow_head(skb
, min_headroom
);
1446 if (vlan_tx_tag_present(skb
)) {
1447 if (WARN_ON(!__vlan_put_tag(skb
,
1449 vlan_tx_tag_get(skb
))))
1455 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1456 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1459 __skb_push(skb
, sizeof(*uh
));
1460 skb_reset_transport_header(skb
);
1463 uh
->dest
= dst_port
;
1464 uh
->source
= src_port
;
1466 uh
->len
= htons(skb
->len
);
1469 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1470 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1472 skb_dst_set(skb
, dst
);
1474 if (!skb_is_gso(skb
) && !(dst
->dev
->features
& NETIF_F_IPV6_CSUM
)) {
1475 __wsum csum
= skb_checksum(skb
, 0, skb
->len
, 0);
1476 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1477 uh
->check
= csum_ipv6_magic(saddr
, daddr
, skb
->len
,
1480 uh
->check
= CSUM_MANGLED_0
;
1482 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1483 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1484 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1485 uh
->check
= ~csum_ipv6_magic(saddr
, daddr
,
1486 skb
->len
, IPPROTO_UDP
, 0);
1489 __skb_push(skb
, sizeof(*ip6h
));
1490 skb_reset_network_header(skb
);
1491 ip6h
= ipv6_hdr(skb
);
1493 ip6h
->priority
= prio
;
1494 ip6h
->flow_lbl
[0] = 0;
1495 ip6h
->flow_lbl
[1] = 0;
1496 ip6h
->flow_lbl
[2] = 0;
1497 ip6h
->payload_len
= htons(skb
->len
);
1498 ip6h
->nexthdr
= IPPROTO_UDP
;
1499 ip6h
->hop_limit
= ttl
;
1500 ip6h
->daddr
= *daddr
;
1501 ip6h
->saddr
= *saddr
;
1503 err
= handle_offloads(skb
);
1507 ip6tunnel_xmit(skb
, dev
);
1512 int vxlan_xmit_skb(struct vxlan_sock
*vs
,
1513 struct rtable
*rt
, struct sk_buff
*skb
,
1514 __be32 src
, __be32 dst
, __u8 tos
, __u8 ttl
, __be16 df
,
1515 __be16 src_port
, __be16 dst_port
, __be32 vni
)
1517 struct vxlanhdr
*vxh
;
1522 if (!skb
->encapsulation
) {
1523 skb_reset_inner_headers(skb
);
1524 skb
->encapsulation
= 1;
1527 min_headroom
= LL_RESERVED_SPACE(rt
->dst
.dev
) + rt
->dst
.header_len
1528 + VXLAN_HLEN
+ sizeof(struct iphdr
)
1529 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
1531 /* Need space for new headers (invalidates iph ptr) */
1532 err
= skb_cow_head(skb
, min_headroom
);
1536 if (vlan_tx_tag_present(skb
)) {
1537 if (WARN_ON(!__vlan_put_tag(skb
,
1539 vlan_tx_tag_get(skb
))))
1545 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1546 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1549 __skb_push(skb
, sizeof(*uh
));
1550 skb_reset_transport_header(skb
);
1553 uh
->dest
= dst_port
;
1554 uh
->source
= src_port
;
1556 uh
->len
= htons(skb
->len
);
1559 err
= handle_offloads(skb
);
1563 return iptunnel_xmit(rt
, skb
, src
, dst
, IPPROTO_UDP
, tos
, ttl
, df
,
1566 EXPORT_SYMBOL_GPL(vxlan_xmit_skb
);
1568 /* Bypass encapsulation if the destination is local */
1569 static void vxlan_encap_bypass(struct sk_buff
*skb
, struct vxlan_dev
*src_vxlan
,
1570 struct vxlan_dev
*dst_vxlan
)
1572 struct pcpu_sw_netstats
*tx_stats
, *rx_stats
;
1573 union vxlan_addr loopback
;
1574 union vxlan_addr
*remote_ip
= &dst_vxlan
->default_dst
.remote_ip
;
1576 tx_stats
= this_cpu_ptr(src_vxlan
->dev
->tstats
);
1577 rx_stats
= this_cpu_ptr(dst_vxlan
->dev
->tstats
);
1578 skb
->pkt_type
= PACKET_HOST
;
1579 skb
->encapsulation
= 0;
1580 skb
->dev
= dst_vxlan
->dev
;
1581 __skb_pull(skb
, skb_network_offset(skb
));
1583 if (remote_ip
->sa
.sa_family
== AF_INET
) {
1584 loopback
.sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1585 loopback
.sa
.sa_family
= AF_INET
;
1586 #if IS_ENABLED(CONFIG_IPV6)
1588 loopback
.sin6
.sin6_addr
= in6addr_loopback
;
1589 loopback
.sa
.sa_family
= AF_INET6
;
1593 if (dst_vxlan
->flags
& VXLAN_F_LEARN
)
1594 vxlan_snoop(skb
->dev
, &loopback
, eth_hdr(skb
)->h_source
);
1596 u64_stats_update_begin(&tx_stats
->syncp
);
1597 tx_stats
->tx_packets
++;
1598 tx_stats
->tx_bytes
+= skb
->len
;
1599 u64_stats_update_end(&tx_stats
->syncp
);
1601 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
1602 u64_stats_update_begin(&rx_stats
->syncp
);
1603 rx_stats
->rx_packets
++;
1604 rx_stats
->rx_bytes
+= skb
->len
;
1605 u64_stats_update_end(&rx_stats
->syncp
);
1607 skb
->dev
->stats
.rx_dropped
++;
1611 static void vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
1612 struct vxlan_rdst
*rdst
, bool did_rsc
)
1614 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1615 struct rtable
*rt
= NULL
;
1616 const struct iphdr
*old_iph
;
1618 union vxlan_addr
*dst
;
1619 __be16 src_port
= 0, dst_port
;
1625 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan
->dst_port
;
1626 vni
= rdst
->remote_vni
;
1627 dst
= &rdst
->remote_ip
;
1629 if (vxlan_addr_any(dst
)) {
1631 /* short-circuited back to local bridge */
1632 vxlan_encap_bypass(skb
, vxlan
, vxlan
);
1638 old_iph
= ip_hdr(skb
);
1641 if (!ttl
&& vxlan_addr_multicast(dst
))
1646 tos
= ip_tunnel_get_dsfield(old_iph
, skb
);
1648 src_port
= vxlan_src_port(vxlan
->port_min
, vxlan
->port_max
, skb
);
1650 if (dst
->sa
.sa_family
== AF_INET
) {
1651 memset(&fl4
, 0, sizeof(fl4
));
1652 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1653 fl4
.flowi4_tos
= RT_TOS(tos
);
1654 fl4
.daddr
= dst
->sin
.sin_addr
.s_addr
;
1655 fl4
.saddr
= vxlan
->saddr
.sin
.sin_addr
.s_addr
;
1657 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1659 netdev_dbg(dev
, "no route to %pI4\n",
1660 &dst
->sin
.sin_addr
.s_addr
);
1661 dev
->stats
.tx_carrier_errors
++;
1665 if (rt
->dst
.dev
== dev
) {
1666 netdev_dbg(dev
, "circular route to %pI4\n",
1667 &dst
->sin
.sin_addr
.s_addr
);
1668 dev
->stats
.collisions
++;
1672 /* Bypass encapsulation if the destination is local */
1673 if (rt
->rt_flags
& RTCF_LOCAL
&&
1674 !(rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1675 struct vxlan_dev
*dst_vxlan
;
1678 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
, dst_port
);
1681 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1685 tos
= ip_tunnel_ecn_encap(tos
, old_iph
, skb
);
1686 ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1688 err
= vxlan_xmit_skb(vxlan
->vn_sock
, rt
, skb
,
1689 fl4
.saddr
, dst
->sin
.sin_addr
.s_addr
,
1690 tos
, ttl
, df
, src_port
, dst_port
,
1695 iptunnel_xmit_stats(err
, &dev
->stats
, dev
->tstats
);
1696 #if IS_ENABLED(CONFIG_IPV6)
1698 struct sock
*sk
= vxlan
->vn_sock
->sock
->sk
;
1699 struct dst_entry
*ndst
;
1703 memset(&fl6
, 0, sizeof(fl6
));
1704 fl6
.flowi6_oif
= rdst
->remote_ifindex
;
1705 fl6
.daddr
= dst
->sin6
.sin6_addr
;
1706 fl6
.saddr
= vxlan
->saddr
.sin6
.sin6_addr
;
1707 fl6
.flowi6_proto
= IPPROTO_UDP
;
1709 if (ipv6_stub
->ipv6_dst_lookup(sk
, &ndst
, &fl6
)) {
1710 netdev_dbg(dev
, "no route to %pI6\n",
1711 &dst
->sin6
.sin6_addr
);
1712 dev
->stats
.tx_carrier_errors
++;
1716 if (ndst
->dev
== dev
) {
1717 netdev_dbg(dev
, "circular route to %pI6\n",
1718 &dst
->sin6
.sin6_addr
);
1720 dev
->stats
.collisions
++;
1724 /* Bypass encapsulation if the destination is local */
1725 flags
= ((struct rt6_info
*)ndst
)->rt6i_flags
;
1726 if (flags
& RTF_LOCAL
&&
1727 !(flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))) {
1728 struct vxlan_dev
*dst_vxlan
;
1731 dst_vxlan
= vxlan_find_vni(dev_net(dev
), vni
, dst_port
);
1734 vxlan_encap_bypass(skb
, vxlan
, dst_vxlan
);
1738 ttl
= ttl
? : ip6_dst_hoplimit(ndst
);
1740 err
= vxlan6_xmit_skb(vxlan
->vn_sock
, ndst
, skb
,
1741 dev
, &fl6
.saddr
, &fl6
.daddr
, 0, ttl
,
1742 src_port
, dst_port
, htonl(vni
<< 8));
1749 dev
->stats
.tx_dropped
++;
1755 dev
->stats
.tx_errors
++;
1760 /* Transmit local packets over Vxlan
1762 * Outer IP header inherits ECN and DF from inner header.
1763 * Outer UDP destination is the VXLAN assigned port.
1764 * source port is based on hash of flow
1766 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1768 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1770 bool did_rsc
= false;
1771 struct vxlan_rdst
*rdst
, *fdst
= NULL
;
1772 struct vxlan_fdb
*f
;
1774 skb_reset_mac_header(skb
);
1777 if ((vxlan
->flags
& VXLAN_F_PROXY
)) {
1778 if (ntohs(eth
->h_proto
) == ETH_P_ARP
)
1779 return arp_reduce(dev
, skb
);
1780 #if IS_ENABLED(CONFIG_IPV6)
1781 else if (ntohs(eth
->h_proto
) == ETH_P_IPV6
&&
1782 skb
->len
>= sizeof(struct ipv6hdr
) + sizeof(struct nd_msg
) &&
1783 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
1786 msg
= (struct nd_msg
*)skb_transport_header(skb
);
1787 if (msg
->icmph
.icmp6_code
== 0 &&
1788 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
)
1789 return neigh_reduce(dev
, skb
);
1794 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1797 if (f
&& (f
->flags
& NTF_ROUTER
) && (vxlan
->flags
& VXLAN_F_RSC
) &&
1798 (ntohs(eth
->h_proto
) == ETH_P_IP
||
1799 ntohs(eth
->h_proto
) == ETH_P_IPV6
)) {
1800 did_rsc
= route_shortcircuit(dev
, skb
);
1802 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1806 f
= vxlan_find_mac(vxlan
, all_zeros_mac
);
1808 if ((vxlan
->flags
& VXLAN_F_L2MISS
) &&
1809 !is_multicast_ether_addr(eth
->h_dest
))
1810 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1812 dev
->stats
.tx_dropped
++;
1814 return NETDEV_TX_OK
;
1818 list_for_each_entry_rcu(rdst
, &f
->remotes
, list
) {
1819 struct sk_buff
*skb1
;
1825 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1827 vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1831 vxlan_xmit_one(skb
, dev
, fdst
, did_rsc
);
1834 return NETDEV_TX_OK
;
1837 /* Walk the forwarding table and purge stale entries */
1838 static void vxlan_cleanup(unsigned long arg
)
1840 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1841 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1844 if (!netif_running(vxlan
->dev
))
1847 spin_lock_bh(&vxlan
->hash_lock
);
1848 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1849 struct hlist_node
*p
, *n
;
1850 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1852 = container_of(p
, struct vxlan_fdb
, hlist
);
1853 unsigned long timeout
;
1855 if (f
->state
& NUD_PERMANENT
)
1858 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1859 if (time_before_eq(timeout
, jiffies
)) {
1860 netdev_dbg(vxlan
->dev
,
1861 "garbage collect %pM\n",
1863 f
->state
= NUD_STALE
;
1864 vxlan_fdb_destroy(vxlan
, f
);
1865 } else if (time_before(timeout
, next_timer
))
1866 next_timer
= timeout
;
1869 spin_unlock_bh(&vxlan
->hash_lock
);
1871 mod_timer(&vxlan
->age_timer
, next_timer
);
1874 static void vxlan_vs_add_dev(struct vxlan_sock
*vs
, struct vxlan_dev
*vxlan
)
1876 __u32 vni
= vxlan
->default_dst
.remote_vni
;
1878 vxlan
->vn_sock
= vs
;
1879 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(vs
, vni
));
1882 /* Setup stats when device is created */
1883 static int vxlan_init(struct net_device
*dev
)
1885 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1886 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
1887 struct vxlan_sock
*vs
;
1890 dev
->tstats
= alloc_percpu(struct pcpu_sw_netstats
);
1894 for_each_possible_cpu(i
) {
1895 struct pcpu_sw_netstats
*vxlan_stats
;
1896 vxlan_stats
= per_cpu_ptr(dev
->tstats
, i
);
1897 u64_stats_init(&vxlan_stats
->syncp
);
1901 spin_lock(&vn
->sock_lock
);
1902 vs
= vxlan_find_sock(dev_net(dev
), vxlan
->dst_port
);
1904 /* If we have a socket with same port already, reuse it */
1905 atomic_inc(&vs
->refcnt
);
1906 vxlan_vs_add_dev(vs
, vxlan
);
1908 /* otherwise make new socket outside of RTNL */
1910 queue_work(vxlan_wq
, &vxlan
->sock_work
);
1912 spin_unlock(&vn
->sock_lock
);
1917 static void vxlan_fdb_delete_default(struct vxlan_dev
*vxlan
)
1919 struct vxlan_fdb
*f
;
1921 spin_lock_bh(&vxlan
->hash_lock
);
1922 f
= __vxlan_find_mac(vxlan
, all_zeros_mac
);
1924 vxlan_fdb_destroy(vxlan
, f
);
1925 spin_unlock_bh(&vxlan
->hash_lock
);
1928 static void vxlan_uninit(struct net_device
*dev
)
1930 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1931 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1933 vxlan_fdb_delete_default(vxlan
);
1936 vxlan_sock_release(vs
);
1937 free_percpu(dev
->tstats
);
1940 /* Start ageing timer and join group when device is brought up */
1941 static int vxlan_open(struct net_device
*dev
)
1943 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1944 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1946 /* socket hasn't been created */
1950 if (vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
)) {
1951 vxlan_sock_hold(vs
);
1953 queue_work(vxlan_wq
, &vxlan
->igmp_join
);
1956 if (vxlan
->age_interval
)
1957 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
1962 /* Purge the forwarding table */
1963 static void vxlan_flush(struct vxlan_dev
*vxlan
)
1967 spin_lock_bh(&vxlan
->hash_lock
);
1968 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1969 struct hlist_node
*p
, *n
;
1970 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1972 = container_of(p
, struct vxlan_fdb
, hlist
);
1973 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1974 if (!is_zero_ether_addr(f
->eth_addr
))
1975 vxlan_fdb_destroy(vxlan
, f
);
1978 spin_unlock_bh(&vxlan
->hash_lock
);
1981 /* Cleanup timer and forwarding table on shutdown */
1982 static int vxlan_stop(struct net_device
*dev
)
1984 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
1985 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1986 struct vxlan_sock
*vs
= vxlan
->vn_sock
;
1988 if (vs
&& vxlan_addr_multicast(&vxlan
->default_dst
.remote_ip
) &&
1989 !vxlan_group_used(vn
, vxlan
)) {
1990 vxlan_sock_hold(vs
);
1992 queue_work(vxlan_wq
, &vxlan
->igmp_leave
);
1995 del_timer_sync(&vxlan
->age_timer
);
2002 /* Stub, nothing needs to be done. */
2003 static void vxlan_set_multicast_list(struct net_device
*dev
)
2007 static int vxlan_change_mtu(struct net_device
*dev
, int new_mtu
)
2009 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2010 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2011 struct net_device
*lowerdev
;
2014 lowerdev
= __dev_get_by_index(dev_net(dev
), dst
->remote_ifindex
);
2015 if (lowerdev
== NULL
)
2016 return eth_change_mtu(dev
, new_mtu
);
2018 if (dst
->remote_ip
.sa
.sa_family
== AF_INET6
)
2019 max_mtu
= lowerdev
->mtu
- VXLAN6_HEADROOM
;
2021 max_mtu
= lowerdev
->mtu
- VXLAN_HEADROOM
;
2023 if (new_mtu
< 68 || new_mtu
> max_mtu
)
2030 static const struct net_device_ops vxlan_netdev_ops
= {
2031 .ndo_init
= vxlan_init
,
2032 .ndo_uninit
= vxlan_uninit
,
2033 .ndo_open
= vxlan_open
,
2034 .ndo_stop
= vxlan_stop
,
2035 .ndo_start_xmit
= vxlan_xmit
,
2036 .ndo_get_stats64
= ip_tunnel_get_stats64
,
2037 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
2038 .ndo_change_mtu
= vxlan_change_mtu
,
2039 .ndo_validate_addr
= eth_validate_addr
,
2040 .ndo_set_mac_address
= eth_mac_addr
,
2041 .ndo_fdb_add
= vxlan_fdb_add
,
2042 .ndo_fdb_del
= vxlan_fdb_delete
,
2043 .ndo_fdb_dump
= vxlan_fdb_dump
,
2046 /* Info for udev, that this is a virtual tunnel endpoint */
2047 static struct device_type vxlan_type
= {
2051 /* Calls the ndo_add_vxlan_port of the caller in order to
2052 * supply the listening VXLAN udp ports. Callers are expected
2053 * to implement the ndo_add_vxlan_port.
2055 void vxlan_get_rx_port(struct net_device
*dev
)
2057 struct vxlan_sock
*vs
;
2058 struct net
*net
= dev_net(dev
);
2059 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2060 sa_family_t sa_family
;
2064 spin_lock(&vn
->sock_lock
);
2065 for (i
= 0; i
< PORT_HASH_SIZE
; ++i
) {
2066 hlist_for_each_entry_rcu(vs
, &vn
->sock_list
[i
], hlist
) {
2067 port
= inet_sk(vs
->sock
->sk
)->inet_sport
;
2068 sa_family
= vs
->sock
->sk
->sk_family
;
2069 dev
->netdev_ops
->ndo_add_vxlan_port(dev
, sa_family
,
2073 spin_unlock(&vn
->sock_lock
);
2075 EXPORT_SYMBOL_GPL(vxlan_get_rx_port
);
2077 /* Initialize the device structure. */
2078 static void vxlan_setup(struct net_device
*dev
)
2080 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2084 eth_hw_addr_random(dev
);
2086 if (vxlan
->default_dst
.remote_ip
.sa
.sa_family
== AF_INET6
)
2087 dev
->hard_header_len
= ETH_HLEN
+ VXLAN6_HEADROOM
;
2089 dev
->hard_header_len
= ETH_HLEN
+ VXLAN_HEADROOM
;
2091 dev
->netdev_ops
= &vxlan_netdev_ops
;
2092 dev
->destructor
= free_netdev
;
2093 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
2095 dev
->tx_queue_len
= 0;
2096 dev
->features
|= NETIF_F_LLTX
;
2097 dev
->features
|= NETIF_F_NETNS_LOCAL
;
2098 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2099 dev
->features
|= NETIF_F_RXCSUM
;
2100 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
2102 dev
->vlan_features
= dev
->features
;
2103 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2104 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2105 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
2106 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
2107 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
2108 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2110 INIT_LIST_HEAD(&vxlan
->next
);
2111 spin_lock_init(&vxlan
->hash_lock
);
2112 INIT_WORK(&vxlan
->igmp_join
, vxlan_igmp_join
);
2113 INIT_WORK(&vxlan
->igmp_leave
, vxlan_igmp_leave
);
2114 INIT_WORK(&vxlan
->sock_work
, vxlan_sock_work
);
2116 init_timer_deferrable(&vxlan
->age_timer
);
2117 vxlan
->age_timer
.function
= vxlan_cleanup
;
2118 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
2120 inet_get_local_port_range(dev_net(dev
), &low
, &high
);
2121 vxlan
->port_min
= low
;
2122 vxlan
->port_max
= high
;
2123 vxlan
->dst_port
= htons(vxlan_port
);
2127 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
2128 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
2131 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
2132 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
2133 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
2134 [IFLA_VXLAN_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2135 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
2136 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
2137 [IFLA_VXLAN_LOCAL6
] = { .len
= sizeof(struct in6_addr
) },
2138 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
2139 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
2140 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
2141 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
2142 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
2143 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
2144 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
2145 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
2146 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
2147 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
2148 [IFLA_VXLAN_PORT
] = { .type
= NLA_U16
},
2151 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
2153 if (tb
[IFLA_ADDRESS
]) {
2154 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
2155 pr_debug("invalid link address (not ethernet)\n");
2159 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
2160 pr_debug("invalid all zero ethernet address\n");
2161 return -EADDRNOTAVAIL
;
2168 if (data
[IFLA_VXLAN_ID
]) {
2169 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2170 if (id
>= VXLAN_VID_MASK
)
2174 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2175 const struct ifla_vxlan_port_range
*p
2176 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2178 if (ntohs(p
->high
) < ntohs(p
->low
)) {
2179 pr_debug("port range %u .. %u not valid\n",
2180 ntohs(p
->low
), ntohs(p
->high
));
2188 static void vxlan_get_drvinfo(struct net_device
*netdev
,
2189 struct ethtool_drvinfo
*drvinfo
)
2191 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
2192 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
2195 static const struct ethtool_ops vxlan_ethtool_ops
= {
2196 .get_drvinfo
= vxlan_get_drvinfo
,
2197 .get_link
= ethtool_op_get_link
,
2200 static void vxlan_del_work(struct work_struct
*work
)
2202 struct vxlan_sock
*vs
= container_of(work
, struct vxlan_sock
, del_work
);
2204 sk_release_kernel(vs
->sock
->sk
);
2208 #if IS_ENABLED(CONFIG_IPV6)
2209 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2210 * could be used for both IPv4 and IPv6 communications, but
2211 * users may set bindv6only=1.
2213 static struct socket
*create_v6_sock(struct net
*net
, __be16 port
)
2216 struct socket
*sock
;
2217 struct sockaddr_in6 vxlan_addr
= {
2218 .sin6_family
= AF_INET6
,
2223 rc
= sock_create_kern(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2225 pr_debug("UDPv6 socket create failed\n");
2229 /* Put in proper namespace */
2231 sk_change_net(sk
, net
);
2233 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_V6ONLY
,
2234 (char *)&val
, sizeof(val
));
2235 rc
= kernel_bind(sock
, (struct sockaddr
*)&vxlan_addr
,
2236 sizeof(struct sockaddr_in6
));
2238 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2239 &vxlan_addr
.sin6_addr
, ntohs(vxlan_addr
.sin6_port
), rc
);
2240 sk_release_kernel(sk
);
2243 /* At this point, IPv6 module should have been loaded in
2244 * sock_create_kern().
2248 /* Disable multicast loopback */
2249 inet_sk(sk
)->mc_loop
= 0;
2255 static struct socket
*create_v6_sock(struct net
*net
, __be16 port
)
2257 return ERR_PTR(-EPFNOSUPPORT
);
2261 static struct socket
*create_v4_sock(struct net
*net
, __be16 port
)
2264 struct socket
*sock
;
2265 struct sockaddr_in vxlan_addr
= {
2266 .sin_family
= AF_INET
,
2267 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2272 /* Create UDP socket for encapsulation receive. */
2273 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
2275 pr_debug("UDP socket create failed\n");
2279 /* Put in proper namespace */
2281 sk_change_net(sk
, net
);
2283 rc
= kernel_bind(sock
, (struct sockaddr
*) &vxlan_addr
,
2284 sizeof(vxlan_addr
));
2286 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2287 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
2288 sk_release_kernel(sk
);
2292 /* Disable multicast loopback */
2293 inet_sk(sk
)->mc_loop
= 0;
2297 /* Create new listen socket if needed */
2298 static struct vxlan_sock
*vxlan_socket_create(struct net
*net
, __be16 port
,
2299 vxlan_rcv_t
*rcv
, void *data
, bool ipv6
)
2301 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2302 struct vxlan_sock
*vs
;
2303 struct socket
*sock
;
2307 vs
= kmalloc(sizeof(*vs
), GFP_KERNEL
);
2309 return ERR_PTR(-ENOMEM
);
2311 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
2312 INIT_HLIST_HEAD(&vs
->vni_list
[h
]);
2314 INIT_WORK(&vs
->del_work
, vxlan_del_work
);
2317 sock
= create_v6_sock(net
, port
);
2319 sock
= create_v4_sock(net
, port
);
2322 return ERR_CAST(sock
);
2327 atomic_set(&vs
->refcnt
, 1);
2330 rcu_assign_sk_user_data(vs
->sock
->sk
, vs
);
2332 spin_lock(&vn
->sock_lock
);
2333 hlist_add_head_rcu(&vs
->hlist
, vs_head(net
, port
));
2334 vxlan_notify_add_rx_port(sk
);
2335 spin_unlock(&vn
->sock_lock
);
2337 /* Mark socket as an encapsulation socket. */
2338 udp_sk(sk
)->encap_type
= 1;
2339 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
2340 #if IS_ENABLED(CONFIG_IPV6)
2342 ipv6_stub
->udpv6_encap_enable();
2350 struct vxlan_sock
*vxlan_sock_add(struct net
*net
, __be16 port
,
2351 vxlan_rcv_t
*rcv
, void *data
,
2352 bool no_share
, bool ipv6
)
2354 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2355 struct vxlan_sock
*vs
;
2357 vs
= vxlan_socket_create(net
, port
, rcv
, data
, ipv6
);
2361 if (no_share
) /* Return error if sharing is not allowed. */
2364 spin_lock(&vn
->sock_lock
);
2365 vs
= vxlan_find_sock(net
, port
);
2368 atomic_inc(&vs
->refcnt
);
2370 vs
= ERR_PTR(-EBUSY
);
2372 spin_unlock(&vn
->sock_lock
);
2375 vs
= ERR_PTR(-EINVAL
);
2379 EXPORT_SYMBOL_GPL(vxlan_sock_add
);
2381 /* Scheduled at device creation to bind to a socket */
2382 static void vxlan_sock_work(struct work_struct
*work
)
2384 struct vxlan_dev
*vxlan
= container_of(work
, struct vxlan_dev
, sock_work
);
2385 struct net
*net
= dev_net(vxlan
->dev
);
2386 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2387 __be16 port
= vxlan
->dst_port
;
2388 struct vxlan_sock
*nvs
;
2390 nvs
= vxlan_sock_add(net
, port
, vxlan_rcv
, NULL
, false, vxlan
->flags
& VXLAN_F_IPV6
);
2391 spin_lock(&vn
->sock_lock
);
2393 vxlan_vs_add_dev(nvs
, vxlan
);
2394 spin_unlock(&vn
->sock_lock
);
2396 dev_put(vxlan
->dev
);
2399 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
2400 struct nlattr
*tb
[], struct nlattr
*data
[])
2402 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2403 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2404 struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2407 bool use_ipv6
= false;
2409 if (!data
[IFLA_VXLAN_ID
])
2412 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
2413 dst
->remote_vni
= vni
;
2415 if (data
[IFLA_VXLAN_GROUP
]) {
2416 dst
->remote_ip
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
2417 dst
->remote_ip
.sa
.sa_family
= AF_INET
;
2418 } else if (data
[IFLA_VXLAN_GROUP6
]) {
2419 if (!IS_ENABLED(CONFIG_IPV6
))
2420 return -EPFNOSUPPORT
;
2422 nla_memcpy(&dst
->remote_ip
.sin6
.sin6_addr
, data
[IFLA_VXLAN_GROUP6
],
2423 sizeof(struct in6_addr
));
2424 dst
->remote_ip
.sa
.sa_family
= AF_INET6
;
2428 if (data
[IFLA_VXLAN_LOCAL
]) {
2429 vxlan
->saddr
.sin
.sin_addr
.s_addr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
2430 vxlan
->saddr
.sa
.sa_family
= AF_INET
;
2431 } else if (data
[IFLA_VXLAN_LOCAL6
]) {
2432 if (!IS_ENABLED(CONFIG_IPV6
))
2433 return -EPFNOSUPPORT
;
2435 /* TODO: respect scope id */
2436 nla_memcpy(&vxlan
->saddr
.sin6
.sin6_addr
, data
[IFLA_VXLAN_LOCAL6
],
2437 sizeof(struct in6_addr
));
2438 vxlan
->saddr
.sa
.sa_family
= AF_INET6
;
2442 if (data
[IFLA_VXLAN_LINK
] &&
2443 (dst
->remote_ifindex
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
2444 struct net_device
*lowerdev
2445 = __dev_get_by_index(net
, dst
->remote_ifindex
);
2448 pr_info("ifindex %d does not exist\n", dst
->remote_ifindex
);
2452 #if IS_ENABLED(CONFIG_IPV6)
2454 struct inet6_dev
*idev
= __in6_dev_get(lowerdev
);
2455 if (idev
&& idev
->cnf
.disable_ipv6
) {
2456 pr_info("IPv6 is disabled via sysctl\n");
2459 vxlan
->flags
|= VXLAN_F_IPV6
;
2464 dev
->mtu
= lowerdev
->mtu
- (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2466 /* update header length based on lower device */
2467 dev
->hard_header_len
= lowerdev
->hard_header_len
+
2468 (use_ipv6
? VXLAN6_HEADROOM
: VXLAN_HEADROOM
);
2471 if (data
[IFLA_VXLAN_TOS
])
2472 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
2474 if (data
[IFLA_VXLAN_TTL
])
2475 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
2477 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
2478 vxlan
->flags
|= VXLAN_F_LEARN
;
2480 if (data
[IFLA_VXLAN_AGEING
])
2481 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
2483 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
2485 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
2486 vxlan
->flags
|= VXLAN_F_PROXY
;
2488 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
2489 vxlan
->flags
|= VXLAN_F_RSC
;
2491 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
2492 vxlan
->flags
|= VXLAN_F_L2MISS
;
2494 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
2495 vxlan
->flags
|= VXLAN_F_L3MISS
;
2497 if (data
[IFLA_VXLAN_LIMIT
])
2498 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
2500 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
2501 const struct ifla_vxlan_port_range
*p
2502 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
2503 vxlan
->port_min
= ntohs(p
->low
);
2504 vxlan
->port_max
= ntohs(p
->high
);
2507 if (data
[IFLA_VXLAN_PORT
])
2508 vxlan
->dst_port
= nla_get_be16(data
[IFLA_VXLAN_PORT
]);
2510 if (vxlan_find_vni(net
, vni
, vxlan
->dst_port
)) {
2511 pr_info("duplicate VNI %u\n", vni
);
2515 SET_ETHTOOL_OPS(dev
, &vxlan_ethtool_ops
);
2517 /* create an fdb entry for a valid default destination */
2518 if (!vxlan_addr_any(&vxlan
->default_dst
.remote_ip
)) {
2519 err
= vxlan_fdb_create(vxlan
, all_zeros_mac
,
2520 &vxlan
->default_dst
.remote_ip
,
2521 NUD_REACHABLE
|NUD_PERMANENT
,
2522 NLM_F_EXCL
|NLM_F_CREATE
,
2524 vxlan
->default_dst
.remote_vni
,
2525 vxlan
->default_dst
.remote_ifindex
,
2531 err
= register_netdevice(dev
);
2533 vxlan_fdb_delete_default(vxlan
);
2537 list_add(&vxlan
->next
, &vn
->vxlan_list
);
2542 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
2544 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
2545 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2547 spin_lock(&vn
->sock_lock
);
2548 if (!hlist_unhashed(&vxlan
->hlist
))
2549 hlist_del_rcu(&vxlan
->hlist
);
2550 spin_unlock(&vn
->sock_lock
);
2552 list_del(&vxlan
->next
);
2553 unregister_netdevice_queue(dev
, head
);
2556 static size_t vxlan_get_size(const struct net_device
*dev
)
2559 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
2560 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_GROUP{6} */
2561 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
2562 nla_total_size(sizeof(struct in6_addr
)) + /* IFLA_VXLAN_LOCAL{6} */
2563 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
2564 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
2565 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
2566 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
2567 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
2568 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
2569 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
2570 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
2571 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
2572 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
2573 nla_total_size(sizeof(__be16
))+ /* IFLA_VXLAN_PORT */
2577 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2579 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2580 const struct vxlan_rdst
*dst
= &vxlan
->default_dst
;
2581 struct ifla_vxlan_port_range ports
= {
2582 .low
= htons(vxlan
->port_min
),
2583 .high
= htons(vxlan
->port_max
),
2586 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, dst
->remote_vni
))
2587 goto nla_put_failure
;
2589 if (!vxlan_addr_any(&dst
->remote_ip
)) {
2590 if (dst
->remote_ip
.sa
.sa_family
== AF_INET
) {
2591 if (nla_put_be32(skb
, IFLA_VXLAN_GROUP
,
2592 dst
->remote_ip
.sin
.sin_addr
.s_addr
))
2593 goto nla_put_failure
;
2594 #if IS_ENABLED(CONFIG_IPV6)
2596 if (nla_put(skb
, IFLA_VXLAN_GROUP6
, sizeof(struct in6_addr
),
2597 &dst
->remote_ip
.sin6
.sin6_addr
))
2598 goto nla_put_failure
;
2603 if (dst
->remote_ifindex
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, dst
->remote_ifindex
))
2604 goto nla_put_failure
;
2606 if (!vxlan_addr_any(&vxlan
->saddr
)) {
2607 if (vxlan
->saddr
.sa
.sa_family
== AF_INET
) {
2608 if (nla_put_be32(skb
, IFLA_VXLAN_LOCAL
,
2609 vxlan
->saddr
.sin
.sin_addr
.s_addr
))
2610 goto nla_put_failure
;
2611 #if IS_ENABLED(CONFIG_IPV6)
2613 if (nla_put(skb
, IFLA_VXLAN_LOCAL6
, sizeof(struct in6_addr
),
2614 &vxlan
->saddr
.sin6
.sin6_addr
))
2615 goto nla_put_failure
;
2620 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
2621 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
2622 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
2623 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
2624 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
2625 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
2626 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
2627 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
2628 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
2629 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
2630 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
2631 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
2632 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
) ||
2633 nla_put_be16(skb
, IFLA_VXLAN_PORT
, vxlan
->dst_port
))
2634 goto nla_put_failure
;
2636 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
2637 goto nla_put_failure
;
2645 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
2647 .maxtype
= IFLA_VXLAN_MAX
,
2648 .policy
= vxlan_policy
,
2649 .priv_size
= sizeof(struct vxlan_dev
),
2650 .setup
= vxlan_setup
,
2651 .validate
= vxlan_validate
,
2652 .newlink
= vxlan_newlink
,
2653 .dellink
= vxlan_dellink
,
2654 .get_size
= vxlan_get_size
,
2655 .fill_info
= vxlan_fill_info
,
2658 static __net_init
int vxlan_init_net(struct net
*net
)
2660 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2663 INIT_LIST_HEAD(&vn
->vxlan_list
);
2664 spin_lock_init(&vn
->sock_lock
);
2666 for (h
= 0; h
< PORT_HASH_SIZE
; ++h
)
2667 INIT_HLIST_HEAD(&vn
->sock_list
[h
]);
2672 static __net_exit
void vxlan_exit_net(struct net
*net
)
2674 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
2675 struct vxlan_dev
*vxlan
;
2679 list_for_each_entry(vxlan
, &vn
->vxlan_list
, next
)
2680 unregister_netdevice_queue(vxlan
->dev
, &list
);
2681 unregister_netdevice_many(&list
);
2685 static struct pernet_operations vxlan_net_ops
= {
2686 .init
= vxlan_init_net
,
2687 .exit
= vxlan_exit_net
,
2688 .id
= &vxlan_net_id
,
2689 .size
= sizeof(struct vxlan_net
),
2692 static int __init
vxlan_init_module(void)
2696 vxlan_wq
= alloc_workqueue("vxlan", 0, 0);
2700 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
2702 rc
= register_pernet_device(&vxlan_net_ops
);
2706 rc
= rtnl_link_register(&vxlan_link_ops
);
2713 unregister_pernet_device(&vxlan_net_ops
);
2715 destroy_workqueue(vxlan_wq
);
2718 late_initcall(vxlan_init_module
);
2720 static void __exit
vxlan_cleanup_module(void)
2722 rtnl_link_unregister(&vxlan_link_ops
);
2723 destroy_workqueue(vxlan_wq
);
2724 unregister_pernet_device(&vxlan_net_ops
);
2727 module_exit(vxlan_cleanup_module
);
2729 MODULE_LICENSE("GPL");
2730 MODULE_VERSION(VXLAN_VERSION
);
2731 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2732 MODULE_ALIAS_RTNL_LINK("vxlan");