blob: 8d97b91ad503e045e1eae670fc1ebd6d04eecdca [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
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900800int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900801{
802 int err = 0;
803 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200804 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200805 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900806 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900807
Toshiaki Makita204177f2014-06-10 20:59:25 +0900808 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900809 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900810
811 /* Add VLANs for the new proto to the device filter. */
812 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200813 vg = nbp_vlan_group(p);
814 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200815 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900816 if (err)
817 goto err_filt;
818 }
819 }
820
821 oldproto = br->vlan_proto;
822 br->vlan_proto = proto;
823
824 recalculate_group_addr(br);
825 br_recalculate_fwd_mask(br);
826
827 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200828 list_for_each_entry(p, &br->port_list, list) {
829 vg = nbp_vlan_group(p);
830 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200831 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200832 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900833
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900834 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900835
836err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200837 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200838 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900839
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200840 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
841 vg = nbp_vlan_group(p);
842 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200843 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200844 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900845
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900846 return err;
847}
848
849int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
850{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900851 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
852 return -EPROTONOSUPPORT;
853
Xin Long047831a2016-04-09 00:03:31 +0800854 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900855}
856
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200857int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
858{
859 switch (val) {
860 case 0:
861 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300862 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200863 break;
864 default:
865 return -EINVAL;
866 }
867
868 return 0;
869}
870
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300871int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
872{
873 struct net_bridge_port *p;
874
875 /* allow to change the option if there are no port vlans configured */
876 list_for_each_entry(p, &br->port_list, list) {
877 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
878
879 if (vg->num_vlans)
880 return -EBUSY;
881 }
882
883 switch (val) {
884 case 0:
885 case 1:
886 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
887 break;
888 default:
889 return -EINVAL;
890 }
891
892 return 0;
893}
894
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200895static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400896{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200897 struct net_bridge_vlan *v;
898
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200899 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200900 return false;
901
902 v = br_vlan_lookup(&vg->vlan_hash, vid);
903 if (v && br_vlan_should_use(v) &&
904 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
905 return true;
906
907 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400908}
909
910static void br_vlan_disable_default_pvid(struct net_bridge *br)
911{
912 struct net_bridge_port *p;
913 u16 pvid = br->default_pvid;
914
915 /* Disable default_pvid on all ports where it is still
916 * configured.
917 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200918 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400919 br_vlan_delete(br, pvid);
920
921 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200922 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400923 nbp_vlan_delete(p, pvid);
924 }
925
926 br->default_pvid = 0;
927}
928
Petr Machata169327d2018-12-12 17:02:50 +0000929int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
930 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400931{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200932 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200933 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400934 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300935 unsigned long *changed;
936 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400937 u16 old_pvid;
938 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400939
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200940 if (!pvid) {
941 br_vlan_disable_default_pvid(br);
942 return 0;
943 }
944
Andy Shevchenko459479d2018-08-30 13:33:18 +0300945 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400946 if (!changed)
947 return -ENOMEM;
948
949 old_pvid = br->default_pvid;
950
951 /* Update default_pvid config only if we do not conflict with
952 * user configuration.
953 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200954 vg = br_vlan_group(br);
955 pvent = br_vlan_find(vg, pvid);
956 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200957 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400958 err = br_vlan_add(br, pvid,
959 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200960 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300961 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000962 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400963 if (err)
964 goto out;
965 br_vlan_delete(br, old_pvid);
966 set_bit(0, changed);
967 }
968
969 list_for_each_entry(p, &br->port_list, list) {
970 /* Update default_pvid config only if we do not conflict with
971 * user configuration.
972 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200973 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400974 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200975 !vlan_default_pvid(vg, old_pvid)) ||
976 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400977 continue;
978
979 err = nbp_vlan_add(p, pvid,
980 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300981 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000982 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400983 if (err)
984 goto err_port;
985 nbp_vlan_delete(p, old_pvid);
986 set_bit(p->port_no, changed);
987 }
988
989 br->default_pvid = pvid;
990
991out:
Andy Shevchenko459479d2018-08-30 13:33:18 +0300992 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400993 return err;
994
995err_port:
996 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
997 if (!test_bit(p->port_no, changed))
998 continue;
999
1000 if (old_pvid)
1001 nbp_vlan_add(p, old_pvid,
1002 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001003 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001004 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001005 nbp_vlan_delete(p, pvid);
1006 }
1007
1008 if (test_bit(0, changed)) {
1009 if (old_pvid)
1010 br_vlan_add(br, old_pvid,
1011 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001012 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001013 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001014 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001015 br_vlan_delete(br, pvid);
1016 }
1017 goto out;
1018}
1019
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001020int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1021{
1022 u16 pvid = val;
1023 int err = 0;
1024
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001025 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001026 return -EINVAL;
1027
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001028 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001029 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001030
1031 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001032 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001033 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1034 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001035 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001036 }
Petr Machata169327d2018-12-12 17:02:50 +00001037 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001038out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001039 return err;
1040}
1041
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001042int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001043{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001044 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001045 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001046 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001047
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001048 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1049 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001050 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001051 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001052 if (ret)
1053 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001054 ret = vlan_tunnel_init(vg);
1055 if (ret)
1056 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001057 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001058 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001059 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001060 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001061 ret = br_vlan_add(br, 1,
1062 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Petr Machata169327d2018-12-12 17:02:50 +00001063 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001064 if (ret)
1065 goto err_vlan_add;
1066
1067out:
1068 return ret;
1069
1070err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -08001071 vlan_tunnel_deinit(vg);
1072err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001073 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001074err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001075 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001076
1077 goto out;
1078}
1079
Petr Machata169327d2018-12-12 17:02:50 +00001080int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001081{
Elad Raz404cdbf2016-01-06 13:01:07 +01001082 struct switchdev_attr attr = {
1083 .orig_dev = p->br->dev,
1084 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1085 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001086 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001087 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001088 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001089 int ret = -ENOMEM;
1090
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001091 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1092 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001093 goto out;
1094
Elad Raz404cdbf2016-01-06 13:01:07 +01001095 ret = switchdev_port_attr_set(p->dev, &attr);
1096 if (ret && ret != -EOPNOTSUPP)
1097 goto err_vlan_enabled;
1098
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001099 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001100 if (ret)
1101 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001102 ret = vlan_tunnel_init(vg);
1103 if (ret)
1104 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001105 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001106 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001107 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001108 bool changed;
1109
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001110 ret = nbp_vlan_add(p, p->br->default_pvid,
1111 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001112 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001113 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001114 if (ret)
1115 goto err_vlan_add;
1116 }
1117out:
1118 return ret;
1119
1120err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001121 RCU_INIT_POINTER(p->vlgrp, NULL);
1122 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001123 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001124err_tunnel_init:
1125 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001126err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001127err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001128 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001129
1130 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001131}
1132
Toshiaki Makita8adff412013-10-16 17:07:13 +09001133/* Must be protected by RTNL.
1134 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001135 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001136 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001137int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001138 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001139{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001140 struct net_bridge_vlan *vlan;
1141 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001142
1143 ASSERT_RTNL();
1144
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001145 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001146 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001147 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001148 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001149 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001150 if (ret && ret != -EOPNOTSUPP)
1151 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001152 *changed = __vlan_add_flags(vlan, flags);
1153
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001154 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001155 }
1156
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001157 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1158 if (!vlan)
1159 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001160
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001161 vlan->vid = vid;
1162 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001163 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001164 if (ret)
1165 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001166 else
1167 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001168
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001169 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001170}
1171
Toshiaki Makita8adff412013-10-16 17:07:13 +09001172/* Must be protected by RTNL.
1173 * Must be called with vid in range from 1 to 4094 inclusive.
1174 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001175int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1176{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001177 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001178
1179 ASSERT_RTNL();
1180
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001181 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001182 if (!v)
1183 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001184 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001185 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001186
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001187 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001188}
1189
1190void nbp_vlan_flush(struct net_bridge_port *port)
1191{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001192 struct net_bridge_vlan_group *vg;
1193
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001194 ASSERT_RTNL();
1195
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001196 vg = nbp_vlan_group(port);
1197 __vlan_flush(vg);
1198 RCU_INIT_POINTER(port->vlgrp, NULL);
1199 synchronize_rcu();
1200 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001201}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001202
1203void br_vlan_get_stats(const struct net_bridge_vlan *v,
1204 struct br_vlan_stats *stats)
1205{
1206 int i;
1207
1208 memset(stats, 0, sizeof(*stats));
1209 for_each_possible_cpu(i) {
1210 u64 rxpackets, rxbytes, txpackets, txbytes;
1211 struct br_vlan_stats *cpu_stats;
1212 unsigned int start;
1213
1214 cpu_stats = per_cpu_ptr(v->stats, i);
1215 do {
1216 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1217 rxpackets = cpu_stats->rx_packets;
1218 rxbytes = cpu_stats->rx_bytes;
1219 txbytes = cpu_stats->tx_bytes;
1220 txpackets = cpu_stats->tx_packets;
1221 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1222
1223 stats->rx_packets += rxpackets;
1224 stats->rx_bytes += rxbytes;
1225 stats->tx_bytes += txbytes;
1226 stats->tx_packets += txpackets;
1227 }
1228}
Petr Machata4d4fd362018-04-29 10:56:08 +03001229
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001230static int __br_vlan_get_pvid(const struct net_device *dev,
1231 struct net_bridge_port *p, u16 *p_pvid)
Petr Machata4d4fd362018-04-29 10:56:08 +03001232{
1233 struct net_bridge_vlan_group *vg;
1234
Ido Schimmel5a6db042018-11-28 20:06:58 +00001235 if (p)
1236 vg = nbp_vlan_group(p);
1237 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001238 vg = br_vlan_group(netdev_priv(dev));
1239 else
1240 return -EINVAL;
1241
1242 *p_pvid = br_get_pvid(vg);
1243 return 0;
1244}
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001245
1246int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1247{
1248 ASSERT_RTNL();
1249
1250 return __br_vlan_get_pvid(dev, br_port_get_check_rtnl(dev), p_pvid);
1251}
Petr Machata4d4fd362018-04-29 10:56:08 +03001252EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1253
Pablo Neira Ayuso7582f5b2019-07-05 21:16:34 +08001254int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1255{
1256 return __br_vlan_get_pvid(dev, br_port_get_check_rcu(dev), p_pvid);
1257}
1258EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1259
Petr Machata4d4fd362018-04-29 10:56:08 +03001260int br_vlan_get_info(const struct net_device *dev, u16 vid,
1261 struct bridge_vlan_info *p_vinfo)
1262{
1263 struct net_bridge_vlan_group *vg;
1264 struct net_bridge_vlan *v;
1265 struct net_bridge_port *p;
1266
1267 ASSERT_RTNL();
1268 p = br_port_get_check_rtnl(dev);
1269 if (p)
1270 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001271 else if (netif_is_bridge_master(dev))
1272 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001273 else
1274 return -EINVAL;
1275
1276 v = br_vlan_find(vg, vid);
1277 if (!v)
1278 return -ENOENT;
1279
1280 p_vinfo->vid = vid;
1281 p_vinfo->flags = v->flags;
1282 return 0;
1283}
1284EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001285
1286static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1287{
1288 return is_vlan_dev(dev) &&
1289 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1290}
1291
1292static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1293 __always_unused void *data)
1294{
1295 return br_vlan_is_bind_vlan_dev(dev);
1296}
1297
1298static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1299{
1300 int found;
1301
1302 rcu_read_lock();
1303 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1304 NULL);
1305 rcu_read_unlock();
1306
1307 return !!found;
1308}
1309
1310struct br_vlan_bind_walk_data {
1311 u16 vid;
1312 struct net_device *result;
1313};
1314
1315static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1316 void *data_in)
1317{
1318 struct br_vlan_bind_walk_data *data = data_in;
1319 int found = 0;
1320
1321 if (br_vlan_is_bind_vlan_dev(dev) &&
1322 vlan_dev_priv(dev)->vlan_id == data->vid) {
1323 data->result = dev;
1324 found = 1;
1325 }
1326
1327 return found;
1328}
1329
1330static struct net_device *
1331br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1332{
1333 struct br_vlan_bind_walk_data data = {
1334 .vid = vid,
1335 };
1336
1337 rcu_read_lock();
1338 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1339 &data);
1340 rcu_read_unlock();
1341
1342 return data.result;
1343}
1344
1345static bool br_vlan_is_dev_up(const struct net_device *dev)
1346{
1347 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1348}
1349
1350static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1351 struct net_device *vlan_dev)
1352{
1353 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1354 struct net_bridge_vlan_group *vg;
1355 struct net_bridge_port *p;
1356 bool has_carrier = false;
1357
Mike Manning8e1acd42019-04-18 18:35:35 +01001358 if (!netif_carrier_ok(br->dev)) {
1359 netif_carrier_off(vlan_dev);
1360 return;
1361 }
1362
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001363 list_for_each_entry(p, &br->port_list, list) {
1364 vg = nbp_vlan_group(p);
1365 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1366 has_carrier = true;
1367 break;
1368 }
1369 }
1370
1371 if (has_carrier)
1372 netif_carrier_on(vlan_dev);
1373 else
1374 netif_carrier_off(vlan_dev);
1375}
1376
1377static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1378{
1379 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1380 struct net_bridge_vlan *vlan;
1381 struct net_device *vlan_dev;
1382
1383 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1384 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1385 vlan->vid);
1386 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001387 if (br_vlan_is_dev_up(p->dev)) {
1388 if (netif_carrier_ok(p->br->dev))
1389 netif_carrier_on(vlan_dev);
1390 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001391 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001392 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001393 }
1394 }
1395}
1396
1397static void br_vlan_upper_change(struct net_device *dev,
1398 struct net_device *upper_dev,
1399 bool linking)
1400{
1401 struct net_bridge *br = netdev_priv(dev);
1402
1403 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1404 return;
1405
1406 if (linking) {
1407 br_vlan_set_vlan_dev_state(br, upper_dev);
1408 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1409 } else {
1410 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1411 br_vlan_has_upper_bind_vlan_dev(dev));
1412 }
1413}
1414
Mike Manning8e1acd42019-04-18 18:35:35 +01001415struct br_vlan_link_state_walk_data {
1416 struct net_bridge *br;
1417};
1418
1419static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1420 void *data_in)
1421{
1422 struct br_vlan_link_state_walk_data *data = data_in;
1423
1424 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1425 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1426
1427 return 0;
1428}
1429
1430static void br_vlan_link_state_change(struct net_device *dev,
1431 struct net_bridge *br)
1432{
1433 struct br_vlan_link_state_walk_data data = {
1434 .br = br
1435 };
1436
1437 rcu_read_lock();
1438 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1439 &data);
1440 rcu_read_unlock();
1441}
1442
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001443/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001444static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1445{
1446 struct net_device *vlan_dev;
1447
1448 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1449 return;
1450
1451 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1452 if (vlan_dev)
1453 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1454}
1455
1456/* Must be protected by RTNL. */
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001457void br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1458 void *ptr)
1459{
1460 struct netdev_notifier_changeupper_info *info;
Mike Manning8e1acd42019-04-18 18:35:35 +01001461 struct net_bridge *br;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001462
1463 switch (event) {
1464 case NETDEV_CHANGEUPPER:
1465 info = ptr;
1466 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1467 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001468
1469 case NETDEV_CHANGE:
1470 case NETDEV_UP:
1471 br = netdev_priv(dev);
1472 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1473 return;
1474 br_vlan_link_state_change(dev, br);
1475 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001476 }
1477}
1478
1479/* Must be protected by RTNL. */
1480void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1481{
1482 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1483 return;
1484
1485 switch (event) {
1486 case NETDEV_CHANGE:
1487 case NETDEV_DOWN:
1488 case NETDEV_UP:
1489 br_vlan_set_all_vlan_dev_state(p);
1490 break;
1491 }
1492}