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 compatability with early adopters.
73 static unsigned int vxlan_port __read_mostly = 8472;
74 module_param_named(udp_port, vxlan_port, uint, 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 unsigned int vxlan_net_id;
83 /* per UDP socket information */
85 struct hlist_node hlist;
87 struct work_struct del_work;
90 struct hlist_head vni_list[VNI_HASH_SIZE];
93 /* per-network namespace private data for this module */
95 struct list_head vxlan_list;
96 struct hlist_head sock_list[PORT_HASH_SIZE];
105 struct vxlan_rdst *remote_next;
108 /* Forwarding table entry */
110 struct hlist_node hlist; /* linked list of entries */
112 unsigned long updated; /* jiffies */
114 struct vxlan_rdst remote;
115 u16 state; /* see ndm_state */
116 u8 flags; /* see ndm_flags */
117 u8 eth_addr[ETH_ALEN];
120 /* Pseudo network device */
122 struct hlist_node hlist; /* vni hash table */
123 struct list_head next; /* vxlan's per namespace list */
124 struct vxlan_sock *vn_sock; /* listening socket */
125 struct net_device *dev;
126 struct vxlan_rdst default_dst; /* default destination */
127 __be32 saddr; /* source address */
129 __u16 port_min; /* source port range */
131 __u8 tos; /* TOS override */
133 u32 flags; /* VXLAN_F_* below */
135 struct work_struct sock_work;
136 struct work_struct igmp_work;
138 unsigned long age_interval;
139 struct timer_list age_timer;
140 spinlock_t hash_lock;
141 unsigned int addrcnt;
142 unsigned int addrmax;
144 struct hlist_head fdb_head[FDB_HASH_SIZE];
147 #define VXLAN_F_LEARN 0x01
148 #define VXLAN_F_PROXY 0x02
149 #define VXLAN_F_RSC 0x04
150 #define VXLAN_F_L2MISS 0x08
151 #define VXLAN_F_L3MISS 0x10
153 /* salt for hash table */
154 static u32 vxlan_salt __read_mostly;
155 static struct workqueue_struct *vxlan_wq;
157 static void vxlan_sock_work(struct work_struct *work);
159 /* Virtual Network hash table head */
160 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
162 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
165 /* Socket hash table head */
166 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
168 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
170 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
173 /* Find VXLAN socket based on network namespace and UDP port */
174 static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
176 struct vxlan_sock *vs;
178 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
179 if (inet_sk(vs->sock->sk)->inet_sport == port)
185 /* Look up VNI in a per net namespace table */
186 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
188 struct vxlan_sock *vs;
189 struct vxlan_dev *vxlan;
191 vs = vxlan_find_port(net, port);
195 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
196 if (vxlan->default_dst.remote_vni == id)
203 /* Fill in neighbour message in skbuff. */
204 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
205 const struct vxlan_fdb *fdb,
206 u32 portid, u32 seq, int type, unsigned int flags,
207 const struct vxlan_rdst *rdst)
209 unsigned long now = jiffies;
210 struct nda_cacheinfo ci;
211 struct nlmsghdr *nlh;
213 bool send_ip, send_eth;
215 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
219 ndm = nlmsg_data(nlh);
220 memset(ndm, 0, sizeof(*ndm));
222 send_eth = send_ip = true;
224 if (type == RTM_GETNEIGH) {
225 ndm->ndm_family = AF_INET;
226 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
227 send_eth = !is_zero_ether_addr(fdb->eth_addr);
229 ndm->ndm_family = AF_BRIDGE;
230 ndm->ndm_state = fdb->state;
231 ndm->ndm_ifindex = vxlan->dev->ifindex;
232 ndm->ndm_flags = fdb->flags;
233 ndm->ndm_type = NDA_DST;
235 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
236 goto nla_put_failure;
238 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
239 goto nla_put_failure;
241 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
242 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
243 goto nla_put_failure;
244 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
245 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
246 goto nla_put_failure;
247 if (rdst->remote_ifindex &&
248 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
249 goto nla_put_failure;
251 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
252 ci.ndm_confirmed = 0;
253 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
256 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
257 goto nla_put_failure;
259 return nlmsg_end(skb, nlh);
262 nlmsg_cancel(skb, nlh);
266 static inline size_t vxlan_nlmsg_size(void)
268 return NLMSG_ALIGN(sizeof(struct ndmsg))
269 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
270 + nla_total_size(sizeof(__be32)) /* NDA_DST */
271 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
272 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
273 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
274 + nla_total_size(sizeof(struct nda_cacheinfo));
277 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
278 const struct vxlan_fdb *fdb, int type)
280 struct net *net = dev_net(vxlan->dev);
284 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
288 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
290 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
291 WARN_ON(err == -EMSGSIZE);
296 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
300 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
303 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
305 struct vxlan_dev *vxlan = netdev_priv(dev);
308 memset(&f, 0, sizeof f);
310 f.remote.remote_ip = ipa; /* goes to NDA_DST */
311 f.remote.remote_vni = VXLAN_N_VID;
313 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
316 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
320 memset(&f, 0, sizeof f);
322 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
324 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
327 /* Hash Ethernet address */
328 static u32 eth_hash(const unsigned char *addr)
330 u64 value = get_unaligned((u64 *)addr);
332 /* only want 6 bytes */
338 return hash_64(value, FDB_HASH_BITS);
341 /* Hash chain to use given mac address */
342 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
345 return &vxlan->fdb_head[eth_hash(mac)];
348 /* Look up Ethernet address in forwarding table */
349 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
353 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
356 hlist_for_each_entry_rcu(f, head, hlist) {
357 if (compare_ether_addr(mac, f->eth_addr) == 0)
364 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
369 f = __vxlan_find_mac(vxlan, mac);
376 /* Add/update destinations for multicast */
377 static int vxlan_fdb_append(struct vxlan_fdb *f,
378 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
380 struct vxlan_rdst *rd_prev, *rd;
383 for (rd = &f->remote; rd; rd = rd->remote_next) {
384 if (rd->remote_ip == ip &&
385 rd->remote_port == port &&
386 rd->remote_vni == vni &&
387 rd->remote_ifindex == ifindex)
391 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
395 rd->remote_port = port;
396 rd->remote_vni = vni;
397 rd->remote_ifindex = ifindex;
398 rd->remote_next = NULL;
399 rd_prev->remote_next = rd;
403 /* Add new entry to forwarding table -- assumes lock held */
404 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
405 const u8 *mac, __be32 ip,
406 __u16 state, __u16 flags,
407 __be16 port, __u32 vni, __u32 ifindex,
413 f = __vxlan_find_mac(vxlan, mac);
415 if (flags & NLM_F_EXCL) {
416 netdev_dbg(vxlan->dev,
417 "lost race to create %pM\n", mac);
420 if (f->state != state) {
422 f->updated = jiffies;
425 if (f->flags != ndm_flags) {
426 f->flags = ndm_flags;
427 f->updated = jiffies;
430 if ((flags & NLM_F_APPEND) &&
431 is_multicast_ether_addr(f->eth_addr)) {
432 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
439 if (!(flags & NLM_F_CREATE))
442 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
445 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
446 f = kmalloc(sizeof(*f), GFP_ATOMIC);
451 f->remote.remote_ip = ip;
452 f->remote.remote_port = port;
453 f->remote.remote_vni = vni;
454 f->remote.remote_ifindex = ifindex;
455 f->remote.remote_next = NULL;
457 f->flags = ndm_flags;
458 f->updated = f->used = jiffies;
459 memcpy(f->eth_addr, mac, ETH_ALEN);
462 hlist_add_head_rcu(&f->hlist,
463 vxlan_fdb_head(vxlan, mac));
467 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
472 static void vxlan_fdb_free(struct rcu_head *head)
474 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
476 while (f->remote.remote_next) {
477 struct vxlan_rdst *rd = f->remote.remote_next;
479 f->remote.remote_next = rd->remote_next;
485 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
487 netdev_dbg(vxlan->dev,
488 "delete %pM\n", f->eth_addr);
491 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
493 hlist_del_rcu(&f->hlist);
494 call_rcu(&f->rcu, vxlan_fdb_free);
497 /* Add static entry (via netlink) */
498 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
499 struct net_device *dev,
500 const unsigned char *addr, u16 flags)
502 struct vxlan_dev *vxlan = netdev_priv(dev);
503 struct net *net = dev_net(vxlan->dev);
509 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
510 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
515 if (tb[NDA_DST] == NULL)
518 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
519 return -EAFNOSUPPORT;
521 ip = nla_get_be32(tb[NDA_DST]);
524 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
526 port = nla_get_be16(tb[NDA_PORT]);
528 port = vxlan->dst_port;
531 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
533 vni = nla_get_u32(tb[NDA_VNI]);
535 vni = vxlan->default_dst.remote_vni;
537 if (tb[NDA_IFINDEX]) {
538 struct net_device *tdev;
540 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
542 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
543 tdev = dev_get_by_index(net, ifindex);
545 return -EADDRNOTAVAIL;
550 spin_lock_bh(&vxlan->hash_lock);
551 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
552 port, vni, ifindex, ndm->ndm_flags);
553 spin_unlock_bh(&vxlan->hash_lock);
558 /* Delete entry (via netlink) */
559 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
560 struct net_device *dev,
561 const unsigned char *addr)
563 struct vxlan_dev *vxlan = netdev_priv(dev);
567 spin_lock_bh(&vxlan->hash_lock);
568 f = vxlan_find_mac(vxlan, addr);
570 vxlan_fdb_destroy(vxlan, f);
573 spin_unlock_bh(&vxlan->hash_lock);
578 /* Dump forwarding table */
579 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
580 struct net_device *dev, int idx)
582 struct vxlan_dev *vxlan = netdev_priv(dev);
585 for (h = 0; h < FDB_HASH_SIZE; ++h) {
589 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
590 struct vxlan_rdst *rd;
591 for (rd = &f->remote; rd; rd = rd->remote_next) {
592 if (idx < cb->args[0])
595 err = vxlan_fdb_info(skb, vxlan, f,
596 NETLINK_CB(cb->skb).portid,
611 /* Watch incoming packets to learn mapping between Ethernet address
612 * and Tunnel endpoint.
613 * Return true if packet is bogus and should be droppped.
615 static bool vxlan_snoop(struct net_device *dev,
616 __be32 src_ip, const u8 *src_mac)
618 struct vxlan_dev *vxlan = netdev_priv(dev);
621 f = vxlan_find_mac(vxlan, src_mac);
623 if (likely(f->remote.remote_ip == src_ip))
626 /* Don't migrate static entries, drop packets */
627 if (f->state & NUD_NOARP)
632 "%pM migrated from %pI4 to %pI4\n",
633 src_mac, &f->remote.remote_ip, &src_ip);
635 f->remote.remote_ip = src_ip;
636 f->updated = jiffies;
637 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
639 /* learned new entry */
640 spin_lock(&vxlan->hash_lock);
642 /* close off race between vxlan_flush and incoming packets */
643 if (netif_running(dev))
644 vxlan_fdb_create(vxlan, src_mac, src_ip,
646 NLM_F_EXCL|NLM_F_CREATE,
648 vxlan->default_dst.remote_vni,
650 spin_unlock(&vxlan->hash_lock);
657 /* See if multicast group is already in use by other ID */
658 static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
660 struct vxlan_dev *vxlan;
662 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
663 if (!netif_running(vxlan->dev))
666 if (vxlan->default_dst.remote_ip == remote_ip)
673 static void vxlan_sock_hold(struct vxlan_sock *vs)
675 atomic_inc(&vs->refcnt);
678 static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
680 if (!atomic_dec_and_test(&vs->refcnt))
683 spin_lock(&vn->sock_lock);
684 hlist_del_rcu(&vs->hlist);
685 spin_unlock(&vn->sock_lock);
687 queue_work(vxlan_wq, &vs->del_work);
690 /* Callback to update multicast group membership.
691 * Scheduled when vxlan goes up/down.
693 static void vxlan_igmp_work(struct work_struct *work)
695 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
696 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
697 struct vxlan_sock *vs = vxlan->vn_sock;
698 struct sock *sk = vs->sock->sk;
699 struct ip_mreqn mreq = {
700 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
701 .imr_ifindex = vxlan->default_dst.remote_ifindex,
705 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
706 ip_mc_join_group(sk, &mreq);
708 ip_mc_leave_group(sk, &mreq);
711 vxlan_sock_release(vn, vs);
715 /* Callback from net/ipv4/udp.c to receive packets */
716 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
719 struct vxlanhdr *vxh;
720 struct vxlan_dev *vxlan;
721 struct pcpu_tstats *stats;
726 /* pop off outer UDP header */
727 __skb_pull(skb, sizeof(struct udphdr));
729 /* Need Vxlan and inner Ethernet header to be present */
730 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
733 /* Drop packets with reserved bits set */
734 vxh = (struct vxlanhdr *) skb->data;
735 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
736 (vxh->vx_vni & htonl(0xff))) {
737 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
738 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
742 __skb_pull(skb, sizeof(struct vxlanhdr));
744 /* Is this VNI defined? */
745 vni = ntohl(vxh->vx_vni) >> 8;
746 port = inet_sk(sk)->inet_sport;
747 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
749 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
754 if (!pskb_may_pull(skb, ETH_HLEN)) {
755 vxlan->dev->stats.rx_length_errors++;
756 vxlan->dev->stats.rx_errors++;
760 skb_reset_mac_header(skb);
762 /* Re-examine inner Ethernet packet */
764 skb->protocol = eth_type_trans(skb, vxlan->dev);
766 /* Ignore packet loops (and multicast echo) */
767 if (compare_ether_addr(eth_hdr(skb)->h_source,
768 vxlan->dev->dev_addr) == 0)
771 if ((vxlan->flags & VXLAN_F_LEARN) &&
772 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
775 __skb_tunnel_rx(skb, vxlan->dev);
776 skb_reset_network_header(skb);
778 /* If the NIC driver gave us an encapsulated packet with
779 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
780 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
781 * for us. Otherwise force the upper layers to verify it.
783 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
784 !(vxlan->dev->features & NETIF_F_RXCSUM))
785 skb->ip_summed = CHECKSUM_NONE;
787 skb->encapsulation = 0;
789 err = IP_ECN_decapsulate(oip, skb);
792 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
793 &oip->saddr, oip->tos);
795 ++vxlan->dev->stats.rx_frame_errors;
796 ++vxlan->dev->stats.rx_errors;
801 stats = this_cpu_ptr(vxlan->dev->tstats);
802 u64_stats_update_begin(&stats->syncp);
804 stats->rx_bytes += skb->len;
805 u64_stats_update_end(&stats->syncp);
811 /* Put UDP header back */
812 __skb_push(skb, sizeof(struct udphdr));
816 /* Consume bad packet */
821 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
823 struct vxlan_dev *vxlan = netdev_priv(dev);
829 if (dev->flags & IFF_NOARP)
832 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
833 dev->stats.tx_dropped++;
838 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
839 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
840 parp->ar_pro != htons(ETH_P_IP) ||
841 parp->ar_op != htons(ARPOP_REQUEST) ||
842 parp->ar_hln != dev->addr_len ||
845 arpptr = (u8 *)parp + sizeof(struct arphdr);
847 arpptr += dev->addr_len; /* sha */
848 memcpy(&sip, arpptr, sizeof(sip));
849 arpptr += sizeof(sip);
850 arpptr += dev->addr_len; /* tha */
851 memcpy(&tip, arpptr, sizeof(tip));
853 if (ipv4_is_loopback(tip) ||
854 ipv4_is_multicast(tip))
857 n = neigh_lookup(&arp_tbl, &tip, dev);
861 struct sk_buff *reply;
863 if (!(n->nud_state & NUD_CONNECTED)) {
868 f = vxlan_find_mac(vxlan, n->ha);
869 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
870 /* bridge-local neighbor */
875 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
880 skb_reset_mac_header(reply);
881 __skb_pull(reply, skb_network_offset(reply));
882 reply->ip_summed = CHECKSUM_UNNECESSARY;
883 reply->pkt_type = PACKET_HOST;
885 if (netif_rx_ni(reply) == NET_RX_DROP)
886 dev->stats.rx_dropped++;
887 } else if (vxlan->flags & VXLAN_F_L3MISS)
888 vxlan_ip_miss(dev, tip);
894 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
896 struct vxlan_dev *vxlan = netdev_priv(dev);
900 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
904 switch (ntohs(eth_hdr(skb)->h_proto)) {
906 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
909 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
918 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
920 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
922 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
926 } else if (vxlan->flags & VXLAN_F_L3MISS)
927 vxlan_ip_miss(dev, pip->daddr);
931 static void vxlan_sock_put(struct sk_buff *skb)
936 /* On transmit, associate with the tunnel socket */
937 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
939 struct vxlan_dev *vxlan = netdev_priv(dev);
940 struct sock *sk = vxlan->vn_sock->sock->sk;
945 skb->destructor = vxlan_sock_put;
948 /* Compute source port for outgoing packet
949 * first choice to use L4 flow hash since it will spread
950 * better and maybe available from hardware
951 * secondary choice is to use jhash on the Ethernet header
953 static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
955 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
958 hash = skb_get_rxhash(skb);
960 hash = jhash(skb->data, 2 * ETH_ALEN,
961 (__force u32) skb->protocol);
963 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
966 static int handle_offloads(struct sk_buff *skb)
968 if (skb_is_gso(skb)) {
969 int err = skb_unclone(skb, GFP_ATOMIC);
973 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
974 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
975 skb->ip_summed = CHECKSUM_NONE;
980 /* Bypass encapsulation if the destination is local */
981 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
982 struct vxlan_dev *dst_vxlan)
984 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
985 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
987 skb->pkt_type = PACKET_HOST;
988 skb->encapsulation = 0;
989 skb->dev = dst_vxlan->dev;
990 __skb_pull(skb, skb_network_offset(skb));
992 if (dst_vxlan->flags & VXLAN_F_LEARN)
993 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
994 eth_hdr(skb)->h_source);
996 u64_stats_update_begin(&tx_stats->syncp);
997 tx_stats->tx_packets++;
998 tx_stats->tx_bytes += skb->len;
999 u64_stats_update_end(&tx_stats->syncp);
1001 if (netif_rx(skb) == NET_RX_SUCCESS) {
1002 u64_stats_update_begin(&rx_stats->syncp);
1003 rx_stats->rx_packets++;
1004 rx_stats->rx_bytes += skb->len;
1005 u64_stats_update_end(&rx_stats->syncp);
1007 skb->dev->stats.rx_dropped++;
1011 static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1012 struct vxlan_rdst *rdst, bool did_rsc)
1014 struct vxlan_dev *vxlan = netdev_priv(dev);
1016 const struct iphdr *old_iph;
1017 struct vxlanhdr *vxh;
1021 __be16 src_port, dst_port;
1027 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1028 vni = rdst->remote_vni;
1029 dst = rdst->remote_ip;
1033 /* short-circuited back to local bridge */
1034 vxlan_encap_bypass(skb, vxlan, vxlan);
1035 return NETDEV_TX_OK;
1040 if (!skb->encapsulation) {
1041 skb_reset_inner_headers(skb);
1042 skb->encapsulation = 1;
1045 /* Need space for new headers (invalidates iph ptr) */
1046 if (skb_cow_head(skb, VXLAN_HEADROOM))
1049 old_iph = ip_hdr(skb);
1052 if (!ttl && IN_MULTICAST(ntohl(dst)))
1057 tos = ip_tunnel_get_dsfield(old_iph, skb);
1059 src_port = vxlan_src_port(vxlan, skb);
1061 memset(&fl4, 0, sizeof(fl4));
1062 fl4.flowi4_oif = rdst->remote_ifindex;
1063 fl4.flowi4_tos = RT_TOS(tos);
1065 fl4.saddr = vxlan->saddr;
1067 rt = ip_route_output_key(dev_net(dev), &fl4);
1069 netdev_dbg(dev, "no route to %pI4\n", &dst);
1070 dev->stats.tx_carrier_errors++;
1074 if (rt->dst.dev == dev) {
1075 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1077 dev->stats.collisions++;
1081 /* Bypass encapsulation if the destination is local */
1082 if (rt->rt_flags & RTCF_LOCAL &&
1083 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1084 struct vxlan_dev *dst_vxlan;
1087 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1090 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1091 return NETDEV_TX_OK;
1093 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1094 vxh->vx_flags = htonl(VXLAN_FLAGS);
1095 vxh->vx_vni = htonl(vni << 8);
1097 __skb_push(skb, sizeof(*uh));
1098 skb_reset_transport_header(skb);
1101 uh->dest = dst_port;
1102 uh->source = src_port;
1104 uh->len = htons(skb->len);
1107 vxlan_set_owner(dev, skb);
1109 if (handle_offloads(skb))
1112 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1113 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1115 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1116 IPPROTO_UDP, tos, ttl, df);
1117 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1119 return NETDEV_TX_OK;
1122 dev->stats.tx_dropped++;
1126 dev->stats.tx_errors++;
1129 return NETDEV_TX_OK;
1132 /* Transmit local packets over Vxlan
1134 * Outer IP header inherits ECN and DF from inner header.
1135 * Outer UDP destination is the VXLAN assigned port.
1136 * source port is based on hash of flow
1138 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1140 struct vxlan_dev *vxlan = netdev_priv(dev);
1142 bool did_rsc = false;
1143 struct vxlan_rdst *rdst0, *rdst;
1144 struct vxlan_fdb *f;
1147 skb_reset_mac_header(skb);
1150 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1151 return arp_reduce(dev, skb);
1153 f = vxlan_find_mac(vxlan, eth->h_dest);
1156 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1157 ntohs(eth->h_proto) == ETH_P_IP) {
1158 did_rsc = route_shortcircuit(dev, skb);
1160 f = vxlan_find_mac(vxlan, eth->h_dest);
1164 rdst0 = &vxlan->default_dst;
1166 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
1167 (vxlan->flags & VXLAN_F_L2MISS) &&
1168 !is_multicast_ether_addr(eth->h_dest))
1169 vxlan_fdb_miss(vxlan, eth->h_dest);
1175 /* if there are multiple destinations, send copies */
1176 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1177 struct sk_buff *skb1;
1179 skb1 = skb_clone(skb, GFP_ATOMIC);
1181 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1182 if (rc == NETDEV_TX_OK)
1187 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1188 if (rc == NETDEV_TX_OK)
1193 /* Walk the forwarding table and purge stale entries */
1194 static void vxlan_cleanup(unsigned long arg)
1196 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1197 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1200 if (!netif_running(vxlan->dev))
1203 spin_lock_bh(&vxlan->hash_lock);
1204 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1205 struct hlist_node *p, *n;
1206 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1208 = container_of(p, struct vxlan_fdb, hlist);
1209 unsigned long timeout;
1211 if (f->state & NUD_PERMANENT)
1214 timeout = f->used + vxlan->age_interval * HZ;
1215 if (time_before_eq(timeout, jiffies)) {
1216 netdev_dbg(vxlan->dev,
1217 "garbage collect %pM\n",
1219 f->state = NUD_STALE;
1220 vxlan_fdb_destroy(vxlan, f);
1221 } else if (time_before(timeout, next_timer))
1222 next_timer = timeout;
1225 spin_unlock_bh(&vxlan->hash_lock);
1227 mod_timer(&vxlan->age_timer, next_timer);
1230 /* Setup stats when device is created */
1231 static int vxlan_init(struct net_device *dev)
1233 struct vxlan_dev *vxlan = netdev_priv(dev);
1234 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1235 struct vxlan_sock *vs;
1236 __u32 vni = vxlan->default_dst.remote_vni;
1238 dev->tstats = alloc_percpu(struct pcpu_tstats);
1242 spin_lock(&vn->sock_lock);
1243 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1245 /* If we have a socket with same port already, reuse it */
1246 atomic_inc(&vs->refcnt);
1247 vxlan->vn_sock = vs;
1248 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1250 /* otherwise make new socket outside of RTNL */
1252 queue_work(vxlan_wq, &vxlan->sock_work);
1254 spin_unlock(&vn->sock_lock);
1259 /* Start ageing timer and join group when device is brought up */
1260 static int vxlan_open(struct net_device *dev)
1262 struct vxlan_dev *vxlan = netdev_priv(dev);
1263 struct vxlan_sock *vs = vxlan->vn_sock;
1265 /* socket hasn't been created */
1269 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1270 vxlan_sock_hold(vs);
1272 queue_work(vxlan_wq, &vxlan->igmp_work);
1275 if (vxlan->age_interval)
1276 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1281 /* Purge the forwarding table */
1282 static void vxlan_flush(struct vxlan_dev *vxlan)
1286 spin_lock_bh(&vxlan->hash_lock);
1287 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1288 struct hlist_node *p, *n;
1289 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1291 = container_of(p, struct vxlan_fdb, hlist);
1292 vxlan_fdb_destroy(vxlan, f);
1295 spin_unlock_bh(&vxlan->hash_lock);
1298 /* Cleanup timer and forwarding table on shutdown */
1299 static int vxlan_stop(struct net_device *dev)
1301 struct vxlan_dev *vxlan = netdev_priv(dev);
1302 struct vxlan_sock *vs = vxlan->vn_sock;
1304 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1305 vxlan_sock_hold(vs);
1307 queue_work(vxlan_wq, &vxlan->igmp_work);
1310 del_timer_sync(&vxlan->age_timer);
1317 /* Stub, nothing needs to be done. */
1318 static void vxlan_set_multicast_list(struct net_device *dev)
1322 static const struct net_device_ops vxlan_netdev_ops = {
1323 .ndo_init = vxlan_init,
1324 .ndo_open = vxlan_open,
1325 .ndo_stop = vxlan_stop,
1326 .ndo_start_xmit = vxlan_xmit,
1327 .ndo_get_stats64 = ip_tunnel_get_stats64,
1328 .ndo_set_rx_mode = vxlan_set_multicast_list,
1329 .ndo_change_mtu = eth_change_mtu,
1330 .ndo_validate_addr = eth_validate_addr,
1331 .ndo_set_mac_address = eth_mac_addr,
1332 .ndo_fdb_add = vxlan_fdb_add,
1333 .ndo_fdb_del = vxlan_fdb_delete,
1334 .ndo_fdb_dump = vxlan_fdb_dump,
1337 /* Info for udev, that this is a virtual tunnel endpoint */
1338 static struct device_type vxlan_type = {
1342 static void vxlan_free(struct net_device *dev)
1344 free_percpu(dev->tstats);
1348 /* Initialize the device structure. */
1349 static void vxlan_setup(struct net_device *dev)
1351 struct vxlan_dev *vxlan = netdev_priv(dev);
1355 eth_hw_addr_random(dev);
1357 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1359 dev->netdev_ops = &vxlan_netdev_ops;
1360 dev->destructor = vxlan_free;
1361 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1363 dev->tx_queue_len = 0;
1364 dev->features |= NETIF_F_LLTX;
1365 dev->features |= NETIF_F_NETNS_LOCAL;
1366 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1367 dev->features |= NETIF_F_RXCSUM;
1368 dev->features |= NETIF_F_GSO_SOFTWARE;
1370 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1371 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1372 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1373 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1375 INIT_LIST_HEAD(&vxlan->next);
1376 spin_lock_init(&vxlan->hash_lock);
1377 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
1378 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
1380 init_timer_deferrable(&vxlan->age_timer);
1381 vxlan->age_timer.function = vxlan_cleanup;
1382 vxlan->age_timer.data = (unsigned long) vxlan;
1384 inet_get_local_port_range(&low, &high);
1385 vxlan->port_min = low;
1386 vxlan->port_max = high;
1387 vxlan->dst_port = htons(vxlan_port);
1391 for (h = 0; h < FDB_HASH_SIZE; ++h)
1392 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1395 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1396 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1397 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1398 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1399 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1400 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1401 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1402 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1403 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1404 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1405 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1406 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1407 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1408 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1409 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1410 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
1413 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1415 if (tb[IFLA_ADDRESS]) {
1416 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1417 pr_debug("invalid link address (not ethernet)\n");
1421 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1422 pr_debug("invalid all zero ethernet address\n");
1423 return -EADDRNOTAVAIL;
1430 if (data[IFLA_VXLAN_ID]) {
1431 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1432 if (id >= VXLAN_VID_MASK)
1436 if (data[IFLA_VXLAN_PORT_RANGE]) {
1437 const struct ifla_vxlan_port_range *p
1438 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1440 if (ntohs(p->high) < ntohs(p->low)) {
1441 pr_debug("port range %u .. %u not valid\n",
1442 ntohs(p->low), ntohs(p->high));
1450 static void vxlan_get_drvinfo(struct net_device *netdev,
1451 struct ethtool_drvinfo *drvinfo)
1453 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1454 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1457 static const struct ethtool_ops vxlan_ethtool_ops = {
1458 .get_drvinfo = vxlan_get_drvinfo,
1459 .get_link = ethtool_op_get_link,
1462 static void vxlan_del_work(struct work_struct *work)
1464 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1466 sk_release_kernel(vs->sock->sk);
1470 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1472 struct vxlan_sock *vs;
1474 struct sockaddr_in vxlan_addr = {
1475 .sin_family = AF_INET,
1476 .sin_addr.s_addr = htonl(INADDR_ANY),
1481 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1483 return ERR_PTR(-ENOMEM);
1485 for (h = 0; h < VNI_HASH_SIZE; ++h)
1486 INIT_HLIST_HEAD(&vs->vni_list[h]);
1488 INIT_WORK(&vs->del_work, vxlan_del_work);
1490 /* Create UDP socket for encapsulation receive. */
1491 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1493 pr_debug("UDP socket create failed\n");
1498 /* Put in proper namespace */
1500 sk_change_net(sk, net);
1502 vxlan_addr.sin_port = port;
1504 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1505 sizeof(vxlan_addr));
1507 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1508 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1509 sk_release_kernel(sk);
1514 /* Disable multicast loopback */
1515 inet_sk(sk)->mc_loop = 0;
1517 /* Mark socket as an encapsulation socket. */
1518 udp_sk(sk)->encap_type = 1;
1519 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1521 atomic_set(&vs->refcnt, 1);
1526 /* Scheduled at device creation to bind to a socket */
1527 static void vxlan_sock_work(struct work_struct *work)
1529 struct vxlan_dev *vxlan
1530 = container_of(work, struct vxlan_dev, sock_work);
1531 struct net_device *dev = vxlan->dev;
1532 struct net *net = dev_net(dev);
1533 __u32 vni = vxlan->default_dst.remote_vni;
1534 __be16 port = vxlan->dst_port;
1535 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1536 struct vxlan_sock *nvs, *ovs;
1538 nvs = vxlan_socket_create(net, port);
1540 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1545 spin_lock(&vn->sock_lock);
1546 /* Look again to see if can reuse socket */
1547 ovs = vxlan_find_port(net, port);
1549 atomic_inc(&ovs->refcnt);
1550 vxlan->vn_sock = ovs;
1551 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1552 spin_unlock(&vn->sock_lock);
1554 sk_release_kernel(nvs->sock->sk);
1557 vxlan->vn_sock = nvs;
1558 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1559 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1560 spin_unlock(&vn->sock_lock);
1566 static int vxlan_newlink(struct net *net, struct net_device *dev,
1567 struct nlattr *tb[], struct nlattr *data[])
1569 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1570 struct vxlan_dev *vxlan = netdev_priv(dev);
1571 struct vxlan_rdst *dst = &vxlan->default_dst;
1575 if (!data[IFLA_VXLAN_ID])
1578 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1579 dst->remote_vni = vni;
1581 if (data[IFLA_VXLAN_GROUP])
1582 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1584 if (data[IFLA_VXLAN_LOCAL])
1585 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1587 if (data[IFLA_VXLAN_LINK] &&
1588 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1589 struct net_device *lowerdev
1590 = __dev_get_by_index(net, dst->remote_ifindex);
1593 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1598 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1600 /* update header length based on lower device */
1601 dev->hard_header_len = lowerdev->hard_header_len +
1605 if (data[IFLA_VXLAN_TOS])
1606 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1608 if (data[IFLA_VXLAN_TTL])
1609 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1611 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1612 vxlan->flags |= VXLAN_F_LEARN;
1614 if (data[IFLA_VXLAN_AGEING])
1615 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1617 vxlan->age_interval = FDB_AGE_DEFAULT;
1619 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1620 vxlan->flags |= VXLAN_F_PROXY;
1622 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1623 vxlan->flags |= VXLAN_F_RSC;
1625 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1626 vxlan->flags |= VXLAN_F_L2MISS;
1628 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1629 vxlan->flags |= VXLAN_F_L3MISS;
1631 if (data[IFLA_VXLAN_LIMIT])
1632 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1634 if (data[IFLA_VXLAN_PORT_RANGE]) {
1635 const struct ifla_vxlan_port_range *p
1636 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1637 vxlan->port_min = ntohs(p->low);
1638 vxlan->port_max = ntohs(p->high);
1641 if (data[IFLA_VXLAN_PORT])
1642 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1644 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1645 pr_info("duplicate VNI %u\n", vni);
1649 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1651 err = register_netdevice(dev);
1655 list_add(&vxlan->next, &vn->vxlan_list);
1660 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1662 struct vxlan_dev *vxlan = netdev_priv(dev);
1663 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1664 struct vxlan_sock *vs = vxlan->vn_sock;
1666 hlist_del_rcu(&vxlan->hlist);
1667 list_del(&vxlan->next);
1668 unregister_netdevice_queue(dev, head);
1670 vxlan_sock_release(vn, vs);
1673 static size_t vxlan_get_size(const struct net_device *dev)
1676 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1677 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1678 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1679 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1680 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1681 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1682 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1683 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1684 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1685 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1686 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1687 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1688 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1689 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1690 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
1694 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1696 const struct vxlan_dev *vxlan = netdev_priv(dev);
1697 const struct vxlan_rdst *dst = &vxlan->default_dst;
1698 struct ifla_vxlan_port_range ports = {
1699 .low = htons(vxlan->port_min),
1700 .high = htons(vxlan->port_max),
1703 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1704 goto nla_put_failure;
1706 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1707 goto nla_put_failure;
1709 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1710 goto nla_put_failure;
1712 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1713 goto nla_put_failure;
1715 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1716 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1717 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1718 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1719 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1720 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1721 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1722 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1723 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1724 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1725 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1726 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1727 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1728 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
1729 goto nla_put_failure;
1731 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1732 goto nla_put_failure;
1740 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1742 .maxtype = IFLA_VXLAN_MAX,
1743 .policy = vxlan_policy,
1744 .priv_size = sizeof(struct vxlan_dev),
1745 .setup = vxlan_setup,
1746 .validate = vxlan_validate,
1747 .newlink = vxlan_newlink,
1748 .dellink = vxlan_dellink,
1749 .get_size = vxlan_get_size,
1750 .fill_info = vxlan_fill_info,
1753 static __net_init int vxlan_init_net(struct net *net)
1755 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1758 INIT_LIST_HEAD(&vn->vxlan_list);
1759 spin_lock_init(&vn->sock_lock);
1761 for (h = 0; h < PORT_HASH_SIZE; ++h)
1762 INIT_HLIST_HEAD(&vn->sock_list[h]);
1767 static __net_exit void vxlan_exit_net(struct net *net)
1769 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1770 struct vxlan_dev *vxlan;
1773 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1774 dev_close(vxlan->dev);
1778 static struct pernet_operations vxlan_net_ops = {
1779 .init = vxlan_init_net,
1780 .exit = vxlan_exit_net,
1781 .id = &vxlan_net_id,
1782 .size = sizeof(struct vxlan_net),
1785 static int __init vxlan_init_module(void)
1789 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1793 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1795 rc = register_pernet_device(&vxlan_net_ops);
1799 rc = rtnl_link_register(&vxlan_link_ops);
1806 unregister_pernet_device(&vxlan_net_ops);
1808 destroy_workqueue(vxlan_wq);
1811 late_initcall(vxlan_init_module);
1813 static void __exit vxlan_cleanup_module(void)
1815 unregister_pernet_device(&vxlan_net_ops);
1816 rtnl_link_unregister(&vxlan_link_ops);
1817 destroy_workqueue(vxlan_wq);
1820 module_exit(vxlan_cleanup_module);
1822 MODULE_LICENSE("GPL");
1823 MODULE_VERSION(VXLAN_VERSION);
1824 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
1825 MODULE_ALIAS_RTNL_LINK("vxlan");