blob: c4a51095850a94e4b99adf9e0b9cb7c43be2b4d6 [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
Randy Dunlap4bbd0262020-09-17 21:35:21 -0700143/* Returns a master vlan, if it didn't exist it gets created. In all cases
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200144 * 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);
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 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200285 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000286 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200287 if (err && err != -EOPNOTSUPP)
288 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900289 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000290
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200291 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200292 if (br_vlan_should_use(v)) {
293 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
294 if (err) {
295 br_err(br, "failed insert local address into bridge forwarding table\n");
296 goto out_filt;
297 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200298 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000299 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200300
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200301 /* set the state before publishing */
302 v->state = BR_STATE_FORWARDING;
303
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200304 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
305 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306 if (err)
307 goto out_fdb_insert;
308
309 __vlan_add_list(v);
310 __vlan_add_flags(v, flags);
Mike Manning80900ac2019-04-18 18:35:34 +0100311
312 if (p)
313 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200314out:
315 return err;
316
317out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200318 if (br_vlan_should_use(v)) {
319 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
320 vg->num_vlans--;
321 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200322
323out_filt:
324 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000325 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800327 if (v->stats && masterv->stats != v->stats)
328 free_percpu(v->stats);
329 v->stats = NULL;
330
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200331 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 v->brvlan = NULL;
333 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200334 } else {
335 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 }
337
338 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000339}
340
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200341static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000342{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200343 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200344 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200345 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200347
348 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200349 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 } else {
351 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200352 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200353 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 }
355
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200356 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200357 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000358 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 if (err)
360 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200361 } else {
362 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
363 if (err && err != -EOPNOTSUPP)
364 goto out;
365 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200366 }
367
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200368 if (br_vlan_should_use(v)) {
369 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
370 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200371 }
372
373 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800374 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200375 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
376 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200377 __vlan_del_list(v);
Mike Manning80900ac2019-04-18 18:35:34 +0100378 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300379 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200380 }
381
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200382 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200383out:
384 return err;
385}
386
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200387static void __vlan_group_free(struct net_bridge_vlan_group *vg)
388{
389 WARN_ON(!list_empty(&vg->vlan_list));
390 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800391 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200392 kfree(vg);
393}
394
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200395static void __vlan_flush(const struct net_bridge *br,
396 const struct net_bridge_port *p,
397 struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200398{
399 struct net_bridge_vlan *vlan, *tmp;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200400 u16 v_start = 0, v_end = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200401
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200402 __vlan_delete_pvid(vg, vg->pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200403 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
404 /* take care of disjoint ranges */
405 if (!v_start) {
406 v_start = vlan->vid;
407 } else if (vlan->vid - v_end != 1) {
408 /* found range end, notify and start next one */
409 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
410 v_start = vlan->vid;
411 }
412 v_end = vlan->vid;
413
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200414 __vlan_del(vlan);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200415 }
416
417 /* notify about the last/whole vlan range */
418 if (v_start)
419 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000420}
421
Vlad Yasevich78851982013-02-13 12:00:14 +0000422struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800423 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200424 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000425 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000426{
Heiner Kallweit281cc282020-11-17 21:25:42 +0100427 struct pcpu_sw_netstats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200428 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000429 u16 vid;
430
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400431 /* If this packet was not filtered at input, let it pass */
432 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000433 goto out;
434
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200435 /* At this point, we know that the frame was filtered and contains
436 * a valid vlan id. If the vlan id has untagged flag set,
437 * send untagged; otherwise, send tagged.
438 */
439 br_vlan_get_tag(skb, &vid);
440 v = br_vlan_find(vg, vid);
441 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400442 * only exception is the bridge is set in promisc mode and the
443 * packet is destined for the bridge device. In this case
444 * pass the packet as is.
445 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200446 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400447 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
448 goto out;
449 } else {
450 kfree_skb(skb);
451 return NULL;
452 }
453 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300454 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200455 stats = this_cpu_ptr(v->stats);
456 u64_stats_update_begin(&stats->syncp);
457 stats->tx_bytes += skb->len;
458 stats->tx_packets++;
459 u64_stats_update_end(&stats->syncp);
460 }
461
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200462 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100463 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800464
465 if (p && (p->flags & BR_VLAN_TUNNEL) &&
466 br_handle_egress_vlan_tunnel(skb, v)) {
467 kfree_skb(skb);
468 return NULL;
469 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000470out:
471 return skb;
472}
473
474/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200475static bool __allowed_ingress(const struct net_bridge *br,
476 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200477 struct sk_buff *skb, u16 *vid,
478 u8 *state)
Vlad Yasevich78851982013-02-13 12:00:14 +0000479{
Heiner Kallweit281cc282020-11-17 21:25:42 +0100480 struct pcpu_sw_netstats *stats;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200481 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900482 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000483
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400484 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900485 /* If vlan tx offload is disabled on bridge device and frame was
486 * sent from vlan device on the bridge device, it does not have
487 * HW accelerated vlan tag.
488 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100489 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200490 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400491 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900492 if (unlikely(!skb))
493 return false;
494 }
495
Toshiaki Makita8580e212014-06-10 20:59:23 +0900496 if (!br_vlan_get_tag(skb, vid)) {
497 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200498 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900499 /* Protocol-mismatch, empty out vlan_tci for new tag */
500 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100501 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100502 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900503 if (unlikely(!skb))
504 return false;
505
506 skb_pull(skb, ETH_HLEN);
507 skb_reset_mac_len(skb);
508 *vid = 0;
509 tagged = false;
510 } else {
511 tagged = true;
512 }
513 } else {
514 /* Untagged frame */
515 tagged = false;
516 }
517
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900518 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200519 u16 pvid = br_get_pvid(vg);
520
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900521 /* Frame had a tag with VID 0 or did not have a tag.
522 * See if pvid is set on this port. That tells us which
523 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000524 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400525 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900526 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000527
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900528 /* PVID is set on this port. Any untagged or priority-tagged
529 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000530 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900531 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900532 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900533 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200534 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900535 else
536 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100537 * At this point, we know that skb->vlan_tci VID
538 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900539 * We update only VID field and preserve PCP field.
540 */
541 skb->vlan_tci |= pvid;
542
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200543 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200544 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
545 if (*state == BR_STATE_FORWARDING) {
546 *state = br_vlan_get_pvid_state(vg);
547 return br_vlan_state_allowed(*state, true);
548 } else {
549 return true;
550 }
551 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200552 }
553 v = br_vlan_find(vg, *vid);
554 if (!v || !br_vlan_should_use(v))
555 goto drop;
556
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200557 if (*state == BR_STATE_FORWARDING) {
558 *state = br_vlan_get_state(v);
559 if (!br_vlan_state_allowed(*state, true))
560 goto drop;
561 }
562
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300563 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200564 stats = this_cpu_ptr(v->stats);
565 u64_stats_update_begin(&stats->syncp);
566 stats->rx_bytes += skb->len;
567 stats->rx_packets++;
568 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000569 }
570
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200571 return true;
572
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900573drop:
574 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000575 return false;
576}
577
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200578bool br_allowed_ingress(const struct net_bridge *br,
579 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200580 u16 *vid, u8 *state)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200581{
582 /* If VLAN filtering is disabled on the bridge, all packets are
583 * permitted.
584 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300585 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200586 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
587 return true;
588 }
589
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200590 return __allowed_ingress(br, vg, skb, vid, state);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200591}
592
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000593/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200594bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000595 const struct sk_buff *skb)
596{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200597 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000598 u16 vid;
599
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400600 /* If this packet was not filtered at input, let it pass */
601 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000602 return true;
603
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000604 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200605 v = br_vlan_find(vg, vid);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200606 if (v && br_vlan_should_use(v) &&
607 br_vlan_state_allowed(br_vlan_get_state(v), false))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000608 return true;
609
610 return false;
611}
612
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900613/* Called under RCU */
614bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
615{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200616 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900617 struct net_bridge *br = p->br;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200618 struct net_bridge_vlan *v;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900619
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400620 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300621 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900622 return true;
623
Ido Schimmeleca1e002015-11-01 18:31:45 +0200624 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200625 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900626 return false;
627
Toshiaki Makita8580e212014-06-10 20:59:23 +0900628 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
629 *vid = 0;
630
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900631 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200632 *vid = br_get_pvid(vg);
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200633 if (!*vid ||
634 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900635 return false;
636
637 return true;
638 }
639
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200640 v = br_vlan_find(vg, *vid);
641 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900642 return true;
643
644 return false;
645}
646
Petr Machatadbd6dc72018-05-30 02:56:55 +0200647static int br_vlan_add_existing(struct net_bridge *br,
648 struct net_bridge_vlan_group *vg,
649 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000650 u16 flags, bool *changed,
651 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200652{
653 int err;
654
Petr Machata169327d2018-12-12 17:02:50 +0000655 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200656 if (err && err != -EOPNOTSUPP)
657 return err;
658
Petr Machatadbd6dc72018-05-30 02:56:55 +0200659 if (!br_vlan_is_brentry(vlan)) {
660 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200661 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
662 err = -EINVAL;
663 goto err_flags;
664 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200665 /* It was only kept for port vlans, now make it real */
666 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
667 vlan->vid);
668 if (err) {
669 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200670 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200671 }
672
673 refcount_inc(&vlan->refcnt);
674 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
675 vg->num_vlans++;
676 *changed = true;
677 }
678
679 if (__vlan_add_flags(vlan, flags))
680 *changed = true;
681
682 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200683
684err_fdb_insert:
685err_flags:
686 br_switchdev_port_vlan_del(br->dev, vlan->vid);
687 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200688}
689
Toshiaki Makita8adff412013-10-16 17:07:13 +0900690/* Must be protected by RTNL.
691 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300692 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900693 */
Petr Machata169327d2018-12-12 17:02:50 +0000694int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
695 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000696{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200697 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200698 struct net_bridge_vlan *vlan;
699 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000700
701 ASSERT_RTNL();
702
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300703 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200704 vg = br_vlan_group(br);
705 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200706 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000707 return br_vlan_add_existing(br, vg, vlan, flags, changed,
708 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000709
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200710 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
711 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000712 return -ENOMEM;
713
Heiner Kallweit281cc282020-11-17 21:25:42 +0100714 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200715 if (!vlan->stats) {
716 kfree(vlan);
717 return -ENOMEM;
718 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200719 vlan->vid = vid;
720 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
721 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
722 vlan->br = br;
723 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300724 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000725 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200726 if (ret) {
727 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200728 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300729 } else {
730 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200731 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000732
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200733 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000734}
735
Toshiaki Makita8adff412013-10-16 17:07:13 +0900736/* Must be protected by RTNL.
737 * Must be called with vid in range from 1 to 4094 inclusive.
738 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000739int br_vlan_delete(struct net_bridge *br, u16 vid)
740{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200741 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200742 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000743
744 ASSERT_RTNL();
745
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200746 vg = br_vlan_group(br);
747 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200748 if (!v || !br_vlan_is_brentry(v))
749 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000750
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900751 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700752 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000753
Roopa Prabhuefa53562017-01-31 22:59:54 -0800754 vlan_tunnel_info_del(vg, v);
755
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200756 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000757}
758
759void br_vlan_flush(struct net_bridge *br)
760{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200761 struct net_bridge_vlan_group *vg;
762
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000763 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000764
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200765 vg = br_vlan_group(br);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200766 __vlan_flush(br, NULL, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200767 RCU_INIT_POINTER(br->vlgrp, NULL);
768 synchronize_rcu();
769 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000770}
771
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200772struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900773{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200774 if (!vg)
775 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900776
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200777 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900778}
779
Toshiaki Makita204177f2014-06-10 20:59:25 +0900780/* Must be protected by RTNL. */
781static void recalculate_group_addr(struct net_bridge *br)
782{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300783 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900784 return;
785
786 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300787 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
788 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900789 /* Bridge Group Address */
790 br->group_addr[5] = 0x00;
791 } else { /* vlan_enabled && ETH_P_8021AD */
792 /* Provider Bridge Group Address */
793 br->group_addr[5] = 0x08;
794 }
795 spin_unlock_bh(&br->lock);
796}
797
798/* Must be protected by RTNL. */
799void br_recalculate_fwd_mask(struct net_bridge *br)
800{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300801 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
802 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900803 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
804 else /* vlan_enabled && ETH_P_8021AD */
805 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
806 ~(1u << br->group_addr[5]);
807}
808
Vladimir Oltean9e781402021-02-13 22:43:16 +0200809int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
810 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000811{
Elad Raz6b72a772016-01-06 13:01:06 +0100812 struct switchdev_attr attr = {
813 .orig_dev = br->dev,
814 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
815 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
816 .u.vlan_filtering = val,
817 };
818 int err;
819
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300820 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300821 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000822
Elad Raz6b72a772016-01-06 13:01:06 +0100823 err = switchdev_port_attr_set(br->dev, &attr);
824 if (err && err != -EOPNOTSUPP)
825 return err;
826
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300827 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400828 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900829 recalculate_group_addr(br);
830 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000831
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300832 return 0;
833}
834
Ido Schimmel1f514452017-05-26 08:37:23 +0200835bool br_vlan_enabled(const struct net_device *dev)
836{
837 struct net_bridge *br = netdev_priv(dev);
838
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300839 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200840}
841EXPORT_SYMBOL_GPL(br_vlan_enabled);
842
wenxu31aed462019-07-05 21:16:36 +0800843int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
844{
845 struct net_bridge *br = netdev_priv(dev);
846
847 *p_proto = ntohs(br->vlan_proto);
848
849 return 0;
850}
851EXPORT_SYMBOL_GPL(br_vlan_get_proto);
852
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900853int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900854{
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200855 struct switchdev_attr attr = {
856 .orig_dev = br->dev,
857 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
858 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
859 .u.vlan_protocol = ntohs(proto),
860 };
Toshiaki Makita204177f2014-06-10 20:59:25 +0900861 int err = 0;
862 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200863 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200864 struct net_bridge_vlan_group *vg;
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200865 __be16 oldproto = br->vlan_proto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900866
Toshiaki Makita204177f2014-06-10 20:59:25 +0900867 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900868 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900869
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200870 err = switchdev_port_attr_set(br->dev, &attr);
871 if (err && err != -EOPNOTSUPP)
872 return err;
873
Toshiaki Makita204177f2014-06-10 20:59:25 +0900874 /* Add VLANs for the new proto to the device filter. */
875 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200876 vg = nbp_vlan_group(p);
877 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200878 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900879 if (err)
880 goto err_filt;
881 }
882 }
883
Toshiaki Makita204177f2014-06-10 20:59:25 +0900884 br->vlan_proto = proto;
885
886 recalculate_group_addr(br);
887 br_recalculate_fwd_mask(br);
888
889 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200890 list_for_each_entry(p, &br->port_list, list) {
891 vg = nbp_vlan_group(p);
892 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200893 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200894 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900895
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900896 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900897
898err_filt:
Danielle Ratson22ec19f32020-11-29 14:54:05 +0200899 attr.u.vlan_protocol = ntohs(oldproto);
900 switchdev_port_attr_set(br->dev, &attr);
901
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200902 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200903 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900904
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200905 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
906 vg = nbp_vlan_group(p);
907 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200908 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200909 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900910
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900911 return err;
912}
913
Vladimir Oltean9e781402021-02-13 22:43:16 +0200914int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
915 struct netlink_ext_ack *extack)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900916{
Menglong Donga98c0c42021-01-17 16:09:50 +0800917 if (!eth_type_vlan(htons(val)))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900918 return -EPROTONOSUPPORT;
919
Xin Long047831a2016-04-09 00:03:31 +0800920 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900921}
922
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200923int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
924{
925 switch (val) {
926 case 0:
927 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300928 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200929 break;
930 default:
931 return -EINVAL;
932 }
933
934 return 0;
935}
936
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300937int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
938{
939 struct net_bridge_port *p;
940
941 /* allow to change the option if there are no port vlans configured */
942 list_for_each_entry(p, &br->port_list, list) {
943 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
944
945 if (vg->num_vlans)
946 return -EBUSY;
947 }
948
949 switch (val) {
950 case 0:
951 case 1:
952 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
953 break;
954 default:
955 return -EINVAL;
956 }
957
958 return 0;
959}
960
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200961static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400962{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200963 struct net_bridge_vlan *v;
964
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200965 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200966 return false;
967
968 v = br_vlan_lookup(&vg->vlan_hash, vid);
969 if (v && br_vlan_should_use(v) &&
970 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
971 return true;
972
973 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400974}
975
976static void br_vlan_disable_default_pvid(struct net_bridge *br)
977{
978 struct net_bridge_port *p;
979 u16 pvid = br->default_pvid;
980
981 /* Disable default_pvid on all ports where it is still
982 * configured.
983 */
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200984 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
985 if (!br_vlan_delete(br, pvid))
986 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
987 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400988
989 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200990 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
991 !nbp_vlan_delete(p, pvid))
992 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400993 }
994
995 br->default_pvid = 0;
996}
997
Petr Machata169327d2018-12-12 17:02:50 +0000998int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
999 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001000{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001001 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001002 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001003 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001004 unsigned long *changed;
1005 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001006 u16 old_pvid;
1007 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001008
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001009 if (!pvid) {
1010 br_vlan_disable_default_pvid(br);
1011 return 0;
1012 }
1013
Andy Shevchenko459479d2018-08-30 13:33:18 +03001014 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001015 if (!changed)
1016 return -ENOMEM;
1017
1018 old_pvid = br->default_pvid;
1019
1020 /* Update default_pvid config only if we do not conflict with
1021 * user configuration.
1022 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001023 vg = br_vlan_group(br);
1024 pvent = br_vlan_find(vg, pvid);
1025 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001026 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001027 err = br_vlan_add(br, pvid,
1028 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001029 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001030 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001031 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001032 if (err)
1033 goto out;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001034
1035 if (br_vlan_delete(br, old_pvid))
1036 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1037 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001038 set_bit(0, changed);
1039 }
1040
1041 list_for_each_entry(p, &br->port_list, list) {
1042 /* Update default_pvid config only if we do not conflict with
1043 * user configuration.
1044 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001045 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001046 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001047 !vlan_default_pvid(vg, old_pvid)) ||
1048 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001049 continue;
1050
1051 err = nbp_vlan_add(p, pvid,
1052 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001053 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001054 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001055 if (err)
1056 goto err_port;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001057 if (nbp_vlan_delete(p, old_pvid))
1058 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1059 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001060 set_bit(p->port_no, changed);
1061 }
1062
1063 br->default_pvid = pvid;
1064
1065out:
Andy Shevchenko459479d2018-08-30 13:33:18 +03001066 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001067 return err;
1068
1069err_port:
1070 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1071 if (!test_bit(p->port_no, changed))
1072 continue;
1073
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001074 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001075 nbp_vlan_add(p, old_pvid,
1076 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001077 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001078 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001079 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1080 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001081 nbp_vlan_delete(p, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001082 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001083 }
1084
1085 if (test_bit(0, changed)) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001086 if (old_pvid) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001087 br_vlan_add(br, old_pvid,
1088 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001089 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001090 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001091 &vlchange, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001092 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1093 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001094 br_vlan_delete(br, pvid);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001095 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001096 }
1097 goto out;
1098}
1099
Vladimir Oltean9e781402021-02-13 22:43:16 +02001100int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1101 struct netlink_ext_ack *extack)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001102{
1103 u16 pvid = val;
1104 int err = 0;
1105
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001106 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001107 return -EINVAL;
1108
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001109 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001110 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001111
1112 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001113 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001114 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1115 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001116 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001117 }
Vladimir Oltean9e781402021-02-13 22:43:16 +02001118 err = __br_vlan_set_default_pvid(br, pvid, extack);
Xin Long047831a2016-04-09 00:03:31 +08001119out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001120 return err;
1121}
1122
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001123int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001124{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001125 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001126 int ret = -ENOMEM;
1127
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001128 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1129 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001130 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001131 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001132 if (ret)
1133 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001134 ret = vlan_tunnel_init(vg);
1135 if (ret)
1136 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001137 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001138 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001139 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001140 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001141
1142out:
1143 return ret;
1144
Roopa Prabhuefa53562017-01-31 22:59:54 -08001145err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001146 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001147err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001148 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001149
1150 goto out;
1151}
1152
Petr Machata169327d2018-12-12 17:02:50 +00001153int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001154{
Elad Raz404cdbf2016-01-06 13:01:07 +01001155 struct switchdev_attr attr = {
1156 .orig_dev = p->br->dev,
1157 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1158 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001159 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001160 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001161 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001162 int ret = -ENOMEM;
1163
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001164 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1165 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001166 goto out;
1167
Elad Raz404cdbf2016-01-06 13:01:07 +01001168 ret = switchdev_port_attr_set(p->dev, &attr);
1169 if (ret && ret != -EOPNOTSUPP)
1170 goto err_vlan_enabled;
1171
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001172 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001173 if (ret)
1174 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001175 ret = vlan_tunnel_init(vg);
1176 if (ret)
1177 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001178 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001179 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001180 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001181 bool changed;
1182
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001183 ret = nbp_vlan_add(p, p->br->default_pvid,
1184 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001185 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001186 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001187 if (ret)
1188 goto err_vlan_add;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001189 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001190 }
1191out:
1192 return ret;
1193
1194err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001195 RCU_INIT_POINTER(p->vlgrp, NULL);
1196 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001197 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001198err_tunnel_init:
1199 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001200err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001201err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001202 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001203
1204 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001205}
1206
Toshiaki Makita8adff412013-10-16 17:07:13 +09001207/* Must be protected by RTNL.
1208 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001209 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001210 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001211int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001212 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001213{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001214 struct net_bridge_vlan *vlan;
1215 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001216
1217 ASSERT_RTNL();
1218
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001219 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001220 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001221 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001222 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001223 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001224 if (ret && ret != -EOPNOTSUPP)
1225 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001226 *changed = __vlan_add_flags(vlan, flags);
1227
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001228 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001229 }
1230
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001231 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1232 if (!vlan)
1233 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001234
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001235 vlan->vid = vid;
1236 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001237 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001238 if (ret)
1239 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001240 else
1241 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001242
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001243 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001244}
1245
Toshiaki Makita8adff412013-10-16 17:07:13 +09001246/* Must be protected by RTNL.
1247 * Must be called with vid in range from 1 to 4094 inclusive.
1248 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001249int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1250{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001251 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001252
1253 ASSERT_RTNL();
1254
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001255 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001256 if (!v)
1257 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001258 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001259 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001260
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001261 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001262}
1263
1264void nbp_vlan_flush(struct net_bridge_port *port)
1265{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001266 struct net_bridge_vlan_group *vg;
1267
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001268 ASSERT_RTNL();
1269
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001270 vg = nbp_vlan_group(port);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001271 __vlan_flush(port->br, port, vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001272 RCU_INIT_POINTER(port->vlgrp, NULL);
1273 synchronize_rcu();
1274 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001275}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001276
1277void br_vlan_get_stats(const struct net_bridge_vlan *v,
Heiner Kallweit281cc282020-11-17 21:25:42 +01001278 struct pcpu_sw_netstats *stats)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001279{
1280 int i;
1281
1282 memset(stats, 0, sizeof(*stats));
1283 for_each_possible_cpu(i) {
1284 u64 rxpackets, rxbytes, txpackets, txbytes;
Heiner Kallweit281cc282020-11-17 21:25:42 +01001285 struct pcpu_sw_netstats *cpu_stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001286 unsigned int start;
1287
1288 cpu_stats = per_cpu_ptr(v->stats, i);
1289 do {
1290 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1291 rxpackets = cpu_stats->rx_packets;
1292 rxbytes = cpu_stats->rx_bytes;
1293 txbytes = cpu_stats->tx_bytes;
1294 txpackets = cpu_stats->tx_packets;
1295 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1296
1297 stats->rx_packets += rxpackets;
1298 stats->rx_bytes += rxbytes;
1299 stats->tx_bytes += txbytes;
1300 stats->tx_packets += txpackets;
1301 }
1302}
Petr Machata4d4fd362018-04-29 10:56:08 +03001303
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001304int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
Petr Machata4d4fd362018-04-29 10:56:08 +03001305{
1306 struct net_bridge_vlan_group *vg;
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001307 struct net_bridge_port *p;
Petr Machata4d4fd362018-04-29 10:56:08 +03001308
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001309 ASSERT_RTNL();
1310 p = br_port_get_check_rtnl(dev);
Ido Schimmel5a6db042018-11-28 20:06:58 +00001311 if (p)
1312 vg = nbp_vlan_group(p);
1313 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001314 vg = br_vlan_group(netdev_priv(dev));
1315 else
1316 return -EINVAL;
1317
1318 *p_pvid = br_get_pvid(vg);
1319 return 0;
1320}
1321EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1322
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001323int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1324{
Vladimir Oltean99f62a72020-09-22 01:07:09 +03001325 struct net_bridge_vlan_group *vg;
1326 struct net_bridge_port *p;
1327
1328 p = br_port_get_check_rcu(dev);
1329 if (p)
1330 vg = nbp_vlan_group_rcu(p);
1331 else if (netif_is_bridge_master(dev))
1332 vg = br_vlan_group_rcu(netdev_priv(dev));
1333 else
1334 return -EINVAL;
1335
1336 *p_pvid = br_get_pvid(vg);
1337 return 0;
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001338}
1339EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1340
Petr Machata4d4fd362018-04-29 10:56:08 +03001341int br_vlan_get_info(const struct net_device *dev, u16 vid,
1342 struct bridge_vlan_info *p_vinfo)
1343{
1344 struct net_bridge_vlan_group *vg;
1345 struct net_bridge_vlan *v;
1346 struct net_bridge_port *p;
1347
1348 ASSERT_RTNL();
1349 p = br_port_get_check_rtnl(dev);
1350 if (p)
1351 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001352 else if (netif_is_bridge_master(dev))
1353 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001354 else
1355 return -EINVAL;
1356
1357 v = br_vlan_find(vg, vid);
1358 if (!v)
1359 return -ENOENT;
1360
1361 p_vinfo->vid = vid;
1362 p_vinfo->flags = v->flags;
Vladimir Olteanf40d9b202019-08-30 03:53:24 +03001363 if (vid == br_get_pvid(vg))
1364 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
Petr Machata4d4fd362018-04-29 10:56:08 +03001365 return 0;
1366}
1367EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001368
1369static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1370{
1371 return is_vlan_dev(dev) &&
1372 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1373}
1374
1375static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001376 __always_unused struct netdev_nested_priv *priv)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001377{
1378 return br_vlan_is_bind_vlan_dev(dev);
1379}
1380
1381static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1382{
1383 int found;
1384
1385 rcu_read_lock();
1386 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1387 NULL);
1388 rcu_read_unlock();
1389
1390 return !!found;
1391}
1392
1393struct br_vlan_bind_walk_data {
1394 u16 vid;
1395 struct net_device *result;
1396};
1397
1398static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001399 struct netdev_nested_priv *priv)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001400{
Taehee Yooeff74232020-09-25 18:13:12 +00001401 struct br_vlan_bind_walk_data *data = priv->data;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001402 int found = 0;
1403
1404 if (br_vlan_is_bind_vlan_dev(dev) &&
1405 vlan_dev_priv(dev)->vlan_id == data->vid) {
1406 data->result = dev;
1407 found = 1;
1408 }
1409
1410 return found;
1411}
1412
1413static struct net_device *
1414br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1415{
1416 struct br_vlan_bind_walk_data data = {
1417 .vid = vid,
1418 };
Taehee Yooeff74232020-09-25 18:13:12 +00001419 struct netdev_nested_priv priv = {
1420 .data = (void *)&data,
1421 };
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001422
1423 rcu_read_lock();
1424 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
Taehee Yooeff74232020-09-25 18:13:12 +00001425 &priv);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001426 rcu_read_unlock();
1427
1428 return data.result;
1429}
1430
1431static bool br_vlan_is_dev_up(const struct net_device *dev)
1432{
1433 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1434}
1435
1436static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1437 struct net_device *vlan_dev)
1438{
1439 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1440 struct net_bridge_vlan_group *vg;
1441 struct net_bridge_port *p;
1442 bool has_carrier = false;
1443
Mike Manning8e1acd42019-04-18 18:35:35 +01001444 if (!netif_carrier_ok(br->dev)) {
1445 netif_carrier_off(vlan_dev);
1446 return;
1447 }
1448
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001449 list_for_each_entry(p, &br->port_list, list) {
1450 vg = nbp_vlan_group(p);
1451 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1452 has_carrier = true;
1453 break;
1454 }
1455 }
1456
1457 if (has_carrier)
1458 netif_carrier_on(vlan_dev);
1459 else
1460 netif_carrier_off(vlan_dev);
1461}
1462
1463static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1464{
1465 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1466 struct net_bridge_vlan *vlan;
1467 struct net_device *vlan_dev;
1468
1469 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1470 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1471 vlan->vid);
1472 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001473 if (br_vlan_is_dev_up(p->dev)) {
1474 if (netif_carrier_ok(p->br->dev))
1475 netif_carrier_on(vlan_dev);
1476 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001477 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001478 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001479 }
1480 }
1481}
1482
1483static void br_vlan_upper_change(struct net_device *dev,
1484 struct net_device *upper_dev,
1485 bool linking)
1486{
1487 struct net_bridge *br = netdev_priv(dev);
1488
1489 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1490 return;
1491
1492 if (linking) {
1493 br_vlan_set_vlan_dev_state(br, upper_dev);
1494 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1495 } else {
1496 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1497 br_vlan_has_upper_bind_vlan_dev(dev));
1498 }
1499}
1500
Mike Manning8e1acd42019-04-18 18:35:35 +01001501struct br_vlan_link_state_walk_data {
1502 struct net_bridge *br;
1503};
1504
1505static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
Taehee Yooeff74232020-09-25 18:13:12 +00001506 struct netdev_nested_priv *priv)
Mike Manning8e1acd42019-04-18 18:35:35 +01001507{
Taehee Yooeff74232020-09-25 18:13:12 +00001508 struct br_vlan_link_state_walk_data *data = priv->data;
Mike Manning8e1acd42019-04-18 18:35:35 +01001509
1510 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1511 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1512
1513 return 0;
1514}
1515
1516static void br_vlan_link_state_change(struct net_device *dev,
1517 struct net_bridge *br)
1518{
1519 struct br_vlan_link_state_walk_data data = {
1520 .br = br
1521 };
Taehee Yooeff74232020-09-25 18:13:12 +00001522 struct netdev_nested_priv priv = {
1523 .data = (void *)&data,
1524 };
Mike Manning8e1acd42019-04-18 18:35:35 +01001525
1526 rcu_read_lock();
1527 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
Taehee Yooeff74232020-09-25 18:13:12 +00001528 &priv);
Mike Manning8e1acd42019-04-18 18:35:35 +01001529 rcu_read_unlock();
1530}
1531
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001532/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001533static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1534{
1535 struct net_device *vlan_dev;
1536
1537 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1538 return;
1539
1540 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1541 if (vlan_dev)
1542 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1543}
1544
1545/* Must be protected by RTNL. */
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001546int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001547{
1548 struct netdev_notifier_changeupper_info *info;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001549 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001550 int vlcmd = 0, ret = 0;
1551 bool changed = false;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001552
1553 switch (event) {
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001554 case NETDEV_REGISTER:
1555 ret = br_vlan_add(br, br->default_pvid,
1556 BRIDGE_VLAN_INFO_PVID |
1557 BRIDGE_VLAN_INFO_UNTAGGED |
1558 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001559 vlcmd = RTM_NEWVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001560 break;
1561 case NETDEV_UNREGISTER:
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001562 changed = !br_vlan_delete(br, br->default_pvid);
1563 vlcmd = RTM_DELVLAN;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001564 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001565 case NETDEV_CHANGEUPPER:
1566 info = ptr;
1567 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1568 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001569
1570 case NETDEV_CHANGE:
1571 case NETDEV_UP:
Mike Manning8e1acd42019-04-18 18:35:35 +01001572 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001573 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001574 br_vlan_link_state_change(dev, br);
1575 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001576 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +02001577 if (changed)
1578 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001579
1580 return ret;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001581}
1582
1583/* Must be protected by RTNL. */
1584void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1585{
1586 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1587 return;
1588
1589 switch (event) {
1590 case NETDEV_CHANGE:
1591 case NETDEV_DOWN:
1592 case NETDEV_UP:
1593 br_vlan_set_all_vlan_dev_state(p);
1594 break;
1595 }
1596}
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001597
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001598static bool br_vlan_stats_fill(struct sk_buff *skb,
1599 const struct net_bridge_vlan *v)
1600{
Heiner Kallweit281cc282020-11-17 21:25:42 +01001601 struct pcpu_sw_netstats stats;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001602 struct nlattr *nest;
1603
1604 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1605 if (!nest)
1606 return false;
1607
1608 br_vlan_get_stats(v, &stats);
1609 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1610 BRIDGE_VLANDB_STATS_PAD) ||
1611 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1612 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1613 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1614 BRIDGE_VLANDB_STATS_PAD) ||
1615 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1616 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1617 goto out_err;
1618
1619 nla_nest_end(skb, nest);
1620
1621 return true;
1622
1623out_err:
1624 nla_nest_cancel(skb, nest);
1625 return false;
1626}
1627
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001628/* v_opts is used to dump the options which must be equal in the whole range */
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001629static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001630 const struct net_bridge_vlan *v_opts,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001631 u16 flags,
1632 bool dump_stats)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001633{
1634 struct bridge_vlan_info info;
1635 struct nlattr *nest;
1636
1637 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1638 if (!nest)
1639 return false;
1640
1641 memset(&info, 0, sizeof(info));
1642 info.vid = vid;
1643 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1644 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1645 if (flags & BRIDGE_VLAN_INFO_PVID)
1646 info.flags |= BRIDGE_VLAN_INFO_PVID;
1647
1648 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1649 goto out_err;
1650
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001651 if (vid_range && vid < vid_range &&
1652 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1653 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1654 goto out_err;
1655
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001656 if (v_opts) {
1657 if (!br_vlan_opts_fill(skb, v_opts))
1658 goto out_err;
1659
1660 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1661 goto out_err;
1662 }
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001663
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001664 nla_nest_end(skb, nest);
1665
1666 return true;
1667
1668out_err:
1669 nla_nest_cancel(skb, nest);
1670 return false;
1671}
1672
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001673static size_t rtnl_vlan_nlmsg_size(void)
1674{
1675 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1676 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1677 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001678 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1679 + br_vlan_opts_nl_size(); /* bridge vlan options */
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001680}
1681
1682void br_vlan_notify(const struct net_bridge *br,
1683 const struct net_bridge_port *p,
1684 u16 vid, u16 vid_range,
1685 int cmd)
1686{
1687 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001688 struct net_bridge_vlan *v = NULL;
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001689 struct br_vlan_msg *bvm;
1690 struct nlmsghdr *nlh;
1691 struct sk_buff *skb;
1692 int err = -ENOBUFS;
1693 struct net *net;
1694 u16 flags = 0;
1695 int ifindex;
1696
1697 /* right now notifications are done only with rtnl held */
1698 ASSERT_RTNL();
1699
1700 if (p) {
1701 ifindex = p->dev->ifindex;
1702 vg = nbp_vlan_group(p);
1703 net = dev_net(p->dev);
1704 } else {
1705 ifindex = br->dev->ifindex;
1706 vg = br_vlan_group(br);
1707 net = dev_net(br->dev);
1708 }
1709
1710 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1711 if (!skb)
1712 goto out_err;
1713
1714 err = -EMSGSIZE;
1715 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1716 if (!nlh)
1717 goto out_err;
1718 bvm = nlmsg_data(nlh);
1719 memset(bvm, 0, sizeof(*bvm));
1720 bvm->family = AF_BRIDGE;
1721 bvm->ifindex = ifindex;
1722
1723 switch (cmd) {
1724 case RTM_NEWVLAN:
1725 /* need to find the vlan due to flags/options */
1726 v = br_vlan_find(vg, vid);
1727 if (!v || !br_vlan_should_use(v))
1728 goto out_kfree;
1729
1730 flags = v->flags;
1731 if (br_get_pvid(vg) == v->vid)
1732 flags |= BRIDGE_VLAN_INFO_PVID;
1733 break;
1734 case RTM_DELVLAN:
1735 break;
1736 default:
1737 goto out_kfree;
1738 }
1739
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001740 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
Nikolay Aleksandrovcf5bddb2020-01-14 19:56:13 +02001741 goto out_err;
1742
1743 nlmsg_end(skb, nlh);
1744 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1745 return;
1746
1747out_err:
1748 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1749out_kfree:
1750 kfree_skb(skb);
1751}
1752
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001753/* check if v_curr can enter a range ending in range_end */
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001754bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1755 const struct net_bridge_vlan *range_end)
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001756{
1757 return v_curr->vid - range_end->vid == 1 &&
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001758 range_end->flags == v_curr->flags &&
Nikolay Aleksandrov99f7c5e2020-03-17 14:08:33 +02001759 br_vlan_opts_eq_range(v_curr, range_end);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001760}
1761
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001762static int br_vlan_dump_dev(const struct net_device *dev,
1763 struct sk_buff *skb,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001764 struct netlink_callback *cb,
1765 u32 dump_flags)
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001766{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001767 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001768 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001769 struct net_bridge_vlan_group *vg;
1770 int idx = 0, s_idx = cb->args[1];
1771 struct nlmsghdr *nlh = NULL;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001772 struct net_bridge_port *p;
1773 struct br_vlan_msg *bvm;
1774 struct net_bridge *br;
1775 int err = 0;
1776 u16 pvid;
1777
1778 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1779 return -EINVAL;
1780
1781 if (netif_is_bridge_master(dev)) {
1782 br = netdev_priv(dev);
1783 vg = br_vlan_group_rcu(br);
1784 p = NULL;
1785 } else {
1786 p = br_port_get_rcu(dev);
1787 if (WARN_ON(!p))
1788 return -EINVAL;
1789 vg = nbp_vlan_group_rcu(p);
1790 br = p->br;
1791 }
1792
1793 if (!vg)
1794 return 0;
1795
1796 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1797 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1798 if (!nlh)
1799 return -EMSGSIZE;
1800 bvm = nlmsg_data(nlh);
1801 memset(bvm, 0, sizeof(*bvm));
1802 bvm->family = PF_BRIDGE;
1803 bvm->ifindex = dev->ifindex;
1804 pvid = br_get_pvid(vg);
1805
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001806 /* idx must stay at range's beginning until it is filled in */
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001807 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
1808 if (!br_vlan_should_use(v))
1809 continue;
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001810 if (idx < s_idx) {
1811 idx++;
1812 continue;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001813 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001814
1815 if (!range_start) {
1816 range_start = v;
1817 range_end = v;
1818 continue;
1819 }
1820
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001821 if (dump_stats || v->vid == pvid ||
1822 !br_vlan_can_enter_range(v, range_end)) {
1823 u16 vlan_flags = br_vlan_flags(range_start, pvid);
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001824
1825 if (!br_vlan_fill_vids(skb, range_start->vid,
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001826 range_end->vid, range_start,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001827 vlan_flags, dump_stats)) {
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001828 err = -EMSGSIZE;
1829 break;
1830 }
1831 /* advance number of filled vlans */
1832 idx += range_end->vid - range_start->vid + 1;
1833
1834 range_start = v;
1835 }
1836 range_end = v;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001837 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001838
1839 /* err will be 0 and range_start will be set in 3 cases here:
1840 * - first vlan (range_start == range_end)
1841 * - last vlan (range_start == range_end, not in range)
1842 * - last vlan range (range_start != range_end, in range)
1843 */
1844 if (!err && range_start &&
1845 !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001846 range_start, br_vlan_flags(range_start, pvid),
1847 dump_stats))
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001848 err = -EMSGSIZE;
1849
1850 cb->args[1] = err ? idx : 0;
1851
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001852 nlmsg_end(skb, nlh);
1853
1854 return err;
1855}
1856
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001857static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1858 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1859};
1860
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001861static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1862{
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001863 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001864 int idx = 0, err = 0, s_idx = cb->args[0];
1865 struct net *net = sock_net(skb->sk);
1866 struct br_vlan_msg *bvm;
1867 struct net_device *dev;
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001868 u32 dump_flags = 0;
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001869
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001870 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
1871 br_vlan_db_dump_pol, cb->extack);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001872 if (err < 0)
1873 return err;
1874
1875 bvm = nlmsg_data(cb->nlh);
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001876 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
1877 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001878
1879 rcu_read_lock();
1880 if (bvm->ifindex) {
1881 dev = dev_get_by_index_rcu(net, bvm->ifindex);
1882 if (!dev) {
1883 err = -ENODEV;
1884 goto out_err;
1885 }
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001886 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001887 if (err && err != -EMSGSIZE)
1888 goto out_err;
1889 } else {
1890 for_each_netdev_rcu(net, dev) {
1891 if (idx < s_idx)
1892 goto skip;
1893
Nikolay Aleksandrov56d09972020-03-19 12:14:14 +02001894 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001895 if (err == -EMSGSIZE)
1896 break;
1897skip:
1898 idx++;
1899 }
1900 }
1901 cb->args[0] = idx;
1902 rcu_read_unlock();
1903
1904 return skb->len;
1905
1906out_err:
1907 rcu_read_unlock();
1908
1909 return err;
1910}
1911
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001912static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
Johannes Berg81408602020-08-18 10:17:31 +02001913 [BRIDGE_VLANDB_ENTRY_INFO] =
1914 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001915 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
Nikolay Aleksandrova580c762020-01-24 13:40:22 +02001916 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +02001917 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001918};
1919
1920static int br_vlan_rtm_process_one(struct net_device *dev,
1921 const struct nlattr *attr,
1922 int cmd, struct netlink_ext_ack *extack)
1923{
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001924 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001925 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001926 bool changed = false, skip_processing = false;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001927 struct net_bridge_vlan_group *vg;
1928 struct net_bridge_port *p = NULL;
1929 int err = 0, cmdmap = 0;
1930 struct net_bridge *br;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001931
1932 if (netif_is_bridge_master(dev)) {
1933 br = netdev_priv(dev);
1934 vg = br_vlan_group(br);
1935 } else {
1936 p = br_port_get_rtnl(dev);
1937 if (WARN_ON(!p))
1938 return -ENODEV;
1939 br = p->br;
1940 vg = nbp_vlan_group(p);
1941 }
1942
1943 if (WARN_ON(!vg))
1944 return -ENODEV;
1945
1946 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
1947 br_vlan_db_policy, extack);
1948 if (err)
1949 return err;
1950
1951 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
1952 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
1953 return -EINVAL;
1954 }
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001955 memset(&vrange_end, 0, sizeof(vrange_end));
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001956
1957 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
1958 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
1959 BRIDGE_VLAN_INFO_RANGE_END)) {
1960 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
1961 return -EINVAL;
1962 }
1963 if (!br_vlan_valid_id(vinfo->vid, extack))
1964 return -EINVAL;
1965
Nikolay Aleksandrov0ab55872020-01-14 19:56:12 +02001966 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
1967 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
1968 /* validate user-provided flags without RANGE_BEGIN */
1969 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
1970 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
1971
1972 /* vinfo_last is the range start, vinfo the range end */
1973 vinfo_last = vinfo;
1974 vinfo = &vrange_end;
1975
1976 if (!br_vlan_valid_id(vinfo->vid, extack) ||
1977 !br_vlan_valid_range(vinfo, vinfo_last, extack))
1978 return -EINVAL;
1979 }
1980
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001981 switch (cmd) {
1982 case RTM_NEWVLAN:
1983 cmdmap = RTM_SETLINK;
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001984 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001985 break;
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02001986 case RTM_DELVLAN:
1987 cmdmap = RTM_DELLINK;
1988 break;
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02001989 }
1990
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +02001991 if (!skip_processing) {
1992 struct bridge_vlan_info *tmp_last = vinfo_last;
1993
1994 /* br_process_vlan_info may overwrite vinfo_last */
1995 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
1996 &changed, extack);
1997
1998 /* notify first if anything changed */
1999 if (changed)
2000 br_ifinfo_notify(cmdmap, br, p);
2001
2002 if (err)
2003 return err;
2004 }
2005
2006 /* deal with options */
2007 if (cmd == RTM_NEWVLAN) {
2008 struct net_bridge_vlan *range_start, *range_end;
2009
2010 if (vinfo_last) {
2011 range_start = br_vlan_find(vg, vinfo_last->vid);
2012 range_end = br_vlan_find(vg, vinfo->vid);
2013 } else {
2014 range_start = br_vlan_find(vg, vinfo->vid);
2015 range_end = range_start;
2016 }
2017
2018 err = br_vlan_process_options(br, p, range_start, range_end,
2019 tb, extack);
2020 }
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002021
2022 return err;
2023}
2024
2025static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2026 struct netlink_ext_ack *extack)
2027{
2028 struct net *net = sock_net(skb->sk);
2029 struct br_vlan_msg *bvm;
2030 struct net_device *dev;
2031 struct nlattr *attr;
2032 int err, vlans = 0;
2033 int rem;
2034
2035 /* this should validate the header and check for remaining bytes */
2036 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2037 extack);
2038 if (err < 0)
2039 return err;
2040
2041 bvm = nlmsg_data(nlh);
2042 dev = __dev_get_by_index(net, bvm->ifindex);
2043 if (!dev)
2044 return -ENODEV;
2045
2046 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2047 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2048 return -EINVAL;
2049 }
2050
2051 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2052 if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
2053 continue;
2054
2055 vlans++;
2056 err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
2057 extack);
2058 if (err)
2059 break;
2060 }
2061 if (!vlans) {
2062 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2063 err = -EINVAL;
2064 }
2065
2066 return err;
2067}
2068
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002069void br_vlan_rtnl_init(void)
2070{
2071 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2072 br_vlan_rtm_dump, 0);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002073 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2074 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002075 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2076 br_vlan_rtm_process, NULL, 0);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002077}
2078
2079void br_vlan_rtnl_uninit(void)
2080{
2081 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +02002082 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
Nikolay Aleksandrovadb3ce92020-01-14 19:56:11 +02002083 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02002084}