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