Linux 3.12.39
[linux/fpc-iii.git] / drivers / net / vxlan.c
blobc8e333306c4c789a6f32e33f6dd6c36271b2e36b
1 /*
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.
9 */
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>
21 #include <linux/in.h>
22 #include <linux/ip.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>
30 #include <net/arp.h>
31 #include <net/ndisc.h>
32 #include <net/ip.h>
33 #include <net/ip_tunnels.h>
34 #include <net/icmp.h>
35 #include <net/udp.h>
36 #include <net/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
43 #if IS_ENABLED(CONFIG_IPV6)
44 #include <net/ipv6.h>
45 #include <net/addrconf.h>
46 #include <net/ip6_tunnel.h>
47 #include <net/ip6_checksum.h>
48 #endif
50 #define VXLAN_VERSION "0.1"
52 #define PORT_HASH_BITS 8
53 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
54 #define VNI_HASH_BITS 10
55 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56 #define FDB_HASH_BITS 8
57 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58 #define FDB_AGE_DEFAULT 300 /* 5 min */
59 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
61 #define VXLAN_N_VID (1u << 24)
62 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
63 /* IP header + UDP + VXLAN + Ethernet header */
64 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
65 /* IPv6 header + UDP + VXLAN + Ethernet header */
66 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
67 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
69 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
71 /* VXLAN protocol header */
72 struct vxlanhdr {
73 __be32 vx_flags;
74 __be32 vx_vni;
77 /* UDP port for VXLAN traffic.
78 * The IANA assigned port is 4789, but the Linux default is 8472
79 * for compatibility with early adopters.
81 static unsigned short vxlan_port __read_mostly = 8472;
82 module_param_named(udp_port, vxlan_port, ushort, 0444);
83 MODULE_PARM_DESC(udp_port, "Destination UDP port");
85 static bool log_ecn_error = true;
86 module_param(log_ecn_error, bool, 0644);
87 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
89 static int vxlan_net_id;
91 static const u8 all_zeros_mac[ETH_ALEN];
93 /* per-network namespace private data for this module */
94 struct vxlan_net {
95 struct list_head vxlan_list;
96 struct hlist_head sock_list[PORT_HASH_SIZE];
97 spinlock_t sock_lock;
100 union vxlan_addr {
101 struct sockaddr_in sin;
102 struct sockaddr_in6 sin6;
103 struct sockaddr sa;
106 struct vxlan_rdst {
107 union vxlan_addr remote_ip;
108 __be16 remote_port;
109 u32 remote_vni;
110 u32 remote_ifindex;
111 struct list_head list;
112 struct rcu_head rcu;
115 /* Forwarding table entry */
116 struct vxlan_fdb {
117 struct hlist_node hlist; /* linked list of entries */
118 struct rcu_head rcu;
119 unsigned long updated; /* jiffies */
120 unsigned long used;
121 struct list_head remotes;
122 u16 state; /* see ndm_state */
123 u8 flags; /* see ndm_flags */
124 u8 eth_addr[ETH_ALEN];
127 /* Pseudo network device */
128 struct vxlan_dev {
129 struct hlist_node hlist; /* vni hash table */
130 struct list_head next; /* vxlan's per namespace list */
131 struct vxlan_sock *vn_sock; /* listening socket */
132 struct net_device *dev;
133 struct vxlan_rdst default_dst; /* default destination */
134 union vxlan_addr saddr; /* source address */
135 __be16 dst_port;
136 __u16 port_min; /* source port range */
137 __u16 port_max;
138 __u8 tos; /* TOS override */
139 __u8 ttl;
140 u32 flags; /* VXLAN_F_* below */
142 struct work_struct sock_work;
143 struct work_struct igmp_join;
144 struct work_struct igmp_leave;
146 unsigned long age_interval;
147 struct timer_list age_timer;
148 spinlock_t hash_lock;
149 unsigned int addrcnt;
150 unsigned int addrmax;
152 struct hlist_head fdb_head[FDB_HASH_SIZE];
155 #define VXLAN_F_LEARN 0x01
156 #define VXLAN_F_PROXY 0x02
157 #define VXLAN_F_RSC 0x04
158 #define VXLAN_F_L2MISS 0x08
159 #define VXLAN_F_L3MISS 0x10
160 #define VXLAN_F_IPV6 0x20 /* internal flag */
162 /* salt for hash table */
163 static u32 vxlan_salt __read_mostly;
164 static struct workqueue_struct *vxlan_wq;
166 static void vxlan_sock_work(struct work_struct *work);
168 #if IS_ENABLED(CONFIG_IPV6)
169 static inline
170 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
172 if (a->sa.sa_family != b->sa.sa_family)
173 return false;
174 if (a->sa.sa_family == AF_INET6)
175 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
176 else
177 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
180 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
182 if (ipa->sa.sa_family == AF_INET6)
183 return ipv6_addr_any(&ipa->sin6.sin6_addr);
184 else
185 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
188 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
190 if (ipa->sa.sa_family == AF_INET6)
191 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
192 else
193 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
196 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
198 if (nla_len(nla) >= sizeof(struct in6_addr)) {
199 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
200 ip->sa.sa_family = AF_INET6;
201 return 0;
202 } else if (nla_len(nla) >= sizeof(__be32)) {
203 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
204 ip->sa.sa_family = AF_INET;
205 return 0;
206 } else {
207 return -EAFNOSUPPORT;
211 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
212 const union vxlan_addr *ip)
214 if (ip->sa.sa_family == AF_INET6)
215 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
216 else
217 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
220 #else /* !CONFIG_IPV6 */
222 static inline
223 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
225 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
228 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
230 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
233 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
235 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
238 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
240 if (nla_len(nla) >= sizeof(struct in6_addr)) {
241 return -EAFNOSUPPORT;
242 } else if (nla_len(nla) >= sizeof(__be32)) {
243 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
244 ip->sa.sa_family = AF_INET;
245 return 0;
246 } else {
247 return -EAFNOSUPPORT;
251 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
252 const union vxlan_addr *ip)
254 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
256 #endif
258 /* Virtual Network hash table head */
259 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
261 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
264 /* Socket hash table head */
265 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
267 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
269 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
272 /* First remote destination for a forwarding entry.
273 * Guaranteed to be non-NULL because remotes are never deleted.
275 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
277 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
280 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
282 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
285 /* Find VXLAN socket based on network namespace, address family and UDP port */
286 static struct vxlan_sock *vxlan_find_sock(struct net *net,
287 sa_family_t family, __be16 port)
289 struct vxlan_sock *vs;
291 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
292 if (inet_sk(vs->sock->sk)->inet_sport == port &&
293 inet_sk(vs->sock->sk)->sk.sk_family == family)
294 return vs;
296 return NULL;
299 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
301 struct vxlan_dev *vxlan;
303 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
304 if (vxlan->default_dst.remote_vni == id)
305 return vxlan;
308 return NULL;
311 /* Look up VNI in a per net namespace table */
312 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
313 sa_family_t family, __be16 port)
315 struct vxlan_sock *vs;
317 vs = vxlan_find_sock(net, family, port);
318 if (!vs)
319 return NULL;
321 return vxlan_vs_find_vni(vs, id);
324 /* Fill in neighbour message in skbuff. */
325 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
326 const struct vxlan_fdb *fdb,
327 u32 portid, u32 seq, int type, unsigned int flags,
328 const struct vxlan_rdst *rdst)
330 unsigned long now = jiffies;
331 struct nda_cacheinfo ci;
332 struct nlmsghdr *nlh;
333 struct ndmsg *ndm;
334 bool send_ip, send_eth;
336 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
337 if (nlh == NULL)
338 return -EMSGSIZE;
340 ndm = nlmsg_data(nlh);
341 memset(ndm, 0, sizeof(*ndm));
343 send_eth = send_ip = true;
345 if (type == RTM_GETNEIGH) {
346 ndm->ndm_family = AF_INET;
347 send_ip = !vxlan_addr_any(&rdst->remote_ip);
348 send_eth = !is_zero_ether_addr(fdb->eth_addr);
349 } else
350 ndm->ndm_family = AF_BRIDGE;
351 ndm->ndm_state = fdb->state;
352 ndm->ndm_ifindex = vxlan->dev->ifindex;
353 ndm->ndm_flags = fdb->flags;
354 ndm->ndm_type = NDA_DST;
356 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
357 goto nla_put_failure;
359 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
360 goto nla_put_failure;
362 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
363 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
364 goto nla_put_failure;
365 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
366 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
367 goto nla_put_failure;
368 if (rdst->remote_ifindex &&
369 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
370 goto nla_put_failure;
372 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
373 ci.ndm_confirmed = 0;
374 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
375 ci.ndm_refcnt = 0;
377 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
378 goto nla_put_failure;
380 return nlmsg_end(skb, nlh);
382 nla_put_failure:
383 nlmsg_cancel(skb, nlh);
384 return -EMSGSIZE;
387 static inline size_t vxlan_nlmsg_size(void)
389 return NLMSG_ALIGN(sizeof(struct ndmsg))
390 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
391 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
392 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
393 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
394 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
395 + nla_total_size(sizeof(struct nda_cacheinfo));
398 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
399 struct vxlan_fdb *fdb, int type)
401 struct net *net = dev_net(vxlan->dev);
402 struct sk_buff *skb;
403 int err = -ENOBUFS;
405 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
406 if (skb == NULL)
407 goto errout;
409 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
410 first_remote_rtnl(fdb));
411 if (err < 0) {
412 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
413 WARN_ON(err == -EMSGSIZE);
414 kfree_skb(skb);
415 goto errout;
418 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
419 return;
420 errout:
421 if (err < 0)
422 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
425 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
427 struct vxlan_dev *vxlan = netdev_priv(dev);
428 struct vxlan_fdb f = {
429 .state = NUD_STALE,
431 struct vxlan_rdst remote = {
432 .remote_ip = *ipa, /* goes to NDA_DST */
433 .remote_vni = VXLAN_N_VID,
436 INIT_LIST_HEAD(&f.remotes);
437 list_add_rcu(&remote.list, &f.remotes);
439 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
442 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
444 struct vxlan_fdb f = {
445 .state = NUD_STALE,
448 INIT_LIST_HEAD(&f.remotes);
449 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
451 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
454 /* Hash Ethernet address */
455 static u32 eth_hash(const unsigned char *addr)
457 u64 value = get_unaligned((u64 *)addr);
459 /* only want 6 bytes */
460 #ifdef __BIG_ENDIAN
461 value >>= 16;
462 #else
463 value <<= 16;
464 #endif
465 return hash_64(value, FDB_HASH_BITS);
468 /* Hash chain to use given mac address */
469 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
470 const u8 *mac)
472 return &vxlan->fdb_head[eth_hash(mac)];
475 /* Look up Ethernet address in forwarding table */
476 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
477 const u8 *mac)
480 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
481 struct vxlan_fdb *f;
483 hlist_for_each_entry_rcu(f, head, hlist) {
484 if (ether_addr_equal(mac, f->eth_addr))
485 return f;
488 return NULL;
491 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
492 const u8 *mac)
494 struct vxlan_fdb *f;
496 f = __vxlan_find_mac(vxlan, mac);
497 if (f)
498 f->used = jiffies;
500 return f;
503 /* caller should hold vxlan->hash_lock */
504 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
505 union vxlan_addr *ip, __be16 port,
506 __u32 vni, __u32 ifindex)
508 struct vxlan_rdst *rd;
510 list_for_each_entry(rd, &f->remotes, list) {
511 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
512 rd->remote_port == port &&
513 rd->remote_vni == vni &&
514 rd->remote_ifindex == ifindex)
515 return rd;
518 return NULL;
521 /* Replace destination of unicast mac */
522 static int vxlan_fdb_replace(struct vxlan_fdb *f,
523 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
525 struct vxlan_rdst *rd;
527 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
528 if (rd)
529 return 0;
531 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
532 if (!rd)
533 return 0;
534 rd->remote_ip = *ip;
535 rd->remote_port = port;
536 rd->remote_vni = vni;
537 rd->remote_ifindex = ifindex;
538 return 1;
541 /* Add/update destinations for multicast */
542 static int vxlan_fdb_append(struct vxlan_fdb *f,
543 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
545 struct vxlan_rdst *rd;
547 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
548 if (rd)
549 return 0;
551 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
552 if (rd == NULL)
553 return -ENOBUFS;
554 rd->remote_ip = *ip;
555 rd->remote_port = port;
556 rd->remote_vni = vni;
557 rd->remote_ifindex = ifindex;
559 list_add_tail_rcu(&rd->list, &f->remotes);
561 return 1;
564 /* Notify netdevs that UDP port started listening */
565 static void vxlan_notify_add_rx_port(struct sock *sk)
567 struct net_device *dev;
568 struct net *net = sock_net(sk);
569 sa_family_t sa_family = sk->sk_family;
570 __be16 port = inet_sk(sk)->inet_sport;
572 rcu_read_lock();
573 for_each_netdev_rcu(net, dev) {
574 if (dev->netdev_ops->ndo_add_vxlan_port)
575 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
576 port);
578 rcu_read_unlock();
581 /* Notify netdevs that UDP port is no more listening */
582 static void vxlan_notify_del_rx_port(struct sock *sk)
584 struct net_device *dev;
585 struct net *net = sock_net(sk);
586 sa_family_t sa_family = sk->sk_family;
587 __be16 port = inet_sk(sk)->inet_sport;
589 rcu_read_lock();
590 for_each_netdev_rcu(net, dev) {
591 if (dev->netdev_ops->ndo_del_vxlan_port)
592 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
593 port);
595 rcu_read_unlock();
598 /* Add new entry to forwarding table -- assumes lock held */
599 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
600 const u8 *mac, union vxlan_addr *ip,
601 __u16 state, __u16 flags,
602 __be16 port, __u32 vni, __u32 ifindex,
603 __u8 ndm_flags)
605 struct vxlan_fdb *f;
606 int notify = 0;
608 f = __vxlan_find_mac(vxlan, mac);
609 if (f) {
610 if (flags & NLM_F_EXCL) {
611 netdev_dbg(vxlan->dev,
612 "lost race to create %pM\n", mac);
613 return -EEXIST;
615 if (f->state != state) {
616 f->state = state;
617 f->updated = jiffies;
618 notify = 1;
620 if (f->flags != ndm_flags) {
621 f->flags = ndm_flags;
622 f->updated = jiffies;
623 notify = 1;
625 if ((flags & NLM_F_REPLACE)) {
626 /* Only change unicasts */
627 if (!(is_multicast_ether_addr(f->eth_addr) ||
628 is_zero_ether_addr(f->eth_addr))) {
629 int rc = vxlan_fdb_replace(f, ip, port, vni,
630 ifindex);
632 if (rc < 0)
633 return rc;
634 notify |= rc;
635 } else
636 return -EOPNOTSUPP;
638 if ((flags & NLM_F_APPEND) &&
639 (is_multicast_ether_addr(f->eth_addr) ||
640 is_zero_ether_addr(f->eth_addr))) {
641 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
643 if (rc < 0)
644 return rc;
645 notify |= rc;
647 } else {
648 if (!(flags & NLM_F_CREATE))
649 return -ENOENT;
651 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
652 return -ENOSPC;
654 /* Disallow replace to add a multicast entry */
655 if ((flags & NLM_F_REPLACE) &&
656 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
657 return -EOPNOTSUPP;
659 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
660 f = kmalloc(sizeof(*f), GFP_ATOMIC);
661 if (!f)
662 return -ENOMEM;
664 notify = 1;
665 f->state = state;
666 f->flags = ndm_flags;
667 f->updated = f->used = jiffies;
668 INIT_LIST_HEAD(&f->remotes);
669 memcpy(f->eth_addr, mac, ETH_ALEN);
671 vxlan_fdb_append(f, ip, port, vni, ifindex);
673 ++vxlan->addrcnt;
674 hlist_add_head_rcu(&f->hlist,
675 vxlan_fdb_head(vxlan, mac));
678 if (notify)
679 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
681 return 0;
684 static void vxlan_fdb_free(struct rcu_head *head)
686 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
687 struct vxlan_rdst *rd, *nd;
689 list_for_each_entry_safe(rd, nd, &f->remotes, list)
690 kfree(rd);
691 kfree(f);
694 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
696 netdev_dbg(vxlan->dev,
697 "delete %pM\n", f->eth_addr);
699 --vxlan->addrcnt;
700 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
702 hlist_del_rcu(&f->hlist);
703 call_rcu(&f->rcu, vxlan_fdb_free);
706 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
707 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
709 struct net *net = dev_net(vxlan->dev);
710 int err;
712 if (tb[NDA_DST]) {
713 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
714 if (err)
715 return err;
716 } else {
717 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
718 if (remote->sa.sa_family == AF_INET) {
719 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
720 ip->sa.sa_family = AF_INET;
721 #if IS_ENABLED(CONFIG_IPV6)
722 } else {
723 ip->sin6.sin6_addr = in6addr_any;
724 ip->sa.sa_family = AF_INET6;
725 #endif
729 if (tb[NDA_PORT]) {
730 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
731 return -EINVAL;
732 *port = nla_get_be16(tb[NDA_PORT]);
733 } else {
734 *port = vxlan->dst_port;
737 if (tb[NDA_VNI]) {
738 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
739 return -EINVAL;
740 *vni = nla_get_u32(tb[NDA_VNI]);
741 } else {
742 *vni = vxlan->default_dst.remote_vni;
745 if (tb[NDA_IFINDEX]) {
746 struct net_device *tdev;
748 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
749 return -EINVAL;
750 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
751 tdev = dev_get_by_index(net, *ifindex);
752 if (!tdev)
753 return -EADDRNOTAVAIL;
754 dev_put(tdev);
755 } else {
756 *ifindex = 0;
759 return 0;
762 /* Add static entry (via netlink) */
763 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
764 struct net_device *dev,
765 const unsigned char *addr, u16 flags)
767 struct vxlan_dev *vxlan = netdev_priv(dev);
768 /* struct net *net = dev_net(vxlan->dev); */
769 union vxlan_addr ip;
770 __be16 port;
771 u32 vni, ifindex;
772 int err;
774 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
775 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
776 ndm->ndm_state);
777 return -EINVAL;
780 if (tb[NDA_DST] == NULL)
781 return -EINVAL;
783 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
784 if (err)
785 return err;
787 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
788 return -EAFNOSUPPORT;
790 spin_lock_bh(&vxlan->hash_lock);
791 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
792 port, vni, ifindex, ndm->ndm_flags);
793 spin_unlock_bh(&vxlan->hash_lock);
795 return err;
798 /* Delete entry (via netlink) */
799 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
800 struct net_device *dev,
801 const unsigned char *addr)
803 struct vxlan_dev *vxlan = netdev_priv(dev);
804 struct vxlan_fdb *f;
805 struct vxlan_rdst *rd = NULL;
806 union vxlan_addr ip;
807 __be16 port;
808 u32 vni, ifindex;
809 int err;
811 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
812 if (err)
813 return err;
815 err = -ENOENT;
817 spin_lock_bh(&vxlan->hash_lock);
818 f = vxlan_find_mac(vxlan, addr);
819 if (!f)
820 goto out;
822 if (!vxlan_addr_any(&ip)) {
823 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
824 if (!rd)
825 goto out;
828 err = 0;
830 /* remove a destination if it's not the only one on the list,
831 * otherwise destroy the fdb entry
833 if (rd && !list_is_singular(&f->remotes)) {
834 list_del_rcu(&rd->list);
835 kfree_rcu(rd, rcu);
836 goto out;
839 vxlan_fdb_destroy(vxlan, f);
841 out:
842 spin_unlock_bh(&vxlan->hash_lock);
844 return err;
847 /* Dump forwarding table */
848 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
849 struct net_device *dev, int idx)
851 struct vxlan_dev *vxlan = netdev_priv(dev);
852 unsigned int h;
854 for (h = 0; h < FDB_HASH_SIZE; ++h) {
855 struct vxlan_fdb *f;
856 int err;
858 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
859 struct vxlan_rdst *rd;
861 if (idx < cb->args[0])
862 goto skip;
864 list_for_each_entry_rcu(rd, &f->remotes, list) {
865 err = vxlan_fdb_info(skb, vxlan, f,
866 NETLINK_CB(cb->skb).portid,
867 cb->nlh->nlmsg_seq,
868 RTM_NEWNEIGH,
869 NLM_F_MULTI, rd);
870 if (err < 0)
871 goto out;
873 skip:
874 ++idx;
877 out:
878 return idx;
881 /* Watch incoming packets to learn mapping between Ethernet address
882 * and Tunnel endpoint.
883 * Return true if packet is bogus and should be droppped.
885 static bool vxlan_snoop(struct net_device *dev,
886 union vxlan_addr *src_ip, const u8 *src_mac)
888 struct vxlan_dev *vxlan = netdev_priv(dev);
889 struct vxlan_fdb *f;
891 f = vxlan_find_mac(vxlan, src_mac);
892 if (likely(f)) {
893 struct vxlan_rdst *rdst = first_remote_rcu(f);
895 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
896 return false;
898 /* Don't migrate static entries, drop packets */
899 if (f->state & NUD_NOARP)
900 return true;
902 if (net_ratelimit())
903 netdev_info(dev,
904 "%pM migrated from %pIS to %pIS\n",
905 src_mac, &rdst->remote_ip, &src_ip);
907 rdst->remote_ip = *src_ip;
908 f->updated = jiffies;
909 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
910 } else {
911 /* learned new entry */
912 spin_lock(&vxlan->hash_lock);
914 /* close off race between vxlan_flush and incoming packets */
915 if (netif_running(dev))
916 vxlan_fdb_create(vxlan, src_mac, src_ip,
917 NUD_REACHABLE,
918 NLM_F_EXCL|NLM_F_CREATE,
919 vxlan->dst_port,
920 vxlan->default_dst.remote_vni,
921 0, NTF_SELF);
922 spin_unlock(&vxlan->hash_lock);
925 return false;
928 /* See if multicast group is already in use by other ID */
929 static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
931 struct vxlan_dev *vxlan;
933 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
934 if (!netif_running(vxlan->dev))
935 continue;
937 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
938 remote_ip))
939 return true;
942 return false;
945 static void vxlan_sock_hold(struct vxlan_sock *vs)
947 atomic_inc(&vs->refcnt);
950 void vxlan_sock_release(struct vxlan_sock *vs)
952 struct sock *sk = vs->sock->sk;
953 struct net *net = sock_net(sk);
954 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
956 if (!atomic_dec_and_test(&vs->refcnt))
957 return;
959 spin_lock(&vn->sock_lock);
960 hlist_del_rcu(&vs->hlist);
961 rcu_assign_sk_user_data(vs->sock->sk, NULL);
962 vxlan_notify_del_rx_port(sk);
963 spin_unlock(&vn->sock_lock);
965 queue_work(vxlan_wq, &vs->del_work);
967 EXPORT_SYMBOL_GPL(vxlan_sock_release);
969 /* Callback to update multicast group membership when first VNI on
970 * multicast asddress is brought up
971 * Done as workqueue because ip_mc_join_group acquires RTNL.
973 static void vxlan_igmp_join(struct work_struct *work)
975 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
976 struct vxlan_sock *vs = vxlan->vn_sock;
977 struct sock *sk = vs->sock->sk;
978 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
979 int ifindex = vxlan->default_dst.remote_ifindex;
981 lock_sock(sk);
982 if (ip->sa.sa_family == AF_INET) {
983 struct ip_mreqn mreq = {
984 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
985 .imr_ifindex = ifindex,
988 ip_mc_join_group(sk, &mreq);
989 #if IS_ENABLED(CONFIG_IPV6)
990 } else {
991 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
992 &ip->sin6.sin6_addr);
993 #endif
995 release_sock(sk);
997 vxlan_sock_release(vs);
998 dev_put(vxlan->dev);
1001 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1002 static void vxlan_igmp_leave(struct work_struct *work)
1004 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
1005 struct vxlan_sock *vs = vxlan->vn_sock;
1006 struct sock *sk = vs->sock->sk;
1007 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1008 int ifindex = vxlan->default_dst.remote_ifindex;
1010 lock_sock(sk);
1011 if (ip->sa.sa_family == AF_INET) {
1012 struct ip_mreqn mreq = {
1013 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1014 .imr_ifindex = ifindex,
1017 ip_mc_leave_group(sk, &mreq);
1018 #if IS_ENABLED(CONFIG_IPV6)
1019 } else {
1020 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1021 &ip->sin6.sin6_addr);
1022 #endif
1025 release_sock(sk);
1027 vxlan_sock_release(vs);
1028 dev_put(vxlan->dev);
1031 /* Callback from net/ipv4/udp.c to receive packets */
1032 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1034 struct vxlan_sock *vs;
1035 struct vxlanhdr *vxh;
1036 __be16 port;
1038 /* Need Vxlan and inner Ethernet header to be present */
1039 if (!pskb_may_pull(skb, VXLAN_HLEN))
1040 goto error;
1042 /* Return packets with reserved bits set */
1043 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1044 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1045 (vxh->vx_vni & htonl(0xff))) {
1046 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1047 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1048 goto error;
1051 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1052 goto drop;
1054 port = inet_sk(sk)->inet_sport;
1056 vs = rcu_dereference_sk_user_data(sk);
1057 if (!vs)
1058 goto drop;
1060 /* If the NIC driver gave us an encapsulated packet
1061 * with the encapsulation mark, the device checksummed it
1062 * for us. Otherwise force the upper layers to verify it.
1064 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation)
1065 skb->ip_summed = CHECKSUM_NONE;
1067 skb->encapsulation = 0;
1069 vs->rcv(vs, skb, vxh->vx_vni);
1070 return 0;
1072 drop:
1073 /* Consume bad packet */
1074 kfree_skb(skb);
1075 return 0;
1077 error:
1078 /* Return non vxlan pkt */
1079 return 1;
1082 static void vxlan_rcv(struct vxlan_sock *vs,
1083 struct sk_buff *skb, __be32 vx_vni)
1085 struct iphdr *oip = NULL;
1086 struct ipv6hdr *oip6 = NULL;
1087 struct vxlan_dev *vxlan;
1088 struct pcpu_tstats *stats;
1089 union vxlan_addr saddr;
1090 __u32 vni;
1091 int err = 0;
1092 union vxlan_addr *remote_ip;
1094 vni = ntohl(vx_vni) >> 8;
1095 /* Is this VNI defined? */
1096 vxlan = vxlan_vs_find_vni(vs, vni);
1097 if (!vxlan)
1098 goto drop;
1100 remote_ip = &vxlan->default_dst.remote_ip;
1101 skb_reset_mac_header(skb);
1102 skb->protocol = eth_type_trans(skb, vxlan->dev);
1104 /* Ignore packet loops (and multicast echo) */
1105 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1106 goto drop;
1108 /* Re-examine inner Ethernet packet */
1109 if (remote_ip->sa.sa_family == AF_INET) {
1110 oip = ip_hdr(skb);
1111 saddr.sin.sin_addr.s_addr = oip->saddr;
1112 saddr.sa.sa_family = AF_INET;
1113 #if IS_ENABLED(CONFIG_IPV6)
1114 } else {
1115 oip6 = ipv6_hdr(skb);
1116 saddr.sin6.sin6_addr = oip6->saddr;
1117 saddr.sa.sa_family = AF_INET6;
1118 #endif
1121 if ((vxlan->flags & VXLAN_F_LEARN) &&
1122 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1123 goto drop;
1125 skb_reset_network_header(skb);
1127 if (oip6)
1128 err = IP6_ECN_decapsulate(oip6, skb);
1129 if (oip)
1130 err = IP_ECN_decapsulate(oip, skb);
1132 if (unlikely(err)) {
1133 if (log_ecn_error) {
1134 if (oip6)
1135 net_info_ratelimited("non-ECT from %pI6\n",
1136 &oip6->saddr);
1137 if (oip)
1138 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1139 &oip->saddr, oip->tos);
1141 if (err > 1) {
1142 ++vxlan->dev->stats.rx_frame_errors;
1143 ++vxlan->dev->stats.rx_errors;
1144 goto drop;
1148 stats = this_cpu_ptr(vxlan->dev->tstats);
1149 u64_stats_update_begin(&stats->syncp);
1150 stats->rx_packets++;
1151 stats->rx_bytes += skb->len;
1152 u64_stats_update_end(&stats->syncp);
1154 netif_rx(skb);
1156 return;
1157 drop:
1158 /* Consume bad packet */
1159 kfree_skb(skb);
1162 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1164 struct vxlan_dev *vxlan = netdev_priv(dev);
1165 struct arphdr *parp;
1166 u8 *arpptr, *sha;
1167 __be32 sip, tip;
1168 struct neighbour *n;
1170 if (dev->flags & IFF_NOARP)
1171 goto out;
1173 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1174 dev->stats.tx_dropped++;
1175 goto out;
1177 parp = arp_hdr(skb);
1179 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1180 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1181 parp->ar_pro != htons(ETH_P_IP) ||
1182 parp->ar_op != htons(ARPOP_REQUEST) ||
1183 parp->ar_hln != dev->addr_len ||
1184 parp->ar_pln != 4)
1185 goto out;
1186 arpptr = (u8 *)parp + sizeof(struct arphdr);
1187 sha = arpptr;
1188 arpptr += dev->addr_len; /* sha */
1189 memcpy(&sip, arpptr, sizeof(sip));
1190 arpptr += sizeof(sip);
1191 arpptr += dev->addr_len; /* tha */
1192 memcpy(&tip, arpptr, sizeof(tip));
1194 if (ipv4_is_loopback(tip) ||
1195 ipv4_is_multicast(tip))
1196 goto out;
1198 n = neigh_lookup(&arp_tbl, &tip, dev);
1200 if (n) {
1201 struct vxlan_fdb *f;
1202 struct sk_buff *reply;
1204 if (!(n->nud_state & NUD_CONNECTED)) {
1205 neigh_release(n);
1206 goto out;
1209 f = vxlan_find_mac(vxlan, n->ha);
1210 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1211 /* bridge-local neighbor */
1212 neigh_release(n);
1213 goto out;
1216 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1217 n->ha, sha);
1219 neigh_release(n);
1221 if (reply == NULL)
1222 goto out;
1224 skb_reset_mac_header(reply);
1225 __skb_pull(reply, skb_network_offset(reply));
1226 reply->ip_summed = CHECKSUM_UNNECESSARY;
1227 reply->pkt_type = PACKET_HOST;
1229 if (netif_rx_ni(reply) == NET_RX_DROP)
1230 dev->stats.rx_dropped++;
1231 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1232 union vxlan_addr ipa = {
1233 .sin.sin_addr.s_addr = tip,
1234 .sin.sin_family = AF_INET,
1237 vxlan_ip_miss(dev, &ipa);
1239 out:
1240 consume_skb(skb);
1241 return NETDEV_TX_OK;
1244 #if IS_ENABLED(CONFIG_IPV6)
1246 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1247 struct neighbour *n, bool isrouter)
1249 struct net_device *dev = request->dev;
1250 struct sk_buff *reply;
1251 struct nd_msg *ns, *na;
1252 struct ipv6hdr *pip6;
1253 u8 *daddr;
1254 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1255 int ns_olen;
1256 int i, len;
1258 if (dev == NULL)
1259 return NULL;
1261 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1262 sizeof(*na) + na_olen + dev->needed_tailroom;
1263 reply = alloc_skb(len, GFP_ATOMIC);
1264 if (reply == NULL)
1265 return NULL;
1267 reply->protocol = htons(ETH_P_IPV6);
1268 reply->dev = dev;
1269 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1270 skb_push(reply, sizeof(struct ethhdr));
1271 skb_set_mac_header(reply, 0);
1273 ns = (struct nd_msg *)skb_transport_header(request);
1275 daddr = eth_hdr(request)->h_source;
1276 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1277 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1278 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1279 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1280 break;
1284 /* Ethernet header */
1285 memcpy(eth_hdr(reply)->h_dest, daddr, ETH_ALEN);
1286 memcpy(eth_hdr(reply)->h_source, n->ha, ETH_ALEN);
1287 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1288 reply->protocol = htons(ETH_P_IPV6);
1290 skb_pull(reply, sizeof(struct ethhdr));
1291 skb_set_network_header(reply, 0);
1292 skb_put(reply, sizeof(struct ipv6hdr));
1294 /* IPv6 header */
1296 pip6 = ipv6_hdr(reply);
1297 memset(pip6, 0, sizeof(struct ipv6hdr));
1298 pip6->version = 6;
1299 pip6->priority = ipv6_hdr(request)->priority;
1300 pip6->nexthdr = IPPROTO_ICMPV6;
1301 pip6->hop_limit = 255;
1302 pip6->daddr = ipv6_hdr(request)->saddr;
1303 pip6->saddr = *(struct in6_addr *)n->primary_key;
1305 skb_pull(reply, sizeof(struct ipv6hdr));
1306 skb_set_transport_header(reply, 0);
1308 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1310 /* Neighbor Advertisement */
1311 memset(na, 0, sizeof(*na)+na_olen);
1312 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1313 na->icmph.icmp6_router = isrouter;
1314 na->icmph.icmp6_override = 1;
1315 na->icmph.icmp6_solicited = 1;
1316 na->target = ns->target;
1317 memcpy(&na->opt[2], n->ha, ETH_ALEN);
1318 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1319 na->opt[1] = na_olen >> 3;
1321 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1322 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1323 csum_partial(na, sizeof(*na)+na_olen, 0));
1325 pip6->payload_len = htons(sizeof(*na)+na_olen);
1327 skb_push(reply, sizeof(struct ipv6hdr));
1329 reply->ip_summed = CHECKSUM_UNNECESSARY;
1331 return reply;
1334 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1336 struct vxlan_dev *vxlan = netdev_priv(dev);
1337 struct nd_msg *msg;
1338 const struct ipv6hdr *iphdr;
1339 const struct in6_addr *saddr, *daddr;
1340 struct neighbour *n;
1341 struct inet6_dev *in6_dev;
1343 in6_dev = __in6_dev_get(dev);
1344 if (!in6_dev)
1345 goto out;
1347 iphdr = ipv6_hdr(skb);
1348 saddr = &iphdr->saddr;
1349 daddr = &iphdr->daddr;
1351 msg = (struct nd_msg *)skb_transport_header(skb);
1352 if (msg->icmph.icmp6_code != 0 ||
1353 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1354 goto out;
1356 if (ipv6_addr_loopback(daddr) ||
1357 ipv6_addr_is_multicast(&msg->target))
1358 goto out;
1360 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1362 if (n) {
1363 struct vxlan_fdb *f;
1364 struct sk_buff *reply;
1366 if (!(n->nud_state & NUD_CONNECTED)) {
1367 neigh_release(n);
1368 goto out;
1371 f = vxlan_find_mac(vxlan, n->ha);
1372 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1373 /* bridge-local neighbor */
1374 neigh_release(n);
1375 goto out;
1378 reply = vxlan_na_create(skb, n,
1379 !!(f ? f->flags & NTF_ROUTER : 0));
1381 neigh_release(n);
1383 if (reply == NULL)
1384 goto out;
1386 if (netif_rx_ni(reply) == NET_RX_DROP)
1387 dev->stats.rx_dropped++;
1389 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1390 union vxlan_addr ipa = {
1391 .sin6.sin6_addr = msg->target,
1392 .sin6.sin6_family = AF_INET6,
1395 vxlan_ip_miss(dev, &ipa);
1398 out:
1399 consume_skb(skb);
1400 return NETDEV_TX_OK;
1402 #endif
1404 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1406 struct vxlan_dev *vxlan = netdev_priv(dev);
1407 struct neighbour *n;
1409 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1410 return false;
1412 n = NULL;
1413 switch (ntohs(eth_hdr(skb)->h_proto)) {
1414 case ETH_P_IP:
1416 struct iphdr *pip;
1418 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1419 return false;
1420 pip = ip_hdr(skb);
1421 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1422 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1423 union vxlan_addr ipa = {
1424 .sin.sin_addr.s_addr = pip->daddr,
1425 .sin.sin_family = AF_INET,
1428 vxlan_ip_miss(dev, &ipa);
1429 return false;
1432 break;
1434 #if IS_ENABLED(CONFIG_IPV6)
1435 case ETH_P_IPV6:
1437 struct ipv6hdr *pip6;
1439 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1440 return false;
1441 pip6 = ipv6_hdr(skb);
1442 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1443 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1444 union vxlan_addr ipa = {
1445 .sin6.sin6_addr = pip6->daddr,
1446 .sin6.sin6_family = AF_INET6,
1449 vxlan_ip_miss(dev, &ipa);
1450 return false;
1453 break;
1455 #endif
1456 default:
1457 return false;
1460 if (n) {
1461 bool diff;
1463 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1464 if (diff) {
1465 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1466 dev->addr_len);
1467 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1469 neigh_release(n);
1470 return diff;
1473 return false;
1476 static void vxlan_sock_put(struct sk_buff *skb)
1478 sock_put(skb->sk);
1481 /* On transmit, associate with the tunnel socket */
1482 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
1484 skb_orphan(skb);
1485 sock_hold(sk);
1486 skb->sk = sk;
1487 skb->destructor = vxlan_sock_put;
1490 /* Compute source port for outgoing packet
1491 * first choice to use L4 flow hash since it will spread
1492 * better and maybe available from hardware
1493 * secondary choice is to use jhash on the Ethernet header
1495 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
1497 unsigned int range = (port_max - port_min) + 1;
1498 u32 hash;
1500 hash = skb_get_rxhash(skb);
1501 if (!hash)
1502 hash = jhash(skb->data, 2 * ETH_ALEN,
1503 (__force u32) skb->protocol);
1505 return htons((((u64) hash * range) >> 32) + port_min);
1507 EXPORT_SYMBOL_GPL(vxlan_src_port);
1509 static int handle_offloads(struct sk_buff *skb)
1511 if (skb_is_gso(skb)) {
1512 int err = skb_unclone(skb, GFP_ATOMIC);
1513 if (unlikely(err))
1514 return err;
1516 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1517 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1518 skb->ip_summed = CHECKSUM_NONE;
1520 return 0;
1523 #if IS_ENABLED(CONFIG_IPV6)
1524 static int vxlan6_xmit_skb(struct vxlan_sock *vs,
1525 struct dst_entry *dst, struct sk_buff *skb,
1526 struct net_device *dev, struct in6_addr *saddr,
1527 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1528 __be16 src_port, __be16 dst_port, __be32 vni)
1530 struct ipv6hdr *ip6h;
1531 struct vxlanhdr *vxh;
1532 struct udphdr *uh;
1533 int min_headroom;
1534 int err;
1536 if (!skb->encapsulation) {
1537 skb_reset_inner_headers(skb);
1538 skb->encapsulation = 1;
1541 skb_scrub_packet(skb, false);
1543 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1544 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1545 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1547 /* Need space for new headers (invalidates iph ptr) */
1548 err = skb_cow_head(skb, min_headroom);
1549 if (unlikely(err))
1550 return err;
1552 if (vlan_tx_tag_present(skb)) {
1553 if (WARN_ON(!__vlan_put_tag(skb,
1554 skb->vlan_proto,
1555 vlan_tx_tag_get(skb))))
1556 return -ENOMEM;
1558 skb->vlan_tci = 0;
1561 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1562 vxh->vx_flags = htonl(VXLAN_FLAGS);
1563 vxh->vx_vni = vni;
1565 __skb_push(skb, sizeof(*uh));
1566 skb_reset_transport_header(skb);
1567 uh = udp_hdr(skb);
1569 uh->dest = dst_port;
1570 uh->source = src_port;
1572 uh->len = htons(skb->len);
1573 uh->check = 0;
1575 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1576 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1577 IPSKB_REROUTED);
1578 skb_dst_set(skb, dst);
1580 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1581 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1582 skb->ip_summed = CHECKSUM_UNNECESSARY;
1583 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1584 IPPROTO_UDP, csum);
1585 if (uh->check == 0)
1586 uh->check = CSUM_MANGLED_0;
1587 } else {
1588 skb->ip_summed = CHECKSUM_PARTIAL;
1589 skb->csum_start = skb_transport_header(skb) - skb->head;
1590 skb->csum_offset = offsetof(struct udphdr, check);
1591 uh->check = ~csum_ipv6_magic(saddr, daddr,
1592 skb->len, IPPROTO_UDP, 0);
1595 __skb_push(skb, sizeof(*ip6h));
1596 skb_reset_network_header(skb);
1597 ip6h = ipv6_hdr(skb);
1598 ip6h->version = 6;
1599 ip6h->priority = prio;
1600 ip6h->flow_lbl[0] = 0;
1601 ip6h->flow_lbl[1] = 0;
1602 ip6h->flow_lbl[2] = 0;
1603 ip6h->payload_len = htons(skb->len);
1604 ip6h->nexthdr = IPPROTO_UDP;
1605 ip6h->hop_limit = ttl;
1606 ip6h->daddr = *daddr;
1607 ip6h->saddr = *saddr;
1609 vxlan_set_owner(vs->sock->sk, skb);
1611 err = handle_offloads(skb);
1612 if (err)
1613 return err;
1615 ip6tunnel_xmit(skb, dev);
1616 return 0;
1618 #endif
1620 int vxlan_xmit_skb(struct vxlan_sock *vs,
1621 struct rtable *rt, struct sk_buff *skb,
1622 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1623 __be16 src_port, __be16 dst_port, __be32 vni)
1625 struct vxlanhdr *vxh;
1626 struct udphdr *uh;
1627 int min_headroom;
1628 int err;
1630 if (!skb->encapsulation) {
1631 skb_reset_inner_headers(skb);
1632 skb->encapsulation = 1;
1635 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1636 + VXLAN_HLEN + sizeof(struct iphdr)
1637 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1639 /* Need space for new headers (invalidates iph ptr) */
1640 err = skb_cow_head(skb, min_headroom);
1641 if (unlikely(err))
1642 return err;
1644 if (vlan_tx_tag_present(skb)) {
1645 if (WARN_ON(!__vlan_put_tag(skb,
1646 skb->vlan_proto,
1647 vlan_tx_tag_get(skb))))
1648 return -ENOMEM;
1650 skb->vlan_tci = 0;
1653 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1654 vxh->vx_flags = htonl(VXLAN_FLAGS);
1655 vxh->vx_vni = vni;
1657 __skb_push(skb, sizeof(*uh));
1658 skb_reset_transport_header(skb);
1659 uh = udp_hdr(skb);
1661 uh->dest = dst_port;
1662 uh->source = src_port;
1664 uh->len = htons(skb->len);
1665 uh->check = 0;
1667 vxlan_set_owner(vs->sock->sk, skb);
1669 err = handle_offloads(skb);
1670 if (err)
1671 return err;
1673 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
1674 false);
1676 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1678 /* Bypass encapsulation if the destination is local */
1679 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1680 struct vxlan_dev *dst_vxlan)
1682 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1683 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1684 union vxlan_addr loopback;
1685 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1686 struct net_device *dev = skb->dev;
1687 int len = skb->len;
1689 skb->pkt_type = PACKET_HOST;
1690 skb->encapsulation = 0;
1691 skb->dev = dst_vxlan->dev;
1692 __skb_pull(skb, skb_network_offset(skb));
1694 if (remote_ip->sa.sa_family == AF_INET) {
1695 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1696 loopback.sa.sa_family = AF_INET;
1697 #if IS_ENABLED(CONFIG_IPV6)
1698 } else {
1699 loopback.sin6.sin6_addr = in6addr_loopback;
1700 loopback.sa.sa_family = AF_INET6;
1701 #endif
1704 if (dst_vxlan->flags & VXLAN_F_LEARN)
1705 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1707 u64_stats_update_begin(&tx_stats->syncp);
1708 tx_stats->tx_packets++;
1709 tx_stats->tx_bytes += len;
1710 u64_stats_update_end(&tx_stats->syncp);
1712 if (netif_rx(skb) == NET_RX_SUCCESS) {
1713 u64_stats_update_begin(&rx_stats->syncp);
1714 rx_stats->rx_packets++;
1715 rx_stats->rx_bytes += len;
1716 u64_stats_update_end(&rx_stats->syncp);
1717 } else {
1718 dev->stats.rx_dropped++;
1722 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1723 struct vxlan_rdst *rdst, bool did_rsc)
1725 struct vxlan_dev *vxlan = netdev_priv(dev);
1726 struct rtable *rt = NULL;
1727 const struct iphdr *old_iph;
1728 struct flowi4 fl4;
1729 union vxlan_addr *dst;
1730 __be16 src_port = 0, dst_port;
1731 u32 vni;
1732 __be16 df = 0;
1733 __u8 tos, ttl;
1734 int err;
1736 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1737 vni = rdst->remote_vni;
1738 dst = &rdst->remote_ip;
1740 if (vxlan_addr_any(dst)) {
1741 if (did_rsc) {
1742 /* short-circuited back to local bridge */
1743 vxlan_encap_bypass(skb, vxlan, vxlan);
1744 return;
1746 goto drop;
1749 old_iph = ip_hdr(skb);
1751 ttl = vxlan->ttl;
1752 if (!ttl && vxlan_addr_multicast(dst))
1753 ttl = 1;
1755 tos = vxlan->tos;
1756 if (tos == 1)
1757 tos = ip_tunnel_get_dsfield(old_iph, skb);
1759 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
1761 if (dst->sa.sa_family == AF_INET) {
1762 memset(&fl4, 0, sizeof(fl4));
1763 fl4.flowi4_oif = rdst->remote_ifindex;
1764 fl4.flowi4_tos = RT_TOS(tos);
1765 fl4.daddr = dst->sin.sin_addr.s_addr;
1766 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1768 rt = ip_route_output_key(dev_net(dev), &fl4);
1769 if (IS_ERR(rt)) {
1770 netdev_dbg(dev, "no route to %pI4\n",
1771 &dst->sin.sin_addr.s_addr);
1772 dev->stats.tx_carrier_errors++;
1773 goto tx_error;
1776 if (rt->dst.dev == dev) {
1777 netdev_dbg(dev, "circular route to %pI4\n",
1778 &dst->sin.sin_addr.s_addr);
1779 dev->stats.collisions++;
1780 goto rt_tx_error;
1783 /* Bypass encapsulation if the destination is local */
1784 if (rt->rt_flags & RTCF_LOCAL &&
1785 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1786 struct vxlan_dev *dst_vxlan;
1788 ip_rt_put(rt);
1789 dst_vxlan = vxlan_find_vni(dev_net(dev), vni,
1790 dst->sa.sa_family, dst_port);
1791 if (!dst_vxlan)
1792 goto tx_error;
1793 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1794 return;
1797 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1798 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1800 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1801 fl4.saddr, dst->sin.sin_addr.s_addr,
1802 tos, ttl, df, src_port, dst_port,
1803 htonl(vni << 8));
1805 if (err < 0)
1806 goto rt_tx_error;
1807 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1808 #if IS_ENABLED(CONFIG_IPV6)
1809 } else {
1810 struct sock *sk = vxlan->vn_sock->sock->sk;
1811 struct dst_entry *ndst;
1812 struct flowi6 fl6;
1813 u32 flags;
1815 memset(&fl6, 0, sizeof(fl6));
1816 fl6.flowi6_oif = rdst->remote_ifindex;
1817 fl6.daddr = dst->sin6.sin6_addr;
1818 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1819 fl6.flowi6_proto = IPPROTO_UDP;
1821 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1822 netdev_dbg(dev, "no route to %pI6\n",
1823 &dst->sin6.sin6_addr);
1824 dev->stats.tx_carrier_errors++;
1825 goto tx_error;
1828 if (ndst->dev == dev) {
1829 netdev_dbg(dev, "circular route to %pI6\n",
1830 &dst->sin6.sin6_addr);
1831 dst_release(ndst);
1832 dev->stats.collisions++;
1833 goto tx_error;
1836 /* Bypass encapsulation if the destination is local */
1837 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1838 if (flags & RTF_LOCAL &&
1839 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1840 struct vxlan_dev *dst_vxlan;
1842 dst_release(ndst);
1843 dst_vxlan = vxlan_find_vni(dev_net(dev), vni,
1844 dst->sa.sa_family, dst_port);
1845 if (!dst_vxlan)
1846 goto tx_error;
1847 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1848 return;
1851 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1853 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
1854 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1855 src_port, dst_port, htonl(vni << 8));
1856 #endif
1859 return;
1861 drop:
1862 dev->stats.tx_dropped++;
1863 goto tx_free;
1865 rt_tx_error:
1866 ip_rt_put(rt);
1867 tx_error:
1868 dev->stats.tx_errors++;
1869 tx_free:
1870 dev_kfree_skb(skb);
1873 /* Transmit local packets over Vxlan
1875 * Outer IP header inherits ECN and DF from inner header.
1876 * Outer UDP destination is the VXLAN assigned port.
1877 * source port is based on hash of flow
1879 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1881 struct vxlan_dev *vxlan = netdev_priv(dev);
1882 struct ethhdr *eth;
1883 bool did_rsc = false;
1884 struct vxlan_rdst *rdst;
1885 struct vxlan_fdb *f;
1887 skb_reset_mac_header(skb);
1888 eth = eth_hdr(skb);
1890 if ((vxlan->flags & VXLAN_F_PROXY)) {
1891 if (ntohs(eth->h_proto) == ETH_P_ARP)
1892 return arp_reduce(dev, skb);
1893 #if IS_ENABLED(CONFIG_IPV6)
1894 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1895 pskb_may_pull(skb, sizeof(struct ipv6hdr)
1896 + sizeof(struct nd_msg)) &&
1897 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1898 struct nd_msg *msg;
1900 msg = (struct nd_msg *)skb_transport_header(skb);
1901 if (msg->icmph.icmp6_code == 0 &&
1902 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1903 return neigh_reduce(dev, skb);
1905 eth = eth_hdr(skb);
1906 #endif
1909 f = vxlan_find_mac(vxlan, eth->h_dest);
1910 did_rsc = false;
1912 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1913 (ntohs(eth->h_proto) == ETH_P_IP ||
1914 ntohs(eth->h_proto) == ETH_P_IPV6)) {
1915 did_rsc = route_shortcircuit(dev, skb);
1916 if (did_rsc)
1917 f = vxlan_find_mac(vxlan, eth->h_dest);
1920 if (f == NULL) {
1921 f = vxlan_find_mac(vxlan, all_zeros_mac);
1922 if (f == NULL) {
1923 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1924 !is_multicast_ether_addr(eth->h_dest))
1925 vxlan_fdb_miss(vxlan, eth->h_dest);
1927 dev->stats.tx_dropped++;
1928 dev_kfree_skb(skb);
1929 return NETDEV_TX_OK;
1933 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1934 struct sk_buff *skb1;
1936 skb1 = skb_clone(skb, GFP_ATOMIC);
1937 if (skb1)
1938 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1941 dev_kfree_skb(skb);
1942 return NETDEV_TX_OK;
1945 /* Walk the forwarding table and purge stale entries */
1946 static void vxlan_cleanup(unsigned long arg)
1948 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1949 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1950 unsigned int h;
1952 if (!netif_running(vxlan->dev))
1953 return;
1955 spin_lock_bh(&vxlan->hash_lock);
1956 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1957 struct hlist_node *p, *n;
1958 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1959 struct vxlan_fdb *f
1960 = container_of(p, struct vxlan_fdb, hlist);
1961 unsigned long timeout;
1963 if (f->state & NUD_PERMANENT)
1964 continue;
1966 timeout = f->used + vxlan->age_interval * HZ;
1967 if (time_before_eq(timeout, jiffies)) {
1968 netdev_dbg(vxlan->dev,
1969 "garbage collect %pM\n",
1970 f->eth_addr);
1971 f->state = NUD_STALE;
1972 vxlan_fdb_destroy(vxlan, f);
1973 } else if (time_before(timeout, next_timer))
1974 next_timer = timeout;
1977 spin_unlock_bh(&vxlan->hash_lock);
1979 mod_timer(&vxlan->age_timer, next_timer);
1982 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1984 __u32 vni = vxlan->default_dst.remote_vni;
1986 vxlan->vn_sock = vs;
1987 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1990 /* Setup stats when device is created */
1991 static int vxlan_init(struct net_device *dev)
1993 struct vxlan_dev *vxlan = netdev_priv(dev);
1994 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1995 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
1996 struct vxlan_sock *vs;
1998 dev->tstats = alloc_percpu(struct pcpu_tstats);
1999 if (!dev->tstats)
2000 return -ENOMEM;
2002 spin_lock(&vn->sock_lock);
2003 vs = vxlan_find_sock(dev_net(dev), ipv6 ? AF_INET6 : AF_INET,
2004 vxlan->dst_port);
2005 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
2006 /* If we have a socket with same port already, reuse it */
2007 vxlan_vs_add_dev(vs, vxlan);
2008 } else {
2009 /* otherwise make new socket outside of RTNL */
2010 dev_hold(dev);
2011 queue_work(vxlan_wq, &vxlan->sock_work);
2013 spin_unlock(&vn->sock_lock);
2015 return 0;
2018 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2020 struct vxlan_fdb *f;
2022 spin_lock_bh(&vxlan->hash_lock);
2023 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2024 if (f)
2025 vxlan_fdb_destroy(vxlan, f);
2026 spin_unlock_bh(&vxlan->hash_lock);
2029 static void vxlan_uninit(struct net_device *dev)
2031 struct vxlan_dev *vxlan = netdev_priv(dev);
2032 struct vxlan_sock *vs = vxlan->vn_sock;
2034 vxlan_fdb_delete_default(vxlan);
2036 if (vs)
2037 vxlan_sock_release(vs);
2038 free_percpu(dev->tstats);
2041 /* Start ageing timer and join group when device is brought up */
2042 static int vxlan_open(struct net_device *dev)
2044 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2045 struct vxlan_dev *vxlan = netdev_priv(dev);
2046 struct vxlan_sock *vs = vxlan->vn_sock;
2048 /* socket hasn't been created */
2049 if (!vs)
2050 return -ENOTCONN;
2052 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2053 vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
2054 vxlan_sock_hold(vs);
2055 dev_hold(dev);
2056 queue_work(vxlan_wq, &vxlan->igmp_join);
2059 if (vxlan->age_interval)
2060 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2062 return 0;
2065 /* Purge the forwarding table */
2066 static void vxlan_flush(struct vxlan_dev *vxlan)
2068 unsigned int h;
2070 spin_lock_bh(&vxlan->hash_lock);
2071 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2072 struct hlist_node *p, *n;
2073 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2074 struct vxlan_fdb *f
2075 = container_of(p, struct vxlan_fdb, hlist);
2076 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2077 if (!is_zero_ether_addr(f->eth_addr))
2078 vxlan_fdb_destroy(vxlan, f);
2081 spin_unlock_bh(&vxlan->hash_lock);
2084 /* Cleanup timer and forwarding table on shutdown */
2085 static int vxlan_stop(struct net_device *dev)
2087 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2088 struct vxlan_dev *vxlan = netdev_priv(dev);
2089 struct vxlan_sock *vs = vxlan->vn_sock;
2091 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2092 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
2093 vxlan_sock_hold(vs);
2094 dev_hold(dev);
2095 queue_work(vxlan_wq, &vxlan->igmp_leave);
2098 del_timer_sync(&vxlan->age_timer);
2100 vxlan_flush(vxlan);
2102 return 0;
2105 /* Stub, nothing needs to be done. */
2106 static void vxlan_set_multicast_list(struct net_device *dev)
2110 static const struct net_device_ops vxlan_netdev_ops = {
2111 .ndo_init = vxlan_init,
2112 .ndo_uninit = vxlan_uninit,
2113 .ndo_open = vxlan_open,
2114 .ndo_stop = vxlan_stop,
2115 .ndo_start_xmit = vxlan_xmit,
2116 .ndo_get_stats64 = ip_tunnel_get_stats64,
2117 .ndo_set_rx_mode = vxlan_set_multicast_list,
2118 .ndo_change_mtu = eth_change_mtu,
2119 .ndo_validate_addr = eth_validate_addr,
2120 .ndo_set_mac_address = eth_mac_addr,
2121 .ndo_fdb_add = vxlan_fdb_add,
2122 .ndo_fdb_del = vxlan_fdb_delete,
2123 .ndo_fdb_dump = vxlan_fdb_dump,
2126 /* Info for udev, that this is a virtual tunnel endpoint */
2127 static struct device_type vxlan_type = {
2128 .name = "vxlan",
2131 /* Calls the ndo_add_vxlan_port of the caller in order to
2132 * supply the listening VXLAN udp ports. Callers are expected
2133 * to implement the ndo_add_vxlan_port.
2135 void vxlan_get_rx_port(struct net_device *dev)
2137 struct vxlan_sock *vs;
2138 struct net *net = dev_net(dev);
2139 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2140 sa_family_t sa_family;
2141 __be16 port;
2142 unsigned int i;
2144 spin_lock(&vn->sock_lock);
2145 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2146 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2147 port = inet_sk(vs->sock->sk)->inet_sport;
2148 sa_family = vs->sock->sk->sk_family;
2149 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2150 port);
2153 spin_unlock(&vn->sock_lock);
2155 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2157 /* Initialize the device structure. */
2158 static void vxlan_setup(struct net_device *dev)
2160 struct vxlan_dev *vxlan = netdev_priv(dev);
2161 unsigned int h;
2162 int low, high;
2164 eth_hw_addr_random(dev);
2165 ether_setup(dev);
2166 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2167 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2168 else
2169 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2171 dev->netdev_ops = &vxlan_netdev_ops;
2172 dev->destructor = free_netdev;
2173 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2175 dev->tx_queue_len = 0;
2176 dev->features |= NETIF_F_LLTX;
2177 dev->features |= NETIF_F_NETNS_LOCAL;
2178 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2179 dev->features |= NETIF_F_RXCSUM;
2180 dev->features |= NETIF_F_GSO_SOFTWARE;
2182 dev->vlan_features = dev->features;
2183 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2184 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2185 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2186 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2187 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
2188 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2190 INIT_LIST_HEAD(&vxlan->next);
2191 spin_lock_init(&vxlan->hash_lock);
2192 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2193 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
2194 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
2196 init_timer_deferrable(&vxlan->age_timer);
2197 vxlan->age_timer.function = vxlan_cleanup;
2198 vxlan->age_timer.data = (unsigned long) vxlan;
2200 inet_get_local_port_range(&low, &high);
2201 vxlan->port_min = low;
2202 vxlan->port_max = high;
2203 vxlan->dst_port = htons(vxlan_port);
2205 vxlan->dev = dev;
2207 for (h = 0; h < FDB_HASH_SIZE; ++h)
2208 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2211 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2212 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2213 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2214 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2215 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2216 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2217 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2218 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2219 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2220 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2221 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2222 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2223 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2224 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2225 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2226 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2227 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2228 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2231 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2233 if (tb[IFLA_ADDRESS]) {
2234 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2235 pr_debug("invalid link address (not ethernet)\n");
2236 return -EINVAL;
2239 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2240 pr_debug("invalid all zero ethernet address\n");
2241 return -EADDRNOTAVAIL;
2245 if (!data)
2246 return -EINVAL;
2248 if (data[IFLA_VXLAN_ID]) {
2249 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2250 if (id >= VXLAN_VID_MASK)
2251 return -ERANGE;
2254 if (data[IFLA_VXLAN_PORT_RANGE]) {
2255 const struct ifla_vxlan_port_range *p
2256 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2258 if (ntohs(p->high) < ntohs(p->low)) {
2259 pr_debug("port range %u .. %u not valid\n",
2260 ntohs(p->low), ntohs(p->high));
2261 return -EINVAL;
2265 return 0;
2268 static void vxlan_get_drvinfo(struct net_device *netdev,
2269 struct ethtool_drvinfo *drvinfo)
2271 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2272 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2275 static const struct ethtool_ops vxlan_ethtool_ops = {
2276 .get_drvinfo = vxlan_get_drvinfo,
2277 .get_link = ethtool_op_get_link,
2280 static void vxlan_del_work(struct work_struct *work)
2282 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2284 sk_release_kernel(vs->sock->sk);
2285 kfree_rcu(vs, rcu);
2288 #if IS_ENABLED(CONFIG_IPV6)
2289 /* Create UDP socket for encapsulation receive. AF_INET6 socket
2290 * could be used for both IPv4 and IPv6 communications, but
2291 * users may set bindv6only=1.
2293 static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2295 struct sock *sk;
2296 struct socket *sock;
2297 struct sockaddr_in6 vxlan_addr = {
2298 .sin6_family = AF_INET6,
2299 .sin6_port = port,
2301 int rc, val = 1;
2303 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2304 if (rc < 0) {
2305 pr_debug("UDPv6 socket create failed\n");
2306 return rc;
2309 /* Put in proper namespace */
2310 sk = sock->sk;
2311 sk_change_net(sk, net);
2313 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2314 (char *)&val, sizeof(val));
2315 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2316 sizeof(struct sockaddr_in6));
2317 if (rc < 0) {
2318 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2319 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2320 sk_release_kernel(sk);
2321 return rc;
2323 /* At this point, IPv6 module should have been loaded in
2324 * sock_create_kern().
2326 BUG_ON(!ipv6_stub);
2328 *psock = sock;
2329 /* Disable multicast loopback */
2330 inet_sk(sk)->mc_loop = 0;
2331 return 0;
2334 #else
2336 static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2338 return -EPFNOSUPPORT;
2340 #endif
2342 static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
2344 struct sock *sk;
2345 struct socket *sock;
2346 struct sockaddr_in vxlan_addr = {
2347 .sin_family = AF_INET,
2348 .sin_addr.s_addr = htonl(INADDR_ANY),
2349 .sin_port = port,
2351 int rc;
2353 /* Create UDP socket for encapsulation receive. */
2354 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
2355 if (rc < 0) {
2356 pr_debug("UDP socket create failed\n");
2357 return rc;
2360 /* Put in proper namespace */
2361 sk = sock->sk;
2362 sk_change_net(sk, net);
2364 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
2365 sizeof(vxlan_addr));
2366 if (rc < 0) {
2367 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2368 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2369 sk_release_kernel(sk);
2370 return rc;
2373 *psock = sock;
2374 /* Disable multicast loopback */
2375 inet_sk(sk)->mc_loop = 0;
2376 return 0;
2379 /* Create new listen socket if needed */
2380 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2381 vxlan_rcv_t *rcv, void *data, bool ipv6)
2383 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2384 struct vxlan_sock *vs;
2385 struct socket *sock;
2386 struct sock *sk;
2387 int rc = 0;
2388 unsigned int h;
2390 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
2391 if (!vs)
2392 return ERR_PTR(-ENOMEM);
2394 for (h = 0; h < VNI_HASH_SIZE; ++h)
2395 INIT_HLIST_HEAD(&vs->vni_list[h]);
2397 INIT_WORK(&vs->del_work, vxlan_del_work);
2399 if (ipv6)
2400 rc = create_v6_sock(net, port, &sock);
2401 else
2402 rc = create_v4_sock(net, port, &sock);
2403 if (rc < 0) {
2404 kfree(vs);
2405 return ERR_PTR(rc);
2408 vs->sock = sock;
2409 sk = sock->sk;
2410 atomic_set(&vs->refcnt, 1);
2411 vs->rcv = rcv;
2412 vs->data = data;
2413 rcu_assign_sk_user_data(vs->sock->sk, vs);
2415 spin_lock(&vn->sock_lock);
2416 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2417 vxlan_notify_add_rx_port(sk);
2418 spin_unlock(&vn->sock_lock);
2420 /* Mark socket as an encapsulation socket. */
2421 udp_sk(sk)->encap_type = 1;
2422 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
2423 #if IS_ENABLED(CONFIG_IPV6)
2424 if (ipv6)
2425 ipv6_stub->udpv6_encap_enable();
2426 else
2427 #endif
2428 udp_encap_enable();
2430 return vs;
2433 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2434 vxlan_rcv_t *rcv, void *data,
2435 bool no_share, bool ipv6)
2437 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2438 struct vxlan_sock *vs;
2440 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
2441 if (!IS_ERR(vs))
2442 return vs;
2444 if (no_share) /* Return error if sharing is not allowed. */
2445 return vs;
2447 spin_lock(&vn->sock_lock);
2448 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
2449 if (vs && ((vs->rcv != rcv) ||
2450 !atomic_add_unless(&vs->refcnt, 1, 0)))
2451 vs = ERR_PTR(-EBUSY);
2452 spin_unlock(&vn->sock_lock);
2454 if (!vs)
2455 vs = ERR_PTR(-EINVAL);
2457 return vs;
2459 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2461 /* Scheduled at device creation to bind to a socket */
2462 static void vxlan_sock_work(struct work_struct *work)
2464 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2465 struct net *net = dev_net(vxlan->dev);
2466 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2467 __be16 port = vxlan->dst_port;
2468 struct vxlan_sock *nvs;
2470 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
2471 spin_lock(&vn->sock_lock);
2472 if (!IS_ERR(nvs))
2473 vxlan_vs_add_dev(nvs, vxlan);
2474 spin_unlock(&vn->sock_lock);
2476 dev_put(vxlan->dev);
2479 static int vxlan_newlink(struct net *net, struct net_device *dev,
2480 struct nlattr *tb[], struct nlattr *data[])
2482 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2483 struct vxlan_dev *vxlan = netdev_priv(dev);
2484 struct vxlan_rdst *dst = &vxlan->default_dst;
2485 __u32 vni;
2486 int err;
2487 bool use_ipv6 = false;
2489 if (!data[IFLA_VXLAN_ID])
2490 return -EINVAL;
2492 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2493 dst->remote_vni = vni;
2495 /* Unless IPv6 is explicitly requested, assume IPv4 */
2496 dst->remote_ip.sa.sa_family = AF_INET;
2497 if (data[IFLA_VXLAN_GROUP]) {
2498 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2499 } else if (data[IFLA_VXLAN_GROUP6]) {
2500 if (!IS_ENABLED(CONFIG_IPV6))
2501 return -EPFNOSUPPORT;
2503 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2504 sizeof(struct in6_addr));
2505 dst->remote_ip.sa.sa_family = AF_INET6;
2506 use_ipv6 = true;
2509 if (data[IFLA_VXLAN_LOCAL]) {
2510 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2511 vxlan->saddr.sa.sa_family = AF_INET;
2512 } else if (data[IFLA_VXLAN_LOCAL6]) {
2513 if (!IS_ENABLED(CONFIG_IPV6))
2514 return -EPFNOSUPPORT;
2516 /* TODO: respect scope id */
2517 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2518 sizeof(struct in6_addr));
2519 vxlan->saddr.sa.sa_family = AF_INET6;
2520 use_ipv6 = true;
2523 if (data[IFLA_VXLAN_LINK] &&
2524 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2525 struct net_device *lowerdev
2526 = __dev_get_by_index(net, dst->remote_ifindex);
2528 if (!lowerdev) {
2529 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2530 return -ENODEV;
2533 #if IS_ENABLED(CONFIG_IPV6)
2534 if (use_ipv6) {
2535 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2536 if (idev && idev->cnf.disable_ipv6) {
2537 pr_info("IPv6 is disabled via sysctl\n");
2538 return -EPERM;
2540 vxlan->flags |= VXLAN_F_IPV6;
2542 #endif
2544 if (!tb[IFLA_MTU])
2545 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2547 dev->needed_headroom = lowerdev->hard_header_len +
2548 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2551 if (data[IFLA_VXLAN_TOS])
2552 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2554 if (data[IFLA_VXLAN_TTL])
2555 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2557 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2558 vxlan->flags |= VXLAN_F_LEARN;
2560 if (data[IFLA_VXLAN_AGEING])
2561 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2562 else
2563 vxlan->age_interval = FDB_AGE_DEFAULT;
2565 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2566 vxlan->flags |= VXLAN_F_PROXY;
2568 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2569 vxlan->flags |= VXLAN_F_RSC;
2571 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2572 vxlan->flags |= VXLAN_F_L2MISS;
2574 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2575 vxlan->flags |= VXLAN_F_L3MISS;
2577 if (data[IFLA_VXLAN_LIMIT])
2578 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2580 if (data[IFLA_VXLAN_PORT_RANGE]) {
2581 const struct ifla_vxlan_port_range *p
2582 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2583 vxlan->port_min = ntohs(p->low);
2584 vxlan->port_max = ntohs(p->high);
2587 if (data[IFLA_VXLAN_PORT])
2588 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2590 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2591 vxlan->dst_port)) {
2592 pr_info("duplicate VNI %u\n", vni);
2593 return -EEXIST;
2596 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2598 /* create an fdb entry for a valid default destination */
2599 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2600 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2601 &vxlan->default_dst.remote_ip,
2602 NUD_REACHABLE|NUD_PERMANENT,
2603 NLM_F_EXCL|NLM_F_CREATE,
2604 vxlan->dst_port,
2605 vxlan->default_dst.remote_vni,
2606 vxlan->default_dst.remote_ifindex,
2607 NTF_SELF);
2608 if (err)
2609 return err;
2612 err = register_netdevice(dev);
2613 if (err) {
2614 vxlan_fdb_delete_default(vxlan);
2615 return err;
2618 list_add(&vxlan->next, &vn->vxlan_list);
2620 return 0;
2623 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2625 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2626 struct vxlan_dev *vxlan = netdev_priv(dev);
2628 spin_lock(&vn->sock_lock);
2629 if (!hlist_unhashed(&vxlan->hlist))
2630 hlist_del_rcu(&vxlan->hlist);
2631 spin_unlock(&vn->sock_lock);
2633 list_del(&vxlan->next);
2634 unregister_netdevice_queue(dev, head);
2637 static size_t vxlan_get_size(const struct net_device *dev)
2640 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
2641 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2642 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2643 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2644 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2645 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2646 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
2647 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2648 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2649 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2650 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
2651 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2652 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2653 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2654 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
2658 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2660 const struct vxlan_dev *vxlan = netdev_priv(dev);
2661 const struct vxlan_rdst *dst = &vxlan->default_dst;
2662 struct ifla_vxlan_port_range ports = {
2663 .low = htons(vxlan->port_min),
2664 .high = htons(vxlan->port_max),
2667 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2668 goto nla_put_failure;
2670 if (!vxlan_addr_any(&dst->remote_ip)) {
2671 if (dst->remote_ip.sa.sa_family == AF_INET) {
2672 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2673 dst->remote_ip.sin.sin_addr.s_addr))
2674 goto nla_put_failure;
2675 #if IS_ENABLED(CONFIG_IPV6)
2676 } else {
2677 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2678 &dst->remote_ip.sin6.sin6_addr))
2679 goto nla_put_failure;
2680 #endif
2684 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2685 goto nla_put_failure;
2687 if (!vxlan_addr_any(&vxlan->saddr)) {
2688 if (vxlan->saddr.sa.sa_family == AF_INET) {
2689 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2690 vxlan->saddr.sin.sin_addr.s_addr))
2691 goto nla_put_failure;
2692 #if IS_ENABLED(CONFIG_IPV6)
2693 } else {
2694 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2695 &vxlan->saddr.sin6.sin6_addr))
2696 goto nla_put_failure;
2697 #endif
2701 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2702 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2703 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2704 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2705 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2706 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2707 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2708 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2709 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2710 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2711 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2712 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2713 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2714 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
2715 goto nla_put_failure;
2717 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2718 goto nla_put_failure;
2720 return 0;
2722 nla_put_failure:
2723 return -EMSGSIZE;
2726 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2727 .kind = "vxlan",
2728 .maxtype = IFLA_VXLAN_MAX,
2729 .policy = vxlan_policy,
2730 .priv_size = sizeof(struct vxlan_dev),
2731 .setup = vxlan_setup,
2732 .validate = vxlan_validate,
2733 .newlink = vxlan_newlink,
2734 .dellink = vxlan_dellink,
2735 .get_size = vxlan_get_size,
2736 .fill_info = vxlan_fill_info,
2739 static __net_init int vxlan_init_net(struct net *net)
2741 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2742 unsigned int h;
2744 INIT_LIST_HEAD(&vn->vxlan_list);
2745 spin_lock_init(&vn->sock_lock);
2747 for (h = 0; h < PORT_HASH_SIZE; ++h)
2748 INIT_HLIST_HEAD(&vn->sock_list[h]);
2750 return 0;
2753 static __net_exit void vxlan_exit_net(struct net *net)
2755 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2756 struct vxlan_dev *vxlan;
2757 LIST_HEAD(list);
2759 rtnl_lock();
2760 list_for_each_entry(vxlan, &vn->vxlan_list, next)
2761 unregister_netdevice_queue(vxlan->dev, &list);
2762 unregister_netdevice_many(&list);
2763 rtnl_unlock();
2766 static struct pernet_operations vxlan_net_ops = {
2767 .init = vxlan_init_net,
2768 .exit = vxlan_exit_net,
2769 .id = &vxlan_net_id,
2770 .size = sizeof(struct vxlan_net),
2773 static int __init vxlan_init_module(void)
2775 int rc;
2777 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2778 if (!vxlan_wq)
2779 return -ENOMEM;
2781 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2783 rc = register_pernet_device(&vxlan_net_ops);
2784 if (rc)
2785 goto out1;
2787 rc = rtnl_link_register(&vxlan_link_ops);
2788 if (rc)
2789 goto out2;
2791 return 0;
2793 out2:
2794 unregister_pernet_device(&vxlan_net_ops);
2795 out1:
2796 destroy_workqueue(vxlan_wq);
2797 return rc;
2799 late_initcall(vxlan_init_module);
2801 static void __exit vxlan_cleanup_module(void)
2803 rtnl_link_unregister(&vxlan_link_ops);
2804 destroy_workqueue(vxlan_wq);
2805 unregister_pernet_device(&vxlan_net_ops);
2806 rcu_barrier();
2808 module_exit(vxlan_cleanup_module);
2810 MODULE_LICENSE("GPL");
2811 MODULE_VERSION(VXLAN_VERSION);
2812 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2813 MODULE_ALIAS_RTNL_LINK("vxlan");