x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / net / vxlan.c
blob4574b95c7938277c5ea4849d6df111476218727c
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 unsigned 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 __be32 vni;
81 u8 flags; /* see ndm_flags */
84 /* salt for hash table */
85 static u32 vxlan_salt __read_mostly;
87 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
89 return vs->flags & VXLAN_F_COLLECT_METADATA ||
90 ip_tunnel_collect_metadata();
93 #if IS_ENABLED(CONFIG_IPV6)
94 static inline
95 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
97 if (a->sa.sa_family != b->sa.sa_family)
98 return false;
99 if (a->sa.sa_family == AF_INET6)
100 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
101 else
102 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
105 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
107 if (ipa->sa.sa_family == AF_INET6)
108 return ipv6_addr_any(&ipa->sin6.sin6_addr);
109 else
110 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
113 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
115 if (ipa->sa.sa_family == AF_INET6)
116 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
117 else
118 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
121 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
123 if (nla_len(nla) >= sizeof(struct in6_addr)) {
124 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
125 ip->sa.sa_family = AF_INET6;
126 return 0;
127 } else if (nla_len(nla) >= sizeof(__be32)) {
128 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
129 ip->sa.sa_family = AF_INET;
130 return 0;
131 } else {
132 return -EAFNOSUPPORT;
136 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
137 const union vxlan_addr *ip)
139 if (ip->sa.sa_family == AF_INET6)
140 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
141 else
142 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
145 #else /* !CONFIG_IPV6 */
147 static inline
148 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
150 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
153 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
155 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
158 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
160 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
163 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
165 if (nla_len(nla) >= sizeof(struct in6_addr)) {
166 return -EAFNOSUPPORT;
167 } else if (nla_len(nla) >= sizeof(__be32)) {
168 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
169 ip->sa.sa_family = AF_INET;
170 return 0;
171 } else {
172 return -EAFNOSUPPORT;
176 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
177 const union vxlan_addr *ip)
179 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
181 #endif
183 /* Virtual Network hash table head */
184 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
186 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
189 /* Socket hash table head */
190 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
194 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
197 /* First remote destination for a forwarding entry.
198 * Guaranteed to be non-NULL because remotes are never deleted.
200 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
202 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
205 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
207 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
210 /* Find VXLAN socket based on network namespace, address family and UDP port
211 * and enabled unshareable flags.
213 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
214 __be16 port, u32 flags)
216 struct vxlan_sock *vs;
218 flags &= VXLAN_F_RCV_FLAGS;
220 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
221 if (inet_sk(vs->sock->sk)->inet_sport == port &&
222 vxlan_get_sk_family(vs) == family &&
223 vs->flags == flags)
224 return vs;
226 return NULL;
229 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
231 struct vxlan_dev *vxlan;
233 /* For flow based devices, map all packets to VNI 0 */
234 if (vs->flags & VXLAN_F_COLLECT_METADATA)
235 vni = 0;
237 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
238 if (vxlan->default_dst.remote_vni == vni)
239 return vxlan;
242 return NULL;
245 /* Look up VNI in a per net namespace table */
246 static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
247 sa_family_t family, __be16 port,
248 u32 flags)
250 struct vxlan_sock *vs;
252 vs = vxlan_find_sock(net, family, port, flags);
253 if (!vs)
254 return NULL;
256 return vxlan_vs_find_vni(vs, vni);
259 /* Fill in neighbour message in skbuff. */
260 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
261 const struct vxlan_fdb *fdb,
262 u32 portid, u32 seq, int type, unsigned int flags,
263 const struct vxlan_rdst *rdst)
265 unsigned long now = jiffies;
266 struct nda_cacheinfo ci;
267 struct nlmsghdr *nlh;
268 struct ndmsg *ndm;
269 bool send_ip, send_eth;
271 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
272 if (nlh == NULL)
273 return -EMSGSIZE;
275 ndm = nlmsg_data(nlh);
276 memset(ndm, 0, sizeof(*ndm));
278 send_eth = send_ip = true;
280 if (type == RTM_GETNEIGH) {
281 ndm->ndm_family = AF_INET;
282 send_ip = !vxlan_addr_any(&rdst->remote_ip);
283 send_eth = !is_zero_ether_addr(fdb->eth_addr);
284 } else
285 ndm->ndm_family = AF_BRIDGE;
286 ndm->ndm_state = fdb->state;
287 ndm->ndm_ifindex = vxlan->dev->ifindex;
288 ndm->ndm_flags = fdb->flags;
289 ndm->ndm_type = RTN_UNICAST;
291 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
292 nla_put_s32(skb, NDA_LINK_NETNSID,
293 peernet2id(dev_net(vxlan->dev), vxlan->net)))
294 goto nla_put_failure;
296 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
297 goto nla_put_failure;
299 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
300 goto nla_put_failure;
302 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
303 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
304 goto nla_put_failure;
305 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
306 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
307 goto nla_put_failure;
308 if ((vxlan->flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
309 nla_put_u32(skb, NDA_SRC_VNI,
310 be32_to_cpu(fdb->vni)))
311 goto nla_put_failure;
312 if (rdst->remote_ifindex &&
313 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
314 goto nla_put_failure;
316 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
317 ci.ndm_confirmed = 0;
318 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
319 ci.ndm_refcnt = 0;
321 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
322 goto nla_put_failure;
324 nlmsg_end(skb, nlh);
325 return 0;
327 nla_put_failure:
328 nlmsg_cancel(skb, nlh);
329 return -EMSGSIZE;
332 static inline size_t vxlan_nlmsg_size(void)
334 return NLMSG_ALIGN(sizeof(struct ndmsg))
335 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
336 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
337 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
338 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
339 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
340 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
341 + nla_total_size(sizeof(struct nda_cacheinfo));
344 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
345 struct vxlan_rdst *rd, int type)
347 struct net *net = dev_net(vxlan->dev);
348 struct sk_buff *skb;
349 int err = -ENOBUFS;
351 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
352 if (skb == NULL)
353 goto errout;
355 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
356 if (err < 0) {
357 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
358 WARN_ON(err == -EMSGSIZE);
359 kfree_skb(skb);
360 goto errout;
363 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
364 return;
365 errout:
366 if (err < 0)
367 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
370 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
372 struct vxlan_dev *vxlan = netdev_priv(dev);
373 struct vxlan_fdb f = {
374 .state = NUD_STALE,
376 struct vxlan_rdst remote = {
377 .remote_ip = *ipa, /* goes to NDA_DST */
378 .remote_vni = cpu_to_be32(VXLAN_N_VID),
381 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
384 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
386 struct vxlan_fdb f = {
387 .state = NUD_STALE,
389 struct vxlan_rdst remote = { };
391 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
393 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
396 /* Hash Ethernet address */
397 static u32 eth_hash(const unsigned char *addr)
399 u64 value = get_unaligned((u64 *)addr);
401 /* only want 6 bytes */
402 #ifdef __BIG_ENDIAN
403 value >>= 16;
404 #else
405 value <<= 16;
406 #endif
407 return hash_64(value, FDB_HASH_BITS);
410 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
412 /* use 1 byte of OUI and 3 bytes of NIC */
413 u32 key = get_unaligned((u32 *)(addr + 2));
415 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
418 /* Hash chain to use given mac address */
419 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
420 const u8 *mac, __be32 vni)
422 if (vxlan->flags & VXLAN_F_COLLECT_METADATA)
423 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
424 else
425 return &vxlan->fdb_head[eth_hash(mac)];
428 /* Look up Ethernet address in forwarding table */
429 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
430 const u8 *mac, __be32 vni)
432 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
433 struct vxlan_fdb *f;
435 hlist_for_each_entry_rcu(f, head, hlist) {
436 if (ether_addr_equal(mac, f->eth_addr)) {
437 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
438 if (vni == f->vni)
439 return f;
440 } else {
441 return f;
446 return NULL;
449 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
450 const u8 *mac, __be32 vni)
452 struct vxlan_fdb *f;
454 f = __vxlan_find_mac(vxlan, mac, vni);
455 if (f)
456 f->used = jiffies;
458 return f;
461 /* caller should hold vxlan->hash_lock */
462 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
463 union vxlan_addr *ip, __be16 port,
464 __be32 vni, __u32 ifindex)
466 struct vxlan_rdst *rd;
468 list_for_each_entry(rd, &f->remotes, list) {
469 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
470 rd->remote_port == port &&
471 rd->remote_vni == vni &&
472 rd->remote_ifindex == ifindex)
473 return rd;
476 return NULL;
479 /* Replace destination of unicast mac */
480 static int vxlan_fdb_replace(struct vxlan_fdb *f,
481 union vxlan_addr *ip, __be16 port, __be32 vni,
482 __u32 ifindex)
484 struct vxlan_rdst *rd;
486 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
487 if (rd)
488 return 0;
490 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
491 if (!rd)
492 return 0;
494 dst_cache_reset(&rd->dst_cache);
495 rd->remote_ip = *ip;
496 rd->remote_port = port;
497 rd->remote_vni = vni;
498 rd->remote_ifindex = ifindex;
499 return 1;
502 /* Add/update destinations for multicast */
503 static int vxlan_fdb_append(struct vxlan_fdb *f,
504 union vxlan_addr *ip, __be16 port, __be32 vni,
505 __u32 ifindex, struct vxlan_rdst **rdp)
507 struct vxlan_rdst *rd;
509 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
510 if (rd)
511 return 0;
513 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
514 if (rd == NULL)
515 return -ENOBUFS;
517 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
518 kfree(rd);
519 return -ENOBUFS;
522 rd->remote_ip = *ip;
523 rd->remote_port = port;
524 rd->remote_vni = vni;
525 rd->remote_ifindex = ifindex;
527 list_add_tail_rcu(&rd->list, &f->remotes);
529 *rdp = rd;
530 return 1;
533 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
534 unsigned int off,
535 struct vxlanhdr *vh, size_t hdrlen,
536 __be32 vni_field,
537 struct gro_remcsum *grc,
538 bool nopartial)
540 size_t start, offset;
542 if (skb->remcsum_offload)
543 return vh;
545 if (!NAPI_GRO_CB(skb)->csum_valid)
546 return NULL;
548 start = vxlan_rco_start(vni_field);
549 offset = start + vxlan_rco_offset(vni_field);
551 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
552 start, offset, grc, nopartial);
554 skb->remcsum_offload = 1;
556 return vh;
559 static struct sk_buff **vxlan_gro_receive(struct sock *sk,
560 struct sk_buff **head,
561 struct sk_buff *skb)
563 struct sk_buff *p, **pp = NULL;
564 struct vxlanhdr *vh, *vh2;
565 unsigned int hlen, off_vx;
566 int flush = 1;
567 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
568 __be32 flags;
569 struct gro_remcsum grc;
571 skb_gro_remcsum_init(&grc);
573 off_vx = skb_gro_offset(skb);
574 hlen = off_vx + sizeof(*vh);
575 vh = skb_gro_header_fast(skb, off_vx);
576 if (skb_gro_header_hard(skb, hlen)) {
577 vh = skb_gro_header_slow(skb, hlen, off_vx);
578 if (unlikely(!vh))
579 goto out;
582 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
584 flags = vh->vx_flags;
586 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
587 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
588 vh->vx_vni, &grc,
589 !!(vs->flags &
590 VXLAN_F_REMCSUM_NOPARTIAL));
592 if (!vh)
593 goto out;
596 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
598 for (p = *head; p; p = p->next) {
599 if (!NAPI_GRO_CB(p)->same_flow)
600 continue;
602 vh2 = (struct vxlanhdr *)(p->data + off_vx);
603 if (vh->vx_flags != vh2->vx_flags ||
604 vh->vx_vni != vh2->vx_vni) {
605 NAPI_GRO_CB(p)->same_flow = 0;
606 continue;
610 pp = call_gro_receive(eth_gro_receive, head, skb);
611 flush = 0;
613 out:
614 skb_gro_remcsum_cleanup(skb, &grc);
615 NAPI_GRO_CB(skb)->flush |= flush;
617 return pp;
620 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
622 /* Sets 'skb->inner_mac_header' since we are always called with
623 * 'skb->encapsulation' set.
625 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
628 /* Add new entry to forwarding table -- assumes lock held */
629 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
630 const u8 *mac, union vxlan_addr *ip,
631 __u16 state, __u16 flags,
632 __be16 port, __be32 src_vni, __be32 vni,
633 __u32 ifindex, __u8 ndm_flags)
635 struct vxlan_rdst *rd = NULL;
636 struct vxlan_fdb *f;
637 int notify = 0;
638 int rc;
640 f = __vxlan_find_mac(vxlan, mac, src_vni);
641 if (f) {
642 if (flags & NLM_F_EXCL) {
643 netdev_dbg(vxlan->dev,
644 "lost race to create %pM\n", mac);
645 return -EEXIST;
647 if (f->state != state) {
648 f->state = state;
649 f->updated = jiffies;
650 notify = 1;
652 if (f->flags != ndm_flags) {
653 f->flags = ndm_flags;
654 f->updated = jiffies;
655 notify = 1;
657 if ((flags & NLM_F_REPLACE)) {
658 /* Only change unicasts */
659 if (!(is_multicast_ether_addr(f->eth_addr) ||
660 is_zero_ether_addr(f->eth_addr))) {
661 notify |= vxlan_fdb_replace(f, ip, port, vni,
662 ifindex);
663 } else
664 return -EOPNOTSUPP;
666 if ((flags & NLM_F_APPEND) &&
667 (is_multicast_ether_addr(f->eth_addr) ||
668 is_zero_ether_addr(f->eth_addr))) {
669 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
671 if (rc < 0)
672 return rc;
673 notify |= rc;
675 } else {
676 if (!(flags & NLM_F_CREATE))
677 return -ENOENT;
679 if (vxlan->cfg.addrmax &&
680 vxlan->addrcnt >= vxlan->cfg.addrmax)
681 return -ENOSPC;
683 /* Disallow replace to add a multicast entry */
684 if ((flags & NLM_F_REPLACE) &&
685 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
686 return -EOPNOTSUPP;
688 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
689 f = kmalloc(sizeof(*f), GFP_ATOMIC);
690 if (!f)
691 return -ENOMEM;
693 notify = 1;
694 f->state = state;
695 f->flags = ndm_flags;
696 f->updated = f->used = jiffies;
697 f->vni = src_vni;
698 INIT_LIST_HEAD(&f->remotes);
699 memcpy(f->eth_addr, mac, ETH_ALEN);
701 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
702 if (rc < 0) {
703 kfree(f);
704 return rc;
707 ++vxlan->addrcnt;
708 hlist_add_head_rcu(&f->hlist,
709 vxlan_fdb_head(vxlan, mac, src_vni));
712 if (notify) {
713 if (rd == NULL)
714 rd = first_remote_rtnl(f);
715 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
718 return 0;
721 static void vxlan_fdb_free(struct rcu_head *head)
723 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
724 struct vxlan_rdst *rd, *nd;
726 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
727 dst_cache_destroy(&rd->dst_cache);
728 kfree(rd);
730 kfree(f);
733 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
735 netdev_dbg(vxlan->dev,
736 "delete %pM\n", f->eth_addr);
738 --vxlan->addrcnt;
739 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
741 hlist_del_rcu(&f->hlist);
742 call_rcu(&f->rcu, vxlan_fdb_free);
745 static void vxlan_dst_free(struct rcu_head *head)
747 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
749 dst_cache_destroy(&rd->dst_cache);
750 kfree(rd);
753 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
754 struct vxlan_rdst *rd)
756 list_del_rcu(&rd->list);
757 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
758 call_rcu(&rd->rcu, vxlan_dst_free);
761 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
762 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
763 __be32 *vni, u32 *ifindex)
765 struct net *net = dev_net(vxlan->dev);
766 int err;
768 if (tb[NDA_DST]) {
769 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
770 if (err)
771 return err;
772 } else {
773 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
774 if (remote->sa.sa_family == AF_INET) {
775 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
776 ip->sa.sa_family = AF_INET;
777 #if IS_ENABLED(CONFIG_IPV6)
778 } else {
779 ip->sin6.sin6_addr = in6addr_any;
780 ip->sa.sa_family = AF_INET6;
781 #endif
785 if (tb[NDA_PORT]) {
786 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
787 return -EINVAL;
788 *port = nla_get_be16(tb[NDA_PORT]);
789 } else {
790 *port = vxlan->cfg.dst_port;
793 if (tb[NDA_VNI]) {
794 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
795 return -EINVAL;
796 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
797 } else {
798 *vni = vxlan->default_dst.remote_vni;
801 if (tb[NDA_SRC_VNI]) {
802 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
803 return -EINVAL;
804 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
805 } else {
806 *src_vni = vxlan->default_dst.remote_vni;
809 if (tb[NDA_IFINDEX]) {
810 struct net_device *tdev;
812 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
813 return -EINVAL;
814 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
815 tdev = __dev_get_by_index(net, *ifindex);
816 if (!tdev)
817 return -EADDRNOTAVAIL;
818 } else {
819 *ifindex = 0;
822 return 0;
825 /* Add static entry (via netlink) */
826 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
827 struct net_device *dev,
828 const unsigned char *addr, u16 vid, u16 flags)
830 struct vxlan_dev *vxlan = netdev_priv(dev);
831 /* struct net *net = dev_net(vxlan->dev); */
832 union vxlan_addr ip;
833 __be16 port;
834 __be32 src_vni, vni;
835 u32 ifindex;
836 int err;
838 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
839 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
840 ndm->ndm_state);
841 return -EINVAL;
844 if (tb[NDA_DST] == NULL)
845 return -EINVAL;
847 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
848 if (err)
849 return err;
851 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
852 return -EAFNOSUPPORT;
854 spin_lock_bh(&vxlan->hash_lock);
855 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
856 port, src_vni, vni, ifindex, ndm->ndm_flags);
857 spin_unlock_bh(&vxlan->hash_lock);
859 return err;
862 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
863 const unsigned char *addr, union vxlan_addr ip,
864 __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
865 u16 vid)
867 struct vxlan_fdb *f;
868 struct vxlan_rdst *rd = NULL;
869 int err = -ENOENT;
871 f = vxlan_find_mac(vxlan, addr, src_vni);
872 if (!f)
873 return err;
875 if (!vxlan_addr_any(&ip)) {
876 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
877 if (!rd)
878 goto out;
881 /* remove a destination if it's not the only one on the list,
882 * otherwise destroy the fdb entry
884 if (rd && !list_is_singular(&f->remotes)) {
885 vxlan_fdb_dst_destroy(vxlan, f, rd);
886 goto out;
889 vxlan_fdb_destroy(vxlan, f);
891 out:
892 return 0;
895 /* Delete entry (via netlink) */
896 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
897 struct net_device *dev,
898 const unsigned char *addr, u16 vid)
900 struct vxlan_dev *vxlan = netdev_priv(dev);
901 union vxlan_addr ip;
902 __be32 src_vni, vni;
903 __be16 port;
904 u32 ifindex;
905 int err;
907 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
908 if (err)
909 return err;
911 spin_lock_bh(&vxlan->hash_lock);
912 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
913 vid);
914 spin_unlock_bh(&vxlan->hash_lock);
916 return err;
919 /* Dump forwarding table */
920 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
921 struct net_device *dev,
922 struct net_device *filter_dev, int *idx)
924 struct vxlan_dev *vxlan = netdev_priv(dev);
925 unsigned int h;
926 int err = 0;
928 for (h = 0; h < FDB_HASH_SIZE; ++h) {
929 struct vxlan_fdb *f;
931 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
932 struct vxlan_rdst *rd;
934 list_for_each_entry_rcu(rd, &f->remotes, list) {
935 if (*idx < cb->args[2])
936 goto skip;
938 err = vxlan_fdb_info(skb, vxlan, f,
939 NETLINK_CB(cb->skb).portid,
940 cb->nlh->nlmsg_seq,
941 RTM_NEWNEIGH,
942 NLM_F_MULTI, rd);
943 if (err < 0)
944 goto out;
945 skip:
946 *idx += 1;
950 out:
951 return err;
954 /* Watch incoming packets to learn mapping between Ethernet address
955 * and Tunnel endpoint.
956 * Return true if packet is bogus and should be dropped.
958 static bool vxlan_snoop(struct net_device *dev,
959 union vxlan_addr *src_ip, const u8 *src_mac,
960 __be32 vni)
962 struct vxlan_dev *vxlan = netdev_priv(dev);
963 struct vxlan_fdb *f;
965 f = vxlan_find_mac(vxlan, src_mac, vni);
966 if (likely(f)) {
967 struct vxlan_rdst *rdst = first_remote_rcu(f);
969 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
970 return false;
972 /* Don't migrate static entries, drop packets */
973 if (f->state & NUD_NOARP)
974 return true;
976 if (net_ratelimit())
977 netdev_info(dev,
978 "%pM migrated from %pIS to %pIS\n",
979 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
981 rdst->remote_ip = *src_ip;
982 f->updated = jiffies;
983 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
984 } else {
985 /* learned new entry */
986 spin_lock(&vxlan->hash_lock);
988 /* close off race between vxlan_flush and incoming packets */
989 if (netif_running(dev))
990 vxlan_fdb_create(vxlan, src_mac, src_ip,
991 NUD_REACHABLE,
992 NLM_F_EXCL|NLM_F_CREATE,
993 vxlan->cfg.dst_port,
994 vni,
995 vxlan->default_dst.remote_vni,
996 0, NTF_SELF);
997 spin_unlock(&vxlan->hash_lock);
1000 return false;
1003 /* See if multicast group is already in use by other ID */
1004 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1006 struct vxlan_dev *vxlan;
1007 struct vxlan_sock *sock4;
1008 #if IS_ENABLED(CONFIG_IPV6)
1009 struct vxlan_sock *sock6;
1010 #endif
1011 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1013 sock4 = rtnl_dereference(dev->vn4_sock);
1015 /* The vxlan_sock is only used by dev, leaving group has
1016 * no effect on other vxlan devices.
1018 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
1019 return false;
1020 #if IS_ENABLED(CONFIG_IPV6)
1021 sock6 = rtnl_dereference(dev->vn6_sock);
1022 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
1023 return false;
1024 #endif
1026 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1027 if (!netif_running(vxlan->dev) || vxlan == dev)
1028 continue;
1030 if (family == AF_INET &&
1031 rtnl_dereference(vxlan->vn4_sock) != sock4)
1032 continue;
1033 #if IS_ENABLED(CONFIG_IPV6)
1034 if (family == AF_INET6 &&
1035 rtnl_dereference(vxlan->vn6_sock) != sock6)
1036 continue;
1037 #endif
1039 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1040 &dev->default_dst.remote_ip))
1041 continue;
1043 if (vxlan->default_dst.remote_ifindex !=
1044 dev->default_dst.remote_ifindex)
1045 continue;
1047 return true;
1050 return false;
1053 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1055 struct vxlan_net *vn;
1057 if (!vs)
1058 return false;
1059 if (!atomic_dec_and_test(&vs->refcnt))
1060 return false;
1062 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1063 spin_lock(&vn->sock_lock);
1064 hlist_del_rcu(&vs->hlist);
1065 udp_tunnel_notify_del_rx_port(vs->sock,
1066 (vs->flags & VXLAN_F_GPE) ?
1067 UDP_TUNNEL_TYPE_VXLAN_GPE :
1068 UDP_TUNNEL_TYPE_VXLAN);
1069 spin_unlock(&vn->sock_lock);
1071 return true;
1074 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1076 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1077 #if IS_ENABLED(CONFIG_IPV6)
1078 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1080 rcu_assign_pointer(vxlan->vn6_sock, NULL);
1081 #endif
1083 rcu_assign_pointer(vxlan->vn4_sock, NULL);
1084 synchronize_net();
1086 vxlan_vs_del_dev(vxlan);
1088 if (__vxlan_sock_release_prep(sock4)) {
1089 udp_tunnel_sock_release(sock4->sock);
1090 kfree(sock4);
1093 #if IS_ENABLED(CONFIG_IPV6)
1094 if (__vxlan_sock_release_prep(sock6)) {
1095 udp_tunnel_sock_release(sock6->sock);
1096 kfree(sock6);
1098 #endif
1101 /* Update multicast group membership when first VNI on
1102 * multicast address is brought up
1104 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1106 struct sock *sk;
1107 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1108 int ifindex = vxlan->default_dst.remote_ifindex;
1109 int ret = -EINVAL;
1111 if (ip->sa.sa_family == AF_INET) {
1112 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1113 struct ip_mreqn mreq = {
1114 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1115 .imr_ifindex = ifindex,
1118 sk = sock4->sock->sk;
1119 lock_sock(sk);
1120 ret = ip_mc_join_group(sk, &mreq);
1121 release_sock(sk);
1122 #if IS_ENABLED(CONFIG_IPV6)
1123 } else {
1124 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1126 sk = sock6->sock->sk;
1127 lock_sock(sk);
1128 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1129 &ip->sin6.sin6_addr);
1130 release_sock(sk);
1131 #endif
1134 return ret;
1137 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1138 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1140 struct sock *sk;
1141 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1142 int ifindex = vxlan->default_dst.remote_ifindex;
1143 int ret = -EINVAL;
1145 if (ip->sa.sa_family == AF_INET) {
1146 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1147 struct ip_mreqn mreq = {
1148 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1149 .imr_ifindex = ifindex,
1152 sk = sock4->sock->sk;
1153 lock_sock(sk);
1154 ret = ip_mc_leave_group(sk, &mreq);
1155 release_sock(sk);
1156 #if IS_ENABLED(CONFIG_IPV6)
1157 } else {
1158 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1160 sk = sock6->sock->sk;
1161 lock_sock(sk);
1162 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1163 &ip->sin6.sin6_addr);
1164 release_sock(sk);
1165 #endif
1168 return ret;
1171 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1172 struct sk_buff *skb, u32 vxflags)
1174 size_t start, offset;
1176 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1177 goto out;
1179 start = vxlan_rco_start(unparsed->vx_vni);
1180 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1182 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1183 return false;
1185 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1186 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1187 out:
1188 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1189 unparsed->vx_vni &= VXLAN_VNI_MASK;
1190 return true;
1193 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1194 struct sk_buff *skb, u32 vxflags,
1195 struct vxlan_metadata *md)
1197 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1198 struct metadata_dst *tun_dst;
1200 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1201 goto out;
1203 md->gbp = ntohs(gbp->policy_id);
1205 tun_dst = (struct metadata_dst *)skb_dst(skb);
1206 if (tun_dst) {
1207 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1208 tun_dst->u.tun_info.options_len = sizeof(*md);
1210 if (gbp->dont_learn)
1211 md->gbp |= VXLAN_GBP_DONT_LEARN;
1213 if (gbp->policy_applied)
1214 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1216 /* In flow-based mode, GBP is carried in dst_metadata */
1217 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1218 skb->mark = md->gbp;
1219 out:
1220 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1223 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1224 __be16 *protocol,
1225 struct sk_buff *skb, u32 vxflags)
1227 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1229 /* Need to have Next Protocol set for interfaces in GPE mode. */
1230 if (!gpe->np_applied)
1231 return false;
1232 /* "The initial version is 0. If a receiver does not support the
1233 * version indicated it MUST drop the packet.
1235 if (gpe->version != 0)
1236 return false;
1237 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1238 * processing MUST occur." However, we don't implement OAM
1239 * processing, thus drop the packet.
1241 if (gpe->oam_flag)
1242 return false;
1244 switch (gpe->next_protocol) {
1245 case VXLAN_GPE_NP_IPV4:
1246 *protocol = htons(ETH_P_IP);
1247 break;
1248 case VXLAN_GPE_NP_IPV6:
1249 *protocol = htons(ETH_P_IPV6);
1250 break;
1251 case VXLAN_GPE_NP_ETHERNET:
1252 *protocol = htons(ETH_P_TEB);
1253 break;
1254 default:
1255 return false;
1258 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1259 return true;
1262 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1263 struct vxlan_sock *vs,
1264 struct sk_buff *skb, __be32 vni)
1266 union vxlan_addr saddr;
1268 skb_reset_mac_header(skb);
1269 skb->protocol = eth_type_trans(skb, vxlan->dev);
1270 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1272 /* Ignore packet loops (and multicast echo) */
1273 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1274 return false;
1276 /* Get address from the outer IP header */
1277 if (vxlan_get_sk_family(vs) == AF_INET) {
1278 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1279 saddr.sa.sa_family = AF_INET;
1280 #if IS_ENABLED(CONFIG_IPV6)
1281 } else {
1282 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1283 saddr.sa.sa_family = AF_INET6;
1284 #endif
1287 if ((vxlan->flags & VXLAN_F_LEARN) &&
1288 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, vni))
1289 return false;
1291 return true;
1294 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1295 struct sk_buff *skb)
1297 int err = 0;
1299 if (vxlan_get_sk_family(vs) == AF_INET)
1300 err = IP_ECN_decapsulate(oiph, skb);
1301 #if IS_ENABLED(CONFIG_IPV6)
1302 else
1303 err = IP6_ECN_decapsulate(oiph, skb);
1304 #endif
1306 if (unlikely(err) && log_ecn_error) {
1307 if (vxlan_get_sk_family(vs) == AF_INET)
1308 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1309 &((struct iphdr *)oiph)->saddr,
1310 ((struct iphdr *)oiph)->tos);
1311 else
1312 net_info_ratelimited("non-ECT from %pI6\n",
1313 &((struct ipv6hdr *)oiph)->saddr);
1315 return err <= 1;
1318 /* Callback from net/ipv4/udp.c to receive packets */
1319 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1321 struct pcpu_sw_netstats *stats;
1322 struct vxlan_dev *vxlan;
1323 struct vxlan_sock *vs;
1324 struct vxlanhdr unparsed;
1325 struct vxlan_metadata _md;
1326 struct vxlan_metadata *md = &_md;
1327 __be16 protocol = htons(ETH_P_TEB);
1328 bool raw_proto = false;
1329 void *oiph;
1330 __be32 vni = 0;
1332 /* Need UDP and VXLAN header to be present */
1333 if (!pskb_may_pull(skb, VXLAN_HLEN))
1334 goto drop;
1336 unparsed = *vxlan_hdr(skb);
1337 /* VNI flag always required to be set */
1338 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1339 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1340 ntohl(vxlan_hdr(skb)->vx_flags),
1341 ntohl(vxlan_hdr(skb)->vx_vni));
1342 /* Return non vxlan pkt */
1343 goto drop;
1345 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1346 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1348 vs = rcu_dereference_sk_user_data(sk);
1349 if (!vs)
1350 goto drop;
1352 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1354 vxlan = vxlan_vs_find_vni(vs, vni);
1355 if (!vxlan)
1356 goto drop;
1358 /* For backwards compatibility, only allow reserved fields to be
1359 * used by VXLAN extensions if explicitly requested.
1361 if (vs->flags & VXLAN_F_GPE) {
1362 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1363 goto drop;
1364 raw_proto = true;
1367 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1368 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1369 goto drop;
1371 if (vxlan_collect_metadata(vs)) {
1372 struct metadata_dst *tun_dst;
1374 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1375 key32_to_tunnel_id(vni), sizeof(*md));
1377 if (!tun_dst)
1378 goto drop;
1380 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1382 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1383 } else {
1384 memset(md, 0, sizeof(*md));
1387 if (vs->flags & VXLAN_F_REMCSUM_RX)
1388 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1389 goto drop;
1390 if (vs->flags & VXLAN_F_GBP)
1391 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1392 /* Note that GBP and GPE can never be active together. This is
1393 * ensured in vxlan_dev_configure.
1396 if (unparsed.vx_flags || unparsed.vx_vni) {
1397 /* If there are any unprocessed flags remaining treat
1398 * this as a malformed packet. This behavior diverges from
1399 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1400 * in reserved fields are to be ignored. The approach here
1401 * maintains compatibility with previous stack code, and also
1402 * is more robust and provides a little more security in
1403 * adding extensions to VXLAN.
1405 goto drop;
1408 if (!raw_proto) {
1409 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1410 goto drop;
1411 } else {
1412 skb_reset_mac_header(skb);
1413 skb->dev = vxlan->dev;
1414 skb->pkt_type = PACKET_HOST;
1417 oiph = skb_network_header(skb);
1418 skb_reset_network_header(skb);
1420 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1421 ++vxlan->dev->stats.rx_frame_errors;
1422 ++vxlan->dev->stats.rx_errors;
1423 goto drop;
1426 stats = this_cpu_ptr(vxlan->dev->tstats);
1427 u64_stats_update_begin(&stats->syncp);
1428 stats->rx_packets++;
1429 stats->rx_bytes += skb->len;
1430 u64_stats_update_end(&stats->syncp);
1432 gro_cells_receive(&vxlan->gro_cells, skb);
1433 return 0;
1435 drop:
1436 /* Consume bad packet */
1437 kfree_skb(skb);
1438 return 0;
1441 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1443 struct vxlan_dev *vxlan = netdev_priv(dev);
1444 struct arphdr *parp;
1445 u8 *arpptr, *sha;
1446 __be32 sip, tip;
1447 struct neighbour *n;
1449 if (dev->flags & IFF_NOARP)
1450 goto out;
1452 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1453 dev->stats.tx_dropped++;
1454 goto out;
1456 parp = arp_hdr(skb);
1458 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1459 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1460 parp->ar_pro != htons(ETH_P_IP) ||
1461 parp->ar_op != htons(ARPOP_REQUEST) ||
1462 parp->ar_hln != dev->addr_len ||
1463 parp->ar_pln != 4)
1464 goto out;
1465 arpptr = (u8 *)parp + sizeof(struct arphdr);
1466 sha = arpptr;
1467 arpptr += dev->addr_len; /* sha */
1468 memcpy(&sip, arpptr, sizeof(sip));
1469 arpptr += sizeof(sip);
1470 arpptr += dev->addr_len; /* tha */
1471 memcpy(&tip, arpptr, sizeof(tip));
1473 if (ipv4_is_loopback(tip) ||
1474 ipv4_is_multicast(tip))
1475 goto out;
1477 n = neigh_lookup(&arp_tbl, &tip, dev);
1479 if (n) {
1480 struct vxlan_fdb *f;
1481 struct sk_buff *reply;
1483 if (!(n->nud_state & NUD_CONNECTED)) {
1484 neigh_release(n);
1485 goto out;
1488 f = vxlan_find_mac(vxlan, n->ha, vni);
1489 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1490 /* bridge-local neighbor */
1491 neigh_release(n);
1492 goto out;
1495 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1496 n->ha, sha);
1498 neigh_release(n);
1500 if (reply == NULL)
1501 goto out;
1503 skb_reset_mac_header(reply);
1504 __skb_pull(reply, skb_network_offset(reply));
1505 reply->ip_summed = CHECKSUM_UNNECESSARY;
1506 reply->pkt_type = PACKET_HOST;
1508 if (netif_rx_ni(reply) == NET_RX_DROP)
1509 dev->stats.rx_dropped++;
1510 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1511 union vxlan_addr ipa = {
1512 .sin.sin_addr.s_addr = tip,
1513 .sin.sin_family = AF_INET,
1516 vxlan_ip_miss(dev, &ipa);
1518 out:
1519 consume_skb(skb);
1520 return NETDEV_TX_OK;
1523 #if IS_ENABLED(CONFIG_IPV6)
1524 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1525 struct neighbour *n, bool isrouter)
1527 struct net_device *dev = request->dev;
1528 struct sk_buff *reply;
1529 struct nd_msg *ns, *na;
1530 struct ipv6hdr *pip6;
1531 u8 *daddr;
1532 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1533 int ns_olen;
1534 int i, len;
1536 if (dev == NULL)
1537 return NULL;
1539 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1540 sizeof(*na) + na_olen + dev->needed_tailroom;
1541 reply = alloc_skb(len, GFP_ATOMIC);
1542 if (reply == NULL)
1543 return NULL;
1545 reply->protocol = htons(ETH_P_IPV6);
1546 reply->dev = dev;
1547 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1548 skb_push(reply, sizeof(struct ethhdr));
1549 skb_reset_mac_header(reply);
1551 ns = (struct nd_msg *)skb_transport_header(request);
1553 daddr = eth_hdr(request)->h_source;
1554 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1555 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1556 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1557 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1558 break;
1562 /* Ethernet header */
1563 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1564 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1565 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1566 reply->protocol = htons(ETH_P_IPV6);
1568 skb_pull(reply, sizeof(struct ethhdr));
1569 skb_reset_network_header(reply);
1570 skb_put(reply, sizeof(struct ipv6hdr));
1572 /* IPv6 header */
1574 pip6 = ipv6_hdr(reply);
1575 memset(pip6, 0, sizeof(struct ipv6hdr));
1576 pip6->version = 6;
1577 pip6->priority = ipv6_hdr(request)->priority;
1578 pip6->nexthdr = IPPROTO_ICMPV6;
1579 pip6->hop_limit = 255;
1580 pip6->daddr = ipv6_hdr(request)->saddr;
1581 pip6->saddr = *(struct in6_addr *)n->primary_key;
1583 skb_pull(reply, sizeof(struct ipv6hdr));
1584 skb_reset_transport_header(reply);
1586 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1588 /* Neighbor Advertisement */
1589 memset(na, 0, sizeof(*na)+na_olen);
1590 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1591 na->icmph.icmp6_router = isrouter;
1592 na->icmph.icmp6_override = 1;
1593 na->icmph.icmp6_solicited = 1;
1594 na->target = ns->target;
1595 ether_addr_copy(&na->opt[2], n->ha);
1596 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1597 na->opt[1] = na_olen >> 3;
1599 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1600 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1601 csum_partial(na, sizeof(*na)+na_olen, 0));
1603 pip6->payload_len = htons(sizeof(*na)+na_olen);
1605 skb_push(reply, sizeof(struct ipv6hdr));
1607 reply->ip_summed = CHECKSUM_UNNECESSARY;
1609 return reply;
1612 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1614 struct vxlan_dev *vxlan = netdev_priv(dev);
1615 struct nd_msg *msg;
1616 const struct ipv6hdr *iphdr;
1617 const struct in6_addr *daddr;
1618 struct neighbour *n;
1619 struct inet6_dev *in6_dev;
1621 in6_dev = __in6_dev_get(dev);
1622 if (!in6_dev)
1623 goto out;
1625 iphdr = ipv6_hdr(skb);
1626 daddr = &iphdr->daddr;
1628 msg = (struct nd_msg *)skb_transport_header(skb);
1629 if (msg->icmph.icmp6_code != 0 ||
1630 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1631 goto out;
1633 if (ipv6_addr_loopback(daddr) ||
1634 ipv6_addr_is_multicast(&msg->target))
1635 goto out;
1637 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1639 if (n) {
1640 struct vxlan_fdb *f;
1641 struct sk_buff *reply;
1643 if (!(n->nud_state & NUD_CONNECTED)) {
1644 neigh_release(n);
1645 goto out;
1648 f = vxlan_find_mac(vxlan, n->ha, vni);
1649 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1650 /* bridge-local neighbor */
1651 neigh_release(n);
1652 goto out;
1655 reply = vxlan_na_create(skb, n,
1656 !!(f ? f->flags & NTF_ROUTER : 0));
1658 neigh_release(n);
1660 if (reply == NULL)
1661 goto out;
1663 if (netif_rx_ni(reply) == NET_RX_DROP)
1664 dev->stats.rx_dropped++;
1666 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1667 union vxlan_addr ipa = {
1668 .sin6.sin6_addr = msg->target,
1669 .sin6.sin6_family = AF_INET6,
1672 vxlan_ip_miss(dev, &ipa);
1675 out:
1676 consume_skb(skb);
1677 return NETDEV_TX_OK;
1679 #endif
1681 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1683 struct vxlan_dev *vxlan = netdev_priv(dev);
1684 struct neighbour *n;
1686 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1687 return false;
1689 n = NULL;
1690 switch (ntohs(eth_hdr(skb)->h_proto)) {
1691 case ETH_P_IP:
1693 struct iphdr *pip;
1695 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1696 return false;
1697 pip = ip_hdr(skb);
1698 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1699 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1700 union vxlan_addr ipa = {
1701 .sin.sin_addr.s_addr = pip->daddr,
1702 .sin.sin_family = AF_INET,
1705 vxlan_ip_miss(dev, &ipa);
1706 return false;
1709 break;
1711 #if IS_ENABLED(CONFIG_IPV6)
1712 case ETH_P_IPV6:
1714 struct ipv6hdr *pip6;
1716 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1717 return false;
1718 pip6 = ipv6_hdr(skb);
1719 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1720 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1721 union vxlan_addr ipa = {
1722 .sin6.sin6_addr = pip6->daddr,
1723 .sin6.sin6_family = AF_INET6,
1726 vxlan_ip_miss(dev, &ipa);
1727 return false;
1730 break;
1732 #endif
1733 default:
1734 return false;
1737 if (n) {
1738 bool diff;
1740 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1741 if (diff) {
1742 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1743 dev->addr_len);
1744 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1746 neigh_release(n);
1747 return diff;
1750 return false;
1753 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1754 struct vxlan_metadata *md)
1756 struct vxlanhdr_gbp *gbp;
1758 if (!md->gbp)
1759 return;
1761 gbp = (struct vxlanhdr_gbp *)vxh;
1762 vxh->vx_flags |= VXLAN_HF_GBP;
1764 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1765 gbp->dont_learn = 1;
1767 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1768 gbp->policy_applied = 1;
1770 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1773 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1774 __be16 protocol)
1776 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1778 gpe->np_applied = 1;
1780 switch (protocol) {
1781 case htons(ETH_P_IP):
1782 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1783 return 0;
1784 case htons(ETH_P_IPV6):
1785 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1786 return 0;
1787 case htons(ETH_P_TEB):
1788 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1789 return 0;
1791 return -EPFNOSUPPORT;
1794 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1795 int iphdr_len, __be32 vni,
1796 struct vxlan_metadata *md, u32 vxflags,
1797 bool udp_sum)
1799 struct vxlanhdr *vxh;
1800 int min_headroom;
1801 int err;
1802 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1803 __be16 inner_protocol = htons(ETH_P_TEB);
1805 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1806 skb->ip_summed == CHECKSUM_PARTIAL) {
1807 int csum_start = skb_checksum_start_offset(skb);
1809 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1810 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1811 (skb->csum_offset == offsetof(struct udphdr, check) ||
1812 skb->csum_offset == offsetof(struct tcphdr, check)))
1813 type |= SKB_GSO_TUNNEL_REMCSUM;
1816 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1817 + VXLAN_HLEN + iphdr_len;
1819 /* Need space for new headers (invalidates iph ptr) */
1820 err = skb_cow_head(skb, min_headroom);
1821 if (unlikely(err))
1822 return err;
1824 err = iptunnel_handle_offloads(skb, type);
1825 if (err)
1826 return err;
1828 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1829 vxh->vx_flags = VXLAN_HF_VNI;
1830 vxh->vx_vni = vxlan_vni_field(vni);
1832 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1833 unsigned int start;
1835 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1836 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1837 vxh->vx_flags |= VXLAN_HF_RCO;
1839 if (!skb_is_gso(skb)) {
1840 skb->ip_summed = CHECKSUM_NONE;
1841 skb->encapsulation = 0;
1845 if (vxflags & VXLAN_F_GBP)
1846 vxlan_build_gbp_hdr(vxh, vxflags, md);
1847 if (vxflags & VXLAN_F_GPE) {
1848 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1849 if (err < 0)
1850 return err;
1851 inner_protocol = skb->protocol;
1854 skb_set_inner_protocol(skb, inner_protocol);
1855 return 0;
1858 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1859 struct vxlan_sock *sock4,
1860 struct sk_buff *skb, int oif, u8 tos,
1861 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
1862 struct dst_cache *dst_cache,
1863 const struct ip_tunnel_info *info)
1865 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1866 struct rtable *rt = NULL;
1867 struct flowi4 fl4;
1869 if (!sock4)
1870 return ERR_PTR(-EIO);
1872 if (tos && !info)
1873 use_cache = false;
1874 if (use_cache) {
1875 rt = dst_cache_get_ip4(dst_cache, saddr);
1876 if (rt)
1877 return rt;
1880 memset(&fl4, 0, sizeof(fl4));
1881 fl4.flowi4_oif = oif;
1882 fl4.flowi4_tos = RT_TOS(tos);
1883 fl4.flowi4_mark = skb->mark;
1884 fl4.flowi4_proto = IPPROTO_UDP;
1885 fl4.daddr = daddr;
1886 fl4.saddr = *saddr;
1887 fl4.fl4_dport = dport;
1888 fl4.fl4_sport = sport;
1890 rt = ip_route_output_key(vxlan->net, &fl4);
1891 if (likely(!IS_ERR(rt))) {
1892 if (rt->dst.dev == dev) {
1893 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1894 ip_rt_put(rt);
1895 return ERR_PTR(-ELOOP);
1898 *saddr = fl4.saddr;
1899 if (use_cache)
1900 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1901 } else {
1902 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1903 return ERR_PTR(-ENETUNREACH);
1905 return rt;
1908 #if IS_ENABLED(CONFIG_IPV6)
1909 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
1910 struct net_device *dev,
1911 struct vxlan_sock *sock6,
1912 struct sk_buff *skb, int oif, u8 tos,
1913 __be32 label,
1914 const struct in6_addr *daddr,
1915 struct in6_addr *saddr,
1916 __be16 dport, __be16 sport,
1917 struct dst_cache *dst_cache,
1918 const struct ip_tunnel_info *info)
1920 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1921 struct dst_entry *ndst;
1922 struct flowi6 fl6;
1923 int err;
1925 if (!sock6)
1926 return ERR_PTR(-EIO);
1928 if (tos && !info)
1929 use_cache = false;
1930 if (use_cache) {
1931 ndst = dst_cache_get_ip6(dst_cache, saddr);
1932 if (ndst)
1933 return ndst;
1936 memset(&fl6, 0, sizeof(fl6));
1937 fl6.flowi6_oif = oif;
1938 fl6.daddr = *daddr;
1939 fl6.saddr = *saddr;
1940 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
1941 fl6.flowi6_mark = skb->mark;
1942 fl6.flowi6_proto = IPPROTO_UDP;
1943 fl6.fl6_dport = dport;
1944 fl6.fl6_sport = sport;
1946 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1947 sock6->sock->sk,
1948 &ndst, &fl6);
1949 if (unlikely(err < 0)) {
1950 netdev_dbg(dev, "no route to %pI6\n", daddr);
1951 return ERR_PTR(-ENETUNREACH);
1954 if (unlikely(ndst->dev == dev)) {
1955 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1956 dst_release(ndst);
1957 return ERR_PTR(-ELOOP);
1960 *saddr = fl6.saddr;
1961 if (use_cache)
1962 dst_cache_set_ip6(dst_cache, ndst, saddr);
1963 return ndst;
1965 #endif
1967 /* Bypass encapsulation if the destination is local */
1968 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1969 struct vxlan_dev *dst_vxlan, __be32 vni)
1971 struct pcpu_sw_netstats *tx_stats, *rx_stats;
1972 union vxlan_addr loopback;
1973 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1974 struct net_device *dev = skb->dev;
1975 int len = skb->len;
1977 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1978 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1979 skb->pkt_type = PACKET_HOST;
1980 skb->encapsulation = 0;
1981 skb->dev = dst_vxlan->dev;
1982 __skb_pull(skb, skb_network_offset(skb));
1984 if (remote_ip->sa.sa_family == AF_INET) {
1985 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1986 loopback.sa.sa_family = AF_INET;
1987 #if IS_ENABLED(CONFIG_IPV6)
1988 } else {
1989 loopback.sin6.sin6_addr = in6addr_loopback;
1990 loopback.sa.sa_family = AF_INET6;
1991 #endif
1994 if (dst_vxlan->flags & VXLAN_F_LEARN)
1995 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, vni);
1997 u64_stats_update_begin(&tx_stats->syncp);
1998 tx_stats->tx_packets++;
1999 tx_stats->tx_bytes += len;
2000 u64_stats_update_end(&tx_stats->syncp);
2002 if (netif_rx(skb) == NET_RX_SUCCESS) {
2003 u64_stats_update_begin(&rx_stats->syncp);
2004 rx_stats->rx_packets++;
2005 rx_stats->rx_bytes += len;
2006 u64_stats_update_end(&rx_stats->syncp);
2007 } else {
2008 dev->stats.rx_dropped++;
2012 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2013 struct vxlan_dev *vxlan, union vxlan_addr *daddr,
2014 __be16 dst_port, __be32 vni, struct dst_entry *dst,
2015 u32 rt_flags)
2017 #if IS_ENABLED(CONFIG_IPV6)
2018 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2019 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2020 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2022 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2023 #endif
2024 /* Bypass encapsulation if the destination is local */
2025 if (rt_flags & RTCF_LOCAL &&
2026 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2027 struct vxlan_dev *dst_vxlan;
2029 dst_release(dst);
2030 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2031 daddr->sa.sa_family, dst_port,
2032 vxlan->flags);
2033 if (!dst_vxlan) {
2034 dev->stats.tx_errors++;
2035 kfree_skb(skb);
2037 return -ENOENT;
2039 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2040 return 1;
2043 return 0;
2046 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2047 __be32 default_vni, struct vxlan_rdst *rdst,
2048 bool did_rsc)
2050 struct dst_cache *dst_cache;
2051 struct ip_tunnel_info *info;
2052 struct vxlan_dev *vxlan = netdev_priv(dev);
2053 const struct iphdr *old_iph = ip_hdr(skb);
2054 union vxlan_addr *dst;
2055 union vxlan_addr remote_ip, local_ip;
2056 struct vxlan_metadata _md;
2057 struct vxlan_metadata *md = &_md;
2058 __be16 src_port = 0, dst_port;
2059 struct dst_entry *ndst = NULL;
2060 __be32 vni, label;
2061 __u8 tos, ttl;
2062 int err;
2063 u32 flags = vxlan->flags;
2064 bool udp_sum = false;
2065 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2067 info = skb_tunnel_info(skb);
2069 if (rdst) {
2070 dst = &rdst->remote_ip;
2071 if (vxlan_addr_any(dst)) {
2072 if (did_rsc) {
2073 /* short-circuited back to local bridge */
2074 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2075 return;
2077 goto drop;
2080 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2081 vni = (rdst->remote_vni) ? : default_vni;
2082 local_ip = vxlan->cfg.saddr;
2083 dst_cache = &rdst->dst_cache;
2084 md->gbp = skb->mark;
2085 ttl = vxlan->cfg.ttl;
2086 if (!ttl && vxlan_addr_multicast(dst))
2087 ttl = 1;
2089 tos = vxlan->cfg.tos;
2090 if (tos == 1)
2091 tos = ip_tunnel_get_dsfield(old_iph, skb);
2093 if (dst->sa.sa_family == AF_INET)
2094 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2095 else
2096 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2097 label = vxlan->cfg.label;
2098 } else {
2099 if (!info) {
2100 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2101 dev->name);
2102 goto drop;
2104 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2105 if (remote_ip.sa.sa_family == AF_INET) {
2106 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2107 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2108 } else {
2109 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2110 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2112 dst = &remote_ip;
2113 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2114 vni = tunnel_id_to_key32(info->key.tun_id);
2115 dst_cache = &info->dst_cache;
2116 if (info->options_len)
2117 md = ip_tunnel_info_opts(info);
2118 ttl = info->key.ttl;
2119 tos = info->key.tos;
2120 label = info->key.label;
2121 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2123 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2124 vxlan->cfg.port_max, true);
2126 rcu_read_lock();
2127 if (dst->sa.sa_family == AF_INET) {
2128 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2129 struct rtable *rt;
2130 __be16 df = 0;
2132 rt = vxlan_get_route(vxlan, dev, sock4, skb,
2133 rdst ? rdst->remote_ifindex : 0, tos,
2134 dst->sin.sin_addr.s_addr,
2135 &local_ip.sin.sin_addr.s_addr,
2136 dst_port, src_port,
2137 dst_cache, info);
2138 if (IS_ERR(rt)) {
2139 err = PTR_ERR(rt);
2140 goto tx_error;
2143 /* Bypass encapsulation if the destination is local */
2144 if (!info) {
2145 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2146 dst_port, vni, &rt->dst,
2147 rt->rt_flags);
2148 if (err)
2149 goto out_unlock;
2150 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2151 df = htons(IP_DF);
2154 ndst = &rt->dst;
2155 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2156 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2157 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2158 vni, md, flags, udp_sum);
2159 if (err < 0)
2160 goto tx_error;
2162 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2163 dst->sin.sin_addr.s_addr, tos, ttl, df,
2164 src_port, dst_port, xnet, !udp_sum);
2165 #if IS_ENABLED(CONFIG_IPV6)
2166 } else {
2167 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2169 ndst = vxlan6_get_route(vxlan, dev, sock6, skb,
2170 rdst ? rdst->remote_ifindex : 0, tos,
2171 label, &dst->sin6.sin6_addr,
2172 &local_ip.sin6.sin6_addr,
2173 dst_port, src_port,
2174 dst_cache, info);
2175 if (IS_ERR(ndst)) {
2176 err = PTR_ERR(ndst);
2177 ndst = NULL;
2178 goto tx_error;
2181 if (!info) {
2182 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2184 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2185 dst_port, vni, ndst,
2186 rt6i_flags);
2187 if (err)
2188 goto out_unlock;
2191 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2192 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2193 skb_scrub_packet(skb, xnet);
2194 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2195 vni, md, flags, udp_sum);
2196 if (err < 0)
2197 goto tx_error;
2199 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2200 &local_ip.sin6.sin6_addr,
2201 &dst->sin6.sin6_addr, tos, ttl,
2202 label, src_port, dst_port, !udp_sum);
2203 #endif
2205 out_unlock:
2206 rcu_read_unlock();
2207 return;
2209 drop:
2210 dev->stats.tx_dropped++;
2211 dev_kfree_skb(skb);
2212 return;
2214 tx_error:
2215 rcu_read_unlock();
2216 if (err == -ELOOP)
2217 dev->stats.collisions++;
2218 else if (err == -ENETUNREACH)
2219 dev->stats.tx_carrier_errors++;
2220 dst_release(ndst);
2221 dev->stats.tx_errors++;
2222 kfree_skb(skb);
2225 /* Transmit local packets over Vxlan
2227 * Outer IP header inherits ECN and DF from inner header.
2228 * Outer UDP destination is the VXLAN assigned port.
2229 * source port is based on hash of flow
2231 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2233 struct vxlan_dev *vxlan = netdev_priv(dev);
2234 const struct ip_tunnel_info *info;
2235 struct ethhdr *eth;
2236 bool did_rsc = false;
2237 struct vxlan_rdst *rdst, *fdst = NULL;
2238 struct vxlan_fdb *f;
2239 __be32 vni = 0;
2241 info = skb_tunnel_info(skb);
2243 skb_reset_mac_header(skb);
2245 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
2246 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2247 info->mode & IP_TUNNEL_INFO_TX) {
2248 vni = tunnel_id_to_key32(info->key.tun_id);
2249 } else {
2250 if (info && info->mode & IP_TUNNEL_INFO_TX)
2251 vxlan_xmit_one(skb, dev, vni, NULL, false);
2252 else
2253 kfree_skb(skb);
2254 return NETDEV_TX_OK;
2258 if (vxlan->flags & VXLAN_F_PROXY) {
2259 eth = eth_hdr(skb);
2260 if (ntohs(eth->h_proto) == ETH_P_ARP)
2261 return arp_reduce(dev, skb, vni);
2262 #if IS_ENABLED(CONFIG_IPV6)
2263 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2264 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2265 + sizeof(struct nd_msg)) &&
2266 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2267 struct nd_msg *msg;
2269 msg = (struct nd_msg *)skb_transport_header(skb);
2270 if (msg->icmph.icmp6_code == 0 &&
2271 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2272 return neigh_reduce(dev, skb, vni);
2274 #endif
2277 eth = eth_hdr(skb);
2278 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2279 did_rsc = false;
2281 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2282 (ntohs(eth->h_proto) == ETH_P_IP ||
2283 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2284 did_rsc = route_shortcircuit(dev, skb);
2285 if (did_rsc)
2286 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2289 if (f == NULL) {
2290 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2291 if (f == NULL) {
2292 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2293 !is_multicast_ether_addr(eth->h_dest))
2294 vxlan_fdb_miss(vxlan, eth->h_dest);
2296 dev->stats.tx_dropped++;
2297 kfree_skb(skb);
2298 return NETDEV_TX_OK;
2302 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2303 struct sk_buff *skb1;
2305 if (!fdst) {
2306 fdst = rdst;
2307 continue;
2309 skb1 = skb_clone(skb, GFP_ATOMIC);
2310 if (skb1)
2311 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2314 if (fdst)
2315 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2316 else
2317 kfree_skb(skb);
2318 return NETDEV_TX_OK;
2321 /* Walk the forwarding table and purge stale entries */
2322 static void vxlan_cleanup(unsigned long arg)
2324 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2325 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2326 unsigned int h;
2328 if (!netif_running(vxlan->dev))
2329 return;
2331 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2332 struct hlist_node *p, *n;
2334 spin_lock_bh(&vxlan->hash_lock);
2335 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2336 struct vxlan_fdb *f
2337 = container_of(p, struct vxlan_fdb, hlist);
2338 unsigned long timeout;
2340 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2341 continue;
2343 timeout = f->used + vxlan->cfg.age_interval * HZ;
2344 if (time_before_eq(timeout, jiffies)) {
2345 netdev_dbg(vxlan->dev,
2346 "garbage collect %pM\n",
2347 f->eth_addr);
2348 f->state = NUD_STALE;
2349 vxlan_fdb_destroy(vxlan, f);
2350 } else if (time_before(timeout, next_timer))
2351 next_timer = timeout;
2353 spin_unlock_bh(&vxlan->hash_lock);
2356 mod_timer(&vxlan->age_timer, next_timer);
2359 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2361 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2363 spin_lock(&vn->sock_lock);
2364 hlist_del_init_rcu(&vxlan->hlist);
2365 spin_unlock(&vn->sock_lock);
2368 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2370 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2371 __be32 vni = vxlan->default_dst.remote_vni;
2373 spin_lock(&vn->sock_lock);
2374 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2375 spin_unlock(&vn->sock_lock);
2378 /* Setup stats when device is created */
2379 static int vxlan_init(struct net_device *dev)
2381 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2382 if (!dev->tstats)
2383 return -ENOMEM;
2385 return 0;
2388 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2390 struct vxlan_fdb *f;
2392 spin_lock_bh(&vxlan->hash_lock);
2393 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2394 if (f)
2395 vxlan_fdb_destroy(vxlan, f);
2396 spin_unlock_bh(&vxlan->hash_lock);
2399 static void vxlan_uninit(struct net_device *dev)
2401 struct vxlan_dev *vxlan = netdev_priv(dev);
2403 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2405 free_percpu(dev->tstats);
2408 /* Start ageing timer and join group when device is brought up */
2409 static int vxlan_open(struct net_device *dev)
2411 struct vxlan_dev *vxlan = netdev_priv(dev);
2412 int ret;
2414 ret = vxlan_sock_add(vxlan);
2415 if (ret < 0)
2416 return ret;
2418 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2419 ret = vxlan_igmp_join(vxlan);
2420 if (ret == -EADDRINUSE)
2421 ret = 0;
2422 if (ret) {
2423 vxlan_sock_release(vxlan);
2424 return ret;
2428 if (vxlan->cfg.age_interval)
2429 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2431 return ret;
2434 /* Purge the forwarding table */
2435 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2437 unsigned int h;
2439 spin_lock_bh(&vxlan->hash_lock);
2440 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2441 struct hlist_node *p, *n;
2442 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2443 struct vxlan_fdb *f
2444 = container_of(p, struct vxlan_fdb, hlist);
2445 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2446 continue;
2447 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2448 if (!is_zero_ether_addr(f->eth_addr))
2449 vxlan_fdb_destroy(vxlan, f);
2452 spin_unlock_bh(&vxlan->hash_lock);
2455 /* Cleanup timer and forwarding table on shutdown */
2456 static int vxlan_stop(struct net_device *dev)
2458 struct vxlan_dev *vxlan = netdev_priv(dev);
2459 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2460 int ret = 0;
2462 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2463 !vxlan_group_used(vn, vxlan))
2464 ret = vxlan_igmp_leave(vxlan);
2466 del_timer_sync(&vxlan->age_timer);
2468 vxlan_flush(vxlan, false);
2469 vxlan_sock_release(vxlan);
2471 return ret;
2474 /* Stub, nothing needs to be done. */
2475 static void vxlan_set_multicast_list(struct net_device *dev)
2479 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2481 struct vxlan_dev *vxlan = netdev_priv(dev);
2482 struct vxlan_rdst *dst = &vxlan->default_dst;
2483 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2484 dst->remote_ifindex);
2485 bool use_ipv6 = false;
2487 if (dst->remote_ip.sa.sa_family == AF_INET6)
2488 use_ipv6 = true;
2490 /* This check is different than dev->max_mtu, because it looks at
2491 * the lowerdev->mtu, rather than the static dev->max_mtu
2493 if (lowerdev) {
2494 int max_mtu = lowerdev->mtu -
2495 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2496 if (new_mtu > max_mtu)
2497 return -EINVAL;
2500 dev->mtu = new_mtu;
2501 return 0;
2504 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2506 struct vxlan_dev *vxlan = netdev_priv(dev);
2507 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2508 __be16 sport, dport;
2510 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2511 vxlan->cfg.port_max, true);
2512 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2514 if (ip_tunnel_info_af(info) == AF_INET) {
2515 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2516 struct rtable *rt;
2518 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
2519 info->key.u.ipv4.dst,
2520 &info->key.u.ipv4.src, dport, sport,
2521 &info->dst_cache, info);
2522 if (IS_ERR(rt))
2523 return PTR_ERR(rt);
2524 ip_rt_put(rt);
2525 } else {
2526 #if IS_ENABLED(CONFIG_IPV6)
2527 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2528 struct dst_entry *ndst;
2530 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
2531 info->key.label, &info->key.u.ipv6.dst,
2532 &info->key.u.ipv6.src, dport, sport,
2533 &info->dst_cache, info);
2534 if (IS_ERR(ndst))
2535 return PTR_ERR(ndst);
2536 dst_release(ndst);
2537 #else /* !CONFIG_IPV6 */
2538 return -EPFNOSUPPORT;
2539 #endif
2541 info->key.tp_src = sport;
2542 info->key.tp_dst = dport;
2543 return 0;
2546 static const struct net_device_ops vxlan_netdev_ether_ops = {
2547 .ndo_init = vxlan_init,
2548 .ndo_uninit = vxlan_uninit,
2549 .ndo_open = vxlan_open,
2550 .ndo_stop = vxlan_stop,
2551 .ndo_start_xmit = vxlan_xmit,
2552 .ndo_get_stats64 = ip_tunnel_get_stats64,
2553 .ndo_set_rx_mode = vxlan_set_multicast_list,
2554 .ndo_change_mtu = vxlan_change_mtu,
2555 .ndo_validate_addr = eth_validate_addr,
2556 .ndo_set_mac_address = eth_mac_addr,
2557 .ndo_fdb_add = vxlan_fdb_add,
2558 .ndo_fdb_del = vxlan_fdb_delete,
2559 .ndo_fdb_dump = vxlan_fdb_dump,
2560 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2563 static const struct net_device_ops vxlan_netdev_raw_ops = {
2564 .ndo_init = vxlan_init,
2565 .ndo_uninit = vxlan_uninit,
2566 .ndo_open = vxlan_open,
2567 .ndo_stop = vxlan_stop,
2568 .ndo_start_xmit = vxlan_xmit,
2569 .ndo_get_stats64 = ip_tunnel_get_stats64,
2570 .ndo_change_mtu = vxlan_change_mtu,
2571 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2574 /* Info for udev, that this is a virtual tunnel endpoint */
2575 static struct device_type vxlan_type = {
2576 .name = "vxlan",
2579 /* Calls the ndo_udp_tunnel_add of the caller in order to
2580 * supply the listening VXLAN udp ports. Callers are expected
2581 * to implement the ndo_udp_tunnel_add.
2583 static void vxlan_push_rx_ports(struct net_device *dev)
2585 struct vxlan_sock *vs;
2586 struct net *net = dev_net(dev);
2587 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2588 unsigned int i;
2590 spin_lock(&vn->sock_lock);
2591 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2592 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2593 udp_tunnel_push_rx_port(dev, vs->sock,
2594 (vs->flags & VXLAN_F_GPE) ?
2595 UDP_TUNNEL_TYPE_VXLAN_GPE :
2596 UDP_TUNNEL_TYPE_VXLAN);
2598 spin_unlock(&vn->sock_lock);
2601 /* Initialize the device structure. */
2602 static void vxlan_setup(struct net_device *dev)
2604 struct vxlan_dev *vxlan = netdev_priv(dev);
2605 unsigned int h;
2607 eth_hw_addr_random(dev);
2608 ether_setup(dev);
2610 dev->needs_free_netdev = true;
2611 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2613 dev->features |= NETIF_F_LLTX;
2614 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2615 dev->features |= NETIF_F_RXCSUM;
2616 dev->features |= NETIF_F_GSO_SOFTWARE;
2618 dev->vlan_features = dev->features;
2619 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2620 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2621 netif_keep_dst(dev);
2622 dev->priv_flags |= IFF_NO_QUEUE;
2624 INIT_LIST_HEAD(&vxlan->next);
2625 spin_lock_init(&vxlan->hash_lock);
2627 init_timer_deferrable(&vxlan->age_timer);
2628 vxlan->age_timer.function = vxlan_cleanup;
2629 vxlan->age_timer.data = (unsigned long) vxlan;
2631 vxlan->cfg.dst_port = htons(vxlan_port);
2633 vxlan->dev = dev;
2635 gro_cells_init(&vxlan->gro_cells, dev);
2637 for (h = 0; h < FDB_HASH_SIZE; ++h)
2638 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2641 static void vxlan_ether_setup(struct net_device *dev)
2643 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2644 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2645 dev->netdev_ops = &vxlan_netdev_ether_ops;
2648 static void vxlan_raw_setup(struct net_device *dev)
2650 dev->header_ops = NULL;
2651 dev->type = ARPHRD_NONE;
2652 dev->hard_header_len = 0;
2653 dev->addr_len = 0;
2654 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2655 dev->netdev_ops = &vxlan_netdev_raw_ops;
2658 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2659 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
2660 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2661 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
2662 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2663 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2664 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
2665 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2666 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2667 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
2668 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2669 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2670 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
2671 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
2672 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2673 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2674 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2675 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
2676 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
2677 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
2678 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2679 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2680 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
2681 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2682 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2683 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
2684 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
2685 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
2688 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2690 if (tb[IFLA_ADDRESS]) {
2691 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2692 pr_debug("invalid link address (not ethernet)\n");
2693 return -EINVAL;
2696 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2697 pr_debug("invalid all zero ethernet address\n");
2698 return -EADDRNOTAVAIL;
2702 if (!data)
2703 return -EINVAL;
2705 if (data[IFLA_VXLAN_ID]) {
2706 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2707 if (id >= VXLAN_N_VID)
2708 return -ERANGE;
2711 if (data[IFLA_VXLAN_PORT_RANGE]) {
2712 const struct ifla_vxlan_port_range *p
2713 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2715 if (ntohs(p->high) < ntohs(p->low)) {
2716 pr_debug("port range %u .. %u not valid\n",
2717 ntohs(p->low), ntohs(p->high));
2718 return -EINVAL;
2722 return 0;
2725 static void vxlan_get_drvinfo(struct net_device *netdev,
2726 struct ethtool_drvinfo *drvinfo)
2728 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2729 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2732 static const struct ethtool_ops vxlan_ethtool_ops = {
2733 .get_drvinfo = vxlan_get_drvinfo,
2734 .get_link = ethtool_op_get_link,
2737 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2738 __be16 port, u32 flags)
2740 struct socket *sock;
2741 struct udp_port_cfg udp_conf;
2742 int err;
2744 memset(&udp_conf, 0, sizeof(udp_conf));
2746 if (ipv6) {
2747 udp_conf.family = AF_INET6;
2748 udp_conf.use_udp6_rx_checksums =
2749 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2750 udp_conf.ipv6_v6only = 1;
2751 } else {
2752 udp_conf.family = AF_INET;
2755 udp_conf.local_udp_port = port;
2757 /* Open UDP socket */
2758 err = udp_sock_create(net, &udp_conf, &sock);
2759 if (err < 0)
2760 return ERR_PTR(err);
2762 return sock;
2765 /* Create new listen socket if needed */
2766 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2767 __be16 port, u32 flags)
2769 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2770 struct vxlan_sock *vs;
2771 struct socket *sock;
2772 unsigned int h;
2773 struct udp_tunnel_sock_cfg tunnel_cfg;
2775 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2776 if (!vs)
2777 return ERR_PTR(-ENOMEM);
2779 for (h = 0; h < VNI_HASH_SIZE; ++h)
2780 INIT_HLIST_HEAD(&vs->vni_list[h]);
2782 sock = vxlan_create_sock(net, ipv6, port, flags);
2783 if (IS_ERR(sock)) {
2784 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2785 PTR_ERR(sock));
2786 kfree(vs);
2787 return ERR_CAST(sock);
2790 vs->sock = sock;
2791 atomic_set(&vs->refcnt, 1);
2792 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2794 spin_lock(&vn->sock_lock);
2795 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2796 udp_tunnel_notify_add_rx_port(sock,
2797 (vs->flags & VXLAN_F_GPE) ?
2798 UDP_TUNNEL_TYPE_VXLAN_GPE :
2799 UDP_TUNNEL_TYPE_VXLAN);
2800 spin_unlock(&vn->sock_lock);
2802 /* Mark socket as an encapsulation socket. */
2803 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
2804 tunnel_cfg.sk_user_data = vs;
2805 tunnel_cfg.encap_type = 1;
2806 tunnel_cfg.encap_rcv = vxlan_rcv;
2807 tunnel_cfg.encap_destroy = NULL;
2808 tunnel_cfg.gro_receive = vxlan_gro_receive;
2809 tunnel_cfg.gro_complete = vxlan_gro_complete;
2811 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2813 return vs;
2816 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
2818 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2819 struct vxlan_sock *vs = NULL;
2821 if (!vxlan->cfg.no_share) {
2822 spin_lock(&vn->sock_lock);
2823 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2824 vxlan->cfg.dst_port, vxlan->flags);
2825 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
2826 spin_unlock(&vn->sock_lock);
2827 return -EBUSY;
2829 spin_unlock(&vn->sock_lock);
2831 if (!vs)
2832 vs = vxlan_socket_create(vxlan->net, ipv6,
2833 vxlan->cfg.dst_port, vxlan->flags);
2834 if (IS_ERR(vs))
2835 return PTR_ERR(vs);
2836 #if IS_ENABLED(CONFIG_IPV6)
2837 if (ipv6)
2838 rcu_assign_pointer(vxlan->vn6_sock, vs);
2839 else
2840 #endif
2841 rcu_assign_pointer(vxlan->vn4_sock, vs);
2842 vxlan_vs_add_dev(vs, vxlan);
2843 return 0;
2846 static int vxlan_sock_add(struct vxlan_dev *vxlan)
2848 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
2849 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
2850 int ret = 0;
2852 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
2853 #if IS_ENABLED(CONFIG_IPV6)
2854 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
2855 if (ipv6 || metadata)
2856 ret = __vxlan_sock_add(vxlan, true);
2857 #endif
2858 if (!ret && (!ipv6 || metadata))
2859 ret = __vxlan_sock_add(vxlan, false);
2860 if (ret < 0)
2861 vxlan_sock_release(vxlan);
2862 return ret;
2865 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
2866 struct vxlan_config *conf,
2867 bool changelink)
2869 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2870 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
2871 struct vxlan_rdst *dst = &vxlan->default_dst;
2872 unsigned short needed_headroom = ETH_HLEN;
2873 bool use_ipv6 = false;
2874 __be16 default_port = vxlan->cfg.dst_port;
2875 struct net_device *lowerdev = NULL;
2877 if (!changelink) {
2878 if (conf->flags & VXLAN_F_GPE) {
2879 /* For now, allow GPE only together with
2880 * COLLECT_METADATA. This can be relaxed later; in such
2881 * case, the other side of the PtP link will have to be
2882 * provided.
2884 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2885 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2886 pr_info("unsupported combination of extensions\n");
2887 return -EINVAL;
2889 vxlan_raw_setup(dev);
2890 } else {
2891 vxlan_ether_setup(dev);
2894 /* MTU range: 68 - 65535 */
2895 dev->min_mtu = ETH_MIN_MTU;
2896 dev->max_mtu = ETH_MAX_MTU;
2897 vxlan->net = src_net;
2900 dst->remote_vni = conf->vni;
2902 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
2904 /* Unless IPv6 is explicitly requested, assume IPv4 */
2905 if (!dst->remote_ip.sa.sa_family)
2906 dst->remote_ip.sa.sa_family = AF_INET;
2908 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
2909 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2910 if (!IS_ENABLED(CONFIG_IPV6))
2911 return -EPFNOSUPPORT;
2912 use_ipv6 = true;
2913 vxlan->flags |= VXLAN_F_IPV6;
2916 if (conf->label && !use_ipv6) {
2917 pr_info("label only supported in use with IPv6\n");
2918 return -EINVAL;
2921 if (conf->remote_ifindex &&
2922 conf->remote_ifindex != vxlan->cfg.remote_ifindex) {
2923 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2924 dst->remote_ifindex = conf->remote_ifindex;
2926 if (!lowerdev) {
2927 pr_info("ifindex %d does not exist\n",
2928 dst->remote_ifindex);
2929 return -ENODEV;
2932 #if IS_ENABLED(CONFIG_IPV6)
2933 if (use_ipv6) {
2934 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2935 if (idev && idev->cnf.disable_ipv6) {
2936 pr_info("IPv6 is disabled via sysctl\n");
2937 return -EPERM;
2940 #endif
2942 if (!conf->mtu)
2943 dev->mtu = lowerdev->mtu -
2944 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2946 needed_headroom = lowerdev->hard_header_len;
2947 } else if (!conf->remote_ifindex &&
2948 vxlan_addr_multicast(&dst->remote_ip)) {
2949 pr_info("multicast destination requires interface to be specified\n");
2950 return -EINVAL;
2953 if (conf->mtu) {
2954 int max_mtu = ETH_MAX_MTU;
2956 if (lowerdev)
2957 max_mtu = lowerdev->mtu;
2959 max_mtu -= (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2961 if (conf->mtu < dev->min_mtu || conf->mtu > dev->max_mtu)
2962 return -EINVAL;
2964 dev->mtu = conf->mtu;
2966 if (conf->mtu > max_mtu)
2967 dev->mtu = max_mtu;
2970 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2971 needed_headroom += VXLAN6_HEADROOM;
2972 else
2973 needed_headroom += VXLAN_HEADROOM;
2974 dev->needed_headroom = needed_headroom;
2976 memcpy(&vxlan->cfg, conf, sizeof(*conf));
2977 if (!vxlan->cfg.dst_port) {
2978 if (conf->flags & VXLAN_F_GPE)
2979 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
2980 else
2981 vxlan->cfg.dst_port = default_port;
2983 vxlan->flags |= conf->flags;
2985 if (!vxlan->cfg.age_interval)
2986 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
2988 if (changelink)
2989 return 0;
2991 list_for_each_entry(tmp, &vn->vxlan_list, next) {
2992 if (tmp->cfg.vni == conf->vni &&
2993 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
2994 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
2995 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
2996 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
2997 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
2998 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
2999 return -EEXIST;
3003 return 0;
3006 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3007 struct vxlan_config *conf)
3009 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3010 struct vxlan_dev *vxlan = netdev_priv(dev);
3011 int err;
3013 err = vxlan_dev_configure(net, dev, conf, false);
3014 if (err)
3015 return err;
3017 dev->ethtool_ops = &vxlan_ethtool_ops;
3019 /* create an fdb entry for a valid default destination */
3020 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3021 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3022 &vxlan->default_dst.remote_ip,
3023 NUD_REACHABLE | NUD_PERMANENT,
3024 NLM_F_EXCL | NLM_F_CREATE,
3025 vxlan->cfg.dst_port,
3026 vxlan->default_dst.remote_vni,
3027 vxlan->default_dst.remote_vni,
3028 vxlan->default_dst.remote_ifindex,
3029 NTF_SELF);
3030 if (err)
3031 return err;
3034 err = register_netdevice(dev);
3035 if (err) {
3036 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3037 return err;
3040 list_add(&vxlan->next, &vn->vxlan_list);
3041 return 0;
3044 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3045 struct net_device *dev, struct vxlan_config *conf,
3046 bool changelink)
3048 struct vxlan_dev *vxlan = netdev_priv(dev);
3050 memset(conf, 0, sizeof(*conf));
3052 /* if changelink operation, start with old existing cfg */
3053 if (changelink)
3054 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3056 if (data[IFLA_VXLAN_ID]) {
3057 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3059 if (changelink && (vni != conf->vni))
3060 return -EOPNOTSUPP;
3061 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3064 if (data[IFLA_VXLAN_GROUP]) {
3065 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3066 } else if (data[IFLA_VXLAN_GROUP6]) {
3067 if (!IS_ENABLED(CONFIG_IPV6))
3068 return -EPFNOSUPPORT;
3070 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3071 conf->remote_ip.sa.sa_family = AF_INET6;
3074 if (data[IFLA_VXLAN_LOCAL]) {
3075 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3076 conf->saddr.sa.sa_family = AF_INET;
3077 } else if (data[IFLA_VXLAN_LOCAL6]) {
3078 if (!IS_ENABLED(CONFIG_IPV6))
3079 return -EPFNOSUPPORT;
3081 /* TODO: respect scope id */
3082 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3083 conf->saddr.sa.sa_family = AF_INET6;
3086 if (data[IFLA_VXLAN_LINK])
3087 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3089 if (data[IFLA_VXLAN_TOS])
3090 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3092 if (data[IFLA_VXLAN_TTL])
3093 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3095 if (data[IFLA_VXLAN_LABEL])
3096 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3097 IPV6_FLOWLABEL_MASK;
3099 if (data[IFLA_VXLAN_LEARNING]) {
3100 if (nla_get_u8(data[IFLA_VXLAN_LEARNING])) {
3101 conf->flags |= VXLAN_F_LEARN;
3102 } else {
3103 conf->flags &= ~VXLAN_F_LEARN;
3104 vxlan->flags &= ~VXLAN_F_LEARN;
3106 } else if (!changelink) {
3107 /* default to learn on a new device */
3108 conf->flags |= VXLAN_F_LEARN;
3111 if (data[IFLA_VXLAN_AGEING]) {
3112 if (changelink)
3113 return -EOPNOTSUPP;
3114 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3117 if (data[IFLA_VXLAN_PROXY]) {
3118 if (changelink)
3119 return -EOPNOTSUPP;
3120 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3121 conf->flags |= VXLAN_F_PROXY;
3124 if (data[IFLA_VXLAN_RSC]) {
3125 if (changelink)
3126 return -EOPNOTSUPP;
3127 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3128 conf->flags |= VXLAN_F_RSC;
3131 if (data[IFLA_VXLAN_L2MISS]) {
3132 if (changelink)
3133 return -EOPNOTSUPP;
3134 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3135 conf->flags |= VXLAN_F_L2MISS;
3138 if (data[IFLA_VXLAN_L3MISS]) {
3139 if (changelink)
3140 return -EOPNOTSUPP;
3141 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3142 conf->flags |= VXLAN_F_L3MISS;
3145 if (data[IFLA_VXLAN_LIMIT]) {
3146 if (changelink)
3147 return -EOPNOTSUPP;
3148 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3151 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3152 if (changelink)
3153 return -EOPNOTSUPP;
3154 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3155 conf->flags |= VXLAN_F_COLLECT_METADATA;
3158 if (data[IFLA_VXLAN_PORT_RANGE]) {
3159 if (!changelink) {
3160 const struct ifla_vxlan_port_range *p
3161 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3162 conf->port_min = ntohs(p->low);
3163 conf->port_max = ntohs(p->high);
3164 } else {
3165 return -EOPNOTSUPP;
3169 if (data[IFLA_VXLAN_PORT]) {
3170 if (changelink)
3171 return -EOPNOTSUPP;
3172 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3175 if (data[IFLA_VXLAN_UDP_CSUM]) {
3176 if (changelink)
3177 return -EOPNOTSUPP;
3178 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3179 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3182 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3183 if (changelink)
3184 return -EOPNOTSUPP;
3185 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3186 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3189 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3190 if (changelink)
3191 return -EOPNOTSUPP;
3192 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3193 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3196 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3197 if (changelink)
3198 return -EOPNOTSUPP;
3199 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3200 conf->flags |= VXLAN_F_REMCSUM_TX;
3203 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3204 if (changelink)
3205 return -EOPNOTSUPP;
3206 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3207 conf->flags |= VXLAN_F_REMCSUM_RX;
3210 if (data[IFLA_VXLAN_GBP]) {
3211 if (changelink)
3212 return -EOPNOTSUPP;
3213 conf->flags |= VXLAN_F_GBP;
3216 if (data[IFLA_VXLAN_GPE]) {
3217 if (changelink)
3218 return -EOPNOTSUPP;
3219 conf->flags |= VXLAN_F_GPE;
3222 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3223 if (changelink)
3224 return -EOPNOTSUPP;
3225 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3228 if (tb[IFLA_MTU]) {
3229 if (changelink)
3230 return -EOPNOTSUPP;
3231 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3234 return 0;
3237 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3238 struct nlattr *tb[], struct nlattr *data[])
3240 struct vxlan_config conf;
3241 int err;
3243 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3244 if (err)
3245 return err;
3247 return __vxlan_dev_create(src_net, dev, &conf);
3250 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3251 struct nlattr *data[])
3253 struct vxlan_dev *vxlan = netdev_priv(dev);
3254 struct vxlan_rdst *dst = &vxlan->default_dst;
3255 struct vxlan_rdst old_dst;
3256 struct vxlan_config conf;
3257 int err;
3259 err = vxlan_nl2conf(tb, data,
3260 dev, &conf, true);
3261 if (err)
3262 return err;
3264 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
3266 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3267 if (err)
3268 return err;
3270 /* handle default dst entry */
3271 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3272 spin_lock_bh(&vxlan->hash_lock);
3273 if (!vxlan_addr_any(&old_dst.remote_ip))
3274 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3275 old_dst.remote_ip,
3276 vxlan->cfg.dst_port,
3277 old_dst.remote_vni,
3278 old_dst.remote_vni,
3279 old_dst.remote_ifindex, 0);
3281 if (!vxlan_addr_any(&dst->remote_ip)) {
3282 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3283 &dst->remote_ip,
3284 NUD_REACHABLE | NUD_PERMANENT,
3285 NLM_F_CREATE | NLM_F_APPEND,
3286 vxlan->cfg.dst_port,
3287 dst->remote_vni,
3288 dst->remote_vni,
3289 dst->remote_ifindex,
3290 NTF_SELF);
3291 if (err) {
3292 spin_unlock_bh(&vxlan->hash_lock);
3293 return err;
3296 spin_unlock_bh(&vxlan->hash_lock);
3299 return 0;
3302 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3304 struct vxlan_dev *vxlan = netdev_priv(dev);
3306 vxlan_flush(vxlan, true);
3308 gro_cells_destroy(&vxlan->gro_cells);
3309 list_del(&vxlan->next);
3310 unregister_netdevice_queue(dev, head);
3313 static size_t vxlan_get_size(const struct net_device *dev)
3316 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
3317 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3318 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3319 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3320 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3321 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
3322 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
3323 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
3324 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3325 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3326 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3327 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
3328 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
3329 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3330 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3331 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3332 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3333 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3334 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3335 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3336 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3337 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3341 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3343 const struct vxlan_dev *vxlan = netdev_priv(dev);
3344 const struct vxlan_rdst *dst = &vxlan->default_dst;
3345 struct ifla_vxlan_port_range ports = {
3346 .low = htons(vxlan->cfg.port_min),
3347 .high = htons(vxlan->cfg.port_max),
3350 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
3351 goto nla_put_failure;
3353 if (!vxlan_addr_any(&dst->remote_ip)) {
3354 if (dst->remote_ip.sa.sa_family == AF_INET) {
3355 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3356 dst->remote_ip.sin.sin_addr.s_addr))
3357 goto nla_put_failure;
3358 #if IS_ENABLED(CONFIG_IPV6)
3359 } else {
3360 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3361 &dst->remote_ip.sin6.sin6_addr))
3362 goto nla_put_failure;
3363 #endif
3367 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
3368 goto nla_put_failure;
3370 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3371 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
3372 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
3373 vxlan->cfg.saddr.sin.sin_addr.s_addr))
3374 goto nla_put_failure;
3375 #if IS_ENABLED(CONFIG_IPV6)
3376 } else {
3377 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
3378 &vxlan->cfg.saddr.sin6.sin6_addr))
3379 goto nla_put_failure;
3380 #endif
3384 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3385 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
3386 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
3387 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3388 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3389 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3390 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3391 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3392 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3393 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3394 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3395 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
3396 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3397 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
3398 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3399 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3400 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
3401 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
3402 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
3403 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3404 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3405 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3406 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3407 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3408 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3409 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3410 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
3411 goto nla_put_failure;
3413 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3414 goto nla_put_failure;
3416 if (vxlan->flags & VXLAN_F_GBP &&
3417 nla_put_flag(skb, IFLA_VXLAN_GBP))
3418 goto nla_put_failure;
3420 if (vxlan->flags & VXLAN_F_GPE &&
3421 nla_put_flag(skb, IFLA_VXLAN_GPE))
3422 goto nla_put_failure;
3424 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3425 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3426 goto nla_put_failure;
3428 return 0;
3430 nla_put_failure:
3431 return -EMSGSIZE;
3434 static struct net *vxlan_get_link_net(const struct net_device *dev)
3436 struct vxlan_dev *vxlan = netdev_priv(dev);
3438 return vxlan->net;
3441 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3442 .kind = "vxlan",
3443 .maxtype = IFLA_VXLAN_MAX,
3444 .policy = vxlan_policy,
3445 .priv_size = sizeof(struct vxlan_dev),
3446 .setup = vxlan_setup,
3447 .validate = vxlan_validate,
3448 .newlink = vxlan_newlink,
3449 .changelink = vxlan_changelink,
3450 .dellink = vxlan_dellink,
3451 .get_size = vxlan_get_size,
3452 .fill_info = vxlan_fill_info,
3453 .get_link_net = vxlan_get_link_net,
3456 struct net_device *vxlan_dev_create(struct net *net, const char *name,
3457 u8 name_assign_type,
3458 struct vxlan_config *conf)
3460 struct nlattr *tb[IFLA_MAX + 1];
3461 struct net_device *dev;
3462 int err;
3464 memset(&tb, 0, sizeof(tb));
3466 dev = rtnl_create_link(net, name, name_assign_type,
3467 &vxlan_link_ops, tb);
3468 if (IS_ERR(dev))
3469 return dev;
3471 err = __vxlan_dev_create(net, dev, conf);
3472 if (err < 0) {
3473 free_netdev(dev);
3474 return ERR_PTR(err);
3477 err = rtnl_configure_link(dev, NULL);
3478 if (err < 0) {
3479 LIST_HEAD(list_kill);
3481 vxlan_dellink(dev, &list_kill);
3482 unregister_netdevice_many(&list_kill);
3483 return ERR_PTR(err);
3486 return dev;
3488 EXPORT_SYMBOL_GPL(vxlan_dev_create);
3490 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3491 struct net_device *dev)
3493 struct vxlan_dev *vxlan, *next;
3494 LIST_HEAD(list_kill);
3496 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3497 struct vxlan_rdst *dst = &vxlan->default_dst;
3499 /* In case we created vxlan device with carrier
3500 * and we loose the carrier due to module unload
3501 * we also need to remove vxlan device. In other
3502 * cases, it's not necessary and remote_ifindex
3503 * is 0 here, so no matches.
3505 if (dst->remote_ifindex == dev->ifindex)
3506 vxlan_dellink(vxlan->dev, &list_kill);
3509 unregister_netdevice_many(&list_kill);
3512 static int vxlan_netdevice_event(struct notifier_block *unused,
3513 unsigned long event, void *ptr)
3515 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3516 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3518 if (event == NETDEV_UNREGISTER)
3519 vxlan_handle_lowerdev_unregister(vn, dev);
3520 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
3521 vxlan_push_rx_ports(dev);
3523 return NOTIFY_DONE;
3526 static struct notifier_block vxlan_notifier_block __read_mostly = {
3527 .notifier_call = vxlan_netdevice_event,
3530 static __net_init int vxlan_init_net(struct net *net)
3532 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3533 unsigned int h;
3535 INIT_LIST_HEAD(&vn->vxlan_list);
3536 spin_lock_init(&vn->sock_lock);
3538 for (h = 0; h < PORT_HASH_SIZE; ++h)
3539 INIT_HLIST_HEAD(&vn->sock_list[h]);
3541 return 0;
3544 static void __net_exit vxlan_exit_net(struct net *net)
3546 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3547 struct vxlan_dev *vxlan, *next;
3548 struct net_device *dev, *aux;
3549 LIST_HEAD(list);
3551 rtnl_lock();
3552 for_each_netdev_safe(net, dev, aux)
3553 if (dev->rtnl_link_ops == &vxlan_link_ops)
3554 unregister_netdevice_queue(dev, &list);
3556 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3557 /* If vxlan->dev is in the same netns, it has already been added
3558 * to the list by the previous loop.
3560 if (!net_eq(dev_net(vxlan->dev), net)) {
3561 gro_cells_destroy(&vxlan->gro_cells);
3562 unregister_netdevice_queue(vxlan->dev, &list);
3566 unregister_netdevice_many(&list);
3567 rtnl_unlock();
3570 static struct pernet_operations vxlan_net_ops = {
3571 .init = vxlan_init_net,
3572 .exit = vxlan_exit_net,
3573 .id = &vxlan_net_id,
3574 .size = sizeof(struct vxlan_net),
3577 static int __init vxlan_init_module(void)
3579 int rc;
3581 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3583 rc = register_pernet_subsys(&vxlan_net_ops);
3584 if (rc)
3585 goto out1;
3587 rc = register_netdevice_notifier(&vxlan_notifier_block);
3588 if (rc)
3589 goto out2;
3591 rc = rtnl_link_register(&vxlan_link_ops);
3592 if (rc)
3593 goto out3;
3595 return 0;
3596 out3:
3597 unregister_netdevice_notifier(&vxlan_notifier_block);
3598 out2:
3599 unregister_pernet_subsys(&vxlan_net_ops);
3600 out1:
3601 return rc;
3603 late_initcall(vxlan_init_module);
3605 static void __exit vxlan_cleanup_module(void)
3607 rtnl_link_unregister(&vxlan_link_ops);
3608 unregister_netdevice_notifier(&vxlan_notifier_block);
3609 unregister_pernet_subsys(&vxlan_net_ops);
3610 /* rcu_barrier() is called by netns */
3612 module_exit(vxlan_cleanup_module);
3614 MODULE_LICENSE("GPL");
3615 MODULE_VERSION(VXLAN_VERSION);
3616 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3617 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3618 MODULE_ALIAS_RTNL_LINK("vxlan");