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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
31 #include <net/ndisc.h>
33 #include <net/ip_tunnels.h>
36 #include <net/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
43 #if IS_ENABLED(CONFIG_IPV6)
45 #include <net/addrconf.h>
46 #include <net/ip6_tunnel.h>
49 #define VXLAN_VERSION "0.1"
51 #define PORT_HASH_BITS 8
52 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
53 #define VNI_HASH_BITS 10
54 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
55 #define FDB_HASH_BITS 8
56 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
57 #define FDB_AGE_DEFAULT 300 /* 5 min */
58 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60 #define VXLAN_N_VID (1u << 24)
61 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
62 /* IP header + UDP + VXLAN + Ethernet header */
63 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
64 /* IPv6 header + UDP + VXLAN + Ethernet header */
65 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
66 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
68 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
70 /* VXLAN protocol header */
76 /* UDP port for VXLAN traffic.
77 * The IANA assigned port is 4789, but the Linux default is 8472
78 * for compatibility with early adopters.
80 static unsigned short vxlan_port __read_mostly = 8472;
81 module_param_named(udp_port, vxlan_port, ushort, 0444);
82 MODULE_PARM_DESC(udp_port, "Destination UDP port");
84 static bool log_ecn_error = true;
85 module_param(log_ecn_error, bool, 0644);
86 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
88 static int vxlan_net_id;
90 static const u8 all_zeros_mac[ETH_ALEN];
92 /* per-network namespace private data for this module */
94 struct list_head vxlan_list;
95 struct hlist_head sock_list[PORT_HASH_SIZE];
100 struct sockaddr_in sin;
101 struct sockaddr_in6 sin6;
106 union vxlan_addr remote_ip;
110 struct list_head list;
114 /* Forwarding table entry */
116 struct hlist_node hlist; /* linked list of entries */
118 unsigned long updated; /* jiffies */
120 struct list_head remotes;
121 u16 state; /* see ndm_state */
122 u8 flags; /* see ndm_flags */
123 u8 eth_addr[ETH_ALEN];
126 /* Pseudo network device */
128 struct hlist_node hlist; /* vni hash table */
129 struct list_head next; /* vxlan's per namespace list */
130 struct vxlan_sock *vn_sock; /* listening socket */
131 struct net_device *dev;
132 struct vxlan_rdst default_dst; /* default destination */
133 union vxlan_addr saddr; /* source address */
135 __u16 port_min; /* source port range */
137 __u8 tos; /* TOS override */
139 u32 flags; /* VXLAN_F_* below */
141 struct work_struct sock_work;
142 struct work_struct igmp_join;
143 struct work_struct igmp_leave;
145 unsigned long age_interval;
146 struct timer_list age_timer;
147 spinlock_t hash_lock;
148 unsigned int addrcnt;
149 unsigned int addrmax;
151 struct hlist_head fdb_head[FDB_HASH_SIZE];
154 #define VXLAN_F_LEARN 0x01
155 #define VXLAN_F_PROXY 0x02
156 #define VXLAN_F_RSC 0x04
157 #define VXLAN_F_L2MISS 0x08
158 #define VXLAN_F_L3MISS 0x10
159 #define VXLAN_F_IPV6 0x20 /* internal flag */
161 /* salt for hash table */
162 static u32 vxlan_salt __read_mostly;
163 static struct workqueue_struct *vxlan_wq;
165 static void vxlan_sock_work(struct work_struct *work);
167 #if IS_ENABLED(CONFIG_IPV6)
169 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
171 if (a->sa.sa_family != b->sa.sa_family)
173 if (a->sa.sa_family == AF_INET6)
174 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
176 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
179 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
181 if (ipa->sa.sa_family == AF_INET6)
182 return ipv6_addr_any(&ipa->sin6.sin6_addr);
184 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
187 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
189 if (ipa->sa.sa_family == AF_INET6)
190 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
192 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
195 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
197 if (nla_len(nla) >= sizeof(struct in6_addr)) {
198 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
199 ip->sa.sa_family = AF_INET6;
201 } else if (nla_len(nla) >= sizeof(__be32)) {
202 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
203 ip->sa.sa_family = AF_INET;
206 return -EAFNOSUPPORT;
210 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
211 const union vxlan_addr *ip)
213 if (ip->sa.sa_family == AF_INET6)
214 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
216 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
219 #else /* !CONFIG_IPV6 */
222 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
224 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
227 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
229 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
232 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
234 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
237 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
239 if (nla_len(nla) >= sizeof(struct in6_addr)) {
240 return -EAFNOSUPPORT;
241 } else if (nla_len(nla) >= sizeof(__be32)) {
242 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
243 ip->sa.sa_family = AF_INET;
246 return -EAFNOSUPPORT;
250 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
251 const union vxlan_addr *ip)
253 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
257 /* Virtual Network hash table head */
258 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
260 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
263 /* Socket hash table head */
264 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
266 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
268 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
271 /* First remote destination for a forwarding entry.
272 * Guaranteed to be non-NULL because remotes are never deleted.
274 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
276 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
279 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
281 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
284 /* Find VXLAN socket based on network namespace and UDP port */
285 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
287 struct vxlan_sock *vs;
289 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
290 if (inet_sk(vs->sock->sk)->inet_sport == port)
296 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
298 struct vxlan_dev *vxlan;
300 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
301 if (vxlan->default_dst.remote_vni == id)
308 /* Look up VNI in a per net namespace table */
309 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
311 struct vxlan_sock *vs;
313 vs = vxlan_find_sock(net, port);
317 return vxlan_vs_find_vni(vs, id);
320 /* Fill in neighbour message in skbuff. */
321 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
322 const struct vxlan_fdb *fdb,
323 u32 portid, u32 seq, int type, unsigned int flags,
324 const struct vxlan_rdst *rdst)
326 unsigned long now = jiffies;
327 struct nda_cacheinfo ci;
328 struct nlmsghdr *nlh;
330 bool send_ip, send_eth;
332 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
336 ndm = nlmsg_data(nlh);
337 memset(ndm, 0, sizeof(*ndm));
339 send_eth = send_ip = true;
341 if (type == RTM_GETNEIGH) {
342 ndm->ndm_family = AF_INET;
343 send_ip = !vxlan_addr_any(&rdst->remote_ip);
344 send_eth = !is_zero_ether_addr(fdb->eth_addr);
346 ndm->ndm_family = AF_BRIDGE;
347 ndm->ndm_state = fdb->state;
348 ndm->ndm_ifindex = vxlan->dev->ifindex;
349 ndm->ndm_flags = fdb->flags;
350 ndm->ndm_type = NDA_DST;
352 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
353 goto nla_put_failure;
355 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
356 goto nla_put_failure;
358 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
359 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
360 goto nla_put_failure;
361 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
362 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
363 goto nla_put_failure;
364 if (rdst->remote_ifindex &&
365 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
366 goto nla_put_failure;
368 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
369 ci.ndm_confirmed = 0;
370 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
373 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
374 goto nla_put_failure;
376 return nlmsg_end(skb, nlh);
379 nlmsg_cancel(skb, nlh);
383 static inline size_t vxlan_nlmsg_size(void)
385 return NLMSG_ALIGN(sizeof(struct ndmsg))
386 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
387 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
388 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
389 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
390 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
391 + nla_total_size(sizeof(struct nda_cacheinfo));
394 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
395 struct vxlan_fdb *fdb, int type)
397 struct net *net = dev_net(vxlan->dev);
401 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
405 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
406 first_remote_rtnl(fdb));
408 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
409 WARN_ON(err == -EMSGSIZE);
414 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
418 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
421 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
423 struct vxlan_dev *vxlan = netdev_priv(dev);
424 struct vxlan_fdb f = {
427 struct vxlan_rdst remote = {
428 .remote_ip = *ipa, /* goes to NDA_DST */
429 .remote_vni = VXLAN_N_VID,
432 INIT_LIST_HEAD(&f.remotes);
433 list_add_rcu(&remote.list, &f.remotes);
435 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
438 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
440 struct vxlan_fdb f = {
444 INIT_LIST_HEAD(&f.remotes);
445 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
447 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
450 /* Hash Ethernet address */
451 static u32 eth_hash(const unsigned char *addr)
453 u64 value = get_unaligned((u64 *)addr);
455 /* only want 6 bytes */
461 return hash_64(value, FDB_HASH_BITS);
464 /* Hash chain to use given mac address */
465 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
468 return &vxlan->fdb_head[eth_hash(mac)];
471 /* Look up Ethernet address in forwarding table */
472 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
476 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
479 hlist_for_each_entry_rcu(f, head, hlist) {
480 if (compare_ether_addr(mac, f->eth_addr) == 0)
487 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
492 f = __vxlan_find_mac(vxlan, mac);
499 /* caller should hold vxlan->hash_lock */
500 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
501 union vxlan_addr *ip, __be16 port,
502 __u32 vni, __u32 ifindex)
504 struct vxlan_rdst *rd;
506 list_for_each_entry(rd, &f->remotes, list) {
507 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
508 rd->remote_port == port &&
509 rd->remote_vni == vni &&
510 rd->remote_ifindex == ifindex)
517 /* Replace destination of unicast mac */
518 static int vxlan_fdb_replace(struct vxlan_fdb *f,
519 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
521 struct vxlan_rdst *rd;
523 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
527 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
531 rd->remote_port = port;
532 rd->remote_vni = vni;
533 rd->remote_ifindex = ifindex;
537 /* Add/update destinations for multicast */
538 static int vxlan_fdb_append(struct vxlan_fdb *f,
539 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
541 struct vxlan_rdst *rd;
543 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
547 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
551 rd->remote_port = port;
552 rd->remote_vni = vni;
553 rd->remote_ifindex = ifindex;
555 list_add_tail_rcu(&rd->list, &f->remotes);
560 /* Add new entry to forwarding table -- assumes lock held */
561 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
562 const u8 *mac, union vxlan_addr *ip,
563 __u16 state, __u16 flags,
564 __be16 port, __u32 vni, __u32 ifindex,
570 f = __vxlan_find_mac(vxlan, mac);
572 if (flags & NLM_F_EXCL) {
573 netdev_dbg(vxlan->dev,
574 "lost race to create %pM\n", mac);
577 if (f->state != state) {
579 f->updated = jiffies;
582 if (f->flags != ndm_flags) {
583 f->flags = ndm_flags;
584 f->updated = jiffies;
587 if ((flags & NLM_F_REPLACE)) {
588 /* Only change unicasts */
589 if (!(is_multicast_ether_addr(f->eth_addr) ||
590 is_zero_ether_addr(f->eth_addr))) {
591 int rc = vxlan_fdb_replace(f, ip, port, vni,
600 if ((flags & NLM_F_APPEND) &&
601 (is_multicast_ether_addr(f->eth_addr) ||
602 is_zero_ether_addr(f->eth_addr))) {
603 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
610 if (!(flags & NLM_F_CREATE))
613 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
616 /* Disallow replace to add a multicast entry */
617 if ((flags & NLM_F_REPLACE) &&
618 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
621 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
622 f = kmalloc(sizeof(*f), GFP_ATOMIC);
628 f->flags = ndm_flags;
629 f->updated = f->used = jiffies;
630 INIT_LIST_HEAD(&f->remotes);
631 memcpy(f->eth_addr, mac, ETH_ALEN);
633 vxlan_fdb_append(f, ip, port, vni, ifindex);
636 hlist_add_head_rcu(&f->hlist,
637 vxlan_fdb_head(vxlan, mac));
641 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
646 static void vxlan_fdb_free(struct rcu_head *head)
648 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
649 struct vxlan_rdst *rd, *nd;
651 list_for_each_entry_safe(rd, nd, &f->remotes, list)
656 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
658 netdev_dbg(vxlan->dev,
659 "delete %pM\n", f->eth_addr);
662 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
664 hlist_del_rcu(&f->hlist);
665 call_rcu(&f->rcu, vxlan_fdb_free);
668 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
669 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
671 struct net *net = dev_net(vxlan->dev);
675 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
679 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
680 if (remote->sa.sa_family == AF_INET) {
681 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
682 ip->sa.sa_family = AF_INET;
683 #if IS_ENABLED(CONFIG_IPV6)
685 ip->sin6.sin6_addr = in6addr_any;
686 ip->sa.sa_family = AF_INET6;
692 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
694 *port = nla_get_be16(tb[NDA_PORT]);
696 *port = vxlan->dst_port;
700 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
702 *vni = nla_get_u32(tb[NDA_VNI]);
704 *vni = vxlan->default_dst.remote_vni;
707 if (tb[NDA_IFINDEX]) {
708 struct net_device *tdev;
710 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
712 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
713 tdev = dev_get_by_index(net, *ifindex);
715 return -EADDRNOTAVAIL;
724 /* Add static entry (via netlink) */
725 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
726 struct net_device *dev,
727 const unsigned char *addr, u16 flags)
729 struct vxlan_dev *vxlan = netdev_priv(dev);
730 /* struct net *net = dev_net(vxlan->dev); */
736 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
737 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
742 if (tb[NDA_DST] == NULL)
745 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
749 spin_lock_bh(&vxlan->hash_lock);
750 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
751 port, vni, ifindex, ndm->ndm_flags);
752 spin_unlock_bh(&vxlan->hash_lock);
757 /* Delete entry (via netlink) */
758 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
759 struct net_device *dev,
760 const unsigned char *addr)
762 struct vxlan_dev *vxlan = netdev_priv(dev);
764 struct vxlan_rdst *rd = NULL;
770 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
776 spin_lock_bh(&vxlan->hash_lock);
777 f = vxlan_find_mac(vxlan, addr);
781 if (!vxlan_addr_any(&ip)) {
782 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
789 /* remove a destination if it's not the only one on the list,
790 * otherwise destroy the fdb entry
792 if (rd && !list_is_singular(&f->remotes)) {
793 list_del_rcu(&rd->list);
798 vxlan_fdb_destroy(vxlan, f);
801 spin_unlock_bh(&vxlan->hash_lock);
806 /* Dump forwarding table */
807 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
808 struct net_device *dev, int idx)
810 struct vxlan_dev *vxlan = netdev_priv(dev);
813 for (h = 0; h < FDB_HASH_SIZE; ++h) {
817 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
818 struct vxlan_rdst *rd;
820 if (idx < cb->args[0])
823 list_for_each_entry_rcu(rd, &f->remotes, list) {
824 err = vxlan_fdb_info(skb, vxlan, f,
825 NETLINK_CB(cb->skb).portid,
840 /* Watch incoming packets to learn mapping between Ethernet address
841 * and Tunnel endpoint.
842 * Return true if packet is bogus and should be droppped.
844 static bool vxlan_snoop(struct net_device *dev,
845 union vxlan_addr *src_ip, const u8 *src_mac)
847 struct vxlan_dev *vxlan = netdev_priv(dev);
850 f = vxlan_find_mac(vxlan, src_mac);
852 struct vxlan_rdst *rdst = first_remote_rcu(f);
854 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
857 /* Don't migrate static entries, drop packets */
858 if (f->state & NUD_NOARP)
863 "%pM migrated from %pIS to %pIS\n",
864 src_mac, &rdst->remote_ip, &src_ip);
866 rdst->remote_ip = *src_ip;
867 f->updated = jiffies;
868 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
870 /* learned new entry */
871 spin_lock(&vxlan->hash_lock);
873 /* close off race between vxlan_flush and incoming packets */
874 if (netif_running(dev))
875 vxlan_fdb_create(vxlan, src_mac, src_ip,
877 NLM_F_EXCL|NLM_F_CREATE,
879 vxlan->default_dst.remote_vni,
881 spin_unlock(&vxlan->hash_lock);
887 /* See if multicast group is already in use by other ID */
888 static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
890 struct vxlan_dev *vxlan;
892 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
893 if (!netif_running(vxlan->dev))
896 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
904 static void vxlan_sock_hold(struct vxlan_sock *vs)
906 atomic_inc(&vs->refcnt);
909 void vxlan_sock_release(struct vxlan_sock *vs)
911 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
913 if (!atomic_dec_and_test(&vs->refcnt))
916 spin_lock(&vn->sock_lock);
917 hlist_del_rcu(&vs->hlist);
918 spin_unlock(&vn->sock_lock);
920 queue_work(vxlan_wq, &vs->del_work);
922 EXPORT_SYMBOL_GPL(vxlan_sock_release);
924 /* Callback to update multicast group membership when first VNI on
925 * multicast asddress is brought up
926 * Done as workqueue because ip_mc_join_group acquires RTNL.
928 static void vxlan_igmp_join(struct work_struct *work)
930 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
931 struct vxlan_sock *vs = vxlan->vn_sock;
932 struct sock *sk = vs->sock->sk;
933 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
934 int ifindex = vxlan->default_dst.remote_ifindex;
937 if (ip->sa.sa_family == AF_INET) {
938 struct ip_mreqn mreq = {
939 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
940 .imr_ifindex = ifindex,
943 ip_mc_join_group(sk, &mreq);
944 #if IS_ENABLED(CONFIG_IPV6)
946 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
947 &ip->sin6.sin6_addr);
952 vxlan_sock_release(vs);
956 /* Inverse of vxlan_igmp_join when last VNI is brought down */
957 static void vxlan_igmp_leave(struct work_struct *work)
959 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
960 struct vxlan_sock *vs = vxlan->vn_sock;
961 struct sock *sk = vs->sock->sk;
962 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
963 int ifindex = vxlan->default_dst.remote_ifindex;
966 if (ip->sa.sa_family == AF_INET) {
967 struct ip_mreqn mreq = {
968 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
969 .imr_ifindex = ifindex,
972 ip_mc_leave_group(sk, &mreq);
973 #if IS_ENABLED(CONFIG_IPV6)
975 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
976 &ip->sin6.sin6_addr);
982 vxlan_sock_release(vs);
986 /* Callback from net/ipv4/udp.c to receive packets */
987 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
989 struct vxlan_sock *vs;
990 struct vxlanhdr *vxh;
993 /* Need Vxlan and inner Ethernet header to be present */
994 if (!pskb_may_pull(skb, VXLAN_HLEN))
997 /* Return packets with reserved bits set */
998 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
999 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1000 (vxh->vx_vni & htonl(0xff))) {
1001 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1002 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1006 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1009 port = inet_sk(sk)->inet_sport;
1011 vs = vxlan_find_sock(sock_net(sk), port);
1015 vs->rcv(vs, skb, vxh->vx_vni);
1019 /* Consume bad packet */
1024 /* Return non vxlan pkt */
1028 static void vxlan_rcv(struct vxlan_sock *vs,
1029 struct sk_buff *skb, __be32 vx_vni)
1031 struct iphdr *oip = NULL;
1032 struct ipv6hdr *oip6 = NULL;
1033 struct vxlan_dev *vxlan;
1034 struct pcpu_tstats *stats;
1035 union vxlan_addr saddr;
1038 union vxlan_addr *remote_ip;
1040 vni = ntohl(vx_vni) >> 8;
1041 /* Is this VNI defined? */
1042 vxlan = vxlan_vs_find_vni(vs, vni);
1046 remote_ip = &vxlan->default_dst.remote_ip;
1047 skb_reset_mac_header(skb);
1048 skb->protocol = eth_type_trans(skb, vxlan->dev);
1050 /* Ignore packet loops (and multicast echo) */
1051 if (compare_ether_addr(eth_hdr(skb)->h_source,
1052 vxlan->dev->dev_addr) == 0)
1055 /* Re-examine inner Ethernet packet */
1056 if (remote_ip->sa.sa_family == AF_INET) {
1058 saddr.sin.sin_addr.s_addr = oip->saddr;
1059 saddr.sa.sa_family = AF_INET;
1060 #if IS_ENABLED(CONFIG_IPV6)
1062 oip6 = ipv6_hdr(skb);
1063 saddr.sin6.sin6_addr = oip6->saddr;
1064 saddr.sa.sa_family = AF_INET6;
1068 if ((vxlan->flags & VXLAN_F_LEARN) &&
1069 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1072 skb_reset_network_header(skb);
1074 /* If the NIC driver gave us an encapsulated packet with
1075 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1076 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1077 * for us. Otherwise force the upper layers to verify it.
1079 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1080 !(vxlan->dev->features & NETIF_F_RXCSUM))
1081 skb->ip_summed = CHECKSUM_NONE;
1083 skb->encapsulation = 0;
1086 err = IP6_ECN_decapsulate(oip6, skb);
1088 err = IP_ECN_decapsulate(oip, skb);
1090 if (unlikely(err)) {
1091 if (log_ecn_error) {
1093 net_info_ratelimited("non-ECT from %pI6\n",
1096 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1097 &oip->saddr, oip->tos);
1100 ++vxlan->dev->stats.rx_frame_errors;
1101 ++vxlan->dev->stats.rx_errors;
1106 stats = this_cpu_ptr(vxlan->dev->tstats);
1107 u64_stats_update_begin(&stats->syncp);
1108 stats->rx_packets++;
1109 stats->rx_bytes += skb->len;
1110 u64_stats_update_end(&stats->syncp);
1116 /* Consume bad packet */
1120 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1122 struct vxlan_dev *vxlan = netdev_priv(dev);
1123 struct arphdr *parp;
1126 struct neighbour *n;
1128 if (dev->flags & IFF_NOARP)
1131 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1132 dev->stats.tx_dropped++;
1135 parp = arp_hdr(skb);
1137 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1138 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1139 parp->ar_pro != htons(ETH_P_IP) ||
1140 parp->ar_op != htons(ARPOP_REQUEST) ||
1141 parp->ar_hln != dev->addr_len ||
1144 arpptr = (u8 *)parp + sizeof(struct arphdr);
1146 arpptr += dev->addr_len; /* sha */
1147 memcpy(&sip, arpptr, sizeof(sip));
1148 arpptr += sizeof(sip);
1149 arpptr += dev->addr_len; /* tha */
1150 memcpy(&tip, arpptr, sizeof(tip));
1152 if (ipv4_is_loopback(tip) ||
1153 ipv4_is_multicast(tip))
1156 n = neigh_lookup(&arp_tbl, &tip, dev);
1159 struct vxlan_fdb *f;
1160 struct sk_buff *reply;
1162 if (!(n->nud_state & NUD_CONNECTED)) {
1167 f = vxlan_find_mac(vxlan, n->ha);
1168 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1169 /* bridge-local neighbor */
1174 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1179 skb_reset_mac_header(reply);
1180 __skb_pull(reply, skb_network_offset(reply));
1181 reply->ip_summed = CHECKSUM_UNNECESSARY;
1182 reply->pkt_type = PACKET_HOST;
1184 if (netif_rx_ni(reply) == NET_RX_DROP)
1185 dev->stats.rx_dropped++;
1186 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1187 union vxlan_addr ipa = {
1188 .sin.sin_addr.s_addr = tip,
1189 .sa.sa_family = AF_INET,
1192 vxlan_ip_miss(dev, &ipa);
1196 return NETDEV_TX_OK;
1199 #if IS_ENABLED(CONFIG_IPV6)
1200 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1202 struct vxlan_dev *vxlan = netdev_priv(dev);
1203 struct neighbour *n;
1204 union vxlan_addr ipa;
1205 const struct ipv6hdr *iphdr;
1206 const struct in6_addr *saddr, *daddr;
1208 struct inet6_dev *in6_dev = NULL;
1210 in6_dev = __in6_dev_get(dev);
1214 if (!pskb_may_pull(skb, skb->len))
1217 iphdr = ipv6_hdr(skb);
1218 saddr = &iphdr->saddr;
1219 daddr = &iphdr->daddr;
1221 if (ipv6_addr_loopback(daddr) ||
1222 ipv6_addr_is_multicast(daddr))
1225 msg = (struct nd_msg *)skb_transport_header(skb);
1226 if (msg->icmph.icmp6_code != 0 ||
1227 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1230 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
1233 struct vxlan_fdb *f;
1235 if (!(n->nud_state & NUD_CONNECTED)) {
1240 f = vxlan_find_mac(vxlan, n->ha);
1241 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1242 /* bridge-local neighbor */
1247 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
1248 !!in6_dev->cnf.forwarding,
1249 true, false, false);
1251 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1252 ipa.sin6.sin6_addr = *daddr;
1253 ipa.sa.sa_family = AF_INET6;
1254 vxlan_ip_miss(dev, &ipa);
1259 return NETDEV_TX_OK;
1263 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1265 struct vxlan_dev *vxlan = netdev_priv(dev);
1266 struct neighbour *n;
1268 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1272 switch (ntohs(eth_hdr(skb)->h_proto)) {
1277 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1280 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1281 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1282 union vxlan_addr ipa = {
1283 .sin.sin_addr.s_addr = pip->daddr,
1284 .sa.sa_family = AF_INET,
1287 vxlan_ip_miss(dev, &ipa);
1293 #if IS_ENABLED(CONFIG_IPV6)
1296 struct ipv6hdr *pip6;
1298 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1300 pip6 = ipv6_hdr(skb);
1301 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1302 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1303 union vxlan_addr ipa = {
1304 .sin6.sin6_addr = pip6->daddr,
1305 .sa.sa_family = AF_INET6,
1308 vxlan_ip_miss(dev, &ipa);
1322 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1324 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1326 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1335 static void vxlan_sock_put(struct sk_buff *skb)
1340 /* On transmit, associate with the tunnel socket */
1341 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
1346 skb->destructor = vxlan_sock_put;
1349 /* Compute source port for outgoing packet
1350 * first choice to use L4 flow hash since it will spread
1351 * better and maybe available from hardware
1352 * secondary choice is to use jhash on the Ethernet header
1354 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
1356 unsigned int range = (port_max - port_min) + 1;
1359 hash = skb_get_rxhash(skb);
1361 hash = jhash(skb->data, 2 * ETH_ALEN,
1362 (__force u32) skb->protocol);
1364 return htons((((u64) hash * range) >> 32) + port_min);
1366 EXPORT_SYMBOL_GPL(vxlan_src_port);
1368 static int handle_offloads(struct sk_buff *skb)
1370 if (skb_is_gso(skb)) {
1371 int err = skb_unclone(skb, GFP_ATOMIC);
1375 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1376 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1377 skb->ip_summed = CHECKSUM_NONE;
1382 #if IS_ENABLED(CONFIG_IPV6)
1383 static int vxlan6_xmit_skb(struct net *net, struct vxlan_sock *vs,
1384 struct dst_entry *dst, struct sk_buff *skb,
1385 struct net_device *dev, struct in6_addr *saddr,
1386 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1387 __be16 src_port, __be16 dst_port, __be32 vni)
1389 struct ipv6hdr *ip6h;
1390 struct vxlanhdr *vxh;
1395 if (!skb->encapsulation) {
1396 skb_reset_inner_headers(skb);
1397 skb->encapsulation = 1;
1400 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1401 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1402 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1404 /* Need space for new headers (invalidates iph ptr) */
1405 err = skb_cow_head(skb, min_headroom);
1409 if (vlan_tx_tag_present(skb)) {
1410 if (WARN_ON(!__vlan_put_tag(skb,
1412 vlan_tx_tag_get(skb))))
1418 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1419 vxh->vx_flags = htonl(VXLAN_FLAGS);
1422 __skb_push(skb, sizeof(*uh));
1423 skb_reset_transport_header(skb);
1426 uh->dest = dst_port;
1427 uh->source = src_port;
1429 uh->len = htons(skb->len);
1432 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1433 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1436 skb_dst_set(skb, dst);
1438 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1439 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1440 skb->ip_summed = CHECKSUM_UNNECESSARY;
1441 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1444 uh->check = CSUM_MANGLED_0;
1446 skb->ip_summed = CHECKSUM_PARTIAL;
1447 skb->csum_start = skb_transport_header(skb) - skb->head;
1448 skb->csum_offset = offsetof(struct udphdr, check);
1449 uh->check = ~csum_ipv6_magic(saddr, daddr,
1450 skb->len, IPPROTO_UDP, 0);
1453 __skb_push(skb, sizeof(*ip6h));
1454 skb_reset_network_header(skb);
1455 ip6h = ipv6_hdr(skb);
1457 ip6h->priority = prio;
1458 ip6h->flow_lbl[0] = 0;
1459 ip6h->flow_lbl[1] = 0;
1460 ip6h->flow_lbl[2] = 0;
1461 ip6h->payload_len = htons(skb->len);
1462 ip6h->nexthdr = IPPROTO_UDP;
1463 ip6h->hop_limit = ttl;
1464 ip6h->daddr = *daddr;
1465 ip6h->saddr = *saddr;
1467 vxlan_set_owner(vs->sock->sk, skb);
1469 err = handle_offloads(skb);
1473 ip6tunnel_xmit(skb, dev);
1478 int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
1479 struct rtable *rt, struct sk_buff *skb,
1480 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1481 __be16 src_port, __be16 dst_port, __be32 vni)
1483 struct vxlanhdr *vxh;
1488 if (!skb->encapsulation) {
1489 skb_reset_inner_headers(skb);
1490 skb->encapsulation = 1;
1493 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1494 + VXLAN_HLEN + sizeof(struct iphdr)
1495 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1497 /* Need space for new headers (invalidates iph ptr) */
1498 err = skb_cow_head(skb, min_headroom);
1502 if (vlan_tx_tag_present(skb)) {
1503 if (WARN_ON(!__vlan_put_tag(skb,
1505 vlan_tx_tag_get(skb))))
1511 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1512 vxh->vx_flags = htonl(VXLAN_FLAGS);
1515 __skb_push(skb, sizeof(*uh));
1516 skb_reset_transport_header(skb);
1519 uh->dest = dst_port;
1520 uh->source = src_port;
1522 uh->len = htons(skb->len);
1525 vxlan_set_owner(vs->sock->sk, skb);
1527 err = handle_offloads(skb);
1531 return iptunnel_xmit(net, rt, skb, src, dst,
1532 IPPROTO_UDP, tos, ttl, df);
1534 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1536 /* Bypass encapsulation if the destination is local */
1537 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1538 struct vxlan_dev *dst_vxlan)
1540 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1541 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1542 union vxlan_addr loopback;
1543 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1545 skb->pkt_type = PACKET_HOST;
1546 skb->encapsulation = 0;
1547 skb->dev = dst_vxlan->dev;
1548 __skb_pull(skb, skb_network_offset(skb));
1550 if (remote_ip->sa.sa_family == AF_INET) {
1551 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1552 loopback.sa.sa_family = AF_INET;
1553 #if IS_ENABLED(CONFIG_IPV6)
1555 loopback.sin6.sin6_addr = in6addr_loopback;
1556 loopback.sa.sa_family = AF_INET6;
1560 if (dst_vxlan->flags & VXLAN_F_LEARN)
1561 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1563 u64_stats_update_begin(&tx_stats->syncp);
1564 tx_stats->tx_packets++;
1565 tx_stats->tx_bytes += skb->len;
1566 u64_stats_update_end(&tx_stats->syncp);
1568 if (netif_rx(skb) == NET_RX_SUCCESS) {
1569 u64_stats_update_begin(&rx_stats->syncp);
1570 rx_stats->rx_packets++;
1571 rx_stats->rx_bytes += skb->len;
1572 u64_stats_update_end(&rx_stats->syncp);
1574 skb->dev->stats.rx_dropped++;
1578 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1579 struct vxlan_rdst *rdst, bool did_rsc)
1581 struct vxlan_dev *vxlan = netdev_priv(dev);
1582 struct rtable *rt = NULL;
1583 const struct iphdr *old_iph;
1585 union vxlan_addr *dst;
1586 __be16 src_port = 0, dst_port;
1592 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1593 vni = rdst->remote_vni;
1594 dst = &rdst->remote_ip;
1596 if (vxlan_addr_any(dst)) {
1598 /* short-circuited back to local bridge */
1599 vxlan_encap_bypass(skb, vxlan, vxlan);
1605 old_iph = ip_hdr(skb);
1608 if (!ttl && vxlan_addr_multicast(dst))
1613 tos = ip_tunnel_get_dsfield(old_iph, skb);
1615 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
1617 if (dst->sa.sa_family == AF_INET) {
1618 memset(&fl4, 0, sizeof(fl4));
1619 fl4.flowi4_oif = rdst->remote_ifindex;
1620 fl4.flowi4_tos = RT_TOS(tos);
1621 fl4.daddr = dst->sin.sin_addr.s_addr;
1622 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1624 rt = ip_route_output_key(dev_net(dev), &fl4);
1626 netdev_dbg(dev, "no route to %pI4\n",
1627 &dst->sin.sin_addr.s_addr);
1628 dev->stats.tx_carrier_errors++;
1632 if (rt->dst.dev == dev) {
1633 netdev_dbg(dev, "circular route to %pI4\n",
1634 &dst->sin.sin_addr.s_addr);
1635 dev->stats.collisions++;
1639 /* Bypass encapsulation if the destination is local */
1640 if (rt->rt_flags & RTCF_LOCAL &&
1641 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1642 struct vxlan_dev *dst_vxlan;
1645 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1648 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1652 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1653 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1655 err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1656 fl4.saddr, dst->sin.sin_addr.s_addr,
1657 tos, ttl, df, src_port, dst_port,
1662 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1663 #if IS_ENABLED(CONFIG_IPV6)
1665 struct sock *sk = vxlan->vn_sock->sock->sk;
1666 struct dst_entry *ndst;
1670 memset(&fl6, 0, sizeof(fl6));
1671 fl6.flowi6_oif = rdst->remote_ifindex;
1672 fl6.daddr = dst->sin6.sin6_addr;
1673 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1674 fl6.flowi6_proto = IPPROTO_UDP;
1676 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1677 netdev_dbg(dev, "no route to %pI6\n",
1678 &dst->sin6.sin6_addr);
1679 dev->stats.tx_carrier_errors++;
1683 if (ndst->dev == dev) {
1684 netdev_dbg(dev, "circular route to %pI6\n",
1685 &dst->sin6.sin6_addr);
1687 dev->stats.collisions++;
1691 /* Bypass encapsulation if the destination is local */
1692 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1693 if (flags & RTF_LOCAL &&
1694 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1695 struct vxlan_dev *dst_vxlan;
1698 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1701 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1705 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1707 err = vxlan6_xmit_skb(dev_net(dev), vxlan->vn_sock, ndst, skb,
1708 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1709 src_port, dst_port, htonl(vni << 8));
1716 dev->stats.tx_dropped++;
1722 dev->stats.tx_errors++;
1727 /* Transmit local packets over Vxlan
1729 * Outer IP header inherits ECN and DF from inner header.
1730 * Outer UDP destination is the VXLAN assigned port.
1731 * source port is based on hash of flow
1733 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1735 struct vxlan_dev *vxlan = netdev_priv(dev);
1737 bool did_rsc = false;
1738 struct vxlan_rdst *rdst;
1739 struct vxlan_fdb *f;
1741 skb_reset_mac_header(skb);
1744 if ((vxlan->flags & VXLAN_F_PROXY)) {
1745 if (ntohs(eth->h_proto) == ETH_P_ARP)
1746 return arp_reduce(dev, skb);
1747 #if IS_ENABLED(CONFIG_IPV6)
1748 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1749 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1750 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1753 msg = (struct nd_msg *)skb_transport_header(skb);
1754 if (msg->icmph.icmp6_code == 0 &&
1755 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1756 return neigh_reduce(dev, skb);
1761 f = vxlan_find_mac(vxlan, eth->h_dest);
1764 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1765 (ntohs(eth->h_proto) == ETH_P_IP ||
1766 ntohs(eth->h_proto) == ETH_P_IPV6)) {
1767 did_rsc = route_shortcircuit(dev, skb);
1769 f = vxlan_find_mac(vxlan, eth->h_dest);
1773 f = vxlan_find_mac(vxlan, all_zeros_mac);
1775 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1776 !is_multicast_ether_addr(eth->h_dest))
1777 vxlan_fdb_miss(vxlan, eth->h_dest);
1779 dev->stats.tx_dropped++;
1781 return NETDEV_TX_OK;
1785 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1786 struct sk_buff *skb1;
1788 skb1 = skb_clone(skb, GFP_ATOMIC);
1790 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1794 return NETDEV_TX_OK;
1797 /* Walk the forwarding table and purge stale entries */
1798 static void vxlan_cleanup(unsigned long arg)
1800 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1801 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1804 if (!netif_running(vxlan->dev))
1807 spin_lock_bh(&vxlan->hash_lock);
1808 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1809 struct hlist_node *p, *n;
1810 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1812 = container_of(p, struct vxlan_fdb, hlist);
1813 unsigned long timeout;
1815 if (f->state & NUD_PERMANENT)
1818 timeout = f->used + vxlan->age_interval * HZ;
1819 if (time_before_eq(timeout, jiffies)) {
1820 netdev_dbg(vxlan->dev,
1821 "garbage collect %pM\n",
1823 f->state = NUD_STALE;
1824 vxlan_fdb_destroy(vxlan, f);
1825 } else if (time_before(timeout, next_timer))
1826 next_timer = timeout;
1829 spin_unlock_bh(&vxlan->hash_lock);
1831 mod_timer(&vxlan->age_timer, next_timer);
1834 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1836 __u32 vni = vxlan->default_dst.remote_vni;
1838 vxlan->vn_sock = vs;
1839 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1842 /* Setup stats when device is created */
1843 static int vxlan_init(struct net_device *dev)
1845 struct vxlan_dev *vxlan = netdev_priv(dev);
1846 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1847 struct vxlan_sock *vs;
1849 dev->tstats = alloc_percpu(struct pcpu_tstats);
1853 spin_lock(&vn->sock_lock);
1854 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1856 /* If we have a socket with same port already, reuse it */
1857 atomic_inc(&vs->refcnt);
1858 vxlan_vs_add_dev(vs, vxlan);
1860 /* otherwise make new socket outside of RTNL */
1862 queue_work(vxlan_wq, &vxlan->sock_work);
1864 spin_unlock(&vn->sock_lock);
1869 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1871 struct vxlan_fdb *f;
1873 spin_lock_bh(&vxlan->hash_lock);
1874 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1876 vxlan_fdb_destroy(vxlan, f);
1877 spin_unlock_bh(&vxlan->hash_lock);
1880 static void vxlan_uninit(struct net_device *dev)
1882 struct vxlan_dev *vxlan = netdev_priv(dev);
1883 struct vxlan_sock *vs = vxlan->vn_sock;
1885 vxlan_fdb_delete_default(vxlan);
1888 vxlan_sock_release(vs);
1889 free_percpu(dev->tstats);
1892 /* Start ageing timer and join group when device is brought up */
1893 static int vxlan_open(struct net_device *dev)
1895 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1896 struct vxlan_dev *vxlan = netdev_priv(dev);
1897 struct vxlan_sock *vs = vxlan->vn_sock;
1899 /* socket hasn't been created */
1903 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1904 vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
1905 vxlan_sock_hold(vs);
1907 queue_work(vxlan_wq, &vxlan->igmp_join);
1910 if (vxlan->age_interval)
1911 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1916 /* Purge the forwarding table */
1917 static void vxlan_flush(struct vxlan_dev *vxlan)
1921 spin_lock_bh(&vxlan->hash_lock);
1922 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1923 struct hlist_node *p, *n;
1924 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1926 = container_of(p, struct vxlan_fdb, hlist);
1927 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1928 if (!is_zero_ether_addr(f->eth_addr))
1929 vxlan_fdb_destroy(vxlan, f);
1932 spin_unlock_bh(&vxlan->hash_lock);
1935 /* Cleanup timer and forwarding table on shutdown */
1936 static int vxlan_stop(struct net_device *dev)
1938 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1939 struct vxlan_dev *vxlan = netdev_priv(dev);
1940 struct vxlan_sock *vs = vxlan->vn_sock;
1942 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1943 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
1944 vxlan_sock_hold(vs);
1946 queue_work(vxlan_wq, &vxlan->igmp_leave);
1949 del_timer_sync(&vxlan->age_timer);
1956 /* Stub, nothing needs to be done. */
1957 static void vxlan_set_multicast_list(struct net_device *dev)
1961 static const struct net_device_ops vxlan_netdev_ops = {
1962 .ndo_init = vxlan_init,
1963 .ndo_uninit = vxlan_uninit,
1964 .ndo_open = vxlan_open,
1965 .ndo_stop = vxlan_stop,
1966 .ndo_start_xmit = vxlan_xmit,
1967 .ndo_get_stats64 = ip_tunnel_get_stats64,
1968 .ndo_set_rx_mode = vxlan_set_multicast_list,
1969 .ndo_change_mtu = eth_change_mtu,
1970 .ndo_validate_addr = eth_validate_addr,
1971 .ndo_set_mac_address = eth_mac_addr,
1972 .ndo_fdb_add = vxlan_fdb_add,
1973 .ndo_fdb_del = vxlan_fdb_delete,
1974 .ndo_fdb_dump = vxlan_fdb_dump,
1977 /* Info for udev, that this is a virtual tunnel endpoint */
1978 static struct device_type vxlan_type = {
1982 /* Initialize the device structure. */
1983 static void vxlan_setup(struct net_device *dev)
1985 struct vxlan_dev *vxlan = netdev_priv(dev);
1989 eth_hw_addr_random(dev);
1991 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
1992 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
1994 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1996 dev->netdev_ops = &vxlan_netdev_ops;
1997 dev->destructor = free_netdev;
1998 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2000 dev->tx_queue_len = 0;
2001 dev->features |= NETIF_F_LLTX;
2002 dev->features |= NETIF_F_NETNS_LOCAL;
2003 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2004 dev->features |= NETIF_F_RXCSUM;
2005 dev->features |= NETIF_F_GSO_SOFTWARE;
2007 dev->vlan_features = dev->features;
2008 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2009 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2010 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2011 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2012 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
2013 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2015 INIT_LIST_HEAD(&vxlan->next);
2016 spin_lock_init(&vxlan->hash_lock);
2017 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2018 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2019 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2021 init_timer_deferrable(&vxlan->age_timer);
2022 vxlan->age_timer.function = vxlan_cleanup;
2023 vxlan->age_timer.data = (unsigned long) vxlan;
2025 inet_get_local_port_range(&low, &high);
2026 vxlan->port_min = low;
2027 vxlan->port_max = high;
2028 vxlan->dst_port = htons(vxlan_port);
2032 for (h = 0; h < FDB_HASH_SIZE; ++h)
2033 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2036 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2037 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2038 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2039 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2040 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2041 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2042 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2043 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2044 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2045 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2046 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2047 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2048 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2049 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2050 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2051 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2052 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2053 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2056 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2058 if (tb[IFLA_ADDRESS]) {
2059 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2060 pr_debug("invalid link address (not ethernet)\n");
2064 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2065 pr_debug("invalid all zero ethernet address\n");
2066 return -EADDRNOTAVAIL;
2073 if (data[IFLA_VXLAN_ID]) {
2074 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2075 if (id >= VXLAN_VID_MASK)
2079 if (data[IFLA_VXLAN_PORT_RANGE]) {
2080 const struct ifla_vxlan_port_range *p
2081 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2083 if (ntohs(p->high) < ntohs(p->low)) {
2084 pr_debug("port range %u .. %u not valid\n",
2085 ntohs(p->low), ntohs(p->high));
2093 static void vxlan_get_drvinfo(struct net_device *netdev,
2094 struct ethtool_drvinfo *drvinfo)
2096 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2097 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2100 static const struct ethtool_ops vxlan_ethtool_ops = {
2101 .get_drvinfo = vxlan_get_drvinfo,
2102 .get_link = ethtool_op_get_link,
2105 static void vxlan_del_work(struct work_struct *work)
2107 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2109 sk_release_kernel(vs->sock->sk);
2113 #if IS_ENABLED(CONFIG_IPV6)
2114 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2115 * could be used for both IPv4 and IPv6 communications, but
2116 * users may set bindv6only=1.
2118 static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2121 struct socket *sock;
2122 struct sockaddr_in6 vxlan_addr = {
2123 .sin6_family = AF_INET6,
2128 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2130 pr_debug("UDPv6 socket create failed\n");
2134 /* Put in proper namespace */
2136 sk_change_net(sk, net);
2138 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2139 (char *)&val, sizeof(val));
2140 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2141 sizeof(struct sockaddr_in6));
2143 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2144 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2145 sk_release_kernel(sk);
2148 /* At this point, IPv6 module should have been loaded in
2149 * sock_create_kern().
2154 /* Disable multicast loopback */
2155 inet_sk(sk)->mc_loop = 0;
2161 static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2163 return -EPFNOSUPPORT;
2167 static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
2170 struct socket *sock;
2171 struct sockaddr_in vxlan_addr = {
2172 .sin_family = AF_INET,
2173 .sin_addr.s_addr = htonl(INADDR_ANY),
2178 /* Create UDP socket for encapsulation receive. */
2179 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2181 pr_debug("UDP socket create failed\n");
2185 /* Put in proper namespace */
2187 sk_change_net(sk, net);
2189 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2190 sizeof(vxlan_addr));
2192 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2193 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2194 sk_release_kernel(sk);
2199 /* Disable multicast loopback */
2200 inet_sk(sk)->mc_loop = 0;
2204 /* Create new listen socket if needed */
2205 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2206 vxlan_rcv_t *rcv, void *data, bool ipv6)
2208 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2209 struct vxlan_sock *vs;
2210 struct socket *sock;
2215 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
2217 return ERR_PTR(-ENOMEM);
2219 for (h = 0; h < VNI_HASH_SIZE; ++h)
2220 INIT_HLIST_HEAD(&vs->vni_list[h]);
2222 INIT_WORK(&vs->del_work, vxlan_del_work);
2225 rc = create_v6_sock(net, port, &sock);
2227 rc = create_v4_sock(net, port, &sock);
2235 atomic_set(&vs->refcnt, 1);
2239 spin_lock(&vn->sock_lock);
2240 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2241 spin_unlock(&vn->sock_lock);
2243 /* Mark socket as an encapsulation socket. */
2244 udp_sk(sk)->encap_type = 1;
2245 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
2246 #if IS_ENABLED(CONFIG_IPV6)
2248 ipv6_stub->udpv6_encap_enable();
2256 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2257 vxlan_rcv_t *rcv, void *data,
2258 bool no_share, bool ipv6)
2260 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2261 struct vxlan_sock *vs;
2263 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
2267 if (no_share) /* Return error if sharing is not allowed. */
2270 spin_lock(&vn->sock_lock);
2271 vs = vxlan_find_sock(net, port);
2274 atomic_inc(&vs->refcnt);
2276 vs = ERR_PTR(-EBUSY);
2278 spin_unlock(&vn->sock_lock);
2281 vs = ERR_PTR(-EINVAL);
2285 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2287 /* Scheduled at device creation to bind to a socket */
2288 static void vxlan_sock_work(struct work_struct *work)
2290 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2291 struct net *net = dev_net(vxlan->dev);
2292 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2293 __be16 port = vxlan->dst_port;
2294 struct vxlan_sock *nvs;
2296 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
2297 spin_lock(&vn->sock_lock);
2299 vxlan_vs_add_dev(nvs, vxlan);
2300 spin_unlock(&vn->sock_lock);
2302 dev_put(vxlan->dev);
2305 static int vxlan_newlink(struct net *net, struct net_device *dev,
2306 struct nlattr *tb[], struct nlattr *data[])
2308 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2309 struct vxlan_dev *vxlan = netdev_priv(dev);
2310 struct vxlan_rdst *dst = &vxlan->default_dst;
2313 bool use_ipv6 = false;
2315 if (!data[IFLA_VXLAN_ID])
2318 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2319 dst->remote_vni = vni;
2321 if (data[IFLA_VXLAN_GROUP]) {
2322 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2323 dst->remote_ip.sa.sa_family = AF_INET;
2324 } else if (data[IFLA_VXLAN_GROUP6]) {
2325 if (!IS_ENABLED(CONFIG_IPV6))
2326 return -EPFNOSUPPORT;
2328 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2329 sizeof(struct in6_addr));
2330 dst->remote_ip.sa.sa_family = AF_INET6;
2334 if (data[IFLA_VXLAN_LOCAL]) {
2335 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2336 vxlan->saddr.sa.sa_family = AF_INET;
2337 } else if (data[IFLA_VXLAN_LOCAL6]) {
2338 if (!IS_ENABLED(CONFIG_IPV6))
2339 return -EPFNOSUPPORT;
2341 /* TODO: respect scope id */
2342 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2343 sizeof(struct in6_addr));
2344 vxlan->saddr.sa.sa_family = AF_INET6;
2348 if (data[IFLA_VXLAN_LINK] &&
2349 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2350 struct net_device *lowerdev
2351 = __dev_get_by_index(net, dst->remote_ifindex);
2354 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2358 #if IS_ENABLED(CONFIG_IPV6)
2360 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2361 if (idev && idev->cnf.disable_ipv6) {
2362 pr_info("IPv6 is disabled via sysctl\n");
2365 vxlan->flags |= VXLAN_F_IPV6;
2370 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2372 /* update header length based on lower device */
2373 dev->hard_header_len = lowerdev->hard_header_len +
2374 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2377 if (data[IFLA_VXLAN_TOS])
2378 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2380 if (data[IFLA_VXLAN_TTL])
2381 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2383 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2384 vxlan->flags |= VXLAN_F_LEARN;
2386 if (data[IFLA_VXLAN_AGEING])
2387 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2389 vxlan->age_interval = FDB_AGE_DEFAULT;
2391 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2392 vxlan->flags |= VXLAN_F_PROXY;
2394 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2395 vxlan->flags |= VXLAN_F_RSC;
2397 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2398 vxlan->flags |= VXLAN_F_L2MISS;
2400 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2401 vxlan->flags |= VXLAN_F_L3MISS;
2403 if (data[IFLA_VXLAN_LIMIT])
2404 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2406 if (data[IFLA_VXLAN_PORT_RANGE]) {
2407 const struct ifla_vxlan_port_range *p
2408 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2409 vxlan->port_min = ntohs(p->low);
2410 vxlan->port_max = ntohs(p->high);
2413 if (data[IFLA_VXLAN_PORT])
2414 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2416 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2417 pr_info("duplicate VNI %u\n", vni);
2421 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2423 /* create an fdb entry for default destination */
2424 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2425 &vxlan->default_dst.remote_ip,
2426 NUD_REACHABLE|NUD_PERMANENT,
2427 NLM_F_EXCL|NLM_F_CREATE,
2428 vxlan->dst_port, vxlan->default_dst.remote_vni,
2429 vxlan->default_dst.remote_ifindex, NTF_SELF);
2433 err = register_netdevice(dev);
2435 vxlan_fdb_delete_default(vxlan);
2439 list_add(&vxlan->next, &vn->vxlan_list);
2444 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2446 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2447 struct vxlan_dev *vxlan = netdev_priv(dev);
2449 spin_lock(&vn->sock_lock);
2450 if (!hlist_unhashed(&vxlan->hlist))
2451 hlist_del_rcu(&vxlan->hlist);
2452 spin_unlock(&vn->sock_lock);
2454 list_del(&vxlan->next);
2455 unregister_netdevice_queue(dev, head);
2458 static size_t vxlan_get_size(const struct net_device *dev)
2461 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2462 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2463 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2464 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2465 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2466 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2467 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2468 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2469 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2470 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2471 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2472 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2473 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2474 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2475 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
2479 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2481 const struct vxlan_dev *vxlan = netdev_priv(dev);
2482 const struct vxlan_rdst *dst = &vxlan->default_dst;
2483 struct ifla_vxlan_port_range ports = {
2484 .low = htons(vxlan->port_min),
2485 .high = htons(vxlan->port_max),
2488 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2489 goto nla_put_failure;
2491 if (!vxlan_addr_any(&dst->remote_ip)) {
2492 if (dst->remote_ip.sa.sa_family == AF_INET) {
2493 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2494 dst->remote_ip.sin.sin_addr.s_addr))
2495 goto nla_put_failure;
2496 #if IS_ENABLED(CONFIG_IPV6)
2498 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2499 &dst->remote_ip.sin6.sin6_addr))
2500 goto nla_put_failure;
2505 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2506 goto nla_put_failure;
2508 if (!vxlan_addr_any(&vxlan->saddr)) {
2509 if (vxlan->saddr.sa.sa_family == AF_INET) {
2510 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2511 vxlan->saddr.sin.sin_addr.s_addr))
2512 goto nla_put_failure;
2513 #if IS_ENABLED(CONFIG_IPV6)
2515 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2516 &vxlan->saddr.sin6.sin6_addr))
2517 goto nla_put_failure;
2522 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2523 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2524 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2525 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2526 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2527 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2528 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2529 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2530 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2531 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2532 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2533 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2534 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2535 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
2536 goto nla_put_failure;
2538 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2539 goto nla_put_failure;
2547 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2549 .maxtype = IFLA_VXLAN_MAX,
2550 .policy = vxlan_policy,
2551 .priv_size = sizeof(struct vxlan_dev),
2552 .setup = vxlan_setup,
2553 .validate = vxlan_validate,
2554 .newlink = vxlan_newlink,
2555 .dellink = vxlan_dellink,
2556 .get_size = vxlan_get_size,
2557 .fill_info = vxlan_fill_info,
2560 static __net_init int vxlan_init_net(struct net *net)
2562 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2565 INIT_LIST_HEAD(&vn->vxlan_list);
2566 spin_lock_init(&vn->sock_lock);
2568 for (h = 0; h < PORT_HASH_SIZE; ++h)
2569 INIT_HLIST_HEAD(&vn->sock_list[h]);
2574 static __net_exit void vxlan_exit_net(struct net *net)
2576 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2577 struct vxlan_dev *vxlan;
2581 list_for_each_entry(vxlan, &vn->vxlan_list, next)
2582 unregister_netdevice_queue(vxlan->dev, &list);
2583 unregister_netdevice_many(&list);
2587 static struct pernet_operations vxlan_net_ops = {
2588 .init = vxlan_init_net,
2589 .exit = vxlan_exit_net,
2590 .id = &vxlan_net_id,
2591 .size = sizeof(struct vxlan_net),
2594 static int __init vxlan_init_module(void)
2598 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2602 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2604 rc = register_pernet_device(&vxlan_net_ops);
2608 rc = rtnl_link_register(&vxlan_link_ops);
2615 unregister_pernet_device(&vxlan_net_ops);
2617 destroy_workqueue(vxlan_wq);
2620 late_initcall(vxlan_init_module);
2622 static void __exit vxlan_cleanup_module(void)
2624 rtnl_link_unregister(&vxlan_link_ops);
2625 destroy_workqueue(vxlan_wq);
2626 unregister_pernet_device(&vxlan_net_ops);
2629 module_exit(vxlan_cleanup_module);
2631 MODULE_LICENSE("GPL");
2632 MODULE_VERSION(VXLAN_VERSION);
2633 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2634 MODULE_ALIAS_RTNL_LINK("vxlan");