2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012 Vyatta Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * - use IANA UDP port number (when defined)
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/rculist.h>
24 #include <linux/netdevice.h>
27 #include <linux/udp.h>
28 #include <linux/igmp.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/hash.h>
32 #include <linux/ethtool.h>
34 #include <net/ndisc.h>
36 #include <net/ip_tunnels.h>
39 #include <net/rtnetlink.h>
40 #include <net/route.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/net_namespace.h>
44 #include <net/netns/generic.h>
46 #define VXLAN_VERSION "0.1"
48 #define VNI_HASH_BITS 10
49 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
50 #define FDB_HASH_BITS 8
51 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
52 #define FDB_AGE_DEFAULT 300 /* 5 min */
53 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55 #define VXLAN_N_VID (1u << 24)
56 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
57 /* IP header + UDP + VXLAN + Ethernet header */
58 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
60 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62 /* VXLAN protocol header */
68 /* UDP port for VXLAN traffic. */
69 static unsigned int vxlan_port __read_mostly = 8472;
70 module_param_named(udp_port, vxlan_port, uint, 0444);
71 MODULE_PARM_DESC(udp_port, "Destination UDP port");
73 static bool log_ecn_error = true;
74 module_param(log_ecn_error, bool, 0644);
75 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
77 /* per-net private data for this module */
78 static unsigned int vxlan_net_id;
80 struct socket *sock; /* UDP encap socket */
81 struct hlist_head vni_list[VNI_HASH_SIZE];
90 struct vxlan_rdst *remote_next;
93 /* Forwarding table entry */
95 struct hlist_node hlist; /* linked list of entries */
97 unsigned long updated; /* jiffies */
99 struct vxlan_rdst remote;
100 u16 state; /* see ndm_state */
101 u8 eth_addr[ETH_ALEN];
104 /* Pseudo network device */
106 struct hlist_node hlist;
107 struct net_device *dev;
108 __u32 vni; /* virtual network id */
109 __be32 gaddr; /* multicast group */
110 __be32 saddr; /* source address */
111 unsigned int link; /* link to multicast over */
112 __u16 port_min; /* source port range */
114 __u8 tos; /* TOS override */
116 u32 flags; /* VXLAN_F_* below */
118 unsigned long age_interval;
119 struct timer_list age_timer;
120 spinlock_t hash_lock;
121 unsigned int addrcnt;
122 unsigned int addrmax;
124 struct hlist_head fdb_head[FDB_HASH_SIZE];
127 #define VXLAN_F_LEARN 0x01
128 #define VXLAN_F_PROXY 0x02
129 #define VXLAN_F_RSC 0x04
130 #define VXLAN_F_L2MISS 0x08
131 #define VXLAN_F_L3MISS 0x10
133 /* salt for hash table */
134 static u32 vxlan_salt __read_mostly;
136 static inline struct hlist_head *vni_head(struct net *net, u32 id)
138 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
140 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
143 /* Look up VNI in a per net namespace table */
144 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
146 struct vxlan_dev *vxlan;
148 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
149 if (vxlan->vni == id)
156 /* Fill in neighbour message in skbuff. */
157 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
158 const struct vxlan_fdb *fdb,
159 u32 portid, u32 seq, int type, unsigned int flags,
160 const struct vxlan_rdst *rdst)
162 unsigned long now = jiffies;
163 struct nda_cacheinfo ci;
164 struct nlmsghdr *nlh;
166 bool send_ip, send_eth;
168 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
172 ndm = nlmsg_data(nlh);
173 memset(ndm, 0, sizeof(*ndm));
175 send_eth = send_ip = true;
177 if (type == RTM_GETNEIGH) {
178 ndm->ndm_family = AF_INET;
179 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
180 send_eth = !is_zero_ether_addr(fdb->eth_addr);
182 ndm->ndm_family = AF_BRIDGE;
183 ndm->ndm_state = fdb->state;
184 ndm->ndm_ifindex = vxlan->dev->ifindex;
185 ndm->ndm_flags = NTF_SELF;
186 ndm->ndm_type = NDA_DST;
188 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
189 goto nla_put_failure;
191 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
192 goto nla_put_failure;
194 if (rdst->remote_port && rdst->remote_port != vxlan_port &&
195 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
196 goto nla_put_failure;
197 if (rdst->remote_vni != vxlan->vni &&
198 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
199 goto nla_put_failure;
200 if (rdst->remote_ifindex &&
201 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
202 goto nla_put_failure;
204 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
205 ci.ndm_confirmed = 0;
206 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
209 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
210 goto nla_put_failure;
212 return nlmsg_end(skb, nlh);
215 nlmsg_cancel(skb, nlh);
219 static inline size_t vxlan_nlmsg_size(void)
221 return NLMSG_ALIGN(sizeof(struct ndmsg))
222 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
223 + nla_total_size(sizeof(__be32)) /* NDA_DST */
224 + nla_total_size(sizeof(__be32)) /* NDA_PORT */
225 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
226 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
227 + nla_total_size(sizeof(struct nda_cacheinfo));
230 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
231 const struct vxlan_fdb *fdb, int type)
233 struct net *net = dev_net(vxlan->dev);
237 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
241 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
243 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
244 WARN_ON(err == -EMSGSIZE);
249 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
253 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
256 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
258 struct vxlan_dev *vxlan = netdev_priv(dev);
261 memset(&f, 0, sizeof f);
263 f.remote.remote_ip = ipa; /* goes to NDA_DST */
264 f.remote.remote_vni = VXLAN_N_VID;
266 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
269 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
273 memset(&f, 0, sizeof f);
275 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
277 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
280 /* Hash Ethernet address */
281 static u32 eth_hash(const unsigned char *addr)
283 u64 value = get_unaligned((u64 *)addr);
285 /* only want 6 bytes */
291 return hash_64(value, FDB_HASH_BITS);
294 /* Hash chain to use given mac address */
295 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
298 return &vxlan->fdb_head[eth_hash(mac)];
301 /* Look up Ethernet address in forwarding table */
302 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
306 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
309 hlist_for_each_entry_rcu(f, head, hlist) {
310 if (compare_ether_addr(mac, f->eth_addr) == 0)
317 /* Add/update destinations for multicast */
318 static int vxlan_fdb_append(struct vxlan_fdb *f,
319 __be32 ip, __u32 port, __u32 vni, __u32 ifindex)
321 struct vxlan_rdst *rd_prev, *rd;
324 for (rd = &f->remote; rd; rd = rd->remote_next) {
325 if (rd->remote_ip == ip &&
326 rd->remote_port == port &&
327 rd->remote_vni == vni &&
328 rd->remote_ifindex == ifindex)
332 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
336 rd->remote_port = port;
337 rd->remote_vni = vni;
338 rd->remote_ifindex = ifindex;
339 rd->remote_next = NULL;
340 rd_prev->remote_next = rd;
344 /* Add new entry to forwarding table -- assumes lock held */
345 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
346 const u8 *mac, __be32 ip,
347 __u16 state, __u16 flags,
348 __u32 port, __u32 vni, __u32 ifindex)
353 f = vxlan_find_mac(vxlan, mac);
355 if (flags & NLM_F_EXCL) {
356 netdev_dbg(vxlan->dev,
357 "lost race to create %pM\n", mac);
360 if (f->state != state) {
362 f->updated = jiffies;
365 if ((flags & NLM_F_APPEND) &&
366 is_multicast_ether_addr(f->eth_addr)) {
367 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
374 if (!(flags & NLM_F_CREATE))
377 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
380 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
381 f = kmalloc(sizeof(*f), GFP_ATOMIC);
386 f->remote.remote_ip = ip;
387 f->remote.remote_port = port;
388 f->remote.remote_vni = vni;
389 f->remote.remote_ifindex = ifindex;
390 f->remote.remote_next = NULL;
392 f->updated = f->used = jiffies;
393 memcpy(f->eth_addr, mac, ETH_ALEN);
396 hlist_add_head_rcu(&f->hlist,
397 vxlan_fdb_head(vxlan, mac));
401 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
406 void vxlan_fdb_free(struct rcu_head *head)
408 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
410 while (f->remote.remote_next) {
411 struct vxlan_rdst *rd = f->remote.remote_next;
413 f->remote.remote_next = rd->remote_next;
419 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
421 netdev_dbg(vxlan->dev,
422 "delete %pM\n", f->eth_addr);
425 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
427 hlist_del_rcu(&f->hlist);
428 call_rcu(&f->rcu, vxlan_fdb_free);
431 /* Add static entry (via netlink) */
432 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
433 struct net_device *dev,
434 const unsigned char *addr, u16 flags)
436 struct vxlan_dev *vxlan = netdev_priv(dev);
437 struct net *net = dev_net(vxlan->dev);
439 u32 port, vni, ifindex;
442 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
443 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
448 if (tb[NDA_DST] == NULL)
451 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
452 return -EAFNOSUPPORT;
454 ip = nla_get_be32(tb[NDA_DST]);
457 if (nla_len(tb[NDA_PORT]) != sizeof(u32))
459 port = nla_get_u32(tb[NDA_PORT]);
464 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
466 vni = nla_get_u32(tb[NDA_VNI]);
470 if (tb[NDA_IFINDEX]) {
471 struct net_device *dev;
473 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
475 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
476 dev = dev_get_by_index(net, ifindex);
478 return -EADDRNOTAVAIL;
483 spin_lock_bh(&vxlan->hash_lock);
484 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, port,
486 spin_unlock_bh(&vxlan->hash_lock);
491 /* Delete entry (via netlink) */
492 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
493 struct net_device *dev,
494 const unsigned char *addr)
496 struct vxlan_dev *vxlan = netdev_priv(dev);
500 spin_lock_bh(&vxlan->hash_lock);
501 f = vxlan_find_mac(vxlan, addr);
503 vxlan_fdb_destroy(vxlan, f);
506 spin_unlock_bh(&vxlan->hash_lock);
511 /* Dump forwarding table */
512 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
513 struct net_device *dev, int idx)
515 struct vxlan_dev *vxlan = netdev_priv(dev);
518 for (h = 0; h < FDB_HASH_SIZE; ++h) {
522 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
523 struct vxlan_rdst *rd;
524 for (rd = &f->remote; rd; rd = rd->remote_next) {
525 if (idx < cb->args[0])
528 err = vxlan_fdb_info(skb, vxlan, f,
529 NETLINK_CB(cb->skb).portid,
544 /* Watch incoming packets to learn mapping between Ethernet address
545 * and Tunnel endpoint.
547 static void vxlan_snoop(struct net_device *dev,
548 __be32 src_ip, const u8 *src_mac)
550 struct vxlan_dev *vxlan = netdev_priv(dev);
554 f = vxlan_find_mac(vxlan, src_mac);
557 if (likely(f->remote.remote_ip == src_ip))
562 "%pM migrated from %pI4 to %pI4\n",
563 src_mac, &f->remote.remote_ip, &src_ip);
565 f->remote.remote_ip = src_ip;
566 f->updated = jiffies;
568 /* learned new entry */
569 spin_lock(&vxlan->hash_lock);
570 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
572 NLM_F_EXCL|NLM_F_CREATE,
573 vxlan_port, vxlan->vni, 0);
574 spin_unlock(&vxlan->hash_lock);
579 /* See if multicast group is already in use by other ID */
580 static bool vxlan_group_used(struct vxlan_net *vn,
581 const struct vxlan_dev *this)
583 const struct vxlan_dev *vxlan;
586 for (h = 0; h < VNI_HASH_SIZE; ++h)
587 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
591 if (!netif_running(vxlan->dev))
594 if (vxlan->gaddr == this->gaddr)
601 /* kernel equivalent to IP_ADD_MEMBERSHIP */
602 static int vxlan_join_group(struct net_device *dev)
604 struct vxlan_dev *vxlan = netdev_priv(dev);
605 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
606 struct sock *sk = vn->sock->sk;
607 struct ip_mreqn mreq = {
608 .imr_multiaddr.s_addr = vxlan->gaddr,
609 .imr_ifindex = vxlan->link,
613 /* Already a member of group */
614 if (vxlan_group_used(vn, vxlan))
617 /* Need to drop RTNL to call multicast join */
620 err = ip_mc_join_group(sk, &mreq);
628 /* kernel equivalent to IP_DROP_MEMBERSHIP */
629 static int vxlan_leave_group(struct net_device *dev)
631 struct vxlan_dev *vxlan = netdev_priv(dev);
632 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
634 struct sock *sk = vn->sock->sk;
635 struct ip_mreqn mreq = {
636 .imr_multiaddr.s_addr = vxlan->gaddr,
637 .imr_ifindex = vxlan->link,
640 /* Only leave group when last vxlan is done. */
641 if (vxlan_group_used(vn, vxlan))
644 /* Need to drop RTNL to call multicast leave */
647 err = ip_mc_leave_group(sk, &mreq);
654 /* Callback from net/ipv4/udp.c to receive packets */
655 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
658 struct vxlanhdr *vxh;
659 struct vxlan_dev *vxlan;
660 struct pcpu_tstats *stats;
664 /* pop off outer UDP header */
665 __skb_pull(skb, sizeof(struct udphdr));
667 /* Need Vxlan and inner Ethernet header to be present */
668 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
671 /* Drop packets with reserved bits set */
672 vxh = (struct vxlanhdr *) skb->data;
673 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
674 (vxh->vx_vni & htonl(0xff))) {
675 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
676 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
680 __skb_pull(skb, sizeof(struct vxlanhdr));
682 /* Is this VNI defined? */
683 vni = ntohl(vxh->vx_vni) >> 8;
684 vxlan = vxlan_find_vni(sock_net(sk), vni);
686 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
690 if (!pskb_may_pull(skb, ETH_HLEN)) {
691 vxlan->dev->stats.rx_length_errors++;
692 vxlan->dev->stats.rx_errors++;
696 skb_reset_mac_header(skb);
698 /* Re-examine inner Ethernet packet */
700 skb->protocol = eth_type_trans(skb, vxlan->dev);
702 /* Ignore packet loops (and multicast echo) */
703 if (compare_ether_addr(eth_hdr(skb)->h_source,
704 vxlan->dev->dev_addr) == 0)
707 if (vxlan->flags & VXLAN_F_LEARN)
708 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
710 __skb_tunnel_rx(skb, vxlan->dev);
711 skb_reset_network_header(skb);
713 /* If the NIC driver gave us an encapsulated packet with
714 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
715 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
716 * for us. Otherwise force the upper layers to verify it.
718 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
719 !(vxlan->dev->features & NETIF_F_RXCSUM))
720 skb->ip_summed = CHECKSUM_NONE;
722 skb->encapsulation = 0;
724 err = IP_ECN_decapsulate(oip, skb);
727 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
728 &oip->saddr, oip->tos);
730 ++vxlan->dev->stats.rx_frame_errors;
731 ++vxlan->dev->stats.rx_errors;
736 stats = this_cpu_ptr(vxlan->dev->tstats);
737 u64_stats_update_begin(&stats->syncp);
739 stats->rx_bytes += skb->len;
740 u64_stats_update_end(&stats->syncp);
746 /* Put UDP header back */
747 __skb_push(skb, sizeof(struct udphdr));
751 /* Consume bad packet */
756 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
758 struct vxlan_dev *vxlan = netdev_priv(dev);
764 if (dev->flags & IFF_NOARP)
767 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
768 dev->stats.tx_dropped++;
773 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
774 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
775 parp->ar_pro != htons(ETH_P_IP) ||
776 parp->ar_op != htons(ARPOP_REQUEST) ||
777 parp->ar_hln != dev->addr_len ||
780 arpptr = (u8 *)parp + sizeof(struct arphdr);
782 arpptr += dev->addr_len; /* sha */
783 memcpy(&sip, arpptr, sizeof(sip));
784 arpptr += sizeof(sip);
785 arpptr += dev->addr_len; /* tha */
786 memcpy(&tip, arpptr, sizeof(tip));
788 if (ipv4_is_loopback(tip) ||
789 ipv4_is_multicast(tip))
792 n = neigh_lookup(&arp_tbl, &tip, dev);
795 struct vxlan_dev *vxlan = netdev_priv(dev);
797 struct sk_buff *reply;
799 if (!(n->nud_state & NUD_CONNECTED)) {
804 f = vxlan_find_mac(vxlan, n->ha);
805 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
806 /* bridge-local neighbor */
811 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
816 skb_reset_mac_header(reply);
817 __skb_pull(reply, skb_network_offset(reply));
818 reply->ip_summed = CHECKSUM_UNNECESSARY;
819 reply->pkt_type = PACKET_HOST;
821 if (netif_rx_ni(reply) == NET_RX_DROP)
822 dev->stats.rx_dropped++;
823 } else if (vxlan->flags & VXLAN_F_L3MISS)
824 vxlan_ip_miss(dev, tip);
830 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
832 struct vxlan_dev *vxlan = netdev_priv(dev);
836 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
840 switch (ntohs(eth_hdr(skb)->h_proto)) {
842 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
845 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
854 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
856 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
858 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
862 } else if (vxlan->flags & VXLAN_F_L3MISS)
863 vxlan_ip_miss(dev, pip->daddr);
867 static void vxlan_sock_free(struct sk_buff *skb)
872 /* On transmit, associate with the tunnel socket */
873 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
875 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
876 struct sock *sk = vn->sock->sk;
881 skb->destructor = vxlan_sock_free;
884 /* Compute source port for outgoing packet
885 * first choice to use L4 flow hash since it will spread
886 * better and maybe available from hardware
887 * secondary choice is to use jhash on the Ethernet header
889 static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
891 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
894 hash = skb_get_rxhash(skb);
896 hash = jhash(skb->data, 2 * ETH_ALEN,
897 (__force u32) skb->protocol);
899 return (((u64) hash * range) >> 32) + vxlan->port_min;
902 static int handle_offloads(struct sk_buff *skb)
904 if (skb_is_gso(skb)) {
905 int err = skb_unclone(skb, GFP_ATOMIC);
909 skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP);
910 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
911 skb->ip_summed = CHECKSUM_NONE;
916 static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
917 struct vxlan_rdst *rdst, bool did_rsc)
919 struct vxlan_dev *vxlan = netdev_priv(dev);
921 const struct iphdr *old_iph;
923 struct vxlanhdr *vxh;
926 unsigned int pkt_len = skb->len;
928 __u16 src_port, dst_port;
933 dst_port = rdst->remote_port ? rdst->remote_port : vxlan_port;
934 vni = rdst->remote_vni;
935 dst = rdst->remote_ip;
939 __skb_pull(skb, skb_network_offset(skb));
940 skb->ip_summed = CHECKSUM_NONE;
941 skb->pkt_type = PACKET_HOST;
943 /* short-circuited back to local bridge */
944 if (netif_rx(skb) == NET_RX_SUCCESS) {
945 struct pcpu_tstats *stats = this_cpu_ptr(dev->tstats);
947 u64_stats_update_begin(&stats->syncp);
949 stats->tx_bytes += pkt_len;
950 u64_stats_update_end(&stats->syncp);
952 dev->stats.tx_errors++;
953 dev->stats.tx_aborted_errors++;
960 if (!skb->encapsulation) {
961 skb_reset_inner_headers(skb);
962 skb->encapsulation = 1;
965 /* Need space for new headers (invalidates iph ptr) */
966 if (skb_cow_head(skb, VXLAN_HEADROOM))
969 old_iph = ip_hdr(skb);
972 if (!ttl && IN_MULTICAST(ntohl(dst)))
977 tos = ip_tunnel_get_dsfield(old_iph, skb);
979 src_port = vxlan_src_port(vxlan, skb);
981 memset(&fl4, 0, sizeof(fl4));
982 fl4.flowi4_oif = rdst->remote_ifindex;
983 fl4.flowi4_tos = RT_TOS(tos);
985 fl4.saddr = vxlan->saddr;
987 rt = ip_route_output_key(dev_net(dev), &fl4);
989 netdev_dbg(dev, "no route to %pI4\n", &dst);
990 dev->stats.tx_carrier_errors++;
994 if (rt->dst.dev == dev) {
995 netdev_dbg(dev, "circular route to %pI4\n", &dst);
997 dev->stats.collisions++;
1001 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1002 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1005 skb_dst_set(skb, &rt->dst);
1007 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1008 vxh->vx_flags = htonl(VXLAN_FLAGS);
1009 vxh->vx_vni = htonl(vni << 8);
1011 __skb_push(skb, sizeof(*uh));
1012 skb_reset_transport_header(skb);
1015 uh->dest = htons(dst_port);
1016 uh->source = htons(src_port);
1018 uh->len = htons(skb->len);
1021 __skb_push(skb, sizeof(*iph));
1022 skb_reset_network_header(skb);
1025 iph->ihl = sizeof(struct iphdr) >> 2;
1027 iph->protocol = IPPROTO_UDP;
1028 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1030 iph->saddr = fl4.saddr;
1031 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1032 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
1036 vxlan_set_owner(dev, skb);
1038 if (handle_offloads(skb))
1041 iptunnel_xmit(skb, dev);
1042 return NETDEV_TX_OK;
1045 dev->stats.tx_dropped++;
1049 dev->stats.tx_errors++;
1052 return NETDEV_TX_OK;
1055 /* Transmit local packets over Vxlan
1057 * Outer IP header inherits ECN and DF from inner header.
1058 * Outer UDP destination is the VXLAN assigned port.
1059 * source port is based on hash of flow
1061 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1063 struct vxlan_dev *vxlan = netdev_priv(dev);
1065 bool did_rsc = false;
1066 struct vxlan_rdst group, *rdst0, *rdst;
1067 struct vxlan_fdb *f;
1070 skb_reset_mac_header(skb);
1073 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1074 return arp_reduce(dev, skb);
1075 else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
1076 did_rsc = route_shortcircuit(dev, skb);
1078 f = vxlan_find_mac(vxlan, eth->h_dest);
1081 group.remote_port = vxlan_port;
1082 group.remote_vni = vxlan->vni;
1083 group.remote_ip = vxlan->gaddr;
1084 group.remote_ifindex = vxlan->link;
1085 group.remote_next = 0;
1088 if (group.remote_ip == htonl(INADDR_ANY) &&
1089 (vxlan->flags & VXLAN_F_L2MISS) &&
1090 !is_multicast_ether_addr(eth->h_dest))
1091 vxlan_fdb_miss(vxlan, eth->h_dest);
1097 /* if there are multiple destinations, send copies */
1098 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1099 struct sk_buff *skb1;
1101 skb1 = skb_clone(skb, GFP_ATOMIC);
1102 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1103 if (rc == NETDEV_TX_OK)
1107 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1108 if (rc == NETDEV_TX_OK)
1113 /* Walk the forwarding table and purge stale entries */
1114 static void vxlan_cleanup(unsigned long arg)
1116 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1117 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1120 if (!netif_running(vxlan->dev))
1123 spin_lock_bh(&vxlan->hash_lock);
1124 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1125 struct hlist_node *p, *n;
1126 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1128 = container_of(p, struct vxlan_fdb, hlist);
1129 unsigned long timeout;
1131 if (f->state & NUD_PERMANENT)
1134 timeout = f->used + vxlan->age_interval * HZ;
1135 if (time_before_eq(timeout, jiffies)) {
1136 netdev_dbg(vxlan->dev,
1137 "garbage collect %pM\n",
1139 f->state = NUD_STALE;
1140 vxlan_fdb_destroy(vxlan, f);
1141 } else if (time_before(timeout, next_timer))
1142 next_timer = timeout;
1145 spin_unlock_bh(&vxlan->hash_lock);
1147 mod_timer(&vxlan->age_timer, next_timer);
1150 /* Setup stats when device is created */
1151 static int vxlan_init(struct net_device *dev)
1153 dev->tstats = alloc_percpu(struct pcpu_tstats);
1160 /* Start ageing timer and join group when device is brought up */
1161 static int vxlan_open(struct net_device *dev)
1163 struct vxlan_dev *vxlan = netdev_priv(dev);
1167 err = vxlan_join_group(dev);
1172 if (vxlan->age_interval)
1173 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1178 /* Purge the forwarding table */
1179 static void vxlan_flush(struct vxlan_dev *vxlan)
1183 spin_lock_bh(&vxlan->hash_lock);
1184 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1185 struct hlist_node *p, *n;
1186 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1188 = container_of(p, struct vxlan_fdb, hlist);
1189 vxlan_fdb_destroy(vxlan, f);
1192 spin_unlock_bh(&vxlan->hash_lock);
1195 /* Cleanup timer and forwarding table on shutdown */
1196 static int vxlan_stop(struct net_device *dev)
1198 struct vxlan_dev *vxlan = netdev_priv(dev);
1201 vxlan_leave_group(dev);
1203 del_timer_sync(&vxlan->age_timer);
1210 /* Stub, nothing needs to be done. */
1211 static void vxlan_set_multicast_list(struct net_device *dev)
1215 static const struct net_device_ops vxlan_netdev_ops = {
1216 .ndo_init = vxlan_init,
1217 .ndo_open = vxlan_open,
1218 .ndo_stop = vxlan_stop,
1219 .ndo_start_xmit = vxlan_xmit,
1220 .ndo_get_stats64 = ip_tunnel_get_stats64,
1221 .ndo_set_rx_mode = vxlan_set_multicast_list,
1222 .ndo_change_mtu = eth_change_mtu,
1223 .ndo_validate_addr = eth_validate_addr,
1224 .ndo_set_mac_address = eth_mac_addr,
1225 .ndo_fdb_add = vxlan_fdb_add,
1226 .ndo_fdb_del = vxlan_fdb_delete,
1227 .ndo_fdb_dump = vxlan_fdb_dump,
1230 /* Info for udev, that this is a virtual tunnel endpoint */
1231 static struct device_type vxlan_type = {
1235 static void vxlan_free(struct net_device *dev)
1237 free_percpu(dev->tstats);
1241 /* Initialize the device structure. */
1242 static void vxlan_setup(struct net_device *dev)
1244 struct vxlan_dev *vxlan = netdev_priv(dev);
1248 eth_hw_addr_random(dev);
1250 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1252 dev->netdev_ops = &vxlan_netdev_ops;
1253 dev->destructor = vxlan_free;
1254 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1256 dev->tx_queue_len = 0;
1257 dev->features |= NETIF_F_LLTX;
1258 dev->features |= NETIF_F_NETNS_LOCAL;
1259 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1260 dev->features |= NETIF_F_RXCSUM;
1261 dev->features |= NETIF_F_GSO_SOFTWARE;
1263 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1264 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1265 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1266 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1268 spin_lock_init(&vxlan->hash_lock);
1270 init_timer_deferrable(&vxlan->age_timer);
1271 vxlan->age_timer.function = vxlan_cleanup;
1272 vxlan->age_timer.data = (unsigned long) vxlan;
1274 inet_get_local_port_range(&low, &high);
1275 vxlan->port_min = low;
1276 vxlan->port_max = high;
1280 for (h = 0; h < FDB_HASH_SIZE; ++h)
1281 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1284 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1285 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1286 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1287 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1288 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1289 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1290 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1291 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1292 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1293 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1294 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1295 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1296 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1297 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1298 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1301 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1303 if (tb[IFLA_ADDRESS]) {
1304 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1305 pr_debug("invalid link address (not ethernet)\n");
1309 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1310 pr_debug("invalid all zero ethernet address\n");
1311 return -EADDRNOTAVAIL;
1318 if (data[IFLA_VXLAN_ID]) {
1319 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1320 if (id >= VXLAN_VID_MASK)
1324 if (data[IFLA_VXLAN_GROUP]) {
1325 __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1326 if (!IN_MULTICAST(ntohl(gaddr))) {
1327 pr_debug("group address is not IPv4 multicast\n");
1328 return -EADDRNOTAVAIL;
1332 if (data[IFLA_VXLAN_PORT_RANGE]) {
1333 const struct ifla_vxlan_port_range *p
1334 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1336 if (ntohs(p->high) < ntohs(p->low)) {
1337 pr_debug("port range %u .. %u not valid\n",
1338 ntohs(p->low), ntohs(p->high));
1346 static void vxlan_get_drvinfo(struct net_device *netdev,
1347 struct ethtool_drvinfo *drvinfo)
1349 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1350 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1353 static const struct ethtool_ops vxlan_ethtool_ops = {
1354 .get_drvinfo = vxlan_get_drvinfo,
1355 .get_link = ethtool_op_get_link,
1358 static int vxlan_newlink(struct net *net, struct net_device *dev,
1359 struct nlattr *tb[], struct nlattr *data[])
1361 struct vxlan_dev *vxlan = netdev_priv(dev);
1365 if (!data[IFLA_VXLAN_ID])
1368 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1369 if (vxlan_find_vni(net, vni)) {
1370 pr_info("duplicate VNI %u\n", vni);
1375 if (data[IFLA_VXLAN_GROUP])
1376 vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1378 if (data[IFLA_VXLAN_LOCAL])
1379 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1381 if (data[IFLA_VXLAN_LINK] &&
1382 (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1383 struct net_device *lowerdev
1384 = __dev_get_by_index(net, vxlan->link);
1387 pr_info("ifindex %d does not exist\n", vxlan->link);
1392 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1394 /* update header length based on lower device */
1395 dev->hard_header_len = lowerdev->hard_header_len +
1399 if (data[IFLA_VXLAN_TOS])
1400 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1402 if (data[IFLA_VXLAN_TTL])
1403 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1405 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1406 vxlan->flags |= VXLAN_F_LEARN;
1408 if (data[IFLA_VXLAN_AGEING])
1409 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1411 vxlan->age_interval = FDB_AGE_DEFAULT;
1413 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1414 vxlan->flags |= VXLAN_F_PROXY;
1416 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1417 vxlan->flags |= VXLAN_F_RSC;
1419 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1420 vxlan->flags |= VXLAN_F_L2MISS;
1422 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1423 vxlan->flags |= VXLAN_F_L3MISS;
1425 if (data[IFLA_VXLAN_LIMIT])
1426 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1428 if (data[IFLA_VXLAN_PORT_RANGE]) {
1429 const struct ifla_vxlan_port_range *p
1430 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1431 vxlan->port_min = ntohs(p->low);
1432 vxlan->port_max = ntohs(p->high);
1435 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1437 err = register_netdevice(dev);
1439 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
1444 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1446 struct vxlan_dev *vxlan = netdev_priv(dev);
1448 hlist_del_rcu(&vxlan->hlist);
1450 unregister_netdevice_queue(dev, head);
1453 static size_t vxlan_get_size(const struct net_device *dev)
1456 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1457 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1458 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1459 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1460 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1461 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1462 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1463 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1464 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1465 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1466 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1467 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1468 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1469 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1473 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1475 const struct vxlan_dev *vxlan = netdev_priv(dev);
1476 struct ifla_vxlan_port_range ports = {
1477 .low = htons(vxlan->port_min),
1478 .high = htons(vxlan->port_max),
1481 if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
1482 goto nla_put_failure;
1484 if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
1485 goto nla_put_failure;
1487 if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
1488 goto nla_put_failure;
1490 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1491 goto nla_put_failure;
1493 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1494 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1495 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1496 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1497 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1498 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1499 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1500 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1501 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1502 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1503 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1504 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1505 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1506 goto nla_put_failure;
1508 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1509 goto nla_put_failure;
1517 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1519 .maxtype = IFLA_VXLAN_MAX,
1520 .policy = vxlan_policy,
1521 .priv_size = sizeof(struct vxlan_dev),
1522 .setup = vxlan_setup,
1523 .validate = vxlan_validate,
1524 .newlink = vxlan_newlink,
1525 .dellink = vxlan_dellink,
1526 .get_size = vxlan_get_size,
1527 .fill_info = vxlan_fill_info,
1530 static __net_init int vxlan_init_net(struct net *net)
1532 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1534 struct sockaddr_in vxlan_addr = {
1535 .sin_family = AF_INET,
1536 .sin_addr.s_addr = htonl(INADDR_ANY),
1541 /* Create UDP socket for encapsulation receive. */
1542 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1544 pr_debug("UDP socket create failed\n");
1547 /* Put in proper namespace */
1549 sk_change_net(sk, net);
1551 vxlan_addr.sin_port = htons(vxlan_port);
1553 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1554 sizeof(vxlan_addr));
1556 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1557 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1558 sk_release_kernel(sk);
1563 /* Disable multicast loopback */
1564 inet_sk(sk)->mc_loop = 0;
1566 /* Mark socket as an encapsulation socket. */
1567 udp_sk(sk)->encap_type = 1;
1568 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1571 for (h = 0; h < VNI_HASH_SIZE; ++h)
1572 INIT_HLIST_HEAD(&vn->vni_list[h]);
1577 static __net_exit void vxlan_exit_net(struct net *net)
1579 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1580 struct vxlan_dev *vxlan;
1584 for (h = 0; h < VNI_HASH_SIZE; ++h)
1585 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
1586 dev_close(vxlan->dev);
1590 sk_release_kernel(vn->sock->sk);
1595 static struct pernet_operations vxlan_net_ops = {
1596 .init = vxlan_init_net,
1597 .exit = vxlan_exit_net,
1598 .id = &vxlan_net_id,
1599 .size = sizeof(struct vxlan_net),
1602 static int __init vxlan_init_module(void)
1606 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1608 rc = register_pernet_device(&vxlan_net_ops);
1612 rc = rtnl_link_register(&vxlan_link_ops);
1619 unregister_pernet_device(&vxlan_net_ops);
1623 module_init(vxlan_init_module);
1625 static void __exit vxlan_cleanup_module(void)
1627 rtnl_link_unregister(&vxlan_link_ops);
1628 unregister_pernet_device(&vxlan_net_ops);
1631 module_exit(vxlan_cleanup_module);
1633 MODULE_LICENSE("GPL");
1634 MODULE_VERSION(VXLAN_VERSION);
1635 MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1636 MODULE_ALIAS_RTNL_LINK("vxlan");