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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/rculist.h>
23 #include <linux/netdevice.h>
26 #include <linux/udp.h>
27 #include <linux/igmp.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/hash.h>
31 #include <linux/ethtool.h>
33 #include <net/ndisc.h>
35 #include <net/ip_tunnels.h>
38 #include <net/rtnetlink.h>
39 #include <net/route.h>
40 #include <net/dsfield.h>
41 #include <net/inet_ecn.h>
42 #include <net/net_namespace.h>
43 #include <net/netns/generic.h>
45 #define VXLAN_VERSION "0.1"
47 #define PORT_HASH_BITS 8
48 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
49 #define VNI_HASH_BITS 10
50 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
51 #define FDB_HASH_BITS 8
52 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
53 #define FDB_AGE_DEFAULT 300 /* 5 min */
54 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
56 #define VXLAN_N_VID (1u << 24)
57 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
58 /* IP header + UDP + VXLAN + Ethernet header */
59 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
60 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
62 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
64 /* VXLAN protocol header */
70 /* UDP port for VXLAN traffic.
71 * The IANA assigned port is 4789, but the Linux default is 8472
72 * for compatibility with early adopters.
74 static unsigned short vxlan_port __read_mostly = 8472;
75 module_param_named(udp_port, vxlan_port, ushort, 0444);
76 MODULE_PARM_DESC(udp_port, "Destination UDP port");
78 static bool log_ecn_error = true;
79 module_param(log_ecn_error, bool, 0644);
80 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
82 static int vxlan_net_id;
84 static const u8 all_zeros_mac[ETH_ALEN];
87 typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb, __be32 key);
89 /* per UDP socket information */
92 struct hlist_node hlist;
94 struct work_struct del_work;
97 struct hlist_head vni_list[VNI_HASH_SIZE];
100 /* per-network namespace private data for this module */
102 struct list_head vxlan_list;
103 struct hlist_head sock_list[PORT_HASH_SIZE];
104 spinlock_t sock_lock;
112 struct list_head list;
116 /* Forwarding table entry */
118 struct hlist_node hlist; /* linked list of entries */
120 unsigned long updated; /* jiffies */
122 struct list_head remotes;
123 u16 state; /* see ndm_state */
124 u8 flags; /* see ndm_flags */
125 u8 eth_addr[ETH_ALEN];
128 /* Pseudo network device */
130 struct hlist_node hlist; /* vni hash table */
131 struct list_head next; /* vxlan's per namespace list */
132 struct vxlan_sock *vn_sock; /* listening socket */
133 struct net_device *dev;
134 struct vxlan_rdst default_dst; /* default destination */
135 __be32 saddr; /* source address */
137 __u16 port_min; /* source port range */
139 __u8 tos; /* TOS override */
141 u32 flags; /* VXLAN_F_* below */
143 struct work_struct sock_work;
144 struct work_struct igmp_join;
145 struct work_struct igmp_leave;
147 unsigned long age_interval;
148 struct timer_list age_timer;
149 spinlock_t hash_lock;
150 unsigned int addrcnt;
151 unsigned int addrmax;
153 struct hlist_head fdb_head[FDB_HASH_SIZE];
156 #define VXLAN_F_LEARN 0x01
157 #define VXLAN_F_PROXY 0x02
158 #define VXLAN_F_RSC 0x04
159 #define VXLAN_F_L2MISS 0x08
160 #define VXLAN_F_L3MISS 0x10
162 /* salt for hash table */
163 static u32 vxlan_salt __read_mostly;
164 static struct workqueue_struct *vxlan_wq;
166 static void vxlan_sock_work(struct work_struct *work);
168 /* Virtual Network hash table head */
169 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
171 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
174 /* Socket hash table head */
175 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
177 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
179 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
182 /* First remote destination for a forwarding entry.
183 * Guaranteed to be non-NULL because remotes are never deleted.
185 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
187 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
190 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
192 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
195 /* Find VXLAN socket based on network namespace and UDP port */
196 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
198 struct vxlan_sock *vs;
200 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
201 if (inet_sk(vs->sock->sk)->inet_sport == port)
207 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
209 struct vxlan_dev *vxlan;
211 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
212 if (vxlan->default_dst.remote_vni == id)
219 /* Look up VNI in a per net namespace table */
220 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
222 struct vxlan_sock *vs;
224 vs = vxlan_find_sock(net, port);
228 return vxlan_vs_find_vni(vs, id);
231 /* Fill in neighbour message in skbuff. */
232 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
233 const struct vxlan_fdb *fdb,
234 u32 portid, u32 seq, int type, unsigned int flags,
235 const struct vxlan_rdst *rdst)
237 unsigned long now = jiffies;
238 struct nda_cacheinfo ci;
239 struct nlmsghdr *nlh;
241 bool send_ip, send_eth;
243 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
247 ndm = nlmsg_data(nlh);
248 memset(ndm, 0, sizeof(*ndm));
250 send_eth = send_ip = true;
252 if (type == RTM_GETNEIGH) {
253 ndm->ndm_family = AF_INET;
254 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
255 send_eth = !is_zero_ether_addr(fdb->eth_addr);
257 ndm->ndm_family = AF_BRIDGE;
258 ndm->ndm_state = fdb->state;
259 ndm->ndm_ifindex = vxlan->dev->ifindex;
260 ndm->ndm_flags = fdb->flags;
261 ndm->ndm_type = NDA_DST;
263 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
264 goto nla_put_failure;
266 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
267 goto nla_put_failure;
269 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
270 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
271 goto nla_put_failure;
272 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
273 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
274 goto nla_put_failure;
275 if (rdst->remote_ifindex &&
276 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
277 goto nla_put_failure;
279 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
280 ci.ndm_confirmed = 0;
281 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
284 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
285 goto nla_put_failure;
287 return nlmsg_end(skb, nlh);
290 nlmsg_cancel(skb, nlh);
294 static inline size_t vxlan_nlmsg_size(void)
296 return NLMSG_ALIGN(sizeof(struct ndmsg))
297 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
298 + nla_total_size(sizeof(__be32)) /* NDA_DST */
299 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
300 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
301 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
302 + nla_total_size(sizeof(struct nda_cacheinfo));
305 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
306 struct vxlan_fdb *fdb, int type)
308 struct net *net = dev_net(vxlan->dev);
312 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
316 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
317 first_remote_rtnl(fdb));
319 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
320 WARN_ON(err == -EMSGSIZE);
325 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
329 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
332 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
334 struct vxlan_dev *vxlan = netdev_priv(dev);
335 struct vxlan_fdb f = {
338 struct vxlan_rdst remote = {
339 .remote_ip = ipa, /* goes to NDA_DST */
340 .remote_vni = VXLAN_N_VID,
343 INIT_LIST_HEAD(&f.remotes);
344 list_add_rcu(&remote.list, &f.remotes);
346 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
349 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
351 struct vxlan_fdb f = {
355 INIT_LIST_HEAD(&f.remotes);
356 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
358 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
361 /* Hash Ethernet address */
362 static u32 eth_hash(const unsigned char *addr)
364 u64 value = get_unaligned((u64 *)addr);
366 /* only want 6 bytes */
372 return hash_64(value, FDB_HASH_BITS);
375 /* Hash chain to use given mac address */
376 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
379 return &vxlan->fdb_head[eth_hash(mac)];
382 /* Look up Ethernet address in forwarding table */
383 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
387 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
390 hlist_for_each_entry_rcu(f, head, hlist) {
391 if (compare_ether_addr(mac, f->eth_addr) == 0)
398 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
403 f = __vxlan_find_mac(vxlan, mac);
410 /* caller should hold vxlan->hash_lock */
411 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
412 __be32 ip, __be16 port,
413 __u32 vni, __u32 ifindex)
415 struct vxlan_rdst *rd;
417 list_for_each_entry(rd, &f->remotes, list) {
418 if (rd->remote_ip == ip &&
419 rd->remote_port == port &&
420 rd->remote_vni == vni &&
421 rd->remote_ifindex == ifindex)
428 /* Replace destination of unicast mac */
429 static int vxlan_fdb_replace(struct vxlan_fdb *f,
430 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
432 struct vxlan_rdst *rd;
434 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
438 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
442 rd->remote_port = port;
443 rd->remote_vni = vni;
444 rd->remote_ifindex = ifindex;
448 /* Add/update destinations for multicast */
449 static int vxlan_fdb_append(struct vxlan_fdb *f,
450 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
452 struct vxlan_rdst *rd;
454 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
458 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
462 rd->remote_port = port;
463 rd->remote_vni = vni;
464 rd->remote_ifindex = ifindex;
466 list_add_tail_rcu(&rd->list, &f->remotes);
471 /* Add new entry to forwarding table -- assumes lock held */
472 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
473 const u8 *mac, __be32 ip,
474 __u16 state, __u16 flags,
475 __be16 port, __u32 vni, __u32 ifindex,
481 f = __vxlan_find_mac(vxlan, mac);
483 if (flags & NLM_F_EXCL) {
484 netdev_dbg(vxlan->dev,
485 "lost race to create %pM\n", mac);
488 if (f->state != state) {
490 f->updated = jiffies;
493 if (f->flags != ndm_flags) {
494 f->flags = ndm_flags;
495 f->updated = jiffies;
498 if ((flags & NLM_F_REPLACE)) {
499 /* Only change unicasts */
500 if (!(is_multicast_ether_addr(f->eth_addr) ||
501 is_zero_ether_addr(f->eth_addr))) {
502 int rc = vxlan_fdb_replace(f, ip, port, vni,
511 if ((flags & NLM_F_APPEND) &&
512 (is_multicast_ether_addr(f->eth_addr) ||
513 is_zero_ether_addr(f->eth_addr))) {
514 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
521 if (!(flags & NLM_F_CREATE))
524 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
527 /* Disallow replace to add a multicast entry */
528 if ((flags & NLM_F_REPLACE) &&
529 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
532 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
533 f = kmalloc(sizeof(*f), GFP_ATOMIC);
539 f->flags = ndm_flags;
540 f->updated = f->used = jiffies;
541 INIT_LIST_HEAD(&f->remotes);
542 memcpy(f->eth_addr, mac, ETH_ALEN);
544 vxlan_fdb_append(f, ip, port, vni, ifindex);
547 hlist_add_head_rcu(&f->hlist,
548 vxlan_fdb_head(vxlan, mac));
552 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
557 static void vxlan_fdb_free_rdst(struct rcu_head *head)
559 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
563 static void vxlan_fdb_free(struct rcu_head *head)
565 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
566 struct vxlan_rdst *rd, *nd;
568 list_for_each_entry_safe(rd, nd, &f->remotes, list)
573 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
575 netdev_dbg(vxlan->dev,
576 "delete %pM\n", f->eth_addr);
579 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
581 hlist_del_rcu(&f->hlist);
582 call_rcu(&f->rcu, vxlan_fdb_free);
585 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
586 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
588 struct net *net = dev_net(vxlan->dev);
591 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
592 return -EAFNOSUPPORT;
594 *ip = nla_get_be32(tb[NDA_DST]);
596 *ip = htonl(INADDR_ANY);
600 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
602 *port = nla_get_be16(tb[NDA_PORT]);
604 *port = vxlan->dst_port;
608 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
610 *vni = nla_get_u32(tb[NDA_VNI]);
612 *vni = vxlan->default_dst.remote_vni;
615 if (tb[NDA_IFINDEX]) {
616 struct net_device *tdev;
618 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
620 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
621 tdev = dev_get_by_index(net, *ifindex);
623 return -EADDRNOTAVAIL;
632 /* Add static entry (via netlink) */
633 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
634 struct net_device *dev,
635 const unsigned char *addr, u16 flags)
637 struct vxlan_dev *vxlan = netdev_priv(dev);
638 /* struct net *net = dev_net(vxlan->dev); */
644 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
645 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
650 if (tb[NDA_DST] == NULL)
653 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
657 spin_lock_bh(&vxlan->hash_lock);
658 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
659 port, vni, ifindex, ndm->ndm_flags);
660 spin_unlock_bh(&vxlan->hash_lock);
665 /* Delete entry (via netlink) */
666 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
667 struct net_device *dev,
668 const unsigned char *addr)
670 struct vxlan_dev *vxlan = netdev_priv(dev);
672 struct vxlan_rdst *rd = NULL;
678 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
684 spin_lock_bh(&vxlan->hash_lock);
685 f = vxlan_find_mac(vxlan, addr);
689 if (ip != htonl(INADDR_ANY)) {
690 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
697 /* remove a destination if it's not the only one on the list,
698 * otherwise destroy the fdb entry
700 if (rd && !list_is_singular(&f->remotes)) {
701 list_del_rcu(&rd->list);
702 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
706 vxlan_fdb_destroy(vxlan, f);
709 spin_unlock_bh(&vxlan->hash_lock);
714 /* Dump forwarding table */
715 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
716 struct net_device *dev, int idx)
718 struct vxlan_dev *vxlan = netdev_priv(dev);
721 for (h = 0; h < FDB_HASH_SIZE; ++h) {
725 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
726 struct vxlan_rdst *rd;
728 if (idx < cb->args[0])
731 list_for_each_entry_rcu(rd, &f->remotes, list) {
732 err = vxlan_fdb_info(skb, vxlan, f,
733 NETLINK_CB(cb->skb).portid,
748 /* Watch incoming packets to learn mapping between Ethernet address
749 * and Tunnel endpoint.
750 * Return true if packet is bogus and should be droppped.
752 static bool vxlan_snoop(struct net_device *dev,
753 __be32 src_ip, const u8 *src_mac)
755 struct vxlan_dev *vxlan = netdev_priv(dev);
758 f = vxlan_find_mac(vxlan, src_mac);
760 struct vxlan_rdst *rdst = first_remote_rcu(f);
762 if (likely(rdst->remote_ip == src_ip))
765 /* Don't migrate static entries, drop packets */
766 if (f->state & NUD_NOARP)
771 "%pM migrated from %pI4 to %pI4\n",
772 src_mac, &rdst->remote_ip, &src_ip);
774 rdst->remote_ip = src_ip;
775 f->updated = jiffies;
776 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
778 /* learned new entry */
779 spin_lock(&vxlan->hash_lock);
781 /* close off race between vxlan_flush and incoming packets */
782 if (netif_running(dev))
783 vxlan_fdb_create(vxlan, src_mac, src_ip,
785 NLM_F_EXCL|NLM_F_CREATE,
787 vxlan->default_dst.remote_vni,
789 spin_unlock(&vxlan->hash_lock);
795 /* See if multicast group is already in use by other ID */
796 static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
798 struct vxlan_dev *vxlan;
800 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
801 if (!netif_running(vxlan->dev))
804 if (vxlan->default_dst.remote_ip == remote_ip)
811 static void vxlan_sock_hold(struct vxlan_sock *vs)
813 atomic_inc(&vs->refcnt);
816 static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
818 if (!atomic_dec_and_test(&vs->refcnt))
821 spin_lock(&vn->sock_lock);
822 hlist_del_rcu(&vs->hlist);
823 spin_unlock(&vn->sock_lock);
825 queue_work(vxlan_wq, &vs->del_work);
828 /* Callback to update multicast group membership when first VNI on
829 * multicast asddress is brought up
830 * Done as workqueue because ip_mc_join_group acquires RTNL.
832 static void vxlan_igmp_join(struct work_struct *work)
834 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
835 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
836 struct vxlan_sock *vs = vxlan->vn_sock;
837 struct sock *sk = vs->sock->sk;
838 struct ip_mreqn mreq = {
839 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
840 .imr_ifindex = vxlan->default_dst.remote_ifindex,
844 ip_mc_join_group(sk, &mreq);
847 vxlan_sock_release(vn, vs);
851 /* Inverse of vxlan_igmp_join when last VNI is brought down */
852 static void vxlan_igmp_leave(struct work_struct *work)
854 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
855 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
856 struct vxlan_sock *vs = vxlan->vn_sock;
857 struct sock *sk = vs->sock->sk;
858 struct ip_mreqn mreq = {
859 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
860 .imr_ifindex = vxlan->default_dst.remote_ifindex,
864 ip_mc_leave_group(sk, &mreq);
867 vxlan_sock_release(vn, vs);
871 /* Callback from net/ipv4/udp.c to receive packets */
872 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
874 struct vxlan_sock *vs;
875 struct vxlanhdr *vxh;
878 /* Need Vxlan and inner Ethernet header to be present */
879 if (!pskb_may_pull(skb, VXLAN_HLEN))
882 /* Return packets with reserved bits set */
883 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
884 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
885 (vxh->vx_vni & htonl(0xff))) {
886 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
887 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
891 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
894 port = inet_sk(sk)->inet_sport;
896 vs = vxlan_find_sock(sock_net(sk), port);
900 vs->rcv(vs, skb, vxh->vx_vni);
904 /* Consume bad packet */
909 /* Return non vxlan pkt */
913 static void vxlan_rcv(struct vxlan_sock *vs,
914 struct sk_buff *skb, __be32 vx_vni)
917 struct vxlan_dev *vxlan;
918 struct pcpu_tstats *stats;
922 vni = ntohl(vx_vni) >> 8;
923 /* Is this VNI defined? */
924 vxlan = vxlan_vs_find_vni(vs, vni);
928 skb_reset_mac_header(skb);
929 skb->protocol = eth_type_trans(skb, vxlan->dev);
931 /* Ignore packet loops (and multicast echo) */
932 if (compare_ether_addr(eth_hdr(skb)->h_source,
933 vxlan->dev->dev_addr) == 0)
936 /* Re-examine inner Ethernet packet */
938 if ((vxlan->flags & VXLAN_F_LEARN) &&
939 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
942 skb_reset_network_header(skb);
944 /* If the NIC driver gave us an encapsulated packet with
945 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
946 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
947 * for us. Otherwise force the upper layers to verify it.
949 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
950 !(vxlan->dev->features & NETIF_F_RXCSUM))
951 skb->ip_summed = CHECKSUM_NONE;
953 skb->encapsulation = 0;
955 err = IP_ECN_decapsulate(oip, skb);
958 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
959 &oip->saddr, oip->tos);
961 ++vxlan->dev->stats.rx_frame_errors;
962 ++vxlan->dev->stats.rx_errors;
967 stats = this_cpu_ptr(vxlan->dev->tstats);
968 u64_stats_update_begin(&stats->syncp);
970 stats->rx_bytes += skb->len;
971 u64_stats_update_end(&stats->syncp);
977 /* Consume bad packet */
981 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
983 struct vxlan_dev *vxlan = netdev_priv(dev);
989 if (dev->flags & IFF_NOARP)
992 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
993 dev->stats.tx_dropped++;
998 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
999 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1000 parp->ar_pro != htons(ETH_P_IP) ||
1001 parp->ar_op != htons(ARPOP_REQUEST) ||
1002 parp->ar_hln != dev->addr_len ||
1005 arpptr = (u8 *)parp + sizeof(struct arphdr);
1007 arpptr += dev->addr_len; /* sha */
1008 memcpy(&sip, arpptr, sizeof(sip));
1009 arpptr += sizeof(sip);
1010 arpptr += dev->addr_len; /* tha */
1011 memcpy(&tip, arpptr, sizeof(tip));
1013 if (ipv4_is_loopback(tip) ||
1014 ipv4_is_multicast(tip))
1017 n = neigh_lookup(&arp_tbl, &tip, dev);
1020 struct vxlan_fdb *f;
1021 struct sk_buff *reply;
1023 if (!(n->nud_state & NUD_CONNECTED)) {
1028 f = vxlan_find_mac(vxlan, n->ha);
1029 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
1030 /* bridge-local neighbor */
1035 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1040 skb_reset_mac_header(reply);
1041 __skb_pull(reply, skb_network_offset(reply));
1042 reply->ip_summed = CHECKSUM_UNNECESSARY;
1043 reply->pkt_type = PACKET_HOST;
1045 if (netif_rx_ni(reply) == NET_RX_DROP)
1046 dev->stats.rx_dropped++;
1047 } else if (vxlan->flags & VXLAN_F_L3MISS)
1048 vxlan_ip_miss(dev, tip);
1051 return NETDEV_TX_OK;
1054 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1056 struct vxlan_dev *vxlan = netdev_priv(dev);
1057 struct neighbour *n;
1060 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1064 switch (ntohs(eth_hdr(skb)->h_proto)) {
1066 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1069 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1078 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1080 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1082 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1086 } else if (vxlan->flags & VXLAN_F_L3MISS)
1087 vxlan_ip_miss(dev, pip->daddr);
1091 static void vxlan_sock_put(struct sk_buff *skb)
1096 /* On transmit, associate with the tunnel socket */
1097 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1099 struct vxlan_dev *vxlan = netdev_priv(dev);
1100 struct sock *sk = vxlan->vn_sock->sock->sk;
1105 skb->destructor = vxlan_sock_put;
1108 /* Compute source port for outgoing packet
1109 * first choice to use L4 flow hash since it will spread
1110 * better and maybe available from hardware
1111 * secondary choice is to use jhash on the Ethernet header
1113 static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
1115 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1118 hash = skb_get_rxhash(skb);
1120 hash = jhash(skb->data, 2 * ETH_ALEN,
1121 (__force u32) skb->protocol);
1123 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
1126 static int handle_offloads(struct sk_buff *skb)
1128 if (skb_is_gso(skb)) {
1129 int err = skb_unclone(skb, GFP_ATOMIC);
1133 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1134 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1135 skb->ip_summed = CHECKSUM_NONE;
1140 /* Bypass encapsulation if the destination is local */
1141 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1142 struct vxlan_dev *dst_vxlan)
1144 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1145 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1147 skb->pkt_type = PACKET_HOST;
1148 skb->encapsulation = 0;
1149 skb->dev = dst_vxlan->dev;
1150 __skb_pull(skb, skb_network_offset(skb));
1152 if (dst_vxlan->flags & VXLAN_F_LEARN)
1153 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1154 eth_hdr(skb)->h_source);
1156 u64_stats_update_begin(&tx_stats->syncp);
1157 tx_stats->tx_packets++;
1158 tx_stats->tx_bytes += skb->len;
1159 u64_stats_update_end(&tx_stats->syncp);
1161 if (netif_rx(skb) == NET_RX_SUCCESS) {
1162 u64_stats_update_begin(&rx_stats->syncp);
1163 rx_stats->rx_packets++;
1164 rx_stats->rx_bytes += skb->len;
1165 u64_stats_update_end(&rx_stats->syncp);
1167 skb->dev->stats.rx_dropped++;
1171 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1172 struct vxlan_rdst *rdst, bool did_rsc)
1174 struct vxlan_dev *vxlan = netdev_priv(dev);
1176 const struct iphdr *old_iph;
1177 struct vxlanhdr *vxh;
1181 __be16 src_port, dst_port;
1187 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1188 vni = rdst->remote_vni;
1189 dst = rdst->remote_ip;
1193 /* short-circuited back to local bridge */
1194 vxlan_encap_bypass(skb, vxlan, vxlan);
1200 if (!skb->encapsulation) {
1201 skb_reset_inner_headers(skb);
1202 skb->encapsulation = 1;
1205 /* Need space for new headers (invalidates iph ptr) */
1206 if (skb_cow_head(skb, VXLAN_HEADROOM))
1209 old_iph = ip_hdr(skb);
1212 if (!ttl && IN_MULTICAST(ntohl(dst)))
1217 tos = ip_tunnel_get_dsfield(old_iph, skb);
1219 src_port = vxlan_src_port(vxlan, skb);
1221 memset(&fl4, 0, sizeof(fl4));
1222 fl4.flowi4_oif = rdst->remote_ifindex;
1223 fl4.flowi4_tos = RT_TOS(tos);
1225 fl4.saddr = vxlan->saddr;
1227 rt = ip_route_output_key(dev_net(dev), &fl4);
1229 netdev_dbg(dev, "no route to %pI4\n", &dst);
1230 dev->stats.tx_carrier_errors++;
1234 if (rt->dst.dev == dev) {
1235 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1237 dev->stats.collisions++;
1241 /* Bypass encapsulation if the destination is local */
1242 if (rt->rt_flags & RTCF_LOCAL &&
1243 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1244 struct vxlan_dev *dst_vxlan;
1247 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1250 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1253 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1254 vxh->vx_flags = htonl(VXLAN_FLAGS);
1255 vxh->vx_vni = htonl(vni << 8);
1257 __skb_push(skb, sizeof(*uh));
1258 skb_reset_transport_header(skb);
1261 uh->dest = dst_port;
1262 uh->source = src_port;
1264 uh->len = htons(skb->len);
1267 vxlan_set_owner(dev, skb);
1269 if (handle_offloads(skb))
1272 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1273 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1275 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1276 IPPROTO_UDP, tos, ttl, df);
1277 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1282 dev->stats.tx_dropped++;
1286 dev->stats.tx_errors++;
1291 /* Transmit local packets over Vxlan
1293 * Outer IP header inherits ECN and DF from inner header.
1294 * Outer UDP destination is the VXLAN assigned port.
1295 * source port is based on hash of flow
1297 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1299 struct vxlan_dev *vxlan = netdev_priv(dev);
1301 bool did_rsc = false;
1302 struct vxlan_rdst *rdst;
1303 struct vxlan_fdb *f;
1305 skb_reset_mac_header(skb);
1308 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1309 return arp_reduce(dev, skb);
1311 f = vxlan_find_mac(vxlan, eth->h_dest);
1314 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1315 ntohs(eth->h_proto) == ETH_P_IP) {
1316 did_rsc = route_shortcircuit(dev, skb);
1318 f = vxlan_find_mac(vxlan, eth->h_dest);
1322 f = vxlan_find_mac(vxlan, all_zeros_mac);
1324 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1325 !is_multicast_ether_addr(eth->h_dest))
1326 vxlan_fdb_miss(vxlan, eth->h_dest);
1328 dev->stats.tx_dropped++;
1330 return NETDEV_TX_OK;
1334 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1335 struct sk_buff *skb1;
1337 skb1 = skb_clone(skb, GFP_ATOMIC);
1339 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1343 return NETDEV_TX_OK;
1346 /* Walk the forwarding table and purge stale entries */
1347 static void vxlan_cleanup(unsigned long arg)
1349 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1350 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1353 if (!netif_running(vxlan->dev))
1356 spin_lock_bh(&vxlan->hash_lock);
1357 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1358 struct hlist_node *p, *n;
1359 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1361 = container_of(p, struct vxlan_fdb, hlist);
1362 unsigned long timeout;
1364 if (f->state & NUD_PERMANENT)
1367 timeout = f->used + vxlan->age_interval * HZ;
1368 if (time_before_eq(timeout, jiffies)) {
1369 netdev_dbg(vxlan->dev,
1370 "garbage collect %pM\n",
1372 f->state = NUD_STALE;
1373 vxlan_fdb_destroy(vxlan, f);
1374 } else if (time_before(timeout, next_timer))
1375 next_timer = timeout;
1378 spin_unlock_bh(&vxlan->hash_lock);
1380 mod_timer(&vxlan->age_timer, next_timer);
1383 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1385 __u32 vni = vxlan->default_dst.remote_vni;
1387 vxlan->vn_sock = vs;
1388 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1391 /* Setup stats when device is created */
1392 static int vxlan_init(struct net_device *dev)
1394 struct vxlan_dev *vxlan = netdev_priv(dev);
1395 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1396 struct vxlan_sock *vs;
1398 dev->tstats = alloc_percpu(struct pcpu_tstats);
1402 spin_lock(&vn->sock_lock);
1403 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1405 /* If we have a socket with same port already, reuse it */
1406 atomic_inc(&vs->refcnt);
1407 vxlan_vs_add_dev(vs, vxlan);
1409 /* otherwise make new socket outside of RTNL */
1411 queue_work(vxlan_wq, &vxlan->sock_work);
1413 spin_unlock(&vn->sock_lock);
1418 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1420 struct vxlan_fdb *f;
1422 spin_lock_bh(&vxlan->hash_lock);
1423 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1425 vxlan_fdb_destroy(vxlan, f);
1426 spin_unlock_bh(&vxlan->hash_lock);
1429 static void vxlan_uninit(struct net_device *dev)
1431 struct vxlan_dev *vxlan = netdev_priv(dev);
1432 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1433 struct vxlan_sock *vs = vxlan->vn_sock;
1435 vxlan_fdb_delete_default(vxlan);
1438 vxlan_sock_release(vn, vs);
1439 free_percpu(dev->tstats);
1442 /* Start ageing timer and join group when device is brought up */
1443 static int vxlan_open(struct net_device *dev)
1445 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1446 struct vxlan_dev *vxlan = netdev_priv(dev);
1447 struct vxlan_sock *vs = vxlan->vn_sock;
1449 /* socket hasn't been created */
1453 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1454 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1455 vxlan_sock_hold(vs);
1457 queue_work(vxlan_wq, &vxlan->igmp_join);
1460 if (vxlan->age_interval)
1461 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1466 /* Purge the forwarding table */
1467 static void vxlan_flush(struct vxlan_dev *vxlan)
1471 spin_lock_bh(&vxlan->hash_lock);
1472 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1473 struct hlist_node *p, *n;
1474 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1476 = container_of(p, struct vxlan_fdb, hlist);
1477 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1478 if (!is_zero_ether_addr(f->eth_addr))
1479 vxlan_fdb_destroy(vxlan, f);
1482 spin_unlock_bh(&vxlan->hash_lock);
1485 /* Cleanup timer and forwarding table on shutdown */
1486 static int vxlan_stop(struct net_device *dev)
1488 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1489 struct vxlan_dev *vxlan = netdev_priv(dev);
1490 struct vxlan_sock *vs = vxlan->vn_sock;
1492 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1493 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1494 vxlan_sock_hold(vs);
1496 queue_work(vxlan_wq, &vxlan->igmp_leave);
1499 del_timer_sync(&vxlan->age_timer);
1506 /* Stub, nothing needs to be done. */
1507 static void vxlan_set_multicast_list(struct net_device *dev)
1511 static const struct net_device_ops vxlan_netdev_ops = {
1512 .ndo_init = vxlan_init,
1513 .ndo_uninit = vxlan_uninit,
1514 .ndo_open = vxlan_open,
1515 .ndo_stop = vxlan_stop,
1516 .ndo_start_xmit = vxlan_xmit,
1517 .ndo_get_stats64 = ip_tunnel_get_stats64,
1518 .ndo_set_rx_mode = vxlan_set_multicast_list,
1519 .ndo_change_mtu = eth_change_mtu,
1520 .ndo_validate_addr = eth_validate_addr,
1521 .ndo_set_mac_address = eth_mac_addr,
1522 .ndo_fdb_add = vxlan_fdb_add,
1523 .ndo_fdb_del = vxlan_fdb_delete,
1524 .ndo_fdb_dump = vxlan_fdb_dump,
1527 /* Info for udev, that this is a virtual tunnel endpoint */
1528 static struct device_type vxlan_type = {
1532 /* Initialize the device structure. */
1533 static void vxlan_setup(struct net_device *dev)
1535 struct vxlan_dev *vxlan = netdev_priv(dev);
1539 eth_hw_addr_random(dev);
1541 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1543 dev->netdev_ops = &vxlan_netdev_ops;
1544 dev->destructor = free_netdev;
1545 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1547 dev->tx_queue_len = 0;
1548 dev->features |= NETIF_F_LLTX;
1549 dev->features |= NETIF_F_NETNS_LOCAL;
1550 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1551 dev->features |= NETIF_F_RXCSUM;
1552 dev->features |= NETIF_F_GSO_SOFTWARE;
1554 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1555 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1556 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1557 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1559 INIT_LIST_HEAD(&vxlan->next);
1560 spin_lock_init(&vxlan->hash_lock);
1561 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1562 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
1563 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
1565 init_timer_deferrable(&vxlan->age_timer);
1566 vxlan->age_timer.function = vxlan_cleanup;
1567 vxlan->age_timer.data = (unsigned long) vxlan;
1569 inet_get_local_port_range(&low, &high);
1570 vxlan->port_min = low;
1571 vxlan->port_max = high;
1572 vxlan->dst_port = htons(vxlan_port);
1576 for (h = 0; h < FDB_HASH_SIZE; ++h)
1577 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1580 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1581 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1582 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1583 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1584 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1585 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1586 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1587 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1588 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1589 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1590 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1591 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1592 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1593 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1594 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1595 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
1598 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1600 if (tb[IFLA_ADDRESS]) {
1601 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1602 pr_debug("invalid link address (not ethernet)\n");
1606 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1607 pr_debug("invalid all zero ethernet address\n");
1608 return -EADDRNOTAVAIL;
1615 if (data[IFLA_VXLAN_ID]) {
1616 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1617 if (id >= VXLAN_VID_MASK)
1621 if (data[IFLA_VXLAN_PORT_RANGE]) {
1622 const struct ifla_vxlan_port_range *p
1623 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1625 if (ntohs(p->high) < ntohs(p->low)) {
1626 pr_debug("port range %u .. %u not valid\n",
1627 ntohs(p->low), ntohs(p->high));
1635 static void vxlan_get_drvinfo(struct net_device *netdev,
1636 struct ethtool_drvinfo *drvinfo)
1638 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1639 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1642 static const struct ethtool_ops vxlan_ethtool_ops = {
1643 .get_drvinfo = vxlan_get_drvinfo,
1644 .get_link = ethtool_op_get_link,
1647 static void vxlan_del_work(struct work_struct *work)
1649 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1651 sk_release_kernel(vs->sock->sk);
1655 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1658 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1659 struct vxlan_sock *vs;
1661 struct sockaddr_in vxlan_addr = {
1662 .sin_family = AF_INET,
1663 .sin_addr.s_addr = htonl(INADDR_ANY),
1669 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1671 pr_debug("memory alocation failure\n");
1672 return ERR_PTR(-ENOMEM);
1675 for (h = 0; h < VNI_HASH_SIZE; ++h)
1676 INIT_HLIST_HEAD(&vs->vni_list[h]);
1678 INIT_WORK(&vs->del_work, vxlan_del_work);
1680 /* Create UDP socket for encapsulation receive. */
1681 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1683 pr_debug("UDP socket create failed\n");
1688 /* Put in proper namespace */
1690 sk_change_net(sk, net);
1692 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1693 sizeof(vxlan_addr));
1695 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1696 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1697 sk_release_kernel(sk);
1701 atomic_set(&vs->refcnt, 1);
1704 /* Disable multicast loopback */
1705 inet_sk(sk)->mc_loop = 0;
1706 spin_lock(&vn->sock_lock);
1707 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1708 spin_unlock(&vn->sock_lock);
1710 /* Mark socket as an encapsulation socket. */
1711 udp_sk(sk)->encap_type = 1;
1712 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1717 static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1720 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1721 struct vxlan_sock *vs;
1723 vs = vxlan_socket_create(net, port, rcv);
1727 spin_lock(&vn->sock_lock);
1728 vs = vxlan_find_sock(net, port);
1731 atomic_inc(&vs->refcnt);
1733 vs = ERR_PTR(-EBUSY);
1735 spin_unlock(&vn->sock_lock);
1738 vs = ERR_PTR(-EINVAL);
1743 /* Scheduled at device creation to bind to a socket */
1744 static void vxlan_sock_work(struct work_struct *work)
1746 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1747 struct net *net = dev_net(vxlan->dev);
1748 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1749 __be16 port = vxlan->dst_port;
1750 struct vxlan_sock *nvs;
1752 nvs = vxlan_sock_add(net, port, vxlan_rcv);
1753 spin_lock(&vn->sock_lock);
1755 vxlan_vs_add_dev(nvs, vxlan);
1756 spin_unlock(&vn->sock_lock);
1758 dev_put(vxlan->dev);
1761 static int vxlan_newlink(struct net *net, struct net_device *dev,
1762 struct nlattr *tb[], struct nlattr *data[])
1764 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1765 struct vxlan_dev *vxlan = netdev_priv(dev);
1766 struct vxlan_rdst *dst = &vxlan->default_dst;
1770 if (!data[IFLA_VXLAN_ID])
1773 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1774 dst->remote_vni = vni;
1776 if (data[IFLA_VXLAN_GROUP])
1777 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1779 if (data[IFLA_VXLAN_LOCAL])
1780 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1782 if (data[IFLA_VXLAN_LINK] &&
1783 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1784 struct net_device *lowerdev
1785 = __dev_get_by_index(net, dst->remote_ifindex);
1788 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1793 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1795 /* update header length based on lower device */
1796 dev->hard_header_len = lowerdev->hard_header_len +
1800 if (data[IFLA_VXLAN_TOS])
1801 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1803 if (data[IFLA_VXLAN_TTL])
1804 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1806 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1807 vxlan->flags |= VXLAN_F_LEARN;
1809 if (data[IFLA_VXLAN_AGEING])
1810 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1812 vxlan->age_interval = FDB_AGE_DEFAULT;
1814 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1815 vxlan->flags |= VXLAN_F_PROXY;
1817 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1818 vxlan->flags |= VXLAN_F_RSC;
1820 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1821 vxlan->flags |= VXLAN_F_L2MISS;
1823 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1824 vxlan->flags |= VXLAN_F_L3MISS;
1826 if (data[IFLA_VXLAN_LIMIT])
1827 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1829 if (data[IFLA_VXLAN_PORT_RANGE]) {
1830 const struct ifla_vxlan_port_range *p
1831 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1832 vxlan->port_min = ntohs(p->low);
1833 vxlan->port_max = ntohs(p->high);
1836 if (data[IFLA_VXLAN_PORT])
1837 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1839 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1840 pr_info("duplicate VNI %u\n", vni);
1844 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1846 /* create an fdb entry for default destination */
1847 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1848 vxlan->default_dst.remote_ip,
1849 NUD_REACHABLE|NUD_PERMANENT,
1850 NLM_F_EXCL|NLM_F_CREATE,
1851 vxlan->dst_port, vxlan->default_dst.remote_vni,
1852 vxlan->default_dst.remote_ifindex, NTF_SELF);
1856 err = register_netdevice(dev);
1858 vxlan_fdb_delete_default(vxlan);
1862 list_add(&vxlan->next, &vn->vxlan_list);
1867 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1869 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1870 struct vxlan_dev *vxlan = netdev_priv(dev);
1872 spin_lock(&vn->sock_lock);
1873 if (!hlist_unhashed(&vxlan->hlist))
1874 hlist_del_rcu(&vxlan->hlist);
1875 spin_unlock(&vn->sock_lock);
1877 list_del(&vxlan->next);
1878 unregister_netdevice_queue(dev, head);
1881 static size_t vxlan_get_size(const struct net_device *dev)
1884 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1885 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1886 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1887 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1888 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1889 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1890 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1891 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1892 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1893 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1894 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1895 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1896 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1897 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1898 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
1902 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1904 const struct vxlan_dev *vxlan = netdev_priv(dev);
1905 const struct vxlan_rdst *dst = &vxlan->default_dst;
1906 struct ifla_vxlan_port_range ports = {
1907 .low = htons(vxlan->port_min),
1908 .high = htons(vxlan->port_max),
1911 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1912 goto nla_put_failure;
1914 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1915 goto nla_put_failure;
1917 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1918 goto nla_put_failure;
1920 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1921 goto nla_put_failure;
1923 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1924 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1925 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1926 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1927 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1928 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1929 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1930 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1931 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1932 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1933 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1934 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1935 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1936 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
1937 goto nla_put_failure;
1939 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1940 goto nla_put_failure;
1948 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1950 .maxtype = IFLA_VXLAN_MAX,
1951 .policy = vxlan_policy,
1952 .priv_size = sizeof(struct vxlan_dev),
1953 .setup = vxlan_setup,
1954 .validate = vxlan_validate,
1955 .newlink = vxlan_newlink,
1956 .dellink = vxlan_dellink,
1957 .get_size = vxlan_get_size,
1958 .fill_info = vxlan_fill_info,
1961 static __net_init int vxlan_init_net(struct net *net)
1963 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1966 INIT_LIST_HEAD(&vn->vxlan_list);
1967 spin_lock_init(&vn->sock_lock);
1969 for (h = 0; h < PORT_HASH_SIZE; ++h)
1970 INIT_HLIST_HEAD(&vn->sock_list[h]);
1975 static __net_exit void vxlan_exit_net(struct net *net)
1977 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1978 struct vxlan_dev *vxlan;
1982 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1983 unregister_netdevice_queue(vxlan->dev, &list);
1984 unregister_netdevice_many(&list);
1988 static struct pernet_operations vxlan_net_ops = {
1989 .init = vxlan_init_net,
1990 .exit = vxlan_exit_net,
1991 .id = &vxlan_net_id,
1992 .size = sizeof(struct vxlan_net),
1995 static int __init vxlan_init_module(void)
1999 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2003 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2005 rc = register_pernet_device(&vxlan_net_ops);
2009 rc = rtnl_link_register(&vxlan_link_ops);
2016 unregister_pernet_device(&vxlan_net_ops);
2018 destroy_workqueue(vxlan_wq);
2021 late_initcall(vxlan_init_module);
2023 static void __exit vxlan_cleanup_module(void)
2025 rtnl_link_unregister(&vxlan_link_ops);
2026 destroy_workqueue(vxlan_wq);
2027 unregister_pernet_device(&vxlan_net_ops);
2030 module_exit(vxlan_cleanup_module);
2032 MODULE_LICENSE("GPL");
2033 MODULE_VERSION(VXLAN_VERSION);
2034 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2035 MODULE_ALIAS_RTNL_LINK("vxlan");