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>
52 #include <net/dst_metadata.h>
54 #define VXLAN_VERSION "0.1"
56 #define PORT_HASH_BITS 8
57 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
61 /* UDP port for VXLAN traffic.
62 * The IANA assigned port is 4789, but the Linux default is 8472
63 * for compatibility with early adopters.
65 static unsigned short vxlan_port __read_mostly = 8472;
66 module_param_named(udp_port, vxlan_port, ushort, 0444);
67 MODULE_PARM_DESC(udp_port, "Destination UDP port");
69 static bool log_ecn_error = true;
70 module_param(log_ecn_error, bool, 0644);
71 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
73 static int vxlan_net_id;
74 static struct rtnl_link_ops vxlan_link_ops;
76 static const u8 all_zeros_mac[ETH_ALEN];
78 static int vxlan_sock_add(struct vxlan_dev *vxlan);
80 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
82 /* per-network namespace private data for this module */
84 struct list_head vxlan_list;
85 struct hlist_head sock_list[PORT_HASH_SIZE];
89 /* Forwarding table entry */
91 struct hlist_node hlist; /* linked list of entries */
93 unsigned long updated; /* jiffies */
95 struct list_head remotes;
96 u8 eth_addr[ETH_ALEN];
97 u16 state; /* see ndm_state */
98 u8 flags; /* see ndm_flags */
101 /* salt for hash table */
102 static u32 vxlan_salt __read_mostly;
103 static struct workqueue_struct *vxlan_wq;
105 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
107 return vs->flags & VXLAN_F_COLLECT_METADATA ||
108 ip_tunnel_collect_metadata();
111 #if IS_ENABLED(CONFIG_IPV6)
113 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
115 if (a->sa.sa_family != b->sa.sa_family)
117 if (a->sa.sa_family == AF_INET6)
118 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
120 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
123 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
125 if (ipa->sa.sa_family == AF_INET6)
126 return ipv6_addr_any(&ipa->sin6.sin6_addr);
128 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
131 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
133 if (ipa->sa.sa_family == AF_INET6)
134 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
136 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
139 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
141 if (nla_len(nla) >= sizeof(struct in6_addr)) {
142 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
143 ip->sa.sa_family = AF_INET6;
145 } else if (nla_len(nla) >= sizeof(__be32)) {
146 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
147 ip->sa.sa_family = AF_INET;
150 return -EAFNOSUPPORT;
154 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
155 const union vxlan_addr *ip)
157 if (ip->sa.sa_family == AF_INET6)
158 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
160 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
163 #else /* !CONFIG_IPV6 */
166 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
168 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
171 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
173 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
176 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
178 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
181 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
183 if (nla_len(nla) >= sizeof(struct in6_addr)) {
184 return -EAFNOSUPPORT;
185 } else if (nla_len(nla) >= sizeof(__be32)) {
186 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
187 ip->sa.sa_family = AF_INET;
190 return -EAFNOSUPPORT;
194 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
195 const union vxlan_addr *ip)
197 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
201 /* Virtual Network hash table head */
202 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
204 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
207 /* Socket hash table head */
208 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
210 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
212 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
215 /* First remote destination for a forwarding entry.
216 * Guaranteed to be non-NULL because remotes are never deleted.
218 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
220 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
223 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
225 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
228 /* Find VXLAN socket based on network namespace, address family and UDP port
229 * and enabled unshareable flags.
231 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
232 __be16 port, u32 flags)
234 struct vxlan_sock *vs;
236 flags &= VXLAN_F_RCV_FLAGS;
238 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
239 if (inet_sk(vs->sock->sk)->inet_sport == port &&
240 vxlan_get_sk_family(vs) == family &&
247 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
249 struct vxlan_dev *vxlan;
251 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
252 if (vxlan->default_dst.remote_vni == id)
259 /* Look up VNI in a per net namespace table */
260 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
261 sa_family_t family, __be16 port,
264 struct vxlan_sock *vs;
266 vs = vxlan_find_sock(net, family, port, flags);
270 return vxlan_vs_find_vni(vs, id);
273 /* Fill in neighbour message in skbuff. */
274 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
275 const struct vxlan_fdb *fdb,
276 u32 portid, u32 seq, int type, unsigned int flags,
277 const struct vxlan_rdst *rdst)
279 unsigned long now = jiffies;
280 struct nda_cacheinfo ci;
281 struct nlmsghdr *nlh;
283 bool send_ip, send_eth;
285 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
289 ndm = nlmsg_data(nlh);
290 memset(ndm, 0, sizeof(*ndm));
292 send_eth = send_ip = true;
294 if (type == RTM_GETNEIGH) {
295 ndm->ndm_family = AF_INET;
296 send_ip = !vxlan_addr_any(&rdst->remote_ip);
297 send_eth = !is_zero_ether_addr(fdb->eth_addr);
299 ndm->ndm_family = AF_BRIDGE;
300 ndm->ndm_state = fdb->state;
301 ndm->ndm_ifindex = vxlan->dev->ifindex;
302 ndm->ndm_flags = fdb->flags;
303 ndm->ndm_type = RTN_UNICAST;
305 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
306 nla_put_s32(skb, NDA_LINK_NETNSID,
307 peernet2id_alloc(dev_net(vxlan->dev), vxlan->net)))
308 goto nla_put_failure;
310 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
311 goto nla_put_failure;
313 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
314 goto nla_put_failure;
316 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
317 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
318 goto nla_put_failure;
319 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
320 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
321 goto nla_put_failure;
322 if (rdst->remote_ifindex &&
323 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
324 goto nla_put_failure;
326 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
327 ci.ndm_confirmed = 0;
328 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
331 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
332 goto nla_put_failure;
338 nlmsg_cancel(skb, nlh);
342 static inline size_t vxlan_nlmsg_size(void)
344 return NLMSG_ALIGN(sizeof(struct ndmsg))
345 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
346 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
347 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
348 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
349 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
350 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
351 + nla_total_size(sizeof(struct nda_cacheinfo));
354 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
355 struct vxlan_rdst *rd, int type)
357 struct net *net = dev_net(vxlan->dev);
361 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
365 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
367 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
368 WARN_ON(err == -EMSGSIZE);
373 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
377 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
380 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
382 struct vxlan_dev *vxlan = netdev_priv(dev);
383 struct vxlan_fdb f = {
386 struct vxlan_rdst remote = {
387 .remote_ip = *ipa, /* goes to NDA_DST */
388 .remote_vni = VXLAN_N_VID,
391 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
394 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
396 struct vxlan_fdb f = {
399 struct vxlan_rdst remote = { };
401 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
403 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
406 /* Hash Ethernet address */
407 static u32 eth_hash(const unsigned char *addr)
409 u64 value = get_unaligned((u64 *)addr);
411 /* only want 6 bytes */
417 return hash_64(value, FDB_HASH_BITS);
420 /* Hash chain to use given mac address */
421 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
424 return &vxlan->fdb_head[eth_hash(mac)];
427 /* Look up Ethernet address in forwarding table */
428 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
431 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
434 hlist_for_each_entry_rcu(f, head, hlist) {
435 if (ether_addr_equal(mac, f->eth_addr))
442 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
447 f = __vxlan_find_mac(vxlan, mac);
454 /* caller should hold vxlan->hash_lock */
455 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
456 union vxlan_addr *ip, __be16 port,
457 __u32 vni, __u32 ifindex)
459 struct vxlan_rdst *rd;
461 list_for_each_entry(rd, &f->remotes, list) {
462 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
463 rd->remote_port == port &&
464 rd->remote_vni == vni &&
465 rd->remote_ifindex == ifindex)
472 /* Replace destination of unicast mac */
473 static int vxlan_fdb_replace(struct vxlan_fdb *f,
474 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
476 struct vxlan_rdst *rd;
478 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
482 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
486 rd->remote_port = port;
487 rd->remote_vni = vni;
488 rd->remote_ifindex = ifindex;
492 /* Add/update destinations for multicast */
493 static int vxlan_fdb_append(struct vxlan_fdb *f,
494 union vxlan_addr *ip, __be16 port, __u32 vni,
495 __u32 ifindex, struct vxlan_rdst **rdp)
497 struct vxlan_rdst *rd;
499 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
503 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
507 rd->remote_port = port;
508 rd->remote_vni = vni;
509 rd->remote_ifindex = ifindex;
511 list_add_tail_rcu(&rd->list, &f->remotes);
517 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
519 struct vxlanhdr *vh, size_t hdrlen,
520 u32 data, struct gro_remcsum *grc,
523 size_t start, offset;
525 if (skb->remcsum_offload)
528 if (!NAPI_GRO_CB(skb)->csum_valid)
531 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
532 offset = start + ((data & VXLAN_RCO_UDP) ?
533 offsetof(struct udphdr, check) :
534 offsetof(struct tcphdr, check));
536 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
537 start, offset, grc, nopartial);
539 skb->remcsum_offload = 1;
544 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
546 struct udp_offload *uoff)
548 struct sk_buff *p, **pp = NULL;
549 struct vxlanhdr *vh, *vh2;
550 unsigned int hlen, off_vx;
552 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
555 struct gro_remcsum grc;
557 skb_gro_remcsum_init(&grc);
559 off_vx = skb_gro_offset(skb);
560 hlen = off_vx + sizeof(*vh);
561 vh = skb_gro_header_fast(skb, off_vx);
562 if (skb_gro_header_hard(skb, hlen)) {
563 vh = skb_gro_header_slow(skb, hlen, off_vx);
568 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
570 flags = ntohl(vh->vx_flags);
572 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
573 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
574 ntohl(vh->vx_vni), &grc,
576 VXLAN_F_REMCSUM_NOPARTIAL));
582 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
586 for (p = *head; p; p = p->next) {
587 if (!NAPI_GRO_CB(p)->same_flow)
590 vh2 = (struct vxlanhdr *)(p->data + off_vx);
591 if (vh->vx_flags != vh2->vx_flags ||
592 vh->vx_vni != vh2->vx_vni) {
593 NAPI_GRO_CB(p)->same_flow = 0;
598 pp = call_gro_receive(eth_gro_receive, head, skb);
601 skb_gro_remcsum_cleanup(skb, &grc);
602 NAPI_GRO_CB(skb)->flush |= flush;
607 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
608 struct udp_offload *uoff)
610 udp_tunnel_gro_complete(skb, nhoff);
612 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
615 /* Notify netdevs that UDP port started listening */
616 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
618 struct net_device *dev;
619 struct sock *sk = vs->sock->sk;
620 struct net *net = sock_net(sk);
621 sa_family_t sa_family = vxlan_get_sk_family(vs);
622 __be16 port = inet_sk(sk)->inet_sport;
625 if (sa_family == AF_INET) {
626 err = udp_add_offload(&vs->udp_offloads);
628 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
632 for_each_netdev_rcu(net, dev) {
633 if (dev->netdev_ops->ndo_add_vxlan_port)
634 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
640 /* Notify netdevs that UDP port is no more listening */
641 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
643 struct net_device *dev;
644 struct sock *sk = vs->sock->sk;
645 struct net *net = sock_net(sk);
646 sa_family_t sa_family = vxlan_get_sk_family(vs);
647 __be16 port = inet_sk(sk)->inet_sport;
650 for_each_netdev_rcu(net, dev) {
651 if (dev->netdev_ops->ndo_del_vxlan_port)
652 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
657 if (sa_family == AF_INET)
658 udp_del_offload(&vs->udp_offloads);
661 /* Add new entry to forwarding table -- assumes lock held */
662 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
663 const u8 *mac, union vxlan_addr *ip,
664 __u16 state, __u16 flags,
665 __be16 port, __u32 vni, __u32 ifindex,
668 struct vxlan_rdst *rd = NULL;
672 f = __vxlan_find_mac(vxlan, mac);
674 if (flags & NLM_F_EXCL) {
675 netdev_dbg(vxlan->dev,
676 "lost race to create %pM\n", mac);
679 if (f->state != state) {
681 f->updated = jiffies;
684 if (f->flags != ndm_flags) {
685 f->flags = ndm_flags;
686 f->updated = jiffies;
689 if ((flags & NLM_F_REPLACE)) {
690 /* Only change unicasts */
691 if (!(is_multicast_ether_addr(f->eth_addr) ||
692 is_zero_ether_addr(f->eth_addr))) {
693 notify |= vxlan_fdb_replace(f, ip, port, vni,
698 if ((flags & NLM_F_APPEND) &&
699 (is_multicast_ether_addr(f->eth_addr) ||
700 is_zero_ether_addr(f->eth_addr))) {
701 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
709 if (!(flags & NLM_F_CREATE))
712 if (vxlan->cfg.addrmax &&
713 vxlan->addrcnt >= vxlan->cfg.addrmax)
716 /* Disallow replace to add a multicast entry */
717 if ((flags & NLM_F_REPLACE) &&
718 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
721 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
722 f = kmalloc(sizeof(*f), GFP_ATOMIC);
728 f->flags = ndm_flags;
729 f->updated = f->used = jiffies;
730 INIT_LIST_HEAD(&f->remotes);
731 memcpy(f->eth_addr, mac, ETH_ALEN);
733 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
736 hlist_add_head_rcu(&f->hlist,
737 vxlan_fdb_head(vxlan, mac));
742 rd = first_remote_rtnl(f);
743 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
749 static void vxlan_fdb_free(struct rcu_head *head)
751 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
752 struct vxlan_rdst *rd, *nd;
754 list_for_each_entry_safe(rd, nd, &f->remotes, list)
759 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
761 netdev_dbg(vxlan->dev,
762 "delete %pM\n", f->eth_addr);
765 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
767 hlist_del_rcu(&f->hlist);
768 call_rcu(&f->rcu, vxlan_fdb_free);
771 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
772 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
774 struct net *net = dev_net(vxlan->dev);
778 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
782 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
783 if (remote->sa.sa_family == AF_INET) {
784 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
785 ip->sa.sa_family = AF_INET;
786 #if IS_ENABLED(CONFIG_IPV6)
788 ip->sin6.sin6_addr = in6addr_any;
789 ip->sa.sa_family = AF_INET6;
795 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
797 *port = nla_get_be16(tb[NDA_PORT]);
799 *port = vxlan->cfg.dst_port;
803 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
805 *vni = nla_get_u32(tb[NDA_VNI]);
807 *vni = vxlan->default_dst.remote_vni;
810 if (tb[NDA_IFINDEX]) {
811 struct net_device *tdev;
813 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
815 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
816 tdev = __dev_get_by_index(net, *ifindex);
818 return -EADDRNOTAVAIL;
826 /* Add static entry (via netlink) */
827 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
828 struct net_device *dev,
829 const unsigned char *addr, u16 vid, u16 flags)
831 struct vxlan_dev *vxlan = netdev_priv(dev);
832 /* struct net *net = dev_net(vxlan->dev); */
838 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
839 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
844 if (tb[NDA_DST] == NULL)
847 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
851 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
852 return -EAFNOSUPPORT;
854 spin_lock_bh(&vxlan->hash_lock);
855 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
856 port, vni, ifindex, ndm->ndm_flags);
857 spin_unlock_bh(&vxlan->hash_lock);
862 /* Delete entry (via netlink) */
863 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
864 struct net_device *dev,
865 const unsigned char *addr, u16 vid)
867 struct vxlan_dev *vxlan = netdev_priv(dev);
869 struct vxlan_rdst *rd = NULL;
875 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
881 spin_lock_bh(&vxlan->hash_lock);
882 f = vxlan_find_mac(vxlan, addr);
886 if (!vxlan_addr_any(&ip)) {
887 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
894 /* remove a destination if it's not the only one on the list,
895 * otherwise destroy the fdb entry
897 if (rd && !list_is_singular(&f->remotes)) {
898 list_del_rcu(&rd->list);
899 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
904 vxlan_fdb_destroy(vxlan, f);
907 spin_unlock_bh(&vxlan->hash_lock);
912 /* Dump forwarding table */
913 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
914 struct net_device *dev,
915 struct net_device *filter_dev, int idx)
917 struct vxlan_dev *vxlan = netdev_priv(dev);
920 for (h = 0; h < FDB_HASH_SIZE; ++h) {
925 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
926 struct vxlan_rdst *rd;
928 list_for_each_entry_rcu(rd, &f->remotes, list) {
929 if (idx < cb->args[0])
932 err = vxlan_fdb_info(skb, vxlan, f,
933 NETLINK_CB(cb->skb).portid,
951 /* Watch incoming packets to learn mapping between Ethernet address
952 * and Tunnel endpoint.
953 * Return true if packet is bogus and should be dropped.
955 static bool vxlan_snoop(struct net_device *dev,
956 union vxlan_addr *src_ip, const u8 *src_mac)
958 struct vxlan_dev *vxlan = netdev_priv(dev);
961 f = vxlan_find_mac(vxlan, src_mac);
963 struct vxlan_rdst *rdst = first_remote_rcu(f);
965 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
968 /* Don't migrate static entries, drop packets */
969 if (f->state & (NUD_PERMANENT | NUD_NOARP))
974 "%pM migrated from %pIS to %pIS\n",
975 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
977 rdst->remote_ip = *src_ip;
978 f->updated = jiffies;
979 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
981 /* learned new entry */
982 spin_lock(&vxlan->hash_lock);
984 /* close off race between vxlan_flush and incoming packets */
985 if (netif_running(dev))
986 vxlan_fdb_create(vxlan, src_mac, src_ip,
988 NLM_F_EXCL|NLM_F_CREATE,
990 vxlan->default_dst.remote_vni,
992 spin_unlock(&vxlan->hash_lock);
998 /* See if multicast group is already in use by other ID */
999 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1001 struct vxlan_dev *vxlan;
1002 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1004 /* The vxlan_sock is only used by dev, leaving group has
1005 * no effect on other vxlan devices.
1007 if (family == AF_INET && dev->vn4_sock &&
1008 atomic_read(&dev->vn4_sock->refcnt) == 1)
1010 #if IS_ENABLED(CONFIG_IPV6)
1011 if (family == AF_INET6 && dev->vn6_sock &&
1012 atomic_read(&dev->vn6_sock->refcnt) == 1)
1016 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1017 if (!netif_running(vxlan->dev) || vxlan == dev)
1020 if (family == AF_INET && vxlan->vn4_sock != dev->vn4_sock)
1022 #if IS_ENABLED(CONFIG_IPV6)
1023 if (family == AF_INET6 && vxlan->vn6_sock != dev->vn6_sock)
1027 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1028 &dev->default_dst.remote_ip))
1031 if (vxlan->default_dst.remote_ifindex !=
1032 dev->default_dst.remote_ifindex)
1041 static void __vxlan_sock_release(struct vxlan_sock *vs)
1043 struct vxlan_net *vn;
1047 if (!atomic_dec_and_test(&vs->refcnt))
1050 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1051 spin_lock(&vn->sock_lock);
1052 hlist_del_rcu(&vs->hlist);
1053 vxlan_notify_del_rx_port(vs);
1054 spin_unlock(&vn->sock_lock);
1056 queue_work(vxlan_wq, &vs->del_work);
1059 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1061 vxlan_vs_del_dev(vxlan);
1063 __vxlan_sock_release(vxlan->vn4_sock);
1064 #if IS_ENABLED(CONFIG_IPV6)
1065 __vxlan_sock_release(vxlan->vn6_sock);
1069 /* Update multicast group membership when first VNI on
1070 * multicast address is brought up
1072 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1075 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1076 int ifindex = vxlan->default_dst.remote_ifindex;
1079 if (ip->sa.sa_family == AF_INET) {
1080 struct ip_mreqn mreq = {
1081 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1082 .imr_ifindex = ifindex,
1085 sk = vxlan->vn4_sock->sock->sk;
1087 ret = ip_mc_join_group(sk, &mreq);
1089 #if IS_ENABLED(CONFIG_IPV6)
1091 sk = vxlan->vn6_sock->sock->sk;
1093 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1094 &ip->sin6.sin6_addr);
1102 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1103 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1106 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1107 int ifindex = vxlan->default_dst.remote_ifindex;
1110 if (ip->sa.sa_family == AF_INET) {
1111 struct ip_mreqn mreq = {
1112 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1113 .imr_ifindex = ifindex,
1116 sk = vxlan->vn4_sock->sock->sk;
1118 ret = ip_mc_leave_group(sk, &mreq);
1120 #if IS_ENABLED(CONFIG_IPV6)
1122 sk = vxlan->vn6_sock->sock->sk;
1124 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1125 &ip->sin6.sin6_addr);
1133 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1134 size_t hdrlen, u32 data, bool nopartial)
1136 size_t start, offset, plen;
1138 if (skb->remcsum_offload)
1141 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1142 offset = start + ((data & VXLAN_RCO_UDP) ?
1143 offsetof(struct udphdr, check) :
1144 offsetof(struct tcphdr, check));
1146 plen = hdrlen + offset + sizeof(u16);
1148 if (!pskb_may_pull(skb, plen))
1151 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1153 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1159 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1160 struct vxlan_metadata *md, u32 vni,
1161 struct metadata_dst *tun_dst)
1163 struct iphdr *oip = NULL;
1164 struct ipv6hdr *oip6 = NULL;
1165 struct vxlan_dev *vxlan;
1166 struct pcpu_sw_netstats *stats;
1167 union vxlan_addr saddr;
1170 /* For flow based devices, map all packets to VNI 0 */
1171 if (vs->flags & VXLAN_F_COLLECT_METADATA)
1174 /* Is this VNI defined? */
1175 vxlan = vxlan_vs_find_vni(vs, vni);
1179 skb_reset_mac_header(skb);
1180 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1181 skb->protocol = eth_type_trans(skb, vxlan->dev);
1182 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1184 /* Ignore packet loops (and multicast echo) */
1185 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1188 /* Get data from the outer IP header */
1189 if (vxlan_get_sk_family(vs) == AF_INET) {
1191 saddr.sin.sin_addr.s_addr = oip->saddr;
1192 saddr.sa.sa_family = AF_INET;
1193 #if IS_ENABLED(CONFIG_IPV6)
1195 oip6 = ipv6_hdr(skb);
1196 saddr.sin6.sin6_addr = oip6->saddr;
1197 saddr.sa.sa_family = AF_INET6;
1202 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1206 if ((vxlan->flags & VXLAN_F_LEARN) &&
1207 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1210 skb_reset_network_header(skb);
1211 /* In flow-based mode, GBP is carried in dst_metadata */
1212 if (!(vs->flags & VXLAN_F_COLLECT_METADATA))
1213 skb->mark = md->gbp;
1216 err = IP6_ECN_decapsulate(oip6, skb);
1218 err = IP_ECN_decapsulate(oip, skb);
1220 if (unlikely(err)) {
1221 if (log_ecn_error) {
1223 net_info_ratelimited("non-ECT from %pI6\n",
1226 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1227 &oip->saddr, oip->tos);
1230 ++vxlan->dev->stats.rx_frame_errors;
1231 ++vxlan->dev->stats.rx_errors;
1238 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1240 atomic_long_inc(&vxlan->dev->rx_dropped);
1244 stats = this_cpu_ptr(vxlan->dev->tstats);
1245 u64_stats_update_begin(&stats->syncp);
1246 stats->rx_packets++;
1247 stats->rx_bytes += skb->len;
1248 u64_stats_update_end(&stats->syncp);
1250 gro_cells_receive(&vxlan->gro_cells, skb);
1257 dst_release((struct dst_entry *)tun_dst);
1259 /* Consume bad packet */
1263 /* Callback from net/ipv4/udp.c to receive packets */
1264 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1266 struct metadata_dst *tun_dst = NULL;
1267 struct vxlan_sock *vs;
1268 struct vxlanhdr *vxh;
1270 struct vxlan_metadata _md;
1271 struct vxlan_metadata *md = &_md;
1273 /* Need Vxlan and inner Ethernet header to be present */
1274 if (!pskb_may_pull(skb, VXLAN_HLEN))
1277 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1278 flags = ntohl(vxh->vx_flags);
1279 vni = ntohl(vxh->vx_vni);
1281 if (flags & VXLAN_HF_VNI) {
1282 flags &= ~VXLAN_HF_VNI;
1284 /* VNI flag always required to be set */
1288 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1290 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1292 vs = rcu_dereference_sk_user_data(sk);
1296 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1297 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1298 !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
1302 flags &= ~VXLAN_HF_RCO;
1303 vni &= VXLAN_VNI_MASK;
1306 if (vxlan_collect_metadata(vs)) {
1307 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1308 cpu_to_be64(vni >> 8), sizeof(*md));
1313 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1315 memset(md, 0, sizeof(*md));
1318 /* For backwards compatibility, only allow reserved fields to be
1319 * used by VXLAN extensions if explicitly requested.
1321 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1322 struct vxlanhdr_gbp *gbp;
1324 gbp = (struct vxlanhdr_gbp *)vxh;
1325 md->gbp = ntohs(gbp->policy_id);
1328 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1329 tun_dst->u.tun_info.options_len = sizeof(*md);
1332 if (gbp->dont_learn)
1333 md->gbp |= VXLAN_GBP_DONT_LEARN;
1335 if (gbp->policy_applied)
1336 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1338 flags &= ~VXLAN_GBP_USED_BITS;
1341 if (flags || vni & ~VXLAN_VNI_MASK) {
1342 /* If there are any unprocessed flags remaining treat
1343 * this as a malformed packet. This behavior diverges from
1344 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1345 * in reserved fields are to be ignored. The approach here
1346 * maintains compatibility with previous stack code, and also
1347 * is more robust and provides a little more security in
1348 * adding extensions to VXLAN.
1354 vxlan_rcv(vs, skb, md, vni >> 8, tun_dst);
1358 /* Consume bad packet */
1363 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1364 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1368 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1370 struct vxlan_dev *vxlan = netdev_priv(dev);
1371 struct arphdr *parp;
1374 struct neighbour *n;
1376 if (dev->flags & IFF_NOARP)
1379 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1380 dev->stats.tx_dropped++;
1383 parp = arp_hdr(skb);
1385 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1386 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1387 parp->ar_pro != htons(ETH_P_IP) ||
1388 parp->ar_op != htons(ARPOP_REQUEST) ||
1389 parp->ar_hln != dev->addr_len ||
1392 arpptr = (u8 *)parp + sizeof(struct arphdr);
1394 arpptr += dev->addr_len; /* sha */
1395 memcpy(&sip, arpptr, sizeof(sip));
1396 arpptr += sizeof(sip);
1397 arpptr += dev->addr_len; /* tha */
1398 memcpy(&tip, arpptr, sizeof(tip));
1400 if (ipv4_is_loopback(tip) ||
1401 ipv4_is_multicast(tip))
1404 n = neigh_lookup(&arp_tbl, &tip, dev);
1407 struct vxlan_fdb *f;
1408 struct sk_buff *reply;
1410 if (!(n->nud_state & NUD_CONNECTED)) {
1415 f = vxlan_find_mac(vxlan, n->ha);
1416 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1417 /* bridge-local neighbor */
1422 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1430 skb_reset_mac_header(reply);
1431 __skb_pull(reply, skb_network_offset(reply));
1432 reply->ip_summed = CHECKSUM_UNNECESSARY;
1433 reply->pkt_type = PACKET_HOST;
1435 if (netif_rx_ni(reply) == NET_RX_DROP)
1436 dev->stats.rx_dropped++;
1437 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1438 union vxlan_addr ipa = {
1439 .sin.sin_addr.s_addr = tip,
1440 .sin.sin_family = AF_INET,
1443 vxlan_ip_miss(dev, &ipa);
1447 return NETDEV_TX_OK;
1450 #if IS_ENABLED(CONFIG_IPV6)
1451 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1452 struct neighbour *n, bool isrouter)
1454 struct net_device *dev = request->dev;
1455 struct sk_buff *reply;
1456 struct nd_msg *ns, *na;
1457 struct ipv6hdr *pip6;
1459 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1466 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1467 sizeof(*na) + na_olen + dev->needed_tailroom;
1468 reply = alloc_skb(len, GFP_ATOMIC);
1472 reply->protocol = htons(ETH_P_IPV6);
1474 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1475 skb_push(reply, sizeof(struct ethhdr));
1476 skb_set_mac_header(reply, 0);
1478 ns = (struct nd_msg *)skb_transport_header(request);
1480 daddr = eth_hdr(request)->h_source;
1481 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1482 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1483 if (!ns->opt[i + 1]) {
1487 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1488 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1493 /* Ethernet header */
1494 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1495 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1496 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1497 reply->protocol = htons(ETH_P_IPV6);
1499 skb_pull(reply, sizeof(struct ethhdr));
1500 skb_set_network_header(reply, 0);
1501 skb_put(reply, sizeof(struct ipv6hdr));
1505 pip6 = ipv6_hdr(reply);
1506 memset(pip6, 0, sizeof(struct ipv6hdr));
1508 pip6->priority = ipv6_hdr(request)->priority;
1509 pip6->nexthdr = IPPROTO_ICMPV6;
1510 pip6->hop_limit = 255;
1511 pip6->daddr = ipv6_hdr(request)->saddr;
1512 pip6->saddr = *(struct in6_addr *)n->primary_key;
1514 skb_pull(reply, sizeof(struct ipv6hdr));
1515 skb_set_transport_header(reply, 0);
1517 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1519 /* Neighbor Advertisement */
1520 memset(na, 0, sizeof(*na)+na_olen);
1521 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1522 na->icmph.icmp6_router = isrouter;
1523 na->icmph.icmp6_override = 1;
1524 na->icmph.icmp6_solicited = 1;
1525 na->target = ns->target;
1526 ether_addr_copy(&na->opt[2], n->ha);
1527 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1528 na->opt[1] = na_olen >> 3;
1530 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1531 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1532 csum_partial(na, sizeof(*na)+na_olen, 0));
1534 pip6->payload_len = htons(sizeof(*na)+na_olen);
1536 skb_push(reply, sizeof(struct ipv6hdr));
1538 reply->ip_summed = CHECKSUM_UNNECESSARY;
1543 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1545 struct vxlan_dev *vxlan = netdev_priv(dev);
1547 const struct ipv6hdr *iphdr;
1548 const struct in6_addr *saddr, *daddr;
1549 struct neighbour *n;
1550 struct inet6_dev *in6_dev;
1553 in6_dev = __in6_dev_get(dev);
1557 iphdr = ipv6_hdr(skb);
1558 saddr = &iphdr->saddr;
1559 daddr = &iphdr->daddr;
1561 msg = (struct nd_msg *)skb_transport_header(skb);
1562 if (msg->icmph.icmp6_code != 0 ||
1563 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1566 if (ipv6_addr_loopback(daddr) ||
1567 ipv6_addr_is_multicast(&msg->target))
1570 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1573 struct vxlan_fdb *f;
1574 struct sk_buff *reply;
1576 if (!(n->nud_state & NUD_CONNECTED)) {
1581 f = vxlan_find_mac(vxlan, n->ha);
1582 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1583 /* bridge-local neighbor */
1588 reply = vxlan_na_create(skb, n,
1589 !!(f ? f->flags & NTF_ROUTER : 0));
1596 if (netif_rx_ni(reply) == NET_RX_DROP)
1597 dev->stats.rx_dropped++;
1599 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1600 union vxlan_addr ipa = {
1601 .sin6.sin6_addr = msg->target,
1602 .sin6.sin6_family = AF_INET6,
1605 vxlan_ip_miss(dev, &ipa);
1611 return NETDEV_TX_OK;
1615 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1617 struct vxlan_dev *vxlan = netdev_priv(dev);
1618 struct neighbour *n;
1620 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1624 switch (ntohs(eth_hdr(skb)->h_proto)) {
1629 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1632 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1633 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1634 union vxlan_addr ipa = {
1635 .sin.sin_addr.s_addr = pip->daddr,
1636 .sin.sin_family = AF_INET,
1639 vxlan_ip_miss(dev, &ipa);
1645 #if IS_ENABLED(CONFIG_IPV6)
1648 struct ipv6hdr *pip6;
1650 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1652 pip6 = ipv6_hdr(skb);
1653 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1654 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1655 union vxlan_addr ipa = {
1656 .sin6.sin6_addr = pip6->daddr,
1657 .sin6.sin6_family = AF_INET6,
1660 vxlan_ip_miss(dev, &ipa);
1674 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1676 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1678 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1687 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1688 struct vxlan_metadata *md)
1690 struct vxlanhdr_gbp *gbp;
1695 gbp = (struct vxlanhdr_gbp *)vxh;
1696 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1698 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1699 gbp->dont_learn = 1;
1701 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1702 gbp->policy_applied = 1;
1704 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1707 #if IS_ENABLED(CONFIG_IPV6)
1708 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1709 struct sk_buff *skb,
1710 struct net_device *dev, struct in6_addr *saddr,
1711 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1712 __be16 src_port, __be16 dst_port, __be32 vni,
1713 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1715 struct vxlanhdr *vxh;
1718 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1719 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1720 u16 hdrlen = sizeof(struct vxlanhdr);
1722 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1723 skb->ip_summed == CHECKSUM_PARTIAL) {
1724 int csum_start = skb_checksum_start_offset(skb);
1726 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1727 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1728 (skb->csum_offset == offsetof(struct udphdr, check) ||
1729 skb->csum_offset == offsetof(struct tcphdr, check))) {
1731 type |= SKB_GSO_TUNNEL_REMCSUM;
1735 skb_scrub_packet(skb, xnet);
1737 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1738 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1739 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1741 /* Need space for new headers (invalidates iph ptr) */
1742 err = skb_cow_head(skb, min_headroom);
1743 if (unlikely(err)) {
1748 skb = vlan_hwaccel_push_inside(skb);
1749 if (WARN_ON(!skb)) {
1754 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1760 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1761 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1764 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1765 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1768 if (skb->csum_offset == offsetof(struct udphdr, check))
1769 data |= VXLAN_RCO_UDP;
1771 vxh->vx_vni |= htonl(data);
1772 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1774 if (!skb_is_gso(skb)) {
1775 skb->ip_summed = CHECKSUM_NONE;
1776 skb->encapsulation = 0;
1780 if (vxflags & VXLAN_F_GBP)
1781 vxlan_build_gbp_hdr(vxh, vxflags, md);
1783 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1785 udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
1786 ttl, src_port, dst_port,
1787 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1795 static int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
1796 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1797 __be16 src_port, __be16 dst_port, __be32 vni,
1798 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1800 struct vxlanhdr *vxh;
1803 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1804 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1805 u16 hdrlen = sizeof(struct vxlanhdr);
1807 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1808 skb->ip_summed == CHECKSUM_PARTIAL) {
1809 int csum_start = skb_checksum_start_offset(skb);
1811 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1812 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1813 (skb->csum_offset == offsetof(struct udphdr, check) ||
1814 skb->csum_offset == offsetof(struct tcphdr, check))) {
1816 type |= SKB_GSO_TUNNEL_REMCSUM;
1820 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1821 + VXLAN_HLEN + sizeof(struct iphdr)
1822 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1824 /* Need space for new headers (invalidates iph ptr) */
1825 err = skb_cow_head(skb, min_headroom);
1826 if (unlikely(err)) {
1831 skb = vlan_hwaccel_push_inside(skb);
1835 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1837 return PTR_ERR(skb);
1839 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1840 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1843 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1844 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1847 if (skb->csum_offset == offsetof(struct udphdr, check))
1848 data |= VXLAN_RCO_UDP;
1850 vxh->vx_vni |= htonl(data);
1851 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1853 if (!skb_is_gso(skb)) {
1854 skb->ip_summed = CHECKSUM_NONE;
1855 skb->encapsulation = 0;
1859 if (vxflags & VXLAN_F_GBP)
1860 vxlan_build_gbp_hdr(vxh, vxflags, md);
1862 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1864 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
1865 ttl, df, src_port, dst_port, xnet,
1866 !(vxflags & VXLAN_F_UDP_CSUM));
1869 #if IS_ENABLED(CONFIG_IPV6)
1870 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
1871 struct sk_buff *skb, int oif,
1872 const struct in6_addr *daddr,
1873 struct in6_addr *saddr)
1875 struct dst_entry *ndst;
1878 memset(&fl6, 0, sizeof(fl6));
1879 fl6.flowi6_oif = oif;
1881 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
1882 fl6.flowi6_mark = skb->mark;
1883 fl6.flowi6_proto = IPPROTO_UDP;
1885 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, vxlan->vn6_sock->sock->sk,
1887 if (unlikely(IS_ERR(ndst)))
1888 return ERR_PTR(-ENETUNREACH);
1895 /* Bypass encapsulation if the destination is local */
1896 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1897 struct vxlan_dev *dst_vxlan)
1899 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1900 union vxlan_addr loopback;
1901 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1902 struct net_device *dev;
1905 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1906 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1907 skb->pkt_type = PACKET_HOST;
1908 skb->encapsulation = 0;
1909 skb->dev = dst_vxlan->dev;
1910 __skb_pull(skb, skb_network_offset(skb));
1912 if (remote_ip->sa.sa_family == AF_INET) {
1913 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1914 loopback.sa.sa_family = AF_INET;
1915 #if IS_ENABLED(CONFIG_IPV6)
1917 loopback.sin6.sin6_addr = in6addr_loopback;
1918 loopback.sa.sa_family = AF_INET6;
1924 if (unlikely(!(dev->flags & IFF_UP))) {
1929 if (dst_vxlan->flags & VXLAN_F_LEARN)
1930 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source);
1932 u64_stats_update_begin(&tx_stats->syncp);
1933 tx_stats->tx_packets++;
1934 tx_stats->tx_bytes += len;
1935 u64_stats_update_end(&tx_stats->syncp);
1937 if (netif_rx(skb) == NET_RX_SUCCESS) {
1938 u64_stats_update_begin(&rx_stats->syncp);
1939 rx_stats->rx_packets++;
1940 rx_stats->rx_bytes += len;
1941 u64_stats_update_end(&rx_stats->syncp);
1944 dev->stats.rx_dropped++;
1949 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1950 struct vxlan_rdst *rdst, bool did_rsc)
1952 struct ip_tunnel_info *info;
1953 struct vxlan_dev *vxlan = netdev_priv(dev);
1955 struct rtable *rt = NULL;
1956 const struct iphdr *old_iph;
1958 union vxlan_addr *dst;
1959 union vxlan_addr remote_ip;
1960 struct vxlan_metadata _md;
1961 struct vxlan_metadata *md = &_md;
1962 __be16 src_port = 0, dst_port;
1967 u32 flags = vxlan->flags;
1969 info = skb_tunnel_info(skb);
1972 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1973 vni = rdst->remote_vni;
1974 dst = &rdst->remote_ip;
1977 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1981 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
1982 vni = be64_to_cpu(info->key.tun_id);
1983 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
1984 if (remote_ip.sa.sa_family == AF_INET)
1985 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1987 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
1991 if (vxlan_addr_any(dst)) {
1993 /* short-circuited back to local bridge */
1994 vxlan_encap_bypass(skb, vxlan, vxlan);
2000 old_iph = ip_hdr(skb);
2002 ttl = vxlan->cfg.ttl;
2003 if (!ttl && vxlan_addr_multicast(dst))
2006 tos = vxlan->cfg.tos;
2008 tos = ip_tunnel_get_dsfield(old_iph, skb);
2010 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2011 vxlan->cfg.port_max, true);
2014 ttl = info->key.ttl;
2015 tos = info->key.tos;
2017 if (info->options_len) {
2018 if (info->options_len < sizeof(*md))
2020 md = ip_tunnel_info_opts(info);
2023 md->gbp = skb->mark;
2026 if (dst->sa.sa_family == AF_INET) {
2027 if (!vxlan->vn4_sock)
2029 sk = vxlan->vn4_sock->sock->sk;
2032 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2035 if (info->key.tun_flags & TUNNEL_CSUM)
2036 flags |= VXLAN_F_UDP_CSUM;
2038 flags &= ~VXLAN_F_UDP_CSUM;
2041 memset(&fl4, 0, sizeof(fl4));
2042 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
2043 fl4.flowi4_tos = RT_TOS(tos);
2044 fl4.flowi4_mark = skb->mark;
2045 fl4.flowi4_proto = IPPROTO_UDP;
2046 fl4.daddr = dst->sin.sin_addr.s_addr;
2047 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
2049 rt = ip_route_output_key(vxlan->net, &fl4);
2051 netdev_dbg(dev, "no route to %pI4\n",
2052 &dst->sin.sin_addr.s_addr);
2053 dev->stats.tx_carrier_errors++;
2057 if (rt->dst.dev == dev) {
2058 netdev_dbg(dev, "circular route to %pI4\n",
2059 &dst->sin.sin_addr.s_addr);
2060 dev->stats.collisions++;
2064 /* Bypass encapsulation if the destination is local */
2065 if (!info && rt->rt_flags & RTCF_LOCAL &&
2066 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2067 struct vxlan_dev *dst_vxlan;
2070 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2071 dst->sa.sa_family, dst_port,
2075 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2079 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2080 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2081 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
2082 dst->sin.sin_addr.s_addr, tos, ttl, df,
2083 src_port, dst_port, htonl(vni << 8), md,
2084 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2087 /* skb is already freed. */
2092 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
2093 #if IS_ENABLED(CONFIG_IPV6)
2095 struct dst_entry *ndst;
2096 struct in6_addr saddr;
2099 if (!vxlan->vn6_sock)
2101 sk = vxlan->vn6_sock->sock->sk;
2103 ndst = vxlan6_get_route(vxlan, skb,
2104 rdst ? rdst->remote_ifindex : 0,
2105 &dst->sin6.sin6_addr, &saddr);
2107 netdev_dbg(dev, "no route to %pI6\n",
2108 &dst->sin6.sin6_addr);
2109 dev->stats.tx_carrier_errors++;
2113 if (ndst->dev == dev) {
2114 netdev_dbg(dev, "circular route to %pI6\n",
2115 &dst->sin6.sin6_addr);
2117 dev->stats.collisions++;
2121 /* Bypass encapsulation if the destination is local */
2122 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2123 if (!info && rt6i_flags & RTF_LOCAL &&
2124 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2125 struct vxlan_dev *dst_vxlan;
2128 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2129 dst->sa.sa_family, dst_port,
2133 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2138 if (info->key.tun_flags & TUNNEL_CSUM)
2139 flags &= ~VXLAN_F_UDP_ZERO_CSUM6_TX;
2141 flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2144 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2145 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &saddr, &dst->sin6.sin6_addr,
2146 0, ttl, src_port, dst_port, htonl(vni << 8), md,
2147 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2155 dev->stats.tx_dropped++;
2161 dev->stats.tx_errors++;
2166 /* Transmit local packets over Vxlan
2168 * Outer IP header inherits ECN and DF from inner header.
2169 * Outer UDP destination is the VXLAN assigned port.
2170 * source port is based on hash of flow
2172 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2174 struct vxlan_dev *vxlan = netdev_priv(dev);
2175 const struct ip_tunnel_info *info;
2177 bool did_rsc = false;
2178 struct vxlan_rdst *rdst, *fdst = NULL;
2179 struct vxlan_fdb *f;
2181 info = skb_tunnel_info(skb);
2183 skb_reset_mac_header(skb);
2186 if ((vxlan->flags & VXLAN_F_PROXY)) {
2187 if (ntohs(eth->h_proto) == ETH_P_ARP)
2188 return arp_reduce(dev, skb);
2189 #if IS_ENABLED(CONFIG_IPV6)
2190 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2191 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2192 + sizeof(struct nd_msg)) &&
2193 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2196 msg = (struct nd_msg *)skb_transport_header(skb);
2197 if (msg->icmph.icmp6_code == 0 &&
2198 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2199 return neigh_reduce(dev, skb);
2205 if (vxlan->flags & VXLAN_F_COLLECT_METADATA &&
2206 info && info->mode & IP_TUNNEL_INFO_TX) {
2207 vxlan_xmit_one(skb, dev, NULL, false);
2208 return NETDEV_TX_OK;
2211 f = vxlan_find_mac(vxlan, eth->h_dest);
2214 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2215 (ntohs(eth->h_proto) == ETH_P_IP ||
2216 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2217 did_rsc = route_shortcircuit(dev, skb);
2219 f = vxlan_find_mac(vxlan, eth->h_dest);
2223 f = vxlan_find_mac(vxlan, all_zeros_mac);
2225 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2226 !is_multicast_ether_addr(eth->h_dest))
2227 vxlan_fdb_miss(vxlan, eth->h_dest);
2229 dev->stats.tx_dropped++;
2231 return NETDEV_TX_OK;
2235 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2236 struct sk_buff *skb1;
2242 skb1 = skb_clone(skb, GFP_ATOMIC);
2244 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2248 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2251 return NETDEV_TX_OK;
2254 /* Walk the forwarding table and purge stale entries */
2255 static void vxlan_cleanup(unsigned long arg)
2257 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2258 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2261 if (!netif_running(vxlan->dev))
2264 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2265 struct hlist_node *p, *n;
2267 spin_lock_bh(&vxlan->hash_lock);
2268 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2270 = container_of(p, struct vxlan_fdb, hlist);
2271 unsigned long timeout;
2273 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2276 timeout = f->used + vxlan->cfg.age_interval * HZ;
2277 if (time_before_eq(timeout, jiffies)) {
2278 netdev_dbg(vxlan->dev,
2279 "garbage collect %pM\n",
2281 f->state = NUD_STALE;
2282 vxlan_fdb_destroy(vxlan, f);
2283 } else if (time_before(timeout, next_timer))
2284 next_timer = timeout;
2286 spin_unlock_bh(&vxlan->hash_lock);
2289 mod_timer(&vxlan->age_timer, next_timer);
2292 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2294 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2296 spin_lock(&vn->sock_lock);
2297 hlist_del_init_rcu(&vxlan->hlist);
2298 spin_unlock(&vn->sock_lock);
2301 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2303 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2304 __u32 vni = vxlan->default_dst.remote_vni;
2306 spin_lock(&vn->sock_lock);
2307 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2308 spin_unlock(&vn->sock_lock);
2311 /* Setup stats when device is created */
2312 static int vxlan_init(struct net_device *dev)
2314 struct vxlan_dev *vxlan = netdev_priv(dev);
2317 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2321 err = gro_cells_init(&vxlan->gro_cells, dev);
2323 free_percpu(dev->tstats);
2330 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2332 struct vxlan_fdb *f;
2334 spin_lock_bh(&vxlan->hash_lock);
2335 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2337 vxlan_fdb_destroy(vxlan, f);
2338 spin_unlock_bh(&vxlan->hash_lock);
2341 static void vxlan_uninit(struct net_device *dev)
2343 struct vxlan_dev *vxlan = netdev_priv(dev);
2345 gro_cells_destroy(&vxlan->gro_cells);
2347 vxlan_fdb_delete_default(vxlan);
2349 free_percpu(dev->tstats);
2352 /* Start ageing timer and join group when device is brought up */
2353 static int vxlan_open(struct net_device *dev)
2355 struct vxlan_dev *vxlan = netdev_priv(dev);
2358 ret = vxlan_sock_add(vxlan);
2362 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2363 ret = vxlan_igmp_join(vxlan);
2364 if (ret == -EADDRINUSE)
2367 vxlan_sock_release(vxlan);
2372 if (vxlan->cfg.age_interval)
2373 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2378 /* Purge the forwarding table */
2379 static void vxlan_flush(struct vxlan_dev *vxlan)
2383 spin_lock_bh(&vxlan->hash_lock);
2384 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2385 struct hlist_node *p, *n;
2386 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2388 = container_of(p, struct vxlan_fdb, hlist);
2389 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2390 if (!is_zero_ether_addr(f->eth_addr))
2391 vxlan_fdb_destroy(vxlan, f);
2394 spin_unlock_bh(&vxlan->hash_lock);
2397 /* Cleanup timer and forwarding table on shutdown */
2398 static int vxlan_stop(struct net_device *dev)
2400 struct vxlan_dev *vxlan = netdev_priv(dev);
2401 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2404 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2405 !vxlan_group_used(vn, vxlan))
2406 ret = vxlan_igmp_leave(vxlan);
2408 del_timer_sync(&vxlan->age_timer);
2411 vxlan_sock_release(vxlan);
2416 /* Stub, nothing needs to be done. */
2417 static void vxlan_set_multicast_list(struct net_device *dev)
2421 static int __vxlan_change_mtu(struct net_device *dev,
2422 struct net_device *lowerdev,
2423 struct vxlan_rdst *dst, int new_mtu, bool strict)
2425 int max_mtu = IP_MAX_MTU;
2428 max_mtu = lowerdev->mtu;
2430 if (dst->remote_ip.sa.sa_family == AF_INET6)
2431 max_mtu -= VXLAN6_HEADROOM;
2433 max_mtu -= VXLAN_HEADROOM;
2438 if (new_mtu > max_mtu) {
2449 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2451 struct vxlan_dev *vxlan = netdev_priv(dev);
2452 struct vxlan_rdst *dst = &vxlan->default_dst;
2453 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2454 dst->remote_ifindex);
2455 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
2458 static int egress_ipv4_tun_info(struct net_device *dev, struct sk_buff *skb,
2459 struct ip_tunnel_info *info,
2460 __be16 sport, __be16 dport)
2462 struct vxlan_dev *vxlan = netdev_priv(dev);
2466 memset(&fl4, 0, sizeof(fl4));
2467 fl4.flowi4_tos = RT_TOS(info->key.tos);
2468 fl4.flowi4_mark = skb->mark;
2469 fl4.flowi4_proto = IPPROTO_UDP;
2470 fl4.daddr = info->key.u.ipv4.dst;
2472 rt = ip_route_output_key(vxlan->net, &fl4);
2477 info->key.u.ipv4.src = fl4.saddr;
2478 info->key.tp_src = sport;
2479 info->key.tp_dst = dport;
2483 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2485 struct vxlan_dev *vxlan = netdev_priv(dev);
2486 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2487 __be16 sport, dport;
2489 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2490 vxlan->cfg.port_max, true);
2491 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2493 if (ip_tunnel_info_af(info) == AF_INET) {
2494 if (!vxlan->vn4_sock)
2496 return egress_ipv4_tun_info(dev, skb, info, sport, dport);
2498 #if IS_ENABLED(CONFIG_IPV6)
2499 struct dst_entry *ndst;
2501 if (!vxlan->vn6_sock)
2503 ndst = vxlan6_get_route(vxlan, skb, 0,
2504 &info->key.u.ipv6.dst,
2505 &info->key.u.ipv6.src);
2507 return PTR_ERR(ndst);
2510 info->key.tp_src = sport;
2511 info->key.tp_dst = dport;
2512 #else /* !CONFIG_IPV6 */
2513 return -EPFNOSUPPORT;
2519 static const struct net_device_ops vxlan_netdev_ops = {
2520 .ndo_init = vxlan_init,
2521 .ndo_uninit = vxlan_uninit,
2522 .ndo_open = vxlan_open,
2523 .ndo_stop = vxlan_stop,
2524 .ndo_start_xmit = vxlan_xmit,
2525 .ndo_get_stats64 = ip_tunnel_get_stats64,
2526 .ndo_set_rx_mode = vxlan_set_multicast_list,
2527 .ndo_change_mtu = vxlan_change_mtu,
2528 .ndo_validate_addr = eth_validate_addr,
2529 .ndo_set_mac_address = eth_mac_addr,
2530 .ndo_fdb_add = vxlan_fdb_add,
2531 .ndo_fdb_del = vxlan_fdb_delete,
2532 .ndo_fdb_dump = vxlan_fdb_dump,
2533 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2536 /* Info for udev, that this is a virtual tunnel endpoint */
2537 static struct device_type vxlan_type = {
2541 /* Calls the ndo_add_vxlan_port of the caller in order to
2542 * supply the listening VXLAN udp ports. Callers are expected
2543 * to implement the ndo_add_vxlan_port.
2545 void vxlan_get_rx_port(struct net_device *dev)
2547 struct vxlan_sock *vs;
2548 struct net *net = dev_net(dev);
2549 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2550 sa_family_t sa_family;
2554 spin_lock(&vn->sock_lock);
2555 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2556 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2557 port = inet_sk(vs->sock->sk)->inet_sport;
2558 sa_family = vxlan_get_sk_family(vs);
2559 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2563 spin_unlock(&vn->sock_lock);
2565 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2567 /* Initialize the device structure. */
2568 static void vxlan_setup(struct net_device *dev)
2570 struct vxlan_dev *vxlan = netdev_priv(dev);
2573 eth_hw_addr_random(dev);
2576 dev->netdev_ops = &vxlan_netdev_ops;
2577 dev->destructor = free_netdev;
2578 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2580 dev->features |= NETIF_F_LLTX;
2581 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2582 dev->features |= NETIF_F_RXCSUM;
2583 dev->features |= NETIF_F_GSO_SOFTWARE;
2585 dev->vlan_features = dev->features;
2586 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2587 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2588 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2589 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2590 netif_keep_dst(dev);
2591 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
2593 INIT_LIST_HEAD(&vxlan->next);
2594 spin_lock_init(&vxlan->hash_lock);
2596 init_timer_deferrable(&vxlan->age_timer);
2597 vxlan->age_timer.function = vxlan_cleanup;
2598 vxlan->age_timer.data = (unsigned long) vxlan;
2600 vxlan->cfg.dst_port = htons(vxlan_port);
2604 for (h = 0; h < FDB_HASH_SIZE; ++h)
2605 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2608 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2609 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2610 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2611 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2612 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2613 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2614 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2615 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2616 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2617 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2618 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2619 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2620 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2621 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2622 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2623 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2624 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2625 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
2626 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2627 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2628 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2629 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2630 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2631 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2632 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2633 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
2636 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2638 if (tb[IFLA_ADDRESS]) {
2639 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2640 pr_debug("invalid link address (not ethernet)\n");
2644 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2645 pr_debug("invalid all zero ethernet address\n");
2646 return -EADDRNOTAVAIL;
2653 if (data[IFLA_VXLAN_ID]) {
2654 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2655 if (id >= VXLAN_N_VID)
2659 if (data[IFLA_VXLAN_PORT_RANGE]) {
2660 const struct ifla_vxlan_port_range *p
2661 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2663 if (ntohs(p->high) < ntohs(p->low)) {
2664 pr_debug("port range %u .. %u not valid\n",
2665 ntohs(p->low), ntohs(p->high));
2673 static void vxlan_get_drvinfo(struct net_device *netdev,
2674 struct ethtool_drvinfo *drvinfo)
2676 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2677 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2680 static const struct ethtool_ops vxlan_ethtool_ops = {
2681 .get_drvinfo = vxlan_get_drvinfo,
2682 .get_link = ethtool_op_get_link,
2685 static void vxlan_del_work(struct work_struct *work)
2687 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2688 udp_tunnel_sock_release(vs->sock);
2692 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2693 __be16 port, u32 flags)
2695 struct socket *sock;
2696 struct udp_port_cfg udp_conf;
2699 memset(&udp_conf, 0, sizeof(udp_conf));
2702 udp_conf.family = AF_INET6;
2703 udp_conf.use_udp6_rx_checksums =
2704 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2705 udp_conf.ipv6_v6only = 1;
2707 udp_conf.family = AF_INET;
2710 udp_conf.local_udp_port = port;
2712 /* Open UDP socket */
2713 err = udp_sock_create(net, &udp_conf, &sock);
2715 return ERR_PTR(err);
2720 /* Create new listen socket if needed */
2721 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2722 __be16 port, u32 flags)
2724 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2725 struct vxlan_sock *vs;
2726 struct socket *sock;
2728 struct udp_tunnel_sock_cfg tunnel_cfg;
2730 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2732 return ERR_PTR(-ENOMEM);
2734 for (h = 0; h < VNI_HASH_SIZE; ++h)
2735 INIT_HLIST_HEAD(&vs->vni_list[h]);
2737 INIT_WORK(&vs->del_work, vxlan_del_work);
2739 sock = vxlan_create_sock(net, ipv6, port, flags);
2741 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2744 return ERR_CAST(sock);
2748 atomic_set(&vs->refcnt, 1);
2749 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2751 /* Initialize the vxlan udp offloads structure */
2752 vs->udp_offloads.port = port;
2753 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2754 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2756 spin_lock(&vn->sock_lock);
2757 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2758 vxlan_notify_add_rx_port(vs);
2759 spin_unlock(&vn->sock_lock);
2761 /* Mark socket as an encapsulation socket. */
2762 tunnel_cfg.sk_user_data = vs;
2763 tunnel_cfg.encap_type = 1;
2764 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2765 tunnel_cfg.encap_destroy = NULL;
2767 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2772 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
2774 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2775 struct vxlan_sock *vs = NULL;
2777 if (!vxlan->cfg.no_share) {
2778 spin_lock(&vn->sock_lock);
2779 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2780 vxlan->cfg.dst_port, vxlan->flags);
2781 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
2782 spin_unlock(&vn->sock_lock);
2785 spin_unlock(&vn->sock_lock);
2788 vs = vxlan_socket_create(vxlan->net, ipv6,
2789 vxlan->cfg.dst_port, vxlan->flags);
2792 #if IS_ENABLED(CONFIG_IPV6)
2794 vxlan->vn6_sock = vs;
2797 vxlan->vn4_sock = vs;
2798 vxlan_vs_add_dev(vs, vxlan);
2802 static int vxlan_sock_add(struct vxlan_dev *vxlan)
2804 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2805 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2808 vxlan->vn4_sock = NULL;
2809 #if IS_ENABLED(CONFIG_IPV6)
2810 vxlan->vn6_sock = NULL;
2811 if (ipv6 || metadata)
2812 ret = __vxlan_sock_add(vxlan, true);
2814 if (!ret && (!ipv6 || metadata))
2815 ret = __vxlan_sock_add(vxlan, false);
2817 vxlan_sock_release(vxlan);
2821 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2822 struct vxlan_config *conf)
2824 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2825 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
2826 struct vxlan_rdst *dst = &vxlan->default_dst;
2827 unsigned short needed_headroom = ETH_HLEN;
2829 bool use_ipv6 = false;
2830 __be16 default_port = vxlan->cfg.dst_port;
2831 struct net_device *lowerdev = NULL;
2833 vxlan->net = src_net;
2835 dst->remote_vni = conf->vni;
2837 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
2839 /* Unless IPv6 is explicitly requested, assume IPv4 */
2840 if (!dst->remote_ip.sa.sa_family)
2841 dst->remote_ip.sa.sa_family = AF_INET;
2843 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
2844 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2845 if (!IS_ENABLED(CONFIG_IPV6))
2846 return -EPFNOSUPPORT;
2848 vxlan->flags |= VXLAN_F_IPV6;
2851 if (conf->remote_ifindex) {
2852 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2853 dst->remote_ifindex = conf->remote_ifindex;
2856 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2860 #if IS_ENABLED(CONFIG_IPV6)
2862 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2863 if (idev && idev->cnf.disable_ipv6) {
2864 pr_info("IPv6 is disabled via sysctl\n");
2871 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2873 needed_headroom = lowerdev->hard_header_len;
2877 dev->gso_max_size = lowerdev->gso_max_size;
2878 dev->gso_max_segs = lowerdev->gso_max_segs;
2882 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2887 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2888 needed_headroom += VXLAN6_HEADROOM;
2890 needed_headroom += VXLAN_HEADROOM;
2891 dev->needed_headroom = needed_headroom;
2893 memcpy(&vxlan->cfg, conf, sizeof(*conf));
2894 if (!vxlan->cfg.dst_port)
2895 vxlan->cfg.dst_port = default_port;
2896 vxlan->flags |= conf->flags;
2898 if (!vxlan->cfg.age_interval)
2899 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
2901 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2902 if (tmp->cfg.vni == conf->vni &&
2903 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2904 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2905 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2906 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2907 (vxlan->flags & VXLAN_F_RCV_FLAGS))
2911 dev->ethtool_ops = &vxlan_ethtool_ops;
2913 /* create an fdb entry for a valid default destination */
2914 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2915 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2916 &vxlan->default_dst.remote_ip,
2917 NUD_REACHABLE|NUD_PERMANENT,
2918 NLM_F_EXCL|NLM_F_CREATE,
2919 vxlan->cfg.dst_port,
2920 vxlan->default_dst.remote_vni,
2921 vxlan->default_dst.remote_ifindex,
2927 err = register_netdevice(dev);
2929 vxlan_fdb_delete_default(vxlan);
2933 list_add(&vxlan->next, &vn->vxlan_list);
2938 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2939 struct nlattr *tb[], struct nlattr *data[])
2941 struct vxlan_config conf;
2944 memset(&conf, 0, sizeof(conf));
2946 if (data[IFLA_VXLAN_ID])
2947 conf.vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2949 if (data[IFLA_VXLAN_GROUP]) {
2950 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2951 } else if (data[IFLA_VXLAN_GROUP6]) {
2952 if (!IS_ENABLED(CONFIG_IPV6))
2953 return -EPFNOSUPPORT;
2955 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2956 conf.remote_ip.sa.sa_family = AF_INET6;
2959 if (data[IFLA_VXLAN_LOCAL]) {
2960 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2961 conf.saddr.sa.sa_family = AF_INET;
2962 } else if (data[IFLA_VXLAN_LOCAL6]) {
2963 if (!IS_ENABLED(CONFIG_IPV6))
2964 return -EPFNOSUPPORT;
2966 /* TODO: respect scope id */
2967 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2968 conf.saddr.sa.sa_family = AF_INET6;
2971 if (data[IFLA_VXLAN_LINK])
2972 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
2974 if (data[IFLA_VXLAN_TOS])
2975 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2977 if (data[IFLA_VXLAN_TTL])
2978 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2980 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2981 conf.flags |= VXLAN_F_LEARN;
2983 if (data[IFLA_VXLAN_AGEING])
2984 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2986 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2987 conf.flags |= VXLAN_F_PROXY;
2989 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2990 conf.flags |= VXLAN_F_RSC;
2992 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2993 conf.flags |= VXLAN_F_L2MISS;
2995 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2996 conf.flags |= VXLAN_F_L3MISS;
2998 if (data[IFLA_VXLAN_LIMIT])
2999 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3001 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3002 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3003 conf.flags |= VXLAN_F_COLLECT_METADATA;
3005 if (data[IFLA_VXLAN_PORT_RANGE]) {
3006 const struct ifla_vxlan_port_range *p
3007 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3008 conf.port_min = ntohs(p->low);
3009 conf.port_max = ntohs(p->high);
3012 if (data[IFLA_VXLAN_PORT])
3013 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3015 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3016 conf.flags |= VXLAN_F_UDP_CSUM;
3018 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3019 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3020 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3022 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3023 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3024 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3026 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3027 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3028 conf.flags |= VXLAN_F_REMCSUM_TX;
3030 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3031 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3032 conf.flags |= VXLAN_F_REMCSUM_RX;
3034 if (data[IFLA_VXLAN_GBP])
3035 conf.flags |= VXLAN_F_GBP;
3037 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3038 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3041 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3043 err = vxlan_dev_configure(src_net, dev, &conf);
3046 pr_info("ifindex %d does not exist\n", conf.remote_ifindex);
3050 pr_info("IPv6 is disabled via sysctl\n");
3054 pr_info("duplicate VNI %u\n", conf.vni);
3061 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3063 struct vxlan_dev *vxlan = netdev_priv(dev);
3065 list_del(&vxlan->next);
3066 unregister_netdevice_queue(dev, head);
3069 static size_t vxlan_get_size(const struct net_device *dev)
3072 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
3073 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3074 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3075 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3076 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3077 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
3078 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
3079 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3080 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3081 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3082 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
3083 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
3084 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3085 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3086 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3087 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3088 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3089 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3090 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3091 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3092 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3096 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3098 const struct vxlan_dev *vxlan = netdev_priv(dev);
3099 const struct vxlan_rdst *dst = &vxlan->default_dst;
3100 struct ifla_vxlan_port_range ports = {
3101 .low = htons(vxlan->cfg.port_min),
3102 .high = htons(vxlan->cfg.port_max),
3105 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
3106 goto nla_put_failure;
3108 if (!vxlan_addr_any(&dst->remote_ip)) {
3109 if (dst->remote_ip.sa.sa_family == AF_INET) {
3110 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3111 dst->remote_ip.sin.sin_addr.s_addr))
3112 goto nla_put_failure;
3113 #if IS_ENABLED(CONFIG_IPV6)
3115 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3116 &dst->remote_ip.sin6.sin6_addr))
3117 goto nla_put_failure;
3122 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
3123 goto nla_put_failure;
3125 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3126 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
3127 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
3128 vxlan->cfg.saddr.sin.sin_addr.s_addr))
3129 goto nla_put_failure;
3130 #if IS_ENABLED(CONFIG_IPV6)
3132 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
3133 &vxlan->cfg.saddr.sin6.sin6_addr))
3134 goto nla_put_failure;
3139 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3140 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
3141 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3142 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3143 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3144 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3145 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3146 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3147 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3148 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3149 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
3150 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3151 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
3152 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3153 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3154 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
3155 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
3156 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
3157 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3158 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3159 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3160 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3161 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3162 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3163 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3164 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
3165 goto nla_put_failure;
3167 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3168 goto nla_put_failure;
3170 if (vxlan->flags & VXLAN_F_GBP &&
3171 nla_put_flag(skb, IFLA_VXLAN_GBP))
3172 goto nla_put_failure;
3174 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3175 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3176 goto nla_put_failure;
3184 static struct net *vxlan_get_link_net(const struct net_device *dev)
3186 struct vxlan_dev *vxlan = netdev_priv(dev);
3191 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3193 .maxtype = IFLA_VXLAN_MAX,
3194 .policy = vxlan_policy,
3195 .priv_size = sizeof(struct vxlan_dev),
3196 .setup = vxlan_setup,
3197 .validate = vxlan_validate,
3198 .newlink = vxlan_newlink,
3199 .dellink = vxlan_dellink,
3200 .get_size = vxlan_get_size,
3201 .fill_info = vxlan_fill_info,
3202 .get_link_net = vxlan_get_link_net,
3205 struct net_device *vxlan_dev_create(struct net *net, const char *name,
3206 u8 name_assign_type,
3207 struct vxlan_config *conf)
3209 struct nlattr *tb[IFLA_MAX + 1];
3210 struct net_device *dev;
3213 memset(&tb, 0, sizeof(tb));
3215 dev = rtnl_create_link(net, name, name_assign_type,
3216 &vxlan_link_ops, tb);
3220 err = vxlan_dev_configure(net, dev, conf);
3223 return ERR_PTR(err);
3226 err = rtnl_configure_link(dev, NULL);
3228 LIST_HEAD(list_kill);
3230 vxlan_dellink(dev, &list_kill);
3231 unregister_netdevice_many(&list_kill);
3232 return ERR_PTR(err);
3237 EXPORT_SYMBOL_GPL(vxlan_dev_create);
3239 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3240 struct net_device *dev)
3242 struct vxlan_dev *vxlan, *next;
3243 LIST_HEAD(list_kill);
3245 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3246 struct vxlan_rdst *dst = &vxlan->default_dst;
3248 /* In case we created vxlan device with carrier
3249 * and we loose the carrier due to module unload
3250 * we also need to remove vxlan device. In other
3251 * cases, it's not necessary and remote_ifindex
3252 * is 0 here, so no matches.
3254 if (dst->remote_ifindex == dev->ifindex)
3255 vxlan_dellink(vxlan->dev, &list_kill);
3258 unregister_netdevice_many(&list_kill);
3261 static int vxlan_lowerdev_event(struct notifier_block *unused,
3262 unsigned long event, void *ptr)
3264 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3265 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3267 if (event == NETDEV_UNREGISTER)
3268 vxlan_handle_lowerdev_unregister(vn, dev);
3273 static struct notifier_block vxlan_notifier_block __read_mostly = {
3274 .notifier_call = vxlan_lowerdev_event,
3277 static __net_init int vxlan_init_net(struct net *net)
3279 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3282 INIT_LIST_HEAD(&vn->vxlan_list);
3283 spin_lock_init(&vn->sock_lock);
3285 for (h = 0; h < PORT_HASH_SIZE; ++h)
3286 INIT_HLIST_HEAD(&vn->sock_list[h]);
3291 static void __net_exit vxlan_exit_net(struct net *net)
3293 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3294 struct vxlan_dev *vxlan, *next;
3295 struct net_device *dev, *aux;
3299 for_each_netdev_safe(net, dev, aux)
3300 if (dev->rtnl_link_ops == &vxlan_link_ops)
3301 unregister_netdevice_queue(dev, &list);
3303 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3304 /* If vxlan->dev is in the same netns, it has already been added
3305 * to the list by the previous loop.
3307 if (!net_eq(dev_net(vxlan->dev), net))
3308 unregister_netdevice_queue(vxlan->dev, &list);
3311 unregister_netdevice_many(&list);
3315 static struct pernet_operations vxlan_net_ops = {
3316 .init = vxlan_init_net,
3317 .exit = vxlan_exit_net,
3318 .id = &vxlan_net_id,
3319 .size = sizeof(struct vxlan_net),
3322 static int __init vxlan_init_module(void)
3326 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3330 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3332 rc = register_pernet_subsys(&vxlan_net_ops);
3336 rc = register_netdevice_notifier(&vxlan_notifier_block);
3340 rc = rtnl_link_register(&vxlan_link_ops);
3346 unregister_netdevice_notifier(&vxlan_notifier_block);
3348 unregister_pernet_subsys(&vxlan_net_ops);
3350 destroy_workqueue(vxlan_wq);
3353 late_initcall(vxlan_init_module);
3355 static void __exit vxlan_cleanup_module(void)
3357 rtnl_link_unregister(&vxlan_link_ops);
3358 unregister_netdevice_notifier(&vxlan_notifier_block);
3359 destroy_workqueue(vxlan_wq);
3360 unregister_pernet_subsys(&vxlan_net_ops);
3361 /* rcu_barrier() is called by netns */
3363 module_exit(vxlan_cleanup_module);
3365 MODULE_LICENSE("GPL");
3366 MODULE_VERSION(VXLAN_VERSION);
3367 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3368 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3369 MODULE_ALIAS_RTNL_LINK("vxlan");