1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Vxlan vni filter for collect metadata mode
4 *
5 * Authors: Roopa Prabhu <roopa@nvidia.com>
6 *
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/etherdevice.h>
12 #include <linux/rhashtable.h>
13 #include <net/rtnetlink.h>
14 #include <net/net_namespace.h>
15 #include <net/sock.h>
16 #include <net/vxlan.h>
17
18 #include "vxlan_private.h"
19
vxlan_vni_cmp(struct rhashtable_compare_arg * arg,const void * ptr)20 static inline int vxlan_vni_cmp(struct rhashtable_compare_arg *arg,
21 const void *ptr)
22 {
23 const struct vxlan_vni_node *vnode = ptr;
24 __be32 vni = *(__be32 *)arg->key;
25
26 return vnode->vni != vni;
27 }
28
29 const struct rhashtable_params vxlan_vni_rht_params = {
30 .head_offset = offsetof(struct vxlan_vni_node, vnode),
31 .key_offset = offsetof(struct vxlan_vni_node, vni),
32 .key_len = sizeof(__be32),
33 .nelem_hint = 3,
34 .max_size = VXLAN_N_VID,
35 .obj_cmpfn = vxlan_vni_cmp,
36 .automatic_shrinking = true,
37 };
38
vxlan_vs_add_del_vninode(struct vxlan_dev * vxlan,struct vxlan_vni_node * v,bool del)39 static void vxlan_vs_add_del_vninode(struct vxlan_dev *vxlan,
40 struct vxlan_vni_node *v,
41 bool del)
42 {
43 struct vxlan_dev_node *node;
44 struct vxlan_sock *vs;
45
46 ASSERT_RTNL();
47
48 if (del) {
49 if (!hlist_unhashed(&v->hlist4.hlist))
50 hlist_del_init_rcu(&v->hlist4.hlist);
51 #if IS_ENABLED(CONFIG_IPV6)
52 if (!hlist_unhashed(&v->hlist6.hlist))
53 hlist_del_init_rcu(&v->hlist6.hlist);
54 #endif
55 return;
56 }
57
58 #if IS_ENABLED(CONFIG_IPV6)
59 vs = rtnl_dereference(vxlan->vn6_sock);
60 if (vs && v) {
61 node = &v->hlist6;
62 hlist_add_head_rcu(&node->hlist, vni_head(vs, v->vni));
63 }
64 #endif
65 vs = rtnl_dereference(vxlan->vn4_sock);
66 if (vs && v) {
67 node = &v->hlist4;
68 hlist_add_head_rcu(&node->hlist, vni_head(vs, v->vni));
69 }
70 }
71
vxlan_vs_add_vnigrp(struct vxlan_dev * vxlan,struct vxlan_sock * vs,bool ipv6)72 void vxlan_vs_add_vnigrp(struct vxlan_dev *vxlan,
73 struct vxlan_sock *vs,
74 bool ipv6)
75 {
76 struct vxlan_vni_group *vg = rtnl_dereference(vxlan->vnigrp);
77 struct vxlan_vni_node *v, *tmp;
78 struct vxlan_dev_node *node;
79
80 ASSERT_RTNL();
81
82 if (!vg)
83 return;
84
85 list_for_each_entry_safe(v, tmp, &vg->vni_list, vlist) {
86 #if IS_ENABLED(CONFIG_IPV6)
87 if (ipv6)
88 node = &v->hlist6;
89 else
90 #endif
91 node = &v->hlist4;
92 node->vxlan = vxlan;
93 hlist_add_head_rcu(&node->hlist, vni_head(vs, v->vni));
94 }
95 }
96
vxlan_vs_del_vnigrp(struct vxlan_dev * vxlan)97 void vxlan_vs_del_vnigrp(struct vxlan_dev *vxlan)
98 {
99 struct vxlan_vni_group *vg = rtnl_dereference(vxlan->vnigrp);
100 struct vxlan_vni_node *v, *tmp;
101
102 ASSERT_RTNL();
103
104 if (!vg)
105 return;
106
107 list_for_each_entry_safe(v, tmp, &vg->vni_list, vlist) {
108 hlist_del_init_rcu(&v->hlist4.hlist);
109 #if IS_ENABLED(CONFIG_IPV6)
110 hlist_del_init_rcu(&v->hlist6.hlist);
111 #endif
112 }
113 }
114
vxlan_vnifilter_stats_get(const struct vxlan_vni_node * vninode,struct vxlan_vni_stats * dest)115 static void vxlan_vnifilter_stats_get(const struct vxlan_vni_node *vninode,
116 struct vxlan_vni_stats *dest)
117 {
118 int i;
119
120 memset(dest, 0, sizeof(*dest));
121 for_each_possible_cpu(i) {
122 struct vxlan_vni_stats_pcpu *pstats;
123 struct vxlan_vni_stats temp;
124 unsigned int start;
125
126 pstats = per_cpu_ptr(vninode->stats, i);
127 do {
128 start = u64_stats_fetch_begin(&pstats->syncp);
129 u64_stats_copy(&temp, &pstats->stats, sizeof(temp));
130 } while (u64_stats_fetch_retry(&pstats->syncp, start));
131
132 dest->rx_packets += temp.rx_packets;
133 dest->rx_bytes += temp.rx_bytes;
134 dest->rx_drops += temp.rx_drops;
135 dest->rx_errors += temp.rx_errors;
136 dest->tx_packets += temp.tx_packets;
137 dest->tx_bytes += temp.tx_bytes;
138 dest->tx_drops += temp.tx_drops;
139 dest->tx_errors += temp.tx_errors;
140 }
141 }
142
vxlan_vnifilter_stats_add(struct vxlan_vni_node * vninode,int type,unsigned int len)143 static void vxlan_vnifilter_stats_add(struct vxlan_vni_node *vninode,
144 int type, unsigned int len)
145 {
146 struct vxlan_vni_stats_pcpu *pstats = this_cpu_ptr(vninode->stats);
147
148 u64_stats_update_begin(&pstats->syncp);
149 switch (type) {
150 case VXLAN_VNI_STATS_RX:
151 pstats->stats.rx_bytes += len;
152 pstats->stats.rx_packets++;
153 break;
154 case VXLAN_VNI_STATS_RX_DROPS:
155 pstats->stats.rx_drops++;
156 break;
157 case VXLAN_VNI_STATS_RX_ERRORS:
158 pstats->stats.rx_errors++;
159 break;
160 case VXLAN_VNI_STATS_TX:
161 pstats->stats.tx_bytes += len;
162 pstats->stats.tx_packets++;
163 break;
164 case VXLAN_VNI_STATS_TX_DROPS:
165 pstats->stats.tx_drops++;
166 break;
167 case VXLAN_VNI_STATS_TX_ERRORS:
168 pstats->stats.tx_errors++;
169 break;
170 }
171 u64_stats_update_end(&pstats->syncp);
172 }
173
vxlan_vnifilter_count(struct vxlan_dev * vxlan,__be32 vni,struct vxlan_vni_node * vninode,int type,unsigned int len)174 void vxlan_vnifilter_count(struct vxlan_dev *vxlan, __be32 vni,
175 struct vxlan_vni_node *vninode,
176 int type, unsigned int len)
177 {
178 struct vxlan_vni_node *vnode;
179
180 if (!(vxlan->cfg.flags & VXLAN_F_VNIFILTER))
181 return;
182
183 if (vninode) {
184 vnode = vninode;
185 } else {
186 vnode = vxlan_vnifilter_lookup(vxlan, vni);
187 if (!vnode)
188 return;
189 }
190
191 vxlan_vnifilter_stats_add(vnode, type, len);
192 }
193
vnirange(struct vxlan_vni_node * vbegin,struct vxlan_vni_node * vend)194 static u32 vnirange(struct vxlan_vni_node *vbegin,
195 struct vxlan_vni_node *vend)
196 {
197 return (be32_to_cpu(vend->vni) - be32_to_cpu(vbegin->vni));
198 }
199
vxlan_vnifilter_entry_nlmsg_size(void)200 static size_t vxlan_vnifilter_entry_nlmsg_size(void)
201 {
202 return NLMSG_ALIGN(sizeof(struct tunnel_msg))
203 + nla_total_size(0) /* VXLAN_VNIFILTER_ENTRY */
204 + nla_total_size(sizeof(u32)) /* VXLAN_VNIFILTER_ENTRY_START */
205 + nla_total_size(sizeof(u32)) /* VXLAN_VNIFILTER_ENTRY_END */
206 + nla_total_size(sizeof(struct in6_addr));/* VXLAN_VNIFILTER_ENTRY_GROUP{6} */
207 }
208
__vnifilter_entry_fill_stats(struct sk_buff * skb,const struct vxlan_vni_node * vbegin)209 static int __vnifilter_entry_fill_stats(struct sk_buff *skb,
210 const struct vxlan_vni_node *vbegin)
211 {
212 struct vxlan_vni_stats vstats;
213 struct nlattr *vstats_attr;
214
215 vstats_attr = nla_nest_start(skb, VXLAN_VNIFILTER_ENTRY_STATS);
216 if (!vstats_attr)
217 goto out_stats_err;
218
219 vxlan_vnifilter_stats_get(vbegin, &vstats);
220 if (nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_RX_BYTES,
221 vstats.rx_bytes, VNIFILTER_ENTRY_STATS_PAD) ||
222 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_RX_PKTS,
223 vstats.rx_packets, VNIFILTER_ENTRY_STATS_PAD) ||
224 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_RX_DROPS,
225 vstats.rx_drops, VNIFILTER_ENTRY_STATS_PAD) ||
226 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_RX_ERRORS,
227 vstats.rx_errors, VNIFILTER_ENTRY_STATS_PAD) ||
228 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_TX_BYTES,
229 vstats.tx_bytes, VNIFILTER_ENTRY_STATS_PAD) ||
230 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_TX_PKTS,
231 vstats.tx_packets, VNIFILTER_ENTRY_STATS_PAD) ||
232 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_TX_DROPS,
233 vstats.tx_drops, VNIFILTER_ENTRY_STATS_PAD) ||
234 nla_put_u64_64bit(skb, VNIFILTER_ENTRY_STATS_TX_ERRORS,
235 vstats.tx_errors, VNIFILTER_ENTRY_STATS_PAD))
236 goto out_stats_err;
237
238 nla_nest_end(skb, vstats_attr);
239
240 return 0;
241
242 out_stats_err:
243 nla_nest_cancel(skb, vstats_attr);
244 return -EMSGSIZE;
245 }
246
vxlan_fill_vni_filter_entry(struct sk_buff * skb,struct vxlan_vni_node * vbegin,struct vxlan_vni_node * vend,bool fill_stats)247 static bool vxlan_fill_vni_filter_entry(struct sk_buff *skb,
248 struct vxlan_vni_node *vbegin,
249 struct vxlan_vni_node *vend,
250 bool fill_stats)
251 {
252 struct nlattr *ventry;
253 u32 vs = be32_to_cpu(vbegin->vni);
254 u32 ve = 0;
255
256 if (vbegin != vend)
257 ve = be32_to_cpu(vend->vni);
258
259 ventry = nla_nest_start(skb, VXLAN_VNIFILTER_ENTRY);
260 if (!ventry)
261 return false;
262
263 if (nla_put_u32(skb, VXLAN_VNIFILTER_ENTRY_START, vs))
264 goto out_err;
265
266 if (ve && nla_put_u32(skb, VXLAN_VNIFILTER_ENTRY_END, ve))
267 goto out_err;
268
269 if (!vxlan_addr_any(&vbegin->remote_ip)) {
270 if (vbegin->remote_ip.sa.sa_family == AF_INET) {
271 if (nla_put_in_addr(skb, VXLAN_VNIFILTER_ENTRY_GROUP,
272 vbegin->remote_ip.sin.sin_addr.s_addr))
273 goto out_err;
274 #if IS_ENABLED(CONFIG_IPV6)
275 } else {
276 if (nla_put_in6_addr(skb, VXLAN_VNIFILTER_ENTRY_GROUP6,
277 &vbegin->remote_ip.sin6.sin6_addr))
278 goto out_err;
279 #endif
280 }
281 }
282
283 if (fill_stats && __vnifilter_entry_fill_stats(skb, vbegin))
284 goto out_err;
285
286 nla_nest_end(skb, ventry);
287
288 return true;
289
290 out_err:
291 nla_nest_cancel(skb, ventry);
292
293 return false;
294 }
295
vxlan_vnifilter_notify(const struct vxlan_dev * vxlan,struct vxlan_vni_node * vninode,int cmd)296 static void vxlan_vnifilter_notify(const struct vxlan_dev *vxlan,
297 struct vxlan_vni_node *vninode, int cmd)
298 {
299 struct tunnel_msg *tmsg;
300 struct sk_buff *skb;
301 struct nlmsghdr *nlh;
302 struct net *net = dev_net(vxlan->dev);
303 int err = -ENOBUFS;
304
305 skb = nlmsg_new(vxlan_vnifilter_entry_nlmsg_size(), GFP_KERNEL);
306 if (!skb)
307 goto out_err;
308
309 err = -EMSGSIZE;
310 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*tmsg), 0);
311 if (!nlh)
312 goto out_err;
313 tmsg = nlmsg_data(nlh);
314 memset(tmsg, 0, sizeof(*tmsg));
315 tmsg->family = AF_BRIDGE;
316 tmsg->ifindex = vxlan->dev->ifindex;
317
318 if (!vxlan_fill_vni_filter_entry(skb, vninode, vninode, false))
319 goto out_err;
320
321 nlmsg_end(skb, nlh);
322 rtnl_notify(skb, net, 0, RTNLGRP_TUNNEL, NULL, GFP_KERNEL);
323
324 return;
325
326 out_err:
327 rtnl_set_sk_err(net, RTNLGRP_TUNNEL, err);
328
329 kfree_skb(skb);
330 }
331
vxlan_vnifilter_dump_dev(const struct net_device * dev,struct sk_buff * skb,struct netlink_callback * cb)332 static int vxlan_vnifilter_dump_dev(const struct net_device *dev,
333 struct sk_buff *skb,
334 struct netlink_callback *cb)
335 {
336 struct vxlan_vni_node *tmp, *v, *vbegin = NULL, *vend = NULL;
337 struct vxlan_dev *vxlan = netdev_priv(dev);
338 struct tunnel_msg *new_tmsg, *tmsg;
339 int idx = 0, s_idx = cb->args[1];
340 struct vxlan_vni_group *vg;
341 struct nlmsghdr *nlh;
342 bool dump_stats;
343 int err = 0;
344
345 if (!(vxlan->cfg.flags & VXLAN_F_VNIFILTER))
346 return -EINVAL;
347
348 /* RCU needed because of the vni locking rules (rcu || rtnl) */
349 vg = rcu_dereference(vxlan->vnigrp);
350 if (!vg || !vg->num_vnis)
351 return 0;
352
353 tmsg = nlmsg_data(cb->nlh);
354 dump_stats = !!(tmsg->flags & TUNNEL_MSG_FLAG_STATS);
355
356 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
357 RTM_NEWTUNNEL, sizeof(*new_tmsg), NLM_F_MULTI);
358 if (!nlh)
359 return -EMSGSIZE;
360 new_tmsg = nlmsg_data(nlh);
361 memset(new_tmsg, 0, sizeof(*new_tmsg));
362 new_tmsg->family = PF_BRIDGE;
363 new_tmsg->ifindex = dev->ifindex;
364
365 list_for_each_entry_safe(v, tmp, &vg->vni_list, vlist) {
366 if (idx < s_idx) {
367 idx++;
368 continue;
369 }
370 if (!vbegin) {
371 vbegin = v;
372 vend = v;
373 continue;
374 }
375 if (!dump_stats && vnirange(vend, v) == 1 &&
376 vxlan_addr_equal(&v->remote_ip, &vend->remote_ip)) {
377 goto update_end;
378 } else {
379 if (!vxlan_fill_vni_filter_entry(skb, vbegin, vend,
380 dump_stats)) {
381 err = -EMSGSIZE;
382 break;
383 }
384 idx += vnirange(vbegin, vend) + 1;
385 vbegin = v;
386 }
387 update_end:
388 vend = v;
389 }
390
391 if (!err && vbegin) {
392 if (!vxlan_fill_vni_filter_entry(skb, vbegin, vend, dump_stats))
393 err = -EMSGSIZE;
394 }
395
396 cb->args[1] = err ? idx : 0;
397
398 nlmsg_end(skb, nlh);
399
400 return err;
401 }
402
vxlan_vnifilter_dump(struct sk_buff * skb,struct netlink_callback * cb)403 static int vxlan_vnifilter_dump(struct sk_buff *skb, struct netlink_callback *cb)
404 {
405 int idx = 0, err = 0, s_idx = cb->args[0];
406 struct net *net = sock_net(skb->sk);
407 struct tunnel_msg *tmsg;
408 struct net_device *dev;
409
410 tmsg = nlmsg_payload(cb->nlh, sizeof(*tmsg));
411 if (!tmsg) {
412 NL_SET_ERR_MSG(cb->extack, "Invalid msg length");
413 return -EINVAL;
414 }
415
416 if (tmsg->flags & ~TUNNEL_MSG_VALID_USER_FLAGS) {
417 NL_SET_ERR_MSG(cb->extack, "Invalid tunnelmsg flags in ancillary header");
418 return -EINVAL;
419 }
420
421 rcu_read_lock();
422 if (tmsg->ifindex) {
423 dev = dev_get_by_index_rcu(net, tmsg->ifindex);
424 if (!dev) {
425 err = -ENODEV;
426 goto out_err;
427 }
428 if (!netif_is_vxlan(dev)) {
429 NL_SET_ERR_MSG(cb->extack,
430 "The device is not a vxlan device");
431 err = -EINVAL;
432 goto out_err;
433 }
434 err = vxlan_vnifilter_dump_dev(dev, skb, cb);
435 /* if the dump completed without an error we return 0 here */
436 if (err != -EMSGSIZE)
437 goto out_err;
438 } else {
439 for_each_netdev_rcu(net, dev) {
440 if (!netif_is_vxlan(dev))
441 continue;
442 if (idx < s_idx)
443 goto skip;
444 err = vxlan_vnifilter_dump_dev(dev, skb, cb);
445 if (err == -EMSGSIZE)
446 break;
447 skip:
448 idx++;
449 }
450 }
451 cb->args[0] = idx;
452 rcu_read_unlock();
453
454 return skb->len;
455
456 out_err:
457 rcu_read_unlock();
458
459 return err;
460 }
461
462 static const struct nla_policy vni_filter_entry_policy[VXLAN_VNIFILTER_ENTRY_MAX + 1] = {
463 [VXLAN_VNIFILTER_ENTRY_START] = { .type = NLA_U32 },
464 [VXLAN_VNIFILTER_ENTRY_END] = { .type = NLA_U32 },
465 [VXLAN_VNIFILTER_ENTRY_GROUP] = NLA_POLICY_EXACT_LEN(sizeof_field(struct iphdr, daddr)),
466 [VXLAN_VNIFILTER_ENTRY_GROUP6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
467 };
468
469 static const struct nla_policy vni_filter_policy[VXLAN_VNIFILTER_MAX + 1] = {
470 [VXLAN_VNIFILTER_ENTRY] = { .type = NLA_NESTED },
471 };
472
vxlan_update_default_fdb_entry(struct vxlan_dev * vxlan,__be32 vni,union vxlan_addr * old_remote_ip,union vxlan_addr * remote_ip,struct netlink_ext_ack * extack)473 static int vxlan_update_default_fdb_entry(struct vxlan_dev *vxlan, __be32 vni,
474 union vxlan_addr *old_remote_ip,
475 union vxlan_addr *remote_ip,
476 struct netlink_ext_ack *extack)
477 {
478 struct vxlan_rdst *dst = &vxlan->default_dst;
479 int err = 0;
480
481 spin_lock_bh(&vxlan->hash_lock);
482 if (remote_ip && !vxlan_addr_any(remote_ip)) {
483 err = vxlan_fdb_update(vxlan, all_zeros_mac,
484 remote_ip,
485 NUD_REACHABLE | NUD_PERMANENT,
486 NLM_F_APPEND | NLM_F_CREATE,
487 vxlan->cfg.dst_port,
488 vni,
489 vni,
490 dst->remote_ifindex,
491 NTF_SELF, 0, true, extack);
492 if (err) {
493 spin_unlock_bh(&vxlan->hash_lock);
494 return err;
495 }
496 }
497
498 if (old_remote_ip && !vxlan_addr_any(old_remote_ip)) {
499 __vxlan_fdb_delete(vxlan, all_zeros_mac,
500 *old_remote_ip,
501 vxlan->cfg.dst_port,
502 vni, vni,
503 dst->remote_ifindex,
504 true);
505 }
506 spin_unlock_bh(&vxlan->hash_lock);
507
508 return err;
509 }
510
vxlan_vni_update_group(struct vxlan_dev * vxlan,struct vxlan_vni_node * vninode,union vxlan_addr * group,bool create,bool * changed,struct netlink_ext_ack * extack)511 static int vxlan_vni_update_group(struct vxlan_dev *vxlan,
512 struct vxlan_vni_node *vninode,
513 union vxlan_addr *group,
514 bool create, bool *changed,
515 struct netlink_ext_ack *extack)
516 {
517 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
518 struct vxlan_rdst *dst = &vxlan->default_dst;
519 union vxlan_addr *newrip = NULL, *oldrip = NULL;
520 union vxlan_addr old_remote_ip;
521 int ret = 0;
522
523 memcpy(&old_remote_ip, &vninode->remote_ip, sizeof(old_remote_ip));
524
525 /* if per vni remote ip is not present use vxlan dev
526 * default dst remote ip for fdb entry
527 */
528 if (group && !vxlan_addr_any(group)) {
529 newrip = group;
530 } else {
531 if (!vxlan_addr_any(&dst->remote_ip))
532 newrip = &dst->remote_ip;
533 }
534
535 /* if old rip exists, and no newrip,
536 * explicitly delete old rip
537 */
538 if (!newrip && !vxlan_addr_any(&old_remote_ip))
539 oldrip = &old_remote_ip;
540
541 if (!newrip && !oldrip)
542 return 0;
543
544 if (!create && oldrip && newrip && vxlan_addr_equal(oldrip, newrip))
545 return 0;
546
547 ret = vxlan_update_default_fdb_entry(vxlan, vninode->vni,
548 oldrip, newrip,
549 extack);
550 if (ret)
551 goto out;
552
553 if (group)
554 memcpy(&vninode->remote_ip, group, sizeof(vninode->remote_ip));
555
556 if (vxlan->dev->flags & IFF_UP) {
557 if (vxlan_addr_multicast(&old_remote_ip) &&
558 !vxlan_group_used(vn, vxlan, vninode->vni,
559 &old_remote_ip,
560 vxlan->default_dst.remote_ifindex)) {
561 ret = vxlan_igmp_leave(vxlan, &old_remote_ip,
562 0);
563 if (ret)
564 goto out;
565 }
566
567 if (vxlan_addr_multicast(&vninode->remote_ip)) {
568 ret = vxlan_igmp_join(vxlan, &vninode->remote_ip, 0);
569 if (ret == -EADDRINUSE)
570 ret = 0;
571 if (ret)
572 goto out;
573 }
574 }
575
576 *changed = true;
577
578 return 0;
579 out:
580 return ret;
581 }
582
vxlan_vnilist_update_group(struct vxlan_dev * vxlan,union vxlan_addr * old_remote_ip,union vxlan_addr * new_remote_ip,struct netlink_ext_ack * extack)583 int vxlan_vnilist_update_group(struct vxlan_dev *vxlan,
584 union vxlan_addr *old_remote_ip,
585 union vxlan_addr *new_remote_ip,
586 struct netlink_ext_ack *extack)
587 {
588 struct list_head *headp, *hpos;
589 struct vxlan_vni_group *vg;
590 struct vxlan_vni_node *vent;
591 int ret;
592
593 vg = rtnl_dereference(vxlan->vnigrp);
594
595 headp = &vg->vni_list;
596 list_for_each_prev(hpos, headp) {
597 vent = list_entry(hpos, struct vxlan_vni_node, vlist);
598 if (vxlan_addr_any(&vent->remote_ip)) {
599 ret = vxlan_update_default_fdb_entry(vxlan, vent->vni,
600 old_remote_ip,
601 new_remote_ip,
602 extack);
603 if (ret)
604 return ret;
605 }
606 }
607
608 return 0;
609 }
610
vxlan_vni_delete_group(struct vxlan_dev * vxlan,struct vxlan_vni_node * vninode)611 static void vxlan_vni_delete_group(struct vxlan_dev *vxlan,
612 struct vxlan_vni_node *vninode)
613 {
614 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
615 struct vxlan_rdst *dst = &vxlan->default_dst;
616
617 /* if per vni remote_ip not present, delete the
618 * default dst remote_ip previously added for this vni
619 */
620 if (!vxlan_addr_any(&vninode->remote_ip) ||
621 !vxlan_addr_any(&dst->remote_ip)) {
622 spin_lock_bh(&vxlan->hash_lock);
623 __vxlan_fdb_delete(vxlan, all_zeros_mac,
624 (vxlan_addr_any(&vninode->remote_ip) ?
625 dst->remote_ip : vninode->remote_ip),
626 vxlan->cfg.dst_port,
627 vninode->vni, vninode->vni,
628 dst->remote_ifindex,
629 true);
630 spin_unlock_bh(&vxlan->hash_lock);
631 }
632
633 if (vxlan->dev->flags & IFF_UP) {
634 if (vxlan_addr_multicast(&vninode->remote_ip) &&
635 !vxlan_group_used(vn, vxlan, vninode->vni,
636 &vninode->remote_ip,
637 dst->remote_ifindex)) {
638 vxlan_igmp_leave(vxlan, &vninode->remote_ip, 0);
639 }
640 }
641 }
642
vxlan_vni_update(struct vxlan_dev * vxlan,struct vxlan_vni_group * vg,__be32 vni,union vxlan_addr * group,bool * changed,struct netlink_ext_ack * extack)643 static int vxlan_vni_update(struct vxlan_dev *vxlan,
644 struct vxlan_vni_group *vg,
645 __be32 vni, union vxlan_addr *group,
646 bool *changed,
647 struct netlink_ext_ack *extack)
648 {
649 struct vxlan_vni_node *vninode;
650 int ret;
651
652 vninode = rhashtable_lookup_fast(&vg->vni_hash, &vni,
653 vxlan_vni_rht_params);
654 if (!vninode)
655 return 0;
656
657 ret = vxlan_vni_update_group(vxlan, vninode, group, false, changed,
658 extack);
659 if (ret)
660 return ret;
661
662 if (*changed)
663 vxlan_vnifilter_notify(vxlan, vninode, RTM_NEWTUNNEL);
664
665 return 0;
666 }
667
__vxlan_vni_add_list(struct vxlan_vni_group * vg,struct vxlan_vni_node * v)668 static void __vxlan_vni_add_list(struct vxlan_vni_group *vg,
669 struct vxlan_vni_node *v)
670 {
671 struct list_head *headp, *hpos;
672 struct vxlan_vni_node *vent;
673
674 headp = &vg->vni_list;
675 list_for_each_prev(hpos, headp) {
676 vent = list_entry(hpos, struct vxlan_vni_node, vlist);
677 if (be32_to_cpu(v->vni) < be32_to_cpu(vent->vni))
678 continue;
679 else
680 break;
681 }
682 list_add_rcu(&v->vlist, hpos);
683 vg->num_vnis++;
684 }
685
__vxlan_vni_del_list(struct vxlan_vni_group * vg,struct vxlan_vni_node * v)686 static void __vxlan_vni_del_list(struct vxlan_vni_group *vg,
687 struct vxlan_vni_node *v)
688 {
689 list_del_rcu(&v->vlist);
690 vg->num_vnis--;
691 }
692
vxlan_vni_alloc(struct vxlan_dev * vxlan,__be32 vni)693 static struct vxlan_vni_node *vxlan_vni_alloc(struct vxlan_dev *vxlan,
694 __be32 vni)
695 {
696 struct vxlan_vni_node *vninode;
697
698 vninode = kzalloc_obj(*vninode);
699 if (!vninode)
700 return NULL;
701 vninode->stats = netdev_alloc_pcpu_stats(struct vxlan_vni_stats_pcpu);
702 if (!vninode->stats) {
703 kfree(vninode);
704 return NULL;
705 }
706 vninode->vni = vni;
707 vninode->hlist4.vxlan = vxlan;
708 #if IS_ENABLED(CONFIG_IPV6)
709 vninode->hlist6.vxlan = vxlan;
710 #endif
711
712 return vninode;
713 }
714
vxlan_vni_free(struct vxlan_vni_node * vninode)715 static void vxlan_vni_free(struct vxlan_vni_node *vninode)
716 {
717 free_percpu(vninode->stats);
718 kfree(vninode);
719 }
720
vxlan_vni_add(struct vxlan_dev * vxlan,struct vxlan_vni_group * vg,u32 vni,union vxlan_addr * group,struct netlink_ext_ack * extack)721 static int vxlan_vni_add(struct vxlan_dev *vxlan,
722 struct vxlan_vni_group *vg,
723 u32 vni, union vxlan_addr *group,
724 struct netlink_ext_ack *extack)
725 {
726 struct vxlan_vni_node *vninode;
727 __be32 v = cpu_to_be32(vni);
728 bool changed = false;
729 int err = 0;
730
731 if (vxlan_vnifilter_lookup(vxlan, v))
732 return vxlan_vni_update(vxlan, vg, v, group, &changed, extack);
733
734 err = vxlan_vni_in_use(vxlan->net, vxlan, &vxlan->cfg, v);
735 if (err) {
736 NL_SET_ERR_MSG(extack, "VNI in use");
737 return err;
738 }
739
740 vninode = vxlan_vni_alloc(vxlan, v);
741 if (!vninode)
742 return -ENOMEM;
743
744 err = rhashtable_lookup_insert_fast(&vg->vni_hash,
745 &vninode->vnode,
746 vxlan_vni_rht_params);
747 if (err) {
748 vxlan_vni_free(vninode);
749 return err;
750 }
751
752 __vxlan_vni_add_list(vg, vninode);
753
754 if (vxlan->dev->flags & IFF_UP)
755 vxlan_vs_add_del_vninode(vxlan, vninode, false);
756
757 err = vxlan_vni_update_group(vxlan, vninode, group, true, &changed,
758 extack);
759
760 vxlan_vnifilter_notify(vxlan, vninode, RTM_NEWTUNNEL);
761
762 return err;
763 }
764
vxlan_vni_node_rcu_free(struct rcu_head * rcu)765 static void vxlan_vni_node_rcu_free(struct rcu_head *rcu)
766 {
767 struct vxlan_vni_node *v;
768
769 v = container_of(rcu, struct vxlan_vni_node, rcu);
770 vxlan_vni_free(v);
771 }
772
vxlan_vni_del(struct vxlan_dev * vxlan,struct vxlan_vni_group * vg,u32 vni,struct netlink_ext_ack * extack)773 static int vxlan_vni_del(struct vxlan_dev *vxlan,
774 struct vxlan_vni_group *vg,
775 u32 vni, struct netlink_ext_ack *extack)
776 {
777 struct vxlan_vni_node *vninode;
778 __be32 v = cpu_to_be32(vni);
779 int err = 0;
780
781 vg = rtnl_dereference(vxlan->vnigrp);
782
783 vninode = rhashtable_lookup_fast(&vg->vni_hash, &v,
784 vxlan_vni_rht_params);
785 if (!vninode) {
786 err = -ENOENT;
787 goto out;
788 }
789
790 vxlan_vni_delete_group(vxlan, vninode);
791
792 err = rhashtable_remove_fast(&vg->vni_hash,
793 &vninode->vnode,
794 vxlan_vni_rht_params);
795 if (err)
796 goto out;
797
798 __vxlan_vni_del_list(vg, vninode);
799
800 vxlan_vnifilter_notify(vxlan, vninode, RTM_DELTUNNEL);
801
802 if (vxlan->dev->flags & IFF_UP)
803 vxlan_vs_add_del_vninode(vxlan, vninode, true);
804
805 call_rcu(&vninode->rcu, vxlan_vni_node_rcu_free);
806
807 return 0;
808 out:
809 return err;
810 }
811
vxlan_vni_add_del(struct vxlan_dev * vxlan,__u32 start_vni,__u32 end_vni,union vxlan_addr * group,int cmd,struct netlink_ext_ack * extack)812 static int vxlan_vni_add_del(struct vxlan_dev *vxlan, __u32 start_vni,
813 __u32 end_vni, union vxlan_addr *group,
814 int cmd, struct netlink_ext_ack *extack)
815 {
816 struct vxlan_vni_group *vg;
817 int v, err = 0;
818
819 vg = rtnl_dereference(vxlan->vnigrp);
820
821 for (v = start_vni; v <= end_vni; v++) {
822 switch (cmd) {
823 case RTM_NEWTUNNEL:
824 err = vxlan_vni_add(vxlan, vg, v, group, extack);
825 break;
826 case RTM_DELTUNNEL:
827 err = vxlan_vni_del(vxlan, vg, v, extack);
828 break;
829 default:
830 err = -EOPNOTSUPP;
831 break;
832 }
833 if (err)
834 goto out;
835 }
836
837 return 0;
838 out:
839 return err;
840 }
841
vxlan_process_vni_filter(struct vxlan_dev * vxlan,struct nlattr * nlvnifilter,int cmd,struct netlink_ext_ack * extack)842 static int vxlan_process_vni_filter(struct vxlan_dev *vxlan,
843 struct nlattr *nlvnifilter,
844 int cmd, struct netlink_ext_ack *extack)
845 {
846 struct nlattr *vattrs[VXLAN_VNIFILTER_ENTRY_MAX + 1];
847 u32 vni_start = 0, vni_end = 0;
848 union vxlan_addr group;
849 int err;
850
851 err = nla_parse_nested(vattrs,
852 VXLAN_VNIFILTER_ENTRY_MAX,
853 nlvnifilter, vni_filter_entry_policy,
854 extack);
855 if (err)
856 return err;
857
858 if (vattrs[VXLAN_VNIFILTER_ENTRY_START]) {
859 vni_start = nla_get_u32(vattrs[VXLAN_VNIFILTER_ENTRY_START]);
860 vni_end = vni_start;
861 }
862
863 if (vattrs[VXLAN_VNIFILTER_ENTRY_END])
864 vni_end = nla_get_u32(vattrs[VXLAN_VNIFILTER_ENTRY_END]);
865
866 if (!vni_start && !vni_end) {
867 NL_SET_ERR_MSG_ATTR(extack, nlvnifilter,
868 "vni start nor end found in vni entry");
869 return -EINVAL;
870 }
871
872 if (vattrs[VXLAN_VNIFILTER_ENTRY_GROUP]) {
873 group.sin.sin_addr.s_addr =
874 nla_get_in_addr(vattrs[VXLAN_VNIFILTER_ENTRY_GROUP]);
875 group.sa.sa_family = AF_INET;
876 } else if (vattrs[VXLAN_VNIFILTER_ENTRY_GROUP6]) {
877 group.sin6.sin6_addr =
878 nla_get_in6_addr(vattrs[VXLAN_VNIFILTER_ENTRY_GROUP6]);
879 group.sa.sa_family = AF_INET6;
880 } else {
881 memset(&group, 0, sizeof(group));
882 }
883
884 if (vxlan_addr_multicast(&group) && !vxlan->default_dst.remote_ifindex) {
885 NL_SET_ERR_MSG(extack,
886 "Local interface required for multicast remote group");
887
888 return -EINVAL;
889 }
890
891 err = vxlan_vni_add_del(vxlan, vni_start, vni_end, &group, cmd,
892 extack);
893 if (err)
894 return err;
895
896 return 0;
897 }
898
vxlan_vnigroup_uninit(struct vxlan_dev * vxlan)899 void vxlan_vnigroup_uninit(struct vxlan_dev *vxlan)
900 {
901 struct vxlan_vni_node *v, *tmp;
902 struct vxlan_vni_group *vg;
903
904 vg = rtnl_dereference(vxlan->vnigrp);
905 list_for_each_entry_safe(v, tmp, &vg->vni_list, vlist) {
906 rhashtable_remove_fast(&vg->vni_hash, &v->vnode,
907 vxlan_vni_rht_params);
908 hlist_del_init_rcu(&v->hlist4.hlist);
909 #if IS_ENABLED(CONFIG_IPV6)
910 hlist_del_init_rcu(&v->hlist6.hlist);
911 #endif
912 __vxlan_vni_del_list(vg, v);
913 vxlan_vnifilter_notify(vxlan, v, RTM_DELTUNNEL);
914 call_rcu(&v->rcu, vxlan_vni_node_rcu_free);
915 }
916 rhashtable_destroy(&vg->vni_hash);
917 kfree(vg);
918 }
919
vxlan_vnigroup_init(struct vxlan_dev * vxlan)920 int vxlan_vnigroup_init(struct vxlan_dev *vxlan)
921 {
922 struct vxlan_vni_group *vg;
923 int ret;
924
925 vg = kzalloc_obj(*vg);
926 if (!vg)
927 return -ENOMEM;
928 ret = rhashtable_init(&vg->vni_hash, &vxlan_vni_rht_params);
929 if (ret) {
930 kfree(vg);
931 return ret;
932 }
933 INIT_LIST_HEAD(&vg->vni_list);
934 rcu_assign_pointer(vxlan->vnigrp, vg);
935
936 return 0;
937 }
938
vxlan_vnifilter_process(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)939 static int vxlan_vnifilter_process(struct sk_buff *skb, struct nlmsghdr *nlh,
940 struct netlink_ext_ack *extack)
941 {
942 struct net *net = sock_net(skb->sk);
943 struct tunnel_msg *tmsg;
944 struct vxlan_dev *vxlan;
945 struct net_device *dev;
946 struct nlattr *attr;
947 int err, vnis = 0;
948 int rem;
949
950 /* this should validate the header and check for remaining bytes */
951 err = nlmsg_parse(nlh, sizeof(*tmsg), NULL, VXLAN_VNIFILTER_MAX,
952 vni_filter_policy, extack);
953 if (err < 0)
954 return err;
955
956 tmsg = nlmsg_data(nlh);
957 dev = __dev_get_by_index(net, tmsg->ifindex);
958 if (!dev)
959 return -ENODEV;
960
961 if (!netif_is_vxlan(dev)) {
962 NL_SET_ERR_MSG_MOD(extack, "The device is not a vxlan device");
963 return -EINVAL;
964 }
965
966 vxlan = netdev_priv(dev);
967
968 if (!(vxlan->cfg.flags & VXLAN_F_VNIFILTER))
969 return -EOPNOTSUPP;
970
971 nlmsg_for_each_attr_type(attr, VXLAN_VNIFILTER_ENTRY, nlh,
972 sizeof(*tmsg), rem) {
973 err = vxlan_process_vni_filter(vxlan, attr, nlh->nlmsg_type,
974 extack);
975 vnis++;
976 if (err)
977 break;
978 }
979
980 if (!vnis) {
981 NL_SET_ERR_MSG_MOD(extack, "No vnis found to process");
982 err = -EINVAL;
983 }
984
985 return err;
986 }
987
988 static const struct rtnl_msg_handler vxlan_vnifilter_rtnl_msg_handlers[] = {
989 {THIS_MODULE, PF_BRIDGE, RTM_GETTUNNEL, NULL, vxlan_vnifilter_dump, 0},
990 {THIS_MODULE, PF_BRIDGE, RTM_NEWTUNNEL, vxlan_vnifilter_process, NULL, 0},
991 {THIS_MODULE, PF_BRIDGE, RTM_DELTUNNEL, vxlan_vnifilter_process, NULL, 0},
992 };
993
vxlan_vnifilter_init(void)994 int vxlan_vnifilter_init(void)
995 {
996 return rtnl_register_many(vxlan_vnifilter_rtnl_msg_handlers);
997 }
998
vxlan_vnifilter_uninit(void)999 void vxlan_vnifilter_uninit(void)
1000 {
1001 rtnl_unregister_many(vxlan_vnifilter_rtnl_msg_handlers);
1002 }
1003