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>
20 #include <net/ipv6_stubs.h>
23 #include <net/rtnetlink.h>
24 #include <net/inet_ecn.h>
25 #include <net/net_namespace.h>
26 #include <net/netns/generic.h>
27 #include <net/tun_proto.h>
28 #include <net/vxlan.h>
29 #include <net/nexthop.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_tunnel.h>
33 #include <net/ip6_checksum.h>
36 #define VXLAN_VERSION "0.1"
38 #define PORT_HASH_BITS 8
39 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
40 #define FDB_AGE_DEFAULT 300 /* 5 min */
41 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43 /* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
45 * for compatibility with early adopters.
47 static unsigned short vxlan_port __read_mostly = 8472;
48 module_param_named(udp_port, vxlan_port, ushort, 0444);
49 MODULE_PARM_DESC(udp_port, "Destination UDP port");
51 static bool log_ecn_error = true;
52 module_param(log_ecn_error, bool, 0644);
53 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55 static unsigned int vxlan_net_id;
56 static struct rtnl_link_ops vxlan_link_ops;
58 static const u8 all_zeros_mac[ETH_ALEN + 2];
60 static int vxlan_sock_add(struct vxlan_dev *vxlan);
62 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
64 /* per-network namespace private data for this module */
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
71 /* Forwarding table entry */
73 struct hlist_node hlist; /* linked list of entries */
75 unsigned long updated; /* jiffies */
77 struct list_head remotes;
78 u8 eth_addr[ETH_ALEN];
79 u16 state; /* see ndm_state */
81 u16 flags; /* see ndm_flags and below */
82 struct list_head nh_list;
83 struct nexthop __rcu *nh;
84 struct vxlan_dev *vdev;
87 #define NTF_VXLAN_ADDED_BY_USER 0x100
89 /* salt for hash table */
90 static u32 vxlan_salt __read_mostly;
92 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
94 return vs->flags & VXLAN_F_COLLECT_METADATA ||
95 ip_tunnel_collect_metadata();
98 #if IS_ENABLED(CONFIG_IPV6)
100 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
102 if (a->sa.sa_family != b->sa.sa_family)
104 if (a->sa.sa_family == AF_INET6)
105 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
107 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
110 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
112 if (nla_len(nla) >= sizeof(struct in6_addr)) {
113 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
114 ip->sa.sa_family = AF_INET6;
116 } else if (nla_len(nla) >= sizeof(__be32)) {
117 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
118 ip->sa.sa_family = AF_INET;
121 return -EAFNOSUPPORT;
125 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
126 const union vxlan_addr *ip)
128 if (ip->sa.sa_family == AF_INET6)
129 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
131 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
134 #else /* !CONFIG_IPV6 */
137 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
139 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
142 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
144 if (nla_len(nla) >= sizeof(struct in6_addr)) {
145 return -EAFNOSUPPORT;
146 } else if (nla_len(nla) >= sizeof(__be32)) {
147 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
148 ip->sa.sa_family = AF_INET;
151 return -EAFNOSUPPORT;
155 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
156 const union vxlan_addr *ip)
158 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
162 /* Virtual Network hash table head */
163 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
165 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
168 /* Socket hash table head */
169 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
171 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
173 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
176 /* First remote destination for a forwarding entry.
177 * Guaranteed to be non-NULL because remotes are never deleted.
179 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
181 if (rcu_access_pointer(fdb->nh))
183 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
186 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
188 if (rcu_access_pointer(fdb->nh))
190 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
193 /* Find VXLAN socket based on network namespace, address family and UDP port
194 * and enabled unshareable flags.
196 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
197 __be16 port, u32 flags, int ifindex)
199 struct vxlan_sock *vs;
201 flags &= VXLAN_F_RCV_FLAGS;
203 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
204 if (inet_sk(vs->sock->sk)->inet_sport == port &&
205 vxlan_get_sk_family(vs) == family &&
206 vs->flags == flags &&
207 vs->sock->sk->sk_bound_dev_if == ifindex)
213 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
216 struct vxlan_dev_node *node;
218 /* For flow based devices, map all packets to VNI 0 */
219 if (vs->flags & VXLAN_F_COLLECT_METADATA)
222 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
223 if (node->vxlan->default_dst.remote_vni != vni)
226 if (IS_ENABLED(CONFIG_IPV6)) {
227 const struct vxlan_config *cfg = &node->vxlan->cfg;
229 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
230 cfg->remote_ifindex != ifindex)
240 /* Look up VNI in a per net namespace table */
241 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
242 __be32 vni, sa_family_t family,
243 __be16 port, u32 flags)
245 struct vxlan_sock *vs;
247 vs = vxlan_find_sock(net, family, port, flags, ifindex);
251 return vxlan_vs_find_vni(vs, ifindex, vni);
254 /* Fill in neighbour message in skbuff. */
255 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
256 const struct vxlan_fdb *fdb,
257 u32 portid, u32 seq, int type, unsigned int flags,
258 const struct vxlan_rdst *rdst)
260 unsigned long now = jiffies;
261 struct nda_cacheinfo ci;
262 bool send_ip, send_eth;
263 struct nlmsghdr *nlh;
267 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
271 ndm = nlmsg_data(nlh);
272 memset(ndm, 0, sizeof(*ndm));
274 send_eth = send_ip = true;
276 nh = rcu_dereference_rtnl(fdb->nh);
277 if (type == RTM_GETNEIGH) {
279 send_ip = !vxlan_addr_any(&rdst->remote_ip);
280 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
282 ndm->ndm_family = nexthop_get_family(nh);
284 send_eth = !is_zero_ether_addr(fdb->eth_addr);
286 ndm->ndm_family = AF_BRIDGE;
287 ndm->ndm_state = fdb->state;
288 ndm->ndm_ifindex = vxlan->dev->ifindex;
289 ndm->ndm_flags = fdb->flags;
290 if (rdst && rdst->offloaded)
291 ndm->ndm_flags |= NTF_OFFLOADED;
292 ndm->ndm_type = RTN_UNICAST;
294 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
295 nla_put_s32(skb, NDA_LINK_NETNSID,
296 peernet2id(dev_net(vxlan->dev), vxlan->net)))
297 goto nla_put_failure;
299 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
300 goto nla_put_failure;
302 if (nla_put_u32(skb, NDA_NH_ID, nh->id))
303 goto nla_put_failure;
305 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
307 goto nla_put_failure;
309 if (rdst->remote_port &&
310 rdst->remote_port != vxlan->cfg.dst_port &&
311 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
312 goto nla_put_failure;
313 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
314 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
315 goto nla_put_failure;
316 if (rdst->remote_ifindex &&
317 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
318 goto nla_put_failure;
321 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
322 nla_put_u32(skb, NDA_SRC_VNI,
323 be32_to_cpu(fdb->vni)))
324 goto nla_put_failure;
326 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
327 ci.ndm_confirmed = 0;
328 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
331 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
332 goto nla_put_failure;
338 nlmsg_cancel(skb, nlh);
342 static inline size_t vxlan_nlmsg_size(void)
344 return NLMSG_ALIGN(sizeof(struct ndmsg))
345 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
346 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
347 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
348 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
349 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
350 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
351 + nla_total_size(sizeof(struct nda_cacheinfo));
354 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
355 struct vxlan_rdst *rd, int type)
357 struct net *net = dev_net(vxlan->dev);
361 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
365 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
367 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
368 WARN_ON(err == -EMSGSIZE);
373 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
377 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
380 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
381 const struct vxlan_fdb *fdb,
382 const struct vxlan_rdst *rd,
383 struct netlink_ext_ack *extack,
384 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
386 fdb_info->info.dev = vxlan->dev;
387 fdb_info->info.extack = extack;
388 fdb_info->remote_ip = rd->remote_ip;
389 fdb_info->remote_port = rd->remote_port;
390 fdb_info->remote_vni = rd->remote_vni;
391 fdb_info->remote_ifindex = rd->remote_ifindex;
392 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
393 fdb_info->vni = fdb->vni;
394 fdb_info->offloaded = rd->offloaded;
395 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
398 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
399 struct vxlan_fdb *fdb,
400 struct vxlan_rdst *rd,
402 struct netlink_ext_ack *extack)
404 struct switchdev_notifier_vxlan_fdb_info info;
405 enum switchdev_notifier_type notifier_type;
411 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
412 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
413 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
414 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
416 return notifier_to_errno(ret);
419 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
420 struct vxlan_rdst *rd, int type, bool swdev_notify,
421 struct netlink_ext_ack *extack)
425 if (swdev_notify && rd) {
428 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
434 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
440 __vxlan_fdb_notify(vxlan, fdb, rd, type);
444 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
446 struct vxlan_dev *vxlan = netdev_priv(dev);
447 struct vxlan_fdb f = {
450 struct vxlan_rdst remote = {
451 .remote_ip = *ipa, /* goes to NDA_DST */
452 .remote_vni = cpu_to_be32(VXLAN_N_VID),
455 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
458 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
460 struct vxlan_fdb f = {
463 struct vxlan_rdst remote = { };
465 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
467 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
470 /* Hash Ethernet address */
471 static u32 eth_hash(const unsigned char *addr)
473 u64 value = get_unaligned((u64 *)addr);
475 /* only want 6 bytes */
481 return hash_64(value, FDB_HASH_BITS);
484 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
486 /* use 1 byte of OUI and 3 bytes of NIC */
487 u32 key = get_unaligned((u32 *)(addr + 2));
489 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
492 static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
494 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
495 return eth_vni_hash(mac, vni);
497 return eth_hash(mac);
500 /* Hash chain to use given mac address */
501 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
502 const u8 *mac, __be32 vni)
504 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
507 /* Look up Ethernet address in forwarding table */
508 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
509 const u8 *mac, __be32 vni)
511 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
514 hlist_for_each_entry_rcu(f, head, hlist) {
515 if (ether_addr_equal(mac, f->eth_addr)) {
516 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
528 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
529 const u8 *mac, __be32 vni)
533 f = __vxlan_find_mac(vxlan, mac, vni);
534 if (f && f->used != jiffies)
540 /* caller should hold vxlan->hash_lock */
541 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
542 union vxlan_addr *ip, __be16 port,
543 __be32 vni, __u32 ifindex)
545 struct vxlan_rdst *rd;
547 list_for_each_entry(rd, &f->remotes, list) {
548 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
549 rd->remote_port == port &&
550 rd->remote_vni == vni &&
551 rd->remote_ifindex == ifindex)
558 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
559 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
561 struct vxlan_dev *vxlan = netdev_priv(dev);
562 u8 eth_addr[ETH_ALEN + 2] = { 0 };
563 struct vxlan_rdst *rdst;
567 if (is_multicast_ether_addr(mac) ||
568 is_zero_ether_addr(mac))
571 ether_addr_copy(eth_addr, mac);
575 f = __vxlan_find_mac(vxlan, eth_addr, vni);
581 rdst = first_remote_rcu(f);
582 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
588 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
590 static int vxlan_fdb_notify_one(struct notifier_block *nb,
591 const struct vxlan_dev *vxlan,
592 const struct vxlan_fdb *f,
593 const struct vxlan_rdst *rdst,
594 struct netlink_ext_ack *extack)
596 struct switchdev_notifier_vxlan_fdb_info fdb_info;
599 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
600 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
602 return notifier_to_errno(rc);
605 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
606 struct notifier_block *nb,
607 struct netlink_ext_ack *extack)
609 struct vxlan_dev *vxlan;
610 struct vxlan_rdst *rdst;
615 if (!netif_is_vxlan(dev))
617 vxlan = netdev_priv(dev);
619 for (h = 0; h < FDB_HASH_SIZE; ++h) {
620 spin_lock_bh(&vxlan->hash_lock[h]);
621 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
623 list_for_each_entry(rdst, &f->remotes, list) {
624 rc = vxlan_fdb_notify_one(nb, vxlan,
632 spin_unlock_bh(&vxlan->hash_lock[h]);
637 spin_unlock_bh(&vxlan->hash_lock[h]);
640 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
642 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
644 struct vxlan_dev *vxlan;
645 struct vxlan_rdst *rdst;
649 if (!netif_is_vxlan(dev))
651 vxlan = netdev_priv(dev);
653 for (h = 0; h < FDB_HASH_SIZE; ++h) {
654 spin_lock_bh(&vxlan->hash_lock[h]);
655 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
657 list_for_each_entry(rdst, &f->remotes, list)
658 rdst->offloaded = false;
659 spin_unlock_bh(&vxlan->hash_lock[h]);
663 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
665 /* Replace destination of unicast mac */
666 static int vxlan_fdb_replace(struct vxlan_fdb *f,
667 union vxlan_addr *ip, __be16 port, __be32 vni,
668 __u32 ifindex, struct vxlan_rdst *oldrd)
670 struct vxlan_rdst *rd;
672 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
676 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
681 dst_cache_reset(&rd->dst_cache);
683 rd->remote_port = port;
684 rd->remote_vni = vni;
685 rd->remote_ifindex = ifindex;
686 rd->offloaded = false;
690 /* Add/update destinations for multicast */
691 static int vxlan_fdb_append(struct vxlan_fdb *f,
692 union vxlan_addr *ip, __be16 port, __be32 vni,
693 __u32 ifindex, struct vxlan_rdst **rdp)
695 struct vxlan_rdst *rd;
697 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
701 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
705 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
711 rd->remote_port = port;
712 rd->offloaded = false;
713 rd->remote_vni = vni;
714 rd->remote_ifindex = ifindex;
716 list_add_tail_rcu(&rd->list, &f->remotes);
722 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
724 struct vxlanhdr *vh, size_t hdrlen,
726 struct gro_remcsum *grc,
729 size_t start, offset;
731 if (skb->remcsum_offload)
734 if (!NAPI_GRO_CB(skb)->csum_valid)
737 start = vxlan_rco_start(vni_field);
738 offset = start + vxlan_rco_offset(vni_field);
740 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
741 start, offset, grc, nopartial);
743 skb->remcsum_offload = 1;
748 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
749 struct list_head *head,
752 struct sk_buff *pp = NULL;
754 struct vxlanhdr *vh, *vh2;
755 unsigned int hlen, off_vx;
757 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
759 struct gro_remcsum grc;
761 skb_gro_remcsum_init(&grc);
763 off_vx = skb_gro_offset(skb);
764 hlen = off_vx + sizeof(*vh);
765 vh = skb_gro_header_fast(skb, off_vx);
766 if (skb_gro_header_hard(skb, hlen)) {
767 vh = skb_gro_header_slow(skb, hlen, off_vx);
772 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
774 flags = vh->vx_flags;
776 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
777 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
780 VXLAN_F_REMCSUM_NOPARTIAL));
786 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
788 list_for_each_entry(p, head, list) {
789 if (!NAPI_GRO_CB(p)->same_flow)
792 vh2 = (struct vxlanhdr *)(p->data + off_vx);
793 if (vh->vx_flags != vh2->vx_flags ||
794 vh->vx_vni != vh2->vx_vni) {
795 NAPI_GRO_CB(p)->same_flow = 0;
800 pp = call_gro_receive(eth_gro_receive, head, skb);
804 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
809 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
811 /* Sets 'skb->inner_mac_header' since we are always called with
812 * 'skb->encapsulation' set.
814 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
817 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
818 __u16 state, __be32 src_vni,
823 f = kmalloc(sizeof(*f), GFP_ATOMIC);
827 f->flags = ndm_flags;
828 f->updated = f->used = jiffies;
832 INIT_LIST_HEAD(&f->nh_list);
833 INIT_LIST_HEAD(&f->remotes);
834 memcpy(f->eth_addr, mac, ETH_ALEN);
839 static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
840 __be32 src_vni, struct vxlan_fdb *f)
843 hlist_add_head_rcu(&f->hlist,
844 vxlan_fdb_head(vxlan, mac, src_vni));
847 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
848 u32 nhid, struct netlink_ext_ack *extack)
850 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
851 struct nh_group *nhg;
855 if (old_nh && old_nh->id == nhid)
858 nh = nexthop_find_by_id(vxlan->net, nhid);
860 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
865 if (!nexthop_get(nh)) {
866 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
870 if (!nh->is_fdb_nh) {
871 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
875 if (!nh->is_group || !nh->nh_grp->mpath) {
876 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
880 /* check nexthop group family */
881 nhg = rtnl_dereference(nh->nh_grp);
882 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
886 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
893 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
900 list_del_rcu(&fdb->nh_list);
903 rcu_assign_pointer(fdb->nh, nh);
904 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
913 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
914 const u8 *mac, union vxlan_addr *ip,
915 __u16 state, __be16 port, __be32 src_vni,
916 __be32 vni, __u32 ifindex, __u16 ndm_flags,
917 u32 nhid, struct vxlan_fdb **fdb,
918 struct netlink_ext_ack *extack)
920 struct vxlan_rdst *rd = NULL;
924 if (vxlan->cfg.addrmax &&
925 vxlan->addrcnt >= vxlan->cfg.addrmax)
928 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
929 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
934 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
936 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
949 static void __vxlan_fdb_free(struct vxlan_fdb *f)
951 struct vxlan_rdst *rd, *nd;
954 nh = rcu_dereference_raw(f->nh);
956 rcu_assign_pointer(f->nh, NULL);
957 list_del_rcu(&f->nh_list);
961 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
962 dst_cache_destroy(&rd->dst_cache);
968 static void vxlan_fdb_free(struct rcu_head *head)
970 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
975 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
976 bool do_notify, bool swdev_notify)
978 struct vxlan_rdst *rd;
980 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
984 if (rcu_access_pointer(f->nh))
985 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
988 list_for_each_entry(rd, &f->remotes, list)
989 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
993 hlist_del_rcu(&f->hlist);
995 call_rcu(&f->rcu, vxlan_fdb_free);
998 static void vxlan_dst_free(struct rcu_head *head)
1000 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
1002 dst_cache_destroy(&rd->dst_cache);
1006 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1007 union vxlan_addr *ip,
1008 __u16 state, __u16 flags,
1009 __be16 port, __be32 vni,
1010 __u32 ifindex, __u16 ndm_flags,
1011 struct vxlan_fdb *f, u32 nhid,
1013 struct netlink_ext_ack *extack)
1015 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1016 struct vxlan_rdst *rd = NULL;
1017 struct vxlan_rdst oldrd;
1022 if (nhid && !rcu_access_pointer(f->nh)) {
1023 NL_SET_ERR_MSG(extack,
1024 "Cannot replace an existing non nexthop fdb with a nexthop");
1028 if (nhid && (flags & NLM_F_APPEND)) {
1029 NL_SET_ERR_MSG(extack,
1030 "Cannot append to a nexthop fdb");
1034 /* Do not allow an externally learned entry to take over an entry added
1037 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1038 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1039 if (f->state != state) {
1041 f->updated = jiffies;
1044 if (f->flags != fdb_flags) {
1045 f->flags = fdb_flags;
1046 f->updated = jiffies;
1051 if ((flags & NLM_F_REPLACE)) {
1052 /* Only change unicasts */
1053 if (!(is_multicast_ether_addr(f->eth_addr) ||
1054 is_zero_ether_addr(f->eth_addr))) {
1056 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1060 rc = vxlan_fdb_replace(f, ip, port, vni,
1065 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
1069 if ((flags & NLM_F_APPEND) &&
1070 (is_multicast_ether_addr(f->eth_addr) ||
1071 is_zero_ether_addr(f->eth_addr))) {
1072 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1079 if (ndm_flags & NTF_USE)
1084 rd = first_remote_rtnl(f);
1086 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
1087 swdev_notify, extack);
1097 if ((flags & NLM_F_REPLACE) && rc)
1099 else if ((flags & NLM_F_APPEND) && rc) {
1100 list_del_rcu(&rd->list);
1101 call_rcu(&rd->rcu, vxlan_dst_free);
1106 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1107 const u8 *mac, union vxlan_addr *ip,
1108 __u16 state, __u16 flags,
1109 __be16 port, __be32 src_vni, __be32 vni,
1110 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1112 struct netlink_ext_ack *extack)
1114 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1115 struct vxlan_fdb *f;
1118 /* Disallow replace to add a multicast entry */
1119 if ((flags & NLM_F_REPLACE) &&
1120 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1123 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1124 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1125 vni, ifindex, fdb_flags, nhid, &f, extack);
1129 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1130 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1131 swdev_notify, extack);
1138 vxlan_fdb_destroy(vxlan, f, false, false);
1142 /* Add new entry to forwarding table -- assumes lock held */
1143 static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1144 const u8 *mac, union vxlan_addr *ip,
1145 __u16 state, __u16 flags,
1146 __be16 port, __be32 src_vni, __be32 vni,
1147 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1149 struct netlink_ext_ack *extack)
1151 struct vxlan_fdb *f;
1153 f = __vxlan_find_mac(vxlan, mac, src_vni);
1155 if (flags & NLM_F_EXCL) {
1156 netdev_dbg(vxlan->dev,
1157 "lost race to create %pM\n", mac);
1161 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1162 vni, ifindex, ndm_flags, f,
1163 nhid, swdev_notify, extack);
1165 if (!(flags & NLM_F_CREATE))
1168 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1169 port, src_vni, vni, ifindex,
1170 ndm_flags, nhid, swdev_notify,
1175 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1176 struct vxlan_rdst *rd, bool swdev_notify)
1178 list_del_rcu(&rd->list);
1179 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1180 call_rcu(&rd->rcu, vxlan_dst_free);
1183 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1184 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1185 __be32 *vni, u32 *ifindex, u32 *nhid)
1187 struct net *net = dev_net(vxlan->dev);
1191 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1195 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1197 if (remote->sa.sa_family == AF_INET) {
1198 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1199 ip->sa.sa_family = AF_INET;
1200 #if IS_ENABLED(CONFIG_IPV6)
1202 ip->sin6.sin6_addr = in6addr_any;
1203 ip->sa.sa_family = AF_INET6;
1209 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1211 *port = nla_get_be16(tb[NDA_PORT]);
1213 *port = vxlan->cfg.dst_port;
1217 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1219 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1221 *vni = vxlan->default_dst.remote_vni;
1224 if (tb[NDA_SRC_VNI]) {
1225 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1227 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1229 *src_vni = vxlan->default_dst.remote_vni;
1232 if (tb[NDA_IFINDEX]) {
1233 struct net_device *tdev;
1235 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1237 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1238 tdev = __dev_get_by_index(net, *ifindex);
1240 return -EADDRNOTAVAIL;
1246 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1253 /* Add static entry (via netlink) */
1254 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1255 struct net_device *dev,
1256 const unsigned char *addr, u16 vid, u16 flags,
1257 struct netlink_ext_ack *extack)
1259 struct vxlan_dev *vxlan = netdev_priv(dev);
1260 /* struct net *net = dev_net(vxlan->dev); */
1261 union vxlan_addr ip;
1263 __be32 src_vni, vni;
1268 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1269 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1274 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
1277 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1282 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1283 return -EAFNOSUPPORT;
1285 hash_index = fdb_head_index(vxlan, addr, src_vni);
1286 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1287 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1288 port, src_vni, vni, ifindex,
1289 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1290 nhid, true, extack);
1291 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1296 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1297 const unsigned char *addr, union vxlan_addr ip,
1298 __be16 port, __be32 src_vni, __be32 vni,
1299 u32 ifindex, bool swdev_notify)
1301 struct vxlan_rdst *rd = NULL;
1302 struct vxlan_fdb *f;
1305 f = vxlan_find_mac(vxlan, addr, src_vni);
1309 if (!vxlan_addr_any(&ip)) {
1310 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1315 /* remove a destination if it's not the only one on the list,
1316 * otherwise destroy the fdb entry
1318 if (rd && !list_is_singular(&f->remotes)) {
1319 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1323 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1329 /* Delete entry (via netlink) */
1330 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1331 struct net_device *dev,
1332 const unsigned char *addr, u16 vid)
1334 struct vxlan_dev *vxlan = netdev_priv(dev);
1335 union vxlan_addr ip;
1336 __be32 src_vni, vni;
1342 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1347 hash_index = fdb_head_index(vxlan, addr, src_vni);
1348 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1349 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1351 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1356 /* Dump forwarding table */
1357 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1358 struct net_device *dev,
1359 struct net_device *filter_dev, int *idx)
1361 struct vxlan_dev *vxlan = netdev_priv(dev);
1365 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1366 struct vxlan_fdb *f;
1368 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1369 struct vxlan_rdst *rd;
1371 if (rcu_access_pointer(f->nh)) {
1372 err = vxlan_fdb_info(skb, vxlan, f,
1373 NETLINK_CB(cb->skb).portid,
1382 list_for_each_entry_rcu(rd, &f->remotes, list) {
1383 if (*idx < cb->args[2])
1386 err = vxlan_fdb_info(skb, vxlan, f,
1387 NETLINK_CB(cb->skb).portid,
1402 static int vxlan_fdb_get(struct sk_buff *skb,
1403 struct nlattr *tb[],
1404 struct net_device *dev,
1405 const unsigned char *addr,
1406 u16 vid, u32 portid, u32 seq,
1407 struct netlink_ext_ack *extack)
1409 struct vxlan_dev *vxlan = netdev_priv(dev);
1410 struct vxlan_fdb *f;
1415 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1417 vni = vxlan->default_dst.remote_vni;
1421 f = __vxlan_find_mac(vxlan, addr, vni);
1423 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1428 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1429 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1435 /* Watch incoming packets to learn mapping between Ethernet address
1436 * and Tunnel endpoint.
1437 * Return true if packet is bogus and should be dropped.
1439 static bool vxlan_snoop(struct net_device *dev,
1440 union vxlan_addr *src_ip, const u8 *src_mac,
1441 u32 src_ifindex, __be32 vni)
1443 struct vxlan_dev *vxlan = netdev_priv(dev);
1444 struct vxlan_fdb *f;
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 if (src_ip->sa.sa_family == AF_INET6 &&
1449 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1450 ifindex = src_ifindex;
1453 f = vxlan_find_mac(vxlan, src_mac, vni);
1455 struct vxlan_rdst *rdst = first_remote_rcu(f);
1457 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1458 rdst->remote_ifindex == ifindex))
1461 /* Don't migrate static entries, drop packets */
1462 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1465 /* Don't override an fdb with nexthop with a learnt entry */
1466 if (rcu_access_pointer(f->nh))
1469 if (net_ratelimit())
1471 "%pM migrated from %pIS to %pIS\n",
1472 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1474 rdst->remote_ip = *src_ip;
1475 f->updated = jiffies;
1476 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1478 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1480 /* learned new entry */
1481 spin_lock(&vxlan->hash_lock[hash_index]);
1483 /* close off race between vxlan_flush and incoming packets */
1484 if (netif_running(dev))
1485 vxlan_fdb_update(vxlan, src_mac, src_ip,
1487 NLM_F_EXCL|NLM_F_CREATE,
1488 vxlan->cfg.dst_port,
1490 vxlan->default_dst.remote_vni,
1491 ifindex, NTF_SELF, 0, true, NULL);
1492 spin_unlock(&vxlan->hash_lock[hash_index]);
1498 /* See if multicast group is already in use by other ID */
1499 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1501 struct vxlan_dev *vxlan;
1502 struct vxlan_sock *sock4;
1503 #if IS_ENABLED(CONFIG_IPV6)
1504 struct vxlan_sock *sock6;
1506 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1508 sock4 = rtnl_dereference(dev->vn4_sock);
1510 /* The vxlan_sock is only used by dev, leaving group has
1511 * no effect on other vxlan devices.
1513 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1515 #if IS_ENABLED(CONFIG_IPV6)
1516 sock6 = rtnl_dereference(dev->vn6_sock);
1517 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1521 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1522 if (!netif_running(vxlan->dev) || vxlan == dev)
1525 if (family == AF_INET &&
1526 rtnl_dereference(vxlan->vn4_sock) != sock4)
1528 #if IS_ENABLED(CONFIG_IPV6)
1529 if (family == AF_INET6 &&
1530 rtnl_dereference(vxlan->vn6_sock) != sock6)
1534 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1535 &dev->default_dst.remote_ip))
1538 if (vxlan->default_dst.remote_ifindex !=
1539 dev->default_dst.remote_ifindex)
1548 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1550 struct vxlan_net *vn;
1554 if (!refcount_dec_and_test(&vs->refcnt))
1557 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1558 spin_lock(&vn->sock_lock);
1559 hlist_del_rcu(&vs->hlist);
1560 udp_tunnel_notify_del_rx_port(vs->sock,
1561 (vs->flags & VXLAN_F_GPE) ?
1562 UDP_TUNNEL_TYPE_VXLAN_GPE :
1563 UDP_TUNNEL_TYPE_VXLAN);
1564 spin_unlock(&vn->sock_lock);
1569 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1571 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1572 #if IS_ENABLED(CONFIG_IPV6)
1573 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1575 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1578 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1581 vxlan_vs_del_dev(vxlan);
1583 if (__vxlan_sock_release_prep(sock4)) {
1584 udp_tunnel_sock_release(sock4->sock);
1588 #if IS_ENABLED(CONFIG_IPV6)
1589 if (__vxlan_sock_release_prep(sock6)) {
1590 udp_tunnel_sock_release(sock6->sock);
1596 /* Update multicast group membership when first VNI on
1597 * multicast address is brought up
1599 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1602 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1603 int ifindex = vxlan->default_dst.remote_ifindex;
1606 if (ip->sa.sa_family == AF_INET) {
1607 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1608 struct ip_mreqn mreq = {
1609 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1610 .imr_ifindex = ifindex,
1613 sk = sock4->sock->sk;
1615 ret = ip_mc_join_group(sk, &mreq);
1617 #if IS_ENABLED(CONFIG_IPV6)
1619 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1621 sk = sock6->sock->sk;
1623 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1624 &ip->sin6.sin6_addr);
1632 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1633 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1636 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1637 int ifindex = vxlan->default_dst.remote_ifindex;
1640 if (ip->sa.sa_family == AF_INET) {
1641 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1642 struct ip_mreqn mreq = {
1643 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1644 .imr_ifindex = ifindex,
1647 sk = sock4->sock->sk;
1649 ret = ip_mc_leave_group(sk, &mreq);
1651 #if IS_ENABLED(CONFIG_IPV6)
1653 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1655 sk = sock6->sock->sk;
1657 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1658 &ip->sin6.sin6_addr);
1666 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1667 struct sk_buff *skb, u32 vxflags)
1669 size_t start, offset;
1671 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1674 start = vxlan_rco_start(unparsed->vx_vni);
1675 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1677 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1680 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1681 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1683 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1684 unparsed->vx_vni &= VXLAN_VNI_MASK;
1688 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1689 struct sk_buff *skb, u32 vxflags,
1690 struct vxlan_metadata *md)
1692 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1693 struct metadata_dst *tun_dst;
1695 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1698 md->gbp = ntohs(gbp->policy_id);
1700 tun_dst = (struct metadata_dst *)skb_dst(skb);
1702 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1703 tun_dst->u.tun_info.options_len = sizeof(*md);
1705 if (gbp->dont_learn)
1706 md->gbp |= VXLAN_GBP_DONT_LEARN;
1708 if (gbp->policy_applied)
1709 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1711 /* In flow-based mode, GBP is carried in dst_metadata */
1712 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1713 skb->mark = md->gbp;
1715 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1718 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1720 struct sk_buff *skb, u32 vxflags)
1722 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1724 /* Need to have Next Protocol set for interfaces in GPE mode. */
1725 if (!gpe->np_applied)
1727 /* "The initial version is 0. If a receiver does not support the
1728 * version indicated it MUST drop the packet.
1730 if (gpe->version != 0)
1732 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1733 * processing MUST occur." However, we don't implement OAM
1734 * processing, thus drop the packet.
1739 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1743 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1747 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1748 struct vxlan_sock *vs,
1749 struct sk_buff *skb, __be32 vni)
1751 union vxlan_addr saddr;
1752 u32 ifindex = skb->dev->ifindex;
1754 skb_reset_mac_header(skb);
1755 skb->protocol = eth_type_trans(skb, vxlan->dev);
1756 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1758 /* Ignore packet loops (and multicast echo) */
1759 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1762 /* Get address from the outer IP header */
1763 if (vxlan_get_sk_family(vs) == AF_INET) {
1764 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1765 saddr.sa.sa_family = AF_INET;
1766 #if IS_ENABLED(CONFIG_IPV6)
1768 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1769 saddr.sa.sa_family = AF_INET6;
1773 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1774 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1780 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1781 struct sk_buff *skb)
1785 if (vxlan_get_sk_family(vs) == AF_INET)
1786 err = IP_ECN_decapsulate(oiph, skb);
1787 #if IS_ENABLED(CONFIG_IPV6)
1789 err = IP6_ECN_decapsulate(oiph, skb);
1792 if (unlikely(err) && log_ecn_error) {
1793 if (vxlan_get_sk_family(vs) == AF_INET)
1794 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1795 &((struct iphdr *)oiph)->saddr,
1796 ((struct iphdr *)oiph)->tos);
1798 net_info_ratelimited("non-ECT from %pI6\n",
1799 &((struct ipv6hdr *)oiph)->saddr);
1804 /* Callback from net/ipv4/udp.c to receive packets */
1805 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1807 struct pcpu_sw_netstats *stats;
1808 struct vxlan_dev *vxlan;
1809 struct vxlan_sock *vs;
1810 struct vxlanhdr unparsed;
1811 struct vxlan_metadata _md;
1812 struct vxlan_metadata *md = &_md;
1813 __be16 protocol = htons(ETH_P_TEB);
1814 bool raw_proto = false;
1818 /* Need UDP and VXLAN header to be present */
1819 if (!pskb_may_pull(skb, VXLAN_HLEN))
1822 unparsed = *vxlan_hdr(skb);
1823 /* VNI flag always required to be set */
1824 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1825 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1826 ntohl(vxlan_hdr(skb)->vx_flags),
1827 ntohl(vxlan_hdr(skb)->vx_vni));
1828 /* Return non vxlan pkt */
1831 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1832 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1834 vs = rcu_dereference_sk_user_data(sk);
1838 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1840 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1844 /* For backwards compatibility, only allow reserved fields to be
1845 * used by VXLAN extensions if explicitly requested.
1847 if (vs->flags & VXLAN_F_GPE) {
1848 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1853 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1854 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1857 if (vxlan_collect_metadata(vs)) {
1858 struct metadata_dst *tun_dst;
1860 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1861 key32_to_tunnel_id(vni), sizeof(*md));
1866 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1868 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1870 memset(md, 0, sizeof(*md));
1873 if (vs->flags & VXLAN_F_REMCSUM_RX)
1874 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1876 if (vs->flags & VXLAN_F_GBP)
1877 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1878 /* Note that GBP and GPE can never be active together. This is
1879 * ensured in vxlan_dev_configure.
1882 if (unparsed.vx_flags || unparsed.vx_vni) {
1883 /* If there are any unprocessed flags remaining treat
1884 * this as a malformed packet. This behavior diverges from
1885 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1886 * in reserved fields are to be ignored. The approach here
1887 * maintains compatibility with previous stack code, and also
1888 * is more robust and provides a little more security in
1889 * adding extensions to VXLAN.
1895 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1898 skb_reset_mac_header(skb);
1899 skb->dev = vxlan->dev;
1900 skb->pkt_type = PACKET_HOST;
1903 oiph = skb_network_header(skb);
1904 skb_reset_network_header(skb);
1906 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1907 ++vxlan->dev->stats.rx_frame_errors;
1908 ++vxlan->dev->stats.rx_errors;
1914 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1916 atomic_long_inc(&vxlan->dev->rx_dropped);
1920 stats = this_cpu_ptr(vxlan->dev->tstats);
1921 u64_stats_update_begin(&stats->syncp);
1922 stats->rx_packets++;
1923 stats->rx_bytes += skb->len;
1924 u64_stats_update_end(&stats->syncp);
1926 gro_cells_receive(&vxlan->gro_cells, skb);
1933 /* Consume bad packet */
1938 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1939 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1941 struct vxlan_dev *vxlan;
1942 struct vxlan_sock *vs;
1943 struct vxlanhdr *hdr;
1946 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1949 hdr = vxlan_hdr(skb);
1951 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1954 vs = rcu_dereference_sk_user_data(sk);
1958 vni = vxlan_vni(hdr->vx_vni);
1959 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1966 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1968 struct vxlan_dev *vxlan = netdev_priv(dev);
1969 struct arphdr *parp;
1972 struct neighbour *n;
1974 if (dev->flags & IFF_NOARP)
1977 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1978 dev->stats.tx_dropped++;
1981 parp = arp_hdr(skb);
1983 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1984 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1985 parp->ar_pro != htons(ETH_P_IP) ||
1986 parp->ar_op != htons(ARPOP_REQUEST) ||
1987 parp->ar_hln != dev->addr_len ||
1990 arpptr = (u8 *)parp + sizeof(struct arphdr);
1992 arpptr += dev->addr_len; /* sha */
1993 memcpy(&sip, arpptr, sizeof(sip));
1994 arpptr += sizeof(sip);
1995 arpptr += dev->addr_len; /* tha */
1996 memcpy(&tip, arpptr, sizeof(tip));
1998 if (ipv4_is_loopback(tip) ||
1999 ipv4_is_multicast(tip))
2002 n = neigh_lookup(&arp_tbl, &tip, dev);
2005 struct vxlan_fdb *f;
2006 struct sk_buff *reply;
2008 if (!(n->nud_state & NUD_CONNECTED)) {
2013 f = vxlan_find_mac(vxlan, n->ha, vni);
2014 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2015 /* bridge-local neighbor */
2020 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2028 skb_reset_mac_header(reply);
2029 __skb_pull(reply, skb_network_offset(reply));
2030 reply->ip_summed = CHECKSUM_UNNECESSARY;
2031 reply->pkt_type = PACKET_HOST;
2033 if (netif_rx_ni(reply) == NET_RX_DROP)
2034 dev->stats.rx_dropped++;
2035 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2036 union vxlan_addr ipa = {
2037 .sin.sin_addr.s_addr = tip,
2038 .sin.sin_family = AF_INET,
2041 vxlan_ip_miss(dev, &ipa);
2045 return NETDEV_TX_OK;
2048 #if IS_ENABLED(CONFIG_IPV6)
2049 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2050 struct neighbour *n, bool isrouter)
2052 struct net_device *dev = request->dev;
2053 struct sk_buff *reply;
2054 struct nd_msg *ns, *na;
2055 struct ipv6hdr *pip6;
2057 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2061 if (dev == NULL || !pskb_may_pull(request, request->len))
2064 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2065 sizeof(*na) + na_olen + dev->needed_tailroom;
2066 reply = alloc_skb(len, GFP_ATOMIC);
2070 reply->protocol = htons(ETH_P_IPV6);
2072 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2073 skb_push(reply, sizeof(struct ethhdr));
2074 skb_reset_mac_header(reply);
2076 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
2078 daddr = eth_hdr(request)->h_source;
2079 ns_olen = request->len - skb_network_offset(request) -
2080 sizeof(struct ipv6hdr) - sizeof(*ns);
2081 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
2082 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2083 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2088 /* Ethernet header */
2089 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2090 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2091 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2092 reply->protocol = htons(ETH_P_IPV6);
2094 skb_pull(reply, sizeof(struct ethhdr));
2095 skb_reset_network_header(reply);
2096 skb_put(reply, sizeof(struct ipv6hdr));
2100 pip6 = ipv6_hdr(reply);
2101 memset(pip6, 0, sizeof(struct ipv6hdr));
2103 pip6->priority = ipv6_hdr(request)->priority;
2104 pip6->nexthdr = IPPROTO_ICMPV6;
2105 pip6->hop_limit = 255;
2106 pip6->daddr = ipv6_hdr(request)->saddr;
2107 pip6->saddr = *(struct in6_addr *)n->primary_key;
2109 skb_pull(reply, sizeof(struct ipv6hdr));
2110 skb_reset_transport_header(reply);
2112 /* Neighbor Advertisement */
2113 na = skb_put_zero(reply, sizeof(*na) + na_olen);
2114 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2115 na->icmph.icmp6_router = isrouter;
2116 na->icmph.icmp6_override = 1;
2117 na->icmph.icmp6_solicited = 1;
2118 na->target = ns->target;
2119 ether_addr_copy(&na->opt[2], n->ha);
2120 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2121 na->opt[1] = na_olen >> 3;
2123 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2124 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2125 csum_partial(na, sizeof(*na)+na_olen, 0));
2127 pip6->payload_len = htons(sizeof(*na)+na_olen);
2129 skb_push(reply, sizeof(struct ipv6hdr));
2131 reply->ip_summed = CHECKSUM_UNNECESSARY;
2136 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
2138 struct vxlan_dev *vxlan = netdev_priv(dev);
2139 const struct in6_addr *daddr;
2140 const struct ipv6hdr *iphdr;
2141 struct inet6_dev *in6_dev;
2142 struct neighbour *n;
2145 in6_dev = __in6_dev_get(dev);
2149 iphdr = ipv6_hdr(skb);
2150 daddr = &iphdr->daddr;
2151 msg = (struct nd_msg *)(iphdr + 1);
2153 if (ipv6_addr_loopback(daddr) ||
2154 ipv6_addr_is_multicast(&msg->target))
2157 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2160 struct vxlan_fdb *f;
2161 struct sk_buff *reply;
2163 if (!(n->nud_state & NUD_CONNECTED)) {
2168 f = vxlan_find_mac(vxlan, n->ha, vni);
2169 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2170 /* bridge-local neighbor */
2175 reply = vxlan_na_create(skb, n,
2176 !!(f ? f->flags & NTF_ROUTER : 0));
2183 if (netif_rx_ni(reply) == NET_RX_DROP)
2184 dev->stats.rx_dropped++;
2186 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2187 union vxlan_addr ipa = {
2188 .sin6.sin6_addr = msg->target,
2189 .sin6.sin6_family = AF_INET6,
2192 vxlan_ip_miss(dev, &ipa);
2197 return NETDEV_TX_OK;
2201 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2203 struct vxlan_dev *vxlan = netdev_priv(dev);
2204 struct neighbour *n;
2206 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2210 switch (ntohs(eth_hdr(skb)->h_proto)) {
2215 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2218 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2219 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2220 union vxlan_addr ipa = {
2221 .sin.sin_addr.s_addr = pip->daddr,
2222 .sin.sin_family = AF_INET,
2225 vxlan_ip_miss(dev, &ipa);
2231 #if IS_ENABLED(CONFIG_IPV6)
2234 struct ipv6hdr *pip6;
2236 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2238 pip6 = ipv6_hdr(skb);
2239 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2240 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2241 union vxlan_addr ipa = {
2242 .sin6.sin6_addr = pip6->daddr,
2243 .sin6.sin6_family = AF_INET6,
2246 vxlan_ip_miss(dev, &ipa);
2260 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2262 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2264 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2273 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2274 struct vxlan_metadata *md)
2276 struct vxlanhdr_gbp *gbp;
2281 gbp = (struct vxlanhdr_gbp *)vxh;
2282 vxh->vx_flags |= VXLAN_HF_GBP;
2284 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2285 gbp->dont_learn = 1;
2287 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2288 gbp->policy_applied = 1;
2290 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2293 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2296 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2298 gpe->np_applied = 1;
2299 gpe->next_protocol = tun_p_from_eth_p(protocol);
2300 if (!gpe->next_protocol)
2301 return -EPFNOSUPPORT;
2305 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2306 int iphdr_len, __be32 vni,
2307 struct vxlan_metadata *md, u32 vxflags,
2310 struct vxlanhdr *vxh;
2313 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2314 __be16 inner_protocol = htons(ETH_P_TEB);
2316 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2317 skb->ip_summed == CHECKSUM_PARTIAL) {
2318 int csum_start = skb_checksum_start_offset(skb);
2320 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2321 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2322 (skb->csum_offset == offsetof(struct udphdr, check) ||
2323 skb->csum_offset == offsetof(struct tcphdr, check)))
2324 type |= SKB_GSO_TUNNEL_REMCSUM;
2327 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2328 + VXLAN_HLEN + iphdr_len;
2330 /* Need space for new headers (invalidates iph ptr) */
2331 err = skb_cow_head(skb, min_headroom);
2335 err = iptunnel_handle_offloads(skb, type);
2339 vxh = __skb_push(skb, sizeof(*vxh));
2340 vxh->vx_flags = VXLAN_HF_VNI;
2341 vxh->vx_vni = vxlan_vni_field(vni);
2343 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2346 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2347 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2348 vxh->vx_flags |= VXLAN_HF_RCO;
2350 if (!skb_is_gso(skb)) {
2351 skb->ip_summed = CHECKSUM_NONE;
2352 skb->encapsulation = 0;
2356 if (vxflags & VXLAN_F_GBP)
2357 vxlan_build_gbp_hdr(vxh, vxflags, md);
2358 if (vxflags & VXLAN_F_GPE) {
2359 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2362 inner_protocol = skb->protocol;
2365 skb_set_inner_protocol(skb, inner_protocol);
2369 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2370 struct vxlan_sock *sock4,
2371 struct sk_buff *skb, int oif, u8 tos,
2372 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2373 struct dst_cache *dst_cache,
2374 const struct ip_tunnel_info *info)
2376 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2377 struct rtable *rt = NULL;
2381 return ERR_PTR(-EIO);
2386 rt = dst_cache_get_ip4(dst_cache, saddr);
2391 memset(&fl4, 0, sizeof(fl4));
2392 fl4.flowi4_oif = oif;
2393 fl4.flowi4_tos = RT_TOS(tos);
2394 fl4.flowi4_mark = skb->mark;
2395 fl4.flowi4_proto = IPPROTO_UDP;
2398 fl4.fl4_dport = dport;
2399 fl4.fl4_sport = sport;
2401 rt = ip_route_output_key(vxlan->net, &fl4);
2403 if (rt->dst.dev == dev) {
2404 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2406 return ERR_PTR(-ELOOP);
2411 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2413 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2414 return ERR_PTR(-ENETUNREACH);
2419 #if IS_ENABLED(CONFIG_IPV6)
2420 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2421 struct net_device *dev,
2422 struct vxlan_sock *sock6,
2423 struct sk_buff *skb, int oif, u8 tos,
2425 const struct in6_addr *daddr,
2426 struct in6_addr *saddr,
2427 __be16 dport, __be16 sport,
2428 struct dst_cache *dst_cache,
2429 const struct ip_tunnel_info *info)
2431 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2432 struct dst_entry *ndst;
2436 return ERR_PTR(-EIO);
2441 ndst = dst_cache_get_ip6(dst_cache, saddr);
2446 memset(&fl6, 0, sizeof(fl6));
2447 fl6.flowi6_oif = oif;
2450 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2451 fl6.flowi6_mark = skb->mark;
2452 fl6.flowi6_proto = IPPROTO_UDP;
2453 fl6.fl6_dport = dport;
2454 fl6.fl6_sport = sport;
2456 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2458 if (unlikely(IS_ERR(ndst))) {
2459 netdev_dbg(dev, "no route to %pI6\n", daddr);
2460 return ERR_PTR(-ENETUNREACH);
2463 if (unlikely(ndst->dev == dev)) {
2464 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2466 return ERR_PTR(-ELOOP);
2471 dst_cache_set_ip6(dst_cache, ndst, saddr);
2476 /* Bypass encapsulation if the destination is local */
2477 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2478 struct vxlan_dev *dst_vxlan, __be32 vni)
2480 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2481 union vxlan_addr loopback;
2482 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2483 struct net_device *dev;
2486 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2487 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2488 skb->pkt_type = PACKET_HOST;
2489 skb->encapsulation = 0;
2490 skb->dev = dst_vxlan->dev;
2491 __skb_pull(skb, skb_network_offset(skb));
2493 if (remote_ip->sa.sa_family == AF_INET) {
2494 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2495 loopback.sa.sa_family = AF_INET;
2496 #if IS_ENABLED(CONFIG_IPV6)
2498 loopback.sin6.sin6_addr = in6addr_loopback;
2499 loopback.sa.sa_family = AF_INET6;
2505 if (unlikely(!(dev->flags & IFF_UP))) {
2510 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
2511 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2513 u64_stats_update_begin(&tx_stats->syncp);
2514 tx_stats->tx_packets++;
2515 tx_stats->tx_bytes += len;
2516 u64_stats_update_end(&tx_stats->syncp);
2518 if (netif_rx(skb) == NET_RX_SUCCESS) {
2519 u64_stats_update_begin(&rx_stats->syncp);
2520 rx_stats->rx_packets++;
2521 rx_stats->rx_bytes += len;
2522 u64_stats_update_end(&rx_stats->syncp);
2525 dev->stats.rx_dropped++;
2530 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2531 struct vxlan_dev *vxlan,
2532 union vxlan_addr *daddr,
2533 __be16 dst_port, int dst_ifindex, __be32 vni,
2534 struct dst_entry *dst,
2537 #if IS_ENABLED(CONFIG_IPV6)
2538 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2539 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2540 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2542 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2544 /* Bypass encapsulation if the destination is local */
2545 if (rt_flags & RTCF_LOCAL &&
2546 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2547 struct vxlan_dev *dst_vxlan;
2550 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2551 daddr->sa.sa_family, dst_port,
2554 dev->stats.tx_errors++;
2559 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2566 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2567 __be32 default_vni, struct vxlan_rdst *rdst,
2570 struct dst_cache *dst_cache;
2571 struct ip_tunnel_info *info;
2572 struct vxlan_dev *vxlan = netdev_priv(dev);
2573 const struct iphdr *old_iph = ip_hdr(skb);
2574 union vxlan_addr *dst;
2575 union vxlan_addr remote_ip, local_ip;
2576 struct vxlan_metadata _md;
2577 struct vxlan_metadata *md = &_md;
2578 __be16 src_port = 0, dst_port;
2579 struct dst_entry *ndst = NULL;
2584 u32 flags = vxlan->cfg.flags;
2585 bool udp_sum = false;
2586 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2588 info = skb_tunnel_info(skb);
2591 dst = &rdst->remote_ip;
2592 if (vxlan_addr_any(dst)) {
2594 /* short-circuited back to local bridge */
2595 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2601 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2602 vni = (rdst->remote_vni) ? : default_vni;
2603 ifindex = rdst->remote_ifindex;
2604 local_ip = vxlan->cfg.saddr;
2605 dst_cache = &rdst->dst_cache;
2606 md->gbp = skb->mark;
2607 if (flags & VXLAN_F_TTL_INHERIT) {
2608 ttl = ip_tunnel_get_ttl(old_iph, skb);
2610 ttl = vxlan->cfg.ttl;
2611 if (!ttl && vxlan_addr_multicast(dst))
2615 tos = vxlan->cfg.tos;
2617 tos = ip_tunnel_get_dsfield(old_iph, skb);
2619 if (dst->sa.sa_family == AF_INET)
2620 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2622 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2623 label = vxlan->cfg.label;
2626 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2630 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2631 if (remote_ip.sa.sa_family == AF_INET) {
2632 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2633 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2635 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2636 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2639 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2640 vni = tunnel_id_to_key32(info->key.tun_id);
2642 dst_cache = &info->dst_cache;
2643 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2644 if (info->options_len < sizeof(*md))
2646 md = ip_tunnel_info_opts(info);
2648 ttl = info->key.ttl;
2649 tos = info->key.tos;
2650 label = info->key.label;
2651 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2653 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2654 vxlan->cfg.port_max, true);
2657 if (dst->sa.sa_family == AF_INET) {
2658 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2663 ifindex = sock4->sock->sk->sk_bound_dev_if;
2665 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2666 dst->sin.sin_addr.s_addr,
2667 &local_ip.sin.sin_addr.s_addr,
2676 /* Bypass encapsulation if the destination is local */
2677 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2678 dst_port, ifindex, vni,
2679 &rt->dst, rt->rt_flags);
2683 if (vxlan->cfg.df == VXLAN_DF_SET) {
2685 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2686 struct ethhdr *eth = eth_hdr(skb);
2688 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2689 (ntohs(eth->h_proto) == ETH_P_IP &&
2690 old_iph->frag_off & htons(IP_DF)))
2693 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2698 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
2700 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
2701 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2702 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2703 vni, md, flags, udp_sum);
2707 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2708 dst->sin.sin_addr.s_addr, tos, ttl, df,
2709 src_port, dst_port, xnet, !udp_sum);
2710 #if IS_ENABLED(CONFIG_IPV6)
2712 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2715 ifindex = sock6->sock->sk->sk_bound_dev_if;
2717 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2718 label, &dst->sin6.sin6_addr,
2719 &local_ip.sin6.sin6_addr,
2723 err = PTR_ERR(ndst);
2729 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2731 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2732 dst_port, ifindex, vni,
2738 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
2740 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
2741 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2742 skb_scrub_packet(skb, xnet);
2743 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2744 vni, md, flags, udp_sum);
2748 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2749 &local_ip.sin6.sin6_addr,
2750 &dst->sin6.sin6_addr, tos, ttl,
2751 label, src_port, dst_port, !udp_sum);
2759 dev->stats.tx_dropped++;
2766 dev->stats.collisions++;
2767 else if (err == -ENETUNREACH)
2768 dev->stats.tx_carrier_errors++;
2770 dev->stats.tx_errors++;
2774 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2775 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2777 struct vxlan_rdst nh_rdst;
2782 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2783 hash = skb_get_hash(skb);
2786 nh = rcu_dereference(f->nh);
2791 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2794 if (likely(do_xmit))
2795 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2802 dev->stats.tx_dropped++;
2806 /* Transmit local packets over Vxlan
2808 * Outer IP header inherits ECN and DF from inner header.
2809 * Outer UDP destination is the VXLAN assigned port.
2810 * source port is based on hash of flow
2812 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2814 struct vxlan_dev *vxlan = netdev_priv(dev);
2815 struct vxlan_rdst *rdst, *fdst = NULL;
2816 const struct ip_tunnel_info *info;
2817 bool did_rsc = false;
2818 struct vxlan_fdb *f;
2822 info = skb_tunnel_info(skb);
2824 skb_reset_mac_header(skb);
2826 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2827 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2828 info->mode & IP_TUNNEL_INFO_TX) {
2829 vni = tunnel_id_to_key32(info->key.tun_id);
2831 if (info && info->mode & IP_TUNNEL_INFO_TX)
2832 vxlan_xmit_one(skb, dev, vni, NULL, false);
2835 return NETDEV_TX_OK;
2839 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2841 if (ntohs(eth->h_proto) == ETH_P_ARP)
2842 return arp_reduce(dev, skb, vni);
2843 #if IS_ENABLED(CONFIG_IPV6)
2844 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2845 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2846 sizeof(struct nd_msg)) &&
2847 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2848 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2850 if (m->icmph.icmp6_code == 0 &&
2851 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2852 return neigh_reduce(dev, skb, vni);
2858 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2861 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2862 (ntohs(eth->h_proto) == ETH_P_IP ||
2863 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2864 did_rsc = route_shortcircuit(dev, skb);
2866 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2870 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2872 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2873 !is_multicast_ether_addr(eth->h_dest))
2874 vxlan_fdb_miss(vxlan, eth->h_dest);
2876 dev->stats.tx_dropped++;
2878 return NETDEV_TX_OK;
2882 if (rcu_access_pointer(f->nh)) {
2883 vxlan_xmit_nh(skb, dev, f,
2884 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2886 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2887 struct sk_buff *skb1;
2893 skb1 = skb_clone(skb, GFP_ATOMIC);
2895 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2898 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2903 return NETDEV_TX_OK;
2906 /* Walk the forwarding table and purge stale entries */
2907 static void vxlan_cleanup(struct timer_list *t)
2909 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2910 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2913 if (!netif_running(vxlan->dev))
2916 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2917 struct hlist_node *p, *n;
2919 spin_lock(&vxlan->hash_lock[h]);
2920 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2922 = container_of(p, struct vxlan_fdb, hlist);
2923 unsigned long timeout;
2925 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2928 if (f->flags & NTF_EXT_LEARNED)
2931 timeout = f->used + vxlan->cfg.age_interval * HZ;
2932 if (time_before_eq(timeout, jiffies)) {
2933 netdev_dbg(vxlan->dev,
2934 "garbage collect %pM\n",
2936 f->state = NUD_STALE;
2937 vxlan_fdb_destroy(vxlan, f, true, true);
2938 } else if (time_before(timeout, next_timer))
2939 next_timer = timeout;
2941 spin_unlock(&vxlan->hash_lock[h]);
2944 mod_timer(&vxlan->age_timer, next_timer);
2947 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2949 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2951 spin_lock(&vn->sock_lock);
2952 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2953 #if IS_ENABLED(CONFIG_IPV6)
2954 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2956 spin_unlock(&vn->sock_lock);
2959 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2960 struct vxlan_dev_node *node)
2962 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2963 __be32 vni = vxlan->default_dst.remote_vni;
2965 node->vxlan = vxlan;
2966 spin_lock(&vn->sock_lock);
2967 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2968 spin_unlock(&vn->sock_lock);
2971 /* Setup stats when device is created */
2972 static int vxlan_init(struct net_device *dev)
2974 struct vxlan_dev *vxlan = netdev_priv(dev);
2977 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2981 err = gro_cells_init(&vxlan->gro_cells, dev);
2983 free_percpu(dev->tstats);
2990 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2992 struct vxlan_fdb *f;
2993 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
2995 spin_lock_bh(&vxlan->hash_lock[hash_index]);
2996 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2998 vxlan_fdb_destroy(vxlan, f, true, true);
2999 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
3002 static void vxlan_uninit(struct net_device *dev)
3004 struct vxlan_dev *vxlan = netdev_priv(dev);
3006 gro_cells_destroy(&vxlan->gro_cells);
3008 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
3010 free_percpu(dev->tstats);
3013 /* Start ageing timer and join group when device is brought up */
3014 static int vxlan_open(struct net_device *dev)
3016 struct vxlan_dev *vxlan = netdev_priv(dev);
3019 ret = vxlan_sock_add(vxlan);
3023 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
3024 ret = vxlan_igmp_join(vxlan);
3025 if (ret == -EADDRINUSE)
3028 vxlan_sock_release(vxlan);
3033 if (vxlan->cfg.age_interval)
3034 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3039 /* Purge the forwarding table */
3040 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
3044 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3045 struct hlist_node *p, *n;
3047 spin_lock_bh(&vxlan->hash_lock[h]);
3048 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3050 = container_of(p, struct vxlan_fdb, hlist);
3051 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3053 /* the all_zeros_mac entry is deleted at vxlan_uninit */
3054 if (!is_zero_ether_addr(f->eth_addr))
3055 vxlan_fdb_destroy(vxlan, f, true, true);
3057 spin_unlock_bh(&vxlan->hash_lock[h]);
3061 /* Cleanup timer and forwarding table on shutdown */
3062 static int vxlan_stop(struct net_device *dev)
3064 struct vxlan_dev *vxlan = netdev_priv(dev);
3065 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3068 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
3069 !vxlan_group_used(vn, vxlan))
3070 ret = vxlan_igmp_leave(vxlan);
3072 del_timer_sync(&vxlan->age_timer);
3074 vxlan_flush(vxlan, false);
3075 vxlan_sock_release(vxlan);
3080 /* Stub, nothing needs to be done. */
3081 static void vxlan_set_multicast_list(struct net_device *dev)
3085 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3087 struct vxlan_dev *vxlan = netdev_priv(dev);
3088 struct vxlan_rdst *dst = &vxlan->default_dst;
3089 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3090 dst->remote_ifindex);
3091 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
3093 /* This check is different than dev->max_mtu, because it looks at
3094 * the lowerdev->mtu, rather than the static dev->max_mtu
3097 int max_mtu = lowerdev->mtu -
3098 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3099 if (new_mtu > max_mtu)
3107 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3109 struct vxlan_dev *vxlan = netdev_priv(dev);
3110 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3111 __be16 sport, dport;
3113 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3114 vxlan->cfg.port_max, true);
3115 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3117 if (ip_tunnel_info_af(info) == AF_INET) {
3118 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3121 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
3122 info->key.u.ipv4.dst,
3123 &info->key.u.ipv4.src, dport, sport,
3124 &info->dst_cache, info);
3129 #if IS_ENABLED(CONFIG_IPV6)
3130 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3131 struct dst_entry *ndst;
3133 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
3134 info->key.label, &info->key.u.ipv6.dst,
3135 &info->key.u.ipv6.src, dport, sport,
3136 &info->dst_cache, info);
3138 return PTR_ERR(ndst);
3140 #else /* !CONFIG_IPV6 */
3141 return -EPFNOSUPPORT;
3144 info->key.tp_src = sport;
3145 info->key.tp_dst = dport;
3149 static const struct net_device_ops vxlan_netdev_ether_ops = {
3150 .ndo_init = vxlan_init,
3151 .ndo_uninit = vxlan_uninit,
3152 .ndo_open = vxlan_open,
3153 .ndo_stop = vxlan_stop,
3154 .ndo_start_xmit = vxlan_xmit,
3155 .ndo_get_stats64 = ip_tunnel_get_stats64,
3156 .ndo_set_rx_mode = vxlan_set_multicast_list,
3157 .ndo_change_mtu = vxlan_change_mtu,
3158 .ndo_validate_addr = eth_validate_addr,
3159 .ndo_set_mac_address = eth_mac_addr,
3160 .ndo_fdb_add = vxlan_fdb_add,
3161 .ndo_fdb_del = vxlan_fdb_delete,
3162 .ndo_fdb_dump = vxlan_fdb_dump,
3163 .ndo_fdb_get = vxlan_fdb_get,
3164 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3165 .ndo_change_proto_down = dev_change_proto_down_generic,
3168 static const struct net_device_ops vxlan_netdev_raw_ops = {
3169 .ndo_init = vxlan_init,
3170 .ndo_uninit = vxlan_uninit,
3171 .ndo_open = vxlan_open,
3172 .ndo_stop = vxlan_stop,
3173 .ndo_start_xmit = vxlan_xmit,
3174 .ndo_get_stats64 = ip_tunnel_get_stats64,
3175 .ndo_change_mtu = vxlan_change_mtu,
3176 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3179 /* Info for udev, that this is a virtual tunnel endpoint */
3180 static struct device_type vxlan_type = {
3184 /* Calls the ndo_udp_tunnel_add of the caller in order to
3185 * supply the listening VXLAN udp ports. Callers are expected
3186 * to implement the ndo_udp_tunnel_add.
3188 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3190 struct vxlan_sock *vs;
3191 struct net *net = dev_net(dev);
3192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3195 spin_lock(&vn->sock_lock);
3196 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3197 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3198 unsigned short type;
3200 if (vs->flags & VXLAN_F_GPE)
3201 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3203 type = UDP_TUNNEL_TYPE_VXLAN;
3206 udp_tunnel_push_rx_port(dev, vs->sock, type);
3208 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3211 spin_unlock(&vn->sock_lock);
3214 /* Initialize the device structure. */
3215 static void vxlan_setup(struct net_device *dev)
3217 struct vxlan_dev *vxlan = netdev_priv(dev);
3220 eth_hw_addr_random(dev);
3223 dev->needs_free_netdev = true;
3224 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3226 dev->features |= NETIF_F_LLTX;
3227 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3228 dev->features |= NETIF_F_RXCSUM;
3229 dev->features |= NETIF_F_GSO_SOFTWARE;
3231 dev->vlan_features = dev->features;
3232 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3233 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3234 netif_keep_dst(dev);
3235 dev->priv_flags |= IFF_NO_QUEUE;
3237 /* MTU range: 68 - 65535 */
3238 dev->min_mtu = ETH_MIN_MTU;
3239 dev->max_mtu = ETH_MAX_MTU;
3241 INIT_LIST_HEAD(&vxlan->next);
3243 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3247 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3248 spin_lock_init(&vxlan->hash_lock[h]);
3249 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3253 static void vxlan_ether_setup(struct net_device *dev)
3255 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3256 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3257 dev->netdev_ops = &vxlan_netdev_ether_ops;
3260 static void vxlan_raw_setup(struct net_device *dev)
3262 dev->header_ops = NULL;
3263 dev->type = ARPHRD_NONE;
3264 dev->hard_header_len = 0;
3266 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3267 dev->netdev_ops = &vxlan_netdev_raw_ops;
3270 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3271 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3272 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
3273 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3274 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3275 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
3276 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3277 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3278 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3279 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3280 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3281 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3282 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3283 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3284 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3285 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3286 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3287 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3288 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3289 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3290 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3291 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3292 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3293 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3294 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3295 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3296 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3297 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3298 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3299 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3302 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3303 struct netlink_ext_ack *extack)
3305 if (tb[IFLA_ADDRESS]) {
3306 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3307 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3308 "Provided link layer address is not Ethernet");
3312 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3313 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3314 "Provided Ethernet address is not unicast");
3315 return -EADDRNOTAVAIL;
3320 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3322 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3323 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3324 "MTU must be between 68 and 65535");
3330 NL_SET_ERR_MSG(extack,
3331 "Required attributes not provided to perform the operation");
3335 if (data[IFLA_VXLAN_ID]) {
3336 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3338 if (id >= VXLAN_N_VID) {
3339 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3340 "VXLAN ID must be lower than 16777216");
3345 if (data[IFLA_VXLAN_PORT_RANGE]) {
3346 const struct ifla_vxlan_port_range *p
3347 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3349 if (ntohs(p->high) < ntohs(p->low)) {
3350 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3351 "Invalid source port range");
3356 if (data[IFLA_VXLAN_DF]) {
3357 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3359 if (df < 0 || df > VXLAN_DF_MAX) {
3360 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3361 "Invalid DF attribute");
3369 static void vxlan_get_drvinfo(struct net_device *netdev,
3370 struct ethtool_drvinfo *drvinfo)
3372 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3373 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3376 static int vxlan_get_link_ksettings(struct net_device *dev,
3377 struct ethtool_link_ksettings *cmd)
3379 struct vxlan_dev *vxlan = netdev_priv(dev);
3380 struct vxlan_rdst *dst = &vxlan->default_dst;
3381 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3382 dst->remote_ifindex);
3385 cmd->base.duplex = DUPLEX_UNKNOWN;
3386 cmd->base.port = PORT_OTHER;
3387 cmd->base.speed = SPEED_UNKNOWN;
3392 return __ethtool_get_link_ksettings(lowerdev, cmd);
3395 static const struct ethtool_ops vxlan_ethtool_ops = {
3396 .get_drvinfo = vxlan_get_drvinfo,
3397 .get_link = ethtool_op_get_link,
3398 .get_link_ksettings = vxlan_get_link_ksettings,
3401 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3402 __be16 port, u32 flags, int ifindex)
3404 struct socket *sock;
3405 struct udp_port_cfg udp_conf;
3408 memset(&udp_conf, 0, sizeof(udp_conf));
3411 udp_conf.family = AF_INET6;
3412 udp_conf.use_udp6_rx_checksums =
3413 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3414 udp_conf.ipv6_v6only = 1;
3416 udp_conf.family = AF_INET;
3419 udp_conf.local_udp_port = port;
3420 udp_conf.bind_ifindex = ifindex;
3422 /* Open UDP socket */
3423 err = udp_sock_create(net, &udp_conf, &sock);
3425 return ERR_PTR(err);
3430 /* Create new listen socket if needed */
3431 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3432 __be16 port, u32 flags,
3435 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3436 struct vxlan_sock *vs;
3437 struct socket *sock;
3439 struct udp_tunnel_sock_cfg tunnel_cfg;
3441 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3443 return ERR_PTR(-ENOMEM);
3445 for (h = 0; h < VNI_HASH_SIZE; ++h)
3446 INIT_HLIST_HEAD(&vs->vni_list[h]);
3448 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3451 return ERR_CAST(sock);
3455 refcount_set(&vs->refcnt, 1);
3456 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3458 spin_lock(&vn->sock_lock);
3459 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3460 udp_tunnel_notify_add_rx_port(sock,
3461 (vs->flags & VXLAN_F_GPE) ?
3462 UDP_TUNNEL_TYPE_VXLAN_GPE :
3463 UDP_TUNNEL_TYPE_VXLAN);
3464 spin_unlock(&vn->sock_lock);
3466 /* Mark socket as an encapsulation socket. */
3467 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3468 tunnel_cfg.sk_user_data = vs;
3469 tunnel_cfg.encap_type = 1;
3470 tunnel_cfg.encap_rcv = vxlan_rcv;
3471 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3472 tunnel_cfg.encap_destroy = NULL;
3473 tunnel_cfg.gro_receive = vxlan_gro_receive;
3474 tunnel_cfg.gro_complete = vxlan_gro_complete;
3476 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3481 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3483 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3484 struct vxlan_sock *vs = NULL;
3485 struct vxlan_dev_node *node;
3486 int l3mdev_index = 0;
3488 if (vxlan->cfg.remote_ifindex)
3489 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3490 vxlan->net, vxlan->cfg.remote_ifindex);
3492 if (!vxlan->cfg.no_share) {
3493 spin_lock(&vn->sock_lock);
3494 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3495 vxlan->cfg.dst_port, vxlan->cfg.flags,
3497 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3498 spin_unlock(&vn->sock_lock);
3501 spin_unlock(&vn->sock_lock);
3504 vs = vxlan_socket_create(vxlan->net, ipv6,
3505 vxlan->cfg.dst_port, vxlan->cfg.flags,
3509 #if IS_ENABLED(CONFIG_IPV6)
3511 rcu_assign_pointer(vxlan->vn6_sock, vs);
3512 node = &vxlan->hlist6;
3516 rcu_assign_pointer(vxlan->vn4_sock, vs);
3517 node = &vxlan->hlist4;
3519 vxlan_vs_add_dev(vs, vxlan, node);
3523 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3525 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3526 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3527 bool ipv4 = !ipv6 || metadata;
3530 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3531 #if IS_ENABLED(CONFIG_IPV6)
3532 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3534 ret = __vxlan_sock_add(vxlan, true);
3535 if (ret < 0 && ret != -EAFNOSUPPORT)
3540 ret = __vxlan_sock_add(vxlan, false);
3542 vxlan_sock_release(vxlan);
3546 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3547 struct net_device **lower,
3548 struct vxlan_dev *old,
3549 struct netlink_ext_ack *extack)
3551 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3552 struct vxlan_dev *tmp;
3553 bool use_ipv6 = false;
3555 if (conf->flags & VXLAN_F_GPE) {
3556 /* For now, allow GPE only together with
3557 * COLLECT_METADATA. This can be relaxed later; in such
3558 * case, the other side of the PtP link will have to be
3561 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3562 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3563 NL_SET_ERR_MSG(extack,
3564 "VXLAN GPE does not support this combination of attributes");
3569 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3570 /* Unless IPv6 is explicitly requested, assume IPv4 */
3571 conf->remote_ip.sa.sa_family = AF_INET;
3572 conf->saddr.sa.sa_family = AF_INET;
3573 } else if (!conf->remote_ip.sa.sa_family) {
3574 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3575 } else if (!conf->saddr.sa.sa_family) {
3576 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3579 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3580 NL_SET_ERR_MSG(extack,
3581 "Local and remote address must be from the same family");
3585 if (vxlan_addr_multicast(&conf->saddr)) {
3586 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3590 if (conf->saddr.sa.sa_family == AF_INET6) {
3591 if (!IS_ENABLED(CONFIG_IPV6)) {
3592 NL_SET_ERR_MSG(extack,
3593 "IPv6 support not enabled in the kernel");
3594 return -EPFNOSUPPORT;
3597 conf->flags |= VXLAN_F_IPV6;
3599 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3601 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3603 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3605 if (local_type & IPV6_ADDR_LINKLOCAL) {
3606 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3607 (remote_type != IPV6_ADDR_ANY)) {
3608 NL_SET_ERR_MSG(extack,
3609 "Invalid combination of local and remote address scopes");
3613 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3616 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3617 NL_SET_ERR_MSG(extack,
3618 "Invalid combination of local and remote address scopes");
3622 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3627 if (conf->label && !use_ipv6) {
3628 NL_SET_ERR_MSG(extack,
3629 "Label attribute only applies to IPv6 VXLAN devices");
3633 if (conf->remote_ifindex) {
3634 struct net_device *lowerdev;
3636 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3638 NL_SET_ERR_MSG(extack,
3639 "Invalid local interface, device not found");
3643 #if IS_ENABLED(CONFIG_IPV6)
3645 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3646 if (idev && idev->cnf.disable_ipv6) {
3647 NL_SET_ERR_MSG(extack,
3648 "IPv6 support disabled by administrator");
3656 if (vxlan_addr_multicast(&conf->remote_ip)) {
3657 NL_SET_ERR_MSG(extack,
3658 "Local interface required for multicast remote destination");
3663 #if IS_ENABLED(CONFIG_IPV6)
3664 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3665 NL_SET_ERR_MSG(extack,
3666 "Local interface required for link-local local/remote addresses");
3674 if (!conf->dst_port) {
3675 if (conf->flags & VXLAN_F_GPE)
3676 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3678 conf->dst_port = htons(vxlan_port);
3681 if (!conf->age_interval)
3682 conf->age_interval = FDB_AGE_DEFAULT;
3684 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3688 if (tmp->cfg.vni != conf->vni)
3690 if (tmp->cfg.dst_port != conf->dst_port)
3692 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3693 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3696 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3697 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3700 NL_SET_ERR_MSG(extack,
3701 "A VXLAN device with the specified VNI already exists");
3708 static void vxlan_config_apply(struct net_device *dev,
3709 struct vxlan_config *conf,
3710 struct net_device *lowerdev,
3711 struct net *src_net,
3714 struct vxlan_dev *vxlan = netdev_priv(dev);
3715 struct vxlan_rdst *dst = &vxlan->default_dst;
3716 unsigned short needed_headroom = ETH_HLEN;
3717 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3718 int max_mtu = ETH_MAX_MTU;
3721 if (conf->flags & VXLAN_F_GPE)
3722 vxlan_raw_setup(dev);
3724 vxlan_ether_setup(dev);
3727 dev->mtu = conf->mtu;
3729 vxlan->net = src_net;
3732 dst->remote_vni = conf->vni;
3734 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3737 dst->remote_ifindex = conf->remote_ifindex;
3739 dev->gso_max_size = lowerdev->gso_max_size;
3740 dev->gso_max_segs = lowerdev->gso_max_segs;
3742 needed_headroom = lowerdev->hard_header_len;
3744 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3746 if (max_mtu < ETH_MIN_MTU)
3747 max_mtu = ETH_MIN_MTU;
3749 if (!changelink && !conf->mtu)
3753 if (dev->mtu > max_mtu)
3756 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3757 needed_headroom += VXLAN6_HEADROOM;
3759 needed_headroom += VXLAN_HEADROOM;
3760 dev->needed_headroom = needed_headroom;
3762 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3765 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3766 struct vxlan_config *conf, bool changelink,
3767 struct netlink_ext_ack *extack)
3769 struct vxlan_dev *vxlan = netdev_priv(dev);
3770 struct net_device *lowerdev;
3773 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3777 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3782 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3783 struct vxlan_config *conf,
3784 struct netlink_ext_ack *extack)
3786 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3787 struct vxlan_dev *vxlan = netdev_priv(dev);
3788 struct net_device *remote_dev = NULL;
3789 struct vxlan_fdb *f = NULL;
3790 bool unregister = false;
3791 struct vxlan_rdst *dst;
3794 dst = &vxlan->default_dst;
3795 err = vxlan_dev_configure(net, dev, conf, false, extack);
3799 dev->ethtool_ops = &vxlan_ethtool_ops;
3801 /* create an fdb entry for a valid default destination */
3802 if (!vxlan_addr_any(&dst->remote_ip)) {
3803 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3805 NUD_REACHABLE | NUD_PERMANENT,
3806 vxlan->cfg.dst_port,
3809 dst->remote_ifindex,
3810 NTF_SELF, 0, &f, extack);
3815 err = register_netdevice(dev);
3820 if (dst->remote_ifindex) {
3821 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3825 err = netdev_upper_dev_link(remote_dev, dev, extack);
3830 err = rtnl_configure_link(dev, NULL);
3835 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3837 /* notify default fdb entry */
3838 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3839 RTM_NEWNEIGH, true, extack);
3841 vxlan_fdb_destroy(vxlan, f, false, false);
3843 netdev_upper_dev_unlink(remote_dev, dev);
3848 list_add(&vxlan->next, &vn->vxlan_list);
3850 dst->remote_dev = remote_dev;
3854 netdev_upper_dev_unlink(remote_dev, dev);
3856 /* unregister_netdevice() destroys the default FDB entry with deletion
3857 * notification. But the addition notification was not sent yet, so
3858 * destroy the entry by hand here.
3861 __vxlan_fdb_free(f);
3864 unregister_netdevice(dev);
3868 /* Set/clear flags based on attribute */
3869 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3870 int attrtype, unsigned long mask, bool changelink,
3871 bool changelink_supported,
3872 struct netlink_ext_ack *extack)
3874 unsigned long flags;
3879 if (changelink && !changelink_supported) {
3880 vxlan_flag_attr_error(attrtype, extack);
3884 if (vxlan_policy[attrtype].type == NLA_FLAG)
3885 flags = conf->flags | mask;
3886 else if (nla_get_u8(tb[attrtype]))
3887 flags = conf->flags | mask;
3889 flags = conf->flags & ~mask;
3891 conf->flags = flags;
3896 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3897 struct net_device *dev, struct vxlan_config *conf,
3898 bool changelink, struct netlink_ext_ack *extack)
3900 struct vxlan_dev *vxlan = netdev_priv(dev);
3903 memset(conf, 0, sizeof(*conf));
3905 /* if changelink operation, start with old existing cfg */
3907 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3909 if (data[IFLA_VXLAN_ID]) {
3910 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3912 if (changelink && (vni != conf->vni)) {
3913 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3916 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3919 if (data[IFLA_VXLAN_GROUP]) {
3920 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3921 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3925 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3926 conf->remote_ip.sa.sa_family = AF_INET;
3927 } else if (data[IFLA_VXLAN_GROUP6]) {
3928 if (!IS_ENABLED(CONFIG_IPV6)) {
3929 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3930 return -EPFNOSUPPORT;
3933 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3934 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3938 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3939 conf->remote_ip.sa.sa_family = AF_INET6;
3942 if (data[IFLA_VXLAN_LOCAL]) {
3943 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3944 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
3948 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3949 conf->saddr.sa.sa_family = AF_INET;
3950 } else if (data[IFLA_VXLAN_LOCAL6]) {
3951 if (!IS_ENABLED(CONFIG_IPV6)) {
3952 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
3953 return -EPFNOSUPPORT;
3956 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3957 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
3961 /* TODO: respect scope id */
3962 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3963 conf->saddr.sa.sa_family = AF_INET6;
3966 if (data[IFLA_VXLAN_LINK])
3967 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3969 if (data[IFLA_VXLAN_TOS])
3970 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3972 if (data[IFLA_VXLAN_TTL])
3973 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3975 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3976 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3977 VXLAN_F_TTL_INHERIT, changelink, false,
3984 if (data[IFLA_VXLAN_LABEL])
3985 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3986 IPV6_FLOWLABEL_MASK;
3988 if (data[IFLA_VXLAN_LEARNING]) {
3989 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3990 VXLAN_F_LEARN, changelink, true,
3994 } else if (!changelink) {
3995 /* default to learn on a new device */
3996 conf->flags |= VXLAN_F_LEARN;
3999 if (data[IFLA_VXLAN_AGEING])
4000 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
4002 if (data[IFLA_VXLAN_PROXY]) {
4003 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4004 VXLAN_F_PROXY, changelink, false,
4010 if (data[IFLA_VXLAN_RSC]) {
4011 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4012 VXLAN_F_RSC, changelink, false,
4018 if (data[IFLA_VXLAN_L2MISS]) {
4019 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4020 VXLAN_F_L2MISS, changelink, false,
4026 if (data[IFLA_VXLAN_L3MISS]) {
4027 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4028 VXLAN_F_L3MISS, changelink, false,
4034 if (data[IFLA_VXLAN_LIMIT]) {
4036 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4037 "Cannot change limit");
4040 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4043 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
4044 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4045 VXLAN_F_COLLECT_METADATA, changelink, false,
4051 if (data[IFLA_VXLAN_PORT_RANGE]) {
4053 const struct ifla_vxlan_port_range *p
4054 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4055 conf->port_min = ntohs(p->low);
4056 conf->port_max = ntohs(p->high);
4058 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4059 "Cannot change port range");
4064 if (data[IFLA_VXLAN_PORT]) {
4066 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4067 "Cannot change port");
4070 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4073 if (data[IFLA_VXLAN_UDP_CSUM]) {
4075 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4076 "Cannot change UDP_CSUM flag");
4079 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4080 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4083 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4084 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4085 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4091 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4092 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4093 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4099 if (data[IFLA_VXLAN_REMCSUM_TX]) {
4100 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4101 VXLAN_F_REMCSUM_TX, changelink, false,
4107 if (data[IFLA_VXLAN_REMCSUM_RX]) {
4108 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4109 VXLAN_F_REMCSUM_RX, changelink, false,
4115 if (data[IFLA_VXLAN_GBP]) {
4116 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4117 VXLAN_F_GBP, changelink, false, extack);
4122 if (data[IFLA_VXLAN_GPE]) {
4123 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4124 VXLAN_F_GPE, changelink, false,
4130 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4131 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4132 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4140 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4141 "Cannot change mtu");
4144 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4147 if (data[IFLA_VXLAN_DF])
4148 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4153 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
4154 struct nlattr *tb[], struct nlattr *data[],
4155 struct netlink_ext_ack *extack)
4157 struct vxlan_config conf;
4160 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4164 return __vxlan_dev_create(src_net, dev, &conf, extack);
4167 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4168 struct nlattr *data[],
4169 struct netlink_ext_ack *extack)
4171 struct vxlan_dev *vxlan = netdev_priv(dev);
4172 struct net_device *lowerdev;
4173 struct vxlan_config conf;
4174 struct vxlan_rdst *dst;
4177 dst = &vxlan->default_dst;
4178 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4182 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4187 if (dst->remote_dev == lowerdev)
4190 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4195 /* handle default dst entry */
4196 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4197 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4199 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4200 if (!vxlan_addr_any(&conf.remote_ip)) {
4201 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4203 NUD_REACHABLE | NUD_PERMANENT,
4204 NLM_F_APPEND | NLM_F_CREATE,
4205 vxlan->cfg.dst_port,
4207 conf.remote_ifindex,
4208 NTF_SELF, 0, true, extack);
4210 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4211 netdev_adjacent_change_abort(dst->remote_dev,
4216 if (!vxlan_addr_any(&dst->remote_ip))
4217 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4219 vxlan->cfg.dst_port,
4222 dst->remote_ifindex,
4224 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4227 if (conf.age_interval != vxlan->cfg.age_interval)
4228 mod_timer(&vxlan->age_timer, jiffies);
4230 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4231 if (lowerdev && lowerdev != dst->remote_dev) {
4232 dst->remote_dev = lowerdev;
4233 netdev_update_lockdep_key(lowerdev);
4235 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4239 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4241 struct vxlan_dev *vxlan = netdev_priv(dev);
4243 vxlan_flush(vxlan, true);
4245 list_del(&vxlan->next);
4246 unregister_netdevice_queue(dev, head);
4247 if (vxlan->default_dst.remote_dev)
4248 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4251 static size_t vxlan_get_size(const struct net_device *dev)
4254 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4255 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4256 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4257 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4258 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4259 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4260 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4261 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4262 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4263 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4264 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4265 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4266 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4267 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4268 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4269 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4270 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4271 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4272 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4273 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4274 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4275 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4276 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4277 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4281 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4283 const struct vxlan_dev *vxlan = netdev_priv(dev);
4284 const struct vxlan_rdst *dst = &vxlan->default_dst;
4285 struct ifla_vxlan_port_range ports = {
4286 .low = htons(vxlan->cfg.port_min),
4287 .high = htons(vxlan->cfg.port_max),
4290 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4291 goto nla_put_failure;
4293 if (!vxlan_addr_any(&dst->remote_ip)) {
4294 if (dst->remote_ip.sa.sa_family == AF_INET) {
4295 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4296 dst->remote_ip.sin.sin_addr.s_addr))
4297 goto nla_put_failure;
4298 #if IS_ENABLED(CONFIG_IPV6)
4300 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4301 &dst->remote_ip.sin6.sin6_addr))
4302 goto nla_put_failure;
4307 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4308 goto nla_put_failure;
4310 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4311 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4312 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4313 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4314 goto nla_put_failure;
4315 #if IS_ENABLED(CONFIG_IPV6)
4317 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4318 &vxlan->cfg.saddr.sin6.sin6_addr))
4319 goto nla_put_failure;
4324 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4325 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4326 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4327 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4328 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4329 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4330 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4331 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4332 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4333 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4334 nla_put_u8(skb, IFLA_VXLAN_RSC,
4335 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4336 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4337 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4338 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4339 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4340 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4341 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4342 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4343 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4344 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4345 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4346 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4347 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4348 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4349 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4350 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4351 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4352 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4353 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4354 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4355 goto nla_put_failure;
4357 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4358 goto nla_put_failure;
4360 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4361 nla_put_flag(skb, IFLA_VXLAN_GBP))
4362 goto nla_put_failure;
4364 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4365 nla_put_flag(skb, IFLA_VXLAN_GPE))
4366 goto nla_put_failure;
4368 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4369 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4370 goto nla_put_failure;
4378 static struct net *vxlan_get_link_net(const struct net_device *dev)
4380 struct vxlan_dev *vxlan = netdev_priv(dev);
4385 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4387 .maxtype = IFLA_VXLAN_MAX,
4388 .policy = vxlan_policy,
4389 .priv_size = sizeof(struct vxlan_dev),
4390 .setup = vxlan_setup,
4391 .validate = vxlan_validate,
4392 .newlink = vxlan_newlink,
4393 .changelink = vxlan_changelink,
4394 .dellink = vxlan_dellink,
4395 .get_size = vxlan_get_size,
4396 .fill_info = vxlan_fill_info,
4397 .get_link_net = vxlan_get_link_net,
4400 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4401 u8 name_assign_type,
4402 struct vxlan_config *conf)
4404 struct nlattr *tb[IFLA_MAX + 1];
4405 struct net_device *dev;
4408 memset(&tb, 0, sizeof(tb));
4410 dev = rtnl_create_link(net, name, name_assign_type,
4411 &vxlan_link_ops, tb, NULL);
4415 err = __vxlan_dev_create(net, dev, conf, NULL);
4418 return ERR_PTR(err);
4421 err = rtnl_configure_link(dev, NULL);
4423 LIST_HEAD(list_kill);
4425 vxlan_dellink(dev, &list_kill);
4426 unregister_netdevice_many(&list_kill);
4427 return ERR_PTR(err);
4432 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4434 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4435 struct net_device *dev)
4437 struct vxlan_dev *vxlan, *next;
4438 LIST_HEAD(list_kill);
4440 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4441 struct vxlan_rdst *dst = &vxlan->default_dst;
4443 /* In case we created vxlan device with carrier
4444 * and we loose the carrier due to module unload
4445 * we also need to remove vxlan device. In other
4446 * cases, it's not necessary and remote_ifindex
4447 * is 0 here, so no matches.
4449 if (dst->remote_ifindex == dev->ifindex)
4450 vxlan_dellink(vxlan->dev, &list_kill);
4453 unregister_netdevice_many(&list_kill);
4456 static int vxlan_netdevice_event(struct notifier_block *unused,
4457 unsigned long event, void *ptr)
4459 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4460 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4462 if (event == NETDEV_UNREGISTER) {
4463 vxlan_offload_rx_ports(dev, false);
4464 vxlan_handle_lowerdev_unregister(vn, dev);
4465 } else if (event == NETDEV_REGISTER) {
4466 vxlan_offload_rx_ports(dev, true);
4467 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4468 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4469 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4475 static struct notifier_block vxlan_notifier_block __read_mostly = {
4476 .notifier_call = vxlan_netdevice_event,
4480 vxlan_fdb_offloaded_set(struct net_device *dev,
4481 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4483 struct vxlan_dev *vxlan = netdev_priv(dev);
4484 struct vxlan_rdst *rdst;
4485 struct vxlan_fdb *f;
4488 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4490 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4492 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4496 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4497 fdb_info->remote_port,
4498 fdb_info->remote_vni,
4499 fdb_info->remote_ifindex);
4503 rdst->offloaded = fdb_info->offloaded;
4506 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4510 vxlan_fdb_external_learn_add(struct net_device *dev,
4511 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4513 struct vxlan_dev *vxlan = netdev_priv(dev);
4514 struct netlink_ext_ack *extack;
4518 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4519 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4521 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4522 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4524 NLM_F_CREATE | NLM_F_REPLACE,
4525 fdb_info->remote_port,
4527 fdb_info->remote_vni,
4528 fdb_info->remote_ifindex,
4529 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4531 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4537 vxlan_fdb_external_learn_del(struct net_device *dev,
4538 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4540 struct vxlan_dev *vxlan = netdev_priv(dev);
4541 struct vxlan_fdb *f;
4545 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4546 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4548 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4551 else if (f->flags & NTF_EXT_LEARNED)
4552 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4553 fdb_info->remote_ip,
4554 fdb_info->remote_port,
4556 fdb_info->remote_vni,
4557 fdb_info->remote_ifindex,
4560 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4565 static int vxlan_switchdev_event(struct notifier_block *unused,
4566 unsigned long event, void *ptr)
4568 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4569 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4573 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4574 vxlan_fdb_offloaded_set(dev, ptr);
4576 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4578 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4580 err = notifier_from_errno(err);
4583 fdb_info->offloaded = true;
4584 vxlan_fdb_offloaded_set(dev, fdb_info);
4586 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4588 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4590 err = notifier_from_errno(err);
4593 fdb_info->offloaded = false;
4594 vxlan_fdb_offloaded_set(dev, fdb_info);
4601 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4602 .notifier_call = vxlan_switchdev_event,
4605 static int vxlan_nexthop_event(struct notifier_block *nb,
4606 unsigned long event, void *ptr)
4608 struct nexthop *nh = ptr;
4609 struct vxlan_fdb *fdb, *tmp;
4611 if (!nh || event != NEXTHOP_EVENT_DEL)
4614 list_for_each_entry_safe(fdb, tmp, &nh->fdb_list, nh_list)
4615 vxlan_fdb_destroy(fdb->vdev, fdb, false, false);
4620 static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = {
4621 .notifier_call = vxlan_nexthop_event,
4624 static __net_init int vxlan_init_net(struct net *net)
4626 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4629 INIT_LIST_HEAD(&vn->vxlan_list);
4630 spin_lock_init(&vn->sock_lock);
4632 for (h = 0; h < PORT_HASH_SIZE; ++h)
4633 INIT_HLIST_HEAD(&vn->sock_list[h]);
4635 return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4638 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4640 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4641 struct vxlan_dev *vxlan, *next;
4642 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);
4657 for (h = 0; h < PORT_HASH_SIZE; ++h)
4658 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4661 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4667 list_for_each_entry(net, net_list, exit_list)
4668 unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4669 list_for_each_entry(net, net_list, exit_list)
4670 vxlan_destroy_tunnels(net, &list);
4672 unregister_netdevice_many(&list);
4676 static struct pernet_operations vxlan_net_ops = {
4677 .init = vxlan_init_net,
4678 .exit_batch = vxlan_exit_batch_net,
4679 .id = &vxlan_net_id,
4680 .size = sizeof(struct vxlan_net),
4683 static int __init vxlan_init_module(void)
4687 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4689 rc = register_pernet_subsys(&vxlan_net_ops);
4693 rc = register_netdevice_notifier(&vxlan_notifier_block);
4697 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4701 rc = rtnl_link_register(&vxlan_link_ops);
4707 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4709 unregister_netdevice_notifier(&vxlan_notifier_block);
4711 unregister_pernet_subsys(&vxlan_net_ops);
4715 late_initcall(vxlan_init_module);
4717 static void __exit vxlan_cleanup_module(void)
4719 rtnl_link_unregister(&vxlan_link_ops);
4720 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4721 unregister_netdevice_notifier(&vxlan_notifier_block);
4722 unregister_pernet_subsys(&vxlan_net_ops);
4723 /* rcu_barrier() is called by netns */
4725 module_exit(vxlan_cleanup_module);
4727 MODULE_LICENSE("GPL");
4728 MODULE_VERSION(VXLAN_VERSION);
4729 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4730 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4731 MODULE_ALIAS_RTNL_LINK("vxlan");