blob: 84ba456a78cce0484573ca6ab2c56ea611e5a731 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Vlad Yasevich243a2e62013-02-13 12:00:09 +00002#include <linux/kernel.h>
3#include <linux/netdevice.h>
4#include <linux/rtnetlink.h>
5#include <linux/slab.h>
Scott Feldman7f109532015-06-12 17:39:50 -07006#include <net/switchdev.h>
Vlad Yasevich243a2e62013-02-13 12:00:09 +00007
8#include "br_private.h"
Roopa Prabhuefa53562017-01-31 22:59:54 -08009#include "br_private_tunnel.h"
Vlad Yasevich243a2e62013-02-13 12:00:09 +000010
Mike Manning80900ac2019-04-18 18:35:34 +010011static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);
12
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020013static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
14 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000015{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020016 const struct net_bridge_vlan *vle = ptr;
17 u16 vid = *(u16 *)arg->key;
18
19 return vle->vid != vid;
20}
21
22static 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),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020026 .nelem_hint = 3,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020027 .max_size = VLAN_N_VID,
28 .obj_cmpfn = br_vlan_cmp,
29 .automatic_shrinking = true,
30};
31
32static 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
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020037static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg,
38 const struct net_bridge_vlan *v)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020039{
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020040 if (vg->pvid == v->vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030041 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000042
43 smp_wmb();
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020044 br_vlan_set_pvid_state(vg, v->state);
45 vg->pvid = v->vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030046
47 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000048}
49
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030050static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000051{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020052 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030053 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000054
55 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020056 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030057
58 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000059}
60
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030061/* return true if anything changed, false otherwise */
62static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000063{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020064 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030065 u16 old_flags = v->flags;
66 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020067
68 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020069 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020070 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020071 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020072
73 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020074 ret = __vlan_add_pvid(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020075 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030076 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000077
78 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020079 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040080 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020081 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030082
83 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000084}
85
Scott Feldman7f109532015-06-12 17:39:50 -070086static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +000087 struct net_bridge_vlan *v, u16 flags,
88 struct netlink_ext_ack *extack)
Scott Feldman7f109532015-06-12 17:39:50 -070089{
Scott Feldman7f109532015-06-12 17:39:50 -070090 int err;
91
Jiri Pirko0944d6b2015-10-09 13:54:11 +020092 /* Try switchdev op first. In case it is not supported, fallback to
93 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070094 */
Ido Schimmel27973792019-01-08 16:48:11 +000095 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020096 if (err == -EOPNOTSUPP)
Ido Schimmel27973792019-01-08 16:48:11 +000097 return vlan_vid_add(dev, br->vlan_proto, v->vid);
98 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
Scott Feldman7f109532015-06-12 17:39:50 -070099 return err;
100}
101
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200102static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000103{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200104 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200105 struct list_head *headp, *hpos;
106 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000107
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200108 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;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200114 list_for_each_prev(hpos, headp) {
115 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
Colin Ian King040c1252021-06-18 11:01:55 +0100116 if (v->vid >= vent->vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200117 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000118 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200119 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200120}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000121
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200122static void __vlan_del_list(struct net_bridge_vlan *v)
123{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200124 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000125}
126
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400127static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +0000128 const struct net_bridge_vlan *v)
Scott Feldman7f109532015-06-12 17:39:50 -0700129{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200130 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700131
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 /* Try switchdev op first. In case it is not supported, fallback to
133 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700134 */
Ido Schimmel27973792019-01-08 16:48:11 +0000135 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;
Scott Feldman7f109532015-06-12 17:39:50 -0700139}
140
Randy Dunlap4bbd0262020-09-17 21:35:21 -0700141/* Returns a master vlan, if it didn't exist it gets created. In all cases
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200142 * a reference is taken to the master vlan before returning.
143 */
Petr Machata169327d2018-12-12 17:02:50 +0000144static struct net_bridge_vlan *
145br_vlan_get_master(struct net_bridge *br, u16 vid,
146 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200147{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200148 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149 struct net_bridge_vlan *masterv;
150
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200151 vg = br_vlan_group(br);
152 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200153 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300154 bool changed;
155
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200156 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000157 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200159 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200160 if (WARN_ON(!masterv))
161 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200162 refcount_set(&masterv->refcnt, 1);
163 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200164 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300165 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200166
167 return masterv;
168}
169
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200170static 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
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200181static void br_vlan_put_master(struct net_bridge_vlan *masterv)
182{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200183 struct net_bridge_vlan_group *vg;
184
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200185 if (!br_vlan_is_master(masterv))
186 return;
187
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200188 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300189 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200190 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200191 &masterv->vnode, br_vlan_rht_params);
192 __vlan_del_list(masterv);
Nikolay Aleksandrov7b54aaa2021-07-19 20:06:27 +0300193 br_multicast_toggle_one_vlan(masterv, false);
Nikolay Aleksandrov613d61d2021-07-19 20:06:26 +0300194 br_multicast_ctx_deinit(&masterv->br_mcast_ctx);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200195 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200196 }
197}
198
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300199static 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 */
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200206 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300207 free_percpu(v->stats);
208 v->stats = NULL;
209 kfree(v);
210}
211
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200212/* 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)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100216 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100218 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200219 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100220 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200221 * will be used for filtering in both the port and the bridge
222 */
Petr Machata169327d2018-12-12 17:02:50 +0000223static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
224 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000225{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200226 struct net_bridge_vlan *masterv = NULL;
227 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200228 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200229 struct net_device *dev;
230 struct net_bridge *br;
231 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000232
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200233 if (br_vlan_is_master(v)) {
234 br = v->br;
235 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200236 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200237 } else {
238 p = v->port;
239 br = p->br;
240 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200241 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000243
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200244 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200245 /* 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 */
Ido Schimmel27973792019-01-08 16:48:11 +0000249 err = __vlan_vid_add(dev, br, v, flags, extack);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400250 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200251 goto out;
252
253 /* need to work on the master vlan too */
254 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300255 bool changed;
256
257 err = br_vlan_add(br, v->vid,
258 flags | BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000259 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200260 if (err)
261 goto out_filt;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200262
263 if (changed)
264 br_vlan_notify(br, NULL, v->vid, 0,
265 RTM_NEWVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200266 }
267
Petr Machata169327d2018-12-12 17:02:50 +0000268 masterv = br_vlan_get_master(br, v->vid, extack);
Zhang Changzhongee4f52a82020-12-04 16:48:56 +0800269 if (!masterv) {
270 err = -ENOMEM;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200271 goto out_filt;
Zhang Changzhongee4f52a82020-12-04 16:48:56 +0800272 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200273 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300274 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
Heiner Kallweit281cc282020-11-17 21:25:42 +0100275 v->stats =
276 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300277 if (!v->stats) {
278 err = -ENOMEM;
279 goto out_filt;
280 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200281 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300282 } else {
283 v->stats = masterv->stats;
284 }
Nikolay Aleksandrov613d61d2021-07-19 20:06:26 +0300285 br_multicast_port_ctx_init(p, v, &v->port_mcast_ctx);
Petr Machata9c86ce22018-05-30 03:00:45 +0200286 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000287 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200288 if (err && err != -EOPNOTSUPP)
289 goto out;
Nikolay Aleksandrov613d61d2021-07-19 20:06:26 +0300290 br_multicast_ctx_init(br, v, &v->br_mcast_ctx);
Nikolay Aleksandrov7b54aaa2021-07-19 20:06:27 +0300291 v->priv_flags |= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900292 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000293
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200294 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200295 if (br_vlan_should_use(v)) {
Vladimir Olteanf6814fd2021-10-26 17:27:39 +0300296 err = br_fdb_add_local(br, p, dev->dev_addr, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200297 if (err) {
298 br_err(br, "failed insert local address into bridge forwarding table\n");
299 goto out_filt;
300 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200301 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000302 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200303
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200304 /* set the state before publishing */
305 v->state = BR_STATE_FORWARDING;
306
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200307 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
308 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200309 if (err)
310 goto out_fdb_insert;
311
312 __vlan_add_list(v);
313 __vlan_add_flags(v, flags);
Nikolay Aleksandrov7b54aaa2021-07-19 20:06:27 +0300314 br_multicast_toggle_one_vlan(v, true);
Mike Manning80900ac2019-04-18 18:35:34 +0100315
316 if (p)
317 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200318out:
319 return err;
320
321out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200322 if (br_vlan_should_use(v)) {
323 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
324 vg->num_vlans--;
325 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326
327out_filt:
328 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000329 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200330 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800331 if (v->stats && masterv->stats != v->stats)
332 free_percpu(v->stats);
333 v->stats = NULL;
334
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200335 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 v->brvlan = NULL;
337 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200338 } else {
339 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200340 }
341
342 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000343}
344
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200345static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000346{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200347 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200348 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351
352 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200353 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 } else {
355 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200356 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200357 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200358 }
359
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200360 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200361 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000362 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200363 if (err)
364 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200365 } else {
366 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
367 if (err && err != -EOPNOTSUPP)
368 goto out;
369 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200370 }
371
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200372 if (br_vlan_should_use(v)) {
373 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
374 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200375 }
376
377 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800378 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200379 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
380 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200381 __vlan_del_list(v);
Mike Manning80900ac2019-04-18 18:35:34 +0100382 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov7b54aaa2021-07-19 20:06:27 +0300383 br_multicast_toggle_one_vlan(v, false);
Nikolay Aleksandrov613d61d2021-07-19 20:06:26 +0300384 br_multicast_port_ctx_deinit(&v->port_mcast_ctx);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300385 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200386 }
387
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200388 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200389out:
390 return err;
391}
392
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200393static 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);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800397 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200398 kfree(vg);
399}
400
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200401static void __vlan_flush(const struct net_bridge *br,
402 const struct net_bridge_port *p,
403 struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200404{
405 struct net_bridge_vlan *vlan, *tmp;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200406 u16 v_start = 0, v_end = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200407
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200408 __vlan_delete_pvid(vg, vg->pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200409 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
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200420 __vlan_del(vlan);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200421 }
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);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000426}
427
Vlad Yasevich78851982013-02-13 12:00:14 +0000428struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800429 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200430 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000431 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000432{
Heiner Kallweit281cc282020-11-17 21:25:42 +0100433 struct pcpu_sw_netstats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200434 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000435 u16 vid;
436
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400437 /* If this packet was not filtered at input, let it pass */
438 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000439 goto out;
440
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200441 /* 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
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400448 * 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 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200452 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400453 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
454 goto out;
455 } else {
456 kfree_skb(skb);
457 return NULL;
458 }
459 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300460 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200461 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
Tobias Waldekranz47211192021-07-22 18:55:38 +0300468 /* 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))
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100477 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800478
479 if (p && (p->flags & BR_VLAN_TUNNEL) &&
480 br_handle_egress_vlan_tunnel(skb, v)) {
481 kfree_skb(skb);
482 return NULL;
483 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000484out:
485 return skb;
486}
487
488/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200489static bool __allowed_ingress(const struct net_bridge *br,
490 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200491 struct sk_buff *skb, u16 *vid,
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300492 u8 *state,
493 struct net_bridge_vlan **vlan)
Vlad Yasevich78851982013-02-13 12:00:14 +0000494{
Heiner Kallweit281cc282020-11-17 21:25:42 +0100495 struct pcpu_sw_netstats *stats;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200496 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900497 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000498
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400499 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900500 /* 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 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100504 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200505 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400506 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900507 if (unlikely(!skb))
508 return false;
509 }
510
Toshiaki Makita8580e212014-06-10 20:59:23 +0900511 if (!br_vlan_get_tag(skb, vid)) {
512 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200513 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900514 /* Protocol-mismatch, empty out vlan_tci for new tag */
515 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100516 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100517 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900518 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
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900533 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200534 u16 pvid = br_get_pvid(vg);
535
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900536 /* 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.
Vlad Yasevich78851982013-02-13 12:00:14 +0000539 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400540 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900541 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000542
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900543 /* PVID is set on this port. Any untagged or priority-tagged
544 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000545 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900546 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900547 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900548 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200549 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900550 else
551 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100552 * At this point, we know that skb->vlan_tci VID
553 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900554 * We update only VID field and preserve PCP field.
555 */
556 skb->vlan_tci |= pvid;
557
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300558 /* 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)) {
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200561 if (*state == BR_STATE_FORWARDING) {
562 *state = br_vlan_get_pvid_state(vg);
563 return br_vlan_state_allowed(*state, true);
564 } else {
565 return true;
566 }
567 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200568 }
569 v = br_vlan_find(vg, *vid);
570 if (!v || !br_vlan_should_use(v))
571 goto drop;
572
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200573 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
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300579 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200580 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);
Vlad Yasevich78851982013-02-13 12:00:14 +0000585 }
586
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300587 *vlan = v;
588
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200589 return true;
590
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900591drop:
592 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000593 return false;
594}
595
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200596bool br_allowed_ingress(const struct net_bridge *br,
597 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300598 u16 *vid, u8 *state,
599 struct net_bridge_vlan **vlan)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200600{
601 /* If VLAN filtering is disabled on the bridge, all packets are
602 * permitted.
603 */
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300604 *vlan = NULL;
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300605 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200606 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
607 return true;
608 }
609
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300610 return __allowed_ingress(br, vg, skb, vid, state, vlan);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200611}
612
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000613/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200614bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000615 const struct sk_buff *skb)
616{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200617 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000618 u16 vid;
619
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400620 /* If this packet was not filtered at input, let it pass */
621 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000622 return true;
623
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000624 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200625 v = br_vlan_find(vg, vid);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200626 if (v && br_vlan_should_use(v) &&
627 br_vlan_state_allowed(br_vlan_get_state(v), false))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000628 return true;
629
630 return false;
631}
632
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900633/* Called under RCU */
634bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
635{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200636 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900637 struct net_bridge *br = p->br;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200638 struct net_bridge_vlan *v;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900639
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400640 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300641 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900642 return true;
643
Ido Schimmeleca1e002015-11-01 18:31:45 +0200644 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200645 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900646 return false;
647
Toshiaki Makita8580e212014-06-10 20:59:23 +0900648 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
649 *vid = 0;
650
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900651 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200652 *vid = br_get_pvid(vg);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200653 if (!*vid ||
654 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900655 return false;
656
657 return true;
658 }
659
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200660 v = br_vlan_find(vg, *vid);
661 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900662 return true;
663
664 return false;
665}
666
Petr Machatadbd6dc72018-05-30 02:56:55 +0200667static int br_vlan_add_existing(struct net_bridge *br,
668 struct net_bridge_vlan_group *vg,
669 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000670 u16 flags, bool *changed,
671 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200672{
673 int err;
674
Petr Machata169327d2018-12-12 17:02:50 +0000675 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200676 if (err && err != -EOPNOTSUPP)
677 return err;
678
Petr Machatadbd6dc72018-05-30 02:56:55 +0200679 if (!br_vlan_is_brentry(vlan)) {
680 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200681 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
682 err = -EINVAL;
683 goto err_flags;
684 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200685 /* It was only kept for port vlans, now make it real */
Vladimir Olteanf6814fd2021-10-26 17:27:39 +0300686 err = br_fdb_add_local(br, NULL, br->dev->dev_addr, vlan->vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200687 if (err) {
688 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200689 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200690 }
691
692 refcount_inc(&vlan->refcnt);
693 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
694 vg->num_vlans++;
695 *changed = true;
Nikolay Aleksandrovb92dace2021-08-16 17:57:04 +0300696 br_multicast_toggle_one_vlan(vlan, true);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200697 }
698
699 if (__vlan_add_flags(vlan, flags))
700 *changed = true;
701
702 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200703
704err_fdb_insert:
705err_flags:
706 br_switchdev_port_vlan_del(br->dev, vlan->vid);
707 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200708}
709
Toshiaki Makita8adff412013-10-16 17:07:13 +0900710/* Must be protected by RTNL.
711 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300712 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900713 */
Petr Machata169327d2018-12-12 17:02:50 +0000714int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
715 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000716{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200717 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200718 struct net_bridge_vlan *vlan;
719 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000720
721 ASSERT_RTNL();
722
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300723 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200724 vg = br_vlan_group(br);
725 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200726 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000727 return br_vlan_add_existing(br, vg, vlan, flags, changed,
728 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000729
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200730 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
731 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000732 return -ENOMEM;
733
Heiner Kallweit281cc282020-11-17 21:25:42 +0100734 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200735 if (!vlan->stats) {
736 kfree(vlan);
737 return -ENOMEM;
738 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200739 vlan->vid = vid;
740 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
741 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
742 vlan->br = br;
743 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300744 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000745 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200746 if (ret) {
747 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200748 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300749 } else {
750 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200751 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000752
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200753 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000754}
755
Toshiaki Makita8adff412013-10-16 17:07:13 +0900756/* Must be protected by RTNL.
757 * Must be called with vid in range from 1 to 4094 inclusive.
758 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000759int br_vlan_delete(struct net_bridge *br, u16 vid)
760{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200761 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200762 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000763
764 ASSERT_RTNL();
765
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200766 vg = br_vlan_group(br);
767 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200768 if (!v || !br_vlan_is_brentry(v))
769 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000770
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900771 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700772 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000773
Roopa Prabhuefa53562017-01-31 22:59:54 -0800774 vlan_tunnel_info_del(vg, v);
775
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200776 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000777}
778
779void br_vlan_flush(struct net_bridge *br)
780{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200781 struct net_bridge_vlan_group *vg;
782
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000783 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000784
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200785 vg = br_vlan_group(br);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200786 __vlan_flush(br, NULL, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200787 RCU_INIT_POINTER(br->vlgrp, NULL);
788 synchronize_rcu();
789 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000790}
791
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200792struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900793{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200794 if (!vg)
795 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900796
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200797 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900798}
799
Toshiaki Makita204177f2014-06-10 20:59:25 +0900800/* Must be protected by RTNL. */
801static void recalculate_group_addr(struct net_bridge *br)
802{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300803 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900804 return;
805
806 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300807 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
808 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900809 /* Bridge Group Address */
810 br->group_addr[5] = 0x00;
811 } else { /* vlan_enabled && ETH_P_8021AD */
812 /* Provider Bridge Group Address */
813 br->group_addr[5] = 0x08;
814 }
815 spin_unlock_bh(&br->lock);
816}
817
818/* Must be protected by RTNL. */
819void br_recalculate_fwd_mask(struct net_bridge *br)
820{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300821 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
822 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900823 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
824 else /* vlan_enabled && ETH_P_8021AD */
825 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
826 ~(1u << br->group_addr[5]);
827}
828
Vladimir Oltean9e781402021-02-13 22:43:16 +0200829int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
830 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000831{
Elad Raz6b72a772016-01-06 13:01:06 +0100832 struct switchdev_attr attr = {
833 .orig_dev = br->dev,
834 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
835 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
836 .u.vlan_filtering = val,
837 };
838 int err;
839
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300840 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300841 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000842
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300843 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vladimir Olteanf7cdb3ec2021-07-26 19:55:28 +0300844
845 err = switchdev_port_attr_set(br->dev, &attr, extack);
846 if (err && err != -EOPNOTSUPP) {
847 br_opt_toggle(br, BROPT_VLAN_ENABLED, !val);
848 return err;
849 }
850
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400851 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900852 recalculate_group_addr(br);
853 br_recalculate_fwd_mask(br);
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300854 if (!val && br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
855 br_info(br, "vlan filtering disabled, automatically disabling multicast vlan snooping\n");
856 br_multicast_toggle_vlan_snooping(br, false, NULL);
857 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000858
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300859 return 0;
860}
861
Ido Schimmel1f514452017-05-26 08:37:23 +0200862bool br_vlan_enabled(const struct net_device *dev)
863{
864 struct net_bridge *br = netdev_priv(dev);
865
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300866 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200867}
868EXPORT_SYMBOL_GPL(br_vlan_enabled);
869
wenxu31aed462019-07-05 21:16:36 +0800870int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
871{
872 struct net_bridge *br = netdev_priv(dev);
873
874 *p_proto = ntohs(br->vlan_proto);
875
876 return 0;
877}
878EXPORT_SYMBOL_GPL(br_vlan_get_proto);
879
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200880int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
881 struct netlink_ext_ack *extack)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900882{
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200883 struct switchdev_attr attr = {
884 .orig_dev = br->dev,
885 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
886 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
887 .u.vlan_protocol = ntohs(proto),
888 };
Toshiaki Makita204177f2014-06-10 20:59:25 +0900889 int err = 0;
890 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200891 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200892 struct net_bridge_vlan_group *vg;
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200893 __be16 oldproto = br->vlan_proto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900894
Toshiaki Makita204177f2014-06-10 20:59:25 +0900895 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900896 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900897
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200898 err = switchdev_port_attr_set(br->dev, &attr, extack);
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200899 if (err && err != -EOPNOTSUPP)
900 return err;
901
Toshiaki Makita204177f2014-06-10 20:59:25 +0900902 /* Add VLANs for the new proto to the device filter. */
903 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200904 vg = nbp_vlan_group(p);
905 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200906 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900907 if (err)
908 goto err_filt;
909 }
910 }
911
Toshiaki Makita204177f2014-06-10 20:59:25 +0900912 br->vlan_proto = proto;
913
914 recalculate_group_addr(br);
915 br_recalculate_fwd_mask(br);
916
917 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200918 list_for_each_entry(p, &br->port_list, list) {
919 vg = nbp_vlan_group(p);
920 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200921 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200922 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900923
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900924 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900925
926err_filt:
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200927 attr.u.vlan_protocol = ntohs(oldproto);
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200928 switchdev_port_attr_set(br->dev, &attr, NULL);
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200929
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200930 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200931 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900932
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200933 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
934 vg = nbp_vlan_group(p);
935 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200936 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200937 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900938
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900939 return err;
940}
941
Vladimir Oltean9e781402021-02-13 22:43:16 +0200942int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
943 struct netlink_ext_ack *extack)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900944{
Menglong Donga98c0c42021-01-17 16:09:50 +0800945 if (!eth_type_vlan(htons(val)))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900946 return -EPROTONOSUPPORT;
947
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200948 return __br_vlan_set_proto(br, htons(val), extack);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900949}
950
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200951int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
952{
953 switch (val) {
954 case 0:
955 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300956 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200957 break;
958 default:
959 return -EINVAL;
960 }
961
962 return 0;
963}
964
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300965int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
966{
967 struct net_bridge_port *p;
968
969 /* allow to change the option if there are no port vlans configured */
970 list_for_each_entry(p, &br->port_list, list) {
971 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
972
973 if (vg->num_vlans)
974 return -EBUSY;
975 }
976
977 switch (val) {
978 case 0:
979 case 1:
980 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
981 break;
982 default:
983 return -EINVAL;
984 }
985
986 return 0;
987}
988
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200989static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400990{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200991 struct net_bridge_vlan *v;
992
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200993 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200994 return false;
995
996 v = br_vlan_lookup(&vg->vlan_hash, vid);
997 if (v && br_vlan_should_use(v) &&
998 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
999 return true;
1000
1001 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001002}
1003
1004static void br_vlan_disable_default_pvid(struct net_bridge *br)
1005{
1006 struct net_bridge_port *p;
1007 u16 pvid = br->default_pvid;
1008
1009 /* Disable default_pvid on all ports where it is still
1010 * configured.
1011 */
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001012 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
1013 if (!br_vlan_delete(br, pvid))
1014 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1015 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001016
1017 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001018 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
1019 !nbp_vlan_delete(p, pvid))
1020 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001021 }
1022
1023 br->default_pvid = 0;
1024}
1025
Petr Machata169327d2018-12-12 17:02:50 +00001026int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1027 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001028{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001029 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001030 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001031 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001032 unsigned long *changed;
1033 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001034 u16 old_pvid;
1035 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001036
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001037 if (!pvid) {
1038 br_vlan_disable_default_pvid(br);
1039 return 0;
1040 }
1041
Andy Shevchenko459479d2018-08-30 13:33:18 +03001042 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001043 if (!changed)
1044 return -ENOMEM;
1045
1046 old_pvid = br->default_pvid;
1047
1048 /* Update default_pvid config only if we do not conflict with
1049 * user configuration.
1050 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001051 vg = br_vlan_group(br);
1052 pvent = br_vlan_find(vg, pvid);
1053 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001054 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001055 err = br_vlan_add(br, pvid,
1056 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001057 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001058 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001059 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001060 if (err)
1061 goto out;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001062
1063 if (br_vlan_delete(br, old_pvid))
1064 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1065 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
Xin Long442b03c2021-11-24 14:39:33 -05001066 __set_bit(0, changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001067 }
1068
1069 list_for_each_entry(p, &br->port_list, list) {
1070 /* Update default_pvid config only if we do not conflict with
1071 * user configuration.
1072 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001073 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001074 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001075 !vlan_default_pvid(vg, old_pvid)) ||
1076 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001077 continue;
1078
1079 err = nbp_vlan_add(p, pvid,
1080 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001081 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001082 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001083 if (err)
1084 goto err_port;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001085 if (nbp_vlan_delete(p, old_pvid))
1086 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1087 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
Xin Long442b03c2021-11-24 14:39:33 -05001088 __set_bit(p->port_no, changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001089 }
1090
1091 br->default_pvid = pvid;
1092
1093out:
Andy Shevchenko459479d2018-08-30 13:33:18 +03001094 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001095 return err;
1096
1097err_port:
1098 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1099 if (!test_bit(p->port_no, changed))
1100 continue;
1101
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001102 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001103 nbp_vlan_add(p, old_pvid,
1104 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001105 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001106 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001107 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1108 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001109 nbp_vlan_delete(p, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001110 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001111 }
1112
1113 if (test_bit(0, changed)) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001114 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001115 br_vlan_add(br, old_pvid,
1116 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001117 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001118 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001119 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001120 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1121 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001122 br_vlan_delete(br, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001123 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001124 }
1125 goto out;
1126}
1127
Vladimir Oltean9e781402021-02-13 22:43:16 +02001128int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1129 struct netlink_ext_ack *extack)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001130{
1131 u16 pvid = val;
1132 int err = 0;
1133
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001134 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001135 return -EINVAL;
1136
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001137 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001138 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001139
1140 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001141 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001142 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1143 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001144 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001145 }
Vladimir Oltean9e781402021-02-13 22:43:16 +02001146 err = __br_vlan_set_default_pvid(br, pvid, extack);
Xin Long047831a2016-04-09 00:03:31 +08001147out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001148 return err;
1149}
1150
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001151int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001152{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001153 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001154 int ret = -ENOMEM;
1155
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001156 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1157 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001158 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001159 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001160 if (ret)
1161 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001162 ret = vlan_tunnel_init(vg);
1163 if (ret)
1164 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001165 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001166 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001167 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001168 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001169
1170out:
1171 return ret;
1172
Roopa Prabhuefa53562017-01-31 22:59:54 -08001173err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001174 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001175err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001176 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001177
1178 goto out;
1179}
1180
Petr Machata169327d2018-12-12 17:02:50 +00001181int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001182{
Elad Raz404cdbf2016-01-06 13:01:07 +01001183 struct switchdev_attr attr = {
1184 .orig_dev = p->br->dev,
1185 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1186 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001187 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001188 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001189 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001190 int ret = -ENOMEM;
1191
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001192 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1193 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001194 goto out;
1195
Vladimir Olteandcbdf132021-02-13 22:43:17 +02001196 ret = switchdev_port_attr_set(p->dev, &attr, extack);
Elad Raz404cdbf2016-01-06 13:01:07 +01001197 if (ret && ret != -EOPNOTSUPP)
1198 goto err_vlan_enabled;
1199
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001200 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001201 if (ret)
1202 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001203 ret = vlan_tunnel_init(vg);
1204 if (ret)
1205 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001206 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001207 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001208 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001209 bool changed;
1210
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001211 ret = nbp_vlan_add(p, p->br->default_pvid,
1212 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001213 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001214 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001215 if (ret)
1216 goto err_vlan_add;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001217 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001218 }
1219out:
1220 return ret;
1221
1222err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001223 RCU_INIT_POINTER(p->vlgrp, NULL);
1224 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001225 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001226err_tunnel_init:
1227 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001228err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001229err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001230 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001231
1232 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001233}
1234
Toshiaki Makita8adff412013-10-16 17:07:13 +09001235/* Must be protected by RTNL.
1236 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001237 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001238 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001239int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001240 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001241{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001242 struct net_bridge_vlan *vlan;
1243 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001244
1245 ASSERT_RTNL();
1246
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001247 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001248 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001249 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001250 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001251 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001252 if (ret && ret != -EOPNOTSUPP)
1253 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001254 *changed = __vlan_add_flags(vlan, flags);
1255
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001256 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001257 }
1258
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001259 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1260 if (!vlan)
1261 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001262
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001263 vlan->vid = vid;
1264 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001265 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001266 if (ret)
1267 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001268 else
1269 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001270
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001271 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001272}
1273
Toshiaki Makita8adff412013-10-16 17:07:13 +09001274/* Must be protected by RTNL.
1275 * Must be called with vid in range from 1 to 4094 inclusive.
1276 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001277int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1278{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001279 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001280
1281 ASSERT_RTNL();
1282
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001283 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001284 if (!v)
1285 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001286 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001287 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001288
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001289 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001290}
1291
1292void nbp_vlan_flush(struct net_bridge_port *port)
1293{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001294 struct net_bridge_vlan_group *vg;
1295
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001296 ASSERT_RTNL();
1297
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001298 vg = nbp_vlan_group(port);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001299 __vlan_flush(port->br, port, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001300 RCU_INIT_POINTER(port->vlgrp, NULL);
1301 synchronize_rcu();
1302 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001303}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001304
1305void br_vlan_get_stats(const struct net_bridge_vlan *v,
Heiner Kallweit281cc282020-11-17 21:25:42 +01001306 struct pcpu_sw_netstats *stats)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001307{
1308 int i;
1309
1310 memset(stats, 0, sizeof(*stats));
1311 for_each_possible_cpu(i) {
1312 u64 rxpackets, rxbytes, txpackets, txbytes;
Heiner Kallweit281cc282020-11-17 21:25:42 +01001313 struct pcpu_sw_netstats *cpu_stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001314 unsigned int start;
1315
1316 cpu_stats = per_cpu_ptr(v->stats, i);
1317 do {
1318 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1319 rxpackets = cpu_stats->rx_packets;
1320 rxbytes = cpu_stats->rx_bytes;
1321 txbytes = cpu_stats->tx_bytes;
1322 txpackets = cpu_stats->tx_packets;
1323 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1324
1325 stats->rx_packets += rxpackets;
1326 stats->rx_bytes += rxbytes;
1327 stats->tx_bytes += txbytes;
1328 stats->tx_packets += txpackets;
1329 }
1330}
Petr Machata4d4fd362018-04-29 10:56:08 +03001331
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001332int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
Petr Machata4d4fd362018-04-29 10:56:08 +03001333{
1334 struct net_bridge_vlan_group *vg;
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001335 struct net_bridge_port *p;
Petr Machata4d4fd362018-04-29 10:56:08 +03001336
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001337 ASSERT_RTNL();
1338 p = br_port_get_check_rtnl(dev);
Ido Schimmel5a6db042018-11-28 20:06:58 +00001339 if (p)
1340 vg = nbp_vlan_group(p);
1341 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001342 vg = br_vlan_group(netdev_priv(dev));
1343 else
1344 return -EINVAL;
1345
1346 *p_pvid = br_get_pvid(vg);
1347 return 0;
1348}
1349EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1350
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001351int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1352{
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001353 struct net_bridge_vlan_group *vg;
1354 struct net_bridge_port *p;
1355
1356 p = br_port_get_check_rcu(dev);
1357 if (p)
1358 vg = nbp_vlan_group_rcu(p);
1359 else if (netif_is_bridge_master(dev))
1360 vg = br_vlan_group_rcu(netdev_priv(dev));
1361 else
1362 return -EINVAL;
1363
1364 *p_pvid = br_get_pvid(vg);
1365 return 0;
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001366}
1367EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1368
Felix Fietkaubcf27662021-03-24 02:30:35 +01001369void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1370 struct net_device_path_ctx *ctx,
1371 struct net_device_path *path)
1372{
1373 struct net_bridge_vlan_group *vg;
1374 int idx = ctx->num_vlans - 1;
1375 u16 vid;
1376
1377 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1378
1379 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1380 return;
1381
1382 vg = br_vlan_group(br);
1383
1384 if (idx >= 0 &&
1385 ctx->vlan[idx].proto == br->vlan_proto) {
1386 vid = ctx->vlan[idx].id;
1387 } else {
1388 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG;
1389 vid = br_get_pvid(vg);
1390 }
1391
1392 path->bridge.vlan_id = vid;
1393 path->bridge.vlan_proto = br->vlan_proto;
1394}
1395
1396int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1397 struct net_bridge_port *dst,
1398 struct net_device_path *path)
1399{
1400 struct net_bridge_vlan_group *vg;
1401 struct net_bridge_vlan *v;
1402
1403 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1404 return 0;
1405
1406 vg = nbp_vlan_group_rcu(dst);
1407 v = br_vlan_find(vg, path->bridge.vlan_id);
1408 if (!v || !br_vlan_should_use(v))
1409 return -EINVAL;
1410
1411 if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1412 return 0;
1413
1414 if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG)
1415 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
Felix Fietkau26267bf2021-03-24 02:30:48 +01001416 else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
1417 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW;
Felix Fietkaubcf27662021-03-24 02:30:35 +01001418 else
1419 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG;
1420
1421 return 0;
1422}
1423
Petr Machata4d4fd362018-04-29 10:56:08 +03001424int br_vlan_get_info(const struct net_device *dev, u16 vid,
1425 struct bridge_vlan_info *p_vinfo)
1426{
1427 struct net_bridge_vlan_group *vg;
1428 struct net_bridge_vlan *v;
1429 struct net_bridge_port *p;
1430
1431 ASSERT_RTNL();
1432 p = br_port_get_check_rtnl(dev);
1433 if (p)
1434 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001435 else if (netif_is_bridge_master(dev))
1436 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001437 else
1438 return -EINVAL;
1439
1440 v = br_vlan_find(vg, vid);
1441 if (!v)
1442 return -ENOENT;
1443
1444 p_vinfo->vid = vid;
1445 p_vinfo->flags = v->flags;
Vladimir Olteanf40d9b202019-08-30 03:53:24 +03001446 if (vid == br_get_pvid(vg))
1447 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
Petr Machata4d4fd362018-04-29 10:56:08 +03001448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001451
Vladimir Olteanee80dd22021-07-26 19:55:29 +03001452int br_vlan_get_info_rcu(const struct net_device *dev, u16 vid,
1453 struct bridge_vlan_info *p_vinfo)
1454{
1455 struct net_bridge_vlan_group *vg;
1456 struct net_bridge_vlan *v;
1457 struct net_bridge_port *p;
1458
1459 p = br_port_get_check_rcu(dev);
1460 if (p)
1461 vg = nbp_vlan_group_rcu(p);
1462 else if (netif_is_bridge_master(dev))
1463 vg = br_vlan_group_rcu(netdev_priv(dev));
1464 else
1465 return -EINVAL;
1466
1467 v = br_vlan_find(vg, vid);
1468 if (!v)
1469 return -ENOENT;
1470
1471 p_vinfo->vid = vid;
1472 p_vinfo->flags = v->flags;
1473 if (vid == br_get_pvid(vg))
1474 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1475 return 0;
1476}
1477EXPORT_SYMBOL_GPL(br_vlan_get_info_rcu);
1478
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001479static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1480{
1481 return is_vlan_dev(dev) &&
1482 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1483}
1484
1485static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001486 __always_unused struct netdev_nested_priv *priv)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001487{
1488 return br_vlan_is_bind_vlan_dev(dev);
1489}
1490
1491static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1492{
1493 int found;
1494
1495 rcu_read_lock();
1496 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1497 NULL);
1498 rcu_read_unlock();
1499
1500 return !!found;
1501}
1502
1503struct br_vlan_bind_walk_data {
1504 u16 vid;
1505 struct net_device *result;
1506};
1507
1508static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001509 struct netdev_nested_priv *priv)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001510{
Taehee Yooeff74232020-09-25 18:13:12 +00001511 struct br_vlan_bind_walk_data *data = priv->data;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001512 int found = 0;
1513
1514 if (br_vlan_is_bind_vlan_dev(dev) &&
1515 vlan_dev_priv(dev)->vlan_id == data->vid) {
1516 data->result = dev;
1517 found = 1;
1518 }
1519
1520 return found;
1521}
1522
1523static struct net_device *
1524br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1525{
1526 struct br_vlan_bind_walk_data data = {
1527 .vid = vid,
1528 };
Taehee Yooeff74232020-09-25 18:13:12 +00001529 struct netdev_nested_priv priv = {
1530 .data = (void *)&data,
1531 };
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001532
1533 rcu_read_lock();
1534 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
Taehee Yooeff74232020-09-25 18:13:12 +00001535 &priv);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001536 rcu_read_unlock();
1537
1538 return data.result;
1539}
1540
1541static bool br_vlan_is_dev_up(const struct net_device *dev)
1542{
1543 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1544}
1545
1546static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1547 struct net_device *vlan_dev)
1548{
1549 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1550 struct net_bridge_vlan_group *vg;
1551 struct net_bridge_port *p;
1552 bool has_carrier = false;
1553
Mike Manning8e1acd42019-04-18 18:35:35 +01001554 if (!netif_carrier_ok(br->dev)) {
1555 netif_carrier_off(vlan_dev);
1556 return;
1557 }
1558
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001559 list_for_each_entry(p, &br->port_list, list) {
1560 vg = nbp_vlan_group(p);
1561 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1562 has_carrier = true;
1563 break;
1564 }
1565 }
1566
1567 if (has_carrier)
1568 netif_carrier_on(vlan_dev);
1569 else
1570 netif_carrier_off(vlan_dev);
1571}
1572
1573static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1574{
1575 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1576 struct net_bridge_vlan *vlan;
1577 struct net_device *vlan_dev;
1578
1579 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1580 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1581 vlan->vid);
1582 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001583 if (br_vlan_is_dev_up(p->dev)) {
1584 if (netif_carrier_ok(p->br->dev))
1585 netif_carrier_on(vlan_dev);
1586 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001587 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001588 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001589 }
1590 }
1591}
1592
1593static void br_vlan_upper_change(struct net_device *dev,
1594 struct net_device *upper_dev,
1595 bool linking)
1596{
1597 struct net_bridge *br = netdev_priv(dev);
1598
1599 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1600 return;
1601
1602 if (linking) {
1603 br_vlan_set_vlan_dev_state(br, upper_dev);
1604 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1605 } else {
1606 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1607 br_vlan_has_upper_bind_vlan_dev(dev));
1608 }
1609}
1610
Mike Manning8e1acd42019-04-18 18:35:35 +01001611struct br_vlan_link_state_walk_data {
1612 struct net_bridge *br;
1613};
1614
1615static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001616 struct netdev_nested_priv *priv)
Mike Manning8e1acd42019-04-18 18:35:35 +01001617{
Taehee Yooeff74232020-09-25 18:13:12 +00001618 struct br_vlan_link_state_walk_data *data = priv->data;
Mike Manning8e1acd42019-04-18 18:35:35 +01001619
1620 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1621 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1622
1623 return 0;
1624}
1625
1626static void br_vlan_link_state_change(struct net_device *dev,
1627 struct net_bridge *br)
1628{
1629 struct br_vlan_link_state_walk_data data = {
1630 .br = br
1631 };
Taehee Yooeff74232020-09-25 18:13:12 +00001632 struct netdev_nested_priv priv = {
1633 .data = (void *)&data,
1634 };
Mike Manning8e1acd42019-04-18 18:35:35 +01001635
1636 rcu_read_lock();
1637 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
Taehee Yooeff74232020-09-25 18:13:12 +00001638 &priv);
Mike Manning8e1acd42019-04-18 18:35:35 +01001639 rcu_read_unlock();
1640}
1641
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001642/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001643static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1644{
1645 struct net_device *vlan_dev;
1646
1647 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1648 return;
1649
1650 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1651 if (vlan_dev)
1652 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1653}
1654
1655/* Must be protected by RTNL. */
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001656int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001657{
1658 struct netdev_notifier_changeupper_info *info;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001659 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001660 int vlcmd = 0, ret = 0;
1661 bool changed = false;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001662
1663 switch (event) {
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001664 case NETDEV_REGISTER:
1665 ret = br_vlan_add(br, br->default_pvid,
1666 BRIDGE_VLAN_INFO_PVID |
1667 BRIDGE_VLAN_INFO_UNTAGGED |
1668 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001669 vlcmd = RTM_NEWVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001670 break;
1671 case NETDEV_UNREGISTER:
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001672 changed = !br_vlan_delete(br, br->default_pvid);
1673 vlcmd = RTM_DELVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001674 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001675 case NETDEV_CHANGEUPPER:
1676 info = ptr;
1677 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1678 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001679
1680 case NETDEV_CHANGE:
1681 case NETDEV_UP:
Mike Manning8e1acd42019-04-18 18:35:35 +01001682 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001683 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001684 br_vlan_link_state_change(dev, br);
1685 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001686 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001687 if (changed)
1688 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001689
1690 return ret;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001691}
1692
1693/* Must be protected by RTNL. */
1694void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1695{
1696 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1697 return;
1698
1699 switch (event) {
1700 case NETDEV_CHANGE:
1701 case NETDEV_DOWN:
1702 case NETDEV_UP:
1703 br_vlan_set_all_vlan_dev_state(p);
1704 break;
1705 }
1706}
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001707
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001708static bool br_vlan_stats_fill(struct sk_buff *skb,
1709 const struct net_bridge_vlan *v)
1710{
Heiner Kallweit281cc282020-11-17 21:25:42 +01001711 struct pcpu_sw_netstats stats;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001712 struct nlattr *nest;
1713
1714 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1715 if (!nest)
1716 return false;
1717
1718 br_vlan_get_stats(v, &stats);
1719 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1720 BRIDGE_VLANDB_STATS_PAD) ||
1721 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1722 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1723 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1724 BRIDGE_VLANDB_STATS_PAD) ||
1725 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1726 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1727 goto out_err;
1728
1729 nla_nest_end(skb, nest);
1730
1731 return true;
1732
1733out_err:
1734 nla_nest_cancel(skb, nest);
1735 return false;
1736}
1737
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001738/* v_opts is used to dump the options which must be equal in the whole range */
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001739static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001740 const struct net_bridge_vlan *v_opts,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001741 u16 flags,
1742 bool dump_stats)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001743{
1744 struct bridge_vlan_info info;
1745 struct nlattr *nest;
1746
1747 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1748 if (!nest)
1749 return false;
1750
1751 memset(&info, 0, sizeof(info));
1752 info.vid = vid;
1753 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1754 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1755 if (flags & BRIDGE_VLAN_INFO_PVID)
1756 info.flags |= BRIDGE_VLAN_INFO_PVID;
1757
1758 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1759 goto out_err;
1760
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001761 if (vid_range && vid < vid_range &&
1762 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1763 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1764 goto out_err;
1765
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001766 if (v_opts) {
1767 if (!br_vlan_opts_fill(skb, v_opts))
1768 goto out_err;
1769
1770 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1771 goto out_err;
1772 }
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001773
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001774 nla_nest_end(skb, nest);
1775
1776 return true;
1777
1778out_err:
1779 nla_nest_cancel(skb, nest);
1780 return false;
1781}
1782
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001783static size_t rtnl_vlan_nlmsg_size(void)
1784{
1785 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1786 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1787 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001788 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1789 + br_vlan_opts_nl_size(); /* bridge vlan options */
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001790}
1791
1792void br_vlan_notify(const struct net_bridge *br,
1793 const struct net_bridge_port *p,
1794 u16 vid, u16 vid_range,
1795 int cmd)
1796{
1797 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001798 struct net_bridge_vlan *v = NULL;
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001799 struct br_vlan_msg *bvm;
1800 struct nlmsghdr *nlh;
1801 struct sk_buff *skb;
1802 int err = -ENOBUFS;
1803 struct net *net;
1804 u16 flags = 0;
1805 int ifindex;
1806
1807 /* right now notifications are done only with rtnl held */
1808 ASSERT_RTNL();
1809
1810 if (p) {
1811 ifindex = p->dev->ifindex;
1812 vg = nbp_vlan_group(p);
1813 net = dev_net(p->dev);
1814 } else {
1815 ifindex = br->dev->ifindex;
1816 vg = br_vlan_group(br);
1817 net = dev_net(br->dev);
1818 }
1819
1820 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1821 if (!skb)
1822 goto out_err;
1823
1824 err = -EMSGSIZE;
1825 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1826 if (!nlh)
1827 goto out_err;
1828 bvm = nlmsg_data(nlh);
1829 memset(bvm, 0, sizeof(*bvm));
1830 bvm->family = AF_BRIDGE;
1831 bvm->ifindex = ifindex;
1832
1833 switch (cmd) {
1834 case RTM_NEWVLAN:
1835 /* need to find the vlan due to flags/options */
1836 v = br_vlan_find(vg, vid);
1837 if (!v || !br_vlan_should_use(v))
1838 goto out_kfree;
1839
1840 flags = v->flags;
1841 if (br_get_pvid(vg) == v->vid)
1842 flags |= BRIDGE_VLAN_INFO_PVID;
1843 break;
1844 case RTM_DELVLAN:
1845 break;
1846 default:
1847 goto out_kfree;
1848 }
1849
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001850 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001851 goto out_err;
1852
1853 nlmsg_end(skb, nlh);
1854 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1855 return;
1856
1857out_err:
1858 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1859out_kfree:
1860 kfree_skb(skb);
1861}
1862
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001863/* check if v_curr can enter a range ending in range_end */
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001864bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1865 const struct net_bridge_vlan *range_end)
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001866{
1867 return v_curr->vid - range_end->vid == 1 &&
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001868 range_end->flags == v_curr->flags &&
Nikolay Aleksandrov99f7c5e2020-03-17 14:08:33 +02001869 br_vlan_opts_eq_range(v_curr, range_end);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001870}
1871
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001872static int br_vlan_dump_dev(const struct net_device *dev,
1873 struct sk_buff *skb,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001874 struct netlink_callback *cb,
1875 u32 dump_flags)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001876{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001877 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001878 bool dump_global = !!(dump_flags & BRIDGE_VLANDB_DUMPF_GLOBAL);
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001879 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001880 struct net_bridge_vlan_group *vg;
1881 int idx = 0, s_idx = cb->args[1];
1882 struct nlmsghdr *nlh = NULL;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001883 struct net_bridge_port *p;
1884 struct br_vlan_msg *bvm;
1885 struct net_bridge *br;
1886 int err = 0;
1887 u16 pvid;
1888
1889 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1890 return -EINVAL;
1891
1892 if (netif_is_bridge_master(dev)) {
1893 br = netdev_priv(dev);
1894 vg = br_vlan_group_rcu(br);
1895 p = NULL;
1896 } else {
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001897 /* global options are dumped only for bridge devices */
1898 if (dump_global)
1899 return 0;
1900
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001901 p = br_port_get_rcu(dev);
1902 if (WARN_ON(!p))
1903 return -EINVAL;
1904 vg = nbp_vlan_group_rcu(p);
1905 br = p->br;
1906 }
1907
1908 if (!vg)
1909 return 0;
1910
1911 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1912 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1913 if (!nlh)
1914 return -EMSGSIZE;
1915 bvm = nlmsg_data(nlh);
1916 memset(bvm, 0, sizeof(*bvm));
1917 bvm->family = PF_BRIDGE;
1918 bvm->ifindex = dev->ifindex;
1919 pvid = br_get_pvid(vg);
1920
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001921 /* idx must stay at range's beginning until it is filled in */
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001922 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001923 if (!dump_global && !br_vlan_should_use(v))
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001924 continue;
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001925 if (idx < s_idx) {
1926 idx++;
1927 continue;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001928 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001929
1930 if (!range_start) {
1931 range_start = v;
1932 range_end = v;
1933 continue;
1934 }
1935
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001936 if (dump_global) {
1937 if (br_vlan_global_opts_can_enter_range(v, range_end))
Nikolay Aleksandrov6c4110d2021-08-10 12:21:39 +03001938 goto update_end;
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001939 if (!br_vlan_global_opts_fill(skb, range_start->vid,
1940 range_end->vid,
1941 range_start)) {
1942 err = -EMSGSIZE;
1943 break;
1944 }
1945 /* advance number of filled vlans */
1946 idx += range_end->vid - range_start->vid + 1;
1947
1948 range_start = v;
1949 } else if (dump_stats || v->vid == pvid ||
1950 !br_vlan_can_enter_range(v, range_end)) {
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001951 u16 vlan_flags = br_vlan_flags(range_start, pvid);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001952
1953 if (!br_vlan_fill_vids(skb, range_start->vid,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001954 range_end->vid, range_start,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001955 vlan_flags, dump_stats)) {
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001956 err = -EMSGSIZE;
1957 break;
1958 }
1959 /* advance number of filled vlans */
1960 idx += range_end->vid - range_start->vid + 1;
1961
1962 range_start = v;
1963 }
Nikolay Aleksandrov6c4110d2021-08-10 12:21:39 +03001964update_end:
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001965 range_end = v;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001966 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001967
1968 /* err will be 0 and range_start will be set in 3 cases here:
1969 * - first vlan (range_start == range_end)
1970 * - last vlan (range_start == range_end, not in range)
1971 * - last vlan range (range_start != range_end, in range)
1972 */
Nikolay Aleksandrov743a53d2021-07-19 20:06:35 +03001973 if (!err && range_start) {
1974 if (dump_global &&
1975 !br_vlan_global_opts_fill(skb, range_start->vid,
1976 range_end->vid, range_start))
1977 err = -EMSGSIZE;
1978 else if (!dump_global &&
1979 !br_vlan_fill_vids(skb, range_start->vid,
1980 range_end->vid, range_start,
1981 br_vlan_flags(range_start, pvid),
1982 dump_stats))
1983 err = -EMSGSIZE;
1984 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001985
1986 cb->args[1] = err ? idx : 0;
1987
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001988 nlmsg_end(skb, nlh);
1989
1990 return err;
1991}
1992
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001993static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1994 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1995};
1996
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001997static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1998{
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001999 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002000 int idx = 0, err = 0, s_idx = cb->args[0];
2001 struct net *net = sock_net(skb->sk);
2002 struct br_vlan_msg *bvm;
2003 struct net_device *dev;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02002004 u32 dump_flags = 0;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002005
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02002006 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
2007 br_vlan_db_dump_pol, cb->extack);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002008 if (err < 0)
2009 return err;
2010
2011 bvm = nlmsg_data(cb->nlh);
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02002012 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
2013 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002014
2015 rcu_read_lock();
2016 if (bvm->ifindex) {
2017 dev = dev_get_by_index_rcu(net, bvm->ifindex);
2018 if (!dev) {
2019 err = -ENODEV;
2020 goto out_err;
2021 }
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02002022 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002023 if (err && err != -EMSGSIZE)
2024 goto out_err;
2025 } else {
2026 for_each_netdev_rcu(net, dev) {
2027 if (idx < s_idx)
2028 goto skip;
2029
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02002030 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002031 if (err == -EMSGSIZE)
2032 break;
2033skip:
2034 idx++;
2035 }
2036 }
2037 cb->args[0] = idx;
2038 rcu_read_unlock();
2039
2040 return skb->len;
2041
2042out_err:
2043 rcu_read_unlock();
2044
2045 return err;
2046}
2047
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002048static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
Johannes Berg81408602020-08-18 10:17:31 +02002049 [BRIDGE_VLANDB_ENTRY_INFO] =
2050 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02002051 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
Nikolay Aleksandrova580c762020-01-24 13:40:22 +02002052 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +02002053 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
Nikolay Aleksandrov2796d842021-08-20 15:42:55 +03002054 [BRIDGE_VLANDB_ENTRY_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002055};
2056
2057static int br_vlan_rtm_process_one(struct net_device *dev,
2058 const struct nlattr *attr,
2059 int cmd, struct netlink_ext_ack *extack)
2060{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02002061 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002062 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02002063 bool changed = false, skip_processing = false;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002064 struct net_bridge_vlan_group *vg;
2065 struct net_bridge_port *p = NULL;
2066 int err = 0, cmdmap = 0;
2067 struct net_bridge *br;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002068
2069 if (netif_is_bridge_master(dev)) {
2070 br = netdev_priv(dev);
2071 vg = br_vlan_group(br);
2072 } else {
2073 p = br_port_get_rtnl(dev);
2074 if (WARN_ON(!p))
2075 return -ENODEV;
2076 br = p->br;
2077 vg = nbp_vlan_group(p);
2078 }
2079
2080 if (WARN_ON(!vg))
2081 return -ENODEV;
2082
2083 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
2084 br_vlan_db_policy, extack);
2085 if (err)
2086 return err;
2087
2088 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
2089 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
2090 return -EINVAL;
2091 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02002092 memset(&vrange_end, 0, sizeof(vrange_end));
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002093
2094 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
2095 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
2096 BRIDGE_VLAN_INFO_RANGE_END)) {
2097 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
2098 return -EINVAL;
2099 }
2100 if (!br_vlan_valid_id(vinfo->vid, extack))
2101 return -EINVAL;
2102
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02002103 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
2104 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
2105 /* validate user-provided flags without RANGE_BEGIN */
2106 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
2107 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
2108
2109 /* vinfo_last is the range start, vinfo the range end */
2110 vinfo_last = vinfo;
2111 vinfo = &vrange_end;
2112
2113 if (!br_vlan_valid_id(vinfo->vid, extack) ||
2114 !br_vlan_valid_range(vinfo, vinfo_last, extack))
2115 return -EINVAL;
2116 }
2117
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002118 switch (cmd) {
2119 case RTM_NEWVLAN:
2120 cmdmap = RTM_SETLINK;
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02002121 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002122 break;
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002123 case RTM_DELVLAN:
2124 cmdmap = RTM_DELLINK;
2125 break;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002126 }
2127
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02002128 if (!skip_processing) {
2129 struct bridge_vlan_info *tmp_last = vinfo_last;
2130
2131 /* br_process_vlan_info may overwrite vinfo_last */
2132 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
2133 &changed, extack);
2134
2135 /* notify first if anything changed */
2136 if (changed)
2137 br_ifinfo_notify(cmdmap, br, p);
2138
2139 if (err)
2140 return err;
2141 }
2142
2143 /* deal with options */
2144 if (cmd == RTM_NEWVLAN) {
2145 struct net_bridge_vlan *range_start, *range_end;
2146
2147 if (vinfo_last) {
2148 range_start = br_vlan_find(vg, vinfo_last->vid);
2149 range_end = br_vlan_find(vg, vinfo->vid);
2150 } else {
2151 range_start = br_vlan_find(vg, vinfo->vid);
2152 range_end = range_start;
2153 }
2154
2155 err = br_vlan_process_options(br, p, range_start, range_end,
2156 tb, extack);
2157 }
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002158
2159 return err;
2160}
2161
2162static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2163 struct netlink_ext_ack *extack)
2164{
2165 struct net *net = sock_net(skb->sk);
2166 struct br_vlan_msg *bvm;
2167 struct net_device *dev;
2168 struct nlattr *attr;
2169 int err, vlans = 0;
2170 int rem;
2171
2172 /* this should validate the header and check for remaining bytes */
2173 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2174 extack);
2175 if (err < 0)
2176 return err;
2177
2178 bvm = nlmsg_data(nlh);
2179 dev = __dev_get_by_index(net, bvm->ifindex);
2180 if (!dev)
2181 return -ENODEV;
2182
2183 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2184 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2185 return -EINVAL;
2186 }
2187
2188 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
Nikolay Aleksandrov47ecd2d2021-07-19 20:06:34 +03002189 switch (nla_type(attr)) {
2190 case BRIDGE_VLANDB_ENTRY:
2191 err = br_vlan_rtm_process_one(dev, attr,
2192 nlh->nlmsg_type,
2193 extack);
2194 break;
2195 case BRIDGE_VLANDB_GLOBAL_OPTIONS:
2196 err = br_vlan_rtm_process_global_options(dev, attr,
2197 nlh->nlmsg_type,
2198 extack);
2199 break;
2200 default:
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002201 continue;
Nikolay Aleksandrov47ecd2d2021-07-19 20:06:34 +03002202 }
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002203
2204 vlans++;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002205 if (err)
2206 break;
2207 }
2208 if (!vlans) {
2209 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2210 err = -EINVAL;
2211 }
2212
2213 return err;
2214}
2215
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002216void br_vlan_rtnl_init(void)
2217{
2218 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2219 br_vlan_rtm_dump, 0);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002220 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2221 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002222 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2223 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002224}
2225
2226void br_vlan_rtnl_uninit(void)
2227{
2228 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002229 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002230 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002231}