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 flags; /* see ndm_flags */
102 u8 eth_addr[ETH_ALEN];
105 /* Pseudo network device */
107 struct hlist_node hlist;
108 struct net_device *dev;
109 struct vxlan_rdst default_dst; /* default destination */
110 __be32 saddr; /* source address */
111 __u16 port_min; /* source port range */
113 __u8 tos; /* TOS override */
115 u32 flags; /* VXLAN_F_* below */
117 unsigned long age_interval;
118 struct timer_list age_timer;
119 spinlock_t hash_lock;
120 unsigned int addrcnt;
121 unsigned int addrmax;
123 struct hlist_head fdb_head[FDB_HASH_SIZE];
126 #define VXLAN_F_LEARN 0x01
127 #define VXLAN_F_PROXY 0x02
128 #define VXLAN_F_RSC 0x04
129 #define VXLAN_F_L2MISS 0x08
130 #define VXLAN_F_L3MISS 0x10
132 /* salt for hash table */
133 static u32 vxlan_salt __read_mostly;
135 static inline struct hlist_head *vni_head(struct net *net, u32 id)
137 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
139 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
142 /* Look up VNI in a per net namespace table */
143 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
145 struct vxlan_dev *vxlan;
147 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
148 if (vxlan->default_dst.remote_vni == id)
155 /* Fill in neighbour message in skbuff. */
156 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
157 const struct vxlan_fdb *fdb,
158 u32 portid, u32 seq, int type, unsigned int flags,
159 const struct vxlan_rdst *rdst)
161 unsigned long now = jiffies;
162 struct nda_cacheinfo ci;
163 struct nlmsghdr *nlh;
165 bool send_ip, send_eth;
167 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
171 ndm = nlmsg_data(nlh);
172 memset(ndm, 0, sizeof(*ndm));
174 send_eth = send_ip = true;
176 if (type == RTM_GETNEIGH) {
177 ndm->ndm_family = AF_INET;
178 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
179 send_eth = !is_zero_ether_addr(fdb->eth_addr);
181 ndm->ndm_family = AF_BRIDGE;
182 ndm->ndm_state = fdb->state;
183 ndm->ndm_ifindex = vxlan->dev->ifindex;
184 ndm->ndm_flags = fdb->flags;
185 ndm->ndm_type = NDA_DST;
187 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
188 goto nla_put_failure;
190 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
191 goto nla_put_failure;
193 if (rdst->remote_port && rdst->remote_port != vxlan_port &&
194 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
195 goto nla_put_failure;
196 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
197 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
198 goto nla_put_failure;
199 if (rdst->remote_ifindex &&
200 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
201 goto nla_put_failure;
203 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
204 ci.ndm_confirmed = 0;
205 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
208 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
209 goto nla_put_failure;
211 return nlmsg_end(skb, nlh);
214 nlmsg_cancel(skb, nlh);
218 static inline size_t vxlan_nlmsg_size(void)
220 return NLMSG_ALIGN(sizeof(struct ndmsg))
221 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
222 + nla_total_size(sizeof(__be32)) /* NDA_DST */
223 + nla_total_size(sizeof(__be32)) /* NDA_PORT */
224 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
225 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
226 + nla_total_size(sizeof(struct nda_cacheinfo));
229 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
230 const struct vxlan_fdb *fdb, int type)
232 struct net *net = dev_net(vxlan->dev);
236 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
240 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
242 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
243 WARN_ON(err == -EMSGSIZE);
248 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
252 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
255 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
257 struct vxlan_dev *vxlan = netdev_priv(dev);
260 memset(&f, 0, sizeof f);
262 f.remote.remote_ip = ipa; /* goes to NDA_DST */
263 f.remote.remote_vni = VXLAN_N_VID;
265 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
268 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
272 memset(&f, 0, sizeof f);
274 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
276 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
279 /* Hash Ethernet address */
280 static u32 eth_hash(const unsigned char *addr)
282 u64 value = get_unaligned((u64 *)addr);
284 /* only want 6 bytes */
290 return hash_64(value, FDB_HASH_BITS);
293 /* Hash chain to use given mac address */
294 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
297 return &vxlan->fdb_head[eth_hash(mac)];
300 /* Look up Ethernet address in forwarding table */
301 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
305 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
308 hlist_for_each_entry_rcu(f, head, hlist) {
309 if (compare_ether_addr(mac, f->eth_addr) == 0)
316 /* Add/update destinations for multicast */
317 static int vxlan_fdb_append(struct vxlan_fdb *f,
318 __be32 ip, __u32 port, __u32 vni, __u32 ifindex)
320 struct vxlan_rdst *rd_prev, *rd;
323 for (rd = &f->remote; rd; rd = rd->remote_next) {
324 if (rd->remote_ip == ip &&
325 rd->remote_port == port &&
326 rd->remote_vni == vni &&
327 rd->remote_ifindex == ifindex)
331 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
335 rd->remote_port = port;
336 rd->remote_vni = vni;
337 rd->remote_ifindex = ifindex;
338 rd->remote_next = NULL;
339 rd_prev->remote_next = rd;
343 /* Add new entry to forwarding table -- assumes lock held */
344 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
345 const u8 *mac, __be32 ip,
346 __u16 state, __u16 flags,
347 __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 (f->flags != ndm_flags) {
366 f->flags = ndm_flags;
367 f->updated = jiffies;
370 if ((flags & NLM_F_APPEND) &&
371 is_multicast_ether_addr(f->eth_addr)) {
372 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
379 if (!(flags & NLM_F_CREATE))
382 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
385 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
386 f = kmalloc(sizeof(*f), GFP_ATOMIC);
391 f->remote.remote_ip = ip;
392 f->remote.remote_port = port;
393 f->remote.remote_vni = vni;
394 f->remote.remote_ifindex = ifindex;
395 f->remote.remote_next = NULL;
397 f->flags = ndm_flags;
398 f->updated = f->used = jiffies;
399 memcpy(f->eth_addr, mac, ETH_ALEN);
402 hlist_add_head_rcu(&f->hlist,
403 vxlan_fdb_head(vxlan, mac));
407 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
412 static void vxlan_fdb_free(struct rcu_head *head)
414 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
416 while (f->remote.remote_next) {
417 struct vxlan_rdst *rd = f->remote.remote_next;
419 f->remote.remote_next = rd->remote_next;
425 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
427 netdev_dbg(vxlan->dev,
428 "delete %pM\n", f->eth_addr);
431 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
433 hlist_del_rcu(&f->hlist);
434 call_rcu(&f->rcu, vxlan_fdb_free);
437 /* Add static entry (via netlink) */
438 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
439 struct net_device *dev,
440 const unsigned char *addr, u16 flags)
442 struct vxlan_dev *vxlan = netdev_priv(dev);
443 struct net *net = dev_net(vxlan->dev);
445 u32 port, vni, ifindex;
448 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
449 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
454 if (tb[NDA_DST] == NULL)
457 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
458 return -EAFNOSUPPORT;
460 ip = nla_get_be32(tb[NDA_DST]);
463 if (nla_len(tb[NDA_PORT]) != sizeof(u32))
465 port = nla_get_u32(tb[NDA_PORT]);
470 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
472 vni = nla_get_u32(tb[NDA_VNI]);
474 vni = vxlan->default_dst.remote_vni;
476 if (tb[NDA_IFINDEX]) {
477 struct net_device *tdev;
479 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
481 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
482 tdev = dev_get_by_index(net, ifindex);
484 return -EADDRNOTAVAIL;
489 spin_lock_bh(&vxlan->hash_lock);
490 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, port,
491 vni, ifindex, ndm->ndm_flags);
492 spin_unlock_bh(&vxlan->hash_lock);
497 /* Delete entry (via netlink) */
498 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
499 struct net_device *dev,
500 const unsigned char *addr)
502 struct vxlan_dev *vxlan = netdev_priv(dev);
506 spin_lock_bh(&vxlan->hash_lock);
507 f = vxlan_find_mac(vxlan, addr);
509 vxlan_fdb_destroy(vxlan, f);
512 spin_unlock_bh(&vxlan->hash_lock);
517 /* Dump forwarding table */
518 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
519 struct net_device *dev, int idx)
521 struct vxlan_dev *vxlan = netdev_priv(dev);
524 for (h = 0; h < FDB_HASH_SIZE; ++h) {
528 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
529 struct vxlan_rdst *rd;
530 for (rd = &f->remote; rd; rd = rd->remote_next) {
531 if (idx < cb->args[0])
534 err = vxlan_fdb_info(skb, vxlan, f,
535 NETLINK_CB(cb->skb).portid,
550 /* Watch incoming packets to learn mapping between Ethernet address
551 * and Tunnel endpoint.
553 static void vxlan_snoop(struct net_device *dev,
554 __be32 src_ip, const u8 *src_mac)
556 struct vxlan_dev *vxlan = netdev_priv(dev);
560 f = vxlan_find_mac(vxlan, src_mac);
563 if (likely(f->remote.remote_ip == src_ip))
568 "%pM migrated from %pI4 to %pI4\n",
569 src_mac, &f->remote.remote_ip, &src_ip);
571 f->remote.remote_ip = src_ip;
572 f->updated = jiffies;
574 /* learned new entry */
575 spin_lock(&vxlan->hash_lock);
576 err = vxlan_fdb_create(vxlan, src_mac, src_ip,
578 NLM_F_EXCL|NLM_F_CREATE,
580 vxlan->default_dst.remote_vni,
582 spin_unlock(&vxlan->hash_lock);
587 /* See if multicast group is already in use by other ID */
588 static bool vxlan_group_used(struct vxlan_net *vn,
589 const struct vxlan_dev *this)
591 const struct vxlan_dev *vxlan;
594 for (h = 0; h < VNI_HASH_SIZE; ++h)
595 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
599 if (!netif_running(vxlan->dev))
602 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
609 /* kernel equivalent to IP_ADD_MEMBERSHIP */
610 static int vxlan_join_group(struct net_device *dev)
612 struct vxlan_dev *vxlan = netdev_priv(dev);
613 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
614 struct sock *sk = vn->sock->sk;
615 struct ip_mreqn mreq = {
616 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
617 .imr_ifindex = vxlan->default_dst.remote_ifindex,
621 /* Already a member of group */
622 if (vxlan_group_used(vn, vxlan))
625 /* Need to drop RTNL to call multicast join */
628 err = ip_mc_join_group(sk, &mreq);
636 /* kernel equivalent to IP_DROP_MEMBERSHIP */
637 static int vxlan_leave_group(struct net_device *dev)
639 struct vxlan_dev *vxlan = netdev_priv(dev);
640 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
642 struct sock *sk = vn->sock->sk;
643 struct ip_mreqn mreq = {
644 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
645 .imr_ifindex = vxlan->default_dst.remote_ifindex,
648 /* Only leave group when last vxlan is done. */
649 if (vxlan_group_used(vn, vxlan))
652 /* Need to drop RTNL to call multicast leave */
655 err = ip_mc_leave_group(sk, &mreq);
662 /* Callback from net/ipv4/udp.c to receive packets */
663 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
666 struct vxlanhdr *vxh;
667 struct vxlan_dev *vxlan;
668 struct pcpu_tstats *stats;
672 /* pop off outer UDP header */
673 __skb_pull(skb, sizeof(struct udphdr));
675 /* Need Vxlan and inner Ethernet header to be present */
676 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
679 /* Drop packets with reserved bits set */
680 vxh = (struct vxlanhdr *) skb->data;
681 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
682 (vxh->vx_vni & htonl(0xff))) {
683 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
684 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
688 __skb_pull(skb, sizeof(struct vxlanhdr));
690 /* Is this VNI defined? */
691 vni = ntohl(vxh->vx_vni) >> 8;
692 vxlan = vxlan_find_vni(sock_net(sk), vni);
694 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
698 if (!pskb_may_pull(skb, ETH_HLEN)) {
699 vxlan->dev->stats.rx_length_errors++;
700 vxlan->dev->stats.rx_errors++;
704 skb_reset_mac_header(skb);
706 /* Re-examine inner Ethernet packet */
708 skb->protocol = eth_type_trans(skb, vxlan->dev);
710 /* Ignore packet loops (and multicast echo) */
711 if (compare_ether_addr(eth_hdr(skb)->h_source,
712 vxlan->dev->dev_addr) == 0)
715 if (vxlan->flags & VXLAN_F_LEARN)
716 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
718 __skb_tunnel_rx(skb, vxlan->dev);
719 skb_reset_network_header(skb);
721 /* If the NIC driver gave us an encapsulated packet with
722 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
723 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
724 * for us. Otherwise force the upper layers to verify it.
726 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
727 !(vxlan->dev->features & NETIF_F_RXCSUM))
728 skb->ip_summed = CHECKSUM_NONE;
730 skb->encapsulation = 0;
732 err = IP_ECN_decapsulate(oip, skb);
735 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
736 &oip->saddr, oip->tos);
738 ++vxlan->dev->stats.rx_frame_errors;
739 ++vxlan->dev->stats.rx_errors;
744 stats = this_cpu_ptr(vxlan->dev->tstats);
745 u64_stats_update_begin(&stats->syncp);
747 stats->rx_bytes += skb->len;
748 u64_stats_update_end(&stats->syncp);
754 /* Put UDP header back */
755 __skb_push(skb, sizeof(struct udphdr));
759 /* Consume bad packet */
764 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
766 struct vxlan_dev *vxlan = netdev_priv(dev);
772 if (dev->flags & IFF_NOARP)
775 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
776 dev->stats.tx_dropped++;
781 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
782 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
783 parp->ar_pro != htons(ETH_P_IP) ||
784 parp->ar_op != htons(ARPOP_REQUEST) ||
785 parp->ar_hln != dev->addr_len ||
788 arpptr = (u8 *)parp + sizeof(struct arphdr);
790 arpptr += dev->addr_len; /* sha */
791 memcpy(&sip, arpptr, sizeof(sip));
792 arpptr += sizeof(sip);
793 arpptr += dev->addr_len; /* tha */
794 memcpy(&tip, arpptr, sizeof(tip));
796 if (ipv4_is_loopback(tip) ||
797 ipv4_is_multicast(tip))
800 n = neigh_lookup(&arp_tbl, &tip, dev);
804 struct sk_buff *reply;
806 if (!(n->nud_state & NUD_CONNECTED)) {
811 f = vxlan_find_mac(vxlan, n->ha);
812 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
813 /* bridge-local neighbor */
818 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
823 skb_reset_mac_header(reply);
824 __skb_pull(reply, skb_network_offset(reply));
825 reply->ip_summed = CHECKSUM_UNNECESSARY;
826 reply->pkt_type = PACKET_HOST;
828 if (netif_rx_ni(reply) == NET_RX_DROP)
829 dev->stats.rx_dropped++;
830 } else if (vxlan->flags & VXLAN_F_L3MISS)
831 vxlan_ip_miss(dev, tip);
837 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
839 struct vxlan_dev *vxlan = netdev_priv(dev);
843 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
847 switch (ntohs(eth_hdr(skb)->h_proto)) {
849 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
852 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
861 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
863 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
865 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
869 } else if (vxlan->flags & VXLAN_F_L3MISS)
870 vxlan_ip_miss(dev, pip->daddr);
874 static void vxlan_sock_free(struct sk_buff *skb)
879 /* On transmit, associate with the tunnel socket */
880 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
882 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
883 struct sock *sk = vn->sock->sk;
888 skb->destructor = vxlan_sock_free;
891 /* Compute source port for outgoing packet
892 * first choice to use L4 flow hash since it will spread
893 * better and maybe available from hardware
894 * secondary choice is to use jhash on the Ethernet header
896 static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
898 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
901 hash = skb_get_rxhash(skb);
903 hash = jhash(skb->data, 2 * ETH_ALEN,
904 (__force u32) skb->protocol);
906 return (((u64) hash * range) >> 32) + vxlan->port_min;
909 static int handle_offloads(struct sk_buff *skb)
911 if (skb_is_gso(skb)) {
912 int err = skb_unclone(skb, GFP_ATOMIC);
916 skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP);
917 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
918 skb->ip_summed = CHECKSUM_NONE;
923 /* Bypass encapsulation if the destination is local */
924 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
925 struct vxlan_dev *dst_vxlan)
927 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
928 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
930 skb->pkt_type = PACKET_HOST;
931 skb->encapsulation = 0;
932 skb->dev = dst_vxlan->dev;
933 __skb_pull(skb, skb_network_offset(skb));
935 if (dst_vxlan->flags & VXLAN_F_LEARN)
936 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
937 eth_hdr(skb)->h_source);
939 u64_stats_update_begin(&tx_stats->syncp);
940 tx_stats->tx_packets++;
941 tx_stats->tx_bytes += skb->len;
942 u64_stats_update_end(&tx_stats->syncp);
944 if (netif_rx(skb) == NET_RX_SUCCESS) {
945 u64_stats_update_begin(&rx_stats->syncp);
946 rx_stats->rx_packets++;
947 rx_stats->rx_bytes += skb->len;
948 u64_stats_update_end(&rx_stats->syncp);
950 skb->dev->stats.rx_dropped++;
954 static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
955 struct vxlan_rdst *rdst, bool did_rsc)
957 struct vxlan_dev *vxlan = netdev_priv(dev);
959 const struct iphdr *old_iph;
961 struct vxlanhdr *vxh;
965 __u16 src_port, dst_port;
970 dst_port = rdst->remote_port ? rdst->remote_port : vxlan_port;
971 vni = rdst->remote_vni;
972 dst = rdst->remote_ip;
976 /* short-circuited back to local bridge */
977 vxlan_encap_bypass(skb, vxlan, vxlan);
983 if (!skb->encapsulation) {
984 skb_reset_inner_headers(skb);
985 skb->encapsulation = 1;
988 /* Need space for new headers (invalidates iph ptr) */
989 if (skb_cow_head(skb, VXLAN_HEADROOM))
992 old_iph = ip_hdr(skb);
995 if (!ttl && IN_MULTICAST(ntohl(dst)))
1000 tos = ip_tunnel_get_dsfield(old_iph, skb);
1002 src_port = vxlan_src_port(vxlan, skb);
1004 memset(&fl4, 0, sizeof(fl4));
1005 fl4.flowi4_oif = rdst->remote_ifindex;
1006 fl4.flowi4_tos = RT_TOS(tos);
1008 fl4.saddr = vxlan->saddr;
1010 rt = ip_route_output_key(dev_net(dev), &fl4);
1012 netdev_dbg(dev, "no route to %pI4\n", &dst);
1013 dev->stats.tx_carrier_errors++;
1017 if (rt->dst.dev == dev) {
1018 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1020 dev->stats.collisions++;
1024 /* Bypass encapsulation if the destination is local */
1025 if (rt->rt_flags & RTCF_LOCAL &&
1026 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1027 struct vxlan_dev *dst_vxlan;
1030 dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
1033 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1034 return NETDEV_TX_OK;
1037 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1038 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1041 skb_dst_set(skb, &rt->dst);
1043 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1044 vxh->vx_flags = htonl(VXLAN_FLAGS);
1045 vxh->vx_vni = htonl(vni << 8);
1047 __skb_push(skb, sizeof(*uh));
1048 skb_reset_transport_header(skb);
1051 uh->dest = htons(dst_port);
1052 uh->source = htons(src_port);
1054 uh->len = htons(skb->len);
1057 __skb_push(skb, sizeof(*iph));
1058 skb_reset_network_header(skb);
1061 iph->ihl = sizeof(struct iphdr) >> 2;
1063 iph->protocol = IPPROTO_UDP;
1064 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1066 iph->saddr = fl4.saddr;
1067 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1068 tunnel_ip_select_ident(skb, old_iph, &rt->dst);
1072 vxlan_set_owner(dev, skb);
1074 if (handle_offloads(skb))
1077 iptunnel_xmit(skb, dev);
1078 return NETDEV_TX_OK;
1081 dev->stats.tx_dropped++;
1085 dev->stats.tx_errors++;
1088 return NETDEV_TX_OK;
1091 /* Transmit local packets over Vxlan
1093 * Outer IP header inherits ECN and DF from inner header.
1094 * Outer UDP destination is the VXLAN assigned port.
1095 * source port is based on hash of flow
1097 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1099 struct vxlan_dev *vxlan = netdev_priv(dev);
1101 bool did_rsc = false;
1102 struct vxlan_rdst *rdst0, *rdst;
1103 struct vxlan_fdb *f;
1106 skb_reset_mac_header(skb);
1109 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1110 return arp_reduce(dev, skb);
1112 f = vxlan_find_mac(vxlan, eth->h_dest);
1115 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1116 ntohs(eth->h_proto) == ETH_P_IP) {
1117 did_rsc = route_shortcircuit(dev, skb);
1119 f = vxlan_find_mac(vxlan, eth->h_dest);
1123 rdst0 = &vxlan->default_dst;
1125 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
1126 (vxlan->flags & VXLAN_F_L2MISS) &&
1127 !is_multicast_ether_addr(eth->h_dest))
1128 vxlan_fdb_miss(vxlan, eth->h_dest);
1134 /* if there are multiple destinations, send copies */
1135 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1136 struct sk_buff *skb1;
1138 skb1 = skb_clone(skb, GFP_ATOMIC);
1139 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1140 if (rc == NETDEV_TX_OK)
1144 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1145 if (rc == NETDEV_TX_OK)
1150 /* Walk the forwarding table and purge stale entries */
1151 static void vxlan_cleanup(unsigned long arg)
1153 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1154 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1157 if (!netif_running(vxlan->dev))
1160 spin_lock_bh(&vxlan->hash_lock);
1161 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1162 struct hlist_node *p, *n;
1163 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1165 = container_of(p, struct vxlan_fdb, hlist);
1166 unsigned long timeout;
1168 if (f->state & NUD_PERMANENT)
1171 timeout = f->used + vxlan->age_interval * HZ;
1172 if (time_before_eq(timeout, jiffies)) {
1173 netdev_dbg(vxlan->dev,
1174 "garbage collect %pM\n",
1176 f->state = NUD_STALE;
1177 vxlan_fdb_destroy(vxlan, f);
1178 } else if (time_before(timeout, next_timer))
1179 next_timer = timeout;
1182 spin_unlock_bh(&vxlan->hash_lock);
1184 mod_timer(&vxlan->age_timer, next_timer);
1187 /* Setup stats when device is created */
1188 static int vxlan_init(struct net_device *dev)
1190 dev->tstats = alloc_percpu(struct pcpu_tstats);
1197 /* Start ageing timer and join group when device is brought up */
1198 static int vxlan_open(struct net_device *dev)
1200 struct vxlan_dev *vxlan = netdev_priv(dev);
1203 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1204 err = vxlan_join_group(dev);
1209 if (vxlan->age_interval)
1210 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1215 /* Purge the forwarding table */
1216 static void vxlan_flush(struct vxlan_dev *vxlan)
1220 spin_lock_bh(&vxlan->hash_lock);
1221 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1222 struct hlist_node *p, *n;
1223 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1225 = container_of(p, struct vxlan_fdb, hlist);
1226 vxlan_fdb_destroy(vxlan, f);
1229 spin_unlock_bh(&vxlan->hash_lock);
1232 /* Cleanup timer and forwarding table on shutdown */
1233 static int vxlan_stop(struct net_device *dev)
1235 struct vxlan_dev *vxlan = netdev_priv(dev);
1237 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
1238 vxlan_leave_group(dev);
1240 del_timer_sync(&vxlan->age_timer);
1247 /* Stub, nothing needs to be done. */
1248 static void vxlan_set_multicast_list(struct net_device *dev)
1252 static const struct net_device_ops vxlan_netdev_ops = {
1253 .ndo_init = vxlan_init,
1254 .ndo_open = vxlan_open,
1255 .ndo_stop = vxlan_stop,
1256 .ndo_start_xmit = vxlan_xmit,
1257 .ndo_get_stats64 = ip_tunnel_get_stats64,
1258 .ndo_set_rx_mode = vxlan_set_multicast_list,
1259 .ndo_change_mtu = eth_change_mtu,
1260 .ndo_validate_addr = eth_validate_addr,
1261 .ndo_set_mac_address = eth_mac_addr,
1262 .ndo_fdb_add = vxlan_fdb_add,
1263 .ndo_fdb_del = vxlan_fdb_delete,
1264 .ndo_fdb_dump = vxlan_fdb_dump,
1267 /* Info for udev, that this is a virtual tunnel endpoint */
1268 static struct device_type vxlan_type = {
1272 static void vxlan_free(struct net_device *dev)
1274 free_percpu(dev->tstats);
1278 /* Initialize the device structure. */
1279 static void vxlan_setup(struct net_device *dev)
1281 struct vxlan_dev *vxlan = netdev_priv(dev);
1285 eth_hw_addr_random(dev);
1287 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1289 dev->netdev_ops = &vxlan_netdev_ops;
1290 dev->destructor = vxlan_free;
1291 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1293 dev->tx_queue_len = 0;
1294 dev->features |= NETIF_F_LLTX;
1295 dev->features |= NETIF_F_NETNS_LOCAL;
1296 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1297 dev->features |= NETIF_F_RXCSUM;
1298 dev->features |= NETIF_F_GSO_SOFTWARE;
1300 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1301 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1302 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1303 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1305 spin_lock_init(&vxlan->hash_lock);
1307 init_timer_deferrable(&vxlan->age_timer);
1308 vxlan->age_timer.function = vxlan_cleanup;
1309 vxlan->age_timer.data = (unsigned long) vxlan;
1311 inet_get_local_port_range(&low, &high);
1312 vxlan->port_min = low;
1313 vxlan->port_max = high;
1317 for (h = 0; h < FDB_HASH_SIZE; ++h)
1318 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1321 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1322 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
1323 [IFLA_VXLAN_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1324 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1325 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1326 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1327 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1328 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1329 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1330 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
1331 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
1332 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1333 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1334 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1335 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
1338 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1340 if (tb[IFLA_ADDRESS]) {
1341 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1342 pr_debug("invalid link address (not ethernet)\n");
1346 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1347 pr_debug("invalid all zero ethernet address\n");
1348 return -EADDRNOTAVAIL;
1355 if (data[IFLA_VXLAN_ID]) {
1356 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1357 if (id >= VXLAN_VID_MASK)
1361 if (data[IFLA_VXLAN_PORT_RANGE]) {
1362 const struct ifla_vxlan_port_range *p
1363 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1365 if (ntohs(p->high) < ntohs(p->low)) {
1366 pr_debug("port range %u .. %u not valid\n",
1367 ntohs(p->low), ntohs(p->high));
1375 static void vxlan_get_drvinfo(struct net_device *netdev,
1376 struct ethtool_drvinfo *drvinfo)
1378 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1379 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1382 static const struct ethtool_ops vxlan_ethtool_ops = {
1383 .get_drvinfo = vxlan_get_drvinfo,
1384 .get_link = ethtool_op_get_link,
1387 static int vxlan_newlink(struct net *net, struct net_device *dev,
1388 struct nlattr *tb[], struct nlattr *data[])
1390 struct vxlan_dev *vxlan = netdev_priv(dev);
1391 struct vxlan_rdst *dst = &vxlan->default_dst;
1395 if (!data[IFLA_VXLAN_ID])
1398 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1399 if (vxlan_find_vni(net, vni)) {
1400 pr_info("duplicate VNI %u\n", vni);
1403 dst->remote_vni = vni;
1405 if (data[IFLA_VXLAN_REMOTE])
1406 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_REMOTE]);
1408 if (data[IFLA_VXLAN_LOCAL])
1409 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1411 if (data[IFLA_VXLAN_LINK] &&
1412 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1413 struct net_device *lowerdev
1414 = __dev_get_by_index(net, dst->remote_ifindex);
1417 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1422 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1424 /* update header length based on lower device */
1425 dev->hard_header_len = lowerdev->hard_header_len +
1429 if (data[IFLA_VXLAN_TOS])
1430 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1432 if (data[IFLA_VXLAN_TTL])
1433 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1435 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1436 vxlan->flags |= VXLAN_F_LEARN;
1438 if (data[IFLA_VXLAN_AGEING])
1439 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1441 vxlan->age_interval = FDB_AGE_DEFAULT;
1443 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1444 vxlan->flags |= VXLAN_F_PROXY;
1446 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1447 vxlan->flags |= VXLAN_F_RSC;
1449 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1450 vxlan->flags |= VXLAN_F_L2MISS;
1452 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1453 vxlan->flags |= VXLAN_F_L3MISS;
1455 if (data[IFLA_VXLAN_LIMIT])
1456 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1458 if (data[IFLA_VXLAN_PORT_RANGE]) {
1459 const struct ifla_vxlan_port_range *p
1460 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1461 vxlan->port_min = ntohs(p->low);
1462 vxlan->port_max = ntohs(p->high);
1465 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1467 err = register_netdevice(dev);
1469 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
1474 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1476 struct vxlan_dev *vxlan = netdev_priv(dev);
1478 hlist_del_rcu(&vxlan->hlist);
1480 unregister_netdevice_queue(dev, head);
1483 static size_t vxlan_get_size(const struct net_device *dev)
1486 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1487 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_REMOTE */
1488 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1489 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1490 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1491 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1492 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
1493 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1494 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1495 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1496 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
1497 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1498 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1499 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1503 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1505 const struct vxlan_dev *vxlan = netdev_priv(dev);
1506 const struct vxlan_rdst *dst = &vxlan->default_dst;
1507 struct ifla_vxlan_port_range ports = {
1508 .low = htons(vxlan->port_min),
1509 .high = htons(vxlan->port_max),
1512 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1513 goto nla_put_failure;
1515 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_REMOTE, dst->remote_ip))
1516 goto nla_put_failure;
1518 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1519 goto nla_put_failure;
1521 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1522 goto nla_put_failure;
1524 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1525 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1526 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1527 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1528 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1529 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1530 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1531 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1532 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1533 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1534 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1535 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1536 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
1537 goto nla_put_failure;
1539 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1540 goto nla_put_failure;
1548 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1550 .maxtype = IFLA_VXLAN_MAX,
1551 .policy = vxlan_policy,
1552 .priv_size = sizeof(struct vxlan_dev),
1553 .setup = vxlan_setup,
1554 .validate = vxlan_validate,
1555 .newlink = vxlan_newlink,
1556 .dellink = vxlan_dellink,
1557 .get_size = vxlan_get_size,
1558 .fill_info = vxlan_fill_info,
1561 static __net_init int vxlan_init_net(struct net *net)
1563 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1565 struct sockaddr_in vxlan_addr = {
1566 .sin_family = AF_INET,
1567 .sin_addr.s_addr = htonl(INADDR_ANY),
1572 /* Create UDP socket for encapsulation receive. */
1573 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1575 pr_debug("UDP socket create failed\n");
1578 /* Put in proper namespace */
1580 sk_change_net(sk, net);
1582 vxlan_addr.sin_port = htons(vxlan_port);
1584 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1585 sizeof(vxlan_addr));
1587 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1588 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1589 sk_release_kernel(sk);
1594 /* Disable multicast loopback */
1595 inet_sk(sk)->mc_loop = 0;
1597 /* Mark socket as an encapsulation socket. */
1598 udp_sk(sk)->encap_type = 1;
1599 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1602 for (h = 0; h < VNI_HASH_SIZE; ++h)
1603 INIT_HLIST_HEAD(&vn->vni_list[h]);
1608 static __net_exit void vxlan_exit_net(struct net *net)
1610 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1611 struct vxlan_dev *vxlan;
1615 for (h = 0; h < VNI_HASH_SIZE; ++h)
1616 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
1617 dev_close(vxlan->dev);
1621 sk_release_kernel(vn->sock->sk);
1626 static struct pernet_operations vxlan_net_ops = {
1627 .init = vxlan_init_net,
1628 .exit = vxlan_exit_net,
1629 .id = &vxlan_net_id,
1630 .size = sizeof(struct vxlan_net),
1633 static int __init vxlan_init_module(void)
1637 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1639 rc = register_pernet_device(&vxlan_net_ops);
1643 rc = rtnl_link_register(&vxlan_link_ops);
1650 unregister_pernet_device(&vxlan_net_ops);
1654 module_init(vxlan_init_module);
1656 static void __exit vxlan_cleanup_module(void)
1658 rtnl_link_unregister(&vxlan_link_ops);
1659 unregister_pernet_device(&vxlan_net_ops);
1662 module_exit(vxlan_cleanup_module);
1664 MODULE_LICENSE("GPL");
1665 MODULE_VERSION(VXLAN_VERSION);
1666 MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1667 MODULE_ALIAS_RTNL_LINK("vxlan");