blob: f9092c71225fd3d91efc594d0a3786ffe468841f [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);
116 if (v->vid < vent->vid)
117 continue;
118 else
119 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000120 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200121 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200122}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000123
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200124static void __vlan_del_list(struct net_bridge_vlan *v)
125{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200126 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000127}
128
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400129static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +0000130 const struct net_bridge_vlan *v)
Scott Feldman7f109532015-06-12 17:39:50 -0700131{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700133
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200134 /* Try switchdev op first. In case it is not supported, fallback to
135 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700136 */
Ido Schimmel27973792019-01-08 16:48:11 +0000137 err = br_switchdev_port_vlan_del(dev, v->vid);
138 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
139 vlan_vid_del(dev, br->vlan_proto, v->vid);
140 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman7f109532015-06-12 17:39:50 -0700141}
142
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200143/* Returns a master vlan, if it didn't exist it gets created. In all cases a
144 * a reference is taken to the master vlan before returning.
145 */
Petr Machata169327d2018-12-12 17:02:50 +0000146static struct net_bridge_vlan *
147br_vlan_get_master(struct net_bridge *br, u16 vid,
148 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200150 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200151 struct net_bridge_vlan *masterv;
152
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200153 vg = br_vlan_group(br);
154 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200155 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300156 bool changed;
157
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000159 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200160 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200161 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200162 if (WARN_ON(!masterv))
163 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200164 refcount_set(&masterv->refcnt, 1);
165 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200166 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300167 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200168
169 return masterv;
170}
171
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200172static void br_master_vlan_rcu_free(struct rcu_head *rcu)
173{
174 struct net_bridge_vlan *v;
175
176 v = container_of(rcu, struct net_bridge_vlan, rcu);
177 WARN_ON(!br_vlan_is_master(v));
178 free_percpu(v->stats);
179 v->stats = NULL;
180 kfree(v);
181}
182
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200183static void br_vlan_put_master(struct net_bridge_vlan *masterv)
184{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200185 struct net_bridge_vlan_group *vg;
186
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200187 if (!br_vlan_is_master(masterv))
188 return;
189
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200190 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300191 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200192 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200193 &masterv->vnode, br_vlan_rht_params);
194 __vlan_del_list(masterv);
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);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200269 if (!masterv)
270 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200271 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300272 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
273 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
274 if (!v->stats) {
275 err = -ENOMEM;
276 goto out_filt;
277 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200278 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300279 } else {
280 v->stats = masterv->stats;
281 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200282 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000283 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200284 if (err && err != -EOPNOTSUPP)
285 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900286 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000287
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200288 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200289 if (br_vlan_should_use(v)) {
290 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
291 if (err) {
292 br_err(br, "failed insert local address into bridge forwarding table\n");
293 goto out_filt;
294 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200295 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000296 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200297
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200298 /* set the state before publishing */
299 v->state = BR_STATE_FORWARDING;
300
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200301 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
302 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200303 if (err)
304 goto out_fdb_insert;
305
306 __vlan_add_list(v);
307 __vlan_add_flags(v, flags);
Mike Manning80900ac2019-04-18 18:35:34 +0100308
309 if (p)
310 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200311out:
312 return err;
313
314out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200315 if (br_vlan_should_use(v)) {
316 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
317 vg->num_vlans--;
318 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200319
320out_filt:
321 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000322 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200323 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800324 if (v->stats && masterv->stats != v->stats)
325 free_percpu(v->stats);
326 v->stats = NULL;
327
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200328 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 v->brvlan = NULL;
330 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200331 } else {
332 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 }
334
335 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000336}
337
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200338static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000339{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200340 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200341 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200342 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200343 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344
345 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200346 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200347 } else {
348 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200349 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351 }
352
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200353 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000355 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 if (err)
357 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200358 } else {
359 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
360 if (err && err != -EOPNOTSUPP)
361 goto out;
362 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200363 }
364
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200365 if (br_vlan_should_use(v)) {
366 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
367 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200368 }
369
370 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800371 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200372 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
373 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200374 __vlan_del_list(v);
Mike Manning80900ac2019-04-18 18:35:34 +0100375 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300376 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200377 }
378
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200379 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200380out:
381 return err;
382}
383
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200384static void __vlan_group_free(struct net_bridge_vlan_group *vg)
385{
386 WARN_ON(!list_empty(&vg->vlan_list));
387 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800388 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200389 kfree(vg);
390}
391
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200392static void __vlan_flush(const struct net_bridge *br,
393 const struct net_bridge_port *p,
394 struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200395{
396 struct net_bridge_vlan *vlan, *tmp;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200397 u16 v_start = 0, v_end = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200398
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200399 __vlan_delete_pvid(vg, vg->pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200400 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
401 /* take care of disjoint ranges */
402 if (!v_start) {
403 v_start = vlan->vid;
404 } else if (vlan->vid - v_end != 1) {
405 /* found range end, notify and start next one */
406 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
407 v_start = vlan->vid;
408 }
409 v_end = vlan->vid;
410
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200411 __vlan_del(vlan);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200412 }
413
414 /* notify about the last/whole vlan range */
415 if (v_start)
416 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000417}
418
Vlad Yasevich78851982013-02-13 12:00:14 +0000419struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800420 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200421 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000422 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000423{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200424 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200425 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000426 u16 vid;
427
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400428 /* If this packet was not filtered at input, let it pass */
429 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000430 goto out;
431
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200432 /* At this point, we know that the frame was filtered and contains
433 * a valid vlan id. If the vlan id has untagged flag set,
434 * send untagged; otherwise, send tagged.
435 */
436 br_vlan_get_tag(skb, &vid);
437 v = br_vlan_find(vg, vid);
438 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400439 * only exception is the bridge is set in promisc mode and the
440 * packet is destined for the bridge device. In this case
441 * pass the packet as is.
442 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200443 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400444 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
445 goto out;
446 } else {
447 kfree_skb(skb);
448 return NULL;
449 }
450 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300451 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200452 stats = this_cpu_ptr(v->stats);
453 u64_stats_update_begin(&stats->syncp);
454 stats->tx_bytes += skb->len;
455 stats->tx_packets++;
456 u64_stats_update_end(&stats->syncp);
457 }
458
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200459 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100460 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800461
462 if (p && (p->flags & BR_VLAN_TUNNEL) &&
463 br_handle_egress_vlan_tunnel(skb, v)) {
464 kfree_skb(skb);
465 return NULL;
466 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000467out:
468 return skb;
469}
470
471/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200472static bool __allowed_ingress(const struct net_bridge *br,
473 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200474 struct sk_buff *skb, u16 *vid,
475 u8 *state)
Vlad Yasevich78851982013-02-13 12:00:14 +0000476{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200477 struct br_vlan_stats *stats;
478 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900479 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000480
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400481 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900482 /* If vlan tx offload is disabled on bridge device and frame was
483 * sent from vlan device on the bridge device, it does not have
484 * HW accelerated vlan tag.
485 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100486 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200487 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400488 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900489 if (unlikely(!skb))
490 return false;
491 }
492
Toshiaki Makita8580e212014-06-10 20:59:23 +0900493 if (!br_vlan_get_tag(skb, vid)) {
494 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200495 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900496 /* Protocol-mismatch, empty out vlan_tci for new tag */
497 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100498 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100499 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900500 if (unlikely(!skb))
501 return false;
502
503 skb_pull(skb, ETH_HLEN);
504 skb_reset_mac_len(skb);
505 *vid = 0;
506 tagged = false;
507 } else {
508 tagged = true;
509 }
510 } else {
511 /* Untagged frame */
512 tagged = false;
513 }
514
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900515 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200516 u16 pvid = br_get_pvid(vg);
517
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900518 /* Frame had a tag with VID 0 or did not have a tag.
519 * See if pvid is set on this port. That tells us which
520 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000521 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400522 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900523 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000524
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900525 /* PVID is set on this port. Any untagged or priority-tagged
526 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000527 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900528 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900529 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900530 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200531 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900532 else
533 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100534 * At this point, we know that skb->vlan_tci VID
535 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900536 * We update only VID field and preserve PCP field.
537 */
538 skb->vlan_tci |= pvid;
539
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200540 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200541 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
542 if (*state == BR_STATE_FORWARDING) {
543 *state = br_vlan_get_pvid_state(vg);
544 return br_vlan_state_allowed(*state, true);
545 } else {
546 return true;
547 }
548 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200549 }
550 v = br_vlan_find(vg, *vid);
551 if (!v || !br_vlan_should_use(v))
552 goto drop;
553
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200554 if (*state == BR_STATE_FORWARDING) {
555 *state = br_vlan_get_state(v);
556 if (!br_vlan_state_allowed(*state, true))
557 goto drop;
558 }
559
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300560 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200561 stats = this_cpu_ptr(v->stats);
562 u64_stats_update_begin(&stats->syncp);
563 stats->rx_bytes += skb->len;
564 stats->rx_packets++;
565 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000566 }
567
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200568 return true;
569
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900570drop:
571 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000572 return false;
573}
574
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200575bool br_allowed_ingress(const struct net_bridge *br,
576 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200577 u16 *vid, u8 *state)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200578{
579 /* If VLAN filtering is disabled on the bridge, all packets are
580 * permitted.
581 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300582 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200583 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
584 return true;
585 }
586
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200587 return __allowed_ingress(br, vg, skb, vid, state);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200588}
589
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000590/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200591bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000592 const struct sk_buff *skb)
593{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200594 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000595 u16 vid;
596
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400597 /* If this packet was not filtered at input, let it pass */
598 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000599 return true;
600
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000601 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200602 v = br_vlan_find(vg, vid);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200603 if (v && br_vlan_should_use(v) &&
604 br_vlan_state_allowed(br_vlan_get_state(v), false))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000605 return true;
606
607 return false;
608}
609
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900610/* Called under RCU */
611bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
612{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200613 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900614 struct net_bridge *br = p->br;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200615 struct net_bridge_vlan *v;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900616
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400617 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300618 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900619 return true;
620
Ido Schimmeleca1e002015-11-01 18:31:45 +0200621 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200622 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900623 return false;
624
Toshiaki Makita8580e212014-06-10 20:59:23 +0900625 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
626 *vid = 0;
627
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900628 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200629 *vid = br_get_pvid(vg);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200630 if (!*vid ||
631 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900632 return false;
633
634 return true;
635 }
636
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200637 v = br_vlan_find(vg, *vid);
638 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900639 return true;
640
641 return false;
642}
643
Petr Machatadbd6dc72018-05-30 02:56:55 +0200644static int br_vlan_add_existing(struct net_bridge *br,
645 struct net_bridge_vlan_group *vg,
646 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000647 u16 flags, bool *changed,
648 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200649{
650 int err;
651
Petr Machata169327d2018-12-12 17:02:50 +0000652 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200653 if (err && err != -EOPNOTSUPP)
654 return err;
655
Petr Machatadbd6dc72018-05-30 02:56:55 +0200656 if (!br_vlan_is_brentry(vlan)) {
657 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200658 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
659 err = -EINVAL;
660 goto err_flags;
661 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200662 /* It was only kept for port vlans, now make it real */
663 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
664 vlan->vid);
665 if (err) {
666 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200667 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200668 }
669
670 refcount_inc(&vlan->refcnt);
671 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
672 vg->num_vlans++;
673 *changed = true;
674 }
675
676 if (__vlan_add_flags(vlan, flags))
677 *changed = true;
678
679 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200680
681err_fdb_insert:
682err_flags:
683 br_switchdev_port_vlan_del(br->dev, vlan->vid);
684 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200685}
686
Toshiaki Makita8adff412013-10-16 17:07:13 +0900687/* Must be protected by RTNL.
688 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300689 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900690 */
Petr Machata169327d2018-12-12 17:02:50 +0000691int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
692 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000693{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200694 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200695 struct net_bridge_vlan *vlan;
696 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000697
698 ASSERT_RTNL();
699
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300700 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200701 vg = br_vlan_group(br);
702 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200703 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000704 return br_vlan_add_existing(br, vg, vlan, flags, changed,
705 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000706
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200707 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
708 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000709 return -ENOMEM;
710
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200711 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
712 if (!vlan->stats) {
713 kfree(vlan);
714 return -ENOMEM;
715 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200716 vlan->vid = vid;
717 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
718 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
719 vlan->br = br;
720 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300721 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000722 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200723 if (ret) {
724 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200725 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300726 } else {
727 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200728 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000729
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200730 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000731}
732
Toshiaki Makita8adff412013-10-16 17:07:13 +0900733/* Must be protected by RTNL.
734 * Must be called with vid in range from 1 to 4094 inclusive.
735 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000736int br_vlan_delete(struct net_bridge *br, u16 vid)
737{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200738 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200739 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000740
741 ASSERT_RTNL();
742
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200743 vg = br_vlan_group(br);
744 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200745 if (!v || !br_vlan_is_brentry(v))
746 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000747
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900748 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700749 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000750
Roopa Prabhuefa53562017-01-31 22:59:54 -0800751 vlan_tunnel_info_del(vg, v);
752
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200753 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000754}
755
756void br_vlan_flush(struct net_bridge *br)
757{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200758 struct net_bridge_vlan_group *vg;
759
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000760 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000761
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200762 vg = br_vlan_group(br);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200763 __vlan_flush(br, NULL, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200764 RCU_INIT_POINTER(br->vlgrp, NULL);
765 synchronize_rcu();
766 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000767}
768
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200769struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900770{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200771 if (!vg)
772 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900773
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200774 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900775}
776
Toshiaki Makita204177f2014-06-10 20:59:25 +0900777/* Must be protected by RTNL. */
778static void recalculate_group_addr(struct net_bridge *br)
779{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300780 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900781 return;
782
783 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300784 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
785 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900786 /* Bridge Group Address */
787 br->group_addr[5] = 0x00;
788 } else { /* vlan_enabled && ETH_P_8021AD */
789 /* Provider Bridge Group Address */
790 br->group_addr[5] = 0x08;
791 }
792 spin_unlock_bh(&br->lock);
793}
794
795/* Must be protected by RTNL. */
796void br_recalculate_fwd_mask(struct net_bridge *br)
797{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300798 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
799 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900800 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
801 else /* vlan_enabled && ETH_P_8021AD */
802 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
803 ~(1u << br->group_addr[5]);
804}
805
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300806int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000807{
Elad Raz6b72a772016-01-06 13:01:06 +0100808 struct switchdev_attr attr = {
809 .orig_dev = br->dev,
810 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
811 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
812 .u.vlan_filtering = val,
813 };
814 int err;
815
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300816 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300817 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000818
Elad Raz6b72a772016-01-06 13:01:06 +0100819 err = switchdev_port_attr_set(br->dev, &attr);
820 if (err && err != -EOPNOTSUPP)
821 return err;
822
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300823 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400824 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900825 recalculate_group_addr(br);
826 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000827
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300828 return 0;
829}
830
831int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
832{
Xin Long047831a2016-04-09 00:03:31 +0800833 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000834}
835
Ido Schimmel1f514452017-05-26 08:37:23 +0200836bool br_vlan_enabled(const struct net_device *dev)
837{
838 struct net_bridge *br = netdev_priv(dev);
839
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300840 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200841}
842EXPORT_SYMBOL_GPL(br_vlan_enabled);
843
wenxu31aed462019-07-05 21:16:36 +0800844int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
845{
846 struct net_bridge *br = netdev_priv(dev);
847
848 *p_proto = ntohs(br->vlan_proto);
849
850 return 0;
851}
852EXPORT_SYMBOL_GPL(br_vlan_get_proto);
853
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900854int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900855{
856 int err = 0;
857 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200858 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200859 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900860 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900861
Toshiaki Makita204177f2014-06-10 20:59:25 +0900862 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900863 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900864
865 /* Add VLANs for the new proto to the device filter. */
866 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200867 vg = nbp_vlan_group(p);
868 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200869 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900870 if (err)
871 goto err_filt;
872 }
873 }
874
875 oldproto = br->vlan_proto;
876 br->vlan_proto = proto;
877
878 recalculate_group_addr(br);
879 br_recalculate_fwd_mask(br);
880
881 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200882 list_for_each_entry(p, &br->port_list, list) {
883 vg = nbp_vlan_group(p);
884 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200885 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200886 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900887
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900888 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900889
890err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200891 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200892 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900893
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200894 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
895 vg = nbp_vlan_group(p);
896 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200897 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200898 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900899
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900900 return err;
901}
902
903int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
904{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900905 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
906 return -EPROTONOSUPPORT;
907
Xin Long047831a2016-04-09 00:03:31 +0800908 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900909}
910
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200911int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
912{
913 switch (val) {
914 case 0:
915 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300916 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200917 break;
918 default:
919 return -EINVAL;
920 }
921
922 return 0;
923}
924
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300925int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
926{
927 struct net_bridge_port *p;
928
929 /* allow to change the option if there are no port vlans configured */
930 list_for_each_entry(p, &br->port_list, list) {
931 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
932
933 if (vg->num_vlans)
934 return -EBUSY;
935 }
936
937 switch (val) {
938 case 0:
939 case 1:
940 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
941 break;
942 default:
943 return -EINVAL;
944 }
945
946 return 0;
947}
948
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200949static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400950{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200951 struct net_bridge_vlan *v;
952
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200953 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200954 return false;
955
956 v = br_vlan_lookup(&vg->vlan_hash, vid);
957 if (v && br_vlan_should_use(v) &&
958 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
959 return true;
960
961 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400962}
963
964static void br_vlan_disable_default_pvid(struct net_bridge *br)
965{
966 struct net_bridge_port *p;
967 u16 pvid = br->default_pvid;
968
969 /* Disable default_pvid on all ports where it is still
970 * configured.
971 */
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200972 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
973 if (!br_vlan_delete(br, pvid))
974 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
975 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400976
977 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200978 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
979 !nbp_vlan_delete(p, pvid))
980 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400981 }
982
983 br->default_pvid = 0;
984}
985
Petr Machata169327d2018-12-12 17:02:50 +0000986int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
987 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400988{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200989 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200990 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400991 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300992 unsigned long *changed;
993 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400994 u16 old_pvid;
995 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400996
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200997 if (!pvid) {
998 br_vlan_disable_default_pvid(br);
999 return 0;
1000 }
1001
Andy Shevchenko459479d2018-08-30 13:33:18 +03001002 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001003 if (!changed)
1004 return -ENOMEM;
1005
1006 old_pvid = br->default_pvid;
1007
1008 /* Update default_pvid config only if we do not conflict with
1009 * user configuration.
1010 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001011 vg = br_vlan_group(br);
1012 pvent = br_vlan_find(vg, pvid);
1013 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001014 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001015 err = br_vlan_add(br, pvid,
1016 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001017 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001018 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001019 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001020 if (err)
1021 goto out;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001022
1023 if (br_vlan_delete(br, old_pvid))
1024 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1025 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001026 set_bit(0, changed);
1027 }
1028
1029 list_for_each_entry(p, &br->port_list, list) {
1030 /* Update default_pvid config only if we do not conflict with
1031 * user configuration.
1032 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001033 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001034 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001035 !vlan_default_pvid(vg, old_pvid)) ||
1036 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001037 continue;
1038
1039 err = nbp_vlan_add(p, pvid,
1040 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001041 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001042 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001043 if (err)
1044 goto err_port;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001045 if (nbp_vlan_delete(p, old_pvid))
1046 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1047 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001048 set_bit(p->port_no, changed);
1049 }
1050
1051 br->default_pvid = pvid;
1052
1053out:
Andy Shevchenko459479d2018-08-30 13:33:18 +03001054 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001055 return err;
1056
1057err_port:
1058 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1059 if (!test_bit(p->port_no, changed))
1060 continue;
1061
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001062 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001063 nbp_vlan_add(p, old_pvid,
1064 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001065 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001066 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001067 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1068 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001069 nbp_vlan_delete(p, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001070 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001071 }
1072
1073 if (test_bit(0, changed)) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001074 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001075 br_vlan_add(br, old_pvid,
1076 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001077 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001078 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001079 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001080 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1081 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001082 br_vlan_delete(br, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001083 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001084 }
1085 goto out;
1086}
1087
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001088int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1089{
1090 u16 pvid = val;
1091 int err = 0;
1092
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001093 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001094 return -EINVAL;
1095
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001096 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001097 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001098
1099 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001100 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001101 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1102 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001103 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001104 }
Petr Machata169327d2018-12-12 17:02:50 +00001105 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001106out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001107 return err;
1108}
1109
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001110int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001111{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001112 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001113 int ret = -ENOMEM;
1114
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001115 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1116 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001117 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001118 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001119 if (ret)
1120 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001121 ret = vlan_tunnel_init(vg);
1122 if (ret)
1123 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001124 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001125 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001126 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001127 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001128
1129out:
1130 return ret;
1131
Roopa Prabhuefa53562017-01-31 22:59:54 -08001132err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001133 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001134err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001135 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001136
1137 goto out;
1138}
1139
Petr Machata169327d2018-12-12 17:02:50 +00001140int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001141{
Elad Raz404cdbf2016-01-06 13:01:07 +01001142 struct switchdev_attr attr = {
1143 .orig_dev = p->br->dev,
1144 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1145 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001146 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001147 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001148 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001149 int ret = -ENOMEM;
1150
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001151 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1152 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001153 goto out;
1154
Elad Raz404cdbf2016-01-06 13:01:07 +01001155 ret = switchdev_port_attr_set(p->dev, &attr);
1156 if (ret && ret != -EOPNOTSUPP)
1157 goto err_vlan_enabled;
1158
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +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 Aleksandrov263344e2015-09-30 20:16:54 +02001165 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001166 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001167 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001168 bool changed;
1169
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001170 ret = nbp_vlan_add(p, p->br->default_pvid,
1171 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001172 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001173 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001174 if (ret)
1175 goto err_vlan_add;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001176 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001177 }
1178out:
1179 return ret;
1180
1181err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001182 RCU_INIT_POINTER(p->vlgrp, NULL);
1183 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001184 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001185err_tunnel_init:
1186 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001187err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001188err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001189 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001190
1191 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001192}
1193
Toshiaki Makita8adff412013-10-16 17:07:13 +09001194/* Must be protected by RTNL.
1195 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001196 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001197 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001198int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001199 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001200{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001201 struct net_bridge_vlan *vlan;
1202 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001203
1204 ASSERT_RTNL();
1205
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001206 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001207 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001208 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001209 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001210 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001211 if (ret && ret != -EOPNOTSUPP)
1212 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001213 *changed = __vlan_add_flags(vlan, flags);
1214
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001215 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001216 }
1217
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001218 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1219 if (!vlan)
1220 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001221
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001222 vlan->vid = vid;
1223 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001224 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001225 if (ret)
1226 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001227 else
1228 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001229
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001230 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001231}
1232
Toshiaki Makita8adff412013-10-16 17:07:13 +09001233/* Must be protected by RTNL.
1234 * Must be called with vid in range from 1 to 4094 inclusive.
1235 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001236int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1237{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001238 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001239
1240 ASSERT_RTNL();
1241
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001242 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001243 if (!v)
1244 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001245 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001246 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001247
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001248 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001249}
1250
1251void nbp_vlan_flush(struct net_bridge_port *port)
1252{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001253 struct net_bridge_vlan_group *vg;
1254
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001255 ASSERT_RTNL();
1256
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001257 vg = nbp_vlan_group(port);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001258 __vlan_flush(port->br, port, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001259 RCU_INIT_POINTER(port->vlgrp, NULL);
1260 synchronize_rcu();
1261 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001262}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001263
1264void br_vlan_get_stats(const struct net_bridge_vlan *v,
1265 struct br_vlan_stats *stats)
1266{
1267 int i;
1268
1269 memset(stats, 0, sizeof(*stats));
1270 for_each_possible_cpu(i) {
1271 u64 rxpackets, rxbytes, txpackets, txbytes;
1272 struct br_vlan_stats *cpu_stats;
1273 unsigned int start;
1274
1275 cpu_stats = per_cpu_ptr(v->stats, i);
1276 do {
1277 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1278 rxpackets = cpu_stats->rx_packets;
1279 rxbytes = cpu_stats->rx_bytes;
1280 txbytes = cpu_stats->tx_bytes;
1281 txpackets = cpu_stats->tx_packets;
1282 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1283
1284 stats->rx_packets += rxpackets;
1285 stats->rx_bytes += rxbytes;
1286 stats->tx_bytes += txbytes;
1287 stats->tx_packets += txpackets;
1288 }
1289}
Petr Machata4d4fd362018-04-29 10:56:08 +03001290
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001291static int __br_vlan_get_pvid(const struct net_device *dev,
1292 struct net_bridge_port *p, u16 *p_pvid)
Petr Machata4d4fd362018-04-29 10:56:08 +03001293{
1294 struct net_bridge_vlan_group *vg;
1295
Ido Schimmel5a6db042018-11-28 20:06:58 +00001296 if (p)
1297 vg = nbp_vlan_group(p);
1298 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001299 vg = br_vlan_group(netdev_priv(dev));
1300 else
1301 return -EINVAL;
1302
1303 *p_pvid = br_get_pvid(vg);
1304 return 0;
1305}
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001306
1307int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1308{
1309 ASSERT_RTNL();
1310
1311 return __br_vlan_get_pvid(dev, br_port_get_check_rtnl(dev), p_pvid);
1312}
Petr Machata4d4fd362018-04-29 10:56:08 +03001313EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1314
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001315int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1316{
1317 return __br_vlan_get_pvid(dev, br_port_get_check_rcu(dev), p_pvid);
1318}
1319EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1320
Petr Machata4d4fd362018-04-29 10:56:08 +03001321int br_vlan_get_info(const struct net_device *dev, u16 vid,
1322 struct bridge_vlan_info *p_vinfo)
1323{
1324 struct net_bridge_vlan_group *vg;
1325 struct net_bridge_vlan *v;
1326 struct net_bridge_port *p;
1327
1328 ASSERT_RTNL();
1329 p = br_port_get_check_rtnl(dev);
1330 if (p)
1331 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001332 else if (netif_is_bridge_master(dev))
1333 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001334 else
1335 return -EINVAL;
1336
1337 v = br_vlan_find(vg, vid);
1338 if (!v)
1339 return -ENOENT;
1340
1341 p_vinfo->vid = vid;
1342 p_vinfo->flags = v->flags;
Vladimir Olteanf40d9b202019-08-30 03:53:24 +03001343 if (vid == br_get_pvid(vg))
1344 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
Petr Machata4d4fd362018-04-29 10:56:08 +03001345 return 0;
1346}
1347EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001348
1349static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1350{
1351 return is_vlan_dev(dev) &&
1352 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1353}
1354
1355static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1356 __always_unused void *data)
1357{
1358 return br_vlan_is_bind_vlan_dev(dev);
1359}
1360
1361static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1362{
1363 int found;
1364
1365 rcu_read_lock();
1366 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1367 NULL);
1368 rcu_read_unlock();
1369
1370 return !!found;
1371}
1372
1373struct br_vlan_bind_walk_data {
1374 u16 vid;
1375 struct net_device *result;
1376};
1377
1378static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1379 void *data_in)
1380{
1381 struct br_vlan_bind_walk_data *data = data_in;
1382 int found = 0;
1383
1384 if (br_vlan_is_bind_vlan_dev(dev) &&
1385 vlan_dev_priv(dev)->vlan_id == data->vid) {
1386 data->result = dev;
1387 found = 1;
1388 }
1389
1390 return found;
1391}
1392
1393static struct net_device *
1394br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1395{
1396 struct br_vlan_bind_walk_data data = {
1397 .vid = vid,
1398 };
1399
1400 rcu_read_lock();
1401 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1402 &data);
1403 rcu_read_unlock();
1404
1405 return data.result;
1406}
1407
1408static bool br_vlan_is_dev_up(const struct net_device *dev)
1409{
1410 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1411}
1412
1413static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1414 struct net_device *vlan_dev)
1415{
1416 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1417 struct net_bridge_vlan_group *vg;
1418 struct net_bridge_port *p;
1419 bool has_carrier = false;
1420
Mike Manning8e1acd42019-04-18 18:35:35 +01001421 if (!netif_carrier_ok(br->dev)) {
1422 netif_carrier_off(vlan_dev);
1423 return;
1424 }
1425
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001426 list_for_each_entry(p, &br->port_list, list) {
1427 vg = nbp_vlan_group(p);
1428 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1429 has_carrier = true;
1430 break;
1431 }
1432 }
1433
1434 if (has_carrier)
1435 netif_carrier_on(vlan_dev);
1436 else
1437 netif_carrier_off(vlan_dev);
1438}
1439
1440static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1441{
1442 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1443 struct net_bridge_vlan *vlan;
1444 struct net_device *vlan_dev;
1445
1446 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1447 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1448 vlan->vid);
1449 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001450 if (br_vlan_is_dev_up(p->dev)) {
1451 if (netif_carrier_ok(p->br->dev))
1452 netif_carrier_on(vlan_dev);
1453 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001454 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001455 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001456 }
1457 }
1458}
1459
1460static void br_vlan_upper_change(struct net_device *dev,
1461 struct net_device *upper_dev,
1462 bool linking)
1463{
1464 struct net_bridge *br = netdev_priv(dev);
1465
1466 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1467 return;
1468
1469 if (linking) {
1470 br_vlan_set_vlan_dev_state(br, upper_dev);
1471 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1472 } else {
1473 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1474 br_vlan_has_upper_bind_vlan_dev(dev));
1475 }
1476}
1477
Mike Manning8e1acd42019-04-18 18:35:35 +01001478struct br_vlan_link_state_walk_data {
1479 struct net_bridge *br;
1480};
1481
1482static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1483 void *data_in)
1484{
1485 struct br_vlan_link_state_walk_data *data = data_in;
1486
1487 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1488 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1489
1490 return 0;
1491}
1492
1493static void br_vlan_link_state_change(struct net_device *dev,
1494 struct net_bridge *br)
1495{
1496 struct br_vlan_link_state_walk_data data = {
1497 .br = br
1498 };
1499
1500 rcu_read_lock();
1501 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1502 &data);
1503 rcu_read_unlock();
1504}
1505
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001506/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001507static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1508{
1509 struct net_device *vlan_dev;
1510
1511 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1512 return;
1513
1514 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1515 if (vlan_dev)
1516 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1517}
1518
1519/* Must be protected by RTNL. */
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001520int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001521{
1522 struct netdev_notifier_changeupper_info *info;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001523 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001524 int vlcmd = 0, ret = 0;
1525 bool changed = false;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001526
1527 switch (event) {
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001528 case NETDEV_REGISTER:
1529 ret = br_vlan_add(br, br->default_pvid,
1530 BRIDGE_VLAN_INFO_PVID |
1531 BRIDGE_VLAN_INFO_UNTAGGED |
1532 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001533 vlcmd = RTM_NEWVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001534 break;
1535 case NETDEV_UNREGISTER:
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001536 changed = !br_vlan_delete(br, br->default_pvid);
1537 vlcmd = RTM_DELVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001538 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001539 case NETDEV_CHANGEUPPER:
1540 info = ptr;
1541 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1542 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001543
1544 case NETDEV_CHANGE:
1545 case NETDEV_UP:
Mike Manning8e1acd42019-04-18 18:35:35 +01001546 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001547 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001548 br_vlan_link_state_change(dev, br);
1549 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001550 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001551 if (changed)
1552 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001553
1554 return ret;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001555}
1556
1557/* Must be protected by RTNL. */
1558void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1559{
1560 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1561 return;
1562
1563 switch (event) {
1564 case NETDEV_CHANGE:
1565 case NETDEV_DOWN:
1566 case NETDEV_UP:
1567 br_vlan_set_all_vlan_dev_state(p);
1568 break;
1569 }
1570}
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001571
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001572static bool br_vlan_stats_fill(struct sk_buff *skb,
1573 const struct net_bridge_vlan *v)
1574{
1575 struct br_vlan_stats stats;
1576 struct nlattr *nest;
1577
1578 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1579 if (!nest)
1580 return false;
1581
1582 br_vlan_get_stats(v, &stats);
1583 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1584 BRIDGE_VLANDB_STATS_PAD) ||
1585 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1586 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1587 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1588 BRIDGE_VLANDB_STATS_PAD) ||
1589 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1590 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1591 goto out_err;
1592
1593 nla_nest_end(skb, nest);
1594
1595 return true;
1596
1597out_err:
1598 nla_nest_cancel(skb, nest);
1599 return false;
1600}
1601
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001602/* v_opts is used to dump the options which must be equal in the whole range */
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001603static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001604 const struct net_bridge_vlan *v_opts,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001605 u16 flags,
1606 bool dump_stats)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001607{
1608 struct bridge_vlan_info info;
1609 struct nlattr *nest;
1610
1611 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1612 if (!nest)
1613 return false;
1614
1615 memset(&info, 0, sizeof(info));
1616 info.vid = vid;
1617 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1618 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1619 if (flags & BRIDGE_VLAN_INFO_PVID)
1620 info.flags |= BRIDGE_VLAN_INFO_PVID;
1621
1622 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1623 goto out_err;
1624
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001625 if (vid_range && vid < vid_range &&
1626 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1627 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1628 goto out_err;
1629
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001630 if (v_opts) {
1631 if (!br_vlan_opts_fill(skb, v_opts))
1632 goto out_err;
1633
1634 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1635 goto out_err;
1636 }
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001637
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001638 nla_nest_end(skb, nest);
1639
1640 return true;
1641
1642out_err:
1643 nla_nest_cancel(skb, nest);
1644 return false;
1645}
1646
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001647static size_t rtnl_vlan_nlmsg_size(void)
1648{
1649 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1650 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1651 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001652 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1653 + br_vlan_opts_nl_size(); /* bridge vlan options */
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001654}
1655
1656void br_vlan_notify(const struct net_bridge *br,
1657 const struct net_bridge_port *p,
1658 u16 vid, u16 vid_range,
1659 int cmd)
1660{
1661 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001662 struct net_bridge_vlan *v = NULL;
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001663 struct br_vlan_msg *bvm;
1664 struct nlmsghdr *nlh;
1665 struct sk_buff *skb;
1666 int err = -ENOBUFS;
1667 struct net *net;
1668 u16 flags = 0;
1669 int ifindex;
1670
1671 /* right now notifications are done only with rtnl held */
1672 ASSERT_RTNL();
1673
1674 if (p) {
1675 ifindex = p->dev->ifindex;
1676 vg = nbp_vlan_group(p);
1677 net = dev_net(p->dev);
1678 } else {
1679 ifindex = br->dev->ifindex;
1680 vg = br_vlan_group(br);
1681 net = dev_net(br->dev);
1682 }
1683
1684 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1685 if (!skb)
1686 goto out_err;
1687
1688 err = -EMSGSIZE;
1689 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1690 if (!nlh)
1691 goto out_err;
1692 bvm = nlmsg_data(nlh);
1693 memset(bvm, 0, sizeof(*bvm));
1694 bvm->family = AF_BRIDGE;
1695 bvm->ifindex = ifindex;
1696
1697 switch (cmd) {
1698 case RTM_NEWVLAN:
1699 /* need to find the vlan due to flags/options */
1700 v = br_vlan_find(vg, vid);
1701 if (!v || !br_vlan_should_use(v))
1702 goto out_kfree;
1703
1704 flags = v->flags;
1705 if (br_get_pvid(vg) == v->vid)
1706 flags |= BRIDGE_VLAN_INFO_PVID;
1707 break;
1708 case RTM_DELVLAN:
1709 break;
1710 default:
1711 goto out_kfree;
1712 }
1713
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001714 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001715 goto out_err;
1716
1717 nlmsg_end(skb, nlh);
1718 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1719 return;
1720
1721out_err:
1722 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1723out_kfree:
1724 kfree_skb(skb);
1725}
1726
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001727/* check if v_curr can enter a range ending in range_end */
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001728bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1729 const struct net_bridge_vlan *range_end)
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001730{
1731 return v_curr->vid - range_end->vid == 1 &&
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001732 range_end->flags == v_curr->flags &&
Nikolay Aleksandrov99f7c5e2020-03-17 14:08:33 +02001733 br_vlan_opts_eq_range(v_curr, range_end);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001734}
1735
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001736static int br_vlan_dump_dev(const struct net_device *dev,
1737 struct sk_buff *skb,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001738 struct netlink_callback *cb,
1739 u32 dump_flags)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001740{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001741 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001742 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001743 struct net_bridge_vlan_group *vg;
1744 int idx = 0, s_idx = cb->args[1];
1745 struct nlmsghdr *nlh = NULL;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001746 struct net_bridge_port *p;
1747 struct br_vlan_msg *bvm;
1748 struct net_bridge *br;
1749 int err = 0;
1750 u16 pvid;
1751
1752 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1753 return -EINVAL;
1754
1755 if (netif_is_bridge_master(dev)) {
1756 br = netdev_priv(dev);
1757 vg = br_vlan_group_rcu(br);
1758 p = NULL;
1759 } else {
1760 p = br_port_get_rcu(dev);
1761 if (WARN_ON(!p))
1762 return -EINVAL;
1763 vg = nbp_vlan_group_rcu(p);
1764 br = p->br;
1765 }
1766
1767 if (!vg)
1768 return 0;
1769
1770 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1771 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1772 if (!nlh)
1773 return -EMSGSIZE;
1774 bvm = nlmsg_data(nlh);
1775 memset(bvm, 0, sizeof(*bvm));
1776 bvm->family = PF_BRIDGE;
1777 bvm->ifindex = dev->ifindex;
1778 pvid = br_get_pvid(vg);
1779
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001780 /* idx must stay at range's beginning until it is filled in */
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001781 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
1782 if (!br_vlan_should_use(v))
1783 continue;
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001784 if (idx < s_idx) {
1785 idx++;
1786 continue;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001787 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001788
1789 if (!range_start) {
1790 range_start = v;
1791 range_end = v;
1792 continue;
1793 }
1794
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001795 if (dump_stats || v->vid == pvid ||
1796 !br_vlan_can_enter_range(v, range_end)) {
1797 u16 vlan_flags = br_vlan_flags(range_start, pvid);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001798
1799 if (!br_vlan_fill_vids(skb, range_start->vid,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001800 range_end->vid, range_start,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001801 vlan_flags, dump_stats)) {
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001802 err = -EMSGSIZE;
1803 break;
1804 }
1805 /* advance number of filled vlans */
1806 idx += range_end->vid - range_start->vid + 1;
1807
1808 range_start = v;
1809 }
1810 range_end = v;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001811 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001812
1813 /* err will be 0 and range_start will be set in 3 cases here:
1814 * - first vlan (range_start == range_end)
1815 * - last vlan (range_start == range_end, not in range)
1816 * - last vlan range (range_start != range_end, in range)
1817 */
1818 if (!err && range_start &&
1819 !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001820 range_start, br_vlan_flags(range_start, pvid),
1821 dump_stats))
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001822 err = -EMSGSIZE;
1823
1824 cb->args[1] = err ? idx : 0;
1825
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001826 nlmsg_end(skb, nlh);
1827
1828 return err;
1829}
1830
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001831static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1832 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1833};
1834
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001835static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1836{
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001837 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001838 int idx = 0, err = 0, s_idx = cb->args[0];
1839 struct net *net = sock_net(skb->sk);
1840 struct br_vlan_msg *bvm;
1841 struct net_device *dev;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001842 u32 dump_flags = 0;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001843
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001844 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
1845 br_vlan_db_dump_pol, cb->extack);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001846 if (err < 0)
1847 return err;
1848
1849 bvm = nlmsg_data(cb->nlh);
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001850 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
1851 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001852
1853 rcu_read_lock();
1854 if (bvm->ifindex) {
1855 dev = dev_get_by_index_rcu(net, bvm->ifindex);
1856 if (!dev) {
1857 err = -ENODEV;
1858 goto out_err;
1859 }
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001860 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001861 if (err && err != -EMSGSIZE)
1862 goto out_err;
1863 } else {
1864 for_each_netdev_rcu(net, dev) {
1865 if (idx < s_idx)
1866 goto skip;
1867
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001868 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001869 if (err == -EMSGSIZE)
1870 break;
1871skip:
1872 idx++;
1873 }
1874 }
1875 cb->args[0] = idx;
1876 rcu_read_unlock();
1877
1878 return skb->len;
1879
1880out_err:
1881 rcu_read_unlock();
1882
1883 return err;
1884}
1885
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001886static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
1887 [BRIDGE_VLANDB_ENTRY_INFO] = { .type = NLA_EXACT_LEN,
1888 .len = sizeof(struct bridge_vlan_info) },
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001889 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
Nikolay Aleksandrova580c762020-01-24 13:40:22 +02001890 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +02001891 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001892};
1893
1894static int br_vlan_rtm_process_one(struct net_device *dev,
1895 const struct nlattr *attr,
1896 int cmd, struct netlink_ext_ack *extack)
1897{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001898 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001899 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001900 bool changed = false, skip_processing = false;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001901 struct net_bridge_vlan_group *vg;
1902 struct net_bridge_port *p = NULL;
1903 int err = 0, cmdmap = 0;
1904 struct net_bridge *br;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001905
1906 if (netif_is_bridge_master(dev)) {
1907 br = netdev_priv(dev);
1908 vg = br_vlan_group(br);
1909 } else {
1910 p = br_port_get_rtnl(dev);
1911 if (WARN_ON(!p))
1912 return -ENODEV;
1913 br = p->br;
1914 vg = nbp_vlan_group(p);
1915 }
1916
1917 if (WARN_ON(!vg))
1918 return -ENODEV;
1919
1920 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
1921 br_vlan_db_policy, extack);
1922 if (err)
1923 return err;
1924
1925 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
1926 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
1927 return -EINVAL;
1928 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001929 memset(&vrange_end, 0, sizeof(vrange_end));
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001930
1931 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
1932 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
1933 BRIDGE_VLAN_INFO_RANGE_END)) {
1934 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
1935 return -EINVAL;
1936 }
1937 if (!br_vlan_valid_id(vinfo->vid, extack))
1938 return -EINVAL;
1939
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001940 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
1941 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
1942 /* validate user-provided flags without RANGE_BEGIN */
1943 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
1944 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
1945
1946 /* vinfo_last is the range start, vinfo the range end */
1947 vinfo_last = vinfo;
1948 vinfo = &vrange_end;
1949
1950 if (!br_vlan_valid_id(vinfo->vid, extack) ||
1951 !br_vlan_valid_range(vinfo, vinfo_last, extack))
1952 return -EINVAL;
1953 }
1954
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001955 switch (cmd) {
1956 case RTM_NEWVLAN:
1957 cmdmap = RTM_SETLINK;
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001958 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001959 break;
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02001960 case RTM_DELVLAN:
1961 cmdmap = RTM_DELLINK;
1962 break;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001963 }
1964
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001965 if (!skip_processing) {
1966 struct bridge_vlan_info *tmp_last = vinfo_last;
1967
1968 /* br_process_vlan_info may overwrite vinfo_last */
1969 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
1970 &changed, extack);
1971
1972 /* notify first if anything changed */
1973 if (changed)
1974 br_ifinfo_notify(cmdmap, br, p);
1975
1976 if (err)
1977 return err;
1978 }
1979
1980 /* deal with options */
1981 if (cmd == RTM_NEWVLAN) {
1982 struct net_bridge_vlan *range_start, *range_end;
1983
1984 if (vinfo_last) {
1985 range_start = br_vlan_find(vg, vinfo_last->vid);
1986 range_end = br_vlan_find(vg, vinfo->vid);
1987 } else {
1988 range_start = br_vlan_find(vg, vinfo->vid);
1989 range_end = range_start;
1990 }
1991
1992 err = br_vlan_process_options(br, p, range_start, range_end,
1993 tb, extack);
1994 }
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001995
1996 return err;
1997}
1998
1999static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2000 struct netlink_ext_ack *extack)
2001{
2002 struct net *net = sock_net(skb->sk);
2003 struct br_vlan_msg *bvm;
2004 struct net_device *dev;
2005 struct nlattr *attr;
2006 int err, vlans = 0;
2007 int rem;
2008
2009 /* this should validate the header and check for remaining bytes */
2010 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2011 extack);
2012 if (err < 0)
2013 return err;
2014
2015 bvm = nlmsg_data(nlh);
2016 dev = __dev_get_by_index(net, bvm->ifindex);
2017 if (!dev)
2018 return -ENODEV;
2019
2020 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2021 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2022 return -EINVAL;
2023 }
2024
2025 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2026 if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
2027 continue;
2028
2029 vlans++;
2030 err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
2031 extack);
2032 if (err)
2033 break;
2034 }
2035 if (!vlans) {
2036 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2037 err = -EINVAL;
2038 }
2039
2040 return err;
2041}
2042
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002043void br_vlan_rtnl_init(void)
2044{
2045 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2046 br_vlan_rtm_dump, 0);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002047 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2048 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002049 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2050 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002051}
2052
2053void br_vlan_rtnl_uninit(void)
2054{
2055 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002056 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002057 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002058}