1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/netdevice.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/slab.h>
6 #include <net/switchdev.h>
7
8 #include "br_private.h"
9 #include "br_private_tunnel.h"
10
11 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);
12
br_vlan_cmp(struct rhashtable_compare_arg * arg,const void * ptr)13 static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
14 const void *ptr)
15 {
16 const struct net_bridge_vlan *vle = ptr;
17 u16 vid = *(u16 *)arg->key;
18
19 return vle->vid != vid;
20 }
21
22 static const struct rhashtable_params br_vlan_rht_params = {
23 .head_offset = offsetof(struct net_bridge_vlan, vnode),
24 .key_offset = offsetof(struct net_bridge_vlan, vid),
25 .key_len = sizeof(u16),
26 .nelem_hint = 3,
27 .max_size = VLAN_N_VID,
28 .obj_cmpfn = br_vlan_cmp,
29 .automatic_shrinking = true,
30 };
31
br_vlan_lookup(struct rhashtable * tbl,u16 vid)32 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
33 {
34 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
35 }
36
__vlan_add_pvid(struct net_bridge_vlan_group * vg,const struct net_bridge_vlan * v)37 static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg,
38 const struct net_bridge_vlan *v)
39 {
40 if (vg->pvid == v->vid)
41 return false;
42
43 smp_wmb();
44 br_vlan_set_pvid_state(vg, v->state);
45 vg->pvid = v->vid;
46
47 return true;
48 }
49
__vlan_delete_pvid(struct net_bridge_vlan_group * vg,u16 vid)50 static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
51 {
52 if (vg->pvid != vid)
53 return false;
54
55 smp_wmb();
56 vg->pvid = 0;
57
58 return true;
59 }
60
61 /* return true if anything changed, false otherwise */
__vlan_add_flags(struct net_bridge_vlan * v,u16 flags)62 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
63 {
64 struct net_bridge_vlan_group *vg;
65 u16 old_flags = v->flags;
66 bool ret;
67
68 if (br_vlan_is_master(v))
69 vg = br_vlan_group(v->br);
70 else
71 vg = nbp_vlan_group(v->port);
72
73 if (flags & BRIDGE_VLAN_INFO_PVID)
74 ret = __vlan_add_pvid(vg, v);
75 else
76 ret = __vlan_delete_pvid(vg, v->vid);
77
78 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
79 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
80 else
81 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
82
83 return ret || !!(old_flags ^ v->flags);
84 }
85
__vlan_vid_add(struct net_device * dev,struct net_bridge * br,struct net_bridge_vlan * v,u16 flags,struct netlink_ext_ack * extack)86 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
87 struct net_bridge_vlan *v, u16 flags,
88 struct netlink_ext_ack *extack)
89 {
90 int err;
91
92 /* Try switchdev op first. In case it is not supported, fallback to
93 * 8021q add.
94 */
95 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
96 if (err == -EOPNOTSUPP)
97 return vlan_vid_add(dev, br->vlan_proto, v->vid);
98 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
99 return err;
100 }
101
__vlan_add_list(struct net_bridge_vlan * v)102 static void __vlan_add_list(struct net_bridge_vlan *v)
103 {
104 struct net_bridge_vlan_group *vg;
105 struct list_head *headp, *hpos;
106 struct net_bridge_vlan *vent;
107
108 if (br_vlan_is_master(v))
109 vg = br_vlan_group(v->br);
110 else
111 vg = nbp_vlan_group(v->port);
112
113 headp = &vg->vlan_list;
114 list_for_each_prev(hpos, headp) {
115 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
116 if (v->vid >= vent->vid)
117 break;
118 }
119 list_add_rcu(&v->vlist, hpos);
120 }
121
__vlan_del_list(struct net_bridge_vlan * v)122 static void __vlan_del_list(struct net_bridge_vlan *v)
123 {
124 list_del_rcu(&v->vlist);
125 }
126
__vlan_vid_del(struct net_device * dev,struct net_bridge * br,const struct net_bridge_vlan * v)127 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
128 const struct net_bridge_vlan *v)
129 {
130 int err;
131
132 /* Try switchdev op first. In case it is not supported, fallback to
133 * 8021q del.
134 */
135 err = br_switchdev_port_vlan_del(dev, v->vid);
136 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
137 vlan_vid_del(dev, br->vlan_proto, v->vid);
138 return err == -EOPNOTSUPP ? 0 : err;
139 }
140
141 /* Returns a master vlan, if it didn't exist it gets created. In all cases
142 * a reference is taken to the master vlan before returning.
143 */
144 static struct net_bridge_vlan *
br_vlan_get_master(struct net_bridge * br,u16 vid,struct netlink_ext_ack * extack)145 br_vlan_get_master(struct net_bridge *br, u16 vid,
146 struct netlink_ext_ack *extack)
147 {
148 struct net_bridge_vlan_group *vg;
149 struct net_bridge_vlan *masterv;
150
151 vg = br_vlan_group(br);
152 masterv = br_vlan_find(vg, vid);
153 if (!masterv) {
154 bool changed;
155
156 /* missing global ctx, create it now */
157 if (br_vlan_add(br, vid, 0, &changed, extack))
158 return NULL;
159 masterv = br_vlan_find(vg, vid);
160 if (WARN_ON(!masterv))
161 return NULL;
162 refcount_set(&masterv->refcnt, 1);
163 return masterv;
164 }
165 refcount_inc(&masterv->refcnt);
166
167 return masterv;
168 }
169
br_master_vlan_rcu_free(struct rcu_head * rcu)170 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
171 {
172 struct net_bridge_vlan *v;
173
174 v = container_of(rcu, struct net_bridge_vlan, rcu);
175 WARN_ON(!br_vlan_is_master(v));
176 free_percpu(v->stats);
177 v->stats = NULL;
178 kfree(v);
179 }
180
br_vlan_put_master(struct net_bridge_vlan * masterv)181 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
182 {
183 struct net_bridge_vlan_group *vg;
184
185 if (!br_vlan_is_master(masterv))
186 return;
187
188 vg = br_vlan_group(masterv->br);
189 if (refcount_dec_and_test(&masterv->refcnt)) {
190 rhashtable_remove_fast(&vg->vlan_hash,
191 &masterv->vnode, br_vlan_rht_params);
192 __vlan_del_list(masterv);
193 br_multicast_toggle_one_vlan(masterv, false);
194 br_multicast_ctx_deinit(&masterv->br_mcast_ctx);
195 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
196 }
197 }
198
nbp_vlan_rcu_free(struct rcu_head * rcu)199 static void nbp_vlan_rcu_free(struct rcu_head *rcu)
200 {
201 struct net_bridge_vlan *v;
202
203 v = container_of(rcu, struct net_bridge_vlan, rcu);
204 WARN_ON(br_vlan_is_master(v));
205 /* if we had per-port stats configured then free them here */
206 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
207 free_percpu(v->stats);
208 v->stats = NULL;
209 kfree(v);
210 }
211
212 /* This is the shared VLAN add function which works for both ports and bridge
213 * devices. There are four possible calls to this function in terms of the
214 * vlan entry type:
215 * 1. vlan is being added on a port (no master flags, global entry exists)
216 * 2. vlan is being added on a bridge (both master and brentry flags)
217 * 3. vlan is being added on a port, but a global entry didn't exist which
218 * is being created right now (master flag set, brentry flag unset), the
219 * global entry is used for global per-vlan features, but not for filtering
220 * 4. same as 3 but with both master and brentry flags set so the entry
221 * will be used for filtering in both the port and the bridge
222 */
__vlan_add(struct net_bridge_vlan * v,u16 flags,struct netlink_ext_ack * extack)223 static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
224 struct netlink_ext_ack *extack)
225 {
226 struct net_bridge_vlan *masterv = NULL;
227 struct net_bridge_port *p = NULL;
228 struct net_bridge_vlan_group *vg;
229 struct net_device *dev;
230 struct net_bridge *br;
231 int err;
232
233 if (br_vlan_is_master(v)) {
234 br = v->br;
235 dev = br->dev;
236 vg = br_vlan_group(br);
237 } else {
238 p = v->port;
239 br = p->br;
240 dev = p->dev;
241 vg = nbp_vlan_group(p);
242 }
243
244 if (p) {
245 /* Add VLAN to the device filter if it is supported.
246 * This ensures tagged traffic enters the bridge when
247 * promiscuous mode is disabled by br_manage_promisc().
248 */
249 err = __vlan_vid_add(dev, br, v, flags, extack);
250 if (err)
251 goto out;
252
253 /* need to work on the master vlan too */
254 if (flags & BRIDGE_VLAN_INFO_MASTER) {
255 bool changed;
256
257 err = br_vlan_add(br, v->vid,
258 flags | BRIDGE_VLAN_INFO_BRENTRY,
259 &changed, extack);
260 if (err)
261 goto out_filt;
262
263 if (changed)
264 br_vlan_notify(br, NULL, v->vid, 0,
265 RTM_NEWVLAN);
266 }
267
268 masterv = br_vlan_get_master(br, v->vid, extack);
269 if (!masterv) {
270 err = -ENOMEM;
271 goto out_filt;
272 }
273 v->brvlan = masterv;
274 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
275 v->stats =
276 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
277 if (!v->stats) {
278 err = -ENOMEM;
279 goto out_filt;
280 }
281 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
282 } else {
283 v->stats = masterv->stats;
284 }
285 br_multicast_port_ctx_init(p, v, &v->port_mcast_ctx);
286 } else {
287 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
288 if (err && err != -EOPNOTSUPP)
289 goto out;
290 br_multicast_ctx_init(br, v, &v->br_mcast_ctx);
291 v->priv_flags |= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
292 }
293
294 /* Add the dev mac and count the vlan only if it's usable */
295 if (br_vlan_should_use(v)) {
296 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
297 if (err) {
298 br_err(br, "failed insert local address into bridge forwarding table\n");
299 goto out_filt;
300 }
301 vg->num_vlans++;
302 }
303
304 /* set the state before publishing */
305 v->state = BR_STATE_FORWARDING;
306
307 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
308 br_vlan_rht_params);
309 if (err)
310 goto out_fdb_insert;
311
312 __vlan_add_list(v);
313 __vlan_add_flags(v, flags);
314 br_multicast_toggle_one_vlan(v, true);
315
316 if (p)
317 nbp_vlan_set_vlan_dev_state(p, v->vid);
318 out:
319 return err;
320
321 out_fdb_insert:
322 if (br_vlan_should_use(v)) {
323 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
324 vg->num_vlans--;
325 }
326
327 out_filt:
328 if (p) {
329 __vlan_vid_del(dev, br, v);
330 if (masterv) {
331 if (v->stats && masterv->stats != v->stats)
332 free_percpu(v->stats);
333 v->stats = NULL;
334
335 br_vlan_put_master(masterv);
336 v->brvlan = NULL;
337 }
338 } else {
339 br_switchdev_port_vlan_del(dev, v->vid);
340 }
341
342 goto out;
343 }
344
__vlan_del(struct net_bridge_vlan * v)345 static int __vlan_del(struct net_bridge_vlan *v)
346 {
347 struct net_bridge_vlan *masterv = v;
348 struct net_bridge_vlan_group *vg;
349 struct net_bridge_port *p = NULL;
350 int err = 0;
351
352 if (br_vlan_is_master(v)) {
353 vg = br_vlan_group(v->br);
354 } else {
355 p = v->port;
356 vg = nbp_vlan_group(v->port);
357 masterv = v->brvlan;
358 }
359
360 __vlan_delete_pvid(vg, v->vid);
361 if (p) {
362 err = __vlan_vid_del(p->dev, p->br, v);
363 if (err)
364 goto out;
365 } else {
366 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
367 if (err && err != -EOPNOTSUPP)
368 goto out;
369 err = 0;
370 }
371
372 if (br_vlan_should_use(v)) {
373 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
374 vg->num_vlans--;
375 }
376
377 if (masterv != v) {
378 vlan_tunnel_info_del(vg, v);
379 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
380 br_vlan_rht_params);
381 __vlan_del_list(v);
382 nbp_vlan_set_vlan_dev_state(p, v->vid);
383 br_multicast_toggle_one_vlan(v, false);
384 br_multicast_port_ctx_deinit(&v->port_mcast_ctx);
385 call_rcu(&v->rcu, nbp_vlan_rcu_free);
386 }
387
388 br_vlan_put_master(masterv);
389 out:
390 return err;
391 }
392
__vlan_group_free(struct net_bridge_vlan_group * vg)393 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
394 {
395 WARN_ON(!list_empty(&vg->vlan_list));
396 rhashtable_destroy(&vg->vlan_hash);
397 vlan_tunnel_deinit(vg);
398 kfree(vg);
399 }
400
__vlan_flush(const struct net_bridge * br,const struct net_bridge_port * p,struct net_bridge_vlan_group * vg)401 static void __vlan_flush(const struct net_bridge *br,
402 const struct net_bridge_port *p,
403 struct net_bridge_vlan_group *vg)
404 {
405 struct net_bridge_vlan *vlan, *tmp;
406 u16 v_start = 0, v_end = 0;
407
408 __vlan_delete_pvid(vg, vg->pvid);
409 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
410 /* take care of disjoint ranges */
411 if (!v_start) {
412 v_start = vlan->vid;
413 } else if (vlan->vid - v_end != 1) {
414 /* found range end, notify and start next one */
415 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
416 v_start = vlan->vid;
417 }
418 v_end = vlan->vid;
419
420 __vlan_del(vlan);
421 }
422
423 /* notify about the last/whole vlan range */
424 if (v_start)
425 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
426 }
427
br_handle_vlan(struct net_bridge * br,const struct net_bridge_port * p,struct net_bridge_vlan_group * vg,struct sk_buff * skb)428 struct sk_buff *br_handle_vlan(struct net_bridge *br,
429 const struct net_bridge_port *p,
430 struct net_bridge_vlan_group *vg,
431 struct sk_buff *skb)
432 {
433 struct pcpu_sw_netstats *stats;
434 struct net_bridge_vlan *v;
435 u16 vid;
436
437 /* If this packet was not filtered at input, let it pass */
438 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
439 goto out;
440
441 /* At this point, we know that the frame was filtered and contains
442 * a valid vlan id. If the vlan id has untagged flag set,
443 * send untagged; otherwise, send tagged.
444 */
445 br_vlan_get_tag(skb, &vid);
446 v = br_vlan_find(vg, vid);
447 /* Vlan entry must be configured at this point. The
448 * only exception is the bridge is set in promisc mode and the
449 * packet is destined for the bridge device. In this case
450 * pass the packet as is.
451 */
452 if (!v || !br_vlan_should_use(v)) {
453 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
454 goto out;
455 } else {
456 kfree_skb(skb);
457 return NULL;
458 }
459 }
460 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
461 stats = this_cpu_ptr(v->stats);
462 u64_stats_update_begin(&stats->syncp);
463 stats->tx_bytes += skb->len;
464 stats->tx_packets++;
465 u64_stats_update_end(&stats->syncp);
466 }
467
468 /* If the skb will be sent using forwarding offload, the assumption is
469 * that the switchdev will inject the packet into hardware together
470 * with the bridge VLAN, so that it can be forwarded according to that
471 * VLAN. The switchdev should deal with popping the VLAN header in
472 * hardware on each egress port as appropriate. So only strip the VLAN
473 * header if forwarding offload is not being used.
474 */
475 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED &&
476 !br_switchdev_frame_uses_tx_fwd_offload(skb))
477 __vlan_hwaccel_clear_tag(skb);
478
479 if (p && (p->flags & BR_VLAN_TUNNEL) &&
480 br_handle_egress_vlan_tunnel(skb, v)) {
481 kfree_skb(skb);
482 return NULL;
483 }
484 out:
485 return skb;
486 }
487
488 /* Called under RCU */
__allowed_ingress(const struct net_bridge * br,struct net_bridge_vlan_group * vg,struct sk_buff * skb,u16 * vid,u8 * state,struct net_bridge_vlan ** vlan)489 static bool __allowed_ingress(const struct net_bridge *br,
490 struct net_bridge_vlan_group *vg,
491 struct sk_buff *skb, u16 *vid,
492 u8 *state,
493 struct net_bridge_vlan **vlan)
494 {
495 struct pcpu_sw_netstats *stats;
496 struct net_bridge_vlan *v;
497 bool tagged;
498
499 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
500 /* If vlan tx offload is disabled on bridge device and frame was
501 * sent from vlan device on the bridge device, it does not have
502 * HW accelerated vlan tag.
503 */
504 if (unlikely(!skb_vlan_tag_present(skb) &&
505 skb->protocol == br->vlan_proto)) {
506 skb = skb_vlan_untag(skb);
507 if (unlikely(!skb))
508 return false;
509 }
510
511 if (!br_vlan_get_tag(skb, vid)) {
512 /* Tagged frame */
513 if (skb->vlan_proto != br->vlan_proto) {
514 /* Protocol-mismatch, empty out vlan_tci for new tag */
515 skb_push(skb, ETH_HLEN);
516 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
517 skb_vlan_tag_get(skb));
518 if (unlikely(!skb))
519 return false;
520
521 skb_pull(skb, ETH_HLEN);
522 skb_reset_mac_len(skb);
523 *vid = 0;
524 tagged = false;
525 } else {
526 tagged = true;
527 }
528 } else {
529 /* Untagged frame */
530 tagged = false;
531 }
532
533 if (!*vid) {
534 u16 pvid = br_get_pvid(vg);
535
536 /* Frame had a tag with VID 0 or did not have a tag.
537 * See if pvid is set on this port. That tells us which
538 * vlan untagged or priority-tagged traffic belongs to.
539 */
540 if (!pvid)
541 goto drop;
542
543 /* PVID is set on this port. Any untagged or priority-tagged
544 * ingress frame is considered to belong to this vlan.
545 */
546 *vid = pvid;
547 if (likely(!tagged))
548 /* Untagged Frame. */
549 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
550 else
551 /* Priority-tagged Frame.
552 * At this point, we know that skb->vlan_tci VID
553 * field was 0.
554 * We update only VID field and preserve PCP field.
555 */
556 skb->vlan_tci |= pvid;
557
558 /* if snooping and stats are disabled we can avoid the lookup */
559 if (!br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) &&
560 !br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
561 if (*state == BR_STATE_FORWARDING) {
562 *state = br_vlan_get_pvid_state(vg);
563 if (!br_vlan_state_allowed(*state, true))
564 goto drop;
565 }
566 return true;
567 }
568 }
569 v = br_vlan_find(vg, *vid);
570 if (!v || !br_vlan_should_use(v))
571 goto drop;
572
573 if (*state == BR_STATE_FORWARDING) {
574 *state = br_vlan_get_state(v);
575 if (!br_vlan_state_allowed(*state, true))
576 goto drop;
577 }
578
579 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
580 stats = this_cpu_ptr(v->stats);
581 u64_stats_update_begin(&stats->syncp);
582 stats->rx_bytes += skb->len;
583 stats->rx_packets++;
584 u64_stats_update_end(&stats->syncp);
585 }
586
587 *vlan = v;
588
589 return true;
590
591 drop:
592 kfree_skb(skb);
593 return false;
594 }
595
br_allowed_ingress(const struct net_bridge * br,struct net_bridge_vlan_group * vg,struct sk_buff * skb,u16 * vid,u8 * state,struct net_bridge_vlan ** vlan)596 bool br_allowed_ingress(const struct net_bridge *br,
597 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
598 u16 *vid, u8 *state,
599 struct net_bridge_vlan **vlan)
600 {
601 /* If VLAN filtering is disabled on the bridge, all packets are
602 * permitted.
603 */
604 *vlan = NULL;
605 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
606 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
607 return true;
608 }
609
610 return __allowed_ingress(br, vg, skb, vid, state, vlan);
611 }
612
613 /* Called under RCU. */
br_allowed_egress(struct net_bridge_vlan_group * vg,const struct sk_buff * skb)614 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
615 const struct sk_buff *skb)
616 {
617 const struct net_bridge_vlan *v;
618 u16 vid;
619
620 /* If this packet was not filtered at input, let it pass */
621 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
622 return true;
623
624 br_vlan_get_tag(skb, &vid);
625 v = br_vlan_find(vg, vid);
626 if (v && br_vlan_should_use(v) &&
627 br_vlan_state_allowed(br_vlan_get_state(v), false))
628 return true;
629
630 return false;
631 }
632
633 /* Called under RCU */
br_should_learn(struct net_bridge_port * p,struct sk_buff * skb,u16 * vid)634 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
635 {
636 struct net_bridge_vlan_group *vg;
637 struct net_bridge *br = p->br;
638 struct net_bridge_vlan *v;
639
640 /* If filtering was disabled at input, let it pass. */
641 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
642 return true;
643
644 vg = nbp_vlan_group_rcu(p);
645 if (!vg || !vg->num_vlans)
646 return false;
647
648 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
649 *vid = 0;
650
651 if (!*vid) {
652 *vid = br_get_pvid(vg);
653 if (!*vid ||
654 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
655 return false;
656
657 return true;
658 }
659
660 v = br_vlan_find(vg, *vid);
661 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
662 return true;
663
664 return false;
665 }
666
br_vlan_add_existing(struct net_bridge * br,struct net_bridge_vlan_group * vg,struct net_bridge_vlan * vlan,u16 flags,bool * changed,struct netlink_ext_ack * extack)667 static int br_vlan_add_existing(struct net_bridge *br,
668 struct net_bridge_vlan_group *vg,
669 struct net_bridge_vlan *vlan,
670 u16 flags, bool *changed,
671 struct netlink_ext_ack *extack)
672 {
673 int err;
674
675 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
676 if (err && err != -EOPNOTSUPP)
677 return err;
678
679 if (!br_vlan_is_brentry(vlan)) {
680 /* Trying to change flags of non-existent bridge vlan */
681 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
682 err = -EINVAL;
683 goto err_flags;
684 }
685 /* It was only kept for port vlans, now make it real */
686 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
687 vlan->vid);
688 if (err) {
689 br_err(br, "failed to insert local address into bridge forwarding table\n");
690 goto err_fdb_insert;
691 }
692
693 refcount_inc(&vlan->refcnt);
694 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
695 vg->num_vlans++;
696 *changed = true;
697 br_multicast_toggle_one_vlan(vlan, true);
698 }
699
700 if (__vlan_add_flags(vlan, flags))
701 *changed = true;
702
703 return 0;
704
705 err_fdb_insert:
706 err_flags:
707 br_switchdev_port_vlan_del(br->dev, vlan->vid);
708 return err;
709 }
710
711 /* Must be protected by RTNL.
712 * Must be called with vid in range from 1 to 4094 inclusive.
713 * changed must be true only if the vlan was created or updated
714 */
br_vlan_add(struct net_bridge * br,u16 vid,u16 flags,bool * changed,struct netlink_ext_ack * extack)715 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
716 struct netlink_ext_ack *extack)
717 {
718 struct net_bridge_vlan_group *vg;
719 struct net_bridge_vlan *vlan;
720 int ret;
721
722 ASSERT_RTNL();
723
724 *changed = false;
725 vg = br_vlan_group(br);
726 vlan = br_vlan_find(vg, vid);
727 if (vlan)
728 return br_vlan_add_existing(br, vg, vlan, flags, changed,
729 extack);
730
731 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
732 if (!vlan)
733 return -ENOMEM;
734
735 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
736 if (!vlan->stats) {
737 kfree(vlan);
738 return -ENOMEM;
739 }
740 vlan->vid = vid;
741 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
742 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
743 vlan->br = br;
744 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
745 refcount_set(&vlan->refcnt, 1);
746 ret = __vlan_add(vlan, flags, extack);
747 if (ret) {
748 free_percpu(vlan->stats);
749 kfree(vlan);
750 } else {
751 *changed = true;
752 }
753
754 return ret;
755 }
756
757 /* Must be protected by RTNL.
758 * Must be called with vid in range from 1 to 4094 inclusive.
759 */
br_vlan_delete(struct net_bridge * br,u16 vid)760 int br_vlan_delete(struct net_bridge *br, u16 vid)
761 {
762 struct net_bridge_vlan_group *vg;
763 struct net_bridge_vlan *v;
764
765 ASSERT_RTNL();
766
767 vg = br_vlan_group(br);
768 v = br_vlan_find(vg, vid);
769 if (!v || !br_vlan_is_brentry(v))
770 return -ENOENT;
771
772 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
773 br_fdb_delete_by_port(br, NULL, vid, 0);
774
775 vlan_tunnel_info_del(vg, v);
776
777 return __vlan_del(v);
778 }
779
br_vlan_flush(struct net_bridge * br)780 void br_vlan_flush(struct net_bridge *br)
781 {
782 struct net_bridge_vlan_group *vg;
783
784 ASSERT_RTNL();
785
786 vg = br_vlan_group(br);
787 __vlan_flush(br, NULL, vg);
788 RCU_INIT_POINTER(br->vlgrp, NULL);
789 synchronize_rcu();
790 __vlan_group_free(vg);
791 }
792
br_vlan_find(struct net_bridge_vlan_group * vg,u16 vid)793 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
794 {
795 if (!vg)
796 return NULL;
797
798 return br_vlan_lookup(&vg->vlan_hash, vid);
799 }
800
801 /* Must be protected by RTNL. */
recalculate_group_addr(struct net_bridge * br)802 static void recalculate_group_addr(struct net_bridge *br)
803 {
804 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
805 return;
806
807 spin_lock_bh(&br->lock);
808 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
809 br->vlan_proto == htons(ETH_P_8021Q)) {
810 /* Bridge Group Address */
811 br->group_addr[5] = 0x00;
812 } else { /* vlan_enabled && ETH_P_8021AD */
813 /* Provider Bridge Group Address */
814 br->group_addr[5] = 0x08;
815 }
816 spin_unlock_bh(&br->lock);
817 }
818
819 /* Must be protected by RTNL. */
br_recalculate_fwd_mask(struct net_bridge * br)820 void br_recalculate_fwd_mask(struct net_bridge *br)
821 {
822 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
823 br->vlan_proto == htons(ETH_P_8021Q))
824 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
825 else /* vlan_enabled && ETH_P_8021AD */
826 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
827 ~(1u << br->group_addr[5]);
828 }
829
br_vlan_filter_toggle(struct net_bridge * br,unsigned long val,struct netlink_ext_ack * extack)830 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
831 struct netlink_ext_ack *extack)
832 {
833 struct switchdev_attr attr = {
834 .orig_dev = br->dev,
835 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
836 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
837 .u.vlan_filtering = val,
838 };
839 int err;
840
841 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
842 return 0;
843
844 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
845
846 err = switchdev_port_attr_set(br->dev, &attr, extack);
847 if (err && err != -EOPNOTSUPP) {
848 br_opt_toggle(br, BROPT_VLAN_ENABLED, !val);
849 return err;
850 }
851
852 br_manage_promisc(br);
853 recalculate_group_addr(br);
854 br_recalculate_fwd_mask(br);
855 if (!val && br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
856 br_info(br, "vlan filtering disabled, automatically disabling multicast vlan snooping\n");
857 br_multicast_toggle_vlan_snooping(br, false, NULL);
858 }
859
860 return 0;
861 }
862
br_vlan_enabled(const struct net_device * dev)863 bool br_vlan_enabled(const struct net_device *dev)
864 {
865 struct net_bridge *br = netdev_priv(dev);
866
867 return br_opt_get(br, BROPT_VLAN_ENABLED);
868 }
869 EXPORT_SYMBOL_GPL(br_vlan_enabled);
870
br_vlan_get_proto(const struct net_device * dev,u16 * p_proto)871 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
872 {
873 struct net_bridge *br = netdev_priv(dev);
874
875 *p_proto = ntohs(br->vlan_proto);
876
877 return 0;
878 }
879 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
880
__br_vlan_set_proto(struct net_bridge * br,__be16 proto,struct netlink_ext_ack * extack)881 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
882 struct netlink_ext_ack *extack)
883 {
884 struct switchdev_attr attr = {
885 .orig_dev = br->dev,
886 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
887 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
888 .u.vlan_protocol = ntohs(proto),
889 };
890 int err = 0;
891 struct net_bridge_port *p;
892 struct net_bridge_vlan *vlan;
893 struct net_bridge_vlan_group *vg;
894 __be16 oldproto = br->vlan_proto;
895
896 if (br->vlan_proto == proto)
897 return 0;
898
899 err = switchdev_port_attr_set(br->dev, &attr, extack);
900 if (err && err != -EOPNOTSUPP)
901 return err;
902
903 /* Add VLANs for the new proto to the device filter. */
904 list_for_each_entry(p, &br->port_list, list) {
905 vg = nbp_vlan_group(p);
906 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
907 if (vlan->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
908 continue;
909 err = vlan_vid_add(p->dev, proto, vlan->vid);
910 if (err)
911 goto err_filt;
912 }
913 }
914
915 br->vlan_proto = proto;
916
917 recalculate_group_addr(br);
918 br_recalculate_fwd_mask(br);
919
920 /* Delete VLANs for the old proto from the device filter. */
921 list_for_each_entry(p, &br->port_list, list) {
922 vg = nbp_vlan_group(p);
923 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
924 if (vlan->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
925 continue;
926 vlan_vid_del(p->dev, oldproto, vlan->vid);
927 }
928 }
929
930 return 0;
931
932 err_filt:
933 attr.u.vlan_protocol = ntohs(oldproto);
934 switchdev_port_attr_set(br->dev, &attr, NULL);
935
936 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist) {
937 if (vlan->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
938 continue;
939 vlan_vid_del(p->dev, proto, vlan->vid);
940 }
941
942 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
943 vg = nbp_vlan_group(p);
944 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
945 if (vlan->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
946 continue;
947 vlan_vid_del(p->dev, proto, vlan->vid);
948 }
949 }
950
951 return err;
952 }
953
br_vlan_set_proto(struct net_bridge * br,unsigned long val,struct netlink_ext_ack * extack)954 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
955 struct netlink_ext_ack *extack)
956 {
957 if (!eth_type_vlan(htons(val)))
958 return -EPROTONOSUPPORT;
959
960 return __br_vlan_set_proto(br, htons(val), extack);
961 }
962
br_vlan_set_stats(struct net_bridge * br,unsigned long val)963 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
964 {
965 switch (val) {
966 case 0:
967 case 1:
968 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
969 break;
970 default:
971 return -EINVAL;
972 }
973
974 return 0;
975 }
976
br_vlan_set_stats_per_port(struct net_bridge * br,unsigned long val)977 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
978 {
979 struct net_bridge_port *p;
980
981 /* allow to change the option if there are no port vlans configured */
982 list_for_each_entry(p, &br->port_list, list) {
983 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
984
985 if (vg->num_vlans)
986 return -EBUSY;
987 }
988
989 switch (val) {
990 case 0:
991 case 1:
992 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
993 break;
994 default:
995 return -EINVAL;
996 }
997
998 return 0;
999 }
1000
vlan_default_pvid(struct net_bridge_vlan_group * vg,u16 vid)1001 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
1002 {
1003 struct net_bridge_vlan *v;
1004
1005 if (vid != vg->pvid)
1006 return false;
1007
1008 v = br_vlan_lookup(&vg->vlan_hash, vid);
1009 if (v && br_vlan_should_use(v) &&
1010 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1011 return true;
1012
1013 return false;
1014 }
1015
br_vlan_disable_default_pvid(struct net_bridge * br)1016 static void br_vlan_disable_default_pvid(struct net_bridge *br)
1017 {
1018 struct net_bridge_port *p;
1019 u16 pvid = br->default_pvid;
1020
1021 /* Disable default_pvid on all ports where it is still
1022 * configured.
1023 */
1024 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
1025 if (!br_vlan_delete(br, pvid))
1026 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1027 }
1028
1029 list_for_each_entry(p, &br->port_list, list) {
1030 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
1031 !nbp_vlan_delete(p, pvid))
1032 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1033 }
1034
1035 br->default_pvid = 0;
1036 }
1037
__br_vlan_set_default_pvid(struct net_bridge * br,u16 pvid,struct netlink_ext_ack * extack)1038 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1039 struct netlink_ext_ack *extack)
1040 {
1041 const struct net_bridge_vlan *pvent;
1042 struct net_bridge_vlan_group *vg;
1043 struct net_bridge_port *p;
1044 unsigned long *changed;
1045 bool vlchange;
1046 u16 old_pvid;
1047 int err = 0;
1048
1049 if (!pvid) {
1050 br_vlan_disable_default_pvid(br);
1051 return 0;
1052 }
1053
1054 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
1055 if (!changed)
1056 return -ENOMEM;
1057
1058 old_pvid = br->default_pvid;
1059
1060 /* Update default_pvid config only if we do not conflict with
1061 * user configuration.
1062 */
1063 vg = br_vlan_group(br);
1064 pvent = br_vlan_find(vg, pvid);
1065 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
1066 (!pvent || !br_vlan_should_use(pvent))) {
1067 err = br_vlan_add(br, pvid,
1068 BRIDGE_VLAN_INFO_PVID |
1069 BRIDGE_VLAN_INFO_UNTAGGED |
1070 BRIDGE_VLAN_INFO_BRENTRY,
1071 &vlchange, extack);
1072 if (err)
1073 goto out;
1074
1075 if (br_vlan_delete(br, old_pvid))
1076 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1077 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
1078 set_bit(0, changed);
1079 }
1080
1081 list_for_each_entry(p, &br->port_list, list) {
1082 /* Update default_pvid config only if we do not conflict with
1083 * user configuration.
1084 */
1085 vg = nbp_vlan_group(p);
1086 if ((old_pvid &&
1087 !vlan_default_pvid(vg, old_pvid)) ||
1088 br_vlan_find(vg, pvid))
1089 continue;
1090
1091 err = nbp_vlan_add(p, pvid,
1092 BRIDGE_VLAN_INFO_PVID |
1093 BRIDGE_VLAN_INFO_UNTAGGED,
1094 &vlchange, extack);
1095 if (err)
1096 goto err_port;
1097 if (nbp_vlan_delete(p, old_pvid))
1098 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1099 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
1100 set_bit(p->port_no, changed);
1101 }
1102
1103 br->default_pvid = pvid;
1104
1105 out:
1106 bitmap_free(changed);
1107 return err;
1108
1109 err_port:
1110 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1111 if (!test_bit(p->port_no, changed))
1112 continue;
1113
1114 if (old_pvid) {
1115 nbp_vlan_add(p, old_pvid,
1116 BRIDGE_VLAN_INFO_PVID |
1117 BRIDGE_VLAN_INFO_UNTAGGED,
1118 &vlchange, NULL);
1119 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1120 }
1121 nbp_vlan_delete(p, pvid);
1122 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1123 }
1124
1125 if (test_bit(0, changed)) {
1126 if (old_pvid) {
1127 br_vlan_add(br, old_pvid,
1128 BRIDGE_VLAN_INFO_PVID |
1129 BRIDGE_VLAN_INFO_UNTAGGED |
1130 BRIDGE_VLAN_INFO_BRENTRY,
1131 &vlchange, NULL);
1132 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1133 }
1134 br_vlan_delete(br, pvid);
1135 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1136 }
1137 goto out;
1138 }
1139
br_vlan_set_default_pvid(struct net_bridge * br,unsigned long val,struct netlink_ext_ack * extack)1140 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1141 struct netlink_ext_ack *extack)
1142 {
1143 u16 pvid = val;
1144 int err = 0;
1145
1146 if (val >= VLAN_VID_MASK)
1147 return -EINVAL;
1148
1149 if (pvid == br->default_pvid)
1150 goto out;
1151
1152 /* Only allow default pvid change when filtering is disabled */
1153 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1154 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1155 err = -EPERM;
1156 goto out;
1157 }
1158 err = __br_vlan_set_default_pvid(br, pvid, extack);
1159 out:
1160 return err;
1161 }
1162
br_vlan_init(struct net_bridge * br)1163 int br_vlan_init(struct net_bridge *br)
1164 {
1165 struct net_bridge_vlan_group *vg;
1166 int ret = -ENOMEM;
1167
1168 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1169 if (!vg)
1170 goto out;
1171 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1172 if (ret)
1173 goto err_rhtbl;
1174 ret = vlan_tunnel_init(vg);
1175 if (ret)
1176 goto err_tunnel_init;
1177 INIT_LIST_HEAD(&vg->vlan_list);
1178 br->vlan_proto = htons(ETH_P_8021Q);
1179 br->default_pvid = 1;
1180 rcu_assign_pointer(br->vlgrp, vg);
1181
1182 out:
1183 return ret;
1184
1185 err_tunnel_init:
1186 rhashtable_destroy(&vg->vlan_hash);
1187 err_rhtbl:
1188 kfree(vg);
1189
1190 goto out;
1191 }
1192
nbp_vlan_init(struct net_bridge_port * p,struct netlink_ext_ack * extack)1193 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1194 {
1195 struct switchdev_attr attr = {
1196 .orig_dev = p->br->dev,
1197 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1198 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1199 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1200 };
1201 struct net_bridge_vlan_group *vg;
1202 int ret = -ENOMEM;
1203
1204 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1205 if (!vg)
1206 goto out;
1207
1208 ret = switchdev_port_attr_set(p->dev, &attr, extack);
1209 if (ret && ret != -EOPNOTSUPP)
1210 goto err_vlan_enabled;
1211
1212 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1213 if (ret)
1214 goto err_rhtbl;
1215 ret = vlan_tunnel_init(vg);
1216 if (ret)
1217 goto err_tunnel_init;
1218 INIT_LIST_HEAD(&vg->vlan_list);
1219 rcu_assign_pointer(p->vlgrp, vg);
1220 if (p->br->default_pvid) {
1221 bool changed;
1222
1223 ret = nbp_vlan_add(p, p->br->default_pvid,
1224 BRIDGE_VLAN_INFO_PVID |
1225 BRIDGE_VLAN_INFO_UNTAGGED,
1226 &changed, extack);
1227 if (ret)
1228 goto err_vlan_add;
1229 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
1230 }
1231 out:
1232 return ret;
1233
1234 err_vlan_add:
1235 RCU_INIT_POINTER(p->vlgrp, NULL);
1236 synchronize_rcu();
1237 vlan_tunnel_deinit(vg);
1238 err_tunnel_init:
1239 rhashtable_destroy(&vg->vlan_hash);
1240 err_rhtbl:
1241 err_vlan_enabled:
1242 kfree(vg);
1243
1244 goto out;
1245 }
1246
1247 /* Must be protected by RTNL.
1248 * Must be called with vid in range from 1 to 4094 inclusive.
1249 * changed must be true only if the vlan was created or updated
1250 */
nbp_vlan_add(struct net_bridge_port * port,u16 vid,u16 flags,bool * changed,struct netlink_ext_ack * extack)1251 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1252 bool *changed, struct netlink_ext_ack *extack)
1253 {
1254 struct net_bridge_vlan *vlan;
1255 int ret;
1256
1257 ASSERT_RTNL();
1258
1259 *changed = false;
1260 vlan = br_vlan_find(nbp_vlan_group(port), vid);
1261 if (vlan) {
1262 /* Pass the flags to the hardware bridge */
1263 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1264 if (ret && ret != -EOPNOTSUPP)
1265 return ret;
1266 *changed = __vlan_add_flags(vlan, flags);
1267
1268 return 0;
1269 }
1270
1271 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1272 if (!vlan)
1273 return -ENOMEM;
1274
1275 vlan->vid = vid;
1276 vlan->port = port;
1277 ret = __vlan_add(vlan, flags, extack);
1278 if (ret)
1279 kfree(vlan);
1280 else
1281 *changed = true;
1282
1283 return ret;
1284 }
1285
1286 /* Must be protected by RTNL.
1287 * Must be called with vid in range from 1 to 4094 inclusive.
1288 */
nbp_vlan_delete(struct net_bridge_port * port,u16 vid)1289 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1290 {
1291 struct net_bridge_vlan *v;
1292
1293 ASSERT_RTNL();
1294
1295 v = br_vlan_find(nbp_vlan_group(port), vid);
1296 if (!v)
1297 return -ENOENT;
1298 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1299 br_fdb_delete_by_port(port->br, port, vid, 0);
1300
1301 return __vlan_del(v);
1302 }
1303
nbp_vlan_flush(struct net_bridge_port * port)1304 void nbp_vlan_flush(struct net_bridge_port *port)
1305 {
1306 struct net_bridge_vlan_group *vg;
1307
1308 ASSERT_RTNL();
1309
1310 vg = nbp_vlan_group(port);
1311 __vlan_flush(port->br, port, vg);
1312 RCU_INIT_POINTER(port->vlgrp, NULL);
1313 synchronize_rcu();
1314 __vlan_group_free(vg);
1315 }
1316
br_vlan_get_stats(const struct net_bridge_vlan * v,struct pcpu_sw_netstats * stats)1317 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1318 struct pcpu_sw_netstats *stats)
1319 {
1320 int i;
1321
1322 memset(stats, 0, sizeof(*stats));
1323 for_each_possible_cpu(i) {
1324 u64 rxpackets, rxbytes, txpackets, txbytes;
1325 struct pcpu_sw_netstats *cpu_stats;
1326 unsigned int start;
1327
1328 cpu_stats = per_cpu_ptr(v->stats, i);
1329 do {
1330 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1331 rxpackets = cpu_stats->rx_packets;
1332 rxbytes = cpu_stats->rx_bytes;
1333 txbytes = cpu_stats->tx_bytes;
1334 txpackets = cpu_stats->tx_packets;
1335 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1336
1337 stats->rx_packets += rxpackets;
1338 stats->rx_bytes += rxbytes;
1339 stats->tx_bytes += txbytes;
1340 stats->tx_packets += txpackets;
1341 }
1342 }
1343
br_vlan_get_pvid(const struct net_device * dev,u16 * p_pvid)1344 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1345 {
1346 struct net_bridge_vlan_group *vg;
1347 struct net_bridge_port *p;
1348
1349 ASSERT_RTNL();
1350 p = br_port_get_check_rtnl(dev);
1351 if (p)
1352 vg = nbp_vlan_group(p);
1353 else if (netif_is_bridge_master(dev))
1354 vg = br_vlan_group(netdev_priv(dev));
1355 else
1356 return -EINVAL;
1357
1358 *p_pvid = br_get_pvid(vg);
1359 return 0;
1360 }
1361 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1362
br_vlan_get_pvid_rcu(const struct net_device * dev,u16 * p_pvid)1363 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1364 {
1365 struct net_bridge_vlan_group *vg;
1366 struct net_bridge_port *p;
1367
1368 p = br_port_get_check_rcu(dev);
1369 if (p)
1370 vg = nbp_vlan_group_rcu(p);
1371 else if (netif_is_bridge_master(dev))
1372 vg = br_vlan_group_rcu(netdev_priv(dev));
1373 else
1374 return -EINVAL;
1375
1376 *p_pvid = br_get_pvid(vg);
1377 return 0;
1378 }
1379 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1380
br_vlan_fill_forward_path_pvid(struct net_bridge * br,struct net_device_path_ctx * ctx,struct net_device_path * path)1381 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1382 struct net_device_path_ctx *ctx,
1383 struct net_device_path *path)
1384 {
1385 struct net_bridge_vlan_group *vg;
1386 int idx = ctx->num_vlans - 1;
1387 u16 vid;
1388
1389 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1390
1391 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1392 return;
1393
1394 vg = br_vlan_group(br);
1395
1396 if (idx >= 0 &&
1397 ctx->vlan[idx].proto == br->vlan_proto) {
1398 vid = ctx->vlan[idx].id;
1399 } else {
1400 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG;
1401 vid = br_get_pvid(vg);
1402 }
1403
1404 path->bridge.vlan_id = vid;
1405 path->bridge.vlan_proto = br->vlan_proto;
1406 }
1407
br_vlan_fill_forward_path_mode(struct net_bridge * br,struct net_bridge_port * dst,struct net_device_path * path)1408 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1409 struct net_bridge_port *dst,
1410 struct net_device_path *path)
1411 {
1412 struct net_bridge_vlan_group *vg;
1413 struct net_bridge_vlan *v;
1414
1415 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1416 return 0;
1417
1418 vg = nbp_vlan_group_rcu(dst);
1419 v = br_vlan_find(vg, path->bridge.vlan_id);
1420 if (!v || !br_vlan_should_use(v))
1421 return -EINVAL;
1422
1423 if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1424 return 0;
1425
1426 if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG)
1427 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1428 else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
1429 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW;
1430 else
1431 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG;
1432
1433 return 0;
1434 }
1435
br_vlan_get_info(const struct net_device * dev,u16 vid,struct bridge_vlan_info * p_vinfo)1436 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1437 struct bridge_vlan_info *p_vinfo)
1438 {
1439 struct net_bridge_vlan_group *vg;
1440 struct net_bridge_vlan *v;
1441 struct net_bridge_port *p;
1442
1443 ASSERT_RTNL();
1444 p = br_port_get_check_rtnl(dev);
1445 if (p)
1446 vg = nbp_vlan_group(p);
1447 else if (netif_is_bridge_master(dev))
1448 vg = br_vlan_group(netdev_priv(dev));
1449 else
1450 return -EINVAL;
1451
1452 v = br_vlan_find(vg, vid);
1453 if (!v)
1454 return -ENOENT;
1455
1456 p_vinfo->vid = vid;
1457 p_vinfo->flags = v->flags;
1458 if (vid == br_get_pvid(vg))
1459 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1460 return 0;
1461 }
1462 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1463
br_vlan_get_info_rcu(const struct net_device * dev,u16 vid,struct bridge_vlan_info * p_vinfo)1464 int br_vlan_get_info_rcu(const struct net_device *dev, u16 vid,
1465 struct bridge_vlan_info *p_vinfo)
1466 {
1467 struct net_bridge_vlan_group *vg;
1468 struct net_bridge_vlan *v;
1469 struct net_bridge_port *p;
1470
1471 p = br_port_get_check_rcu(dev);
1472 if (p)
1473 vg = nbp_vlan_group_rcu(p);
1474 else if (netif_is_bridge_master(dev))
1475 vg = br_vlan_group_rcu(netdev_priv(dev));
1476 else
1477 return -EINVAL;
1478
1479 v = br_vlan_find(vg, vid);
1480 if (!v)
1481 return -ENOENT;
1482
1483 p_vinfo->vid = vid;
1484 p_vinfo->flags = v->flags;
1485 if (vid == br_get_pvid(vg))
1486 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1487 return 0;
1488 }
1489 EXPORT_SYMBOL_GPL(br_vlan_get_info_rcu);
1490
br_vlan_is_bind_vlan_dev(const struct net_device * dev)1491 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1492 {
1493 return is_vlan_dev(dev) &&
1494 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1495 }
1496
br_vlan_is_bind_vlan_dev_fn(struct net_device * dev,__always_unused struct netdev_nested_priv * priv)1497 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1498 __always_unused struct netdev_nested_priv *priv)
1499 {
1500 return br_vlan_is_bind_vlan_dev(dev);
1501 }
1502
br_vlan_has_upper_bind_vlan_dev(struct net_device * dev)1503 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1504 {
1505 int found;
1506
1507 rcu_read_lock();
1508 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1509 NULL);
1510 rcu_read_unlock();
1511
1512 return !!found;
1513 }
1514
1515 struct br_vlan_bind_walk_data {
1516 u16 vid;
1517 struct net_device *result;
1518 };
1519
br_vlan_match_bind_vlan_dev_fn(struct net_device * dev,struct netdev_nested_priv * priv)1520 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1521 struct netdev_nested_priv *priv)
1522 {
1523 struct br_vlan_bind_walk_data *data = priv->data;
1524 int found = 0;
1525
1526 if (br_vlan_is_bind_vlan_dev(dev) &&
1527 vlan_dev_priv(dev)->vlan_id == data->vid) {
1528 data->result = dev;
1529 found = 1;
1530 }
1531
1532 return found;
1533 }
1534
1535 static struct net_device *
br_vlan_get_upper_bind_vlan_dev(struct net_device * dev,u16 vid)1536 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1537 {
1538 struct br_vlan_bind_walk_data data = {
1539 .vid = vid,
1540 };
1541 struct netdev_nested_priv priv = {
1542 .data = (void *)&data,
1543 };
1544
1545 rcu_read_lock();
1546 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1547 &priv);
1548 rcu_read_unlock();
1549
1550 return data.result;
1551 }
1552
br_vlan_is_dev_up(const struct net_device * dev)1553 static bool br_vlan_is_dev_up(const struct net_device *dev)
1554 {
1555 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1556 }
1557
br_vlan_set_vlan_dev_state(const struct net_bridge * br,struct net_device * vlan_dev)1558 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1559 struct net_device *vlan_dev)
1560 {
1561 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1562 struct net_bridge_vlan_group *vg;
1563 struct net_bridge_port *p;
1564 bool has_carrier = false;
1565
1566 if (!netif_carrier_ok(br->dev)) {
1567 netif_carrier_off(vlan_dev);
1568 return;
1569 }
1570
1571 list_for_each_entry(p, &br->port_list, list) {
1572 vg = nbp_vlan_group(p);
1573 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1574 has_carrier = true;
1575 break;
1576 }
1577 }
1578
1579 if (has_carrier)
1580 netif_carrier_on(vlan_dev);
1581 else
1582 netif_carrier_off(vlan_dev);
1583 }
1584
br_vlan_set_all_vlan_dev_state(struct net_bridge_port * p)1585 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1586 {
1587 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1588 struct net_bridge_vlan *vlan;
1589 struct net_device *vlan_dev;
1590
1591 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1592 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1593 vlan->vid);
1594 if (vlan_dev) {
1595 if (br_vlan_is_dev_up(p->dev)) {
1596 if (netif_carrier_ok(p->br->dev))
1597 netif_carrier_on(vlan_dev);
1598 } else {
1599 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1600 }
1601 }
1602 }
1603 }
1604
br_vlan_upper_change(struct net_device * dev,struct net_device * upper_dev,bool linking)1605 static void br_vlan_upper_change(struct net_device *dev,
1606 struct net_device *upper_dev,
1607 bool linking)
1608 {
1609 struct net_bridge *br = netdev_priv(dev);
1610
1611 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1612 return;
1613
1614 if (linking) {
1615 br_vlan_set_vlan_dev_state(br, upper_dev);
1616 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1617 } else {
1618 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1619 br_vlan_has_upper_bind_vlan_dev(dev));
1620 }
1621 }
1622
1623 struct br_vlan_link_state_walk_data {
1624 struct net_bridge *br;
1625 };
1626
br_vlan_link_state_change_fn(struct net_device * vlan_dev,struct netdev_nested_priv * priv)1627 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1628 struct netdev_nested_priv *priv)
1629 {
1630 struct br_vlan_link_state_walk_data *data = priv->data;
1631
1632 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1633 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1634
1635 return 0;
1636 }
1637
br_vlan_link_state_change(struct net_device * dev,struct net_bridge * br)1638 static void br_vlan_link_state_change(struct net_device *dev,
1639 struct net_bridge *br)
1640 {
1641 struct br_vlan_link_state_walk_data data = {
1642 .br = br
1643 };
1644 struct netdev_nested_priv priv = {
1645 .data = (void *)&data,
1646 };
1647
1648 rcu_read_lock();
1649 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1650 &priv);
1651 rcu_read_unlock();
1652 }
1653
1654 /* Must be protected by RTNL. */
nbp_vlan_set_vlan_dev_state(struct net_bridge_port * p,u16 vid)1655 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1656 {
1657 struct net_device *vlan_dev;
1658
1659 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1660 return;
1661
1662 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1663 if (vlan_dev)
1664 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1665 }
1666
1667 /* Must be protected by RTNL. */
br_vlan_bridge_event(struct net_device * dev,unsigned long event,void * ptr)1668 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1669 {
1670 struct netdev_notifier_changeupper_info *info;
1671 struct net_bridge *br = netdev_priv(dev);
1672 int vlcmd = 0, ret = 0;
1673 bool changed = false;
1674
1675 switch (event) {
1676 case NETDEV_REGISTER:
1677 ret = br_vlan_add(br, br->default_pvid,
1678 BRIDGE_VLAN_INFO_PVID |
1679 BRIDGE_VLAN_INFO_UNTAGGED |
1680 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1681 vlcmd = RTM_NEWVLAN;
1682 break;
1683 case NETDEV_UNREGISTER:
1684 changed = !br_vlan_delete(br, br->default_pvid);
1685 vlcmd = RTM_DELVLAN;
1686 break;
1687 case NETDEV_CHANGEUPPER:
1688 info = ptr;
1689 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1690 break;
1691
1692 case NETDEV_CHANGE:
1693 case NETDEV_UP:
1694 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1695 break;
1696 br_vlan_link_state_change(dev, br);
1697 break;
1698 }
1699 if (changed)
1700 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
1701
1702 return ret;
1703 }
1704
1705 /* Must be protected by RTNL. */
br_vlan_port_event(struct net_bridge_port * p,unsigned long event)1706 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1707 {
1708 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1709 return;
1710
1711 switch (event) {
1712 case NETDEV_CHANGE:
1713 case NETDEV_DOWN:
1714 case NETDEV_UP:
1715 br_vlan_set_all_vlan_dev_state(p);
1716 break;
1717 }
1718 }
1719
br_vlan_stats_fill(struct sk_buff * skb,const struct net_bridge_vlan * v)1720 static bool br_vlan_stats_fill(struct sk_buff *skb,
1721 const struct net_bridge_vlan *v)
1722 {
1723 struct pcpu_sw_netstats stats;
1724 struct nlattr *nest;
1725
1726 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1727 if (!nest)
1728 return false;
1729
1730 br_vlan_get_stats(v, &stats);
1731 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1732 BRIDGE_VLANDB_STATS_PAD) ||
1733 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1734 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1735 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1736 BRIDGE_VLANDB_STATS_PAD) ||
1737 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1738 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1739 goto out_err;
1740
1741 nla_nest_end(skb, nest);
1742
1743 return true;
1744
1745 out_err:
1746 nla_nest_cancel(skb, nest);
1747 return false;
1748 }
1749
1750 /* v_opts is used to dump the options which must be equal in the whole range */
br_vlan_fill_vids(struct sk_buff * skb,u16 vid,u16 vid_range,const struct net_bridge_vlan * v_opts,u16 flags,bool dump_stats)1751 static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
1752 const struct net_bridge_vlan *v_opts,
1753 u16 flags,
1754 bool dump_stats)
1755 {
1756 struct bridge_vlan_info info;
1757 struct nlattr *nest;
1758
1759 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1760 if (!nest)
1761 return false;
1762
1763 memset(&info, 0, sizeof(info));
1764 info.vid = vid;
1765 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1766 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1767 if (flags & BRIDGE_VLAN_INFO_PVID)
1768 info.flags |= BRIDGE_VLAN_INFO_PVID;
1769
1770 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1771 goto out_err;
1772
1773 if (vid_range && vid < vid_range &&
1774 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1775 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1776 goto out_err;
1777
1778 if (v_opts) {
1779 if (!br_vlan_opts_fill(skb, v_opts))
1780 goto out_err;
1781
1782 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1783 goto out_err;
1784 }
1785
1786 nla_nest_end(skb, nest);
1787
1788 return true;
1789
1790 out_err:
1791 nla_nest_cancel(skb, nest);
1792 return false;
1793 }
1794
rtnl_vlan_nlmsg_size(void)1795 static size_t rtnl_vlan_nlmsg_size(void)
1796 {
1797 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1798 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1799 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1800 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1801 + br_vlan_opts_nl_size(); /* bridge vlan options */
1802 }
1803
br_vlan_notify(const struct net_bridge * br,const struct net_bridge_port * p,u16 vid,u16 vid_range,int cmd)1804 void br_vlan_notify(const struct net_bridge *br,
1805 const struct net_bridge_port *p,
1806 u16 vid, u16 vid_range,
1807 int cmd)
1808 {
1809 struct net_bridge_vlan_group *vg;
1810 struct net_bridge_vlan *v = NULL;
1811 struct br_vlan_msg *bvm;
1812 struct nlmsghdr *nlh;
1813 struct sk_buff *skb;
1814 int err = -ENOBUFS;
1815 struct net *net;
1816 u16 flags = 0;
1817 int ifindex;
1818
1819 /* right now notifications are done only with rtnl held */
1820 ASSERT_RTNL();
1821
1822 if (p) {
1823 ifindex = p->dev->ifindex;
1824 vg = nbp_vlan_group(p);
1825 net = dev_net(p->dev);
1826 } else {
1827 ifindex = br->dev->ifindex;
1828 vg = br_vlan_group(br);
1829 net = dev_net(br->dev);
1830 }
1831
1832 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1833 if (!skb)
1834 goto out_err;
1835
1836 err = -EMSGSIZE;
1837 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1838 if (!nlh)
1839 goto out_err;
1840 bvm = nlmsg_data(nlh);
1841 memset(bvm, 0, sizeof(*bvm));
1842 bvm->family = AF_BRIDGE;
1843 bvm->ifindex = ifindex;
1844
1845 switch (cmd) {
1846 case RTM_NEWVLAN:
1847 /* need to find the vlan due to flags/options */
1848 v = br_vlan_find(vg, vid);
1849 if (!v || !br_vlan_should_use(v))
1850 goto out_kfree;
1851
1852 flags = v->flags;
1853 if (br_get_pvid(vg) == v->vid)
1854 flags |= BRIDGE_VLAN_INFO_PVID;
1855 break;
1856 case RTM_DELVLAN:
1857 break;
1858 default:
1859 goto out_kfree;
1860 }
1861
1862 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
1863 goto out_err;
1864
1865 nlmsg_end(skb, nlh);
1866 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1867 return;
1868
1869 out_err:
1870 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1871 out_kfree:
1872 kfree_skb(skb);
1873 }
1874
br_vlan_replay_one(struct notifier_block * nb,struct net_device * dev,struct switchdev_obj_port_vlan * vlan,const void * ctx,unsigned long action,struct netlink_ext_ack * extack)1875 static int br_vlan_replay_one(struct notifier_block *nb,
1876 struct net_device *dev,
1877 struct switchdev_obj_port_vlan *vlan,
1878 const void *ctx, unsigned long action,
1879 struct netlink_ext_ack *extack)
1880 {
1881 struct switchdev_notifier_port_obj_info obj_info = {
1882 .info = {
1883 .dev = dev,
1884 .extack = extack,
1885 .ctx = ctx,
1886 },
1887 .obj = &vlan->obj,
1888 };
1889 int err;
1890
1891 err = nb->notifier_call(nb, action, &obj_info);
1892 return notifier_to_errno(err);
1893 }
1894
br_vlan_replay(struct net_device * br_dev,struct net_device * dev,const void * ctx,bool adding,struct notifier_block * nb,struct netlink_ext_ack * extack)1895 int br_vlan_replay(struct net_device *br_dev, struct net_device *dev,
1896 const void *ctx, bool adding, struct notifier_block *nb,
1897 struct netlink_ext_ack *extack)
1898 {
1899 struct net_bridge_vlan_group *vg;
1900 struct net_bridge_vlan *v;
1901 struct net_bridge_port *p;
1902 struct net_bridge *br;
1903 unsigned long action;
1904 int err = 0;
1905 u16 pvid;
1906
1907 ASSERT_RTNL();
1908
1909 if (!nb)
1910 return 0;
1911
1912 if (!netif_is_bridge_master(br_dev))
1913 return -EINVAL;
1914
1915 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1916 return -EINVAL;
1917
1918 if (netif_is_bridge_master(dev)) {
1919 br = netdev_priv(dev);
1920 vg = br_vlan_group(br);
1921 p = NULL;
1922 } else {
1923 p = br_port_get_rtnl(dev);
1924 if (WARN_ON(!p))
1925 return -EINVAL;
1926 vg = nbp_vlan_group(p);
1927 br = p->br;
1928 }
1929
1930 if (!vg)
1931 return 0;
1932
1933 if (adding)
1934 action = SWITCHDEV_PORT_OBJ_ADD;
1935 else
1936 action = SWITCHDEV_PORT_OBJ_DEL;
1937
1938 pvid = br_get_pvid(vg);
1939
1940 list_for_each_entry(v, &vg->vlan_list, vlist) {
1941 struct switchdev_obj_port_vlan vlan = {
1942 .obj.orig_dev = dev,
1943 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1944 .flags = br_vlan_flags(v, pvid),
1945 .vid = v->vid,
1946 };
1947
1948 if (!br_vlan_should_use(v))
1949 continue;
1950
1951 err = br_vlan_replay_one(nb, dev, &vlan, ctx, action, extack);
1952 if (err)
1953 return err;
1954 }
1955
1956 return err;
1957 }
1958
1959 /* check if v_curr can enter a range ending in range_end */
br_vlan_can_enter_range(const struct net_bridge_vlan * v_curr,const struct net_bridge_vlan * range_end)1960 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1961 const struct net_bridge_vlan *range_end)
1962 {
1963 return v_curr->vid - range_end->vid == 1 &&
1964 range_end->flags == v_curr->flags &&
1965 br_vlan_opts_eq_range(v_curr, range_end);
1966 }
1967
br_vlan_dump_dev(const struct net_device * dev,struct sk_buff * skb,struct netlink_callback * cb,u32 dump_flags)1968 static int br_vlan_dump_dev(const struct net_device *dev,
1969 struct sk_buff *skb,
1970 struct netlink_callback *cb,
1971 u32 dump_flags)
1972 {
1973 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1974 bool dump_global = !!(dump_flags & BRIDGE_VLANDB_DUMPF_GLOBAL);
1975 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
1976 struct net_bridge_vlan_group *vg;
1977 int idx = 0, s_idx = cb->args[1];
1978 struct nlmsghdr *nlh = NULL;
1979 struct net_bridge_port *p;
1980 struct br_vlan_msg *bvm;
1981 struct net_bridge *br;
1982 int err = 0;
1983 u16 pvid;
1984
1985 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1986 return -EINVAL;
1987
1988 if (netif_is_bridge_master(dev)) {
1989 br = netdev_priv(dev);
1990 vg = br_vlan_group_rcu(br);
1991 p = NULL;
1992 } else {
1993 /* global options are dumped only for bridge devices */
1994 if (dump_global)
1995 return 0;
1996
1997 p = br_port_get_rcu(dev);
1998 if (WARN_ON(!p))
1999 return -EINVAL;
2000 vg = nbp_vlan_group_rcu(p);
2001 br = p->br;
2002 }
2003
2004 if (!vg)
2005 return 0;
2006
2007 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2008 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
2009 if (!nlh)
2010 return -EMSGSIZE;
2011 bvm = nlmsg_data(nlh);
2012 memset(bvm, 0, sizeof(*bvm));
2013 bvm->family = PF_BRIDGE;
2014 bvm->ifindex = dev->ifindex;
2015 pvid = br_get_pvid(vg);
2016
2017 /* idx must stay at range's beginning until it is filled in */
2018 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
2019 if (!dump_global && !br_vlan_should_use(v))
2020 continue;
2021 if (idx < s_idx) {
2022 idx++;
2023 continue;
2024 }
2025
2026 if (!range_start) {
2027 range_start = v;
2028 range_end = v;
2029 continue;
2030 }
2031
2032 if (dump_global) {
2033 if (br_vlan_global_opts_can_enter_range(v, range_end))
2034 goto update_end;
2035 if (!br_vlan_global_opts_fill(skb, range_start->vid,
2036 range_end->vid,
2037 range_start)) {
2038 err = -EMSGSIZE;
2039 break;
2040 }
2041 /* advance number of filled vlans */
2042 idx += range_end->vid - range_start->vid + 1;
2043
2044 range_start = v;
2045 } else if (dump_stats || v->vid == pvid ||
2046 !br_vlan_can_enter_range(v, range_end)) {
2047 u16 vlan_flags = br_vlan_flags(range_start, pvid);
2048
2049 if (!br_vlan_fill_vids(skb, range_start->vid,
2050 range_end->vid, range_start,
2051 vlan_flags, dump_stats)) {
2052 err = -EMSGSIZE;
2053 break;
2054 }
2055 /* advance number of filled vlans */
2056 idx += range_end->vid - range_start->vid + 1;
2057
2058 range_start = v;
2059 }
2060 update_end:
2061 range_end = v;
2062 }
2063
2064 /* err will be 0 and range_start will be set in 3 cases here:
2065 * - first vlan (range_start == range_end)
2066 * - last vlan (range_start == range_end, not in range)
2067 * - last vlan range (range_start != range_end, in range)
2068 */
2069 if (!err && range_start) {
2070 if (dump_global &&
2071 !br_vlan_global_opts_fill(skb, range_start->vid,
2072 range_end->vid, range_start))
2073 err = -EMSGSIZE;
2074 else if (!dump_global &&
2075 !br_vlan_fill_vids(skb, range_start->vid,
2076 range_end->vid, range_start,
2077 br_vlan_flags(range_start, pvid),
2078 dump_stats))
2079 err = -EMSGSIZE;
2080 }
2081
2082 cb->args[1] = err ? idx : 0;
2083
2084 nlmsg_end(skb, nlh);
2085
2086 return err;
2087 }
2088
2089 static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
2090 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
2091 };
2092
br_vlan_rtm_dump(struct sk_buff * skb,struct netlink_callback * cb)2093 static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
2094 {
2095 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
2096 int idx = 0, err = 0, s_idx = cb->args[0];
2097 struct net *net = sock_net(skb->sk);
2098 struct br_vlan_msg *bvm;
2099 struct net_device *dev;
2100 u32 dump_flags = 0;
2101
2102 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
2103 br_vlan_db_dump_pol, cb->extack);
2104 if (err < 0)
2105 return err;
2106
2107 bvm = nlmsg_data(cb->nlh);
2108 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
2109 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
2110
2111 rcu_read_lock();
2112 if (bvm->ifindex) {
2113 dev = dev_get_by_index_rcu(net, bvm->ifindex);
2114 if (!dev) {
2115 err = -ENODEV;
2116 goto out_err;
2117 }
2118 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2119 /* if the dump completed without an error we return 0 here */
2120 if (err != -EMSGSIZE)
2121 goto out_err;
2122 } else {
2123 for_each_netdev_rcu(net, dev) {
2124 if (idx < s_idx)
2125 goto skip;
2126
2127 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2128 if (err == -EMSGSIZE)
2129 break;
2130 skip:
2131 idx++;
2132 }
2133 }
2134 cb->args[0] = idx;
2135 rcu_read_unlock();
2136
2137 return skb->len;
2138
2139 out_err:
2140 rcu_read_unlock();
2141
2142 return err;
2143 }
2144
2145 static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
2146 [BRIDGE_VLANDB_ENTRY_INFO] =
2147 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
2148 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
2149 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
2150 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
2151 [BRIDGE_VLANDB_ENTRY_MCAST_ROUTER] = { .type = NLA_U8 },
2152 };
2153
br_vlan_rtm_process_one(struct net_device * dev,const struct nlattr * attr,int cmd,struct netlink_ext_ack * extack)2154 static int br_vlan_rtm_process_one(struct net_device *dev,
2155 const struct nlattr *attr,
2156 int cmd, struct netlink_ext_ack *extack)
2157 {
2158 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
2159 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
2160 bool changed = false, skip_processing = false;
2161 struct net_bridge_vlan_group *vg;
2162 struct net_bridge_port *p = NULL;
2163 int err = 0, cmdmap = 0;
2164 struct net_bridge *br;
2165
2166 if (netif_is_bridge_master(dev)) {
2167 br = netdev_priv(dev);
2168 vg = br_vlan_group(br);
2169 } else {
2170 p = br_port_get_rtnl(dev);
2171 if (WARN_ON(!p))
2172 return -ENODEV;
2173 br = p->br;
2174 vg = nbp_vlan_group(p);
2175 }
2176
2177 if (WARN_ON(!vg))
2178 return -ENODEV;
2179
2180 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
2181 br_vlan_db_policy, extack);
2182 if (err)
2183 return err;
2184
2185 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
2186 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
2187 return -EINVAL;
2188 }
2189 memset(&vrange_end, 0, sizeof(vrange_end));
2190
2191 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
2192 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
2193 BRIDGE_VLAN_INFO_RANGE_END)) {
2194 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
2195 return -EINVAL;
2196 }
2197 if (!br_vlan_valid_id(vinfo->vid, extack))
2198 return -EINVAL;
2199
2200 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
2201 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
2202 /* validate user-provided flags without RANGE_BEGIN */
2203 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
2204 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
2205
2206 /* vinfo_last is the range start, vinfo the range end */
2207 vinfo_last = vinfo;
2208 vinfo = &vrange_end;
2209
2210 if (!br_vlan_valid_id(vinfo->vid, extack) ||
2211 !br_vlan_valid_range(vinfo, vinfo_last, extack))
2212 return -EINVAL;
2213 }
2214
2215 switch (cmd) {
2216 case RTM_NEWVLAN:
2217 cmdmap = RTM_SETLINK;
2218 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
2219 break;
2220 case RTM_DELVLAN:
2221 cmdmap = RTM_DELLINK;
2222 break;
2223 }
2224
2225 if (!skip_processing) {
2226 struct bridge_vlan_info *tmp_last = vinfo_last;
2227
2228 /* br_process_vlan_info may overwrite vinfo_last */
2229 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
2230 &changed, extack);
2231
2232 /* notify first if anything changed */
2233 if (changed)
2234 br_ifinfo_notify(cmdmap, br, p);
2235
2236 if (err)
2237 return err;
2238 }
2239
2240 /* deal with options */
2241 if (cmd == RTM_NEWVLAN) {
2242 struct net_bridge_vlan *range_start, *range_end;
2243
2244 if (vinfo_last) {
2245 range_start = br_vlan_find(vg, vinfo_last->vid);
2246 range_end = br_vlan_find(vg, vinfo->vid);
2247 } else {
2248 range_start = br_vlan_find(vg, vinfo->vid);
2249 range_end = range_start;
2250 }
2251
2252 err = br_vlan_process_options(br, p, range_start, range_end,
2253 tb, extack);
2254 }
2255
2256 return err;
2257 }
2258
br_vlan_rtm_process(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)2259 static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2260 struct netlink_ext_ack *extack)
2261 {
2262 struct net *net = sock_net(skb->sk);
2263 struct br_vlan_msg *bvm;
2264 struct net_device *dev;
2265 struct nlattr *attr;
2266 int err, vlans = 0;
2267 int rem;
2268
2269 /* this should validate the header and check for remaining bytes */
2270 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2271 extack);
2272 if (err < 0)
2273 return err;
2274
2275 bvm = nlmsg_data(nlh);
2276 dev = __dev_get_by_index(net, bvm->ifindex);
2277 if (!dev)
2278 return -ENODEV;
2279
2280 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2281 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2282 return -EINVAL;
2283 }
2284
2285 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2286 switch (nla_type(attr)) {
2287 case BRIDGE_VLANDB_ENTRY:
2288 err = br_vlan_rtm_process_one(dev, attr,
2289 nlh->nlmsg_type,
2290 extack);
2291 break;
2292 case BRIDGE_VLANDB_GLOBAL_OPTIONS:
2293 err = br_vlan_rtm_process_global_options(dev, attr,
2294 nlh->nlmsg_type,
2295 extack);
2296 break;
2297 default:
2298 continue;
2299 }
2300
2301 vlans++;
2302 if (err)
2303 break;
2304 }
2305 if (!vlans) {
2306 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2307 err = -EINVAL;
2308 }
2309
2310 return err;
2311 }
2312
br_vlan_rtnl_init(void)2313 void br_vlan_rtnl_init(void)
2314 {
2315 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2316 br_vlan_rtm_dump, 0);
2317 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2318 br_vlan_rtm_process, NULL, 0);
2319 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2320 br_vlan_rtm_process, NULL, 0);
2321 }
2322
br_vlan_rtnl_uninit(void)2323 void br_vlan_rtnl_uninit(void)
2324 {
2325 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
2326 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
2327 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
2328 }
2329