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;
274 flags &= VXLAN_F_RCV_FLAGS;
276 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
277 if (inet_sk(vs->sock->sk)->inet_sport == port &&
278 inet_sk(vs->sock->sk)->sk.sk_family == family &&
285 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
287 struct vxlan_dev *vxlan;
289 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
290 if (vxlan->default_dst.remote_vni == id)
297 /* Look up VNI in a per net namespace table */
298 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
299 sa_family_t family, __be16 port,
302 struct vxlan_sock *vs;
304 vs = vxlan_find_sock(net, family, port, flags);
308 return vxlan_vs_find_vni(vs, id);
311 /* Fill in neighbour message in skbuff. */
312 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
313 const struct vxlan_fdb *fdb,
314 u32 portid, u32 seq, int type, unsigned int flags,
315 const struct vxlan_rdst *rdst)
317 unsigned long now = jiffies;
318 struct nda_cacheinfo ci;
319 struct nlmsghdr *nlh;
321 bool send_ip, send_eth;
323 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
327 ndm = nlmsg_data(nlh);
328 memset(ndm, 0, sizeof(*ndm));
330 send_eth = send_ip = true;
332 if (type == RTM_GETNEIGH) {
333 ndm->ndm_family = AF_INET;
334 send_ip = !vxlan_addr_any(&rdst->remote_ip);
335 send_eth = !is_zero_ether_addr(fdb->eth_addr);
337 ndm->ndm_family = AF_BRIDGE;
338 ndm->ndm_state = fdb->state;
339 ndm->ndm_ifindex = vxlan->dev->ifindex;
340 ndm->ndm_flags = fdb->flags;
341 ndm->ndm_type = RTN_UNICAST;
343 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
344 nla_put_s32(skb, NDA_LINK_NETNSID,
345 peernet2id(dev_net(vxlan->dev), vxlan->net)))
346 goto nla_put_failure;
348 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
349 goto nla_put_failure;
351 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
352 goto nla_put_failure;
354 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
355 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
356 goto nla_put_failure;
357 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
358 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
359 goto nla_put_failure;
360 if (rdst->remote_ifindex &&
361 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
362 goto nla_put_failure;
364 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
365 ci.ndm_confirmed = 0;
366 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
369 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
370 goto nla_put_failure;
376 nlmsg_cancel(skb, nlh);
380 static inline size_t vxlan_nlmsg_size(void)
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
388 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
389 + nla_total_size(sizeof(struct nda_cacheinfo));
392 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
393 struct vxlan_rdst *rd, int type)
395 struct net *net = dev_net(vxlan->dev);
399 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
403 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
418 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
420 struct vxlan_dev *vxlan = netdev_priv(dev);
421 struct vxlan_fdb f = {
424 struct vxlan_rdst remote = {
425 .remote_ip = *ipa, /* goes to NDA_DST */
426 .remote_vni = VXLAN_N_VID,
429 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
432 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
434 struct vxlan_fdb f = {
437 struct vxlan_rdst remote = { };
439 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
441 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
444 /* Hash Ethernet address */
445 static u32 eth_hash(const unsigned char *addr)
447 u64 value = get_unaligned((u64 *)addr);
449 /* only want 6 bytes */
455 return hash_64(value, FDB_HASH_BITS);
458 /* Hash chain to use given mac address */
459 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
462 return &vxlan->fdb_head[eth_hash(mac)];
465 /* Look up Ethernet address in forwarding table */
466 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
469 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
472 hlist_for_each_entry_rcu(f, head, hlist) {
473 if (ether_addr_equal(mac, f->eth_addr))
480 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
485 f = __vxlan_find_mac(vxlan, mac);
492 /* caller should hold vxlan->hash_lock */
493 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
494 union vxlan_addr *ip, __be16 port,
495 __u32 vni, __u32 ifindex)
497 struct vxlan_rdst *rd;
499 list_for_each_entry(rd, &f->remotes, list) {
500 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
501 rd->remote_port == port &&
502 rd->remote_vni == vni &&
503 rd->remote_ifindex == ifindex)
510 /* Replace destination of unicast mac */
511 static int vxlan_fdb_replace(struct vxlan_fdb *f,
512 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
514 struct vxlan_rdst *rd;
516 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
520 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
524 rd->remote_port = port;
525 rd->remote_vni = vni;
526 rd->remote_ifindex = ifindex;
530 /* Add/update destinations for multicast */
531 static int vxlan_fdb_append(struct vxlan_fdb *f,
532 union vxlan_addr *ip, __be16 port, __u32 vni,
533 __u32 ifindex, struct vxlan_rdst **rdp)
535 struct vxlan_rdst *rd;
537 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
541 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
545 rd->remote_port = port;
546 rd->remote_vni = vni;
547 rd->remote_ifindex = ifindex;
549 list_add_tail_rcu(&rd->list, &f->remotes);
555 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
557 struct vxlanhdr *vh, size_t hdrlen,
560 size_t start, offset, plen;
562 if (skb->remcsum_offload)
565 if (!NAPI_GRO_CB(skb)->csum_valid)
568 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
569 offset = start + ((data & VXLAN_RCO_UDP) ?
570 offsetof(struct udphdr, check) :
571 offsetof(struct tcphdr, check));
573 plen = hdrlen + offset + sizeof(u16);
575 /* Pull checksum that will be written */
576 if (skb_gro_header_hard(skb, off + plen)) {
577 vh = skb_gro_header_slow(skb, off + plen, off);
582 skb_gro_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
584 skb->remcsum_offload = 1;
589 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
591 struct udp_offload *uoff)
593 struct sk_buff *p, **pp = NULL;
594 struct vxlanhdr *vh, *vh2;
595 unsigned int hlen, off_vx;
597 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
601 off_vx = skb_gro_offset(skb);
602 hlen = off_vx + sizeof(*vh);
603 vh = skb_gro_header_fast(skb, off_vx);
604 if (skb_gro_header_hard(skb, hlen)) {
605 vh = skb_gro_header_slow(skb, hlen, off_vx);
610 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
611 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
613 flags = ntohl(vh->vx_flags);
615 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
616 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
625 for (p = *head; p; p = p->next) {
626 if (!NAPI_GRO_CB(p)->same_flow)
629 vh2 = (struct vxlanhdr *)(p->data + off_vx);
630 if (vh->vx_flags != vh2->vx_flags ||
631 vh->vx_vni != vh2->vx_vni) {
632 NAPI_GRO_CB(p)->same_flow = 0;
637 pp = eth_gro_receive(head, skb);
640 NAPI_GRO_CB(skb)->flush |= flush;
645 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
646 struct udp_offload *uoff)
648 udp_tunnel_gro_complete(skb, nhoff);
650 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
653 /* Notify netdevs that UDP port started listening */
654 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
656 struct net_device *dev;
657 struct sock *sk = vs->sock->sk;
658 struct net *net = sock_net(sk);
659 sa_family_t sa_family = sk->sk_family;
660 __be16 port = inet_sk(sk)->inet_sport;
663 if (sa_family == AF_INET) {
664 err = udp_add_offload(&vs->udp_offloads);
666 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
670 for_each_netdev_rcu(net, dev) {
671 if (dev->netdev_ops->ndo_add_vxlan_port)
672 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
678 /* Notify netdevs that UDP port is no more listening */
679 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
681 struct net_device *dev;
682 struct sock *sk = vs->sock->sk;
683 struct net *net = sock_net(sk);
684 sa_family_t sa_family = sk->sk_family;
685 __be16 port = inet_sk(sk)->inet_sport;
688 for_each_netdev_rcu(net, dev) {
689 if (dev->netdev_ops->ndo_del_vxlan_port)
690 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
695 if (sa_family == AF_INET)
696 udp_del_offload(&vs->udp_offloads);
699 /* Add new entry to forwarding table -- assumes lock held */
700 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
701 const u8 *mac, union vxlan_addr *ip,
702 __u16 state, __u16 flags,
703 __be16 port, __u32 vni, __u32 ifindex,
706 struct vxlan_rdst *rd = NULL;
710 f = __vxlan_find_mac(vxlan, mac);
712 if (flags & NLM_F_EXCL) {
713 netdev_dbg(vxlan->dev,
714 "lost race to create %pM\n", mac);
717 if (f->state != state) {
719 f->updated = jiffies;
722 if (f->flags != ndm_flags) {
723 f->flags = ndm_flags;
724 f->updated = jiffies;
727 if ((flags & NLM_F_REPLACE)) {
728 /* Only change unicasts */
729 if (!(is_multicast_ether_addr(f->eth_addr) ||
730 is_zero_ether_addr(f->eth_addr))) {
731 int rc = vxlan_fdb_replace(f, ip, port, vni,
740 if ((flags & NLM_F_APPEND) &&
741 (is_multicast_ether_addr(f->eth_addr) ||
742 is_zero_ether_addr(f->eth_addr))) {
743 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
751 if (!(flags & NLM_F_CREATE))
754 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
757 /* Disallow replace to add a multicast entry */
758 if ((flags & NLM_F_REPLACE) &&
759 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
762 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
763 f = kmalloc(sizeof(*f), GFP_ATOMIC);
769 f->flags = ndm_flags;
770 f->updated = f->used = jiffies;
771 INIT_LIST_HEAD(&f->remotes);
772 memcpy(f->eth_addr, mac, ETH_ALEN);
774 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
777 hlist_add_head_rcu(&f->hlist,
778 vxlan_fdb_head(vxlan, mac));
783 rd = first_remote_rtnl(f);
784 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
790 static void vxlan_fdb_free(struct rcu_head *head)
792 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
793 struct vxlan_rdst *rd, *nd;
795 list_for_each_entry_safe(rd, nd, &f->remotes, list)
800 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
802 netdev_dbg(vxlan->dev,
803 "delete %pM\n", f->eth_addr);
806 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
808 hlist_del_rcu(&f->hlist);
809 call_rcu(&f->rcu, vxlan_fdb_free);
812 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
813 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
815 struct net *net = dev_net(vxlan->dev);
819 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
823 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
824 if (remote->sa.sa_family == AF_INET) {
825 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
826 ip->sa.sa_family = AF_INET;
827 #if IS_ENABLED(CONFIG_IPV6)
829 ip->sin6.sin6_addr = in6addr_any;
830 ip->sa.sa_family = AF_INET6;
836 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
838 *port = nla_get_be16(tb[NDA_PORT]);
840 *port = vxlan->dst_port;
844 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
846 *vni = nla_get_u32(tb[NDA_VNI]);
848 *vni = vxlan->default_dst.remote_vni;
851 if (tb[NDA_IFINDEX]) {
852 struct net_device *tdev;
854 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
856 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
857 tdev = __dev_get_by_index(net, *ifindex);
859 return -EADDRNOTAVAIL;
867 /* Add static entry (via netlink) */
868 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
869 struct net_device *dev,
870 const unsigned char *addr, u16 vid, u16 flags)
872 struct vxlan_dev *vxlan = netdev_priv(dev);
873 /* struct net *net = dev_net(vxlan->dev); */
879 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
880 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
885 if (tb[NDA_DST] == NULL)
888 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
892 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
893 return -EAFNOSUPPORT;
895 spin_lock_bh(&vxlan->hash_lock);
896 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
897 port, vni, ifindex, ndm->ndm_flags);
898 spin_unlock_bh(&vxlan->hash_lock);
903 /* Delete entry (via netlink) */
904 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
905 struct net_device *dev,
906 const unsigned char *addr, u16 vid)
908 struct vxlan_dev *vxlan = netdev_priv(dev);
910 struct vxlan_rdst *rd = NULL;
916 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
922 spin_lock_bh(&vxlan->hash_lock);
923 f = vxlan_find_mac(vxlan, addr);
927 if (!vxlan_addr_any(&ip)) {
928 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
935 /* remove a destination if it's not the only one on the list,
936 * otherwise destroy the fdb entry
938 if (rd && !list_is_singular(&f->remotes)) {
939 list_del_rcu(&rd->list);
940 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
945 vxlan_fdb_destroy(vxlan, f);
948 spin_unlock_bh(&vxlan->hash_lock);
953 /* Dump forwarding table */
954 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
955 struct net_device *dev,
956 struct net_device *filter_dev, int idx)
958 struct vxlan_dev *vxlan = netdev_priv(dev);
961 for (h = 0; h < FDB_HASH_SIZE; ++h) {
965 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
966 struct vxlan_rdst *rd;
968 if (idx < cb->args[0])
971 list_for_each_entry_rcu(rd, &f->remotes, list) {
972 err = vxlan_fdb_info(skb, vxlan, f,
973 NETLINK_CB(cb->skb).portid,
988 /* Watch incoming packets to learn mapping between Ethernet address
989 * and Tunnel endpoint.
990 * Return true if packet is bogus and should be droppped.
992 static bool vxlan_snoop(struct net_device *dev,
993 union vxlan_addr *src_ip, const u8 *src_mac)
995 struct vxlan_dev *vxlan = netdev_priv(dev);
998 f = vxlan_find_mac(vxlan, src_mac);
1000 struct vxlan_rdst *rdst = first_remote_rcu(f);
1002 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
1005 /* Don't migrate static entries, drop packets */
1006 if (f->state & NUD_NOARP)
1009 if (net_ratelimit())
1011 "%pM migrated from %pIS to %pIS\n",
1012 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1014 rdst->remote_ip = *src_ip;
1015 f->updated = jiffies;
1016 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
1018 /* learned new entry */
1019 spin_lock(&vxlan->hash_lock);
1021 /* close off race between vxlan_flush and incoming packets */
1022 if (netif_running(dev))
1023 vxlan_fdb_create(vxlan, src_mac, src_ip,
1025 NLM_F_EXCL|NLM_F_CREATE,
1027 vxlan->default_dst.remote_vni,
1029 spin_unlock(&vxlan->hash_lock);
1035 /* See if multicast group is already in use by other ID */
1036 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1038 struct vxlan_dev *vxlan;
1040 /* The vxlan_sock is only used by dev, leaving group has
1041 * no effect on other vxlan devices.
1043 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1046 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1047 if (!netif_running(vxlan->dev) || vxlan == dev)
1050 if (vxlan->vn_sock != dev->vn_sock)
1053 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1054 &dev->default_dst.remote_ip))
1057 if (vxlan->default_dst.remote_ifindex !=
1058 dev->default_dst.remote_ifindex)
1067 static void vxlan_sock_hold(struct vxlan_sock *vs)
1069 atomic_inc(&vs->refcnt);
1072 void vxlan_sock_release(struct vxlan_sock *vs)
1074 struct sock *sk = vs->sock->sk;
1075 struct net *net = sock_net(sk);
1076 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1078 if (!atomic_dec_and_test(&vs->refcnt))
1081 spin_lock(&vn->sock_lock);
1082 hlist_del_rcu(&vs->hlist);
1083 vxlan_notify_del_rx_port(vs);
1084 spin_unlock(&vn->sock_lock);
1086 queue_work(vxlan_wq, &vs->del_work);
1088 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1090 /* Callback to update multicast group membership when first VNI on
1091 * multicast asddress is brought up
1092 * Done as workqueue because ip_mc_join_group acquires RTNL.
1094 static void vxlan_igmp_join(struct work_struct *work)
1096 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
1097 struct vxlan_sock *vs = vxlan->vn_sock;
1098 struct sock *sk = vs->sock->sk;
1099 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1100 int ifindex = vxlan->default_dst.remote_ifindex;
1103 if (ip->sa.sa_family == AF_INET) {
1104 struct ip_mreqn mreq = {
1105 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1106 .imr_ifindex = ifindex,
1109 ip_mc_join_group(sk, &mreq);
1110 #if IS_ENABLED(CONFIG_IPV6)
1112 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1113 &ip->sin6.sin6_addr);
1118 vxlan_sock_release(vs);
1119 dev_put(vxlan->dev);
1122 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1123 static void vxlan_igmp_leave(struct work_struct *work)
1125 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1126 struct vxlan_sock *vs = vxlan->vn_sock;
1127 struct sock *sk = vs->sock->sk;
1128 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1129 int ifindex = vxlan->default_dst.remote_ifindex;
1132 if (ip->sa.sa_family == AF_INET) {
1133 struct ip_mreqn mreq = {
1134 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1135 .imr_ifindex = ifindex,
1138 ip_mc_leave_group(sk, &mreq);
1139 #if IS_ENABLED(CONFIG_IPV6)
1141 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1142 &ip->sin6.sin6_addr);
1148 vxlan_sock_release(vs);
1149 dev_put(vxlan->dev);
1152 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1153 size_t hdrlen, u32 data)
1155 size_t start, offset, plen;
1157 if (skb->remcsum_offload) {
1158 /* Already processed in GRO path */
1159 skb->remcsum_offload = 0;
1163 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1164 offset = start + ((data & VXLAN_RCO_UDP) ?
1165 offsetof(struct udphdr, check) :
1166 offsetof(struct tcphdr, check));
1168 plen = hdrlen + offset + sizeof(u16);
1170 if (!pskb_may_pull(skb, plen))
1173 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1175 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
1180 /* Callback from net/ipv4/udp.c to receive packets */
1181 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1183 struct vxlan_sock *vs;
1184 struct vxlanhdr *vxh;
1186 struct vxlan_metadata md = {0};
1188 /* Need Vxlan and inner Ethernet header to be present */
1189 if (!pskb_may_pull(skb, VXLAN_HLEN))
1192 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1193 flags = ntohl(vxh->vx_flags);
1194 vni = ntohl(vxh->vx_vni);
1196 if (flags & VXLAN_HF_VNI) {
1197 flags &= ~VXLAN_HF_VNI;
1199 /* VNI flag always required to be set */
1203 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1205 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1207 vs = rcu_dereference_sk_user_data(sk);
1211 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1212 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1216 flags &= ~VXLAN_HF_RCO;
1217 vni &= VXLAN_VID_MASK;
1220 /* For backwards compatibility, only allow reserved fields to be
1221 * used by VXLAN extensions if explicitly requested.
1223 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1224 struct vxlanhdr_gbp *gbp;
1226 gbp = (struct vxlanhdr_gbp *)vxh;
1227 md.gbp = ntohs(gbp->policy_id);
1229 if (gbp->dont_learn)
1230 md.gbp |= VXLAN_GBP_DONT_LEARN;
1232 if (gbp->policy_applied)
1233 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1235 flags &= ~VXLAN_GBP_USED_BITS;
1238 if (flags || (vni & ~VXLAN_VID_MASK)) {
1239 /* If there are any unprocessed flags remaining treat
1240 * this as a malformed packet. This behavior diverges from
1241 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1242 * in reserved fields are to be ignored. The approach here
1243 * maintains compatbility with previous stack code, and also
1244 * is more robust and provides a little more security in
1245 * adding extensions to VXLAN.
1251 md.vni = vxh->vx_vni;
1252 vs->rcv(vs, skb, &md);
1256 /* Consume bad packet */
1261 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1262 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1265 /* Return non vxlan pkt */
1269 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1270 struct vxlan_metadata *md)
1272 struct iphdr *oip = NULL;
1273 struct ipv6hdr *oip6 = NULL;
1274 struct vxlan_dev *vxlan;
1275 struct pcpu_sw_netstats *stats;
1276 union vxlan_addr saddr;
1279 union vxlan_addr *remote_ip;
1281 vni = ntohl(md->vni) >> 8;
1282 /* Is this VNI defined? */
1283 vxlan = vxlan_vs_find_vni(vs, vni);
1287 remote_ip = &vxlan->default_dst.remote_ip;
1288 skb_reset_mac_header(skb);
1289 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1290 skb->protocol = eth_type_trans(skb, vxlan->dev);
1291 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1293 /* Ignore packet loops (and multicast echo) */
1294 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1297 /* Re-examine inner Ethernet packet */
1298 if (remote_ip->sa.sa_family == AF_INET) {
1300 saddr.sin.sin_addr.s_addr = oip->saddr;
1301 saddr.sa.sa_family = AF_INET;
1302 #if IS_ENABLED(CONFIG_IPV6)
1304 oip6 = ipv6_hdr(skb);
1305 saddr.sin6.sin6_addr = oip6->saddr;
1306 saddr.sa.sa_family = AF_INET6;
1310 if ((vxlan->flags & VXLAN_F_LEARN) &&
1311 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1314 skb_reset_network_header(skb);
1315 skb->mark = md->gbp;
1318 err = IP6_ECN_decapsulate(oip6, skb);
1320 err = IP_ECN_decapsulate(oip, skb);
1322 if (unlikely(err)) {
1323 if (log_ecn_error) {
1325 net_info_ratelimited("non-ECT from %pI6\n",
1328 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1329 &oip->saddr, oip->tos);
1332 ++vxlan->dev->stats.rx_frame_errors;
1333 ++vxlan->dev->stats.rx_errors;
1338 stats = this_cpu_ptr(vxlan->dev->tstats);
1339 u64_stats_update_begin(&stats->syncp);
1340 stats->rx_packets++;
1341 stats->rx_bytes += skb->len;
1342 u64_stats_update_end(&stats->syncp);
1348 /* Consume bad packet */
1352 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1354 struct vxlan_dev *vxlan = netdev_priv(dev);
1355 struct arphdr *parp;
1358 struct neighbour *n;
1360 if (dev->flags & IFF_NOARP)
1363 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1364 dev->stats.tx_dropped++;
1367 parp = arp_hdr(skb);
1369 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1370 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1371 parp->ar_pro != htons(ETH_P_IP) ||
1372 parp->ar_op != htons(ARPOP_REQUEST) ||
1373 parp->ar_hln != dev->addr_len ||
1376 arpptr = (u8 *)parp + sizeof(struct arphdr);
1378 arpptr += dev->addr_len; /* sha */
1379 memcpy(&sip, arpptr, sizeof(sip));
1380 arpptr += sizeof(sip);
1381 arpptr += dev->addr_len; /* tha */
1382 memcpy(&tip, arpptr, sizeof(tip));
1384 if (ipv4_is_loopback(tip) ||
1385 ipv4_is_multicast(tip))
1388 n = neigh_lookup(&arp_tbl, &tip, dev);
1391 struct vxlan_fdb *f;
1392 struct sk_buff *reply;
1394 if (!(n->nud_state & NUD_CONNECTED)) {
1399 f = vxlan_find_mac(vxlan, n->ha);
1400 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1401 /* bridge-local neighbor */
1406 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1414 skb_reset_mac_header(reply);
1415 __skb_pull(reply, skb_network_offset(reply));
1416 reply->ip_summed = CHECKSUM_UNNECESSARY;
1417 reply->pkt_type = PACKET_HOST;
1419 if (netif_rx_ni(reply) == NET_RX_DROP)
1420 dev->stats.rx_dropped++;
1421 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1422 union vxlan_addr ipa = {
1423 .sin.sin_addr.s_addr = tip,
1424 .sin.sin_family = AF_INET,
1427 vxlan_ip_miss(dev, &ipa);
1431 return NETDEV_TX_OK;
1434 #if IS_ENABLED(CONFIG_IPV6)
1435 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1436 struct neighbour *n, bool isrouter)
1438 struct net_device *dev = request->dev;
1439 struct sk_buff *reply;
1440 struct nd_msg *ns, *na;
1441 struct ipv6hdr *pip6;
1443 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1450 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1451 sizeof(*na) + na_olen + dev->needed_tailroom;
1452 reply = alloc_skb(len, GFP_ATOMIC);
1456 reply->protocol = htons(ETH_P_IPV6);
1458 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1459 skb_push(reply, sizeof(struct ethhdr));
1460 skb_set_mac_header(reply, 0);
1462 ns = (struct nd_msg *)skb_transport_header(request);
1464 daddr = eth_hdr(request)->h_source;
1465 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1466 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1467 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1468 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1473 /* Ethernet header */
1474 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1475 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1476 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1477 reply->protocol = htons(ETH_P_IPV6);
1479 skb_pull(reply, sizeof(struct ethhdr));
1480 skb_set_network_header(reply, 0);
1481 skb_put(reply, sizeof(struct ipv6hdr));
1485 pip6 = ipv6_hdr(reply);
1486 memset(pip6, 0, sizeof(struct ipv6hdr));
1488 pip6->priority = ipv6_hdr(request)->priority;
1489 pip6->nexthdr = IPPROTO_ICMPV6;
1490 pip6->hop_limit = 255;
1491 pip6->daddr = ipv6_hdr(request)->saddr;
1492 pip6->saddr = *(struct in6_addr *)n->primary_key;
1494 skb_pull(reply, sizeof(struct ipv6hdr));
1495 skb_set_transport_header(reply, 0);
1497 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1499 /* Neighbor Advertisement */
1500 memset(na, 0, sizeof(*na)+na_olen);
1501 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1502 na->icmph.icmp6_router = isrouter;
1503 na->icmph.icmp6_override = 1;
1504 na->icmph.icmp6_solicited = 1;
1505 na->target = ns->target;
1506 ether_addr_copy(&na->opt[2], n->ha);
1507 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1508 na->opt[1] = na_olen >> 3;
1510 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1511 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1512 csum_partial(na, sizeof(*na)+na_olen, 0));
1514 pip6->payload_len = htons(sizeof(*na)+na_olen);
1516 skb_push(reply, sizeof(struct ipv6hdr));
1518 reply->ip_summed = CHECKSUM_UNNECESSARY;
1523 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1525 struct vxlan_dev *vxlan = netdev_priv(dev);
1527 const struct ipv6hdr *iphdr;
1528 const struct in6_addr *saddr, *daddr;
1529 struct neighbour *n;
1530 struct inet6_dev *in6_dev;
1532 in6_dev = __in6_dev_get(dev);
1536 iphdr = ipv6_hdr(skb);
1537 saddr = &iphdr->saddr;
1538 daddr = &iphdr->daddr;
1540 msg = (struct nd_msg *)skb_transport_header(skb);
1541 if (msg->icmph.icmp6_code != 0 ||
1542 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1545 if (ipv6_addr_loopback(daddr) ||
1546 ipv6_addr_is_multicast(&msg->target))
1549 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1552 struct vxlan_fdb *f;
1553 struct sk_buff *reply;
1555 if (!(n->nud_state & NUD_CONNECTED)) {
1560 f = vxlan_find_mac(vxlan, n->ha);
1561 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1562 /* bridge-local neighbor */
1567 reply = vxlan_na_create(skb, n,
1568 !!(f ? f->flags & NTF_ROUTER : 0));
1575 if (netif_rx_ni(reply) == NET_RX_DROP)
1576 dev->stats.rx_dropped++;
1578 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1579 union vxlan_addr ipa = {
1580 .sin6.sin6_addr = msg->target,
1581 .sin6.sin6_family = AF_INET6,
1584 vxlan_ip_miss(dev, &ipa);
1589 return NETDEV_TX_OK;
1593 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1595 struct vxlan_dev *vxlan = netdev_priv(dev);
1596 struct neighbour *n;
1598 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1602 switch (ntohs(eth_hdr(skb)->h_proto)) {
1607 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1610 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1611 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1612 union vxlan_addr ipa = {
1613 .sin.sin_addr.s_addr = pip->daddr,
1614 .sin.sin_family = AF_INET,
1617 vxlan_ip_miss(dev, &ipa);
1623 #if IS_ENABLED(CONFIG_IPV6)
1626 struct ipv6hdr *pip6;
1628 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1630 pip6 = ipv6_hdr(skb);
1631 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1632 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1633 union vxlan_addr ipa = {
1634 .sin6.sin6_addr = pip6->daddr,
1635 .sin6.sin6_family = AF_INET6,
1638 vxlan_ip_miss(dev, &ipa);
1652 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1654 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1656 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1665 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1666 struct vxlan_metadata *md)
1668 struct vxlanhdr_gbp *gbp;
1673 gbp = (struct vxlanhdr_gbp *)vxh;
1674 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1676 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1677 gbp->dont_learn = 1;
1679 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1680 gbp->policy_applied = 1;
1682 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
1687 struct net_device *dev, struct in6_addr *saddr,
1688 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1689 __be16 src_port, __be16 dst_port,
1690 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1692 struct vxlanhdr *vxh;
1695 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1696 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1697 u16 hdrlen = sizeof(struct vxlanhdr);
1699 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1700 skb->ip_summed == CHECKSUM_PARTIAL) {
1701 int csum_start = skb_checksum_start_offset(skb);
1703 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1704 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1705 (skb->csum_offset == offsetof(struct udphdr, check) ||
1706 skb->csum_offset == offsetof(struct tcphdr, check))) {
1708 type |= SKB_GSO_TUNNEL_REMCSUM;
1712 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1718 skb_scrub_packet(skb, xnet);
1720 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1721 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1722 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1724 /* Need space for new headers (invalidates iph ptr) */
1725 err = skb_cow_head(skb, min_headroom);
1726 if (unlikely(err)) {
1731 skb = vlan_hwaccel_push_inside(skb);
1732 if (WARN_ON(!skb)) {
1737 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1738 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1739 vxh->vx_vni = md->vni;
1741 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1742 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1745 if (skb->csum_offset == offsetof(struct udphdr, check))
1746 data |= VXLAN_RCO_UDP;
1748 vxh->vx_vni |= htonl(data);
1749 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1751 if (!skb_is_gso(skb)) {
1752 skb->ip_summed = CHECKSUM_NONE;
1753 skb->encapsulation = 0;
1757 if (vxflags & VXLAN_F_GBP)
1758 vxlan_build_gbp_hdr(vxh, vxflags, md);
1760 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1762 udp_tunnel6_xmit_skb(dst, skb, dev, saddr, daddr, prio,
1763 ttl, src_port, dst_port,
1764 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1772 int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
1773 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1774 __be16 src_port, __be16 dst_port,
1775 struct vxlan_metadata *md, bool xnet, u32 vxflags)
1777 struct vxlanhdr *vxh;
1780 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1781 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1782 u16 hdrlen = sizeof(struct vxlanhdr);
1784 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1785 skb->ip_summed == CHECKSUM_PARTIAL) {
1786 int csum_start = skb_checksum_start_offset(skb);
1788 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1789 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1790 (skb->csum_offset == offsetof(struct udphdr, check) ||
1791 skb->csum_offset == offsetof(struct tcphdr, check))) {
1793 type |= SKB_GSO_TUNNEL_REMCSUM;
1797 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1799 return PTR_ERR(skb);
1801 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1802 + VXLAN_HLEN + sizeof(struct iphdr)
1803 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1805 /* Need space for new headers (invalidates iph ptr) */
1806 err = skb_cow_head(skb, min_headroom);
1807 if (unlikely(err)) {
1812 skb = vlan_hwaccel_push_inside(skb);
1816 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1817 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1818 vxh->vx_vni = md->vni;
1820 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1821 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1824 if (skb->csum_offset == offsetof(struct udphdr, check))
1825 data |= VXLAN_RCO_UDP;
1827 vxh->vx_vni |= htonl(data);
1828 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1830 if (!skb_is_gso(skb)) {
1831 skb->ip_summed = CHECKSUM_NONE;
1832 skb->encapsulation = 0;
1836 if (vxflags & VXLAN_F_GBP)
1837 vxlan_build_gbp_hdr(vxh, vxflags, md);
1839 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1841 return udp_tunnel_xmit_skb(rt, skb, src, dst, tos,
1842 ttl, df, src_port, dst_port, xnet,
1843 !(vxflags & VXLAN_F_UDP_CSUM));
1845 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1847 /* Bypass encapsulation if the destination is local */
1848 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1849 struct vxlan_dev *dst_vxlan)
1851 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1852 union vxlan_addr loopback;
1853 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1854 struct net_device *dev = skb->dev;
1857 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1858 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1859 skb->pkt_type = PACKET_HOST;
1860 skb->encapsulation = 0;
1861 skb->dev = dst_vxlan->dev;
1862 __skb_pull(skb, skb_network_offset(skb));
1864 if (remote_ip->sa.sa_family == AF_INET) {
1865 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1866 loopback.sa.sa_family = AF_INET;
1867 #if IS_ENABLED(CONFIG_IPV6)
1869 loopback.sin6.sin6_addr = in6addr_loopback;
1870 loopback.sa.sa_family = AF_INET6;
1874 if (dst_vxlan->flags & VXLAN_F_LEARN)
1875 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1877 u64_stats_update_begin(&tx_stats->syncp);
1878 tx_stats->tx_packets++;
1879 tx_stats->tx_bytes += len;
1880 u64_stats_update_end(&tx_stats->syncp);
1882 if (netif_rx(skb) == NET_RX_SUCCESS) {
1883 u64_stats_update_begin(&rx_stats->syncp);
1884 rx_stats->rx_packets++;
1885 rx_stats->rx_bytes += len;
1886 u64_stats_update_end(&rx_stats->syncp);
1888 dev->stats.rx_dropped++;
1892 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1893 struct vxlan_rdst *rdst, bool did_rsc)
1895 struct vxlan_dev *vxlan = netdev_priv(dev);
1896 struct rtable *rt = NULL;
1897 const struct iphdr *old_iph;
1899 union vxlan_addr *dst;
1900 struct vxlan_metadata md;
1901 __be16 src_port = 0, dst_port;
1907 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1908 vni = rdst->remote_vni;
1909 dst = &rdst->remote_ip;
1911 if (vxlan_addr_any(dst)) {
1913 /* short-circuited back to local bridge */
1914 vxlan_encap_bypass(skb, vxlan, vxlan);
1920 old_iph = ip_hdr(skb);
1923 if (!ttl && vxlan_addr_multicast(dst))
1928 tos = ip_tunnel_get_dsfield(old_iph, skb);
1930 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1931 vxlan->port_max, true);
1933 if (dst->sa.sa_family == AF_INET) {
1934 memset(&fl4, 0, sizeof(fl4));
1935 fl4.flowi4_oif = rdst->remote_ifindex;
1936 fl4.flowi4_tos = RT_TOS(tos);
1937 fl4.daddr = dst->sin.sin_addr.s_addr;
1938 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1940 rt = ip_route_output_key(vxlan->net, &fl4);
1942 netdev_dbg(dev, "no route to %pI4\n",
1943 &dst->sin.sin_addr.s_addr);
1944 dev->stats.tx_carrier_errors++;
1948 if (rt->dst.dev == dev) {
1949 netdev_dbg(dev, "circular route to %pI4\n",
1950 &dst->sin.sin_addr.s_addr);
1951 dev->stats.collisions++;
1955 /* Bypass encapsulation if the destination is local */
1956 if (rt->rt_flags & RTCF_LOCAL &&
1957 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1958 struct vxlan_dev *dst_vxlan;
1961 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1962 dst->sa.sa_family, dst_port,
1966 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1970 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1971 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1972 md.vni = htonl(vni << 8);
1975 err = vxlan_xmit_skb(rt, skb, fl4.saddr,
1976 dst->sin.sin_addr.s_addr, tos, ttl, df,
1977 src_port, dst_port, &md,
1978 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1981 /* skb is already freed. */
1986 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1987 #if IS_ENABLED(CONFIG_IPV6)
1989 struct sock *sk = vxlan->vn_sock->sock->sk;
1990 struct dst_entry *ndst;
1994 memset(&fl6, 0, sizeof(fl6));
1995 fl6.flowi6_oif = rdst->remote_ifindex;
1996 fl6.daddr = dst->sin6.sin6_addr;
1997 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1998 fl6.flowi6_proto = IPPROTO_UDP;
2000 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2001 netdev_dbg(dev, "no route to %pI6\n",
2002 &dst->sin6.sin6_addr);
2003 dev->stats.tx_carrier_errors++;
2007 if (ndst->dev == dev) {
2008 netdev_dbg(dev, "circular route to %pI6\n",
2009 &dst->sin6.sin6_addr);
2011 dev->stats.collisions++;
2015 /* Bypass encapsulation if the destination is local */
2016 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2017 if (flags & RTF_LOCAL &&
2018 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2019 struct vxlan_dev *dst_vxlan;
2022 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2023 dst->sa.sa_family, dst_port,
2027 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2031 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2032 md.vni = htonl(vni << 8);
2035 err = vxlan6_xmit_skb(ndst, skb, dev, &fl6.saddr, &fl6.daddr,
2036 0, ttl, src_port, dst_port, &md,
2037 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2045 dev->stats.tx_dropped++;
2051 dev->stats.tx_errors++;
2056 /* Transmit local packets over Vxlan
2058 * Outer IP header inherits ECN and DF from inner header.
2059 * Outer UDP destination is the VXLAN assigned port.
2060 * source port is based on hash of flow
2062 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2064 struct vxlan_dev *vxlan = netdev_priv(dev);
2066 bool did_rsc = false;
2067 struct vxlan_rdst *rdst, *fdst = NULL;
2068 struct vxlan_fdb *f;
2070 skb_reset_mac_header(skb);
2073 if ((vxlan->flags & VXLAN_F_PROXY)) {
2074 if (ntohs(eth->h_proto) == ETH_P_ARP)
2075 return arp_reduce(dev, skb);
2076 #if IS_ENABLED(CONFIG_IPV6)
2077 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2078 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2079 + sizeof(struct nd_msg)) &&
2080 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2083 msg = (struct nd_msg *)skb_transport_header(skb);
2084 if (msg->icmph.icmp6_code == 0 &&
2085 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2086 return neigh_reduce(dev, skb);
2092 f = vxlan_find_mac(vxlan, eth->h_dest);
2095 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2096 (ntohs(eth->h_proto) == ETH_P_IP ||
2097 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2098 did_rsc = route_shortcircuit(dev, skb);
2100 f = vxlan_find_mac(vxlan, eth->h_dest);
2104 f = vxlan_find_mac(vxlan, all_zeros_mac);
2106 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2107 !is_multicast_ether_addr(eth->h_dest))
2108 vxlan_fdb_miss(vxlan, eth->h_dest);
2110 dev->stats.tx_dropped++;
2112 return NETDEV_TX_OK;
2116 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2117 struct sk_buff *skb1;
2123 skb1 = skb_clone(skb, GFP_ATOMIC);
2125 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2129 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2132 return NETDEV_TX_OK;
2135 /* Walk the forwarding table and purge stale entries */
2136 static void vxlan_cleanup(unsigned long arg)
2138 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2139 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2142 if (!netif_running(vxlan->dev))
2145 spin_lock_bh(&vxlan->hash_lock);
2146 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2147 struct hlist_node *p, *n;
2148 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2150 = container_of(p, struct vxlan_fdb, hlist);
2151 unsigned long timeout;
2153 if (f->state & NUD_PERMANENT)
2156 timeout = f->used + vxlan->age_interval * HZ;
2157 if (time_before_eq(timeout, jiffies)) {
2158 netdev_dbg(vxlan->dev,
2159 "garbage collect %pM\n",
2161 f->state = NUD_STALE;
2162 vxlan_fdb_destroy(vxlan, f);
2163 } else if (time_before(timeout, next_timer))
2164 next_timer = timeout;
2167 spin_unlock_bh(&vxlan->hash_lock);
2169 mod_timer(&vxlan->age_timer, next_timer);
2172 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2174 __u32 vni = vxlan->default_dst.remote_vni;
2176 vxlan->vn_sock = vs;
2177 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2180 /* Setup stats when device is created */
2181 static int vxlan_init(struct net_device *dev)
2183 struct vxlan_dev *vxlan = netdev_priv(dev);
2184 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2185 struct vxlan_sock *vs;
2186 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2188 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2192 spin_lock(&vn->sock_lock);
2193 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2194 vxlan->dst_port, vxlan->flags);
2195 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
2196 /* If we have a socket with same port already, reuse it */
2197 vxlan_vs_add_dev(vs, vxlan);
2199 /* otherwise make new socket outside of RTNL */
2201 queue_work(vxlan_wq, &vxlan->sock_work);
2203 spin_unlock(&vn->sock_lock);
2208 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2210 struct vxlan_fdb *f;
2212 spin_lock_bh(&vxlan->hash_lock);
2213 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2215 vxlan_fdb_destroy(vxlan, f);
2216 spin_unlock_bh(&vxlan->hash_lock);
2219 static void vxlan_uninit(struct net_device *dev)
2221 struct vxlan_dev *vxlan = netdev_priv(dev);
2222 struct vxlan_sock *vs = vxlan->vn_sock;
2224 vxlan_fdb_delete_default(vxlan);
2227 vxlan_sock_release(vs);
2228 free_percpu(dev->tstats);
2231 /* Start ageing timer and join group when device is brought up */
2232 static int vxlan_open(struct net_device *dev)
2234 struct vxlan_dev *vxlan = netdev_priv(dev);
2235 struct vxlan_sock *vs = vxlan->vn_sock;
2237 /* socket hasn't been created */
2241 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2242 vxlan_sock_hold(vs);
2244 queue_work(vxlan_wq, &vxlan->igmp_join);
2247 if (vxlan->age_interval)
2248 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2253 /* Purge the forwarding table */
2254 static void vxlan_flush(struct vxlan_dev *vxlan)
2258 spin_lock_bh(&vxlan->hash_lock);
2259 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2260 struct hlist_node *p, *n;
2261 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2263 = container_of(p, struct vxlan_fdb, hlist);
2264 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2265 if (!is_zero_ether_addr(f->eth_addr))
2266 vxlan_fdb_destroy(vxlan, f);
2269 spin_unlock_bh(&vxlan->hash_lock);
2272 /* Cleanup timer and forwarding table on shutdown */
2273 static int vxlan_stop(struct net_device *dev)
2275 struct vxlan_dev *vxlan = netdev_priv(dev);
2276 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2277 struct vxlan_sock *vs = vxlan->vn_sock;
2279 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2280 !vxlan_group_used(vn, vxlan)) {
2281 vxlan_sock_hold(vs);
2283 queue_work(vxlan_wq, &vxlan->igmp_leave);
2286 del_timer_sync(&vxlan->age_timer);
2293 /* Stub, nothing needs to be done. */
2294 static void vxlan_set_multicast_list(struct net_device *dev)
2298 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2300 struct vxlan_dev *vxlan = netdev_priv(dev);
2301 struct vxlan_rdst *dst = &vxlan->default_dst;
2302 struct net_device *lowerdev;
2305 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2306 if (lowerdev == NULL)
2307 return eth_change_mtu(dev, new_mtu);
2309 if (dst->remote_ip.sa.sa_family == AF_INET6)
2310 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2312 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2314 if (new_mtu < 68 || new_mtu > max_mtu)
2321 static const struct net_device_ops vxlan_netdev_ops = {
2322 .ndo_init = vxlan_init,
2323 .ndo_uninit = vxlan_uninit,
2324 .ndo_open = vxlan_open,
2325 .ndo_stop = vxlan_stop,
2326 .ndo_start_xmit = vxlan_xmit,
2327 .ndo_get_stats64 = ip_tunnel_get_stats64,
2328 .ndo_set_rx_mode = vxlan_set_multicast_list,
2329 .ndo_change_mtu = vxlan_change_mtu,
2330 .ndo_validate_addr = eth_validate_addr,
2331 .ndo_set_mac_address = eth_mac_addr,
2332 .ndo_fdb_add = vxlan_fdb_add,
2333 .ndo_fdb_del = vxlan_fdb_delete,
2334 .ndo_fdb_dump = vxlan_fdb_dump,
2337 /* Info for udev, that this is a virtual tunnel endpoint */
2338 static struct device_type vxlan_type = {
2342 /* Calls the ndo_add_vxlan_port of the caller in order to
2343 * supply the listening VXLAN udp ports. Callers are expected
2344 * to implement the ndo_add_vxlan_port.
2346 void vxlan_get_rx_port(struct net_device *dev)
2348 struct vxlan_sock *vs;
2349 struct net *net = dev_net(dev);
2350 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2351 sa_family_t sa_family;
2355 spin_lock(&vn->sock_lock);
2356 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2357 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2358 port = inet_sk(vs->sock->sk)->inet_sport;
2359 sa_family = vs->sock->sk->sk_family;
2360 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2364 spin_unlock(&vn->sock_lock);
2366 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2368 /* Initialize the device structure. */
2369 static void vxlan_setup(struct net_device *dev)
2371 struct vxlan_dev *vxlan = netdev_priv(dev);
2374 eth_hw_addr_random(dev);
2376 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2377 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2379 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2381 dev->netdev_ops = &vxlan_netdev_ops;
2382 dev->destructor = free_netdev;
2383 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2385 dev->tx_queue_len = 0;
2386 dev->features |= NETIF_F_LLTX;
2387 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2388 dev->features |= NETIF_F_RXCSUM;
2389 dev->features |= NETIF_F_GSO_SOFTWARE;
2391 dev->vlan_features = dev->features;
2392 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2393 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2394 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2395 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2396 netif_keep_dst(dev);
2397 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2399 INIT_LIST_HEAD(&vxlan->next);
2400 spin_lock_init(&vxlan->hash_lock);
2401 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2402 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2403 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2405 init_timer_deferrable(&vxlan->age_timer);
2406 vxlan->age_timer.function = vxlan_cleanup;
2407 vxlan->age_timer.data = (unsigned long) vxlan;
2409 vxlan->dst_port = htons(vxlan_port);
2413 for (h = 0; h < FDB_HASH_SIZE; ++h)
2414 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2417 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2418 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2419 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2420 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2421 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2422 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2423 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2424 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2425 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2426 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2427 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2428 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2429 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2430 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2431 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2432 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2433 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2434 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2435 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2437 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2438 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2439 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2440 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2443 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2445 if (tb[IFLA_ADDRESS]) {
2446 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2447 pr_debug("invalid link address (not ethernet)\n");
2451 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2452 pr_debug("invalid all zero ethernet address\n");
2453 return -EADDRNOTAVAIL;
2460 if (data[IFLA_VXLAN_ID]) {
2461 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2462 if (id >= VXLAN_VID_MASK)
2466 if (data[IFLA_VXLAN_PORT_RANGE]) {
2467 const struct ifla_vxlan_port_range *p
2468 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2470 if (ntohs(p->high) < ntohs(p->low)) {
2471 pr_debug("port range %u .. %u not valid\n",
2472 ntohs(p->low), ntohs(p->high));
2480 static void vxlan_get_drvinfo(struct net_device *netdev,
2481 struct ethtool_drvinfo *drvinfo)
2483 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2484 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2487 static const struct ethtool_ops vxlan_ethtool_ops = {
2488 .get_drvinfo = vxlan_get_drvinfo,
2489 .get_link = ethtool_op_get_link,
2492 static void vxlan_del_work(struct work_struct *work)
2494 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2495 udp_tunnel_sock_release(vs->sock);
2499 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2500 __be16 port, u32 flags)
2502 struct socket *sock;
2503 struct udp_port_cfg udp_conf;
2506 memset(&udp_conf, 0, sizeof(udp_conf));
2509 udp_conf.family = AF_INET6;
2510 udp_conf.use_udp6_rx_checksums =
2511 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2513 udp_conf.family = AF_INET;
2514 udp_conf.local_ip.s_addr = INADDR_ANY;
2517 udp_conf.local_udp_port = port;
2519 /* Open UDP socket */
2520 err = udp_sock_create(net, &udp_conf, &sock);
2522 return ERR_PTR(err);
2527 /* Create new listen socket if needed */
2528 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2529 vxlan_rcv_t *rcv, void *data,
2532 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2533 struct vxlan_sock *vs;
2534 struct socket *sock;
2536 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2537 struct udp_tunnel_sock_cfg tunnel_cfg;
2539 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2541 return ERR_PTR(-ENOMEM);
2543 for (h = 0; h < VNI_HASH_SIZE; ++h)
2544 INIT_HLIST_HEAD(&vs->vni_list[h]);
2546 INIT_WORK(&vs->del_work, vxlan_del_work);
2548 sock = vxlan_create_sock(net, ipv6, port, flags);
2551 return ERR_CAST(sock);
2555 atomic_set(&vs->refcnt, 1);
2558 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2560 /* Initialize the vxlan udp offloads structure */
2561 vs->udp_offloads.port = port;
2562 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2563 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2565 spin_lock(&vn->sock_lock);
2566 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2567 vxlan_notify_add_rx_port(vs);
2568 spin_unlock(&vn->sock_lock);
2570 /* Mark socket as an encapsulation socket. */
2571 tunnel_cfg.sk_user_data = vs;
2572 tunnel_cfg.encap_type = 1;
2573 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2574 tunnel_cfg.encap_destroy = NULL;
2576 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2581 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2582 vxlan_rcv_t *rcv, void *data,
2583 bool no_share, u32 flags)
2585 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2586 struct vxlan_sock *vs;
2587 bool ipv6 = flags & VXLAN_F_IPV6;
2589 vs = vxlan_socket_create(net, port, rcv, data, flags);
2593 if (no_share) /* Return error if sharing is not allowed. */
2596 spin_lock(&vn->sock_lock);
2597 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
2598 if (vs && ((vs->rcv != rcv) ||
2599 !atomic_add_unless(&vs->refcnt, 1, 0)))
2600 vs = ERR_PTR(-EBUSY);
2601 spin_unlock(&vn->sock_lock);
2604 vs = ERR_PTR(-EINVAL);
2608 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2610 /* Scheduled at device creation to bind to a socket */
2611 static void vxlan_sock_work(struct work_struct *work)
2613 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2614 struct net *net = vxlan->net;
2615 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2616 __be16 port = vxlan->dst_port;
2617 struct vxlan_sock *nvs;
2619 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
2620 spin_lock(&vn->sock_lock);
2622 vxlan_vs_add_dev(nvs, vxlan);
2623 spin_unlock(&vn->sock_lock);
2625 dev_put(vxlan->dev);
2628 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2629 struct nlattr *tb[], struct nlattr *data[])
2631 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2632 struct vxlan_dev *vxlan = netdev_priv(dev);
2633 struct vxlan_rdst *dst = &vxlan->default_dst;
2636 bool use_ipv6 = false;
2638 if (!data[IFLA_VXLAN_ID])
2641 vxlan->net = src_net;
2643 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2644 dst->remote_vni = vni;
2646 /* Unless IPv6 is explicitly requested, assume IPv4 */
2647 dst->remote_ip.sa.sa_family = AF_INET;
2648 if (data[IFLA_VXLAN_GROUP]) {
2649 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2650 } else if (data[IFLA_VXLAN_GROUP6]) {
2651 if (!IS_ENABLED(CONFIG_IPV6))
2652 return -EPFNOSUPPORT;
2654 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2655 sizeof(struct in6_addr));
2656 dst->remote_ip.sa.sa_family = AF_INET6;
2660 if (data[IFLA_VXLAN_LOCAL]) {
2661 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2662 vxlan->saddr.sa.sa_family = AF_INET;
2663 } else if (data[IFLA_VXLAN_LOCAL6]) {
2664 if (!IS_ENABLED(CONFIG_IPV6))
2665 return -EPFNOSUPPORT;
2667 /* TODO: respect scope id */
2668 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2669 sizeof(struct in6_addr));
2670 vxlan->saddr.sa.sa_family = AF_INET6;
2674 if (data[IFLA_VXLAN_LINK] &&
2675 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2676 struct net_device *lowerdev
2677 = __dev_get_by_index(src_net, dst->remote_ifindex);
2680 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2684 #if IS_ENABLED(CONFIG_IPV6)
2686 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2687 if (idev && idev->cnf.disable_ipv6) {
2688 pr_info("IPv6 is disabled via sysctl\n");
2691 vxlan->flags |= VXLAN_F_IPV6;
2696 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2698 dev->needed_headroom = lowerdev->hard_header_len +
2699 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2700 } else if (use_ipv6)
2701 vxlan->flags |= VXLAN_F_IPV6;
2703 if (data[IFLA_VXLAN_TOS])
2704 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2706 if (data[IFLA_VXLAN_TTL])
2707 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2709 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2710 vxlan->flags |= VXLAN_F_LEARN;
2712 if (data[IFLA_VXLAN_AGEING])
2713 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2715 vxlan->age_interval = FDB_AGE_DEFAULT;
2717 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2718 vxlan->flags |= VXLAN_F_PROXY;
2720 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2721 vxlan->flags |= VXLAN_F_RSC;
2723 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2724 vxlan->flags |= VXLAN_F_L2MISS;
2726 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2727 vxlan->flags |= VXLAN_F_L3MISS;
2729 if (data[IFLA_VXLAN_LIMIT])
2730 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2732 if (data[IFLA_VXLAN_PORT_RANGE]) {
2733 const struct ifla_vxlan_port_range *p
2734 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2735 vxlan->port_min = ntohs(p->low);
2736 vxlan->port_max = ntohs(p->high);
2739 if (data[IFLA_VXLAN_PORT])
2740 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2742 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2743 vxlan->flags |= VXLAN_F_UDP_CSUM;
2745 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2746 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2747 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2749 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2750 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2751 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2753 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2754 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2755 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2757 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2758 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2759 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2761 if (data[IFLA_VXLAN_GBP])
2762 vxlan->flags |= VXLAN_F_GBP;
2764 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2765 vxlan->dst_port, vxlan->flags)) {
2766 pr_info("duplicate VNI %u\n", vni);
2770 dev->ethtool_ops = &vxlan_ethtool_ops;
2772 /* create an fdb entry for a valid default destination */
2773 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2774 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2775 &vxlan->default_dst.remote_ip,
2776 NUD_REACHABLE|NUD_PERMANENT,
2777 NLM_F_EXCL|NLM_F_CREATE,
2779 vxlan->default_dst.remote_vni,
2780 vxlan->default_dst.remote_ifindex,
2786 err = register_netdevice(dev);
2788 vxlan_fdb_delete_default(vxlan);
2792 list_add(&vxlan->next, &vn->vxlan_list);
2797 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2799 struct vxlan_dev *vxlan = netdev_priv(dev);
2800 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2802 spin_lock(&vn->sock_lock);
2803 if (!hlist_unhashed(&vxlan->hlist))
2804 hlist_del_rcu(&vxlan->hlist);
2805 spin_unlock(&vn->sock_lock);
2807 list_del(&vxlan->next);
2808 unregister_netdevice_queue(dev, head);
2811 static size_t vxlan_get_size(const struct net_device *dev)
2814 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2815 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2816 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2817 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2818 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2819 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2820 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2821 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2822 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2823 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2824 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2825 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2826 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2827 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2828 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2837 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2839 const struct vxlan_dev *vxlan = netdev_priv(dev);
2840 const struct vxlan_rdst *dst = &vxlan->default_dst;
2841 struct ifla_vxlan_port_range ports = {
2842 .low = htons(vxlan->port_min),
2843 .high = htons(vxlan->port_max),
2846 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2847 goto nla_put_failure;
2849 if (!vxlan_addr_any(&dst->remote_ip)) {
2850 if (dst->remote_ip.sa.sa_family == AF_INET) {
2851 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2852 dst->remote_ip.sin.sin_addr.s_addr))
2853 goto nla_put_failure;
2854 #if IS_ENABLED(CONFIG_IPV6)
2856 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2857 &dst->remote_ip.sin6.sin6_addr))
2858 goto nla_put_failure;
2863 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2864 goto nla_put_failure;
2866 if (!vxlan_addr_any(&vxlan->saddr)) {
2867 if (vxlan->saddr.sa.sa_family == AF_INET) {
2868 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2869 vxlan->saddr.sin.sin_addr.s_addr))
2870 goto nla_put_failure;
2871 #if IS_ENABLED(CONFIG_IPV6)
2873 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2874 &vxlan->saddr.sin6.sin6_addr))
2875 goto nla_put_failure;
2880 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2881 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2882 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2883 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2884 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2885 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2886 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2887 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2888 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2889 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2890 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2891 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2892 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2893 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2894 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2895 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2896 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2897 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2898 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2899 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2900 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2901 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2902 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2903 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2904 goto nla_put_failure;
2906 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2907 goto nla_put_failure;
2909 if (vxlan->flags & VXLAN_F_GBP &&
2910 nla_put_flag(skb, IFLA_VXLAN_GBP))
2911 goto nla_put_failure;
2919 static struct net *vxlan_get_link_net(const struct net_device *dev)
2921 struct vxlan_dev *vxlan = netdev_priv(dev);
2926 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2928 .maxtype = IFLA_VXLAN_MAX,
2929 .policy = vxlan_policy,
2930 .priv_size = sizeof(struct vxlan_dev),
2931 .setup = vxlan_setup,
2932 .validate = vxlan_validate,
2933 .newlink = vxlan_newlink,
2934 .dellink = vxlan_dellink,
2935 .get_size = vxlan_get_size,
2936 .fill_info = vxlan_fill_info,
2937 .get_link_net = vxlan_get_link_net,
2940 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2941 struct net_device *dev)
2943 struct vxlan_dev *vxlan, *next;
2944 LIST_HEAD(list_kill);
2946 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2947 struct vxlan_rdst *dst = &vxlan->default_dst;
2949 /* In case we created vxlan device with carrier
2950 * and we loose the carrier due to module unload
2951 * we also need to remove vxlan device. In other
2952 * cases, it's not necessary and remote_ifindex
2953 * is 0 here, so no matches.
2955 if (dst->remote_ifindex == dev->ifindex)
2956 vxlan_dellink(vxlan->dev, &list_kill);
2959 unregister_netdevice_many(&list_kill);
2962 static int vxlan_lowerdev_event(struct notifier_block *unused,
2963 unsigned long event, void *ptr)
2965 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2966 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2968 if (event == NETDEV_UNREGISTER)
2969 vxlan_handle_lowerdev_unregister(vn, dev);
2974 static struct notifier_block vxlan_notifier_block __read_mostly = {
2975 .notifier_call = vxlan_lowerdev_event,
2978 static __net_init int vxlan_init_net(struct net *net)
2980 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2983 INIT_LIST_HEAD(&vn->vxlan_list);
2984 spin_lock_init(&vn->sock_lock);
2986 for (h = 0; h < PORT_HASH_SIZE; ++h)
2987 INIT_HLIST_HEAD(&vn->sock_list[h]);
2992 static void __net_exit vxlan_exit_net(struct net *net)
2994 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2995 struct vxlan_dev *vxlan, *next;
2996 struct net_device *dev, *aux;
3000 for_each_netdev_safe(net, dev, aux)
3001 if (dev->rtnl_link_ops == &vxlan_link_ops)
3002 unregister_netdevice_queue(dev, &list);
3004 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3005 /* If vxlan->dev is in the same netns, it has already been added
3006 * to the list by the previous loop.
3008 if (!net_eq(dev_net(vxlan->dev), net))
3009 unregister_netdevice_queue(dev, &list);
3012 unregister_netdevice_many(&list);
3016 static struct pernet_operations vxlan_net_ops = {
3017 .init = vxlan_init_net,
3018 .exit = vxlan_exit_net,
3019 .id = &vxlan_net_id,
3020 .size = sizeof(struct vxlan_net),
3023 static int __init vxlan_init_module(void)
3027 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3031 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3033 rc = register_pernet_subsys(&vxlan_net_ops);
3037 rc = register_netdevice_notifier(&vxlan_notifier_block);
3041 rc = rtnl_link_register(&vxlan_link_ops);
3047 unregister_netdevice_notifier(&vxlan_notifier_block);
3049 unregister_pernet_subsys(&vxlan_net_ops);
3051 destroy_workqueue(vxlan_wq);
3054 late_initcall(vxlan_init_module);
3056 static void __exit vxlan_cleanup_module(void)
3058 rtnl_link_unregister(&vxlan_link_ops);
3059 unregister_netdevice_notifier(&vxlan_notifier_block);
3060 destroy_workqueue(vxlan_wq);
3061 unregister_pernet_subsys(&vxlan_net_ops);
3062 /* rcu_barrier() is called by netns */
3064 module_exit(vxlan_cleanup_module);
3066 MODULE_LICENSE("GPL");
3067 MODULE_VERSION(VXLAN_VERSION);
3068 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3069 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3070 MODULE_ALIAS_RTNL_LINK("vxlan");