2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012-2013 Vyatta Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
31 #include <net/ndisc.h>
33 #include <net/ip_tunnels.h>
36 #include <net/udp_tunnel.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/vxlan.h>
44 #include <net/protocol.h>
45 #include <net/udp_tunnel.h>
46 #if IS_ENABLED(CONFIG_IPV6)
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
52 #include <net/dst_metadata.h>
54 #define VXLAN_VERSION "0.1"
56 #define PORT_HASH_BITS 8
57 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
61 /* UDP port for VXLAN traffic.
62 * The IANA assigned port is 4789, but the Linux default is 8472
63 * for compatibility with early adopters.
65 static unsigned short vxlan_port __read_mostly = 8472;
66 module_param_named(udp_port, vxlan_port, ushort, 0444);
67 MODULE_PARM_DESC(udp_port, "Destination UDP port");
69 static bool log_ecn_error = true;
70 module_param(log_ecn_error, bool, 0644);
71 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
73 static int vxlan_net_id;
74 static struct rtnl_link_ops vxlan_link_ops;
76 static const u8 all_zeros_mac[ETH_ALEN];
78 /* per-network namespace private data for this module */
80 struct list_head vxlan_list;
81 struct hlist_head sock_list[PORT_HASH_SIZE];
85 /* Forwarding table entry */
87 struct hlist_node hlist; /* linked list of entries */
89 unsigned long updated; /* jiffies */
91 struct list_head remotes;
92 u8 eth_addr[ETH_ALEN];
93 u16 state; /* see ndm_state */
94 u8 flags; /* see ndm_flags */
97 /* salt for hash table */
98 static u32 vxlan_salt __read_mostly;
99 static struct workqueue_struct *vxlan_wq;
101 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
103 return vs->flags & VXLAN_F_COLLECT_METADATA ||
104 ip_tunnel_collect_metadata();
107 #if IS_ENABLED(CONFIG_IPV6)
109 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
111 if (a->sa.sa_family != b->sa.sa_family)
113 if (a->sa.sa_family == AF_INET6)
114 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
116 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
119 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
121 if (ipa->sa.sa_family == AF_INET6)
122 return ipv6_addr_any(&ipa->sin6.sin6_addr);
124 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
127 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
129 if (ipa->sa.sa_family == AF_INET6)
130 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
132 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
135 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
137 if (nla_len(nla) >= sizeof(struct in6_addr)) {
138 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
139 ip->sa.sa_family = AF_INET6;
141 } else if (nla_len(nla) >= sizeof(__be32)) {
142 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
143 ip->sa.sa_family = AF_INET;
146 return -EAFNOSUPPORT;
150 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
151 const union vxlan_addr *ip)
153 if (ip->sa.sa_family == AF_INET6)
154 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
156 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
159 #else /* !CONFIG_IPV6 */
162 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
164 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
167 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
169 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
172 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
177 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 return -EAFNOSUPPORT;
181 } else if (nla_len(nla) >= sizeof(__be32)) {
182 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
183 ip->sa.sa_family = AF_INET;
186 return -EAFNOSUPPORT;
190 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
191 const union vxlan_addr *ip)
193 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
197 /* Virtual Network hash table head */
198 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
200 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
203 /* Socket hash table head */
204 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
206 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
208 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
211 /* First remote destination for a forwarding entry.
212 * Guaranteed to be non-NULL because remotes are never deleted.
214 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
216 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
219 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
221 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
224 /* Find VXLAN socket based on network namespace, address family and UDP port
225 * and enabled unshareable flags.
227 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
228 __be16 port, u32 flags)
230 struct vxlan_sock *vs;
232 flags &= VXLAN_F_RCV_FLAGS;
234 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
235 if (inet_sk(vs->sock->sk)->inet_sport == port &&
236 inet_sk(vs->sock->sk)->sk.sk_family == family &&
243 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
245 struct vxlan_dev *vxlan;
247 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
248 if (vxlan->default_dst.remote_vni == id)
255 /* Look up VNI in a per net namespace table */
256 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
257 sa_family_t family, __be16 port,
260 struct vxlan_sock *vs;
262 vs = vxlan_find_sock(net, family, port, flags);
266 return vxlan_vs_find_vni(vs, id);
269 /* Fill in neighbour message in skbuff. */
270 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
271 const struct vxlan_fdb *fdb,
272 u32 portid, u32 seq, int type, unsigned int flags,
273 const struct vxlan_rdst *rdst)
275 unsigned long now = jiffies;
276 struct nda_cacheinfo ci;
277 struct nlmsghdr *nlh;
279 bool send_ip, send_eth;
281 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
285 ndm = nlmsg_data(nlh);
286 memset(ndm, 0, sizeof(*ndm));
288 send_eth = send_ip = true;
290 if (type == RTM_GETNEIGH) {
291 ndm->ndm_family = AF_INET;
292 send_ip = !vxlan_addr_any(&rdst->remote_ip);
293 send_eth = !is_zero_ether_addr(fdb->eth_addr);
295 ndm->ndm_family = AF_BRIDGE;
296 ndm->ndm_state = fdb->state;
297 ndm->ndm_ifindex = vxlan->dev->ifindex;
298 ndm->ndm_flags = fdb->flags;
299 ndm->ndm_type = RTN_UNICAST;
301 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
302 nla_put_s32(skb, NDA_LINK_NETNSID,
303 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
304 goto nla_put_failure;
306 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
307 goto nla_put_failure;
309 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
310 goto nla_put_failure;
312 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
313 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
314 goto nla_put_failure;
315 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
316 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
317 goto nla_put_failure;
318 if (rdst->remote_ifindex &&
319 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
320 goto nla_put_failure;
322 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
323 ci.ndm_confirmed = 0;
324 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
327 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
328 goto nla_put_failure;
334 nlmsg_cancel(skb, nlh);
338 static inline size_t vxlan_nlmsg_size(void)
340 return NLMSG_ALIGN(sizeof(struct ndmsg))
341 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
342 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
343 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
344 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
345 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
346 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
347 + nla_total_size(sizeof(struct nda_cacheinfo));
350 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
351 struct vxlan_rdst *rd, int type)
353 struct net *net = dev_net(vxlan->dev);
357 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
361 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
363 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
364 WARN_ON(err == -EMSGSIZE);
369 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
373 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
376 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
378 struct vxlan_dev *vxlan = netdev_priv(dev);
379 struct vxlan_fdb f = {
382 struct vxlan_rdst remote = {
383 .remote_ip = *ipa, /* goes to NDA_DST */
384 .remote_vni = VXLAN_N_VID,
387 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
390 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
392 struct vxlan_fdb f = {
395 struct vxlan_rdst remote = { };
397 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
399 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
402 /* Hash Ethernet address */
403 static u32 eth_hash(const unsigned char *addr)
405 u64 value = get_unaligned((u64 *)addr);
407 /* only want 6 bytes */
413 return hash_64(value, FDB_HASH_BITS);
416 /* Hash chain to use given mac address */
417 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
420 return &vxlan->fdb_head[eth_hash(mac)];
423 /* Look up Ethernet address in forwarding table */
424 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
427 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
430 hlist_for_each_entry_rcu(f, head, hlist) {
431 if (ether_addr_equal(mac, f->eth_addr))
438 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
443 f = __vxlan_find_mac(vxlan, mac);
450 /* caller should hold vxlan->hash_lock */
451 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
452 union vxlan_addr *ip, __be16 port,
453 __u32 vni, __u32 ifindex)
455 struct vxlan_rdst *rd;
457 list_for_each_entry(rd, &f->remotes, list) {
458 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
459 rd->remote_port == port &&
460 rd->remote_vni == vni &&
461 rd->remote_ifindex == ifindex)
468 /* Replace destination of unicast mac */
469 static int vxlan_fdb_replace(struct vxlan_fdb *f,
470 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
472 struct vxlan_rdst *rd;
474 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
478 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
482 rd->remote_port = port;
483 rd->remote_vni = vni;
484 rd->remote_ifindex = ifindex;
488 /* Add/update destinations for multicast */
489 static int vxlan_fdb_append(struct vxlan_fdb *f,
490 union vxlan_addr *ip, __be16 port, __u32 vni,
491 __u32 ifindex, struct vxlan_rdst **rdp)
493 struct vxlan_rdst *rd;
495 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
499 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
503 rd->remote_port = port;
504 rd->remote_vni = vni;
505 rd->remote_ifindex = ifindex;
507 list_add_tail_rcu(&rd->list, &f->remotes);
513 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
515 struct vxlanhdr *vh, size_t hdrlen,
516 u32 data, struct gro_remcsum *grc,
519 size_t start, offset, plen;
521 if (skb->remcsum_offload)
524 if (!NAPI_GRO_CB(skb)->csum_valid)
527 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
528 offset = start + ((data & VXLAN_RCO_UDP) ?
529 offsetof(struct udphdr, check) :
530 offsetof(struct tcphdr, check));
532 plen = hdrlen + offset + sizeof(u16);
534 /* Pull checksum that will be written */
535 if (skb_gro_header_hard(skb, off + plen)) {
536 vh = skb_gro_header_slow(skb, off + plen, off);
541 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
542 start, offset, grc, nopartial);
544 skb->remcsum_offload = 1;
549 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
551 struct udp_offload *uoff)
553 struct sk_buff *p, **pp = NULL;
554 struct vxlanhdr *vh, *vh2;
555 unsigned int hlen, off_vx;
557 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
560 struct gro_remcsum grc;
562 skb_gro_remcsum_init(&grc);
564 off_vx = skb_gro_offset(skb);
565 hlen = off_vx + sizeof(*vh);
566 vh = skb_gro_header_fast(skb, off_vx);
567 if (skb_gro_header_hard(skb, hlen)) {
568 vh = skb_gro_header_slow(skb, hlen, off_vx);
573 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
574 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
576 flags = ntohl(vh->vx_flags);
578 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
579 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
580 ntohl(vh->vx_vni), &grc,
582 VXLAN_F_REMCSUM_NOPARTIAL));
590 for (p = *head; p; p = p->next) {
591 if (!NAPI_GRO_CB(p)->same_flow)
594 vh2 = (struct vxlanhdr *)(p->data + off_vx);
595 if (vh->vx_flags != vh2->vx_flags ||
596 vh->vx_vni != vh2->vx_vni) {
597 NAPI_GRO_CB(p)->same_flow = 0;
602 pp = eth_gro_receive(head, skb);
605 skb_gro_remcsum_cleanup(skb, &grc);
606 NAPI_GRO_CB(skb)->flush |= flush;
611 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
612 struct udp_offload *uoff)
614 udp_tunnel_gro_complete(skb, nhoff);
616 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
619 /* Notify netdevs that UDP port started listening */
620 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
622 struct net_device *dev;
623 struct sock *sk = vs->sock->sk;
624 struct net *net = sock_net(sk);
625 sa_family_t sa_family = sk->sk_family;
626 __be16 port = inet_sk(sk)->inet_sport;
629 if (sa_family == AF_INET) {
630 err = udp_add_offload(&vs->udp_offloads);
632 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
636 for_each_netdev_rcu(net, dev) {
637 if (dev->netdev_ops->ndo_add_vxlan_port)
638 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
644 /* Notify netdevs that UDP port is no more listening */
645 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
647 struct net_device *dev;
648 struct sock *sk = vs->sock->sk;
649 struct net *net = sock_net(sk);
650 sa_family_t sa_family = sk->sk_family;
651 __be16 port = inet_sk(sk)->inet_sport;
654 for_each_netdev_rcu(net, dev) {
655 if (dev->netdev_ops->ndo_del_vxlan_port)
656 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
661 if (sa_family == AF_INET)
662 udp_del_offload(&vs->udp_offloads);
665 /* Add new entry to forwarding table -- assumes lock held */
666 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
667 const u8 *mac, union vxlan_addr *ip,
668 __u16 state, __u16 flags,
669 __be16 port, __u32 vni, __u32 ifindex,
672 struct vxlan_rdst *rd = NULL;
676 f = __vxlan_find_mac(vxlan, mac);
678 if (flags & NLM_F_EXCL) {
679 netdev_dbg(vxlan->dev,
680 "lost race to create %pM\n", mac);
683 if (f->state != state) {
685 f->updated = jiffies;
688 if (f->flags != ndm_flags) {
689 f->flags = ndm_flags;
690 f->updated = jiffies;
693 if ((flags & NLM_F_REPLACE)) {
694 /* Only change unicasts */
695 if (!(is_multicast_ether_addr(f->eth_addr) ||
696 is_zero_ether_addr(f->eth_addr))) {
697 notify |= vxlan_fdb_replace(f, ip, port, vni,
702 if ((flags & NLM_F_APPEND) &&
703 (is_multicast_ether_addr(f->eth_addr) ||
704 is_zero_ether_addr(f->eth_addr))) {
705 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
713 if (!(flags & NLM_F_CREATE))
716 if (vxlan->cfg.addrmax &&
717 vxlan->addrcnt >= vxlan->cfg.addrmax)
720 /* Disallow replace to add a multicast entry */
721 if ((flags & NLM_F_REPLACE) &&
722 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
725 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
726 f = kmalloc(sizeof(*f), GFP_ATOMIC);
732 f->flags = ndm_flags;
733 f->updated = f->used = jiffies;
734 INIT_LIST_HEAD(&f->remotes);
735 memcpy(f->eth_addr, mac, ETH_ALEN);
737 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
740 hlist_add_head_rcu(&f->hlist,
741 vxlan_fdb_head(vxlan, mac));
746 rd = first_remote_rtnl(f);
747 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
753 static void vxlan_fdb_free(struct rcu_head *head)
755 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
756 struct vxlan_rdst *rd, *nd;
758 list_for_each_entry_safe(rd, nd, &f->remotes, list)
763 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
765 netdev_dbg(vxlan->dev,
766 "delete %pM\n", f->eth_addr);
769 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
771 hlist_del_rcu(&f->hlist);
772 call_rcu(&f->rcu, vxlan_fdb_free);
775 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
776 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
778 struct net *net = dev_net(vxlan->dev);
782 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
786 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
787 if (remote->sa.sa_family == AF_INET) {
788 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
789 ip->sa.sa_family = AF_INET;
790 #if IS_ENABLED(CONFIG_IPV6)
792 ip->sin6.sin6_addr = in6addr_any;
793 ip->sa.sa_family = AF_INET6;
799 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
801 *port = nla_get_be16(tb[NDA_PORT]);
803 *port = vxlan->cfg.dst_port;
807 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
809 *vni = nla_get_u32(tb[NDA_VNI]);
811 *vni = vxlan->default_dst.remote_vni;
814 if (tb[NDA_IFINDEX]) {
815 struct net_device *tdev;
817 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
819 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
820 tdev = __dev_get_by_index(net, *ifindex);
822 return -EADDRNOTAVAIL;
830 /* Add static entry (via netlink) */
831 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
832 struct net_device *dev,
833 const unsigned char *addr, u16 vid, u16 flags)
835 struct vxlan_dev *vxlan = netdev_priv(dev);
836 /* struct net *net = dev_net(vxlan->dev); */
842 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
843 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
848 if (tb[NDA_DST] == NULL)
851 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
855 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
856 return -EAFNOSUPPORT;
858 spin_lock_bh(&vxlan->hash_lock);
859 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
860 port, vni, ifindex, ndm->ndm_flags);
861 spin_unlock_bh(&vxlan->hash_lock);
866 /* Delete entry (via netlink) */
867 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
868 struct net_device *dev,
869 const unsigned char *addr, u16 vid)
871 struct vxlan_dev *vxlan = netdev_priv(dev);
873 struct vxlan_rdst *rd = NULL;
879 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
885 spin_lock_bh(&vxlan->hash_lock);
886 f = vxlan_find_mac(vxlan, addr);
890 if (!vxlan_addr_any(&ip)) {
891 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
898 /* remove a destination if it's not the only one on the list,
899 * otherwise destroy the fdb entry
901 if (rd && !list_is_singular(&f->remotes)) {
902 list_del_rcu(&rd->list);
903 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
908 vxlan_fdb_destroy(vxlan, f);
911 spin_unlock_bh(&vxlan->hash_lock);
916 /* Dump forwarding table */
917 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
918 struct net_device *dev,
919 struct net_device *filter_dev, int idx)
921 struct vxlan_dev *vxlan = netdev_priv(dev);
924 for (h = 0; h < FDB_HASH_SIZE; ++h) {
928 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
929 struct vxlan_rdst *rd;
931 if (idx < cb->args[0])
934 list_for_each_entry_rcu(rd, &f->remotes, list) {
935 err = vxlan_fdb_info(skb, vxlan, f,
936 NETLINK_CB(cb->skb).portid,
951 /* Watch incoming packets to learn mapping between Ethernet address
952 * and Tunnel endpoint.
953 * Return true if packet is bogus and should be dropped.
955 static bool vxlan_snoop(struct net_device *dev,
956 union vxlan_addr *src_ip, const u8 *src_mac)
958 struct vxlan_dev *vxlan = netdev_priv(dev);
961 f = vxlan_find_mac(vxlan, src_mac);
963 struct vxlan_rdst *rdst = first_remote_rcu(f);
965 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
968 /* Don't migrate static entries, drop packets */
969 if (f->state & NUD_NOARP)
974 "%pM migrated from %pIS to %pIS\n",
975 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
977 rdst->remote_ip = *src_ip;
978 f->updated = jiffies;
979 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
981 /* learned new entry */
982 spin_lock(&vxlan->hash_lock);
984 /* close off race between vxlan_flush and incoming packets */
985 if (netif_running(dev))
986 vxlan_fdb_create(vxlan, src_mac, src_ip,
988 NLM_F_EXCL|NLM_F_CREATE,
990 vxlan->default_dst.remote_vni,
992 spin_unlock(&vxlan->hash_lock);
998 /* See if multicast group is already in use by other ID */
999 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1001 struct vxlan_dev *vxlan;
1003 /* The vxlan_sock is only used by dev, leaving group has
1004 * no effect on other vxlan devices.
1006 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1009 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1010 if (!netif_running(vxlan->dev) || vxlan == dev)
1013 if (vxlan->vn_sock != dev->vn_sock)
1016 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1017 &dev->default_dst.remote_ip))
1020 if (vxlan->default_dst.remote_ifindex !=
1021 dev->default_dst.remote_ifindex)
1030 void vxlan_sock_release(struct vxlan_sock *vs)
1032 struct sock *sk = vs->sock->sk;
1033 struct net *net = sock_net(sk);
1034 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1036 if (!atomic_dec_and_test(&vs->refcnt))
1039 spin_lock(&vn->sock_lock);
1040 hlist_del_rcu(&vs->hlist);
1041 vxlan_notify_del_rx_port(vs);
1042 spin_unlock(&vn->sock_lock);
1044 queue_work(vxlan_wq, &vs->del_work);
1046 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1048 /* Update multicast group membership when first VNI on
1049 * multicast address is brought up
1051 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1053 struct vxlan_sock *vs = vxlan->vn_sock;
1054 struct sock *sk = vs->sock->sk;
1055 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1056 int ifindex = vxlan->default_dst.remote_ifindex;
1060 if (ip->sa.sa_family == AF_INET) {
1061 struct ip_mreqn mreq = {
1062 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1063 .imr_ifindex = ifindex,
1066 ret = ip_mc_join_group(sk, &mreq);
1067 #if IS_ENABLED(CONFIG_IPV6)
1069 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1070 &ip->sin6.sin6_addr);
1078 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1079 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1081 struct vxlan_sock *vs = vxlan->vn_sock;
1082 struct sock *sk = vs->sock->sk;
1083 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1084 int ifindex = vxlan->default_dst.remote_ifindex;
1088 if (ip->sa.sa_family == AF_INET) {
1089 struct ip_mreqn mreq = {
1090 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1091 .imr_ifindex = ifindex,
1094 ret = ip_mc_leave_group(sk, &mreq);
1095 #if IS_ENABLED(CONFIG_IPV6)
1097 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1098 &ip->sin6.sin6_addr);
1106 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1107 size_t hdrlen, u32 data, bool nopartial)
1109 size_t start, offset, plen;
1111 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1112 offset = start + ((data & VXLAN_RCO_UDP) ?
1113 offsetof(struct udphdr, check) :
1114 offsetof(struct tcphdr, check));
1116 plen = hdrlen + offset + sizeof(u16);
1118 if (!pskb_may_pull(skb, plen))
1121 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1123 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1129 /* Callback from net/ipv4/udp.c to receive packets */
1130 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1132 struct metadata_dst *tun_dst = NULL;
1133 struct ip_tunnel_info *info;
1134 struct vxlan_sock *vs;
1135 struct vxlanhdr *vxh;
1137 struct vxlan_metadata _md;
1138 struct vxlan_metadata *md = &_md;
1140 /* Need Vxlan and inner Ethernet header to be present */
1141 if (!pskb_may_pull(skb, VXLAN_HLEN))
1144 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1145 flags = ntohl(vxh->vx_flags);
1146 vni = ntohl(vxh->vx_vni);
1148 if (flags & VXLAN_HF_VNI) {
1149 flags &= ~VXLAN_HF_VNI;
1151 /* VNI flag always required to be set */
1155 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1157 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1159 vs = rcu_dereference_sk_user_data(sk);
1163 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1164 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1165 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
1169 flags &= ~VXLAN_HF_RCO;
1170 vni &= VXLAN_VNI_MASK;
1173 if (vxlan_collect_metadata(vs)) {
1174 const struct iphdr *iph = ip_hdr(skb);
1176 tun_dst = metadata_dst_alloc(sizeof(*md), GFP_ATOMIC);
1180 info = &tun_dst->u.tun_info;
1181 info->key.ipv4_src = iph->saddr;
1182 info->key.ipv4_dst = iph->daddr;
1183 info->key.ipv4_tos = iph->tos;
1184 info->key.ipv4_ttl = iph->ttl;
1185 info->key.tp_src = udp_hdr(skb)->source;
1186 info->key.tp_dst = udp_hdr(skb)->dest;
1188 info->mode = IP_TUNNEL_INFO_RX;
1189 info->key.tun_flags = TUNNEL_KEY;
1190 info->key.tun_id = cpu_to_be64(vni >> 8);
1191 if (udp_hdr(skb)->check != 0)
1192 info->key.tun_flags |= TUNNEL_CSUM;
1194 md = ip_tunnel_info_opts(info, sizeof(*md));
1195 md->tun_dst = tun_dst;
1197 memset(md, 0, sizeof(*md));
1200 /* For backwards compatibility, only allow reserved fields to be
1201 * used by VXLAN extensions if explicitly requested.
1203 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1204 struct vxlanhdr_gbp *gbp;
1206 gbp = (struct vxlanhdr_gbp *)vxh;
1207 md->gbp = ntohs(gbp->policy_id);
1210 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
1212 if (gbp->dont_learn)
1213 md->gbp |= VXLAN_GBP_DONT_LEARN;
1215 if (gbp->policy_applied)
1216 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1218 flags &= ~VXLAN_GBP_USED_BITS;
1221 if (flags || vni & ~VXLAN_VNI_MASK) {
1222 /* If there are any unprocessed flags remaining treat
1223 * this as a malformed packet. This behavior diverges from
1224 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1225 * in reserved fields are to be ignored. The approach here
1226 * maintains compatibility with previous stack code, and also
1227 * is more robust and provides a little more security in
1228 * adding extensions to VXLAN.
1234 md->vni = vxh->vx_vni;
1235 vs->rcv(vs, skb, md);
1239 /* Consume bad packet */
1244 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1245 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1249 dst_release((struct dst_entry *)tun_dst);
1251 /* Return non vxlan pkt */
1255 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1256 struct vxlan_metadata *md)
1258 struct iphdr *oip = NULL;
1259 struct ipv6hdr *oip6 = NULL;
1260 struct vxlan_dev *vxlan;
1261 struct pcpu_sw_netstats *stats;
1262 union vxlan_addr saddr;
1265 union vxlan_addr *remote_ip;
1267 /* For flow based devices, map all packets to VNI 0 */
1268 if (vs->flags & VXLAN_F_FLOW_BASED)
1271 vni = ntohl(md->vni) >> 8;
1273 /* Is this VNI defined? */
1274 vxlan = vxlan_vs_find_vni(vs, vni);
1278 remote_ip = &vxlan->default_dst.remote_ip;
1279 skb_reset_mac_header(skb);
1280 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1281 skb->protocol = eth_type_trans(skb, vxlan->dev);
1282 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1284 /* Ignore packet loops (and multicast echo) */
1285 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1288 /* Re-examine inner Ethernet packet */
1289 if (remote_ip->sa.sa_family == AF_INET) {
1291 saddr.sin.sin_addr.s_addr = oip->saddr;
1292 saddr.sa.sa_family = AF_INET;
1293 #if IS_ENABLED(CONFIG_IPV6)
1295 oip6 = ipv6_hdr(skb);
1296 saddr.sin6.sin6_addr = oip6->saddr;
1297 saddr.sa.sa_family = AF_INET6;
1302 skb_dst_set(skb, (struct dst_entry *)md->tun_dst);
1306 if ((vxlan->flags & VXLAN_F_LEARN) &&
1307 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1310 skb_reset_network_header(skb);
1311 /* In flow-based mode, GBP is carried in dst_metadata */
1312 if (!(vs->flags & VXLAN_F_FLOW_BASED))
1313 skb->mark = md->gbp;
1316 err = IP6_ECN_decapsulate(oip6, skb);
1318 err = IP_ECN_decapsulate(oip, skb);
1320 if (unlikely(err)) {
1321 if (log_ecn_error) {
1323 net_info_ratelimited("non-ECT from %pI6\n",
1326 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1327 &oip->saddr, oip->tos);
1330 ++vxlan->dev->stats.rx_frame_errors;
1331 ++vxlan->dev->stats.rx_errors;
1336 stats = this_cpu_ptr(vxlan->dev->tstats);
1337 u64_stats_update_begin(&stats->syncp);
1338 stats->rx_packets++;
1339 stats->rx_bytes += skb->len;
1340 u64_stats_update_end(&stats->syncp);
1347 dst_release((struct dst_entry *)md->tun_dst);
1349 /* Consume bad packet */
1353 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1355 struct vxlan_dev *vxlan = netdev_priv(dev);
1356 struct arphdr *parp;
1359 struct neighbour *n;
1361 if (dev->flags & IFF_NOARP)
1364 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1365 dev->stats.tx_dropped++;
1368 parp = arp_hdr(skb);
1370 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1371 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1372 parp->ar_pro != htons(ETH_P_IP) ||
1373 parp->ar_op != htons(ARPOP_REQUEST) ||
1374 parp->ar_hln != dev->addr_len ||
1377 arpptr = (u8 *)parp + sizeof(struct arphdr);
1379 arpptr += dev->addr_len; /* sha */
1380 memcpy(&sip, arpptr, sizeof(sip));
1381 arpptr += sizeof(sip);
1382 arpptr += dev->addr_len; /* tha */
1383 memcpy(&tip, arpptr, sizeof(tip));
1385 if (ipv4_is_loopback(tip) ||
1386 ipv4_is_multicast(tip))
1389 n = neigh_lookup(&arp_tbl, &tip, dev);
1392 struct vxlan_fdb *f;
1393 struct sk_buff *reply;
1395 if (!(n->nud_state & NUD_CONNECTED)) {
1400 f = vxlan_find_mac(vxlan, n->ha);
1401 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1402 /* bridge-local neighbor */
1407 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1415 skb_reset_mac_header(reply);
1416 __skb_pull(reply, skb_network_offset(reply));
1417 reply->ip_summed = CHECKSUM_UNNECESSARY;
1418 reply->pkt_type = PACKET_HOST;
1420 if (netif_rx_ni(reply) == NET_RX_DROP)
1421 dev->stats.rx_dropped++;
1422 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1423 union vxlan_addr ipa = {
1424 .sin.sin_addr.s_addr = tip,
1425 .sin.sin_family = AF_INET,
1428 vxlan_ip_miss(dev, &ipa);
1432 return NETDEV_TX_OK;
1435 #if IS_ENABLED(CONFIG_IPV6)
1436 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1437 struct neighbour *n, bool isrouter)
1439 struct net_device *dev = request->dev;
1440 struct sk_buff *reply;
1441 struct nd_msg *ns, *na;
1442 struct ipv6hdr *pip6;
1444 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1451 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1452 sizeof(*na) + na_olen + dev->needed_tailroom;
1453 reply = alloc_skb(len, GFP_ATOMIC);
1457 reply->protocol = htons(ETH_P_IPV6);
1459 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1460 skb_push(reply, sizeof(struct ethhdr));
1461 skb_set_mac_header(reply, 0);
1463 ns = (struct nd_msg *)skb_transport_header(request);
1465 daddr = eth_hdr(request)->h_source;
1466 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1467 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1468 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1469 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1474 /* Ethernet header */
1475 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1476 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1477 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1478 reply->protocol = htons(ETH_P_IPV6);
1480 skb_pull(reply, sizeof(struct ethhdr));
1481 skb_set_network_header(reply, 0);
1482 skb_put(reply, sizeof(struct ipv6hdr));
1486 pip6 = ipv6_hdr(reply);
1487 memset(pip6, 0, sizeof(struct ipv6hdr));
1489 pip6->priority = ipv6_hdr(request)->priority;
1490 pip6->nexthdr = IPPROTO_ICMPV6;
1491 pip6->hop_limit = 255;
1492 pip6->daddr = ipv6_hdr(request)->saddr;
1493 pip6->saddr = *(struct in6_addr *)n->primary_key;
1495 skb_pull(reply, sizeof(struct ipv6hdr));
1496 skb_set_transport_header(reply, 0);
1498 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1500 /* Neighbor Advertisement */
1501 memset(na, 0, sizeof(*na)+na_olen);
1502 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1503 na->icmph.icmp6_router = isrouter;
1504 na->icmph.icmp6_override = 1;
1505 na->icmph.icmp6_solicited = 1;
1506 na->target = ns->target;
1507 ether_addr_copy(&na->opt[2], n->ha);
1508 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1509 na->opt[1] = na_olen >> 3;
1511 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1512 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1513 csum_partial(na, sizeof(*na)+na_olen, 0));
1515 pip6->payload_len = htons(sizeof(*na)+na_olen);
1517 skb_push(reply, sizeof(struct ipv6hdr));
1519 reply->ip_summed = CHECKSUM_UNNECESSARY;
1524 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1526 struct vxlan_dev *vxlan = netdev_priv(dev);
1528 const struct ipv6hdr *iphdr;
1529 const struct in6_addr *saddr, *daddr;
1530 struct neighbour *n;
1531 struct inet6_dev *in6_dev;
1533 in6_dev = __in6_dev_get(dev);
1537 iphdr = ipv6_hdr(skb);
1538 saddr = &iphdr->saddr;
1539 daddr = &iphdr->daddr;
1541 msg = (struct nd_msg *)skb_transport_header(skb);
1542 if (msg->icmph.icmp6_code != 0 ||
1543 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1546 if (ipv6_addr_loopback(daddr) ||
1547 ipv6_addr_is_multicast(&msg->target))
1550 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1553 struct vxlan_fdb *f;
1554 struct sk_buff *reply;
1556 if (!(n->nud_state & NUD_CONNECTED)) {
1561 f = vxlan_find_mac(vxlan, n->ha);
1562 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1563 /* bridge-local neighbor */
1568 reply = vxlan_na_create(skb, n,
1569 !!(f ? f->flags & NTF_ROUTER : 0));
1576 if (netif_rx_ni(reply) == NET_RX_DROP)
1577 dev->stats.rx_dropped++;
1579 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1580 union vxlan_addr ipa = {
1581 .sin6.sin6_addr = msg->target,
1582 .sin6.sin6_family = AF_INET6,
1585 vxlan_ip_miss(dev, &ipa);
1590 return NETDEV_TX_OK;
1594 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1596 struct vxlan_dev *vxlan = netdev_priv(dev);
1597 struct neighbour *n;
1599 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1603 switch (ntohs(eth_hdr(skb)->h_proto)) {
1608 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1611 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1612 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1613 union vxlan_addr ipa = {
1614 .sin.sin_addr.s_addr = pip->daddr,
1615 .sin.sin_family = AF_INET,
1618 vxlan_ip_miss(dev, &ipa);
1624 #if IS_ENABLED(CONFIG_IPV6)
1627 struct ipv6hdr *pip6;
1629 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1631 pip6 = ipv6_hdr(skb);
1632 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1633 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1634 union vxlan_addr ipa = {
1635 .sin6.sin6_addr = pip6->daddr,
1636 .sin6.sin6_family = AF_INET6,
1639 vxlan_ip_miss(dev, &ipa);
1653 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1655 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1657 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1666 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1667 struct vxlan_metadata *md)
1669 struct vxlanhdr_gbp *gbp;
1674 gbp = (struct vxlanhdr_gbp *)vxh;
1675 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1677 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1678 gbp->dont_learn = 1;
1680 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1681 gbp->policy_applied = 1;
1683 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1686 #if IS_ENABLED(CONFIG_IPV6)
1687 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1688 struct sk_buff *skb,
1689 struct net_device *dev, struct in6_addr *saddr,
1690 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1691 __be16 src_port, __be16 dst_port,
1692 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1694 struct vxlanhdr *vxh;
1697 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1698 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1699 u16 hdrlen = sizeof(struct vxlanhdr);
1701 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1702 skb->ip_summed == CHECKSUM_PARTIAL) {
1703 int csum_start = skb_checksum_start_offset(skb);
1705 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1706 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1707 (skb->csum_offset == offsetof(struct udphdr, check) ||
1708 skb->csum_offset == offsetof(struct tcphdr, check))) {
1710 type |= SKB_GSO_TUNNEL_REMCSUM;
1714 skb_scrub_packet(skb, xnet);
1716 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1717 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1718 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1720 /* Need space for new headers (invalidates iph ptr) */
1721 err = skb_cow_head(skb, min_headroom);
1722 if (unlikely(err)) {
1727 skb = vlan_hwaccel_push_inside(skb);
1728 if (WARN_ON(!skb)) {
1733 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1739 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1740 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1741 vxh->vx_vni = md->vni;
1743 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1744 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1747 if (skb->csum_offset == offsetof(struct udphdr, check))
1748 data |= VXLAN_RCO_UDP;
1750 vxh->vx_vni |= htonl(data);
1751 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1753 if (!skb_is_gso(skb)) {
1754 skb->ip_summed = CHECKSUM_NONE;
1755 skb->encapsulation = 0;
1759 if (vxflags & VXLAN_F_GBP)
1760 vxlan_build_gbp_hdr(vxh, vxflags, md);
1762 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1764 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
1765 ttl, src_port, dst_port,
1766 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1774 int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
1775 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1776 __be16 src_port, __be16 dst_port,
1777 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1779 struct vxlanhdr *vxh;
1782 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1783 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1784 u16 hdrlen = sizeof(struct vxlanhdr);
1786 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1787 skb->ip_summed == CHECKSUM_PARTIAL) {
1788 int csum_start = skb_checksum_start_offset(skb);
1790 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1791 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1792 (skb->csum_offset == offsetof(struct udphdr, check) ||
1793 skb->csum_offset == offsetof(struct tcphdr, check))) {
1795 type |= SKB_GSO_TUNNEL_REMCSUM;
1799 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1800 + VXLAN_HLEN + sizeof(struct iphdr)
1801 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1803 /* Need space for new headers (invalidates iph ptr) */
1804 err = skb_cow_head(skb, min_headroom);
1805 if (unlikely(err)) {
1810 skb = vlan_hwaccel_push_inside(skb);
1814 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1816 return PTR_ERR(skb);
1818 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1819 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1820 vxh->vx_vni = md->vni;
1822 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1823 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1826 if (skb->csum_offset == offsetof(struct udphdr, check))
1827 data |= VXLAN_RCO_UDP;
1829 vxh->vx_vni |= htonl(data);
1830 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1832 if (!skb_is_gso(skb)) {
1833 skb->ip_summed = CHECKSUM_NONE;
1834 skb->encapsulation = 0;
1838 if (vxflags & VXLAN_F_GBP)
1839 vxlan_build_gbp_hdr(vxh, vxflags, md);
1841 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1843 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
1844 ttl, df, src_port, dst_port, xnet,
1845 !(vxflags & VXLAN_F_UDP_CSUM));
1847 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1849 /* Bypass encapsulation if the destination is local */
1850 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1851 struct vxlan_dev *dst_vxlan)
1853 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1854 union vxlan_addr loopback;
1855 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1856 struct net_device *dev = skb->dev;
1859 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1860 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1861 skb->pkt_type = PACKET_HOST;
1862 skb->encapsulation = 0;
1863 skb->dev = dst_vxlan->dev;
1864 __skb_pull(skb, skb_network_offset(skb));
1866 if (remote_ip->sa.sa_family == AF_INET) {
1867 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1868 loopback.sa.sa_family = AF_INET;
1869 #if IS_ENABLED(CONFIG_IPV6)
1871 loopback.sin6.sin6_addr = in6addr_loopback;
1872 loopback.sa.sa_family = AF_INET6;
1876 if (dst_vxlan->flags & VXLAN_F_LEARN)
1877 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1879 u64_stats_update_begin(&tx_stats->syncp);
1880 tx_stats->tx_packets++;
1881 tx_stats->tx_bytes += len;
1882 u64_stats_update_end(&tx_stats->syncp);
1884 if (netif_rx(skb) == NET_RX_SUCCESS) {
1885 u64_stats_update_begin(&rx_stats->syncp);
1886 rx_stats->rx_packets++;
1887 rx_stats->rx_bytes += len;
1888 u64_stats_update_end(&rx_stats->syncp);
1890 dev->stats.rx_dropped++;
1894 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1895 struct vxlan_rdst *rdst, bool did_rsc)
1897 struct ip_tunnel_info *info;
1898 struct vxlan_dev *vxlan = netdev_priv(dev);
1899 struct sock *sk = vxlan->vn_sock->sock->sk;
1900 struct rtable *rt = NULL;
1901 const struct iphdr *old_iph;
1903 union vxlan_addr *dst;
1904 union vxlan_addr remote_ip;
1905 struct vxlan_metadata _md;
1906 struct vxlan_metadata *md = &_md;
1907 __be16 src_port = 0, dst_port;
1912 u32 flags = vxlan->flags;
1914 /* FIXME: Support IPv6 */
1915 info = skb_tunnel_info(skb, AF_INET);
1918 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1919 vni = rdst->remote_vni;
1920 dst = &rdst->remote_ip;
1923 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1928 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
1929 vni = be64_to_cpu(info->key.tun_id);
1930 remote_ip.sin.sin_family = AF_INET;
1931 remote_ip.sin.sin_addr.s_addr = info->key.ipv4_dst;
1935 if (vxlan_addr_any(dst)) {
1937 /* short-circuited back to local bridge */
1938 vxlan_encap_bypass(skb, vxlan, vxlan);
1944 old_iph = ip_hdr(skb);
1946 ttl = vxlan->cfg.ttl;
1947 if (!ttl && vxlan_addr_multicast(dst))
1950 tos = vxlan->cfg.tos;
1952 tos = ip_tunnel_get_dsfield(old_iph, skb);
1954 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1955 vxlan->cfg.port_max, true);
1957 if (dst->sa.sa_family == AF_INET) {
1959 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
1961 if (info->key.tun_flags & TUNNEL_CSUM)
1962 flags |= VXLAN_F_UDP_CSUM;
1964 flags &= ~VXLAN_F_UDP_CSUM;
1966 ttl = info->key.ipv4_ttl;
1967 tos = info->key.ipv4_tos;
1969 if (info->options_len)
1970 md = ip_tunnel_info_opts(info, sizeof(*md));
1972 md->gbp = skb->mark;
1975 memset(&fl4, 0, sizeof(fl4));
1976 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
1977 fl4.flowi4_tos = RT_TOS(tos);
1978 fl4.flowi4_mark = skb->mark;
1979 fl4.flowi4_proto = IPPROTO_UDP;
1980 fl4.daddr = dst->sin.sin_addr.s_addr;
1981 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1983 rt = ip_route_output_key(vxlan->net, &fl4);
1985 netdev_dbg(dev, "no route to %pI4\n",
1986 &dst->sin.sin_addr.s_addr);
1987 dev->stats.tx_carrier_errors++;
1991 if (rt->dst.dev == dev) {
1992 netdev_dbg(dev, "circular route to %pI4\n",
1993 &dst->sin.sin_addr.s_addr);
1994 dev->stats.collisions++;
1998 /* Bypass encapsulation if the destination is local */
1999 if (rt->rt_flags & RTCF_LOCAL &&
2000 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2001 struct vxlan_dev *dst_vxlan;
2004 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2005 dst->sa.sa_family, dst_port,
2009 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2013 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2014 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2015 md->vni = htonl(vni << 8);
2016 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
2017 dst->sin.sin_addr.s_addr, tos, ttl, df,
2018 src_port, dst_port, md,
2019 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2022 /* skb is already freed. */
2027 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
2028 #if IS_ENABLED(CONFIG_IPV6)
2030 struct dst_entry *ndst;
2034 memset(&fl6, 0, sizeof(fl6));
2035 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
2036 fl6.daddr = dst->sin6.sin6_addr;
2037 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
2038 fl6.flowi6_mark = skb->mark;
2039 fl6.flowi6_proto = IPPROTO_UDP;
2041 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2042 netdev_dbg(dev, "no route to %pI6\n",
2043 &dst->sin6.sin6_addr);
2044 dev->stats.tx_carrier_errors++;
2048 if (ndst->dev == dev) {
2049 netdev_dbg(dev, "circular route to %pI6\n",
2050 &dst->sin6.sin6_addr);
2052 dev->stats.collisions++;
2056 /* Bypass encapsulation if the destination is local */
2057 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2058 if (flags & RTF_LOCAL &&
2059 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2060 struct vxlan_dev *dst_vxlan;
2063 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2064 dst->sa.sa_family, dst_port,
2068 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2072 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2073 md->vni = htonl(vni << 8);
2074 md->gbp = skb->mark;
2076 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
2077 0, ttl, src_port, dst_port, md,
2078 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2086 dev->stats.tx_dropped++;
2092 dev->stats.tx_errors++;
2097 /* Transmit local packets over Vxlan
2099 * Outer IP header inherits ECN and DF from inner header.
2100 * Outer UDP destination is the VXLAN assigned port.
2101 * source port is based on hash of flow
2103 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2105 struct vxlan_dev *vxlan = netdev_priv(dev);
2106 const struct ip_tunnel_info *info;
2108 bool did_rsc = false;
2109 struct vxlan_rdst *rdst, *fdst = NULL;
2110 struct vxlan_fdb *f;
2112 /* FIXME: Support IPv6 */
2113 info = skb_tunnel_info(skb, AF_INET);
2115 skb_reset_mac_header(skb);
2118 if ((vxlan->flags & VXLAN_F_PROXY)) {
2119 if (ntohs(eth->h_proto) == ETH_P_ARP)
2120 return arp_reduce(dev, skb);
2121 #if IS_ENABLED(CONFIG_IPV6)
2122 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2123 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2124 + sizeof(struct nd_msg)) &&
2125 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2128 msg = (struct nd_msg *)skb_transport_header(skb);
2129 if (msg->icmph.icmp6_code == 0 &&
2130 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2131 return neigh_reduce(dev, skb);
2137 if (vxlan->flags & VXLAN_F_FLOW_BASED &&
2138 info && info->mode == IP_TUNNEL_INFO_TX) {
2139 vxlan_xmit_one(skb, dev, NULL, false);
2140 return NETDEV_TX_OK;
2143 f = vxlan_find_mac(vxlan, eth->h_dest);
2146 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2147 (ntohs(eth->h_proto) == ETH_P_IP ||
2148 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2149 did_rsc = route_shortcircuit(dev, skb);
2151 f = vxlan_find_mac(vxlan, eth->h_dest);
2155 f = vxlan_find_mac(vxlan, all_zeros_mac);
2157 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2158 !is_multicast_ether_addr(eth->h_dest))
2159 vxlan_fdb_miss(vxlan, eth->h_dest);
2161 dev->stats.tx_dropped++;
2163 return NETDEV_TX_OK;
2167 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2168 struct sk_buff *skb1;
2174 skb1 = skb_clone(skb, GFP_ATOMIC);
2176 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2180 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2183 return NETDEV_TX_OK;
2186 /* Walk the forwarding table and purge stale entries */
2187 static void vxlan_cleanup(unsigned long arg)
2189 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2190 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2193 if (!netif_running(vxlan->dev))
2196 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2197 struct hlist_node *p, *n;
2199 spin_lock_bh(&vxlan->hash_lock);
2200 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2202 = container_of(p, struct vxlan_fdb, hlist);
2203 unsigned long timeout;
2205 if (f->state & NUD_PERMANENT)
2208 timeout = f->used + vxlan->cfg.age_interval * HZ;
2209 if (time_before_eq(timeout, jiffies)) {
2210 netdev_dbg(vxlan->dev,
2211 "garbage collect %pM\n",
2213 f->state = NUD_STALE;
2214 vxlan_fdb_destroy(vxlan, f);
2215 } else if (time_before(timeout, next_timer))
2216 next_timer = timeout;
2218 spin_unlock_bh(&vxlan->hash_lock);
2221 mod_timer(&vxlan->age_timer, next_timer);
2224 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2226 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2227 __u32 vni = vxlan->default_dst.remote_vni;
2229 vxlan->vn_sock = vs;
2230 spin_lock(&vn->sock_lock);
2231 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2232 spin_unlock(&vn->sock_lock);
2235 /* Setup stats when device is created */
2236 static int vxlan_init(struct net_device *dev)
2238 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2245 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2247 struct vxlan_fdb *f;
2249 spin_lock_bh(&vxlan->hash_lock);
2250 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2252 vxlan_fdb_destroy(vxlan, f);
2253 spin_unlock_bh(&vxlan->hash_lock);
2256 static void vxlan_uninit(struct net_device *dev)
2258 struct vxlan_dev *vxlan = netdev_priv(dev);
2260 vxlan_fdb_delete_default(vxlan);
2262 free_percpu(dev->tstats);
2265 /* Start ageing timer and join group when device is brought up */
2266 static int vxlan_open(struct net_device *dev)
2268 struct vxlan_dev *vxlan = netdev_priv(dev);
2269 struct vxlan_sock *vs;
2272 vs = vxlan_sock_add(vxlan->net, vxlan->cfg.dst_port, vxlan_rcv,
2273 NULL, vxlan->cfg.no_share, vxlan->flags);
2277 vxlan_vs_add_dev(vs, vxlan);
2279 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2280 ret = vxlan_igmp_join(vxlan);
2282 vxlan_sock_release(vs);
2287 if (vxlan->cfg.age_interval)
2288 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2293 /* Purge the forwarding table */
2294 static void vxlan_flush(struct vxlan_dev *vxlan)
2298 spin_lock_bh(&vxlan->hash_lock);
2299 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2300 struct hlist_node *p, *n;
2301 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2303 = container_of(p, struct vxlan_fdb, hlist);
2304 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2305 if (!is_zero_ether_addr(f->eth_addr))
2306 vxlan_fdb_destroy(vxlan, f);
2309 spin_unlock_bh(&vxlan->hash_lock);
2312 /* Cleanup timer and forwarding table on shutdown */
2313 static int vxlan_stop(struct net_device *dev)
2315 struct vxlan_dev *vxlan = netdev_priv(dev);
2316 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2317 struct vxlan_sock *vs = vxlan->vn_sock;
2320 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2321 !vxlan_group_used(vn, vxlan))
2322 ret = vxlan_igmp_leave(vxlan);
2324 del_timer_sync(&vxlan->age_timer);
2327 vxlan_sock_release(vs);
2332 /* Stub, nothing needs to be done. */
2333 static void vxlan_set_multicast_list(struct net_device *dev)
2337 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2339 struct vxlan_dev *vxlan = netdev_priv(dev);
2340 struct vxlan_rdst *dst = &vxlan->default_dst;
2341 struct net_device *lowerdev;
2344 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2345 if (lowerdev == NULL)
2346 return eth_change_mtu(dev, new_mtu);
2348 if (dst->remote_ip.sa.sa_family == AF_INET6)
2349 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2351 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2353 if (new_mtu < 68 || new_mtu > max_mtu)
2360 static const struct net_device_ops vxlan_netdev_ops = {
2361 .ndo_init = vxlan_init,
2362 .ndo_uninit = vxlan_uninit,
2363 .ndo_open = vxlan_open,
2364 .ndo_stop = vxlan_stop,
2365 .ndo_start_xmit = vxlan_xmit,
2366 .ndo_get_stats64 = ip_tunnel_get_stats64,
2367 .ndo_set_rx_mode = vxlan_set_multicast_list,
2368 .ndo_change_mtu = vxlan_change_mtu,
2369 .ndo_validate_addr = eth_validate_addr,
2370 .ndo_set_mac_address = eth_mac_addr,
2371 .ndo_fdb_add = vxlan_fdb_add,
2372 .ndo_fdb_del = vxlan_fdb_delete,
2373 .ndo_fdb_dump = vxlan_fdb_dump,
2376 /* Info for udev, that this is a virtual tunnel endpoint */
2377 static struct device_type vxlan_type = {
2381 /* Calls the ndo_add_vxlan_port of the caller in order to
2382 * supply the listening VXLAN udp ports. Callers are expected
2383 * to implement the ndo_add_vxlan_port.
2385 void vxlan_get_rx_port(struct net_device *dev)
2387 struct vxlan_sock *vs;
2388 struct net *net = dev_net(dev);
2389 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2390 sa_family_t sa_family;
2394 spin_lock(&vn->sock_lock);
2395 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2396 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2397 port = inet_sk(vs->sock->sk)->inet_sport;
2398 sa_family = vs->sock->sk->sk_family;
2399 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2403 spin_unlock(&vn->sock_lock);
2405 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2407 /* Initialize the device structure. */
2408 static void vxlan_setup(struct net_device *dev)
2410 struct vxlan_dev *vxlan = netdev_priv(dev);
2413 eth_hw_addr_random(dev);
2415 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2416 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2418 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2420 dev->netdev_ops = &vxlan_netdev_ops;
2421 dev->destructor = free_netdev;
2422 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2424 dev->tx_queue_len = 0;
2425 dev->features |= NETIF_F_LLTX;
2426 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2427 dev->features |= NETIF_F_RXCSUM;
2428 dev->features |= NETIF_F_GSO_SOFTWARE;
2430 dev->vlan_features = dev->features;
2431 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2432 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2433 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2434 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2435 netif_keep_dst(dev);
2436 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2438 INIT_LIST_HEAD(&vxlan->next);
2439 spin_lock_init(&vxlan->hash_lock);
2441 init_timer_deferrable(&vxlan->age_timer);
2442 vxlan->age_timer.function = vxlan_cleanup;
2443 vxlan->age_timer.data = (unsigned long) vxlan;
2445 vxlan->cfg.dst_port = htons(vxlan_port);
2449 for (h = 0; h < FDB_HASH_SIZE; ++h)
2450 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2453 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2454 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2455 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2456 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2457 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2458 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2459 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2460 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2461 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2462 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2463 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2464 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2465 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2466 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2467 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2468 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2469 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2470 [IFLA_VXLAN_FLOWBASED] = { .type = NLA_U8 },
2471 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2472 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2473 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2474 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2475 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2476 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2477 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2478 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
2481 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2483 if (tb[IFLA_ADDRESS]) {
2484 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2485 pr_debug("invalid link address (not ethernet)\n");
2489 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2490 pr_debug("invalid all zero ethernet address\n");
2491 return -EADDRNOTAVAIL;
2498 if (data[IFLA_VXLAN_ID]) {
2499 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2500 if (id >= VXLAN_VID_MASK)
2504 if (data[IFLA_VXLAN_PORT_RANGE]) {
2505 const struct ifla_vxlan_port_range *p
2506 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2508 if (ntohs(p->high) < ntohs(p->low)) {
2509 pr_debug("port range %u .. %u not valid\n",
2510 ntohs(p->low), ntohs(p->high));
2518 static void vxlan_get_drvinfo(struct net_device *netdev,
2519 struct ethtool_drvinfo *drvinfo)
2521 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2522 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2525 static const struct ethtool_ops vxlan_ethtool_ops = {
2526 .get_drvinfo = vxlan_get_drvinfo,
2527 .get_link = ethtool_op_get_link,
2530 static void vxlan_del_work(struct work_struct *work)
2532 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2533 udp_tunnel_sock_release(vs->sock);
2537 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2538 __be16 port, u32 flags)
2540 struct socket *sock;
2541 struct udp_port_cfg udp_conf;
2544 memset(&udp_conf, 0, sizeof(udp_conf));
2547 udp_conf.family = AF_INET6;
2548 udp_conf.use_udp6_rx_checksums =
2549 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2551 udp_conf.family = AF_INET;
2554 udp_conf.local_udp_port = port;
2556 /* Open UDP socket */
2557 err = udp_sock_create(net, &udp_conf, &sock);
2559 return ERR_PTR(err);
2564 /* Create new listen socket if needed */
2565 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2566 vxlan_rcv_t *rcv, void *data,
2569 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2570 struct vxlan_sock *vs;
2571 struct socket *sock;
2573 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2574 struct udp_tunnel_sock_cfg tunnel_cfg;
2576 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2578 return ERR_PTR(-ENOMEM);
2580 for (h = 0; h < VNI_HASH_SIZE; ++h)
2581 INIT_HLIST_HEAD(&vs->vni_list[h]);
2583 INIT_WORK(&vs->del_work, vxlan_del_work);
2585 sock = vxlan_create_sock(net, ipv6, port, flags);
2587 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2590 return ERR_CAST(sock);
2594 atomic_set(&vs->refcnt, 1);
2597 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2599 /* Initialize the vxlan udp offloads structure */
2600 vs->udp_offloads.port = port;
2601 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2602 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2604 spin_lock(&vn->sock_lock);
2605 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2606 vxlan_notify_add_rx_port(vs);
2607 spin_unlock(&vn->sock_lock);
2609 /* Mark socket as an encapsulation socket. */
2610 tunnel_cfg.sk_user_data = vs;
2611 tunnel_cfg.encap_type = 1;
2612 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2613 tunnel_cfg.encap_destroy = NULL;
2615 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2620 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2621 vxlan_rcv_t *rcv, void *data,
2622 bool no_share, u32 flags)
2624 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2625 struct vxlan_sock *vs;
2626 bool ipv6 = flags & VXLAN_F_IPV6;
2629 spin_lock(&vn->sock_lock);
2630 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2632 if (vs && vs->rcv == rcv) {
2633 if (!atomic_add_unless(&vs->refcnt, 1, 0))
2634 vs = ERR_PTR(-EBUSY);
2635 spin_unlock(&vn->sock_lock);
2638 spin_unlock(&vn->sock_lock);
2641 return vxlan_socket_create(net, port, rcv, data, flags);
2643 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2645 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2646 struct vxlan_config *conf)
2648 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2649 struct vxlan_dev *vxlan = netdev_priv(dev);
2650 struct vxlan_rdst *dst = &vxlan->default_dst;
2652 bool use_ipv6 = false;
2653 __be16 default_port = vxlan->cfg.dst_port;
2655 vxlan->net = src_net;
2657 dst->remote_vni = conf->vni;
2659 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
2661 /* Unless IPv6 is explicitly requested, assume IPv4 */
2662 if (!dst->remote_ip.sa.sa_family)
2663 dst->remote_ip.sa.sa_family = AF_INET;
2665 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
2666 vxlan->cfg.saddr.sa.sa_family == AF_INET6)
2669 if (conf->remote_ifindex) {
2670 struct net_device *lowerdev
2671 = __dev_get_by_index(src_net, conf->remote_ifindex);
2673 dst->remote_ifindex = conf->remote_ifindex;
2676 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2680 #if IS_ENABLED(CONFIG_IPV6)
2682 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2683 if (idev && idev->cnf.disable_ipv6) {
2684 pr_info("IPv6 is disabled via sysctl\n");
2687 vxlan->flags |= VXLAN_F_IPV6;
2692 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2694 dev->needed_headroom = lowerdev->hard_header_len +
2695 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2696 } else if (use_ipv6)
2697 vxlan->flags |= VXLAN_F_IPV6;
2699 memcpy(&vxlan->cfg, conf, sizeof(*conf));
2700 if (!vxlan->cfg.dst_port)
2701 vxlan->cfg.dst_port = default_port;
2702 vxlan->flags |= conf->flags;
2704 if (!vxlan->cfg.age_interval)
2705 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
2707 if (vxlan_find_vni(src_net, conf->vni, use_ipv6 ? AF_INET6 : AF_INET,
2708 vxlan->cfg.dst_port, vxlan->flags))
2711 dev->ethtool_ops = &vxlan_ethtool_ops;
2713 /* create an fdb entry for a valid default destination */
2714 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2715 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2716 &vxlan->default_dst.remote_ip,
2717 NUD_REACHABLE|NUD_PERMANENT,
2718 NLM_F_EXCL|NLM_F_CREATE,
2719 vxlan->cfg.dst_port,
2720 vxlan->default_dst.remote_vni,
2721 vxlan->default_dst.remote_ifindex,
2727 err = register_netdevice(dev);
2729 vxlan_fdb_delete_default(vxlan);
2733 list_add(&vxlan->next, &vn->vxlan_list);
2738 struct net_device *vxlan_dev_create(struct net *net, const char *name,
2739 u8 name_assign_type, struct vxlan_config *conf)
2741 struct nlattr *tb[IFLA_MAX+1];
2742 struct net_device *dev;
2745 memset(&tb, 0, sizeof(tb));
2747 dev = rtnl_create_link(net, name, name_assign_type,
2748 &vxlan_link_ops, tb);
2752 err = vxlan_dev_configure(net, dev, conf);
2755 return ERR_PTR(err);
2760 EXPORT_SYMBOL_GPL(vxlan_dev_create);
2762 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2763 struct nlattr *tb[], struct nlattr *data[])
2765 struct vxlan_config conf;
2768 if (!data[IFLA_VXLAN_ID])
2771 memset(&conf, 0, sizeof(conf));
2772 conf.vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2774 if (data[IFLA_VXLAN_GROUP]) {
2775 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2776 } else if (data[IFLA_VXLAN_GROUP6]) {
2777 if (!IS_ENABLED(CONFIG_IPV6))
2778 return -EPFNOSUPPORT;
2780 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2781 conf.remote_ip.sa.sa_family = AF_INET6;
2784 if (data[IFLA_VXLAN_LOCAL]) {
2785 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2786 conf.saddr.sa.sa_family = AF_INET;
2787 } else if (data[IFLA_VXLAN_LOCAL6]) {
2788 if (!IS_ENABLED(CONFIG_IPV6))
2789 return -EPFNOSUPPORT;
2791 /* TODO: respect scope id */
2792 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2793 conf.saddr.sa.sa_family = AF_INET6;
2796 if (data[IFLA_VXLAN_LINK])
2797 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
2799 if (data[IFLA_VXLAN_TOS])
2800 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2802 if (data[IFLA_VXLAN_TTL])
2803 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2805 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2806 conf.flags |= VXLAN_F_LEARN;
2808 if (data[IFLA_VXLAN_AGEING])
2809 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2811 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2812 conf.flags |= VXLAN_F_PROXY;
2814 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2815 conf.flags |= VXLAN_F_RSC;
2817 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2818 conf.flags |= VXLAN_F_L2MISS;
2820 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2821 conf.flags |= VXLAN_F_L3MISS;
2823 if (data[IFLA_VXLAN_LIMIT])
2824 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2826 if (data[IFLA_VXLAN_FLOWBASED] &&
2827 nla_get_u8(data[IFLA_VXLAN_FLOWBASED]))
2828 conf.flags |= VXLAN_F_FLOW_BASED;
2830 if (data[IFLA_VXLAN_PORT_RANGE]) {
2831 const struct ifla_vxlan_port_range *p
2832 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2833 conf.port_min = ntohs(p->low);
2834 conf.port_max = ntohs(p->high);
2837 if (data[IFLA_VXLAN_PORT])
2838 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2840 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2841 conf.flags |= VXLAN_F_UDP_CSUM;
2843 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2844 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2845 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2847 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2848 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2849 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2851 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2852 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2853 conf.flags |= VXLAN_F_REMCSUM_TX;
2855 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2856 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2857 conf.flags |= VXLAN_F_REMCSUM_RX;
2859 if (data[IFLA_VXLAN_GBP])
2860 conf.flags |= VXLAN_F_GBP;
2862 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2863 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2865 err = vxlan_dev_configure(src_net, dev, &conf);
2868 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
2872 pr_info("IPv6 is disabled via sysctl\n");
2876 pr_info("duplicate VNI %u\n", conf.vni);
2883 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2885 struct vxlan_dev *vxlan = netdev_priv(dev);
2886 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2888 spin_lock(&vn->sock_lock);
2889 if (!hlist_unhashed(&vxlan->hlist))
2890 hlist_del_rcu(&vxlan->hlist);
2891 spin_unlock(&vn->sock_lock);
2893 list_del(&vxlan->next);
2894 unregister_netdevice_queue(dev, head);
2897 static size_t vxlan_get_size(const struct net_device *dev)
2900 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2901 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2902 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2903 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2904 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2905 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2906 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2907 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2908 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2909 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2910 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2911 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_FLOWBASED */
2912 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2913 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2914 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2915 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2916 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2917 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2918 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2919 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2920 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2924 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2926 const struct vxlan_dev *vxlan = netdev_priv(dev);
2927 const struct vxlan_rdst *dst = &vxlan->default_dst;
2928 struct ifla_vxlan_port_range ports = {
2929 .low = htons(vxlan->cfg.port_min),
2930 .high = htons(vxlan->cfg.port_max),
2933 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2934 goto nla_put_failure;
2936 if (!vxlan_addr_any(&dst->remote_ip)) {
2937 if (dst->remote_ip.sa.sa_family == AF_INET) {
2938 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2939 dst->remote_ip.sin.sin_addr.s_addr))
2940 goto nla_put_failure;
2941 #if IS_ENABLED(CONFIG_IPV6)
2943 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2944 &dst->remote_ip.sin6.sin6_addr))
2945 goto nla_put_failure;
2950 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2951 goto nla_put_failure;
2953 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
2954 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
2955 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2956 vxlan->cfg.saddr.sin.sin_addr.s_addr))
2957 goto nla_put_failure;
2958 #if IS_ENABLED(CONFIG_IPV6)
2960 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2961 &vxlan->cfg.saddr.sin6.sin6_addr))
2962 goto nla_put_failure;
2967 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
2968 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
2969 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2970 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2971 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2972 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2973 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2974 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2975 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2976 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2977 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2978 nla_put_u8(skb, IFLA_VXLAN_FLOWBASED,
2979 !!(vxlan->flags & VXLAN_F_FLOW_BASED)) ||
2980 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
2981 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
2982 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
2983 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2984 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2985 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2986 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2987 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2988 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2989 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2990 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2991 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2992 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2993 goto nla_put_failure;
2995 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2996 goto nla_put_failure;
2998 if (vxlan->flags & VXLAN_F_GBP &&
2999 nla_put_flag(skb, IFLA_VXLAN_GBP))
3000 goto nla_put_failure;
3002 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3003 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3004 goto nla_put_failure;
3012 static struct net *vxlan_get_link_net(const struct net_device *dev)
3014 struct vxlan_dev *vxlan = netdev_priv(dev);
3019 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3021 .maxtype = IFLA_VXLAN_MAX,
3022 .policy = vxlan_policy,
3023 .priv_size = sizeof(struct vxlan_dev),
3024 .setup = vxlan_setup,
3025 .validate = vxlan_validate,
3026 .newlink = vxlan_newlink,
3027 .dellink = vxlan_dellink,
3028 .get_size = vxlan_get_size,
3029 .fill_info = vxlan_fill_info,
3030 .get_link_net = vxlan_get_link_net,
3033 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3034 struct net_device *dev)
3036 struct vxlan_dev *vxlan, *next;
3037 LIST_HEAD(list_kill);
3039 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3040 struct vxlan_rdst *dst = &vxlan->default_dst;
3042 /* In case we created vxlan device with carrier
3043 * and we loose the carrier due to module unload
3044 * we also need to remove vxlan device. In other
3045 * cases, it's not necessary and remote_ifindex
3046 * is 0 here, so no matches.
3048 if (dst->remote_ifindex == dev->ifindex)
3049 vxlan_dellink(vxlan->dev, &list_kill);
3052 unregister_netdevice_many(&list_kill);
3055 static int vxlan_lowerdev_event(struct notifier_block *unused,
3056 unsigned long event, void *ptr)
3058 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3059 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3061 if (event == NETDEV_UNREGISTER)
3062 vxlan_handle_lowerdev_unregister(vn, dev);
3067 static struct notifier_block vxlan_notifier_block __read_mostly = {
3068 .notifier_call = vxlan_lowerdev_event,
3071 static __net_init int vxlan_init_net(struct net *net)
3073 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3076 INIT_LIST_HEAD(&vn->vxlan_list);
3077 spin_lock_init(&vn->sock_lock);
3079 for (h = 0; h < PORT_HASH_SIZE; ++h)
3080 INIT_HLIST_HEAD(&vn->sock_list[h]);
3085 static void __net_exit vxlan_exit_net(struct net *net)
3087 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3088 struct vxlan_dev *vxlan, *next;
3089 struct net_device *dev, *aux;
3093 for_each_netdev_safe(net, dev, aux)
3094 if (dev->rtnl_link_ops == &vxlan_link_ops)
3095 unregister_netdevice_queue(dev, &list);
3097 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3098 /* If vxlan->dev is in the same netns, it has already been added
3099 * to the list by the previous loop.
3101 if (!net_eq(dev_net(vxlan->dev), net))
3102 unregister_netdevice_queue(vxlan->dev, &list);
3105 unregister_netdevice_many(&list);
3109 static struct pernet_operations vxlan_net_ops = {
3110 .init = vxlan_init_net,
3111 .exit = vxlan_exit_net,
3112 .id = &vxlan_net_id,
3113 .size = sizeof(struct vxlan_net),
3116 static int __init vxlan_init_module(void)
3120 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3124 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3126 rc = register_pernet_subsys(&vxlan_net_ops);
3130 rc = register_netdevice_notifier(&vxlan_notifier_block);
3134 rc = rtnl_link_register(&vxlan_link_ops);
3140 unregister_netdevice_notifier(&vxlan_notifier_block);
3142 unregister_pernet_subsys(&vxlan_net_ops);
3144 destroy_workqueue(vxlan_wq);
3147 late_initcall(vxlan_init_module);
3149 static void __exit vxlan_cleanup_module(void)
3151 rtnl_link_unregister(&vxlan_link_ops);
3152 unregister_netdevice_notifier(&vxlan_notifier_block);
3153 destroy_workqueue(vxlan_wq);
3154 unregister_pernet_subsys(&vxlan_net_ops);
3155 /* rcu_barrier() is called by netns */
3157 module_exit(vxlan_cleanup_module);
3159 MODULE_LICENSE("GPL");
3160 MODULE_VERSION(VXLAN_VERSION);
3161 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3162 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3163 MODULE_ALIAS_RTNL_LINK("vxlan");