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