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)
61 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
63 /* VXLAN protocol header */
69 /* UDP port for VXLAN traffic.
70 * The IANA assigned port is 4789, but the Linux default is 8472
71 * for compatibility with early adopters.
73 static unsigned short vxlan_port __read_mostly = 8472;
74 module_param_named(udp_port, vxlan_port, ushort, 0444);
75 MODULE_PARM_DESC(udp_port, "Destination UDP port");
77 static bool log_ecn_error = true;
78 module_param(log_ecn_error, bool, 0644);
79 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81 static int vxlan_net_id;
83 static const u8 all_zeros_mac[ETH_ALEN];
85 /* per UDP socket information */
87 struct hlist_node hlist;
89 struct work_struct del_work;
92 struct hlist_head vni_list[VNI_HASH_SIZE];
95 /* per-network namespace private data for this module */
97 struct list_head vxlan_list;
98 struct hlist_head sock_list[PORT_HASH_SIZE];
107 struct list_head list;
111 /* Forwarding table entry */
113 struct hlist_node hlist; /* linked list of entries */
115 unsigned long updated; /* jiffies */
117 struct list_head remotes;
118 u16 state; /* see ndm_state */
119 u8 flags; /* see ndm_flags */
120 u8 eth_addr[ETH_ALEN];
123 /* Pseudo network device */
125 struct hlist_node hlist; /* vni hash table */
126 struct list_head next; /* vxlan's per namespace list */
127 struct vxlan_sock *vn_sock; /* listening socket */
128 struct net_device *dev;
129 struct vxlan_rdst default_dst; /* default destination */
130 __be32 saddr; /* source address */
132 __u16 port_min; /* source port range */
134 __u8 tos; /* TOS override */
136 u32 flags; /* VXLAN_F_* below */
138 struct work_struct sock_work;
139 struct work_struct igmp_join;
140 struct work_struct igmp_leave;
142 unsigned long age_interval;
143 struct timer_list age_timer;
144 spinlock_t hash_lock;
145 unsigned int addrcnt;
146 unsigned int addrmax;
148 struct hlist_head fdb_head[FDB_HASH_SIZE];
151 #define VXLAN_F_LEARN 0x01
152 #define VXLAN_F_PROXY 0x02
153 #define VXLAN_F_RSC 0x04
154 #define VXLAN_F_L2MISS 0x08
155 #define VXLAN_F_L3MISS 0x10
157 /* salt for hash table */
158 static u32 vxlan_salt __read_mostly;
159 static struct workqueue_struct *vxlan_wq;
161 static void vxlan_sock_work(struct work_struct *work);
163 /* Virtual Network hash table head */
164 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
166 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
169 /* Socket hash table head */
170 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
172 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
174 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
177 /* First remote destination for a forwarding entry.
178 * Guaranteed to be non-NULL because remotes are never deleted.
180 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
182 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
185 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
190 /* Find VXLAN socket based on network namespace and UDP port */
191 static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
193 struct vxlan_sock *vs;
195 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
196 if (inet_sk(vs->sock->sk)->inet_sport == port)
202 /* Look up VNI in a per net namespace table */
203 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
205 struct vxlan_sock *vs;
206 struct vxlan_dev *vxlan;
208 vs = vxlan_find_port(net, port);
212 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
213 if (vxlan->default_dst.remote_vni == id)
220 /* Fill in neighbour message in skbuff. */
221 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
222 const struct vxlan_fdb *fdb,
223 u32 portid, u32 seq, int type, unsigned int flags,
224 const struct vxlan_rdst *rdst)
226 unsigned long now = jiffies;
227 struct nda_cacheinfo ci;
228 struct nlmsghdr *nlh;
230 bool send_ip, send_eth;
232 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
236 ndm = nlmsg_data(nlh);
237 memset(ndm, 0, sizeof(*ndm));
239 send_eth = send_ip = true;
241 if (type == RTM_GETNEIGH) {
242 ndm->ndm_family = AF_INET;
243 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
244 send_eth = !is_zero_ether_addr(fdb->eth_addr);
246 ndm->ndm_family = AF_BRIDGE;
247 ndm->ndm_state = fdb->state;
248 ndm->ndm_ifindex = vxlan->dev->ifindex;
249 ndm->ndm_flags = fdb->flags;
250 ndm->ndm_type = NDA_DST;
252 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
253 goto nla_put_failure;
255 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
256 goto nla_put_failure;
258 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
259 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
260 goto nla_put_failure;
261 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
262 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
263 goto nla_put_failure;
264 if (rdst->remote_ifindex &&
265 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
266 goto nla_put_failure;
268 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
269 ci.ndm_confirmed = 0;
270 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
273 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
274 goto nla_put_failure;
276 return nlmsg_end(skb, nlh);
279 nlmsg_cancel(skb, nlh);
283 static inline size_t vxlan_nlmsg_size(void)
285 return NLMSG_ALIGN(sizeof(struct ndmsg))
286 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
287 + nla_total_size(sizeof(__be32)) /* NDA_DST */
288 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
289 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
290 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
291 + nla_total_size(sizeof(struct nda_cacheinfo));
294 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
295 struct vxlan_fdb *fdb, int type)
297 struct net *net = dev_net(vxlan->dev);
301 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
305 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
306 first_remote_rtnl(fdb));
308 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
309 WARN_ON(err == -EMSGSIZE);
314 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
318 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
321 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
323 struct vxlan_dev *vxlan = netdev_priv(dev);
324 struct vxlan_fdb f = {
327 struct vxlan_rdst remote = {
328 .remote_ip = ipa, /* goes to NDA_DST */
329 .remote_vni = VXLAN_N_VID,
332 INIT_LIST_HEAD(&f.remotes);
333 list_add_rcu(&remote.list, &f.remotes);
335 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
338 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
340 struct vxlan_fdb f = {
344 INIT_LIST_HEAD(&f.remotes);
345 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
347 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
350 /* Hash Ethernet address */
351 static u32 eth_hash(const unsigned char *addr)
353 u64 value = get_unaligned((u64 *)addr);
355 /* only want 6 bytes */
361 return hash_64(value, FDB_HASH_BITS);
364 /* Hash chain to use given mac address */
365 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
368 return &vxlan->fdb_head[eth_hash(mac)];
371 /* Look up Ethernet address in forwarding table */
372 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
376 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
379 hlist_for_each_entry_rcu(f, head, hlist) {
380 if (compare_ether_addr(mac, f->eth_addr) == 0)
387 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
392 f = __vxlan_find_mac(vxlan, mac);
399 /* caller should hold vxlan->hash_lock */
400 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
401 __be32 ip, __be16 port,
402 __u32 vni, __u32 ifindex)
404 struct vxlan_rdst *rd;
406 list_for_each_entry(rd, &f->remotes, list) {
407 if (rd->remote_ip == ip &&
408 rd->remote_port == port &&
409 rd->remote_vni == vni &&
410 rd->remote_ifindex == ifindex)
417 /* Replace destination of unicast mac */
418 static int vxlan_fdb_replace(struct vxlan_fdb *f,
419 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
421 struct vxlan_rdst *rd;
423 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
427 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
431 rd->remote_port = port;
432 rd->remote_vni = vni;
433 rd->remote_ifindex = ifindex;
437 /* Add/update destinations for multicast */
438 static int vxlan_fdb_append(struct vxlan_fdb *f,
439 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
441 struct vxlan_rdst *rd;
443 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
447 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
451 rd->remote_port = port;
452 rd->remote_vni = vni;
453 rd->remote_ifindex = ifindex;
455 list_add_tail_rcu(&rd->list, &f->remotes);
460 /* Add new entry to forwarding table -- assumes lock held */
461 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
462 const u8 *mac, __be32 ip,
463 __u16 state, __u16 flags,
464 __be16 port, __u32 vni, __u32 ifindex,
470 f = __vxlan_find_mac(vxlan, mac);
472 if (flags & NLM_F_EXCL) {
473 netdev_dbg(vxlan->dev,
474 "lost race to create %pM\n", mac);
477 if (f->state != state) {
479 f->updated = jiffies;
482 if (f->flags != ndm_flags) {
483 f->flags = ndm_flags;
484 f->updated = jiffies;
487 if ((flags & NLM_F_REPLACE)) {
488 /* Only change unicasts */
489 if (!(is_multicast_ether_addr(f->eth_addr) ||
490 is_zero_ether_addr(f->eth_addr))) {
491 int rc = vxlan_fdb_replace(f, ip, port, vni,
500 if ((flags & NLM_F_APPEND) &&
501 (is_multicast_ether_addr(f->eth_addr) ||
502 is_zero_ether_addr(f->eth_addr))) {
503 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
510 if (!(flags & NLM_F_CREATE))
513 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
516 /* Disallow replace to add a multicast entry */
517 if ((flags & NLM_F_REPLACE) &&
518 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
521 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
522 f = kmalloc(sizeof(*f), GFP_ATOMIC);
528 f->flags = ndm_flags;
529 f->updated = f->used = jiffies;
530 INIT_LIST_HEAD(&f->remotes);
531 memcpy(f->eth_addr, mac, ETH_ALEN);
533 vxlan_fdb_append(f, ip, port, vni, ifindex);
536 hlist_add_head_rcu(&f->hlist,
537 vxlan_fdb_head(vxlan, mac));
541 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
546 static void vxlan_fdb_free_rdst(struct rcu_head *head)
548 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
552 static void vxlan_fdb_free(struct rcu_head *head)
554 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
555 struct vxlan_rdst *rd, *nd;
557 list_for_each_entry_safe(rd, nd, &f->remotes, list)
562 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
564 netdev_dbg(vxlan->dev,
565 "delete %pM\n", f->eth_addr);
568 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
570 hlist_del_rcu(&f->hlist);
571 call_rcu(&f->rcu, vxlan_fdb_free);
574 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
575 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
577 struct net *net = dev_net(vxlan->dev);
580 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
581 return -EAFNOSUPPORT;
583 *ip = nla_get_be32(tb[NDA_DST]);
585 *ip = htonl(INADDR_ANY);
589 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
591 *port = nla_get_be16(tb[NDA_PORT]);
593 *port = vxlan->dst_port;
597 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
599 *vni = nla_get_u32(tb[NDA_VNI]);
601 *vni = vxlan->default_dst.remote_vni;
604 if (tb[NDA_IFINDEX]) {
605 struct net_device *tdev;
607 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
609 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
610 tdev = dev_get_by_index(net, *ifindex);
612 return -EADDRNOTAVAIL;
621 /* Add static entry (via netlink) */
622 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
623 struct net_device *dev,
624 const unsigned char *addr, u16 flags)
626 struct vxlan_dev *vxlan = netdev_priv(dev);
627 /* struct net *net = dev_net(vxlan->dev); */
633 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
634 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
639 if (tb[NDA_DST] == NULL)
642 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
646 spin_lock_bh(&vxlan->hash_lock);
647 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
648 port, vni, ifindex, ndm->ndm_flags);
649 spin_unlock_bh(&vxlan->hash_lock);
654 /* Delete entry (via netlink) */
655 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
656 struct net_device *dev,
657 const unsigned char *addr)
659 struct vxlan_dev *vxlan = netdev_priv(dev);
661 struct vxlan_rdst *rd = NULL;
667 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
673 spin_lock_bh(&vxlan->hash_lock);
674 f = vxlan_find_mac(vxlan, addr);
678 if (ip != htonl(INADDR_ANY)) {
679 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
686 /* remove a destination if it's not the only one on the list,
687 * otherwise destroy the fdb entry
689 if (rd && !list_is_singular(&f->remotes)) {
690 list_del_rcu(&rd->list);
691 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
695 vxlan_fdb_destroy(vxlan, f);
698 spin_unlock_bh(&vxlan->hash_lock);
703 /* Dump forwarding table */
704 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
705 struct net_device *dev, int idx)
707 struct vxlan_dev *vxlan = netdev_priv(dev);
710 for (h = 0; h < FDB_HASH_SIZE; ++h) {
714 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
715 struct vxlan_rdst *rd;
717 if (idx < cb->args[0])
720 list_for_each_entry_rcu(rd, &f->remotes, list) {
721 err = vxlan_fdb_info(skb, vxlan, f,
722 NETLINK_CB(cb->skb).portid,
737 /* Watch incoming packets to learn mapping between Ethernet address
738 * and Tunnel endpoint.
739 * Return true if packet is bogus and should be droppped.
741 static bool vxlan_snoop(struct net_device *dev,
742 __be32 src_ip, const u8 *src_mac)
744 struct vxlan_dev *vxlan = netdev_priv(dev);
747 f = vxlan_find_mac(vxlan, src_mac);
749 struct vxlan_rdst *rdst = first_remote_rcu(f);
751 if (likely(rdst->remote_ip == src_ip))
754 /* Don't migrate static entries, drop packets */
755 if (f->state & NUD_NOARP)
760 "%pM migrated from %pI4 to %pI4\n",
761 src_mac, &rdst->remote_ip, &src_ip);
763 rdst->remote_ip = src_ip;
764 f->updated = jiffies;
765 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
767 /* learned new entry */
768 spin_lock(&vxlan->hash_lock);
770 /* close off race between vxlan_flush and incoming packets */
771 if (netif_running(dev))
772 vxlan_fdb_create(vxlan, src_mac, src_ip,
774 NLM_F_EXCL|NLM_F_CREATE,
776 vxlan->default_dst.remote_vni,
778 spin_unlock(&vxlan->hash_lock);
784 /* See if multicast group is already in use by other ID */
785 static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
787 struct vxlan_dev *vxlan;
789 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
790 if (!netif_running(vxlan->dev))
793 if (vxlan->default_dst.remote_ip == remote_ip)
800 static void vxlan_sock_hold(struct vxlan_sock *vs)
802 atomic_inc(&vs->refcnt);
805 static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
807 if (!atomic_dec_and_test(&vs->refcnt))
810 spin_lock(&vn->sock_lock);
811 hlist_del_rcu(&vs->hlist);
812 spin_unlock(&vn->sock_lock);
814 queue_work(vxlan_wq, &vs->del_work);
817 /* Callback to update multicast group membership when first VNI on
818 * multicast asddress is brought up
819 * Done as workqueue because ip_mc_join_group acquires RTNL.
821 static void vxlan_igmp_join(struct work_struct *work)
823 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
824 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
825 struct vxlan_sock *vs = vxlan->vn_sock;
826 struct sock *sk = vs->sock->sk;
827 struct ip_mreqn mreq = {
828 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
829 .imr_ifindex = vxlan->default_dst.remote_ifindex,
833 ip_mc_join_group(sk, &mreq);
836 vxlan_sock_release(vn, vs);
840 /* Inverse of vxlan_igmp_join when last VNI is brought down */
841 static void vxlan_igmp_leave(struct work_struct *work)
843 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
844 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
845 struct vxlan_sock *vs = vxlan->vn_sock;
846 struct sock *sk = vs->sock->sk;
847 struct ip_mreqn mreq = {
848 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
849 .imr_ifindex = vxlan->default_dst.remote_ifindex,
853 ip_mc_leave_group(sk, &mreq);
856 vxlan_sock_release(vn, vs);
860 /* Callback from net/ipv4/udp.c to receive packets */
861 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
864 struct vxlanhdr *vxh;
865 struct vxlan_dev *vxlan;
866 struct pcpu_tstats *stats;
871 /* pop off outer UDP header */
872 __skb_pull(skb, sizeof(struct udphdr));
874 /* Need Vxlan and inner Ethernet header to be present */
875 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
878 /* Drop packets with reserved bits set */
879 vxh = (struct vxlanhdr *) skb->data;
880 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
881 (vxh->vx_vni & htonl(0xff))) {
882 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
883 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
887 __skb_pull(skb, sizeof(struct vxlanhdr));
889 /* Is this VNI defined? */
890 vni = ntohl(vxh->vx_vni) >> 8;
891 port = inet_sk(sk)->inet_sport;
892 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
894 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
899 if (!pskb_may_pull(skb, ETH_HLEN)) {
900 vxlan->dev->stats.rx_length_errors++;
901 vxlan->dev->stats.rx_errors++;
905 skb_reset_mac_header(skb);
907 /* Re-examine inner Ethernet packet */
909 skb->protocol = eth_type_trans(skb, vxlan->dev);
911 /* Ignore packet loops (and multicast echo) */
912 if (compare_ether_addr(eth_hdr(skb)->h_source,
913 vxlan->dev->dev_addr) == 0)
916 if ((vxlan->flags & VXLAN_F_LEARN) &&
917 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
920 __skb_tunnel_rx(skb, vxlan->dev);
921 skb_reset_network_header(skb);
923 /* If the NIC driver gave us an encapsulated packet with
924 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
925 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
926 * for us. Otherwise force the upper layers to verify it.
928 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
929 !(vxlan->dev->features & NETIF_F_RXCSUM))
930 skb->ip_summed = CHECKSUM_NONE;
932 skb->encapsulation = 0;
934 err = IP_ECN_decapsulate(oip, skb);
937 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
938 &oip->saddr, oip->tos);
940 ++vxlan->dev->stats.rx_frame_errors;
941 ++vxlan->dev->stats.rx_errors;
946 stats = this_cpu_ptr(vxlan->dev->tstats);
947 u64_stats_update_begin(&stats->syncp);
949 stats->rx_bytes += skb->len;
950 u64_stats_update_end(&stats->syncp);
956 /* Put UDP header back */
957 __skb_push(skb, sizeof(struct udphdr));
961 /* Consume bad packet */
966 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
968 struct vxlan_dev *vxlan = netdev_priv(dev);
974 if (dev->flags & IFF_NOARP)
977 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
978 dev->stats.tx_dropped++;
983 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
984 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
985 parp->ar_pro != htons(ETH_P_IP) ||
986 parp->ar_op != htons(ARPOP_REQUEST) ||
987 parp->ar_hln != dev->addr_len ||
990 arpptr = (u8 *)parp + sizeof(struct arphdr);
992 arpptr += dev->addr_len; /* sha */
993 memcpy(&sip, arpptr, sizeof(sip));
994 arpptr += sizeof(sip);
995 arpptr += dev->addr_len; /* tha */
996 memcpy(&tip, arpptr, sizeof(tip));
998 if (ipv4_is_loopback(tip) ||
999 ipv4_is_multicast(tip))
1002 n = neigh_lookup(&arp_tbl, &tip, dev);
1005 struct vxlan_fdb *f;
1006 struct sk_buff *reply;
1008 if (!(n->nud_state & NUD_CONNECTED)) {
1013 f = vxlan_find_mac(vxlan, n->ha);
1014 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
1015 /* bridge-local neighbor */
1020 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1025 skb_reset_mac_header(reply);
1026 __skb_pull(reply, skb_network_offset(reply));
1027 reply->ip_summed = CHECKSUM_UNNECESSARY;
1028 reply->pkt_type = PACKET_HOST;
1030 if (netif_rx_ni(reply) == NET_RX_DROP)
1031 dev->stats.rx_dropped++;
1032 } else if (vxlan->flags & VXLAN_F_L3MISS)
1033 vxlan_ip_miss(dev, tip);
1036 return NETDEV_TX_OK;
1039 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1041 struct vxlan_dev *vxlan = netdev_priv(dev);
1042 struct neighbour *n;
1045 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1049 switch (ntohs(eth_hdr(skb)->h_proto)) {
1051 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1054 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1063 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1065 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1067 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1071 } else if (vxlan->flags & VXLAN_F_L3MISS)
1072 vxlan_ip_miss(dev, pip->daddr);
1076 static void vxlan_sock_put(struct sk_buff *skb)
1081 /* On transmit, associate with the tunnel socket */
1082 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1084 struct vxlan_dev *vxlan = netdev_priv(dev);
1085 struct sock *sk = vxlan->vn_sock->sock->sk;
1090 skb->destructor = vxlan_sock_put;
1093 /* Compute source port for outgoing packet
1094 * first choice to use L4 flow hash since it will spread
1095 * better and maybe available from hardware
1096 * secondary choice is to use jhash on the Ethernet header
1098 static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
1100 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1103 hash = skb_get_rxhash(skb);
1105 hash = jhash(skb->data, 2 * ETH_ALEN,
1106 (__force u32) skb->protocol);
1108 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
1111 static int handle_offloads(struct sk_buff *skb)
1113 if (skb_is_gso(skb)) {
1114 int err = skb_unclone(skb, GFP_ATOMIC);
1118 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1119 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1120 skb->ip_summed = CHECKSUM_NONE;
1125 /* Bypass encapsulation if the destination is local */
1126 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1127 struct vxlan_dev *dst_vxlan)
1129 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1130 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1132 skb->pkt_type = PACKET_HOST;
1133 skb->encapsulation = 0;
1134 skb->dev = dst_vxlan->dev;
1135 __skb_pull(skb, skb_network_offset(skb));
1137 if (dst_vxlan->flags & VXLAN_F_LEARN)
1138 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1139 eth_hdr(skb)->h_source);
1141 u64_stats_update_begin(&tx_stats->syncp);
1142 tx_stats->tx_packets++;
1143 tx_stats->tx_bytes += skb->len;
1144 u64_stats_update_end(&tx_stats->syncp);
1146 if (netif_rx(skb) == NET_RX_SUCCESS) {
1147 u64_stats_update_begin(&rx_stats->syncp);
1148 rx_stats->rx_packets++;
1149 rx_stats->rx_bytes += skb->len;
1150 u64_stats_update_end(&rx_stats->syncp);
1152 skb->dev->stats.rx_dropped++;
1156 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1157 struct vxlan_rdst *rdst, bool did_rsc)
1159 struct vxlan_dev *vxlan = netdev_priv(dev);
1161 const struct iphdr *old_iph;
1162 struct vxlanhdr *vxh;
1166 __be16 src_port, dst_port;
1172 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1173 vni = rdst->remote_vni;
1174 dst = rdst->remote_ip;
1178 /* short-circuited back to local bridge */
1179 vxlan_encap_bypass(skb, vxlan, vxlan);
1185 if (!skb->encapsulation) {
1186 skb_reset_inner_headers(skb);
1187 skb->encapsulation = 1;
1190 /* Need space for new headers (invalidates iph ptr) */
1191 if (skb_cow_head(skb, VXLAN_HEADROOM))
1194 old_iph = ip_hdr(skb);
1197 if (!ttl && IN_MULTICAST(ntohl(dst)))
1202 tos = ip_tunnel_get_dsfield(old_iph, skb);
1204 src_port = vxlan_src_port(vxlan, skb);
1206 memset(&fl4, 0, sizeof(fl4));
1207 fl4.flowi4_oif = rdst->remote_ifindex;
1208 fl4.flowi4_tos = RT_TOS(tos);
1210 fl4.saddr = vxlan->saddr;
1212 rt = ip_route_output_key(dev_net(dev), &fl4);
1214 netdev_dbg(dev, "no route to %pI4\n", &dst);
1215 dev->stats.tx_carrier_errors++;
1219 if (rt->dst.dev == dev) {
1220 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1222 dev->stats.collisions++;
1226 /* Bypass encapsulation if the destination is local */
1227 if (rt->rt_flags & RTCF_LOCAL &&
1228 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1229 struct vxlan_dev *dst_vxlan;
1232 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1235 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1238 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1239 vxh->vx_flags = htonl(VXLAN_FLAGS);
1240 vxh->vx_vni = htonl(vni << 8);
1242 __skb_push(skb, sizeof(*uh));
1243 skb_reset_transport_header(skb);
1246 uh->dest = dst_port;
1247 uh->source = src_port;
1249 uh->len = htons(skb->len);
1252 vxlan_set_owner(dev, skb);
1254 if (handle_offloads(skb))
1257 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1258 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1260 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1261 IPPROTO_UDP, tos, ttl, df);
1262 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1267 dev->stats.tx_dropped++;
1271 dev->stats.tx_errors++;
1276 /* Transmit local packets over Vxlan
1278 * Outer IP header inherits ECN and DF from inner header.
1279 * Outer UDP destination is the VXLAN assigned port.
1280 * source port is based on hash of flow
1282 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1284 struct vxlan_dev *vxlan = netdev_priv(dev);
1286 bool did_rsc = false;
1287 struct vxlan_rdst *rdst;
1288 struct vxlan_fdb *f;
1290 skb_reset_mac_header(skb);
1293 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1294 return arp_reduce(dev, skb);
1296 f = vxlan_find_mac(vxlan, eth->h_dest);
1299 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1300 ntohs(eth->h_proto) == ETH_P_IP) {
1301 did_rsc = route_shortcircuit(dev, skb);
1303 f = vxlan_find_mac(vxlan, eth->h_dest);
1307 f = vxlan_find_mac(vxlan, all_zeros_mac);
1309 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1310 !is_multicast_ether_addr(eth->h_dest))
1311 vxlan_fdb_miss(vxlan, eth->h_dest);
1313 dev->stats.tx_dropped++;
1315 return NETDEV_TX_OK;
1319 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1320 struct sk_buff *skb1;
1322 skb1 = skb_clone(skb, GFP_ATOMIC);
1324 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1328 return NETDEV_TX_OK;
1331 /* Walk the forwarding table and purge stale entries */
1332 static void vxlan_cleanup(unsigned long arg)
1334 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1335 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1338 if (!netif_running(vxlan->dev))
1341 spin_lock_bh(&vxlan->hash_lock);
1342 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1343 struct hlist_node *p, *n;
1344 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1346 = container_of(p, struct vxlan_fdb, hlist);
1347 unsigned long timeout;
1349 if (f->state & NUD_PERMANENT)
1352 timeout = f->used + vxlan->age_interval * HZ;
1353 if (time_before_eq(timeout, jiffies)) {
1354 netdev_dbg(vxlan->dev,
1355 "garbage collect %pM\n",
1357 f->state = NUD_STALE;
1358 vxlan_fdb_destroy(vxlan, f);
1359 } else if (time_before(timeout, next_timer))
1360 next_timer = timeout;
1363 spin_unlock_bh(&vxlan->hash_lock);
1365 mod_timer(&vxlan->age_timer, next_timer);
1368 /* Setup stats when device is created */
1369 static int vxlan_init(struct net_device *dev)
1371 struct vxlan_dev *vxlan = netdev_priv(dev);
1372 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1373 struct vxlan_sock *vs;
1374 __u32 vni = vxlan->default_dst.remote_vni;
1376 dev->tstats = alloc_percpu(struct pcpu_tstats);
1380 spin_lock(&vn->sock_lock);
1381 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1383 /* If we have a socket with same port already, reuse it */
1384 atomic_inc(&vs->refcnt);
1385 vxlan->vn_sock = vs;
1386 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1388 /* otherwise make new socket outside of RTNL */
1390 queue_work(vxlan_wq, &vxlan->sock_work);
1392 spin_unlock(&vn->sock_lock);
1397 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1399 struct vxlan_fdb *f;
1401 spin_lock_bh(&vxlan->hash_lock);
1402 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1404 vxlan_fdb_destroy(vxlan, f);
1405 spin_unlock_bh(&vxlan->hash_lock);
1408 static void vxlan_uninit(struct net_device *dev)
1410 struct vxlan_dev *vxlan = netdev_priv(dev);
1411 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1412 struct vxlan_sock *vs = vxlan->vn_sock;
1414 vxlan_fdb_delete_default(vxlan);
1417 vxlan_sock_release(vn, vs);
1418 free_percpu(dev->tstats);
1421 /* Start ageing timer and join group when device is brought up */
1422 static int vxlan_open(struct net_device *dev)
1424 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1425 struct vxlan_dev *vxlan = netdev_priv(dev);
1426 struct vxlan_sock *vs = vxlan->vn_sock;
1428 /* socket hasn't been created */
1432 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1433 vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1434 vxlan_sock_hold(vs);
1436 queue_work(vxlan_wq, &vxlan->igmp_join);
1439 if (vxlan->age_interval)
1440 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1445 /* Purge the forwarding table */
1446 static void vxlan_flush(struct vxlan_dev *vxlan)
1450 spin_lock_bh(&vxlan->hash_lock);
1451 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1452 struct hlist_node *p, *n;
1453 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1455 = container_of(p, struct vxlan_fdb, hlist);
1456 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1457 if (!is_zero_ether_addr(f->eth_addr))
1458 vxlan_fdb_destroy(vxlan, f);
1461 spin_unlock_bh(&vxlan->hash_lock);
1464 /* Cleanup timer and forwarding table on shutdown */
1465 static int vxlan_stop(struct net_device *dev)
1467 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1468 struct vxlan_dev *vxlan = netdev_priv(dev);
1469 struct vxlan_sock *vs = vxlan->vn_sock;
1471 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1472 ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1473 vxlan_sock_hold(vs);
1475 queue_work(vxlan_wq, &vxlan->igmp_leave);
1478 del_timer_sync(&vxlan->age_timer);
1485 /* Stub, nothing needs to be done. */
1486 static void vxlan_set_multicast_list(struct net_device *dev)
1490 static const struct net_device_ops vxlan_netdev_ops = {
1491 .ndo_init = vxlan_init,
1492 .ndo_uninit = vxlan_uninit,
1493 .ndo_open = vxlan_open,
1494 .ndo_stop = vxlan_stop,
1495 .ndo_start_xmit = vxlan_xmit,
1496 .ndo_get_stats64 = ip_tunnel_get_stats64,
1497 .ndo_set_rx_mode = vxlan_set_multicast_list,
1498 .ndo_change_mtu = eth_change_mtu,
1499 .ndo_validate_addr = eth_validate_addr,
1500 .ndo_set_mac_address = eth_mac_addr,
1501 .ndo_fdb_add = vxlan_fdb_add,
1502 .ndo_fdb_del = vxlan_fdb_delete,
1503 .ndo_fdb_dump = vxlan_fdb_dump,
1506 /* Info for udev, that this is a virtual tunnel endpoint */
1507 static struct device_type vxlan_type = {
1511 /* Initialize the device structure. */
1512 static void vxlan_setup(struct net_device *dev)
1514 struct vxlan_dev *vxlan = netdev_priv(dev);
1518 eth_hw_addr_random(dev);
1520 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1522 dev->netdev_ops = &vxlan_netdev_ops;
1523 dev->destructor = free_netdev;
1524 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1526 dev->tx_queue_len = 0;
1527 dev->features |= NETIF_F_LLTX;
1528 dev->features |= NETIF_F_NETNS_LOCAL;
1529 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1530 dev->features |= NETIF_F_RXCSUM;
1531 dev->features |= NETIF_F_GSO_SOFTWARE;
1533 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1534 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1535 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1536 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1538 INIT_LIST_HEAD(&vxlan->next);
1539 spin_lock_init(&vxlan->hash_lock);
1540 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1541 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
1542 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
1544 init_timer_deferrable(&vxlan->age_timer);
1545 vxlan->age_timer.function = vxlan_cleanup;
1546 vxlan->age_timer.data = (unsigned long) vxlan;
1548 inet_get_local_port_range(&low, &high);
1549 vxlan->port_min = low;
1550 vxlan->port_max = high;
1551 vxlan->dst_port = htons(vxlan_port);
1555 for (h = 0; h < FDB_HASH_SIZE; ++h)
1556 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1559 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1560 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1561 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1562 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1563 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1564 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1565 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1566 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1567 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1568 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1569 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1570 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1571 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1572 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1573 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1574 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
1577 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1579 if (tb[IFLA_ADDRESS]) {
1580 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1581 pr_debug("invalid link address (not ethernet)\n");
1585 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1586 pr_debug("invalid all zero ethernet address\n");
1587 return -EADDRNOTAVAIL;
1594 if (data[IFLA_VXLAN_ID]) {
1595 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1596 if (id >= VXLAN_VID_MASK)
1600 if (data[IFLA_VXLAN_PORT_RANGE]) {
1601 const struct ifla_vxlan_port_range *p
1602 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1604 if (ntohs(p->high) < ntohs(p->low)) {
1605 pr_debug("port range %u .. %u not valid\n",
1606 ntohs(p->low), ntohs(p->high));
1614 static void vxlan_get_drvinfo(struct net_device *netdev,
1615 struct ethtool_drvinfo *drvinfo)
1617 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1618 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1621 static const struct ethtool_ops vxlan_ethtool_ops = {
1622 .get_drvinfo = vxlan_get_drvinfo,
1623 .get_link = ethtool_op_get_link,
1626 static void vxlan_del_work(struct work_struct *work)
1628 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1630 sk_release_kernel(vs->sock->sk);
1634 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1636 struct vxlan_sock *vs;
1638 struct sockaddr_in vxlan_addr = {
1639 .sin_family = AF_INET,
1640 .sin_addr.s_addr = htonl(INADDR_ANY),
1646 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1648 return ERR_PTR(-ENOMEM);
1650 for (h = 0; h < VNI_HASH_SIZE; ++h)
1651 INIT_HLIST_HEAD(&vs->vni_list[h]);
1653 INIT_WORK(&vs->del_work, vxlan_del_work);
1655 /* Create UDP socket for encapsulation receive. */
1656 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1658 pr_debug("UDP socket create failed\n");
1663 /* Put in proper namespace */
1665 sk_change_net(sk, net);
1667 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1668 sizeof(vxlan_addr));
1670 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1671 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1672 sk_release_kernel(sk);
1677 /* Disable multicast loopback */
1678 inet_sk(sk)->mc_loop = 0;
1680 /* Mark socket as an encapsulation socket. */
1681 udp_sk(sk)->encap_type = 1;
1682 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1684 atomic_set(&vs->refcnt, 1);
1689 /* Scheduled at device creation to bind to a socket */
1690 static void vxlan_sock_work(struct work_struct *work)
1692 struct vxlan_dev *vxlan
1693 = container_of(work, struct vxlan_dev, sock_work);
1694 struct net_device *dev = vxlan->dev;
1695 struct net *net = dev_net(dev);
1696 __u32 vni = vxlan->default_dst.remote_vni;
1697 __be16 port = vxlan->dst_port;
1698 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1699 struct vxlan_sock *nvs, *ovs;
1701 nvs = vxlan_socket_create(net, port);
1703 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1708 spin_lock(&vn->sock_lock);
1709 /* Look again to see if can reuse socket */
1710 ovs = vxlan_find_port(net, port);
1712 atomic_inc(&ovs->refcnt);
1713 vxlan->vn_sock = ovs;
1714 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1715 spin_unlock(&vn->sock_lock);
1717 sk_release_kernel(nvs->sock->sk);
1720 vxlan->vn_sock = nvs;
1721 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1722 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1723 spin_unlock(&vn->sock_lock);
1729 static int vxlan_newlink(struct net *net, struct net_device *dev,
1730 struct nlattr *tb[], struct nlattr *data[])
1732 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1733 struct vxlan_dev *vxlan = netdev_priv(dev);
1734 struct vxlan_rdst *dst = &vxlan->default_dst;
1738 if (!data[IFLA_VXLAN_ID])
1741 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1742 dst->remote_vni = vni;
1744 if (data[IFLA_VXLAN_GROUP])
1745 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1747 if (data[IFLA_VXLAN_LOCAL])
1748 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1750 if (data[IFLA_VXLAN_LINK] &&
1751 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1752 struct net_device *lowerdev
1753 = __dev_get_by_index(net, dst->remote_ifindex);
1756 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1761 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1763 /* update header length based on lower device */
1764 dev->hard_header_len = lowerdev->hard_header_len +
1768 if (data[IFLA_VXLAN_TOS])
1769 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1771 if (data[IFLA_VXLAN_TTL])
1772 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1774 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1775 vxlan->flags |= VXLAN_F_LEARN;
1777 if (data[IFLA_VXLAN_AGEING])
1778 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1780 vxlan->age_interval = FDB_AGE_DEFAULT;
1782 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1783 vxlan->flags |= VXLAN_F_PROXY;
1785 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1786 vxlan->flags |= VXLAN_F_RSC;
1788 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1789 vxlan->flags |= VXLAN_F_L2MISS;
1791 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1792 vxlan->flags |= VXLAN_F_L3MISS;
1794 if (data[IFLA_VXLAN_LIMIT])
1795 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1797 if (data[IFLA_VXLAN_PORT_RANGE]) {
1798 const struct ifla_vxlan_port_range *p
1799 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1800 vxlan->port_min = ntohs(p->low);
1801 vxlan->port_max = ntohs(p->high);
1804 if (data[IFLA_VXLAN_PORT])
1805 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1807 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1808 pr_info("duplicate VNI %u\n", vni);
1812 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1814 /* create an fdb entry for default destination */
1815 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1816 vxlan->default_dst.remote_ip,
1817 NUD_REACHABLE|NUD_PERMANENT,
1818 NLM_F_EXCL|NLM_F_CREATE,
1819 vxlan->dst_port, vxlan->default_dst.remote_vni,
1820 vxlan->default_dst.remote_ifindex, NTF_SELF);
1824 err = register_netdevice(dev);
1826 vxlan_fdb_delete_default(vxlan);
1830 list_add(&vxlan->next, &vn->vxlan_list);
1835 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1837 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1838 struct vxlan_dev *vxlan = netdev_priv(dev);
1840 spin_lock(&vn->sock_lock);
1841 hlist_del_rcu(&vxlan->hlist);
1842 spin_unlock(&vn->sock_lock);
1844 list_del(&vxlan->next);
1845 unregister_netdevice_queue(dev, head);
1848 static size_t vxlan_get_size(const struct net_device *dev)
1851 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1852 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1853 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1854 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1855 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1856 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1857 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1858 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1859 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1860 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1861 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1862 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1863 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1864 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1865 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
1869 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1871 const struct vxlan_dev *vxlan = netdev_priv(dev);
1872 const struct vxlan_rdst *dst = &vxlan->default_dst;
1873 struct ifla_vxlan_port_range ports = {
1874 .low = htons(vxlan->port_min),
1875 .high = htons(vxlan->port_max),
1878 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1879 goto nla_put_failure;
1881 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1882 goto nla_put_failure;
1884 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1885 goto nla_put_failure;
1887 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1888 goto nla_put_failure;
1890 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1891 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1892 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1893 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1894 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1895 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1896 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1897 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1898 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1899 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1900 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1901 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1902 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1903 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
1904 goto nla_put_failure;
1906 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1907 goto nla_put_failure;
1915 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1917 .maxtype = IFLA_VXLAN_MAX,
1918 .policy = vxlan_policy,
1919 .priv_size = sizeof(struct vxlan_dev),
1920 .setup = vxlan_setup,
1921 .validate = vxlan_validate,
1922 .newlink = vxlan_newlink,
1923 .dellink = vxlan_dellink,
1924 .get_size = vxlan_get_size,
1925 .fill_info = vxlan_fill_info,
1928 static __net_init int vxlan_init_net(struct net *net)
1930 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1933 INIT_LIST_HEAD(&vn->vxlan_list);
1934 spin_lock_init(&vn->sock_lock);
1936 for (h = 0; h < PORT_HASH_SIZE; ++h)
1937 INIT_HLIST_HEAD(&vn->sock_list[h]);
1942 static __net_exit void vxlan_exit_net(struct net *net)
1944 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1945 struct vxlan_dev *vxlan;
1949 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1950 unregister_netdevice_queue(vxlan->dev, &list);
1951 unregister_netdevice_many(&list);
1955 static struct pernet_operations vxlan_net_ops = {
1956 .init = vxlan_init_net,
1957 .exit = vxlan_exit_net,
1958 .id = &vxlan_net_id,
1959 .size = sizeof(struct vxlan_net),
1962 static int __init vxlan_init_module(void)
1966 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1970 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1972 rc = register_pernet_device(&vxlan_net_ops);
1976 rc = rtnl_link_register(&vxlan_link_ops);
1983 unregister_pernet_device(&vxlan_net_ops);
1985 destroy_workqueue(vxlan_wq);
1988 late_initcall(vxlan_init_module);
1990 static void __exit vxlan_cleanup_module(void)
1992 rtnl_link_unregister(&vxlan_link_ops);
1993 destroy_workqueue(vxlan_wq);
1994 unregister_pernet_device(&vxlan_net_ops);
1997 module_exit(vxlan_cleanup_module);
1999 MODULE_LICENSE("GPL");
2000 MODULE_VERSION(VXLAN_VERSION);
2001 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2002 MODULE_ALIAS_RTNL_LINK("vxlan");