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;
86 #define NTF_VXLAN_ADDED_BY_USER 0x100
88 /* salt for hash table */
89 static u32 vxlan_salt __read_mostly;
91 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
93 return vs->flags & VXLAN_F_COLLECT_METADATA ||
94 ip_tunnel_collect_metadata();
97 #if IS_ENABLED(CONFIG_IPV6)
99 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
101 if (a->sa.sa_family != b->sa.sa_family)
103 if (a->sa.sa_family == AF_INET6)
104 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
106 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
109 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
111 if (nla_len(nla) >= sizeof(struct in6_addr)) {
112 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
113 ip->sa.sa_family = AF_INET6;
115 } else if (nla_len(nla) >= sizeof(__be32)) {
116 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
117 ip->sa.sa_family = AF_INET;
120 return -EAFNOSUPPORT;
124 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
125 const union vxlan_addr *ip)
127 if (ip->sa.sa_family == AF_INET6)
128 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
130 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
133 #else /* !CONFIG_IPV6 */
136 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
138 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
141 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
143 if (nla_len(nla) >= sizeof(struct in6_addr)) {
144 return -EAFNOSUPPORT;
145 } else if (nla_len(nla) >= sizeof(__be32)) {
146 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
147 ip->sa.sa_family = AF_INET;
150 return -EAFNOSUPPORT;
154 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
155 const union vxlan_addr *ip)
157 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
161 /* Virtual Network hash table head */
162 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
164 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
167 /* Socket hash table head */
168 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
170 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
172 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
175 /* First remote destination for a forwarding entry.
176 * Guaranteed to be non-NULL because remotes are never deleted.
178 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
180 if (rcu_access_pointer(fdb->nh))
182 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
185 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187 if (rcu_access_pointer(fdb->nh))
189 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
192 /* Find VXLAN socket based on network namespace, address family and UDP port
193 * and enabled unshareable flags.
195 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
196 __be16 port, u32 flags, int ifindex)
198 struct vxlan_sock *vs;
200 flags &= VXLAN_F_RCV_FLAGS;
202 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
203 if (inet_sk(vs->sock->sk)->inet_sport == port &&
204 vxlan_get_sk_family(vs) == family &&
205 vs->flags == flags &&
206 vs->sock->sk->sk_bound_dev_if == ifindex)
212 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
215 struct vxlan_dev_node *node;
217 /* For flow based devices, map all packets to VNI 0 */
218 if (vs->flags & VXLAN_F_COLLECT_METADATA)
221 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
222 if (node->vxlan->default_dst.remote_vni != vni)
225 if (IS_ENABLED(CONFIG_IPV6)) {
226 const struct vxlan_config *cfg = &node->vxlan->cfg;
228 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
229 cfg->remote_ifindex != ifindex)
239 /* Look up VNI in a per net namespace table */
240 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
241 __be32 vni, sa_family_t family,
242 __be16 port, u32 flags)
244 struct vxlan_sock *vs;
246 vs = vxlan_find_sock(net, family, port, flags, ifindex);
250 return vxlan_vs_find_vni(vs, ifindex, vni);
253 /* Fill in neighbour message in skbuff. */
254 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
255 const struct vxlan_fdb *fdb,
256 u32 portid, u32 seq, int type, unsigned int flags,
257 const struct vxlan_rdst *rdst)
259 unsigned long now = jiffies;
260 struct nda_cacheinfo ci;
261 bool send_ip, send_eth;
262 struct nlmsghdr *nlh;
266 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
270 ndm = nlmsg_data(nlh);
271 memset(ndm, 0, sizeof(*ndm));
273 send_eth = send_ip = true;
275 nh = rcu_dereference_rtnl(fdb->nh);
276 if (type == RTM_GETNEIGH) {
278 send_ip = !vxlan_addr_any(&rdst->remote_ip);
279 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
281 ndm->ndm_family = nexthop_get_family(nh);
283 send_eth = !is_zero_ether_addr(fdb->eth_addr);
285 ndm->ndm_family = AF_BRIDGE;
286 ndm->ndm_state = fdb->state;
287 ndm->ndm_ifindex = vxlan->dev->ifindex;
288 ndm->ndm_flags = fdb->flags;
289 if (rdst && rdst->offloaded)
290 ndm->ndm_flags |= NTF_OFFLOADED;
291 ndm->ndm_type = RTN_UNICAST;
293 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
294 nla_put_s32(skb, NDA_LINK_NETNSID,
295 peernet2id(dev_net(vxlan->dev), vxlan->net)))
296 goto nla_put_failure;
298 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
299 goto nla_put_failure;
301 if (nla_put_u32(skb, NDA_NH_ID, nh->id))
302 goto nla_put_failure;
304 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
306 goto nla_put_failure;
308 if (rdst->remote_port &&
309 rdst->remote_port != vxlan->cfg.dst_port &&
310 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
311 goto nla_put_failure;
312 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
313 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
314 goto nla_put_failure;
315 if (rdst->remote_ifindex &&
316 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
317 goto nla_put_failure;
320 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
321 nla_put_u32(skb, NDA_SRC_VNI,
322 be32_to_cpu(fdb->vni)))
323 goto nla_put_failure;
325 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
326 ci.ndm_confirmed = 0;
327 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
330 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
331 goto nla_put_failure;
337 nlmsg_cancel(skb, nlh);
341 static inline size_t vxlan_nlmsg_size(void)
343 return NLMSG_ALIGN(sizeof(struct ndmsg))
344 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
345 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
346 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
347 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
348 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
349 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
350 + nla_total_size(sizeof(struct nda_cacheinfo));
353 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
354 struct vxlan_rdst *rd, int type)
356 struct net *net = dev_net(vxlan->dev);
360 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
364 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
366 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
367 WARN_ON(err == -EMSGSIZE);
372 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
376 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
379 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
380 const struct vxlan_fdb *fdb,
381 const struct vxlan_rdst *rd,
382 struct netlink_ext_ack *extack,
383 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
385 fdb_info->info.dev = vxlan->dev;
386 fdb_info->info.extack = extack;
387 fdb_info->remote_ip = rd->remote_ip;
388 fdb_info->remote_port = rd->remote_port;
389 fdb_info->remote_vni = rd->remote_vni;
390 fdb_info->remote_ifindex = rd->remote_ifindex;
391 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
392 fdb_info->vni = fdb->vni;
393 fdb_info->offloaded = rd->offloaded;
394 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
397 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
398 struct vxlan_fdb *fdb,
399 struct vxlan_rdst *rd,
401 struct netlink_ext_ack *extack)
403 struct switchdev_notifier_vxlan_fdb_info info;
404 enum switchdev_notifier_type notifier_type;
410 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
411 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
412 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
413 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
415 return notifier_to_errno(ret);
418 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
419 struct vxlan_rdst *rd, int type, bool swdev_notify,
420 struct netlink_ext_ack *extack)
424 if (swdev_notify && rd) {
427 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
433 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
439 __vxlan_fdb_notify(vxlan, fdb, rd, type);
443 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
445 struct vxlan_dev *vxlan = netdev_priv(dev);
446 struct vxlan_fdb f = {
449 struct vxlan_rdst remote = {
450 .remote_ip = *ipa, /* goes to NDA_DST */
451 .remote_vni = cpu_to_be32(VXLAN_N_VID),
454 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
457 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
459 struct vxlan_fdb f = {
462 struct vxlan_rdst remote = { };
464 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
466 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
469 /* Hash Ethernet address */
470 static u32 eth_hash(const unsigned char *addr)
472 u64 value = get_unaligned((u64 *)addr);
474 /* only want 6 bytes */
480 return hash_64(value, FDB_HASH_BITS);
483 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
485 /* use 1 byte of OUI and 3 bytes of NIC */
486 u32 key = get_unaligned((u32 *)(addr + 2));
488 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
491 static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
493 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
494 return eth_vni_hash(mac, vni);
496 return eth_hash(mac);
499 /* Hash chain to use given mac address */
500 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
501 const u8 *mac, __be32 vni)
503 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
506 /* Look up Ethernet address in forwarding table */
507 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
508 const u8 *mac, __be32 vni)
510 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
513 hlist_for_each_entry_rcu(f, head, hlist) {
514 if (ether_addr_equal(mac, f->eth_addr)) {
515 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
527 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
528 const u8 *mac, __be32 vni)
532 f = __vxlan_find_mac(vxlan, mac, vni);
533 if (f && f->used != jiffies)
539 /* caller should hold vxlan->hash_lock */
540 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
541 union vxlan_addr *ip, __be16 port,
542 __be32 vni, __u32 ifindex)
544 struct vxlan_rdst *rd;
546 list_for_each_entry(rd, &f->remotes, list) {
547 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
548 rd->remote_port == port &&
549 rd->remote_vni == vni &&
550 rd->remote_ifindex == ifindex)
557 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
558 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
560 struct vxlan_dev *vxlan = netdev_priv(dev);
561 u8 eth_addr[ETH_ALEN + 2] = { 0 };
562 struct vxlan_rdst *rdst;
566 if (is_multicast_ether_addr(mac) ||
567 is_zero_ether_addr(mac))
570 ether_addr_copy(eth_addr, mac);
574 f = __vxlan_find_mac(vxlan, eth_addr, vni);
580 rdst = first_remote_rcu(f);
581 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
587 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
589 static int vxlan_fdb_notify_one(struct notifier_block *nb,
590 const struct vxlan_dev *vxlan,
591 const struct vxlan_fdb *f,
592 const struct vxlan_rdst *rdst,
593 struct netlink_ext_ack *extack)
595 struct switchdev_notifier_vxlan_fdb_info fdb_info;
598 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
599 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
601 return notifier_to_errno(rc);
604 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
605 struct notifier_block *nb,
606 struct netlink_ext_ack *extack)
608 struct vxlan_dev *vxlan;
609 struct vxlan_rdst *rdst;
614 if (!netif_is_vxlan(dev))
616 vxlan = netdev_priv(dev);
618 for (h = 0; h < FDB_HASH_SIZE; ++h) {
619 spin_lock_bh(&vxlan->hash_lock[h]);
620 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
622 list_for_each_entry(rdst, &f->remotes, list) {
623 rc = vxlan_fdb_notify_one(nb, vxlan,
631 spin_unlock_bh(&vxlan->hash_lock[h]);
636 spin_unlock_bh(&vxlan->hash_lock[h]);
639 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
641 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
643 struct vxlan_dev *vxlan;
644 struct vxlan_rdst *rdst;
648 if (!netif_is_vxlan(dev))
650 vxlan = netdev_priv(dev);
652 for (h = 0; h < FDB_HASH_SIZE; ++h) {
653 spin_lock_bh(&vxlan->hash_lock[h]);
654 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
656 list_for_each_entry(rdst, &f->remotes, list)
657 rdst->offloaded = false;
658 spin_unlock_bh(&vxlan->hash_lock[h]);
662 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
664 /* Replace destination of unicast mac */
665 static int vxlan_fdb_replace(struct vxlan_fdb *f,
666 union vxlan_addr *ip, __be16 port, __be32 vni,
667 __u32 ifindex, struct vxlan_rdst *oldrd)
669 struct vxlan_rdst *rd;
671 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
675 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
680 dst_cache_reset(&rd->dst_cache);
682 rd->remote_port = port;
683 rd->remote_vni = vni;
684 rd->remote_ifindex = ifindex;
685 rd->offloaded = false;
689 /* Add/update destinations for multicast */
690 static int vxlan_fdb_append(struct vxlan_fdb *f,
691 union vxlan_addr *ip, __be16 port, __be32 vni,
692 __u32 ifindex, struct vxlan_rdst **rdp)
694 struct vxlan_rdst *rd;
696 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
700 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
704 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
710 rd->remote_port = port;
711 rd->offloaded = false;
712 rd->remote_vni = vni;
713 rd->remote_ifindex = ifindex;
715 list_add_tail_rcu(&rd->list, &f->remotes);
721 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
723 struct vxlanhdr *vh, size_t hdrlen,
725 struct gro_remcsum *grc,
728 size_t start, offset;
730 if (skb->remcsum_offload)
733 if (!NAPI_GRO_CB(skb)->csum_valid)
736 start = vxlan_rco_start(vni_field);
737 offset = start + vxlan_rco_offset(vni_field);
739 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
740 start, offset, grc, nopartial);
742 skb->remcsum_offload = 1;
747 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
748 struct list_head *head,
751 struct sk_buff *pp = NULL;
753 struct vxlanhdr *vh, *vh2;
754 unsigned int hlen, off_vx;
756 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
758 struct gro_remcsum grc;
760 skb_gro_remcsum_init(&grc);
762 off_vx = skb_gro_offset(skb);
763 hlen = off_vx + sizeof(*vh);
764 vh = skb_gro_header_fast(skb, off_vx);
765 if (skb_gro_header_hard(skb, hlen)) {
766 vh = skb_gro_header_slow(skb, hlen, off_vx);
771 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
773 flags = vh->vx_flags;
775 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
776 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
779 VXLAN_F_REMCSUM_NOPARTIAL));
785 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
787 list_for_each_entry(p, head, list) {
788 if (!NAPI_GRO_CB(p)->same_flow)
791 vh2 = (struct vxlanhdr *)(p->data + off_vx);
792 if (vh->vx_flags != vh2->vx_flags ||
793 vh->vx_vni != vh2->vx_vni) {
794 NAPI_GRO_CB(p)->same_flow = 0;
799 pp = call_gro_receive(eth_gro_receive, head, skb);
803 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
808 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
810 /* Sets 'skb->inner_mac_header' since we are always called with
811 * 'skb->encapsulation' set.
813 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
816 static struct vxlan_fdb *vxlan_fdb_alloc(const u8 *mac, __u16 state,
817 __be32 src_vni, __u16 ndm_flags)
821 f = kmalloc(sizeof(*f), GFP_ATOMIC);
825 f->flags = ndm_flags;
826 f->updated = f->used = jiffies;
829 INIT_LIST_HEAD(&f->nh_list);
830 INIT_LIST_HEAD(&f->remotes);
831 memcpy(f->eth_addr, mac, ETH_ALEN);
836 static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
837 __be32 src_vni, struct vxlan_fdb *f)
840 hlist_add_head_rcu(&f->hlist,
841 vxlan_fdb_head(vxlan, mac, src_vni));
844 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
845 u32 nhid, struct netlink_ext_ack *extack)
847 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
848 struct nh_group *nhg;
852 if (old_nh && old_nh->id == nhid)
855 nh = nexthop_find_by_id(vxlan->net, nhid);
857 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
862 if (!nexthop_get(nh)) {
863 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
867 if (!nh->is_fdb_nh) {
868 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
872 if (!nh->is_group || !nh->nh_grp->mpath) {
873 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
877 /* check nexthop group family */
878 nhg = rtnl_dereference(nh->nh_grp);
879 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
883 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
890 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
897 list_del_rcu(&fdb->nh_list);
900 rcu_assign_pointer(fdb->nh, nh);
901 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
910 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
911 const u8 *mac, union vxlan_addr *ip,
912 __u16 state, __be16 port, __be32 src_vni,
913 __be32 vni, __u32 ifindex, __u16 ndm_flags,
914 u32 nhid, struct vxlan_fdb **fdb,
915 struct netlink_ext_ack *extack)
917 struct vxlan_rdst *rd = NULL;
921 if (vxlan->cfg.addrmax &&
922 vxlan->addrcnt >= vxlan->cfg.addrmax)
925 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
926 f = vxlan_fdb_alloc(mac, state, src_vni, ndm_flags);
931 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
933 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
946 static void __vxlan_fdb_free(struct vxlan_fdb *f)
948 struct vxlan_rdst *rd, *nd;
951 nh = rcu_dereference_raw(f->nh);
953 rcu_assign_pointer(f->nh, NULL);
954 list_del_rcu(&f->nh_list);
958 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
959 dst_cache_destroy(&rd->dst_cache);
965 static void vxlan_fdb_free(struct rcu_head *head)
967 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
972 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
973 bool do_notify, bool swdev_notify)
975 struct vxlan_rdst *rd;
977 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
981 if (rcu_access_pointer(f->nh))
982 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
985 list_for_each_entry(rd, &f->remotes, list)
986 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
990 hlist_del_rcu(&f->hlist);
991 call_rcu(&f->rcu, vxlan_fdb_free);
994 static void vxlan_dst_free(struct rcu_head *head)
996 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
998 dst_cache_destroy(&rd->dst_cache);
1002 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1003 union vxlan_addr *ip,
1004 __u16 state, __u16 flags,
1005 __be16 port, __be32 vni,
1006 __u32 ifindex, __u16 ndm_flags,
1007 struct vxlan_fdb *f, u32 nhid,
1009 struct netlink_ext_ack *extack)
1011 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1012 struct vxlan_rdst *rd = NULL;
1013 struct vxlan_rdst oldrd;
1018 if (nhid && !rcu_access_pointer(f->nh)) {
1019 NL_SET_ERR_MSG(extack,
1020 "Cannot replace an existing non nexthop fdb with a nexthop");
1024 if (nhid && (flags & NLM_F_APPEND)) {
1025 NL_SET_ERR_MSG(extack,
1026 "Cannot append to a nexthop fdb");
1030 /* Do not allow an externally learned entry to take over an entry added
1033 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1034 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1035 if (f->state != state) {
1037 f->updated = jiffies;
1040 if (f->flags != fdb_flags) {
1041 f->flags = fdb_flags;
1042 f->updated = jiffies;
1047 if ((flags & NLM_F_REPLACE)) {
1048 /* Only change unicasts */
1049 if (!(is_multicast_ether_addr(f->eth_addr) ||
1050 is_zero_ether_addr(f->eth_addr))) {
1052 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1056 rc = vxlan_fdb_replace(f, ip, port, vni,
1061 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
1065 if ((flags & NLM_F_APPEND) &&
1066 (is_multicast_ether_addr(f->eth_addr) ||
1067 is_zero_ether_addr(f->eth_addr))) {
1068 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1075 if (ndm_flags & NTF_USE)
1080 rd = first_remote_rtnl(f);
1082 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
1083 swdev_notify, extack);
1093 if ((flags & NLM_F_REPLACE) && rc)
1095 else if ((flags & NLM_F_APPEND) && rc) {
1096 list_del_rcu(&rd->list);
1097 call_rcu(&rd->rcu, vxlan_dst_free);
1102 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1103 const u8 *mac, union vxlan_addr *ip,
1104 __u16 state, __u16 flags,
1105 __be16 port, __be32 src_vni, __be32 vni,
1106 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1108 struct netlink_ext_ack *extack)
1110 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1111 struct vxlan_fdb *f;
1114 /* Disallow replace to add a multicast entry */
1115 if ((flags & NLM_F_REPLACE) &&
1116 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1119 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1120 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1121 vni, ifindex, fdb_flags, nhid, &f, extack);
1125 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1126 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1127 swdev_notify, extack);
1134 vxlan_fdb_destroy(vxlan, f, false, false);
1138 /* Add new entry to forwarding table -- assumes lock held */
1139 static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1140 const u8 *mac, union vxlan_addr *ip,
1141 __u16 state, __u16 flags,
1142 __be16 port, __be32 src_vni, __be32 vni,
1143 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1145 struct netlink_ext_ack *extack)
1147 struct vxlan_fdb *f;
1149 f = __vxlan_find_mac(vxlan, mac, src_vni);
1151 if (flags & NLM_F_EXCL) {
1152 netdev_dbg(vxlan->dev,
1153 "lost race to create %pM\n", mac);
1157 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1158 vni, ifindex, ndm_flags, f,
1159 nhid, swdev_notify, extack);
1161 if (!(flags & NLM_F_CREATE))
1164 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1165 port, src_vni, vni, ifindex,
1166 ndm_flags, nhid, swdev_notify,
1171 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1172 struct vxlan_rdst *rd, bool swdev_notify)
1174 list_del_rcu(&rd->list);
1175 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1176 call_rcu(&rd->rcu, vxlan_dst_free);
1179 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1180 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1181 __be32 *vni, u32 *ifindex, u32 *nhid)
1183 struct net *net = dev_net(vxlan->dev);
1187 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1191 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1193 if (remote->sa.sa_family == AF_INET) {
1194 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1195 ip->sa.sa_family = AF_INET;
1196 #if IS_ENABLED(CONFIG_IPV6)
1198 ip->sin6.sin6_addr = in6addr_any;
1199 ip->sa.sa_family = AF_INET6;
1205 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1207 *port = nla_get_be16(tb[NDA_PORT]);
1209 *port = vxlan->cfg.dst_port;
1213 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1215 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1217 *vni = vxlan->default_dst.remote_vni;
1220 if (tb[NDA_SRC_VNI]) {
1221 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1223 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1225 *src_vni = vxlan->default_dst.remote_vni;
1228 if (tb[NDA_IFINDEX]) {
1229 struct net_device *tdev;
1231 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1233 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1234 tdev = __dev_get_by_index(net, *ifindex);
1236 return -EADDRNOTAVAIL;
1242 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1249 /* Add static entry (via netlink) */
1250 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1251 struct net_device *dev,
1252 const unsigned char *addr, u16 vid, u16 flags,
1253 struct netlink_ext_ack *extack)
1255 struct vxlan_dev *vxlan = netdev_priv(dev);
1256 /* struct net *net = dev_net(vxlan->dev); */
1257 union vxlan_addr ip;
1259 __be32 src_vni, vni;
1264 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1265 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1270 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
1273 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1278 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1279 return -EAFNOSUPPORT;
1281 hash_index = fdb_head_index(vxlan, addr, src_vni);
1282 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1283 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1284 port, src_vni, vni, ifindex,
1285 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1286 nhid, true, extack);
1287 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1292 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1293 const unsigned char *addr, union vxlan_addr ip,
1294 __be16 port, __be32 src_vni, __be32 vni,
1295 u32 ifindex, bool swdev_notify)
1297 struct vxlan_rdst *rd = NULL;
1298 struct vxlan_fdb *f;
1301 f = vxlan_find_mac(vxlan, addr, src_vni);
1305 if (!vxlan_addr_any(&ip)) {
1306 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1311 /* remove a destination if it's not the only one on the list,
1312 * otherwise destroy the fdb entry
1314 if (rd && !list_is_singular(&f->remotes)) {
1315 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1319 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1325 /* Delete entry (via netlink) */
1326 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1327 struct net_device *dev,
1328 const unsigned char *addr, u16 vid)
1330 struct vxlan_dev *vxlan = netdev_priv(dev);
1331 union vxlan_addr ip;
1332 __be32 src_vni, vni;
1338 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1343 hash_index = fdb_head_index(vxlan, addr, src_vni);
1344 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1345 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1347 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1352 /* Dump forwarding table */
1353 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1354 struct net_device *dev,
1355 struct net_device *filter_dev, int *idx)
1357 struct vxlan_dev *vxlan = netdev_priv(dev);
1361 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1362 struct vxlan_fdb *f;
1364 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1365 struct vxlan_rdst *rd;
1367 if (rcu_access_pointer(f->nh)) {
1368 err = vxlan_fdb_info(skb, vxlan, f,
1369 NETLINK_CB(cb->skb).portid,
1378 list_for_each_entry_rcu(rd, &f->remotes, list) {
1379 if (*idx < cb->args[2])
1382 err = vxlan_fdb_info(skb, vxlan, f,
1383 NETLINK_CB(cb->skb).portid,
1398 static int vxlan_fdb_get(struct sk_buff *skb,
1399 struct nlattr *tb[],
1400 struct net_device *dev,
1401 const unsigned char *addr,
1402 u16 vid, u32 portid, u32 seq,
1403 struct netlink_ext_ack *extack)
1405 struct vxlan_dev *vxlan = netdev_priv(dev);
1406 struct vxlan_fdb *f;
1411 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1413 vni = vxlan->default_dst.remote_vni;
1417 f = __vxlan_find_mac(vxlan, addr, vni);
1419 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1424 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1425 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1431 /* Watch incoming packets to learn mapping between Ethernet address
1432 * and Tunnel endpoint.
1433 * Return true if packet is bogus and should be dropped.
1435 static bool vxlan_snoop(struct net_device *dev,
1436 union vxlan_addr *src_ip, const u8 *src_mac,
1437 u32 src_ifindex, __be32 vni)
1439 struct vxlan_dev *vxlan = netdev_priv(dev);
1440 struct vxlan_fdb *f;
1443 #if IS_ENABLED(CONFIG_IPV6)
1444 if (src_ip->sa.sa_family == AF_INET6 &&
1445 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1446 ifindex = src_ifindex;
1449 f = vxlan_find_mac(vxlan, src_mac, vni);
1451 struct vxlan_rdst *rdst = first_remote_rcu(f);
1453 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1454 rdst->remote_ifindex == ifindex))
1457 /* Don't migrate static entries, drop packets */
1458 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1461 /* Don't override an fdb with nexthop with a learnt entry */
1462 if (rcu_access_pointer(f->nh))
1465 if (net_ratelimit())
1467 "%pM migrated from %pIS to %pIS\n",
1468 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1470 rdst->remote_ip = *src_ip;
1471 f->updated = jiffies;
1472 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1474 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1476 /* learned new entry */
1477 spin_lock(&vxlan->hash_lock[hash_index]);
1479 /* close off race between vxlan_flush and incoming packets */
1480 if (netif_running(dev))
1481 vxlan_fdb_update(vxlan, src_mac, src_ip,
1483 NLM_F_EXCL|NLM_F_CREATE,
1484 vxlan->cfg.dst_port,
1486 vxlan->default_dst.remote_vni,
1487 ifindex, NTF_SELF, 0, true, NULL);
1488 spin_unlock(&vxlan->hash_lock[hash_index]);
1494 /* See if multicast group is already in use by other ID */
1495 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1497 struct vxlan_dev *vxlan;
1498 struct vxlan_sock *sock4;
1499 #if IS_ENABLED(CONFIG_IPV6)
1500 struct vxlan_sock *sock6;
1502 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1504 sock4 = rtnl_dereference(dev->vn4_sock);
1506 /* The vxlan_sock is only used by dev, leaving group has
1507 * no effect on other vxlan devices.
1509 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1511 #if IS_ENABLED(CONFIG_IPV6)
1512 sock6 = rtnl_dereference(dev->vn6_sock);
1513 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1517 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1518 if (!netif_running(vxlan->dev) || vxlan == dev)
1521 if (family == AF_INET &&
1522 rtnl_dereference(vxlan->vn4_sock) != sock4)
1524 #if IS_ENABLED(CONFIG_IPV6)
1525 if (family == AF_INET6 &&
1526 rtnl_dereference(vxlan->vn6_sock) != sock6)
1530 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1531 &dev->default_dst.remote_ip))
1534 if (vxlan->default_dst.remote_ifindex !=
1535 dev->default_dst.remote_ifindex)
1544 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1546 struct vxlan_net *vn;
1550 if (!refcount_dec_and_test(&vs->refcnt))
1553 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1554 spin_lock(&vn->sock_lock);
1555 hlist_del_rcu(&vs->hlist);
1556 udp_tunnel_notify_del_rx_port(vs->sock,
1557 (vs->flags & VXLAN_F_GPE) ?
1558 UDP_TUNNEL_TYPE_VXLAN_GPE :
1559 UDP_TUNNEL_TYPE_VXLAN);
1560 spin_unlock(&vn->sock_lock);
1565 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1567 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1568 #if IS_ENABLED(CONFIG_IPV6)
1569 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1571 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1574 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1577 vxlan_vs_del_dev(vxlan);
1579 if (__vxlan_sock_release_prep(sock4)) {
1580 udp_tunnel_sock_release(sock4->sock);
1584 #if IS_ENABLED(CONFIG_IPV6)
1585 if (__vxlan_sock_release_prep(sock6)) {
1586 udp_tunnel_sock_release(sock6->sock);
1592 /* Update multicast group membership when first VNI on
1593 * multicast address is brought up
1595 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1598 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1599 int ifindex = vxlan->default_dst.remote_ifindex;
1602 if (ip->sa.sa_family == AF_INET) {
1603 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1604 struct ip_mreqn mreq = {
1605 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1606 .imr_ifindex = ifindex,
1609 sk = sock4->sock->sk;
1611 ret = ip_mc_join_group(sk, &mreq);
1613 #if IS_ENABLED(CONFIG_IPV6)
1615 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1617 sk = sock6->sock->sk;
1619 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1620 &ip->sin6.sin6_addr);
1628 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1629 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1632 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1633 int ifindex = vxlan->default_dst.remote_ifindex;
1636 if (ip->sa.sa_family == AF_INET) {
1637 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1638 struct ip_mreqn mreq = {
1639 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1640 .imr_ifindex = ifindex,
1643 sk = sock4->sock->sk;
1645 ret = ip_mc_leave_group(sk, &mreq);
1647 #if IS_ENABLED(CONFIG_IPV6)
1649 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1651 sk = sock6->sock->sk;
1653 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1654 &ip->sin6.sin6_addr);
1662 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1663 struct sk_buff *skb, u32 vxflags)
1665 size_t start, offset;
1667 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1670 start = vxlan_rco_start(unparsed->vx_vni);
1671 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1673 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1676 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1677 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1679 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1680 unparsed->vx_vni &= VXLAN_VNI_MASK;
1684 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1685 struct sk_buff *skb, u32 vxflags,
1686 struct vxlan_metadata *md)
1688 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1689 struct metadata_dst *tun_dst;
1691 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1694 md->gbp = ntohs(gbp->policy_id);
1696 tun_dst = (struct metadata_dst *)skb_dst(skb);
1698 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1699 tun_dst->u.tun_info.options_len = sizeof(*md);
1701 if (gbp->dont_learn)
1702 md->gbp |= VXLAN_GBP_DONT_LEARN;
1704 if (gbp->policy_applied)
1705 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1707 /* In flow-based mode, GBP is carried in dst_metadata */
1708 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1709 skb->mark = md->gbp;
1711 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1714 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1716 struct sk_buff *skb, u32 vxflags)
1718 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1720 /* Need to have Next Protocol set for interfaces in GPE mode. */
1721 if (!gpe->np_applied)
1723 /* "The initial version is 0. If a receiver does not support the
1724 * version indicated it MUST drop the packet.
1726 if (gpe->version != 0)
1728 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1729 * processing MUST occur." However, we don't implement OAM
1730 * processing, thus drop the packet.
1735 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1739 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1743 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1744 struct vxlan_sock *vs,
1745 struct sk_buff *skb, __be32 vni)
1747 union vxlan_addr saddr;
1748 u32 ifindex = skb->dev->ifindex;
1750 skb_reset_mac_header(skb);
1751 skb->protocol = eth_type_trans(skb, vxlan->dev);
1752 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1754 /* Ignore packet loops (and multicast echo) */
1755 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1758 /* Get address from the outer IP header */
1759 if (vxlan_get_sk_family(vs) == AF_INET) {
1760 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1761 saddr.sa.sa_family = AF_INET;
1762 #if IS_ENABLED(CONFIG_IPV6)
1764 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1765 saddr.sa.sa_family = AF_INET6;
1769 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1770 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1776 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1777 struct sk_buff *skb)
1781 if (vxlan_get_sk_family(vs) == AF_INET)
1782 err = IP_ECN_decapsulate(oiph, skb);
1783 #if IS_ENABLED(CONFIG_IPV6)
1785 err = IP6_ECN_decapsulate(oiph, skb);
1788 if (unlikely(err) && log_ecn_error) {
1789 if (vxlan_get_sk_family(vs) == AF_INET)
1790 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1791 &((struct iphdr *)oiph)->saddr,
1792 ((struct iphdr *)oiph)->tos);
1794 net_info_ratelimited("non-ECT from %pI6\n",
1795 &((struct ipv6hdr *)oiph)->saddr);
1800 /* Callback from net/ipv4/udp.c to receive packets */
1801 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1803 struct pcpu_sw_netstats *stats;
1804 struct vxlan_dev *vxlan;
1805 struct vxlan_sock *vs;
1806 struct vxlanhdr unparsed;
1807 struct vxlan_metadata _md;
1808 struct vxlan_metadata *md = &_md;
1809 __be16 protocol = htons(ETH_P_TEB);
1810 bool raw_proto = false;
1814 /* Need UDP and VXLAN header to be present */
1815 if (!pskb_may_pull(skb, VXLAN_HLEN))
1818 unparsed = *vxlan_hdr(skb);
1819 /* VNI flag always required to be set */
1820 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1821 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1822 ntohl(vxlan_hdr(skb)->vx_flags),
1823 ntohl(vxlan_hdr(skb)->vx_vni));
1824 /* Return non vxlan pkt */
1827 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1828 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1830 vs = rcu_dereference_sk_user_data(sk);
1834 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1836 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1840 /* For backwards compatibility, only allow reserved fields to be
1841 * used by VXLAN extensions if explicitly requested.
1843 if (vs->flags & VXLAN_F_GPE) {
1844 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1849 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1850 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1853 if (vxlan_collect_metadata(vs)) {
1854 struct metadata_dst *tun_dst;
1856 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1857 key32_to_tunnel_id(vni), sizeof(*md));
1862 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1864 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1866 memset(md, 0, sizeof(*md));
1869 if (vs->flags & VXLAN_F_REMCSUM_RX)
1870 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1872 if (vs->flags & VXLAN_F_GBP)
1873 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1874 /* Note that GBP and GPE can never be active together. This is
1875 * ensured in vxlan_dev_configure.
1878 if (unparsed.vx_flags || unparsed.vx_vni) {
1879 /* If there are any unprocessed flags remaining treat
1880 * this as a malformed packet. This behavior diverges from
1881 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1882 * in reserved fields are to be ignored. The approach here
1883 * maintains compatibility with previous stack code, and also
1884 * is more robust and provides a little more security in
1885 * adding extensions to VXLAN.
1891 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1894 skb_reset_mac_header(skb);
1895 skb->dev = vxlan->dev;
1896 skb->pkt_type = PACKET_HOST;
1899 oiph = skb_network_header(skb);
1900 skb_reset_network_header(skb);
1902 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1903 ++vxlan->dev->stats.rx_frame_errors;
1904 ++vxlan->dev->stats.rx_errors;
1910 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1912 atomic_long_inc(&vxlan->dev->rx_dropped);
1916 stats = this_cpu_ptr(vxlan->dev->tstats);
1917 u64_stats_update_begin(&stats->syncp);
1918 stats->rx_packets++;
1919 stats->rx_bytes += skb->len;
1920 u64_stats_update_end(&stats->syncp);
1922 gro_cells_receive(&vxlan->gro_cells, skb);
1929 /* Consume bad packet */
1934 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1935 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1937 struct vxlan_dev *vxlan;
1938 struct vxlan_sock *vs;
1939 struct vxlanhdr *hdr;
1942 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1945 hdr = vxlan_hdr(skb);
1947 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1950 vs = rcu_dereference_sk_user_data(sk);
1954 vni = vxlan_vni(hdr->vx_vni);
1955 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1962 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1964 struct vxlan_dev *vxlan = netdev_priv(dev);
1965 struct arphdr *parp;
1968 struct neighbour *n;
1970 if (dev->flags & IFF_NOARP)
1973 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1974 dev->stats.tx_dropped++;
1977 parp = arp_hdr(skb);
1979 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1980 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1981 parp->ar_pro != htons(ETH_P_IP) ||
1982 parp->ar_op != htons(ARPOP_REQUEST) ||
1983 parp->ar_hln != dev->addr_len ||
1986 arpptr = (u8 *)parp + sizeof(struct arphdr);
1988 arpptr += dev->addr_len; /* sha */
1989 memcpy(&sip, arpptr, sizeof(sip));
1990 arpptr += sizeof(sip);
1991 arpptr += dev->addr_len; /* tha */
1992 memcpy(&tip, arpptr, sizeof(tip));
1994 if (ipv4_is_loopback(tip) ||
1995 ipv4_is_multicast(tip))
1998 n = neigh_lookup(&arp_tbl, &tip, dev);
2001 struct vxlan_fdb *f;
2002 struct sk_buff *reply;
2004 if (!(n->nud_state & NUD_CONNECTED)) {
2009 f = vxlan_find_mac(vxlan, n->ha, vni);
2010 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2011 /* bridge-local neighbor */
2016 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2024 skb_reset_mac_header(reply);
2025 __skb_pull(reply, skb_network_offset(reply));
2026 reply->ip_summed = CHECKSUM_UNNECESSARY;
2027 reply->pkt_type = PACKET_HOST;
2029 if (netif_rx_ni(reply) == NET_RX_DROP)
2030 dev->stats.rx_dropped++;
2031 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2032 union vxlan_addr ipa = {
2033 .sin.sin_addr.s_addr = tip,
2034 .sin.sin_family = AF_INET,
2037 vxlan_ip_miss(dev, &ipa);
2041 return NETDEV_TX_OK;
2044 #if IS_ENABLED(CONFIG_IPV6)
2045 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2046 struct neighbour *n, bool isrouter)
2048 struct net_device *dev = request->dev;
2049 struct sk_buff *reply;
2050 struct nd_msg *ns, *na;
2051 struct ipv6hdr *pip6;
2053 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2057 if (dev == NULL || !pskb_may_pull(request, request->len))
2060 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2061 sizeof(*na) + na_olen + dev->needed_tailroom;
2062 reply = alloc_skb(len, GFP_ATOMIC);
2066 reply->protocol = htons(ETH_P_IPV6);
2068 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2069 skb_push(reply, sizeof(struct ethhdr));
2070 skb_reset_mac_header(reply);
2072 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
2074 daddr = eth_hdr(request)->h_source;
2075 ns_olen = request->len - skb_network_offset(request) -
2076 sizeof(struct ipv6hdr) - sizeof(*ns);
2077 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
2078 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2079 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2084 /* Ethernet header */
2085 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2086 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2087 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2088 reply->protocol = htons(ETH_P_IPV6);
2090 skb_pull(reply, sizeof(struct ethhdr));
2091 skb_reset_network_header(reply);
2092 skb_put(reply, sizeof(struct ipv6hdr));
2096 pip6 = ipv6_hdr(reply);
2097 memset(pip6, 0, sizeof(struct ipv6hdr));
2099 pip6->priority = ipv6_hdr(request)->priority;
2100 pip6->nexthdr = IPPROTO_ICMPV6;
2101 pip6->hop_limit = 255;
2102 pip6->daddr = ipv6_hdr(request)->saddr;
2103 pip6->saddr = *(struct in6_addr *)n->primary_key;
2105 skb_pull(reply, sizeof(struct ipv6hdr));
2106 skb_reset_transport_header(reply);
2108 /* Neighbor Advertisement */
2109 na = skb_put_zero(reply, sizeof(*na) + na_olen);
2110 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2111 na->icmph.icmp6_router = isrouter;
2112 na->icmph.icmp6_override = 1;
2113 na->icmph.icmp6_solicited = 1;
2114 na->target = ns->target;
2115 ether_addr_copy(&na->opt[2], n->ha);
2116 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2117 na->opt[1] = na_olen >> 3;
2119 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2120 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2121 csum_partial(na, sizeof(*na)+na_olen, 0));
2123 pip6->payload_len = htons(sizeof(*na)+na_olen);
2125 skb_push(reply, sizeof(struct ipv6hdr));
2127 reply->ip_summed = CHECKSUM_UNNECESSARY;
2132 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
2134 struct vxlan_dev *vxlan = netdev_priv(dev);
2135 const struct in6_addr *daddr;
2136 const struct ipv6hdr *iphdr;
2137 struct inet6_dev *in6_dev;
2138 struct neighbour *n;
2141 in6_dev = __in6_dev_get(dev);
2145 iphdr = ipv6_hdr(skb);
2146 daddr = &iphdr->daddr;
2147 msg = (struct nd_msg *)(iphdr + 1);
2149 if (ipv6_addr_loopback(daddr) ||
2150 ipv6_addr_is_multicast(&msg->target))
2153 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2156 struct vxlan_fdb *f;
2157 struct sk_buff *reply;
2159 if (!(n->nud_state & NUD_CONNECTED)) {
2164 f = vxlan_find_mac(vxlan, n->ha, vni);
2165 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2166 /* bridge-local neighbor */
2171 reply = vxlan_na_create(skb, n,
2172 !!(f ? f->flags & NTF_ROUTER : 0));
2179 if (netif_rx_ni(reply) == NET_RX_DROP)
2180 dev->stats.rx_dropped++;
2182 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2183 union vxlan_addr ipa = {
2184 .sin6.sin6_addr = msg->target,
2185 .sin6.sin6_family = AF_INET6,
2188 vxlan_ip_miss(dev, &ipa);
2193 return NETDEV_TX_OK;
2197 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2199 struct vxlan_dev *vxlan = netdev_priv(dev);
2200 struct neighbour *n;
2202 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2206 switch (ntohs(eth_hdr(skb)->h_proto)) {
2211 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2214 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2215 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2216 union vxlan_addr ipa = {
2217 .sin.sin_addr.s_addr = pip->daddr,
2218 .sin.sin_family = AF_INET,
2221 vxlan_ip_miss(dev, &ipa);
2227 #if IS_ENABLED(CONFIG_IPV6)
2230 struct ipv6hdr *pip6;
2232 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2234 pip6 = ipv6_hdr(skb);
2235 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2236 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2237 union vxlan_addr ipa = {
2238 .sin6.sin6_addr = pip6->daddr,
2239 .sin6.sin6_family = AF_INET6,
2242 vxlan_ip_miss(dev, &ipa);
2256 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2258 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2260 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2269 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2270 struct vxlan_metadata *md)
2272 struct vxlanhdr_gbp *gbp;
2277 gbp = (struct vxlanhdr_gbp *)vxh;
2278 vxh->vx_flags |= VXLAN_HF_GBP;
2280 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2281 gbp->dont_learn = 1;
2283 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2284 gbp->policy_applied = 1;
2286 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2289 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2292 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2294 gpe->np_applied = 1;
2295 gpe->next_protocol = tun_p_from_eth_p(protocol);
2296 if (!gpe->next_protocol)
2297 return -EPFNOSUPPORT;
2301 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2302 int iphdr_len, __be32 vni,
2303 struct vxlan_metadata *md, u32 vxflags,
2306 struct vxlanhdr *vxh;
2309 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2310 __be16 inner_protocol = htons(ETH_P_TEB);
2312 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2313 skb->ip_summed == CHECKSUM_PARTIAL) {
2314 int csum_start = skb_checksum_start_offset(skb);
2316 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2317 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2318 (skb->csum_offset == offsetof(struct udphdr, check) ||
2319 skb->csum_offset == offsetof(struct tcphdr, check)))
2320 type |= SKB_GSO_TUNNEL_REMCSUM;
2323 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2324 + VXLAN_HLEN + iphdr_len;
2326 /* Need space for new headers (invalidates iph ptr) */
2327 err = skb_cow_head(skb, min_headroom);
2331 err = iptunnel_handle_offloads(skb, type);
2335 vxh = __skb_push(skb, sizeof(*vxh));
2336 vxh->vx_flags = VXLAN_HF_VNI;
2337 vxh->vx_vni = vxlan_vni_field(vni);
2339 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2342 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2343 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2344 vxh->vx_flags |= VXLAN_HF_RCO;
2346 if (!skb_is_gso(skb)) {
2347 skb->ip_summed = CHECKSUM_NONE;
2348 skb->encapsulation = 0;
2352 if (vxflags & VXLAN_F_GBP)
2353 vxlan_build_gbp_hdr(vxh, vxflags, md);
2354 if (vxflags & VXLAN_F_GPE) {
2355 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2358 inner_protocol = skb->protocol;
2361 skb_set_inner_protocol(skb, inner_protocol);
2365 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2366 struct vxlan_sock *sock4,
2367 struct sk_buff *skb, int oif, u8 tos,
2368 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2369 struct dst_cache *dst_cache,
2370 const struct ip_tunnel_info *info)
2372 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2373 struct rtable *rt = NULL;
2377 return ERR_PTR(-EIO);
2382 rt = dst_cache_get_ip4(dst_cache, saddr);
2387 memset(&fl4, 0, sizeof(fl4));
2388 fl4.flowi4_oif = oif;
2389 fl4.flowi4_tos = RT_TOS(tos);
2390 fl4.flowi4_mark = skb->mark;
2391 fl4.flowi4_proto = IPPROTO_UDP;
2394 fl4.fl4_dport = dport;
2395 fl4.fl4_sport = sport;
2397 rt = ip_route_output_key(vxlan->net, &fl4);
2399 if (rt->dst.dev == dev) {
2400 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2402 return ERR_PTR(-ELOOP);
2407 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2409 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2410 return ERR_PTR(-ENETUNREACH);
2415 #if IS_ENABLED(CONFIG_IPV6)
2416 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2417 struct net_device *dev,
2418 struct vxlan_sock *sock6,
2419 struct sk_buff *skb, int oif, u8 tos,
2421 const struct in6_addr *daddr,
2422 struct in6_addr *saddr,
2423 __be16 dport, __be16 sport,
2424 struct dst_cache *dst_cache,
2425 const struct ip_tunnel_info *info)
2427 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2428 struct dst_entry *ndst;
2432 return ERR_PTR(-EIO);
2437 ndst = dst_cache_get_ip6(dst_cache, saddr);
2442 memset(&fl6, 0, sizeof(fl6));
2443 fl6.flowi6_oif = oif;
2446 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2447 fl6.flowi6_mark = skb->mark;
2448 fl6.flowi6_proto = IPPROTO_UDP;
2449 fl6.fl6_dport = dport;
2450 fl6.fl6_sport = sport;
2452 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2454 if (unlikely(IS_ERR(ndst))) {
2455 netdev_dbg(dev, "no route to %pI6\n", daddr);
2456 return ERR_PTR(-ENETUNREACH);
2459 if (unlikely(ndst->dev == dev)) {
2460 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2462 return ERR_PTR(-ELOOP);
2467 dst_cache_set_ip6(dst_cache, ndst, saddr);
2472 /* Bypass encapsulation if the destination is local */
2473 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2474 struct vxlan_dev *dst_vxlan, __be32 vni)
2476 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2477 union vxlan_addr loopback;
2478 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2479 struct net_device *dev;
2482 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2483 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2484 skb->pkt_type = PACKET_HOST;
2485 skb->encapsulation = 0;
2486 skb->dev = dst_vxlan->dev;
2487 __skb_pull(skb, skb_network_offset(skb));
2489 if (remote_ip->sa.sa_family == AF_INET) {
2490 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2491 loopback.sa.sa_family = AF_INET;
2492 #if IS_ENABLED(CONFIG_IPV6)
2494 loopback.sin6.sin6_addr = in6addr_loopback;
2495 loopback.sa.sa_family = AF_INET6;
2501 if (unlikely(!(dev->flags & IFF_UP))) {
2506 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
2507 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2509 u64_stats_update_begin(&tx_stats->syncp);
2510 tx_stats->tx_packets++;
2511 tx_stats->tx_bytes += len;
2512 u64_stats_update_end(&tx_stats->syncp);
2514 if (netif_rx(skb) == NET_RX_SUCCESS) {
2515 u64_stats_update_begin(&rx_stats->syncp);
2516 rx_stats->rx_packets++;
2517 rx_stats->rx_bytes += len;
2518 u64_stats_update_end(&rx_stats->syncp);
2521 dev->stats.rx_dropped++;
2526 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2527 struct vxlan_dev *vxlan,
2528 union vxlan_addr *daddr,
2529 __be16 dst_port, int dst_ifindex, __be32 vni,
2530 struct dst_entry *dst,
2533 #if IS_ENABLED(CONFIG_IPV6)
2534 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2535 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2536 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2538 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2540 /* Bypass encapsulation if the destination is local */
2541 if (rt_flags & RTCF_LOCAL &&
2542 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2543 struct vxlan_dev *dst_vxlan;
2546 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2547 daddr->sa.sa_family, dst_port,
2550 dev->stats.tx_errors++;
2555 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2562 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2563 __be32 default_vni, struct vxlan_rdst *rdst,
2566 struct dst_cache *dst_cache;
2567 struct ip_tunnel_info *info;
2568 struct vxlan_dev *vxlan = netdev_priv(dev);
2569 const struct iphdr *old_iph = ip_hdr(skb);
2570 union vxlan_addr *dst;
2571 union vxlan_addr remote_ip, local_ip;
2572 struct vxlan_metadata _md;
2573 struct vxlan_metadata *md = &_md;
2574 __be16 src_port = 0, dst_port;
2575 struct dst_entry *ndst = NULL;
2580 u32 flags = vxlan->cfg.flags;
2581 bool udp_sum = false;
2582 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2584 info = skb_tunnel_info(skb);
2587 dst = &rdst->remote_ip;
2588 if (vxlan_addr_any(dst)) {
2590 /* short-circuited back to local bridge */
2591 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2597 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2598 vni = (rdst->remote_vni) ? : default_vni;
2599 ifindex = rdst->remote_ifindex;
2600 local_ip = vxlan->cfg.saddr;
2601 dst_cache = &rdst->dst_cache;
2602 md->gbp = skb->mark;
2603 if (flags & VXLAN_F_TTL_INHERIT) {
2604 ttl = ip_tunnel_get_ttl(old_iph, skb);
2606 ttl = vxlan->cfg.ttl;
2607 if (!ttl && vxlan_addr_multicast(dst))
2611 tos = vxlan->cfg.tos;
2613 tos = ip_tunnel_get_dsfield(old_iph, skb);
2615 if (dst->sa.sa_family == AF_INET)
2616 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2618 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2619 label = vxlan->cfg.label;
2622 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2626 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2627 if (remote_ip.sa.sa_family == AF_INET) {
2628 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2629 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2631 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2632 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2635 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2636 vni = tunnel_id_to_key32(info->key.tun_id);
2638 dst_cache = &info->dst_cache;
2639 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2640 if (info->options_len < sizeof(*md))
2642 md = ip_tunnel_info_opts(info);
2644 ttl = info->key.ttl;
2645 tos = info->key.tos;
2646 label = info->key.label;
2647 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2649 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2650 vxlan->cfg.port_max, true);
2653 if (dst->sa.sa_family == AF_INET) {
2654 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2659 ifindex = sock4->sock->sk->sk_bound_dev_if;
2661 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2662 dst->sin.sin_addr.s_addr,
2663 &local_ip.sin.sin_addr.s_addr,
2672 /* Bypass encapsulation if the destination is local */
2673 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2674 dst_port, ifindex, vni,
2675 &rt->dst, rt->rt_flags);
2679 if (vxlan->cfg.df == VXLAN_DF_SET) {
2681 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2682 struct ethhdr *eth = eth_hdr(skb);
2684 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2685 (ntohs(eth->h_proto) == ETH_P_IP &&
2686 old_iph->frag_off & htons(IP_DF)))
2689 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2694 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
2696 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
2697 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2698 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2699 vni, md, flags, udp_sum);
2703 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2704 dst->sin.sin_addr.s_addr, tos, ttl, df,
2705 src_port, dst_port, xnet, !udp_sum);
2706 #if IS_ENABLED(CONFIG_IPV6)
2708 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2711 ifindex = sock6->sock->sk->sk_bound_dev_if;
2713 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2714 label, &dst->sin6.sin6_addr,
2715 &local_ip.sin6.sin6_addr,
2719 err = PTR_ERR(ndst);
2725 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2727 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2728 dst_port, ifindex, vni,
2734 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
2736 tos = ip_tunnel_ecn_encap(RT_TOS(tos), old_iph, skb);
2737 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2738 skb_scrub_packet(skb, xnet);
2739 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2740 vni, md, flags, udp_sum);
2744 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2745 &local_ip.sin6.sin6_addr,
2746 &dst->sin6.sin6_addr, tos, ttl,
2747 label, src_port, dst_port, !udp_sum);
2755 dev->stats.tx_dropped++;
2762 dev->stats.collisions++;
2763 else if (err == -ENETUNREACH)
2764 dev->stats.tx_carrier_errors++;
2766 dev->stats.tx_errors++;
2770 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2771 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2773 struct vxlan_rdst nh_rdst;
2778 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2779 hash = skb_get_hash(skb);
2782 nh = rcu_dereference(f->nh);
2787 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2790 if (likely(do_xmit))
2791 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2798 dev->stats.tx_dropped++;
2802 /* Transmit local packets over Vxlan
2804 * Outer IP header inherits ECN and DF from inner header.
2805 * Outer UDP destination is the VXLAN assigned port.
2806 * source port is based on hash of flow
2808 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2810 struct vxlan_dev *vxlan = netdev_priv(dev);
2811 struct vxlan_rdst *rdst, *fdst = NULL;
2812 const struct ip_tunnel_info *info;
2813 bool did_rsc = false;
2814 struct vxlan_fdb *f;
2818 info = skb_tunnel_info(skb);
2820 skb_reset_mac_header(skb);
2822 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2823 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2824 info->mode & IP_TUNNEL_INFO_TX) {
2825 vni = tunnel_id_to_key32(info->key.tun_id);
2827 if (info && info->mode & IP_TUNNEL_INFO_TX)
2828 vxlan_xmit_one(skb, dev, vni, NULL, false);
2831 return NETDEV_TX_OK;
2835 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2837 if (ntohs(eth->h_proto) == ETH_P_ARP)
2838 return arp_reduce(dev, skb, vni);
2839 #if IS_ENABLED(CONFIG_IPV6)
2840 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2841 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2842 sizeof(struct nd_msg)) &&
2843 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2844 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2846 if (m->icmph.icmp6_code == 0 &&
2847 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2848 return neigh_reduce(dev, skb, vni);
2854 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2857 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2858 (ntohs(eth->h_proto) == ETH_P_IP ||
2859 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2860 did_rsc = route_shortcircuit(dev, skb);
2862 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2866 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2868 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2869 !is_multicast_ether_addr(eth->h_dest))
2870 vxlan_fdb_miss(vxlan, eth->h_dest);
2872 dev->stats.tx_dropped++;
2874 return NETDEV_TX_OK;
2878 if (rcu_access_pointer(f->nh)) {
2879 vxlan_xmit_nh(skb, dev, f,
2880 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2882 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2883 struct sk_buff *skb1;
2889 skb1 = skb_clone(skb, GFP_ATOMIC);
2891 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2894 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2899 return NETDEV_TX_OK;
2902 /* Walk the forwarding table and purge stale entries */
2903 static void vxlan_cleanup(struct timer_list *t)
2905 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2906 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2909 if (!netif_running(vxlan->dev))
2912 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2913 struct hlist_node *p, *n;
2915 spin_lock(&vxlan->hash_lock[h]);
2916 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2918 = container_of(p, struct vxlan_fdb, hlist);
2919 unsigned long timeout;
2921 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2924 if (f->flags & NTF_EXT_LEARNED)
2927 timeout = f->used + vxlan->cfg.age_interval * HZ;
2928 if (time_before_eq(timeout, jiffies)) {
2929 netdev_dbg(vxlan->dev,
2930 "garbage collect %pM\n",
2932 f->state = NUD_STALE;
2933 vxlan_fdb_destroy(vxlan, f, true, true);
2934 } else if (time_before(timeout, next_timer))
2935 next_timer = timeout;
2937 spin_unlock(&vxlan->hash_lock[h]);
2940 mod_timer(&vxlan->age_timer, next_timer);
2943 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2945 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2947 spin_lock(&vn->sock_lock);
2948 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2949 #if IS_ENABLED(CONFIG_IPV6)
2950 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2952 spin_unlock(&vn->sock_lock);
2955 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2956 struct vxlan_dev_node *node)
2958 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2959 __be32 vni = vxlan->default_dst.remote_vni;
2961 node->vxlan = vxlan;
2962 spin_lock(&vn->sock_lock);
2963 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2964 spin_unlock(&vn->sock_lock);
2967 /* Setup stats when device is created */
2968 static int vxlan_init(struct net_device *dev)
2970 struct vxlan_dev *vxlan = netdev_priv(dev);
2973 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2977 err = gro_cells_init(&vxlan->gro_cells, dev);
2979 free_percpu(dev->tstats);
2986 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2988 struct vxlan_fdb *f;
2989 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
2991 spin_lock_bh(&vxlan->hash_lock[hash_index]);
2992 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2994 vxlan_fdb_destroy(vxlan, f, true, true);
2995 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
2998 static void vxlan_uninit(struct net_device *dev)
3000 struct vxlan_dev *vxlan = netdev_priv(dev);
3002 gro_cells_destroy(&vxlan->gro_cells);
3004 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
3006 free_percpu(dev->tstats);
3009 /* Start ageing timer and join group when device is brought up */
3010 static int vxlan_open(struct net_device *dev)
3012 struct vxlan_dev *vxlan = netdev_priv(dev);
3015 ret = vxlan_sock_add(vxlan);
3019 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
3020 ret = vxlan_igmp_join(vxlan);
3021 if (ret == -EADDRINUSE)
3024 vxlan_sock_release(vxlan);
3029 if (vxlan->cfg.age_interval)
3030 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3035 /* Purge the forwarding table */
3036 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
3040 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3041 struct hlist_node *p, *n;
3043 spin_lock_bh(&vxlan->hash_lock[h]);
3044 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3046 = container_of(p, struct vxlan_fdb, hlist);
3047 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3049 /* the all_zeros_mac entry is deleted at vxlan_uninit */
3050 if (!is_zero_ether_addr(f->eth_addr))
3051 vxlan_fdb_destroy(vxlan, f, true, true);
3053 spin_unlock_bh(&vxlan->hash_lock[h]);
3057 /* Cleanup timer and forwarding table on shutdown */
3058 static int vxlan_stop(struct net_device *dev)
3060 struct vxlan_dev *vxlan = netdev_priv(dev);
3061 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3064 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
3065 !vxlan_group_used(vn, vxlan))
3066 ret = vxlan_igmp_leave(vxlan);
3068 del_timer_sync(&vxlan->age_timer);
3070 vxlan_flush(vxlan, false);
3071 vxlan_sock_release(vxlan);
3076 /* Stub, nothing needs to be done. */
3077 static void vxlan_set_multicast_list(struct net_device *dev)
3081 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3083 struct vxlan_dev *vxlan = netdev_priv(dev);
3084 struct vxlan_rdst *dst = &vxlan->default_dst;
3085 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3086 dst->remote_ifindex);
3087 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
3089 /* This check is different than dev->max_mtu, because it looks at
3090 * the lowerdev->mtu, rather than the static dev->max_mtu
3093 int max_mtu = lowerdev->mtu -
3094 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3095 if (new_mtu > max_mtu)
3103 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3105 struct vxlan_dev *vxlan = netdev_priv(dev);
3106 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3107 __be16 sport, dport;
3109 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3110 vxlan->cfg.port_max, true);
3111 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3113 if (ip_tunnel_info_af(info) == AF_INET) {
3114 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3117 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
3118 info->key.u.ipv4.dst,
3119 &info->key.u.ipv4.src, dport, sport,
3120 &info->dst_cache, info);
3125 #if IS_ENABLED(CONFIG_IPV6)
3126 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3127 struct dst_entry *ndst;
3129 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
3130 info->key.label, &info->key.u.ipv6.dst,
3131 &info->key.u.ipv6.src, dport, sport,
3132 &info->dst_cache, info);
3134 return PTR_ERR(ndst);
3136 #else /* !CONFIG_IPV6 */
3137 return -EPFNOSUPPORT;
3140 info->key.tp_src = sport;
3141 info->key.tp_dst = dport;
3145 static const struct net_device_ops vxlan_netdev_ether_ops = {
3146 .ndo_init = vxlan_init,
3147 .ndo_uninit = vxlan_uninit,
3148 .ndo_open = vxlan_open,
3149 .ndo_stop = vxlan_stop,
3150 .ndo_start_xmit = vxlan_xmit,
3151 .ndo_get_stats64 = ip_tunnel_get_stats64,
3152 .ndo_set_rx_mode = vxlan_set_multicast_list,
3153 .ndo_change_mtu = vxlan_change_mtu,
3154 .ndo_validate_addr = eth_validate_addr,
3155 .ndo_set_mac_address = eth_mac_addr,
3156 .ndo_fdb_add = vxlan_fdb_add,
3157 .ndo_fdb_del = vxlan_fdb_delete,
3158 .ndo_fdb_dump = vxlan_fdb_dump,
3159 .ndo_fdb_get = vxlan_fdb_get,
3160 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3161 .ndo_change_proto_down = dev_change_proto_down_generic,
3164 static const struct net_device_ops vxlan_netdev_raw_ops = {
3165 .ndo_init = vxlan_init,
3166 .ndo_uninit = vxlan_uninit,
3167 .ndo_open = vxlan_open,
3168 .ndo_stop = vxlan_stop,
3169 .ndo_start_xmit = vxlan_xmit,
3170 .ndo_get_stats64 = ip_tunnel_get_stats64,
3171 .ndo_change_mtu = vxlan_change_mtu,
3172 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3175 /* Info for udev, that this is a virtual tunnel endpoint */
3176 static struct device_type vxlan_type = {
3180 /* Calls the ndo_udp_tunnel_add of the caller in order to
3181 * supply the listening VXLAN udp ports. Callers are expected
3182 * to implement the ndo_udp_tunnel_add.
3184 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3186 struct vxlan_sock *vs;
3187 struct net *net = dev_net(dev);
3188 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3191 spin_lock(&vn->sock_lock);
3192 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3193 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3194 unsigned short type;
3196 if (vs->flags & VXLAN_F_GPE)
3197 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3199 type = UDP_TUNNEL_TYPE_VXLAN;
3202 udp_tunnel_push_rx_port(dev, vs->sock, type);
3204 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3207 spin_unlock(&vn->sock_lock);
3210 /* Initialize the device structure. */
3211 static void vxlan_setup(struct net_device *dev)
3213 struct vxlan_dev *vxlan = netdev_priv(dev);
3216 eth_hw_addr_random(dev);
3219 dev->needs_free_netdev = true;
3220 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3222 dev->features |= NETIF_F_LLTX;
3223 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3224 dev->features |= NETIF_F_RXCSUM;
3225 dev->features |= NETIF_F_GSO_SOFTWARE;
3227 dev->vlan_features = dev->features;
3228 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3229 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3230 netif_keep_dst(dev);
3231 dev->priv_flags |= IFF_NO_QUEUE;
3233 /* MTU range: 68 - 65535 */
3234 dev->min_mtu = ETH_MIN_MTU;
3235 dev->max_mtu = ETH_MAX_MTU;
3237 INIT_LIST_HEAD(&vxlan->next);
3239 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3243 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3244 spin_lock_init(&vxlan->hash_lock[h]);
3245 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3249 static void vxlan_ether_setup(struct net_device *dev)
3251 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3252 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3253 dev->netdev_ops = &vxlan_netdev_ether_ops;
3256 static void vxlan_raw_setup(struct net_device *dev)
3258 dev->header_ops = NULL;
3259 dev->type = ARPHRD_NONE;
3260 dev->hard_header_len = 0;
3262 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3263 dev->netdev_ops = &vxlan_netdev_raw_ops;
3266 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3267 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3268 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
3269 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3270 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3271 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
3272 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3273 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3274 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3275 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3276 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3277 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3278 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3279 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3280 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3281 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3282 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3283 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3284 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3285 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3286 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3287 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3288 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3289 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3290 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3291 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3292 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3293 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3294 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3295 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3298 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3299 struct netlink_ext_ack *extack)
3301 if (tb[IFLA_ADDRESS]) {
3302 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3303 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3304 "Provided link layer address is not Ethernet");
3308 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3309 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3310 "Provided Ethernet address is not unicast");
3311 return -EADDRNOTAVAIL;
3316 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3318 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3319 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3320 "MTU must be between 68 and 65535");
3326 NL_SET_ERR_MSG(extack,
3327 "Required attributes not provided to perform the operation");
3331 if (data[IFLA_VXLAN_ID]) {
3332 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3334 if (id >= VXLAN_N_VID) {
3335 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3336 "VXLAN ID must be lower than 16777216");
3341 if (data[IFLA_VXLAN_PORT_RANGE]) {
3342 const struct ifla_vxlan_port_range *p
3343 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3345 if (ntohs(p->high) < ntohs(p->low)) {
3346 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3347 "Invalid source port range");
3352 if (data[IFLA_VXLAN_DF]) {
3353 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3355 if (df < 0 || df > VXLAN_DF_MAX) {
3356 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3357 "Invalid DF attribute");
3365 static void vxlan_get_drvinfo(struct net_device *netdev,
3366 struct ethtool_drvinfo *drvinfo)
3368 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3369 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3372 static int vxlan_get_link_ksettings(struct net_device *dev,
3373 struct ethtool_link_ksettings *cmd)
3375 struct vxlan_dev *vxlan = netdev_priv(dev);
3376 struct vxlan_rdst *dst = &vxlan->default_dst;
3377 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3378 dst->remote_ifindex);
3381 cmd->base.duplex = DUPLEX_UNKNOWN;
3382 cmd->base.port = PORT_OTHER;
3383 cmd->base.speed = SPEED_UNKNOWN;
3388 return __ethtool_get_link_ksettings(lowerdev, cmd);
3391 static const struct ethtool_ops vxlan_ethtool_ops = {
3392 .get_drvinfo = vxlan_get_drvinfo,
3393 .get_link = ethtool_op_get_link,
3394 .get_link_ksettings = vxlan_get_link_ksettings,
3397 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3398 __be16 port, u32 flags, int ifindex)
3400 struct socket *sock;
3401 struct udp_port_cfg udp_conf;
3404 memset(&udp_conf, 0, sizeof(udp_conf));
3407 udp_conf.family = AF_INET6;
3408 udp_conf.use_udp6_rx_checksums =
3409 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3410 udp_conf.ipv6_v6only = 1;
3412 udp_conf.family = AF_INET;
3415 udp_conf.local_udp_port = port;
3416 udp_conf.bind_ifindex = ifindex;
3418 /* Open UDP socket */
3419 err = udp_sock_create(net, &udp_conf, &sock);
3421 return ERR_PTR(err);
3426 /* Create new listen socket if needed */
3427 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3428 __be16 port, u32 flags,
3431 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3432 struct vxlan_sock *vs;
3433 struct socket *sock;
3435 struct udp_tunnel_sock_cfg tunnel_cfg;
3437 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3439 return ERR_PTR(-ENOMEM);
3441 for (h = 0; h < VNI_HASH_SIZE; ++h)
3442 INIT_HLIST_HEAD(&vs->vni_list[h]);
3444 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3447 return ERR_CAST(sock);
3451 refcount_set(&vs->refcnt, 1);
3452 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3454 spin_lock(&vn->sock_lock);
3455 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3456 udp_tunnel_notify_add_rx_port(sock,
3457 (vs->flags & VXLAN_F_GPE) ?
3458 UDP_TUNNEL_TYPE_VXLAN_GPE :
3459 UDP_TUNNEL_TYPE_VXLAN);
3460 spin_unlock(&vn->sock_lock);
3462 /* Mark socket as an encapsulation socket. */
3463 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3464 tunnel_cfg.sk_user_data = vs;
3465 tunnel_cfg.encap_type = 1;
3466 tunnel_cfg.encap_rcv = vxlan_rcv;
3467 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3468 tunnel_cfg.encap_destroy = NULL;
3469 tunnel_cfg.gro_receive = vxlan_gro_receive;
3470 tunnel_cfg.gro_complete = vxlan_gro_complete;
3472 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3477 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3479 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3480 struct vxlan_sock *vs = NULL;
3481 struct vxlan_dev_node *node;
3482 int l3mdev_index = 0;
3484 if (vxlan->cfg.remote_ifindex)
3485 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3486 vxlan->net, vxlan->cfg.remote_ifindex);
3488 if (!vxlan->cfg.no_share) {
3489 spin_lock(&vn->sock_lock);
3490 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3491 vxlan->cfg.dst_port, vxlan->cfg.flags,
3493 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3494 spin_unlock(&vn->sock_lock);
3497 spin_unlock(&vn->sock_lock);
3500 vs = vxlan_socket_create(vxlan->net, ipv6,
3501 vxlan->cfg.dst_port, vxlan->cfg.flags,
3505 #if IS_ENABLED(CONFIG_IPV6)
3507 rcu_assign_pointer(vxlan->vn6_sock, vs);
3508 node = &vxlan->hlist6;
3512 rcu_assign_pointer(vxlan->vn4_sock, vs);
3513 node = &vxlan->hlist4;
3515 vxlan_vs_add_dev(vs, vxlan, node);
3519 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3521 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3522 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3523 bool ipv4 = !ipv6 || metadata;
3526 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3527 #if IS_ENABLED(CONFIG_IPV6)
3528 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3530 ret = __vxlan_sock_add(vxlan, true);
3531 if (ret < 0 && ret != -EAFNOSUPPORT)
3536 ret = __vxlan_sock_add(vxlan, false);
3538 vxlan_sock_release(vxlan);
3542 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3543 struct net_device **lower,
3544 struct vxlan_dev *old,
3545 struct netlink_ext_ack *extack)
3547 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3548 struct vxlan_dev *tmp;
3549 bool use_ipv6 = false;
3551 if (conf->flags & VXLAN_F_GPE) {
3552 /* For now, allow GPE only together with
3553 * COLLECT_METADATA. This can be relaxed later; in such
3554 * case, the other side of the PtP link will have to be
3557 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3558 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3559 NL_SET_ERR_MSG(extack,
3560 "VXLAN GPE does not support this combination of attributes");
3565 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3566 /* Unless IPv6 is explicitly requested, assume IPv4 */
3567 conf->remote_ip.sa.sa_family = AF_INET;
3568 conf->saddr.sa.sa_family = AF_INET;
3569 } else if (!conf->remote_ip.sa.sa_family) {
3570 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3571 } else if (!conf->saddr.sa.sa_family) {
3572 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3575 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3576 NL_SET_ERR_MSG(extack,
3577 "Local and remote address must be from the same family");
3581 if (vxlan_addr_multicast(&conf->saddr)) {
3582 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3586 if (conf->saddr.sa.sa_family == AF_INET6) {
3587 if (!IS_ENABLED(CONFIG_IPV6)) {
3588 NL_SET_ERR_MSG(extack,
3589 "IPv6 support not enabled in the kernel");
3590 return -EPFNOSUPPORT;
3593 conf->flags |= VXLAN_F_IPV6;
3595 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3597 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3599 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3601 if (local_type & IPV6_ADDR_LINKLOCAL) {
3602 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3603 (remote_type != IPV6_ADDR_ANY)) {
3604 NL_SET_ERR_MSG(extack,
3605 "Invalid combination of local and remote address scopes");
3609 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3612 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3613 NL_SET_ERR_MSG(extack,
3614 "Invalid combination of local and remote address scopes");
3618 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3623 if (conf->label && !use_ipv6) {
3624 NL_SET_ERR_MSG(extack,
3625 "Label attribute only applies to IPv6 VXLAN devices");
3629 if (conf->remote_ifindex) {
3630 struct net_device *lowerdev;
3632 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3634 NL_SET_ERR_MSG(extack,
3635 "Invalid local interface, device not found");
3639 #if IS_ENABLED(CONFIG_IPV6)
3641 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3642 if (idev && idev->cnf.disable_ipv6) {
3643 NL_SET_ERR_MSG(extack,
3644 "IPv6 support disabled by administrator");
3652 if (vxlan_addr_multicast(&conf->remote_ip)) {
3653 NL_SET_ERR_MSG(extack,
3654 "Local interface required for multicast remote destination");
3659 #if IS_ENABLED(CONFIG_IPV6)
3660 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3661 NL_SET_ERR_MSG(extack,
3662 "Local interface required for link-local local/remote addresses");
3670 if (!conf->dst_port) {
3671 if (conf->flags & VXLAN_F_GPE)
3672 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3674 conf->dst_port = htons(vxlan_port);
3677 if (!conf->age_interval)
3678 conf->age_interval = FDB_AGE_DEFAULT;
3680 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3684 if (tmp->cfg.vni != conf->vni)
3686 if (tmp->cfg.dst_port != conf->dst_port)
3688 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3689 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3692 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3693 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3696 NL_SET_ERR_MSG(extack,
3697 "A VXLAN device with the specified VNI already exists");
3704 static void vxlan_config_apply(struct net_device *dev,
3705 struct vxlan_config *conf,
3706 struct net_device *lowerdev,
3707 struct net *src_net,
3710 struct vxlan_dev *vxlan = netdev_priv(dev);
3711 struct vxlan_rdst *dst = &vxlan->default_dst;
3712 unsigned short needed_headroom = ETH_HLEN;
3713 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3714 int max_mtu = ETH_MAX_MTU;
3717 if (conf->flags & VXLAN_F_GPE)
3718 vxlan_raw_setup(dev);
3720 vxlan_ether_setup(dev);
3723 dev->mtu = conf->mtu;
3725 vxlan->net = src_net;
3728 dst->remote_vni = conf->vni;
3730 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3733 dst->remote_ifindex = conf->remote_ifindex;
3735 dev->gso_max_size = lowerdev->gso_max_size;
3736 dev->gso_max_segs = lowerdev->gso_max_segs;
3738 needed_headroom = lowerdev->hard_header_len;
3740 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3742 if (max_mtu < ETH_MIN_MTU)
3743 max_mtu = ETH_MIN_MTU;
3745 if (!changelink && !conf->mtu)
3749 if (dev->mtu > max_mtu)
3752 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3753 needed_headroom += VXLAN6_HEADROOM;
3755 needed_headroom += VXLAN_HEADROOM;
3756 dev->needed_headroom = needed_headroom;
3758 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3761 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3762 struct vxlan_config *conf, bool changelink,
3763 struct netlink_ext_ack *extack)
3765 struct vxlan_dev *vxlan = netdev_priv(dev);
3766 struct net_device *lowerdev;
3769 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3773 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3778 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3779 struct vxlan_config *conf,
3780 struct netlink_ext_ack *extack)
3782 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3783 struct vxlan_dev *vxlan = netdev_priv(dev);
3784 struct net_device *remote_dev = NULL;
3785 struct vxlan_fdb *f = NULL;
3786 bool unregister = false;
3787 struct vxlan_rdst *dst;
3790 dst = &vxlan->default_dst;
3791 err = vxlan_dev_configure(net, dev, conf, false, extack);
3795 dev->ethtool_ops = &vxlan_ethtool_ops;
3797 /* create an fdb entry for a valid default destination */
3798 if (!vxlan_addr_any(&dst->remote_ip)) {
3799 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3801 NUD_REACHABLE | NUD_PERMANENT,
3802 vxlan->cfg.dst_port,
3805 dst->remote_ifindex,
3806 NTF_SELF, 0, &f, extack);
3811 err = register_netdevice(dev);
3816 if (dst->remote_ifindex) {
3817 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3821 err = netdev_upper_dev_link(remote_dev, dev, extack);
3826 err = rtnl_configure_link(dev, NULL);
3831 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3833 /* notify default fdb entry */
3834 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3835 RTM_NEWNEIGH, true, extack);
3837 vxlan_fdb_destroy(vxlan, f, false, false);
3839 netdev_upper_dev_unlink(remote_dev, dev);
3844 list_add(&vxlan->next, &vn->vxlan_list);
3846 dst->remote_dev = remote_dev;
3850 netdev_upper_dev_unlink(remote_dev, dev);
3852 /* unregister_netdevice() destroys the default FDB entry with deletion
3853 * notification. But the addition notification was not sent yet, so
3854 * destroy the entry by hand here.
3857 __vxlan_fdb_free(f);
3860 unregister_netdevice(dev);
3864 /* Set/clear flags based on attribute */
3865 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3866 int attrtype, unsigned long mask, bool changelink,
3867 bool changelink_supported,
3868 struct netlink_ext_ack *extack)
3870 unsigned long flags;
3875 if (changelink && !changelink_supported) {
3876 vxlan_flag_attr_error(attrtype, extack);
3880 if (vxlan_policy[attrtype].type == NLA_FLAG)
3881 flags = conf->flags | mask;
3882 else if (nla_get_u8(tb[attrtype]))
3883 flags = conf->flags | mask;
3885 flags = conf->flags & ~mask;
3887 conf->flags = flags;
3892 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3893 struct net_device *dev, struct vxlan_config *conf,
3894 bool changelink, struct netlink_ext_ack *extack)
3896 struct vxlan_dev *vxlan = netdev_priv(dev);
3899 memset(conf, 0, sizeof(*conf));
3901 /* if changelink operation, start with old existing cfg */
3903 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3905 if (data[IFLA_VXLAN_ID]) {
3906 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3908 if (changelink && (vni != conf->vni)) {
3909 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3912 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3915 if (data[IFLA_VXLAN_GROUP]) {
3916 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3917 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3921 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3922 conf->remote_ip.sa.sa_family = AF_INET;
3923 } else if (data[IFLA_VXLAN_GROUP6]) {
3924 if (!IS_ENABLED(CONFIG_IPV6)) {
3925 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3926 return -EPFNOSUPPORT;
3929 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3930 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3934 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3935 conf->remote_ip.sa.sa_family = AF_INET6;
3938 if (data[IFLA_VXLAN_LOCAL]) {
3939 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3940 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
3944 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3945 conf->saddr.sa.sa_family = AF_INET;
3946 } else if (data[IFLA_VXLAN_LOCAL6]) {
3947 if (!IS_ENABLED(CONFIG_IPV6)) {
3948 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
3949 return -EPFNOSUPPORT;
3952 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3953 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
3957 /* TODO: respect scope id */
3958 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3959 conf->saddr.sa.sa_family = AF_INET6;
3962 if (data[IFLA_VXLAN_LINK])
3963 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3965 if (data[IFLA_VXLAN_TOS])
3966 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3968 if (data[IFLA_VXLAN_TTL])
3969 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3971 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3972 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3973 VXLAN_F_TTL_INHERIT, changelink, false,
3980 if (data[IFLA_VXLAN_LABEL])
3981 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3982 IPV6_FLOWLABEL_MASK;
3984 if (data[IFLA_VXLAN_LEARNING]) {
3985 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3986 VXLAN_F_LEARN, changelink, true,
3990 } else if (!changelink) {
3991 /* default to learn on a new device */
3992 conf->flags |= VXLAN_F_LEARN;
3995 if (data[IFLA_VXLAN_AGEING])
3996 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3998 if (data[IFLA_VXLAN_PROXY]) {
3999 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4000 VXLAN_F_PROXY, changelink, false,
4006 if (data[IFLA_VXLAN_RSC]) {
4007 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4008 VXLAN_F_RSC, changelink, false,
4014 if (data[IFLA_VXLAN_L2MISS]) {
4015 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4016 VXLAN_F_L2MISS, changelink, false,
4022 if (data[IFLA_VXLAN_L3MISS]) {
4023 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4024 VXLAN_F_L3MISS, changelink, false,
4030 if (data[IFLA_VXLAN_LIMIT]) {
4032 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4033 "Cannot change limit");
4036 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4039 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
4040 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4041 VXLAN_F_COLLECT_METADATA, changelink, false,
4047 if (data[IFLA_VXLAN_PORT_RANGE]) {
4049 const struct ifla_vxlan_port_range *p
4050 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4051 conf->port_min = ntohs(p->low);
4052 conf->port_max = ntohs(p->high);
4054 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4055 "Cannot change port range");
4060 if (data[IFLA_VXLAN_PORT]) {
4062 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4063 "Cannot change port");
4066 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4069 if (data[IFLA_VXLAN_UDP_CSUM]) {
4071 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4072 "Cannot change UDP_CSUM flag");
4075 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4076 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4079 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4080 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4081 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4087 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4088 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4089 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4095 if (data[IFLA_VXLAN_REMCSUM_TX]) {
4096 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4097 VXLAN_F_REMCSUM_TX, changelink, false,
4103 if (data[IFLA_VXLAN_REMCSUM_RX]) {
4104 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4105 VXLAN_F_REMCSUM_RX, changelink, false,
4111 if (data[IFLA_VXLAN_GBP]) {
4112 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4113 VXLAN_F_GBP, changelink, false, extack);
4118 if (data[IFLA_VXLAN_GPE]) {
4119 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4120 VXLAN_F_GPE, changelink, false,
4126 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4127 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4128 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4136 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4137 "Cannot change mtu");
4140 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4143 if (data[IFLA_VXLAN_DF])
4144 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4149 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
4150 struct nlattr *tb[], struct nlattr *data[],
4151 struct netlink_ext_ack *extack)
4153 struct vxlan_config conf;
4156 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4160 return __vxlan_dev_create(src_net, dev, &conf, extack);
4163 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4164 struct nlattr *data[],
4165 struct netlink_ext_ack *extack)
4167 struct vxlan_dev *vxlan = netdev_priv(dev);
4168 struct net_device *lowerdev;
4169 struct vxlan_config conf;
4170 struct vxlan_rdst *dst;
4173 dst = &vxlan->default_dst;
4174 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4178 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4183 if (dst->remote_dev == lowerdev)
4186 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4191 /* handle default dst entry */
4192 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4193 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4195 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4196 if (!vxlan_addr_any(&conf.remote_ip)) {
4197 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4199 NUD_REACHABLE | NUD_PERMANENT,
4200 NLM_F_APPEND | NLM_F_CREATE,
4201 vxlan->cfg.dst_port,
4203 conf.remote_ifindex,
4204 NTF_SELF, 0, true, extack);
4206 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4207 netdev_adjacent_change_abort(dst->remote_dev,
4212 if (!vxlan_addr_any(&dst->remote_ip))
4213 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4215 vxlan->cfg.dst_port,
4218 dst->remote_ifindex,
4220 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4223 if (conf.age_interval != vxlan->cfg.age_interval)
4224 mod_timer(&vxlan->age_timer, jiffies);
4226 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4227 if (lowerdev && lowerdev != dst->remote_dev) {
4228 dst->remote_dev = lowerdev;
4229 netdev_update_lockdep_key(lowerdev);
4231 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4235 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4237 struct vxlan_dev *vxlan = netdev_priv(dev);
4239 vxlan_flush(vxlan, true);
4241 list_del(&vxlan->next);
4242 unregister_netdevice_queue(dev, head);
4243 if (vxlan->default_dst.remote_dev)
4244 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4247 static size_t vxlan_get_size(const struct net_device *dev)
4250 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4251 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4252 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4253 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4254 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4255 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4256 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4257 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4258 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4259 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4260 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4261 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4262 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4263 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4264 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4265 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4266 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4267 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4268 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4269 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4270 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4271 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4272 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4273 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4277 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4279 const struct vxlan_dev *vxlan = netdev_priv(dev);
4280 const struct vxlan_rdst *dst = &vxlan->default_dst;
4281 struct ifla_vxlan_port_range ports = {
4282 .low = htons(vxlan->cfg.port_min),
4283 .high = htons(vxlan->cfg.port_max),
4286 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4287 goto nla_put_failure;
4289 if (!vxlan_addr_any(&dst->remote_ip)) {
4290 if (dst->remote_ip.sa.sa_family == AF_INET) {
4291 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4292 dst->remote_ip.sin.sin_addr.s_addr))
4293 goto nla_put_failure;
4294 #if IS_ENABLED(CONFIG_IPV6)
4296 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4297 &dst->remote_ip.sin6.sin6_addr))
4298 goto nla_put_failure;
4303 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4304 goto nla_put_failure;
4306 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4307 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4308 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4309 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4310 goto nla_put_failure;
4311 #if IS_ENABLED(CONFIG_IPV6)
4313 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4314 &vxlan->cfg.saddr.sin6.sin6_addr))
4315 goto nla_put_failure;
4320 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4321 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4322 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4323 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4324 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4325 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4326 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4327 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4328 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4329 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4330 nla_put_u8(skb, IFLA_VXLAN_RSC,
4331 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4332 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4333 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4334 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4335 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4336 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4337 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4338 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4339 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4340 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4341 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4342 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4343 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4344 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4345 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4346 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4347 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4348 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4349 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4350 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4351 goto nla_put_failure;
4353 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4354 goto nla_put_failure;
4356 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4357 nla_put_flag(skb, IFLA_VXLAN_GBP))
4358 goto nla_put_failure;
4360 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4361 nla_put_flag(skb, IFLA_VXLAN_GPE))
4362 goto nla_put_failure;
4364 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4365 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4366 goto nla_put_failure;
4374 static struct net *vxlan_get_link_net(const struct net_device *dev)
4376 struct vxlan_dev *vxlan = netdev_priv(dev);
4381 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4383 .maxtype = IFLA_VXLAN_MAX,
4384 .policy = vxlan_policy,
4385 .priv_size = sizeof(struct vxlan_dev),
4386 .setup = vxlan_setup,
4387 .validate = vxlan_validate,
4388 .newlink = vxlan_newlink,
4389 .changelink = vxlan_changelink,
4390 .dellink = vxlan_dellink,
4391 .get_size = vxlan_get_size,
4392 .fill_info = vxlan_fill_info,
4393 .get_link_net = vxlan_get_link_net,
4396 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4397 u8 name_assign_type,
4398 struct vxlan_config *conf)
4400 struct nlattr *tb[IFLA_MAX + 1];
4401 struct net_device *dev;
4404 memset(&tb, 0, sizeof(tb));
4406 dev = rtnl_create_link(net, name, name_assign_type,
4407 &vxlan_link_ops, tb, NULL);
4411 err = __vxlan_dev_create(net, dev, conf, NULL);
4414 return ERR_PTR(err);
4417 err = rtnl_configure_link(dev, NULL);
4419 LIST_HEAD(list_kill);
4421 vxlan_dellink(dev, &list_kill);
4422 unregister_netdevice_many(&list_kill);
4423 return ERR_PTR(err);
4428 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4430 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4431 struct net_device *dev)
4433 struct vxlan_dev *vxlan, *next;
4434 LIST_HEAD(list_kill);
4436 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4437 struct vxlan_rdst *dst = &vxlan->default_dst;
4439 /* In case we created vxlan device with carrier
4440 * and we loose the carrier due to module unload
4441 * we also need to remove vxlan device. In other
4442 * cases, it's not necessary and remote_ifindex
4443 * is 0 here, so no matches.
4445 if (dst->remote_ifindex == dev->ifindex)
4446 vxlan_dellink(vxlan->dev, &list_kill);
4449 unregister_netdevice_many(&list_kill);
4452 static int vxlan_netdevice_event(struct notifier_block *unused,
4453 unsigned long event, void *ptr)
4455 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4456 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4458 if (event == NETDEV_UNREGISTER) {
4459 vxlan_offload_rx_ports(dev, false);
4460 vxlan_handle_lowerdev_unregister(vn, dev);
4461 } else if (event == NETDEV_REGISTER) {
4462 vxlan_offload_rx_ports(dev, true);
4463 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4464 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4465 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4471 static struct notifier_block vxlan_notifier_block __read_mostly = {
4472 .notifier_call = vxlan_netdevice_event,
4476 vxlan_fdb_offloaded_set(struct net_device *dev,
4477 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4479 struct vxlan_dev *vxlan = netdev_priv(dev);
4480 struct vxlan_rdst *rdst;
4481 struct vxlan_fdb *f;
4484 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4486 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4488 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4492 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4493 fdb_info->remote_port,
4494 fdb_info->remote_vni,
4495 fdb_info->remote_ifindex);
4499 rdst->offloaded = fdb_info->offloaded;
4502 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4506 vxlan_fdb_external_learn_add(struct net_device *dev,
4507 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4509 struct vxlan_dev *vxlan = netdev_priv(dev);
4510 struct netlink_ext_ack *extack;
4514 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4515 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4517 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4518 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4520 NLM_F_CREATE | NLM_F_REPLACE,
4521 fdb_info->remote_port,
4523 fdb_info->remote_vni,
4524 fdb_info->remote_ifindex,
4525 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4527 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4533 vxlan_fdb_external_learn_del(struct net_device *dev,
4534 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4536 struct vxlan_dev *vxlan = netdev_priv(dev);
4537 struct vxlan_fdb *f;
4541 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4542 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4544 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4547 else if (f->flags & NTF_EXT_LEARNED)
4548 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4549 fdb_info->remote_ip,
4550 fdb_info->remote_port,
4552 fdb_info->remote_vni,
4553 fdb_info->remote_ifindex,
4556 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4561 static int vxlan_switchdev_event(struct notifier_block *unused,
4562 unsigned long event, void *ptr)
4564 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4565 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4569 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4570 vxlan_fdb_offloaded_set(dev, ptr);
4572 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4574 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4576 err = notifier_from_errno(err);
4579 fdb_info->offloaded = true;
4580 vxlan_fdb_offloaded_set(dev, fdb_info);
4582 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4584 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4586 err = notifier_from_errno(err);
4589 fdb_info->offloaded = false;
4590 vxlan_fdb_offloaded_set(dev, fdb_info);
4597 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4598 .notifier_call = vxlan_switchdev_event,
4601 static __net_init int vxlan_init_net(struct net *net)
4603 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4606 INIT_LIST_HEAD(&vn->vxlan_list);
4607 spin_lock_init(&vn->sock_lock);
4609 for (h = 0; h < PORT_HASH_SIZE; ++h)
4610 INIT_HLIST_HEAD(&vn->sock_list[h]);
4615 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4617 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4618 struct vxlan_dev *vxlan, *next;
4619 struct net_device *dev, *aux;
4622 for_each_netdev_safe(net, dev, aux)
4623 if (dev->rtnl_link_ops == &vxlan_link_ops)
4624 unregister_netdevice_queue(dev, head);
4626 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4627 /* If vxlan->dev is in the same netns, it has already been added
4628 * to the list by the previous loop.
4630 if (!net_eq(dev_net(vxlan->dev), net))
4631 unregister_netdevice_queue(vxlan->dev, head);
4634 for (h = 0; h < PORT_HASH_SIZE; ++h)
4635 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4638 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4644 list_for_each_entry(net, net_list, exit_list)
4645 vxlan_destroy_tunnels(net, &list);
4647 unregister_netdevice_many(&list);
4651 static struct pernet_operations vxlan_net_ops = {
4652 .init = vxlan_init_net,
4653 .exit_batch = vxlan_exit_batch_net,
4654 .id = &vxlan_net_id,
4655 .size = sizeof(struct vxlan_net),
4658 static int __init vxlan_init_module(void)
4662 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4664 rc = register_pernet_subsys(&vxlan_net_ops);
4668 rc = register_netdevice_notifier(&vxlan_notifier_block);
4672 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4676 rc = rtnl_link_register(&vxlan_link_ops);
4682 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4684 unregister_netdevice_notifier(&vxlan_notifier_block);
4686 unregister_pernet_subsys(&vxlan_net_ops);
4690 late_initcall(vxlan_init_module);
4692 static void __exit vxlan_cleanup_module(void)
4694 rtnl_link_unregister(&vxlan_link_ops);
4695 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4696 unregister_netdevice_notifier(&vxlan_notifier_block);
4697 unregister_pernet_subsys(&vxlan_net_ops);
4698 /* rcu_barrier() is called by netns */
4700 module_exit(vxlan_cleanup_module);
4702 MODULE_LICENSE("GPL");
4703 MODULE_VERSION(VXLAN_VERSION);
4704 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4705 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4706 MODULE_ALIAS_RTNL_LINK("vxlan");