Revert "ALSA: hda: Flush interrupts on disabling"
[linux/fpc-iii.git] / drivers / net / vxlan.c
blobb6ee0c1690d83116693eabe91bd6bbddc6bdcd53
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/module.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/udp.h>
18 #include <linux/igmp.h>
19 #include <linux/if_ether.h>
20 #include <linux/ethtool.h>
21 #include <net/arp.h>
22 #include <net/ndisc.h>
23 #include <net/ip.h>
24 #include <net/icmp.h>
25 #include <net/rtnetlink.h>
26 #include <net/inet_ecn.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
29 #include <net/vxlan.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_tunnel.h>
33 #include <net/ip6_checksum.h>
34 #endif
36 #define VXLAN_VERSION "0.1"
38 #define PORT_HASH_BITS 8
39 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
40 #define FDB_AGE_DEFAULT 300 /* 5 min */
41 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
43 /* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
45 * for compatibility with early adopters.
47 static unsigned short vxlan_port __read_mostly = 8472;
48 module_param_named(udp_port, vxlan_port, ushort, 0444);
49 MODULE_PARM_DESC(udp_port, "Destination UDP port");
51 static bool log_ecn_error = true;
52 module_param(log_ecn_error, bool, 0644);
53 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
55 static int vxlan_net_id;
56 static struct rtnl_link_ops vxlan_link_ops;
58 static const u8 all_zeros_mac[ETH_ALEN + 2];
60 static int vxlan_sock_add(struct vxlan_dev *vxlan);
62 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
64 /* per-network namespace private data for this module */
65 struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
68 spinlock_t sock_lock;
71 /* Forwarding table entry */
72 struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
77 struct list_head remotes;
78 u8 eth_addr[ETH_ALEN];
79 u16 state; /* see ndm_state */
80 u8 flags; /* see ndm_flags */
83 /* salt for hash table */
84 static u32 vxlan_salt __read_mostly;
86 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
88 return vs->flags & VXLAN_F_COLLECT_METADATA ||
89 ip_tunnel_collect_metadata();
92 #if IS_ENABLED(CONFIG_IPV6)
93 static inline
94 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
96 if (a->sa.sa_family != b->sa.sa_family)
97 return false;
98 if (a->sa.sa_family == AF_INET6)
99 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
100 else
101 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
104 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
106 if (ipa->sa.sa_family == AF_INET6)
107 return ipv6_addr_any(&ipa->sin6.sin6_addr);
108 else
109 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
112 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
114 if (ipa->sa.sa_family == AF_INET6)
115 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
116 else
117 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
120 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
122 if (nla_len(nla) >= sizeof(struct in6_addr)) {
123 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
124 ip->sa.sa_family = AF_INET6;
125 return 0;
126 } else if (nla_len(nla) >= sizeof(__be32)) {
127 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
128 ip->sa.sa_family = AF_INET;
129 return 0;
130 } else {
131 return -EAFNOSUPPORT;
135 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
136 const union vxlan_addr *ip)
138 if (ip->sa.sa_family == AF_INET6)
139 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
140 else
141 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
144 #else /* !CONFIG_IPV6 */
146 static inline
147 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
149 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
152 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
154 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
157 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
159 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
162 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
164 if (nla_len(nla) >= sizeof(struct in6_addr)) {
165 return -EAFNOSUPPORT;
166 } else if (nla_len(nla) >= sizeof(__be32)) {
167 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
168 ip->sa.sa_family = AF_INET;
169 return 0;
170 } else {
171 return -EAFNOSUPPORT;
175 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
176 const union vxlan_addr *ip)
178 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
180 #endif
182 /* Virtual Network hash table head */
183 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
185 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
188 /* Socket hash table head */
189 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
191 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
193 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
196 /* First remote destination for a forwarding entry.
197 * Guaranteed to be non-NULL because remotes are never deleted.
199 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
201 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
204 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
206 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
209 /* Find VXLAN socket based on network namespace, address family and UDP port
210 * and enabled unshareable flags.
212 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
213 __be16 port, u32 flags)
215 struct vxlan_sock *vs;
217 flags &= VXLAN_F_RCV_FLAGS;
219 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
220 if (inet_sk(vs->sock->sk)->inet_sport == port &&
221 vxlan_get_sk_family(vs) == family &&
222 vs->flags == flags)
223 return vs;
225 return NULL;
228 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
230 struct vxlan_dev_node *node;
232 /* For flow based devices, map all packets to VNI 0 */
233 if (vs->flags & VXLAN_F_COLLECT_METADATA)
234 vni = 0;
236 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
237 if (node->vxlan->default_dst.remote_vni == vni)
238 return node->vxlan;
241 return NULL;
244 /* Look up VNI in a per net namespace table */
245 static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
246 sa_family_t family, __be16 port,
247 u32 flags)
249 struct vxlan_sock *vs;
251 vs = vxlan_find_sock(net, family, port, flags);
252 if (!vs)
253 return NULL;
255 return vxlan_vs_find_vni(vs, vni);
258 /* Fill in neighbour message in skbuff. */
259 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
260 const struct vxlan_fdb *fdb,
261 u32 portid, u32 seq, int type, unsigned int flags,
262 const struct vxlan_rdst *rdst)
264 unsigned long now = jiffies;
265 struct nda_cacheinfo ci;
266 struct nlmsghdr *nlh;
267 struct ndmsg *ndm;
268 bool send_ip, send_eth;
270 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
271 if (nlh == NULL)
272 return -EMSGSIZE;
274 ndm = nlmsg_data(nlh);
275 memset(ndm, 0, sizeof(*ndm));
277 send_eth = send_ip = true;
279 if (type == RTM_GETNEIGH) {
280 ndm->ndm_family = AF_INET;
281 send_ip = !vxlan_addr_any(&rdst->remote_ip);
282 send_eth = !is_zero_ether_addr(fdb->eth_addr);
283 } else
284 ndm->ndm_family = AF_BRIDGE;
285 ndm->ndm_state = fdb->state;
286 ndm->ndm_ifindex = vxlan->dev->ifindex;
287 ndm->ndm_flags = fdb->flags;
288 ndm->ndm_type = RTN_UNICAST;
290 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
291 nla_put_s32(skb, NDA_LINK_NETNSID,
292 peernet2id(dev_net(vxlan->dev), vxlan->net)))
293 goto nla_put_failure;
295 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
296 goto nla_put_failure;
298 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
299 goto nla_put_failure;
301 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
302 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
303 goto nla_put_failure;
304 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
305 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
306 goto nla_put_failure;
307 if (rdst->remote_ifindex &&
308 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
309 goto nla_put_failure;
311 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
312 ci.ndm_confirmed = 0;
313 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
314 ci.ndm_refcnt = 0;
316 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
317 goto nla_put_failure;
319 nlmsg_end(skb, nlh);
320 return 0;
322 nla_put_failure:
323 nlmsg_cancel(skb, nlh);
324 return -EMSGSIZE;
327 static inline size_t vxlan_nlmsg_size(void)
329 return NLMSG_ALIGN(sizeof(struct ndmsg))
330 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
331 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
332 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
333 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
334 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
335 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
336 + nla_total_size(sizeof(struct nda_cacheinfo));
339 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
340 struct vxlan_rdst *rd, int type)
342 struct net *net = dev_net(vxlan->dev);
343 struct sk_buff *skb;
344 int err = -ENOBUFS;
346 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
347 if (skb == NULL)
348 goto errout;
350 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
351 if (err < 0) {
352 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
353 WARN_ON(err == -EMSGSIZE);
354 kfree_skb(skb);
355 goto errout;
358 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
359 return;
360 errout:
361 if (err < 0)
362 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
365 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
367 struct vxlan_dev *vxlan = netdev_priv(dev);
368 struct vxlan_fdb f = {
369 .state = NUD_STALE,
371 struct vxlan_rdst remote = {
372 .remote_ip = *ipa, /* goes to NDA_DST */
373 .remote_vni = cpu_to_be32(VXLAN_N_VID),
376 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
379 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
381 struct vxlan_fdb f = {
382 .state = NUD_STALE,
384 struct vxlan_rdst remote = { };
386 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
388 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
391 /* Hash Ethernet address */
392 static u32 eth_hash(const unsigned char *addr)
394 u64 value = get_unaligned((u64 *)addr);
396 /* only want 6 bytes */
397 #ifdef __BIG_ENDIAN
398 value >>= 16;
399 #else
400 value <<= 16;
401 #endif
402 return hash_64(value, FDB_HASH_BITS);
405 /* Hash chain to use given mac address */
406 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
407 const u8 *mac)
409 return &vxlan->fdb_head[eth_hash(mac)];
412 /* Look up Ethernet address in forwarding table */
413 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
414 const u8 *mac)
416 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
417 struct vxlan_fdb *f;
419 hlist_for_each_entry_rcu(f, head, hlist) {
420 if (ether_addr_equal(mac, f->eth_addr))
421 return f;
424 return NULL;
427 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
428 const u8 *mac)
430 struct vxlan_fdb *f;
432 f = __vxlan_find_mac(vxlan, mac);
433 if (f)
434 f->used = jiffies;
436 return f;
439 /* caller should hold vxlan->hash_lock */
440 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
441 union vxlan_addr *ip, __be16 port,
442 __be32 vni, __u32 ifindex)
444 struct vxlan_rdst *rd;
446 list_for_each_entry(rd, &f->remotes, list) {
447 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
448 rd->remote_port == port &&
449 rd->remote_vni == vni &&
450 rd->remote_ifindex == ifindex)
451 return rd;
454 return NULL;
457 /* Replace destination of unicast mac */
458 static int vxlan_fdb_replace(struct vxlan_fdb *f,
459 union vxlan_addr *ip, __be16 port, __be32 vni,
460 __u32 ifindex)
462 struct vxlan_rdst *rd;
464 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
465 if (rd)
466 return 0;
468 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
469 if (!rd)
470 return 0;
472 dst_cache_reset(&rd->dst_cache);
473 rd->remote_ip = *ip;
474 rd->remote_port = port;
475 rd->remote_vni = vni;
476 rd->remote_ifindex = ifindex;
477 return 1;
480 /* Add/update destinations for multicast */
481 static int vxlan_fdb_append(struct vxlan_fdb *f,
482 union vxlan_addr *ip, __be16 port, __be32 vni,
483 __u32 ifindex, struct vxlan_rdst **rdp)
485 struct vxlan_rdst *rd;
487 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
488 if (rd)
489 return 0;
491 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
492 if (rd == NULL)
493 return -ENOBUFS;
495 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
496 kfree(rd);
497 return -ENOBUFS;
500 rd->remote_ip = *ip;
501 rd->remote_port = port;
502 rd->remote_vni = vni;
503 rd->remote_ifindex = ifindex;
505 list_add_tail_rcu(&rd->list, &f->remotes);
507 *rdp = rd;
508 return 1;
511 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
512 unsigned int off,
513 struct vxlanhdr *vh, size_t hdrlen,
514 __be32 vni_field,
515 struct gro_remcsum *grc,
516 bool nopartial)
518 size_t start, offset;
520 if (skb->remcsum_offload)
521 return vh;
523 if (!NAPI_GRO_CB(skb)->csum_valid)
524 return NULL;
526 start = vxlan_rco_start(vni_field);
527 offset = start + vxlan_rco_offset(vni_field);
529 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
530 start, offset, grc, nopartial);
532 skb->remcsum_offload = 1;
534 return vh;
537 static struct sk_buff **vxlan_gro_receive(struct sock *sk,
538 struct sk_buff **head,
539 struct sk_buff *skb)
541 struct sk_buff *p, **pp = NULL;
542 struct vxlanhdr *vh, *vh2;
543 unsigned int hlen, off_vx;
544 int flush = 1;
545 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
546 __be32 flags;
547 struct gro_remcsum grc;
549 skb_gro_remcsum_init(&grc);
551 off_vx = skb_gro_offset(skb);
552 hlen = off_vx + sizeof(*vh);
553 vh = skb_gro_header_fast(skb, off_vx);
554 if (skb_gro_header_hard(skb, hlen)) {
555 vh = skb_gro_header_slow(skb, hlen, off_vx);
556 if (unlikely(!vh))
557 goto out;
560 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
562 flags = vh->vx_flags;
564 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
565 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
566 vh->vx_vni, &grc,
567 !!(vs->flags &
568 VXLAN_F_REMCSUM_NOPARTIAL));
570 if (!vh)
571 goto out;
574 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
576 for (p = *head; p; p = p->next) {
577 if (!NAPI_GRO_CB(p)->same_flow)
578 continue;
580 vh2 = (struct vxlanhdr *)(p->data + off_vx);
581 if (vh->vx_flags != vh2->vx_flags ||
582 vh->vx_vni != vh2->vx_vni) {
583 NAPI_GRO_CB(p)->same_flow = 0;
584 continue;
588 pp = call_gro_receive(eth_gro_receive, head, skb);
589 flush = 0;
591 out:
592 skb_gro_remcsum_cleanup(skb, &grc);
593 NAPI_GRO_CB(skb)->flush |= flush;
595 return pp;
598 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
600 /* Sets 'skb->inner_mac_header' since we are always called with
601 * 'skb->encapsulation' set.
603 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
606 /* Add new entry to forwarding table -- assumes lock held */
607 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
608 const u8 *mac, union vxlan_addr *ip,
609 __u16 state, __u16 flags,
610 __be16 port, __be32 vni, __u32 ifindex,
611 __u8 ndm_flags)
613 struct vxlan_rdst *rd = NULL;
614 struct vxlan_fdb *f;
615 int notify = 0;
616 int rc;
618 f = __vxlan_find_mac(vxlan, mac);
619 if (f) {
620 if (flags & NLM_F_EXCL) {
621 netdev_dbg(vxlan->dev,
622 "lost race to create %pM\n", mac);
623 return -EEXIST;
625 if (f->state != state) {
626 f->state = state;
627 f->updated = jiffies;
628 notify = 1;
630 if (f->flags != ndm_flags) {
631 f->flags = ndm_flags;
632 f->updated = jiffies;
633 notify = 1;
635 if ((flags & NLM_F_REPLACE)) {
636 /* Only change unicasts */
637 if (!(is_multicast_ether_addr(f->eth_addr) ||
638 is_zero_ether_addr(f->eth_addr))) {
639 notify |= vxlan_fdb_replace(f, ip, port, vni,
640 ifindex);
641 } else
642 return -EOPNOTSUPP;
644 if ((flags & NLM_F_APPEND) &&
645 (is_multicast_ether_addr(f->eth_addr) ||
646 is_zero_ether_addr(f->eth_addr))) {
647 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
649 if (rc < 0)
650 return rc;
651 notify |= rc;
653 } else {
654 if (!(flags & NLM_F_CREATE))
655 return -ENOENT;
657 if (vxlan->cfg.addrmax &&
658 vxlan->addrcnt >= vxlan->cfg.addrmax)
659 return -ENOSPC;
661 /* Disallow replace to add a multicast entry */
662 if ((flags & NLM_F_REPLACE) &&
663 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
664 return -EOPNOTSUPP;
666 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
667 f = kmalloc(sizeof(*f), GFP_ATOMIC);
668 if (!f)
669 return -ENOMEM;
671 notify = 1;
672 f->state = state;
673 f->flags = ndm_flags;
674 f->updated = f->used = jiffies;
675 INIT_LIST_HEAD(&f->remotes);
676 memcpy(f->eth_addr, mac, ETH_ALEN);
678 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
679 if (rc < 0) {
680 kfree(f);
681 return rc;
684 ++vxlan->addrcnt;
685 hlist_add_head_rcu(&f->hlist,
686 vxlan_fdb_head(vxlan, mac));
689 if (notify) {
690 if (rd == NULL)
691 rd = first_remote_rtnl(f);
692 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
695 return 0;
698 static void vxlan_fdb_free(struct rcu_head *head)
700 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
701 struct vxlan_rdst *rd, *nd;
703 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
704 dst_cache_destroy(&rd->dst_cache);
705 kfree(rd);
707 kfree(f);
710 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
712 netdev_dbg(vxlan->dev,
713 "delete %pM\n", f->eth_addr);
715 --vxlan->addrcnt;
716 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
718 hlist_del_rcu(&f->hlist);
719 call_rcu(&f->rcu, vxlan_fdb_free);
722 static void vxlan_dst_free(struct rcu_head *head)
724 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
726 dst_cache_destroy(&rd->dst_cache);
727 kfree(rd);
730 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
731 struct vxlan_rdst *rd)
733 list_del_rcu(&rd->list);
734 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
735 call_rcu(&rd->rcu, vxlan_dst_free);
738 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
739 union vxlan_addr *ip, __be16 *port, __be32 *vni,
740 u32 *ifindex)
742 struct net *net = dev_net(vxlan->dev);
743 int err;
745 if (tb[NDA_DST]) {
746 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
747 if (err)
748 return err;
749 } else {
750 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
751 if (remote->sa.sa_family == AF_INET) {
752 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
753 ip->sa.sa_family = AF_INET;
754 #if IS_ENABLED(CONFIG_IPV6)
755 } else {
756 ip->sin6.sin6_addr = in6addr_any;
757 ip->sa.sa_family = AF_INET6;
758 #endif
762 if (tb[NDA_PORT]) {
763 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
764 return -EINVAL;
765 *port = nla_get_be16(tb[NDA_PORT]);
766 } else {
767 *port = vxlan->cfg.dst_port;
770 if (tb[NDA_VNI]) {
771 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
772 return -EINVAL;
773 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
774 } else {
775 *vni = vxlan->default_dst.remote_vni;
778 if (tb[NDA_IFINDEX]) {
779 struct net_device *tdev;
781 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
782 return -EINVAL;
783 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
784 tdev = __dev_get_by_index(net, *ifindex);
785 if (!tdev)
786 return -EADDRNOTAVAIL;
787 } else {
788 *ifindex = 0;
791 return 0;
794 /* Add static entry (via netlink) */
795 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
796 struct net_device *dev,
797 const unsigned char *addr, u16 vid, u16 flags)
799 struct vxlan_dev *vxlan = netdev_priv(dev);
800 /* struct net *net = dev_net(vxlan->dev); */
801 union vxlan_addr ip;
802 __be16 port;
803 __be32 vni;
804 u32 ifindex;
805 int err;
807 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
808 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
809 ndm->ndm_state);
810 return -EINVAL;
813 if (tb[NDA_DST] == NULL)
814 return -EINVAL;
816 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
817 if (err)
818 return err;
820 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
821 return -EAFNOSUPPORT;
823 spin_lock_bh(&vxlan->hash_lock);
824 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
825 port, vni, ifindex, ndm->ndm_flags);
826 spin_unlock_bh(&vxlan->hash_lock);
828 return err;
831 /* Delete entry (via netlink) */
832 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
833 struct net_device *dev,
834 const unsigned char *addr, u16 vid)
836 struct vxlan_dev *vxlan = netdev_priv(dev);
837 struct vxlan_fdb *f;
838 struct vxlan_rdst *rd = NULL;
839 union vxlan_addr ip;
840 __be16 port;
841 __be32 vni;
842 u32 ifindex;
843 int err;
845 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
846 if (err)
847 return err;
849 err = -ENOENT;
851 spin_lock_bh(&vxlan->hash_lock);
852 f = vxlan_find_mac(vxlan, addr);
853 if (!f)
854 goto out;
856 if (!vxlan_addr_any(&ip)) {
857 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
858 if (!rd)
859 goto out;
862 err = 0;
864 /* remove a destination if it's not the only one on the list,
865 * otherwise destroy the fdb entry
867 if (rd && !list_is_singular(&f->remotes)) {
868 vxlan_fdb_dst_destroy(vxlan, f, rd);
869 goto out;
872 vxlan_fdb_destroy(vxlan, f);
874 out:
875 spin_unlock_bh(&vxlan->hash_lock);
877 return err;
880 /* Dump forwarding table */
881 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
882 struct net_device *dev,
883 struct net_device *filter_dev, int *idx)
885 struct vxlan_dev *vxlan = netdev_priv(dev);
886 unsigned int h;
887 int err = 0;
889 for (h = 0; h < FDB_HASH_SIZE; ++h) {
890 struct vxlan_fdb *f;
892 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
893 struct vxlan_rdst *rd;
895 list_for_each_entry_rcu(rd, &f->remotes, list) {
896 if (*idx < cb->args[2])
897 goto skip;
899 err = vxlan_fdb_info(skb, vxlan, f,
900 NETLINK_CB(cb->skb).portid,
901 cb->nlh->nlmsg_seq,
902 RTM_NEWNEIGH,
903 NLM_F_MULTI, rd);
904 if (err < 0)
905 goto out;
906 skip:
907 *idx += 1;
911 out:
912 return err;
915 /* Watch incoming packets to learn mapping between Ethernet address
916 * and Tunnel endpoint.
917 * Return true if packet is bogus and should be dropped.
919 static bool vxlan_snoop(struct net_device *dev,
920 union vxlan_addr *src_ip, const u8 *src_mac)
922 struct vxlan_dev *vxlan = netdev_priv(dev);
923 struct vxlan_fdb *f;
925 f = vxlan_find_mac(vxlan, src_mac);
926 if (likely(f)) {
927 struct vxlan_rdst *rdst = first_remote_rcu(f);
929 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
930 return false;
932 /* Don't migrate static entries, drop packets */
933 if (f->state & (NUD_PERMANENT | NUD_NOARP))
934 return true;
936 if (net_ratelimit())
937 netdev_info(dev,
938 "%pM migrated from %pIS to %pIS\n",
939 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
941 rdst->remote_ip = *src_ip;
942 f->updated = jiffies;
943 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
944 } else {
945 /* learned new entry */
946 spin_lock(&vxlan->hash_lock);
948 /* close off race between vxlan_flush and incoming packets */
949 if (netif_running(dev))
950 vxlan_fdb_create(vxlan, src_mac, src_ip,
951 NUD_REACHABLE,
952 NLM_F_EXCL|NLM_F_CREATE,
953 vxlan->cfg.dst_port,
954 vxlan->default_dst.remote_vni,
955 0, NTF_SELF);
956 spin_unlock(&vxlan->hash_lock);
959 return false;
962 /* See if multicast group is already in use by other ID */
963 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
965 struct vxlan_dev *vxlan;
966 struct vxlan_sock *sock4;
967 #if IS_ENABLED(CONFIG_IPV6)
968 struct vxlan_sock *sock6;
969 #endif
970 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
972 sock4 = rtnl_dereference(dev->vn4_sock);
974 /* The vxlan_sock is only used by dev, leaving group has
975 * no effect on other vxlan devices.
977 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
978 return false;
979 #if IS_ENABLED(CONFIG_IPV6)
980 sock6 = rtnl_dereference(dev->vn6_sock);
981 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
982 return false;
983 #endif
985 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
986 if (!netif_running(vxlan->dev) || vxlan == dev)
987 continue;
989 if (family == AF_INET &&
990 rtnl_dereference(vxlan->vn4_sock) != sock4)
991 continue;
992 #if IS_ENABLED(CONFIG_IPV6)
993 if (family == AF_INET6 &&
994 rtnl_dereference(vxlan->vn6_sock) != sock6)
995 continue;
996 #endif
998 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
999 &dev->default_dst.remote_ip))
1000 continue;
1002 if (vxlan->default_dst.remote_ifindex !=
1003 dev->default_dst.remote_ifindex)
1004 continue;
1006 return true;
1009 return false;
1012 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1014 struct vxlan_net *vn;
1016 if (!vs)
1017 return false;
1018 if (!atomic_dec_and_test(&vs->refcnt))
1019 return false;
1021 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1022 spin_lock(&vn->sock_lock);
1023 hlist_del_rcu(&vs->hlist);
1024 udp_tunnel_notify_del_rx_port(vs->sock,
1025 (vs->flags & VXLAN_F_GPE) ?
1026 UDP_TUNNEL_TYPE_VXLAN_GPE :
1027 UDP_TUNNEL_TYPE_VXLAN);
1028 spin_unlock(&vn->sock_lock);
1030 return true;
1033 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1035 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1036 #if IS_ENABLED(CONFIG_IPV6)
1037 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1039 rcu_assign_pointer(vxlan->vn6_sock, NULL);
1040 #endif
1042 rcu_assign_pointer(vxlan->vn4_sock, NULL);
1043 synchronize_net();
1045 vxlan_vs_del_dev(vxlan);
1047 if (__vxlan_sock_release_prep(sock4)) {
1048 udp_tunnel_sock_release(sock4->sock);
1049 kfree(sock4);
1052 #if IS_ENABLED(CONFIG_IPV6)
1053 if (__vxlan_sock_release_prep(sock6)) {
1054 udp_tunnel_sock_release(sock6->sock);
1055 kfree(sock6);
1057 #endif
1060 /* Update multicast group membership when first VNI on
1061 * multicast address is brought up
1063 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1065 struct sock *sk;
1066 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1067 int ifindex = vxlan->default_dst.remote_ifindex;
1068 int ret = -EINVAL;
1070 if (ip->sa.sa_family == AF_INET) {
1071 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1072 struct ip_mreqn mreq = {
1073 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1074 .imr_ifindex = ifindex,
1077 sk = sock4->sock->sk;
1078 lock_sock(sk);
1079 ret = ip_mc_join_group(sk, &mreq);
1080 release_sock(sk);
1081 #if IS_ENABLED(CONFIG_IPV6)
1082 } else {
1083 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1085 sk = sock6->sock->sk;
1086 lock_sock(sk);
1087 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1088 &ip->sin6.sin6_addr);
1089 release_sock(sk);
1090 #endif
1093 return ret;
1096 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1097 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1099 struct sock *sk;
1100 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1101 int ifindex = vxlan->default_dst.remote_ifindex;
1102 int ret = -EINVAL;
1104 if (ip->sa.sa_family == AF_INET) {
1105 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1106 struct ip_mreqn mreq = {
1107 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1108 .imr_ifindex = ifindex,
1111 sk = sock4->sock->sk;
1112 lock_sock(sk);
1113 ret = ip_mc_leave_group(sk, &mreq);
1114 release_sock(sk);
1115 #if IS_ENABLED(CONFIG_IPV6)
1116 } else {
1117 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1119 sk = sock6->sock->sk;
1120 lock_sock(sk);
1121 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1122 &ip->sin6.sin6_addr);
1123 release_sock(sk);
1124 #endif
1127 return ret;
1130 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1131 struct sk_buff *skb, u32 vxflags)
1133 size_t start, offset;
1135 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1136 goto out;
1138 start = vxlan_rco_start(unparsed->vx_vni);
1139 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1141 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1142 return false;
1144 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1145 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1146 out:
1147 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1148 unparsed->vx_vni &= VXLAN_VNI_MASK;
1149 return true;
1152 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1153 struct sk_buff *skb, u32 vxflags,
1154 struct vxlan_metadata *md)
1156 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1157 struct metadata_dst *tun_dst;
1159 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1160 goto out;
1162 md->gbp = ntohs(gbp->policy_id);
1164 tun_dst = (struct metadata_dst *)skb_dst(skb);
1165 if (tun_dst) {
1166 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1167 tun_dst->u.tun_info.options_len = sizeof(*md);
1169 if (gbp->dont_learn)
1170 md->gbp |= VXLAN_GBP_DONT_LEARN;
1172 if (gbp->policy_applied)
1173 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1175 /* In flow-based mode, GBP is carried in dst_metadata */
1176 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1177 skb->mark = md->gbp;
1178 out:
1179 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1182 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1183 __be16 *protocol,
1184 struct sk_buff *skb, u32 vxflags)
1186 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1188 /* Need to have Next Protocol set for interfaces in GPE mode. */
1189 if (!gpe->np_applied)
1190 return false;
1191 /* "The initial version is 0. If a receiver does not support the
1192 * version indicated it MUST drop the packet.
1194 if (gpe->version != 0)
1195 return false;
1196 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1197 * processing MUST occur." However, we don't implement OAM
1198 * processing, thus drop the packet.
1200 if (gpe->oam_flag)
1201 return false;
1203 switch (gpe->next_protocol) {
1204 case VXLAN_GPE_NP_IPV4:
1205 *protocol = htons(ETH_P_IP);
1206 break;
1207 case VXLAN_GPE_NP_IPV6:
1208 *protocol = htons(ETH_P_IPV6);
1209 break;
1210 case VXLAN_GPE_NP_ETHERNET:
1211 *protocol = htons(ETH_P_TEB);
1212 break;
1213 default:
1214 return false;
1217 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1218 return true;
1221 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1222 struct vxlan_sock *vs,
1223 struct sk_buff *skb)
1225 union vxlan_addr saddr;
1227 skb_reset_mac_header(skb);
1228 skb->protocol = eth_type_trans(skb, vxlan->dev);
1229 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1231 /* Ignore packet loops (and multicast echo) */
1232 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1233 return false;
1235 /* Get address from the outer IP header */
1236 if (vxlan_get_sk_family(vs) == AF_INET) {
1237 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1238 saddr.sa.sa_family = AF_INET;
1239 #if IS_ENABLED(CONFIG_IPV6)
1240 } else {
1241 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1242 saddr.sa.sa_family = AF_INET6;
1243 #endif
1246 if ((vxlan->flags & VXLAN_F_LEARN) &&
1247 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1248 return false;
1250 return true;
1253 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1254 struct sk_buff *skb)
1256 int err = 0;
1258 if (vxlan_get_sk_family(vs) == AF_INET)
1259 err = IP_ECN_decapsulate(oiph, skb);
1260 #if IS_ENABLED(CONFIG_IPV6)
1261 else
1262 err = IP6_ECN_decapsulate(oiph, skb);
1263 #endif
1265 if (unlikely(err) && log_ecn_error) {
1266 if (vxlan_get_sk_family(vs) == AF_INET)
1267 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1268 &((struct iphdr *)oiph)->saddr,
1269 ((struct iphdr *)oiph)->tos);
1270 else
1271 net_info_ratelimited("non-ECT from %pI6\n",
1272 &((struct ipv6hdr *)oiph)->saddr);
1274 return err <= 1;
1277 /* Callback from net/ipv4/udp.c to receive packets */
1278 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1280 struct pcpu_sw_netstats *stats;
1281 struct vxlan_dev *vxlan;
1282 struct vxlan_sock *vs;
1283 struct vxlanhdr unparsed;
1284 struct vxlan_metadata _md;
1285 struct vxlan_metadata *md = &_md;
1286 __be16 protocol = htons(ETH_P_TEB);
1287 bool raw_proto = false;
1288 void *oiph;
1290 /* Need UDP and VXLAN header to be present */
1291 if (!pskb_may_pull(skb, VXLAN_HLEN))
1292 goto drop;
1294 unparsed = *vxlan_hdr(skb);
1295 /* VNI flag always required to be set */
1296 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1297 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1298 ntohl(vxlan_hdr(skb)->vx_flags),
1299 ntohl(vxlan_hdr(skb)->vx_vni));
1300 /* Return non vxlan pkt */
1301 goto drop;
1303 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1304 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1306 vs = rcu_dereference_sk_user_data(sk);
1307 if (!vs)
1308 goto drop;
1310 vxlan = vxlan_vs_find_vni(vs, vxlan_vni(vxlan_hdr(skb)->vx_vni));
1311 if (!vxlan)
1312 goto drop;
1314 /* For backwards compatibility, only allow reserved fields to be
1315 * used by VXLAN extensions if explicitly requested.
1317 if (vs->flags & VXLAN_F_GPE) {
1318 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1319 goto drop;
1320 raw_proto = true;
1323 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1324 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1325 goto drop;
1327 if (vxlan_collect_metadata(vs)) {
1328 __be32 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1329 struct metadata_dst *tun_dst;
1331 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1332 key32_to_tunnel_id(vni), sizeof(*md));
1334 if (!tun_dst)
1335 goto drop;
1337 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1339 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1340 } else {
1341 memset(md, 0, sizeof(*md));
1344 if (vs->flags & VXLAN_F_REMCSUM_RX)
1345 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1346 goto drop;
1347 if (vs->flags & VXLAN_F_GBP)
1348 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1349 /* Note that GBP and GPE can never be active together. This is
1350 * ensured in vxlan_dev_configure.
1353 if (unparsed.vx_flags || unparsed.vx_vni) {
1354 /* If there are any unprocessed flags remaining treat
1355 * this as a malformed packet. This behavior diverges from
1356 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1357 * in reserved fields are to be ignored. The approach here
1358 * maintains compatibility with previous stack code, and also
1359 * is more robust and provides a little more security in
1360 * adding extensions to VXLAN.
1362 goto drop;
1365 if (!raw_proto) {
1366 if (!vxlan_set_mac(vxlan, vs, skb))
1367 goto drop;
1368 } else {
1369 skb_reset_mac_header(skb);
1370 skb->dev = vxlan->dev;
1371 skb->pkt_type = PACKET_HOST;
1374 oiph = skb_network_header(skb);
1375 skb_reset_network_header(skb);
1377 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1378 ++vxlan->dev->stats.rx_frame_errors;
1379 ++vxlan->dev->stats.rx_errors;
1380 goto drop;
1383 rcu_read_lock();
1385 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1386 rcu_read_unlock();
1387 atomic_long_inc(&vxlan->dev->rx_dropped);
1388 goto drop;
1391 stats = this_cpu_ptr(vxlan->dev->tstats);
1392 u64_stats_update_begin(&stats->syncp);
1393 stats->rx_packets++;
1394 stats->rx_bytes += skb->len;
1395 u64_stats_update_end(&stats->syncp);
1397 gro_cells_receive(&vxlan->gro_cells, skb);
1399 rcu_read_unlock();
1401 return 0;
1403 drop:
1404 /* Consume bad packet */
1405 kfree_skb(skb);
1406 return 0;
1409 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1411 struct vxlan_dev *vxlan = netdev_priv(dev);
1412 struct arphdr *parp;
1413 u8 *arpptr, *sha;
1414 __be32 sip, tip;
1415 struct neighbour *n;
1417 if (dev->flags & IFF_NOARP)
1418 goto out;
1420 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1421 dev->stats.tx_dropped++;
1422 goto out;
1424 parp = arp_hdr(skb);
1426 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1427 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1428 parp->ar_pro != htons(ETH_P_IP) ||
1429 parp->ar_op != htons(ARPOP_REQUEST) ||
1430 parp->ar_hln != dev->addr_len ||
1431 parp->ar_pln != 4)
1432 goto out;
1433 arpptr = (u8 *)parp + sizeof(struct arphdr);
1434 sha = arpptr;
1435 arpptr += dev->addr_len; /* sha */
1436 memcpy(&sip, arpptr, sizeof(sip));
1437 arpptr += sizeof(sip);
1438 arpptr += dev->addr_len; /* tha */
1439 memcpy(&tip, arpptr, sizeof(tip));
1441 if (ipv4_is_loopback(tip) ||
1442 ipv4_is_multicast(tip))
1443 goto out;
1445 n = neigh_lookup(&arp_tbl, &tip, dev);
1447 if (n) {
1448 struct vxlan_fdb *f;
1449 struct sk_buff *reply;
1451 if (!(n->nud_state & NUD_CONNECTED)) {
1452 neigh_release(n);
1453 goto out;
1456 f = vxlan_find_mac(vxlan, n->ha);
1457 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1458 /* bridge-local neighbor */
1459 neigh_release(n);
1460 goto out;
1463 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1464 n->ha, sha);
1466 neigh_release(n);
1468 if (reply == NULL)
1469 goto out;
1471 skb_reset_mac_header(reply);
1472 __skb_pull(reply, skb_network_offset(reply));
1473 reply->ip_summed = CHECKSUM_UNNECESSARY;
1474 reply->pkt_type = PACKET_HOST;
1476 if (netif_rx_ni(reply) == NET_RX_DROP)
1477 dev->stats.rx_dropped++;
1478 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1479 union vxlan_addr ipa = {
1480 .sin.sin_addr.s_addr = tip,
1481 .sin.sin_family = AF_INET,
1484 vxlan_ip_miss(dev, &ipa);
1486 out:
1487 consume_skb(skb);
1488 return NETDEV_TX_OK;
1491 #if IS_ENABLED(CONFIG_IPV6)
1492 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1493 struct neighbour *n, bool isrouter)
1495 struct net_device *dev = request->dev;
1496 struct sk_buff *reply;
1497 struct nd_msg *ns, *na;
1498 struct ipv6hdr *pip6;
1499 u8 *daddr;
1500 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1501 int ns_olen;
1502 int i, len;
1504 if (dev == NULL)
1505 return NULL;
1507 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1508 sizeof(*na) + na_olen + dev->needed_tailroom;
1509 reply = alloc_skb(len, GFP_ATOMIC);
1510 if (reply == NULL)
1511 return NULL;
1513 reply->protocol = htons(ETH_P_IPV6);
1514 reply->dev = dev;
1515 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1516 skb_push(reply, sizeof(struct ethhdr));
1517 skb_reset_mac_header(reply);
1519 ns = (struct nd_msg *)skb_transport_header(request);
1521 daddr = eth_hdr(request)->h_source;
1522 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1523 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1524 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1525 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1526 break;
1530 /* Ethernet header */
1531 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1532 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1533 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1534 reply->protocol = htons(ETH_P_IPV6);
1536 skb_pull(reply, sizeof(struct ethhdr));
1537 skb_reset_network_header(reply);
1538 skb_put(reply, sizeof(struct ipv6hdr));
1540 /* IPv6 header */
1542 pip6 = ipv6_hdr(reply);
1543 memset(pip6, 0, sizeof(struct ipv6hdr));
1544 pip6->version = 6;
1545 pip6->priority = ipv6_hdr(request)->priority;
1546 pip6->nexthdr = IPPROTO_ICMPV6;
1547 pip6->hop_limit = 255;
1548 pip6->daddr = ipv6_hdr(request)->saddr;
1549 pip6->saddr = *(struct in6_addr *)n->primary_key;
1551 skb_pull(reply, sizeof(struct ipv6hdr));
1552 skb_reset_transport_header(reply);
1554 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1556 /* Neighbor Advertisement */
1557 memset(na, 0, sizeof(*na)+na_olen);
1558 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1559 na->icmph.icmp6_router = isrouter;
1560 na->icmph.icmp6_override = 1;
1561 na->icmph.icmp6_solicited = 1;
1562 na->target = ns->target;
1563 ether_addr_copy(&na->opt[2], n->ha);
1564 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1565 na->opt[1] = na_olen >> 3;
1567 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1568 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1569 csum_partial(na, sizeof(*na)+na_olen, 0));
1571 pip6->payload_len = htons(sizeof(*na)+na_olen);
1573 skb_push(reply, sizeof(struct ipv6hdr));
1575 reply->ip_summed = CHECKSUM_UNNECESSARY;
1577 return reply;
1580 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1582 struct vxlan_dev *vxlan = netdev_priv(dev);
1583 struct nd_msg *msg;
1584 const struct ipv6hdr *iphdr;
1585 const struct in6_addr *saddr, *daddr;
1586 struct neighbour *n;
1587 struct inet6_dev *in6_dev;
1589 in6_dev = __in6_dev_get(dev);
1590 if (!in6_dev)
1591 goto out;
1593 iphdr = ipv6_hdr(skb);
1594 saddr = &iphdr->saddr;
1595 daddr = &iphdr->daddr;
1597 msg = (struct nd_msg *)skb_transport_header(skb);
1598 if (msg->icmph.icmp6_code != 0 ||
1599 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1600 goto out;
1602 if (ipv6_addr_loopback(daddr) ||
1603 ipv6_addr_is_multicast(&msg->target))
1604 goto out;
1606 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1608 if (n) {
1609 struct vxlan_fdb *f;
1610 struct sk_buff *reply;
1612 if (!(n->nud_state & NUD_CONNECTED)) {
1613 neigh_release(n);
1614 goto out;
1617 f = vxlan_find_mac(vxlan, n->ha);
1618 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1619 /* bridge-local neighbor */
1620 neigh_release(n);
1621 goto out;
1624 reply = vxlan_na_create(skb, n,
1625 !!(f ? f->flags & NTF_ROUTER : 0));
1627 neigh_release(n);
1629 if (reply == NULL)
1630 goto out;
1632 if (netif_rx_ni(reply) == NET_RX_DROP)
1633 dev->stats.rx_dropped++;
1635 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1636 union vxlan_addr ipa = {
1637 .sin6.sin6_addr = msg->target,
1638 .sin6.sin6_family = AF_INET6,
1641 vxlan_ip_miss(dev, &ipa);
1644 out:
1645 consume_skb(skb);
1646 return NETDEV_TX_OK;
1648 #endif
1650 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1652 struct vxlan_dev *vxlan = netdev_priv(dev);
1653 struct neighbour *n;
1655 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1656 return false;
1658 n = NULL;
1659 switch (ntohs(eth_hdr(skb)->h_proto)) {
1660 case ETH_P_IP:
1662 struct iphdr *pip;
1664 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1665 return false;
1666 pip = ip_hdr(skb);
1667 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1668 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1669 union vxlan_addr ipa = {
1670 .sin.sin_addr.s_addr = pip->daddr,
1671 .sin.sin_family = AF_INET,
1674 vxlan_ip_miss(dev, &ipa);
1675 return false;
1678 break;
1680 #if IS_ENABLED(CONFIG_IPV6)
1681 case ETH_P_IPV6:
1683 struct ipv6hdr *pip6;
1685 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1686 return false;
1687 pip6 = ipv6_hdr(skb);
1688 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1689 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1690 union vxlan_addr ipa = {
1691 .sin6.sin6_addr = pip6->daddr,
1692 .sin6.sin6_family = AF_INET6,
1695 vxlan_ip_miss(dev, &ipa);
1696 return false;
1699 break;
1701 #endif
1702 default:
1703 return false;
1706 if (n) {
1707 bool diff;
1709 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1710 if (diff) {
1711 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1712 dev->addr_len);
1713 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1715 neigh_release(n);
1716 return diff;
1719 return false;
1722 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1723 struct vxlan_metadata *md)
1725 struct vxlanhdr_gbp *gbp;
1727 if (!md->gbp)
1728 return;
1730 gbp = (struct vxlanhdr_gbp *)vxh;
1731 vxh->vx_flags |= VXLAN_HF_GBP;
1733 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1734 gbp->dont_learn = 1;
1736 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1737 gbp->policy_applied = 1;
1739 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1742 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1743 __be16 protocol)
1745 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1747 gpe->np_applied = 1;
1749 switch (protocol) {
1750 case htons(ETH_P_IP):
1751 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1752 return 0;
1753 case htons(ETH_P_IPV6):
1754 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1755 return 0;
1756 case htons(ETH_P_TEB):
1757 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1758 return 0;
1760 return -EPFNOSUPPORT;
1763 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1764 int iphdr_len, __be32 vni,
1765 struct vxlan_metadata *md, u32 vxflags,
1766 bool udp_sum)
1768 struct vxlanhdr *vxh;
1769 int min_headroom;
1770 int err;
1771 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1772 __be16 inner_protocol = htons(ETH_P_TEB);
1774 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1775 skb->ip_summed == CHECKSUM_PARTIAL) {
1776 int csum_start = skb_checksum_start_offset(skb);
1778 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1779 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1780 (skb->csum_offset == offsetof(struct udphdr, check) ||
1781 skb->csum_offset == offsetof(struct tcphdr, check)))
1782 type |= SKB_GSO_TUNNEL_REMCSUM;
1785 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1786 + VXLAN_HLEN + iphdr_len
1787 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1789 /* Need space for new headers (invalidates iph ptr) */
1790 err = skb_cow_head(skb, min_headroom);
1791 if (unlikely(err))
1792 goto out_free;
1794 skb = vlan_hwaccel_push_inside(skb);
1795 if (WARN_ON(!skb))
1796 return -ENOMEM;
1798 err = iptunnel_handle_offloads(skb, type);
1799 if (err)
1800 goto out_free;
1802 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1803 vxh->vx_flags = VXLAN_HF_VNI;
1804 vxh->vx_vni = vxlan_vni_field(vni);
1806 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1807 unsigned int start;
1809 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1810 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1811 vxh->vx_flags |= VXLAN_HF_RCO;
1813 if (!skb_is_gso(skb)) {
1814 skb->ip_summed = CHECKSUM_NONE;
1815 skb->encapsulation = 0;
1819 if (vxflags & VXLAN_F_GBP)
1820 vxlan_build_gbp_hdr(vxh, vxflags, md);
1821 if (vxflags & VXLAN_F_GPE) {
1822 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1823 if (err < 0)
1824 goto out_free;
1825 inner_protocol = skb->protocol;
1828 skb_set_inner_protocol(skb, inner_protocol);
1829 return 0;
1831 out_free:
1832 kfree_skb(skb);
1833 return err;
1836 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan,
1837 struct sk_buff *skb, int oif, u8 tos,
1838 __be32 daddr, __be32 *saddr,
1839 struct dst_cache *dst_cache,
1840 const struct ip_tunnel_info *info)
1842 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1843 struct rtable *rt = NULL;
1844 struct flowi4 fl4;
1846 if (tos && !info)
1847 use_cache = false;
1848 if (use_cache) {
1849 rt = dst_cache_get_ip4(dst_cache, saddr);
1850 if (rt)
1851 return rt;
1854 memset(&fl4, 0, sizeof(fl4));
1855 fl4.flowi4_oif = oif;
1856 fl4.flowi4_tos = RT_TOS(tos);
1857 fl4.flowi4_mark = skb->mark;
1858 fl4.flowi4_proto = IPPROTO_UDP;
1859 fl4.daddr = daddr;
1860 fl4.saddr = *saddr;
1862 rt = ip_route_output_key(vxlan->net, &fl4);
1863 if (!IS_ERR(rt)) {
1864 *saddr = fl4.saddr;
1865 if (use_cache)
1866 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1868 return rt;
1871 #if IS_ENABLED(CONFIG_IPV6)
1872 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
1873 struct sk_buff *skb, int oif, u8 tos,
1874 __be32 label,
1875 const struct in6_addr *daddr,
1876 struct in6_addr *saddr,
1877 struct dst_cache *dst_cache,
1878 const struct ip_tunnel_info *info)
1880 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
1881 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1882 struct dst_entry *ndst;
1883 struct flowi6 fl6;
1884 int err;
1886 if (!sock6)
1887 return ERR_PTR(-EIO);
1889 if (tos && !info)
1890 use_cache = false;
1891 if (use_cache) {
1892 ndst = dst_cache_get_ip6(dst_cache, saddr);
1893 if (ndst)
1894 return ndst;
1897 memset(&fl6, 0, sizeof(fl6));
1898 fl6.flowi6_oif = oif;
1899 fl6.daddr = *daddr;
1900 fl6.saddr = *saddr;
1901 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
1902 fl6.flowi6_mark = skb->mark;
1903 fl6.flowi6_proto = IPPROTO_UDP;
1905 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1906 sock6->sock->sk,
1907 &ndst, &fl6);
1908 if (err < 0)
1909 return ERR_PTR(err);
1911 *saddr = fl6.saddr;
1912 if (use_cache)
1913 dst_cache_set_ip6(dst_cache, ndst, saddr);
1914 return ndst;
1916 #endif
1918 /* Bypass encapsulation if the destination is local */
1919 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1920 struct vxlan_dev *dst_vxlan)
1922 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1923 union vxlan_addr loopback;
1924 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1925 struct net_device *dev;
1926 int len = skb->len;
1928 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1929 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1930 skb->pkt_type = PACKET_HOST;
1931 skb->encapsulation = 0;
1932 skb->dev = dst_vxlan->dev;
1933 __skb_pull(skb, skb_network_offset(skb));
1935 if (remote_ip->sa.sa_family == AF_INET) {
1936 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1937 loopback.sa.sa_family = AF_INET;
1938 #if IS_ENABLED(CONFIG_IPV6)
1939 } else {
1940 loopback.sin6.sin6_addr = in6addr_loopback;
1941 loopback.sa.sa_family = AF_INET6;
1942 #endif
1945 rcu_read_lock();
1946 dev = skb->dev;
1947 if (unlikely(!(dev->flags & IFF_UP))) {
1948 kfree_skb(skb);
1949 goto drop;
1952 if (dst_vxlan->flags & VXLAN_F_LEARN)
1953 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source);
1955 u64_stats_update_begin(&tx_stats->syncp);
1956 tx_stats->tx_packets++;
1957 tx_stats->tx_bytes += len;
1958 u64_stats_update_end(&tx_stats->syncp);
1960 if (netif_rx(skb) == NET_RX_SUCCESS) {
1961 u64_stats_update_begin(&rx_stats->syncp);
1962 rx_stats->rx_packets++;
1963 rx_stats->rx_bytes += len;
1964 u64_stats_update_end(&rx_stats->syncp);
1965 } else {
1966 drop:
1967 dev->stats.rx_dropped++;
1969 rcu_read_unlock();
1972 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1973 struct vxlan_rdst *rdst, bool did_rsc)
1975 struct dst_cache *dst_cache;
1976 struct ip_tunnel_info *info;
1977 struct vxlan_dev *vxlan = netdev_priv(dev);
1978 struct sock *sk;
1979 struct rtable *rt = NULL;
1980 const struct iphdr *old_iph;
1981 union vxlan_addr *dst;
1982 union vxlan_addr remote_ip, local_ip;
1983 struct vxlan_metadata _md;
1984 struct vxlan_metadata *md = &_md;
1985 __be16 src_port = 0, dst_port;
1986 __be32 vni, label;
1987 __be16 df = 0;
1988 __u8 tos, ttl;
1989 int err;
1990 u32 flags = vxlan->flags;
1991 bool udp_sum = false;
1992 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
1994 info = skb_tunnel_info(skb);
1996 rcu_read_lock();
1997 if (rdst) {
1998 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1999 vni = rdst->remote_vni;
2000 dst = &rdst->remote_ip;
2001 local_ip = vxlan->cfg.saddr;
2002 dst_cache = &rdst->dst_cache;
2003 } else {
2004 if (!info) {
2005 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2006 dev->name);
2007 goto drop;
2009 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2010 vni = tunnel_id_to_key32(info->key.tun_id);
2011 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2012 if (remote_ip.sa.sa_family == AF_INET) {
2013 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2014 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2015 } else {
2016 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2017 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2019 dst = &remote_ip;
2020 dst_cache = &info->dst_cache;
2023 if (vxlan_addr_any(dst)) {
2024 if (did_rsc) {
2025 /* short-circuited back to local bridge */
2026 vxlan_encap_bypass(skb, vxlan, vxlan);
2027 goto out_unlock;
2029 goto drop;
2032 old_iph = ip_hdr(skb);
2034 ttl = vxlan->cfg.ttl;
2035 if (!ttl && vxlan_addr_multicast(dst))
2036 ttl = 1;
2038 tos = vxlan->cfg.tos;
2039 if (tos == 1)
2040 tos = ip_tunnel_get_dsfield(old_iph, skb);
2042 label = vxlan->cfg.label;
2043 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2044 vxlan->cfg.port_max, true);
2046 if (info) {
2047 ttl = info->key.ttl;
2048 tos = info->key.tos;
2049 label = info->key.label;
2050 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2052 if (info->options_len)
2053 md = ip_tunnel_info_opts(info);
2054 } else {
2055 md->gbp = skb->mark;
2058 if (dst->sa.sa_family == AF_INET) {
2059 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2061 if (!sock4)
2062 goto drop;
2063 sk = sock4->sock->sk;
2065 rt = vxlan_get_route(vxlan, skb,
2066 rdst ? rdst->remote_ifindex : 0, tos,
2067 dst->sin.sin_addr.s_addr,
2068 &local_ip.sin.sin_addr.s_addr,
2069 dst_cache, info);
2070 if (IS_ERR(rt)) {
2071 netdev_dbg(dev, "no route to %pI4\n",
2072 &dst->sin.sin_addr.s_addr);
2073 dev->stats.tx_carrier_errors++;
2074 goto tx_error;
2077 if (rt->dst.dev == dev) {
2078 netdev_dbg(dev, "circular route to %pI4\n",
2079 &dst->sin.sin_addr.s_addr);
2080 dev->stats.collisions++;
2081 goto rt_tx_error;
2084 /* Bypass encapsulation if the destination is local */
2085 if (!info && rt->rt_flags & RTCF_LOCAL &&
2086 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2087 struct vxlan_dev *dst_vxlan;
2089 ip_rt_put(rt);
2090 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2091 dst->sa.sa_family, dst_port,
2092 vxlan->flags);
2093 if (!dst_vxlan)
2094 goto tx_error;
2095 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2096 goto out_unlock;
2099 if (!info)
2100 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2101 else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
2102 df = htons(IP_DF);
2104 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2105 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2106 err = vxlan_build_skb(skb, &rt->dst, sizeof(struct iphdr),
2107 vni, md, flags, udp_sum);
2108 if (err < 0)
2109 goto xmit_tx_error;
2111 udp_tunnel_xmit_skb(rt, sk, skb, local_ip.sin.sin_addr.s_addr,
2112 dst->sin.sin_addr.s_addr, tos, ttl, df,
2113 src_port, dst_port, xnet, !udp_sum);
2114 #if IS_ENABLED(CONFIG_IPV6)
2115 } else {
2116 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2117 struct dst_entry *ndst;
2118 u32 rt6i_flags;
2120 if (!sock6)
2121 goto drop;
2122 sk = sock6->sock->sk;
2124 ndst = vxlan6_get_route(vxlan, skb,
2125 rdst ? rdst->remote_ifindex : 0, tos,
2126 label, &dst->sin6.sin6_addr,
2127 &local_ip.sin6.sin6_addr,
2128 dst_cache, info);
2129 if (IS_ERR(ndst)) {
2130 netdev_dbg(dev, "no route to %pI6\n",
2131 &dst->sin6.sin6_addr);
2132 dev->stats.tx_carrier_errors++;
2133 goto tx_error;
2136 if (ndst->dev == dev) {
2137 netdev_dbg(dev, "circular route to %pI6\n",
2138 &dst->sin6.sin6_addr);
2139 dst_release(ndst);
2140 dev->stats.collisions++;
2141 goto tx_error;
2144 /* Bypass encapsulation if the destination is local */
2145 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2146 if (!info && rt6i_flags & RTF_LOCAL &&
2147 !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2148 struct vxlan_dev *dst_vxlan;
2150 dst_release(ndst);
2151 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2152 dst->sa.sa_family, dst_port,
2153 vxlan->flags);
2154 if (!dst_vxlan)
2155 goto tx_error;
2156 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2157 goto out_unlock;
2160 if (!info)
2161 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2163 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2164 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2165 skb_scrub_packet(skb, xnet);
2166 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2167 vni, md, flags, udp_sum);
2168 if (err < 0) {
2169 dst_release(ndst);
2170 dev->stats.tx_errors++;
2171 goto out_unlock;
2173 udp_tunnel6_xmit_skb(ndst, sk, skb, dev,
2174 &local_ip.sin6.sin6_addr,
2175 &dst->sin6.sin6_addr, tos, ttl,
2176 label, src_port, dst_port, !udp_sum);
2177 #endif
2179 out_unlock:
2180 rcu_read_unlock();
2181 return;
2183 drop:
2184 dev->stats.tx_dropped++;
2185 goto tx_free;
2187 xmit_tx_error:
2188 /* skb is already freed. */
2189 skb = NULL;
2190 rt_tx_error:
2191 ip_rt_put(rt);
2192 tx_error:
2193 dev->stats.tx_errors++;
2194 tx_free:
2195 dev_kfree_skb(skb);
2196 rcu_read_unlock();
2199 /* Transmit local packets over Vxlan
2201 * Outer IP header inherits ECN and DF from inner header.
2202 * Outer UDP destination is the VXLAN assigned port.
2203 * source port is based on hash of flow
2205 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2207 struct vxlan_dev *vxlan = netdev_priv(dev);
2208 const struct ip_tunnel_info *info;
2209 struct ethhdr *eth;
2210 bool did_rsc = false;
2211 struct vxlan_rdst *rdst, *fdst = NULL;
2212 struct vxlan_fdb *f;
2214 info = skb_tunnel_info(skb);
2216 skb_reset_mac_header(skb);
2218 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2219 if (info && info->mode & IP_TUNNEL_INFO_TX)
2220 vxlan_xmit_one(skb, dev, NULL, false);
2221 else
2222 kfree_skb(skb);
2223 return NETDEV_TX_OK;
2226 if (vxlan->flags & VXLAN_F_PROXY) {
2227 eth = eth_hdr(skb);
2228 if (ntohs(eth->h_proto) == ETH_P_ARP)
2229 return arp_reduce(dev, skb);
2230 #if IS_ENABLED(CONFIG_IPV6)
2231 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2232 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2233 + sizeof(struct nd_msg)) &&
2234 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2235 struct nd_msg *msg;
2237 msg = (struct nd_msg *)skb_transport_header(skb);
2238 if (msg->icmph.icmp6_code == 0 &&
2239 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2240 return neigh_reduce(dev, skb);
2242 #endif
2245 eth = eth_hdr(skb);
2246 f = vxlan_find_mac(vxlan, eth->h_dest);
2247 did_rsc = false;
2249 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2250 (ntohs(eth->h_proto) == ETH_P_IP ||
2251 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2252 did_rsc = route_shortcircuit(dev, skb);
2253 if (did_rsc)
2254 f = vxlan_find_mac(vxlan, eth->h_dest);
2257 if (f == NULL) {
2258 f = vxlan_find_mac(vxlan, all_zeros_mac);
2259 if (f == NULL) {
2260 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2261 !is_multicast_ether_addr(eth->h_dest))
2262 vxlan_fdb_miss(vxlan, eth->h_dest);
2264 dev->stats.tx_dropped++;
2265 kfree_skb(skb);
2266 return NETDEV_TX_OK;
2270 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2271 struct sk_buff *skb1;
2273 if (!fdst) {
2274 fdst = rdst;
2275 continue;
2277 skb1 = skb_clone(skb, GFP_ATOMIC);
2278 if (skb1)
2279 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2282 if (fdst)
2283 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2284 else
2285 kfree_skb(skb);
2286 return NETDEV_TX_OK;
2289 /* Walk the forwarding table and purge stale entries */
2290 static void vxlan_cleanup(unsigned long arg)
2292 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2293 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2294 unsigned int h;
2296 if (!netif_running(vxlan->dev))
2297 return;
2299 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2300 struct hlist_node *p, *n;
2302 spin_lock_bh(&vxlan->hash_lock);
2303 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2304 struct vxlan_fdb *f
2305 = container_of(p, struct vxlan_fdb, hlist);
2306 unsigned long timeout;
2308 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2309 continue;
2311 timeout = f->used + vxlan->cfg.age_interval * HZ;
2312 if (time_before_eq(timeout, jiffies)) {
2313 netdev_dbg(vxlan->dev,
2314 "garbage collect %pM\n",
2315 f->eth_addr);
2316 f->state = NUD_STALE;
2317 vxlan_fdb_destroy(vxlan, f);
2318 } else if (time_before(timeout, next_timer))
2319 next_timer = timeout;
2321 spin_unlock_bh(&vxlan->hash_lock);
2324 mod_timer(&vxlan->age_timer, next_timer);
2327 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2329 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2331 spin_lock(&vn->sock_lock);
2332 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2333 #if IS_ENABLED(CONFIG_IPV6)
2334 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2335 #endif
2336 spin_unlock(&vn->sock_lock);
2339 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2340 struct vxlan_dev_node *node)
2342 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2343 __be32 vni = vxlan->default_dst.remote_vni;
2345 node->vxlan = vxlan;
2346 spin_lock(&vn->sock_lock);
2347 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2348 spin_unlock(&vn->sock_lock);
2351 /* Setup stats when device is created */
2352 static int vxlan_init(struct net_device *dev)
2354 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2355 if (!dev->tstats)
2356 return -ENOMEM;
2358 return 0;
2361 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2363 struct vxlan_fdb *f;
2365 spin_lock_bh(&vxlan->hash_lock);
2366 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2367 if (f)
2368 vxlan_fdb_destroy(vxlan, f);
2369 spin_unlock_bh(&vxlan->hash_lock);
2372 static void vxlan_uninit(struct net_device *dev)
2374 struct vxlan_dev *vxlan = netdev_priv(dev);
2376 gro_cells_destroy(&vxlan->gro_cells);
2378 vxlan_fdb_delete_default(vxlan);
2380 free_percpu(dev->tstats);
2383 /* Start ageing timer and join group when device is brought up */
2384 static int vxlan_open(struct net_device *dev)
2386 struct vxlan_dev *vxlan = netdev_priv(dev);
2387 int ret;
2389 ret = vxlan_sock_add(vxlan);
2390 if (ret < 0)
2391 return ret;
2393 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2394 ret = vxlan_igmp_join(vxlan);
2395 if (ret == -EADDRINUSE)
2396 ret = 0;
2397 if (ret) {
2398 vxlan_sock_release(vxlan);
2399 return ret;
2403 if (vxlan->cfg.age_interval)
2404 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2406 return ret;
2409 /* Purge the forwarding table */
2410 static void vxlan_flush(struct vxlan_dev *vxlan)
2412 unsigned int h;
2414 spin_lock_bh(&vxlan->hash_lock);
2415 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2416 struct hlist_node *p, *n;
2417 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2418 struct vxlan_fdb *f
2419 = container_of(p, struct vxlan_fdb, hlist);
2420 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2421 if (!is_zero_ether_addr(f->eth_addr))
2422 vxlan_fdb_destroy(vxlan, f);
2425 spin_unlock_bh(&vxlan->hash_lock);
2428 /* Cleanup timer and forwarding table on shutdown */
2429 static int vxlan_stop(struct net_device *dev)
2431 struct vxlan_dev *vxlan = netdev_priv(dev);
2432 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2433 int ret = 0;
2435 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2436 !vxlan_group_used(vn, vxlan))
2437 ret = vxlan_igmp_leave(vxlan);
2439 del_timer_sync(&vxlan->age_timer);
2441 vxlan_flush(vxlan);
2442 vxlan_sock_release(vxlan);
2444 return ret;
2447 /* Stub, nothing needs to be done. */
2448 static void vxlan_set_multicast_list(struct net_device *dev)
2452 static int __vxlan_change_mtu(struct net_device *dev,
2453 struct net_device *lowerdev,
2454 struct vxlan_rdst *dst, int new_mtu, bool strict)
2456 int max_mtu = IP_MAX_MTU;
2458 if (lowerdev)
2459 max_mtu = lowerdev->mtu;
2461 if (dst->remote_ip.sa.sa_family == AF_INET6)
2462 max_mtu -= VXLAN6_HEADROOM;
2463 else
2464 max_mtu -= VXLAN_HEADROOM;
2466 if (new_mtu < 68)
2467 return -EINVAL;
2469 if (new_mtu > max_mtu) {
2470 if (strict)
2471 return -EINVAL;
2473 new_mtu = max_mtu;
2476 dev->mtu = new_mtu;
2477 return 0;
2480 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2482 struct vxlan_dev *vxlan = netdev_priv(dev);
2483 struct vxlan_rdst *dst = &vxlan->default_dst;
2484 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2485 dst->remote_ifindex);
2486 return __vxlan_change_mtu(dev, lowerdev, dst, new_mtu, true);
2489 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2491 struct vxlan_dev *vxlan = netdev_priv(dev);
2492 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2493 __be16 sport, dport;
2495 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2496 vxlan->cfg.port_max, true);
2497 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2499 if (ip_tunnel_info_af(info) == AF_INET) {
2500 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2501 struct rtable *rt;
2503 if (!sock4)
2504 return -EINVAL;
2505 rt = vxlan_get_route(vxlan, skb, 0, info->key.tos,
2506 info->key.u.ipv4.dst,
2507 &info->key.u.ipv4.src,
2508 &info->dst_cache, info);
2509 if (IS_ERR(rt))
2510 return PTR_ERR(rt);
2511 ip_rt_put(rt);
2512 } else {
2513 #if IS_ENABLED(CONFIG_IPV6)
2514 struct dst_entry *ndst;
2516 ndst = vxlan6_get_route(vxlan, skb, 0, info->key.tos,
2517 info->key.label, &info->key.u.ipv6.dst,
2518 &info->key.u.ipv6.src,
2519 &info->dst_cache, info);
2520 if (IS_ERR(ndst))
2521 return PTR_ERR(ndst);
2522 dst_release(ndst);
2523 #else /* !CONFIG_IPV6 */
2524 return -EPFNOSUPPORT;
2525 #endif
2527 info->key.tp_src = sport;
2528 info->key.tp_dst = dport;
2529 return 0;
2532 static const struct net_device_ops vxlan_netdev_ether_ops = {
2533 .ndo_init = vxlan_init,
2534 .ndo_uninit = vxlan_uninit,
2535 .ndo_open = vxlan_open,
2536 .ndo_stop = vxlan_stop,
2537 .ndo_start_xmit = vxlan_xmit,
2538 .ndo_get_stats64 = ip_tunnel_get_stats64,
2539 .ndo_set_rx_mode = vxlan_set_multicast_list,
2540 .ndo_change_mtu = vxlan_change_mtu,
2541 .ndo_validate_addr = eth_validate_addr,
2542 .ndo_set_mac_address = eth_mac_addr,
2543 .ndo_fdb_add = vxlan_fdb_add,
2544 .ndo_fdb_del = vxlan_fdb_delete,
2545 .ndo_fdb_dump = vxlan_fdb_dump,
2546 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2549 static const struct net_device_ops vxlan_netdev_raw_ops = {
2550 .ndo_init = vxlan_init,
2551 .ndo_uninit = vxlan_uninit,
2552 .ndo_open = vxlan_open,
2553 .ndo_stop = vxlan_stop,
2554 .ndo_start_xmit = vxlan_xmit,
2555 .ndo_get_stats64 = ip_tunnel_get_stats64,
2556 .ndo_change_mtu = vxlan_change_mtu,
2557 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2560 /* Info for udev, that this is a virtual tunnel endpoint */
2561 static struct device_type vxlan_type = {
2562 .name = "vxlan",
2565 /* Calls the ndo_udp_tunnel_add of the caller in order to
2566 * supply the listening VXLAN udp ports. Callers are expected
2567 * to implement the ndo_udp_tunnel_add.
2569 static void vxlan_push_rx_ports(struct net_device *dev)
2571 struct vxlan_sock *vs;
2572 struct net *net = dev_net(dev);
2573 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2574 unsigned int i;
2576 spin_lock(&vn->sock_lock);
2577 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2578 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2579 udp_tunnel_push_rx_port(dev, vs->sock,
2580 (vs->flags & VXLAN_F_GPE) ?
2581 UDP_TUNNEL_TYPE_VXLAN_GPE :
2582 UDP_TUNNEL_TYPE_VXLAN);
2584 spin_unlock(&vn->sock_lock);
2587 /* Initialize the device structure. */
2588 static void vxlan_setup(struct net_device *dev)
2590 struct vxlan_dev *vxlan = netdev_priv(dev);
2591 unsigned int h;
2593 eth_hw_addr_random(dev);
2594 ether_setup(dev);
2596 dev->destructor = free_netdev;
2597 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2599 dev->features |= NETIF_F_LLTX;
2600 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2601 dev->features |= NETIF_F_RXCSUM;
2602 dev->features |= NETIF_F_GSO_SOFTWARE;
2604 dev->vlan_features = dev->features;
2605 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2606 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2607 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2608 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2609 netif_keep_dst(dev);
2610 dev->priv_flags |= IFF_NO_QUEUE;
2612 INIT_LIST_HEAD(&vxlan->next);
2613 spin_lock_init(&vxlan->hash_lock);
2615 init_timer_deferrable(&vxlan->age_timer);
2616 vxlan->age_timer.function = vxlan_cleanup;
2617 vxlan->age_timer.data = (unsigned long) vxlan;
2619 vxlan->cfg.dst_port = htons(vxlan_port);
2621 vxlan->dev = dev;
2623 gro_cells_init(&vxlan->gro_cells, dev);
2625 for (h = 0; h < FDB_HASH_SIZE; ++h)
2626 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2629 static void vxlan_ether_setup(struct net_device *dev)
2631 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2632 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2633 dev->netdev_ops = &vxlan_netdev_ether_ops;
2636 static void vxlan_raw_setup(struct net_device *dev)
2638 dev->header_ops = NULL;
2639 dev->type = ARPHRD_NONE;
2640 dev->hard_header_len = 0;
2641 dev->addr_len = 0;
2642 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2643 dev->netdev_ops = &vxlan_netdev_raw_ops;
2646 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2647 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2648 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2649 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2650 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2651 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2652 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2653 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2654 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2655 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
2656 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2657 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2658 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2659 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2660 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2661 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2662 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2663 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2664 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
2665 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2666 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2667 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2668 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2669 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2670 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2671 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2672 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
2673 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
2676 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2678 if (tb[IFLA_ADDRESS]) {
2679 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2680 pr_debug("invalid link address (not ethernet)\n");
2681 return -EINVAL;
2684 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2685 pr_debug("invalid all zero ethernet address\n");
2686 return -EADDRNOTAVAIL;
2690 if (!data)
2691 return -EINVAL;
2693 if (data[IFLA_VXLAN_ID]) {
2694 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2695 if (id >= VXLAN_N_VID)
2696 return -ERANGE;
2699 if (data[IFLA_VXLAN_PORT_RANGE]) {
2700 const struct ifla_vxlan_port_range *p
2701 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2703 if (ntohs(p->high) < ntohs(p->low)) {
2704 pr_debug("port range %u .. %u not valid\n",
2705 ntohs(p->low), ntohs(p->high));
2706 return -EINVAL;
2710 return 0;
2713 static void vxlan_get_drvinfo(struct net_device *netdev,
2714 struct ethtool_drvinfo *drvinfo)
2716 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2717 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2720 static const struct ethtool_ops vxlan_ethtool_ops = {
2721 .get_drvinfo = vxlan_get_drvinfo,
2722 .get_link = ethtool_op_get_link,
2725 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2726 __be16 port, u32 flags)
2728 struct socket *sock;
2729 struct udp_port_cfg udp_conf;
2730 int err;
2732 memset(&udp_conf, 0, sizeof(udp_conf));
2734 if (ipv6) {
2735 udp_conf.family = AF_INET6;
2736 udp_conf.use_udp6_rx_checksums =
2737 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2738 udp_conf.ipv6_v6only = 1;
2739 } else {
2740 udp_conf.family = AF_INET;
2743 udp_conf.local_udp_port = port;
2745 /* Open UDP socket */
2746 err = udp_sock_create(net, &udp_conf, &sock);
2747 if (err < 0)
2748 return ERR_PTR(err);
2750 return sock;
2753 /* Create new listen socket if needed */
2754 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2755 __be16 port, u32 flags)
2757 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2758 struct vxlan_sock *vs;
2759 struct socket *sock;
2760 unsigned int h;
2761 struct udp_tunnel_sock_cfg tunnel_cfg;
2763 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2764 if (!vs)
2765 return ERR_PTR(-ENOMEM);
2767 for (h = 0; h < VNI_HASH_SIZE; ++h)
2768 INIT_HLIST_HEAD(&vs->vni_list[h]);
2770 sock = vxlan_create_sock(net, ipv6, port, flags);
2771 if (IS_ERR(sock)) {
2772 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2773 PTR_ERR(sock));
2774 kfree(vs);
2775 return ERR_CAST(sock);
2778 vs->sock = sock;
2779 atomic_set(&vs->refcnt, 1);
2780 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2782 spin_lock(&vn->sock_lock);
2783 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2784 udp_tunnel_notify_add_rx_port(sock,
2785 (vs->flags & VXLAN_F_GPE) ?
2786 UDP_TUNNEL_TYPE_VXLAN_GPE :
2787 UDP_TUNNEL_TYPE_VXLAN);
2788 spin_unlock(&vn->sock_lock);
2790 /* Mark socket as an encapsulation socket. */
2791 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
2792 tunnel_cfg.sk_user_data = vs;
2793 tunnel_cfg.encap_type = 1;
2794 tunnel_cfg.encap_rcv = vxlan_rcv;
2795 tunnel_cfg.encap_destroy = NULL;
2796 tunnel_cfg.gro_receive = vxlan_gro_receive;
2797 tunnel_cfg.gro_complete = vxlan_gro_complete;
2799 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2801 return vs;
2804 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
2806 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2807 struct vxlan_sock *vs = NULL;
2808 struct vxlan_dev_node *node;
2810 if (!vxlan->cfg.no_share) {
2811 spin_lock(&vn->sock_lock);
2812 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2813 vxlan->cfg.dst_port, vxlan->flags);
2814 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
2815 spin_unlock(&vn->sock_lock);
2816 return -EBUSY;
2818 spin_unlock(&vn->sock_lock);
2820 if (!vs)
2821 vs = vxlan_socket_create(vxlan->net, ipv6,
2822 vxlan->cfg.dst_port, vxlan->flags);
2823 if (IS_ERR(vs))
2824 return PTR_ERR(vs);
2825 #if IS_ENABLED(CONFIG_IPV6)
2826 if (ipv6) {
2827 rcu_assign_pointer(vxlan->vn6_sock, vs);
2828 node = &vxlan->hlist6;
2829 } else
2830 #endif
2832 rcu_assign_pointer(vxlan->vn4_sock, vs);
2833 node = &vxlan->hlist4;
2835 vxlan_vs_add_dev(vs, vxlan, node);
2836 return 0;
2839 static int vxlan_sock_add(struct vxlan_dev *vxlan)
2841 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2842 bool ipv6 = vxlan->flags & VXLAN_F_IPV6 || metadata;
2843 bool ipv4 = !ipv6 || metadata;
2844 int ret = 0;
2846 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
2847 #if IS_ENABLED(CONFIG_IPV6)
2848 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
2849 if (ipv6) {
2850 ret = __vxlan_sock_add(vxlan, true);
2851 if (ret < 0 && ret != -EAFNOSUPPORT)
2852 ipv4 = false;
2854 #endif
2855 if (ipv4)
2856 ret = __vxlan_sock_add(vxlan, false);
2857 if (ret < 0)
2858 vxlan_sock_release(vxlan);
2859 return ret;
2862 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2863 struct vxlan_config *conf)
2865 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2866 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
2867 struct vxlan_rdst *dst = &vxlan->default_dst;
2868 unsigned short needed_headroom = ETH_HLEN;
2869 int err;
2870 bool use_ipv6 = false;
2871 __be16 default_port = vxlan->cfg.dst_port;
2872 struct net_device *lowerdev = NULL;
2874 if (conf->flags & VXLAN_F_GPE) {
2875 /* For now, allow GPE only together with COLLECT_METADATA.
2876 * This can be relaxed later; in such case, the other side
2877 * of the PtP link will have to be provided.
2879 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2880 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2881 pr_info("unsupported combination of extensions\n");
2882 return -EINVAL;
2885 vxlan_raw_setup(dev);
2886 } else {
2887 vxlan_ether_setup(dev);
2890 vxlan->net = src_net;
2892 dst->remote_vni = conf->vni;
2894 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
2896 /* Unless IPv6 is explicitly requested, assume IPv4 */
2897 if (!dst->remote_ip.sa.sa_family)
2898 dst->remote_ip.sa.sa_family = AF_INET;
2900 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
2901 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2902 if (!IS_ENABLED(CONFIG_IPV6))
2903 return -EPFNOSUPPORT;
2904 use_ipv6 = true;
2905 vxlan->flags |= VXLAN_F_IPV6;
2908 if (conf->label && !use_ipv6) {
2909 pr_info("label only supported in use with IPv6\n");
2910 return -EINVAL;
2913 if (conf->remote_ifindex) {
2914 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2915 dst->remote_ifindex = conf->remote_ifindex;
2917 if (!lowerdev) {
2918 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2919 return -ENODEV;
2922 #if IS_ENABLED(CONFIG_IPV6)
2923 if (use_ipv6) {
2924 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2925 if (idev && idev->cnf.disable_ipv6) {
2926 pr_info("IPv6 is disabled via sysctl\n");
2927 return -EPERM;
2930 #endif
2932 if (!conf->mtu)
2933 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2935 needed_headroom = lowerdev->hard_header_len;
2936 } else if (vxlan_addr_multicast(&dst->remote_ip)) {
2937 pr_info("multicast destination requires interface to be specified\n");
2938 return -EINVAL;
2941 if (lowerdev) {
2942 dev->gso_max_size = lowerdev->gso_max_size;
2943 dev->gso_max_segs = lowerdev->gso_max_segs;
2946 if (conf->mtu) {
2947 err = __vxlan_change_mtu(dev, lowerdev, dst, conf->mtu, false);
2948 if (err)
2949 return err;
2952 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2953 needed_headroom += VXLAN6_HEADROOM;
2954 else
2955 needed_headroom += VXLAN_HEADROOM;
2956 dev->needed_headroom = needed_headroom;
2958 memcpy(&vxlan->cfg, conf, sizeof(*conf));
2959 if (!vxlan->cfg.dst_port) {
2960 if (conf->flags & VXLAN_F_GPE)
2961 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
2962 else
2963 vxlan->cfg.dst_port = default_port;
2965 vxlan->flags |= conf->flags;
2967 if (!vxlan->cfg.age_interval)
2968 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
2970 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2971 if (tmp->cfg.vni == conf->vni &&
2972 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2973 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2974 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2975 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2976 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2977 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2978 return -EEXIST;
2982 dev->ethtool_ops = &vxlan_ethtool_ops;
2984 /* create an fdb entry for a valid default destination */
2985 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2986 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2987 &vxlan->default_dst.remote_ip,
2988 NUD_REACHABLE|NUD_PERMANENT,
2989 NLM_F_EXCL|NLM_F_CREATE,
2990 vxlan->cfg.dst_port,
2991 vxlan->default_dst.remote_vni,
2992 vxlan->default_dst.remote_ifindex,
2993 NTF_SELF);
2994 if (err)
2995 return err;
2998 err = register_netdevice(dev);
2999 if (err) {
3000 vxlan_fdb_delete_default(vxlan);
3001 return err;
3004 list_add(&vxlan->next, &vn->vxlan_list);
3006 return 0;
3009 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3010 struct nlattr *tb[], struct nlattr *data[])
3012 struct vxlan_config conf;
3014 memset(&conf, 0, sizeof(conf));
3016 if (data[IFLA_VXLAN_ID])
3017 conf.vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3019 if (data[IFLA_VXLAN_GROUP]) {
3020 conf.remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3021 } else if (data[IFLA_VXLAN_GROUP6]) {
3022 if (!IS_ENABLED(CONFIG_IPV6))
3023 return -EPFNOSUPPORT;
3025 conf.remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3026 conf.remote_ip.sa.sa_family = AF_INET6;
3029 if (data[IFLA_VXLAN_LOCAL]) {
3030 conf.saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3031 conf.saddr.sa.sa_family = AF_INET;
3032 } else if (data[IFLA_VXLAN_LOCAL6]) {
3033 if (!IS_ENABLED(CONFIG_IPV6))
3034 return -EPFNOSUPPORT;
3036 /* TODO: respect scope id */
3037 conf.saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3038 conf.saddr.sa.sa_family = AF_INET6;
3041 if (data[IFLA_VXLAN_LINK])
3042 conf.remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3044 if (data[IFLA_VXLAN_TOS])
3045 conf.tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3047 if (data[IFLA_VXLAN_TTL])
3048 conf.ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3050 if (data[IFLA_VXLAN_LABEL])
3051 conf.label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3052 IPV6_FLOWLABEL_MASK;
3054 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3055 conf.flags |= VXLAN_F_LEARN;
3057 if (data[IFLA_VXLAN_AGEING])
3058 conf.age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3060 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
3061 conf.flags |= VXLAN_F_PROXY;
3063 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
3064 conf.flags |= VXLAN_F_RSC;
3066 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3067 conf.flags |= VXLAN_F_L2MISS;
3069 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3070 conf.flags |= VXLAN_F_L3MISS;
3072 if (data[IFLA_VXLAN_LIMIT])
3073 conf.addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3075 if (data[IFLA_VXLAN_COLLECT_METADATA] &&
3076 nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3077 conf.flags |= VXLAN_F_COLLECT_METADATA;
3079 if (data[IFLA_VXLAN_PORT_RANGE]) {
3080 const struct ifla_vxlan_port_range *p
3081 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3082 conf.port_min = ntohs(p->low);
3083 conf.port_max = ntohs(p->high);
3086 if (data[IFLA_VXLAN_PORT])
3087 conf.dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3089 if (data[IFLA_VXLAN_UDP_CSUM] &&
3090 !nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3091 conf.flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3093 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
3094 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3095 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3097 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
3098 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3099 conf.flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3101 if (data[IFLA_VXLAN_REMCSUM_TX] &&
3102 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3103 conf.flags |= VXLAN_F_REMCSUM_TX;
3105 if (data[IFLA_VXLAN_REMCSUM_RX] &&
3106 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3107 conf.flags |= VXLAN_F_REMCSUM_RX;
3109 if (data[IFLA_VXLAN_GBP])
3110 conf.flags |= VXLAN_F_GBP;
3112 if (data[IFLA_VXLAN_GPE])
3113 conf.flags |= VXLAN_F_GPE;
3115 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
3116 conf.flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3118 if (tb[IFLA_MTU])
3119 conf.mtu = nla_get_u32(tb[IFLA_MTU]);
3121 return vxlan_dev_configure(src_net, dev, &conf);
3124 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3126 struct vxlan_dev *vxlan = netdev_priv(dev);
3128 list_del(&vxlan->next);
3129 unregister_netdevice_queue(dev, head);
3132 static size_t vxlan_get_size(const struct net_device *dev)
3135 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
3136 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3137 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3138 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3139 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3140 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
3141 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
3142 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
3143 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3144 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3145 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3146 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
3147 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
3148 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3149 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3150 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3151 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3152 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3153 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3154 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3155 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3156 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3160 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3162 const struct vxlan_dev *vxlan = netdev_priv(dev);
3163 const struct vxlan_rdst *dst = &vxlan->default_dst;
3164 struct ifla_vxlan_port_range ports = {
3165 .low = htons(vxlan->cfg.port_min),
3166 .high = htons(vxlan->cfg.port_max),
3169 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
3170 goto nla_put_failure;
3172 if (!vxlan_addr_any(&dst->remote_ip)) {
3173 if (dst->remote_ip.sa.sa_family == AF_INET) {
3174 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3175 dst->remote_ip.sin.sin_addr.s_addr))
3176 goto nla_put_failure;
3177 #if IS_ENABLED(CONFIG_IPV6)
3178 } else {
3179 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3180 &dst->remote_ip.sin6.sin6_addr))
3181 goto nla_put_failure;
3182 #endif
3186 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
3187 goto nla_put_failure;
3189 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3190 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
3191 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
3192 vxlan->cfg.saddr.sin.sin_addr.s_addr))
3193 goto nla_put_failure;
3194 #if IS_ENABLED(CONFIG_IPV6)
3195 } else {
3196 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
3197 &vxlan->cfg.saddr.sin6.sin6_addr))
3198 goto nla_put_failure;
3199 #endif
3203 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3204 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
3205 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
3206 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3207 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3208 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3209 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3210 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3211 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3212 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3213 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3214 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
3215 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3216 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
3217 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3218 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3219 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
3220 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
3221 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
3222 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3223 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3224 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3225 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3226 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3227 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3228 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3229 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
3230 goto nla_put_failure;
3232 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3233 goto nla_put_failure;
3235 if (vxlan->flags & VXLAN_F_GBP &&
3236 nla_put_flag(skb, IFLA_VXLAN_GBP))
3237 goto nla_put_failure;
3239 if (vxlan->flags & VXLAN_F_GPE &&
3240 nla_put_flag(skb, IFLA_VXLAN_GPE))
3241 goto nla_put_failure;
3243 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3244 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3245 goto nla_put_failure;
3247 return 0;
3249 nla_put_failure:
3250 return -EMSGSIZE;
3253 static struct net *vxlan_get_link_net(const struct net_device *dev)
3255 struct vxlan_dev *vxlan = netdev_priv(dev);
3257 return vxlan->net;
3260 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3261 .kind = "vxlan",
3262 .maxtype = IFLA_VXLAN_MAX,
3263 .policy = vxlan_policy,
3264 .priv_size = sizeof(struct vxlan_dev),
3265 .setup = vxlan_setup,
3266 .validate = vxlan_validate,
3267 .newlink = vxlan_newlink,
3268 .dellink = vxlan_dellink,
3269 .get_size = vxlan_get_size,
3270 .fill_info = vxlan_fill_info,
3271 .get_link_net = vxlan_get_link_net,
3274 struct net_device *vxlan_dev_create(struct net *net, const char *name,
3275 u8 name_assign_type,
3276 struct vxlan_config *conf)
3278 struct nlattr *tb[IFLA_MAX + 1];
3279 struct net_device *dev;
3280 int err;
3282 memset(&tb, 0, sizeof(tb));
3284 dev = rtnl_create_link(net, name, name_assign_type,
3285 &vxlan_link_ops, tb);
3286 if (IS_ERR(dev))
3287 return dev;
3289 err = vxlan_dev_configure(net, dev, conf);
3290 if (err < 0) {
3291 free_netdev(dev);
3292 return ERR_PTR(err);
3295 err = rtnl_configure_link(dev, NULL);
3296 if (err < 0) {
3297 LIST_HEAD(list_kill);
3299 vxlan_dellink(dev, &list_kill);
3300 unregister_netdevice_many(&list_kill);
3301 return ERR_PTR(err);
3304 return dev;
3306 EXPORT_SYMBOL_GPL(vxlan_dev_create);
3308 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3309 struct net_device *dev)
3311 struct vxlan_dev *vxlan, *next;
3312 LIST_HEAD(list_kill);
3314 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3315 struct vxlan_rdst *dst = &vxlan->default_dst;
3317 /* In case we created vxlan device with carrier
3318 * and we loose the carrier due to module unload
3319 * we also need to remove vxlan device. In other
3320 * cases, it's not necessary and remote_ifindex
3321 * is 0 here, so no matches.
3323 if (dst->remote_ifindex == dev->ifindex)
3324 vxlan_dellink(vxlan->dev, &list_kill);
3327 unregister_netdevice_many(&list_kill);
3330 static int vxlan_netdevice_event(struct notifier_block *unused,
3331 unsigned long event, void *ptr)
3333 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3334 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3336 if (event == NETDEV_UNREGISTER)
3337 vxlan_handle_lowerdev_unregister(vn, dev);
3338 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
3339 vxlan_push_rx_ports(dev);
3341 return NOTIFY_DONE;
3344 static struct notifier_block vxlan_notifier_block __read_mostly = {
3345 .notifier_call = vxlan_netdevice_event,
3348 static __net_init int vxlan_init_net(struct net *net)
3350 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3351 unsigned int h;
3353 INIT_LIST_HEAD(&vn->vxlan_list);
3354 spin_lock_init(&vn->sock_lock);
3356 for (h = 0; h < PORT_HASH_SIZE; ++h)
3357 INIT_HLIST_HEAD(&vn->sock_list[h]);
3359 return 0;
3362 static void __net_exit vxlan_exit_net(struct net *net)
3364 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3365 struct vxlan_dev *vxlan, *next;
3366 struct net_device *dev, *aux;
3367 LIST_HEAD(list);
3369 rtnl_lock();
3370 for_each_netdev_safe(net, dev, aux)
3371 if (dev->rtnl_link_ops == &vxlan_link_ops)
3372 unregister_netdevice_queue(dev, &list);
3374 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3375 /* If vxlan->dev is in the same netns, it has already been added
3376 * to the list by the previous loop.
3378 if (!net_eq(dev_net(vxlan->dev), net))
3379 unregister_netdevice_queue(vxlan->dev, &list);
3382 unregister_netdevice_many(&list);
3383 rtnl_unlock();
3386 static struct pernet_operations vxlan_net_ops = {
3387 .init = vxlan_init_net,
3388 .exit = vxlan_exit_net,
3389 .id = &vxlan_net_id,
3390 .size = sizeof(struct vxlan_net),
3393 static int __init vxlan_init_module(void)
3395 int rc;
3397 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3399 rc = register_pernet_subsys(&vxlan_net_ops);
3400 if (rc)
3401 goto out1;
3403 rc = register_netdevice_notifier(&vxlan_notifier_block);
3404 if (rc)
3405 goto out2;
3407 rc = rtnl_link_register(&vxlan_link_ops);
3408 if (rc)
3409 goto out3;
3411 return 0;
3412 out3:
3413 unregister_netdevice_notifier(&vxlan_notifier_block);
3414 out2:
3415 unregister_pernet_subsys(&vxlan_net_ops);
3416 out1:
3417 return rc;
3419 late_initcall(vxlan_init_module);
3421 static void __exit vxlan_cleanup_module(void)
3423 rtnl_link_unregister(&vxlan_link_ops);
3424 unregister_netdevice_notifier(&vxlan_notifier_block);
3425 unregister_pernet_subsys(&vxlan_net_ops);
3426 /* rcu_barrier() is called by netns */
3428 module_exit(vxlan_cleanup_module);
3430 MODULE_LICENSE("GPL");
3431 MODULE_VERSION(VXLAN_VERSION);
3432 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3433 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3434 MODULE_ALIAS_RTNL_LINK("vxlan");