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 __rcu *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, UDP port,
194 * enabled unshareable flags and socket device binding (see l3mdev with
197 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
198 __be16 port, u32 flags, int ifindex)
200 struct vxlan_sock *vs;
202 flags &= VXLAN_F_RCV_FLAGS;
204 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
205 if (inet_sk(vs->sock->sk)->inet_sport == port &&
206 vxlan_get_sk_family(vs) == family &&
207 vs->flags == flags &&
208 vs->sock->sk->sk_bound_dev_if == ifindex)
214 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
217 struct vxlan_dev_node *node;
219 /* For flow based devices, map all packets to VNI 0 */
220 if (vs->flags & VXLAN_F_COLLECT_METADATA)
223 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
224 if (node->vxlan->default_dst.remote_vni != vni)
227 if (IS_ENABLED(CONFIG_IPV6)) {
228 const struct vxlan_config *cfg = &node->vxlan->cfg;
230 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
231 cfg->remote_ifindex != ifindex)
241 /* Look up VNI in a per net namespace table */
242 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
243 __be32 vni, sa_family_t family,
244 __be16 port, u32 flags)
246 struct vxlan_sock *vs;
248 vs = vxlan_find_sock(net, family, port, flags, ifindex);
252 return vxlan_vs_find_vni(vs, ifindex, vni);
255 /* Fill in neighbour message in skbuff. */
256 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
257 const struct vxlan_fdb *fdb,
258 u32 portid, u32 seq, int type, unsigned int flags,
259 const struct vxlan_rdst *rdst)
261 unsigned long now = jiffies;
262 struct nda_cacheinfo ci;
263 bool send_ip, send_eth;
264 struct nlmsghdr *nlh;
270 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
274 ndm = nlmsg_data(nlh);
275 memset(ndm, 0, sizeof(*ndm));
277 send_eth = send_ip = true;
280 nh = rcu_dereference(fdb->nh);
282 nh_family = nexthop_get_family(nh);
287 if (type == RTM_GETNEIGH) {
289 send_ip = !vxlan_addr_any(&rdst->remote_ip);
290 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
292 ndm->ndm_family = nh_family;
294 send_eth = !is_zero_ether_addr(fdb->eth_addr);
296 ndm->ndm_family = AF_BRIDGE;
297 ndm->ndm_state = fdb->state;
298 ndm->ndm_ifindex = vxlan->dev->ifindex;
299 ndm->ndm_flags = fdb->flags;
300 if (rdst && rdst->offloaded)
301 ndm->ndm_flags |= NTF_OFFLOADED;
302 ndm->ndm_type = RTN_UNICAST;
304 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
305 nla_put_s32(skb, NDA_LINK_NETNSID,
306 peernet2id(dev_net(vxlan->dev), vxlan->net)))
307 goto nla_put_failure;
309 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
310 goto nla_put_failure;
312 if (nla_put_u32(skb, NDA_NH_ID, nh_id))
313 goto nla_put_failure;
315 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
317 goto nla_put_failure;
319 if (rdst->remote_port &&
320 rdst->remote_port != vxlan->cfg.dst_port &&
321 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
322 goto nla_put_failure;
323 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
324 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
325 goto nla_put_failure;
326 if (rdst->remote_ifindex &&
327 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
328 goto nla_put_failure;
331 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
332 nla_put_u32(skb, NDA_SRC_VNI,
333 be32_to_cpu(fdb->vni)))
334 goto nla_put_failure;
336 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
337 ci.ndm_confirmed = 0;
338 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
341 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
342 goto nla_put_failure;
348 nlmsg_cancel(skb, nlh);
352 static inline size_t vxlan_nlmsg_size(void)
354 return NLMSG_ALIGN(sizeof(struct ndmsg))
355 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
356 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
357 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
358 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
359 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
360 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
361 + nla_total_size(sizeof(struct nda_cacheinfo));
364 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
365 struct vxlan_rdst *rd, int type)
367 struct net *net = dev_net(vxlan->dev);
371 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
375 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
377 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
378 WARN_ON(err == -EMSGSIZE);
383 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
387 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
390 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
391 const struct vxlan_fdb *fdb,
392 const struct vxlan_rdst *rd,
393 struct netlink_ext_ack *extack,
394 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
396 fdb_info->info.dev = vxlan->dev;
397 fdb_info->info.extack = extack;
398 fdb_info->remote_ip = rd->remote_ip;
399 fdb_info->remote_port = rd->remote_port;
400 fdb_info->remote_vni = rd->remote_vni;
401 fdb_info->remote_ifindex = rd->remote_ifindex;
402 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
403 fdb_info->vni = fdb->vni;
404 fdb_info->offloaded = rd->offloaded;
405 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
408 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
409 struct vxlan_fdb *fdb,
410 struct vxlan_rdst *rd,
412 struct netlink_ext_ack *extack)
414 struct switchdev_notifier_vxlan_fdb_info info;
415 enum switchdev_notifier_type notifier_type;
421 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
422 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
423 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
424 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
426 return notifier_to_errno(ret);
429 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
430 struct vxlan_rdst *rd, int type, bool swdev_notify,
431 struct netlink_ext_ack *extack)
435 if (swdev_notify && rd) {
438 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
444 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
450 __vxlan_fdb_notify(vxlan, fdb, rd, type);
454 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
456 struct vxlan_dev *vxlan = netdev_priv(dev);
457 struct vxlan_fdb f = {
460 struct vxlan_rdst remote = {
461 .remote_ip = *ipa, /* goes to NDA_DST */
462 .remote_vni = cpu_to_be32(VXLAN_N_VID),
465 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
468 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
470 struct vxlan_fdb f = {
473 struct vxlan_rdst remote = { };
475 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
477 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
480 /* Hash Ethernet address */
481 static u32 eth_hash(const unsigned char *addr)
483 u64 value = get_unaligned((u64 *)addr);
485 /* only want 6 bytes */
491 return hash_64(value, FDB_HASH_BITS);
494 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
496 /* use 1 byte of OUI and 3 bytes of NIC */
497 u32 key = get_unaligned((u32 *)(addr + 2));
499 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
502 static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
504 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
505 return eth_vni_hash(mac, vni);
507 return eth_hash(mac);
510 /* Hash chain to use given mac address */
511 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
512 const u8 *mac, __be32 vni)
514 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
517 /* Look up Ethernet address in forwarding table */
518 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
519 const u8 *mac, __be32 vni)
521 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
524 hlist_for_each_entry_rcu(f, head, hlist) {
525 if (ether_addr_equal(mac, f->eth_addr)) {
526 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
538 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
539 const u8 *mac, __be32 vni)
543 f = __vxlan_find_mac(vxlan, mac, vni);
544 if (f && f->used != jiffies)
550 /* caller should hold vxlan->hash_lock */
551 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
552 union vxlan_addr *ip, __be16 port,
553 __be32 vni, __u32 ifindex)
555 struct vxlan_rdst *rd;
557 list_for_each_entry(rd, &f->remotes, list) {
558 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
559 rd->remote_port == port &&
560 rd->remote_vni == vni &&
561 rd->remote_ifindex == ifindex)
568 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
569 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
571 struct vxlan_dev *vxlan = netdev_priv(dev);
572 u8 eth_addr[ETH_ALEN + 2] = { 0 };
573 struct vxlan_rdst *rdst;
577 if (is_multicast_ether_addr(mac) ||
578 is_zero_ether_addr(mac))
581 ether_addr_copy(eth_addr, mac);
585 f = __vxlan_find_mac(vxlan, eth_addr, vni);
591 rdst = first_remote_rcu(f);
592 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
598 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
600 static int vxlan_fdb_notify_one(struct notifier_block *nb,
601 const struct vxlan_dev *vxlan,
602 const struct vxlan_fdb *f,
603 const struct vxlan_rdst *rdst,
604 struct netlink_ext_ack *extack)
606 struct switchdev_notifier_vxlan_fdb_info fdb_info;
609 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
610 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
612 return notifier_to_errno(rc);
615 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
616 struct notifier_block *nb,
617 struct netlink_ext_ack *extack)
619 struct vxlan_dev *vxlan;
620 struct vxlan_rdst *rdst;
625 if (!netif_is_vxlan(dev))
627 vxlan = netdev_priv(dev);
629 for (h = 0; h < FDB_HASH_SIZE; ++h) {
630 spin_lock_bh(&vxlan->hash_lock[h]);
631 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
633 list_for_each_entry(rdst, &f->remotes, list) {
634 rc = vxlan_fdb_notify_one(nb, vxlan,
642 spin_unlock_bh(&vxlan->hash_lock[h]);
647 spin_unlock_bh(&vxlan->hash_lock[h]);
650 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
652 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
654 struct vxlan_dev *vxlan;
655 struct vxlan_rdst *rdst;
659 if (!netif_is_vxlan(dev))
661 vxlan = netdev_priv(dev);
663 for (h = 0; h < FDB_HASH_SIZE; ++h) {
664 spin_lock_bh(&vxlan->hash_lock[h]);
665 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
667 list_for_each_entry(rdst, &f->remotes, list)
668 rdst->offloaded = false;
669 spin_unlock_bh(&vxlan->hash_lock[h]);
673 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
675 /* Replace destination of unicast mac */
676 static int vxlan_fdb_replace(struct vxlan_fdb *f,
677 union vxlan_addr *ip, __be16 port, __be32 vni,
678 __u32 ifindex, struct vxlan_rdst *oldrd)
680 struct vxlan_rdst *rd;
682 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
686 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
691 dst_cache_reset(&rd->dst_cache);
693 rd->remote_port = port;
694 rd->remote_vni = vni;
695 rd->remote_ifindex = ifindex;
696 rd->offloaded = false;
700 /* Add/update destinations for multicast */
701 static int vxlan_fdb_append(struct vxlan_fdb *f,
702 union vxlan_addr *ip, __be16 port, __be32 vni,
703 __u32 ifindex, struct vxlan_rdst **rdp)
705 struct vxlan_rdst *rd;
707 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
711 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
715 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
721 rd->remote_port = port;
722 rd->offloaded = false;
723 rd->remote_vni = vni;
724 rd->remote_ifindex = ifindex;
726 list_add_tail_rcu(&rd->list, &f->remotes);
732 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
734 struct vxlanhdr *vh, size_t hdrlen,
736 struct gro_remcsum *grc,
739 size_t start, offset;
741 if (skb->remcsum_offload)
744 if (!NAPI_GRO_CB(skb)->csum_valid)
747 start = vxlan_rco_start(vni_field);
748 offset = start + vxlan_rco_offset(vni_field);
750 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
751 start, offset, grc, nopartial);
753 skb->remcsum_offload = 1;
758 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
759 struct list_head *head,
762 struct sk_buff *pp = NULL;
764 struct vxlanhdr *vh, *vh2;
765 unsigned int hlen, off_vx;
767 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
769 struct gro_remcsum grc;
771 skb_gro_remcsum_init(&grc);
773 off_vx = skb_gro_offset(skb);
774 hlen = off_vx + sizeof(*vh);
775 vh = skb_gro_header_fast(skb, off_vx);
776 if (skb_gro_header_hard(skb, hlen)) {
777 vh = skb_gro_header_slow(skb, hlen, off_vx);
782 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
784 flags = vh->vx_flags;
786 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
787 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
790 VXLAN_F_REMCSUM_NOPARTIAL));
796 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
798 list_for_each_entry(p, head, list) {
799 if (!NAPI_GRO_CB(p)->same_flow)
802 vh2 = (struct vxlanhdr *)(p->data + off_vx);
803 if (vh->vx_flags != vh2->vx_flags ||
804 vh->vx_vni != vh2->vx_vni) {
805 NAPI_GRO_CB(p)->same_flow = 0;
810 pp = call_gro_receive(eth_gro_receive, head, skb);
814 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
819 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
821 /* Sets 'skb->inner_mac_header' since we are always called with
822 * 'skb->encapsulation' set.
824 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
827 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
828 __u16 state, __be32 src_vni,
833 f = kmalloc(sizeof(*f), GFP_ATOMIC);
837 f->flags = ndm_flags;
838 f->updated = f->used = jiffies;
841 RCU_INIT_POINTER(f->vdev, vxlan);
842 INIT_LIST_HEAD(&f->nh_list);
843 INIT_LIST_HEAD(&f->remotes);
844 memcpy(f->eth_addr, mac, ETH_ALEN);
849 static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
850 __be32 src_vni, struct vxlan_fdb *f)
853 hlist_add_head_rcu(&f->hlist,
854 vxlan_fdb_head(vxlan, mac, src_vni));
857 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
858 u32 nhid, struct netlink_ext_ack *extack)
860 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
864 if (old_nh && old_nh->id == nhid)
867 nh = nexthop_find_by_id(vxlan->net, nhid);
869 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
874 if (!nexthop_get(nh)) {
875 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
879 if (!nexthop_is_fdb(nh)) {
880 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
884 if (!nexthop_is_multipath(nh)) {
885 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
889 /* check nexthop group family */
890 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
892 if (!nexthop_has_v4(nh)) {
894 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
899 if (nexthop_has_v4(nh)) {
901 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
908 list_del_rcu(&fdb->nh_list);
911 rcu_assign_pointer(fdb->nh, nh);
912 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
921 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
922 const u8 *mac, union vxlan_addr *ip,
923 __u16 state, __be16 port, __be32 src_vni,
924 __be32 vni, __u32 ifindex, __u16 ndm_flags,
925 u32 nhid, struct vxlan_fdb **fdb,
926 struct netlink_ext_ack *extack)
928 struct vxlan_rdst *rd = NULL;
932 if (vxlan->cfg.addrmax &&
933 vxlan->addrcnt >= vxlan->cfg.addrmax)
936 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
937 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
942 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
944 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
957 static void __vxlan_fdb_free(struct vxlan_fdb *f)
959 struct vxlan_rdst *rd, *nd;
962 nh = rcu_dereference_raw(f->nh);
964 rcu_assign_pointer(f->nh, NULL);
965 rcu_assign_pointer(f->vdev, NULL);
969 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
970 dst_cache_destroy(&rd->dst_cache);
976 static void vxlan_fdb_free(struct rcu_head *head)
978 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
983 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
984 bool do_notify, bool swdev_notify)
986 struct vxlan_rdst *rd;
988 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
992 if (rcu_access_pointer(f->nh))
993 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
996 list_for_each_entry(rd, &f->remotes, list)
997 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
1001 hlist_del_rcu(&f->hlist);
1002 list_del_rcu(&f->nh_list);
1003 call_rcu(&f->rcu, vxlan_fdb_free);
1006 static void vxlan_dst_free(struct rcu_head *head)
1008 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
1010 dst_cache_destroy(&rd->dst_cache);
1014 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1015 union vxlan_addr *ip,
1016 __u16 state, __u16 flags,
1017 __be16 port, __be32 vni,
1018 __u32 ifindex, __u16 ndm_flags,
1019 struct vxlan_fdb *f, u32 nhid,
1021 struct netlink_ext_ack *extack)
1023 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1024 struct vxlan_rdst *rd = NULL;
1025 struct vxlan_rdst oldrd;
1030 if (nhid && !rcu_access_pointer(f->nh)) {
1031 NL_SET_ERR_MSG(extack,
1032 "Cannot replace an existing non nexthop fdb with a nexthop");
1036 if (nhid && (flags & NLM_F_APPEND)) {
1037 NL_SET_ERR_MSG(extack,
1038 "Cannot append to a nexthop fdb");
1042 /* Do not allow an externally learned entry to take over an entry added
1045 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1046 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1047 if (f->state != state) {
1049 f->updated = jiffies;
1052 if (f->flags != fdb_flags) {
1053 f->flags = fdb_flags;
1054 f->updated = jiffies;
1059 if ((flags & NLM_F_REPLACE)) {
1060 /* Only change unicasts */
1061 if (!(is_multicast_ether_addr(f->eth_addr) ||
1062 is_zero_ether_addr(f->eth_addr))) {
1064 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1068 rc = vxlan_fdb_replace(f, ip, port, vni,
1073 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
1077 if ((flags & NLM_F_APPEND) &&
1078 (is_multicast_ether_addr(f->eth_addr) ||
1079 is_zero_ether_addr(f->eth_addr))) {
1080 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1087 if (ndm_flags & NTF_USE)
1092 rd = first_remote_rtnl(f);
1094 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
1095 swdev_notify, extack);
1105 if ((flags & NLM_F_REPLACE) && rc)
1107 else if ((flags & NLM_F_APPEND) && rc) {
1108 list_del_rcu(&rd->list);
1109 call_rcu(&rd->rcu, vxlan_dst_free);
1114 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1115 const u8 *mac, union vxlan_addr *ip,
1116 __u16 state, __u16 flags,
1117 __be16 port, __be32 src_vni, __be32 vni,
1118 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1120 struct netlink_ext_ack *extack)
1122 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1123 struct vxlan_fdb *f;
1126 /* Disallow replace to add a multicast entry */
1127 if ((flags & NLM_F_REPLACE) &&
1128 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1131 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1132 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1133 vni, ifindex, fdb_flags, nhid, &f, extack);
1137 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1138 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1139 swdev_notify, extack);
1146 vxlan_fdb_destroy(vxlan, f, false, false);
1150 /* Add new entry to forwarding table -- assumes lock held */
1151 static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1152 const u8 *mac, union vxlan_addr *ip,
1153 __u16 state, __u16 flags,
1154 __be16 port, __be32 src_vni, __be32 vni,
1155 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1157 struct netlink_ext_ack *extack)
1159 struct vxlan_fdb *f;
1161 f = __vxlan_find_mac(vxlan, mac, src_vni);
1163 if (flags & NLM_F_EXCL) {
1164 netdev_dbg(vxlan->dev,
1165 "lost race to create %pM\n", mac);
1169 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1170 vni, ifindex, ndm_flags, f,
1171 nhid, swdev_notify, extack);
1173 if (!(flags & NLM_F_CREATE))
1176 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1177 port, src_vni, vni, ifindex,
1178 ndm_flags, nhid, swdev_notify,
1183 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1184 struct vxlan_rdst *rd, bool swdev_notify)
1186 list_del_rcu(&rd->list);
1187 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1188 call_rcu(&rd->rcu, vxlan_dst_free);
1191 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1192 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1193 __be32 *vni, u32 *ifindex, u32 *nhid)
1195 struct net *net = dev_net(vxlan->dev);
1198 if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] ||
1203 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1207 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1209 if (remote->sa.sa_family == AF_INET) {
1210 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1211 ip->sa.sa_family = AF_INET;
1212 #if IS_ENABLED(CONFIG_IPV6)
1214 ip->sin6.sin6_addr = in6addr_any;
1215 ip->sa.sa_family = AF_INET6;
1221 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1223 *port = nla_get_be16(tb[NDA_PORT]);
1225 *port = vxlan->cfg.dst_port;
1229 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1231 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1233 *vni = vxlan->default_dst.remote_vni;
1236 if (tb[NDA_SRC_VNI]) {
1237 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1239 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1241 *src_vni = vxlan->default_dst.remote_vni;
1244 if (tb[NDA_IFINDEX]) {
1245 struct net_device *tdev;
1247 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1249 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1250 tdev = __dev_get_by_index(net, *ifindex);
1252 return -EADDRNOTAVAIL;
1258 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1265 /* Add static entry (via netlink) */
1266 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1267 struct net_device *dev,
1268 const unsigned char *addr, u16 vid, u16 flags,
1269 struct netlink_ext_ack *extack)
1271 struct vxlan_dev *vxlan = netdev_priv(dev);
1272 /* struct net *net = dev_net(vxlan->dev); */
1273 union vxlan_addr ip;
1275 __be32 src_vni, vni;
1280 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1281 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1286 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
1289 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1294 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1295 return -EAFNOSUPPORT;
1297 hash_index = fdb_head_index(vxlan, addr, src_vni);
1298 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1299 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1300 port, src_vni, vni, ifindex,
1301 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1302 nhid, true, extack);
1303 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1308 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1309 const unsigned char *addr, union vxlan_addr ip,
1310 __be16 port, __be32 src_vni, __be32 vni,
1311 u32 ifindex, bool swdev_notify)
1313 struct vxlan_rdst *rd = NULL;
1314 struct vxlan_fdb *f;
1317 f = vxlan_find_mac(vxlan, addr, src_vni);
1321 if (!vxlan_addr_any(&ip)) {
1322 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1327 /* remove a destination if it's not the only one on the list,
1328 * otherwise destroy the fdb entry
1330 if (rd && !list_is_singular(&f->remotes)) {
1331 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1335 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1341 /* Delete entry (via netlink) */
1342 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1343 struct net_device *dev,
1344 const unsigned char *addr, u16 vid)
1346 struct vxlan_dev *vxlan = netdev_priv(dev);
1347 union vxlan_addr ip;
1348 __be32 src_vni, vni;
1354 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1359 hash_index = fdb_head_index(vxlan, addr, src_vni);
1360 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1361 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1363 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1368 /* Dump forwarding table */
1369 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1370 struct net_device *dev,
1371 struct net_device *filter_dev, int *idx)
1373 struct vxlan_dev *vxlan = netdev_priv(dev);
1377 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1378 struct vxlan_fdb *f;
1381 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1382 struct vxlan_rdst *rd;
1384 if (rcu_access_pointer(f->nh)) {
1385 if (*idx < cb->args[2])
1387 err = vxlan_fdb_info(skb, vxlan, f,
1388 NETLINK_CB(cb->skb).portid,
1401 list_for_each_entry_rcu(rd, &f->remotes, list) {
1402 if (*idx < cb->args[2])
1405 err = vxlan_fdb_info(skb, vxlan, f,
1406 NETLINK_CB(cb->skb).portid,
1424 static int vxlan_fdb_get(struct sk_buff *skb,
1425 struct nlattr *tb[],
1426 struct net_device *dev,
1427 const unsigned char *addr,
1428 u16 vid, u32 portid, u32 seq,
1429 struct netlink_ext_ack *extack)
1431 struct vxlan_dev *vxlan = netdev_priv(dev);
1432 struct vxlan_fdb *f;
1437 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1439 vni = vxlan->default_dst.remote_vni;
1443 f = __vxlan_find_mac(vxlan, addr, vni);
1445 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1450 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1451 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1457 /* Watch incoming packets to learn mapping between Ethernet address
1458 * and Tunnel endpoint.
1459 * Return true if packet is bogus and should be dropped.
1461 static bool vxlan_snoop(struct net_device *dev,
1462 union vxlan_addr *src_ip, const u8 *src_mac,
1463 u32 src_ifindex, __be32 vni)
1465 struct vxlan_dev *vxlan = netdev_priv(dev);
1466 struct vxlan_fdb *f;
1469 #if IS_ENABLED(CONFIG_IPV6)
1470 if (src_ip->sa.sa_family == AF_INET6 &&
1471 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1472 ifindex = src_ifindex;
1475 f = vxlan_find_mac(vxlan, src_mac, vni);
1477 struct vxlan_rdst *rdst = first_remote_rcu(f);
1479 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1480 rdst->remote_ifindex == ifindex))
1483 /* Don't migrate static entries, drop packets */
1484 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1487 /* Don't override an fdb with nexthop with a learnt entry */
1488 if (rcu_access_pointer(f->nh))
1491 if (net_ratelimit())
1493 "%pM migrated from %pIS to %pIS\n",
1494 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1496 rdst->remote_ip = *src_ip;
1497 f->updated = jiffies;
1498 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1500 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1502 /* learned new entry */
1503 spin_lock(&vxlan->hash_lock[hash_index]);
1505 /* close off race between vxlan_flush and incoming packets */
1506 if (netif_running(dev))
1507 vxlan_fdb_update(vxlan, src_mac, src_ip,
1509 NLM_F_EXCL|NLM_F_CREATE,
1510 vxlan->cfg.dst_port,
1512 vxlan->default_dst.remote_vni,
1513 ifindex, NTF_SELF, 0, true, NULL);
1514 spin_unlock(&vxlan->hash_lock[hash_index]);
1520 /* See if multicast group is already in use by other ID */
1521 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1523 struct vxlan_dev *vxlan;
1524 struct vxlan_sock *sock4;
1525 #if IS_ENABLED(CONFIG_IPV6)
1526 struct vxlan_sock *sock6;
1528 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1530 sock4 = rtnl_dereference(dev->vn4_sock);
1532 /* The vxlan_sock is only used by dev, leaving group has
1533 * no effect on other vxlan devices.
1535 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1537 #if IS_ENABLED(CONFIG_IPV6)
1538 sock6 = rtnl_dereference(dev->vn6_sock);
1539 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1543 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1544 if (!netif_running(vxlan->dev) || vxlan == dev)
1547 if (family == AF_INET &&
1548 rtnl_dereference(vxlan->vn4_sock) != sock4)
1550 #if IS_ENABLED(CONFIG_IPV6)
1551 if (family == AF_INET6 &&
1552 rtnl_dereference(vxlan->vn6_sock) != sock6)
1556 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1557 &dev->default_dst.remote_ip))
1560 if (vxlan->default_dst.remote_ifindex !=
1561 dev->default_dst.remote_ifindex)
1570 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1572 struct vxlan_net *vn;
1576 if (!refcount_dec_and_test(&vs->refcnt))
1579 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1580 spin_lock(&vn->sock_lock);
1581 hlist_del_rcu(&vs->hlist);
1582 udp_tunnel_notify_del_rx_port(vs->sock,
1583 (vs->flags & VXLAN_F_GPE) ?
1584 UDP_TUNNEL_TYPE_VXLAN_GPE :
1585 UDP_TUNNEL_TYPE_VXLAN);
1586 spin_unlock(&vn->sock_lock);
1591 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1593 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1594 #if IS_ENABLED(CONFIG_IPV6)
1595 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1597 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1600 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1603 vxlan_vs_del_dev(vxlan);
1605 if (__vxlan_sock_release_prep(sock4)) {
1606 udp_tunnel_sock_release(sock4->sock);
1610 #if IS_ENABLED(CONFIG_IPV6)
1611 if (__vxlan_sock_release_prep(sock6)) {
1612 udp_tunnel_sock_release(sock6->sock);
1618 /* Update multicast group membership when first VNI on
1619 * multicast address is brought up
1621 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1624 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1625 int ifindex = vxlan->default_dst.remote_ifindex;
1628 if (ip->sa.sa_family == AF_INET) {
1629 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1630 struct ip_mreqn mreq = {
1631 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1632 .imr_ifindex = ifindex,
1635 sk = sock4->sock->sk;
1637 ret = ip_mc_join_group(sk, &mreq);
1639 #if IS_ENABLED(CONFIG_IPV6)
1641 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1643 sk = sock6->sock->sk;
1645 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1646 &ip->sin6.sin6_addr);
1654 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1655 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1658 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1659 int ifindex = vxlan->default_dst.remote_ifindex;
1662 if (ip->sa.sa_family == AF_INET) {
1663 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1664 struct ip_mreqn mreq = {
1665 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1666 .imr_ifindex = ifindex,
1669 sk = sock4->sock->sk;
1671 ret = ip_mc_leave_group(sk, &mreq);
1673 #if IS_ENABLED(CONFIG_IPV6)
1675 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1677 sk = sock6->sock->sk;
1679 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1680 &ip->sin6.sin6_addr);
1688 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1689 struct sk_buff *skb, u32 vxflags)
1691 size_t start, offset;
1693 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1696 start = vxlan_rco_start(unparsed->vx_vni);
1697 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1699 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1702 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1703 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1705 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1706 unparsed->vx_vni &= VXLAN_VNI_MASK;
1710 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1711 struct sk_buff *skb, u32 vxflags,
1712 struct vxlan_metadata *md)
1714 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1715 struct metadata_dst *tun_dst;
1717 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1720 md->gbp = ntohs(gbp->policy_id);
1722 tun_dst = (struct metadata_dst *)skb_dst(skb);
1724 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1725 tun_dst->u.tun_info.options_len = sizeof(*md);
1727 if (gbp->dont_learn)
1728 md->gbp |= VXLAN_GBP_DONT_LEARN;
1730 if (gbp->policy_applied)
1731 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1733 /* In flow-based mode, GBP is carried in dst_metadata */
1734 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1735 skb->mark = md->gbp;
1737 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1740 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1742 struct sk_buff *skb, u32 vxflags)
1744 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1746 /* Need to have Next Protocol set for interfaces in GPE mode. */
1747 if (!gpe->np_applied)
1749 /* "The initial version is 0. If a receiver does not support the
1750 * version indicated it MUST drop the packet.
1752 if (gpe->version != 0)
1754 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1755 * processing MUST occur." However, we don't implement OAM
1756 * processing, thus drop the packet.
1761 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1765 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1769 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1770 struct vxlan_sock *vs,
1771 struct sk_buff *skb, __be32 vni)
1773 union vxlan_addr saddr;
1774 u32 ifindex = skb->dev->ifindex;
1776 skb_reset_mac_header(skb);
1777 skb->protocol = eth_type_trans(skb, vxlan->dev);
1778 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1780 /* Ignore packet loops (and multicast echo) */
1781 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1784 /* Get address from the outer IP header */
1785 if (vxlan_get_sk_family(vs) == AF_INET) {
1786 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1787 saddr.sa.sa_family = AF_INET;
1788 #if IS_ENABLED(CONFIG_IPV6)
1790 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1791 saddr.sa.sa_family = AF_INET6;
1795 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1796 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1802 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1803 struct sk_buff *skb)
1807 if (vxlan_get_sk_family(vs) == AF_INET)
1808 err = IP_ECN_decapsulate(oiph, skb);
1809 #if IS_ENABLED(CONFIG_IPV6)
1811 err = IP6_ECN_decapsulate(oiph, skb);
1814 if (unlikely(err) && log_ecn_error) {
1815 if (vxlan_get_sk_family(vs) == AF_INET)
1816 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1817 &((struct iphdr *)oiph)->saddr,
1818 ((struct iphdr *)oiph)->tos);
1820 net_info_ratelimited("non-ECT from %pI6\n",
1821 &((struct ipv6hdr *)oiph)->saddr);
1826 /* Callback from net/ipv4/udp.c to receive packets */
1827 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1829 struct vxlan_dev *vxlan;
1830 struct vxlan_sock *vs;
1831 struct vxlanhdr unparsed;
1832 struct vxlan_metadata _md;
1833 struct vxlan_metadata *md = &_md;
1834 __be16 protocol = htons(ETH_P_TEB);
1835 bool raw_proto = false;
1839 /* Need UDP and VXLAN header to be present */
1840 if (!pskb_may_pull(skb, VXLAN_HLEN))
1843 unparsed = *vxlan_hdr(skb);
1844 /* VNI flag always required to be set */
1845 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1846 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1847 ntohl(vxlan_hdr(skb)->vx_flags),
1848 ntohl(vxlan_hdr(skb)->vx_vni));
1849 /* Return non vxlan pkt */
1852 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1853 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1855 vs = rcu_dereference_sk_user_data(sk);
1859 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1861 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1865 /* For backwards compatibility, only allow reserved fields to be
1866 * used by VXLAN extensions if explicitly requested.
1868 if (vs->flags & VXLAN_F_GPE) {
1869 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1874 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1875 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1878 if (vs->flags & VXLAN_F_REMCSUM_RX)
1879 if (unlikely(!vxlan_remcsum(&unparsed, skb, vs->flags)))
1882 if (vxlan_collect_metadata(vs)) {
1883 struct metadata_dst *tun_dst;
1885 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1886 key32_to_tunnel_id(vni), sizeof(*md));
1891 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1893 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1895 memset(md, 0, sizeof(*md));
1898 if (vs->flags & VXLAN_F_GBP)
1899 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1900 /* Note that GBP and GPE can never be active together. This is
1901 * ensured in vxlan_dev_configure.
1904 if (unparsed.vx_flags || unparsed.vx_vni) {
1905 /* If there are any unprocessed flags remaining treat
1906 * this as a malformed packet. This behavior diverges from
1907 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1908 * in reserved fields are to be ignored. The approach here
1909 * maintains compatibility with previous stack code, and also
1910 * is more robust and provides a little more security in
1911 * adding extensions to VXLAN.
1917 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1920 skb_reset_mac_header(skb);
1921 skb->dev = vxlan->dev;
1922 skb->pkt_type = PACKET_HOST;
1925 oiph = skb_network_header(skb);
1926 skb_reset_network_header(skb);
1928 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1929 ++vxlan->dev->stats.rx_frame_errors;
1930 ++vxlan->dev->stats.rx_errors;
1936 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1938 atomic_long_inc(&vxlan->dev->rx_dropped);
1942 dev_sw_netstats_rx_add(vxlan->dev, skb->len);
1943 gro_cells_receive(&vxlan->gro_cells, skb);
1950 /* Consume bad packet */
1955 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1956 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1958 struct vxlan_dev *vxlan;
1959 struct vxlan_sock *vs;
1960 struct vxlanhdr *hdr;
1963 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1966 hdr = vxlan_hdr(skb);
1968 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1971 vs = rcu_dereference_sk_user_data(sk);
1975 vni = vxlan_vni(hdr->vx_vni);
1976 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1983 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1985 struct vxlan_dev *vxlan = netdev_priv(dev);
1986 struct arphdr *parp;
1989 struct neighbour *n;
1991 if (dev->flags & IFF_NOARP)
1994 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1995 dev->stats.tx_dropped++;
1998 parp = arp_hdr(skb);
2000 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
2001 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
2002 parp->ar_pro != htons(ETH_P_IP) ||
2003 parp->ar_op != htons(ARPOP_REQUEST) ||
2004 parp->ar_hln != dev->addr_len ||
2007 arpptr = (u8 *)parp + sizeof(struct arphdr);
2009 arpptr += dev->addr_len; /* sha */
2010 memcpy(&sip, arpptr, sizeof(sip));
2011 arpptr += sizeof(sip);
2012 arpptr += dev->addr_len; /* tha */
2013 memcpy(&tip, arpptr, sizeof(tip));
2015 if (ipv4_is_loopback(tip) ||
2016 ipv4_is_multicast(tip))
2019 n = neigh_lookup(&arp_tbl, &tip, dev);
2022 struct vxlan_fdb *f;
2023 struct sk_buff *reply;
2025 if (!(n->nud_state & NUD_CONNECTED)) {
2030 f = vxlan_find_mac(vxlan, n->ha, vni);
2031 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2032 /* bridge-local neighbor */
2037 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2045 skb_reset_mac_header(reply);
2046 __skb_pull(reply, skb_network_offset(reply));
2047 reply->ip_summed = CHECKSUM_UNNECESSARY;
2048 reply->pkt_type = PACKET_HOST;
2050 if (netif_rx_ni(reply) == NET_RX_DROP)
2051 dev->stats.rx_dropped++;
2052 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2053 union vxlan_addr ipa = {
2054 .sin.sin_addr.s_addr = tip,
2055 .sin.sin_family = AF_INET,
2058 vxlan_ip_miss(dev, &ipa);
2062 return NETDEV_TX_OK;
2065 #if IS_ENABLED(CONFIG_IPV6)
2066 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2067 struct neighbour *n, bool isrouter)
2069 struct net_device *dev = request->dev;
2070 struct sk_buff *reply;
2071 struct nd_msg *ns, *na;
2072 struct ipv6hdr *pip6;
2074 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2078 if (dev == NULL || !pskb_may_pull(request, request->len))
2081 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2082 sizeof(*na) + na_olen + dev->needed_tailroom;
2083 reply = alloc_skb(len, GFP_ATOMIC);
2087 reply->protocol = htons(ETH_P_IPV6);
2089 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2090 skb_push(reply, sizeof(struct ethhdr));
2091 skb_reset_mac_header(reply);
2093 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
2095 daddr = eth_hdr(request)->h_source;
2096 ns_olen = request->len - skb_network_offset(request) -
2097 sizeof(struct ipv6hdr) - sizeof(*ns);
2098 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
2099 if (!ns->opt[i + 1]) {
2103 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2104 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2109 /* Ethernet header */
2110 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2111 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2112 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2113 reply->protocol = htons(ETH_P_IPV6);
2115 skb_pull(reply, sizeof(struct ethhdr));
2116 skb_reset_network_header(reply);
2117 skb_put(reply, sizeof(struct ipv6hdr));
2121 pip6 = ipv6_hdr(reply);
2122 memset(pip6, 0, sizeof(struct ipv6hdr));
2124 pip6->priority = ipv6_hdr(request)->priority;
2125 pip6->nexthdr = IPPROTO_ICMPV6;
2126 pip6->hop_limit = 255;
2127 pip6->daddr = ipv6_hdr(request)->saddr;
2128 pip6->saddr = *(struct in6_addr *)n->primary_key;
2130 skb_pull(reply, sizeof(struct ipv6hdr));
2131 skb_reset_transport_header(reply);
2133 /* Neighbor Advertisement */
2134 na = skb_put_zero(reply, sizeof(*na) + na_olen);
2135 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2136 na->icmph.icmp6_router = isrouter;
2137 na->icmph.icmp6_override = 1;
2138 na->icmph.icmp6_solicited = 1;
2139 na->target = ns->target;
2140 ether_addr_copy(&na->opt[2], n->ha);
2141 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2142 na->opt[1] = na_olen >> 3;
2144 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2145 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2146 csum_partial(na, sizeof(*na)+na_olen, 0));
2148 pip6->payload_len = htons(sizeof(*na)+na_olen);
2150 skb_push(reply, sizeof(struct ipv6hdr));
2152 reply->ip_summed = CHECKSUM_UNNECESSARY;
2157 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
2159 struct vxlan_dev *vxlan = netdev_priv(dev);
2160 const struct in6_addr *daddr;
2161 const struct ipv6hdr *iphdr;
2162 struct inet6_dev *in6_dev;
2163 struct neighbour *n;
2166 in6_dev = __in6_dev_get(dev);
2170 iphdr = ipv6_hdr(skb);
2171 daddr = &iphdr->daddr;
2172 msg = (struct nd_msg *)(iphdr + 1);
2174 if (ipv6_addr_loopback(daddr) ||
2175 ipv6_addr_is_multicast(&msg->target))
2178 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2181 struct vxlan_fdb *f;
2182 struct sk_buff *reply;
2184 if (!(n->nud_state & NUD_CONNECTED)) {
2189 f = vxlan_find_mac(vxlan, n->ha, vni);
2190 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2191 /* bridge-local neighbor */
2196 reply = vxlan_na_create(skb, n,
2197 !!(f ? f->flags & NTF_ROUTER : 0));
2204 if (netif_rx_ni(reply) == NET_RX_DROP)
2205 dev->stats.rx_dropped++;
2207 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2208 union vxlan_addr ipa = {
2209 .sin6.sin6_addr = msg->target,
2210 .sin6.sin6_family = AF_INET6,
2213 vxlan_ip_miss(dev, &ipa);
2218 return NETDEV_TX_OK;
2222 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2224 struct vxlan_dev *vxlan = netdev_priv(dev);
2225 struct neighbour *n;
2227 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2231 switch (ntohs(eth_hdr(skb)->h_proto)) {
2236 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2239 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2240 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2241 union vxlan_addr ipa = {
2242 .sin.sin_addr.s_addr = pip->daddr,
2243 .sin.sin_family = AF_INET,
2246 vxlan_ip_miss(dev, &ipa);
2252 #if IS_ENABLED(CONFIG_IPV6)
2255 struct ipv6hdr *pip6;
2257 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2259 pip6 = ipv6_hdr(skb);
2260 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2261 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2262 union vxlan_addr ipa = {
2263 .sin6.sin6_addr = pip6->daddr,
2264 .sin6.sin6_family = AF_INET6,
2267 vxlan_ip_miss(dev, &ipa);
2281 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2283 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2285 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2294 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2295 struct vxlan_metadata *md)
2297 struct vxlanhdr_gbp *gbp;
2302 gbp = (struct vxlanhdr_gbp *)vxh;
2303 vxh->vx_flags |= VXLAN_HF_GBP;
2305 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2306 gbp->dont_learn = 1;
2308 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2309 gbp->policy_applied = 1;
2311 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2314 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2317 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2319 gpe->np_applied = 1;
2320 gpe->next_protocol = tun_p_from_eth_p(protocol);
2321 if (!gpe->next_protocol)
2322 return -EPFNOSUPPORT;
2326 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2327 int iphdr_len, __be32 vni,
2328 struct vxlan_metadata *md, u32 vxflags,
2331 struct vxlanhdr *vxh;
2334 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2335 __be16 inner_protocol = htons(ETH_P_TEB);
2337 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2338 skb->ip_summed == CHECKSUM_PARTIAL) {
2339 int csum_start = skb_checksum_start_offset(skb);
2341 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2342 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2343 (skb->csum_offset == offsetof(struct udphdr, check) ||
2344 skb->csum_offset == offsetof(struct tcphdr, check)))
2345 type |= SKB_GSO_TUNNEL_REMCSUM;
2348 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2349 + VXLAN_HLEN + iphdr_len;
2351 /* Need space for new headers (invalidates iph ptr) */
2352 err = skb_cow_head(skb, min_headroom);
2356 err = iptunnel_handle_offloads(skb, type);
2360 vxh = __skb_push(skb, sizeof(*vxh));
2361 vxh->vx_flags = VXLAN_HF_VNI;
2362 vxh->vx_vni = vxlan_vni_field(vni);
2364 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2367 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2368 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2369 vxh->vx_flags |= VXLAN_HF_RCO;
2371 if (!skb_is_gso(skb)) {
2372 skb->ip_summed = CHECKSUM_NONE;
2373 skb->encapsulation = 0;
2377 if (vxflags & VXLAN_F_GBP)
2378 vxlan_build_gbp_hdr(vxh, vxflags, md);
2379 if (vxflags & VXLAN_F_GPE) {
2380 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2383 inner_protocol = skb->protocol;
2386 skb_set_inner_protocol(skb, inner_protocol);
2390 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2391 struct vxlan_sock *sock4,
2392 struct sk_buff *skb, int oif, u8 tos,
2393 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2394 struct dst_cache *dst_cache,
2395 const struct ip_tunnel_info *info)
2397 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2398 struct rtable *rt = NULL;
2402 return ERR_PTR(-EIO);
2407 rt = dst_cache_get_ip4(dst_cache, saddr);
2412 memset(&fl4, 0, sizeof(fl4));
2413 fl4.flowi4_oif = oif;
2414 fl4.flowi4_tos = RT_TOS(tos);
2415 fl4.flowi4_mark = skb->mark;
2416 fl4.flowi4_proto = IPPROTO_UDP;
2419 fl4.fl4_dport = dport;
2420 fl4.fl4_sport = sport;
2422 rt = ip_route_output_key(vxlan->net, &fl4);
2424 if (rt->dst.dev == dev) {
2425 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2427 return ERR_PTR(-ELOOP);
2432 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2434 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2435 return ERR_PTR(-ENETUNREACH);
2440 #if IS_ENABLED(CONFIG_IPV6)
2441 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2442 struct net_device *dev,
2443 struct vxlan_sock *sock6,
2444 struct sk_buff *skb, int oif, u8 tos,
2446 const struct in6_addr *daddr,
2447 struct in6_addr *saddr,
2448 __be16 dport, __be16 sport,
2449 struct dst_cache *dst_cache,
2450 const struct ip_tunnel_info *info)
2452 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2453 struct dst_entry *ndst;
2457 return ERR_PTR(-EIO);
2462 ndst = dst_cache_get_ip6(dst_cache, saddr);
2467 memset(&fl6, 0, sizeof(fl6));
2468 fl6.flowi6_oif = oif;
2471 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2472 fl6.flowi6_mark = skb->mark;
2473 fl6.flowi6_proto = IPPROTO_UDP;
2474 fl6.fl6_dport = dport;
2475 fl6.fl6_sport = sport;
2477 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2479 if (unlikely(IS_ERR(ndst))) {
2480 netdev_dbg(dev, "no route to %pI6\n", daddr);
2481 return ERR_PTR(-ENETUNREACH);
2484 if (unlikely(ndst->dev == dev)) {
2485 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2487 return ERR_PTR(-ELOOP);
2492 dst_cache_set_ip6(dst_cache, ndst, saddr);
2497 /* Bypass encapsulation if the destination is local */
2498 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2499 struct vxlan_dev *dst_vxlan, __be32 vni,
2502 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2503 union vxlan_addr loopback;
2504 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2505 struct net_device *dev;
2508 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2509 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2510 skb->pkt_type = PACKET_HOST;
2511 skb->encapsulation = 0;
2512 skb->dev = dst_vxlan->dev;
2513 __skb_pull(skb, skb_network_offset(skb));
2515 if (remote_ip->sa.sa_family == AF_INET) {
2516 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2517 loopback.sa.sa_family = AF_INET;
2518 #if IS_ENABLED(CONFIG_IPV6)
2520 loopback.sin6.sin6_addr = in6addr_loopback;
2521 loopback.sa.sa_family = AF_INET6;
2527 if (unlikely(!(dev->flags & IFF_UP))) {
2532 if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
2533 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2535 u64_stats_update_begin(&tx_stats->syncp);
2536 tx_stats->tx_packets++;
2537 tx_stats->tx_bytes += len;
2538 u64_stats_update_end(&tx_stats->syncp);
2540 if (netif_rx(skb) == NET_RX_SUCCESS) {
2541 u64_stats_update_begin(&rx_stats->syncp);
2542 rx_stats->rx_packets++;
2543 rx_stats->rx_bytes += len;
2544 u64_stats_update_end(&rx_stats->syncp);
2547 dev->stats.rx_dropped++;
2552 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2553 struct vxlan_dev *vxlan,
2554 union vxlan_addr *daddr,
2555 __be16 dst_port, int dst_ifindex, __be32 vni,
2556 struct dst_entry *dst,
2559 #if IS_ENABLED(CONFIG_IPV6)
2560 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2561 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2562 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2564 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2566 /* Bypass encapsulation if the destination is local */
2567 if (rt_flags & RTCF_LOCAL &&
2568 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2569 struct vxlan_dev *dst_vxlan;
2572 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2573 daddr->sa.sa_family, dst_port,
2576 dev->stats.tx_errors++;
2581 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
2588 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2589 __be32 default_vni, struct vxlan_rdst *rdst,
2592 struct dst_cache *dst_cache;
2593 struct ip_tunnel_info *info;
2594 struct vxlan_dev *vxlan = netdev_priv(dev);
2595 const struct iphdr *old_iph = ip_hdr(skb);
2596 union vxlan_addr *dst;
2597 union vxlan_addr remote_ip, local_ip;
2598 struct vxlan_metadata _md;
2599 struct vxlan_metadata *md = &_md;
2600 __be16 src_port = 0, dst_port;
2601 struct dst_entry *ndst = NULL;
2606 u32 flags = vxlan->cfg.flags;
2607 bool udp_sum = false;
2608 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2610 info = skb_tunnel_info(skb);
2613 dst = &rdst->remote_ip;
2614 if (vxlan_addr_any(dst)) {
2616 /* short-circuited back to local bridge */
2617 vxlan_encap_bypass(skb, vxlan, vxlan,
2624 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2625 vni = (rdst->remote_vni) ? : default_vni;
2626 ifindex = rdst->remote_ifindex;
2627 local_ip = vxlan->cfg.saddr;
2628 dst_cache = &rdst->dst_cache;
2629 md->gbp = skb->mark;
2630 if (flags & VXLAN_F_TTL_INHERIT) {
2631 ttl = ip_tunnel_get_ttl(old_iph, skb);
2633 ttl = vxlan->cfg.ttl;
2634 if (!ttl && vxlan_addr_multicast(dst))
2638 tos = vxlan->cfg.tos;
2640 tos = ip_tunnel_get_dsfield(old_iph, skb);
2642 if (dst->sa.sa_family == AF_INET)
2643 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2645 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2646 label = vxlan->cfg.label;
2649 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2653 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2654 if (remote_ip.sa.sa_family == AF_INET) {
2655 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2656 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2658 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2659 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2662 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2663 vni = tunnel_id_to_key32(info->key.tun_id);
2665 dst_cache = &info->dst_cache;
2666 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2667 if (info->options_len < sizeof(*md))
2669 md = ip_tunnel_info_opts(info);
2671 ttl = info->key.ttl;
2672 tos = info->key.tos;
2673 label = info->key.label;
2674 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2676 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2677 vxlan->cfg.port_max, true);
2680 if (dst->sa.sa_family == AF_INET) {
2681 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2686 ifindex = sock4->sock->sk->sk_bound_dev_if;
2688 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2689 dst->sin.sin_addr.s_addr,
2690 &local_ip.sin.sin_addr.s_addr,
2699 /* Bypass encapsulation if the destination is local */
2700 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2701 dst_port, ifindex, vni,
2702 &rt->dst, rt->rt_flags);
2706 if (vxlan->cfg.df == VXLAN_DF_SET) {
2708 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2709 struct ethhdr *eth = eth_hdr(skb);
2711 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2712 (ntohs(eth->h_proto) == ETH_P_IP &&
2713 old_iph->frag_off & htons(IP_DF)))
2716 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2721 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM,
2722 netif_is_any_bridge_port(dev));
2727 struct in_addr src, dst;
2729 src = remote_ip.sin.sin_addr;
2730 dst = local_ip.sin.sin_addr;
2731 info->key.u.ipv4.src = src.s_addr;
2732 info->key.u.ipv4.dst = dst.s_addr;
2734 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2739 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2740 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2741 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2742 vni, md, flags, udp_sum);
2746 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2747 dst->sin.sin_addr.s_addr, tos, ttl, df,
2748 src_port, dst_port, xnet, !udp_sum);
2749 #if IS_ENABLED(CONFIG_IPV6)
2751 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2754 ifindex = sock6->sock->sk->sk_bound_dev_if;
2756 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2757 label, &dst->sin6.sin6_addr,
2758 &local_ip.sin6.sin6_addr,
2762 err = PTR_ERR(ndst);
2768 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2770 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2771 dst_port, ifindex, vni,
2777 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM,
2778 netif_is_any_bridge_port(dev));
2783 struct in6_addr src, dst;
2785 src = remote_ip.sin6.sin6_addr;
2786 dst = local_ip.sin6.sin6_addr;
2787 info->key.u.ipv6.src = src;
2788 info->key.u.ipv6.dst = dst;
2791 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2796 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2797 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2798 skb_scrub_packet(skb, xnet);
2799 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2800 vni, md, flags, udp_sum);
2804 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2805 &local_ip.sin6.sin6_addr,
2806 &dst->sin6.sin6_addr, tos, ttl,
2807 label, src_port, dst_port, !udp_sum);
2815 dev->stats.tx_dropped++;
2822 dev->stats.collisions++;
2823 else if (err == -ENETUNREACH)
2824 dev->stats.tx_carrier_errors++;
2826 dev->stats.tx_errors++;
2830 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2831 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2833 struct vxlan_rdst nh_rdst;
2838 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2839 hash = skb_get_hash(skb);
2842 nh = rcu_dereference(f->nh);
2847 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2850 if (likely(do_xmit))
2851 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2858 dev->stats.tx_dropped++;
2862 /* Transmit local packets over Vxlan
2864 * Outer IP header inherits ECN and DF from inner header.
2865 * Outer UDP destination is the VXLAN assigned port.
2866 * source port is based on hash of flow
2868 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2870 struct vxlan_dev *vxlan = netdev_priv(dev);
2871 struct vxlan_rdst *rdst, *fdst = NULL;
2872 const struct ip_tunnel_info *info;
2873 bool did_rsc = false;
2874 struct vxlan_fdb *f;
2878 info = skb_tunnel_info(skb);
2880 skb_reset_mac_header(skb);
2882 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2883 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2884 info->mode & IP_TUNNEL_INFO_TX) {
2885 vni = tunnel_id_to_key32(info->key.tun_id);
2887 if (info && info->mode & IP_TUNNEL_INFO_TX)
2888 vxlan_xmit_one(skb, dev, vni, NULL, false);
2891 return NETDEV_TX_OK;
2895 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2897 if (ntohs(eth->h_proto) == ETH_P_ARP)
2898 return arp_reduce(dev, skb, vni);
2899 #if IS_ENABLED(CONFIG_IPV6)
2900 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2901 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2902 sizeof(struct nd_msg)) &&
2903 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2904 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2906 if (m->icmph.icmp6_code == 0 &&
2907 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2908 return neigh_reduce(dev, skb, vni);
2914 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2917 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2918 (ntohs(eth->h_proto) == ETH_P_IP ||
2919 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2920 did_rsc = route_shortcircuit(dev, skb);
2922 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2926 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2928 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2929 !is_multicast_ether_addr(eth->h_dest))
2930 vxlan_fdb_miss(vxlan, eth->h_dest);
2932 dev->stats.tx_dropped++;
2934 return NETDEV_TX_OK;
2938 if (rcu_access_pointer(f->nh)) {
2939 vxlan_xmit_nh(skb, dev, f,
2940 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2942 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2943 struct sk_buff *skb1;
2949 skb1 = skb_clone(skb, GFP_ATOMIC);
2951 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2954 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2959 return NETDEV_TX_OK;
2962 /* Walk the forwarding table and purge stale entries */
2963 static void vxlan_cleanup(struct timer_list *t)
2965 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2966 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2969 if (!netif_running(vxlan->dev))
2972 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2973 struct hlist_node *p, *n;
2975 spin_lock(&vxlan->hash_lock[h]);
2976 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2978 = container_of(p, struct vxlan_fdb, hlist);
2979 unsigned long timeout;
2981 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2984 if (f->flags & NTF_EXT_LEARNED)
2987 timeout = f->used + vxlan->cfg.age_interval * HZ;
2988 if (time_before_eq(timeout, jiffies)) {
2989 netdev_dbg(vxlan->dev,
2990 "garbage collect %pM\n",
2992 f->state = NUD_STALE;
2993 vxlan_fdb_destroy(vxlan, f, true, true);
2994 } else if (time_before(timeout, next_timer))
2995 next_timer = timeout;
2997 spin_unlock(&vxlan->hash_lock[h]);
3000 mod_timer(&vxlan->age_timer, next_timer);
3003 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
3005 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3007 spin_lock(&vn->sock_lock);
3008 hlist_del_init_rcu(&vxlan->hlist4.hlist);
3009 #if IS_ENABLED(CONFIG_IPV6)
3010 hlist_del_init_rcu(&vxlan->hlist6.hlist);
3012 spin_unlock(&vn->sock_lock);
3015 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
3016 struct vxlan_dev_node *node)
3018 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3019 __be32 vni = vxlan->default_dst.remote_vni;
3021 node->vxlan = vxlan;
3022 spin_lock(&vn->sock_lock);
3023 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
3024 spin_unlock(&vn->sock_lock);
3027 /* Setup stats when device is created */
3028 static int vxlan_init(struct net_device *dev)
3030 struct vxlan_dev *vxlan = netdev_priv(dev);
3033 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3037 err = gro_cells_init(&vxlan->gro_cells, dev);
3039 free_percpu(dev->tstats);
3046 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
3048 struct vxlan_fdb *f;
3049 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
3051 spin_lock_bh(&vxlan->hash_lock[hash_index]);
3052 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
3054 vxlan_fdb_destroy(vxlan, f, true, true);
3055 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
3058 static void vxlan_uninit(struct net_device *dev)
3060 struct vxlan_dev *vxlan = netdev_priv(dev);
3062 gro_cells_destroy(&vxlan->gro_cells);
3064 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
3066 free_percpu(dev->tstats);
3069 /* Start ageing timer and join group when device is brought up */
3070 static int vxlan_open(struct net_device *dev)
3072 struct vxlan_dev *vxlan = netdev_priv(dev);
3075 ret = vxlan_sock_add(vxlan);
3079 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
3080 ret = vxlan_igmp_join(vxlan);
3081 if (ret == -EADDRINUSE)
3084 vxlan_sock_release(vxlan);
3089 if (vxlan->cfg.age_interval)
3090 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3095 /* Purge the forwarding table */
3096 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
3100 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3101 struct hlist_node *p, *n;
3103 spin_lock_bh(&vxlan->hash_lock[h]);
3104 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3106 = container_of(p, struct vxlan_fdb, hlist);
3107 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3109 /* the all_zeros_mac entry is deleted at vxlan_uninit */
3110 if (is_zero_ether_addr(f->eth_addr) &&
3111 f->vni == vxlan->cfg.vni)
3113 vxlan_fdb_destroy(vxlan, f, true, true);
3115 spin_unlock_bh(&vxlan->hash_lock[h]);
3119 /* Cleanup timer and forwarding table on shutdown */
3120 static int vxlan_stop(struct net_device *dev)
3122 struct vxlan_dev *vxlan = netdev_priv(dev);
3123 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3126 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
3127 !vxlan_group_used(vn, vxlan))
3128 ret = vxlan_igmp_leave(vxlan);
3130 del_timer_sync(&vxlan->age_timer);
3132 vxlan_flush(vxlan, false);
3133 vxlan_sock_release(vxlan);
3138 /* Stub, nothing needs to be done. */
3139 static void vxlan_set_multicast_list(struct net_device *dev)
3143 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3145 struct vxlan_dev *vxlan = netdev_priv(dev);
3146 struct vxlan_rdst *dst = &vxlan->default_dst;
3147 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3148 dst->remote_ifindex);
3149 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
3151 /* This check is different than dev->max_mtu, because it looks at
3152 * the lowerdev->mtu, rather than the static dev->max_mtu
3155 int max_mtu = lowerdev->mtu -
3156 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3157 if (new_mtu > max_mtu)
3165 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3167 struct vxlan_dev *vxlan = netdev_priv(dev);
3168 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3169 __be16 sport, dport;
3171 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3172 vxlan->cfg.port_max, true);
3173 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3175 if (ip_tunnel_info_af(info) == AF_INET) {
3176 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3179 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
3180 info->key.u.ipv4.dst,
3181 &info->key.u.ipv4.src, dport, sport,
3182 &info->dst_cache, info);
3187 #if IS_ENABLED(CONFIG_IPV6)
3188 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3189 struct dst_entry *ndst;
3191 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
3192 info->key.label, &info->key.u.ipv6.dst,
3193 &info->key.u.ipv6.src, dport, sport,
3194 &info->dst_cache, info);
3196 return PTR_ERR(ndst);
3198 #else /* !CONFIG_IPV6 */
3199 return -EPFNOSUPPORT;
3202 info->key.tp_src = sport;
3203 info->key.tp_dst = dport;
3207 static const struct net_device_ops vxlan_netdev_ether_ops = {
3208 .ndo_init = vxlan_init,
3209 .ndo_uninit = vxlan_uninit,
3210 .ndo_open = vxlan_open,
3211 .ndo_stop = vxlan_stop,
3212 .ndo_start_xmit = vxlan_xmit,
3213 .ndo_get_stats64 = ip_tunnel_get_stats64,
3214 .ndo_set_rx_mode = vxlan_set_multicast_list,
3215 .ndo_change_mtu = vxlan_change_mtu,
3216 .ndo_validate_addr = eth_validate_addr,
3217 .ndo_set_mac_address = eth_mac_addr,
3218 .ndo_fdb_add = vxlan_fdb_add,
3219 .ndo_fdb_del = vxlan_fdb_delete,
3220 .ndo_fdb_dump = vxlan_fdb_dump,
3221 .ndo_fdb_get = vxlan_fdb_get,
3222 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3223 .ndo_change_proto_down = dev_change_proto_down_generic,
3226 static const struct net_device_ops vxlan_netdev_raw_ops = {
3227 .ndo_init = vxlan_init,
3228 .ndo_uninit = vxlan_uninit,
3229 .ndo_open = vxlan_open,
3230 .ndo_stop = vxlan_stop,
3231 .ndo_start_xmit = vxlan_xmit,
3232 .ndo_get_stats64 = ip_tunnel_get_stats64,
3233 .ndo_change_mtu = vxlan_change_mtu,
3234 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3237 /* Info for udev, that this is a virtual tunnel endpoint */
3238 static struct device_type vxlan_type = {
3242 /* Calls the ndo_udp_tunnel_add of the caller in order to
3243 * supply the listening VXLAN udp ports. Callers are expected
3244 * to implement the ndo_udp_tunnel_add.
3246 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3248 struct vxlan_sock *vs;
3249 struct net *net = dev_net(dev);
3250 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3253 spin_lock(&vn->sock_lock);
3254 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3255 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3256 unsigned short type;
3258 if (vs->flags & VXLAN_F_GPE)
3259 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3261 type = UDP_TUNNEL_TYPE_VXLAN;
3264 udp_tunnel_push_rx_port(dev, vs->sock, type);
3266 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3269 spin_unlock(&vn->sock_lock);
3272 /* Initialize the device structure. */
3273 static void vxlan_setup(struct net_device *dev)
3275 struct vxlan_dev *vxlan = netdev_priv(dev);
3278 eth_hw_addr_random(dev);
3281 dev->needs_free_netdev = true;
3282 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3284 dev->features |= NETIF_F_LLTX;
3285 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3286 dev->features |= NETIF_F_RXCSUM;
3287 dev->features |= NETIF_F_GSO_SOFTWARE;
3289 dev->vlan_features = dev->features;
3290 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3291 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3292 netif_keep_dst(dev);
3293 dev->priv_flags |= IFF_NO_QUEUE;
3295 /* MTU range: 68 - 65535 */
3296 dev->min_mtu = ETH_MIN_MTU;
3297 dev->max_mtu = ETH_MAX_MTU;
3299 INIT_LIST_HEAD(&vxlan->next);
3301 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3305 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3306 spin_lock_init(&vxlan->hash_lock[h]);
3307 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3311 static void vxlan_ether_setup(struct net_device *dev)
3313 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3314 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3315 dev->netdev_ops = &vxlan_netdev_ether_ops;
3318 static void vxlan_raw_setup(struct net_device *dev)
3320 dev->header_ops = NULL;
3321 dev->type = ARPHRD_NONE;
3322 dev->hard_header_len = 0;
3324 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3325 dev->netdev_ops = &vxlan_netdev_raw_ops;
3328 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3329 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3330 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
3331 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3332 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3333 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
3334 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3335 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3336 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3337 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3338 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3339 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3340 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3341 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3342 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3343 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3344 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3345 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3346 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3347 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3348 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3349 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3350 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3351 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3352 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3353 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3354 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3355 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3356 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3357 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3360 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3361 struct netlink_ext_ack *extack)
3363 if (tb[IFLA_ADDRESS]) {
3364 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3365 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3366 "Provided link layer address is not Ethernet");
3370 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3371 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3372 "Provided Ethernet address is not unicast");
3373 return -EADDRNOTAVAIL;
3378 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3380 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3381 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3382 "MTU must be between 68 and 65535");
3388 NL_SET_ERR_MSG(extack,
3389 "Required attributes not provided to perform the operation");
3393 if (data[IFLA_VXLAN_ID]) {
3394 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3396 if (id >= VXLAN_N_VID) {
3397 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3398 "VXLAN ID must be lower than 16777216");
3403 if (data[IFLA_VXLAN_PORT_RANGE]) {
3404 const struct ifla_vxlan_port_range *p
3405 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3407 if (ntohs(p->high) < ntohs(p->low)) {
3408 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3409 "Invalid source port range");
3414 if (data[IFLA_VXLAN_DF]) {
3415 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3417 if (df < 0 || df > VXLAN_DF_MAX) {
3418 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3419 "Invalid DF attribute");
3427 static void vxlan_get_drvinfo(struct net_device *netdev,
3428 struct ethtool_drvinfo *drvinfo)
3430 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3431 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3434 static int vxlan_get_link_ksettings(struct net_device *dev,
3435 struct ethtool_link_ksettings *cmd)
3437 struct vxlan_dev *vxlan = netdev_priv(dev);
3438 struct vxlan_rdst *dst = &vxlan->default_dst;
3439 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3440 dst->remote_ifindex);
3443 cmd->base.duplex = DUPLEX_UNKNOWN;
3444 cmd->base.port = PORT_OTHER;
3445 cmd->base.speed = SPEED_UNKNOWN;
3450 return __ethtool_get_link_ksettings(lowerdev, cmd);
3453 static const struct ethtool_ops vxlan_ethtool_ops = {
3454 .get_drvinfo = vxlan_get_drvinfo,
3455 .get_link = ethtool_op_get_link,
3456 .get_link_ksettings = vxlan_get_link_ksettings,
3459 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3460 __be16 port, u32 flags, int ifindex)
3462 struct socket *sock;
3463 struct udp_port_cfg udp_conf;
3466 memset(&udp_conf, 0, sizeof(udp_conf));
3469 udp_conf.family = AF_INET6;
3470 udp_conf.use_udp6_rx_checksums =
3471 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3472 udp_conf.ipv6_v6only = 1;
3474 udp_conf.family = AF_INET;
3477 udp_conf.local_udp_port = port;
3478 udp_conf.bind_ifindex = ifindex;
3480 /* Open UDP socket */
3481 err = udp_sock_create(net, &udp_conf, &sock);
3483 return ERR_PTR(err);
3488 /* Create new listen socket if needed */
3489 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3490 __be16 port, u32 flags,
3493 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3494 struct vxlan_sock *vs;
3495 struct socket *sock;
3497 struct udp_tunnel_sock_cfg tunnel_cfg;
3499 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3501 return ERR_PTR(-ENOMEM);
3503 for (h = 0; h < VNI_HASH_SIZE; ++h)
3504 INIT_HLIST_HEAD(&vs->vni_list[h]);
3506 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3509 return ERR_CAST(sock);
3513 refcount_set(&vs->refcnt, 1);
3514 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3516 spin_lock(&vn->sock_lock);
3517 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3518 udp_tunnel_notify_add_rx_port(sock,
3519 (vs->flags & VXLAN_F_GPE) ?
3520 UDP_TUNNEL_TYPE_VXLAN_GPE :
3521 UDP_TUNNEL_TYPE_VXLAN);
3522 spin_unlock(&vn->sock_lock);
3524 /* Mark socket as an encapsulation socket. */
3525 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3526 tunnel_cfg.sk_user_data = vs;
3527 tunnel_cfg.encap_type = 1;
3528 tunnel_cfg.encap_rcv = vxlan_rcv;
3529 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3530 tunnel_cfg.encap_destroy = NULL;
3531 tunnel_cfg.gro_receive = vxlan_gro_receive;
3532 tunnel_cfg.gro_complete = vxlan_gro_complete;
3534 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3539 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3541 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3542 struct vxlan_sock *vs = NULL;
3543 struct vxlan_dev_node *node;
3544 int l3mdev_index = 0;
3546 if (vxlan->cfg.remote_ifindex)
3547 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3548 vxlan->net, vxlan->cfg.remote_ifindex);
3550 if (!vxlan->cfg.no_share) {
3551 spin_lock(&vn->sock_lock);
3552 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3553 vxlan->cfg.dst_port, vxlan->cfg.flags,
3555 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3556 spin_unlock(&vn->sock_lock);
3559 spin_unlock(&vn->sock_lock);
3562 vs = vxlan_socket_create(vxlan->net, ipv6,
3563 vxlan->cfg.dst_port, vxlan->cfg.flags,
3567 #if IS_ENABLED(CONFIG_IPV6)
3569 rcu_assign_pointer(vxlan->vn6_sock, vs);
3570 node = &vxlan->hlist6;
3574 rcu_assign_pointer(vxlan->vn4_sock, vs);
3575 node = &vxlan->hlist4;
3577 vxlan_vs_add_dev(vs, vxlan, node);
3581 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3583 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3584 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3585 bool ipv4 = !ipv6 || metadata;
3588 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3589 #if IS_ENABLED(CONFIG_IPV6)
3590 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3592 ret = __vxlan_sock_add(vxlan, true);
3593 if (ret < 0 && ret != -EAFNOSUPPORT)
3598 ret = __vxlan_sock_add(vxlan, false);
3600 vxlan_sock_release(vxlan);
3604 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3605 struct net_device **lower,
3606 struct vxlan_dev *old,
3607 struct netlink_ext_ack *extack)
3609 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3610 struct vxlan_dev *tmp;
3611 bool use_ipv6 = false;
3613 if (conf->flags & VXLAN_F_GPE) {
3614 /* For now, allow GPE only together with
3615 * COLLECT_METADATA. This can be relaxed later; in such
3616 * case, the other side of the PtP link will have to be
3619 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3620 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3621 NL_SET_ERR_MSG(extack,
3622 "VXLAN GPE does not support this combination of attributes");
3627 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3628 /* Unless IPv6 is explicitly requested, assume IPv4 */
3629 conf->remote_ip.sa.sa_family = AF_INET;
3630 conf->saddr.sa.sa_family = AF_INET;
3631 } else if (!conf->remote_ip.sa.sa_family) {
3632 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3633 } else if (!conf->saddr.sa.sa_family) {
3634 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3637 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3638 NL_SET_ERR_MSG(extack,
3639 "Local and remote address must be from the same family");
3643 if (vxlan_addr_multicast(&conf->saddr)) {
3644 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3648 if (conf->saddr.sa.sa_family == AF_INET6) {
3649 if (!IS_ENABLED(CONFIG_IPV6)) {
3650 NL_SET_ERR_MSG(extack,
3651 "IPv6 support not enabled in the kernel");
3652 return -EPFNOSUPPORT;
3655 conf->flags |= VXLAN_F_IPV6;
3657 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3659 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3661 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3663 if (local_type & IPV6_ADDR_LINKLOCAL) {
3664 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3665 (remote_type != IPV6_ADDR_ANY)) {
3666 NL_SET_ERR_MSG(extack,
3667 "Invalid combination of local and remote address scopes");
3671 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3674 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3675 NL_SET_ERR_MSG(extack,
3676 "Invalid combination of local and remote address scopes");
3680 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3685 if (conf->label && !use_ipv6) {
3686 NL_SET_ERR_MSG(extack,
3687 "Label attribute only applies to IPv6 VXLAN devices");
3691 if (conf->remote_ifindex) {
3692 struct net_device *lowerdev;
3694 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3696 NL_SET_ERR_MSG(extack,
3697 "Invalid local interface, device not found");
3701 #if IS_ENABLED(CONFIG_IPV6)
3703 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3704 if (idev && idev->cnf.disable_ipv6) {
3705 NL_SET_ERR_MSG(extack,
3706 "IPv6 support disabled by administrator");
3714 if (vxlan_addr_multicast(&conf->remote_ip)) {
3715 NL_SET_ERR_MSG(extack,
3716 "Local interface required for multicast remote destination");
3721 #if IS_ENABLED(CONFIG_IPV6)
3722 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3723 NL_SET_ERR_MSG(extack,
3724 "Local interface required for link-local local/remote addresses");
3732 if (!conf->dst_port) {
3733 if (conf->flags & VXLAN_F_GPE)
3734 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3736 conf->dst_port = htons(vxlan_port);
3739 if (!conf->age_interval)
3740 conf->age_interval = FDB_AGE_DEFAULT;
3742 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3746 if (tmp->cfg.vni != conf->vni)
3748 if (tmp->cfg.dst_port != conf->dst_port)
3750 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3751 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3754 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3755 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3758 NL_SET_ERR_MSG(extack,
3759 "A VXLAN device with the specified VNI already exists");
3766 static void vxlan_config_apply(struct net_device *dev,
3767 struct vxlan_config *conf,
3768 struct net_device *lowerdev,
3769 struct net *src_net,
3772 struct vxlan_dev *vxlan = netdev_priv(dev);
3773 struct vxlan_rdst *dst = &vxlan->default_dst;
3774 unsigned short needed_headroom = ETH_HLEN;
3775 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3776 int max_mtu = ETH_MAX_MTU;
3779 if (conf->flags & VXLAN_F_GPE)
3780 vxlan_raw_setup(dev);
3782 vxlan_ether_setup(dev);
3785 dev->mtu = conf->mtu;
3787 vxlan->net = src_net;
3790 dst->remote_vni = conf->vni;
3792 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3795 dst->remote_ifindex = conf->remote_ifindex;
3797 dev->gso_max_size = lowerdev->gso_max_size;
3798 dev->gso_max_segs = lowerdev->gso_max_segs;
3800 needed_headroom = lowerdev->hard_header_len;
3802 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3804 if (max_mtu < ETH_MIN_MTU)
3805 max_mtu = ETH_MIN_MTU;
3807 if (!changelink && !conf->mtu)
3811 if (dev->mtu > max_mtu)
3814 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3815 needed_headroom += VXLAN6_HEADROOM;
3817 needed_headroom += VXLAN_HEADROOM;
3818 dev->needed_headroom = needed_headroom;
3820 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3823 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3824 struct vxlan_config *conf, bool changelink,
3825 struct netlink_ext_ack *extack)
3827 struct vxlan_dev *vxlan = netdev_priv(dev);
3828 struct net_device *lowerdev;
3831 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3835 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3840 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3841 struct vxlan_config *conf,
3842 struct netlink_ext_ack *extack)
3844 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3845 struct vxlan_dev *vxlan = netdev_priv(dev);
3846 struct net_device *remote_dev = NULL;
3847 struct vxlan_fdb *f = NULL;
3848 bool unregister = false;
3849 struct vxlan_rdst *dst;
3852 dst = &vxlan->default_dst;
3853 err = vxlan_dev_configure(net, dev, conf, false, extack);
3857 dev->ethtool_ops = &vxlan_ethtool_ops;
3859 /* create an fdb entry for a valid default destination */
3860 if (!vxlan_addr_any(&dst->remote_ip)) {
3861 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3863 NUD_REACHABLE | NUD_PERMANENT,
3864 vxlan->cfg.dst_port,
3867 dst->remote_ifindex,
3868 NTF_SELF, 0, &f, extack);
3873 err = register_netdevice(dev);
3878 if (dst->remote_ifindex) {
3879 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3883 err = netdev_upper_dev_link(remote_dev, dev, extack);
3888 err = rtnl_configure_link(dev, NULL);
3893 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3895 /* notify default fdb entry */
3896 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3897 RTM_NEWNEIGH, true, extack);
3899 vxlan_fdb_destroy(vxlan, f, false, false);
3901 netdev_upper_dev_unlink(remote_dev, dev);
3906 list_add(&vxlan->next, &vn->vxlan_list);
3908 dst->remote_dev = remote_dev;
3912 netdev_upper_dev_unlink(remote_dev, dev);
3914 /* unregister_netdevice() destroys the default FDB entry with deletion
3915 * notification. But the addition notification was not sent yet, so
3916 * destroy the entry by hand here.
3919 __vxlan_fdb_free(f);
3922 unregister_netdevice(dev);
3926 /* Set/clear flags based on attribute */
3927 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3928 int attrtype, unsigned long mask, bool changelink,
3929 bool changelink_supported,
3930 struct netlink_ext_ack *extack)
3932 unsigned long flags;
3937 if (changelink && !changelink_supported) {
3938 vxlan_flag_attr_error(attrtype, extack);
3942 if (vxlan_policy[attrtype].type == NLA_FLAG)
3943 flags = conf->flags | mask;
3944 else if (nla_get_u8(tb[attrtype]))
3945 flags = conf->flags | mask;
3947 flags = conf->flags & ~mask;
3949 conf->flags = flags;
3954 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3955 struct net_device *dev, struct vxlan_config *conf,
3956 bool changelink, struct netlink_ext_ack *extack)
3958 struct vxlan_dev *vxlan = netdev_priv(dev);
3961 memset(conf, 0, sizeof(*conf));
3963 /* if changelink operation, start with old existing cfg */
3965 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3967 if (data[IFLA_VXLAN_ID]) {
3968 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3970 if (changelink && (vni != conf->vni)) {
3971 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3974 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3977 if (data[IFLA_VXLAN_GROUP]) {
3978 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3979 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3983 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3984 conf->remote_ip.sa.sa_family = AF_INET;
3985 } else if (data[IFLA_VXLAN_GROUP6]) {
3986 if (!IS_ENABLED(CONFIG_IPV6)) {
3987 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3988 return -EPFNOSUPPORT;
3991 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3992 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3996 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3997 conf->remote_ip.sa.sa_family = AF_INET6;
4000 if (data[IFLA_VXLAN_LOCAL]) {
4001 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
4002 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
4006 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
4007 conf->saddr.sa.sa_family = AF_INET;
4008 } else if (data[IFLA_VXLAN_LOCAL6]) {
4009 if (!IS_ENABLED(CONFIG_IPV6)) {
4010 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
4011 return -EPFNOSUPPORT;
4014 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
4015 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
4019 /* TODO: respect scope id */
4020 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
4021 conf->saddr.sa.sa_family = AF_INET6;
4024 if (data[IFLA_VXLAN_LINK])
4025 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
4027 if (data[IFLA_VXLAN_TOS])
4028 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
4030 if (data[IFLA_VXLAN_TTL])
4031 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
4033 if (data[IFLA_VXLAN_TTL_INHERIT]) {
4034 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
4035 VXLAN_F_TTL_INHERIT, changelink, false,
4042 if (data[IFLA_VXLAN_LABEL])
4043 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
4044 IPV6_FLOWLABEL_MASK;
4046 if (data[IFLA_VXLAN_LEARNING]) {
4047 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
4048 VXLAN_F_LEARN, changelink, true,
4052 } else if (!changelink) {
4053 /* default to learn on a new device */
4054 conf->flags |= VXLAN_F_LEARN;
4057 if (data[IFLA_VXLAN_AGEING])
4058 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
4060 if (data[IFLA_VXLAN_PROXY]) {
4061 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4062 VXLAN_F_PROXY, changelink, false,
4068 if (data[IFLA_VXLAN_RSC]) {
4069 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4070 VXLAN_F_RSC, changelink, false,
4076 if (data[IFLA_VXLAN_L2MISS]) {
4077 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4078 VXLAN_F_L2MISS, changelink, false,
4084 if (data[IFLA_VXLAN_L3MISS]) {
4085 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4086 VXLAN_F_L3MISS, changelink, false,
4092 if (data[IFLA_VXLAN_LIMIT]) {
4094 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4095 "Cannot change limit");
4098 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4101 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
4102 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4103 VXLAN_F_COLLECT_METADATA, changelink, false,
4109 if (data[IFLA_VXLAN_PORT_RANGE]) {
4111 const struct ifla_vxlan_port_range *p
4112 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4113 conf->port_min = ntohs(p->low);
4114 conf->port_max = ntohs(p->high);
4116 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4117 "Cannot change port range");
4122 if (data[IFLA_VXLAN_PORT]) {
4124 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4125 "Cannot change port");
4128 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4131 if (data[IFLA_VXLAN_UDP_CSUM]) {
4133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4134 "Cannot change UDP_CSUM flag");
4137 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4138 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4141 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4142 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4143 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4149 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4150 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4151 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4157 if (data[IFLA_VXLAN_REMCSUM_TX]) {
4158 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4159 VXLAN_F_REMCSUM_TX, changelink, false,
4165 if (data[IFLA_VXLAN_REMCSUM_RX]) {
4166 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4167 VXLAN_F_REMCSUM_RX, changelink, false,
4173 if (data[IFLA_VXLAN_GBP]) {
4174 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4175 VXLAN_F_GBP, changelink, false, extack);
4180 if (data[IFLA_VXLAN_GPE]) {
4181 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4182 VXLAN_F_GPE, changelink, false,
4188 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4189 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4190 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4198 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4199 "Cannot change mtu");
4202 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4205 if (data[IFLA_VXLAN_DF])
4206 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4211 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
4212 struct nlattr *tb[], struct nlattr *data[],
4213 struct netlink_ext_ack *extack)
4215 struct vxlan_config conf;
4218 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4222 return __vxlan_dev_create(src_net, dev, &conf, extack);
4225 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4226 struct nlattr *data[],
4227 struct netlink_ext_ack *extack)
4229 struct vxlan_dev *vxlan = netdev_priv(dev);
4230 struct net_device *lowerdev;
4231 struct vxlan_config conf;
4232 struct vxlan_rdst *dst;
4235 dst = &vxlan->default_dst;
4236 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4240 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4245 if (dst->remote_dev == lowerdev)
4248 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4253 /* handle default dst entry */
4254 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4255 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4257 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4258 if (!vxlan_addr_any(&conf.remote_ip)) {
4259 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4261 NUD_REACHABLE | NUD_PERMANENT,
4262 NLM_F_APPEND | NLM_F_CREATE,
4263 vxlan->cfg.dst_port,
4265 conf.remote_ifindex,
4266 NTF_SELF, 0, true, extack);
4268 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4269 netdev_adjacent_change_abort(dst->remote_dev,
4274 if (!vxlan_addr_any(&dst->remote_ip))
4275 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4277 vxlan->cfg.dst_port,
4280 dst->remote_ifindex,
4282 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4285 if (conf.age_interval != vxlan->cfg.age_interval)
4286 mod_timer(&vxlan->age_timer, jiffies);
4288 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4289 if (lowerdev && lowerdev != dst->remote_dev)
4290 dst->remote_dev = lowerdev;
4291 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4295 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4297 struct vxlan_dev *vxlan = netdev_priv(dev);
4299 vxlan_flush(vxlan, true);
4301 list_del(&vxlan->next);
4302 unregister_netdevice_queue(dev, head);
4303 if (vxlan->default_dst.remote_dev)
4304 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4307 static size_t vxlan_get_size(const struct net_device *dev)
4310 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4311 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4312 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4313 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4314 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4315 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4316 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4317 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4318 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4319 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4320 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4321 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4322 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4323 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4324 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4325 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4326 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4327 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4328 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4329 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4330 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4331 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4332 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4333 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4337 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4339 const struct vxlan_dev *vxlan = netdev_priv(dev);
4340 const struct vxlan_rdst *dst = &vxlan->default_dst;
4341 struct ifla_vxlan_port_range ports = {
4342 .low = htons(vxlan->cfg.port_min),
4343 .high = htons(vxlan->cfg.port_max),
4346 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4347 goto nla_put_failure;
4349 if (!vxlan_addr_any(&dst->remote_ip)) {
4350 if (dst->remote_ip.sa.sa_family == AF_INET) {
4351 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4352 dst->remote_ip.sin.sin_addr.s_addr))
4353 goto nla_put_failure;
4354 #if IS_ENABLED(CONFIG_IPV6)
4356 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4357 &dst->remote_ip.sin6.sin6_addr))
4358 goto nla_put_failure;
4363 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4364 goto nla_put_failure;
4366 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4367 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4368 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4369 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4370 goto nla_put_failure;
4371 #if IS_ENABLED(CONFIG_IPV6)
4373 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4374 &vxlan->cfg.saddr.sin6.sin6_addr))
4375 goto nla_put_failure;
4380 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4381 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4382 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4383 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4384 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4385 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4386 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4387 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4388 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4389 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4390 nla_put_u8(skb, IFLA_VXLAN_RSC,
4391 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4392 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4393 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4394 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4395 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4396 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4397 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4398 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4399 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4400 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4401 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4402 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4403 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4404 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4405 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4406 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4407 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4408 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4409 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4410 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4411 goto nla_put_failure;
4413 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4414 goto nla_put_failure;
4416 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4417 nla_put_flag(skb, IFLA_VXLAN_GBP))
4418 goto nla_put_failure;
4420 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4421 nla_put_flag(skb, IFLA_VXLAN_GPE))
4422 goto nla_put_failure;
4424 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4425 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4426 goto nla_put_failure;
4434 static struct net *vxlan_get_link_net(const struct net_device *dev)
4436 struct vxlan_dev *vxlan = netdev_priv(dev);
4441 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4443 .maxtype = IFLA_VXLAN_MAX,
4444 .policy = vxlan_policy,
4445 .priv_size = sizeof(struct vxlan_dev),
4446 .setup = vxlan_setup,
4447 .validate = vxlan_validate,
4448 .newlink = vxlan_newlink,
4449 .changelink = vxlan_changelink,
4450 .dellink = vxlan_dellink,
4451 .get_size = vxlan_get_size,
4452 .fill_info = vxlan_fill_info,
4453 .get_link_net = vxlan_get_link_net,
4456 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4457 u8 name_assign_type,
4458 struct vxlan_config *conf)
4460 struct nlattr *tb[IFLA_MAX + 1];
4461 struct net_device *dev;
4464 memset(&tb, 0, sizeof(tb));
4466 dev = rtnl_create_link(net, name, name_assign_type,
4467 &vxlan_link_ops, tb, NULL);
4471 err = __vxlan_dev_create(net, dev, conf, NULL);
4474 return ERR_PTR(err);
4477 err = rtnl_configure_link(dev, NULL);
4479 LIST_HEAD(list_kill);
4481 vxlan_dellink(dev, &list_kill);
4482 unregister_netdevice_many(&list_kill);
4483 return ERR_PTR(err);
4488 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4490 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4491 struct net_device *dev)
4493 struct vxlan_dev *vxlan, *next;
4494 LIST_HEAD(list_kill);
4496 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4497 struct vxlan_rdst *dst = &vxlan->default_dst;
4499 /* In case we created vxlan device with carrier
4500 * and we loose the carrier due to module unload
4501 * we also need to remove vxlan device. In other
4502 * cases, it's not necessary and remote_ifindex
4503 * is 0 here, so no matches.
4505 if (dst->remote_ifindex == dev->ifindex)
4506 vxlan_dellink(vxlan->dev, &list_kill);
4509 unregister_netdevice_many(&list_kill);
4512 static int vxlan_netdevice_event(struct notifier_block *unused,
4513 unsigned long event, void *ptr)
4515 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4516 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4518 if (event == NETDEV_UNREGISTER) {
4519 if (!dev->udp_tunnel_nic_info)
4520 vxlan_offload_rx_ports(dev, false);
4521 vxlan_handle_lowerdev_unregister(vn, dev);
4522 } else if (event == NETDEV_REGISTER) {
4523 if (!dev->udp_tunnel_nic_info)
4524 vxlan_offload_rx_ports(dev, true);
4525 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4526 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4527 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4533 static struct notifier_block vxlan_notifier_block __read_mostly = {
4534 .notifier_call = vxlan_netdevice_event,
4538 vxlan_fdb_offloaded_set(struct net_device *dev,
4539 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4541 struct vxlan_dev *vxlan = netdev_priv(dev);
4542 struct vxlan_rdst *rdst;
4543 struct vxlan_fdb *f;
4546 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4548 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4550 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4554 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4555 fdb_info->remote_port,
4556 fdb_info->remote_vni,
4557 fdb_info->remote_ifindex);
4561 rdst->offloaded = fdb_info->offloaded;
4564 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4568 vxlan_fdb_external_learn_add(struct net_device *dev,
4569 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4571 struct vxlan_dev *vxlan = netdev_priv(dev);
4572 struct netlink_ext_ack *extack;
4576 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4577 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4579 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4580 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4582 NLM_F_CREATE | NLM_F_REPLACE,
4583 fdb_info->remote_port,
4585 fdb_info->remote_vni,
4586 fdb_info->remote_ifindex,
4587 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4589 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4595 vxlan_fdb_external_learn_del(struct net_device *dev,
4596 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4598 struct vxlan_dev *vxlan = netdev_priv(dev);
4599 struct vxlan_fdb *f;
4603 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4604 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4606 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4609 else if (f->flags & NTF_EXT_LEARNED)
4610 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4611 fdb_info->remote_ip,
4612 fdb_info->remote_port,
4614 fdb_info->remote_vni,
4615 fdb_info->remote_ifindex,
4618 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4623 static int vxlan_switchdev_event(struct notifier_block *unused,
4624 unsigned long event, void *ptr)
4626 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4627 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4631 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4632 vxlan_fdb_offloaded_set(dev, ptr);
4634 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4636 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4638 err = notifier_from_errno(err);
4641 fdb_info->offloaded = true;
4642 vxlan_fdb_offloaded_set(dev, fdb_info);
4644 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4646 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4648 err = notifier_from_errno(err);
4651 fdb_info->offloaded = false;
4652 vxlan_fdb_offloaded_set(dev, fdb_info);
4659 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4660 .notifier_call = vxlan_switchdev_event,
4663 static void vxlan_fdb_nh_flush(struct nexthop *nh)
4665 struct vxlan_fdb *fdb;
4666 struct vxlan_dev *vxlan;
4670 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
4671 vxlan = rcu_dereference(fdb->vdev);
4673 hash_index = fdb_head_index(vxlan, fdb->eth_addr,
4674 vxlan->default_dst.remote_vni);
4675 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4676 if (!hlist_unhashed(&fdb->hlist))
4677 vxlan_fdb_destroy(vxlan, fdb, false, false);
4678 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4683 static int vxlan_nexthop_event(struct notifier_block *nb,
4684 unsigned long event, void *ptr)
4686 struct nexthop *nh = ptr;
4688 if (!nh || event != NEXTHOP_EVENT_DEL)
4691 vxlan_fdb_nh_flush(nh);
4696 static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = {
4697 .notifier_call = vxlan_nexthop_event,
4700 static __net_init int vxlan_init_net(struct net *net)
4702 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4705 INIT_LIST_HEAD(&vn->vxlan_list);
4706 spin_lock_init(&vn->sock_lock);
4708 for (h = 0; h < PORT_HASH_SIZE; ++h)
4709 INIT_HLIST_HEAD(&vn->sock_list[h]);
4711 return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4714 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4716 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4717 struct vxlan_dev *vxlan, *next;
4718 struct net_device *dev, *aux;
4721 for_each_netdev_safe(net, dev, aux)
4722 if (dev->rtnl_link_ops == &vxlan_link_ops)
4723 unregister_netdevice_queue(dev, head);
4725 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4726 /* If vxlan->dev is in the same netns, it has already been added
4727 * to the list by the previous loop.
4729 if (!net_eq(dev_net(vxlan->dev), net))
4730 unregister_netdevice_queue(vxlan->dev, head);
4733 for (h = 0; h < PORT_HASH_SIZE; ++h)
4734 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4737 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4743 list_for_each_entry(net, net_list, exit_list)
4744 unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4745 list_for_each_entry(net, net_list, exit_list)
4746 vxlan_destroy_tunnels(net, &list);
4748 unregister_netdevice_many(&list);
4752 static struct pernet_operations vxlan_net_ops = {
4753 .init = vxlan_init_net,
4754 .exit_batch = vxlan_exit_batch_net,
4755 .id = &vxlan_net_id,
4756 .size = sizeof(struct vxlan_net),
4759 static int __init vxlan_init_module(void)
4763 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4765 rc = register_pernet_subsys(&vxlan_net_ops);
4769 rc = register_netdevice_notifier(&vxlan_notifier_block);
4773 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4777 rc = rtnl_link_register(&vxlan_link_ops);
4783 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4785 unregister_netdevice_notifier(&vxlan_notifier_block);
4787 unregister_pernet_subsys(&vxlan_net_ops);
4791 late_initcall(vxlan_init_module);
4793 static void __exit vxlan_cleanup_module(void)
4795 rtnl_link_unregister(&vxlan_link_ops);
4796 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4797 unregister_netdevice_notifier(&vxlan_notifier_block);
4798 unregister_pernet_subsys(&vxlan_net_ops);
4799 /* rcu_barrier() is called by netns */
4801 module_exit(vxlan_cleanup_module);
4803 MODULE_LICENSE("GPL");
4804 MODULE_VERSION(VXLAN_VERSION);
4805 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4806 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4807 MODULE_ALIAS_RTNL_LINK("vxlan");