blob: ce11fb2b05561cd8f01069da477677ef6e983520 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * VXLAN: Virtual eXtensible Local Area Network
4 *
5 * Copyright (c) 2012-2013 Vyatta Inc.
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
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>
29
30#if IS_ENABLED(CONFIG_IPV6)
31#include <net/ip6_tunnel.h>
32#include <net/ip6_checksum.h>
33#endif
34
35#define VXLAN_VERSION "0.1"
36
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 */
41
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.
45 */
46static unsigned short vxlan_port __read_mostly = 8472;
47module_param_named(udp_port, vxlan_port, ushort, 0444);
48MODULE_PARM_DESC(udp_port, "Destination UDP port");
49
50static bool log_ecn_error = true;
51module_param(log_ecn_error, bool, 0644);
52MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
53
54static unsigned int vxlan_net_id;
55static struct rtnl_link_ops vxlan_link_ops;
56
57static const u8 all_zeros_mac[ETH_ALEN + 2];
58
59static int vxlan_sock_add(struct vxlan_dev *vxlan);
60
61static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
62
63/* per-network namespace private data for this module */
64struct vxlan_net {
65 struct list_head vxlan_list;
66 struct hlist_head sock_list[PORT_HASH_SIZE];
67 spinlock_t sock_lock;
68};
69
70/* Forwarding table entry */
71struct 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 */
81};
82
83#define NTF_VXLAN_ADDED_BY_USER 0x100
84
85/* salt for hash table */
86static u32 vxlan_salt __read_mostly;
87
88static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
89{
90 return vs->flags & VXLAN_F_COLLECT_METADATA ||
91 ip_tunnel_collect_metadata();
92}
93
94#if IS_ENABLED(CONFIG_IPV6)
95static inline
96bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
97{
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;
104}
105
106static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
107{
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;
118 }
119}
120
121static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
122 const union vxlan_addr *ip)
123{
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);
128}
129
130#else /* !CONFIG_IPV6 */
131
132static inline
133bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
134{
135 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
136}
137
138static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
139{
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;
148 }
149}
150
151static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
152 const union vxlan_addr *ip)
153{
154 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
155}
156#endif
157
158/* Virtual Network hash table head */
159static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
160{
161 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
162}
163
164/* Socket hash table head */
165static inline struct hlist_head *vs_head(struct net *net, __be16 port)
166{
167 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
168
169 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
170}
171
172/* First remote destination for a forwarding entry.
173 * Guaranteed to be non-NULL because remotes are never deleted.
174 */
175static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
176{
177 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
178}
179
180static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
181{
182 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
183}
184
185/* Find VXLAN socket based on network namespace, address family and UDP port
186 * and enabled unshareable flags.
187 */
188static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
189 __be16 port, u32 flags, int ifindex)
190{
191 struct vxlan_sock *vs;
192
193 flags &= VXLAN_F_RCV_FLAGS;
194
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;
201 }
202 return NULL;
203}
204
205static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
206 __be32 vni)
207{
208 struct vxlan_dev_node *node;
209
210 /* For flow based devices, map all packets to VNI 0 */
211 if (vs->flags & VXLAN_F_COLLECT_METADATA)
212 vni = 0;
213
214 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
215 if (node->vxlan->default_dst.remote_vni != vni)
216 continue;
217
218 if (IS_ENABLED(CONFIG_IPV6)) {
219 const struct vxlan_config *cfg = &node->vxlan->cfg;
220
221 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
222 cfg->remote_ifindex != ifindex)
223 continue;
224 }
225
226 return node->vxlan;
227 }
228
229 return NULL;
230}
231
232/* Look up VNI in a per net namespace table */
233static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
234 __be32 vni, sa_family_t family,
235 __be16 port, u32 flags)
236{
237 struct vxlan_sock *vs;
238
239 vs = vxlan_find_sock(net, family, port, flags, ifindex);
240 if (!vs)
241 return NULL;
242
243 return vxlan_vs_find_vni(vs, ifindex, vni);
244}
245
246/* Fill in neighbour message in skbuff. */
247static 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)
251{
252 unsigned long now = jiffies;
253 struct nda_cacheinfo ci;
254 struct nlmsghdr *nlh;
255 struct ndmsg *ndm;
256 bool send_ip, send_eth;
257
258 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
259 if (nlh == NULL)
260 return -EMSGSIZE;
261
262 ndm = nlmsg_data(nlh);
263 memset(ndm, 0, sizeof(*ndm));
264
265 send_eth = send_ip = true;
266
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;
279
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;
284
285 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
286 goto nla_put_failure;
287
288 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
289 goto nla_put_failure;
290
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;
304
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;
309
310 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
311 goto nla_put_failure;
312
313 nlmsg_end(skb, nlh);
314 return 0;
315
316nla_put_failure:
317 nlmsg_cancel(skb, nlh);
318 return -EMSGSIZE;
319}
320
321static inline size_t vxlan_nlmsg_size(void)
322{
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));
331}
332
333static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
334 struct vxlan_rdst *rd, int type)
335{
336 struct net *net = dev_net(vxlan->dev);
337 struct sk_buff *skb;
338 int err = -ENOBUFS;
339
340 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
341 if (skb == NULL)
342 goto errout;
343
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;
350 }
351
352 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
353 return;
354errout:
355 if (err < 0)
356 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
357}
358
359static 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)
364{
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;
375}
376
377static 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)
382{
383 struct switchdev_notifier_vxlan_fdb_info info;
384 enum switchdev_notifier_type notifier_type;
385 int ret;
386
387 if (WARN_ON(!rd))
388 return 0;
389
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);
396}
397
398static 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)
401{
402 int err;
403
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;
416 }
417 }
418
419 __vxlan_fdb_notify(vxlan, fdb, rd, type);
420 return 0;
421}
422
423static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
424{
425 struct vxlan_dev *vxlan = netdev_priv(dev);
426 struct vxlan_fdb f = {
427 .state = NUD_STALE,
428 };
429 struct vxlan_rdst remote = {
430 .remote_ip = *ipa, /* goes to NDA_DST */
431 .remote_vni = cpu_to_be32(VXLAN_N_VID),
432 };
433
434 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
435}
436
437static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
438{
439 struct vxlan_fdb f = {
440 .state = NUD_STALE,
441 };
442 struct vxlan_rdst remote = { };
443
444 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
445
446 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
447}
448
449/* Hash Ethernet address */
450static u32 eth_hash(const unsigned char *addr)
451{
452 u64 value = get_unaligned((u64 *)addr);
453
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);
461}
462
463static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
464{
465 /* use 1 byte of OUI and 3 bytes of NIC */
466 u32 key = get_unaligned((u32 *)(addr + 2));
467
468 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
469}
470
471static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
472{
473 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
474 return eth_vni_hash(mac, vni);
475 else
476 return eth_hash(mac);
477}
478
479/* Hash chain to use given mac address */
480static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
481 const u8 *mac, __be32 vni)
482{
483 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
484}
485
486/* Look up Ethernet address in forwarding table */
487static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
488 const u8 *mac, __be32 vni)
489{
490 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
491 struct vxlan_fdb *f;
492
493 hlist_for_each_entry_rcu(f, head, hlist) {
494 if (ether_addr_equal(mac, f->eth_addr)) {
495 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
496 if (vni == f->vni)
497 return f;
498 } else {
499 return f;
500 }
501 }
502 }
503
504 return NULL;
505}
506
507static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
508 const u8 *mac, __be32 vni)
509{
510 struct vxlan_fdb *f;
511
512 f = __vxlan_find_mac(vxlan, mac, vni);
513 if (f && f->used != jiffies)
514 f->used = jiffies;
515
516 return f;
517}
518
519/* caller should hold vxlan->hash_lock */
520static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
521 union vxlan_addr *ip, __be16 port,
522 __be32 vni, __u32 ifindex)
523{
524 struct vxlan_rdst *rd;
525
526 list_for_each_entry(rd, &f->remotes, list) {
527 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
528 rd->remote_port == port &&
529 rd->remote_vni == vni &&
530 rd->remote_ifindex == ifindex)
531 return rd;
532 }
533
534 return NULL;
535}
536
537int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
538 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
539{
540 struct vxlan_dev *vxlan = netdev_priv(dev);
541 u8 eth_addr[ETH_ALEN + 2] = { 0 };
542 struct vxlan_rdst *rdst;
543 struct vxlan_fdb *f;
544 int rc = 0;
545
546 if (is_multicast_ether_addr(mac) ||
547 is_zero_ether_addr(mac))
548 return -EINVAL;
549
550 ether_addr_copy(eth_addr, mac);
551
552 rcu_read_lock();
553
554 f = __vxlan_find_mac(vxlan, eth_addr, vni);
555 if (!f) {
556 rc = -ENOENT;
557 goto out;
558 }
559
560 rdst = first_remote_rcu(f);
561 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
562
563out:
564 rcu_read_unlock();
565 return rc;
566}
567EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
568
569static int vxlan_fdb_notify_one(struct notifier_block *nb,
570 const struct vxlan_dev *vxlan,
571 const struct vxlan_fdb *f,
572 const struct vxlan_rdst *rdst,
573 struct netlink_ext_ack *extack)
574{
575 struct switchdev_notifier_vxlan_fdb_info fdb_info;
576 int rc;
577
578 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
579 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
580 &fdb_info);
581 return notifier_to_errno(rc);
582}
583
584int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
585 struct notifier_block *nb,
586 struct netlink_ext_ack *extack)
587{
588 struct vxlan_dev *vxlan;
589 struct vxlan_rdst *rdst;
590 struct vxlan_fdb *f;
591 unsigned int h;
592 int rc = 0;
593
594 if (!netif_is_vxlan(dev))
595 return -EINVAL;
596 vxlan = netdev_priv(dev);
597
598 for (h = 0; h < FDB_HASH_SIZE; ++h) {
599 spin_lock_bh(&vxlan->hash_lock[h]);
600 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
601 if (f->vni == vni) {
602 list_for_each_entry(rdst, &f->remotes, list) {
603 rc = vxlan_fdb_notify_one(nb, vxlan,
604 f, rdst,
605 extack);
606 if (rc)
607 goto unlock;
608 }
609 }
610 }
611 spin_unlock_bh(&vxlan->hash_lock[h]);
612 }
613 return 0;
614
615unlock:
616 spin_unlock_bh(&vxlan->hash_lock[h]);
617 return rc;
618}
619EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
620
621void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
622{
623 struct vxlan_dev *vxlan;
624 struct vxlan_rdst *rdst;
625 struct vxlan_fdb *f;
626 unsigned int h;
627
628 if (!netif_is_vxlan(dev))
629 return;
630 vxlan = netdev_priv(dev);
631
632 for (h = 0; h < FDB_HASH_SIZE; ++h) {
633 spin_lock_bh(&vxlan->hash_lock[h]);
634 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
635 if (f->vni == vni)
636 list_for_each_entry(rdst, &f->remotes, list)
637 rdst->offloaded = false;
638 spin_unlock_bh(&vxlan->hash_lock[h]);
639 }
640
641}
642EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
643
644/* Replace destination of unicast mac */
645static int vxlan_fdb_replace(struct vxlan_fdb *f,
646 union vxlan_addr *ip, __be16 port, __be32 vni,
647 __u32 ifindex, struct vxlan_rdst *oldrd)
648{
649 struct vxlan_rdst *rd;
650
651 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
652 if (rd)
653 return 0;
654
655 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
656 if (!rd)
657 return 0;
658
659 *oldrd = *rd;
660 dst_cache_reset(&rd->dst_cache);
661 rd->remote_ip = *ip;
662 rd->remote_port = port;
663 rd->remote_vni = vni;
664 rd->remote_ifindex = ifindex;
665 rd->offloaded = false;
666 return 1;
667}
668
669/* Add/update destinations for multicast */
670static int vxlan_fdb_append(struct vxlan_fdb *f,
671 union vxlan_addr *ip, __be16 port, __be32 vni,
672 __u32 ifindex, struct vxlan_rdst **rdp)
673{
674 struct vxlan_rdst *rd;
675
676 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
677 if (rd)
678 return 0;
679
680 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
681 if (rd == NULL)
682 return -ENOMEM;
683
684 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
685 kfree(rd);
686 return -ENOMEM;
687 }
688
689 rd->remote_ip = *ip;
690 rd->remote_port = port;
691 rd->offloaded = false;
692 rd->remote_vni = vni;
693 rd->remote_ifindex = ifindex;
694
695 list_add_tail_rcu(&rd->list, &f->remotes);
696
697 *rdp = rd;
698 return 1;
699}
700
701static bool vxlan_parse_gpe_proto(struct vxlanhdr *hdr, __be16 *protocol)
702{
703 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)hdr;
704
705 /* Need to have Next Protocol set for interfaces in GPE mode. */
706 if (!gpe->np_applied)
707 return false;
708 /* "The initial version is 0. If a receiver does not support the
709 * version indicated it MUST drop the packet.
710 */
711 if (gpe->version != 0)
712 return false;
713 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
714 * processing MUST occur." However, we don't implement OAM
715 * processing, thus drop the packet.
716 */
717 if (gpe->oam_flag)
718 return false;
719
720 *protocol = tun_p_to_eth_p(gpe->next_protocol);
721 if (!*protocol)
722 return false;
723
724 return true;
725}
726
727static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
728 unsigned int off,
729 struct vxlanhdr *vh, size_t hdrlen,
730 __be32 vni_field,
731 struct gro_remcsum *grc,
732 bool nopartial)
733{
734 size_t start, offset;
735
736 if (skb->remcsum_offload)
737 return vh;
738
739 if (!NAPI_GRO_CB(skb)->csum_valid)
740 return NULL;
741
742 start = vxlan_rco_start(vni_field);
743 offset = start + vxlan_rco_offset(vni_field);
744
745 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
746 start, offset, grc, nopartial);
747
748 skb->remcsum_offload = 1;
749
750 return vh;
751}
752
753static struct sk_buff *vxlan_gro_receive(struct sock *sk,
754 struct list_head *head,
755 struct sk_buff *skb)
756{
757 struct sk_buff *pp = NULL;
758 struct sk_buff *p;
759 struct vxlanhdr *vh, *vh2;
760 unsigned int hlen, off_vx;
761 int flush = 1;
762 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
763 __be32 flags;
764 struct gro_remcsum grc;
765
766 skb_gro_remcsum_init(&grc);
767
768 off_vx = skb_gro_offset(skb);
769 hlen = off_vx + sizeof(*vh);
770 vh = skb_gro_header_fast(skb, off_vx);
771 if (skb_gro_header_hard(skb, hlen)) {
772 vh = skb_gro_header_slow(skb, hlen, off_vx);
773 if (unlikely(!vh))
774 goto out;
775 }
776
777 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
778
779 flags = vh->vx_flags;
780
781 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
782 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
783 vh->vx_vni, &grc,
784 !!(vs->flags &
785 VXLAN_F_REMCSUM_NOPARTIAL));
786
787 if (!vh)
788 goto out;
789 }
790
791 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
792
793 list_for_each_entry(p, head, list) {
794 if (!NAPI_GRO_CB(p)->same_flow)
795 continue;
796
797 vh2 = (struct vxlanhdr *)(p->data + off_vx);
798 if (vh->vx_flags != vh2->vx_flags ||
799 vh->vx_vni != vh2->vx_vni) {
800 NAPI_GRO_CB(p)->same_flow = 0;
801 continue;
802 }
803 }
804
805 pp = call_gro_receive(eth_gro_receive, head, skb);
806 flush = 0;
807
808out:
809 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
810
811 return pp;
812}
813
814static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
815{
816 /* Sets 'skb->inner_mac_header' since we are always called with
817 * 'skb->encapsulation' set.
818 */
819 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
820}
821
822static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan,
823 const u8 *mac, __u16 state,
824 __be32 src_vni, __u16 ndm_flags)
825{
826 struct vxlan_fdb *f;
827
828 f = kmalloc(sizeof(*f), GFP_ATOMIC);
829 if (!f)
830 return NULL;
831 f->state = state;
832 f->flags = ndm_flags;
833 f->updated = f->used = jiffies;
834 f->vni = src_vni;
835 INIT_LIST_HEAD(&f->remotes);
836 memcpy(f->eth_addr, mac, ETH_ALEN);
837
838 return f;
839}
840
841static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
842 __be32 src_vni, struct vxlan_fdb *f)
843{
844 ++vxlan->addrcnt;
845 hlist_add_head_rcu(&f->hlist,
846 vxlan_fdb_head(vxlan, mac, src_vni));
847}
848
849static int vxlan_fdb_create(struct vxlan_dev *vxlan,
850 const u8 *mac, union vxlan_addr *ip,
851 __u16 state, __be16 port, __be32 src_vni,
852 __be32 vni, __u32 ifindex, __u16 ndm_flags,
853 struct vxlan_fdb **fdb)
854{
855 struct vxlan_rdst *rd = NULL;
856 struct vxlan_fdb *f;
857 int rc;
858
859 if (vxlan->cfg.addrmax &&
860 vxlan->addrcnt >= vxlan->cfg.addrmax)
861 return -ENOSPC;
862
863 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
864 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
865 if (!f)
866 return -ENOMEM;
867
868 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
869 if (rc < 0) {
870 kfree(f);
871 return rc;
872 }
873
874 *fdb = f;
875
876 return 0;
877}
878
879static void __vxlan_fdb_free(struct vxlan_fdb *f)
880{
881 struct vxlan_rdst *rd, *nd;
882
883 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
884 dst_cache_destroy(&rd->dst_cache);
885 kfree(rd);
886 }
887 kfree(f);
888}
889
890static void vxlan_fdb_free(struct rcu_head *head)
891{
892 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
893
894 __vxlan_fdb_free(f);
895}
896
897static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
898 bool do_notify, bool swdev_notify)
899{
900 struct vxlan_rdst *rd;
901
902 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
903
904 --vxlan->addrcnt;
905 if (do_notify)
906 list_for_each_entry(rd, &f->remotes, list)
907 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
908 swdev_notify, NULL);
909
910 hlist_del_rcu(&f->hlist);
911 call_rcu(&f->rcu, vxlan_fdb_free);
912}
913
914static void vxlan_dst_free(struct rcu_head *head)
915{
916 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
917
918 dst_cache_destroy(&rd->dst_cache);
919 kfree(rd);
920}
921
922static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
923 union vxlan_addr *ip,
924 __u16 state, __u16 flags,
925 __be16 port, __be32 vni,
926 __u32 ifindex, __u16 ndm_flags,
927 struct vxlan_fdb *f,
928 bool swdev_notify,
929 struct netlink_ext_ack *extack)
930{
931 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
932 struct vxlan_rdst *rd = NULL;
933 struct vxlan_rdst oldrd;
934 int notify = 0;
935 int rc = 0;
936 int err;
937
938 /* Do not allow an externally learned entry to take over an entry added
939 * by the user.
940 */
941 if (!(fdb_flags & NTF_EXT_LEARNED) ||
942 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
943 if (f->state != state) {
944 f->state = state;
945 f->updated = jiffies;
946 notify = 1;
947 }
948 if (f->flags != fdb_flags) {
949 f->flags = fdb_flags;
950 f->updated = jiffies;
951 notify = 1;
952 }
953 }
954
955 if ((flags & NLM_F_REPLACE)) {
956 /* Only change unicasts */
957 if (!(is_multicast_ether_addr(f->eth_addr) ||
958 is_zero_ether_addr(f->eth_addr))) {
959 rc = vxlan_fdb_replace(f, ip, port, vni,
960 ifindex, &oldrd);
961 notify |= rc;
962 } else {
963 return -EOPNOTSUPP;
964 }
965 }
966 if ((flags & NLM_F_APPEND) &&
967 (is_multicast_ether_addr(f->eth_addr) ||
968 is_zero_ether_addr(f->eth_addr))) {
969 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
970
971 if (rc < 0)
972 return rc;
973 notify |= rc;
974 }
975
976 if (ndm_flags & NTF_USE)
977 f->used = jiffies;
978
979 if (notify) {
980 if (rd == NULL)
981 rd = first_remote_rtnl(f);
982
983 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
984 swdev_notify, extack);
985 if (err)
986 goto err_notify;
987 }
988
989 return 0;
990
991err_notify:
992 if ((flags & NLM_F_REPLACE) && rc)
993 *rd = oldrd;
994 else if ((flags & NLM_F_APPEND) && rc) {
995 list_del_rcu(&rd->list);
996 call_rcu(&rd->rcu, vxlan_dst_free);
997 }
998 return err;
999}
1000
1001static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1002 const u8 *mac, union vxlan_addr *ip,
1003 __u16 state, __u16 flags,
1004 __be16 port, __be32 src_vni, __be32 vni,
1005 __u32 ifindex, __u16 ndm_flags,
1006 bool swdev_notify,
1007 struct netlink_ext_ack *extack)
1008{
1009 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1010 struct vxlan_fdb *f;
1011 int rc;
1012
1013 /* Disallow replace to add a multicast entry */
1014 if ((flags & NLM_F_REPLACE) &&
1015 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1016 return -EOPNOTSUPP;
1017
1018 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1019 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1020 vni, ifindex, fdb_flags, &f);
1021 if (rc < 0)
1022 return rc;
1023
1024 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1025 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1026 swdev_notify, extack);
1027 if (rc)
1028 goto err_notify;
1029
1030 return 0;
1031
1032err_notify:
1033 vxlan_fdb_destroy(vxlan, f, false, false);
1034 return rc;
1035}
1036
1037/* Add new entry to forwarding table -- assumes lock held */
1038static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1039 const u8 *mac, union vxlan_addr *ip,
1040 __u16 state, __u16 flags,
1041 __be16 port, __be32 src_vni, __be32 vni,
1042 __u32 ifindex, __u16 ndm_flags,
1043 bool swdev_notify,
1044 struct netlink_ext_ack *extack)
1045{
1046 struct vxlan_fdb *f;
1047
1048 f = __vxlan_find_mac(vxlan, mac, src_vni);
1049 if (f) {
1050 if (flags & NLM_F_EXCL) {
1051 netdev_dbg(vxlan->dev,
1052 "lost race to create %pM\n", mac);
1053 return -EEXIST;
1054 }
1055
1056 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1057 vni, ifindex, ndm_flags, f,
1058 swdev_notify, extack);
1059 } else {
1060 if (!(flags & NLM_F_CREATE))
1061 return -ENOENT;
1062
1063 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1064 port, src_vni, vni, ifindex,
1065 ndm_flags, swdev_notify, extack);
1066 }
1067}
1068
1069static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1070 struct vxlan_rdst *rd, bool swdev_notify)
1071{
1072 list_del_rcu(&rd->list);
1073 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1074 call_rcu(&rd->rcu, vxlan_dst_free);
1075}
1076
1077static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1078 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1079 __be32 *vni, u32 *ifindex)
1080{
1081 struct net *net = dev_net(vxlan->dev);
1082 int err;
1083
1084 if (tb[NDA_DST]) {
1085 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1086 if (err)
1087 return err;
1088 } else {
1089 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1090 if (remote->sa.sa_family == AF_INET) {
1091 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1092 ip->sa.sa_family = AF_INET;
1093#if IS_ENABLED(CONFIG_IPV6)
1094 } else {
1095 ip->sin6.sin6_addr = in6addr_any;
1096 ip->sa.sa_family = AF_INET6;
1097#endif
1098 }
1099 }
1100
1101 if (tb[NDA_PORT]) {
1102 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1103 return -EINVAL;
1104 *port = nla_get_be16(tb[NDA_PORT]);
1105 } else {
1106 *port = vxlan->cfg.dst_port;
1107 }
1108
1109 if (tb[NDA_VNI]) {
1110 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1111 return -EINVAL;
1112 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1113 } else {
1114 *vni = vxlan->default_dst.remote_vni;
1115 }
1116
1117 if (tb[NDA_SRC_VNI]) {
1118 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1119 return -EINVAL;
1120 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1121 } else {
1122 *src_vni = vxlan->default_dst.remote_vni;
1123 }
1124
1125 if (tb[NDA_IFINDEX]) {
1126 struct net_device *tdev;
1127
1128 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1129 return -EINVAL;
1130 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1131 tdev = __dev_get_by_index(net, *ifindex);
1132 if (!tdev)
1133 return -EADDRNOTAVAIL;
1134 } else {
1135 *ifindex = 0;
1136 }
1137
1138 return 0;
1139}
1140
1141/* Add static entry (via netlink) */
1142static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1143 struct net_device *dev,
1144 const unsigned char *addr, u16 vid, u16 flags,
1145 struct netlink_ext_ack *extack)
1146{
1147 struct vxlan_dev *vxlan = netdev_priv(dev);
1148 /* struct net *net = dev_net(vxlan->dev); */
1149 union vxlan_addr ip;
1150 __be16 port;
1151 __be32 src_vni, vni;
1152 u32 ifindex;
1153 u32 hash_index;
1154 int err;
1155
1156 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1157 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1158 ndm->ndm_state);
1159 return -EINVAL;
1160 }
1161
1162 if (tb[NDA_DST] == NULL)
1163 return -EINVAL;
1164
1165 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1166 if (err)
1167 return err;
1168
1169 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1170 return -EAFNOSUPPORT;
1171
1172 hash_index = fdb_head_index(vxlan, addr, src_vni);
1173 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1174 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1175 port, src_vni, vni, ifindex,
1176 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1177 true, extack);
1178 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1179
1180 return err;
1181}
1182
1183static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1184 const unsigned char *addr, union vxlan_addr ip,
1185 __be16 port, __be32 src_vni, __be32 vni,
1186 u32 ifindex, bool swdev_notify)
1187{
1188 struct vxlan_fdb *f;
1189 struct vxlan_rdst *rd = NULL;
1190 int err = -ENOENT;
1191
1192 f = vxlan_find_mac(vxlan, addr, src_vni);
1193 if (!f)
1194 return err;
1195
1196 if (!vxlan_addr_any(&ip)) {
1197 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1198 if (!rd)
1199 goto out;
1200 }
1201
1202 /* remove a destination if it's not the only one on the list,
1203 * otherwise destroy the fdb entry
1204 */
1205 if (rd && !list_is_singular(&f->remotes)) {
1206 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1207 goto out;
1208 }
1209
1210 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1211
1212out:
1213 return 0;
1214}
1215
1216/* Delete entry (via netlink) */
1217static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1218 struct net_device *dev,
1219 const unsigned char *addr, u16 vid)
1220{
1221 struct vxlan_dev *vxlan = netdev_priv(dev);
1222 union vxlan_addr ip;
1223 __be32 src_vni, vni;
1224 __be16 port;
1225 u32 ifindex;
1226 u32 hash_index;
1227 int err;
1228
1229 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
1230 if (err)
1231 return err;
1232
1233 hash_index = fdb_head_index(vxlan, addr, src_vni);
1234 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1235 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1236 true);
1237 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1238
1239 return err;
1240}
1241
1242/* Dump forwarding table */
1243static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1244 struct net_device *dev,
1245 struct net_device *filter_dev, int *idx)
1246{
1247 struct vxlan_dev *vxlan = netdev_priv(dev);
1248 unsigned int h;
1249 int err = 0;
1250
1251 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1252 struct vxlan_fdb *f;
1253
1254 rcu_read_lock();
1255 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1256 struct vxlan_rdst *rd;
1257
1258 list_for_each_entry_rcu(rd, &f->remotes, list) {
1259 if (*idx < cb->args[2])
1260 goto skip;
1261
1262 err = vxlan_fdb_info(skb, vxlan, f,
1263 NETLINK_CB(cb->skb).portid,
1264 cb->nlh->nlmsg_seq,
1265 RTM_NEWNEIGH,
1266 NLM_F_MULTI, rd);
1267 if (err < 0) {
1268 rcu_read_unlock();
1269 goto out;
1270 }
1271skip:
1272 *idx += 1;
1273 }
1274 }
1275 rcu_read_unlock();
1276 }
1277out:
1278 return err;
1279}
1280
1281static int vxlan_fdb_get(struct sk_buff *skb,
1282 struct nlattr *tb[],
1283 struct net_device *dev,
1284 const unsigned char *addr,
1285 u16 vid, u32 portid, u32 seq,
1286 struct netlink_ext_ack *extack)
1287{
1288 struct vxlan_dev *vxlan = netdev_priv(dev);
1289 struct vxlan_fdb *f;
1290 __be32 vni;
1291 int err;
1292
1293 if (tb[NDA_VNI])
1294 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1295 else
1296 vni = vxlan->default_dst.remote_vni;
1297
1298 rcu_read_lock();
1299
1300 f = __vxlan_find_mac(vxlan, addr, vni);
1301 if (!f) {
1302 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1303 err = -ENOENT;
1304 goto errout;
1305 }
1306
1307 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1308 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1309errout:
1310 rcu_read_unlock();
1311 return err;
1312}
1313
1314/* Watch incoming packets to learn mapping between Ethernet address
1315 * and Tunnel endpoint.
1316 * Return true if packet is bogus and should be dropped.
1317 */
1318static bool vxlan_snoop(struct net_device *dev,
1319 union vxlan_addr *src_ip, const u8 *src_mac,
1320 u32 src_ifindex, __be32 vni)
1321{
1322 struct vxlan_dev *vxlan = netdev_priv(dev);
1323 struct vxlan_fdb *f;
1324 u32 ifindex = 0;
1325
1326 /* Ignore packets from invalid src-address */
1327 if (!is_valid_ether_addr(src_mac))
1328 return true;
1329
1330#if IS_ENABLED(CONFIG_IPV6)
1331 if (src_ip->sa.sa_family == AF_INET6 &&
1332 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1333 ifindex = src_ifindex;
1334#endif
1335
1336 f = vxlan_find_mac(vxlan, src_mac, vni);
1337 if (likely(f)) {
1338 struct vxlan_rdst *rdst = first_remote_rcu(f);
1339
1340 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1341 rdst->remote_ifindex == ifindex))
1342 return false;
1343
1344 /* Don't migrate static entries, drop packets */
1345 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1346 return true;
1347
1348 if (net_ratelimit())
1349 netdev_info(dev,
1350 "%pM migrated from %pIS to %pIS\n",
1351 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1352
1353 rdst->remote_ip = *src_ip;
1354 f->updated = jiffies;
1355 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1356 } else {
1357 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1358
1359 /* learned new entry */
1360 spin_lock(&vxlan->hash_lock[hash_index]);
1361
1362 /* close off race between vxlan_flush and incoming packets */
1363 if (netif_running(dev))
1364 vxlan_fdb_update(vxlan, src_mac, src_ip,
1365 NUD_REACHABLE,
1366 NLM_F_EXCL|NLM_F_CREATE,
1367 vxlan->cfg.dst_port,
1368 vni,
1369 vxlan->default_dst.remote_vni,
1370 ifindex, NTF_SELF, true, NULL);
1371 spin_unlock(&vxlan->hash_lock[hash_index]);
1372 }
1373
1374 return false;
1375}
1376
1377/* See if multicast group is already in use by other ID */
1378static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1379{
1380 struct vxlan_dev *vxlan;
1381 struct vxlan_sock *sock4;
1382#if IS_ENABLED(CONFIG_IPV6)
1383 struct vxlan_sock *sock6;
1384#endif
1385 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1386
1387 sock4 = rtnl_dereference(dev->vn4_sock);
1388
1389 /* The vxlan_sock is only used by dev, leaving group has
1390 * no effect on other vxlan devices.
1391 */
1392 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1393 return false;
1394#if IS_ENABLED(CONFIG_IPV6)
1395 sock6 = rtnl_dereference(dev->vn6_sock);
1396 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1397 return false;
1398#endif
1399
1400 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1401 if (!netif_running(vxlan->dev) || vxlan == dev)
1402 continue;
1403
1404 if (family == AF_INET &&
1405 rtnl_dereference(vxlan->vn4_sock) != sock4)
1406 continue;
1407#if IS_ENABLED(CONFIG_IPV6)
1408 if (family == AF_INET6 &&
1409 rtnl_dereference(vxlan->vn6_sock) != sock6)
1410 continue;
1411#endif
1412
1413 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1414 &dev->default_dst.remote_ip))
1415 continue;
1416
1417 if (vxlan->default_dst.remote_ifindex !=
1418 dev->default_dst.remote_ifindex)
1419 continue;
1420
1421 return true;
1422 }
1423
1424 return false;
1425}
1426
1427static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1428{
1429 struct vxlan_net *vn;
1430
1431 if (!vs)
1432 return false;
1433 if (!refcount_dec_and_test(&vs->refcnt))
1434 return false;
1435
1436 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1437 spin_lock(&vn->sock_lock);
1438 hlist_del_rcu(&vs->hlist);
1439 udp_tunnel_notify_del_rx_port(vs->sock,
1440 (vs->flags & VXLAN_F_GPE) ?
1441 UDP_TUNNEL_TYPE_VXLAN_GPE :
1442 UDP_TUNNEL_TYPE_VXLAN);
1443 spin_unlock(&vn->sock_lock);
1444
1445 return true;
1446}
1447
1448static void vxlan_sock_release(struct vxlan_dev *vxlan)
1449{
1450 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1451#if IS_ENABLED(CONFIG_IPV6)
1452 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1453
1454 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1455#endif
1456
1457 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1458 synchronize_net();
1459
1460 vxlan_vs_del_dev(vxlan);
1461
1462 if (__vxlan_sock_release_prep(sock4)) {
1463 udp_tunnel_sock_release(sock4->sock);
1464 kfree(sock4);
1465 }
1466
1467#if IS_ENABLED(CONFIG_IPV6)
1468 if (__vxlan_sock_release_prep(sock6)) {
1469 udp_tunnel_sock_release(sock6->sock);
1470 kfree(sock6);
1471 }
1472#endif
1473}
1474
1475/* Update multicast group membership when first VNI on
1476 * multicast address is brought up
1477 */
1478static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1479{
1480 struct sock *sk;
1481 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1482 int ifindex = vxlan->default_dst.remote_ifindex;
1483 int ret = -EINVAL;
1484
1485 if (ip->sa.sa_family == AF_INET) {
1486 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1487 struct ip_mreqn mreq = {
1488 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1489 .imr_ifindex = ifindex,
1490 };
1491
1492 sk = sock4->sock->sk;
1493 lock_sock(sk);
1494 ret = ip_mc_join_group(sk, &mreq);
1495 release_sock(sk);
1496#if IS_ENABLED(CONFIG_IPV6)
1497 } else {
1498 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1499
1500 sk = sock6->sock->sk;
1501 lock_sock(sk);
1502 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1503 &ip->sin6.sin6_addr);
1504 release_sock(sk);
1505#endif
1506 }
1507
1508 return ret;
1509}
1510
1511/* Inverse of vxlan_igmp_join when last VNI is brought down */
1512static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1513{
1514 struct sock *sk;
1515 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1516 int ifindex = vxlan->default_dst.remote_ifindex;
1517 int ret = -EINVAL;
1518
1519 if (ip->sa.sa_family == AF_INET) {
1520 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1521 struct ip_mreqn mreq = {
1522 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1523 .imr_ifindex = ifindex,
1524 };
1525
1526 sk = sock4->sock->sk;
1527 lock_sock(sk);
1528 ret = ip_mc_leave_group(sk, &mreq);
1529 release_sock(sk);
1530#if IS_ENABLED(CONFIG_IPV6)
1531 } else {
1532 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1533
1534 sk = sock6->sock->sk;
1535 lock_sock(sk);
1536 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1537 &ip->sin6.sin6_addr);
1538 release_sock(sk);
1539#endif
1540 }
1541
1542 return ret;
1543}
1544
1545static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1546 struct sk_buff *skb, u32 vxflags)
1547{
1548 size_t start, offset;
1549
1550 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1551 goto out;
1552
1553 start = vxlan_rco_start(unparsed->vx_vni);
1554 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1555
1556 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1557 return false;
1558
1559 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1560 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1561out:
1562 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1563 unparsed->vx_vni &= VXLAN_VNI_MASK;
1564 return true;
1565}
1566
1567static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1568 struct sk_buff *skb, u32 vxflags,
1569 struct vxlan_metadata *md)
1570{
1571 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1572 struct metadata_dst *tun_dst;
1573
1574 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1575 goto out;
1576
1577 md->gbp = ntohs(gbp->policy_id);
1578
1579 tun_dst = (struct metadata_dst *)skb_dst(skb);
1580 if (tun_dst) {
1581 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1582 tun_dst->u.tun_info.options_len = sizeof(*md);
1583 }
1584 if (gbp->dont_learn)
1585 md->gbp |= VXLAN_GBP_DONT_LEARN;
1586
1587 if (gbp->policy_applied)
1588 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1589
1590 /* In flow-based mode, GBP is carried in dst_metadata */
1591 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1592 skb->mark = md->gbp;
1593out:
1594 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1595}
1596
1597static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1598 struct vxlan_sock *vs,
1599 struct sk_buff *skb, __be32 vni)
1600{
1601 union vxlan_addr saddr;
1602 u32 ifindex = skb->dev->ifindex;
1603
1604 skb_reset_mac_header(skb);
1605 skb->protocol = eth_type_trans(skb, vxlan->dev);
1606 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1607
1608 /* Ignore packet loops (and multicast echo) */
1609 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1610 return false;
1611
1612 /* Get address from the outer IP header */
1613 if (vxlan_get_sk_family(vs) == AF_INET) {
1614 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1615 saddr.sa.sa_family = AF_INET;
1616#if IS_ENABLED(CONFIG_IPV6)
1617 } else {
1618 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1619 saddr.sa.sa_family = AF_INET6;
1620#endif
1621 }
1622
1623 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1624 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1625 return false;
1626
1627 return true;
1628}
1629
1630static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1631 struct sk_buff *skb)
1632{
1633 int err = 0;
1634
1635 if (vxlan_get_sk_family(vs) == AF_INET)
1636 err = IP_ECN_decapsulate(oiph, skb);
1637#if IS_ENABLED(CONFIG_IPV6)
1638 else
1639 err = IP6_ECN_decapsulate(oiph, skb);
1640#endif
1641
1642 if (unlikely(err) && log_ecn_error) {
1643 if (vxlan_get_sk_family(vs) == AF_INET)
1644 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1645 &((struct iphdr *)oiph)->saddr,
1646 ((struct iphdr *)oiph)->tos);
1647 else
1648 net_info_ratelimited("non-ECT from %pI6\n",
1649 &((struct ipv6hdr *)oiph)->saddr);
1650 }
1651 return err <= 1;
1652}
1653
1654/* Callback from net/ipv4/udp.c to receive packets */
1655static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1656{
1657 struct pcpu_sw_netstats *stats;
1658 struct vxlan_dev *vxlan;
1659 struct vxlan_sock *vs;
1660 struct vxlanhdr unparsed;
1661 struct vxlan_metadata _md;
1662 struct vxlan_metadata *md = &_md;
1663 __be16 protocol = htons(ETH_P_TEB);
1664 bool raw_proto = false;
1665 void *oiph;
1666 __be32 vni = 0;
1667
1668 /* Need UDP and VXLAN header to be present */
1669 if (!pskb_may_pull(skb, VXLAN_HLEN))
1670 goto drop;
1671
1672 unparsed = *vxlan_hdr(skb);
1673 /* VNI flag always required to be set */
1674 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1675 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1676 ntohl(vxlan_hdr(skb)->vx_flags),
1677 ntohl(vxlan_hdr(skb)->vx_vni));
1678 /* Return non vxlan pkt */
1679 goto drop;
1680 }
1681 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1682 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1683
1684 vs = rcu_dereference_sk_user_data(sk);
1685 if (!vs)
1686 goto drop;
1687
1688 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1689
1690 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1691 if (!vxlan)
1692 goto drop;
1693
1694 /* For backwards compatibility, only allow reserved fields to be
1695 * used by VXLAN extensions if explicitly requested.
1696 */
1697 if (vs->flags & VXLAN_F_GPE) {
1698 if (!vxlan_parse_gpe_proto(&unparsed, &protocol))
1699 goto drop;
1700 unparsed.vx_flags &= ~VXLAN_GPE_USED_BITS;
1701 raw_proto = true;
1702 }
1703
1704 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1705 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1706 goto drop;
1707
1708 if (vxlan_collect_metadata(vs)) {
1709 struct metadata_dst *tun_dst;
1710
1711 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1712 key32_to_tunnel_id(vni), sizeof(*md));
1713
1714 if (!tun_dst)
1715 goto drop;
1716
1717 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1718
1719 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1720 } else {
1721 memset(md, 0, sizeof(*md));
1722 }
1723
1724 if (vs->flags & VXLAN_F_REMCSUM_RX)
1725 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1726 goto drop;
1727 if (vs->flags & VXLAN_F_GBP)
1728 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1729 /* Note that GBP and GPE can never be active together. This is
1730 * ensured in vxlan_dev_configure.
1731 */
1732
1733 if (unparsed.vx_flags || unparsed.vx_vni) {
1734 /* If there are any unprocessed flags remaining treat
1735 * this as a malformed packet. This behavior diverges from
1736 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1737 * in reserved fields are to be ignored. The approach here
1738 * maintains compatibility with previous stack code, and also
1739 * is more robust and provides a little more security in
1740 * adding extensions to VXLAN.
1741 */
1742 goto drop;
1743 }
1744
1745 if (!raw_proto) {
1746 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1747 goto drop;
1748 } else {
1749 skb_reset_mac_header(skb);
1750 skb->dev = vxlan->dev;
1751 skb->pkt_type = PACKET_HOST;
1752 }
1753
1754 oiph = skb_network_header(skb);
1755 skb_reset_network_header(skb);
1756
1757 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1758 ++vxlan->dev->stats.rx_frame_errors;
1759 ++vxlan->dev->stats.rx_errors;
1760 goto drop;
1761 }
1762
1763 rcu_read_lock();
1764
1765 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1766 rcu_read_unlock();
1767 atomic_long_inc(&vxlan->dev->rx_dropped);
1768 goto drop;
1769 }
1770
1771 stats = this_cpu_ptr(vxlan->dev->tstats);
1772 u64_stats_update_begin(&stats->syncp);
1773 stats->rx_packets++;
1774 stats->rx_bytes += skb->len;
1775 u64_stats_update_end(&stats->syncp);
1776
1777 gro_cells_receive(&vxlan->gro_cells, skb);
1778
1779 rcu_read_unlock();
1780
1781 return 0;
1782
1783drop:
1784 /* Consume bad packet */
1785 kfree_skb(skb);
1786 return 0;
1787}
1788
1789/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1790static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1791{
1792 struct vxlan_dev *vxlan;
1793 struct vxlan_sock *vs;
1794 struct vxlanhdr *hdr;
1795 __be32 vni;
1796
1797 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1798 return -EINVAL;
1799
1800 hdr = vxlan_hdr(skb);
1801
1802 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1803 return -EINVAL;
1804
1805 vs = rcu_dereference_sk_user_data(sk);
1806 if (!vs)
1807 return -ENOENT;
1808
1809 vni = vxlan_vni(hdr->vx_vni);
1810 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1811 if (!vxlan)
1812 return -ENOENT;
1813
1814 return 0;
1815}
1816
1817static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1818{
1819 struct vxlan_dev *vxlan = netdev_priv(dev);
1820 struct arphdr *parp;
1821 u8 *arpptr, *sha;
1822 __be32 sip, tip;
1823 struct neighbour *n;
1824
1825 if (dev->flags & IFF_NOARP)
1826 goto out;
1827
1828 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1829 dev->stats.tx_dropped++;
1830 goto out;
1831 }
1832 parp = arp_hdr(skb);
1833
1834 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1835 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1836 parp->ar_pro != htons(ETH_P_IP) ||
1837 parp->ar_op != htons(ARPOP_REQUEST) ||
1838 parp->ar_hln != dev->addr_len ||
1839 parp->ar_pln != 4)
1840 goto out;
1841 arpptr = (u8 *)parp + sizeof(struct arphdr);
1842 sha = arpptr;
1843 arpptr += dev->addr_len; /* sha */
1844 memcpy(&sip, arpptr, sizeof(sip));
1845 arpptr += sizeof(sip);
1846 arpptr += dev->addr_len; /* tha */
1847 memcpy(&tip, arpptr, sizeof(tip));
1848
1849 if (ipv4_is_loopback(tip) ||
1850 ipv4_is_multicast(tip))
1851 goto out;
1852
1853 n = neigh_lookup(&arp_tbl, &tip, dev);
1854
1855 if (n) {
1856 struct vxlan_fdb *f;
1857 struct sk_buff *reply;
1858
1859 if (!(n->nud_state & NUD_CONNECTED)) {
1860 neigh_release(n);
1861 goto out;
1862 }
1863
1864 f = vxlan_find_mac(vxlan, n->ha, vni);
1865 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1866 /* bridge-local neighbor */
1867 neigh_release(n);
1868 goto out;
1869 }
1870
1871 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1872 n->ha, sha);
1873
1874 neigh_release(n);
1875
1876 if (reply == NULL)
1877 goto out;
1878
1879 skb_reset_mac_header(reply);
1880 __skb_pull(reply, skb_network_offset(reply));
1881 reply->ip_summed = CHECKSUM_UNNECESSARY;
1882 reply->pkt_type = PACKET_HOST;
1883
1884 if (netif_rx_ni(reply) == NET_RX_DROP)
1885 dev->stats.rx_dropped++;
1886 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1887 union vxlan_addr ipa = {
1888 .sin.sin_addr.s_addr = tip,
1889 .sin.sin_family = AF_INET,
1890 };
1891
1892 vxlan_ip_miss(dev, &ipa);
1893 }
1894out:
1895 consume_skb(skb);
1896 return NETDEV_TX_OK;
1897}
1898
1899#if IS_ENABLED(CONFIG_IPV6)
1900static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1901 struct neighbour *n, bool isrouter)
1902{
1903 struct net_device *dev = request->dev;
1904 struct sk_buff *reply;
1905 struct nd_msg *ns, *na;
1906 struct ipv6hdr *pip6;
1907 u8 *daddr;
1908 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1909 int ns_olen;
1910 int i, len;
1911
1912 if (dev == NULL || !pskb_may_pull(request, request->len))
1913 return NULL;
1914
1915 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1916 sizeof(*na) + na_olen + dev->needed_tailroom;
1917 reply = alloc_skb(len, GFP_ATOMIC);
1918 if (reply == NULL)
1919 return NULL;
1920
1921 reply->protocol = htons(ETH_P_IPV6);
1922 reply->dev = dev;
1923 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1924 skb_push(reply, sizeof(struct ethhdr));
1925 skb_reset_mac_header(reply);
1926
1927 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
1928
1929 daddr = eth_hdr(request)->h_source;
1930 ns_olen = request->len - skb_network_offset(request) -
1931 sizeof(struct ipv6hdr) - sizeof(*ns);
1932 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1933 if (!ns->opt[i + 1]) {
1934 kfree_skb(reply);
1935 return NULL;
1936 }
1937 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1938 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1939 break;
1940 }
1941 }
1942
1943 /* Ethernet header */
1944 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1945 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1946 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1947 reply->protocol = htons(ETH_P_IPV6);
1948
1949 skb_pull(reply, sizeof(struct ethhdr));
1950 skb_reset_network_header(reply);
1951 skb_put(reply, sizeof(struct ipv6hdr));
1952
1953 /* IPv6 header */
1954
1955 pip6 = ipv6_hdr(reply);
1956 memset(pip6, 0, sizeof(struct ipv6hdr));
1957 pip6->version = 6;
1958 pip6->priority = ipv6_hdr(request)->priority;
1959 pip6->nexthdr = IPPROTO_ICMPV6;
1960 pip6->hop_limit = 255;
1961 pip6->daddr = ipv6_hdr(request)->saddr;
1962 pip6->saddr = *(struct in6_addr *)n->primary_key;
1963
1964 skb_pull(reply, sizeof(struct ipv6hdr));
1965 skb_reset_transport_header(reply);
1966
1967 /* Neighbor Advertisement */
1968 na = skb_put_zero(reply, sizeof(*na) + na_olen);
1969 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1970 na->icmph.icmp6_router = isrouter;
1971 na->icmph.icmp6_override = 1;
1972 na->icmph.icmp6_solicited = 1;
1973 na->target = ns->target;
1974 ether_addr_copy(&na->opt[2], n->ha);
1975 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1976 na->opt[1] = na_olen >> 3;
1977
1978 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1979 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1980 csum_partial(na, sizeof(*na)+na_olen, 0));
1981
1982 pip6->payload_len = htons(sizeof(*na)+na_olen);
1983
1984 skb_push(reply, sizeof(struct ipv6hdr));
1985
1986 reply->ip_summed = CHECKSUM_UNNECESSARY;
1987
1988 return reply;
1989}
1990
1991static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1992{
1993 struct vxlan_dev *vxlan = netdev_priv(dev);
1994 const struct in6_addr *daddr;
1995 const struct ipv6hdr *iphdr;
1996 struct inet6_dev *in6_dev;
1997 struct neighbour *n;
1998 struct nd_msg *msg;
1999
2000 rcu_read_lock();
2001 in6_dev = __in6_dev_get(dev);
2002 if (!in6_dev)
2003 goto out;
2004
2005 iphdr = ipv6_hdr(skb);
2006 daddr = &iphdr->daddr;
2007 msg = (struct nd_msg *)(iphdr + 1);
2008
2009 if (ipv6_addr_loopback(daddr) ||
2010 ipv6_addr_is_multicast(&msg->target))
2011 goto out;
2012
2013 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2014
2015 if (n) {
2016 struct vxlan_fdb *f;
2017 struct sk_buff *reply;
2018
2019 if (!(n->nud_state & NUD_CONNECTED)) {
2020 neigh_release(n);
2021 goto out;
2022 }
2023
2024 f = vxlan_find_mac(vxlan, n->ha, vni);
2025 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2026 /* bridge-local neighbor */
2027 neigh_release(n);
2028 goto out;
2029 }
2030
2031 reply = vxlan_na_create(skb, n,
2032 !!(f ? f->flags & NTF_ROUTER : 0));
2033
2034 neigh_release(n);
2035
2036 if (reply == NULL)
2037 goto out;
2038
2039 if (netif_rx_ni(reply) == NET_RX_DROP)
2040 dev->stats.rx_dropped++;
2041
2042 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2043 union vxlan_addr ipa = {
2044 .sin6.sin6_addr = msg->target,
2045 .sin6.sin6_family = AF_INET6,
2046 };
2047
2048 vxlan_ip_miss(dev, &ipa);
2049 }
2050
2051out:
2052 rcu_read_unlock();
2053 consume_skb(skb);
2054 return NETDEV_TX_OK;
2055}
2056#endif
2057
2058static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2059{
2060 struct vxlan_dev *vxlan = netdev_priv(dev);
2061 struct neighbour *n;
2062
2063 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2064 return false;
2065
2066 n = NULL;
2067 switch (ntohs(eth_hdr(skb)->h_proto)) {
2068 case ETH_P_IP:
2069 {
2070 struct iphdr *pip;
2071
2072 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2073 return false;
2074 pip = ip_hdr(skb);
2075 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2076 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2077 union vxlan_addr ipa = {
2078 .sin.sin_addr.s_addr = pip->daddr,
2079 .sin.sin_family = AF_INET,
2080 };
2081
2082 vxlan_ip_miss(dev, &ipa);
2083 return false;
2084 }
2085
2086 break;
2087 }
2088#if IS_ENABLED(CONFIG_IPV6)
2089 case ETH_P_IPV6:
2090 {
2091 struct ipv6hdr *pip6;
2092
2093 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2094 return false;
2095 pip6 = ipv6_hdr(skb);
2096 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2097 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2098 union vxlan_addr ipa = {
2099 .sin6.sin6_addr = pip6->daddr,
2100 .sin6.sin6_family = AF_INET6,
2101 };
2102
2103 vxlan_ip_miss(dev, &ipa);
2104 return false;
2105 }
2106
2107 break;
2108 }
2109#endif
2110 default:
2111 return false;
2112 }
2113
2114 if (n) {
2115 bool diff;
2116
2117 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2118 if (diff) {
2119 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2120 dev->addr_len);
2121 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2122 }
2123 neigh_release(n);
2124 return diff;
2125 }
2126
2127 return false;
2128}
2129
2130static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2131 struct vxlan_metadata *md)
2132{
2133 struct vxlanhdr_gbp *gbp;
2134
2135 if (!md->gbp)
2136 return;
2137
2138 gbp = (struct vxlanhdr_gbp *)vxh;
2139 vxh->vx_flags |= VXLAN_HF_GBP;
2140
2141 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2142 gbp->dont_learn = 1;
2143
2144 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2145 gbp->policy_applied = 1;
2146
2147 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2148}
2149
2150static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2151 __be16 protocol)
2152{
2153 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2154
2155 gpe->np_applied = 1;
2156 gpe->next_protocol = tun_p_from_eth_p(protocol);
2157 if (!gpe->next_protocol)
2158 return -EPFNOSUPPORT;
2159 return 0;
2160}
2161
2162static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2163 int iphdr_len, __be32 vni,
2164 struct vxlan_metadata *md, u32 vxflags,
2165 bool udp_sum)
2166{
2167 struct vxlanhdr *vxh;
2168 int min_headroom;
2169 int err;
2170 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2171 __be16 inner_protocol = htons(ETH_P_TEB);
2172
2173 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2174 skb->ip_summed == CHECKSUM_PARTIAL) {
2175 int csum_start = skb_checksum_start_offset(skb);
2176
2177 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2178 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2179 (skb->csum_offset == offsetof(struct udphdr, check) ||
2180 skb->csum_offset == offsetof(struct tcphdr, check)))
2181 type |= SKB_GSO_TUNNEL_REMCSUM;
2182 }
2183
2184 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2185 + VXLAN_HLEN + iphdr_len;
2186
2187 /* Need space for new headers (invalidates iph ptr) */
2188 err = skb_cow_head(skb, min_headroom);
2189 if (unlikely(err))
2190 return err;
2191
2192 err = iptunnel_handle_offloads(skb, type);
2193 if (err)
2194 return err;
2195
2196 vxh = __skb_push(skb, sizeof(*vxh));
2197 vxh->vx_flags = VXLAN_HF_VNI;
2198 vxh->vx_vni = vxlan_vni_field(vni);
2199
2200 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2201 unsigned int start;
2202
2203 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2204 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2205 vxh->vx_flags |= VXLAN_HF_RCO;
2206
2207 if (!skb_is_gso(skb)) {
2208 skb->ip_summed = CHECKSUM_NONE;
2209 skb->encapsulation = 0;
2210 }
2211 }
2212
2213 if (vxflags & VXLAN_F_GBP)
2214 vxlan_build_gbp_hdr(vxh, vxflags, md);
2215 if (vxflags & VXLAN_F_GPE) {
2216 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2217 if (err < 0)
2218 return err;
2219 inner_protocol = skb->protocol;
2220 }
2221
2222 skb_set_inner_protocol(skb, inner_protocol);
2223 return 0;
2224}
2225
2226static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2227 struct vxlan_sock *sock4,
2228 struct sk_buff *skb, int oif, u8 tos,
2229 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2230 struct dst_cache *dst_cache,
2231 const struct ip_tunnel_info *info)
2232{
2233 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2234 struct rtable *rt = NULL;
2235 struct flowi4 fl4;
2236
2237 if (!sock4)
2238 return ERR_PTR(-EIO);
2239
2240 if (tos && !info)
2241 use_cache = false;
2242 if (use_cache) {
2243 rt = dst_cache_get_ip4(dst_cache, saddr);
2244 if (rt)
2245 return rt;
2246 }
2247
2248 memset(&fl4, 0, sizeof(fl4));
2249 fl4.flowi4_oif = oif;
2250 fl4.flowi4_tos = RT_TOS(tos);
2251 fl4.flowi4_mark = skb->mark;
2252 fl4.flowi4_proto = IPPROTO_UDP;
2253 fl4.daddr = daddr;
2254 fl4.saddr = *saddr;
2255 fl4.fl4_dport = dport;
2256 fl4.fl4_sport = sport;
2257
2258 rt = ip_route_output_key(vxlan->net, &fl4);
2259 if (!IS_ERR(rt)) {
2260 if (rt->dst.dev == dev) {
2261 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2262 ip_rt_put(rt);
2263 return ERR_PTR(-ELOOP);
2264 }
2265
2266 *saddr = fl4.saddr;
2267 if (use_cache)
2268 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2269 } else {
2270 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2271 return ERR_PTR(-ENETUNREACH);
2272 }
2273 return rt;
2274}
2275
2276#if IS_ENABLED(CONFIG_IPV6)
2277static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2278 struct net_device *dev,
2279 struct vxlan_sock *sock6,
2280 struct sk_buff *skb, int oif, u8 tos,
2281 __be32 label,
2282 const struct in6_addr *daddr,
2283 struct in6_addr *saddr,
2284 __be16 dport, __be16 sport,
2285 struct dst_cache *dst_cache,
2286 const struct ip_tunnel_info *info)
2287{
2288 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2289 struct dst_entry *ndst;
2290 struct flowi6 fl6;
2291
2292 if (!sock6)
2293 return ERR_PTR(-EIO);
2294
2295 if (tos && !info)
2296 use_cache = false;
2297 if (use_cache) {
2298 ndst = dst_cache_get_ip6(dst_cache, saddr);
2299 if (ndst)
2300 return ndst;
2301 }
2302
2303 memset(&fl6, 0, sizeof(fl6));
2304 fl6.flowi6_oif = oif;
2305 fl6.daddr = *daddr;
2306 fl6.saddr = *saddr;
2307 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2308 fl6.flowi6_mark = skb->mark;
2309 fl6.flowi6_proto = IPPROTO_UDP;
2310 fl6.fl6_dport = dport;
2311 fl6.fl6_sport = sport;
2312
2313 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2314 &fl6, NULL);
2315 if (unlikely(IS_ERR(ndst))) {
2316 netdev_dbg(dev, "no route to %pI6\n", daddr);
2317 return ERR_PTR(-ENETUNREACH);
2318 }
2319
2320 if (unlikely(ndst->dev == dev)) {
2321 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2322 dst_release(ndst);
2323 return ERR_PTR(-ELOOP);
2324 }
2325
2326 *saddr = fl6.saddr;
2327 if (use_cache)
2328 dst_cache_set_ip6(dst_cache, ndst, saddr);
2329 return ndst;
2330}
2331#endif
2332
2333/* Bypass encapsulation if the destination is local */
2334static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2335 struct vxlan_dev *dst_vxlan, __be32 vni)
2336{
2337 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2338 union vxlan_addr loopback;
2339 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2340 struct net_device *dev;
2341 int len = skb->len;
2342
2343 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2344 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2345 skb->pkt_type = PACKET_HOST;
2346 skb->encapsulation = 0;
2347 skb->dev = dst_vxlan->dev;
2348 __skb_pull(skb, skb_network_offset(skb));
2349
2350 if (remote_ip->sa.sa_family == AF_INET) {
2351 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2352 loopback.sa.sa_family = AF_INET;
2353#if IS_ENABLED(CONFIG_IPV6)
2354 } else {
2355 loopback.sin6.sin6_addr = in6addr_loopback;
2356 loopback.sa.sa_family = AF_INET6;
2357#endif
2358 }
2359
2360 rcu_read_lock();
2361 dev = skb->dev;
2362 if (unlikely(!(dev->flags & IFF_UP))) {
2363 kfree_skb(skb);
2364 goto drop;
2365 }
2366
2367 if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
2368 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2369
2370 u64_stats_update_begin(&tx_stats->syncp);
2371 tx_stats->tx_packets++;
2372 tx_stats->tx_bytes += len;
2373 u64_stats_update_end(&tx_stats->syncp);
2374
2375 if (netif_rx(skb) == NET_RX_SUCCESS) {
2376 u64_stats_update_begin(&rx_stats->syncp);
2377 rx_stats->rx_packets++;
2378 rx_stats->rx_bytes += len;
2379 u64_stats_update_end(&rx_stats->syncp);
2380 } else {
2381drop:
2382 dev->stats.rx_dropped++;
2383 }
2384 rcu_read_unlock();
2385}
2386
2387static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2388 struct vxlan_dev *vxlan,
2389 union vxlan_addr *daddr,
2390 __be16 dst_port, int dst_ifindex, __be32 vni,
2391 struct dst_entry *dst,
2392 u32 rt_flags)
2393{
2394#if IS_ENABLED(CONFIG_IPV6)
2395 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2396 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2397 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2398 */
2399 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2400#endif
2401 /* Bypass encapsulation if the destination is local */
2402 if (rt_flags & RTCF_LOCAL &&
2403 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2404 struct vxlan_dev *dst_vxlan;
2405
2406 dst_release(dst);
2407 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2408 daddr->sa.sa_family, dst_port,
2409 vxlan->cfg.flags);
2410 if (!dst_vxlan) {
2411 dev->stats.tx_errors++;
2412 kfree_skb(skb);
2413
2414 return -ENOENT;
2415 }
2416 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2417 return 1;
2418 }
2419
2420 return 0;
2421}
2422
2423static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2424 __be32 default_vni, struct vxlan_rdst *rdst,
2425 bool did_rsc)
2426{
2427 struct dst_cache *dst_cache;
2428 struct ip_tunnel_info *info;
2429 struct vxlan_dev *vxlan = netdev_priv(dev);
2430 const struct iphdr *old_iph = ip_hdr(skb);
2431 union vxlan_addr *dst;
2432 union vxlan_addr remote_ip, local_ip;
2433 struct vxlan_metadata _md;
2434 struct vxlan_metadata *md = &_md;
2435 __be16 src_port = 0, dst_port;
2436 struct dst_entry *ndst = NULL;
2437 __be32 vni, label;
2438 __u8 tos, ttl;
2439 int ifindex;
2440 int err;
2441 u32 flags = vxlan->cfg.flags;
2442 bool udp_sum = false;
2443 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2444
2445 info = skb_tunnel_info(skb);
2446
2447 if (rdst) {
2448 dst = &rdst->remote_ip;
2449 if (vxlan_addr_any(dst)) {
2450 if (did_rsc) {
2451 /* short-circuited back to local bridge */
2452 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2453 return;
2454 }
2455 goto drop;
2456 }
2457
2458 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2459 vni = (rdst->remote_vni) ? : default_vni;
2460 ifindex = rdst->remote_ifindex;
2461 local_ip = vxlan->cfg.saddr;
2462 dst_cache = &rdst->dst_cache;
2463 md->gbp = skb->mark;
2464 if (flags & VXLAN_F_TTL_INHERIT) {
2465 ttl = ip_tunnel_get_ttl(old_iph, skb);
2466 } else {
2467 ttl = vxlan->cfg.ttl;
2468 if (!ttl && vxlan_addr_multicast(dst))
2469 ttl = 1;
2470 }
2471
2472 tos = vxlan->cfg.tos;
2473 if (tos == 1)
2474 tos = ip_tunnel_get_dsfield(old_iph, skb);
2475
2476 if (dst->sa.sa_family == AF_INET)
2477 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2478 else
2479 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2480 label = vxlan->cfg.label;
2481 } else {
2482 if (!info) {
2483 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2484 dev->name);
2485 goto drop;
2486 }
2487 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2488 if (remote_ip.sa.sa_family == AF_INET) {
2489 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2490 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2491 } else {
2492 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2493 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2494 }
2495 dst = &remote_ip;
2496 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2497 vni = tunnel_id_to_key32(info->key.tun_id);
2498 ifindex = 0;
2499 dst_cache = &info->dst_cache;
2500 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2501 if (info->options_len < sizeof(*md))
2502 goto drop;
2503 md = ip_tunnel_info_opts(info);
2504 }
2505 ttl = info->key.ttl;
2506 tos = info->key.tos;
2507 label = info->key.label;
2508 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2509 }
2510 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2511 vxlan->cfg.port_max, true);
2512
2513 rcu_read_lock();
2514 if (dst->sa.sa_family == AF_INET) {
2515 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2516 struct rtable *rt;
2517 __be16 df = 0;
2518
2519 if (!ifindex)
2520 ifindex = sock4->sock->sk->sk_bound_dev_if;
2521
2522 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2523 dst->sin.sin_addr.s_addr,
2524 &local_ip.sin.sin_addr.s_addr,
2525 dst_port, src_port,
2526 dst_cache, info);
2527 if (IS_ERR(rt)) {
2528 err = PTR_ERR(rt);
2529 goto tx_error;
2530 }
2531
2532 if (!info) {
2533 /* Bypass encapsulation if the destination is local */
2534 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2535 dst_port, ifindex, vni,
2536 &rt->dst, rt->rt_flags);
2537 if (err)
2538 goto out_unlock;
2539
2540 if (vxlan->cfg.df == VXLAN_DF_SET) {
2541 df = htons(IP_DF);
2542 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2543 struct ethhdr *eth = eth_hdr(skb);
2544
2545 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2546 (ntohs(eth->h_proto) == ETH_P_IP &&
2547 old_iph->frag_off & htons(IP_DF)))
2548 df = htons(IP_DF);
2549 }
2550 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2551 df = htons(IP_DF);
2552 }
2553
2554 ndst = &rt->dst;
2555 skb_tunnel_check_pmtu(skb, ndst, vxlan_headroom(flags & VXLAN_F_GPE));
2556
2557 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2558 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2559 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2560 vni, md, flags, udp_sum);
2561 if (err < 0)
2562 goto tx_error;
2563
2564 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2565 dst->sin.sin_addr.s_addr, tos, ttl, df,
2566 src_port, dst_port, xnet, !udp_sum);
2567#if IS_ENABLED(CONFIG_IPV6)
2568 } else {
2569 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2570
2571 if (!ifindex)
2572 ifindex = sock6->sock->sk->sk_bound_dev_if;
2573
2574 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2575 label, &dst->sin6.sin6_addr,
2576 &local_ip.sin6.sin6_addr,
2577 dst_port, src_port,
2578 dst_cache, info);
2579 if (IS_ERR(ndst)) {
2580 err = PTR_ERR(ndst);
2581 ndst = NULL;
2582 goto tx_error;
2583 }
2584
2585 if (!info) {
2586 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2587
2588 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2589 dst_port, ifindex, vni,
2590 ndst, rt6i_flags);
2591 if (err)
2592 goto out_unlock;
2593 }
2594
2595 skb_tunnel_check_pmtu(skb, ndst, vxlan_headroom((flags & VXLAN_F_GPE) | VXLAN_F_IPV6));
2596
2597 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2598 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2599 skb_scrub_packet(skb, xnet);
2600 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2601 vni, md, flags, udp_sum);
2602 if (err < 0)
2603 goto tx_error;
2604
2605 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2606 &local_ip.sin6.sin6_addr,
2607 &dst->sin6.sin6_addr, tos, ttl,
2608 label, src_port, dst_port, !udp_sum);
2609#endif
2610 }
2611out_unlock:
2612 rcu_read_unlock();
2613 return;
2614
2615drop:
2616 dev->stats.tx_dropped++;
2617 dev_kfree_skb(skb);
2618 return;
2619
2620tx_error:
2621 rcu_read_unlock();
2622 if (err == -ELOOP)
2623 dev->stats.collisions++;
2624 else if (err == -ENETUNREACH)
2625 dev->stats.tx_carrier_errors++;
2626 dst_release(ndst);
2627 dev->stats.tx_errors++;
2628 kfree_skb(skb);
2629}
2630
2631/* Transmit local packets over Vxlan
2632 *
2633 * Outer IP header inherits ECN and DF from inner header.
2634 * Outer UDP destination is the VXLAN assigned port.
2635 * source port is based on hash of flow
2636 */
2637static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2638{
2639 struct vxlan_dev *vxlan = netdev_priv(dev);
2640 struct vxlan_rdst *rdst, *fdst = NULL;
2641 const struct ip_tunnel_info *info;
2642 bool did_rsc = false;
2643 struct vxlan_fdb *f;
2644 struct ethhdr *eth;
2645 __be32 vni = 0;
2646
2647 info = skb_tunnel_info(skb);
2648
2649 skb_reset_mac_header(skb);
2650
2651 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2652 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2653 info->mode & IP_TUNNEL_INFO_TX) {
2654 vni = tunnel_id_to_key32(info->key.tun_id);
2655 } else {
2656 if (info && info->mode & IP_TUNNEL_INFO_TX)
2657 vxlan_xmit_one(skb, dev, vni, NULL, false);
2658 else
2659 kfree_skb(skb);
2660 return NETDEV_TX_OK;
2661 }
2662 }
2663
2664 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2665 eth = eth_hdr(skb);
2666 if (ntohs(eth->h_proto) == ETH_P_ARP)
2667 return arp_reduce(dev, skb, vni);
2668#if IS_ENABLED(CONFIG_IPV6)
2669 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2670 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2671 sizeof(struct nd_msg)) &&
2672 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2673 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2674
2675 if (m->icmph.icmp6_code == 0 &&
2676 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2677 return neigh_reduce(dev, skb, vni);
2678 }
2679#endif
2680 }
2681
2682 eth = eth_hdr(skb);
2683 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2684 did_rsc = false;
2685
2686 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2687 (ntohs(eth->h_proto) == ETH_P_IP ||
2688 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2689 did_rsc = route_shortcircuit(dev, skb);
2690 if (did_rsc)
2691 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2692 }
2693
2694 if (f == NULL) {
2695 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2696 if (f == NULL) {
2697 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2698 !is_multicast_ether_addr(eth->h_dest))
2699 vxlan_fdb_miss(vxlan, eth->h_dest);
2700
2701 dev->stats.tx_dropped++;
2702 kfree_skb(skb);
2703 return NETDEV_TX_OK;
2704 }
2705 }
2706
2707 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2708 struct sk_buff *skb1;
2709
2710 if (!fdst) {
2711 fdst = rdst;
2712 continue;
2713 }
2714 skb1 = skb_clone(skb, GFP_ATOMIC);
2715 if (skb1)
2716 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2717 }
2718
2719 if (fdst)
2720 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2721 else
2722 kfree_skb(skb);
2723 return NETDEV_TX_OK;
2724}
2725
2726/* Walk the forwarding table and purge stale entries */
2727static void vxlan_cleanup(struct timer_list *t)
2728{
2729 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2730 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2731 unsigned int h;
2732
2733 if (!netif_running(vxlan->dev))
2734 return;
2735
2736 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2737 struct hlist_node *p, *n;
2738
2739 spin_lock(&vxlan->hash_lock[h]);
2740 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2741 struct vxlan_fdb *f
2742 = container_of(p, struct vxlan_fdb, hlist);
2743 unsigned long timeout;
2744
2745 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2746 continue;
2747
2748 if (f->flags & NTF_EXT_LEARNED)
2749 continue;
2750
2751 timeout = f->used + vxlan->cfg.age_interval * HZ;
2752 if (time_before_eq(timeout, jiffies)) {
2753 netdev_dbg(vxlan->dev,
2754 "garbage collect %pM\n",
2755 f->eth_addr);
2756 f->state = NUD_STALE;
2757 vxlan_fdb_destroy(vxlan, f, true, true);
2758 } else if (time_before(timeout, next_timer))
2759 next_timer = timeout;
2760 }
2761 spin_unlock(&vxlan->hash_lock[h]);
2762 }
2763
2764 mod_timer(&vxlan->age_timer, next_timer);
2765}
2766
2767static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2768{
2769 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2770
2771 spin_lock(&vn->sock_lock);
2772 hlist_del_init_rcu(&vxlan->hlist4.hlist);
2773#if IS_ENABLED(CONFIG_IPV6)
2774 hlist_del_init_rcu(&vxlan->hlist6.hlist);
2775#endif
2776 spin_unlock(&vn->sock_lock);
2777}
2778
2779static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2780 struct vxlan_dev_node *node)
2781{
2782 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2783 __be32 vni = vxlan->default_dst.remote_vni;
2784
2785 node->vxlan = vxlan;
2786 spin_lock(&vn->sock_lock);
2787 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2788 spin_unlock(&vn->sock_lock);
2789}
2790
2791/* Setup stats when device is created */
2792static int vxlan_init(struct net_device *dev)
2793{
2794 struct vxlan_dev *vxlan = netdev_priv(dev);
2795 int err;
2796
2797 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2798 if (!dev->tstats)
2799 return -ENOMEM;
2800
2801 err = gro_cells_init(&vxlan->gro_cells, dev);
2802 if (err) {
2803 free_percpu(dev->tstats);
2804 return err;
2805 }
2806
2807 return 0;
2808}
2809
2810static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2811{
2812 struct vxlan_fdb *f;
2813 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
2814
2815 spin_lock_bh(&vxlan->hash_lock[hash_index]);
2816 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2817 if (f)
2818 vxlan_fdb_destroy(vxlan, f, true, true);
2819 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
2820}
2821
2822static void vxlan_uninit(struct net_device *dev)
2823{
2824 struct vxlan_dev *vxlan = netdev_priv(dev);
2825
2826 gro_cells_destroy(&vxlan->gro_cells);
2827
2828 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2829
2830 free_percpu(dev->tstats);
2831}
2832
2833/* Start ageing timer and join group when device is brought up */
2834static int vxlan_open(struct net_device *dev)
2835{
2836 struct vxlan_dev *vxlan = netdev_priv(dev);
2837 int ret;
2838
2839 ret = vxlan_sock_add(vxlan);
2840 if (ret < 0)
2841 return ret;
2842
2843 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2844 ret = vxlan_igmp_join(vxlan);
2845 if (ret == -EADDRINUSE)
2846 ret = 0;
2847 if (ret) {
2848 vxlan_sock_release(vxlan);
2849 return ret;
2850 }
2851 }
2852
2853 if (vxlan->cfg.age_interval)
2854 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2855
2856 return ret;
2857}
2858
2859/* Purge the forwarding table */
2860static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2861{
2862 unsigned int h;
2863
2864 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2865 struct hlist_node *p, *n;
2866
2867 spin_lock_bh(&vxlan->hash_lock[h]);
2868 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2869 struct vxlan_fdb *f
2870 = container_of(p, struct vxlan_fdb, hlist);
2871 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2872 continue;
2873 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2874 if (is_zero_ether_addr(f->eth_addr) &&
2875 f->vni == vxlan->cfg.vni)
2876 continue;
2877 vxlan_fdb_destroy(vxlan, f, true, true);
2878 }
2879 spin_unlock_bh(&vxlan->hash_lock[h]);
2880 }
2881}
2882
2883/* Cleanup timer and forwarding table on shutdown */
2884static int vxlan_stop(struct net_device *dev)
2885{
2886 struct vxlan_dev *vxlan = netdev_priv(dev);
2887 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2888 int ret = 0;
2889
2890 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2891 !vxlan_group_used(vn, vxlan))
2892 ret = vxlan_igmp_leave(vxlan);
2893
2894 del_timer_sync(&vxlan->age_timer);
2895
2896 vxlan_flush(vxlan, false);
2897 vxlan_sock_release(vxlan);
2898
2899 return ret;
2900}
2901
2902/* Stub, nothing needs to be done. */
2903static void vxlan_set_multicast_list(struct net_device *dev)
2904{
2905}
2906
2907static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2908{
2909 struct vxlan_dev *vxlan = netdev_priv(dev);
2910 struct vxlan_rdst *dst = &vxlan->default_dst;
2911 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2912 dst->remote_ifindex);
2913
2914 /* This check is different than dev->max_mtu, because it looks at
2915 * the lowerdev->mtu, rather than the static dev->max_mtu
2916 */
2917 if (lowerdev) {
2918 int max_mtu = lowerdev->mtu - vxlan_headroom(vxlan->cfg.flags);
2919 if (new_mtu > max_mtu)
2920 return -EINVAL;
2921 }
2922
2923 dev->mtu = new_mtu;
2924 return 0;
2925}
2926
2927static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2928{
2929 struct vxlan_dev *vxlan = netdev_priv(dev);
2930 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2931 __be16 sport, dport;
2932
2933 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2934 vxlan->cfg.port_max, true);
2935 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2936
2937 if (ip_tunnel_info_af(info) == AF_INET) {
2938 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2939 struct rtable *rt;
2940
2941 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
2942 info->key.u.ipv4.dst,
2943 &info->key.u.ipv4.src, dport, sport,
2944 &info->dst_cache, info);
2945 if (IS_ERR(rt))
2946 return PTR_ERR(rt);
2947 ip_rt_put(rt);
2948 } else {
2949#if IS_ENABLED(CONFIG_IPV6)
2950 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2951 struct dst_entry *ndst;
2952
2953 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
2954 info->key.label, &info->key.u.ipv6.dst,
2955 &info->key.u.ipv6.src, dport, sport,
2956 &info->dst_cache, info);
2957 if (IS_ERR(ndst))
2958 return PTR_ERR(ndst);
2959 dst_release(ndst);
2960#else /* !CONFIG_IPV6 */
2961 return -EPFNOSUPPORT;
2962#endif
2963 }
2964 info->key.tp_src = sport;
2965 info->key.tp_dst = dport;
2966 return 0;
2967}
2968
2969static const struct net_device_ops vxlan_netdev_ether_ops = {
2970 .ndo_init = vxlan_init,
2971 .ndo_uninit = vxlan_uninit,
2972 .ndo_open = vxlan_open,
2973 .ndo_stop = vxlan_stop,
2974 .ndo_start_xmit = vxlan_xmit,
2975 .ndo_get_stats64 = ip_tunnel_get_stats64,
2976 .ndo_set_rx_mode = vxlan_set_multicast_list,
2977 .ndo_change_mtu = vxlan_change_mtu,
2978 .ndo_validate_addr = eth_validate_addr,
2979 .ndo_set_mac_address = eth_mac_addr,
2980 .ndo_fdb_add = vxlan_fdb_add,
2981 .ndo_fdb_del = vxlan_fdb_delete,
2982 .ndo_fdb_dump = vxlan_fdb_dump,
2983 .ndo_fdb_get = vxlan_fdb_get,
2984 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2985 .ndo_change_proto_down = dev_change_proto_down_generic,
2986};
2987
2988static const struct net_device_ops vxlan_netdev_raw_ops = {
2989 .ndo_init = vxlan_init,
2990 .ndo_uninit = vxlan_uninit,
2991 .ndo_open = vxlan_open,
2992 .ndo_stop = vxlan_stop,
2993 .ndo_start_xmit = vxlan_xmit,
2994 .ndo_get_stats64 = ip_tunnel_get_stats64,
2995 .ndo_change_mtu = vxlan_change_mtu,
2996 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2997};
2998
2999/* Info for udev, that this is a virtual tunnel endpoint */
3000static struct device_type vxlan_type = {
3001 .name = "vxlan",
3002};
3003
3004/* Calls the ndo_udp_tunnel_add of the caller in order to
3005 * supply the listening VXLAN udp ports. Callers are expected
3006 * to implement the ndo_udp_tunnel_add.
3007 */
3008static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3009{
3010 struct vxlan_sock *vs;
3011 struct net *net = dev_net(dev);
3012 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3013 unsigned int i;
3014
3015 spin_lock(&vn->sock_lock);
3016 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3017 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3018 unsigned short type;
3019
3020 if (vs->flags & VXLAN_F_GPE)
3021 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3022 else
3023 type = UDP_TUNNEL_TYPE_VXLAN;
3024
3025 if (push)
3026 udp_tunnel_push_rx_port(dev, vs->sock, type);
3027 else
3028 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3029 }
3030 }
3031 spin_unlock(&vn->sock_lock);
3032}
3033
3034/* Initialize the device structure. */
3035static void vxlan_setup(struct net_device *dev)
3036{
3037 struct vxlan_dev *vxlan = netdev_priv(dev);
3038 unsigned int h;
3039
3040 eth_hw_addr_random(dev);
3041 ether_setup(dev);
3042
3043 dev->needs_free_netdev = true;
3044 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3045
3046 dev->features |= NETIF_F_LLTX;
3047 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3048 dev->features |= NETIF_F_RXCSUM;
3049 dev->features |= NETIF_F_GSO_SOFTWARE;
3050
3051 dev->vlan_features = dev->features;
3052 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3053 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3054 netif_keep_dst(dev);
3055 dev->priv_flags |= IFF_NO_QUEUE;
3056
3057 /* MTU range: 68 - 65535 */
3058 dev->min_mtu = ETH_MIN_MTU;
3059 dev->max_mtu = ETH_MAX_MTU;
3060
3061 INIT_LIST_HEAD(&vxlan->next);
3062
3063 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3064
3065 vxlan->dev = dev;
3066
3067 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3068 spin_lock_init(&vxlan->hash_lock[h]);
3069 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3070 }
3071}
3072
3073static void vxlan_ether_setup(struct net_device *dev)
3074{
3075 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3076 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3077 dev->netdev_ops = &vxlan_netdev_ether_ops;
3078}
3079
3080static void vxlan_raw_setup(struct net_device *dev)
3081{
3082 dev->header_ops = NULL;
3083 dev->type = ARPHRD_NONE;
3084 dev->hard_header_len = 0;
3085 dev->addr_len = 0;
3086 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3087 dev->netdev_ops = &vxlan_netdev_raw_ops;
3088}
3089
3090static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3091 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3092 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
3093 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3094 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3095 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
3096 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3097 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3098 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3099 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3100 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3101 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3102 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3103 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3104 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3105 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3106 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3107 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3108 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3109 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3110 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3111 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3112 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3113 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3114 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3115 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3116 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3117 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3118 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3119 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3120};
3121
3122static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3123 struct netlink_ext_ack *extack)
3124{
3125 if (tb[IFLA_ADDRESS]) {
3126 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3127 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3128 "Provided link layer address is not Ethernet");
3129 return -EINVAL;
3130 }
3131
3132 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3134 "Provided Ethernet address is not unicast");
3135 return -EADDRNOTAVAIL;
3136 }
3137 }
3138
3139 if (tb[IFLA_MTU]) {
3140 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3141
3142 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3143 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3144 "MTU must be between 68 and 65535");
3145 return -EINVAL;
3146 }
3147 }
3148
3149 if (!data) {
3150 NL_SET_ERR_MSG(extack,
3151 "Required attributes not provided to perform the operation");
3152 return -EINVAL;
3153 }
3154
3155 if (data[IFLA_VXLAN_ID]) {
3156 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3157
3158 if (id >= VXLAN_N_VID) {
3159 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3160 "VXLAN ID must be lower than 16777216");
3161 return -ERANGE;
3162 }
3163 }
3164
3165 if (data[IFLA_VXLAN_PORT_RANGE]) {
3166 const struct ifla_vxlan_port_range *p
3167 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3168
3169 if (ntohs(p->high) < ntohs(p->low)) {
3170 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3171 "Invalid source port range");
3172 return -EINVAL;
3173 }
3174 }
3175
3176 if (data[IFLA_VXLAN_DF]) {
3177 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3178
3179 if (df < 0 || df > VXLAN_DF_MAX) {
3180 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3181 "Invalid DF attribute");
3182 return -EINVAL;
3183 }
3184 }
3185
3186 return 0;
3187}
3188
3189static void vxlan_get_drvinfo(struct net_device *netdev,
3190 struct ethtool_drvinfo *drvinfo)
3191{
3192 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3193 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3194}
3195
3196static const struct ethtool_ops vxlan_ethtool_ops = {
3197 .get_drvinfo = vxlan_get_drvinfo,
3198 .get_link = ethtool_op_get_link,
3199};
3200
3201static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3202 __be16 port, u32 flags, int ifindex)
3203{
3204 struct socket *sock;
3205 struct udp_port_cfg udp_conf;
3206 int err;
3207
3208 memset(&udp_conf, 0, sizeof(udp_conf));
3209
3210 if (ipv6) {
3211 udp_conf.family = AF_INET6;
3212 udp_conf.use_udp6_rx_checksums =
3213 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3214 udp_conf.ipv6_v6only = 1;
3215 } else {
3216 udp_conf.family = AF_INET;
3217 }
3218
3219 udp_conf.local_udp_port = port;
3220 udp_conf.bind_ifindex = ifindex;
3221
3222 /* Open UDP socket */
3223 err = udp_sock_create(net, &udp_conf, &sock);
3224 if (err < 0)
3225 return ERR_PTR(err);
3226
3227 return sock;
3228}
3229
3230/* Create new listen socket if needed */
3231static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3232 __be16 port, u32 flags,
3233 int ifindex)
3234{
3235 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3236 struct vxlan_sock *vs;
3237 struct socket *sock;
3238 unsigned int h;
3239 struct udp_tunnel_sock_cfg tunnel_cfg;
3240
3241 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3242 if (!vs)
3243 return ERR_PTR(-ENOMEM);
3244
3245 for (h = 0; h < VNI_HASH_SIZE; ++h)
3246 INIT_HLIST_HEAD(&vs->vni_list[h]);
3247
3248 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3249 if (IS_ERR(sock)) {
3250 kfree(vs);
3251 return ERR_CAST(sock);
3252 }
3253
3254 vs->sock = sock;
3255 refcount_set(&vs->refcnt, 1);
3256 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3257
3258 spin_lock(&vn->sock_lock);
3259 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3260 udp_tunnel_notify_add_rx_port(sock,
3261 (vs->flags & VXLAN_F_GPE) ?
3262 UDP_TUNNEL_TYPE_VXLAN_GPE :
3263 UDP_TUNNEL_TYPE_VXLAN);
3264 spin_unlock(&vn->sock_lock);
3265
3266 /* Mark socket as an encapsulation socket. */
3267 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3268 tunnel_cfg.sk_user_data = vs;
3269 tunnel_cfg.encap_type = 1;
3270 tunnel_cfg.encap_rcv = vxlan_rcv;
3271 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3272 tunnel_cfg.encap_destroy = NULL;
3273 tunnel_cfg.gro_receive = vxlan_gro_receive;
3274 tunnel_cfg.gro_complete = vxlan_gro_complete;
3275
3276 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3277
3278 return vs;
3279}
3280
3281static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3282{
3283 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3284 struct vxlan_sock *vs = NULL;
3285 struct vxlan_dev_node *node;
3286 int l3mdev_index = 0;
3287
3288 if (vxlan->cfg.remote_ifindex)
3289 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3290 vxlan->net, vxlan->cfg.remote_ifindex);
3291
3292 if (!vxlan->cfg.no_share) {
3293 spin_lock(&vn->sock_lock);
3294 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3295 vxlan->cfg.dst_port, vxlan->cfg.flags,
3296 l3mdev_index);
3297 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3298 spin_unlock(&vn->sock_lock);
3299 return -EBUSY;
3300 }
3301 spin_unlock(&vn->sock_lock);
3302 }
3303 if (!vs)
3304 vs = vxlan_socket_create(vxlan->net, ipv6,
3305 vxlan->cfg.dst_port, vxlan->cfg.flags,
3306 l3mdev_index);
3307 if (IS_ERR(vs))
3308 return PTR_ERR(vs);
3309#if IS_ENABLED(CONFIG_IPV6)
3310 if (ipv6) {
3311 rcu_assign_pointer(vxlan->vn6_sock, vs);
3312 node = &vxlan->hlist6;
3313 } else
3314#endif
3315 {
3316 rcu_assign_pointer(vxlan->vn4_sock, vs);
3317 node = &vxlan->hlist4;
3318 }
3319 vxlan_vs_add_dev(vs, vxlan, node);
3320 return 0;
3321}
3322
3323static int vxlan_sock_add(struct vxlan_dev *vxlan)
3324{
3325 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3326 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3327 bool ipv4 = !ipv6 || metadata;
3328 int ret = 0;
3329
3330 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3331#if IS_ENABLED(CONFIG_IPV6)
3332 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3333 if (ipv6) {
3334 ret = __vxlan_sock_add(vxlan, true);
3335 if (ret < 0 && ret != -EAFNOSUPPORT)
3336 ipv4 = false;
3337 }
3338#endif
3339 if (ipv4)
3340 ret = __vxlan_sock_add(vxlan, false);
3341 if (ret < 0)
3342 vxlan_sock_release(vxlan);
3343 return ret;
3344}
3345
3346static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3347 struct net_device **lower,
3348 struct vxlan_dev *old,
3349 struct netlink_ext_ack *extack)
3350{
3351 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3352 struct vxlan_dev *tmp;
3353 bool use_ipv6 = false;
3354
3355 if (conf->flags & VXLAN_F_GPE) {
3356 /* For now, allow GPE only together with
3357 * COLLECT_METADATA. This can be relaxed later; in such
3358 * case, the other side of the PtP link will have to be
3359 * provided.
3360 */
3361 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3362 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3363 NL_SET_ERR_MSG(extack,
3364 "VXLAN GPE does not support this combination of attributes");
3365 return -EINVAL;
3366 }
3367 }
3368
3369 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3370 /* Unless IPv6 is explicitly requested, assume IPv4 */
3371 conf->remote_ip.sa.sa_family = AF_INET;
3372 conf->saddr.sa.sa_family = AF_INET;
3373 } else if (!conf->remote_ip.sa.sa_family) {
3374 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3375 } else if (!conf->saddr.sa.sa_family) {
3376 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3377 }
3378
3379 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3380 NL_SET_ERR_MSG(extack,
3381 "Local and remote address must be from the same family");
3382 return -EINVAL;
3383 }
3384
3385 if (vxlan_addr_multicast(&conf->saddr)) {
3386 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3387 return -EINVAL;
3388 }
3389
3390 if (conf->saddr.sa.sa_family == AF_INET6) {
3391 if (!IS_ENABLED(CONFIG_IPV6)) {
3392 NL_SET_ERR_MSG(extack,
3393 "IPv6 support not enabled in the kernel");
3394 return -EPFNOSUPPORT;
3395 }
3396 use_ipv6 = true;
3397 conf->flags |= VXLAN_F_IPV6;
3398
3399 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3400 int local_type =
3401 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3402 int remote_type =
3403 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3404
3405 if (local_type & IPV6_ADDR_LINKLOCAL) {
3406 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3407 (remote_type != IPV6_ADDR_ANY)) {
3408 NL_SET_ERR_MSG(extack,
3409 "Invalid combination of local and remote address scopes");
3410 return -EINVAL;
3411 }
3412
3413 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3414 } else {
3415 if (remote_type ==
3416 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3417 NL_SET_ERR_MSG(extack,
3418 "Invalid combination of local and remote address scopes");
3419 return -EINVAL;
3420 }
3421
3422 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3423 }
3424 }
3425 }
3426
3427 if (conf->label && !use_ipv6) {
3428 NL_SET_ERR_MSG(extack,
3429 "Label attribute only applies to IPv6 VXLAN devices");
3430 return -EINVAL;
3431 }
3432
3433 if (conf->remote_ifindex) {
3434 struct net_device *lowerdev;
3435
3436 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3437 if (!lowerdev) {
3438 NL_SET_ERR_MSG(extack,
3439 "Invalid local interface, device not found");
3440 return -ENODEV;
3441 }
3442
3443#if IS_ENABLED(CONFIG_IPV6)
3444 if (use_ipv6) {
3445 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3446 if (idev && idev->cnf.disable_ipv6) {
3447 NL_SET_ERR_MSG(extack,
3448 "IPv6 support disabled by administrator");
3449 return -EPERM;
3450 }
3451 }
3452#endif
3453
3454 *lower = lowerdev;
3455 } else {
3456 if (vxlan_addr_multicast(&conf->remote_ip)) {
3457 NL_SET_ERR_MSG(extack,
3458 "Local interface required for multicast remote destination");
3459
3460 return -EINVAL;
3461 }
3462
3463#if IS_ENABLED(CONFIG_IPV6)
3464 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3465 NL_SET_ERR_MSG(extack,
3466 "Local interface required for link-local local/remote addresses");
3467 return -EINVAL;
3468 }
3469#endif
3470
3471 *lower = NULL;
3472 }
3473
3474 if (!conf->dst_port) {
3475 if (conf->flags & VXLAN_F_GPE)
3476 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3477 else
3478 conf->dst_port = htons(vxlan_port);
3479 }
3480
3481 if (!conf->age_interval)
3482 conf->age_interval = FDB_AGE_DEFAULT;
3483
3484 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3485 if (tmp == old)
3486 continue;
3487
3488 if (tmp->cfg.vni != conf->vni)
3489 continue;
3490 if (tmp->cfg.dst_port != conf->dst_port)
3491 continue;
3492 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3493 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3494 continue;
3495
3496 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3497 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3498 continue;
3499
3500 NL_SET_ERR_MSG(extack,
3501 "A VXLAN device with the specified VNI already exists");
3502 return -EEXIST;
3503 }
3504
3505 return 0;
3506}
3507
3508static void vxlan_config_apply(struct net_device *dev,
3509 struct vxlan_config *conf,
3510 struct net_device *lowerdev,
3511 struct net *src_net,
3512 bool changelink)
3513{
3514 struct vxlan_dev *vxlan = netdev_priv(dev);
3515 struct vxlan_rdst *dst = &vxlan->default_dst;
3516 unsigned short needed_headroom = ETH_HLEN;
3517 int max_mtu = ETH_MAX_MTU;
3518 u32 flags = conf->flags;
3519
3520 if (!changelink) {
3521 if (flags & VXLAN_F_GPE)
3522 vxlan_raw_setup(dev);
3523 else
3524 vxlan_ether_setup(dev);
3525
3526 if (conf->mtu)
3527 dev->mtu = conf->mtu;
3528
3529 vxlan->net = src_net;
3530 }
3531
3532 dst->remote_vni = conf->vni;
3533
3534 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3535
3536 if (lowerdev) {
3537 dst->remote_ifindex = conf->remote_ifindex;
3538
3539 dev->gso_max_size = lowerdev->gso_max_size;
3540 dev->gso_max_segs = lowerdev->gso_max_segs;
3541
3542 needed_headroom = lowerdev->hard_header_len;
3543 needed_headroom += lowerdev->needed_headroom;
3544
3545 dev->needed_tailroom = lowerdev->needed_tailroom;
3546
3547 max_mtu = lowerdev->mtu - vxlan_headroom(flags);
3548 if (max_mtu < ETH_MIN_MTU)
3549 max_mtu = ETH_MIN_MTU;
3550
3551 if (!changelink && !conf->mtu)
3552 dev->mtu = max_mtu;
3553 }
3554
3555 if (dev->mtu > max_mtu)
3556 dev->mtu = max_mtu;
3557
3558 if (flags & VXLAN_F_COLLECT_METADATA)
3559 flags |= VXLAN_F_IPV6;
3560 needed_headroom += vxlan_headroom(flags);
3561 dev->needed_headroom = needed_headroom;
3562
3563 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3564}
3565
3566static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3567 struct vxlan_config *conf, bool changelink,
3568 struct netlink_ext_ack *extack)
3569{
3570 struct vxlan_dev *vxlan = netdev_priv(dev);
3571 struct net_device *lowerdev;
3572 int ret;
3573
3574 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3575 if (ret)
3576 return ret;
3577
3578 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3579
3580 return 0;
3581}
3582
3583static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3584 struct vxlan_config *conf,
3585 struct netlink_ext_ack *extack)
3586{
3587 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3588 struct vxlan_dev *vxlan = netdev_priv(dev);
3589 struct net_device *remote_dev = NULL;
3590 struct vxlan_fdb *f = NULL;
3591 bool unregister = false;
3592 struct vxlan_rdst *dst;
3593 int err;
3594
3595 dst = &vxlan->default_dst;
3596 err = vxlan_dev_configure(net, dev, conf, false, extack);
3597 if (err)
3598 return err;
3599
3600 dev->ethtool_ops = &vxlan_ethtool_ops;
3601
3602 /* create an fdb entry for a valid default destination */
3603 if (!vxlan_addr_any(&dst->remote_ip)) {
3604 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3605 &dst->remote_ip,
3606 NUD_REACHABLE | NUD_PERMANENT,
3607 vxlan->cfg.dst_port,
3608 dst->remote_vni,
3609 dst->remote_vni,
3610 dst->remote_ifindex,
3611 NTF_SELF, &f);
3612 if (err)
3613 return err;
3614 }
3615
3616 err = register_netdevice(dev);
3617 if (err)
3618 goto errout;
3619 unregister = true;
3620
3621 if (dst->remote_ifindex) {
3622 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3623 if (!remote_dev) {
3624 err = -ENODEV;
3625 goto errout;
3626 }
3627
3628 err = netdev_upper_dev_link(remote_dev, dev, extack);
3629 if (err)
3630 goto errout;
3631 }
3632
3633 err = rtnl_configure_link(dev, NULL);
3634 if (err)
3635 goto unlink;
3636
3637 if (f) {
3638 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3639
3640 /* notify default fdb entry */
3641 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3642 RTM_NEWNEIGH, true, extack);
3643 if (err) {
3644 vxlan_fdb_destroy(vxlan, f, false, false);
3645 if (remote_dev)
3646 netdev_upper_dev_unlink(remote_dev, dev);
3647 goto unregister;
3648 }
3649 }
3650
3651 list_add(&vxlan->next, &vn->vxlan_list);
3652 if (remote_dev)
3653 dst->remote_dev = remote_dev;
3654 return 0;
3655unlink:
3656 if (remote_dev)
3657 netdev_upper_dev_unlink(remote_dev, dev);
3658errout:
3659 /* unregister_netdevice() destroys the default FDB entry with deletion
3660 * notification. But the addition notification was not sent yet, so
3661 * destroy the entry by hand here.
3662 */
3663 if (f)
3664 __vxlan_fdb_free(f);
3665unregister:
3666 if (unregister)
3667 unregister_netdevice(dev);
3668 return err;
3669}
3670
3671/* Set/clear flags based on attribute */
3672static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3673 int attrtype, unsigned long mask, bool changelink,
3674 bool changelink_supported,
3675 struct netlink_ext_ack *extack)
3676{
3677 unsigned long flags;
3678
3679 if (!tb[attrtype])
3680 return 0;
3681
3682 if (changelink && !changelink_supported) {
3683 vxlan_flag_attr_error(attrtype, extack);
3684 return -EOPNOTSUPP;
3685 }
3686
3687 if (vxlan_policy[attrtype].type == NLA_FLAG)
3688 flags = conf->flags | mask;
3689 else if (nla_get_u8(tb[attrtype]))
3690 flags = conf->flags | mask;
3691 else
3692 flags = conf->flags & ~mask;
3693
3694 conf->flags = flags;
3695
3696 return 0;
3697}
3698
3699static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3700 struct net_device *dev, struct vxlan_config *conf,
3701 bool changelink, struct netlink_ext_ack *extack)
3702{
3703 struct vxlan_dev *vxlan = netdev_priv(dev);
3704 int err = 0;
3705
3706 memset(conf, 0, sizeof(*conf));
3707
3708 /* if changelink operation, start with old existing cfg */
3709 if (changelink)
3710 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3711
3712 if (data[IFLA_VXLAN_ID]) {
3713 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3714
3715 if (changelink && (vni != conf->vni)) {
3716 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3717 return -EOPNOTSUPP;
3718 }
3719 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3720 }
3721
3722 if (data[IFLA_VXLAN_GROUP]) {
3723 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3724 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3725 return -EOPNOTSUPP;
3726 }
3727
3728 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3729 conf->remote_ip.sa.sa_family = AF_INET;
3730 } else if (data[IFLA_VXLAN_GROUP6]) {
3731 if (!IS_ENABLED(CONFIG_IPV6)) {
3732 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
3733 return -EPFNOSUPPORT;
3734 }
3735
3736 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
3737 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
3738 return -EOPNOTSUPP;
3739 }
3740
3741 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3742 conf->remote_ip.sa.sa_family = AF_INET6;
3743 }
3744
3745 if (data[IFLA_VXLAN_LOCAL]) {
3746 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
3747 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
3748 return -EOPNOTSUPP;
3749 }
3750
3751 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3752 conf->saddr.sa.sa_family = AF_INET;
3753 } else if (data[IFLA_VXLAN_LOCAL6]) {
3754 if (!IS_ENABLED(CONFIG_IPV6)) {
3755 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
3756 return -EPFNOSUPPORT;
3757 }
3758
3759 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
3760 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
3761 return -EOPNOTSUPP;
3762 }
3763
3764 /* TODO: respect scope id */
3765 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3766 conf->saddr.sa.sa_family = AF_INET6;
3767 }
3768
3769 if (data[IFLA_VXLAN_LINK])
3770 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3771
3772 if (data[IFLA_VXLAN_TOS])
3773 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
3774
3775 if (data[IFLA_VXLAN_TTL])
3776 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3777
3778 if (data[IFLA_VXLAN_TTL_INHERIT]) {
3779 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
3780 VXLAN_F_TTL_INHERIT, changelink, false,
3781 extack);
3782 if (err)
3783 return err;
3784
3785 }
3786
3787 if (data[IFLA_VXLAN_LABEL])
3788 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3789 IPV6_FLOWLABEL_MASK;
3790
3791 if (data[IFLA_VXLAN_LEARNING]) {
3792 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
3793 VXLAN_F_LEARN, changelink, true,
3794 extack);
3795 if (err)
3796 return err;
3797 } else if (!changelink) {
3798 /* default to learn on a new device */
3799 conf->flags |= VXLAN_F_LEARN;
3800 }
3801
3802 if (data[IFLA_VXLAN_AGEING])
3803 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3804
3805 if (data[IFLA_VXLAN_PROXY]) {
3806 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
3807 VXLAN_F_PROXY, changelink, false,
3808 extack);
3809 if (err)
3810 return err;
3811 }
3812
3813 if (data[IFLA_VXLAN_RSC]) {
3814 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
3815 VXLAN_F_RSC, changelink, false,
3816 extack);
3817 if (err)
3818 return err;
3819 }
3820
3821 if (data[IFLA_VXLAN_L2MISS]) {
3822 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
3823 VXLAN_F_L2MISS, changelink, false,
3824 extack);
3825 if (err)
3826 return err;
3827 }
3828
3829 if (data[IFLA_VXLAN_L3MISS]) {
3830 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
3831 VXLAN_F_L3MISS, changelink, false,
3832 extack);
3833 if (err)
3834 return err;
3835 }
3836
3837 if (data[IFLA_VXLAN_LIMIT]) {
3838 if (changelink) {
3839 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
3840 "Cannot change limit");
3841 return -EOPNOTSUPP;
3842 }
3843 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3844 }
3845
3846 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3847 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
3848 VXLAN_F_COLLECT_METADATA, changelink, false,
3849 extack);
3850 if (err)
3851 return err;
3852 }
3853
3854 if (data[IFLA_VXLAN_PORT_RANGE]) {
3855 if (!changelink) {
3856 const struct ifla_vxlan_port_range *p
3857 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3858 conf->port_min = ntohs(p->low);
3859 conf->port_max = ntohs(p->high);
3860 } else {
3861 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
3862 "Cannot change port range");
3863 return -EOPNOTSUPP;
3864 }
3865 }
3866
3867 if (data[IFLA_VXLAN_PORT]) {
3868 if (changelink) {
3869 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
3870 "Cannot change port");
3871 return -EOPNOTSUPP;
3872 }
3873 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3874 }
3875
3876 if (data[IFLA_VXLAN_UDP_CSUM]) {
3877 if (changelink) {
3878 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
3879 "Cannot change UDP_CSUM flag");
3880 return -EOPNOTSUPP;
3881 }
3882 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3883 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3884 }
3885
3886 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3887 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3888 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
3889 false, extack);
3890 if (err)
3891 return err;
3892 }
3893
3894 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3895 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3896 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
3897 false, extack);
3898 if (err)
3899 return err;
3900 }
3901
3902 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3903 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
3904 VXLAN_F_REMCSUM_TX, changelink, false,
3905 extack);
3906 if (err)
3907 return err;
3908 }
3909
3910 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3911 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
3912 VXLAN_F_REMCSUM_RX, changelink, false,
3913 extack);
3914 if (err)
3915 return err;
3916 }
3917
3918 if (data[IFLA_VXLAN_GBP]) {
3919 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
3920 VXLAN_F_GBP, changelink, false, extack);
3921 if (err)
3922 return err;
3923 }
3924
3925 if (data[IFLA_VXLAN_GPE]) {
3926 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
3927 VXLAN_F_GPE, changelink, false,
3928 extack);
3929 if (err)
3930 return err;
3931 }
3932
3933 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3934 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
3935 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
3936 false, extack);
3937 if (err)
3938 return err;
3939 }
3940
3941 if (tb[IFLA_MTU]) {
3942 if (changelink) {
3943 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3944 "Cannot change mtu");
3945 return -EOPNOTSUPP;
3946 }
3947 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3948 }
3949
3950 if (data[IFLA_VXLAN_DF])
3951 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
3952
3953 return 0;
3954}
3955
3956static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3957 struct nlattr *tb[], struct nlattr *data[],
3958 struct netlink_ext_ack *extack)
3959{
3960 struct vxlan_config conf;
3961 int err;
3962
3963 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
3964 if (err)
3965 return err;
3966
3967 return __vxlan_dev_create(src_net, dev, &conf, extack);
3968}
3969
3970static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3971 struct nlattr *data[],
3972 struct netlink_ext_ack *extack)
3973{
3974 struct vxlan_dev *vxlan = netdev_priv(dev);
3975 struct net_device *lowerdev;
3976 struct vxlan_config conf;
3977 struct vxlan_rdst *dst;
3978 int err;
3979
3980 dst = &vxlan->default_dst;
3981 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
3982 if (err)
3983 return err;
3984
3985 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
3986 vxlan, extack);
3987 if (err)
3988 return err;
3989
3990 if (dst->remote_dev == lowerdev)
3991 lowerdev = NULL;
3992
3993 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
3994 extack);
3995 if (err)
3996 return err;
3997
3998 /* handle default dst entry */
3999 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4000 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4001
4002 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4003 if (!vxlan_addr_any(&conf.remote_ip)) {
4004 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4005 &conf.remote_ip,
4006 NUD_REACHABLE | NUD_PERMANENT,
4007 NLM_F_APPEND | NLM_F_CREATE,
4008 vxlan->cfg.dst_port,
4009 conf.vni, conf.vni,
4010 conf.remote_ifindex,
4011 NTF_SELF, true, extack);
4012 if (err) {
4013 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4014 netdev_adjacent_change_abort(dst->remote_dev,
4015 lowerdev, dev);
4016 return err;
4017 }
4018 }
4019 if (!vxlan_addr_any(&dst->remote_ip))
4020 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4021 dst->remote_ip,
4022 vxlan->cfg.dst_port,
4023 dst->remote_vni,
4024 dst->remote_vni,
4025 dst->remote_ifindex,
4026 true);
4027 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4028 }
4029
4030 if (conf.age_interval != vxlan->cfg.age_interval)
4031 mod_timer(&vxlan->age_timer, jiffies);
4032
4033 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4034 if (lowerdev && lowerdev != dst->remote_dev) {
4035 dst->remote_dev = lowerdev;
4036 netdev_update_lockdep_key(lowerdev);
4037 }
4038 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4039 return 0;
4040}
4041
4042static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4043{
4044 struct vxlan_dev *vxlan = netdev_priv(dev);
4045
4046 vxlan_flush(vxlan, true);
4047
4048 list_del(&vxlan->next);
4049 unregister_netdevice_queue(dev, head);
4050 if (vxlan->default_dst.remote_dev)
4051 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4052}
4053
4054static size_t vxlan_get_size(const struct net_device *dev)
4055{
4056
4057 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4058 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4059 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4060 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4061 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4062 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4063 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4064 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4065 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4066 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4067 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4068 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4069 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4070 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4071 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4072 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4073 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4074 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4075 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4076 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4077 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4078 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4079 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4080 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4081 0;
4082}
4083
4084static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4085{
4086 const struct vxlan_dev *vxlan = netdev_priv(dev);
4087 const struct vxlan_rdst *dst = &vxlan->default_dst;
4088 struct ifla_vxlan_port_range ports = {
4089 .low = htons(vxlan->cfg.port_min),
4090 .high = htons(vxlan->cfg.port_max),
4091 };
4092
4093 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4094 goto nla_put_failure;
4095
4096 if (!vxlan_addr_any(&dst->remote_ip)) {
4097 if (dst->remote_ip.sa.sa_family == AF_INET) {
4098 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4099 dst->remote_ip.sin.sin_addr.s_addr))
4100 goto nla_put_failure;
4101#if IS_ENABLED(CONFIG_IPV6)
4102 } else {
4103 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4104 &dst->remote_ip.sin6.sin6_addr))
4105 goto nla_put_failure;
4106#endif
4107 }
4108 }
4109
4110 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4111 goto nla_put_failure;
4112
4113 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4114 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4115 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4116 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4117 goto nla_put_failure;
4118#if IS_ENABLED(CONFIG_IPV6)
4119 } else {
4120 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4121 &vxlan->cfg.saddr.sin6.sin6_addr))
4122 goto nla_put_failure;
4123#endif
4124 }
4125 }
4126
4127 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4128 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4129 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4130 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4131 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4132 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4133 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4134 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4135 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4136 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4137 nla_put_u8(skb, IFLA_VXLAN_RSC,
4138 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4139 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4140 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4141 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4142 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4143 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4144 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4145 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4146 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4147 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4148 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4149 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4150 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4151 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4152 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4153 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4154 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4155 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4156 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4157 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4158 goto nla_put_failure;
4159
4160 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4161 goto nla_put_failure;
4162
4163 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4164 nla_put_flag(skb, IFLA_VXLAN_GBP))
4165 goto nla_put_failure;
4166
4167 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4168 nla_put_flag(skb, IFLA_VXLAN_GPE))
4169 goto nla_put_failure;
4170
4171 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4172 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4173 goto nla_put_failure;
4174
4175 return 0;
4176
4177nla_put_failure:
4178 return -EMSGSIZE;
4179}
4180
4181static struct net *vxlan_get_link_net(const struct net_device *dev)
4182{
4183 struct vxlan_dev *vxlan = netdev_priv(dev);
4184
4185 return vxlan->net;
4186}
4187
4188static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4189 .kind = "vxlan",
4190 .maxtype = IFLA_VXLAN_MAX,
4191 .policy = vxlan_policy,
4192 .priv_size = sizeof(struct vxlan_dev),
4193 .setup = vxlan_setup,
4194 .validate = vxlan_validate,
4195 .newlink = vxlan_newlink,
4196 .changelink = vxlan_changelink,
4197 .dellink = vxlan_dellink,
4198 .get_size = vxlan_get_size,
4199 .fill_info = vxlan_fill_info,
4200 .get_link_net = vxlan_get_link_net,
4201};
4202
4203struct net_device *vxlan_dev_create(struct net *net, const char *name,
4204 u8 name_assign_type,
4205 struct vxlan_config *conf)
4206{
4207 struct nlattr *tb[IFLA_MAX + 1];
4208 struct net_device *dev;
4209 int err;
4210
4211 memset(&tb, 0, sizeof(tb));
4212
4213 dev = rtnl_create_link(net, name, name_assign_type,
4214 &vxlan_link_ops, tb, NULL);
4215 if (IS_ERR(dev))
4216 return dev;
4217
4218 err = __vxlan_dev_create(net, dev, conf, NULL);
4219 if (err < 0) {
4220 free_netdev(dev);
4221 return ERR_PTR(err);
4222 }
4223
4224 err = rtnl_configure_link(dev, NULL);
4225 if (err < 0) {
4226 LIST_HEAD(list_kill);
4227
4228 vxlan_dellink(dev, &list_kill);
4229 unregister_netdevice_many(&list_kill);
4230 return ERR_PTR(err);
4231 }
4232
4233 return dev;
4234}
4235EXPORT_SYMBOL_GPL(vxlan_dev_create);
4236
4237static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4238 struct net_device *dev)
4239{
4240 struct vxlan_dev *vxlan, *next;
4241 LIST_HEAD(list_kill);
4242
4243 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4244 struct vxlan_rdst *dst = &vxlan->default_dst;
4245
4246 /* In case we created vxlan device with carrier
4247 * and we loose the carrier due to module unload
4248 * we also need to remove vxlan device. In other
4249 * cases, it's not necessary and remote_ifindex
4250 * is 0 here, so no matches.
4251 */
4252 if (dst->remote_ifindex == dev->ifindex)
4253 vxlan_dellink(vxlan->dev, &list_kill);
4254 }
4255
4256 unregister_netdevice_many(&list_kill);
4257}
4258
4259static int vxlan_netdevice_event(struct notifier_block *unused,
4260 unsigned long event, void *ptr)
4261{
4262 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4263 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4264
4265 if (event == NETDEV_UNREGISTER) {
4266 vxlan_offload_rx_ports(dev, false);
4267 vxlan_handle_lowerdev_unregister(vn, dev);
4268 } else if (event == NETDEV_REGISTER) {
4269 vxlan_offload_rx_ports(dev, true);
4270 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4271 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4272 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4273 }
4274
4275 return NOTIFY_DONE;
4276}
4277
4278static struct notifier_block vxlan_notifier_block __read_mostly = {
4279 .notifier_call = vxlan_netdevice_event,
4280};
4281
4282static void
4283vxlan_fdb_offloaded_set(struct net_device *dev,
4284 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4285{
4286 struct vxlan_dev *vxlan = netdev_priv(dev);
4287 struct vxlan_rdst *rdst;
4288 struct vxlan_fdb *f;
4289 u32 hash_index;
4290
4291 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4292
4293 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4294
4295 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4296 if (!f)
4297 goto out;
4298
4299 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4300 fdb_info->remote_port,
4301 fdb_info->remote_vni,
4302 fdb_info->remote_ifindex);
4303 if (!rdst)
4304 goto out;
4305
4306 rdst->offloaded = fdb_info->offloaded;
4307
4308out:
4309 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4310}
4311
4312static int
4313vxlan_fdb_external_learn_add(struct net_device *dev,
4314 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4315{
4316 struct vxlan_dev *vxlan = netdev_priv(dev);
4317 struct netlink_ext_ack *extack;
4318 u32 hash_index;
4319 int err;
4320
4321 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4322 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4323
4324 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4325 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4326 NUD_REACHABLE,
4327 NLM_F_CREATE | NLM_F_REPLACE,
4328 fdb_info->remote_port,
4329 fdb_info->vni,
4330 fdb_info->remote_vni,
4331 fdb_info->remote_ifindex,
4332 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4333 false, extack);
4334 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4335
4336 return err;
4337}
4338
4339static int
4340vxlan_fdb_external_learn_del(struct net_device *dev,
4341 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4342{
4343 struct vxlan_dev *vxlan = netdev_priv(dev);
4344 struct vxlan_fdb *f;
4345 u32 hash_index;
4346 int err = 0;
4347
4348 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4349 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4350
4351 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4352 if (!f)
4353 err = -ENOENT;
4354 else if (f->flags & NTF_EXT_LEARNED)
4355 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4356 fdb_info->remote_ip,
4357 fdb_info->remote_port,
4358 fdb_info->vni,
4359 fdb_info->remote_vni,
4360 fdb_info->remote_ifindex,
4361 false);
4362
4363 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4364
4365 return err;
4366}
4367
4368static int vxlan_switchdev_event(struct notifier_block *unused,
4369 unsigned long event, void *ptr)
4370{
4371 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4372 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4373 int err = 0;
4374
4375 switch (event) {
4376 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4377 vxlan_fdb_offloaded_set(dev, ptr);
4378 break;
4379 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4380 fdb_info = ptr;
4381 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4382 if (err) {
4383 err = notifier_from_errno(err);
4384 break;
4385 }
4386 fdb_info->offloaded = true;
4387 vxlan_fdb_offloaded_set(dev, fdb_info);
4388 break;
4389 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4390 fdb_info = ptr;
4391 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4392 if (err) {
4393 err = notifier_from_errno(err);
4394 break;
4395 }
4396 fdb_info->offloaded = false;
4397 vxlan_fdb_offloaded_set(dev, fdb_info);
4398 break;
4399 }
4400
4401 return err;
4402}
4403
4404static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4405 .notifier_call = vxlan_switchdev_event,
4406};
4407
4408static __net_init int vxlan_init_net(struct net *net)
4409{
4410 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4411 unsigned int h;
4412
4413 INIT_LIST_HEAD(&vn->vxlan_list);
4414 spin_lock_init(&vn->sock_lock);
4415
4416 for (h = 0; h < PORT_HASH_SIZE; ++h)
4417 INIT_HLIST_HEAD(&vn->sock_list[h]);
4418
4419 return 0;
4420}
4421
4422static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4423{
4424 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4425 struct vxlan_dev *vxlan, *next;
4426 struct net_device *dev, *aux;
4427
4428 for_each_netdev_safe(net, dev, aux)
4429 if (dev->rtnl_link_ops == &vxlan_link_ops)
4430 unregister_netdevice_queue(dev, head);
4431
4432 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4433 /* If vxlan->dev is in the same netns, it has already been added
4434 * to the list by the previous loop.
4435 */
4436 if (!net_eq(dev_net(vxlan->dev), net))
4437 unregister_netdevice_queue(vxlan->dev, head);
4438 }
4439
4440}
4441
4442static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4443{
4444 struct net *net;
4445 LIST_HEAD(list);
4446 unsigned int h;
4447
4448 rtnl_lock();
4449 list_for_each_entry(net, net_list, exit_list)
4450 vxlan_destroy_tunnels(net, &list);
4451
4452 unregister_netdevice_many(&list);
4453 rtnl_unlock();
4454
4455 list_for_each_entry(net, net_list, exit_list) {
4456 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4457
4458 for (h = 0; h < PORT_HASH_SIZE; ++h)
4459 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4460 }
4461}
4462
4463static struct pernet_operations vxlan_net_ops = {
4464 .init = vxlan_init_net,
4465 .exit_batch = vxlan_exit_batch_net,
4466 .id = &vxlan_net_id,
4467 .size = sizeof(struct vxlan_net),
4468};
4469
4470static int __init vxlan_init_module(void)
4471{
4472 int rc;
4473
4474 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4475
4476 rc = register_pernet_subsys(&vxlan_net_ops);
4477 if (rc)
4478 goto out1;
4479
4480 rc = register_netdevice_notifier(&vxlan_notifier_block);
4481 if (rc)
4482 goto out2;
4483
4484 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4485 if (rc)
4486 goto out3;
4487
4488 rc = rtnl_link_register(&vxlan_link_ops);
4489 if (rc)
4490 goto out4;
4491
4492 return 0;
4493out4:
4494 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4495out3:
4496 unregister_netdevice_notifier(&vxlan_notifier_block);
4497out2:
4498 unregister_pernet_subsys(&vxlan_net_ops);
4499out1:
4500 return rc;
4501}
4502late_initcall(vxlan_init_module);
4503
4504static void __exit vxlan_cleanup_module(void)
4505{
4506 rtnl_link_unregister(&vxlan_link_ops);
4507 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4508 unregister_netdevice_notifier(&vxlan_notifier_block);
4509 unregister_pernet_subsys(&vxlan_net_ops);
4510 /* rcu_barrier() is called by netns */
4511}
4512module_exit(vxlan_cleanup_module);
4513
4514MODULE_LICENSE("GPL");
4515MODULE_VERSION(VXLAN_VERSION);
4516MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4517MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4518MODULE_ALIAS_RTNL_LINK("vxlan");