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