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 static struct vxlan_sock *vxlan_find_sock(struct net *net,
268 sa_family_t family, __be16 port)
270 struct vxlan_sock *vs;
272 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
273 if (inet_sk(vs->sock->sk)->inet_sport == port &&
274 inet_sk(vs->sock->sk)->sk.sk_family == family)
280 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
282 struct vxlan_dev *vxlan;
284 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
285 if (vxlan->default_dst.remote_vni == id)
292 /* Look up VNI in a per net namespace table */
293 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
294 sa_family_t family, __be16 port)
296 struct vxlan_sock *vs;
298 vs = vxlan_find_sock(net, family, port);
302 return vxlan_vs_find_vni(vs, id);
305 /* Fill in neighbour message in skbuff. */
306 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
307 const struct vxlan_fdb *fdb,
308 u32 portid, u32 seq, int type, unsigned int flags,
309 const struct vxlan_rdst *rdst)
311 unsigned long now = jiffies;
312 struct nda_cacheinfo ci;
313 struct nlmsghdr *nlh;
315 bool send_ip, send_eth;
317 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
321 ndm = nlmsg_data(nlh);
322 memset(ndm, 0, sizeof(*ndm));
324 send_eth = send_ip = true;
326 if (type == RTM_GETNEIGH) {
327 ndm->ndm_family = AF_INET;
328 send_ip = !vxlan_addr_any(&rdst->remote_ip);
329 send_eth = !is_zero_ether_addr(fdb->eth_addr);
331 ndm->ndm_family = AF_BRIDGE;
332 ndm->ndm_state = fdb->state;
333 ndm->ndm_ifindex = vxlan->dev->ifindex;
334 ndm->ndm_flags = fdb->flags;
335 ndm->ndm_type = RTN_UNICAST;
337 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
338 goto nla_put_failure;
340 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
341 goto nla_put_failure;
343 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
344 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
345 goto nla_put_failure;
346 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
347 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
348 goto nla_put_failure;
349 if (rdst->remote_ifindex &&
350 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
351 goto nla_put_failure;
353 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
354 ci.ndm_confirmed = 0;
355 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
358 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
359 goto nla_put_failure;
361 return nlmsg_end(skb, nlh);
364 nlmsg_cancel(skb, nlh);
368 static inline size_t vxlan_nlmsg_size(void)
370 return NLMSG_ALIGN(sizeof(struct ndmsg))
371 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
372 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
373 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
374 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
375 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
376 + nla_total_size(sizeof(struct nda_cacheinfo));
379 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
380 struct vxlan_rdst *rd, int type)
382 struct net *net = dev_net(vxlan->dev);
386 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
390 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
392 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
393 WARN_ON(err == -EMSGSIZE);
398 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
402 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
405 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
407 struct vxlan_dev *vxlan = netdev_priv(dev);
408 struct vxlan_fdb f = {
411 struct vxlan_rdst remote = {
412 .remote_ip = *ipa, /* goes to NDA_DST */
413 .remote_vni = VXLAN_N_VID,
416 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
419 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
421 struct vxlan_fdb f = {
424 struct vxlan_rdst remote = { };
426 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
428 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
431 /* Hash Ethernet address */
432 static u32 eth_hash(const unsigned char *addr)
434 u64 value = get_unaligned((u64 *)addr);
436 /* only want 6 bytes */
442 return hash_64(value, FDB_HASH_BITS);
445 /* Hash chain to use given mac address */
446 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
449 return &vxlan->fdb_head[eth_hash(mac)];
452 /* Look up Ethernet address in forwarding table */
453 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
456 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
459 hlist_for_each_entry_rcu(f, head, hlist) {
460 if (ether_addr_equal(mac, f->eth_addr))
467 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
472 f = __vxlan_find_mac(vxlan, mac);
479 /* caller should hold vxlan->hash_lock */
480 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
481 union vxlan_addr *ip, __be16 port,
482 __u32 vni, __u32 ifindex)
484 struct vxlan_rdst *rd;
486 list_for_each_entry(rd, &f->remotes, list) {
487 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
488 rd->remote_port == port &&
489 rd->remote_vni == vni &&
490 rd->remote_ifindex == ifindex)
497 /* Replace destination of unicast mac */
498 static int vxlan_fdb_replace(struct vxlan_fdb *f,
499 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
501 struct vxlan_rdst *rd;
503 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
507 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
511 rd->remote_port = port;
512 rd->remote_vni = vni;
513 rd->remote_ifindex = ifindex;
517 /* Add/update destinations for multicast */
518 static int vxlan_fdb_append(struct vxlan_fdb *f,
519 union vxlan_addr *ip, __be16 port, __u32 vni,
520 __u32 ifindex, struct vxlan_rdst **rdp)
522 struct vxlan_rdst *rd;
524 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
528 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
532 rd->remote_port = port;
533 rd->remote_vni = vni;
534 rd->remote_ifindex = ifindex;
536 list_add_tail_rcu(&rd->list, &f->remotes);
542 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
544 struct vxlanhdr *vh, size_t hdrlen,
547 size_t start, offset, plen;
550 if (skb->remcsum_offload)
553 if (!NAPI_GRO_CB(skb)->csum_valid)
556 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
557 offset = start + ((data & VXLAN_RCO_UDP) ?
558 offsetof(struct udphdr, check) :
559 offsetof(struct tcphdr, check));
561 plen = hdrlen + offset + sizeof(u16);
563 /* Pull checksum that will be written */
564 if (skb_gro_header_hard(skb, off + plen)) {
565 vh = skb_gro_header_slow(skb, off + plen, off);
570 delta = remcsum_adjust((void *)vh + hdrlen,
571 NAPI_GRO_CB(skb)->csum, start, offset);
573 /* Adjust skb->csum since we changed the packet */
574 skb->csum = csum_add(skb->csum, delta);
575 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
577 skb->remcsum_offload = 1;
582 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
584 struct udp_offload *uoff)
586 struct sk_buff *p, **pp = NULL;
587 struct vxlanhdr *vh, *vh2;
588 unsigned int hlen, off_vx;
590 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
594 off_vx = skb_gro_offset(skb);
595 hlen = off_vx + sizeof(*vh);
596 vh = skb_gro_header_fast(skb, off_vx);
597 if (skb_gro_header_hard(skb, hlen)) {
598 vh = skb_gro_header_slow(skb, hlen, off_vx);
603 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
604 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
606 flags = ntohl(vh->vx_flags);
608 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
609 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
618 for (p = *head; p; p = p->next) {
619 if (!NAPI_GRO_CB(p)->same_flow)
622 vh2 = (struct vxlanhdr *)(p->data + off_vx);
623 if (vh->vx_vni != vh2->vx_vni) {
624 NAPI_GRO_CB(p)->same_flow = 0;
629 pp = eth_gro_receive(head, skb);
632 NAPI_GRO_CB(skb)->flush |= flush;
637 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
638 struct udp_offload *uoff)
640 udp_tunnel_gro_complete(skb, nhoff);
642 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
645 /* Notify netdevs that UDP port started listening */
646 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
648 struct net_device *dev;
649 struct sock *sk = vs->sock->sk;
650 struct net *net = sock_net(sk);
651 sa_family_t sa_family = sk->sk_family;
652 __be16 port = inet_sk(sk)->inet_sport;
655 if (sa_family == AF_INET) {
656 err = udp_add_offload(&vs->udp_offloads);
658 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
662 for_each_netdev_rcu(net, dev) {
663 if (dev->netdev_ops->ndo_add_vxlan_port)
664 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
670 /* Notify netdevs that UDP port is no more listening */
671 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
673 struct net_device *dev;
674 struct sock *sk = vs->sock->sk;
675 struct net *net = sock_net(sk);
676 sa_family_t sa_family = sk->sk_family;
677 __be16 port = inet_sk(sk)->inet_sport;
680 for_each_netdev_rcu(net, dev) {
681 if (dev->netdev_ops->ndo_del_vxlan_port)
682 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
687 if (sa_family == AF_INET)
688 udp_del_offload(&vs->udp_offloads);
691 /* Add new entry to forwarding table -- assumes lock held */
692 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
693 const u8 *mac, union vxlan_addr *ip,
694 __u16 state, __u16 flags,
695 __be16 port, __u32 vni, __u32 ifindex,
698 struct vxlan_rdst *rd = NULL;
702 f = __vxlan_find_mac(vxlan, mac);
704 if (flags & NLM_F_EXCL) {
705 netdev_dbg(vxlan->dev,
706 "lost race to create %pM\n", mac);
709 if (f->state != state) {
711 f->updated = jiffies;
714 if (f->flags != ndm_flags) {
715 f->flags = ndm_flags;
716 f->updated = jiffies;
719 if ((flags & NLM_F_REPLACE)) {
720 /* Only change unicasts */
721 if (!(is_multicast_ether_addr(f->eth_addr) ||
722 is_zero_ether_addr(f->eth_addr))) {
723 int rc = vxlan_fdb_replace(f, ip, port, vni,
732 if ((flags & NLM_F_APPEND) &&
733 (is_multicast_ether_addr(f->eth_addr) ||
734 is_zero_ether_addr(f->eth_addr))) {
735 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
743 if (!(flags & NLM_F_CREATE))
746 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
749 /* Disallow replace to add a multicast entry */
750 if ((flags & NLM_F_REPLACE) &&
751 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
754 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
755 f = kmalloc(sizeof(*f), GFP_ATOMIC);
761 f->flags = ndm_flags;
762 f->updated = f->used = jiffies;
763 INIT_LIST_HEAD(&f->remotes);
764 memcpy(f->eth_addr, mac, ETH_ALEN);
766 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
769 hlist_add_head_rcu(&f->hlist,
770 vxlan_fdb_head(vxlan, mac));
775 rd = first_remote_rtnl(f);
776 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
782 static void vxlan_fdb_free(struct rcu_head *head)
784 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
785 struct vxlan_rdst *rd, *nd;
787 list_for_each_entry_safe(rd, nd, &f->remotes, list)
792 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
794 netdev_dbg(vxlan->dev,
795 "delete %pM\n", f->eth_addr);
798 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
800 hlist_del_rcu(&f->hlist);
801 call_rcu(&f->rcu, vxlan_fdb_free);
804 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
805 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
807 struct net *net = dev_net(vxlan->dev);
811 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
815 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
816 if (remote->sa.sa_family == AF_INET) {
817 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
818 ip->sa.sa_family = AF_INET;
819 #if IS_ENABLED(CONFIG_IPV6)
821 ip->sin6.sin6_addr = in6addr_any;
822 ip->sa.sa_family = AF_INET6;
828 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
830 *port = nla_get_be16(tb[NDA_PORT]);
832 *port = vxlan->dst_port;
836 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
838 *vni = nla_get_u32(tb[NDA_VNI]);
840 *vni = vxlan->default_dst.remote_vni;
843 if (tb[NDA_IFINDEX]) {
844 struct net_device *tdev;
846 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
848 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
849 tdev = __dev_get_by_index(net, *ifindex);
851 return -EADDRNOTAVAIL;
859 /* Add static entry (via netlink) */
860 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
861 struct net_device *dev,
862 const unsigned char *addr, u16 vid, u16 flags)
864 struct vxlan_dev *vxlan = netdev_priv(dev);
865 /* struct net *net = dev_net(vxlan->dev); */
871 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
872 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
877 if (tb[NDA_DST] == NULL)
880 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
884 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
885 return -EAFNOSUPPORT;
887 spin_lock_bh(&vxlan->hash_lock);
888 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
889 port, vni, ifindex, ndm->ndm_flags);
890 spin_unlock_bh(&vxlan->hash_lock);
895 /* Delete entry (via netlink) */
896 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
897 struct net_device *dev,
898 const unsigned char *addr, u16 vid)
900 struct vxlan_dev *vxlan = netdev_priv(dev);
902 struct vxlan_rdst *rd = NULL;
908 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
914 spin_lock_bh(&vxlan->hash_lock);
915 f = vxlan_find_mac(vxlan, addr);
919 if (!vxlan_addr_any(&ip)) {
920 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
927 /* remove a destination if it's not the only one on the list,
928 * otherwise destroy the fdb entry
930 if (rd && !list_is_singular(&f->remotes)) {
931 list_del_rcu(&rd->list);
932 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
937 vxlan_fdb_destroy(vxlan, f);
940 spin_unlock_bh(&vxlan->hash_lock);
945 /* Dump forwarding table */
946 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
947 struct net_device *dev,
948 struct net_device *filter_dev, int idx)
950 struct vxlan_dev *vxlan = netdev_priv(dev);
953 for (h = 0; h < FDB_HASH_SIZE; ++h) {
957 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
958 struct vxlan_rdst *rd;
960 if (idx < cb->args[0])
963 list_for_each_entry_rcu(rd, &f->remotes, list) {
964 err = vxlan_fdb_info(skb, vxlan, f,
965 NETLINK_CB(cb->skb).portid,
980 /* Watch incoming packets to learn mapping between Ethernet address
981 * and Tunnel endpoint.
982 * Return true if packet is bogus and should be droppped.
984 static bool vxlan_snoop(struct net_device *dev,
985 union vxlan_addr *src_ip, const u8 *src_mac)
987 struct vxlan_dev *vxlan = netdev_priv(dev);
990 f = vxlan_find_mac(vxlan, src_mac);
992 struct vxlan_rdst *rdst = first_remote_rcu(f);
994 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
997 /* Don't migrate static entries, drop packets */
998 if (f->state & NUD_NOARP)
1001 if (net_ratelimit())
1003 "%pM migrated from %pIS to %pIS\n",
1004 src_mac, &rdst->remote_ip, &src_ip);
1006 rdst->remote_ip = *src_ip;
1007 f->updated = jiffies;
1008 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
1010 /* learned new entry */
1011 spin_lock(&vxlan->hash_lock);
1013 /* close off race between vxlan_flush and incoming packets */
1014 if (netif_running(dev))
1015 vxlan_fdb_create(vxlan, src_mac, src_ip,
1017 NLM_F_EXCL|NLM_F_CREATE,
1019 vxlan->default_dst.remote_vni,
1021 spin_unlock(&vxlan->hash_lock);
1027 /* See if multicast group is already in use by other ID */
1028 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1030 struct vxlan_dev *vxlan;
1032 /* The vxlan_sock is only used by dev, leaving group has
1033 * no effect on other vxlan devices.
1035 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1038 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1039 if (!netif_running(vxlan->dev) || vxlan == dev)
1042 if (vxlan->vn_sock != dev->vn_sock)
1045 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1046 &dev->default_dst.remote_ip))
1049 if (vxlan->default_dst.remote_ifindex !=
1050 dev->default_dst.remote_ifindex)
1059 static void vxlan_sock_hold(struct vxlan_sock *vs)
1061 atomic_inc(&vs->refcnt);
1064 void vxlan_sock_release(struct vxlan_sock *vs)
1066 struct sock *sk = vs->sock->sk;
1067 struct net *net = sock_net(sk);
1068 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1070 if (!atomic_dec_and_test(&vs->refcnt))
1073 spin_lock(&vn->sock_lock);
1074 hlist_del_rcu(&vs->hlist);
1075 vxlan_notify_del_rx_port(vs);
1076 spin_unlock(&vn->sock_lock);
1078 queue_work(vxlan_wq, &vs->del_work);
1080 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1082 /* Callback to update multicast group membership when first VNI on
1083 * multicast asddress is brought up
1084 * Done as workqueue because ip_mc_join_group acquires RTNL.
1086 static void vxlan_igmp_join(struct work_struct *work)
1088 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
1089 struct vxlan_sock *vs = vxlan->vn_sock;
1090 struct sock *sk = vs->sock->sk;
1091 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1092 int ifindex = vxlan->default_dst.remote_ifindex;
1095 if (ip->sa.sa_family == AF_INET) {
1096 struct ip_mreqn mreq = {
1097 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1098 .imr_ifindex = ifindex,
1101 ip_mc_join_group(sk, &mreq);
1102 #if IS_ENABLED(CONFIG_IPV6)
1104 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1105 &ip->sin6.sin6_addr);
1110 vxlan_sock_release(vs);
1111 dev_put(vxlan->dev);
1114 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1115 static void vxlan_igmp_leave(struct work_struct *work)
1117 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1118 struct vxlan_sock *vs = vxlan->vn_sock;
1119 struct sock *sk = vs->sock->sk;
1120 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1121 int ifindex = vxlan->default_dst.remote_ifindex;
1124 if (ip->sa.sa_family == AF_INET) {
1125 struct ip_mreqn mreq = {
1126 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1127 .imr_ifindex = ifindex,
1130 ip_mc_leave_group(sk, &mreq);
1131 #if IS_ENABLED(CONFIG_IPV6)
1133 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1134 &ip->sin6.sin6_addr);
1140 vxlan_sock_release(vs);
1141 dev_put(vxlan->dev);
1144 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1145 size_t hdrlen, u32 data)
1147 size_t start, offset, plen;
1150 if (skb->remcsum_offload) {
1151 /* Already processed in GRO path */
1152 skb->remcsum_offload = 0;
1156 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1157 offset = start + ((data & VXLAN_RCO_UDP) ?
1158 offsetof(struct udphdr, check) :
1159 offsetof(struct tcphdr, check));
1161 plen = hdrlen + offset + sizeof(u16);
1163 if (!pskb_may_pull(skb, plen))
1166 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1168 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1169 __skb_checksum_complete(skb);
1171 delta = remcsum_adjust((void *)vh + hdrlen,
1172 skb->csum, start, offset);
1174 /* Adjust skb->csum since we changed the packet */
1175 skb->csum = csum_add(skb->csum, delta);
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;
1187 /* Need Vxlan and inner Ethernet header to be present */
1188 if (!pskb_may_pull(skb, VXLAN_HLEN))
1191 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1192 flags = ntohl(vxh->vx_flags);
1193 vni = ntohl(vxh->vx_vni);
1195 if (flags & VXLAN_HF_VNI) {
1196 flags &= ~VXLAN_HF_VNI;
1198 /* VNI flag always required to be set */
1202 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1204 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1206 vs = rcu_dereference_sk_user_data(sk);
1210 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1211 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1215 flags &= ~VXLAN_HF_RCO;
1216 vni &= VXLAN_VID_MASK;
1219 if (flags || (vni & ~VXLAN_VID_MASK)) {
1220 /* If there are any unprocessed flags remaining treat
1221 * this as a malformed packet. This behavior diverges from
1222 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1223 * in reserved fields are to be ignored. The approach here
1224 * maintains compatbility with previous stack code, and also
1225 * is more robust and provides a little more security in
1226 * adding extensions to VXLAN.
1232 vs->rcv(vs, skb, vxh->vx_vni);
1236 /* Consume bad packet */
1241 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1242 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1245 /* Return non vxlan pkt */
1249 static void vxlan_rcv(struct vxlan_sock *vs,
1250 struct sk_buff *skb, __be32 vx_vni)
1252 struct iphdr *oip = NULL;
1253 struct ipv6hdr *oip6 = NULL;
1254 struct vxlan_dev *vxlan;
1255 struct pcpu_sw_netstats *stats;
1256 union vxlan_addr saddr;
1259 union vxlan_addr *remote_ip;
1261 vni = ntohl(vx_vni) >> 8;
1262 /* Is this VNI defined? */
1263 vxlan = vxlan_vs_find_vni(vs, vni);
1267 remote_ip = &vxlan->default_dst.remote_ip;
1268 skb_reset_mac_header(skb);
1269 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1270 skb->protocol = eth_type_trans(skb, vxlan->dev);
1271 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1273 /* Ignore packet loops (and multicast echo) */
1274 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1277 /* Re-examine inner Ethernet packet */
1278 if (remote_ip->sa.sa_family == AF_INET) {
1280 saddr.sin.sin_addr.s_addr = oip->saddr;
1281 saddr.sa.sa_family = AF_INET;
1282 #if IS_ENABLED(CONFIG_IPV6)
1284 oip6 = ipv6_hdr(skb);
1285 saddr.sin6.sin6_addr = oip6->saddr;
1286 saddr.sa.sa_family = AF_INET6;
1290 if ((vxlan->flags & VXLAN_F_LEARN) &&
1291 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1294 skb_reset_network_header(skb);
1297 err = IP6_ECN_decapsulate(oip6, skb);
1299 err = IP_ECN_decapsulate(oip, skb);
1301 if (unlikely(err)) {
1302 if (log_ecn_error) {
1304 net_info_ratelimited("non-ECT from %pI6\n",
1307 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1308 &oip->saddr, oip->tos);
1311 ++vxlan->dev->stats.rx_frame_errors;
1312 ++vxlan->dev->stats.rx_errors;
1317 stats = this_cpu_ptr(vxlan->dev->tstats);
1318 u64_stats_update_begin(&stats->syncp);
1319 stats->rx_packets++;
1320 stats->rx_bytes += skb->len;
1321 u64_stats_update_end(&stats->syncp);
1327 /* Consume bad packet */
1331 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1333 struct vxlan_dev *vxlan = netdev_priv(dev);
1334 struct arphdr *parp;
1337 struct neighbour *n;
1339 if (dev->flags & IFF_NOARP)
1342 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1343 dev->stats.tx_dropped++;
1346 parp = arp_hdr(skb);
1348 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1349 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1350 parp->ar_pro != htons(ETH_P_IP) ||
1351 parp->ar_op != htons(ARPOP_REQUEST) ||
1352 parp->ar_hln != dev->addr_len ||
1355 arpptr = (u8 *)parp + sizeof(struct arphdr);
1357 arpptr += dev->addr_len; /* sha */
1358 memcpy(&sip, arpptr, sizeof(sip));
1359 arpptr += sizeof(sip);
1360 arpptr += dev->addr_len; /* tha */
1361 memcpy(&tip, arpptr, sizeof(tip));
1363 if (ipv4_is_loopback(tip) ||
1364 ipv4_is_multicast(tip))
1367 n = neigh_lookup(&arp_tbl, &tip, dev);
1370 struct vxlan_fdb *f;
1371 struct sk_buff *reply;
1373 if (!(n->nud_state & NUD_CONNECTED)) {
1378 f = vxlan_find_mac(vxlan, n->ha);
1379 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1380 /* bridge-local neighbor */
1385 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1393 skb_reset_mac_header(reply);
1394 __skb_pull(reply, skb_network_offset(reply));
1395 reply->ip_summed = CHECKSUM_UNNECESSARY;
1396 reply->pkt_type = PACKET_HOST;
1398 if (netif_rx_ni(reply) == NET_RX_DROP)
1399 dev->stats.rx_dropped++;
1400 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1401 union vxlan_addr ipa = {
1402 .sin.sin_addr.s_addr = tip,
1403 .sin.sin_family = AF_INET,
1406 vxlan_ip_miss(dev, &ipa);
1410 return NETDEV_TX_OK;
1413 #if IS_ENABLED(CONFIG_IPV6)
1414 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1415 struct neighbour *n, bool isrouter)
1417 struct net_device *dev = request->dev;
1418 struct sk_buff *reply;
1419 struct nd_msg *ns, *na;
1420 struct ipv6hdr *pip6;
1422 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1429 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1430 sizeof(*na) + na_olen + dev->needed_tailroom;
1431 reply = alloc_skb(len, GFP_ATOMIC);
1435 reply->protocol = htons(ETH_P_IPV6);
1437 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1438 skb_push(reply, sizeof(struct ethhdr));
1439 skb_set_mac_header(reply, 0);
1441 ns = (struct nd_msg *)skb_transport_header(request);
1443 daddr = eth_hdr(request)->h_source;
1444 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1445 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1446 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1447 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1452 /* Ethernet header */
1453 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1454 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1455 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1456 reply->protocol = htons(ETH_P_IPV6);
1458 skb_pull(reply, sizeof(struct ethhdr));
1459 skb_set_network_header(reply, 0);
1460 skb_put(reply, sizeof(struct ipv6hdr));
1464 pip6 = ipv6_hdr(reply);
1465 memset(pip6, 0, sizeof(struct ipv6hdr));
1467 pip6->priority = ipv6_hdr(request)->priority;
1468 pip6->nexthdr = IPPROTO_ICMPV6;
1469 pip6->hop_limit = 255;
1470 pip6->daddr = ipv6_hdr(request)->saddr;
1471 pip6->saddr = *(struct in6_addr *)n->primary_key;
1473 skb_pull(reply, sizeof(struct ipv6hdr));
1474 skb_set_transport_header(reply, 0);
1476 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1478 /* Neighbor Advertisement */
1479 memset(na, 0, sizeof(*na)+na_olen);
1480 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1481 na->icmph.icmp6_router = isrouter;
1482 na->icmph.icmp6_override = 1;
1483 na->icmph.icmp6_solicited = 1;
1484 na->target = ns->target;
1485 ether_addr_copy(&na->opt[2], n->ha);
1486 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1487 na->opt[1] = na_olen >> 3;
1489 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1490 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1491 csum_partial(na, sizeof(*na)+na_olen, 0));
1493 pip6->payload_len = htons(sizeof(*na)+na_olen);
1495 skb_push(reply, sizeof(struct ipv6hdr));
1497 reply->ip_summed = CHECKSUM_UNNECESSARY;
1502 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1504 struct vxlan_dev *vxlan = netdev_priv(dev);
1506 const struct ipv6hdr *iphdr;
1507 const struct in6_addr *saddr, *daddr;
1508 struct neighbour *n;
1509 struct inet6_dev *in6_dev;
1511 in6_dev = __in6_dev_get(dev);
1515 iphdr = ipv6_hdr(skb);
1516 saddr = &iphdr->saddr;
1517 daddr = &iphdr->daddr;
1519 msg = (struct nd_msg *)skb_transport_header(skb);
1520 if (msg->icmph.icmp6_code != 0 ||
1521 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1524 if (ipv6_addr_loopback(daddr) ||
1525 ipv6_addr_is_multicast(&msg->target))
1528 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1531 struct vxlan_fdb *f;
1532 struct sk_buff *reply;
1534 if (!(n->nud_state & NUD_CONNECTED)) {
1539 f = vxlan_find_mac(vxlan, n->ha);
1540 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1541 /* bridge-local neighbor */
1546 reply = vxlan_na_create(skb, n,
1547 !!(f ? f->flags & NTF_ROUTER : 0));
1554 if (netif_rx_ni(reply) == NET_RX_DROP)
1555 dev->stats.rx_dropped++;
1557 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1558 union vxlan_addr ipa = {
1559 .sin6.sin6_addr = msg->target,
1560 .sin6.sin6_family = AF_INET6,
1563 vxlan_ip_miss(dev, &ipa);
1568 return NETDEV_TX_OK;
1572 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1574 struct vxlan_dev *vxlan = netdev_priv(dev);
1575 struct neighbour *n;
1577 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1581 switch (ntohs(eth_hdr(skb)->h_proto)) {
1586 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1589 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1590 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1591 union vxlan_addr ipa = {
1592 .sin.sin_addr.s_addr = pip->daddr,
1593 .sin.sin_family = AF_INET,
1596 vxlan_ip_miss(dev, &ipa);
1602 #if IS_ENABLED(CONFIG_IPV6)
1605 struct ipv6hdr *pip6;
1607 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1609 pip6 = ipv6_hdr(skb);
1610 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1611 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1612 union vxlan_addr ipa = {
1613 .sin6.sin6_addr = pip6->daddr,
1614 .sin6.sin6_family = AF_INET6,
1617 vxlan_ip_miss(dev, &ipa);
1631 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1633 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1635 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1644 #if IS_ENABLED(CONFIG_IPV6)
1645 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1646 struct dst_entry *dst, struct sk_buff *skb,
1647 struct net_device *dev, struct in6_addr *saddr,
1648 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1649 __be16 src_port, __be16 dst_port, __be32 vni,
1652 struct vxlanhdr *vxh;
1655 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
1656 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1657 u16 hdrlen = sizeof(struct vxlanhdr);
1659 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1660 skb->ip_summed == CHECKSUM_PARTIAL) {
1661 int csum_start = skb_checksum_start_offset(skb);
1663 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1664 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1665 (skb->csum_offset == offsetof(struct udphdr, check) ||
1666 skb->csum_offset == offsetof(struct tcphdr, check))) {
1668 type |= SKB_GSO_TUNNEL_REMCSUM;
1672 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1678 skb_scrub_packet(skb, xnet);
1680 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1681 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1682 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1684 /* Need space for new headers (invalidates iph ptr) */
1685 err = skb_cow_head(skb, min_headroom);
1686 if (unlikely(err)) {
1691 skb = vlan_hwaccel_push_inside(skb);
1692 if (WARN_ON(!skb)) {
1697 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1698 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1701 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1702 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1705 if (skb->csum_offset == offsetof(struct udphdr, check))
1706 data |= VXLAN_RCO_UDP;
1708 vxh->vx_vni |= htonl(data);
1709 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1711 if (!skb_is_gso(skb)) {
1712 skb->ip_summed = CHECKSUM_NONE;
1713 skb->encapsulation = 0;
1717 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1719 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1720 ttl, src_port, dst_port);
1728 int vxlan_xmit_skb(struct vxlan_sock *vs,
1729 struct rtable *rt, struct sk_buff *skb,
1730 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1731 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
1733 struct vxlanhdr *vxh;
1736 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
1737 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1738 u16 hdrlen = sizeof(struct vxlanhdr);
1740 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1741 skb->ip_summed == CHECKSUM_PARTIAL) {
1742 int csum_start = skb_checksum_start_offset(skb);
1744 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1745 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1746 (skb->csum_offset == offsetof(struct udphdr, check) ||
1747 skb->csum_offset == offsetof(struct tcphdr, check))) {
1749 type |= SKB_GSO_TUNNEL_REMCSUM;
1753 skb = iptunnel_handle_offloads(skb, udp_sum, type);
1755 return PTR_ERR(skb);
1757 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1758 + VXLAN_HLEN + sizeof(struct iphdr)
1759 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1761 /* Need space for new headers (invalidates iph ptr) */
1762 err = skb_cow_head(skb, min_headroom);
1763 if (unlikely(err)) {
1768 skb = vlan_hwaccel_push_inside(skb);
1772 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1773 vxh->vx_flags = htonl(VXLAN_HF_VNI);
1776 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1777 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1780 if (skb->csum_offset == offsetof(struct udphdr, check))
1781 data |= VXLAN_RCO_UDP;
1783 vxh->vx_vni |= htonl(data);
1784 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1786 if (!skb_is_gso(skb)) {
1787 skb->ip_summed = CHECKSUM_NONE;
1788 skb->encapsulation = 0;
1792 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1794 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1795 ttl, df, src_port, dst_port, xnet);
1797 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1799 /* Bypass encapsulation if the destination is local */
1800 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1801 struct vxlan_dev *dst_vxlan)
1803 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1804 union vxlan_addr loopback;
1805 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1806 struct net_device *dev = skb->dev;
1809 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1810 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1811 skb->pkt_type = PACKET_HOST;
1812 skb->encapsulation = 0;
1813 skb->dev = dst_vxlan->dev;
1814 __skb_pull(skb, skb_network_offset(skb));
1816 if (remote_ip->sa.sa_family == AF_INET) {
1817 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1818 loopback.sa.sa_family = AF_INET;
1819 #if IS_ENABLED(CONFIG_IPV6)
1821 loopback.sin6.sin6_addr = in6addr_loopback;
1822 loopback.sa.sa_family = AF_INET6;
1826 if (dst_vxlan->flags & VXLAN_F_LEARN)
1827 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1829 u64_stats_update_begin(&tx_stats->syncp);
1830 tx_stats->tx_packets++;
1831 tx_stats->tx_bytes += len;
1832 u64_stats_update_end(&tx_stats->syncp);
1834 if (netif_rx(skb) == NET_RX_SUCCESS) {
1835 u64_stats_update_begin(&rx_stats->syncp);
1836 rx_stats->rx_packets++;
1837 rx_stats->rx_bytes += len;
1838 u64_stats_update_end(&rx_stats->syncp);
1840 dev->stats.rx_dropped++;
1844 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1845 struct vxlan_rdst *rdst, bool did_rsc)
1847 struct vxlan_dev *vxlan = netdev_priv(dev);
1848 struct rtable *rt = NULL;
1849 const struct iphdr *old_iph;
1851 union vxlan_addr *dst;
1852 __be16 src_port = 0, dst_port;
1858 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1859 vni = rdst->remote_vni;
1860 dst = &rdst->remote_ip;
1862 if (vxlan_addr_any(dst)) {
1864 /* short-circuited back to local bridge */
1865 vxlan_encap_bypass(skb, vxlan, vxlan);
1871 old_iph = ip_hdr(skb);
1874 if (!ttl && vxlan_addr_multicast(dst))
1879 tos = ip_tunnel_get_dsfield(old_iph, skb);
1881 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1882 vxlan->port_max, true);
1884 if (dst->sa.sa_family == AF_INET) {
1885 memset(&fl4, 0, sizeof(fl4));
1886 fl4.flowi4_oif = rdst->remote_ifindex;
1887 fl4.flowi4_tos = RT_TOS(tos);
1888 fl4.daddr = dst->sin.sin_addr.s_addr;
1889 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1891 rt = ip_route_output_key(vxlan->net, &fl4);
1893 netdev_dbg(dev, "no route to %pI4\n",
1894 &dst->sin.sin_addr.s_addr);
1895 dev->stats.tx_carrier_errors++;
1899 if (rt->dst.dev == dev) {
1900 netdev_dbg(dev, "circular route to %pI4\n",
1901 &dst->sin.sin_addr.s_addr);
1902 dev->stats.collisions++;
1906 /* Bypass encapsulation if the destination is local */
1907 if (rt->rt_flags & RTCF_LOCAL &&
1908 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1909 struct vxlan_dev *dst_vxlan;
1912 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1913 dst->sa.sa_family, dst_port);
1916 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1920 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1921 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1923 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1924 fl4.saddr, dst->sin.sin_addr.s_addr,
1925 tos, ttl, df, src_port, dst_port,
1927 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1929 /* skb is already freed. */
1934 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1935 #if IS_ENABLED(CONFIG_IPV6)
1937 struct sock *sk = vxlan->vn_sock->sock->sk;
1938 struct dst_entry *ndst;
1942 memset(&fl6, 0, sizeof(fl6));
1943 fl6.flowi6_oif = rdst->remote_ifindex;
1944 fl6.daddr = dst->sin6.sin6_addr;
1945 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1946 fl6.flowi6_proto = IPPROTO_UDP;
1948 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1949 netdev_dbg(dev, "no route to %pI6\n",
1950 &dst->sin6.sin6_addr);
1951 dev->stats.tx_carrier_errors++;
1955 if (ndst->dev == dev) {
1956 netdev_dbg(dev, "circular route to %pI6\n",
1957 &dst->sin6.sin6_addr);
1959 dev->stats.collisions++;
1963 /* Bypass encapsulation if the destination is local */
1964 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1965 if (flags & RTF_LOCAL &&
1966 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1967 struct vxlan_dev *dst_vxlan;
1970 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1971 dst->sa.sa_family, dst_port);
1974 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1978 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1980 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
1981 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1982 src_port, dst_port, htonl(vni << 8),
1983 !net_eq(vxlan->net, dev_net(vxlan->dev)));
1990 dev->stats.tx_dropped++;
1996 dev->stats.tx_errors++;
2001 /* Transmit local packets over Vxlan
2003 * Outer IP header inherits ECN and DF from inner header.
2004 * Outer UDP destination is the VXLAN assigned port.
2005 * source port is based on hash of flow
2007 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2009 struct vxlan_dev *vxlan = netdev_priv(dev);
2011 bool did_rsc = false;
2012 struct vxlan_rdst *rdst, *fdst = NULL;
2013 struct vxlan_fdb *f;
2015 skb_reset_mac_header(skb);
2018 if ((vxlan->flags & VXLAN_F_PROXY)) {
2019 if (ntohs(eth->h_proto) == ETH_P_ARP)
2020 return arp_reduce(dev, skb);
2021 #if IS_ENABLED(CONFIG_IPV6)
2022 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2023 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2024 + sizeof(struct nd_msg)) &&
2025 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2028 msg = (struct nd_msg *)skb_transport_header(skb);
2029 if (msg->icmph.icmp6_code == 0 &&
2030 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2031 return neigh_reduce(dev, skb);
2037 f = vxlan_find_mac(vxlan, eth->h_dest);
2040 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2041 (ntohs(eth->h_proto) == ETH_P_IP ||
2042 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2043 did_rsc = route_shortcircuit(dev, skb);
2045 f = vxlan_find_mac(vxlan, eth->h_dest);
2049 f = vxlan_find_mac(vxlan, all_zeros_mac);
2051 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2052 !is_multicast_ether_addr(eth->h_dest))
2053 vxlan_fdb_miss(vxlan, eth->h_dest);
2055 dev->stats.tx_dropped++;
2057 return NETDEV_TX_OK;
2061 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2062 struct sk_buff *skb1;
2068 skb1 = skb_clone(skb, GFP_ATOMIC);
2070 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2074 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2077 return NETDEV_TX_OK;
2080 /* Walk the forwarding table and purge stale entries */
2081 static void vxlan_cleanup(unsigned long arg)
2083 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2084 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2087 if (!netif_running(vxlan->dev))
2090 spin_lock_bh(&vxlan->hash_lock);
2091 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2092 struct hlist_node *p, *n;
2093 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2095 = container_of(p, struct vxlan_fdb, hlist);
2096 unsigned long timeout;
2098 if (f->state & NUD_PERMANENT)
2101 timeout = f->used + vxlan->age_interval * HZ;
2102 if (time_before_eq(timeout, jiffies)) {
2103 netdev_dbg(vxlan->dev,
2104 "garbage collect %pM\n",
2106 f->state = NUD_STALE;
2107 vxlan_fdb_destroy(vxlan, f);
2108 } else if (time_before(timeout, next_timer))
2109 next_timer = timeout;
2112 spin_unlock_bh(&vxlan->hash_lock);
2114 mod_timer(&vxlan->age_timer, next_timer);
2117 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2119 __u32 vni = vxlan->default_dst.remote_vni;
2121 vxlan->vn_sock = vs;
2122 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2125 /* Setup stats when device is created */
2126 static int vxlan_init(struct net_device *dev)
2128 struct vxlan_dev *vxlan = netdev_priv(dev);
2129 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2130 struct vxlan_sock *vs;
2131 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2133 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2137 spin_lock(&vn->sock_lock);
2138 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2140 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
2141 /* If we have a socket with same port already, reuse it */
2142 vxlan_vs_add_dev(vs, vxlan);
2144 /* otherwise make new socket outside of RTNL */
2146 queue_work(vxlan_wq, &vxlan->sock_work);
2148 spin_unlock(&vn->sock_lock);
2153 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2155 struct vxlan_fdb *f;
2157 spin_lock_bh(&vxlan->hash_lock);
2158 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2160 vxlan_fdb_destroy(vxlan, f);
2161 spin_unlock_bh(&vxlan->hash_lock);
2164 static void vxlan_uninit(struct net_device *dev)
2166 struct vxlan_dev *vxlan = netdev_priv(dev);
2167 struct vxlan_sock *vs = vxlan->vn_sock;
2169 vxlan_fdb_delete_default(vxlan);
2172 vxlan_sock_release(vs);
2173 free_percpu(dev->tstats);
2176 /* Start ageing timer and join group when device is brought up */
2177 static int vxlan_open(struct net_device *dev)
2179 struct vxlan_dev *vxlan = netdev_priv(dev);
2180 struct vxlan_sock *vs = vxlan->vn_sock;
2182 /* socket hasn't been created */
2186 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2187 vxlan_sock_hold(vs);
2189 queue_work(vxlan_wq, &vxlan->igmp_join);
2192 if (vxlan->age_interval)
2193 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2198 /* Purge the forwarding table */
2199 static void vxlan_flush(struct vxlan_dev *vxlan)
2203 spin_lock_bh(&vxlan->hash_lock);
2204 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2205 struct hlist_node *p, *n;
2206 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2208 = container_of(p, struct vxlan_fdb, hlist);
2209 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2210 if (!is_zero_ether_addr(f->eth_addr))
2211 vxlan_fdb_destroy(vxlan, f);
2214 spin_unlock_bh(&vxlan->hash_lock);
2217 /* Cleanup timer and forwarding table on shutdown */
2218 static int vxlan_stop(struct net_device *dev)
2220 struct vxlan_dev *vxlan = netdev_priv(dev);
2221 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2222 struct vxlan_sock *vs = vxlan->vn_sock;
2224 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2225 !vxlan_group_used(vn, vxlan)) {
2226 vxlan_sock_hold(vs);
2228 queue_work(vxlan_wq, &vxlan->igmp_leave);
2231 del_timer_sync(&vxlan->age_timer);
2238 /* Stub, nothing needs to be done. */
2239 static void vxlan_set_multicast_list(struct net_device *dev)
2243 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2245 struct vxlan_dev *vxlan = netdev_priv(dev);
2246 struct vxlan_rdst *dst = &vxlan->default_dst;
2247 struct net_device *lowerdev;
2250 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2251 if (lowerdev == NULL)
2252 return eth_change_mtu(dev, new_mtu);
2254 if (dst->remote_ip.sa.sa_family == AF_INET6)
2255 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2257 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2259 if (new_mtu < 68 || new_mtu > max_mtu)
2266 static const struct net_device_ops vxlan_netdev_ops = {
2267 .ndo_init = vxlan_init,
2268 .ndo_uninit = vxlan_uninit,
2269 .ndo_open = vxlan_open,
2270 .ndo_stop = vxlan_stop,
2271 .ndo_start_xmit = vxlan_xmit,
2272 .ndo_get_stats64 = ip_tunnel_get_stats64,
2273 .ndo_set_rx_mode = vxlan_set_multicast_list,
2274 .ndo_change_mtu = vxlan_change_mtu,
2275 .ndo_validate_addr = eth_validate_addr,
2276 .ndo_set_mac_address = eth_mac_addr,
2277 .ndo_fdb_add = vxlan_fdb_add,
2278 .ndo_fdb_del = vxlan_fdb_delete,
2279 .ndo_fdb_dump = vxlan_fdb_dump,
2282 /* Info for udev, that this is a virtual tunnel endpoint */
2283 static struct device_type vxlan_type = {
2287 /* Calls the ndo_add_vxlan_port of the caller in order to
2288 * supply the listening VXLAN udp ports. Callers are expected
2289 * to implement the ndo_add_vxlan_port.
2291 void vxlan_get_rx_port(struct net_device *dev)
2293 struct vxlan_sock *vs;
2294 struct net *net = dev_net(dev);
2295 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2296 sa_family_t sa_family;
2300 spin_lock(&vn->sock_lock);
2301 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2302 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2303 port = inet_sk(vs->sock->sk)->inet_sport;
2304 sa_family = vs->sock->sk->sk_family;
2305 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2309 spin_unlock(&vn->sock_lock);
2311 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2313 /* Initialize the device structure. */
2314 static void vxlan_setup(struct net_device *dev)
2316 struct vxlan_dev *vxlan = netdev_priv(dev);
2319 eth_hw_addr_random(dev);
2321 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2322 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2324 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2326 dev->netdev_ops = &vxlan_netdev_ops;
2327 dev->destructor = free_netdev;
2328 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2330 dev->tx_queue_len = 0;
2331 dev->features |= NETIF_F_LLTX;
2332 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2333 dev->features |= NETIF_F_RXCSUM;
2334 dev->features |= NETIF_F_GSO_SOFTWARE;
2336 dev->vlan_features = dev->features;
2337 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2338 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2339 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2340 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2341 netif_keep_dst(dev);
2342 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2344 INIT_LIST_HEAD(&vxlan->next);
2345 spin_lock_init(&vxlan->hash_lock);
2346 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2347 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2348 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2350 init_timer_deferrable(&vxlan->age_timer);
2351 vxlan->age_timer.function = vxlan_cleanup;
2352 vxlan->age_timer.data = (unsigned long) vxlan;
2354 vxlan->dst_port = htons(vxlan_port);
2358 for (h = 0; h < FDB_HASH_SIZE; ++h)
2359 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2362 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2363 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2364 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2365 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2366 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2367 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2368 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2369 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2370 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2371 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2372 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2373 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2374 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2375 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2376 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2377 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2378 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2379 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2380 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2381 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2382 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2383 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2384 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2387 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2389 if (tb[IFLA_ADDRESS]) {
2390 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2391 pr_debug("invalid link address (not ethernet)\n");
2395 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2396 pr_debug("invalid all zero ethernet address\n");
2397 return -EADDRNOTAVAIL;
2404 if (data[IFLA_VXLAN_ID]) {
2405 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2406 if (id >= VXLAN_VID_MASK)
2410 if (data[IFLA_VXLAN_PORT_RANGE]) {
2411 const struct ifla_vxlan_port_range *p
2412 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2414 if (ntohs(p->high) < ntohs(p->low)) {
2415 pr_debug("port range %u .. %u not valid\n",
2416 ntohs(p->low), ntohs(p->high));
2424 static void vxlan_get_drvinfo(struct net_device *netdev,
2425 struct ethtool_drvinfo *drvinfo)
2427 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2428 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2431 static const struct ethtool_ops vxlan_ethtool_ops = {
2432 .get_drvinfo = vxlan_get_drvinfo,
2433 .get_link = ethtool_op_get_link,
2436 static void vxlan_del_work(struct work_struct *work)
2438 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2439 udp_tunnel_sock_release(vs->sock);
2443 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2444 __be16 port, u32 flags)
2446 struct socket *sock;
2447 struct udp_port_cfg udp_conf;
2450 memset(&udp_conf, 0, sizeof(udp_conf));
2453 udp_conf.family = AF_INET6;
2454 udp_conf.use_udp6_tx_checksums =
2455 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2456 udp_conf.use_udp6_rx_checksums =
2457 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2459 udp_conf.family = AF_INET;
2460 udp_conf.local_ip.s_addr = INADDR_ANY;
2461 udp_conf.use_udp_checksums =
2462 !!(flags & VXLAN_F_UDP_CSUM);
2465 udp_conf.local_udp_port = port;
2467 /* Open UDP socket */
2468 err = udp_sock_create(net, &udp_conf, &sock);
2470 return ERR_PTR(err);
2475 /* Create new listen socket if needed */
2476 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2477 vxlan_rcv_t *rcv, void *data,
2480 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2481 struct vxlan_sock *vs;
2482 struct socket *sock;
2484 bool ipv6 = !!(flags & VXLAN_F_IPV6);
2485 struct udp_tunnel_sock_cfg tunnel_cfg;
2487 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2489 return ERR_PTR(-ENOMEM);
2491 for (h = 0; h < VNI_HASH_SIZE; ++h)
2492 INIT_HLIST_HEAD(&vs->vni_list[h]);
2494 INIT_WORK(&vs->del_work, vxlan_del_work);
2496 sock = vxlan_create_sock(net, ipv6, port, flags);
2499 return ERR_CAST(sock);
2503 atomic_set(&vs->refcnt, 1);
2508 /* Initialize the vxlan udp offloads structure */
2509 vs->udp_offloads.port = port;
2510 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2511 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2513 spin_lock(&vn->sock_lock);
2514 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2515 vxlan_notify_add_rx_port(vs);
2516 spin_unlock(&vn->sock_lock);
2518 /* Mark socket as an encapsulation socket. */
2519 tunnel_cfg.sk_user_data = vs;
2520 tunnel_cfg.encap_type = 1;
2521 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2522 tunnel_cfg.encap_destroy = NULL;
2524 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2529 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2530 vxlan_rcv_t *rcv, void *data,
2531 bool no_share, u32 flags)
2533 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2534 struct vxlan_sock *vs;
2535 bool ipv6 = flags & VXLAN_F_IPV6;
2537 vs = vxlan_socket_create(net, port, rcv, data, flags);
2541 if (no_share) /* Return error if sharing is not allowed. */
2544 spin_lock(&vn->sock_lock);
2545 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
2546 if (vs && ((vs->rcv != rcv) ||
2547 !atomic_add_unless(&vs->refcnt, 1, 0)))
2548 vs = ERR_PTR(-EBUSY);
2549 spin_unlock(&vn->sock_lock);
2552 vs = ERR_PTR(-EINVAL);
2556 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2558 /* Scheduled at device creation to bind to a socket */
2559 static void vxlan_sock_work(struct work_struct *work)
2561 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2562 struct net *net = vxlan->net;
2563 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2564 __be16 port = vxlan->dst_port;
2565 struct vxlan_sock *nvs;
2567 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
2568 spin_lock(&vn->sock_lock);
2570 vxlan_vs_add_dev(nvs, vxlan);
2571 spin_unlock(&vn->sock_lock);
2573 dev_put(vxlan->dev);
2576 static int vxlan_newlink(struct net *net, struct net_device *dev,
2577 struct nlattr *tb[], struct nlattr *data[])
2579 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2580 struct vxlan_dev *vxlan = netdev_priv(dev);
2581 struct vxlan_rdst *dst = &vxlan->default_dst;
2584 bool use_ipv6 = false;
2586 if (!data[IFLA_VXLAN_ID])
2589 vxlan->net = dev_net(dev);
2591 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2592 dst->remote_vni = vni;
2594 /* Unless IPv6 is explicitly requested, assume IPv4 */
2595 dst->remote_ip.sa.sa_family = AF_INET;
2596 if (data[IFLA_VXLAN_GROUP]) {
2597 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2598 } else if (data[IFLA_VXLAN_GROUP6]) {
2599 if (!IS_ENABLED(CONFIG_IPV6))
2600 return -EPFNOSUPPORT;
2602 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2603 sizeof(struct in6_addr));
2604 dst->remote_ip.sa.sa_family = AF_INET6;
2608 if (data[IFLA_VXLAN_LOCAL]) {
2609 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2610 vxlan->saddr.sa.sa_family = AF_INET;
2611 } else if (data[IFLA_VXLAN_LOCAL6]) {
2612 if (!IS_ENABLED(CONFIG_IPV6))
2613 return -EPFNOSUPPORT;
2615 /* TODO: respect scope id */
2616 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2617 sizeof(struct in6_addr));
2618 vxlan->saddr.sa.sa_family = AF_INET6;
2622 if (data[IFLA_VXLAN_LINK] &&
2623 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2624 struct net_device *lowerdev
2625 = __dev_get_by_index(net, dst->remote_ifindex);
2628 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2632 #if IS_ENABLED(CONFIG_IPV6)
2634 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2635 if (idev && idev->cnf.disable_ipv6) {
2636 pr_info("IPv6 is disabled via sysctl\n");
2639 vxlan->flags |= VXLAN_F_IPV6;
2644 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2646 dev->needed_headroom = lowerdev->hard_header_len +
2647 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2648 } else if (use_ipv6)
2649 vxlan->flags |= VXLAN_F_IPV6;
2651 if (data[IFLA_VXLAN_TOS])
2652 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2654 if (data[IFLA_VXLAN_TTL])
2655 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2657 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2658 vxlan->flags |= VXLAN_F_LEARN;
2660 if (data[IFLA_VXLAN_AGEING])
2661 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2663 vxlan->age_interval = FDB_AGE_DEFAULT;
2665 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2666 vxlan->flags |= VXLAN_F_PROXY;
2668 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2669 vxlan->flags |= VXLAN_F_RSC;
2671 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2672 vxlan->flags |= VXLAN_F_L2MISS;
2674 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2675 vxlan->flags |= VXLAN_F_L3MISS;
2677 if (data[IFLA_VXLAN_LIMIT])
2678 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2680 if (data[IFLA_VXLAN_PORT_RANGE]) {
2681 const struct ifla_vxlan_port_range *p
2682 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2683 vxlan->port_min = ntohs(p->low);
2684 vxlan->port_max = ntohs(p->high);
2687 if (data[IFLA_VXLAN_PORT])
2688 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2690 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2691 vxlan->flags |= VXLAN_F_UDP_CSUM;
2693 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2694 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2695 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2697 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2698 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2699 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2701 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2702 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2703 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2705 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2706 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2707 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2709 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2711 pr_info("duplicate VNI %u\n", vni);
2715 dev->ethtool_ops = &vxlan_ethtool_ops;
2717 /* create an fdb entry for a valid default destination */
2718 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2719 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2720 &vxlan->default_dst.remote_ip,
2721 NUD_REACHABLE|NUD_PERMANENT,
2722 NLM_F_EXCL|NLM_F_CREATE,
2724 vxlan->default_dst.remote_vni,
2725 vxlan->default_dst.remote_ifindex,
2731 err = register_netdevice(dev);
2733 vxlan_fdb_delete_default(vxlan);
2737 list_add(&vxlan->next, &vn->vxlan_list);
2742 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2744 struct vxlan_dev *vxlan = netdev_priv(dev);
2745 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2747 spin_lock(&vn->sock_lock);
2748 if (!hlist_unhashed(&vxlan->hlist))
2749 hlist_del_rcu(&vxlan->hlist);
2750 spin_unlock(&vn->sock_lock);
2752 list_del(&vxlan->next);
2753 unregister_netdevice_queue(dev, head);
2756 static size_t vxlan_get_size(const struct net_device *dev)
2759 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2760 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2761 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2762 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2763 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2764 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2765 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2766 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2767 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2768 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2769 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2770 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2771 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2772 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2773 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2774 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2775 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2776 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2777 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2778 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2782 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2784 const struct vxlan_dev *vxlan = netdev_priv(dev);
2785 const struct vxlan_rdst *dst = &vxlan->default_dst;
2786 struct ifla_vxlan_port_range ports = {
2787 .low = htons(vxlan->port_min),
2788 .high = htons(vxlan->port_max),
2791 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2792 goto nla_put_failure;
2794 if (!vxlan_addr_any(&dst->remote_ip)) {
2795 if (dst->remote_ip.sa.sa_family == AF_INET) {
2796 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2797 dst->remote_ip.sin.sin_addr.s_addr))
2798 goto nla_put_failure;
2799 #if IS_ENABLED(CONFIG_IPV6)
2801 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2802 &dst->remote_ip.sin6.sin6_addr))
2803 goto nla_put_failure;
2808 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2809 goto nla_put_failure;
2811 if (!vxlan_addr_any(&vxlan->saddr)) {
2812 if (vxlan->saddr.sa.sa_family == AF_INET) {
2813 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2814 vxlan->saddr.sin.sin_addr.s_addr))
2815 goto nla_put_failure;
2816 #if IS_ENABLED(CONFIG_IPV6)
2818 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2819 &vxlan->saddr.sin6.sin6_addr))
2820 goto nla_put_failure;
2825 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2826 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2827 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2828 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2829 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2830 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2831 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2832 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2833 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2834 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2835 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2836 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2837 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2838 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2839 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2840 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2841 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2842 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2843 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2844 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2845 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2846 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2847 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2848 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2849 goto nla_put_failure;
2851 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2852 goto nla_put_failure;
2860 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2862 .maxtype = IFLA_VXLAN_MAX,
2863 .policy = vxlan_policy,
2864 .priv_size = sizeof(struct vxlan_dev),
2865 .setup = vxlan_setup,
2866 .validate = vxlan_validate,
2867 .newlink = vxlan_newlink,
2868 .dellink = vxlan_dellink,
2869 .get_size = vxlan_get_size,
2870 .fill_info = vxlan_fill_info,
2873 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2874 struct net_device *dev)
2876 struct vxlan_dev *vxlan, *next;
2877 LIST_HEAD(list_kill);
2879 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2880 struct vxlan_rdst *dst = &vxlan->default_dst;
2882 /* In case we created vxlan device with carrier
2883 * and we loose the carrier due to module unload
2884 * we also need to remove vxlan device. In other
2885 * cases, it's not necessary and remote_ifindex
2886 * is 0 here, so no matches.
2888 if (dst->remote_ifindex == dev->ifindex)
2889 vxlan_dellink(vxlan->dev, &list_kill);
2892 unregister_netdevice_many(&list_kill);
2895 static int vxlan_lowerdev_event(struct notifier_block *unused,
2896 unsigned long event, void *ptr)
2898 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2899 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2901 if (event == NETDEV_UNREGISTER)
2902 vxlan_handle_lowerdev_unregister(vn, dev);
2907 static struct notifier_block vxlan_notifier_block __read_mostly = {
2908 .notifier_call = vxlan_lowerdev_event,
2911 static __net_init int vxlan_init_net(struct net *net)
2913 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2916 INIT_LIST_HEAD(&vn->vxlan_list);
2917 spin_lock_init(&vn->sock_lock);
2919 for (h = 0; h < PORT_HASH_SIZE; ++h)
2920 INIT_HLIST_HEAD(&vn->sock_list[h]);
2925 static void __net_exit vxlan_exit_net(struct net *net)
2927 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2928 struct vxlan_dev *vxlan, *next;
2929 struct net_device *dev, *aux;
2933 for_each_netdev_safe(net, dev, aux)
2934 if (dev->rtnl_link_ops == &vxlan_link_ops)
2935 unregister_netdevice_queue(dev, &list);
2937 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2938 /* If vxlan->dev is in the same netns, it has already been added
2939 * to the list by the previous loop.
2941 if (!net_eq(dev_net(vxlan->dev), net))
2942 unregister_netdevice_queue(dev, &list);
2945 unregister_netdevice_many(&list);
2949 static struct pernet_operations vxlan_net_ops = {
2950 .init = vxlan_init_net,
2951 .exit = vxlan_exit_net,
2952 .id = &vxlan_net_id,
2953 .size = sizeof(struct vxlan_net),
2956 static int __init vxlan_init_module(void)
2960 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2964 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2966 rc = register_pernet_subsys(&vxlan_net_ops);
2970 rc = register_netdevice_notifier(&vxlan_notifier_block);
2974 rc = rtnl_link_register(&vxlan_link_ops);
2980 unregister_netdevice_notifier(&vxlan_notifier_block);
2982 unregister_pernet_subsys(&vxlan_net_ops);
2984 destroy_workqueue(vxlan_wq);
2987 late_initcall(vxlan_init_module);
2989 static void __exit vxlan_cleanup_module(void)
2991 rtnl_link_unregister(&vxlan_link_ops);
2992 unregister_netdevice_notifier(&vxlan_notifier_block);
2993 destroy_workqueue(vxlan_wq);
2994 unregister_pernet_subsys(&vxlan_net_ops);
2995 /* rcu_barrier() is called by netns */
2997 module_exit(vxlan_cleanup_module);
2999 MODULE_LICENSE("GPL");
3000 MODULE_VERSION(VXLAN_VERSION);
3001 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3002 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3003 MODULE_ALIAS_RTNL_LINK("vxlan");