1 // SPDX-License-Identifier: GPL-2.0-only
3 * VXLAN: Virtual eXtensible Local Area Network
5 * Copyright (c) 2012-2013 Vyatta Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/udp.h>
15 #include <linux/igmp.h>
16 #include <linux/if_ether.h>
17 #include <linux/ethtool.h>
19 #include <net/ndisc.h>
21 #include <net/ipv6_stubs.h>
24 #include <net/rtnetlink.h>
25 #include <net/inet_ecn.h>
26 #include <net/net_namespace.h>
27 #include <net/netns/generic.h>
28 #include <net/tun_proto.h>
29 #include <net/vxlan.h>
30 #include <net/nexthop.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <net/ip6_tunnel.h>
34 #include <net/ip6_checksum.h>
37 #include "vxlan_private.h"
39 #define VXLAN_VERSION "0.1"
41 #define FDB_AGE_DEFAULT 300 /* 5 min */
42 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
44 /* UDP port for VXLAN traffic.
45 * The IANA assigned port is 4789, but the Linux default is 8472
46 * for compatibility with early adopters.
48 static unsigned short vxlan_port __read_mostly = 8472;
49 module_param_named(udp_port, vxlan_port, ushort, 0444);
50 MODULE_PARM_DESC(udp_port, "Destination UDP port");
52 static bool log_ecn_error = true;
53 module_param(log_ecn_error, bool, 0644);
54 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
56 unsigned int vxlan_net_id;
58 const u8 all_zeros_mac[ETH_ALEN + 2];
59 static struct rtnl_link_ops vxlan_link_ops;
61 static int vxlan_sock_add(struct vxlan_dev *vxlan);
63 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
65 /* salt for hash table */
66 static u32 vxlan_salt __read_mostly;
68 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
70 return vs->flags & VXLAN_F_COLLECT_METADATA ||
71 ip_tunnel_collect_metadata();
74 #if IS_ENABLED(CONFIG_IPV6)
75 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
77 if (nla_len(nla) >= sizeof(struct in6_addr)) {
78 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
79 ip->sa.sa_family = AF_INET6;
81 } else if (nla_len(nla) >= sizeof(__be32)) {
82 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
83 ip->sa.sa_family = AF_INET;
90 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
91 const union vxlan_addr *ip)
93 if (ip->sa.sa_family == AF_INET6)
94 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
96 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
99 #else /* !CONFIG_IPV6 */
101 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
103 if (nla_len(nla) >= sizeof(struct in6_addr)) {
104 return -EAFNOSUPPORT;
105 } else if (nla_len(nla) >= sizeof(__be32)) {
106 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
107 ip->sa.sa_family = AF_INET;
110 return -EAFNOSUPPORT;
114 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
115 const union vxlan_addr *ip)
117 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
121 /* Find VXLAN socket based on network namespace, address family, UDP port,
122 * enabled unshareable flags and socket device binding (see l3mdev with
125 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
126 __be16 port, u32 flags, int ifindex)
128 struct vxlan_sock *vs;
130 flags &= VXLAN_F_RCV_FLAGS;
132 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
133 if (inet_sk(vs->sock->sk)->inet_sport == port &&
134 vxlan_get_sk_family(vs) == family &&
135 vs->flags == flags &&
136 vs->sock->sk->sk_bound_dev_if == ifindex)
142 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs,
143 int ifindex, __be32 vni,
144 struct vxlan_vni_node **vninode)
146 struct vxlan_vni_node *vnode;
147 struct vxlan_dev_node *node;
149 /* For flow based devices, map all packets to VNI 0 */
150 if (vs->flags & VXLAN_F_COLLECT_METADATA &&
151 !(vs->flags & VXLAN_F_VNIFILTER))
154 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
158 if (node->vxlan->cfg.flags & VXLAN_F_VNIFILTER) {
159 vnode = vxlan_vnifilter_lookup(node->vxlan, vni);
162 } else if (node->vxlan->default_dst.remote_vni != vni) {
166 if (IS_ENABLED(CONFIG_IPV6)) {
167 const struct vxlan_config *cfg = &node->vxlan->cfg;
169 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
170 cfg->remote_ifindex != ifindex)
182 /* Look up VNI in a per net namespace table */
183 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
184 __be32 vni, sa_family_t family,
185 __be16 port, u32 flags)
187 struct vxlan_sock *vs;
189 vs = vxlan_find_sock(net, family, port, flags, ifindex);
193 return vxlan_vs_find_vni(vs, ifindex, vni, NULL);
196 /* Fill in neighbour message in skbuff. */
197 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
198 const struct vxlan_fdb *fdb,
199 u32 portid, u32 seq, int type, unsigned int flags,
200 const struct vxlan_rdst *rdst)
202 unsigned long now = jiffies;
203 struct nda_cacheinfo ci;
204 bool send_ip, send_eth;
205 struct nlmsghdr *nlh;
211 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
215 ndm = nlmsg_data(nlh);
216 memset(ndm, 0, sizeof(*ndm));
218 send_eth = send_ip = true;
221 nh = rcu_dereference(fdb->nh);
223 nh_family = nexthop_get_family(nh);
228 if (type == RTM_GETNEIGH) {
230 send_ip = !vxlan_addr_any(&rdst->remote_ip);
231 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
233 ndm->ndm_family = nh_family;
235 send_eth = !is_zero_ether_addr(fdb->eth_addr);
237 ndm->ndm_family = AF_BRIDGE;
238 ndm->ndm_state = fdb->state;
239 ndm->ndm_ifindex = vxlan->dev->ifindex;
240 ndm->ndm_flags = fdb->flags;
241 if (rdst && rdst->offloaded)
242 ndm->ndm_flags |= NTF_OFFLOADED;
243 ndm->ndm_type = RTN_UNICAST;
245 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
246 nla_put_s32(skb, NDA_LINK_NETNSID,
247 peernet2id(dev_net(vxlan->dev), vxlan->net)))
248 goto nla_put_failure;
250 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
251 goto nla_put_failure;
253 if (nla_put_u32(skb, NDA_NH_ID, nh_id))
254 goto nla_put_failure;
256 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
258 goto nla_put_failure;
260 if (rdst->remote_port &&
261 rdst->remote_port != vxlan->cfg.dst_port &&
262 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
263 goto nla_put_failure;
264 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
265 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
266 goto nla_put_failure;
267 if (rdst->remote_ifindex &&
268 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
269 goto nla_put_failure;
272 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
273 nla_put_u32(skb, NDA_SRC_VNI,
274 be32_to_cpu(fdb->vni)))
275 goto nla_put_failure;
277 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
278 ci.ndm_confirmed = 0;
279 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
282 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
283 goto nla_put_failure;
289 nlmsg_cancel(skb, nlh);
293 static inline size_t vxlan_nlmsg_size(void)
295 return NLMSG_ALIGN(sizeof(struct ndmsg))
296 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
297 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
298 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
299 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
300 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
301 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
302 + nla_total_size(sizeof(struct nda_cacheinfo));
305 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
306 struct vxlan_rdst *rd, int type)
308 struct net *net = dev_net(vxlan->dev);
312 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
316 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
318 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
319 WARN_ON(err == -EMSGSIZE);
324 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
328 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
331 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
332 const struct vxlan_fdb *fdb,
333 const struct vxlan_rdst *rd,
334 struct netlink_ext_ack *extack,
335 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
337 fdb_info->info.dev = vxlan->dev;
338 fdb_info->info.extack = extack;
339 fdb_info->remote_ip = rd->remote_ip;
340 fdb_info->remote_port = rd->remote_port;
341 fdb_info->remote_vni = rd->remote_vni;
342 fdb_info->remote_ifindex = rd->remote_ifindex;
343 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
344 fdb_info->vni = fdb->vni;
345 fdb_info->offloaded = rd->offloaded;
346 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
349 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
350 struct vxlan_fdb *fdb,
351 struct vxlan_rdst *rd,
353 struct netlink_ext_ack *extack)
355 struct switchdev_notifier_vxlan_fdb_info info;
356 enum switchdev_notifier_type notifier_type;
362 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
363 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
364 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
365 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
367 return notifier_to_errno(ret);
370 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
371 struct vxlan_rdst *rd, int type, bool swdev_notify,
372 struct netlink_ext_ack *extack)
376 if (swdev_notify && rd) {
379 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
385 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
391 __vxlan_fdb_notify(vxlan, fdb, rd, type);
395 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
397 struct vxlan_dev *vxlan = netdev_priv(dev);
398 struct vxlan_fdb f = {
401 struct vxlan_rdst remote = {
402 .remote_ip = *ipa, /* goes to NDA_DST */
403 .remote_vni = cpu_to_be32(VXLAN_N_VID),
406 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
409 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
411 struct vxlan_fdb f = {
414 struct vxlan_rdst remote = { };
416 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
418 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
421 /* Hash Ethernet address */
422 static u32 eth_hash(const unsigned char *addr)
424 u64 value = get_unaligned((u64 *)addr);
426 /* only want 6 bytes */
432 return hash_64(value, FDB_HASH_BITS);
435 u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
437 /* use 1 byte of OUI and 3 bytes of NIC */
438 u32 key = get_unaligned((u32 *)(addr + 2));
440 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
443 u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
445 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
446 return eth_vni_hash(mac, vni);
448 return eth_hash(mac);
451 /* Hash chain to use given mac address */
452 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
453 const u8 *mac, __be32 vni)
455 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
458 /* Look up Ethernet address in forwarding table */
459 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
460 const u8 *mac, __be32 vni)
462 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
465 hlist_for_each_entry_rcu(f, head, hlist) {
466 if (ether_addr_equal(mac, f->eth_addr)) {
467 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
479 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
480 const u8 *mac, __be32 vni)
484 f = __vxlan_find_mac(vxlan, mac, vni);
485 if (f && f->used != jiffies)
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 __be32 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 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
510 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
512 struct vxlan_dev *vxlan = netdev_priv(dev);
513 u8 eth_addr[ETH_ALEN + 2] = { 0 };
514 struct vxlan_rdst *rdst;
518 if (is_multicast_ether_addr(mac) ||
519 is_zero_ether_addr(mac))
522 ether_addr_copy(eth_addr, mac);
526 f = __vxlan_find_mac(vxlan, eth_addr, vni);
532 rdst = first_remote_rcu(f);
533 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
539 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
541 static int vxlan_fdb_notify_one(struct notifier_block *nb,
542 const struct vxlan_dev *vxlan,
543 const struct vxlan_fdb *f,
544 const struct vxlan_rdst *rdst,
545 struct netlink_ext_ack *extack)
547 struct switchdev_notifier_vxlan_fdb_info fdb_info;
550 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
551 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
553 return notifier_to_errno(rc);
556 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
557 struct notifier_block *nb,
558 struct netlink_ext_ack *extack)
560 struct vxlan_dev *vxlan;
561 struct vxlan_rdst *rdst;
566 if (!netif_is_vxlan(dev))
568 vxlan = netdev_priv(dev);
570 for (h = 0; h < FDB_HASH_SIZE; ++h) {
571 spin_lock_bh(&vxlan->hash_lock[h]);
572 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
574 list_for_each_entry(rdst, &f->remotes, list) {
575 rc = vxlan_fdb_notify_one(nb, vxlan,
583 spin_unlock_bh(&vxlan->hash_lock[h]);
588 spin_unlock_bh(&vxlan->hash_lock[h]);
591 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
593 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
595 struct vxlan_dev *vxlan;
596 struct vxlan_rdst *rdst;
600 if (!netif_is_vxlan(dev))
602 vxlan = netdev_priv(dev);
604 for (h = 0; h < FDB_HASH_SIZE; ++h) {
605 spin_lock_bh(&vxlan->hash_lock[h]);
606 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
608 list_for_each_entry(rdst, &f->remotes, list)
609 rdst->offloaded = false;
610 spin_unlock_bh(&vxlan->hash_lock[h]);
614 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
616 /* Replace destination of unicast mac */
617 static int vxlan_fdb_replace(struct vxlan_fdb *f,
618 union vxlan_addr *ip, __be16 port, __be32 vni,
619 __u32 ifindex, struct vxlan_rdst *oldrd)
621 struct vxlan_rdst *rd;
623 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
627 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
632 dst_cache_reset(&rd->dst_cache);
634 rd->remote_port = port;
635 rd->remote_vni = vni;
636 rd->remote_ifindex = ifindex;
637 rd->offloaded = false;
641 /* Add/update destinations for multicast */
642 static int vxlan_fdb_append(struct vxlan_fdb *f,
643 union vxlan_addr *ip, __be16 port, __be32 vni,
644 __u32 ifindex, struct vxlan_rdst **rdp)
646 struct vxlan_rdst *rd;
648 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
652 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
656 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
662 rd->remote_port = port;
663 rd->offloaded = false;
664 rd->remote_vni = vni;
665 rd->remote_ifindex = ifindex;
667 list_add_tail_rcu(&rd->list, &f->remotes);
673 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
675 struct vxlanhdr *vh, size_t hdrlen,
677 struct gro_remcsum *grc,
680 size_t start, offset;
682 if (skb->remcsum_offload)
685 if (!NAPI_GRO_CB(skb)->csum_valid)
688 start = vxlan_rco_start(vni_field);
689 offset = start + vxlan_rco_offset(vni_field);
691 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
692 start, offset, grc, nopartial);
694 skb->remcsum_offload = 1;
699 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
700 struct list_head *head,
703 struct sk_buff *pp = NULL;
705 struct vxlanhdr *vh, *vh2;
706 unsigned int hlen, off_vx;
708 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
710 struct gro_remcsum grc;
712 skb_gro_remcsum_init(&grc);
714 off_vx = skb_gro_offset(skb);
715 hlen = off_vx + sizeof(*vh);
716 vh = skb_gro_header_fast(skb, off_vx);
717 if (skb_gro_header_hard(skb, hlen)) {
718 vh = skb_gro_header_slow(skb, hlen, off_vx);
723 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
725 flags = vh->vx_flags;
727 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
728 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
731 VXLAN_F_REMCSUM_NOPARTIAL));
737 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
739 list_for_each_entry(p, head, list) {
740 if (!NAPI_GRO_CB(p)->same_flow)
743 vh2 = (struct vxlanhdr *)(p->data + off_vx);
744 if (vh->vx_flags != vh2->vx_flags ||
745 vh->vx_vni != vh2->vx_vni) {
746 NAPI_GRO_CB(p)->same_flow = 0;
751 pp = call_gro_receive(eth_gro_receive, head, skb);
755 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
760 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
762 /* Sets 'skb->inner_mac_header' since we are always called with
763 * 'skb->encapsulation' set.
765 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
768 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
769 __u16 state, __be32 src_vni,
774 f = kmalloc(sizeof(*f), GFP_ATOMIC);
778 f->flags = ndm_flags;
779 f->updated = f->used = jiffies;
782 RCU_INIT_POINTER(f->vdev, vxlan);
783 INIT_LIST_HEAD(&f->nh_list);
784 INIT_LIST_HEAD(&f->remotes);
785 memcpy(f->eth_addr, mac, ETH_ALEN);
790 static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
791 __be32 src_vni, struct vxlan_fdb *f)
794 hlist_add_head_rcu(&f->hlist,
795 vxlan_fdb_head(vxlan, mac, src_vni));
798 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
799 u32 nhid, struct netlink_ext_ack *extack)
801 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
805 if (old_nh && old_nh->id == nhid)
808 nh = nexthop_find_by_id(vxlan->net, nhid);
810 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
814 if (!nexthop_get(nh)) {
815 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
819 if (!nexthop_is_fdb(nh)) {
820 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
824 if (!nexthop_is_multipath(nh)) {
825 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
829 /* check nexthop group family */
830 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
832 if (!nexthop_has_v4(nh)) {
834 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
839 if (nexthop_has_v4(nh)) {
841 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
847 list_del_rcu(&fdb->nh_list);
850 rcu_assign_pointer(fdb->nh, nh);
851 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
860 int vxlan_fdb_create(struct vxlan_dev *vxlan,
861 const u8 *mac, union vxlan_addr *ip,
862 __u16 state, __be16 port, __be32 src_vni,
863 __be32 vni, __u32 ifindex, __u16 ndm_flags,
864 u32 nhid, struct vxlan_fdb **fdb,
865 struct netlink_ext_ack *extack)
867 struct vxlan_rdst *rd = NULL;
871 if (vxlan->cfg.addrmax &&
872 vxlan->addrcnt >= vxlan->cfg.addrmax)
875 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
876 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
881 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
883 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
896 static void __vxlan_fdb_free(struct vxlan_fdb *f)
898 struct vxlan_rdst *rd, *nd;
901 nh = rcu_dereference_raw(f->nh);
903 rcu_assign_pointer(f->nh, NULL);
904 rcu_assign_pointer(f->vdev, NULL);
908 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
909 dst_cache_destroy(&rd->dst_cache);
915 static void vxlan_fdb_free(struct rcu_head *head)
917 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
922 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
923 bool do_notify, bool swdev_notify)
925 struct vxlan_rdst *rd;
927 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
931 if (rcu_access_pointer(f->nh))
932 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
935 list_for_each_entry(rd, &f->remotes, list)
936 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
940 hlist_del_rcu(&f->hlist);
941 list_del_rcu(&f->nh_list);
942 call_rcu(&f->rcu, vxlan_fdb_free);
945 static void vxlan_dst_free(struct rcu_head *head)
947 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
949 dst_cache_destroy(&rd->dst_cache);
953 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
954 union vxlan_addr *ip,
955 __u16 state, __u16 flags,
956 __be16 port, __be32 vni,
957 __u32 ifindex, __u16 ndm_flags,
958 struct vxlan_fdb *f, u32 nhid,
960 struct netlink_ext_ack *extack)
962 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
963 struct vxlan_rdst *rd = NULL;
964 struct vxlan_rdst oldrd;
969 if (nhid && !rcu_access_pointer(f->nh)) {
970 NL_SET_ERR_MSG(extack,
971 "Cannot replace an existing non nexthop fdb with a nexthop");
975 if (nhid && (flags & NLM_F_APPEND)) {
976 NL_SET_ERR_MSG(extack,
977 "Cannot append to a nexthop fdb");
981 /* Do not allow an externally learned entry to take over an entry added
984 if (!(fdb_flags & NTF_EXT_LEARNED) ||
985 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
986 if (f->state != state) {
988 f->updated = jiffies;
991 if (f->flags != fdb_flags) {
992 f->flags = fdb_flags;
993 f->updated = jiffies;
998 if ((flags & NLM_F_REPLACE)) {
999 /* Only change unicasts */
1000 if (!(is_multicast_ether_addr(f->eth_addr) ||
1001 is_zero_ether_addr(f->eth_addr))) {
1003 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1007 rc = vxlan_fdb_replace(f, ip, port, vni,
1012 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
1016 if ((flags & NLM_F_APPEND) &&
1017 (is_multicast_ether_addr(f->eth_addr) ||
1018 is_zero_ether_addr(f->eth_addr))) {
1019 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1026 if (ndm_flags & NTF_USE)
1031 rd = first_remote_rtnl(f);
1033 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
1034 swdev_notify, extack);
1044 if ((flags & NLM_F_REPLACE) && rc)
1046 else if ((flags & NLM_F_APPEND) && rc) {
1047 list_del_rcu(&rd->list);
1048 call_rcu(&rd->rcu, vxlan_dst_free);
1053 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1054 const u8 *mac, union vxlan_addr *ip,
1055 __u16 state, __u16 flags,
1056 __be16 port, __be32 src_vni, __be32 vni,
1057 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1059 struct netlink_ext_ack *extack)
1061 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1062 struct vxlan_fdb *f;
1065 /* Disallow replace to add a multicast entry */
1066 if ((flags & NLM_F_REPLACE) &&
1067 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1070 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1071 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1072 vni, ifindex, fdb_flags, nhid, &f, extack);
1076 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1077 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1078 swdev_notify, extack);
1085 vxlan_fdb_destroy(vxlan, f, false, false);
1089 /* Add new entry to forwarding table -- assumes lock held */
1090 int vxlan_fdb_update(struct vxlan_dev *vxlan,
1091 const u8 *mac, union vxlan_addr *ip,
1092 __u16 state, __u16 flags,
1093 __be16 port, __be32 src_vni, __be32 vni,
1094 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1096 struct netlink_ext_ack *extack)
1098 struct vxlan_fdb *f;
1100 f = __vxlan_find_mac(vxlan, mac, src_vni);
1102 if (flags & NLM_F_EXCL) {
1103 netdev_dbg(vxlan->dev,
1104 "lost race to create %pM\n", mac);
1108 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1109 vni, ifindex, ndm_flags, f,
1110 nhid, swdev_notify, extack);
1112 if (!(flags & NLM_F_CREATE))
1115 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1116 port, src_vni, vni, ifindex,
1117 ndm_flags, nhid, swdev_notify,
1122 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1123 struct vxlan_rdst *rd, bool swdev_notify)
1125 list_del_rcu(&rd->list);
1126 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1127 call_rcu(&rd->rcu, vxlan_dst_free);
1130 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1131 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1132 __be32 *vni, u32 *ifindex, u32 *nhid)
1134 struct net *net = dev_net(vxlan->dev);
1137 if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] ||
1142 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1146 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1148 if (remote->sa.sa_family == AF_INET) {
1149 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1150 ip->sa.sa_family = AF_INET;
1151 #if IS_ENABLED(CONFIG_IPV6)
1153 ip->sin6.sin6_addr = in6addr_any;
1154 ip->sa.sa_family = AF_INET6;
1160 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1162 *port = nla_get_be16(tb[NDA_PORT]);
1164 *port = vxlan->cfg.dst_port;
1168 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1170 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1172 *vni = vxlan->default_dst.remote_vni;
1175 if (tb[NDA_SRC_VNI]) {
1176 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1178 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1180 *src_vni = vxlan->default_dst.remote_vni;
1183 if (tb[NDA_IFINDEX]) {
1184 struct net_device *tdev;
1186 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1188 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1189 tdev = __dev_get_by_index(net, *ifindex);
1191 return -EADDRNOTAVAIL;
1197 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1204 /* Add static entry (via netlink) */
1205 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1206 struct net_device *dev,
1207 const unsigned char *addr, u16 vid, u16 flags,
1208 struct netlink_ext_ack *extack)
1210 struct vxlan_dev *vxlan = netdev_priv(dev);
1211 /* struct net *net = dev_net(vxlan->dev); */
1212 union vxlan_addr ip;
1214 __be32 src_vni, vni;
1219 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1220 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1225 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
1228 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1233 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1234 return -EAFNOSUPPORT;
1236 hash_index = fdb_head_index(vxlan, addr, src_vni);
1237 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1238 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1239 port, src_vni, vni, ifindex,
1240 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1241 nhid, true, extack);
1242 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1247 int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1248 const unsigned char *addr, union vxlan_addr ip,
1249 __be16 port, __be32 src_vni, __be32 vni,
1250 u32 ifindex, bool swdev_notify)
1252 struct vxlan_rdst *rd = NULL;
1253 struct vxlan_fdb *f;
1256 f = vxlan_find_mac(vxlan, addr, src_vni);
1260 if (!vxlan_addr_any(&ip)) {
1261 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1266 /* remove a destination if it's not the only one on the list,
1267 * otherwise destroy the fdb entry
1269 if (rd && !list_is_singular(&f->remotes)) {
1270 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1274 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1280 /* Delete entry (via netlink) */
1281 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1282 struct net_device *dev,
1283 const unsigned char *addr, u16 vid)
1285 struct vxlan_dev *vxlan = netdev_priv(dev);
1286 union vxlan_addr ip;
1287 __be32 src_vni, vni;
1293 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1298 hash_index = fdb_head_index(vxlan, addr, src_vni);
1299 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1300 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1302 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1307 /* Dump forwarding table */
1308 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1309 struct net_device *dev,
1310 struct net_device *filter_dev, int *idx)
1312 struct vxlan_dev *vxlan = netdev_priv(dev);
1316 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1317 struct vxlan_fdb *f;
1320 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1321 struct vxlan_rdst *rd;
1323 if (rcu_access_pointer(f->nh)) {
1324 if (*idx < cb->args[2])
1326 err = vxlan_fdb_info(skb, vxlan, f,
1327 NETLINK_CB(cb->skb).portid,
1340 list_for_each_entry_rcu(rd, &f->remotes, list) {
1341 if (*idx < cb->args[2])
1344 err = vxlan_fdb_info(skb, vxlan, f,
1345 NETLINK_CB(cb->skb).portid,
1363 static int vxlan_fdb_get(struct sk_buff *skb,
1364 struct nlattr *tb[],
1365 struct net_device *dev,
1366 const unsigned char *addr,
1367 u16 vid, u32 portid, u32 seq,
1368 struct netlink_ext_ack *extack)
1370 struct vxlan_dev *vxlan = netdev_priv(dev);
1371 struct vxlan_fdb *f;
1376 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1378 vni = vxlan->default_dst.remote_vni;
1382 f = __vxlan_find_mac(vxlan, addr, vni);
1384 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1389 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1390 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1396 /* Watch incoming packets to learn mapping between Ethernet address
1397 * and Tunnel endpoint.
1398 * Return true if packet is bogus and should be dropped.
1400 static bool vxlan_snoop(struct net_device *dev,
1401 union vxlan_addr *src_ip, const u8 *src_mac,
1402 u32 src_ifindex, __be32 vni)
1404 struct vxlan_dev *vxlan = netdev_priv(dev);
1405 struct vxlan_fdb *f;
1408 #if IS_ENABLED(CONFIG_IPV6)
1409 if (src_ip->sa.sa_family == AF_INET6 &&
1410 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1411 ifindex = src_ifindex;
1414 f = vxlan_find_mac(vxlan, src_mac, vni);
1416 struct vxlan_rdst *rdst = first_remote_rcu(f);
1418 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1419 rdst->remote_ifindex == ifindex))
1422 /* Don't migrate static entries, drop packets */
1423 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1426 /* Don't override an fdb with nexthop with a learnt entry */
1427 if (rcu_access_pointer(f->nh))
1430 if (net_ratelimit())
1432 "%pM migrated from %pIS to %pIS\n",
1433 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1435 rdst->remote_ip = *src_ip;
1436 f->updated = jiffies;
1437 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1439 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1441 /* learned new entry */
1442 spin_lock(&vxlan->hash_lock[hash_index]);
1444 /* close off race between vxlan_flush and incoming packets */
1445 if (netif_running(dev))
1446 vxlan_fdb_update(vxlan, src_mac, src_ip,
1448 NLM_F_EXCL|NLM_F_CREATE,
1449 vxlan->cfg.dst_port,
1451 vxlan->default_dst.remote_vni,
1452 ifindex, NTF_SELF, 0, true, NULL);
1453 spin_unlock(&vxlan->hash_lock[hash_index]);
1459 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1461 struct vxlan_net *vn;
1465 if (!refcount_dec_and_test(&vs->refcnt))
1468 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1469 spin_lock(&vn->sock_lock);
1470 hlist_del_rcu(&vs->hlist);
1471 udp_tunnel_notify_del_rx_port(vs->sock,
1472 (vs->flags & VXLAN_F_GPE) ?
1473 UDP_TUNNEL_TYPE_VXLAN_GPE :
1474 UDP_TUNNEL_TYPE_VXLAN);
1475 spin_unlock(&vn->sock_lock);
1480 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1482 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1483 #if IS_ENABLED(CONFIG_IPV6)
1484 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1486 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1489 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1492 if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
1493 vxlan_vs_del_vnigrp(vxlan);
1495 vxlan_vs_del_dev(vxlan);
1497 if (__vxlan_sock_release_prep(sock4)) {
1498 udp_tunnel_sock_release(sock4->sock);
1502 #if IS_ENABLED(CONFIG_IPV6)
1503 if (__vxlan_sock_release_prep(sock6)) {
1504 udp_tunnel_sock_release(sock6->sock);
1510 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1511 struct sk_buff *skb, u32 vxflags)
1513 size_t start, offset;
1515 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1518 start = vxlan_rco_start(unparsed->vx_vni);
1519 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1521 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1524 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1525 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1527 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1528 unparsed->vx_vni &= VXLAN_VNI_MASK;
1532 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1533 struct sk_buff *skb, u32 vxflags,
1534 struct vxlan_metadata *md)
1536 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1537 struct metadata_dst *tun_dst;
1539 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1542 md->gbp = ntohs(gbp->policy_id);
1544 tun_dst = (struct metadata_dst *)skb_dst(skb);
1546 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1547 tun_dst->u.tun_info.options_len = sizeof(*md);
1549 if (gbp->dont_learn)
1550 md->gbp |= VXLAN_GBP_DONT_LEARN;
1552 if (gbp->policy_applied)
1553 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1555 /* In flow-based mode, GBP is carried in dst_metadata */
1556 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1557 skb->mark = md->gbp;
1559 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1562 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1564 struct sk_buff *skb, u32 vxflags)
1566 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1568 /* Need to have Next Protocol set for interfaces in GPE mode. */
1569 if (!gpe->np_applied)
1571 /* "The initial version is 0. If a receiver does not support the
1572 * version indicated it MUST drop the packet.
1574 if (gpe->version != 0)
1576 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1577 * processing MUST occur." However, we don't implement OAM
1578 * processing, thus drop the packet.
1583 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1587 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1591 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1592 struct vxlan_sock *vs,
1593 struct sk_buff *skb, __be32 vni)
1595 union vxlan_addr saddr;
1596 u32 ifindex = skb->dev->ifindex;
1598 skb_reset_mac_header(skb);
1599 skb->protocol = eth_type_trans(skb, vxlan->dev);
1600 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1602 /* Ignore packet loops (and multicast echo) */
1603 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1606 /* Get address from the outer IP header */
1607 if (vxlan_get_sk_family(vs) == AF_INET) {
1608 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1609 saddr.sa.sa_family = AF_INET;
1610 #if IS_ENABLED(CONFIG_IPV6)
1612 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1613 saddr.sa.sa_family = AF_INET6;
1617 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1618 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1624 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1625 struct sk_buff *skb)
1629 if (vxlan_get_sk_family(vs) == AF_INET)
1630 err = IP_ECN_decapsulate(oiph, skb);
1631 #if IS_ENABLED(CONFIG_IPV6)
1633 err = IP6_ECN_decapsulate(oiph, skb);
1636 if (unlikely(err) && log_ecn_error) {
1637 if (vxlan_get_sk_family(vs) == AF_INET)
1638 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1639 &((struct iphdr *)oiph)->saddr,
1640 ((struct iphdr *)oiph)->tos);
1642 net_info_ratelimited("non-ECT from %pI6\n",
1643 &((struct ipv6hdr *)oiph)->saddr);
1648 /* Callback from net/ipv4/udp.c to receive packets */
1649 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1651 struct vxlan_vni_node *vninode = NULL;
1652 struct vxlan_dev *vxlan;
1653 struct vxlan_sock *vs;
1654 struct vxlanhdr unparsed;
1655 struct vxlan_metadata _md;
1656 struct vxlan_metadata *md = &_md;
1657 __be16 protocol = htons(ETH_P_TEB);
1658 bool raw_proto = false;
1662 /* Need UDP and VXLAN header to be present */
1663 if (!pskb_may_pull(skb, VXLAN_HLEN))
1666 unparsed = *vxlan_hdr(skb);
1667 /* VNI flag always required to be set */
1668 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1669 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1670 ntohl(vxlan_hdr(skb)->vx_flags),
1671 ntohl(vxlan_hdr(skb)->vx_vni));
1672 /* Return non vxlan pkt */
1675 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1676 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1678 vs = rcu_dereference_sk_user_data(sk);
1682 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1684 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, &vninode);
1688 /* For backwards compatibility, only allow reserved fields to be
1689 * used by VXLAN extensions if explicitly requested.
1691 if (vs->flags & VXLAN_F_GPE) {
1692 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1697 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1698 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1701 if (vs->flags & VXLAN_F_REMCSUM_RX)
1702 if (unlikely(!vxlan_remcsum(&unparsed, skb, vs->flags)))
1705 if (vxlan_collect_metadata(vs)) {
1706 struct metadata_dst *tun_dst;
1708 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1709 key32_to_tunnel_id(vni), sizeof(*md));
1714 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1716 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1718 memset(md, 0, sizeof(*md));
1721 if (vs->flags & VXLAN_F_GBP)
1722 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1723 /* Note that GBP and GPE can never be active together. This is
1724 * ensured in vxlan_dev_configure.
1727 if (unparsed.vx_flags || unparsed.vx_vni) {
1728 /* If there are any unprocessed flags remaining treat
1729 * this as a malformed packet. This behavior diverges from
1730 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1731 * in reserved fields are to be ignored. The approach here
1732 * maintains compatibility with previous stack code, and also
1733 * is more robust and provides a little more security in
1734 * adding extensions to VXLAN.
1740 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1743 skb_reset_mac_header(skb);
1744 skb->dev = vxlan->dev;
1745 skb->pkt_type = PACKET_HOST;
1748 oiph = skb_network_header(skb);
1749 skb_reset_network_header(skb);
1751 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1752 ++vxlan->dev->stats.rx_frame_errors;
1753 ++vxlan->dev->stats.rx_errors;
1754 vxlan_vnifilter_count(vxlan, vni, vninode,
1755 VXLAN_VNI_STATS_RX_ERRORS, 0);
1761 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1763 dev_core_stats_rx_dropped_inc(vxlan->dev);
1764 vxlan_vnifilter_count(vxlan, vni, vninode,
1765 VXLAN_VNI_STATS_RX_DROPS, 0);
1769 dev_sw_netstats_rx_add(vxlan->dev, skb->len);
1770 vxlan_vnifilter_count(vxlan, vni, vninode, VXLAN_VNI_STATS_RX, skb->len);
1771 gro_cells_receive(&vxlan->gro_cells, skb);
1778 /* Consume bad packet */
1783 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1784 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1786 struct vxlan_dev *vxlan;
1787 struct vxlan_sock *vs;
1788 struct vxlanhdr *hdr;
1791 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1794 hdr = vxlan_hdr(skb);
1796 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1799 vs = rcu_dereference_sk_user_data(sk);
1803 vni = vxlan_vni(hdr->vx_vni);
1804 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, NULL);
1811 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1813 struct vxlan_dev *vxlan = netdev_priv(dev);
1814 struct arphdr *parp;
1817 struct neighbour *n;
1819 if (dev->flags & IFF_NOARP)
1822 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1823 dev->stats.tx_dropped++;
1826 parp = arp_hdr(skb);
1828 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1829 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1830 parp->ar_pro != htons(ETH_P_IP) ||
1831 parp->ar_op != htons(ARPOP_REQUEST) ||
1832 parp->ar_hln != dev->addr_len ||
1835 arpptr = (u8 *)parp + sizeof(struct arphdr);
1837 arpptr += dev->addr_len; /* sha */
1838 memcpy(&sip, arpptr, sizeof(sip));
1839 arpptr += sizeof(sip);
1840 arpptr += dev->addr_len; /* tha */
1841 memcpy(&tip, arpptr, sizeof(tip));
1843 if (ipv4_is_loopback(tip) ||
1844 ipv4_is_multicast(tip))
1847 n = neigh_lookup(&arp_tbl, &tip, dev);
1850 struct vxlan_fdb *f;
1851 struct sk_buff *reply;
1853 if (!(n->nud_state & NUD_CONNECTED)) {
1858 f = vxlan_find_mac(vxlan, n->ha, vni);
1859 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1860 /* bridge-local neighbor */
1865 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1873 skb_reset_mac_header(reply);
1874 __skb_pull(reply, skb_network_offset(reply));
1875 reply->ip_summed = CHECKSUM_UNNECESSARY;
1876 reply->pkt_type = PACKET_HOST;
1878 if (netif_rx(reply) == NET_RX_DROP) {
1879 dev->stats.rx_dropped++;
1880 vxlan_vnifilter_count(vxlan, vni, NULL,
1881 VXLAN_VNI_STATS_RX_DROPS, 0);
1884 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1885 union vxlan_addr ipa = {
1886 .sin.sin_addr.s_addr = tip,
1887 .sin.sin_family = AF_INET,
1890 vxlan_ip_miss(dev, &ipa);
1894 return NETDEV_TX_OK;
1897 #if IS_ENABLED(CONFIG_IPV6)
1898 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1899 struct neighbour *n, bool isrouter)
1901 struct net_device *dev = request->dev;
1902 struct sk_buff *reply;
1903 struct nd_msg *ns, *na;
1904 struct ipv6hdr *pip6;
1906 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1910 if (dev == NULL || !pskb_may_pull(request, request->len))
1913 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1914 sizeof(*na) + na_olen + dev->needed_tailroom;
1915 reply = alloc_skb(len, GFP_ATOMIC);
1919 reply->protocol = htons(ETH_P_IPV6);
1921 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1922 skb_push(reply, sizeof(struct ethhdr));
1923 skb_reset_mac_header(reply);
1925 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
1927 daddr = eth_hdr(request)->h_source;
1928 ns_olen = request->len - skb_network_offset(request) -
1929 sizeof(struct ipv6hdr) - sizeof(*ns);
1930 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1931 if (!ns->opt[i + 1]) {
1935 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1936 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1941 /* Ethernet header */
1942 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1943 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1944 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1945 reply->protocol = htons(ETH_P_IPV6);
1947 skb_pull(reply, sizeof(struct ethhdr));
1948 skb_reset_network_header(reply);
1949 skb_put(reply, sizeof(struct ipv6hdr));
1953 pip6 = ipv6_hdr(reply);
1954 memset(pip6, 0, sizeof(struct ipv6hdr));
1956 pip6->priority = ipv6_hdr(request)->priority;
1957 pip6->nexthdr = IPPROTO_ICMPV6;
1958 pip6->hop_limit = 255;
1959 pip6->daddr = ipv6_hdr(request)->saddr;
1960 pip6->saddr = *(struct in6_addr *)n->primary_key;
1962 skb_pull(reply, sizeof(struct ipv6hdr));
1963 skb_reset_transport_header(reply);
1965 /* Neighbor Advertisement */
1966 na = skb_put_zero(reply, sizeof(*na) + na_olen);
1967 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1968 na->icmph.icmp6_router = isrouter;
1969 na->icmph.icmp6_override = 1;
1970 na->icmph.icmp6_solicited = 1;
1971 na->target = ns->target;
1972 ether_addr_copy(&na->opt[2], n->ha);
1973 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1974 na->opt[1] = na_olen >> 3;
1976 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1977 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1978 csum_partial(na, sizeof(*na)+na_olen, 0));
1980 pip6->payload_len = htons(sizeof(*na)+na_olen);
1982 skb_push(reply, sizeof(struct ipv6hdr));
1984 reply->ip_summed = CHECKSUM_UNNECESSARY;
1989 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1991 struct vxlan_dev *vxlan = netdev_priv(dev);
1992 const struct in6_addr *daddr;
1993 const struct ipv6hdr *iphdr;
1994 struct inet6_dev *in6_dev;
1995 struct neighbour *n;
1999 in6_dev = __in6_dev_get(dev);
2003 iphdr = ipv6_hdr(skb);
2004 daddr = &iphdr->daddr;
2005 msg = (struct nd_msg *)(iphdr + 1);
2007 if (ipv6_addr_loopback(daddr) ||
2008 ipv6_addr_is_multicast(&msg->target))
2011 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2014 struct vxlan_fdb *f;
2015 struct sk_buff *reply;
2017 if (!(n->nud_state & NUD_CONNECTED)) {
2022 f = vxlan_find_mac(vxlan, n->ha, vni);
2023 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2024 /* bridge-local neighbor */
2029 reply = vxlan_na_create(skb, n,
2030 !!(f ? f->flags & NTF_ROUTER : 0));
2037 if (netif_rx(reply) == NET_RX_DROP) {
2038 dev->stats.rx_dropped++;
2039 vxlan_vnifilter_count(vxlan, vni, NULL,
2040 VXLAN_VNI_STATS_RX_DROPS, 0);
2042 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2043 union vxlan_addr ipa = {
2044 .sin6.sin6_addr = msg->target,
2045 .sin6.sin6_family = AF_INET6,
2048 vxlan_ip_miss(dev, &ipa);
2054 return NETDEV_TX_OK;
2058 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2060 struct vxlan_dev *vxlan = netdev_priv(dev);
2061 struct neighbour *n;
2063 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2067 switch (ntohs(eth_hdr(skb)->h_proto)) {
2072 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2075 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2076 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2077 union vxlan_addr ipa = {
2078 .sin.sin_addr.s_addr = pip->daddr,
2079 .sin.sin_family = AF_INET,
2082 vxlan_ip_miss(dev, &ipa);
2088 #if IS_ENABLED(CONFIG_IPV6)
2091 struct ipv6hdr *pip6;
2093 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2095 pip6 = ipv6_hdr(skb);
2096 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2097 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2098 union vxlan_addr ipa = {
2099 .sin6.sin6_addr = pip6->daddr,
2100 .sin6.sin6_family = AF_INET6,
2103 vxlan_ip_miss(dev, &ipa);
2117 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2119 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2121 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2130 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2131 struct vxlan_metadata *md)
2133 struct vxlanhdr_gbp *gbp;
2138 gbp = (struct vxlanhdr_gbp *)vxh;
2139 vxh->vx_flags |= VXLAN_HF_GBP;
2141 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2142 gbp->dont_learn = 1;
2144 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2145 gbp->policy_applied = 1;
2147 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2150 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2153 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2155 gpe->np_applied = 1;
2156 gpe->next_protocol = tun_p_from_eth_p(protocol);
2157 if (!gpe->next_protocol)
2158 return -EPFNOSUPPORT;
2162 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2163 int iphdr_len, __be32 vni,
2164 struct vxlan_metadata *md, u32 vxflags,
2167 struct vxlanhdr *vxh;
2170 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2171 __be16 inner_protocol = htons(ETH_P_TEB);
2173 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2174 skb->ip_summed == CHECKSUM_PARTIAL) {
2175 int csum_start = skb_checksum_start_offset(skb);
2177 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2178 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2179 (skb->csum_offset == offsetof(struct udphdr, check) ||
2180 skb->csum_offset == offsetof(struct tcphdr, check)))
2181 type |= SKB_GSO_TUNNEL_REMCSUM;
2184 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2185 + VXLAN_HLEN + iphdr_len;
2187 /* Need space for new headers (invalidates iph ptr) */
2188 err = skb_cow_head(skb, min_headroom);
2192 err = iptunnel_handle_offloads(skb, type);
2196 vxh = __skb_push(skb, sizeof(*vxh));
2197 vxh->vx_flags = VXLAN_HF_VNI;
2198 vxh->vx_vni = vxlan_vni_field(vni);
2200 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2203 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2204 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2205 vxh->vx_flags |= VXLAN_HF_RCO;
2207 if (!skb_is_gso(skb)) {
2208 skb->ip_summed = CHECKSUM_NONE;
2209 skb->encapsulation = 0;
2213 if (vxflags & VXLAN_F_GBP)
2214 vxlan_build_gbp_hdr(vxh, vxflags, md);
2215 if (vxflags & VXLAN_F_GPE) {
2216 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2219 inner_protocol = skb->protocol;
2222 skb_set_inner_protocol(skb, inner_protocol);
2226 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2227 struct vxlan_sock *sock4,
2228 struct sk_buff *skb, int oif, u8 tos,
2229 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2230 struct dst_cache *dst_cache,
2231 const struct ip_tunnel_info *info)
2233 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2234 struct rtable *rt = NULL;
2238 return ERR_PTR(-EIO);
2243 rt = dst_cache_get_ip4(dst_cache, saddr);
2248 memset(&fl4, 0, sizeof(fl4));
2249 fl4.flowi4_oif = oif;
2250 fl4.flowi4_tos = RT_TOS(tos);
2251 fl4.flowi4_mark = skb->mark;
2252 fl4.flowi4_proto = IPPROTO_UDP;
2255 fl4.fl4_dport = dport;
2256 fl4.fl4_sport = sport;
2258 rt = ip_route_output_key(vxlan->net, &fl4);
2260 if (rt->dst.dev == dev) {
2261 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2263 return ERR_PTR(-ELOOP);
2268 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2270 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2271 return ERR_PTR(-ENETUNREACH);
2276 #if IS_ENABLED(CONFIG_IPV6)
2277 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2278 struct net_device *dev,
2279 struct vxlan_sock *sock6,
2280 struct sk_buff *skb, int oif, u8 tos,
2282 const struct in6_addr *daddr,
2283 struct in6_addr *saddr,
2284 __be16 dport, __be16 sport,
2285 struct dst_cache *dst_cache,
2286 const struct ip_tunnel_info *info)
2288 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2289 struct dst_entry *ndst;
2293 return ERR_PTR(-EIO);
2298 ndst = dst_cache_get_ip6(dst_cache, saddr);
2303 memset(&fl6, 0, sizeof(fl6));
2304 fl6.flowi6_oif = oif;
2307 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2308 fl6.flowi6_mark = skb->mark;
2309 fl6.flowi6_proto = IPPROTO_UDP;
2310 fl6.fl6_dport = dport;
2311 fl6.fl6_sport = sport;
2313 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2316 netdev_dbg(dev, "no route to %pI6\n", daddr);
2317 return ERR_PTR(-ENETUNREACH);
2320 if (unlikely(ndst->dev == dev)) {
2321 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2323 return ERR_PTR(-ELOOP);
2328 dst_cache_set_ip6(dst_cache, ndst, saddr);
2333 /* Bypass encapsulation if the destination is local */
2334 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2335 struct vxlan_dev *dst_vxlan, __be32 vni,
2338 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2339 union vxlan_addr loopback;
2340 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2341 struct net_device *dev;
2344 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2345 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2346 skb->pkt_type = PACKET_HOST;
2347 skb->encapsulation = 0;
2348 skb->dev = dst_vxlan->dev;
2349 __skb_pull(skb, skb_network_offset(skb));
2351 if (remote_ip->sa.sa_family == AF_INET) {
2352 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2353 loopback.sa.sa_family = AF_INET;
2354 #if IS_ENABLED(CONFIG_IPV6)
2356 loopback.sin6.sin6_addr = in6addr_loopback;
2357 loopback.sa.sa_family = AF_INET6;
2363 if (unlikely(!(dev->flags & IFF_UP))) {
2368 if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
2369 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2371 u64_stats_update_begin(&tx_stats->syncp);
2372 tx_stats->tx_packets++;
2373 tx_stats->tx_bytes += len;
2374 u64_stats_update_end(&tx_stats->syncp);
2375 vxlan_vnifilter_count(src_vxlan, vni, NULL, VXLAN_VNI_STATS_TX, len);
2377 if (__netif_rx(skb) == NET_RX_SUCCESS) {
2378 u64_stats_update_begin(&rx_stats->syncp);
2379 rx_stats->rx_packets++;
2380 rx_stats->rx_bytes += len;
2381 u64_stats_update_end(&rx_stats->syncp);
2382 vxlan_vnifilter_count(dst_vxlan, vni, NULL, VXLAN_VNI_STATS_RX,
2386 dev->stats.rx_dropped++;
2387 vxlan_vnifilter_count(dst_vxlan, vni, NULL,
2388 VXLAN_VNI_STATS_RX_DROPS, 0);
2393 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2394 struct vxlan_dev *vxlan,
2395 union vxlan_addr *daddr,
2396 __be16 dst_port, int dst_ifindex, __be32 vni,
2397 struct dst_entry *dst,
2400 #if IS_ENABLED(CONFIG_IPV6)
2401 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2402 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2403 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2405 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2407 /* Bypass encapsulation if the destination is local */
2408 if (rt_flags & RTCF_LOCAL &&
2409 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2410 struct vxlan_dev *dst_vxlan;
2413 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2414 daddr->sa.sa_family, dst_port,
2417 dev->stats.tx_errors++;
2418 vxlan_vnifilter_count(vxlan, vni, NULL,
2419 VXLAN_VNI_STATS_TX_ERRORS, 0);
2424 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
2431 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2432 __be32 default_vni, struct vxlan_rdst *rdst,
2435 struct dst_cache *dst_cache;
2436 struct ip_tunnel_info *info;
2437 struct vxlan_dev *vxlan = netdev_priv(dev);
2438 const struct iphdr *old_iph = ip_hdr(skb);
2439 union vxlan_addr *dst;
2440 union vxlan_addr remote_ip, local_ip;
2441 struct vxlan_metadata _md;
2442 struct vxlan_metadata *md = &_md;
2443 unsigned int pkt_len = skb->len;
2444 __be16 src_port = 0, dst_port;
2445 struct dst_entry *ndst = NULL;
2449 u32 flags = vxlan->cfg.flags;
2450 bool udp_sum = false;
2451 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2453 #if IS_ENABLED(CONFIG_IPV6)
2457 info = skb_tunnel_info(skb);
2460 dst = &rdst->remote_ip;
2461 if (vxlan_addr_any(dst)) {
2463 /* short-circuited back to local bridge */
2464 vxlan_encap_bypass(skb, vxlan, vxlan,
2471 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2472 vni = (rdst->remote_vni) ? : default_vni;
2473 ifindex = rdst->remote_ifindex;
2474 local_ip = vxlan->cfg.saddr;
2475 dst_cache = &rdst->dst_cache;
2476 md->gbp = skb->mark;
2477 if (flags & VXLAN_F_TTL_INHERIT) {
2478 ttl = ip_tunnel_get_ttl(old_iph, skb);
2480 ttl = vxlan->cfg.ttl;
2481 if (!ttl && vxlan_addr_multicast(dst))
2485 tos = vxlan->cfg.tos;
2487 tos = ip_tunnel_get_dsfield(old_iph, skb);
2489 if (dst->sa.sa_family == AF_INET)
2490 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2492 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2493 #if IS_ENABLED(CONFIG_IPV6)
2494 label = vxlan->cfg.label;
2498 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2502 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2503 if (remote_ip.sa.sa_family == AF_INET) {
2504 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2505 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2507 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2508 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2511 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2512 vni = tunnel_id_to_key32(info->key.tun_id);
2514 dst_cache = &info->dst_cache;
2515 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2516 if (info->options_len < sizeof(*md))
2518 md = ip_tunnel_info_opts(info);
2520 ttl = info->key.ttl;
2521 tos = info->key.tos;
2522 #if IS_ENABLED(CONFIG_IPV6)
2523 label = info->key.label;
2525 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2527 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2528 vxlan->cfg.port_max, true);
2531 if (dst->sa.sa_family == AF_INET) {
2532 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2537 ifindex = sock4->sock->sk->sk_bound_dev_if;
2539 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2540 dst->sin.sin_addr.s_addr,
2541 &local_ip.sin.sin_addr.s_addr,
2550 /* Bypass encapsulation if the destination is local */
2551 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2552 dst_port, ifindex, vni,
2553 &rt->dst, rt->rt_flags);
2557 if (vxlan->cfg.df == VXLAN_DF_SET) {
2559 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2560 struct ethhdr *eth = eth_hdr(skb);
2562 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2563 (ntohs(eth->h_proto) == ETH_P_IP &&
2564 old_iph->frag_off & htons(IP_DF)))
2567 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2572 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM,
2573 netif_is_any_bridge_port(dev));
2578 struct ip_tunnel_info *unclone;
2579 struct in_addr src, dst;
2581 unclone = skb_tunnel_info_unclone(skb);
2582 if (unlikely(!unclone))
2585 src = remote_ip.sin.sin_addr;
2586 dst = local_ip.sin.sin_addr;
2587 unclone->key.u.ipv4.src = src.s_addr;
2588 unclone->key.u.ipv4.dst = dst.s_addr;
2590 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2595 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2596 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2597 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2598 vni, md, flags, udp_sum);
2602 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2603 dst->sin.sin_addr.s_addr, tos, ttl, df,
2604 src_port, dst_port, xnet, !udp_sum);
2605 #if IS_ENABLED(CONFIG_IPV6)
2607 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2610 ifindex = sock6->sock->sk->sk_bound_dev_if;
2612 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2613 label, &dst->sin6.sin6_addr,
2614 &local_ip.sin6.sin6_addr,
2618 err = PTR_ERR(ndst);
2624 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2626 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2627 dst_port, ifindex, vni,
2633 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM,
2634 netif_is_any_bridge_port(dev));
2639 struct ip_tunnel_info *unclone;
2640 struct in6_addr src, dst;
2642 unclone = skb_tunnel_info_unclone(skb);
2643 if (unlikely(!unclone))
2646 src = remote_ip.sin6.sin6_addr;
2647 dst = local_ip.sin6.sin6_addr;
2648 unclone->key.u.ipv6.src = src;
2649 unclone->key.u.ipv6.dst = dst;
2652 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2657 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2658 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2659 skb_scrub_packet(skb, xnet);
2660 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2661 vni, md, flags, udp_sum);
2665 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2666 &local_ip.sin6.sin6_addr,
2667 &dst->sin6.sin6_addr, tos, ttl,
2668 label, src_port, dst_port, !udp_sum);
2671 vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX, pkt_len);
2677 dev->stats.tx_dropped++;
2678 vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX_DROPS, 0);
2685 dev->stats.collisions++;
2686 else if (err == -ENETUNREACH)
2687 dev->stats.tx_carrier_errors++;
2689 dev->stats.tx_errors++;
2690 vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX_ERRORS, 0);
2694 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2695 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2697 struct vxlan_rdst nh_rdst;
2702 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2703 hash = skb_get_hash(skb);
2706 nh = rcu_dereference(f->nh);
2711 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2714 if (likely(do_xmit))
2715 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2722 dev->stats.tx_dropped++;
2723 vxlan_vnifilter_count(netdev_priv(dev), vni, NULL,
2724 VXLAN_VNI_STATS_TX_DROPS, 0);
2728 /* Transmit local packets over Vxlan
2730 * Outer IP header inherits ECN and DF from inner header.
2731 * Outer UDP destination is the VXLAN assigned port.
2732 * source port is based on hash of flow
2734 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2736 struct vxlan_dev *vxlan = netdev_priv(dev);
2737 struct vxlan_rdst *rdst, *fdst = NULL;
2738 const struct ip_tunnel_info *info;
2739 bool did_rsc = false;
2740 struct vxlan_fdb *f;
2744 info = skb_tunnel_info(skb);
2746 skb_reset_mac_header(skb);
2748 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2749 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2750 info->mode & IP_TUNNEL_INFO_TX) {
2751 vni = tunnel_id_to_key32(info->key.tun_id);
2753 if (info && info->mode & IP_TUNNEL_INFO_TX)
2754 vxlan_xmit_one(skb, dev, vni, NULL, false);
2757 return NETDEV_TX_OK;
2761 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2763 if (ntohs(eth->h_proto) == ETH_P_ARP)
2764 return arp_reduce(dev, skb, vni);
2765 #if IS_ENABLED(CONFIG_IPV6)
2766 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2767 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2768 sizeof(struct nd_msg)) &&
2769 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2770 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2772 if (m->icmph.icmp6_code == 0 &&
2773 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2774 return neigh_reduce(dev, skb, vni);
2780 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2783 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2784 (ntohs(eth->h_proto) == ETH_P_IP ||
2785 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2786 did_rsc = route_shortcircuit(dev, skb);
2788 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2792 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2794 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2795 !is_multicast_ether_addr(eth->h_dest))
2796 vxlan_fdb_miss(vxlan, eth->h_dest);
2798 dev->stats.tx_dropped++;
2799 vxlan_vnifilter_count(vxlan, vni, NULL,
2800 VXLAN_VNI_STATS_TX_DROPS, 0);
2802 return NETDEV_TX_OK;
2806 if (rcu_access_pointer(f->nh)) {
2807 vxlan_xmit_nh(skb, dev, f,
2808 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2810 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2811 struct sk_buff *skb1;
2817 skb1 = skb_clone(skb, GFP_ATOMIC);
2819 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2822 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2827 return NETDEV_TX_OK;
2830 /* Walk the forwarding table and purge stale entries */
2831 static void vxlan_cleanup(struct timer_list *t)
2833 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2834 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2837 if (!netif_running(vxlan->dev))
2840 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2841 struct hlist_node *p, *n;
2843 spin_lock(&vxlan->hash_lock[h]);
2844 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2846 = container_of(p, struct vxlan_fdb, hlist);
2847 unsigned long timeout;
2849 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2852 if (f->flags & NTF_EXT_LEARNED)
2855 timeout = f->used + vxlan->cfg.age_interval * HZ;
2856 if (time_before_eq(timeout, jiffies)) {
2857 netdev_dbg(vxlan->dev,
2858 "garbage collect %pM\n",
2860 f->state = NUD_STALE;
2861 vxlan_fdb_destroy(vxlan, f, true, true);
2862 } else if (time_before(timeout, next_timer))
2863 next_timer = timeout;
2865 spin_unlock(&vxlan->hash_lock[h]);
2868 mod_timer(&vxlan->age_timer, next_timer);
2871 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2873 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2875 spin_lock(&vn->sock_lock);
2876 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2877 #if IS_ENABLED(CONFIG_IPV6)
2878 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2880 spin_unlock(&vn->sock_lock);
2883 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2884 struct vxlan_dev_node *node)
2886 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2887 __be32 vni = vxlan->default_dst.remote_vni;
2889 node->vxlan = vxlan;
2890 spin_lock(&vn->sock_lock);
2891 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2892 spin_unlock(&vn->sock_lock);
2895 /* Setup stats when device is created */
2896 static int vxlan_init(struct net_device *dev)
2898 struct vxlan_dev *vxlan = netdev_priv(dev);
2901 if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
2902 vxlan_vnigroup_init(vxlan);
2904 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2908 err = gro_cells_init(&vxlan->gro_cells, dev);
2910 free_percpu(dev->tstats);
2917 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2919 struct vxlan_fdb *f;
2920 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
2922 spin_lock_bh(&vxlan->hash_lock[hash_index]);
2923 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2925 vxlan_fdb_destroy(vxlan, f, true, true);
2926 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
2929 static void vxlan_uninit(struct net_device *dev)
2931 struct vxlan_dev *vxlan = netdev_priv(dev);
2933 if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
2934 vxlan_vnigroup_uninit(vxlan);
2936 gro_cells_destroy(&vxlan->gro_cells);
2938 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2940 free_percpu(dev->tstats);
2943 /* Start ageing timer and join group when device is brought up */
2944 static int vxlan_open(struct net_device *dev)
2946 struct vxlan_dev *vxlan = netdev_priv(dev);
2949 ret = vxlan_sock_add(vxlan);
2953 ret = vxlan_multicast_join(vxlan);
2955 vxlan_sock_release(vxlan);
2959 if (vxlan->cfg.age_interval)
2960 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2965 /* Purge the forwarding table */
2966 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2970 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2971 struct hlist_node *p, *n;
2973 spin_lock_bh(&vxlan->hash_lock[h]);
2974 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2976 = container_of(p, struct vxlan_fdb, hlist);
2977 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2979 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2980 if (is_zero_ether_addr(f->eth_addr) &&
2981 f->vni == vxlan->cfg.vni)
2983 vxlan_fdb_destroy(vxlan, f, true, true);
2985 spin_unlock_bh(&vxlan->hash_lock[h]);
2989 /* Cleanup timer and forwarding table on shutdown */
2990 static int vxlan_stop(struct net_device *dev)
2992 struct vxlan_dev *vxlan = netdev_priv(dev);
2994 vxlan_multicast_leave(vxlan);
2996 del_timer_sync(&vxlan->age_timer);
2998 vxlan_flush(vxlan, false);
2999 vxlan_sock_release(vxlan);
3004 /* Stub, nothing needs to be done. */
3005 static void vxlan_set_multicast_list(struct net_device *dev)
3009 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3011 struct vxlan_dev *vxlan = netdev_priv(dev);
3012 struct vxlan_rdst *dst = &vxlan->default_dst;
3013 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3014 dst->remote_ifindex);
3015 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
3017 /* This check is different than dev->max_mtu, because it looks at
3018 * the lowerdev->mtu, rather than the static dev->max_mtu
3021 int max_mtu = lowerdev->mtu -
3022 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3023 if (new_mtu > max_mtu)
3031 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3033 struct vxlan_dev *vxlan = netdev_priv(dev);
3034 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3035 __be16 sport, dport;
3037 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3038 vxlan->cfg.port_max, true);
3039 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3041 if (ip_tunnel_info_af(info) == AF_INET) {
3042 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3045 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
3046 info->key.u.ipv4.dst,
3047 &info->key.u.ipv4.src, dport, sport,
3048 &info->dst_cache, info);
3053 #if IS_ENABLED(CONFIG_IPV6)
3054 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3055 struct dst_entry *ndst;
3057 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
3058 info->key.label, &info->key.u.ipv6.dst,
3059 &info->key.u.ipv6.src, dport, sport,
3060 &info->dst_cache, info);
3062 return PTR_ERR(ndst);
3064 #else /* !CONFIG_IPV6 */
3065 return -EPFNOSUPPORT;
3068 info->key.tp_src = sport;
3069 info->key.tp_dst = dport;
3073 static const struct net_device_ops vxlan_netdev_ether_ops = {
3074 .ndo_init = vxlan_init,
3075 .ndo_uninit = vxlan_uninit,
3076 .ndo_open = vxlan_open,
3077 .ndo_stop = vxlan_stop,
3078 .ndo_start_xmit = vxlan_xmit,
3079 .ndo_get_stats64 = dev_get_tstats64,
3080 .ndo_set_rx_mode = vxlan_set_multicast_list,
3081 .ndo_change_mtu = vxlan_change_mtu,
3082 .ndo_validate_addr = eth_validate_addr,
3083 .ndo_set_mac_address = eth_mac_addr,
3084 .ndo_fdb_add = vxlan_fdb_add,
3085 .ndo_fdb_del = vxlan_fdb_delete,
3086 .ndo_fdb_dump = vxlan_fdb_dump,
3087 .ndo_fdb_get = vxlan_fdb_get,
3088 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3091 static const struct net_device_ops vxlan_netdev_raw_ops = {
3092 .ndo_init = vxlan_init,
3093 .ndo_uninit = vxlan_uninit,
3094 .ndo_open = vxlan_open,
3095 .ndo_stop = vxlan_stop,
3096 .ndo_start_xmit = vxlan_xmit,
3097 .ndo_get_stats64 = dev_get_tstats64,
3098 .ndo_change_mtu = vxlan_change_mtu,
3099 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3102 /* Info for udev, that this is a virtual tunnel endpoint */
3103 static struct device_type vxlan_type = {
3107 /* Calls the ndo_udp_tunnel_add of the caller in order to
3108 * supply the listening VXLAN udp ports. Callers are expected
3109 * to implement the ndo_udp_tunnel_add.
3111 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3113 struct vxlan_sock *vs;
3114 struct net *net = dev_net(dev);
3115 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3118 spin_lock(&vn->sock_lock);
3119 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3120 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3121 unsigned short type;
3123 if (vs->flags & VXLAN_F_GPE)
3124 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3126 type = UDP_TUNNEL_TYPE_VXLAN;
3129 udp_tunnel_push_rx_port(dev, vs->sock, type);
3131 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3134 spin_unlock(&vn->sock_lock);
3137 /* Initialize the device structure. */
3138 static void vxlan_setup(struct net_device *dev)
3140 struct vxlan_dev *vxlan = netdev_priv(dev);
3143 eth_hw_addr_random(dev);
3146 dev->needs_free_netdev = true;
3147 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3149 dev->features |= NETIF_F_LLTX;
3150 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
3151 dev->features |= NETIF_F_RXCSUM;
3152 dev->features |= NETIF_F_GSO_SOFTWARE;
3154 dev->vlan_features = dev->features;
3155 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
3156 dev->hw_features |= NETIF_F_RXCSUM;
3157 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3158 netif_keep_dst(dev);
3159 dev->priv_flags |= IFF_NO_QUEUE | IFF_CHANGE_PROTO_DOWN;
3161 /* MTU range: 68 - 65535 */
3162 dev->min_mtu = ETH_MIN_MTU;
3163 dev->max_mtu = ETH_MAX_MTU;
3165 INIT_LIST_HEAD(&vxlan->next);
3167 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3171 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3172 spin_lock_init(&vxlan->hash_lock[h]);
3173 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3177 static void vxlan_ether_setup(struct net_device *dev)
3179 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3180 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3181 dev->netdev_ops = &vxlan_netdev_ether_ops;
3184 static void vxlan_raw_setup(struct net_device *dev)
3186 dev->header_ops = NULL;
3187 dev->type = ARPHRD_NONE;
3188 dev->hard_header_len = 0;
3190 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3191 dev->netdev_ops = &vxlan_netdev_raw_ops;
3194 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3195 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3196 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
3197 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3198 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3199 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
3200 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3201 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3202 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3203 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3204 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3205 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3206 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3207 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3208 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3209 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3210 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3211 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3212 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3213 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3214 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3215 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3216 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3217 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3218 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3219 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3220 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3221 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3222 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3223 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3224 [IFLA_VXLAN_VNIFILTER] = { .type = NLA_U8 },
3227 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3228 struct netlink_ext_ack *extack)
3230 if (tb[IFLA_ADDRESS]) {
3231 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3232 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3233 "Provided link layer address is not Ethernet");
3237 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3238 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3239 "Provided Ethernet address is not unicast");
3240 return -EADDRNOTAVAIL;
3245 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3247 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3248 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3249 "MTU must be between 68 and 65535");
3255 NL_SET_ERR_MSG(extack,
3256 "Required attributes not provided to perform the operation");
3260 if (data[IFLA_VXLAN_ID]) {
3261 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3263 if (id >= VXLAN_N_VID) {
3264 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3265 "VXLAN ID must be lower than 16777216");
3270 if (data[IFLA_VXLAN_PORT_RANGE]) {
3271 const struct ifla_vxlan_port_range *p
3272 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3274 if (ntohs(p->high) < ntohs(p->low)) {
3275 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3276 "Invalid source port range");
3281 if (data[IFLA_VXLAN_DF]) {
3282 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3284 if (df < 0 || df > VXLAN_DF_MAX) {
3285 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3286 "Invalid DF attribute");
3294 static void vxlan_get_drvinfo(struct net_device *netdev,
3295 struct ethtool_drvinfo *drvinfo)
3297 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3298 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3301 static int vxlan_get_link_ksettings(struct net_device *dev,
3302 struct ethtool_link_ksettings *cmd)
3304 struct vxlan_dev *vxlan = netdev_priv(dev);
3305 struct vxlan_rdst *dst = &vxlan->default_dst;
3306 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3307 dst->remote_ifindex);
3310 cmd->base.duplex = DUPLEX_UNKNOWN;
3311 cmd->base.port = PORT_OTHER;
3312 cmd->base.speed = SPEED_UNKNOWN;
3317 return __ethtool_get_link_ksettings(lowerdev, cmd);
3320 static const struct ethtool_ops vxlan_ethtool_ops = {
3321 .get_drvinfo = vxlan_get_drvinfo,
3322 .get_link = ethtool_op_get_link,
3323 .get_link_ksettings = vxlan_get_link_ksettings,
3326 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3327 __be16 port, u32 flags, int ifindex)
3329 struct socket *sock;
3330 struct udp_port_cfg udp_conf;
3333 memset(&udp_conf, 0, sizeof(udp_conf));
3336 udp_conf.family = AF_INET6;
3337 udp_conf.use_udp6_rx_checksums =
3338 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3339 udp_conf.ipv6_v6only = 1;
3341 udp_conf.family = AF_INET;
3344 udp_conf.local_udp_port = port;
3345 udp_conf.bind_ifindex = ifindex;
3347 /* Open UDP socket */
3348 err = udp_sock_create(net, &udp_conf, &sock);
3350 return ERR_PTR(err);
3352 udp_allow_gso(sock->sk);
3356 /* Create new listen socket if needed */
3357 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3358 __be16 port, u32 flags,
3361 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3362 struct vxlan_sock *vs;
3363 struct socket *sock;
3365 struct udp_tunnel_sock_cfg tunnel_cfg;
3367 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3369 return ERR_PTR(-ENOMEM);
3371 for (h = 0; h < VNI_HASH_SIZE; ++h)
3372 INIT_HLIST_HEAD(&vs->vni_list[h]);
3374 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3377 return ERR_CAST(sock);
3381 refcount_set(&vs->refcnt, 1);
3382 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3384 spin_lock(&vn->sock_lock);
3385 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3386 udp_tunnel_notify_add_rx_port(sock,
3387 (vs->flags & VXLAN_F_GPE) ?
3388 UDP_TUNNEL_TYPE_VXLAN_GPE :
3389 UDP_TUNNEL_TYPE_VXLAN);
3390 spin_unlock(&vn->sock_lock);
3392 /* Mark socket as an encapsulation socket. */
3393 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3394 tunnel_cfg.sk_user_data = vs;
3395 tunnel_cfg.encap_type = 1;
3396 tunnel_cfg.encap_rcv = vxlan_rcv;
3397 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3398 tunnel_cfg.encap_destroy = NULL;
3399 tunnel_cfg.gro_receive = vxlan_gro_receive;
3400 tunnel_cfg.gro_complete = vxlan_gro_complete;
3402 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3407 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3409 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3410 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3411 struct vxlan_sock *vs = NULL;
3412 struct vxlan_dev_node *node;
3413 int l3mdev_index = 0;
3415 if (vxlan->cfg.remote_ifindex)
3416 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3417 vxlan->net, vxlan->cfg.remote_ifindex);
3419 if (!vxlan->cfg.no_share) {
3420 spin_lock(&vn->sock_lock);
3421 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3422 vxlan->cfg.dst_port, vxlan->cfg.flags,
3424 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3425 spin_unlock(&vn->sock_lock);
3428 spin_unlock(&vn->sock_lock);
3431 vs = vxlan_socket_create(vxlan->net, ipv6,
3432 vxlan->cfg.dst_port, vxlan->cfg.flags,
3436 #if IS_ENABLED(CONFIG_IPV6)
3438 rcu_assign_pointer(vxlan->vn6_sock, vs);
3439 node = &vxlan->hlist6;
3443 rcu_assign_pointer(vxlan->vn4_sock, vs);
3444 node = &vxlan->hlist4;
3447 if (metadata && (vxlan->cfg.flags & VXLAN_F_VNIFILTER))
3448 vxlan_vs_add_vnigrp(vxlan, vs, ipv6);
3450 vxlan_vs_add_dev(vs, vxlan, node);
3455 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3457 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3458 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3459 bool ipv4 = !ipv6 || metadata;
3462 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3463 #if IS_ENABLED(CONFIG_IPV6)
3464 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3466 ret = __vxlan_sock_add(vxlan, true);
3467 if (ret < 0 && ret != -EAFNOSUPPORT)
3472 ret = __vxlan_sock_add(vxlan, false);
3474 vxlan_sock_release(vxlan);
3478 int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan,
3479 struct vxlan_config *conf, __be32 vni)
3481 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3482 struct vxlan_dev *tmp;
3484 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3487 if (tmp->cfg.flags & VXLAN_F_VNIFILTER) {
3488 if (!vxlan_vnifilter_lookup(tmp, vni))
3490 } else if (tmp->cfg.vni != vni) {
3493 if (tmp->cfg.dst_port != conf->dst_port)
3495 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3496 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3499 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3500 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3509 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3510 struct net_device **lower,
3511 struct vxlan_dev *old,
3512 struct netlink_ext_ack *extack)
3514 bool use_ipv6 = false;
3516 if (conf->flags & VXLAN_F_GPE) {
3517 /* For now, allow GPE only together with
3518 * COLLECT_METADATA. This can be relaxed later; in such
3519 * case, the other side of the PtP link will have to be
3522 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3523 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3524 NL_SET_ERR_MSG(extack,
3525 "VXLAN GPE does not support this combination of attributes");
3530 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3531 /* Unless IPv6 is explicitly requested, assume IPv4 */
3532 conf->remote_ip.sa.sa_family = AF_INET;
3533 conf->saddr.sa.sa_family = AF_INET;
3534 } else if (!conf->remote_ip.sa.sa_family) {
3535 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3536 } else if (!conf->saddr.sa.sa_family) {
3537 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3540 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3541 NL_SET_ERR_MSG(extack,
3542 "Local and remote address must be from the same family");
3546 if (vxlan_addr_multicast(&conf->saddr)) {
3547 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3551 if (conf->saddr.sa.sa_family == AF_INET6) {
3552 if (!IS_ENABLED(CONFIG_IPV6)) {
3553 NL_SET_ERR_MSG(extack,
3554 "IPv6 support not enabled in the kernel");
3555 return -EPFNOSUPPORT;
3558 conf->flags |= VXLAN_F_IPV6;
3560 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3562 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3564 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3566 if (local_type & IPV6_ADDR_LINKLOCAL) {
3567 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3568 (remote_type != IPV6_ADDR_ANY)) {
3569 NL_SET_ERR_MSG(extack,
3570 "Invalid combination of local and remote address scopes");
3574 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3577 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3578 NL_SET_ERR_MSG(extack,
3579 "Invalid combination of local and remote address scopes");
3583 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3588 if (conf->label && !use_ipv6) {
3589 NL_SET_ERR_MSG(extack,
3590 "Label attribute only applies to IPv6 VXLAN devices");
3594 if (conf->remote_ifindex) {
3595 struct net_device *lowerdev;
3597 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3599 NL_SET_ERR_MSG(extack,
3600 "Invalid local interface, device not found");
3604 #if IS_ENABLED(CONFIG_IPV6)
3606 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3608 if (idev && idev->cnf.disable_ipv6) {
3609 NL_SET_ERR_MSG(extack,
3610 "IPv6 support disabled by administrator");
3618 if (vxlan_addr_multicast(&conf->remote_ip)) {
3619 NL_SET_ERR_MSG(extack,
3620 "Local interface required for multicast remote destination");
3625 #if IS_ENABLED(CONFIG_IPV6)
3626 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3627 NL_SET_ERR_MSG(extack,
3628 "Local interface required for link-local local/remote addresses");
3636 if (!conf->dst_port) {
3637 if (conf->flags & VXLAN_F_GPE)
3638 conf->dst_port = htons(IANA_VXLAN_GPE_UDP_PORT);
3640 conf->dst_port = htons(vxlan_port);
3643 if (!conf->age_interval)
3644 conf->age_interval = FDB_AGE_DEFAULT;
3646 if (vxlan_vni_in_use(src_net, old, conf, conf->vni)) {
3647 NL_SET_ERR_MSG(extack,
3648 "A VXLAN device with the specified VNI already exists");
3655 static void vxlan_config_apply(struct net_device *dev,
3656 struct vxlan_config *conf,
3657 struct net_device *lowerdev,
3658 struct net *src_net,
3661 struct vxlan_dev *vxlan = netdev_priv(dev);
3662 struct vxlan_rdst *dst = &vxlan->default_dst;
3663 unsigned short needed_headroom = ETH_HLEN;
3664 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3665 int max_mtu = ETH_MAX_MTU;
3668 if (conf->flags & VXLAN_F_GPE)
3669 vxlan_raw_setup(dev);
3671 vxlan_ether_setup(dev);
3674 dev->mtu = conf->mtu;
3676 vxlan->net = src_net;
3679 dst->remote_vni = conf->vni;
3681 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3684 dst->remote_ifindex = conf->remote_ifindex;
3686 netif_set_gso_max_size(dev, lowerdev->gso_max_size);
3687 netif_set_gso_max_segs(dev, lowerdev->gso_max_segs);
3689 needed_headroom = lowerdev->hard_header_len;
3690 needed_headroom += lowerdev->needed_headroom;
3692 dev->needed_tailroom = lowerdev->needed_tailroom;
3694 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3696 if (max_mtu < ETH_MIN_MTU)
3697 max_mtu = ETH_MIN_MTU;
3699 if (!changelink && !conf->mtu)
3703 if (dev->mtu > max_mtu)
3706 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3707 needed_headroom += VXLAN6_HEADROOM;
3709 needed_headroom += VXLAN_HEADROOM;
3710 dev->needed_headroom = needed_headroom;
3712 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3715 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3716 struct vxlan_config *conf, bool changelink,
3717 struct netlink_ext_ack *extack)
3719 struct vxlan_dev *vxlan = netdev_priv(dev);
3720 struct net_device *lowerdev;
3723 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3727 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3732 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3733 struct vxlan_config *conf,
3734 struct netlink_ext_ack *extack)
3736 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3737 struct vxlan_dev *vxlan = netdev_priv(dev);
3738 struct net_device *remote_dev = NULL;
3739 struct vxlan_fdb *f = NULL;
3740 bool unregister = false;
3741 struct vxlan_rdst *dst;
3744 dst = &vxlan->default_dst;
3745 err = vxlan_dev_configure(net, dev, conf, false, extack);
3749 dev->ethtool_ops = &vxlan_ethtool_ops;
3751 /* create an fdb entry for a valid default destination */
3752 if (!vxlan_addr_any(&dst->remote_ip)) {
3753 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3755 NUD_REACHABLE | NUD_PERMANENT,
3756 vxlan->cfg.dst_port,
3759 dst->remote_ifindex,
3760 NTF_SELF, 0, &f, extack);
3765 err = register_netdevice(dev);
3770 if (dst->remote_ifindex) {
3771 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3777 err = netdev_upper_dev_link(remote_dev, dev, extack);
3782 err = rtnl_configure_link(dev, NULL);
3787 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3789 /* notify default fdb entry */
3790 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3791 RTM_NEWNEIGH, true, extack);
3793 vxlan_fdb_destroy(vxlan, f, false, false);
3795 netdev_upper_dev_unlink(remote_dev, dev);
3800 list_add(&vxlan->next, &vn->vxlan_list);
3802 dst->remote_dev = remote_dev;
3806 netdev_upper_dev_unlink(remote_dev, dev);
3808 /* unregister_netdevice() destroys the default FDB entry with deletion
3809 * notification. But the addition notification was not sent yet, so
3810 * destroy the entry by hand here.
3813 __vxlan_fdb_free(f);
3816 unregister_netdevice(dev);
3820 /* Set/clear flags based on attribute */
3821 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3822 int attrtype, unsigned long mask, bool changelink,
3823 bool changelink_supported,
3824 struct netlink_ext_ack *extack)
3826 unsigned long flags;
3831 if (changelink && !changelink_supported) {
3832 vxlan_flag_attr_error(attrtype, extack);
3836 if (vxlan_policy[attrtype].type == NLA_FLAG)
3837 flags = conf->flags | mask;
3838 else if (nla_get_u8(tb[attrtype]))
3839 flags = conf->flags | mask;
3841 flags = conf->flags & ~mask;
3843 conf->flags = flags;
3848 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3849 struct net_device *dev, struct vxlan_config *conf,
3850 bool changelink, struct netlink_ext_ack *extack)
3852 struct vxlan_dev *vxlan = netdev_priv(dev);
3855 memset(conf, 0, sizeof(*conf));
3857 /* if changelink operation, start with old existing cfg */
3859 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3861 if (data[IFLA_VXLAN_ID]) {
3862 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3864 if (changelink && (vni != conf->vni)) {
3865 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3868 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3871 if (data[IFLA_VXLAN_GROUP]) {
3872 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3873 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3877 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3878 conf->remote_ip.sa.sa_family = AF_INET;
3879 } else if (data[IFLA_VXLAN_GROUP6]) {
3880 if (!IS_ENABLED(CONFIG_IPV6)) {
3881 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3882 return -EPFNOSUPPORT;
3885 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3886 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3890 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3891 conf->remote_ip.sa.sa_family = AF_INET6;
3894 if (data[IFLA_VXLAN_LOCAL]) {
3895 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3896 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
3900 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3901 conf->saddr.sa.sa_family = AF_INET;
3902 } else if (data[IFLA_VXLAN_LOCAL6]) {
3903 if (!IS_ENABLED(CONFIG_IPV6)) {
3904 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
3905 return -EPFNOSUPPORT;
3908 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3909 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
3913 /* TODO: respect scope id */
3914 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3915 conf->saddr.sa.sa_family = AF_INET6;
3918 if (data[IFLA_VXLAN_LINK])
3919 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3921 if (data[IFLA_VXLAN_TOS])
3922 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3924 if (data[IFLA_VXLAN_TTL])
3925 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3927 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3928 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3929 VXLAN_F_TTL_INHERIT, changelink, false,
3936 if (data[IFLA_VXLAN_LABEL])
3937 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3938 IPV6_FLOWLABEL_MASK;
3940 if (data[IFLA_VXLAN_LEARNING]) {
3941 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3942 VXLAN_F_LEARN, changelink, true,
3946 } else if (!changelink) {
3947 /* default to learn on a new device */
3948 conf->flags |= VXLAN_F_LEARN;
3951 if (data[IFLA_VXLAN_AGEING])
3952 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3954 if (data[IFLA_VXLAN_PROXY]) {
3955 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
3956 VXLAN_F_PROXY, changelink, false,
3962 if (data[IFLA_VXLAN_RSC]) {
3963 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
3964 VXLAN_F_RSC, changelink, false,
3970 if (data[IFLA_VXLAN_L2MISS]) {
3971 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
3972 VXLAN_F_L2MISS, changelink, false,
3978 if (data[IFLA_VXLAN_L3MISS]) {
3979 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
3980 VXLAN_F_L3MISS, changelink, false,
3986 if (data[IFLA_VXLAN_LIMIT]) {
3988 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
3989 "Cannot change limit");
3992 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3995 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3996 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
3997 VXLAN_F_COLLECT_METADATA, changelink, false,
4003 if (data[IFLA_VXLAN_PORT_RANGE]) {
4005 const struct ifla_vxlan_port_range *p
4006 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4007 conf->port_min = ntohs(p->low);
4008 conf->port_max = ntohs(p->high);
4010 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4011 "Cannot change port range");
4016 if (data[IFLA_VXLAN_PORT]) {
4018 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4019 "Cannot change port");
4022 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4025 if (data[IFLA_VXLAN_UDP_CSUM]) {
4027 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4028 "Cannot change UDP_CSUM flag");
4031 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4032 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4035 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4036 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4037 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4043 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4044 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4045 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4051 if (data[IFLA_VXLAN_REMCSUM_TX]) {
4052 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4053 VXLAN_F_REMCSUM_TX, changelink, false,
4059 if (data[IFLA_VXLAN_REMCSUM_RX]) {
4060 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4061 VXLAN_F_REMCSUM_RX, changelink, false,
4067 if (data[IFLA_VXLAN_GBP]) {
4068 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4069 VXLAN_F_GBP, changelink, false, extack);
4074 if (data[IFLA_VXLAN_GPE]) {
4075 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4076 VXLAN_F_GPE, changelink, false,
4082 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4083 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4084 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4092 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4093 "Cannot change mtu");
4096 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4099 if (data[IFLA_VXLAN_DF])
4100 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4102 if (data[IFLA_VXLAN_VNIFILTER]) {
4103 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_VNIFILTER,
4104 VXLAN_F_VNIFILTER, changelink, false,
4109 if ((conf->flags & VXLAN_F_VNIFILTER) &&
4110 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
4111 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_VNIFILTER],
4112 "vxlan vnifilter only valid in collect metadata mode");
4120 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
4121 struct nlattr *tb[], struct nlattr *data[],
4122 struct netlink_ext_ack *extack)
4124 struct vxlan_config conf;
4127 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4131 return __vxlan_dev_create(src_net, dev, &conf, extack);
4134 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4135 struct nlattr *data[],
4136 struct netlink_ext_ack *extack)
4138 struct vxlan_dev *vxlan = netdev_priv(dev);
4139 struct net_device *lowerdev;
4140 struct vxlan_config conf;
4141 struct vxlan_rdst *dst;
4144 dst = &vxlan->default_dst;
4145 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4149 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4154 if (dst->remote_dev == lowerdev)
4157 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4162 /* handle default dst entry */
4163 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4164 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4166 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4167 if (!vxlan_addr_any(&conf.remote_ip)) {
4168 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4170 NUD_REACHABLE | NUD_PERMANENT,
4171 NLM_F_APPEND | NLM_F_CREATE,
4172 vxlan->cfg.dst_port,
4174 conf.remote_ifindex,
4175 NTF_SELF, 0, true, extack);
4177 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4178 netdev_adjacent_change_abort(dst->remote_dev,
4183 if (!vxlan_addr_any(&dst->remote_ip))
4184 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4186 vxlan->cfg.dst_port,
4189 dst->remote_ifindex,
4191 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4193 /* If vni filtering device, also update fdb entries of
4194 * all vnis that were using default remote ip
4196 if (vxlan->cfg.flags & VXLAN_F_VNIFILTER) {
4197 err = vxlan_vnilist_update_group(vxlan, &dst->remote_ip,
4198 &conf.remote_ip, extack);
4200 netdev_adjacent_change_abort(dst->remote_dev,
4207 if (conf.age_interval != vxlan->cfg.age_interval)
4208 mod_timer(&vxlan->age_timer, jiffies);
4210 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4211 if (lowerdev && lowerdev != dst->remote_dev)
4212 dst->remote_dev = lowerdev;
4213 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4217 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4219 struct vxlan_dev *vxlan = netdev_priv(dev);
4221 vxlan_flush(vxlan, true);
4223 list_del(&vxlan->next);
4224 unregister_netdevice_queue(dev, head);
4225 if (vxlan->default_dst.remote_dev)
4226 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4229 static size_t vxlan_get_size(const struct net_device *dev)
4232 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4233 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4234 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4235 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4236 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4237 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4238 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4239 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4240 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4241 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4242 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4243 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4244 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4245 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4246 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4247 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4248 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4249 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4250 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4251 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4252 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4253 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4254 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4255 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4259 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4261 const struct vxlan_dev *vxlan = netdev_priv(dev);
4262 const struct vxlan_rdst *dst = &vxlan->default_dst;
4263 struct ifla_vxlan_port_range ports = {
4264 .low = htons(vxlan->cfg.port_min),
4265 .high = htons(vxlan->cfg.port_max),
4268 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4269 goto nla_put_failure;
4271 if (!vxlan_addr_any(&dst->remote_ip)) {
4272 if (dst->remote_ip.sa.sa_family == AF_INET) {
4273 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4274 dst->remote_ip.sin.sin_addr.s_addr))
4275 goto nla_put_failure;
4276 #if IS_ENABLED(CONFIG_IPV6)
4278 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4279 &dst->remote_ip.sin6.sin6_addr))
4280 goto nla_put_failure;
4285 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4286 goto nla_put_failure;
4288 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4289 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4290 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4291 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4292 goto nla_put_failure;
4293 #if IS_ENABLED(CONFIG_IPV6)
4295 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4296 &vxlan->cfg.saddr.sin6.sin6_addr))
4297 goto nla_put_failure;
4302 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4303 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4304 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4305 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4306 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4307 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4308 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4309 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4310 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4311 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4312 nla_put_u8(skb, IFLA_VXLAN_RSC,
4313 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4314 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4315 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4316 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4317 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4318 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4319 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4320 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4321 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4322 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4323 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4324 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4325 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4326 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4327 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4328 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4329 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4330 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4331 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4332 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4333 goto nla_put_failure;
4335 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4336 goto nla_put_failure;
4338 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4339 nla_put_flag(skb, IFLA_VXLAN_GBP))
4340 goto nla_put_failure;
4342 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4343 nla_put_flag(skb, IFLA_VXLAN_GPE))
4344 goto nla_put_failure;
4346 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4347 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4348 goto nla_put_failure;
4350 if (vxlan->cfg.flags & VXLAN_F_VNIFILTER &&
4351 nla_put_u8(skb, IFLA_VXLAN_VNIFILTER,
4352 !!(vxlan->cfg.flags & VXLAN_F_VNIFILTER)))
4353 goto nla_put_failure;
4361 static struct net *vxlan_get_link_net(const struct net_device *dev)
4363 struct vxlan_dev *vxlan = netdev_priv(dev);
4368 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4370 .maxtype = IFLA_VXLAN_MAX,
4371 .policy = vxlan_policy,
4372 .priv_size = sizeof(struct vxlan_dev),
4373 .setup = vxlan_setup,
4374 .validate = vxlan_validate,
4375 .newlink = vxlan_newlink,
4376 .changelink = vxlan_changelink,
4377 .dellink = vxlan_dellink,
4378 .get_size = vxlan_get_size,
4379 .fill_info = vxlan_fill_info,
4380 .get_link_net = vxlan_get_link_net,
4383 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4384 u8 name_assign_type,
4385 struct vxlan_config *conf)
4387 struct nlattr *tb[IFLA_MAX + 1];
4388 struct net_device *dev;
4391 memset(&tb, 0, sizeof(tb));
4393 dev = rtnl_create_link(net, name, name_assign_type,
4394 &vxlan_link_ops, tb, NULL);
4398 err = __vxlan_dev_create(net, dev, conf, NULL);
4401 return ERR_PTR(err);
4404 err = rtnl_configure_link(dev, NULL);
4406 LIST_HEAD(list_kill);
4408 vxlan_dellink(dev, &list_kill);
4409 unregister_netdevice_many(&list_kill);
4410 return ERR_PTR(err);
4415 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4417 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4418 struct net_device *dev)
4420 struct vxlan_dev *vxlan, *next;
4421 LIST_HEAD(list_kill);
4423 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4424 struct vxlan_rdst *dst = &vxlan->default_dst;
4426 /* In case we created vxlan device with carrier
4427 * and we loose the carrier due to module unload
4428 * we also need to remove vxlan device. In other
4429 * cases, it's not necessary and remote_ifindex
4430 * is 0 here, so no matches.
4432 if (dst->remote_ifindex == dev->ifindex)
4433 vxlan_dellink(vxlan->dev, &list_kill);
4436 unregister_netdevice_many(&list_kill);
4439 static int vxlan_netdevice_event(struct notifier_block *unused,
4440 unsigned long event, void *ptr)
4442 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4443 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4445 if (event == NETDEV_UNREGISTER)
4446 vxlan_handle_lowerdev_unregister(vn, dev);
4447 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
4448 vxlan_offload_rx_ports(dev, true);
4449 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
4450 vxlan_offload_rx_ports(dev, false);
4455 static struct notifier_block vxlan_notifier_block __read_mostly = {
4456 .notifier_call = vxlan_netdevice_event,
4460 vxlan_fdb_offloaded_set(struct net_device *dev,
4461 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4463 struct vxlan_dev *vxlan = netdev_priv(dev);
4464 struct vxlan_rdst *rdst;
4465 struct vxlan_fdb *f;
4468 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4470 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4472 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4476 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4477 fdb_info->remote_port,
4478 fdb_info->remote_vni,
4479 fdb_info->remote_ifindex);
4483 rdst->offloaded = fdb_info->offloaded;
4486 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4490 vxlan_fdb_external_learn_add(struct net_device *dev,
4491 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4493 struct vxlan_dev *vxlan = netdev_priv(dev);
4494 struct netlink_ext_ack *extack;
4498 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4499 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4501 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4502 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4504 NLM_F_CREATE | NLM_F_REPLACE,
4505 fdb_info->remote_port,
4507 fdb_info->remote_vni,
4508 fdb_info->remote_ifindex,
4509 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4511 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4517 vxlan_fdb_external_learn_del(struct net_device *dev,
4518 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4520 struct vxlan_dev *vxlan = netdev_priv(dev);
4521 struct vxlan_fdb *f;
4525 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4526 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4528 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4531 else if (f->flags & NTF_EXT_LEARNED)
4532 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4533 fdb_info->remote_ip,
4534 fdb_info->remote_port,
4536 fdb_info->remote_vni,
4537 fdb_info->remote_ifindex,
4540 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4545 static int vxlan_switchdev_event(struct notifier_block *unused,
4546 unsigned long event, void *ptr)
4548 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4549 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4553 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4554 vxlan_fdb_offloaded_set(dev, ptr);
4556 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4558 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4560 err = notifier_from_errno(err);
4563 fdb_info->offloaded = true;
4564 vxlan_fdb_offloaded_set(dev, fdb_info);
4566 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4568 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4570 err = notifier_from_errno(err);
4573 fdb_info->offloaded = false;
4574 vxlan_fdb_offloaded_set(dev, fdb_info);
4581 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4582 .notifier_call = vxlan_switchdev_event,
4585 static void vxlan_fdb_nh_flush(struct nexthop *nh)
4587 struct vxlan_fdb *fdb;
4588 struct vxlan_dev *vxlan;
4592 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
4593 vxlan = rcu_dereference(fdb->vdev);
4595 hash_index = fdb_head_index(vxlan, fdb->eth_addr,
4596 vxlan->default_dst.remote_vni);
4597 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4598 if (!hlist_unhashed(&fdb->hlist))
4599 vxlan_fdb_destroy(vxlan, fdb, false, false);
4600 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4605 static int vxlan_nexthop_event(struct notifier_block *nb,
4606 unsigned long event, void *ptr)
4608 struct nh_notifier_info *info = ptr;
4611 if (event != NEXTHOP_EVENT_DEL)
4614 nh = nexthop_find_by_id(info->net, info->id);
4618 vxlan_fdb_nh_flush(nh);
4623 static __net_init int vxlan_init_net(struct net *net)
4625 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4628 INIT_LIST_HEAD(&vn->vxlan_list);
4629 spin_lock_init(&vn->sock_lock);
4630 vn->nexthop_notifier_block.notifier_call = vxlan_nexthop_event;
4632 for (h = 0; h < PORT_HASH_SIZE; ++h)
4633 INIT_HLIST_HEAD(&vn->sock_list[h]);
4635 return register_nexthop_notifier(net, &vn->nexthop_notifier_block,
4639 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4641 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4642 struct vxlan_dev *vxlan, *next;
4643 struct net_device *dev, *aux;
4645 for_each_netdev_safe(net, dev, aux)
4646 if (dev->rtnl_link_ops == &vxlan_link_ops)
4647 unregister_netdevice_queue(dev, head);
4649 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4650 /* If vxlan->dev is in the same netns, it has already been added
4651 * to the list by the previous loop.
4653 if (!net_eq(dev_net(vxlan->dev), net))
4654 unregister_netdevice_queue(vxlan->dev, head);
4659 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4665 list_for_each_entry(net, net_list, exit_list) {
4666 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4668 unregister_nexthop_notifier(net, &vn->nexthop_notifier_block);
4671 list_for_each_entry(net, net_list, exit_list)
4672 vxlan_destroy_tunnels(net, &list);
4674 unregister_netdevice_many(&list);
4677 list_for_each_entry(net, net_list, exit_list) {
4678 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4680 for (h = 0; h < PORT_HASH_SIZE; ++h)
4681 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4685 static struct pernet_operations vxlan_net_ops = {
4686 .init = vxlan_init_net,
4687 .exit_batch = vxlan_exit_batch_net,
4688 .id = &vxlan_net_id,
4689 .size = sizeof(struct vxlan_net),
4692 static int __init vxlan_init_module(void)
4696 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4698 rc = register_pernet_subsys(&vxlan_net_ops);
4702 rc = register_netdevice_notifier(&vxlan_notifier_block);
4706 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4710 rc = rtnl_link_register(&vxlan_link_ops);
4714 vxlan_vnifilter_init();
4718 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4720 unregister_netdevice_notifier(&vxlan_notifier_block);
4722 unregister_pernet_subsys(&vxlan_net_ops);
4726 late_initcall(vxlan_init_module);
4728 static void __exit vxlan_cleanup_module(void)
4730 vxlan_vnifilter_uninit();
4731 rtnl_link_unregister(&vxlan_link_ops);
4732 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4733 unregister_netdevice_notifier(&vxlan_notifier_block);
4734 unregister_pernet_subsys(&vxlan_net_ops);
4735 /* rcu_barrier() is called by netns */
4737 module_exit(vxlan_cleanup_module);
4739 MODULE_LICENSE("GPL");
4740 MODULE_VERSION(VXLAN_VERSION);
4742 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4743 MODULE_ALIAS_RTNL_LINK("vxlan");