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];
104 struct vxlan_rdst *remote_next;
107 /* Forwarding table entry */
109 struct hlist_node hlist; /* linked list of entries */
111 unsigned long updated; /* jiffies */
113 struct vxlan_rdst remote;
114 u16 state; /* see ndm_state */
115 u8 flags; /* see ndm_flags */
116 u8 eth_addr[ETH_ALEN];
119 /* Pseudo network device */
121 struct hlist_node hlist; /* vni hash table */
122 struct list_head next; /* vxlan's per namespace list */
123 struct vxlan_sock *vn_sock; /* listening socket */
124 struct net_device *dev;
125 struct vxlan_rdst default_dst; /* default destination */
126 __be32 saddr; /* source address */
128 __u16 port_min; /* source port range */
130 __u8 tos; /* TOS override */
132 u32 flags; /* VXLAN_F_* below */
134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
143 #define VXLAN_F_LEARN 0x01
144 #define VXLAN_F_PROXY 0x02
145 #define VXLAN_F_RSC 0x04
146 #define VXLAN_F_L2MISS 0x08
147 #define VXLAN_F_L3MISS 0x10
149 /* salt for hash table */
150 static u32 vxlan_salt __read_mostly;
152 /* Virtual Network hash table head */
153 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
155 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
158 /* Socket hash table head */
159 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
161 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
163 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
166 /* Find VXLAN socket based on network namespace and UDP port */
167 static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
169 struct vxlan_sock *vs;
171 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
172 if (inet_sk(vs->sock->sk)->inet_sport == port)
178 /* Look up VNI in a per net namespace table */
179 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
181 struct vxlan_sock *vs;
182 struct vxlan_dev *vxlan;
184 vs = vxlan_find_port(net, port);
188 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
189 if (vxlan->default_dst.remote_vni == id)
196 /* Fill in neighbour message in skbuff. */
197 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
198 const struct vxlan_fdb *fdb,
199 u32 portid, u32 seq, int type, unsigned int flags,
200 const struct vxlan_rdst *rdst)
202 unsigned long now = jiffies;
203 struct nda_cacheinfo ci;
204 struct nlmsghdr *nlh;
206 bool send_ip, send_eth;
208 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
212 ndm = nlmsg_data(nlh);
213 memset(ndm, 0, sizeof(*ndm));
215 send_eth = send_ip = true;
217 if (type == RTM_GETNEIGH) {
218 ndm->ndm_family = AF_INET;
219 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
220 send_eth = !is_zero_ether_addr(fdb->eth_addr);
222 ndm->ndm_family = AF_BRIDGE;
223 ndm->ndm_state = fdb->state;
224 ndm->ndm_ifindex = vxlan->dev->ifindex;
225 ndm->ndm_flags = fdb->flags;
226 ndm->ndm_type = NDA_DST;
228 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
229 goto nla_put_failure;
231 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
232 goto nla_put_failure;
234 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
235 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
236 goto nla_put_failure;
237 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
238 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
239 goto nla_put_failure;
240 if (rdst->remote_ifindex &&
241 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
242 goto nla_put_failure;
244 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
245 ci.ndm_confirmed = 0;
246 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
249 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
250 goto nla_put_failure;
252 return nlmsg_end(skb, nlh);
255 nlmsg_cancel(skb, nlh);
259 static inline size_t vxlan_nlmsg_size(void)
261 return NLMSG_ALIGN(sizeof(struct ndmsg))
262 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
263 + nla_total_size(sizeof(__be32)) /* NDA_DST */
264 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
265 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
266 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
267 + nla_total_size(sizeof(struct nda_cacheinfo));
270 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
271 const struct vxlan_fdb *fdb, int type)
273 struct net *net = dev_net(vxlan->dev);
277 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
281 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
283 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
284 WARN_ON(err == -EMSGSIZE);
289 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
293 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
296 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
298 struct vxlan_dev *vxlan = netdev_priv(dev);
301 memset(&f, 0, sizeof f);
303 f.remote.remote_ip = ipa; /* goes to NDA_DST */
304 f.remote.remote_vni = VXLAN_N_VID;
306 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
309 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
313 memset(&f, 0, sizeof f);
315 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
317 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
320 /* Hash Ethernet address */
321 static u32 eth_hash(const unsigned char *addr)
323 u64 value = get_unaligned((u64 *)addr);
325 /* only want 6 bytes */
331 return hash_64(value, FDB_HASH_BITS);
334 /* Hash chain to use given mac address */
335 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
338 return &vxlan->fdb_head[eth_hash(mac)];
341 /* Look up Ethernet address in forwarding table */
342 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
346 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
349 hlist_for_each_entry_rcu(f, head, hlist) {
350 if (compare_ether_addr(mac, f->eth_addr) == 0)
357 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
362 f = __vxlan_find_mac(vxlan, mac);
369 /* Add/update destinations for multicast */
370 static int vxlan_fdb_append(struct vxlan_fdb *f,
371 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
373 struct vxlan_rdst *rd_prev, *rd;
376 for (rd = &f->remote; rd; rd = rd->remote_next) {
377 if (rd->remote_ip == ip &&
378 rd->remote_port == port &&
379 rd->remote_vni == vni &&
380 rd->remote_ifindex == ifindex)
384 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
388 rd->remote_port = port;
389 rd->remote_vni = vni;
390 rd->remote_ifindex = ifindex;
391 rd->remote_next = NULL;
392 rd_prev->remote_next = rd;
396 /* Add new entry to forwarding table -- assumes lock held */
397 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
398 const u8 *mac, __be32 ip,
399 __u16 state, __u16 flags,
400 __be16 port, __u32 vni, __u32 ifindex,
406 f = __vxlan_find_mac(vxlan, mac);
408 if (flags & NLM_F_EXCL) {
409 netdev_dbg(vxlan->dev,
410 "lost race to create %pM\n", mac);
413 if (f->state != state) {
415 f->updated = jiffies;
418 if (f->flags != ndm_flags) {
419 f->flags = ndm_flags;
420 f->updated = jiffies;
423 if ((flags & NLM_F_APPEND) &&
424 is_multicast_ether_addr(f->eth_addr)) {
425 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
432 if (!(flags & NLM_F_CREATE))
435 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
438 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
439 f = kmalloc(sizeof(*f), GFP_ATOMIC);
444 f->remote.remote_ip = ip;
445 f->remote.remote_port = port;
446 f->remote.remote_vni = vni;
447 f->remote.remote_ifindex = ifindex;
448 f->remote.remote_next = NULL;
450 f->flags = ndm_flags;
451 f->updated = f->used = jiffies;
452 memcpy(f->eth_addr, mac, ETH_ALEN);
455 hlist_add_head_rcu(&f->hlist,
456 vxlan_fdb_head(vxlan, mac));
460 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
465 static void vxlan_fdb_free(struct rcu_head *head)
467 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
469 while (f->remote.remote_next) {
470 struct vxlan_rdst *rd = f->remote.remote_next;
472 f->remote.remote_next = rd->remote_next;
478 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
480 netdev_dbg(vxlan->dev,
481 "delete %pM\n", f->eth_addr);
484 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
486 hlist_del_rcu(&f->hlist);
487 call_rcu(&f->rcu, vxlan_fdb_free);
490 /* Add static entry (via netlink) */
491 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
492 struct net_device *dev,
493 const unsigned char *addr, u16 flags)
495 struct vxlan_dev *vxlan = netdev_priv(dev);
496 struct net *net = dev_net(vxlan->dev);
502 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
503 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
508 if (tb[NDA_DST] == NULL)
511 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
512 return -EAFNOSUPPORT;
514 ip = nla_get_be32(tb[NDA_DST]);
517 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
519 port = nla_get_be16(tb[NDA_PORT]);
521 port = vxlan->dst_port;
524 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
526 vni = nla_get_u32(tb[NDA_VNI]);
528 vni = vxlan->default_dst.remote_vni;
530 if (tb[NDA_IFINDEX]) {
531 struct net_device *tdev;
533 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
535 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
536 tdev = dev_get_by_index(net, ifindex);
538 return -EADDRNOTAVAIL;
543 spin_lock_bh(&vxlan->hash_lock);
544 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
545 port, vni, ifindex, ndm->ndm_flags);
546 spin_unlock_bh(&vxlan->hash_lock);
551 /* Delete entry (via netlink) */
552 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
553 struct net_device *dev,
554 const unsigned char *addr)
556 struct vxlan_dev *vxlan = netdev_priv(dev);
560 spin_lock_bh(&vxlan->hash_lock);
561 f = vxlan_find_mac(vxlan, addr);
563 vxlan_fdb_destroy(vxlan, f);
566 spin_unlock_bh(&vxlan->hash_lock);
571 /* Dump forwarding table */
572 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
573 struct net_device *dev, int idx)
575 struct vxlan_dev *vxlan = netdev_priv(dev);
578 for (h = 0; h < FDB_HASH_SIZE; ++h) {
582 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
583 struct vxlan_rdst *rd;
584 for (rd = &f->remote; rd; rd = rd->remote_next) {
585 if (idx < cb->args[0])
588 err = vxlan_fdb_info(skb, vxlan, f,
589 NETLINK_CB(cb->skb).portid,
604 /* Watch incoming packets to learn mapping between Ethernet address
605 * and Tunnel endpoint.
606 * Return true if packet is bogus and should be droppped.
608 static bool vxlan_snoop(struct net_device *dev,
609 __be32 src_ip, const u8 *src_mac)
611 struct vxlan_dev *vxlan = netdev_priv(dev);
614 f = vxlan_find_mac(vxlan, src_mac);
616 if (likely(f->remote.remote_ip == src_ip))
619 /* Don't migrate static entries, drop packets */
620 if (f->state & NUD_NOARP)
625 "%pM migrated from %pI4 to %pI4\n",
626 src_mac, &f->remote.remote_ip, &src_ip);
628 f->remote.remote_ip = src_ip;
629 f->updated = jiffies;
631 /* learned new entry */
632 spin_lock(&vxlan->hash_lock);
634 /* close off race between vxlan_flush and incoming packets */
635 if (netif_running(dev))
636 vxlan_fdb_create(vxlan, src_mac, src_ip,
638 NLM_F_EXCL|NLM_F_CREATE,
640 vxlan->default_dst.remote_vni,
642 spin_unlock(&vxlan->hash_lock);
649 /* See if multicast group is already in use by other ID */
650 static bool vxlan_group_used(struct vxlan_net *vn,
651 const struct vxlan_dev *this)
653 struct vxlan_dev *vxlan;
655 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
659 if (!netif_running(vxlan->dev))
662 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
669 /* kernel equivalent to IP_ADD_MEMBERSHIP */
670 static int vxlan_join_group(struct net_device *dev)
672 struct vxlan_dev *vxlan = netdev_priv(dev);
673 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
674 struct sock *sk = vxlan->vn_sock->sock->sk;
675 struct ip_mreqn mreq = {
676 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
677 .imr_ifindex = vxlan->default_dst.remote_ifindex,
681 /* Already a member of group */
682 if (vxlan_group_used(vn, vxlan))
685 /* Need to drop RTNL to call multicast join */
688 err = ip_mc_join_group(sk, &mreq);
696 /* kernel equivalent to IP_DROP_MEMBERSHIP */
697 static int vxlan_leave_group(struct net_device *dev)
699 struct vxlan_dev *vxlan = netdev_priv(dev);
700 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
702 struct sock *sk = vxlan->vn_sock->sock->sk;
703 struct ip_mreqn mreq = {
704 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
705 .imr_ifindex = vxlan->default_dst.remote_ifindex,
708 /* Only leave group when last vxlan is done. */
709 if (vxlan_group_used(vn, vxlan))
712 /* Need to drop RTNL to call multicast leave */
715 err = ip_mc_leave_group(sk, &mreq);
722 /* Callback from net/ipv4/udp.c to receive packets */
723 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
726 struct vxlanhdr *vxh;
727 struct vxlan_dev *vxlan;
728 struct pcpu_tstats *stats;
733 /* pop off outer UDP header */
734 __skb_pull(skb, sizeof(struct udphdr));
736 /* Need Vxlan and inner Ethernet header to be present */
737 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
740 /* Drop packets with reserved bits set */
741 vxh = (struct vxlanhdr *) skb->data;
742 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
743 (vxh->vx_vni & htonl(0xff))) {
744 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
745 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
749 __skb_pull(skb, sizeof(struct vxlanhdr));
751 /* Is this VNI defined? */
752 vni = ntohl(vxh->vx_vni) >> 8;
753 port = inet_sk(sk)->inet_sport;
754 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
756 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
761 if (!pskb_may_pull(skb, ETH_HLEN)) {
762 vxlan->dev->stats.rx_length_errors++;
763 vxlan->dev->stats.rx_errors++;
767 skb_reset_mac_header(skb);
769 /* Re-examine inner Ethernet packet */
771 skb->protocol = eth_type_trans(skb, vxlan->dev);
773 /* Ignore packet loops (and multicast echo) */
774 if (compare_ether_addr(eth_hdr(skb)->h_source,
775 vxlan->dev->dev_addr) == 0)
778 if ((vxlan->flags & VXLAN_F_LEARN) &&
779 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
782 __skb_tunnel_rx(skb, vxlan->dev);
783 skb_reset_network_header(skb);
785 /* If the NIC driver gave us an encapsulated packet with
786 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
787 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
788 * for us. Otherwise force the upper layers to verify it.
790 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
791 !(vxlan->dev->features & NETIF_F_RXCSUM))
792 skb->ip_summed = CHECKSUM_NONE;
794 skb->encapsulation = 0;
796 err = IP_ECN_decapsulate(oip, skb);
799 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
800 &oip->saddr, oip->tos);
802 ++vxlan->dev->stats.rx_frame_errors;
803 ++vxlan->dev->stats.rx_errors;
808 stats = this_cpu_ptr(vxlan->dev->tstats);
809 u64_stats_update_begin(&stats->syncp);
811 stats->rx_bytes += skb->len;
812 u64_stats_update_end(&stats->syncp);
818 /* Put UDP header back */
819 __skb_push(skb, sizeof(struct udphdr));
823 /* Consume bad packet */
828 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
830 struct vxlan_dev *vxlan = netdev_priv(dev);
836 if (dev->flags & IFF_NOARP)
839 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
840 dev->stats.tx_dropped++;
845 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
846 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
847 parp->ar_pro != htons(ETH_P_IP) ||
848 parp->ar_op != htons(ARPOP_REQUEST) ||
849 parp->ar_hln != dev->addr_len ||
852 arpptr = (u8 *)parp + sizeof(struct arphdr);
854 arpptr += dev->addr_len; /* sha */
855 memcpy(&sip, arpptr, sizeof(sip));
856 arpptr += sizeof(sip);
857 arpptr += dev->addr_len; /* tha */
858 memcpy(&tip, arpptr, sizeof(tip));
860 if (ipv4_is_loopback(tip) ||
861 ipv4_is_multicast(tip))
864 n = neigh_lookup(&arp_tbl, &tip, dev);
868 struct sk_buff *reply;
870 if (!(n->nud_state & NUD_CONNECTED)) {
875 f = vxlan_find_mac(vxlan, n->ha);
876 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
877 /* bridge-local neighbor */
882 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
887 skb_reset_mac_header(reply);
888 __skb_pull(reply, skb_network_offset(reply));
889 reply->ip_summed = CHECKSUM_UNNECESSARY;
890 reply->pkt_type = PACKET_HOST;
892 if (netif_rx_ni(reply) == NET_RX_DROP)
893 dev->stats.rx_dropped++;
894 } else if (vxlan->flags & VXLAN_F_L3MISS)
895 vxlan_ip_miss(dev, tip);
901 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
903 struct vxlan_dev *vxlan = netdev_priv(dev);
907 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
911 switch (ntohs(eth_hdr(skb)->h_proto)) {
913 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
916 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
925 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
927 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
929 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
933 } else if (vxlan->flags & VXLAN_F_L3MISS)
934 vxlan_ip_miss(dev, pip->daddr);
938 static void vxlan_sock_put(struct sk_buff *skb)
943 /* On transmit, associate with the tunnel socket */
944 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
946 struct vxlan_dev *vxlan = netdev_priv(dev);
947 struct sock *sk = vxlan->vn_sock->sock->sk;
952 skb->destructor = vxlan_sock_put;
955 /* Compute source port for outgoing packet
956 * first choice to use L4 flow hash since it will spread
957 * better and maybe available from hardware
958 * secondary choice is to use jhash on the Ethernet header
960 static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
962 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
965 hash = skb_get_rxhash(skb);
967 hash = jhash(skb->data, 2 * ETH_ALEN,
968 (__force u32) skb->protocol);
970 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
973 static int handle_offloads(struct sk_buff *skb)
975 if (skb_is_gso(skb)) {
976 int err = skb_unclone(skb, GFP_ATOMIC);
980 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
981 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
982 skb->ip_summed = CHECKSUM_NONE;
987 /* Bypass encapsulation if the destination is local */
988 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
989 struct vxlan_dev *dst_vxlan)
991 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
992 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
994 skb->pkt_type = PACKET_HOST;
995 skb->encapsulation = 0;
996 skb->dev = dst_vxlan->dev;
997 __skb_pull(skb, skb_network_offset(skb));
999 if (dst_vxlan->flags & VXLAN_F_LEARN)
1000 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1001 eth_hdr(skb)->h_source);
1003 u64_stats_update_begin(&tx_stats->syncp);
1004 tx_stats->tx_packets++;
1005 tx_stats->tx_bytes += skb->len;
1006 u64_stats_update_end(&tx_stats->syncp);
1008 if (netif_rx(skb) == NET_RX_SUCCESS) {
1009 u64_stats_update_begin(&rx_stats->syncp);
1010 rx_stats->rx_packets++;
1011 rx_stats->rx_bytes += skb->len;
1012 u64_stats_update_end(&rx_stats->syncp);
1014 skb->dev->stats.rx_dropped++;
1018 static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1019 struct vxlan_rdst *rdst, bool did_rsc)
1021 struct vxlan_dev *vxlan = netdev_priv(dev);
1023 const struct iphdr *old_iph;
1025 struct vxlanhdr *vxh;
1029 __be16 src_port, dst_port;
1034 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1035 vni = rdst->remote_vni;
1036 dst = rdst->remote_ip;
1040 /* short-circuited back to local bridge */
1041 vxlan_encap_bypass(skb, vxlan, vxlan);
1042 return NETDEV_TX_OK;
1047 if (!skb->encapsulation) {
1048 skb_reset_inner_headers(skb);
1049 skb->encapsulation = 1;
1052 /* Need space for new headers (invalidates iph ptr) */
1053 if (skb_cow_head(skb, VXLAN_HEADROOM))
1056 old_iph = ip_hdr(skb);
1059 if (!ttl && IN_MULTICAST(ntohl(dst)))
1064 tos = ip_tunnel_get_dsfield(old_iph, skb);
1066 src_port = vxlan_src_port(vxlan, skb);
1068 memset(&fl4, 0, sizeof(fl4));
1069 fl4.flowi4_oif = rdst->remote_ifindex;
1070 fl4.flowi4_tos = RT_TOS(tos);
1072 fl4.saddr = vxlan->saddr;
1074 rt = ip_route_output_key(dev_net(dev), &fl4);
1076 netdev_dbg(dev, "no route to %pI4\n", &dst);
1077 dev->stats.tx_carrier_errors++;
1081 if (rt->dst.dev == dev) {
1082 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1084 dev->stats.collisions++;
1088 /* Bypass encapsulation if the destination is local */
1089 if (rt->rt_flags & RTCF_LOCAL &&
1090 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1091 struct vxlan_dev *dst_vxlan;
1094 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1097 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1098 return NETDEV_TX_OK;
1101 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1102 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1105 skb_dst_set(skb, &rt->dst);
1107 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1108 vxh->vx_flags = htonl(VXLAN_FLAGS);
1109 vxh->vx_vni = htonl(vni << 8);
1111 __skb_push(skb, sizeof(*uh));
1112 skb_reset_transport_header(skb);
1115 uh->dest = dst_port;
1116 uh->source = src_port;
1118 uh->len = htons(skb->len);
1121 __skb_push(skb, sizeof(*iph));
1122 skb_reset_network_header(skb);
1125 iph->ihl = sizeof(struct iphdr) >> 2;
1127 iph->protocol = IPPROTO_UDP;
1128 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1130 iph->saddr = fl4.saddr;
1131 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1132 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
1136 vxlan_set_owner(dev, skb);
1138 if (handle_offloads(skb))
1141 iptunnel_xmit(skb, dev);
1142 return NETDEV_TX_OK;
1145 dev->stats.tx_dropped++;
1149 dev->stats.tx_errors++;
1152 return NETDEV_TX_OK;
1155 /* Transmit local packets over Vxlan
1157 * Outer IP header inherits ECN and DF from inner header.
1158 * Outer UDP destination is the VXLAN assigned port.
1159 * source port is based on hash of flow
1161 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1163 struct vxlan_dev *vxlan = netdev_priv(dev);
1165 bool did_rsc = false;
1166 struct vxlan_rdst *rdst0, *rdst;
1167 struct vxlan_fdb *f;
1170 skb_reset_mac_header(skb);
1173 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1174 return arp_reduce(dev, skb);
1176 f = vxlan_find_mac(vxlan, eth->h_dest);
1179 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1180 ntohs(eth->h_proto) == ETH_P_IP) {
1181 did_rsc = route_shortcircuit(dev, skb);
1183 f = vxlan_find_mac(vxlan, eth->h_dest);
1187 rdst0 = &vxlan->default_dst;
1189 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
1190 (vxlan->flags & VXLAN_F_L2MISS) &&
1191 !is_multicast_ether_addr(eth->h_dest))
1192 vxlan_fdb_miss(vxlan, eth->h_dest);
1198 /* if there are multiple destinations, send copies */
1199 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1200 struct sk_buff *skb1;
1202 skb1 = skb_clone(skb, GFP_ATOMIC);
1204 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1205 if (rc == NETDEV_TX_OK)
1210 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1211 if (rc == NETDEV_TX_OK)
1216 /* Walk the forwarding table and purge stale entries */
1217 static void vxlan_cleanup(unsigned long arg)
1219 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1220 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1223 if (!netif_running(vxlan->dev))
1226 spin_lock_bh(&vxlan->hash_lock);
1227 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1228 struct hlist_node *p, *n;
1229 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1231 = container_of(p, struct vxlan_fdb, hlist);
1232 unsigned long timeout;
1234 if (f->state & NUD_PERMANENT)
1237 timeout = f->used + vxlan->age_interval * HZ;
1238 if (time_before_eq(timeout, jiffies)) {
1239 netdev_dbg(vxlan->dev,
1240 "garbage collect %pM\n",
1242 f->state = NUD_STALE;
1243 vxlan_fdb_destroy(vxlan, f);
1244 } else if (time_before(timeout, next_timer))
1245 next_timer = timeout;
1248 spin_unlock_bh(&vxlan->hash_lock);
1250 mod_timer(&vxlan->age_timer, next_timer);
1253 /* Setup stats when device is created */
1254 static int vxlan_init(struct net_device *dev)
1256 dev->tstats = alloc_percpu(struct pcpu_tstats);
1263 /* Start ageing timer and join group when device is brought up */
1264 static int vxlan_open(struct net_device *dev)
1266 struct vxlan_dev *vxlan = netdev_priv(dev);
1269 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1270 err = vxlan_join_group(dev);
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);
1303 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
1304 vxlan_leave_group(dev);
1306 del_timer_sync(&vxlan->age_timer);
1313 /* Stub, nothing needs to be done. */
1314 static void vxlan_set_multicast_list(struct net_device *dev)
1318 static const struct net_device_ops vxlan_netdev_ops = {
1319 .ndo_init = vxlan_init,
1320 .ndo_open = vxlan_open,
1321 .ndo_stop = vxlan_stop,
1322 .ndo_start_xmit = vxlan_xmit,
1323 .ndo_get_stats64 = ip_tunnel_get_stats64,
1324 .ndo_set_rx_mode = vxlan_set_multicast_list,
1325 .ndo_change_mtu = eth_change_mtu,
1326 .ndo_validate_addr = eth_validate_addr,
1327 .ndo_set_mac_address = eth_mac_addr,
1328 .ndo_fdb_add = vxlan_fdb_add,
1329 .ndo_fdb_del = vxlan_fdb_delete,
1330 .ndo_fdb_dump = vxlan_fdb_dump,
1333 /* Info for udev, that this is a virtual tunnel endpoint */
1334 static struct device_type vxlan_type = {
1338 static void vxlan_free(struct net_device *dev)
1340 free_percpu(dev->tstats);
1344 /* Initialize the device structure. */
1345 static void vxlan_setup(struct net_device *dev)
1347 struct vxlan_dev *vxlan = netdev_priv(dev);
1351 eth_hw_addr_random(dev);
1353 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1355 dev->netdev_ops = &vxlan_netdev_ops;
1356 dev->destructor = vxlan_free;
1357 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1359 dev->tx_queue_len = 0;
1360 dev->features |= NETIF_F_LLTX;
1361 dev->features |= NETIF_F_NETNS_LOCAL;
1362 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1363 dev->features |= NETIF_F_RXCSUM;
1364 dev->features |= NETIF_F_GSO_SOFTWARE;
1366 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1367 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1368 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1369 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1371 INIT_LIST_HEAD(&vxlan->next);
1372 spin_lock_init(&vxlan->hash_lock);
1374 init_timer_deferrable(&vxlan->age_timer);
1375 vxlan->age_timer.function = vxlan_cleanup;
1376 vxlan->age_timer.data = (unsigned long) vxlan;
1378 inet_get_local_port_range(&low, &high);
1379 vxlan->port_min = low;
1380 vxlan->port_max = high;
1381 vxlan->dst_port = htons(vxlan_port);
1385 for (h = 0; h < FDB_HASH_SIZE; ++h)
1386 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1389 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1390 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1391 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1392 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1393 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1394 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1395 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1396 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1397 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1398 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1399 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1400 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1401 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1402 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1403 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1404 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
1407 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1409 if (tb[IFLA_ADDRESS]) {
1410 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1411 pr_debug("invalid link address (not ethernet)\n");
1415 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1416 pr_debug("invalid all zero ethernet address\n");
1417 return -EADDRNOTAVAIL;
1424 if (data[IFLA_VXLAN_ID]) {
1425 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1426 if (id >= VXLAN_VID_MASK)
1430 if (data[IFLA_VXLAN_PORT_RANGE]) {
1431 const struct ifla_vxlan_port_range *p
1432 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1434 if (ntohs(p->high) < ntohs(p->low)) {
1435 pr_debug("port range %u .. %u not valid\n",
1436 ntohs(p->low), ntohs(p->high));
1444 static void vxlan_get_drvinfo(struct net_device *netdev,
1445 struct ethtool_drvinfo *drvinfo)
1447 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1448 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1451 static const struct ethtool_ops vxlan_ethtool_ops = {
1452 .get_drvinfo = vxlan_get_drvinfo,
1453 .get_link = ethtool_op_get_link,
1456 static void vxlan_del_work(struct work_struct *work)
1458 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1460 sk_release_kernel(vs->sock->sk);
1464 /* Create new listen socket if needed */
1465 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1467 struct vxlan_sock *vs;
1469 struct sockaddr_in vxlan_addr = {
1470 .sin_family = AF_INET,
1471 .sin_addr.s_addr = htonl(INADDR_ANY),
1476 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1478 return ERR_PTR(-ENOMEM);
1480 for (h = 0; h < VNI_HASH_SIZE; ++h)
1481 INIT_HLIST_HEAD(&vs->vni_list[h]);
1483 INIT_WORK(&vs->del_work, vxlan_del_work);
1485 /* Create UDP socket for encapsulation receive. */
1486 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1488 pr_debug("UDP socket create failed\n");
1493 /* Put in proper namespace */
1495 sk_change_net(sk, net);
1497 vxlan_addr.sin_port = port;
1499 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1500 sizeof(vxlan_addr));
1502 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1503 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1504 sk_release_kernel(sk);
1509 /* Disable multicast loopback */
1510 inet_sk(sk)->mc_loop = 0;
1512 /* Mark socket as an encapsulation socket. */
1513 udp_sk(sk)->encap_type = 1;
1514 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1521 static int vxlan_newlink(struct net *net, struct net_device *dev,
1522 struct nlattr *tb[], struct nlattr *data[])
1524 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1525 struct vxlan_dev *vxlan = netdev_priv(dev);
1526 struct vxlan_rdst *dst = &vxlan->default_dst;
1527 struct vxlan_sock *vs;
1531 if (!data[IFLA_VXLAN_ID])
1534 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1535 dst->remote_vni = vni;
1537 if (data[IFLA_VXLAN_GROUP])
1538 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1540 if (data[IFLA_VXLAN_LOCAL])
1541 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1543 if (data[IFLA_VXLAN_LINK] &&
1544 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1545 struct net_device *lowerdev
1546 = __dev_get_by_index(net, dst->remote_ifindex);
1549 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1554 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1556 /* update header length based on lower device */
1557 dev->hard_header_len = lowerdev->hard_header_len +
1561 if (data[IFLA_VXLAN_TOS])
1562 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1564 if (data[IFLA_VXLAN_TTL])
1565 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1567 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1568 vxlan->flags |= VXLAN_F_LEARN;
1570 if (data[IFLA_VXLAN_AGEING])
1571 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1573 vxlan->age_interval = FDB_AGE_DEFAULT;
1575 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1576 vxlan->flags |= VXLAN_F_PROXY;
1578 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1579 vxlan->flags |= VXLAN_F_RSC;
1581 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1582 vxlan->flags |= VXLAN_F_L2MISS;
1584 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1585 vxlan->flags |= VXLAN_F_L3MISS;
1587 if (data[IFLA_VXLAN_LIMIT])
1588 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1590 if (data[IFLA_VXLAN_PORT_RANGE]) {
1591 const struct ifla_vxlan_port_range *p
1592 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1593 vxlan->port_min = ntohs(p->low);
1594 vxlan->port_max = ntohs(p->high);
1597 if (data[IFLA_VXLAN_PORT])
1598 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1600 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1601 pr_info("duplicate VNI %u\n", vni);
1605 vs = vxlan_find_port(net, vxlan->dst_port);
1609 /* Drop lock because socket create acquires RTNL lock */
1611 vs = vxlan_socket_create(net, vxlan->dst_port);
1616 hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
1618 vxlan->vn_sock = vs;
1620 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1622 err = register_netdevice(dev);
1624 if (--vs->refcnt == 0) {
1626 sk_release_kernel(vs->sock->sk);
1633 list_add(&vxlan->next, &vn->vxlan_list);
1634 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1639 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1641 struct vxlan_dev *vxlan = netdev_priv(dev);
1642 struct vxlan_sock *vs = vxlan->vn_sock;
1644 hlist_del_rcu(&vxlan->hlist);
1645 list_del(&vxlan->next);
1646 unregister_netdevice_queue(dev, head);
1648 if (--vs->refcnt == 0) {
1649 hlist_del_rcu(&vs->hlist);
1650 schedule_work(&vs->del_work);
1654 static size_t vxlan_get_size(const struct net_device *dev)
1657 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1658 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1659 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1660 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1661 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1662 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1663 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1664 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1665 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1666 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1667 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1668 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1669 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1670 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1671 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
1675 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1677 const struct vxlan_dev *vxlan = netdev_priv(dev);
1678 const struct vxlan_rdst *dst = &vxlan->default_dst;
1679 struct ifla_vxlan_port_range ports = {
1680 .low = htons(vxlan->port_min),
1681 .high = htons(vxlan->port_max),
1684 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1685 goto nla_put_failure;
1687 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1688 goto nla_put_failure;
1690 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1691 goto nla_put_failure;
1693 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1694 goto nla_put_failure;
1696 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1697 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1698 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1699 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1700 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1701 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1702 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1703 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1704 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1705 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1706 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1707 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1708 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1709 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
1710 goto nla_put_failure;
1712 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1713 goto nla_put_failure;
1721 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1723 .maxtype = IFLA_VXLAN_MAX,
1724 .policy = vxlan_policy,
1725 .priv_size = sizeof(struct vxlan_dev),
1726 .setup = vxlan_setup,
1727 .validate = vxlan_validate,
1728 .newlink = vxlan_newlink,
1729 .dellink = vxlan_dellink,
1730 .get_size = vxlan_get_size,
1731 .fill_info = vxlan_fill_info,
1734 static __net_init int vxlan_init_net(struct net *net)
1736 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1739 INIT_LIST_HEAD(&vn->vxlan_list);
1741 for (h = 0; h < PORT_HASH_SIZE; ++h)
1742 INIT_HLIST_HEAD(&vn->sock_list[h]);
1747 static __net_exit void vxlan_exit_net(struct net *net)
1749 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1750 struct vxlan_dev *vxlan;
1753 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1754 dev_close(vxlan->dev);
1758 static struct pernet_operations vxlan_net_ops = {
1759 .init = vxlan_init_net,
1760 .exit = vxlan_exit_net,
1761 .id = &vxlan_net_id,
1762 .size = sizeof(struct vxlan_net),
1765 static int __init vxlan_init_module(void)
1769 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1771 rc = register_pernet_device(&vxlan_net_ops);
1775 rc = rtnl_link_register(&vxlan_link_ops);
1782 unregister_pernet_device(&vxlan_net_ops);
1786 late_initcall(vxlan_init_module);
1788 static void __exit vxlan_cleanup_module(void)
1790 rtnl_link_unregister(&vxlan_link_ops);
1791 unregister_pernet_device(&vxlan_net_ops);
1794 module_exit(vxlan_cleanup_module);
1796 MODULE_LICENSE("GPL");
1797 MODULE_VERSION(VXLAN_VERSION);
1798 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
1799 MODULE_ALIAS_RTNL_LINK("vxlan");