Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / vxlan.c
blob083f3f0bf37fd1cb8e8fcbf7265a70b5263c7283
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VXLAN: Virtual eXtensible Local Area Network
5 * Copyright (c) 2012-2013 Vyatta Inc.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/udp.h>
15 #include <linux/igmp.h>
16 #include <linux/if_ether.h>
17 #include <linux/ethtool.h>
18 #include <net/arp.h>
19 #include <net/ndisc.h>
20 #include <net/ipv6_stubs.h>
21 #include <net/ip.h>
22 #include <net/icmp.h>
23 #include <net/rtnetlink.h>
24 #include <net/inet_ecn.h>
25 #include <net/net_namespace.h>
26 #include <net/netns/generic.h>
27 #include <net/tun_proto.h>
28 #include <net/vxlan.h>
30 #if IS_ENABLED(CONFIG_IPV6)
31 #include <net/ip6_tunnel.h>
32 #include <net/ip6_checksum.h>
33 #endif
35 #define VXLAN_VERSION "0.1"
37 #define PORT_HASH_BITS 8
38 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
39 #define FDB_AGE_DEFAULT 300 /* 5 min */
40 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42 /* UDP port for VXLAN traffic.
43 * The IANA assigned port is 4789, but the Linux default is 8472
44 * for compatibility with early adopters.
46 static unsigned short vxlan_port __read_mostly = 8472;
47 module_param_named(udp_port, vxlan_port, ushort, 0444);
48 MODULE_PARM_DESC(udp_port, "Destination UDP port");
50 static bool log_ecn_error = true;
51 module_param(log_ecn_error, bool, 0644);
52 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54 static unsigned int vxlan_net_id;
55 static struct rtnl_link_ops vxlan_link_ops;
57 static const u8 all_zeros_mac[ETH_ALEN + 2];
59 static int vxlan_sock_add(struct vxlan_dev *vxlan);
61 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63 /* per-network namespace private data for this module */
64 struct vxlan_net {
65 struct list_head vxlan_list;
66 struct hlist_head sock_list[PORT_HASH_SIZE];
67 spinlock_t sock_lock;
70 /* Forwarding table entry */
71 struct vxlan_fdb {
72 struct hlist_node hlist; /* linked list of entries */
73 struct rcu_head rcu;
74 unsigned long updated; /* jiffies */
75 unsigned long used;
76 struct list_head remotes;
77 u8 eth_addr[ETH_ALEN];
78 u16 state; /* see ndm_state */
79 __be32 vni;
80 u16 flags; /* see ndm_flags and below */
83 #define NTF_VXLAN_ADDED_BY_USER 0x100
85 /* salt for hash table */
86 static u32 vxlan_salt __read_mostly;
88 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
90 return vs->flags & VXLAN_F_COLLECT_METADATA ||
91 ip_tunnel_collect_metadata();
94 #if IS_ENABLED(CONFIG_IPV6)
95 static inline
96 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
98 if (a->sa.sa_family != b->sa.sa_family)
99 return false;
100 if (a->sa.sa_family == AF_INET6)
101 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
102 else
103 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
106 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
108 if (nla_len(nla) >= sizeof(struct in6_addr)) {
109 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
110 ip->sa.sa_family = AF_INET6;
111 return 0;
112 } else if (nla_len(nla) >= sizeof(__be32)) {
113 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
114 ip->sa.sa_family = AF_INET;
115 return 0;
116 } else {
117 return -EAFNOSUPPORT;
121 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
122 const union vxlan_addr *ip)
124 if (ip->sa.sa_family == AF_INET6)
125 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
126 else
127 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
130 #else /* !CONFIG_IPV6 */
132 static inline
133 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
135 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
138 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
140 if (nla_len(nla) >= sizeof(struct in6_addr)) {
141 return -EAFNOSUPPORT;
142 } else if (nla_len(nla) >= sizeof(__be32)) {
143 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
144 ip->sa.sa_family = AF_INET;
145 return 0;
146 } else {
147 return -EAFNOSUPPORT;
151 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
152 const union vxlan_addr *ip)
154 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
156 #endif
158 /* Virtual Network hash table head */
159 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
161 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
164 /* Socket hash table head */
165 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
167 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
169 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
172 /* First remote destination for a forwarding entry.
173 * Guaranteed to be non-NULL because remotes are never deleted.
175 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
177 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
180 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
182 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
185 /* Find VXLAN socket based on network namespace, address family and UDP port
186 * and enabled unshareable flags.
188 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
189 __be16 port, u32 flags, int ifindex)
191 struct vxlan_sock *vs;
193 flags &= VXLAN_F_RCV_FLAGS;
195 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
196 if (inet_sk(vs->sock->sk)->inet_sport == port &&
197 vxlan_get_sk_family(vs) == family &&
198 vs->flags == flags &&
199 vs->sock->sk->sk_bound_dev_if == ifindex)
200 return vs;
202 return NULL;
205 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
206 __be32 vni)
208 struct vxlan_dev_node *node;
210 /* For flow based devices, map all packets to VNI 0 */
211 if (vs->flags & VXLAN_F_COLLECT_METADATA)
212 vni = 0;
214 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
215 if (node->vxlan->default_dst.remote_vni != vni)
216 continue;
218 if (IS_ENABLED(CONFIG_IPV6)) {
219 const struct vxlan_config *cfg = &node->vxlan->cfg;
221 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
222 cfg->remote_ifindex != ifindex)
223 continue;
226 return node->vxlan;
229 return NULL;
232 /* Look up VNI in a per net namespace table */
233 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
234 __be32 vni, sa_family_t family,
235 __be16 port, u32 flags)
237 struct vxlan_sock *vs;
239 vs = vxlan_find_sock(net, family, port, flags, ifindex);
240 if (!vs)
241 return NULL;
243 return vxlan_vs_find_vni(vs, ifindex, vni);
246 /* Fill in neighbour message in skbuff. */
247 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
248 const struct vxlan_fdb *fdb,
249 u32 portid, u32 seq, int type, unsigned int flags,
250 const struct vxlan_rdst *rdst)
252 unsigned long now = jiffies;
253 struct nda_cacheinfo ci;
254 struct nlmsghdr *nlh;
255 struct ndmsg *ndm;
256 bool send_ip, send_eth;
258 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
259 if (nlh == NULL)
260 return -EMSGSIZE;
262 ndm = nlmsg_data(nlh);
263 memset(ndm, 0, sizeof(*ndm));
265 send_eth = send_ip = true;
267 if (type == RTM_GETNEIGH) {
268 send_ip = !vxlan_addr_any(&rdst->remote_ip);
269 send_eth = !is_zero_ether_addr(fdb->eth_addr);
270 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
271 } else
272 ndm->ndm_family = AF_BRIDGE;
273 ndm->ndm_state = fdb->state;
274 ndm->ndm_ifindex = vxlan->dev->ifindex;
275 ndm->ndm_flags = fdb->flags;
276 if (rdst->offloaded)
277 ndm->ndm_flags |= NTF_OFFLOADED;
278 ndm->ndm_type = RTN_UNICAST;
280 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
281 nla_put_s32(skb, NDA_LINK_NETNSID,
282 peernet2id(dev_net(vxlan->dev), vxlan->net)))
283 goto nla_put_failure;
285 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
286 goto nla_put_failure;
288 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
289 goto nla_put_failure;
291 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
292 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
293 goto nla_put_failure;
294 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
295 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
296 goto nla_put_failure;
297 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
298 nla_put_u32(skb, NDA_SRC_VNI,
299 be32_to_cpu(fdb->vni)))
300 goto nla_put_failure;
301 if (rdst->remote_ifindex &&
302 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
303 goto nla_put_failure;
305 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
306 ci.ndm_confirmed = 0;
307 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
308 ci.ndm_refcnt = 0;
310 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
311 goto nla_put_failure;
313 nlmsg_end(skb, nlh);
314 return 0;
316 nla_put_failure:
317 nlmsg_cancel(skb, nlh);
318 return -EMSGSIZE;
321 static inline size_t vxlan_nlmsg_size(void)
323 return NLMSG_ALIGN(sizeof(struct ndmsg))
324 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
325 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
326 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
327 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
328 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
329 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
330 + nla_total_size(sizeof(struct nda_cacheinfo));
333 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
334 struct vxlan_rdst *rd, int type)
336 struct net *net = dev_net(vxlan->dev);
337 struct sk_buff *skb;
338 int err = -ENOBUFS;
340 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
341 if (skb == NULL)
342 goto errout;
344 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
345 if (err < 0) {
346 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
347 WARN_ON(err == -EMSGSIZE);
348 kfree_skb(skb);
349 goto errout;
352 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
353 return;
354 errout:
355 if (err < 0)
356 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
359 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
360 const struct vxlan_fdb *fdb,
361 const struct vxlan_rdst *rd,
362 struct netlink_ext_ack *extack,
363 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
365 fdb_info->info.dev = vxlan->dev;
366 fdb_info->info.extack = extack;
367 fdb_info->remote_ip = rd->remote_ip;
368 fdb_info->remote_port = rd->remote_port;
369 fdb_info->remote_vni = rd->remote_vni;
370 fdb_info->remote_ifindex = rd->remote_ifindex;
371 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
372 fdb_info->vni = fdb->vni;
373 fdb_info->offloaded = rd->offloaded;
374 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
377 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
378 struct vxlan_fdb *fdb,
379 struct vxlan_rdst *rd,
380 bool adding,
381 struct netlink_ext_ack *extack)
383 struct switchdev_notifier_vxlan_fdb_info info;
384 enum switchdev_notifier_type notifier_type;
385 int ret;
387 if (WARN_ON(!rd))
388 return 0;
390 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
391 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
392 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
393 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
394 &info.info, extack);
395 return notifier_to_errno(ret);
398 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
399 struct vxlan_rdst *rd, int type, bool swdev_notify,
400 struct netlink_ext_ack *extack)
402 int err;
404 if (swdev_notify) {
405 switch (type) {
406 case RTM_NEWNEIGH:
407 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
408 true, extack);
409 if (err)
410 return err;
411 break;
412 case RTM_DELNEIGH:
413 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
414 false, extack);
415 break;
419 __vxlan_fdb_notify(vxlan, fdb, rd, type);
420 return 0;
423 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
425 struct vxlan_dev *vxlan = netdev_priv(dev);
426 struct vxlan_fdb f = {
427 .state = NUD_STALE,
429 struct vxlan_rdst remote = {
430 .remote_ip = *ipa, /* goes to NDA_DST */
431 .remote_vni = cpu_to_be32(VXLAN_N_VID),
434 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
437 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
439 struct vxlan_fdb f = {
440 .state = NUD_STALE,
442 struct vxlan_rdst remote = { };
444 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
446 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
449 /* Hash Ethernet address */
450 static u32 eth_hash(const unsigned char *addr)
452 u64 value = get_unaligned((u64 *)addr);
454 /* only want 6 bytes */
455 #ifdef __BIG_ENDIAN
456 value >>= 16;
457 #else
458 value <<= 16;
459 #endif
460 return hash_64(value, FDB_HASH_BITS);
463 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
465 /* use 1 byte of OUI and 3 bytes of NIC */
466 u32 key = get_unaligned((u32 *)(addr + 2));
468 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
471 /* Hash chain to use given mac address */
472 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
473 const u8 *mac, __be32 vni)
475 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
476 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
477 else
478 return &vxlan->fdb_head[eth_hash(mac)];
481 /* Look up Ethernet address in forwarding table */
482 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
483 const u8 *mac, __be32 vni)
485 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
486 struct vxlan_fdb *f;
488 hlist_for_each_entry_rcu(f, head, hlist) {
489 if (ether_addr_equal(mac, f->eth_addr)) {
490 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
491 if (vni == f->vni)
492 return f;
493 } else {
494 return f;
499 return NULL;
502 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
503 const u8 *mac, __be32 vni)
505 struct vxlan_fdb *f;
507 f = __vxlan_find_mac(vxlan, mac, vni);
508 if (f && f->used != jiffies)
509 f->used = jiffies;
511 return f;
514 /* caller should hold vxlan->hash_lock */
515 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
516 union vxlan_addr *ip, __be16 port,
517 __be32 vni, __u32 ifindex)
519 struct vxlan_rdst *rd;
521 list_for_each_entry(rd, &f->remotes, list) {
522 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
523 rd->remote_port == port &&
524 rd->remote_vni == vni &&
525 rd->remote_ifindex == ifindex)
526 return rd;
529 return NULL;
532 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
533 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
535 struct vxlan_dev *vxlan = netdev_priv(dev);
536 u8 eth_addr[ETH_ALEN + 2] = { 0 };
537 struct vxlan_rdst *rdst;
538 struct vxlan_fdb *f;
539 int rc = 0;
541 if (is_multicast_ether_addr(mac) ||
542 is_zero_ether_addr(mac))
543 return -EINVAL;
545 ether_addr_copy(eth_addr, mac);
547 rcu_read_lock();
549 f = __vxlan_find_mac(vxlan, eth_addr, vni);
550 if (!f) {
551 rc = -ENOENT;
552 goto out;
555 rdst = first_remote_rcu(f);
556 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
558 out:
559 rcu_read_unlock();
560 return rc;
562 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
564 static int vxlan_fdb_notify_one(struct notifier_block *nb,
565 const struct vxlan_dev *vxlan,
566 const struct vxlan_fdb *f,
567 const struct vxlan_rdst *rdst,
568 struct netlink_ext_ack *extack)
570 struct switchdev_notifier_vxlan_fdb_info fdb_info;
571 int rc;
573 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
574 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
575 &fdb_info);
576 return notifier_to_errno(rc);
579 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
580 struct notifier_block *nb,
581 struct netlink_ext_ack *extack)
583 struct vxlan_dev *vxlan;
584 struct vxlan_rdst *rdst;
585 struct vxlan_fdb *f;
586 unsigned int h;
587 int rc = 0;
589 if (!netif_is_vxlan(dev))
590 return -EINVAL;
591 vxlan = netdev_priv(dev);
593 spin_lock_bh(&vxlan->hash_lock);
594 for (h = 0; h < FDB_HASH_SIZE; ++h) {
595 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
596 if (f->vni == vni) {
597 list_for_each_entry(rdst, &f->remotes, list) {
598 rc = vxlan_fdb_notify_one(nb, vxlan,
599 f, rdst,
600 extack);
601 if (rc)
602 goto out;
608 out:
609 spin_unlock_bh(&vxlan->hash_lock);
610 return rc;
612 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
614 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
616 struct vxlan_dev *vxlan;
617 struct vxlan_rdst *rdst;
618 struct vxlan_fdb *f;
619 unsigned int h;
621 if (!netif_is_vxlan(dev))
622 return;
623 vxlan = netdev_priv(dev);
625 spin_lock_bh(&vxlan->hash_lock);
626 for (h = 0; h < FDB_HASH_SIZE; ++h) {
627 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
628 if (f->vni == vni)
629 list_for_each_entry(rdst, &f->remotes, list)
630 rdst->offloaded = false;
632 spin_unlock_bh(&vxlan->hash_lock);
634 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
636 /* Replace destination of unicast mac */
637 static int vxlan_fdb_replace(struct vxlan_fdb *f,
638 union vxlan_addr *ip, __be16 port, __be32 vni,
639 __u32 ifindex, struct vxlan_rdst *oldrd)
641 struct vxlan_rdst *rd;
643 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
644 if (rd)
645 return 0;
647 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
648 if (!rd)
649 return 0;
651 *oldrd = *rd;
652 dst_cache_reset(&rd->dst_cache);
653 rd->remote_ip = *ip;
654 rd->remote_port = port;
655 rd->remote_vni = vni;
656 rd->remote_ifindex = ifindex;
657 rd->offloaded = false;
658 return 1;
661 /* Add/update destinations for multicast */
662 static int vxlan_fdb_append(struct vxlan_fdb *f,
663 union vxlan_addr *ip, __be16 port, __be32 vni,
664 __u32 ifindex, struct vxlan_rdst **rdp)
666 struct vxlan_rdst *rd;
668 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
669 if (rd)
670 return 0;
672 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
673 if (rd == NULL)
674 return -ENOBUFS;
676 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
677 kfree(rd);
678 return -ENOBUFS;
681 rd->remote_ip = *ip;
682 rd->remote_port = port;
683 rd->offloaded = false;
684 rd->remote_vni = vni;
685 rd->remote_ifindex = ifindex;
687 list_add_tail_rcu(&rd->list, &f->remotes);
689 *rdp = rd;
690 return 1;
693 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
694 unsigned int off,
695 struct vxlanhdr *vh, size_t hdrlen,
696 __be32 vni_field,
697 struct gro_remcsum *grc,
698 bool nopartial)
700 size_t start, offset;
702 if (skb->remcsum_offload)
703 return vh;
705 if (!NAPI_GRO_CB(skb)->csum_valid)
706 return NULL;
708 start = vxlan_rco_start(vni_field);
709 offset = start + vxlan_rco_offset(vni_field);
711 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
712 start, offset, grc, nopartial);
714 skb->remcsum_offload = 1;
716 return vh;
719 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
720 struct list_head *head,
721 struct sk_buff *skb)
723 struct sk_buff *pp = NULL;
724 struct sk_buff *p;
725 struct vxlanhdr *vh, *vh2;
726 unsigned int hlen, off_vx;
727 int flush = 1;
728 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
729 __be32 flags;
730 struct gro_remcsum grc;
732 skb_gro_remcsum_init(&grc);
734 off_vx = skb_gro_offset(skb);
735 hlen = off_vx + sizeof(*vh);
736 vh = skb_gro_header_fast(skb, off_vx);
737 if (skb_gro_header_hard(skb, hlen)) {
738 vh = skb_gro_header_slow(skb, hlen, off_vx);
739 if (unlikely(!vh))
740 goto out;
743 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
745 flags = vh->vx_flags;
747 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
748 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
749 vh->vx_vni, &grc,
750 !!(vs->flags &
751 VXLAN_F_REMCSUM_NOPARTIAL));
753 if (!vh)
754 goto out;
757 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
759 list_for_each_entry(p, head, list) {
760 if (!NAPI_GRO_CB(p)->same_flow)
761 continue;
763 vh2 = (struct vxlanhdr *)(p->data + off_vx);
764 if (vh->vx_flags != vh2->vx_flags ||
765 vh->vx_vni != vh2->vx_vni) {
766 NAPI_GRO_CB(p)->same_flow = 0;
767 continue;
771 pp = call_gro_receive(eth_gro_receive, head, skb);
772 flush = 0;
774 out:
775 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
777 return pp;
780 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
782 /* Sets 'skb->inner_mac_header' since we are always called with
783 * 'skb->encapsulation' set.
785 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
788 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
789 const u8 *mac, __u16 state,
790 __be32 src_vni, __u16 ndm_flags)
792 struct vxlan_fdb *f;
794 f = kmalloc(sizeof(*f), GFP_ATOMIC);
795 if (!f)
796 return NULL;
797 f->state = state;
798 f->flags = ndm_flags;
799 f->updated = f->used = jiffies;
800 f->vni = src_vni;
801 INIT_LIST_HEAD(&f->remotes);
802 memcpy(f->eth_addr, mac, ETH_ALEN);
804 return f;
807 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
808 const u8 *mac, union vxlan_addr *ip,
809 __u16 state, __be16 port, __be32 src_vni,
810 __be32 vni, __u32 ifindex, __u16 ndm_flags,
811 struct vxlan_fdb **fdb)
813 struct vxlan_rdst *rd = NULL;
814 struct vxlan_fdb *f;
815 int rc;
817 if (vxlan->cfg.addrmax &&
818 vxlan->addrcnt >= vxlan->cfg.addrmax)
819 return -ENOSPC;
821 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
822 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
823 if (!f)
824 return -ENOMEM;
826 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
827 if (rc < 0) {
828 kfree(f);
829 return rc;
832 ++vxlan->addrcnt;
833 hlist_add_head_rcu(&f->hlist,
834 vxlan_fdb_head(vxlan, mac, src_vni));
836 *fdb = f;
838 return 0;
841 static void vxlan_fdb_free(struct rcu_head *head)
843 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
844 struct vxlan_rdst *rd, *nd;
846 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
847 dst_cache_destroy(&rd->dst_cache);
848 kfree(rd);
850 kfree(f);
853 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
854 bool do_notify, bool swdev_notify)
856 struct vxlan_rdst *rd;
858 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
860 --vxlan->addrcnt;
861 if (do_notify)
862 list_for_each_entry(rd, &f->remotes, list)
863 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
864 swdev_notify, NULL);
866 hlist_del_rcu(&f->hlist);
867 call_rcu(&f->rcu, vxlan_fdb_free);
870 static void vxlan_dst_free(struct rcu_head *head)
872 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
874 dst_cache_destroy(&rd->dst_cache);
875 kfree(rd);
878 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
879 union vxlan_addr *ip,
880 __u16 state, __u16 flags,
881 __be16 port, __be32 vni,
882 __u32 ifindex, __u16 ndm_flags,
883 struct vxlan_fdb *f,
884 bool swdev_notify,
885 struct netlink_ext_ack *extack)
887 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
888 struct vxlan_rdst *rd = NULL;
889 struct vxlan_rdst oldrd;
890 int notify = 0;
891 int rc = 0;
892 int err;
894 /* Do not allow an externally learned entry to take over an entry added
895 * by the user.
897 if (!(fdb_flags & NTF_EXT_LEARNED) ||
898 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
899 if (f->state != state) {
900 f->state = state;
901 f->updated = jiffies;
902 notify = 1;
904 if (f->flags != fdb_flags) {
905 f->flags = fdb_flags;
906 f->updated = jiffies;
907 notify = 1;
911 if ((flags & NLM_F_REPLACE)) {
912 /* Only change unicasts */
913 if (!(is_multicast_ether_addr(f->eth_addr) ||
914 is_zero_ether_addr(f->eth_addr))) {
915 rc = vxlan_fdb_replace(f, ip, port, vni,
916 ifindex, &oldrd);
917 notify |= rc;
918 } else {
919 return -EOPNOTSUPP;
922 if ((flags & NLM_F_APPEND) &&
923 (is_multicast_ether_addr(f->eth_addr) ||
924 is_zero_ether_addr(f->eth_addr))) {
925 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
927 if (rc < 0)
928 return rc;
929 notify |= rc;
932 if (ndm_flags & NTF_USE)
933 f->used = jiffies;
935 if (notify) {
936 if (rd == NULL)
937 rd = first_remote_rtnl(f);
939 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
940 swdev_notify, extack);
941 if (err)
942 goto err_notify;
945 return 0;
947 err_notify:
948 if ((flags & NLM_F_REPLACE) && rc)
949 *rd = oldrd;
950 else if ((flags & NLM_F_APPEND) && rc) {
951 list_del_rcu(&rd->list);
952 call_rcu(&rd->rcu, vxlan_dst_free);
954 return err;
957 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
958 const u8 *mac, union vxlan_addr *ip,
959 __u16 state, __u16 flags,
960 __be16 port, __be32 src_vni, __be32 vni,
961 __u32 ifindex, __u16 ndm_flags,
962 bool swdev_notify,
963 struct netlink_ext_ack *extack)
965 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
966 struct vxlan_fdb *f;
967 int rc;
969 /* Disallow replace to add a multicast entry */
970 if ((flags & NLM_F_REPLACE) &&
971 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
972 return -EOPNOTSUPP;
974 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
975 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
976 vni, ifindex, fdb_flags, &f);
977 if (rc < 0)
978 return rc;
980 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
981 swdev_notify, extack);
982 if (rc)
983 goto err_notify;
985 return 0;
987 err_notify:
988 vxlan_fdb_destroy(vxlan, f, false, false);
989 return rc;
992 /* Add new entry to forwarding table -- assumes lock held */
993 static int vxlan_fdb_update(struct vxlan_dev *vxlan,
994 const u8 *mac, union vxlan_addr *ip,
995 __u16 state, __u16 flags,
996 __be16 port, __be32 src_vni, __be32 vni,
997 __u32 ifindex, __u16 ndm_flags,
998 bool swdev_notify,
999 struct netlink_ext_ack *extack)
1001 struct vxlan_fdb *f;
1003 f = __vxlan_find_mac(vxlan, mac, src_vni);
1004 if (f) {
1005 if (flags & NLM_F_EXCL) {
1006 netdev_dbg(vxlan->dev,
1007 "lost race to create %pM\n", mac);
1008 return -EEXIST;
1011 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1012 vni, ifindex, ndm_flags, f,
1013 swdev_notify, extack);
1014 } else {
1015 if (!(flags & NLM_F_CREATE))
1016 return -ENOENT;
1018 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1019 port, src_vni, vni, ifindex,
1020 ndm_flags, swdev_notify, extack);
1024 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1025 struct vxlan_rdst *rd, bool swdev_notify)
1027 list_del_rcu(&rd->list);
1028 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1029 call_rcu(&rd->rcu, vxlan_dst_free);
1032 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1033 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1034 __be32 *vni, u32 *ifindex)
1036 struct net *net = dev_net(vxlan->dev);
1037 int err;
1039 if (tb[NDA_DST]) {
1040 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1041 if (err)
1042 return err;
1043 } else {
1044 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1045 if (remote->sa.sa_family == AF_INET) {
1046 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1047 ip->sa.sa_family = AF_INET;
1048 #if IS_ENABLED(CONFIG_IPV6)
1049 } else {
1050 ip->sin6.sin6_addr = in6addr_any;
1051 ip->sa.sa_family = AF_INET6;
1052 #endif
1056 if (tb[NDA_PORT]) {
1057 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1058 return -EINVAL;
1059 *port = nla_get_be16(tb[NDA_PORT]);
1060 } else {
1061 *port = vxlan->cfg.dst_port;
1064 if (tb[NDA_VNI]) {
1065 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1066 return -EINVAL;
1067 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1068 } else {
1069 *vni = vxlan->default_dst.remote_vni;
1072 if (tb[NDA_SRC_VNI]) {
1073 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1074 return -EINVAL;
1075 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1076 } else {
1077 *src_vni = vxlan->default_dst.remote_vni;
1080 if (tb[NDA_IFINDEX]) {
1081 struct net_device *tdev;
1083 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1084 return -EINVAL;
1085 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1086 tdev = __dev_get_by_index(net, *ifindex);
1087 if (!tdev)
1088 return -EADDRNOTAVAIL;
1089 } else {
1090 *ifindex = 0;
1093 return 0;
1096 /* Add static entry (via netlink) */
1097 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1098 struct net_device *dev,
1099 const unsigned char *addr, u16 vid, u16 flags,
1100 struct netlink_ext_ack *extack)
1102 struct vxlan_dev *vxlan = netdev_priv(dev);
1103 /* struct net *net = dev_net(vxlan->dev); */
1104 union vxlan_addr ip;
1105 __be16 port;
1106 __be32 src_vni, vni;
1107 u32 ifindex;
1108 int err;
1110 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1111 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1112 ndm->ndm_state);
1113 return -EINVAL;
1116 if (tb[NDA_DST] == NULL)
1117 return -EINVAL;
1119 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1120 if (err)
1121 return err;
1123 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1124 return -EAFNOSUPPORT;
1126 spin_lock_bh(&vxlan->hash_lock);
1127 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1128 port, src_vni, vni, ifindex,
1129 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1130 true, extack);
1131 spin_unlock_bh(&vxlan->hash_lock);
1133 return err;
1136 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1137 const unsigned char *addr, union vxlan_addr ip,
1138 __be16 port, __be32 src_vni, __be32 vni,
1139 u32 ifindex, bool swdev_notify)
1141 struct vxlan_fdb *f;
1142 struct vxlan_rdst *rd = NULL;
1143 int err = -ENOENT;
1145 f = vxlan_find_mac(vxlan, addr, src_vni);
1146 if (!f)
1147 return err;
1149 if (!vxlan_addr_any(&ip)) {
1150 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1151 if (!rd)
1152 goto out;
1155 /* remove a destination if it's not the only one on the list,
1156 * otherwise destroy the fdb entry
1158 if (rd && !list_is_singular(&f->remotes)) {
1159 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1160 goto out;
1163 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1165 out:
1166 return 0;
1169 /* Delete entry (via netlink) */
1170 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1171 struct net_device *dev,
1172 const unsigned char *addr, u16 vid)
1174 struct vxlan_dev *vxlan = netdev_priv(dev);
1175 union vxlan_addr ip;
1176 __be32 src_vni, vni;
1177 __be16 port;
1178 u32 ifindex;
1179 int err;
1181 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1182 if (err)
1183 return err;
1185 spin_lock_bh(&vxlan->hash_lock);
1186 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1187 true);
1188 spin_unlock_bh(&vxlan->hash_lock);
1190 return err;
1193 /* Dump forwarding table */
1194 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1195 struct net_device *dev,
1196 struct net_device *filter_dev, int *idx)
1198 struct vxlan_dev *vxlan = netdev_priv(dev);
1199 unsigned int h;
1200 int err = 0;
1202 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1203 struct vxlan_fdb *f;
1205 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1206 struct vxlan_rdst *rd;
1208 list_for_each_entry_rcu(rd, &f->remotes, list) {
1209 if (*idx < cb->args[2])
1210 goto skip;
1212 err = vxlan_fdb_info(skb, vxlan, f,
1213 NETLINK_CB(cb->skb).portid,
1214 cb->nlh->nlmsg_seq,
1215 RTM_NEWNEIGH,
1216 NLM_F_MULTI, rd);
1217 if (err < 0)
1218 goto out;
1219 skip:
1220 *idx += 1;
1224 out:
1225 return err;
1228 static int vxlan_fdb_get(struct sk_buff *skb,
1229 struct nlattr *tb[],
1230 struct net_device *dev,
1231 const unsigned char *addr,
1232 u16 vid, u32 portid, u32 seq,
1233 struct netlink_ext_ack *extack)
1235 struct vxlan_dev *vxlan = netdev_priv(dev);
1236 struct vxlan_fdb *f;
1237 __be32 vni;
1238 int err;
1240 if (tb[NDA_VNI])
1241 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1242 else
1243 vni = vxlan->default_dst.remote_vni;
1245 rcu_read_lock();
1247 f = __vxlan_find_mac(vxlan, addr, vni);
1248 if (!f) {
1249 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1250 err = -ENOENT;
1251 goto errout;
1254 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1255 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1256 errout:
1257 rcu_read_unlock();
1258 return err;
1261 /* Watch incoming packets to learn mapping between Ethernet address
1262 * and Tunnel endpoint.
1263 * Return true if packet is bogus and should be dropped.
1265 static bool vxlan_snoop(struct net_device *dev,
1266 union vxlan_addr *src_ip, const u8 *src_mac,
1267 u32 src_ifindex, __be32 vni)
1269 struct vxlan_dev *vxlan = netdev_priv(dev);
1270 struct vxlan_fdb *f;
1271 u32 ifindex = 0;
1273 #if IS_ENABLED(CONFIG_IPV6)
1274 if (src_ip->sa.sa_family == AF_INET6 &&
1275 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1276 ifindex = src_ifindex;
1277 #endif
1279 f = vxlan_find_mac(vxlan, src_mac, vni);
1280 if (likely(f)) {
1281 struct vxlan_rdst *rdst = first_remote_rcu(f);
1283 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1284 rdst->remote_ifindex == ifindex))
1285 return false;
1287 /* Don't migrate static entries, drop packets */
1288 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1289 return true;
1291 if (net_ratelimit())
1292 netdev_info(dev,
1293 "%pM migrated from %pIS to %pIS\n",
1294 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1296 rdst->remote_ip = *src_ip;
1297 f->updated = jiffies;
1298 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1299 } else {
1300 /* learned new entry */
1301 spin_lock(&vxlan->hash_lock);
1303 /* close off race between vxlan_flush and incoming packets */
1304 if (netif_running(dev))
1305 vxlan_fdb_update(vxlan, src_mac, src_ip,
1306 NUD_REACHABLE,
1307 NLM_F_EXCL|NLM_F_CREATE,
1308 vxlan->cfg.dst_port,
1309 vni,
1310 vxlan->default_dst.remote_vni,
1311 ifindex, NTF_SELF, true, NULL);
1312 spin_unlock(&vxlan->hash_lock);
1315 return false;
1318 /* See if multicast group is already in use by other ID */
1319 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1321 struct vxlan_dev *vxlan;
1322 struct vxlan_sock *sock4;
1323 #if IS_ENABLED(CONFIG_IPV6)
1324 struct vxlan_sock *sock6;
1325 #endif
1326 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1328 sock4 = rtnl_dereference(dev->vn4_sock);
1330 /* The vxlan_sock is only used by dev, leaving group has
1331 * no effect on other vxlan devices.
1333 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1334 return false;
1335 #if IS_ENABLED(CONFIG_IPV6)
1336 sock6 = rtnl_dereference(dev->vn6_sock);
1337 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1338 return false;
1339 #endif
1341 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1342 if (!netif_running(vxlan->dev) || vxlan == dev)
1343 continue;
1345 if (family == AF_INET &&
1346 rtnl_dereference(vxlan->vn4_sock) != sock4)
1347 continue;
1348 #if IS_ENABLED(CONFIG_IPV6)
1349 if (family == AF_INET6 &&
1350 rtnl_dereference(vxlan->vn6_sock) != sock6)
1351 continue;
1352 #endif
1354 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1355 &dev->default_dst.remote_ip))
1356 continue;
1358 if (vxlan->default_dst.remote_ifindex !=
1359 dev->default_dst.remote_ifindex)
1360 continue;
1362 return true;
1365 return false;
1368 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1370 struct vxlan_net *vn;
1372 if (!vs)
1373 return false;
1374 if (!refcount_dec_and_test(&vs->refcnt))
1375 return false;
1377 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1378 spin_lock(&vn->sock_lock);
1379 hlist_del_rcu(&vs->hlist);
1380 udp_tunnel_notify_del_rx_port(vs->sock,
1381 (vs->flags & VXLAN_F_GPE) ?
1382 UDP_TUNNEL_TYPE_VXLAN_GPE :
1383 UDP_TUNNEL_TYPE_VXLAN);
1384 spin_unlock(&vn->sock_lock);
1386 return true;
1389 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1391 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1392 #if IS_ENABLED(CONFIG_IPV6)
1393 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1395 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1396 #endif
1398 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1399 synchronize_net();
1401 vxlan_vs_del_dev(vxlan);
1403 if (__vxlan_sock_release_prep(sock4)) {
1404 udp_tunnel_sock_release(sock4->sock);
1405 kfree(sock4);
1408 #if IS_ENABLED(CONFIG_IPV6)
1409 if (__vxlan_sock_release_prep(sock6)) {
1410 udp_tunnel_sock_release(sock6->sock);
1411 kfree(sock6);
1413 #endif
1416 /* Update multicast group membership when first VNI on
1417 * multicast address is brought up
1419 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1421 struct sock *sk;
1422 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1423 int ifindex = vxlan->default_dst.remote_ifindex;
1424 int ret = -EINVAL;
1426 if (ip->sa.sa_family == AF_INET) {
1427 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1428 struct ip_mreqn mreq = {
1429 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1430 .imr_ifindex = ifindex,
1433 sk = sock4->sock->sk;
1434 lock_sock(sk);
1435 ret = ip_mc_join_group(sk, &mreq);
1436 release_sock(sk);
1437 #if IS_ENABLED(CONFIG_IPV6)
1438 } else {
1439 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1441 sk = sock6->sock->sk;
1442 lock_sock(sk);
1443 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1444 &ip->sin6.sin6_addr);
1445 release_sock(sk);
1446 #endif
1449 return ret;
1452 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1453 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1455 struct sock *sk;
1456 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1457 int ifindex = vxlan->default_dst.remote_ifindex;
1458 int ret = -EINVAL;
1460 if (ip->sa.sa_family == AF_INET) {
1461 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1462 struct ip_mreqn mreq = {
1463 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1464 .imr_ifindex = ifindex,
1467 sk = sock4->sock->sk;
1468 lock_sock(sk);
1469 ret = ip_mc_leave_group(sk, &mreq);
1470 release_sock(sk);
1471 #if IS_ENABLED(CONFIG_IPV6)
1472 } else {
1473 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1475 sk = sock6->sock->sk;
1476 lock_sock(sk);
1477 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1478 &ip->sin6.sin6_addr);
1479 release_sock(sk);
1480 #endif
1483 return ret;
1486 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1487 struct sk_buff *skb, u32 vxflags)
1489 size_t start, offset;
1491 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1492 goto out;
1494 start = vxlan_rco_start(unparsed->vx_vni);
1495 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1497 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1498 return false;
1500 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1501 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1502 out:
1503 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1504 unparsed->vx_vni &= VXLAN_VNI_MASK;
1505 return true;
1508 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1509 struct sk_buff *skb, u32 vxflags,
1510 struct vxlan_metadata *md)
1512 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1513 struct metadata_dst *tun_dst;
1515 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1516 goto out;
1518 md->gbp = ntohs(gbp->policy_id);
1520 tun_dst = (struct metadata_dst *)skb_dst(skb);
1521 if (tun_dst) {
1522 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1523 tun_dst->u.tun_info.options_len = sizeof(*md);
1525 if (gbp->dont_learn)
1526 md->gbp |= VXLAN_GBP_DONT_LEARN;
1528 if (gbp->policy_applied)
1529 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1531 /* In flow-based mode, GBP is carried in dst_metadata */
1532 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1533 skb->mark = md->gbp;
1534 out:
1535 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1538 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1539 __be16 *protocol,
1540 struct sk_buff *skb, u32 vxflags)
1542 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1544 /* Need to have Next Protocol set for interfaces in GPE mode. */
1545 if (!gpe->np_applied)
1546 return false;
1547 /* "The initial version is 0. If a receiver does not support the
1548 * version indicated it MUST drop the packet.
1550 if (gpe->version != 0)
1551 return false;
1552 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1553 * processing MUST occur." However, we don't implement OAM
1554 * processing, thus drop the packet.
1556 if (gpe->oam_flag)
1557 return false;
1559 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1560 if (!*protocol)
1561 return false;
1563 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1564 return true;
1567 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1568 struct vxlan_sock *vs,
1569 struct sk_buff *skb, __be32 vni)
1571 union vxlan_addr saddr;
1572 u32 ifindex = skb->dev->ifindex;
1574 skb_reset_mac_header(skb);
1575 skb->protocol = eth_type_trans(skb, vxlan->dev);
1576 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1578 /* Ignore packet loops (and multicast echo) */
1579 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1580 return false;
1582 /* Get address from the outer IP header */
1583 if (vxlan_get_sk_family(vs) == AF_INET) {
1584 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1585 saddr.sa.sa_family = AF_INET;
1586 #if IS_ENABLED(CONFIG_IPV6)
1587 } else {
1588 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1589 saddr.sa.sa_family = AF_INET6;
1590 #endif
1593 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1594 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1595 return false;
1597 return true;
1600 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1601 struct sk_buff *skb)
1603 int err = 0;
1605 if (vxlan_get_sk_family(vs) == AF_INET)
1606 err = IP_ECN_decapsulate(oiph, skb);
1607 #if IS_ENABLED(CONFIG_IPV6)
1608 else
1609 err = IP6_ECN_decapsulate(oiph, skb);
1610 #endif
1612 if (unlikely(err) && log_ecn_error) {
1613 if (vxlan_get_sk_family(vs) == AF_INET)
1614 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1615 &((struct iphdr *)oiph)->saddr,
1616 ((struct iphdr *)oiph)->tos);
1617 else
1618 net_info_ratelimited("non-ECT from %pI6\n",
1619 &((struct ipv6hdr *)oiph)->saddr);
1621 return err <= 1;
1624 /* Callback from net/ipv4/udp.c to receive packets */
1625 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1627 struct pcpu_sw_netstats *stats;
1628 struct vxlan_dev *vxlan;
1629 struct vxlan_sock *vs;
1630 struct vxlanhdr unparsed;
1631 struct vxlan_metadata _md;
1632 struct vxlan_metadata *md = &_md;
1633 __be16 protocol = htons(ETH_P_TEB);
1634 bool raw_proto = false;
1635 void *oiph;
1636 __be32 vni = 0;
1638 /* Need UDP and VXLAN header to be present */
1639 if (!pskb_may_pull(skb, VXLAN_HLEN))
1640 goto drop;
1642 unparsed = *vxlan_hdr(skb);
1643 /* VNI flag always required to be set */
1644 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1645 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1646 ntohl(vxlan_hdr(skb)->vx_flags),
1647 ntohl(vxlan_hdr(skb)->vx_vni));
1648 /* Return non vxlan pkt */
1649 goto drop;
1651 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1652 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1654 vs = rcu_dereference_sk_user_data(sk);
1655 if (!vs)
1656 goto drop;
1658 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1660 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1661 if (!vxlan)
1662 goto drop;
1664 /* For backwards compatibility, only allow reserved fields to be
1665 * used by VXLAN extensions if explicitly requested.
1667 if (vs->flags & VXLAN_F_GPE) {
1668 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1669 goto drop;
1670 raw_proto = true;
1673 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1674 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1675 goto drop;
1677 if (vxlan_collect_metadata(vs)) {
1678 struct metadata_dst *tun_dst;
1680 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1681 key32_to_tunnel_id(vni), sizeof(*md));
1683 if (!tun_dst)
1684 goto drop;
1686 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1688 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1689 } else {
1690 memset(md, 0, sizeof(*md));
1693 if (vs->flags & VXLAN_F_REMCSUM_RX)
1694 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1695 goto drop;
1696 if (vs->flags & VXLAN_F_GBP)
1697 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1698 /* Note that GBP and GPE can never be active together. This is
1699 * ensured in vxlan_dev_configure.
1702 if (unparsed.vx_flags || unparsed.vx_vni) {
1703 /* If there are any unprocessed flags remaining treat
1704 * this as a malformed packet. This behavior diverges from
1705 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1706 * in reserved fields are to be ignored. The approach here
1707 * maintains compatibility with previous stack code, and also
1708 * is more robust and provides a little more security in
1709 * adding extensions to VXLAN.
1711 goto drop;
1714 if (!raw_proto) {
1715 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1716 goto drop;
1717 } else {
1718 skb_reset_mac_header(skb);
1719 skb->dev = vxlan->dev;
1720 skb->pkt_type = PACKET_HOST;
1723 oiph = skb_network_header(skb);
1724 skb_reset_network_header(skb);
1726 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1727 ++vxlan->dev->stats.rx_frame_errors;
1728 ++vxlan->dev->stats.rx_errors;
1729 goto drop;
1732 rcu_read_lock();
1734 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1735 rcu_read_unlock();
1736 atomic_long_inc(&vxlan->dev->rx_dropped);
1737 goto drop;
1740 stats = this_cpu_ptr(vxlan->dev->tstats);
1741 u64_stats_update_begin(&stats->syncp);
1742 stats->rx_packets++;
1743 stats->rx_bytes += skb->len;
1744 u64_stats_update_end(&stats->syncp);
1746 gro_cells_receive(&vxlan->gro_cells, skb);
1748 rcu_read_unlock();
1750 return 0;
1752 drop:
1753 /* Consume bad packet */
1754 kfree_skb(skb);
1755 return 0;
1758 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1759 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1761 struct vxlan_dev *vxlan;
1762 struct vxlan_sock *vs;
1763 struct vxlanhdr *hdr;
1764 __be32 vni;
1766 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1767 return -EINVAL;
1769 hdr = vxlan_hdr(skb);
1771 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1772 return -EINVAL;
1774 vs = rcu_dereference_sk_user_data(sk);
1775 if (!vs)
1776 return -ENOENT;
1778 vni = vxlan_vni(hdr->vx_vni);
1779 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1780 if (!vxlan)
1781 return -ENOENT;
1783 return 0;
1786 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1788 struct vxlan_dev *vxlan = netdev_priv(dev);
1789 struct arphdr *parp;
1790 u8 *arpptr, *sha;
1791 __be32 sip, tip;
1792 struct neighbour *n;
1794 if (dev->flags & IFF_NOARP)
1795 goto out;
1797 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1798 dev->stats.tx_dropped++;
1799 goto out;
1801 parp = arp_hdr(skb);
1803 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1804 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1805 parp->ar_pro != htons(ETH_P_IP) ||
1806 parp->ar_op != htons(ARPOP_REQUEST) ||
1807 parp->ar_hln != dev->addr_len ||
1808 parp->ar_pln != 4)
1809 goto out;
1810 arpptr = (u8 *)parp + sizeof(struct arphdr);
1811 sha = arpptr;
1812 arpptr += dev->addr_len; /* sha */
1813 memcpy(&sip, arpptr, sizeof(sip));
1814 arpptr += sizeof(sip);
1815 arpptr += dev->addr_len; /* tha */
1816 memcpy(&tip, arpptr, sizeof(tip));
1818 if (ipv4_is_loopback(tip) ||
1819 ipv4_is_multicast(tip))
1820 goto out;
1822 n = neigh_lookup(&arp_tbl, &tip, dev);
1824 if (n) {
1825 struct vxlan_fdb *f;
1826 struct sk_buff *reply;
1828 if (!(n->nud_state & NUD_CONNECTED)) {
1829 neigh_release(n);
1830 goto out;
1833 f = vxlan_find_mac(vxlan, n->ha, vni);
1834 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1835 /* bridge-local neighbor */
1836 neigh_release(n);
1837 goto out;
1840 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1841 n->ha, sha);
1843 neigh_release(n);
1845 if (reply == NULL)
1846 goto out;
1848 skb_reset_mac_header(reply);
1849 __skb_pull(reply, skb_network_offset(reply));
1850 reply->ip_summed = CHECKSUM_UNNECESSARY;
1851 reply->pkt_type = PACKET_HOST;
1853 if (netif_rx_ni(reply) == NET_RX_DROP)
1854 dev->stats.rx_dropped++;
1855 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1856 union vxlan_addr ipa = {
1857 .sin.sin_addr.s_addr = tip,
1858 .sin.sin_family = AF_INET,
1861 vxlan_ip_miss(dev, &ipa);
1863 out:
1864 consume_skb(skb);
1865 return NETDEV_TX_OK;
1868 #if IS_ENABLED(CONFIG_IPV6)
1869 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1870 struct neighbour *n, bool isrouter)
1872 struct net_device *dev = request->dev;
1873 struct sk_buff *reply;
1874 struct nd_msg *ns, *na;
1875 struct ipv6hdr *pip6;
1876 u8 *daddr;
1877 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1878 int ns_olen;
1879 int i, len;
1881 if (dev == NULL || !pskb_may_pull(request, request->len))
1882 return NULL;
1884 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1885 sizeof(*na) + na_olen + dev->needed_tailroom;
1886 reply = alloc_skb(len, GFP_ATOMIC);
1887 if (reply == NULL)
1888 return NULL;
1890 reply->protocol = htons(ETH_P_IPV6);
1891 reply->dev = dev;
1892 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1893 skb_push(reply, sizeof(struct ethhdr));
1894 skb_reset_mac_header(reply);
1896 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
1898 daddr = eth_hdr(request)->h_source;
1899 ns_olen = request->len - skb_network_offset(request) -
1900 sizeof(struct ipv6hdr) - sizeof(*ns);
1901 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1902 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1903 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1904 break;
1908 /* Ethernet header */
1909 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1910 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1911 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1912 reply->protocol = htons(ETH_P_IPV6);
1914 skb_pull(reply, sizeof(struct ethhdr));
1915 skb_reset_network_header(reply);
1916 skb_put(reply, sizeof(struct ipv6hdr));
1918 /* IPv6 header */
1920 pip6 = ipv6_hdr(reply);
1921 memset(pip6, 0, sizeof(struct ipv6hdr));
1922 pip6->version = 6;
1923 pip6->priority = ipv6_hdr(request)->priority;
1924 pip6->nexthdr = IPPROTO_ICMPV6;
1925 pip6->hop_limit = 255;
1926 pip6->daddr = ipv6_hdr(request)->saddr;
1927 pip6->saddr = *(struct in6_addr *)n->primary_key;
1929 skb_pull(reply, sizeof(struct ipv6hdr));
1930 skb_reset_transport_header(reply);
1932 /* Neighbor Advertisement */
1933 na = skb_put_zero(reply, sizeof(*na) + na_olen);
1934 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1935 na->icmph.icmp6_router = isrouter;
1936 na->icmph.icmp6_override = 1;
1937 na->icmph.icmp6_solicited = 1;
1938 na->target = ns->target;
1939 ether_addr_copy(&na->opt[2], n->ha);
1940 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1941 na->opt[1] = na_olen >> 3;
1943 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1944 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1945 csum_partial(na, sizeof(*na)+na_olen, 0));
1947 pip6->payload_len = htons(sizeof(*na)+na_olen);
1949 skb_push(reply, sizeof(struct ipv6hdr));
1951 reply->ip_summed = CHECKSUM_UNNECESSARY;
1953 return reply;
1956 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1958 struct vxlan_dev *vxlan = netdev_priv(dev);
1959 const struct in6_addr *daddr;
1960 const struct ipv6hdr *iphdr;
1961 struct inet6_dev *in6_dev;
1962 struct neighbour *n;
1963 struct nd_msg *msg;
1965 in6_dev = __in6_dev_get(dev);
1966 if (!in6_dev)
1967 goto out;
1969 iphdr = ipv6_hdr(skb);
1970 daddr = &iphdr->daddr;
1971 msg = (struct nd_msg *)(iphdr + 1);
1973 if (ipv6_addr_loopback(daddr) ||
1974 ipv6_addr_is_multicast(&msg->target))
1975 goto out;
1977 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1979 if (n) {
1980 struct vxlan_fdb *f;
1981 struct sk_buff *reply;
1983 if (!(n->nud_state & NUD_CONNECTED)) {
1984 neigh_release(n);
1985 goto out;
1988 f = vxlan_find_mac(vxlan, n->ha, vni);
1989 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1990 /* bridge-local neighbor */
1991 neigh_release(n);
1992 goto out;
1995 reply = vxlan_na_create(skb, n,
1996 !!(f ? f->flags & NTF_ROUTER : 0));
1998 neigh_release(n);
2000 if (reply == NULL)
2001 goto out;
2003 if (netif_rx_ni(reply) == NET_RX_DROP)
2004 dev->stats.rx_dropped++;
2006 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2007 union vxlan_addr ipa = {
2008 .sin6.sin6_addr = msg->target,
2009 .sin6.sin6_family = AF_INET6,
2012 vxlan_ip_miss(dev, &ipa);
2015 out:
2016 consume_skb(skb);
2017 return NETDEV_TX_OK;
2019 #endif
2021 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2023 struct vxlan_dev *vxlan = netdev_priv(dev);
2024 struct neighbour *n;
2026 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2027 return false;
2029 n = NULL;
2030 switch (ntohs(eth_hdr(skb)->h_proto)) {
2031 case ETH_P_IP:
2033 struct iphdr *pip;
2035 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2036 return false;
2037 pip = ip_hdr(skb);
2038 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2039 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2040 union vxlan_addr ipa = {
2041 .sin.sin_addr.s_addr = pip->daddr,
2042 .sin.sin_family = AF_INET,
2045 vxlan_ip_miss(dev, &ipa);
2046 return false;
2049 break;
2051 #if IS_ENABLED(CONFIG_IPV6)
2052 case ETH_P_IPV6:
2054 struct ipv6hdr *pip6;
2056 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2057 return false;
2058 pip6 = ipv6_hdr(skb);
2059 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2060 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2061 union vxlan_addr ipa = {
2062 .sin6.sin6_addr = pip6->daddr,
2063 .sin6.sin6_family = AF_INET6,
2066 vxlan_ip_miss(dev, &ipa);
2067 return false;
2070 break;
2072 #endif
2073 default:
2074 return false;
2077 if (n) {
2078 bool diff;
2080 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2081 if (diff) {
2082 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2083 dev->addr_len);
2084 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2086 neigh_release(n);
2087 return diff;
2090 return false;
2093 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2094 struct vxlan_metadata *md)
2096 struct vxlanhdr_gbp *gbp;
2098 if (!md->gbp)
2099 return;
2101 gbp = (struct vxlanhdr_gbp *)vxh;
2102 vxh->vx_flags |= VXLAN_HF_GBP;
2104 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2105 gbp->dont_learn = 1;
2107 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2108 gbp->policy_applied = 1;
2110 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2113 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2114 __be16 protocol)
2116 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2118 gpe->np_applied = 1;
2119 gpe->next_protocol = tun_p_from_eth_p(protocol);
2120 if (!gpe->next_protocol)
2121 return -EPFNOSUPPORT;
2122 return 0;
2125 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2126 int iphdr_len, __be32 vni,
2127 struct vxlan_metadata *md, u32 vxflags,
2128 bool udp_sum)
2130 struct vxlanhdr *vxh;
2131 int min_headroom;
2132 int err;
2133 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2134 __be16 inner_protocol = htons(ETH_P_TEB);
2136 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2137 skb->ip_summed == CHECKSUM_PARTIAL) {
2138 int csum_start = skb_checksum_start_offset(skb);
2140 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2141 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2142 (skb->csum_offset == offsetof(struct udphdr, check) ||
2143 skb->csum_offset == offsetof(struct tcphdr, check)))
2144 type |= SKB_GSO_TUNNEL_REMCSUM;
2147 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2148 + VXLAN_HLEN + iphdr_len;
2150 /* Need space for new headers (invalidates iph ptr) */
2151 err = skb_cow_head(skb, min_headroom);
2152 if (unlikely(err))
2153 return err;
2155 err = iptunnel_handle_offloads(skb, type);
2156 if (err)
2157 return err;
2159 vxh = __skb_push(skb, sizeof(*vxh));
2160 vxh->vx_flags = VXLAN_HF_VNI;
2161 vxh->vx_vni = vxlan_vni_field(vni);
2163 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2164 unsigned int start;
2166 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2167 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2168 vxh->vx_flags |= VXLAN_HF_RCO;
2170 if (!skb_is_gso(skb)) {
2171 skb->ip_summed = CHECKSUM_NONE;
2172 skb->encapsulation = 0;
2176 if (vxflags & VXLAN_F_GBP)
2177 vxlan_build_gbp_hdr(vxh, vxflags, md);
2178 if (vxflags & VXLAN_F_GPE) {
2179 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2180 if (err < 0)
2181 return err;
2182 inner_protocol = skb->protocol;
2185 skb_set_inner_protocol(skb, inner_protocol);
2186 return 0;
2189 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2190 struct vxlan_sock *sock4,
2191 struct sk_buff *skb, int oif, u8 tos,
2192 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2193 struct dst_cache *dst_cache,
2194 const struct ip_tunnel_info *info)
2196 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2197 struct rtable *rt = NULL;
2198 struct flowi4 fl4;
2200 if (!sock4)
2201 return ERR_PTR(-EIO);
2203 if (tos && !info)
2204 use_cache = false;
2205 if (use_cache) {
2206 rt = dst_cache_get_ip4(dst_cache, saddr);
2207 if (rt)
2208 return rt;
2211 memset(&fl4, 0, sizeof(fl4));
2212 fl4.flowi4_oif = oif;
2213 fl4.flowi4_tos = RT_TOS(tos);
2214 fl4.flowi4_mark = skb->mark;
2215 fl4.flowi4_proto = IPPROTO_UDP;
2216 fl4.daddr = daddr;
2217 fl4.saddr = *saddr;
2218 fl4.fl4_dport = dport;
2219 fl4.fl4_sport = sport;
2221 rt = ip_route_output_key(vxlan->net, &fl4);
2222 if (likely(!IS_ERR(rt))) {
2223 if (rt->dst.dev == dev) {
2224 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2225 ip_rt_put(rt);
2226 return ERR_PTR(-ELOOP);
2229 *saddr = fl4.saddr;
2230 if (use_cache)
2231 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2232 } else {
2233 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2234 return ERR_PTR(-ENETUNREACH);
2236 return rt;
2239 #if IS_ENABLED(CONFIG_IPV6)
2240 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2241 struct net_device *dev,
2242 struct vxlan_sock *sock6,
2243 struct sk_buff *skb, int oif, u8 tos,
2244 __be32 label,
2245 const struct in6_addr *daddr,
2246 struct in6_addr *saddr,
2247 __be16 dport, __be16 sport,
2248 struct dst_cache *dst_cache,
2249 const struct ip_tunnel_info *info)
2251 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2252 struct dst_entry *ndst;
2253 struct flowi6 fl6;
2254 int err;
2256 if (!sock6)
2257 return ERR_PTR(-EIO);
2259 if (tos && !info)
2260 use_cache = false;
2261 if (use_cache) {
2262 ndst = dst_cache_get_ip6(dst_cache, saddr);
2263 if (ndst)
2264 return ndst;
2267 memset(&fl6, 0, sizeof(fl6));
2268 fl6.flowi6_oif = oif;
2269 fl6.daddr = *daddr;
2270 fl6.saddr = *saddr;
2271 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2272 fl6.flowi6_mark = skb->mark;
2273 fl6.flowi6_proto = IPPROTO_UDP;
2274 fl6.fl6_dport = dport;
2275 fl6.fl6_sport = sport;
2277 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
2278 sock6->sock->sk,
2279 &ndst, &fl6);
2280 if (unlikely(err < 0)) {
2281 netdev_dbg(dev, "no route to %pI6\n", daddr);
2282 return ERR_PTR(-ENETUNREACH);
2285 if (unlikely(ndst->dev == dev)) {
2286 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2287 dst_release(ndst);
2288 return ERR_PTR(-ELOOP);
2291 *saddr = fl6.saddr;
2292 if (use_cache)
2293 dst_cache_set_ip6(dst_cache, ndst, saddr);
2294 return ndst;
2296 #endif
2298 /* Bypass encapsulation if the destination is local */
2299 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2300 struct vxlan_dev *dst_vxlan, __be32 vni)
2302 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2303 union vxlan_addr loopback;
2304 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2305 struct net_device *dev;
2306 int len = skb->len;
2308 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2309 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2310 skb->pkt_type = PACKET_HOST;
2311 skb->encapsulation = 0;
2312 skb->dev = dst_vxlan->dev;
2313 __skb_pull(skb, skb_network_offset(skb));
2315 if (remote_ip->sa.sa_family == AF_INET) {
2316 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2317 loopback.sa.sa_family = AF_INET;
2318 #if IS_ENABLED(CONFIG_IPV6)
2319 } else {
2320 loopback.sin6.sin6_addr = in6addr_loopback;
2321 loopback.sa.sa_family = AF_INET6;
2322 #endif
2325 rcu_read_lock();
2326 dev = skb->dev;
2327 if (unlikely(!(dev->flags & IFF_UP))) {
2328 kfree_skb(skb);
2329 goto drop;
2332 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
2333 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2335 u64_stats_update_begin(&tx_stats->syncp);
2336 tx_stats->tx_packets++;
2337 tx_stats->tx_bytes += len;
2338 u64_stats_update_end(&tx_stats->syncp);
2340 if (netif_rx(skb) == NET_RX_SUCCESS) {
2341 u64_stats_update_begin(&rx_stats->syncp);
2342 rx_stats->rx_packets++;
2343 rx_stats->rx_bytes += len;
2344 u64_stats_update_end(&rx_stats->syncp);
2345 } else {
2346 drop:
2347 dev->stats.rx_dropped++;
2349 rcu_read_unlock();
2352 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2353 struct vxlan_dev *vxlan,
2354 union vxlan_addr *daddr,
2355 __be16 dst_port, int dst_ifindex, __be32 vni,
2356 struct dst_entry *dst,
2357 u32 rt_flags)
2359 #if IS_ENABLED(CONFIG_IPV6)
2360 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2361 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2362 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2364 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2365 #endif
2366 /* Bypass encapsulation if the destination is local */
2367 if (rt_flags & RTCF_LOCAL &&
2368 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2369 struct vxlan_dev *dst_vxlan;
2371 dst_release(dst);
2372 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2373 daddr->sa.sa_family, dst_port,
2374 vxlan->cfg.flags);
2375 if (!dst_vxlan) {
2376 dev->stats.tx_errors++;
2377 kfree_skb(skb);
2379 return -ENOENT;
2381 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2382 return 1;
2385 return 0;
2388 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2389 __be32 default_vni, struct vxlan_rdst *rdst,
2390 bool did_rsc)
2392 struct dst_cache *dst_cache;
2393 struct ip_tunnel_info *info;
2394 struct vxlan_dev *vxlan = netdev_priv(dev);
2395 const struct iphdr *old_iph = ip_hdr(skb);
2396 union vxlan_addr *dst;
2397 union vxlan_addr remote_ip, local_ip;
2398 struct vxlan_metadata _md;
2399 struct vxlan_metadata *md = &_md;
2400 __be16 src_port = 0, dst_port;
2401 struct dst_entry *ndst = NULL;
2402 __be32 vni, label;
2403 __u8 tos, ttl;
2404 int ifindex;
2405 int err;
2406 u32 flags = vxlan->cfg.flags;
2407 bool udp_sum = false;
2408 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2410 info = skb_tunnel_info(skb);
2412 if (rdst) {
2413 dst = &rdst->remote_ip;
2414 if (vxlan_addr_any(dst)) {
2415 if (did_rsc) {
2416 /* short-circuited back to local bridge */
2417 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2418 return;
2420 goto drop;
2423 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2424 vni = (rdst->remote_vni) ? : default_vni;
2425 ifindex = rdst->remote_ifindex;
2426 local_ip = vxlan->cfg.saddr;
2427 dst_cache = &rdst->dst_cache;
2428 md->gbp = skb->mark;
2429 if (flags & VXLAN_F_TTL_INHERIT) {
2430 ttl = ip_tunnel_get_ttl(old_iph, skb);
2431 } else {
2432 ttl = vxlan->cfg.ttl;
2433 if (!ttl && vxlan_addr_multicast(dst))
2434 ttl = 1;
2437 tos = vxlan->cfg.tos;
2438 if (tos == 1)
2439 tos = ip_tunnel_get_dsfield(old_iph, skb);
2441 if (dst->sa.sa_family == AF_INET)
2442 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2443 else
2444 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2445 label = vxlan->cfg.label;
2446 } else {
2447 if (!info) {
2448 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2449 dev->name);
2450 goto drop;
2452 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2453 if (remote_ip.sa.sa_family == AF_INET) {
2454 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2455 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2456 } else {
2457 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2458 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2460 dst = &remote_ip;
2461 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2462 vni = tunnel_id_to_key32(info->key.tun_id);
2463 ifindex = 0;
2464 dst_cache = &info->dst_cache;
2465 if (info->options_len &&
2466 info->key.tun_flags & TUNNEL_VXLAN_OPT)
2467 md = ip_tunnel_info_opts(info);
2468 ttl = info->key.ttl;
2469 tos = info->key.tos;
2470 label = info->key.label;
2471 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2473 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2474 vxlan->cfg.port_max, true);
2476 rcu_read_lock();
2477 if (dst->sa.sa_family == AF_INET) {
2478 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2479 struct rtable *rt;
2480 __be16 df = 0;
2482 if (!ifindex)
2483 ifindex = sock4->sock->sk->sk_bound_dev_if;
2485 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2486 dst->sin.sin_addr.s_addr,
2487 &local_ip.sin.sin_addr.s_addr,
2488 dst_port, src_port,
2489 dst_cache, info);
2490 if (IS_ERR(rt)) {
2491 err = PTR_ERR(rt);
2492 goto tx_error;
2495 if (!info) {
2496 /* Bypass encapsulation if the destination is local */
2497 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2498 dst_port, ifindex, vni,
2499 &rt->dst, rt->rt_flags);
2500 if (err)
2501 goto out_unlock;
2503 if (vxlan->cfg.df == VXLAN_DF_SET) {
2504 df = htons(IP_DF);
2505 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2506 struct ethhdr *eth = eth_hdr(skb);
2508 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2509 (ntohs(eth->h_proto) == ETH_P_IP &&
2510 old_iph->frag_off & htons(IP_DF)))
2511 df = htons(IP_DF);
2513 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2514 df = htons(IP_DF);
2517 ndst = &rt->dst;
2518 skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
2520 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2521 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2522 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2523 vni, md, flags, udp_sum);
2524 if (err < 0)
2525 goto tx_error;
2527 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2528 dst->sin.sin_addr.s_addr, tos, ttl, df,
2529 src_port, dst_port, xnet, !udp_sum);
2530 #if IS_ENABLED(CONFIG_IPV6)
2531 } else {
2532 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2534 if (!ifindex)
2535 ifindex = sock6->sock->sk->sk_bound_dev_if;
2537 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2538 label, &dst->sin6.sin6_addr,
2539 &local_ip.sin6.sin6_addr,
2540 dst_port, src_port,
2541 dst_cache, info);
2542 if (IS_ERR(ndst)) {
2543 err = PTR_ERR(ndst);
2544 ndst = NULL;
2545 goto tx_error;
2548 if (!info) {
2549 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2551 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2552 dst_port, ifindex, vni,
2553 ndst, rt6i_flags);
2554 if (err)
2555 goto out_unlock;
2558 skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
2560 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2561 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2562 skb_scrub_packet(skb, xnet);
2563 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2564 vni, md, flags, udp_sum);
2565 if (err < 0)
2566 goto tx_error;
2568 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2569 &local_ip.sin6.sin6_addr,
2570 &dst->sin6.sin6_addr, tos, ttl,
2571 label, src_port, dst_port, !udp_sum);
2572 #endif
2574 out_unlock:
2575 rcu_read_unlock();
2576 return;
2578 drop:
2579 dev->stats.tx_dropped++;
2580 dev_kfree_skb(skb);
2581 return;
2583 tx_error:
2584 rcu_read_unlock();
2585 if (err == -ELOOP)
2586 dev->stats.collisions++;
2587 else if (err == -ENETUNREACH)
2588 dev->stats.tx_carrier_errors++;
2589 dst_release(ndst);
2590 dev->stats.tx_errors++;
2591 kfree_skb(skb);
2594 /* Transmit local packets over Vxlan
2596 * Outer IP header inherits ECN and DF from inner header.
2597 * Outer UDP destination is the VXLAN assigned port.
2598 * source port is based on hash of flow
2600 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2602 struct vxlan_dev *vxlan = netdev_priv(dev);
2603 struct vxlan_rdst *rdst, *fdst = NULL;
2604 const struct ip_tunnel_info *info;
2605 bool did_rsc = false;
2606 struct vxlan_fdb *f;
2607 struct ethhdr *eth;
2608 __be32 vni = 0;
2610 info = skb_tunnel_info(skb);
2612 skb_reset_mac_header(skb);
2614 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2615 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2616 info->mode & IP_TUNNEL_INFO_TX) {
2617 vni = tunnel_id_to_key32(info->key.tun_id);
2618 } else {
2619 if (info && info->mode & IP_TUNNEL_INFO_TX)
2620 vxlan_xmit_one(skb, dev, vni, NULL, false);
2621 else
2622 kfree_skb(skb);
2623 return NETDEV_TX_OK;
2627 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2628 eth = eth_hdr(skb);
2629 if (ntohs(eth->h_proto) == ETH_P_ARP)
2630 return arp_reduce(dev, skb, vni);
2631 #if IS_ENABLED(CONFIG_IPV6)
2632 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2633 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2634 sizeof(struct nd_msg)) &&
2635 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2636 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2638 if (m->icmph.icmp6_code == 0 &&
2639 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2640 return neigh_reduce(dev, skb, vni);
2642 #endif
2645 eth = eth_hdr(skb);
2646 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2647 did_rsc = false;
2649 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2650 (ntohs(eth->h_proto) == ETH_P_IP ||
2651 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2652 did_rsc = route_shortcircuit(dev, skb);
2653 if (did_rsc)
2654 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2657 if (f == NULL) {
2658 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2659 if (f == NULL) {
2660 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2661 !is_multicast_ether_addr(eth->h_dest))
2662 vxlan_fdb_miss(vxlan, eth->h_dest);
2664 dev->stats.tx_dropped++;
2665 kfree_skb(skb);
2666 return NETDEV_TX_OK;
2670 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2671 struct sk_buff *skb1;
2673 if (!fdst) {
2674 fdst = rdst;
2675 continue;
2677 skb1 = skb_clone(skb, GFP_ATOMIC);
2678 if (skb1)
2679 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2682 if (fdst)
2683 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2684 else
2685 kfree_skb(skb);
2686 return NETDEV_TX_OK;
2689 /* Walk the forwarding table and purge stale entries */
2690 static void vxlan_cleanup(struct timer_list *t)
2692 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2693 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2694 unsigned int h;
2696 if (!netif_running(vxlan->dev))
2697 return;
2699 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2700 struct hlist_node *p, *n;
2702 spin_lock(&vxlan->hash_lock);
2703 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2704 struct vxlan_fdb *f
2705 = container_of(p, struct vxlan_fdb, hlist);
2706 unsigned long timeout;
2708 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2709 continue;
2711 if (f->flags & NTF_EXT_LEARNED)
2712 continue;
2714 timeout = f->used + vxlan->cfg.age_interval * HZ;
2715 if (time_before_eq(timeout, jiffies)) {
2716 netdev_dbg(vxlan->dev,
2717 "garbage collect %pM\n",
2718 f->eth_addr);
2719 f->state = NUD_STALE;
2720 vxlan_fdb_destroy(vxlan, f, true, true);
2721 } else if (time_before(timeout, next_timer))
2722 next_timer = timeout;
2724 spin_unlock(&vxlan->hash_lock);
2727 mod_timer(&vxlan->age_timer, next_timer);
2730 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2732 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2734 spin_lock(&vn->sock_lock);
2735 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2736 #if IS_ENABLED(CONFIG_IPV6)
2737 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2738 #endif
2739 spin_unlock(&vn->sock_lock);
2742 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2743 struct vxlan_dev_node *node)
2745 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2746 __be32 vni = vxlan->default_dst.remote_vni;
2748 node->vxlan = vxlan;
2749 spin_lock(&vn->sock_lock);
2750 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2751 spin_unlock(&vn->sock_lock);
2754 /* Setup stats when device is created */
2755 static int vxlan_init(struct net_device *dev)
2757 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2758 if (!dev->tstats)
2759 return -ENOMEM;
2761 return 0;
2764 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2766 struct vxlan_fdb *f;
2768 spin_lock_bh(&vxlan->hash_lock);
2769 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2770 if (f)
2771 vxlan_fdb_destroy(vxlan, f, true, true);
2772 spin_unlock_bh(&vxlan->hash_lock);
2775 static void vxlan_uninit(struct net_device *dev)
2777 struct vxlan_dev *vxlan = netdev_priv(dev);
2779 gro_cells_destroy(&vxlan->gro_cells);
2781 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2783 free_percpu(dev->tstats);
2786 /* Start ageing timer and join group when device is brought up */
2787 static int vxlan_open(struct net_device *dev)
2789 struct vxlan_dev *vxlan = netdev_priv(dev);
2790 int ret;
2792 ret = vxlan_sock_add(vxlan);
2793 if (ret < 0)
2794 return ret;
2796 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2797 ret = vxlan_igmp_join(vxlan);
2798 if (ret == -EADDRINUSE)
2799 ret = 0;
2800 if (ret) {
2801 vxlan_sock_release(vxlan);
2802 return ret;
2806 if (vxlan->cfg.age_interval)
2807 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2809 return ret;
2812 /* Purge the forwarding table */
2813 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2815 unsigned int h;
2817 spin_lock_bh(&vxlan->hash_lock);
2818 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2819 struct hlist_node *p, *n;
2820 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2821 struct vxlan_fdb *f
2822 = container_of(p, struct vxlan_fdb, hlist);
2823 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2824 continue;
2825 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2826 if (!is_zero_ether_addr(f->eth_addr))
2827 vxlan_fdb_destroy(vxlan, f, true, true);
2830 spin_unlock_bh(&vxlan->hash_lock);
2833 /* Cleanup timer and forwarding table on shutdown */
2834 static int vxlan_stop(struct net_device *dev)
2836 struct vxlan_dev *vxlan = netdev_priv(dev);
2837 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2838 int ret = 0;
2840 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2841 !vxlan_group_used(vn, vxlan))
2842 ret = vxlan_igmp_leave(vxlan);
2844 del_timer_sync(&vxlan->age_timer);
2846 vxlan_flush(vxlan, false);
2847 vxlan_sock_release(vxlan);
2849 return ret;
2852 /* Stub, nothing needs to be done. */
2853 static void vxlan_set_multicast_list(struct net_device *dev)
2857 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2859 struct vxlan_dev *vxlan = netdev_priv(dev);
2860 struct vxlan_rdst *dst = &vxlan->default_dst;
2861 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2862 dst->remote_ifindex);
2863 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
2865 /* This check is different than dev->max_mtu, because it looks at
2866 * the lowerdev->mtu, rather than the static dev->max_mtu
2868 if (lowerdev) {
2869 int max_mtu = lowerdev->mtu -
2870 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2871 if (new_mtu > max_mtu)
2872 return -EINVAL;
2875 dev->mtu = new_mtu;
2876 return 0;
2879 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2881 struct vxlan_dev *vxlan = netdev_priv(dev);
2882 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2883 __be16 sport, dport;
2885 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2886 vxlan->cfg.port_max, true);
2887 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2889 if (ip_tunnel_info_af(info) == AF_INET) {
2890 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2891 struct rtable *rt;
2893 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
2894 info->key.u.ipv4.dst,
2895 &info->key.u.ipv4.src, dport, sport,
2896 &info->dst_cache, info);
2897 if (IS_ERR(rt))
2898 return PTR_ERR(rt);
2899 ip_rt_put(rt);
2900 } else {
2901 #if IS_ENABLED(CONFIG_IPV6)
2902 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2903 struct dst_entry *ndst;
2905 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
2906 info->key.label, &info->key.u.ipv6.dst,
2907 &info->key.u.ipv6.src, dport, sport,
2908 &info->dst_cache, info);
2909 if (IS_ERR(ndst))
2910 return PTR_ERR(ndst);
2911 dst_release(ndst);
2912 #else /* !CONFIG_IPV6 */
2913 return -EPFNOSUPPORT;
2914 #endif
2916 info->key.tp_src = sport;
2917 info->key.tp_dst = dport;
2918 return 0;
2921 static const struct net_device_ops vxlan_netdev_ether_ops = {
2922 .ndo_init = vxlan_init,
2923 .ndo_uninit = vxlan_uninit,
2924 .ndo_open = vxlan_open,
2925 .ndo_stop = vxlan_stop,
2926 .ndo_start_xmit = vxlan_xmit,
2927 .ndo_get_stats64 = ip_tunnel_get_stats64,
2928 .ndo_set_rx_mode = vxlan_set_multicast_list,
2929 .ndo_change_mtu = vxlan_change_mtu,
2930 .ndo_validate_addr = eth_validate_addr,
2931 .ndo_set_mac_address = eth_mac_addr,
2932 .ndo_fdb_add = vxlan_fdb_add,
2933 .ndo_fdb_del = vxlan_fdb_delete,
2934 .ndo_fdb_dump = vxlan_fdb_dump,
2935 .ndo_fdb_get = vxlan_fdb_get,
2936 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2937 .ndo_change_proto_down = dev_change_proto_down_generic,
2940 static const struct net_device_ops vxlan_netdev_raw_ops = {
2941 .ndo_init = vxlan_init,
2942 .ndo_uninit = vxlan_uninit,
2943 .ndo_open = vxlan_open,
2944 .ndo_stop = vxlan_stop,
2945 .ndo_start_xmit = vxlan_xmit,
2946 .ndo_get_stats64 = ip_tunnel_get_stats64,
2947 .ndo_change_mtu = vxlan_change_mtu,
2948 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2951 /* Info for udev, that this is a virtual tunnel endpoint */
2952 static struct device_type vxlan_type = {
2953 .name = "vxlan",
2956 /* Calls the ndo_udp_tunnel_add of the caller in order to
2957 * supply the listening VXLAN udp ports. Callers are expected
2958 * to implement the ndo_udp_tunnel_add.
2960 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
2962 struct vxlan_sock *vs;
2963 struct net *net = dev_net(dev);
2964 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2965 unsigned int i;
2967 spin_lock(&vn->sock_lock);
2968 for (i = 0; i < PORT_HASH_SIZE; ++i) {
2969 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2970 unsigned short type;
2972 if (vs->flags & VXLAN_F_GPE)
2973 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2974 else
2975 type = UDP_TUNNEL_TYPE_VXLAN;
2977 if (push)
2978 udp_tunnel_push_rx_port(dev, vs->sock, type);
2979 else
2980 udp_tunnel_drop_rx_port(dev, vs->sock, type);
2983 spin_unlock(&vn->sock_lock);
2986 /* Initialize the device structure. */
2987 static void vxlan_setup(struct net_device *dev)
2989 struct vxlan_dev *vxlan = netdev_priv(dev);
2990 unsigned int h;
2992 eth_hw_addr_random(dev);
2993 ether_setup(dev);
2995 dev->needs_free_netdev = true;
2996 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2998 dev->features |= NETIF_F_LLTX;
2999 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3000 dev->features |= NETIF_F_RXCSUM;
3001 dev->features |= NETIF_F_GSO_SOFTWARE;
3003 dev->vlan_features = dev->features;
3004 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3005 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3006 netif_keep_dst(dev);
3007 dev->priv_flags |= IFF_NO_QUEUE;
3009 /* MTU range: 68 - 65535 */
3010 dev->min_mtu = ETH_MIN_MTU;
3011 dev->max_mtu = ETH_MAX_MTU;
3013 INIT_LIST_HEAD(&vxlan->next);
3014 spin_lock_init(&vxlan->hash_lock);
3016 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3018 vxlan->dev = dev;
3020 gro_cells_init(&vxlan->gro_cells, dev);
3022 for (h = 0; h < FDB_HASH_SIZE; ++h)
3023 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3026 static void vxlan_ether_setup(struct net_device *dev)
3028 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3029 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3030 dev->netdev_ops = &vxlan_netdev_ether_ops;
3033 static void vxlan_raw_setup(struct net_device *dev)
3035 dev->header_ops = NULL;
3036 dev->type = ARPHRD_NONE;
3037 dev->hard_header_len = 0;
3038 dev->addr_len = 0;
3039 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3040 dev->netdev_ops = &vxlan_netdev_raw_ops;
3043 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3044 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3045 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
3046 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3047 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3048 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
3049 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3050 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3051 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3052 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3053 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3054 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3055 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3056 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3057 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3058 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3059 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3060 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3061 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3062 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3063 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3064 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3065 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3066 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3067 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3068 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3069 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3070 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3071 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3072 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3075 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3076 struct netlink_ext_ack *extack)
3078 if (tb[IFLA_ADDRESS]) {
3079 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3080 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3081 "Provided link layer address is not Ethernet");
3082 return -EINVAL;
3085 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3086 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3087 "Provided Ethernet address is not unicast");
3088 return -EADDRNOTAVAIL;
3092 if (tb[IFLA_MTU]) {
3093 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3095 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3096 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3097 "MTU must be between 68 and 65535");
3098 return -EINVAL;
3102 if (!data) {
3103 NL_SET_ERR_MSG(extack,
3104 "Required attributes not provided to perform the operation");
3105 return -EINVAL;
3108 if (data[IFLA_VXLAN_ID]) {
3109 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3111 if (id >= VXLAN_N_VID) {
3112 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
3113 "VXLAN ID must be lower than 16777216");
3114 return -ERANGE;
3118 if (data[IFLA_VXLAN_PORT_RANGE]) {
3119 const struct ifla_vxlan_port_range *p
3120 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3122 if (ntohs(p->high) < ntohs(p->low)) {
3123 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3124 "Invalid source port range");
3125 return -EINVAL;
3129 if (data[IFLA_VXLAN_DF]) {
3130 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3132 if (df < 0 || df > VXLAN_DF_MAX) {
3133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_DF],
3134 "Invalid DF attribute");
3135 return -EINVAL;
3139 return 0;
3142 static void vxlan_get_drvinfo(struct net_device *netdev,
3143 struct ethtool_drvinfo *drvinfo)
3145 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3146 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3149 static const struct ethtool_ops vxlan_ethtool_ops = {
3150 .get_drvinfo = vxlan_get_drvinfo,
3151 .get_link = ethtool_op_get_link,
3154 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3155 __be16 port, u32 flags, int ifindex)
3157 struct socket *sock;
3158 struct udp_port_cfg udp_conf;
3159 int err;
3161 memset(&udp_conf, 0, sizeof(udp_conf));
3163 if (ipv6) {
3164 udp_conf.family = AF_INET6;
3165 udp_conf.use_udp6_rx_checksums =
3166 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3167 udp_conf.ipv6_v6only = 1;
3168 } else {
3169 udp_conf.family = AF_INET;
3172 udp_conf.local_udp_port = port;
3173 udp_conf.bind_ifindex = ifindex;
3175 /* Open UDP socket */
3176 err = udp_sock_create(net, &udp_conf, &sock);
3177 if (err < 0)
3178 return ERR_PTR(err);
3180 return sock;
3183 /* Create new listen socket if needed */
3184 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3185 __be16 port, u32 flags,
3186 int ifindex)
3188 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3189 struct vxlan_sock *vs;
3190 struct socket *sock;
3191 unsigned int h;
3192 struct udp_tunnel_sock_cfg tunnel_cfg;
3194 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3195 if (!vs)
3196 return ERR_PTR(-ENOMEM);
3198 for (h = 0; h < VNI_HASH_SIZE; ++h)
3199 INIT_HLIST_HEAD(&vs->vni_list[h]);
3201 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3202 if (IS_ERR(sock)) {
3203 kfree(vs);
3204 return ERR_CAST(sock);
3207 vs->sock = sock;
3208 refcount_set(&vs->refcnt, 1);
3209 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3211 spin_lock(&vn->sock_lock);
3212 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3213 udp_tunnel_notify_add_rx_port(sock,
3214 (vs->flags & VXLAN_F_GPE) ?
3215 UDP_TUNNEL_TYPE_VXLAN_GPE :
3216 UDP_TUNNEL_TYPE_VXLAN);
3217 spin_unlock(&vn->sock_lock);
3219 /* Mark socket as an encapsulation socket. */
3220 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3221 tunnel_cfg.sk_user_data = vs;
3222 tunnel_cfg.encap_type = 1;
3223 tunnel_cfg.encap_rcv = vxlan_rcv;
3224 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3225 tunnel_cfg.encap_destroy = NULL;
3226 tunnel_cfg.gro_receive = vxlan_gro_receive;
3227 tunnel_cfg.gro_complete = vxlan_gro_complete;
3229 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3231 return vs;
3234 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3236 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3237 struct vxlan_sock *vs = NULL;
3238 struct vxlan_dev_node *node;
3239 int l3mdev_index = 0;
3241 if (vxlan->cfg.remote_ifindex)
3242 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3243 vxlan->net, vxlan->cfg.remote_ifindex);
3245 if (!vxlan->cfg.no_share) {
3246 spin_lock(&vn->sock_lock);
3247 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3248 vxlan->cfg.dst_port, vxlan->cfg.flags,
3249 l3mdev_index);
3250 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3251 spin_unlock(&vn->sock_lock);
3252 return -EBUSY;
3254 spin_unlock(&vn->sock_lock);
3256 if (!vs)
3257 vs = vxlan_socket_create(vxlan->net, ipv6,
3258 vxlan->cfg.dst_port, vxlan->cfg.flags,
3259 l3mdev_index);
3260 if (IS_ERR(vs))
3261 return PTR_ERR(vs);
3262 #if IS_ENABLED(CONFIG_IPV6)
3263 if (ipv6) {
3264 rcu_assign_pointer(vxlan->vn6_sock, vs);
3265 node = &vxlan->hlist6;
3266 } else
3267 #endif
3269 rcu_assign_pointer(vxlan->vn4_sock, vs);
3270 node = &vxlan->hlist4;
3272 vxlan_vs_add_dev(vs, vxlan, node);
3273 return 0;
3276 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3278 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3279 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3280 bool ipv4 = !ipv6 || metadata;
3281 int ret = 0;
3283 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3284 #if IS_ENABLED(CONFIG_IPV6)
3285 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3286 if (ipv6) {
3287 ret = __vxlan_sock_add(vxlan, true);
3288 if (ret < 0 && ret != -EAFNOSUPPORT)
3289 ipv4 = false;
3291 #endif
3292 if (ipv4)
3293 ret = __vxlan_sock_add(vxlan, false);
3294 if (ret < 0)
3295 vxlan_sock_release(vxlan);
3296 return ret;
3299 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3300 struct net_device **lower,
3301 struct vxlan_dev *old,
3302 struct netlink_ext_ack *extack)
3304 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3305 struct vxlan_dev *tmp;
3306 bool use_ipv6 = false;
3308 if (conf->flags & VXLAN_F_GPE) {
3309 /* For now, allow GPE only together with
3310 * COLLECT_METADATA. This can be relaxed later; in such
3311 * case, the other side of the PtP link will have to be
3312 * provided.
3314 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3315 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3316 NL_SET_ERR_MSG(extack,
3317 "VXLAN GPE does not support this combination of attributes");
3318 return -EINVAL;
3322 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3323 /* Unless IPv6 is explicitly requested, assume IPv4 */
3324 conf->remote_ip.sa.sa_family = AF_INET;
3325 conf->saddr.sa.sa_family = AF_INET;
3326 } else if (!conf->remote_ip.sa.sa_family) {
3327 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3328 } else if (!conf->saddr.sa.sa_family) {
3329 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3332 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3333 NL_SET_ERR_MSG(extack,
3334 "Local and remote address must be from the same family");
3335 return -EINVAL;
3338 if (vxlan_addr_multicast(&conf->saddr)) {
3339 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3340 return -EINVAL;
3343 if (conf->saddr.sa.sa_family == AF_INET6) {
3344 if (!IS_ENABLED(CONFIG_IPV6)) {
3345 NL_SET_ERR_MSG(extack,
3346 "IPv6 support not enabled in the kernel");
3347 return -EPFNOSUPPORT;
3349 use_ipv6 = true;
3350 conf->flags |= VXLAN_F_IPV6;
3352 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3353 int local_type =
3354 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3355 int remote_type =
3356 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3358 if (local_type & IPV6_ADDR_LINKLOCAL) {
3359 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3360 (remote_type != IPV6_ADDR_ANY)) {
3361 NL_SET_ERR_MSG(extack,
3362 "Invalid combination of local and remote address scopes");
3363 return -EINVAL;
3366 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3367 } else {
3368 if (remote_type ==
3369 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3370 NL_SET_ERR_MSG(extack,
3371 "Invalid combination of local and remote address scopes");
3372 return -EINVAL;
3375 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3380 if (conf->label && !use_ipv6) {
3381 NL_SET_ERR_MSG(extack,
3382 "Label attribute only applies to IPv6 VXLAN devices");
3383 return -EINVAL;
3386 if (conf->remote_ifindex) {
3387 struct net_device *lowerdev;
3389 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3390 if (!lowerdev) {
3391 NL_SET_ERR_MSG(extack,
3392 "Invalid local interface, device not found");
3393 return -ENODEV;
3396 #if IS_ENABLED(CONFIG_IPV6)
3397 if (use_ipv6) {
3398 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3399 if (idev && idev->cnf.disable_ipv6) {
3400 NL_SET_ERR_MSG(extack,
3401 "IPv6 support disabled by administrator");
3402 return -EPERM;
3405 #endif
3407 *lower = lowerdev;
3408 } else {
3409 if (vxlan_addr_multicast(&conf->remote_ip)) {
3410 NL_SET_ERR_MSG(extack,
3411 "Local interface required for multicast remote destination");
3413 return -EINVAL;
3416 #if IS_ENABLED(CONFIG_IPV6)
3417 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3418 NL_SET_ERR_MSG(extack,
3419 "Local interface required for link-local local/remote addresses");
3420 return -EINVAL;
3422 #endif
3424 *lower = NULL;
3427 if (!conf->dst_port) {
3428 if (conf->flags & VXLAN_F_GPE)
3429 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3430 else
3431 conf->dst_port = htons(vxlan_port);
3434 if (!conf->age_interval)
3435 conf->age_interval = FDB_AGE_DEFAULT;
3437 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3438 if (tmp == old)
3439 continue;
3441 if (tmp->cfg.vni != conf->vni)
3442 continue;
3443 if (tmp->cfg.dst_port != conf->dst_port)
3444 continue;
3445 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3446 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3447 continue;
3449 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3450 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3451 continue;
3453 NL_SET_ERR_MSG(extack,
3454 "A VXLAN device with the specified VNI already exists");
3455 return -EEXIST;
3458 return 0;
3461 static void vxlan_config_apply(struct net_device *dev,
3462 struct vxlan_config *conf,
3463 struct net_device *lowerdev,
3464 struct net *src_net,
3465 bool changelink)
3467 struct vxlan_dev *vxlan = netdev_priv(dev);
3468 struct vxlan_rdst *dst = &vxlan->default_dst;
3469 unsigned short needed_headroom = ETH_HLEN;
3470 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3471 int max_mtu = ETH_MAX_MTU;
3473 if (!changelink) {
3474 if (conf->flags & VXLAN_F_GPE)
3475 vxlan_raw_setup(dev);
3476 else
3477 vxlan_ether_setup(dev);
3479 if (conf->mtu)
3480 dev->mtu = conf->mtu;
3482 vxlan->net = src_net;
3485 dst->remote_vni = conf->vni;
3487 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3489 if (lowerdev) {
3490 dst->remote_ifindex = conf->remote_ifindex;
3492 dev->gso_max_size = lowerdev->gso_max_size;
3493 dev->gso_max_segs = lowerdev->gso_max_segs;
3495 needed_headroom = lowerdev->hard_header_len;
3497 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3498 VXLAN_HEADROOM);
3499 if (max_mtu < ETH_MIN_MTU)
3500 max_mtu = ETH_MIN_MTU;
3502 if (!changelink && !conf->mtu)
3503 dev->mtu = max_mtu;
3506 if (dev->mtu > max_mtu)
3507 dev->mtu = max_mtu;
3509 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3510 needed_headroom += VXLAN6_HEADROOM;
3511 else
3512 needed_headroom += VXLAN_HEADROOM;
3513 dev->needed_headroom = needed_headroom;
3515 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3518 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3519 struct vxlan_config *conf, bool changelink,
3520 struct netlink_ext_ack *extack)
3522 struct vxlan_dev *vxlan = netdev_priv(dev);
3523 struct net_device *lowerdev;
3524 int ret;
3526 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3527 if (ret)
3528 return ret;
3530 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3532 return 0;
3535 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3536 struct vxlan_config *conf,
3537 struct netlink_ext_ack *extack)
3539 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3540 struct vxlan_dev *vxlan = netdev_priv(dev);
3541 struct vxlan_fdb *f = NULL;
3542 bool unregister = false;
3543 int err;
3545 err = vxlan_dev_configure(net, dev, conf, false, extack);
3546 if (err)
3547 return err;
3549 dev->ethtool_ops = &vxlan_ethtool_ops;
3551 /* create an fdb entry for a valid default destination */
3552 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3553 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3554 &vxlan->default_dst.remote_ip,
3555 NUD_REACHABLE | NUD_PERMANENT,
3556 vxlan->cfg.dst_port,
3557 vxlan->default_dst.remote_vni,
3558 vxlan->default_dst.remote_vni,
3559 vxlan->default_dst.remote_ifindex,
3560 NTF_SELF, &f);
3561 if (err)
3562 return err;
3565 err = register_netdevice(dev);
3566 if (err)
3567 goto errout;
3568 unregister = true;
3570 err = rtnl_configure_link(dev, NULL);
3571 if (err)
3572 goto errout;
3574 /* notify default fdb entry */
3575 if (f) {
3576 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3577 RTM_NEWNEIGH, true, extack);
3578 if (err)
3579 goto errout;
3582 list_add(&vxlan->next, &vn->vxlan_list);
3583 return 0;
3585 errout:
3586 /* unregister_netdevice() destroys the default FDB entry with deletion
3587 * notification. But the addition notification was not sent yet, so
3588 * destroy the entry by hand here.
3590 if (f)
3591 vxlan_fdb_destroy(vxlan, f, false, false);
3592 if (unregister)
3593 unregister_netdevice(dev);
3594 return err;
3597 /* Set/clear flags based on attribute */
3598 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3599 int attrtype, unsigned long mask, bool changelink,
3600 bool changelink_supported,
3601 struct netlink_ext_ack *extack)
3603 unsigned long flags;
3605 if (!tb[attrtype])
3606 return 0;
3608 if (changelink && !changelink_supported) {
3609 vxlan_flag_attr_error(attrtype, extack);
3610 return -EOPNOTSUPP;
3613 if (vxlan_policy[attrtype].type == NLA_FLAG)
3614 flags = conf->flags | mask;
3615 else if (nla_get_u8(tb[attrtype]))
3616 flags = conf->flags | mask;
3617 else
3618 flags = conf->flags & ~mask;
3620 conf->flags = flags;
3622 return 0;
3625 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3626 struct net_device *dev, struct vxlan_config *conf,
3627 bool changelink, struct netlink_ext_ack *extack)
3629 struct vxlan_dev *vxlan = netdev_priv(dev);
3630 int err = 0;
3632 memset(conf, 0, sizeof(*conf));
3634 /* if changelink operation, start with old existing cfg */
3635 if (changelink)
3636 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3638 if (data[IFLA_VXLAN_ID]) {
3639 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3641 if (changelink && (vni != conf->vni)) {
3642 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3643 return -EOPNOTSUPP;
3645 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3648 if (data[IFLA_VXLAN_GROUP]) {
3649 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3650 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3651 return -EOPNOTSUPP;
3654 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3655 conf->remote_ip.sa.sa_family = AF_INET;
3656 } else if (data[IFLA_VXLAN_GROUP6]) {
3657 if (!IS_ENABLED(CONFIG_IPV6)) {
3658 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3659 return -EPFNOSUPPORT;
3662 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3663 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3664 return -EOPNOTSUPP;
3667 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3668 conf->remote_ip.sa.sa_family = AF_INET6;
3671 if (data[IFLA_VXLAN_LOCAL]) {
3672 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3673 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
3674 return -EOPNOTSUPP;
3677 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3678 conf->saddr.sa.sa_family = AF_INET;
3679 } else if (data[IFLA_VXLAN_LOCAL6]) {
3680 if (!IS_ENABLED(CONFIG_IPV6)) {
3681 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
3682 return -EPFNOSUPPORT;
3685 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3686 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
3687 return -EOPNOTSUPP;
3690 /* TODO: respect scope id */
3691 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3692 conf->saddr.sa.sa_family = AF_INET6;
3695 if (data[IFLA_VXLAN_LINK])
3696 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3698 if (data[IFLA_VXLAN_TOS])
3699 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3701 if (data[IFLA_VXLAN_TTL])
3702 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3704 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3705 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3706 VXLAN_F_TTL_INHERIT, changelink, false,
3707 extack);
3708 if (err)
3709 return err;
3713 if (data[IFLA_VXLAN_LABEL])
3714 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3715 IPV6_FLOWLABEL_MASK;
3717 if (data[IFLA_VXLAN_LEARNING]) {
3718 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3719 VXLAN_F_LEARN, changelink, true,
3720 extack);
3721 if (err)
3722 return err;
3723 } else if (!changelink) {
3724 /* default to learn on a new device */
3725 conf->flags |= VXLAN_F_LEARN;
3728 if (data[IFLA_VXLAN_AGEING])
3729 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3731 if (data[IFLA_VXLAN_PROXY]) {
3732 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
3733 VXLAN_F_PROXY, changelink, false,
3734 extack);
3735 if (err)
3736 return err;
3739 if (data[IFLA_VXLAN_RSC]) {
3740 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
3741 VXLAN_F_RSC, changelink, false,
3742 extack);
3743 if (err)
3744 return err;
3747 if (data[IFLA_VXLAN_L2MISS]) {
3748 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
3749 VXLAN_F_L2MISS, changelink, false,
3750 extack);
3751 if (err)
3752 return err;
3755 if (data[IFLA_VXLAN_L3MISS]) {
3756 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
3757 VXLAN_F_L3MISS, changelink, false,
3758 extack);
3759 if (err)
3760 return err;
3763 if (data[IFLA_VXLAN_LIMIT]) {
3764 if (changelink) {
3765 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
3766 "Cannot change limit");
3767 return -EOPNOTSUPP;
3769 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3772 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3773 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
3774 VXLAN_F_COLLECT_METADATA, changelink, false,
3775 extack);
3776 if (err)
3777 return err;
3780 if (data[IFLA_VXLAN_PORT_RANGE]) {
3781 if (!changelink) {
3782 const struct ifla_vxlan_port_range *p
3783 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3784 conf->port_min = ntohs(p->low);
3785 conf->port_max = ntohs(p->high);
3786 } else {
3787 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3788 "Cannot change port range");
3789 return -EOPNOTSUPP;
3793 if (data[IFLA_VXLAN_PORT]) {
3794 if (changelink) {
3795 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
3796 "Cannot change port");
3797 return -EOPNOTSUPP;
3799 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3802 if (data[IFLA_VXLAN_UDP_CSUM]) {
3803 if (changelink) {
3804 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
3805 "Cannot change UDP_CSUM flag");
3806 return -EOPNOTSUPP;
3808 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3809 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3812 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3813 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3814 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
3815 false, extack);
3816 if (err)
3817 return err;
3820 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3821 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3822 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
3823 false, extack);
3824 if (err)
3825 return err;
3828 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3829 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
3830 VXLAN_F_REMCSUM_TX, changelink, false,
3831 extack);
3832 if (err)
3833 return err;
3836 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3837 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
3838 VXLAN_F_REMCSUM_RX, changelink, false,
3839 extack);
3840 if (err)
3841 return err;
3844 if (data[IFLA_VXLAN_GBP]) {
3845 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
3846 VXLAN_F_GBP, changelink, false, extack);
3847 if (err)
3848 return err;
3851 if (data[IFLA_VXLAN_GPE]) {
3852 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
3853 VXLAN_F_GPE, changelink, false,
3854 extack);
3855 if (err)
3856 return err;
3859 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3860 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
3861 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
3862 false, extack);
3863 if (err)
3864 return err;
3867 if (tb[IFLA_MTU]) {
3868 if (changelink) {
3869 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3870 "Cannot change mtu");
3871 return -EOPNOTSUPP;
3873 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3876 if (data[IFLA_VXLAN_DF])
3877 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3879 return 0;
3882 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3883 struct nlattr *tb[], struct nlattr *data[],
3884 struct netlink_ext_ack *extack)
3886 struct vxlan_config conf;
3887 int err;
3889 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
3890 if (err)
3891 return err;
3893 return __vxlan_dev_create(src_net, dev, &conf, extack);
3896 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3897 struct nlattr *data[],
3898 struct netlink_ext_ack *extack)
3900 struct vxlan_dev *vxlan = netdev_priv(dev);
3901 struct vxlan_rdst *dst = &vxlan->default_dst;
3902 struct net_device *lowerdev;
3903 struct vxlan_config conf;
3904 int err;
3906 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
3907 if (err)
3908 return err;
3910 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
3911 vxlan, extack);
3912 if (err)
3913 return err;
3915 /* handle default dst entry */
3916 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
3917 spin_lock_bh(&vxlan->hash_lock);
3918 if (!vxlan_addr_any(&conf.remote_ip)) {
3919 err = vxlan_fdb_update(vxlan, all_zeros_mac,
3920 &conf.remote_ip,
3921 NUD_REACHABLE | NUD_PERMANENT,
3922 NLM_F_APPEND | NLM_F_CREATE,
3923 vxlan->cfg.dst_port,
3924 conf.vni, conf.vni,
3925 conf.remote_ifindex,
3926 NTF_SELF, true, extack);
3927 if (err) {
3928 spin_unlock_bh(&vxlan->hash_lock);
3929 return err;
3932 if (!vxlan_addr_any(&dst->remote_ip))
3933 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3934 dst->remote_ip,
3935 vxlan->cfg.dst_port,
3936 dst->remote_vni,
3937 dst->remote_vni,
3938 dst->remote_ifindex,
3939 true);
3940 spin_unlock_bh(&vxlan->hash_lock);
3943 if (conf.age_interval != vxlan->cfg.age_interval)
3944 mod_timer(&vxlan->age_timer, jiffies);
3946 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
3947 return 0;
3950 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3952 struct vxlan_dev *vxlan = netdev_priv(dev);
3954 vxlan_flush(vxlan, true);
3956 list_del(&vxlan->next);
3957 unregister_netdevice_queue(dev, head);
3960 static size_t vxlan_get_size(const struct net_device *dev)
3963 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
3964 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3965 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3966 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3967 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3968 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
3969 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
3970 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
3971 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
3972 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
3973 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3974 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3975 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3976 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
3977 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
3978 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3979 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3980 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3981 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3982 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3983 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3984 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3985 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3986 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3990 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3992 const struct vxlan_dev *vxlan = netdev_priv(dev);
3993 const struct vxlan_rdst *dst = &vxlan->default_dst;
3994 struct ifla_vxlan_port_range ports = {
3995 .low = htons(vxlan->cfg.port_min),
3996 .high = htons(vxlan->cfg.port_max),
3999 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4000 goto nla_put_failure;
4002 if (!vxlan_addr_any(&dst->remote_ip)) {
4003 if (dst->remote_ip.sa.sa_family == AF_INET) {
4004 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4005 dst->remote_ip.sin.sin_addr.s_addr))
4006 goto nla_put_failure;
4007 #if IS_ENABLED(CONFIG_IPV6)
4008 } else {
4009 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4010 &dst->remote_ip.sin6.sin6_addr))
4011 goto nla_put_failure;
4012 #endif
4016 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4017 goto nla_put_failure;
4019 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4020 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4021 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4022 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4023 goto nla_put_failure;
4024 #if IS_ENABLED(CONFIG_IPV6)
4025 } else {
4026 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4027 &vxlan->cfg.saddr.sin6.sin6_addr))
4028 goto nla_put_failure;
4029 #endif
4033 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4034 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4035 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4036 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4037 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4038 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4039 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4040 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4041 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4042 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4043 nla_put_u8(skb, IFLA_VXLAN_RSC,
4044 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4045 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4046 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4047 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4048 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4049 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4050 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4051 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4052 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4053 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4054 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4055 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4056 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4057 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4058 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4059 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4060 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4061 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4062 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4063 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4064 goto nla_put_failure;
4066 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4067 goto nla_put_failure;
4069 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4070 nla_put_flag(skb, IFLA_VXLAN_GBP))
4071 goto nla_put_failure;
4073 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4074 nla_put_flag(skb, IFLA_VXLAN_GPE))
4075 goto nla_put_failure;
4077 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4078 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4079 goto nla_put_failure;
4081 return 0;
4083 nla_put_failure:
4084 return -EMSGSIZE;
4087 static struct net *vxlan_get_link_net(const struct net_device *dev)
4089 struct vxlan_dev *vxlan = netdev_priv(dev);
4091 return vxlan->net;
4094 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4095 .kind = "vxlan",
4096 .maxtype = IFLA_VXLAN_MAX,
4097 .policy = vxlan_policy,
4098 .priv_size = sizeof(struct vxlan_dev),
4099 .setup = vxlan_setup,
4100 .validate = vxlan_validate,
4101 .newlink = vxlan_newlink,
4102 .changelink = vxlan_changelink,
4103 .dellink = vxlan_dellink,
4104 .get_size = vxlan_get_size,
4105 .fill_info = vxlan_fill_info,
4106 .get_link_net = vxlan_get_link_net,
4109 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4110 u8 name_assign_type,
4111 struct vxlan_config *conf)
4113 struct nlattr *tb[IFLA_MAX + 1];
4114 struct net_device *dev;
4115 int err;
4117 memset(&tb, 0, sizeof(tb));
4119 dev = rtnl_create_link(net, name, name_assign_type,
4120 &vxlan_link_ops, tb, NULL);
4121 if (IS_ERR(dev))
4122 return dev;
4124 err = __vxlan_dev_create(net, dev, conf, NULL);
4125 if (err < 0) {
4126 free_netdev(dev);
4127 return ERR_PTR(err);
4130 err = rtnl_configure_link(dev, NULL);
4131 if (err < 0) {
4132 LIST_HEAD(list_kill);
4134 vxlan_dellink(dev, &list_kill);
4135 unregister_netdevice_many(&list_kill);
4136 return ERR_PTR(err);
4139 return dev;
4141 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4143 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4144 struct net_device *dev)
4146 struct vxlan_dev *vxlan, *next;
4147 LIST_HEAD(list_kill);
4149 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4150 struct vxlan_rdst *dst = &vxlan->default_dst;
4152 /* In case we created vxlan device with carrier
4153 * and we loose the carrier due to module unload
4154 * we also need to remove vxlan device. In other
4155 * cases, it's not necessary and remote_ifindex
4156 * is 0 here, so no matches.
4158 if (dst->remote_ifindex == dev->ifindex)
4159 vxlan_dellink(vxlan->dev, &list_kill);
4162 unregister_netdevice_many(&list_kill);
4165 static int vxlan_netdevice_event(struct notifier_block *unused,
4166 unsigned long event, void *ptr)
4168 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4169 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4171 if (event == NETDEV_UNREGISTER) {
4172 vxlan_offload_rx_ports(dev, false);
4173 vxlan_handle_lowerdev_unregister(vn, dev);
4174 } else if (event == NETDEV_REGISTER) {
4175 vxlan_offload_rx_ports(dev, true);
4176 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4177 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4178 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4181 return NOTIFY_DONE;
4184 static struct notifier_block vxlan_notifier_block __read_mostly = {
4185 .notifier_call = vxlan_netdevice_event,
4188 static void
4189 vxlan_fdb_offloaded_set(struct net_device *dev,
4190 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4192 struct vxlan_dev *vxlan = netdev_priv(dev);
4193 struct vxlan_rdst *rdst;
4194 struct vxlan_fdb *f;
4196 spin_lock_bh(&vxlan->hash_lock);
4198 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4199 if (!f)
4200 goto out;
4202 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4203 fdb_info->remote_port,
4204 fdb_info->remote_vni,
4205 fdb_info->remote_ifindex);
4206 if (!rdst)
4207 goto out;
4209 rdst->offloaded = fdb_info->offloaded;
4211 out:
4212 spin_unlock_bh(&vxlan->hash_lock);
4215 static int
4216 vxlan_fdb_external_learn_add(struct net_device *dev,
4217 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4219 struct vxlan_dev *vxlan = netdev_priv(dev);
4220 struct netlink_ext_ack *extack;
4221 int err;
4223 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4225 spin_lock_bh(&vxlan->hash_lock);
4226 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4227 NUD_REACHABLE,
4228 NLM_F_CREATE | NLM_F_REPLACE,
4229 fdb_info->remote_port,
4230 fdb_info->vni,
4231 fdb_info->remote_vni,
4232 fdb_info->remote_ifindex,
4233 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4234 false, extack);
4235 spin_unlock_bh(&vxlan->hash_lock);
4237 return err;
4240 static int
4241 vxlan_fdb_external_learn_del(struct net_device *dev,
4242 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4244 struct vxlan_dev *vxlan = netdev_priv(dev);
4245 struct vxlan_fdb *f;
4246 int err = 0;
4248 spin_lock_bh(&vxlan->hash_lock);
4250 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4251 if (!f)
4252 err = -ENOENT;
4253 else if (f->flags & NTF_EXT_LEARNED)
4254 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4255 fdb_info->remote_ip,
4256 fdb_info->remote_port,
4257 fdb_info->vni,
4258 fdb_info->remote_vni,
4259 fdb_info->remote_ifindex,
4260 false);
4262 spin_unlock_bh(&vxlan->hash_lock);
4264 return err;
4267 static int vxlan_switchdev_event(struct notifier_block *unused,
4268 unsigned long event, void *ptr)
4270 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4271 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4272 int err = 0;
4274 switch (event) {
4275 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4276 vxlan_fdb_offloaded_set(dev, ptr);
4277 break;
4278 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4279 fdb_info = ptr;
4280 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4281 if (err) {
4282 err = notifier_from_errno(err);
4283 break;
4285 fdb_info->offloaded = true;
4286 vxlan_fdb_offloaded_set(dev, fdb_info);
4287 break;
4288 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4289 fdb_info = ptr;
4290 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4291 if (err) {
4292 err = notifier_from_errno(err);
4293 break;
4295 fdb_info->offloaded = false;
4296 vxlan_fdb_offloaded_set(dev, fdb_info);
4297 break;
4300 return err;
4303 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4304 .notifier_call = vxlan_switchdev_event,
4307 static __net_init int vxlan_init_net(struct net *net)
4309 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4310 unsigned int h;
4312 INIT_LIST_HEAD(&vn->vxlan_list);
4313 spin_lock_init(&vn->sock_lock);
4315 for (h = 0; h < PORT_HASH_SIZE; ++h)
4316 INIT_HLIST_HEAD(&vn->sock_list[h]);
4318 return 0;
4321 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4323 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4324 struct vxlan_dev *vxlan, *next;
4325 struct net_device *dev, *aux;
4326 unsigned int h;
4328 for_each_netdev_safe(net, dev, aux)
4329 if (dev->rtnl_link_ops == &vxlan_link_ops)
4330 unregister_netdevice_queue(dev, head);
4332 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4333 /* If vxlan->dev is in the same netns, it has already been added
4334 * to the list by the previous loop.
4336 if (!net_eq(dev_net(vxlan->dev), net))
4337 unregister_netdevice_queue(vxlan->dev, head);
4340 for (h = 0; h < PORT_HASH_SIZE; ++h)
4341 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4344 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4346 struct net *net;
4347 LIST_HEAD(list);
4349 rtnl_lock();
4350 list_for_each_entry(net, net_list, exit_list)
4351 vxlan_destroy_tunnels(net, &list);
4353 unregister_netdevice_many(&list);
4354 rtnl_unlock();
4357 static struct pernet_operations vxlan_net_ops = {
4358 .init = vxlan_init_net,
4359 .exit_batch = vxlan_exit_batch_net,
4360 .id = &vxlan_net_id,
4361 .size = sizeof(struct vxlan_net),
4364 static int __init vxlan_init_module(void)
4366 int rc;
4368 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4370 rc = register_pernet_subsys(&vxlan_net_ops);
4371 if (rc)
4372 goto out1;
4374 rc = register_netdevice_notifier(&vxlan_notifier_block);
4375 if (rc)
4376 goto out2;
4378 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4379 if (rc)
4380 goto out3;
4382 rc = rtnl_link_register(&vxlan_link_ops);
4383 if (rc)
4384 goto out4;
4386 return 0;
4387 out4:
4388 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4389 out3:
4390 unregister_netdevice_notifier(&vxlan_notifier_block);
4391 out2:
4392 unregister_pernet_subsys(&vxlan_net_ops);
4393 out1:
4394 return rc;
4396 late_initcall(vxlan_init_module);
4398 static void __exit vxlan_cleanup_module(void)
4400 rtnl_link_unregister(&vxlan_link_ops);
4401 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4402 unregister_netdevice_notifier(&vxlan_notifier_block);
4403 unregister_pernet_subsys(&vxlan_net_ops);
4404 /* rcu_barrier() is called by netns */
4406 module_exit(vxlan_cleanup_module);
4408 MODULE_LICENSE("GPL");
4409 MODULE_VERSION(VXLAN_VERSION);
4410 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4411 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4412 MODULE_ALIAS_RTNL_LINK("vxlan");