blob: bb98984cd27d0edb68243f8ee6a027dfddc0861d [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 Aleksandrovf418af62017-10-27 13:19:37 +030037static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020038{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020039 if (vg->pvid == vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030040 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000041
42 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020043 vg->pvid = vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030044
45 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000046}
47
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030048static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000049{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020050 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030051 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000052
53 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020054 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030055
56 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000057}
58
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030059/* return true if anything changed, false otherwise */
60static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000061{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020062 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030063 u16 old_flags = v->flags;
64 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020065
66 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020067 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020068 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020069 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020070
71 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030072 ret = __vlan_add_pvid(vg, v->vid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020073 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030074 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000075
76 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020077 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040078 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020079 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030080
81 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000082}
83
Scott Feldman7f109532015-06-12 17:39:50 -070084static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +000085 struct net_bridge_vlan *v, u16 flags,
86 struct netlink_ext_ack *extack)
Scott Feldman7f109532015-06-12 17:39:50 -070087{
Scott Feldman7f109532015-06-12 17:39:50 -070088 int err;
89
Jiri Pirko0944d6b2015-10-09 13:54:11 +020090 /* Try switchdev op first. In case it is not supported, fallback to
91 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070092 */
Ido Schimmel27973792019-01-08 16:48:11 +000093 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020094 if (err == -EOPNOTSUPP)
Ido Schimmel27973792019-01-08 16:48:11 +000095 return vlan_vid_add(dev, br->vlan_proto, v->vid);
96 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
Scott Feldman7f109532015-06-12 17:39:50 -070097 return err;
98}
99
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200100static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200102 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200103 struct list_head *headp, *hpos;
104 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000105
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200106 if (br_vlan_is_master(v))
107 vg = br_vlan_group(v->br);
108 else
109 vg = nbp_vlan_group(v->port);
110
111 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200112 list_for_each_prev(hpos, headp) {
113 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
114 if (v->vid < vent->vid)
115 continue;
116 else
117 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000118 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200119 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200120}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000121
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200122static void __vlan_del_list(struct net_bridge_vlan *v)
123{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200124 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000125}
126
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400127static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +0000128 const struct net_bridge_vlan *v)
Scott Feldman7f109532015-06-12 17:39:50 -0700129{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200130 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700131
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 /* Try switchdev op first. In case it is not supported, fallback to
133 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700134 */
Ido Schimmel27973792019-01-08 16:48:11 +0000135 err = br_switchdev_port_vlan_del(dev, v->vid);
136 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
137 vlan_vid_del(dev, br->vlan_proto, v->vid);
138 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman7f109532015-06-12 17:39:50 -0700139}
140
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200141/* Returns a master vlan, if it didn't exist it gets created. In all cases a
142 * a reference is taken to the master vlan before returning.
143 */
Petr Machata169327d2018-12-12 17:02:50 +0000144static struct net_bridge_vlan *
145br_vlan_get_master(struct net_bridge *br, u16 vid,
146 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200147{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200148 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149 struct net_bridge_vlan *masterv;
150
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200151 vg = br_vlan_group(br);
152 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200153 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300154 bool changed;
155
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200156 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000157 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200159 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200160 if (WARN_ON(!masterv))
161 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200162 refcount_set(&masterv->refcnt, 1);
163 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200164 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300165 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200166
167 return masterv;
168}
169
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200170static void br_master_vlan_rcu_free(struct rcu_head *rcu)
171{
172 struct net_bridge_vlan *v;
173
174 v = container_of(rcu, struct net_bridge_vlan, rcu);
175 WARN_ON(!br_vlan_is_master(v));
176 free_percpu(v->stats);
177 v->stats = NULL;
178 kfree(v);
179}
180
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200181static void br_vlan_put_master(struct net_bridge_vlan *masterv)
182{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200183 struct net_bridge_vlan_group *vg;
184
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200185 if (!br_vlan_is_master(masterv))
186 return;
187
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200188 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300189 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200190 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200191 &masterv->vnode, br_vlan_rht_params);
192 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200193 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200194 }
195}
196
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300197static void nbp_vlan_rcu_free(struct rcu_head *rcu)
198{
199 struct net_bridge_vlan *v;
200
201 v = container_of(rcu, struct net_bridge_vlan, rcu);
202 WARN_ON(br_vlan_is_master(v));
203 /* if we had per-port stats configured then free them here */
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200204 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300205 free_percpu(v->stats);
206 v->stats = NULL;
207 kfree(v);
208}
209
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210/* This is the shared VLAN add function which works for both ports and bridge
211 * devices. There are four possible calls to this function in terms of the
212 * vlan entry type:
213 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100214 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100216 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100218 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200219 * will be used for filtering in both the port and the bridge
220 */
Petr Machata169327d2018-12-12 17:02:50 +0000221static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
222 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000223{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200224 struct net_bridge_vlan *masterv = NULL;
225 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200226 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200227 struct net_device *dev;
228 struct net_bridge *br;
229 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000230
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200231 if (br_vlan_is_master(v)) {
232 br = v->br;
233 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200234 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200235 } else {
236 p = v->port;
237 br = p->br;
238 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200239 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200240 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000241
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200243 /* Add VLAN to the device filter if it is supported.
244 * This ensures tagged traffic enters the bridge when
245 * promiscuous mode is disabled by br_manage_promisc().
246 */
Ido Schimmel27973792019-01-08 16:48:11 +0000247 err = __vlan_vid_add(dev, br, v, flags, extack);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400248 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200249 goto out;
250
251 /* need to work on the master vlan too */
252 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300253 bool changed;
254
255 err = br_vlan_add(br, v->vid,
256 flags | BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000257 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200258 if (err)
259 goto out_filt;
260 }
261
Petr Machata169327d2018-12-12 17:02:50 +0000262 masterv = br_vlan_get_master(br, v->vid, extack);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200263 if (!masterv)
264 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200265 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300266 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
267 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
268 if (!v->stats) {
269 err = -ENOMEM;
270 goto out_filt;
271 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200272 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300273 } else {
274 v->stats = masterv->stats;
275 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200276 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000277 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200278 if (err && err != -EOPNOTSUPP)
279 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900280 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000281
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200282 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200283 if (br_vlan_should_use(v)) {
284 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
285 if (err) {
286 br_err(br, "failed insert local address into bridge forwarding table\n");
287 goto out_filt;
288 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200289 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000290 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200291
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200292 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
293 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200294 if (err)
295 goto out_fdb_insert;
296
297 __vlan_add_list(v);
298 __vlan_add_flags(v, flags);
Mike Manning80900ac2019-04-18 18:35:34 +0100299
300 if (p)
301 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200302out:
303 return err;
304
305out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200306 if (br_vlan_should_use(v)) {
307 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
308 vg->num_vlans--;
309 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200310
311out_filt:
312 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000313 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200314 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800315 if (v->stats && masterv->stats != v->stats)
316 free_percpu(v->stats);
317 v->stats = NULL;
318
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200319 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200320 v->brvlan = NULL;
321 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200322 } else {
323 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324 }
325
326 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000327}
328
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000330{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200331 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200332 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200334 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200335
336 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200337 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200338 } else {
339 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200340 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200341 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200342 }
343
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200344 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200345 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000346 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200347 if (err)
348 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200349 } else {
350 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
351 if (err && err != -EOPNOTSUPP)
352 goto out;
353 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 }
355
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200356 if (br_vlan_should_use(v)) {
357 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
358 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 }
360
361 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800362 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200363 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
364 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200365 __vlan_del_list(v);
Mike Manning80900ac2019-04-18 18:35:34 +0100366 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300367 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200368 }
369
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200370 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200371out:
372 return err;
373}
374
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200375static void __vlan_group_free(struct net_bridge_vlan_group *vg)
376{
377 WARN_ON(!list_empty(&vg->vlan_list));
378 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800379 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200380 kfree(vg);
381}
382
383static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200384{
385 struct net_bridge_vlan *vlan, *tmp;
386
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200387 __vlan_delete_pvid(vg, vg->pvid);
388 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200389 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000390}
391
Vlad Yasevich78851982013-02-13 12:00:14 +0000392struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800393 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200394 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000395 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000396{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200397 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200398 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000399 u16 vid;
400
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400401 /* If this packet was not filtered at input, let it pass */
402 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000403 goto out;
404
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200405 /* At this point, we know that the frame was filtered and contains
406 * a valid vlan id. If the vlan id has untagged flag set,
407 * send untagged; otherwise, send tagged.
408 */
409 br_vlan_get_tag(skb, &vid);
410 v = br_vlan_find(vg, vid);
411 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400412 * only exception is the bridge is set in promisc mode and the
413 * packet is destined for the bridge device. In this case
414 * pass the packet as is.
415 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200416 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400417 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
418 goto out;
419 } else {
420 kfree_skb(skb);
421 return NULL;
422 }
423 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300424 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200425 stats = this_cpu_ptr(v->stats);
426 u64_stats_update_begin(&stats->syncp);
427 stats->tx_bytes += skb->len;
428 stats->tx_packets++;
429 u64_stats_update_end(&stats->syncp);
430 }
431
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200432 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100433 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800434
435 if (p && (p->flags & BR_VLAN_TUNNEL) &&
436 br_handle_egress_vlan_tunnel(skb, v)) {
437 kfree_skb(skb);
438 return NULL;
439 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000440out:
441 return skb;
442}
443
444/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200445static bool __allowed_ingress(const struct net_bridge *br,
446 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200447 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000448{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200449 struct br_vlan_stats *stats;
450 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900451 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000452
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400453 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900454 /* If vlan tx offload is disabled on bridge device and frame was
455 * sent from vlan device on the bridge device, it does not have
456 * HW accelerated vlan tag.
457 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100458 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200459 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400460 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900461 if (unlikely(!skb))
462 return false;
463 }
464
Toshiaki Makita8580e212014-06-10 20:59:23 +0900465 if (!br_vlan_get_tag(skb, vid)) {
466 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200467 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900468 /* Protocol-mismatch, empty out vlan_tci for new tag */
469 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100470 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100471 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900472 if (unlikely(!skb))
473 return false;
474
475 skb_pull(skb, ETH_HLEN);
476 skb_reset_mac_len(skb);
477 *vid = 0;
478 tagged = false;
479 } else {
480 tagged = true;
481 }
482 } else {
483 /* Untagged frame */
484 tagged = false;
485 }
486
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900487 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200488 u16 pvid = br_get_pvid(vg);
489
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900490 /* Frame had a tag with VID 0 or did not have a tag.
491 * See if pvid is set on this port. That tells us which
492 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000493 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400494 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900495 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000496
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900497 /* PVID is set on this port. Any untagged or priority-tagged
498 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000499 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900500 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900501 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900502 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200503 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900504 else
505 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100506 * At this point, we know that skb->vlan_tci VID
507 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900508 * We update only VID field and preserve PCP field.
509 */
510 skb->vlan_tci |= pvid;
511
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200512 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300513 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200514 return true;
515 }
516 v = br_vlan_find(vg, *vid);
517 if (!v || !br_vlan_should_use(v))
518 goto drop;
519
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300520 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200521 stats = this_cpu_ptr(v->stats);
522 u64_stats_update_begin(&stats->syncp);
523 stats->rx_bytes += skb->len;
524 stats->rx_packets++;
525 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000526 }
527
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200528 return true;
529
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900530drop:
531 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000532 return false;
533}
534
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200535bool br_allowed_ingress(const struct net_bridge *br,
536 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
537 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200538{
539 /* If VLAN filtering is disabled on the bridge, all packets are
540 * permitted.
541 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300542 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200543 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
544 return true;
545 }
546
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200547 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200548}
549
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000550/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200551bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000552 const struct sk_buff *skb)
553{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200554 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000555 u16 vid;
556
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400557 /* If this packet was not filtered at input, let it pass */
558 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000559 return true;
560
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000561 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200562 v = br_vlan_find(vg, vid);
563 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000564 return true;
565
566 return false;
567}
568
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900569/* Called under RCU */
570bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
571{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200572 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900573 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900574
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400575 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300576 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900577 return true;
578
Ido Schimmeleca1e002015-11-01 18:31:45 +0200579 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200580 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900581 return false;
582
Toshiaki Makita8580e212014-06-10 20:59:23 +0900583 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
584 *vid = 0;
585
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900586 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200587 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400588 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900589 return false;
590
591 return true;
592 }
593
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200594 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900595 return true;
596
597 return false;
598}
599
Petr Machatadbd6dc72018-05-30 02:56:55 +0200600static int br_vlan_add_existing(struct net_bridge *br,
601 struct net_bridge_vlan_group *vg,
602 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000603 u16 flags, bool *changed,
604 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200605{
606 int err;
607
Petr Machata169327d2018-12-12 17:02:50 +0000608 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200609 if (err && err != -EOPNOTSUPP)
610 return err;
611
Petr Machatadbd6dc72018-05-30 02:56:55 +0200612 if (!br_vlan_is_brentry(vlan)) {
613 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200614 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
615 err = -EINVAL;
616 goto err_flags;
617 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200618 /* It was only kept for port vlans, now make it real */
619 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
620 vlan->vid);
621 if (err) {
622 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200623 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200624 }
625
626 refcount_inc(&vlan->refcnt);
627 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
628 vg->num_vlans++;
629 *changed = true;
630 }
631
632 if (__vlan_add_flags(vlan, flags))
633 *changed = true;
634
635 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200636
637err_fdb_insert:
638err_flags:
639 br_switchdev_port_vlan_del(br->dev, vlan->vid);
640 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200641}
642
Toshiaki Makita8adff412013-10-16 17:07:13 +0900643/* Must be protected by RTNL.
644 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300645 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900646 */
Petr Machata169327d2018-12-12 17:02:50 +0000647int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
648 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000649{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200650 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200651 struct net_bridge_vlan *vlan;
652 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000653
654 ASSERT_RTNL();
655
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300656 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200657 vg = br_vlan_group(br);
658 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200659 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000660 return br_vlan_add_existing(br, vg, vlan, flags, changed,
661 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000662
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200663 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
664 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000665 return -ENOMEM;
666
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200667 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
668 if (!vlan->stats) {
669 kfree(vlan);
670 return -ENOMEM;
671 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200672 vlan->vid = vid;
673 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
674 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
675 vlan->br = br;
676 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300677 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000678 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200679 if (ret) {
680 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200681 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300682 } else {
683 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200684 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000685
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200686 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000687}
688
Toshiaki Makita8adff412013-10-16 17:07:13 +0900689/* Must be protected by RTNL.
690 * Must be called with vid in range from 1 to 4094 inclusive.
691 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000692int br_vlan_delete(struct net_bridge *br, u16 vid)
693{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200694 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200695 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000696
697 ASSERT_RTNL();
698
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200699 vg = br_vlan_group(br);
700 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200701 if (!v || !br_vlan_is_brentry(v))
702 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000703
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900704 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700705 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000706
Roopa Prabhuefa53562017-01-31 22:59:54 -0800707 vlan_tunnel_info_del(vg, v);
708
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200709 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000710}
711
712void br_vlan_flush(struct net_bridge *br)
713{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200714 struct net_bridge_vlan_group *vg;
715
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000716 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000717
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200718 vg = br_vlan_group(br);
719 __vlan_flush(vg);
720 RCU_INIT_POINTER(br->vlgrp, NULL);
721 synchronize_rcu();
722 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000723}
724
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200725struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900726{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200727 if (!vg)
728 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900729
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200730 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900731}
732
Toshiaki Makita204177f2014-06-10 20:59:25 +0900733/* Must be protected by RTNL. */
734static void recalculate_group_addr(struct net_bridge *br)
735{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300736 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900737 return;
738
739 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300740 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
741 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900742 /* Bridge Group Address */
743 br->group_addr[5] = 0x00;
744 } else { /* vlan_enabled && ETH_P_8021AD */
745 /* Provider Bridge Group Address */
746 br->group_addr[5] = 0x08;
747 }
748 spin_unlock_bh(&br->lock);
749}
750
751/* Must be protected by RTNL. */
752void br_recalculate_fwd_mask(struct net_bridge *br)
753{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300754 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
755 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900756 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
757 else /* vlan_enabled && ETH_P_8021AD */
758 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
759 ~(1u << br->group_addr[5]);
760}
761
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300762int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000763{
Elad Raz6b72a772016-01-06 13:01:06 +0100764 struct switchdev_attr attr = {
765 .orig_dev = br->dev,
766 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
767 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
768 .u.vlan_filtering = val,
769 };
770 int err;
771
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300772 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300773 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000774
Elad Raz6b72a772016-01-06 13:01:06 +0100775 err = switchdev_port_attr_set(br->dev, &attr);
776 if (err && err != -EOPNOTSUPP)
777 return err;
778
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300779 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400780 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900781 recalculate_group_addr(br);
782 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000783
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300784 return 0;
785}
786
787int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
788{
Xin Long047831a2016-04-09 00:03:31 +0800789 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000790}
791
Ido Schimmel1f514452017-05-26 08:37:23 +0200792bool br_vlan_enabled(const struct net_device *dev)
793{
794 struct net_bridge *br = netdev_priv(dev);
795
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300796 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200797}
798EXPORT_SYMBOL_GPL(br_vlan_enabled);
799
wenxu31aed462019-07-05 21:16:36 +0800800int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
801{
802 struct net_bridge *br = netdev_priv(dev);
803
804 *p_proto = ntohs(br->vlan_proto);
805
806 return 0;
807}
808EXPORT_SYMBOL_GPL(br_vlan_get_proto);
809
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900810int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900811{
812 int err = 0;
813 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200814 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200815 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900816 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900817
Toshiaki Makita204177f2014-06-10 20:59:25 +0900818 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900819 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900820
821 /* Add VLANs for the new proto to the device filter. */
822 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200823 vg = nbp_vlan_group(p);
824 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200825 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900826 if (err)
827 goto err_filt;
828 }
829 }
830
831 oldproto = br->vlan_proto;
832 br->vlan_proto = proto;
833
834 recalculate_group_addr(br);
835 br_recalculate_fwd_mask(br);
836
837 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200838 list_for_each_entry(p, &br->port_list, list) {
839 vg = nbp_vlan_group(p);
840 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200841 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200842 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900843
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900844 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900845
846err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200847 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200848 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900849
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200850 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
851 vg = nbp_vlan_group(p);
852 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200853 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200854 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900855
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900856 return err;
857}
858
859int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
860{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900861 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
862 return -EPROTONOSUPPORT;
863
Xin Long047831a2016-04-09 00:03:31 +0800864 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900865}
866
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200867int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
868{
869 switch (val) {
870 case 0:
871 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300872 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200873 break;
874 default:
875 return -EINVAL;
876 }
877
878 return 0;
879}
880
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300881int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
882{
883 struct net_bridge_port *p;
884
885 /* allow to change the option if there are no port vlans configured */
886 list_for_each_entry(p, &br->port_list, list) {
887 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
888
889 if (vg->num_vlans)
890 return -EBUSY;
891 }
892
893 switch (val) {
894 case 0:
895 case 1:
896 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
897 break;
898 default:
899 return -EINVAL;
900 }
901
902 return 0;
903}
904
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200905static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400906{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200907 struct net_bridge_vlan *v;
908
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200909 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200910 return false;
911
912 v = br_vlan_lookup(&vg->vlan_hash, vid);
913 if (v && br_vlan_should_use(v) &&
914 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
915 return true;
916
917 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400918}
919
920static void br_vlan_disable_default_pvid(struct net_bridge *br)
921{
922 struct net_bridge_port *p;
923 u16 pvid = br->default_pvid;
924
925 /* Disable default_pvid on all ports where it is still
926 * configured.
927 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200928 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400929 br_vlan_delete(br, pvid);
930
931 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200932 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400933 nbp_vlan_delete(p, pvid);
934 }
935
936 br->default_pvid = 0;
937}
938
Petr Machata169327d2018-12-12 17:02:50 +0000939int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
940 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400941{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200942 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200943 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400944 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300945 unsigned long *changed;
946 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400947 u16 old_pvid;
948 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400949
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200950 if (!pvid) {
951 br_vlan_disable_default_pvid(br);
952 return 0;
953 }
954
Andy Shevchenko459479d2018-08-30 13:33:18 +0300955 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400956 if (!changed)
957 return -ENOMEM;
958
959 old_pvid = br->default_pvid;
960
961 /* Update default_pvid config only if we do not conflict with
962 * user configuration.
963 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200964 vg = br_vlan_group(br);
965 pvent = br_vlan_find(vg, pvid);
966 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200967 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400968 err = br_vlan_add(br, pvid,
969 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200970 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300971 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000972 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400973 if (err)
974 goto out;
975 br_vlan_delete(br, old_pvid);
976 set_bit(0, changed);
977 }
978
979 list_for_each_entry(p, &br->port_list, list) {
980 /* Update default_pvid config only if we do not conflict with
981 * user configuration.
982 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200983 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400984 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200985 !vlan_default_pvid(vg, old_pvid)) ||
986 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400987 continue;
988
989 err = nbp_vlan_add(p, pvid,
990 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300991 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000992 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400993 if (err)
994 goto err_port;
995 nbp_vlan_delete(p, old_pvid);
996 set_bit(p->port_no, changed);
997 }
998
999 br->default_pvid = pvid;
1000
1001out:
Andy Shevchenko459479d2018-08-30 13:33:18 +03001002 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001003 return err;
1004
1005err_port:
1006 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1007 if (!test_bit(p->port_no, changed))
1008 continue;
1009
1010 if (old_pvid)
1011 nbp_vlan_add(p, old_pvid,
1012 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001013 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001014 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001015 nbp_vlan_delete(p, pvid);
1016 }
1017
1018 if (test_bit(0, changed)) {
1019 if (old_pvid)
1020 br_vlan_add(br, old_pvid,
1021 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001022 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001023 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001024 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001025 br_vlan_delete(br, pvid);
1026 }
1027 goto out;
1028}
1029
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001030int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1031{
1032 u16 pvid = val;
1033 int err = 0;
1034
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001035 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001036 return -EINVAL;
1037
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001038 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001039 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001040
1041 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001042 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001043 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1044 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001045 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001046 }
Petr Machata169327d2018-12-12 17:02:50 +00001047 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001048out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001049 return err;
1050}
1051
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001052int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001053{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001054 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001055 int ret = -ENOMEM;
1056
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001057 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1058 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001059 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001060 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001061 if (ret)
1062 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001063 ret = vlan_tunnel_init(vg);
1064 if (ret)
1065 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001066 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001067 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001068 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001069 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001070
1071out:
1072 return ret;
1073
Roopa Prabhuefa53562017-01-31 22:59:54 -08001074err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001075 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001076err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001077 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001078
1079 goto out;
1080}
1081
Petr Machata169327d2018-12-12 17:02:50 +00001082int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001083{
Elad Raz404cdbf2016-01-06 13:01:07 +01001084 struct switchdev_attr attr = {
1085 .orig_dev = p->br->dev,
1086 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1087 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001088 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001089 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001090 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001091 int ret = -ENOMEM;
1092
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001093 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1094 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001095 goto out;
1096
Elad Raz404cdbf2016-01-06 13:01:07 +01001097 ret = switchdev_port_attr_set(p->dev, &attr);
1098 if (ret && ret != -EOPNOTSUPP)
1099 goto err_vlan_enabled;
1100
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001101 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001102 if (ret)
1103 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001104 ret = vlan_tunnel_init(vg);
1105 if (ret)
1106 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001107 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001108 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001109 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001110 bool changed;
1111
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001112 ret = nbp_vlan_add(p, p->br->default_pvid,
1113 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001114 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001115 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001116 if (ret)
1117 goto err_vlan_add;
1118 }
1119out:
1120 return ret;
1121
1122err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001123 RCU_INIT_POINTER(p->vlgrp, NULL);
1124 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001125 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001126err_tunnel_init:
1127 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001128err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001129err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001130 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001131
1132 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001133}
1134
Toshiaki Makita8adff412013-10-16 17:07:13 +09001135/* Must be protected by RTNL.
1136 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001137 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001138 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001139int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001140 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001141{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001142 struct net_bridge_vlan *vlan;
1143 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001144
1145 ASSERT_RTNL();
1146
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001147 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001148 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001149 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001150 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001151 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001152 if (ret && ret != -EOPNOTSUPP)
1153 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001154 *changed = __vlan_add_flags(vlan, flags);
1155
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001156 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001157 }
1158
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001159 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1160 if (!vlan)
1161 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001162
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001163 vlan->vid = vid;
1164 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001165 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001166 if (ret)
1167 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001168 else
1169 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001170
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001171 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001172}
1173
Toshiaki Makita8adff412013-10-16 17:07:13 +09001174/* Must be protected by RTNL.
1175 * Must be called with vid in range from 1 to 4094 inclusive.
1176 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001177int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1178{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001179 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001180
1181 ASSERT_RTNL();
1182
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001183 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001184 if (!v)
1185 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001186 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001187 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001188
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001189 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001190}
1191
1192void nbp_vlan_flush(struct net_bridge_port *port)
1193{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001194 struct net_bridge_vlan_group *vg;
1195
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001196 ASSERT_RTNL();
1197
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001198 vg = nbp_vlan_group(port);
1199 __vlan_flush(vg);
1200 RCU_INIT_POINTER(port->vlgrp, NULL);
1201 synchronize_rcu();
1202 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001203}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001204
1205void br_vlan_get_stats(const struct net_bridge_vlan *v,
1206 struct br_vlan_stats *stats)
1207{
1208 int i;
1209
1210 memset(stats, 0, sizeof(*stats));
1211 for_each_possible_cpu(i) {
1212 u64 rxpackets, rxbytes, txpackets, txbytes;
1213 struct br_vlan_stats *cpu_stats;
1214 unsigned int start;
1215
1216 cpu_stats = per_cpu_ptr(v->stats, i);
1217 do {
1218 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1219 rxpackets = cpu_stats->rx_packets;
1220 rxbytes = cpu_stats->rx_bytes;
1221 txbytes = cpu_stats->tx_bytes;
1222 txpackets = cpu_stats->tx_packets;
1223 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1224
1225 stats->rx_packets += rxpackets;
1226 stats->rx_bytes += rxbytes;
1227 stats->tx_bytes += txbytes;
1228 stats->tx_packets += txpackets;
1229 }
1230}
Petr Machata4d4fd362018-04-29 10:56:08 +03001231
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001232static int __br_vlan_get_pvid(const struct net_device *dev,
1233 struct net_bridge_port *p, u16 *p_pvid)
Petr Machata4d4fd362018-04-29 10:56:08 +03001234{
1235 struct net_bridge_vlan_group *vg;
1236
Ido Schimmel5a6db042018-11-28 20:06:58 +00001237 if (p)
1238 vg = nbp_vlan_group(p);
1239 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001240 vg = br_vlan_group(netdev_priv(dev));
1241 else
1242 return -EINVAL;
1243
1244 *p_pvid = br_get_pvid(vg);
1245 return 0;
1246}
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001247
1248int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1249{
1250 ASSERT_RTNL();
1251
1252 return __br_vlan_get_pvid(dev, br_port_get_check_rtnl(dev), p_pvid);
1253}
Petr Machata4d4fd362018-04-29 10:56:08 +03001254EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1255
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001256int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1257{
1258 return __br_vlan_get_pvid(dev, br_port_get_check_rcu(dev), p_pvid);
1259}
1260EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1261
Petr Machata4d4fd362018-04-29 10:56:08 +03001262int br_vlan_get_info(const struct net_device *dev, u16 vid,
1263 struct bridge_vlan_info *p_vinfo)
1264{
1265 struct net_bridge_vlan_group *vg;
1266 struct net_bridge_vlan *v;
1267 struct net_bridge_port *p;
1268
1269 ASSERT_RTNL();
1270 p = br_port_get_check_rtnl(dev);
1271 if (p)
1272 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001273 else if (netif_is_bridge_master(dev))
1274 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001275 else
1276 return -EINVAL;
1277
1278 v = br_vlan_find(vg, vid);
1279 if (!v)
1280 return -ENOENT;
1281
1282 p_vinfo->vid = vid;
1283 p_vinfo->flags = v->flags;
Vladimir Olteanf40d9b202019-08-30 03:53:24 +03001284 if (vid == br_get_pvid(vg))
1285 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
Petr Machata4d4fd362018-04-29 10:56:08 +03001286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001289
1290static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1291{
1292 return is_vlan_dev(dev) &&
1293 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1294}
1295
1296static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1297 __always_unused void *data)
1298{
1299 return br_vlan_is_bind_vlan_dev(dev);
1300}
1301
1302static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1303{
1304 int found;
1305
1306 rcu_read_lock();
1307 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1308 NULL);
1309 rcu_read_unlock();
1310
1311 return !!found;
1312}
1313
1314struct br_vlan_bind_walk_data {
1315 u16 vid;
1316 struct net_device *result;
1317};
1318
1319static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1320 void *data_in)
1321{
1322 struct br_vlan_bind_walk_data *data = data_in;
1323 int found = 0;
1324
1325 if (br_vlan_is_bind_vlan_dev(dev) &&
1326 vlan_dev_priv(dev)->vlan_id == data->vid) {
1327 data->result = dev;
1328 found = 1;
1329 }
1330
1331 return found;
1332}
1333
1334static struct net_device *
1335br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1336{
1337 struct br_vlan_bind_walk_data data = {
1338 .vid = vid,
1339 };
1340
1341 rcu_read_lock();
1342 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1343 &data);
1344 rcu_read_unlock();
1345
1346 return data.result;
1347}
1348
1349static bool br_vlan_is_dev_up(const struct net_device *dev)
1350{
1351 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1352}
1353
1354static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1355 struct net_device *vlan_dev)
1356{
1357 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1358 struct net_bridge_vlan_group *vg;
1359 struct net_bridge_port *p;
1360 bool has_carrier = false;
1361
Mike Manning8e1acd42019-04-18 18:35:35 +01001362 if (!netif_carrier_ok(br->dev)) {
1363 netif_carrier_off(vlan_dev);
1364 return;
1365 }
1366
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001367 list_for_each_entry(p, &br->port_list, list) {
1368 vg = nbp_vlan_group(p);
1369 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1370 has_carrier = true;
1371 break;
1372 }
1373 }
1374
1375 if (has_carrier)
1376 netif_carrier_on(vlan_dev);
1377 else
1378 netif_carrier_off(vlan_dev);
1379}
1380
1381static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1382{
1383 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1384 struct net_bridge_vlan *vlan;
1385 struct net_device *vlan_dev;
1386
1387 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1388 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1389 vlan->vid);
1390 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001391 if (br_vlan_is_dev_up(p->dev)) {
1392 if (netif_carrier_ok(p->br->dev))
1393 netif_carrier_on(vlan_dev);
1394 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001395 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001396 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001397 }
1398 }
1399}
1400
1401static void br_vlan_upper_change(struct net_device *dev,
1402 struct net_device *upper_dev,
1403 bool linking)
1404{
1405 struct net_bridge *br = netdev_priv(dev);
1406
1407 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1408 return;
1409
1410 if (linking) {
1411 br_vlan_set_vlan_dev_state(br, upper_dev);
1412 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1413 } else {
1414 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1415 br_vlan_has_upper_bind_vlan_dev(dev));
1416 }
1417}
1418
Mike Manning8e1acd42019-04-18 18:35:35 +01001419struct br_vlan_link_state_walk_data {
1420 struct net_bridge *br;
1421};
1422
1423static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1424 void *data_in)
1425{
1426 struct br_vlan_link_state_walk_data *data = data_in;
1427
1428 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1429 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1430
1431 return 0;
1432}
1433
1434static void br_vlan_link_state_change(struct net_device *dev,
1435 struct net_bridge *br)
1436{
1437 struct br_vlan_link_state_walk_data data = {
1438 .br = br
1439 };
1440
1441 rcu_read_lock();
1442 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1443 &data);
1444 rcu_read_unlock();
1445}
1446
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001447/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001448static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1449{
1450 struct net_device *vlan_dev;
1451
1452 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1453 return;
1454
1455 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1456 if (vlan_dev)
1457 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1458}
1459
1460/* Must be protected by RTNL. */
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001461int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001462{
1463 struct netdev_notifier_changeupper_info *info;
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001464 struct net_bridge *br = netdev_priv(dev);
1465 bool changed;
1466 int ret = 0;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001467
1468 switch (event) {
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001469 case NETDEV_REGISTER:
1470 ret = br_vlan_add(br, br->default_pvid,
1471 BRIDGE_VLAN_INFO_PVID |
1472 BRIDGE_VLAN_INFO_UNTAGGED |
1473 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1474 break;
1475 case NETDEV_UNREGISTER:
1476 br_vlan_delete(br, br->default_pvid);
1477 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001478 case NETDEV_CHANGEUPPER:
1479 info = ptr;
1480 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1481 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001482
1483 case NETDEV_CHANGE:
1484 case NETDEV_UP:
Mike Manning8e1acd42019-04-18 18:35:35 +01001485 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001486 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001487 br_vlan_link_state_change(dev, br);
1488 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001489 }
Nikolay Aleksandrov091adf92019-08-02 13:57:36 +03001490
1491 return ret;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001492}
1493
1494/* Must be protected by RTNL. */
1495void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1496{
1497 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1498 return;
1499
1500 switch (event) {
1501 case NETDEV_CHANGE:
1502 case NETDEV_DOWN:
1503 case NETDEV_UP:
1504 br_vlan_set_all_vlan_dev_state(p);
1505 break;
1506 }
1507}