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/udp_tunnel.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/vxlan.h>
44 #include <net/protocol.h>
45 #include <net/udp_tunnel.h>
46 #if IS_ENABLED(CONFIG_IPV6)
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
53 #define VXLAN_VERSION "0.1"
55 #define PORT_HASH_BITS 8
56 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
57 #define VNI_HASH_BITS 10
58 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59 #define FDB_HASH_BITS 8
60 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61 #define FDB_AGE_DEFAULT 300 /* 5 min */
62 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
64 /* UDP port for VXLAN traffic.
65 * The IANA assigned port is 4789, but the Linux default is 8472
66 * for compatibility with early adopters.
68 static unsigned short vxlan_port __read_mostly = 8472;
69 module_param_named(udp_port, vxlan_port, ushort, 0444);
70 MODULE_PARM_DESC(udp_port, "Destination UDP port");
72 static bool log_ecn_error = true;
73 module_param(log_ecn_error, bool, 0644);
74 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
76 static int vxlan_net_id;
78 static const u8 all_zeros_mac[ETH_ALEN];
80 /* per-network namespace private data for this module */
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
88 struct sockaddr_in sin;
89 struct sockaddr_in6 sin6;
94 union vxlan_addr remote_ip;
98 struct list_head list;
102 /* Forwarding table entry */
104 struct hlist_node hlist; /* linked list of entries */
106 unsigned long updated; /* jiffies */
108 struct list_head remotes;
109 u16 state; /* see ndm_state */
110 u8 flags; /* see ndm_flags */
111 u8 eth_addr[ETH_ALEN];
114 /* Pseudo network device */
116 struct hlist_node hlist; /* vni hash table */
117 struct list_head next; /* vxlan's per namespace list */
118 struct vxlan_sock *vn_sock; /* listening socket */
119 struct net_device *dev;
120 struct net *net; /* netns for packet i/o */
121 struct vxlan_rdst default_dst; /* default destination */
122 union vxlan_addr saddr; /* source address */
124 __u16 port_min; /* source port range */
126 __u8 tos; /* TOS override */
128 u32 flags; /* VXLAN_F_* in vxlan.h */
130 struct work_struct sock_work;
131 struct work_struct igmp_join;
132 struct work_struct igmp_leave;
134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
143 /* salt for hash table */
144 static u32 vxlan_salt __read_mostly;
145 static struct workqueue_struct *vxlan_wq;
147 static void vxlan_sock_work(struct work_struct *work);
149 #if IS_ENABLED(CONFIG_IPV6)
151 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
153 if (a->sa.sa_family != b->sa.sa_family)
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
161 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
169 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
177 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
181 ip->sa.sa_family = AF_INET6;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
184 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
185 ip->sa.sa_family = AF_INET;
188 return -EAFNOSUPPORT;
192 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
193 const union vxlan_addr *ip)
195 if (ip->sa.sa_family == AF_INET6)
196 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
198 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
201 #else /* !CONFIG_IPV6 */
204 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
209 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
214 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
219 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
224 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
225 ip->sa.sa_family = AF_INET;
228 return -EAFNOSUPPORT;
232 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
233 const union vxlan_addr *ip)
235 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
239 /* Virtual Network hash table head */
240 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
245 /* Socket hash table head */
246 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
253 /* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
256 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
261 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
266 /* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
269 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
272 struct vxlan_sock *vs;
273 u32 match_flags = flags & VXLAN_F_UNSHAREABLE;
275 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
276 if (inet_sk(vs->sock->sk)->inet_sport == port &&
277 inet_sk(vs->sock->sk)->sk.sk_family == family &&
278 (vs->flags & VXLAN_F_UNSHAREABLE) == match_flags)
284 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
286 struct vxlan_dev *vxlan;
288 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
289 if (vxlan->default_dst.remote_vni == id)
296 /* Look up VNI in a per net namespace table */
297 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
298 sa_family_t family, __be16 port,
301 struct vxlan_sock *vs;
303 vs = vxlan_find_sock(net, family, port, flags);
307 return vxlan_vs_find_vni(vs, id);
310 /* Fill in neighbour message in skbuff. */
311 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
312 const struct vxlan_fdb *fdb,
313 u32 portid, u32 seq, int type, unsigned int flags,
314 const struct vxlan_rdst *rdst)
316 unsigned long now = jiffies;
317 struct nda_cacheinfo ci;
318 struct nlmsghdr *nlh;
320 bool send_ip, send_eth;
322 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
326 ndm = nlmsg_data(nlh);
327 memset(ndm, 0, sizeof(*ndm));
329 send_eth = send_ip = true;
331 if (type == RTM_GETNEIGH) {
332 ndm->ndm_family = AF_INET;
333 send_ip = !vxlan_addr_any(&rdst->remote_ip);
334 send_eth = !is_zero_ether_addr(fdb->eth_addr);
336 ndm->ndm_family = AF_BRIDGE;
337 ndm->ndm_state = fdb->state;
338 ndm->ndm_ifindex = vxlan->dev->ifindex;
339 ndm->ndm_flags = fdb->flags;
340 ndm->ndm_type = RTN_UNICAST;
342 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
343 nla_put_s32(skb, NDA_NDM_IFINDEX_NETNSID,
344 peernet2id(vxlan->net, dev_net(vxlan->dev))))
345 goto nla_put_failure;
347 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
348 goto nla_put_failure;
350 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
351 goto nla_put_failure;
353 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
354 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
355 goto nla_put_failure;
356 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
357 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
358 goto nla_put_failure;
359 if (rdst->remote_ifindex &&
360 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
361 goto nla_put_failure;
363 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
364 ci.ndm_confirmed = 0;
365 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
369 goto nla_put_failure;
375 nlmsg_cancel(skb, nlh);
379 static inline size_t vxlan_nlmsg_size(void)
381 return NLMSG_ALIGN(sizeof(struct ndmsg))
382 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
383 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
384 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
385 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
386 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
387 + nla_total_size(sizeof(struct nda_cacheinfo));
390 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
391 struct vxlan_rdst *rd, int type)
393 struct net *net = dev_net(vxlan->dev);
397 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
401 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
403 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
404 WARN_ON(err == -EMSGSIZE);
409 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
413 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
418 struct vxlan_dev *vxlan = netdev_priv(dev);
419 struct vxlan_fdb f = {
422 struct vxlan_rdst remote = {
423 .remote_ip = *ipa, /* goes to NDA_DST */
424 .remote_vni = VXLAN_N_VID,
427 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
430 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
432 struct vxlan_fdb f = {
435 struct vxlan_rdst remote = { };
437 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
439 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
442 /* Hash Ethernet address */
443 static u32 eth_hash(const unsigned char *addr)
445 u64 value = get_unaligned((u64 *)addr);
447 /* only want 6 bytes */
453 return hash_64(value, FDB_HASH_BITS);
456 /* Hash chain to use given mac address */
457 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
460 return &vxlan->fdb_head[eth_hash(mac)];
463 /* Look up Ethernet address in forwarding table */
464 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
467 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
470 hlist_for_each_entry_rcu(f, head, hlist) {
471 if (ether_addr_equal(mac, f->eth_addr))
478 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
483 f = __vxlan_find_mac(vxlan, mac);
490 /* caller should hold vxlan->hash_lock */
491 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
492 union vxlan_addr *ip, __be16 port,
493 __u32 vni, __u32 ifindex)
495 struct vxlan_rdst *rd;
497 list_for_each_entry(rd, &f->remotes, list) {
498 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
499 rd->remote_port == port &&
500 rd->remote_vni == vni &&
501 rd->remote_ifindex == ifindex)
508 /* Replace destination of unicast mac */
509 static int vxlan_fdb_replace(struct vxlan_fdb *f,
510 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
512 struct vxlan_rdst *rd;
514 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
518 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
522 rd->remote_port = port;
523 rd->remote_vni = vni;
524 rd->remote_ifindex = ifindex;
528 /* Add/update destinations for multicast */
529 static int vxlan_fdb_append(struct vxlan_fdb *f,
530 union vxlan_addr *ip, __be16 port, __u32 vni,
531 __u32 ifindex, struct vxlan_rdst **rdp)
533 struct vxlan_rdst *rd;
535 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
539 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
543 rd->remote_port = port;
544 rd->remote_vni = vni;
545 rd->remote_ifindex = ifindex;
547 list_add_tail_rcu(&rd->list, &f->remotes);
553 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
555 struct vxlanhdr *vh, size_t hdrlen,
558 size_t start, offset, plen;
561 if (skb->remcsum_offload)
564 if (!NAPI_GRO_CB(skb)->csum_valid)
567 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
568 offset = start + ((data & VXLAN_RCO_UDP) ?
569 offsetof(struct udphdr, check) :
570 offsetof(struct tcphdr, check));
572 plen = hdrlen + offset + sizeof(u16);
574 /* Pull checksum that will be written */
575 if (skb_gro_header_hard(skb, off + plen)) {
576 vh = skb_gro_header_slow(skb, off + plen, off);
581 delta = remcsum_adjust((void *)vh + hdrlen,
582 NAPI_GRO_CB(skb)->csum, start, offset);
584 /* Adjust skb->csum since we changed the packet */
585 skb->csum = csum_add(skb->csum, delta);
586 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
588 skb->remcsum_offload = 1;
593 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
595 struct udp_offload *uoff)
597 struct sk_buff *p, **pp = NULL;
598 struct vxlanhdr *vh, *vh2;
599 unsigned int hlen, off_vx;
601 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
605 off_vx = skb_gro_offset(skb);
606 hlen = off_vx + sizeof(*vh);
607 vh = skb_gro_header_fast(skb, off_vx);
608 if (skb_gro_header_hard(skb, hlen)) {
609 vh = skb_gro_header_slow(skb, hlen, off_vx);
614 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
615 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
617 flags = ntohl(vh->vx_flags);
619 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
620 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
629 for (p = *head; p; p = p->next) {
630 if (!NAPI_GRO_CB(p)->same_flow)
633 vh2 = (struct vxlanhdr *)(p->data + off_vx);
634 if (vh->vx_flags != vh2->vx_flags ||
635 vh->vx_vni != vh2->vx_vni) {
636 NAPI_GRO_CB(p)->same_flow = 0;
641 pp = eth_gro_receive(head, skb);
644 NAPI_GRO_CB(skb)->flush |= flush;
649 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
650 struct udp_offload *uoff)
652 udp_tunnel_gro_complete(skb, nhoff);
654 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
657 /* Notify netdevs that UDP port started listening */
658 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
660 struct net_device *dev;
661 struct sock *sk = vs->sock->sk;
662 struct net *net = sock_net(sk);
663 sa_family_t sa_family = sk->sk_family;
664 __be16 port = inet_sk(sk)->inet_sport;
667 if (sa_family == AF_INET) {
668 err = udp_add_offload(&vs->udp_offloads);
670 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
674 for_each_netdev_rcu(net, dev) {
675 if (dev->netdev_ops->ndo_add_vxlan_port)
676 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
682 /* Notify netdevs that UDP port is no more listening */
683 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
685 struct net_device *dev;
686 struct sock *sk = vs->sock->sk;
687 struct net *net = sock_net(sk);
688 sa_family_t sa_family = sk->sk_family;
689 __be16 port = inet_sk(sk)->inet_sport;
692 for_each_netdev_rcu(net, dev) {
693 if (dev->netdev_ops->ndo_del_vxlan_port)
694 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
699 if (sa_family == AF_INET)
700 udp_del_offload(&vs->udp_offloads);
703 /* Add new entry to forwarding table -- assumes lock held */
704 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
705 const u8 *mac, union vxlan_addr *ip,
706 __u16 state, __u16 flags,
707 __be16 port, __u32 vni, __u32 ifindex,
710 struct vxlan_rdst *rd = NULL;
714 f = __vxlan_find_mac(vxlan, mac);
716 if (flags & NLM_F_EXCL) {
717 netdev_dbg(vxlan->dev,
718 "lost race to create %pM\n", mac);
721 if (f->state != state) {
723 f->updated = jiffies;
726 if (f->flags != ndm_flags) {
727 f->flags = ndm_flags;
728 f->updated = jiffies;
731 if ((flags & NLM_F_REPLACE)) {
732 /* Only change unicasts */
733 if (!(is_multicast_ether_addr(f->eth_addr) ||
734 is_zero_ether_addr(f->eth_addr))) {
735 int rc = vxlan_fdb_replace(f, ip, port, vni,
744 if ((flags & NLM_F_APPEND) &&
745 (is_multicast_ether_addr(f->eth_addr) ||
746 is_zero_ether_addr(f->eth_addr))) {
747 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
755 if (!(flags & NLM_F_CREATE))
758 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
761 /* Disallow replace to add a multicast entry */
762 if ((flags & NLM_F_REPLACE) &&
763 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
766 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
767 f = kmalloc(sizeof(*f), GFP_ATOMIC);
773 f->flags = ndm_flags;
774 f->updated = f->used = jiffies;
775 INIT_LIST_HEAD(&f->remotes);
776 memcpy(f->eth_addr, mac, ETH_ALEN);
778 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
781 hlist_add_head_rcu(&f->hlist,
782 vxlan_fdb_head(vxlan, mac));
787 rd = first_remote_rtnl(f);
788 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
794 static void vxlan_fdb_free(struct rcu_head *head)
796 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
797 struct vxlan_rdst *rd, *nd;
799 list_for_each_entry_safe(rd, nd, &f->remotes, list)
804 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
806 netdev_dbg(vxlan->dev,
807 "delete %pM\n", f->eth_addr);
810 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
812 hlist_del_rcu(&f->hlist);
813 call_rcu(&f->rcu, vxlan_fdb_free);
816 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
817 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
819 struct net *net = dev_net(vxlan->dev);
823 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
827 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
828 if (remote->sa.sa_family == AF_INET) {
829 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
830 ip->sa.sa_family = AF_INET;
831 #if IS_ENABLED(CONFIG_IPV6)
833 ip->sin6.sin6_addr = in6addr_any;
834 ip->sa.sa_family = AF_INET6;
840 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
842 *port = nla_get_be16(tb[NDA_PORT]);
844 *port = vxlan->dst_port;
848 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
850 *vni = nla_get_u32(tb[NDA_VNI]);
852 *vni = vxlan->default_dst.remote_vni;
855 if (tb[NDA_IFINDEX]) {
856 struct net_device *tdev;
858 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
860 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
861 tdev = __dev_get_by_index(net, *ifindex);
863 return -EADDRNOTAVAIL;
871 /* Add static entry (via netlink) */
872 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
873 struct net_device *dev,
874 const unsigned char *addr, u16 vid, u16 flags)
876 struct vxlan_dev *vxlan = netdev_priv(dev);
877 /* struct net *net = dev_net(vxlan->dev); */
883 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
884 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
889 if (tb[NDA_DST] == NULL)
892 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
896 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
897 return -EAFNOSUPPORT;
899 spin_lock_bh(&vxlan->hash_lock);
900 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
901 port, vni, ifindex, ndm->ndm_flags);
902 spin_unlock_bh(&vxlan->hash_lock);
907 /* Delete entry (via netlink) */
908 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
909 struct net_device *dev,
910 const unsigned char *addr, u16 vid)
912 struct vxlan_dev *vxlan = netdev_priv(dev);
914 struct vxlan_rdst *rd = NULL;
920 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
926 spin_lock_bh(&vxlan->hash_lock);
927 f = vxlan_find_mac(vxlan, addr);
931 if (!vxlan_addr_any(&ip)) {
932 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
939 /* remove a destination if it's not the only one on the list,
940 * otherwise destroy the fdb entry
942 if (rd && !list_is_singular(&f->remotes)) {
943 list_del_rcu(&rd->list);
944 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
949 vxlan_fdb_destroy(vxlan, f);
952 spin_unlock_bh(&vxlan->hash_lock);
957 /* Dump forwarding table */
958 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
959 struct net_device *dev,
960 struct net_device *filter_dev, int idx)
962 struct vxlan_dev *vxlan = netdev_priv(dev);
965 for (h = 0; h < FDB_HASH_SIZE; ++h) {
969 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
970 struct vxlan_rdst *rd;
972 if (idx < cb->args[0])
975 list_for_each_entry_rcu(rd, &f->remotes, list) {
976 err = vxlan_fdb_info(skb, vxlan, f,
977 NETLINK_CB(cb->skb).portid,
992 /* Watch incoming packets to learn mapping between Ethernet address
993 * and Tunnel endpoint.
994 * Return true if packet is bogus and should be droppped.
996 static bool vxlan_snoop(struct net_device *dev,
997 union vxlan_addr *src_ip, const u8 *src_mac)
999 struct vxlan_dev *vxlan = netdev_priv(dev);
1000 struct vxlan_fdb *f;
1002 f = vxlan_find_mac(vxlan, src_mac);
1004 struct vxlan_rdst *rdst = first_remote_rcu(f);
1006 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
1009 /* Don't migrate static entries, drop packets */
1010 if (f->state & NUD_NOARP)
1013 if (net_ratelimit())
1015 "%pM migrated from %pIS to %pIS\n",
1016 src_mac, &rdst->remote_ip, &src_ip);
1018 rdst->remote_ip = *src_ip;
1019 f->updated = jiffies;
1020 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
1022 /* learned new entry */
1023 spin_lock(&vxlan->hash_lock);
1025 /* close off race between vxlan_flush and incoming packets */
1026 if (netif_running(dev))
1027 vxlan_fdb_create(vxlan, src_mac, src_ip,
1029 NLM_F_EXCL|NLM_F_CREATE,
1031 vxlan->default_dst.remote_vni,
1033 spin_unlock(&vxlan->hash_lock);
1039 /* See if multicast group is already in use by other ID */
1040 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1042 struct vxlan_dev *vxlan;
1044 /* The vxlan_sock is only used by dev, leaving group has
1045 * no effect on other vxlan devices.
1047 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1050 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1051 if (!netif_running(vxlan->dev) || vxlan == dev)
1054 if (vxlan->vn_sock != dev->vn_sock)
1057 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1058 &dev->default_dst.remote_ip))
1061 if (vxlan->default_dst.remote_ifindex !=
1062 dev->default_dst.remote_ifindex)
1071 static void vxlan_sock_hold(struct vxlan_sock *vs)
1073 atomic_inc(&vs->refcnt);
1076 void vxlan_sock_release(struct vxlan_sock *vs)
1078 struct sock *sk = vs->sock->sk;
1079 struct net *net = sock_net(sk);
1080 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1082 if (!atomic_dec_and_test(&vs->refcnt))
1085 spin_lock(&vn->sock_lock);
1086 hlist_del_rcu(&vs->hlist);
1087 vxlan_notify_del_rx_port(vs);
1088 spin_unlock(&vn->sock_lock);
1090 queue_work(vxlan_wq, &vs->del_work);
1092 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1094 /* Callback to update multicast group membership when first VNI on
1095 * multicast asddress is brought up
1096 * Done as workqueue because ip_mc_join_group acquires RTNL.
1098 static void vxlan_igmp_join(struct work_struct *work)
1100 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
1101 struct vxlan_sock *vs = vxlan->vn_sock;
1102 struct sock *sk = vs->sock->sk;
1103 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1104 int ifindex = vxlan->default_dst.remote_ifindex;
1107 if (ip->sa.sa_family == AF_INET) {
1108 struct ip_mreqn mreq = {
1109 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1110 .imr_ifindex = ifindex,
1113 ip_mc_join_group(sk, &mreq);
1114 #if IS_ENABLED(CONFIG_IPV6)
1116 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1117 &ip->sin6.sin6_addr);
1122 vxlan_sock_release(vs);
1123 dev_put(vxlan->dev);
1126 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1127 static void vxlan_igmp_leave(struct work_struct *work)
1129 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1130 struct vxlan_sock *vs = vxlan->vn_sock;
1131 struct sock *sk = vs->sock->sk;
1132 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1133 int ifindex = vxlan->default_dst.remote_ifindex;
1136 if (ip->sa.sa_family == AF_INET) {
1137 struct ip_mreqn mreq = {
1138 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1139 .imr_ifindex = ifindex,
1142 ip_mc_leave_group(sk, &mreq);
1143 #if IS_ENABLED(CONFIG_IPV6)
1145 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1146 &ip->sin6.sin6_addr);
1152 vxlan_sock_release(vs);
1153 dev_put(vxlan->dev);
1156 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1157 size_t hdrlen, u32 data)
1159 size_t start, offset, plen;
1162 if (skb->remcsum_offload) {
1163 /* Already processed in GRO path */
1164 skb->remcsum_offload = 0;
1168 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1169 offset = start + ((data & VXLAN_RCO_UDP) ?
1170 offsetof(struct udphdr, check) :
1171 offsetof(struct tcphdr, check));
1173 plen = hdrlen + offset + sizeof(u16);
1175 if (!pskb_may_pull(skb, plen))
1178 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1180 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1181 __skb_checksum_complete(skb);
1183 delta = remcsum_adjust((void *)vh + hdrlen,
1184 skb->csum, start, offset);
1186 /* Adjust skb->csum since we changed the packet */
1187 skb->csum = csum_add(skb->csum, delta);
1192 /* Callback from net/ipv4/udp.c to receive packets */
1193 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1195 struct vxlan_sock *vs;
1196 struct vxlanhdr *vxh;
1198 struct vxlan_metadata md = {0};
1200 /* Need Vxlan and inner Ethernet header to be present */
1201 if (!pskb_may_pull(skb, VXLAN_HLEN))
1204 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1205 flags = ntohl(vxh->vx_flags);
1206 vni = ntohl(vxh->vx_vni);
1208 if (flags & VXLAN_HF_VNI) {
1209 flags &= ~VXLAN_HF_VNI;
1211 /* VNI flag always required to be set */
1215 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1217 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1219 vs = rcu_dereference_sk_user_data(sk);
1223 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1224 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1228 flags &= ~VXLAN_HF_RCO;
1229 vni &= VXLAN_VID_MASK;
1232 /* For backwards compatibility, only allow reserved fields to be
1233 * used by VXLAN extensions if explicitly requested.
1235 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1236 struct vxlanhdr_gbp *gbp;
1238 gbp = (struct vxlanhdr_gbp *)vxh;
1239 md.gbp = ntohs(gbp->policy_id);
1241 if (gbp->dont_learn)
1242 md.gbp |= VXLAN_GBP_DONT_LEARN;
1244 if (gbp->policy_applied)
1245 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1247 flags &= ~VXLAN_GBP_USED_BITS;
1250 if (flags || (vni & ~VXLAN_VID_MASK)) {
1251 /* If there are any unprocessed flags remaining treat
1252 * this as a malformed packet. This behavior diverges from
1253 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1254 * in reserved fields are to be ignored. The approach here
1255 * maintains compatbility with previous stack code, and also
1256 * is more robust and provides a little more security in
1257 * adding extensions to VXLAN.
1263 md.vni = vxh->vx_vni;
1264 vs->rcv(vs, skb, &md);
1268 /* Consume bad packet */
1273 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1274 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1277 /* Return non vxlan pkt */
1281 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1282 struct vxlan_metadata *md)
1284 struct iphdr *oip = NULL;
1285 struct ipv6hdr *oip6 = NULL;
1286 struct vxlan_dev *vxlan;
1287 struct pcpu_sw_netstats *stats;
1288 union vxlan_addr saddr;
1291 union vxlan_addr *remote_ip;
1293 vni = ntohl(md->vni) >> 8;
1294 /* Is this VNI defined? */
1295 vxlan = vxlan_vs_find_vni(vs, vni);
1299 remote_ip = &vxlan->default_dst.remote_ip;
1300 skb_reset_mac_header(skb);
1301 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1302 skb->protocol = eth_type_trans(skb, vxlan->dev);
1303 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1305 /* Ignore packet loops (and multicast echo) */
1306 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1309 /* Re-examine inner Ethernet packet */
1310 if (remote_ip->sa.sa_family == AF_INET) {
1312 saddr.sin.sin_addr.s_addr = oip->saddr;
1313 saddr.sa.sa_family = AF_INET;
1314 #if IS_ENABLED(CONFIG_IPV6)
1316 oip6 = ipv6_hdr(skb);
1317 saddr.sin6.sin6_addr = oip6->saddr;
1318 saddr.sa.sa_family = AF_INET6;
1322 if ((vxlan->flags & VXLAN_F_LEARN) &&
1323 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1326 skb_reset_network_header(skb);
1327 skb->mark = md->gbp;
1330 err = IP6_ECN_decapsulate(oip6, skb);
1332 err = IP_ECN_decapsulate(oip, skb);
1334 if (unlikely(err)) {
1335 if (log_ecn_error) {
1337 net_info_ratelimited("non-ECT from %pI6\n",
1340 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1341 &oip->saddr, oip->tos);
1344 ++vxlan->dev->stats.rx_frame_errors;
1345 ++vxlan->dev->stats.rx_errors;
1350 stats = this_cpu_ptr(vxlan->dev->tstats);
1351 u64_stats_update_begin(&stats->syncp);
1352 stats->rx_packets++;
1353 stats->rx_bytes += skb->len;
1354 u64_stats_update_end(&stats->syncp);
1360 /* Consume bad packet */
1364 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1366 struct vxlan_dev *vxlan = netdev_priv(dev);
1367 struct arphdr *parp;
1370 struct neighbour *n;
1372 if (dev->flags & IFF_NOARP)
1375 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1376 dev->stats.tx_dropped++;
1379 parp = arp_hdr(skb);
1381 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1382 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1383 parp->ar_pro != htons(ETH_P_IP) ||
1384 parp->ar_op != htons(ARPOP_REQUEST) ||
1385 parp->ar_hln != dev->addr_len ||
1388 arpptr = (u8 *)parp + sizeof(struct arphdr);
1390 arpptr += dev->addr_len; /* sha */
1391 memcpy(&sip, arpptr, sizeof(sip));
1392 arpptr += sizeof(sip);
1393 arpptr += dev->addr_len; /* tha */
1394 memcpy(&tip, arpptr, sizeof(tip));
1396 if (ipv4_is_loopback(tip) ||
1397 ipv4_is_multicast(tip))
1400 n = neigh_lookup(&arp_tbl, &tip, dev);
1403 struct vxlan_fdb *f;
1404 struct sk_buff *reply;
1406 if (!(n->nud_state & NUD_CONNECTED)) {
1411 f = vxlan_find_mac(vxlan, n->ha);
1412 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1413 /* bridge-local neighbor */
1418 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1426 skb_reset_mac_header(reply);
1427 __skb_pull(reply, skb_network_offset(reply));
1428 reply->ip_summed = CHECKSUM_UNNECESSARY;
1429 reply->pkt_type = PACKET_HOST;
1431 if (netif_rx_ni(reply) == NET_RX_DROP)
1432 dev->stats.rx_dropped++;
1433 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1434 union vxlan_addr ipa = {
1435 .sin.sin_addr.s_addr = tip,
1436 .sin.sin_family = AF_INET,
1439 vxlan_ip_miss(dev, &ipa);
1443 return NETDEV_TX_OK;
1446 #if IS_ENABLED(CONFIG_IPV6)
1447 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1448 struct neighbour *n, bool isrouter)
1450 struct net_device *dev = request->dev;
1451 struct sk_buff *reply;
1452 struct nd_msg *ns, *na;
1453 struct ipv6hdr *pip6;
1455 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1462 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1463 sizeof(*na) + na_olen + dev->needed_tailroom;
1464 reply = alloc_skb(len, GFP_ATOMIC);
1468 reply->protocol = htons(ETH_P_IPV6);
1470 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1471 skb_push(reply, sizeof(struct ethhdr));
1472 skb_set_mac_header(reply, 0);
1474 ns = (struct nd_msg *)skb_transport_header(request);
1476 daddr = eth_hdr(request)->h_source;
1477 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1478 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1479 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1480 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1485 /* Ethernet header */
1486 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1487 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1488 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1489 reply->protocol = htons(ETH_P_IPV6);
1491 skb_pull(reply, sizeof(struct ethhdr));
1492 skb_set_network_header(reply, 0);
1493 skb_put(reply, sizeof(struct ipv6hdr));
1497 pip6 = ipv6_hdr(reply);
1498 memset(pip6, 0, sizeof(struct ipv6hdr));
1500 pip6->priority = ipv6_hdr(request)->priority;
1501 pip6->nexthdr = IPPROTO_ICMPV6;
1502 pip6->hop_limit = 255;
1503 pip6->daddr = ipv6_hdr(request)->saddr;
1504 pip6->saddr = *(struct in6_addr *)n->primary_key;
1506 skb_pull(reply, sizeof(struct ipv6hdr));
1507 skb_set_transport_header(reply, 0);
1509 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1511 /* Neighbor Advertisement */
1512 memset(na, 0, sizeof(*na)+na_olen);
1513 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1514 na->icmph.icmp6_router = isrouter;
1515 na->icmph.icmp6_override = 1;
1516 na->icmph.icmp6_solicited = 1;
1517 na->target = ns->target;
1518 ether_addr_copy(&na->opt[2], n->ha);
1519 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1520 na->opt[1] = na_olen >> 3;
1522 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1523 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1524 csum_partial(na, sizeof(*na)+na_olen, 0));
1526 pip6->payload_len = htons(sizeof(*na)+na_olen);
1528 skb_push(reply, sizeof(struct ipv6hdr));
1530 reply->ip_summed = CHECKSUM_UNNECESSARY;
1535 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1537 struct vxlan_dev *vxlan = netdev_priv(dev);
1539 const struct ipv6hdr *iphdr;
1540 const struct in6_addr *saddr, *daddr;
1541 struct neighbour *n;
1542 struct inet6_dev *in6_dev;
1544 in6_dev = __in6_dev_get(dev);
1548 iphdr = ipv6_hdr(skb);
1549 saddr = &iphdr->saddr;
1550 daddr = &iphdr->daddr;
1552 msg = (struct nd_msg *)skb_transport_header(skb);
1553 if (msg->icmph.icmp6_code != 0 ||
1554 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1557 if (ipv6_addr_loopback(daddr) ||
1558 ipv6_addr_is_multicast(&msg->target))
1561 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1564 struct vxlan_fdb *f;
1565 struct sk_buff *reply;
1567 if (!(n->nud_state & NUD_CONNECTED)) {
1572 f = vxlan_find_mac(vxlan, n->ha);
1573 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1574 /* bridge-local neighbor */
1579 reply = vxlan_na_create(skb, n,
1580 !!(f ? f->flags & NTF_ROUTER : 0));
1587 if (netif_rx_ni(reply) == NET_RX_DROP)
1588 dev->stats.rx_dropped++;
1590 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1591 union vxlan_addr ipa = {
1592 .sin6.sin6_addr = msg->target,
1593 .sin6.sin6_family = AF_INET6,
1596 vxlan_ip_miss(dev, &ipa);
1601 return NETDEV_TX_OK;
1605 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1607 struct vxlan_dev *vxlan = netdev_priv(dev);
1608 struct neighbour *n;
1610 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1614 switch (ntohs(eth_hdr(skb)->h_proto)) {
1619 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1622 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1623 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1624 union vxlan_addr ipa = {
1625 .sin.sin_addr.s_addr = pip->daddr,
1626 .sin.sin_family = AF_INET,
1629 vxlan_ip_miss(dev, &ipa);
1635 #if IS_ENABLED(CONFIG_IPV6)
1638 struct ipv6hdr *pip6;
1640 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1642 pip6 = ipv6_hdr(skb);
1643 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1644 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1645 union vxlan_addr ipa = {
1646 .sin6.sin6_addr = pip6->daddr,
1647 .sin6.sin6_family = AF_INET6,
1650 vxlan_ip_miss(dev, &ipa);
1664 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1666 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1668 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1677 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, struct vxlan_sock *vs,
1678 struct vxlan_metadata *md)
1680 struct vxlanhdr_gbp *gbp;
1682 gbp = (struct vxlanhdr_gbp *)vxh;
1683 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1685 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1686 gbp->dont_learn = 1;
1688 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1689 gbp->policy_applied = 1;
1691 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1694 #if IS_ENABLED(CONFIG_IPV6)
1695 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1696 struct dst_entry *dst, struct sk_buff *skb,
1697 struct net_device *dev, struct in6_addr *saddr,
1698 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1699 __be16 src_port, __be16 dst_port,
1700 struct vxlan_metadata *md, bool xnet)
1702 struct vxlanhdr *vxh;
1705 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
1706 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1707 u16 hdrlen = sizeof(struct vxlanhdr);
1709 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1710 skb->ip_summed == CHECKSUM_PARTIAL) {
1711 int csum_start = skb_checksum_start_offset(skb);
1713 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1714 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1715 (skb->csum_offset == offsetof(struct udphdr, check) ||
1716 skb->csum_offset == offsetof(struct tcphdr, check))) {
1718 type |= SKB_GSO_TUNNEL_REMCSUM;
1722 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1728 skb_scrub_packet(skb, xnet);
1730 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1731 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1732 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1734 /* Need space for new headers (invalidates iph ptr) */
1735 err = skb_cow_head(skb, min_headroom);
1736 if (unlikely(err)) {
1741 skb = vlan_hwaccel_push_inside(skb);
1742 if (WARN_ON(!skb)) {
1747 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1748 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1749 vxh->vx_vni = md->vni;
1751 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1752 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1755 if (skb->csum_offset == offsetof(struct udphdr, check))
1756 data |= VXLAN_RCO_UDP;
1758 vxh->vx_vni |= htonl(data);
1759 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1761 if (!skb_is_gso(skb)) {
1762 skb->ip_summed = CHECKSUM_NONE;
1763 skb->encapsulation = 0;
1767 if (vs->flags & VXLAN_F_GBP)
1768 vxlan_build_gbp_hdr(vxh, vs, md);
1770 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1772 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1773 ttl, src_port, dst_port);
1781 int vxlan_xmit_skb(struct vxlan_sock *vs,
1782 struct rtable *rt, struct sk_buff *skb,
1783 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1784 __be16 src_port, __be16 dst_port,
1785 struct vxlan_metadata *md, bool xnet)
1787 struct vxlanhdr *vxh;
1790 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
1791 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1792 u16 hdrlen = sizeof(struct vxlanhdr);
1794 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1795 skb->ip_summed == CHECKSUM_PARTIAL) {
1796 int csum_start = skb_checksum_start_offset(skb);
1798 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1799 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1800 (skb->csum_offset == offsetof(struct udphdr, check) ||
1801 skb->csum_offset == offsetof(struct tcphdr, check))) {
1803 type |= SKB_GSO_TUNNEL_REMCSUM;
1807 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1809 return PTR_ERR(skb);
1811 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1812 + VXLAN_HLEN + sizeof(struct iphdr)
1813 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1815 /* Need space for new headers (invalidates iph ptr) */
1816 err = skb_cow_head(skb, min_headroom);
1817 if (unlikely(err)) {
1822 skb = vlan_hwaccel_push_inside(skb);
1826 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1827 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1828 vxh->vx_vni = md->vni;
1830 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1831 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1834 if (skb->csum_offset == offsetof(struct udphdr, check))
1835 data |= VXLAN_RCO_UDP;
1837 vxh->vx_vni |= htonl(data);
1838 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1840 if (!skb_is_gso(skb)) {
1841 skb->ip_summed = CHECKSUM_NONE;
1842 skb->encapsulation = 0;
1846 if (vs->flags & VXLAN_F_GBP)
1847 vxlan_build_gbp_hdr(vxh, vs, md);
1849 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1851 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1852 ttl, df, src_port, dst_port, xnet);
1854 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1856 /* Bypass encapsulation if the destination is local */
1857 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1858 struct vxlan_dev *dst_vxlan)
1860 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1861 union vxlan_addr loopback;
1862 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1863 struct net_device *dev = skb->dev;
1866 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1867 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1868 skb->pkt_type = PACKET_HOST;
1869 skb->encapsulation = 0;
1870 skb->dev = dst_vxlan->dev;
1871 __skb_pull(skb, skb_network_offset(skb));
1873 if (remote_ip->sa.sa_family == AF_INET) {
1874 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1875 loopback.sa.sa_family = AF_INET;
1876 #if IS_ENABLED(CONFIG_IPV6)
1878 loopback.sin6.sin6_addr = in6addr_loopback;
1879 loopback.sa.sa_family = AF_INET6;
1883 if (dst_vxlan->flags & VXLAN_F_LEARN)
1884 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1886 u64_stats_update_begin(&tx_stats->syncp);
1887 tx_stats->tx_packets++;
1888 tx_stats->tx_bytes += len;
1889 u64_stats_update_end(&tx_stats->syncp);
1891 if (netif_rx(skb) == NET_RX_SUCCESS) {
1892 u64_stats_update_begin(&rx_stats->syncp);
1893 rx_stats->rx_packets++;
1894 rx_stats->rx_bytes += len;
1895 u64_stats_update_end(&rx_stats->syncp);
1897 dev->stats.rx_dropped++;
1901 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1902 struct vxlan_rdst *rdst, bool did_rsc)
1904 struct vxlan_dev *vxlan = netdev_priv(dev);
1905 struct rtable *rt = NULL;
1906 const struct iphdr *old_iph;
1908 union vxlan_addr *dst;
1909 struct vxlan_metadata md;
1910 __be16 src_port = 0, dst_port;
1916 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1917 vni = rdst->remote_vni;
1918 dst = &rdst->remote_ip;
1920 if (vxlan_addr_any(dst)) {
1922 /* short-circuited back to local bridge */
1923 vxlan_encap_bypass(skb, vxlan, vxlan);
1929 old_iph = ip_hdr(skb);
1932 if (!ttl && vxlan_addr_multicast(dst))
1937 tos = ip_tunnel_get_dsfield(old_iph, skb);
1939 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1940 vxlan->port_max, true);
1942 if (dst->sa.sa_family == AF_INET) {
1943 memset(&fl4, 0, sizeof(fl4));
1944 fl4.flowi4_oif = rdst->remote_ifindex;
1945 fl4.flowi4_tos = RT_TOS(tos);
1946 fl4.daddr = dst->sin.sin_addr.s_addr;
1947 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1949 rt = ip_route_output_key(vxlan->net, &fl4);
1951 netdev_dbg(dev, "no route to %pI4\n",
1952 &dst->sin.sin_addr.s_addr);
1953 dev->stats.tx_carrier_errors++;
1957 if (rt->dst.dev == dev) {
1958 netdev_dbg(dev, "circular route to %pI4\n",
1959 &dst->sin.sin_addr.s_addr);
1960 dev->stats.collisions++;
1964 /* Bypass encapsulation if the destination is local */
1965 if (rt->rt_flags & RTCF_LOCAL &&
1966 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1967 struct vxlan_dev *dst_vxlan;
1970 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1971 dst->sa.sa_family, dst_port,
1975 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1979 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1980 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1981 md.vni = htonl(vni << 8);
1984 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1985 fl4.saddr, dst->sin.sin_addr.s_addr,
1986 tos, ttl, df, src_port, dst_port, &md,
1987 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1989 /* skb is already freed. */
1994 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1995 #if IS_ENABLED(CONFIG_IPV6)
1997 struct sock *sk = vxlan->vn_sock->sock->sk;
1998 struct dst_entry *ndst;
2002 memset(&fl6, 0, sizeof(fl6));
2003 fl6.flowi6_oif = rdst->remote_ifindex;
2004 fl6.daddr = dst->sin6.sin6_addr;
2005 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
2006 fl6.flowi6_proto = IPPROTO_UDP;
2008 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2009 netdev_dbg(dev, "no route to %pI6\n",
2010 &dst->sin6.sin6_addr);
2011 dev->stats.tx_carrier_errors++;
2015 if (ndst->dev == dev) {
2016 netdev_dbg(dev, "circular route to %pI6\n",
2017 &dst->sin6.sin6_addr);
2019 dev->stats.collisions++;
2023 /* Bypass encapsulation if the destination is local */
2024 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2025 if (flags & RTF_LOCAL &&
2026 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2027 struct vxlan_dev *dst_vxlan;
2030 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2031 dst->sa.sa_family, dst_port,
2035 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2039 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2040 md.vni = htonl(vni << 8);
2043 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
2044 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
2045 src_port, dst_port, &md,
2046 !net_eq(vxlan->net, dev_net(vxlan->dev)));
2053 dev->stats.tx_dropped++;
2059 dev->stats.tx_errors++;
2064 /* Transmit local packets over Vxlan
2066 * Outer IP header inherits ECN and DF from inner header.
2067 * Outer UDP destination is the VXLAN assigned port.
2068 * source port is based on hash of flow
2070 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2072 struct vxlan_dev *vxlan = netdev_priv(dev);
2074 bool did_rsc = false;
2075 struct vxlan_rdst *rdst, *fdst = NULL;
2076 struct vxlan_fdb *f;
2078 skb_reset_mac_header(skb);
2081 if ((vxlan->flags & VXLAN_F_PROXY)) {
2082 if (ntohs(eth->h_proto) == ETH_P_ARP)
2083 return arp_reduce(dev, skb);
2084 #if IS_ENABLED(CONFIG_IPV6)
2085 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2086 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2087 + sizeof(struct nd_msg)) &&
2088 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2091 msg = (struct nd_msg *)skb_transport_header(skb);
2092 if (msg->icmph.icmp6_code == 0 &&
2093 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2094 return neigh_reduce(dev, skb);
2100 f = vxlan_find_mac(vxlan, eth->h_dest);
2103 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2104 (ntohs(eth->h_proto) == ETH_P_IP ||
2105 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2106 did_rsc = route_shortcircuit(dev, skb);
2108 f = vxlan_find_mac(vxlan, eth->h_dest);
2112 f = vxlan_find_mac(vxlan, all_zeros_mac);
2114 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2115 !is_multicast_ether_addr(eth->h_dest))
2116 vxlan_fdb_miss(vxlan, eth->h_dest);
2118 dev->stats.tx_dropped++;
2120 return NETDEV_TX_OK;
2124 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2125 struct sk_buff *skb1;
2131 skb1 = skb_clone(skb, GFP_ATOMIC);
2133 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2137 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2140 return NETDEV_TX_OK;
2143 /* Walk the forwarding table and purge stale entries */
2144 static void vxlan_cleanup(unsigned long arg)
2146 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2147 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2150 if (!netif_running(vxlan->dev))
2153 spin_lock_bh(&vxlan->hash_lock);
2154 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2155 struct hlist_node *p, *n;
2156 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2158 = container_of(p, struct vxlan_fdb, hlist);
2159 unsigned long timeout;
2161 if (f->state & NUD_PERMANENT)
2164 timeout = f->used + vxlan->age_interval * HZ;
2165 if (time_before_eq(timeout, jiffies)) {
2166 netdev_dbg(vxlan->dev,
2167 "garbage collect %pM\n",
2169 f->state = NUD_STALE;
2170 vxlan_fdb_destroy(vxlan, f);
2171 } else if (time_before(timeout, next_timer))
2172 next_timer = timeout;
2175 spin_unlock_bh(&vxlan->hash_lock);
2177 mod_timer(&vxlan->age_timer, next_timer);
2180 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2182 __u32 vni = vxlan->default_dst.remote_vni;
2184 vxlan->vn_sock = vs;
2185 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2188 /* Setup stats when device is created */
2189 static int vxlan_init(struct net_device *dev)
2191 struct vxlan_dev *vxlan = netdev_priv(dev);
2192 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2193 struct vxlan_sock *vs;
2194 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2196 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2200 spin_lock(&vn->sock_lock);
2201 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2202 vxlan->dst_port, vxlan->flags);
2203 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
2204 /* If we have a socket with same port already, reuse it */
2205 vxlan_vs_add_dev(vs, vxlan);
2207 /* otherwise make new socket outside of RTNL */
2209 queue_work(vxlan_wq, &vxlan->sock_work);
2211 spin_unlock(&vn->sock_lock);
2216 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2218 struct vxlan_fdb *f;
2220 spin_lock_bh(&vxlan->hash_lock);
2221 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2223 vxlan_fdb_destroy(vxlan, f);
2224 spin_unlock_bh(&vxlan->hash_lock);
2227 static void vxlan_uninit(struct net_device *dev)
2229 struct vxlan_dev *vxlan = netdev_priv(dev);
2230 struct vxlan_sock *vs = vxlan->vn_sock;
2232 vxlan_fdb_delete_default(vxlan);
2235 vxlan_sock_release(vs);
2236 free_percpu(dev->tstats);
2239 /* Start ageing timer and join group when device is brought up */
2240 static int vxlan_open(struct net_device *dev)
2242 struct vxlan_dev *vxlan = netdev_priv(dev);
2243 struct vxlan_sock *vs = vxlan->vn_sock;
2245 /* socket hasn't been created */
2249 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2250 vxlan_sock_hold(vs);
2252 queue_work(vxlan_wq, &vxlan->igmp_join);
2255 if (vxlan->age_interval)
2256 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2261 /* Purge the forwarding table */
2262 static void vxlan_flush(struct vxlan_dev *vxlan)
2266 spin_lock_bh(&vxlan->hash_lock);
2267 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2268 struct hlist_node *p, *n;
2269 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2271 = container_of(p, struct vxlan_fdb, hlist);
2272 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2273 if (!is_zero_ether_addr(f->eth_addr))
2274 vxlan_fdb_destroy(vxlan, f);
2277 spin_unlock_bh(&vxlan->hash_lock);
2280 /* Cleanup timer and forwarding table on shutdown */
2281 static int vxlan_stop(struct net_device *dev)
2283 struct vxlan_dev *vxlan = netdev_priv(dev);
2284 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2285 struct vxlan_sock *vs = vxlan->vn_sock;
2287 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2288 !vxlan_group_used(vn, vxlan)) {
2289 vxlan_sock_hold(vs);
2291 queue_work(vxlan_wq, &vxlan->igmp_leave);
2294 del_timer_sync(&vxlan->age_timer);
2301 /* Stub, nothing needs to be done. */
2302 static void vxlan_set_multicast_list(struct net_device *dev)
2306 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2308 struct vxlan_dev *vxlan = netdev_priv(dev);
2309 struct vxlan_rdst *dst = &vxlan->default_dst;
2310 struct net_device *lowerdev;
2313 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2314 if (lowerdev == NULL)
2315 return eth_change_mtu(dev, new_mtu);
2317 if (dst->remote_ip.sa.sa_family == AF_INET6)
2318 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2320 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2322 if (new_mtu < 68 || new_mtu > max_mtu)
2329 static const struct net_device_ops vxlan_netdev_ops = {
2330 .ndo_init = vxlan_init,
2331 .ndo_uninit = vxlan_uninit,
2332 .ndo_open = vxlan_open,
2333 .ndo_stop = vxlan_stop,
2334 .ndo_start_xmit = vxlan_xmit,
2335 .ndo_get_stats64 = ip_tunnel_get_stats64,
2336 .ndo_set_rx_mode = vxlan_set_multicast_list,
2337 .ndo_change_mtu = vxlan_change_mtu,
2338 .ndo_validate_addr = eth_validate_addr,
2339 .ndo_set_mac_address = eth_mac_addr,
2340 .ndo_fdb_add = vxlan_fdb_add,
2341 .ndo_fdb_del = vxlan_fdb_delete,
2342 .ndo_fdb_dump = vxlan_fdb_dump,
2345 /* Info for udev, that this is a virtual tunnel endpoint */
2346 static struct device_type vxlan_type = {
2350 /* Calls the ndo_add_vxlan_port of the caller in order to
2351 * supply the listening VXLAN udp ports. Callers are expected
2352 * to implement the ndo_add_vxlan_port.
2354 void vxlan_get_rx_port(struct net_device *dev)
2356 struct vxlan_sock *vs;
2357 struct net *net = dev_net(dev);
2358 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2359 sa_family_t sa_family;
2363 spin_lock(&vn->sock_lock);
2364 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2365 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2366 port = inet_sk(vs->sock->sk)->inet_sport;
2367 sa_family = vs->sock->sk->sk_family;
2368 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2372 spin_unlock(&vn->sock_lock);
2374 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2376 /* Initialize the device structure. */
2377 static void vxlan_setup(struct net_device *dev)
2379 struct vxlan_dev *vxlan = netdev_priv(dev);
2382 eth_hw_addr_random(dev);
2384 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2385 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2387 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2389 dev->netdev_ops = &vxlan_netdev_ops;
2390 dev->destructor = free_netdev;
2391 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2393 dev->tx_queue_len = 0;
2394 dev->features |= NETIF_F_LLTX;
2395 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2396 dev->features |= NETIF_F_RXCSUM;
2397 dev->features |= NETIF_F_GSO_SOFTWARE;
2399 dev->vlan_features = dev->features;
2400 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2401 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2402 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2403 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2404 netif_keep_dst(dev);
2405 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2407 INIT_LIST_HEAD(&vxlan->next);
2408 spin_lock_init(&vxlan->hash_lock);
2409 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2410 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2411 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2413 init_timer_deferrable(&vxlan->age_timer);
2414 vxlan->age_timer.function = vxlan_cleanup;
2415 vxlan->age_timer.data = (unsigned long) vxlan;
2417 vxlan->dst_port = htons(vxlan_port);
2421 for (h = 0; h < FDB_HASH_SIZE; ++h)
2422 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2425 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2426 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2427 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2428 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2429 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2430 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2431 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2432 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2433 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2434 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2435 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2436 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2437 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2438 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2439 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2440 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2441 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2442 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2443 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2444 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2445 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2446 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2447 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2448 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2451 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2453 if (tb[IFLA_ADDRESS]) {
2454 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2455 pr_debug("invalid link address (not ethernet)\n");
2459 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2460 pr_debug("invalid all zero ethernet address\n");
2461 return -EADDRNOTAVAIL;
2468 if (data[IFLA_VXLAN_ID]) {
2469 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2470 if (id >= VXLAN_VID_MASK)
2474 if (data[IFLA_VXLAN_PORT_RANGE]) {
2475 const struct ifla_vxlan_port_range *p
2476 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2478 if (ntohs(p->high) < ntohs(p->low)) {
2479 pr_debug("port range %u .. %u not valid\n",
2480 ntohs(p->low), ntohs(p->high));
2488 static void vxlan_get_drvinfo(struct net_device *netdev,
2489 struct ethtool_drvinfo *drvinfo)
2491 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2492 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2495 static const struct ethtool_ops vxlan_ethtool_ops = {
2496 .get_drvinfo = vxlan_get_drvinfo,
2497 .get_link = ethtool_op_get_link,
2500 static void vxlan_del_work(struct work_struct *work)
2502 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2503 udp_tunnel_sock_release(vs->sock);
2507 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2508 __be16 port, u32 flags)
2510 struct socket *sock;
2511 struct udp_port_cfg udp_conf;
2514 memset(&udp_conf, 0, sizeof(udp_conf));
2517 udp_conf.family = AF_INET6;
2518 udp_conf.use_udp6_tx_checksums =
2519 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2520 udp_conf.use_udp6_rx_checksums =
2521 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2523 udp_conf.family = AF_INET;
2524 udp_conf.local_ip.s_addr = INADDR_ANY;
2525 udp_conf.use_udp_checksums =
2526 !!(flags & VXLAN_F_UDP_CSUM);
2529 udp_conf.local_udp_port = port;
2531 /* Open UDP socket */
2532 err = udp_sock_create(net, &udp_conf, &sock);
2534 return ERR_PTR(err);
2539 /* Create new listen socket if needed */
2540 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2541 vxlan_rcv_t *rcv, void *data,
2544 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2545 struct vxlan_sock *vs;
2546 struct socket *sock;
2548 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2549 struct udp_tunnel_sock_cfg tunnel_cfg;
2551 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2553 return ERR_PTR(-ENOMEM);
2555 for (h = 0; h < VNI_HASH_SIZE; ++h)
2556 INIT_HLIST_HEAD(&vs->vni_list[h]);
2558 INIT_WORK(&vs->del_work, vxlan_del_work);
2560 sock = vxlan_create_sock(net, ipv6, port, flags);
2563 return ERR_CAST(sock);
2567 atomic_set(&vs->refcnt, 1);
2572 /* Initialize the vxlan udp offloads structure */
2573 vs->udp_offloads.port = port;
2574 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2575 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2577 spin_lock(&vn->sock_lock);
2578 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2579 vxlan_notify_add_rx_port(vs);
2580 spin_unlock(&vn->sock_lock);
2582 /* Mark socket as an encapsulation socket. */
2583 tunnel_cfg.sk_user_data = vs;
2584 tunnel_cfg.encap_type = 1;
2585 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2586 tunnel_cfg.encap_destroy = NULL;
2588 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2593 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2594 vxlan_rcv_t *rcv, void *data,
2595 bool no_share, u32 flags)
2597 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2598 struct vxlan_sock *vs;
2599 bool ipv6 = flags & VXLAN_F_IPV6;
2601 vs = vxlan_socket_create(net, port, rcv, data, flags);
2605 if (no_share) /* Return error if sharing is not allowed. */
2608 spin_lock(&vn->sock_lock);
2609 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
2610 if (vs && ((vs->rcv != rcv) ||
2611 !atomic_add_unless(&vs->refcnt, 1, 0)))
2612 vs = ERR_PTR(-EBUSY);
2613 spin_unlock(&vn->sock_lock);
2616 vs = ERR_PTR(-EINVAL);
2620 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2622 /* Scheduled at device creation to bind to a socket */
2623 static void vxlan_sock_work(struct work_struct *work)
2625 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2626 struct net *net = vxlan->net;
2627 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2628 __be16 port = vxlan->dst_port;
2629 struct vxlan_sock *nvs;
2631 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
2632 spin_lock(&vn->sock_lock);
2634 vxlan_vs_add_dev(nvs, vxlan);
2635 spin_unlock(&vn->sock_lock);
2637 dev_put(vxlan->dev);
2640 static int vxlan_newlink(struct net *net, struct net_device *dev,
2641 struct nlattr *tb[], struct nlattr *data[])
2643 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2644 struct vxlan_dev *vxlan = netdev_priv(dev);
2645 struct vxlan_rdst *dst = &vxlan->default_dst;
2648 bool use_ipv6 = false;
2650 if (!data[IFLA_VXLAN_ID])
2653 vxlan->net = dev_net(dev);
2655 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2656 dst->remote_vni = vni;
2658 /* Unless IPv6 is explicitly requested, assume IPv4 */
2659 dst->remote_ip.sa.sa_family = AF_INET;
2660 if (data[IFLA_VXLAN_GROUP]) {
2661 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2662 } else if (data[IFLA_VXLAN_GROUP6]) {
2663 if (!IS_ENABLED(CONFIG_IPV6))
2664 return -EPFNOSUPPORT;
2666 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2667 sizeof(struct in6_addr));
2668 dst->remote_ip.sa.sa_family = AF_INET6;
2672 if (data[IFLA_VXLAN_LOCAL]) {
2673 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2674 vxlan->saddr.sa.sa_family = AF_INET;
2675 } else if (data[IFLA_VXLAN_LOCAL6]) {
2676 if (!IS_ENABLED(CONFIG_IPV6))
2677 return -EPFNOSUPPORT;
2679 /* TODO: respect scope id */
2680 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2681 sizeof(struct in6_addr));
2682 vxlan->saddr.sa.sa_family = AF_INET6;
2686 if (data[IFLA_VXLAN_LINK] &&
2687 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2688 struct net_device *lowerdev
2689 = __dev_get_by_index(net, dst->remote_ifindex);
2692 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2696 #if IS_ENABLED(CONFIG_IPV6)
2698 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2699 if (idev && idev->cnf.disable_ipv6) {
2700 pr_info("IPv6 is disabled via sysctl\n");
2703 vxlan->flags |= VXLAN_F_IPV6;
2708 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2710 dev->needed_headroom = lowerdev->hard_header_len +
2711 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2712 } else if (use_ipv6)
2713 vxlan->flags |= VXLAN_F_IPV6;
2715 if (data[IFLA_VXLAN_TOS])
2716 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2718 if (data[IFLA_VXLAN_TTL])
2719 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2721 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2722 vxlan->flags |= VXLAN_F_LEARN;
2724 if (data[IFLA_VXLAN_AGEING])
2725 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2727 vxlan->age_interval = FDB_AGE_DEFAULT;
2729 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2730 vxlan->flags |= VXLAN_F_PROXY;
2732 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2733 vxlan->flags |= VXLAN_F_RSC;
2735 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2736 vxlan->flags |= VXLAN_F_L2MISS;
2738 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2739 vxlan->flags |= VXLAN_F_L3MISS;
2741 if (data[IFLA_VXLAN_LIMIT])
2742 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2744 if (data[IFLA_VXLAN_PORT_RANGE]) {
2745 const struct ifla_vxlan_port_range *p
2746 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2747 vxlan->port_min = ntohs(p->low);
2748 vxlan->port_max = ntohs(p->high);
2751 if (data[IFLA_VXLAN_PORT])
2752 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2754 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2755 vxlan->flags |= VXLAN_F_UDP_CSUM;
2757 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2758 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2759 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2761 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2762 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2763 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2765 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2766 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2767 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2769 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2770 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2771 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2773 if (data[IFLA_VXLAN_GBP])
2774 vxlan->flags |= VXLAN_F_GBP;
2776 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2777 vxlan->dst_port, vxlan->flags)) {
2778 pr_info("duplicate VNI %u\n", vni);
2782 dev->ethtool_ops = &vxlan_ethtool_ops;
2784 /* create an fdb entry for a valid default destination */
2785 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2786 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2787 &vxlan->default_dst.remote_ip,
2788 NUD_REACHABLE|NUD_PERMANENT,
2789 NLM_F_EXCL|NLM_F_CREATE,
2791 vxlan->default_dst.remote_vni,
2792 vxlan->default_dst.remote_ifindex,
2798 err = register_netdevice(dev);
2800 vxlan_fdb_delete_default(vxlan);
2804 list_add(&vxlan->next, &vn->vxlan_list);
2809 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2811 struct vxlan_dev *vxlan = netdev_priv(dev);
2812 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2814 spin_lock(&vn->sock_lock);
2815 if (!hlist_unhashed(&vxlan->hlist))
2816 hlist_del_rcu(&vxlan->hlist);
2817 spin_unlock(&vn->sock_lock);
2819 list_del(&vxlan->next);
2820 unregister_netdevice_queue(dev, head);
2823 static size_t vxlan_get_size(const struct net_device *dev)
2826 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2827 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2828 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2829 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2834 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2835 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2836 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2837 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2838 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2839 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2840 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2841 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2842 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2843 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2844 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2845 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2849 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2851 const struct vxlan_dev *vxlan = netdev_priv(dev);
2852 const struct vxlan_rdst *dst = &vxlan->default_dst;
2853 struct ifla_vxlan_port_range ports = {
2854 .low = htons(vxlan->port_min),
2855 .high = htons(vxlan->port_max),
2858 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2859 goto nla_put_failure;
2861 if (!vxlan_addr_any(&dst->remote_ip)) {
2862 if (dst->remote_ip.sa.sa_family == AF_INET) {
2863 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2864 dst->remote_ip.sin.sin_addr.s_addr))
2865 goto nla_put_failure;
2866 #if IS_ENABLED(CONFIG_IPV6)
2868 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2869 &dst->remote_ip.sin6.sin6_addr))
2870 goto nla_put_failure;
2875 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2876 goto nla_put_failure;
2878 if (!vxlan_addr_any(&vxlan->saddr)) {
2879 if (vxlan->saddr.sa.sa_family == AF_INET) {
2880 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2881 vxlan->saddr.sin.sin_addr.s_addr))
2882 goto nla_put_failure;
2883 #if IS_ENABLED(CONFIG_IPV6)
2885 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2886 &vxlan->saddr.sin6.sin6_addr))
2887 goto nla_put_failure;
2892 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2893 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2894 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2895 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2896 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2897 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2898 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2899 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2900 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2901 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2902 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2903 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2904 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2905 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2906 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2907 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2908 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2909 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2910 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2911 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2912 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2913 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2914 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2915 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2916 goto nla_put_failure;
2918 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2919 goto nla_put_failure;
2921 if (vxlan->flags & VXLAN_F_GBP &&
2922 nla_put_flag(skb, IFLA_VXLAN_GBP))
2923 goto nla_put_failure;
2931 static struct net *vxlan_get_link_net(const struct net_device *dev)
2933 struct vxlan_dev *vxlan = netdev_priv(dev);
2938 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2940 .maxtype = IFLA_VXLAN_MAX,
2941 .policy = vxlan_policy,
2942 .priv_size = sizeof(struct vxlan_dev),
2943 .setup = vxlan_setup,
2944 .validate = vxlan_validate,
2945 .newlink = vxlan_newlink,
2946 .dellink = vxlan_dellink,
2947 .get_size = vxlan_get_size,
2948 .fill_info = vxlan_fill_info,
2949 .get_link_net = vxlan_get_link_net,
2952 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2953 struct net_device *dev)
2955 struct vxlan_dev *vxlan, *next;
2956 LIST_HEAD(list_kill);
2958 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2959 struct vxlan_rdst *dst = &vxlan->default_dst;
2961 /* In case we created vxlan device with carrier
2962 * and we loose the carrier due to module unload
2963 * we also need to remove vxlan device. In other
2964 * cases, it's not necessary and remote_ifindex
2965 * is 0 here, so no matches.
2967 if (dst->remote_ifindex == dev->ifindex)
2968 vxlan_dellink(vxlan->dev, &list_kill);
2971 unregister_netdevice_many(&list_kill);
2974 static int vxlan_lowerdev_event(struct notifier_block *unused,
2975 unsigned long event, void *ptr)
2977 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2978 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2980 if (event == NETDEV_UNREGISTER)
2981 vxlan_handle_lowerdev_unregister(vn, dev);
2986 static struct notifier_block vxlan_notifier_block __read_mostly = {
2987 .notifier_call = vxlan_lowerdev_event,
2990 static __net_init int vxlan_init_net(struct net *net)
2992 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2995 INIT_LIST_HEAD(&vn->vxlan_list);
2996 spin_lock_init(&vn->sock_lock);
2998 for (h = 0; h < PORT_HASH_SIZE; ++h)
2999 INIT_HLIST_HEAD(&vn->sock_list[h]);
3004 static void __net_exit vxlan_exit_net(struct net *net)
3006 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3007 struct vxlan_dev *vxlan, *next;
3008 struct net_device *dev, *aux;
3012 for_each_netdev_safe(net, dev, aux)
3013 if (dev->rtnl_link_ops == &vxlan_link_ops)
3014 unregister_netdevice_queue(dev, &list);
3016 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3017 /* If vxlan->dev is in the same netns, it has already been added
3018 * to the list by the previous loop.
3020 if (!net_eq(dev_net(vxlan->dev), net))
3021 unregister_netdevice_queue(dev, &list);
3024 unregister_netdevice_many(&list);
3028 static struct pernet_operations vxlan_net_ops = {
3029 .init = vxlan_init_net,
3030 .exit = vxlan_exit_net,
3031 .id = &vxlan_net_id,
3032 .size = sizeof(struct vxlan_net),
3035 static int __init vxlan_init_module(void)
3039 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3043 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3045 rc = register_pernet_subsys(&vxlan_net_ops);
3049 rc = register_netdevice_notifier(&vxlan_notifier_block);
3053 rc = rtnl_link_register(&vxlan_link_ops);
3059 unregister_netdevice_notifier(&vxlan_notifier_block);
3061 unregister_pernet_subsys(&vxlan_net_ops);
3063 destroy_workqueue(vxlan_wq);
3066 late_initcall(vxlan_init_module);
3068 static void __exit vxlan_cleanup_module(void)
3070 rtnl_link_unregister(&vxlan_link_ops);
3071 unregister_netdevice_notifier(&vxlan_notifier_block);
3072 destroy_workqueue(vxlan_wq);
3073 unregister_pernet_subsys(&vxlan_net_ops);
3074 /* rcu_barrier() is called by netns */
3076 module_exit(vxlan_cleanup_module);
3078 MODULE_LICENSE("GPL");
3079 MODULE_VERSION(VXLAN_VERSION);
3080 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3081 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3082 MODULE_ALIAS_RTNL_LINK("vxlan");