blob: a7e869da21bfdd67fb8c8258f654553638422b1f [file] [log] [blame]
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001#include <linux/kernel.h>
2#include <linux/netdevice.h>
3#include <linux/rtnetlink.h>
4#include <linux/slab.h>
Scott Feldman7f109532015-06-12 17:39:50 -07005#include <net/switchdev.h>
Vlad Yasevich243a2e62013-02-13 12:00:09 +00006
7#include "br_private.h"
Roopa Prabhuefa53562017-01-31 22:59:54 -08008#include "br_private_tunnel.h"
Vlad Yasevich243a2e62013-02-13 12:00:09 +00009
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020010static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
11 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000012{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020013 const struct net_bridge_vlan *vle = ptr;
14 u16 vid = *(u16 *)arg->key;
15
16 return vle->vid != vid;
17}
18
19static const struct rhashtable_params br_vlan_rht_params = {
20 .head_offset = offsetof(struct net_bridge_vlan, vnode),
21 .key_offset = offsetof(struct net_bridge_vlan, vid),
22 .key_len = sizeof(u16),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020023 .nelem_hint = 3,
24 .locks_mul = 1,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020025 .max_size = VLAN_N_VID,
26 .obj_cmpfn = br_vlan_cmp,
27 .automatic_shrinking = true,
28};
29
30static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
31{
32 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
33}
34
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030035static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020036{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020037 if (vg->pvid == vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030038 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000039
40 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020041 vg->pvid = vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030042
43 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000044}
45
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030046static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000047{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020048 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030049 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000050
51 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020052 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030053
54 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000055}
56
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030057/* return true if anything changed, false otherwise */
58static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000059{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020060 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030061 u16 old_flags = v->flags;
62 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020063
64 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020065 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020066 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020067 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020068
69 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030070 ret = __vlan_add_pvid(vg, v->vid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020071 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030072 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000073
74 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020075 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040076 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020077 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030078
79 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000080}
81
Scott Feldman7f109532015-06-12 17:39:50 -070082static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
83 u16 vid, u16 flags)
84{
Scott Feldman7f109532015-06-12 17:39:50 -070085 int err;
86
Jiri Pirko0944d6b2015-10-09 13:54:11 +020087 /* Try switchdev op first. In case it is not supported, fallback to
88 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070089 */
Petr Machatad66e4342018-05-30 02:56:03 +020090 err = br_switchdev_port_vlan_add(dev, vid, flags);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020091 if (err == -EOPNOTSUPP)
92 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070093 return err;
94}
95
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020096static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000097{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020098 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020099 struct list_head *headp, *hpos;
100 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200102 if (br_vlan_is_master(v))
103 vg = br_vlan_group(v->br);
104 else
105 vg = nbp_vlan_group(v->port);
106
107 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200108 list_for_each_prev(hpos, headp) {
109 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
110 if (v->vid < vent->vid)
111 continue;
112 else
113 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000114 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200115 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200116}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000117
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200118static void __vlan_del_list(struct net_bridge_vlan *v)
119{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200120 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000121}
122
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400123static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
124 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700125{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200126 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700127
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200128 /* Try switchdev op first. In case it is not supported, fallback to
129 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700130 */
Petr Machatad66e4342018-05-30 02:56:03 +0200131 err = br_switchdev_port_vlan_del(dev, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700133 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200134 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700135 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400136 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700137}
138
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200139/* Returns a master vlan, if it didn't exist it gets created. In all cases a
140 * a reference is taken to the master vlan before returning.
141 */
142static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
143{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200144 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200145 struct net_bridge_vlan *masterv;
146
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200147 vg = br_vlan_group(br);
148 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300150 bool changed;
151
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200152 /* missing global ctx, create it now */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300153 if (br_vlan_add(br, vid, 0, &changed))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200154 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200155 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200156 if (WARN_ON(!masterv))
157 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200158 refcount_set(&masterv->refcnt, 1);
159 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200160 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300161 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200162
163 return masterv;
164}
165
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200166static void br_master_vlan_rcu_free(struct rcu_head *rcu)
167{
168 struct net_bridge_vlan *v;
169
170 v = container_of(rcu, struct net_bridge_vlan, rcu);
171 WARN_ON(!br_vlan_is_master(v));
172 free_percpu(v->stats);
173 v->stats = NULL;
174 kfree(v);
175}
176
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200177static void br_vlan_put_master(struct net_bridge_vlan *masterv)
178{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200179 struct net_bridge_vlan_group *vg;
180
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200181 if (!br_vlan_is_master(masterv))
182 return;
183
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200184 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300185 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200186 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200187 &masterv->vnode, br_vlan_rht_params);
188 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200189 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200190 }
191}
192
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300193static void nbp_vlan_rcu_free(struct rcu_head *rcu)
194{
195 struct net_bridge_vlan *v;
196
197 v = container_of(rcu, struct net_bridge_vlan, rcu);
198 WARN_ON(br_vlan_is_master(v));
199 /* if we had per-port stats configured then free them here */
200 if (v->brvlan->stats != v->stats)
201 free_percpu(v->stats);
202 v->stats = NULL;
203 kfree(v);
204}
205
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200206/* This is the shared VLAN add function which works for both ports and bridge
207 * devices. There are four possible calls to this function in terms of the
208 * vlan entry type:
209 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100210 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200211 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100212 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200213 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100214 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 * will be used for filtering in both the port and the bridge
216 */
217static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000218{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200219 struct net_bridge_vlan *masterv = NULL;
220 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200221 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200222 struct net_device *dev;
223 struct net_bridge *br;
224 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000225
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200226 if (br_vlan_is_master(v)) {
227 br = v->br;
228 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200229 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200230 } else {
231 p = v->port;
232 br = p->br;
233 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200234 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200235 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000236
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200237 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200238 /* Add VLAN to the device filter if it is supported.
239 * This ensures tagged traffic enters the bridge when
240 * promiscuous mode is disabled by br_manage_promisc().
241 */
242 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400243 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200244 goto out;
245
246 /* need to work on the master vlan too */
247 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300248 bool changed;
249
250 err = br_vlan_add(br, v->vid,
251 flags | BRIDGE_VLAN_INFO_BRENTRY,
252 &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200253 if (err)
254 goto out_filt;
255 }
256
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200257 masterv = br_vlan_get_master(br, v->vid);
258 if (!masterv)
259 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200260 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300261 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
262 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
263 if (!v->stats) {
264 err = -ENOMEM;
265 goto out_filt;
266 }
267 } else {
268 v->stats = masterv->stats;
269 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200270 } else {
271 err = br_switchdev_port_vlan_add(dev, v->vid, flags);
272 if (err && err != -EOPNOTSUPP)
273 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900274 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000275
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200276 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200277 if (br_vlan_should_use(v)) {
278 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
279 if (err) {
280 br_err(br, "failed insert local address into bridge forwarding table\n");
281 goto out_filt;
282 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200283 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000284 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200285
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200286 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
287 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 if (err)
289 goto out_fdb_insert;
290
291 __vlan_add_list(v);
292 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200293out:
294 return err;
295
296out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200297 if (br_vlan_should_use(v)) {
298 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
299 vg->num_vlans--;
300 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200301
302out_filt:
303 if (p) {
304 __vlan_vid_del(dev, br, v->vid);
305 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800306 if (v->stats && masterv->stats != v->stats)
307 free_percpu(v->stats);
308 v->stats = NULL;
309
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200310 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200311 v->brvlan = NULL;
312 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200313 } else {
314 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200315 }
316
317 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000318}
319
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200320static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000321{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200322 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200323 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326
327 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200328 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 } else {
330 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200331 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 }
334
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200335 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 if (p) {
337 err = __vlan_vid_del(p->dev, p->br, v->vid);
338 if (err)
339 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200340 } else {
341 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
342 if (err && err != -EOPNOTSUPP)
343 goto out;
344 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200345 }
346
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200347 if (br_vlan_should_use(v)) {
348 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
349 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 }
351
352 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800353 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200354 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
355 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 __vlan_del_list(v);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300357 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200358 }
359
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200360 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200361out:
362 return err;
363}
364
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200365static void __vlan_group_free(struct net_bridge_vlan_group *vg)
366{
367 WARN_ON(!list_empty(&vg->vlan_list));
368 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800369 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200370 kfree(vg);
371}
372
373static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200374{
375 struct net_bridge_vlan *vlan, *tmp;
376
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200377 __vlan_delete_pvid(vg, vg->pvid);
378 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200379 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000380}
381
Vlad Yasevich78851982013-02-13 12:00:14 +0000382struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800383 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200384 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000385 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000386{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200387 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200388 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000389 u16 vid;
390
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400391 /* If this packet was not filtered at input, let it pass */
392 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000393 goto out;
394
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200395 /* At this point, we know that the frame was filtered and contains
396 * a valid vlan id. If the vlan id has untagged flag set,
397 * send untagged; otherwise, send tagged.
398 */
399 br_vlan_get_tag(skb, &vid);
400 v = br_vlan_find(vg, vid);
401 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400402 * only exception is the bridge is set in promisc mode and the
403 * packet is destined for the bridge device. In this case
404 * pass the packet as is.
405 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200406 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400407 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
408 goto out;
409 } else {
410 kfree_skb(skb);
411 return NULL;
412 }
413 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300414 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200415 stats = this_cpu_ptr(v->stats);
416 u64_stats_update_begin(&stats->syncp);
417 stats->tx_bytes += skb->len;
418 stats->tx_packets++;
419 u64_stats_update_end(&stats->syncp);
420 }
421
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200422 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100423 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800424
425 if (p && (p->flags & BR_VLAN_TUNNEL) &&
426 br_handle_egress_vlan_tunnel(skb, v)) {
427 kfree_skb(skb);
428 return NULL;
429 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000430out:
431 return skb;
432}
433
434/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200435static bool __allowed_ingress(const struct net_bridge *br,
436 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200437 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000438{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200439 struct br_vlan_stats *stats;
440 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900441 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000442
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400443 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900444 /* If vlan tx offload is disabled on bridge device and frame was
445 * sent from vlan device on the bridge device, it does not have
446 * HW accelerated vlan tag.
447 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100448 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200449 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400450 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900451 if (unlikely(!skb))
452 return false;
453 }
454
Toshiaki Makita8580e212014-06-10 20:59:23 +0900455 if (!br_vlan_get_tag(skb, vid)) {
456 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200457 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900458 /* Protocol-mismatch, empty out vlan_tci for new tag */
459 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100460 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100461 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900462 if (unlikely(!skb))
463 return false;
464
465 skb_pull(skb, ETH_HLEN);
466 skb_reset_mac_len(skb);
467 *vid = 0;
468 tagged = false;
469 } else {
470 tagged = true;
471 }
472 } else {
473 /* Untagged frame */
474 tagged = false;
475 }
476
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900477 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200478 u16 pvid = br_get_pvid(vg);
479
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900480 /* Frame had a tag with VID 0 or did not have a tag.
481 * See if pvid is set on this port. That tells us which
482 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000483 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400484 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900485 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000486
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900487 /* PVID is set on this port. Any untagged or priority-tagged
488 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000489 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900490 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900491 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900492 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200493 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900494 else
495 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100496 * At this point, we know that skb->vlan_tci VID
497 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900498 * We update only VID field and preserve PCP field.
499 */
500 skb->vlan_tci |= pvid;
501
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200502 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300503 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200504 return true;
505 }
506 v = br_vlan_find(vg, *vid);
507 if (!v || !br_vlan_should_use(v))
508 goto drop;
509
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300510 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200511 stats = this_cpu_ptr(v->stats);
512 u64_stats_update_begin(&stats->syncp);
513 stats->rx_bytes += skb->len;
514 stats->rx_packets++;
515 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000516 }
517
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200518 return true;
519
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900520drop:
521 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000522 return false;
523}
524
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200525bool br_allowed_ingress(const struct net_bridge *br,
526 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
527 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200528{
529 /* If VLAN filtering is disabled on the bridge, all packets are
530 * permitted.
531 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300532 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200533 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
534 return true;
535 }
536
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200537 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200538}
539
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000540/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200541bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000542 const struct sk_buff *skb)
543{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200544 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000545 u16 vid;
546
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400547 /* If this packet was not filtered at input, let it pass */
548 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000549 return true;
550
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000551 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200552 v = br_vlan_find(vg, vid);
553 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000554 return true;
555
556 return false;
557}
558
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900559/* Called under RCU */
560bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
561{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200562 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900563 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900564
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400565 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300566 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900567 return true;
568
Ido Schimmeleca1e002015-11-01 18:31:45 +0200569 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200570 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900571 return false;
572
Toshiaki Makita8580e212014-06-10 20:59:23 +0900573 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
574 *vid = 0;
575
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900576 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200577 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400578 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900579 return false;
580
581 return true;
582 }
583
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200584 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900585 return true;
586
587 return false;
588}
589
Petr Machatadbd6dc72018-05-30 02:56:55 +0200590static int br_vlan_add_existing(struct net_bridge *br,
591 struct net_bridge_vlan_group *vg,
592 struct net_bridge_vlan *vlan,
593 u16 flags, bool *changed)
594{
595 int err;
596
Petr Machata9c86ce22018-05-30 03:00:45 +0200597 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags);
598 if (err && err != -EOPNOTSUPP)
599 return err;
600
Petr Machatadbd6dc72018-05-30 02:56:55 +0200601 if (!br_vlan_is_brentry(vlan)) {
602 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200603 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
604 err = -EINVAL;
605 goto err_flags;
606 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200607 /* It was only kept for port vlans, now make it real */
608 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
609 vlan->vid);
610 if (err) {
611 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200612 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200613 }
614
615 refcount_inc(&vlan->refcnt);
616 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
617 vg->num_vlans++;
618 *changed = true;
619 }
620
621 if (__vlan_add_flags(vlan, flags))
622 *changed = true;
623
624 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200625
626err_fdb_insert:
627err_flags:
628 br_switchdev_port_vlan_del(br->dev, vlan->vid);
629 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200630}
631
Toshiaki Makita8adff412013-10-16 17:07:13 +0900632/* Must be protected by RTNL.
633 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300634 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900635 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300636int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000637{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200638 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200639 struct net_bridge_vlan *vlan;
640 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000641
642 ASSERT_RTNL();
643
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300644 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200645 vg = br_vlan_group(br);
646 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200647 if (vlan)
648 return br_vlan_add_existing(br, vg, vlan, flags, changed);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000649
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200650 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
651 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000652 return -ENOMEM;
653
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200654 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
655 if (!vlan->stats) {
656 kfree(vlan);
657 return -ENOMEM;
658 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200659 vlan->vid = vid;
660 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
661 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
662 vlan->br = br;
663 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300664 refcount_set(&vlan->refcnt, 1);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200665 ret = __vlan_add(vlan, flags);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200666 if (ret) {
667 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200668 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300669 } else {
670 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200671 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000672
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200673 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000674}
675
Toshiaki Makita8adff412013-10-16 17:07:13 +0900676/* Must be protected by RTNL.
677 * Must be called with vid in range from 1 to 4094 inclusive.
678 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000679int br_vlan_delete(struct net_bridge *br, u16 vid)
680{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200681 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200682 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000683
684 ASSERT_RTNL();
685
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200686 vg = br_vlan_group(br);
687 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200688 if (!v || !br_vlan_is_brentry(v))
689 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000690
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900691 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700692 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000693
Roopa Prabhuefa53562017-01-31 22:59:54 -0800694 vlan_tunnel_info_del(vg, v);
695
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200696 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000697}
698
699void br_vlan_flush(struct net_bridge *br)
700{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200701 struct net_bridge_vlan_group *vg;
702
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000703 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000704
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200705 vg = br_vlan_group(br);
706 __vlan_flush(vg);
707 RCU_INIT_POINTER(br->vlgrp, NULL);
708 synchronize_rcu();
709 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000710}
711
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200712struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900713{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200714 if (!vg)
715 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900716
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200717 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900718}
719
Toshiaki Makita204177f2014-06-10 20:59:25 +0900720/* Must be protected by RTNL. */
721static void recalculate_group_addr(struct net_bridge *br)
722{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300723 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900724 return;
725
726 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300727 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
728 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900729 /* Bridge Group Address */
730 br->group_addr[5] = 0x00;
731 } else { /* vlan_enabled && ETH_P_8021AD */
732 /* Provider Bridge Group Address */
733 br->group_addr[5] = 0x08;
734 }
735 spin_unlock_bh(&br->lock);
736}
737
738/* Must be protected by RTNL. */
739void br_recalculate_fwd_mask(struct net_bridge *br)
740{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300741 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
742 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900743 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
744 else /* vlan_enabled && ETH_P_8021AD */
745 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
746 ~(1u << br->group_addr[5]);
747}
748
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300749int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000750{
Elad Raz6b72a772016-01-06 13:01:06 +0100751 struct switchdev_attr attr = {
752 .orig_dev = br->dev,
753 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
754 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
755 .u.vlan_filtering = val,
756 };
757 int err;
758
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300759 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300760 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000761
Elad Raz6b72a772016-01-06 13:01:06 +0100762 err = switchdev_port_attr_set(br->dev, &attr);
763 if (err && err != -EOPNOTSUPP)
764 return err;
765
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300766 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400767 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900768 recalculate_group_addr(br);
769 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000770
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300771 return 0;
772}
773
774int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
775{
Xin Long047831a2016-04-09 00:03:31 +0800776 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000777}
778
Ido Schimmel1f514452017-05-26 08:37:23 +0200779bool br_vlan_enabled(const struct net_device *dev)
780{
781 struct net_bridge *br = netdev_priv(dev);
782
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300783 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200784}
785EXPORT_SYMBOL_GPL(br_vlan_enabled);
786
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900787int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900788{
789 int err = 0;
790 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200791 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200792 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900793 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900794
Toshiaki Makita204177f2014-06-10 20:59:25 +0900795 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900796 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900797
798 /* Add VLANs for the new proto to the device filter. */
799 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200800 vg = nbp_vlan_group(p);
801 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200802 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900803 if (err)
804 goto err_filt;
805 }
806 }
807
808 oldproto = br->vlan_proto;
809 br->vlan_proto = proto;
810
811 recalculate_group_addr(br);
812 br_recalculate_fwd_mask(br);
813
814 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200815 list_for_each_entry(p, &br->port_list, list) {
816 vg = nbp_vlan_group(p);
817 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200818 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200819 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900820
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900821 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900822
823err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200824 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200825 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900826
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200827 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
828 vg = nbp_vlan_group(p);
829 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200830 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200831 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900832
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900833 return err;
834}
835
836int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
837{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900838 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
839 return -EPROTONOSUPPORT;
840
Xin Long047831a2016-04-09 00:03:31 +0800841 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900842}
843
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200844int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
845{
846 switch (val) {
847 case 0:
848 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300849 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200850 break;
851 default:
852 return -EINVAL;
853 }
854
855 return 0;
856}
857
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300858int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
859{
860 struct net_bridge_port *p;
861
862 /* allow to change the option if there are no port vlans configured */
863 list_for_each_entry(p, &br->port_list, list) {
864 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
865
866 if (vg->num_vlans)
867 return -EBUSY;
868 }
869
870 switch (val) {
871 case 0:
872 case 1:
873 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
874 break;
875 default:
876 return -EINVAL;
877 }
878
879 return 0;
880}
881
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200882static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400883{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200884 struct net_bridge_vlan *v;
885
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200886 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200887 return false;
888
889 v = br_vlan_lookup(&vg->vlan_hash, vid);
890 if (v && br_vlan_should_use(v) &&
891 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
892 return true;
893
894 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400895}
896
897static void br_vlan_disable_default_pvid(struct net_bridge *br)
898{
899 struct net_bridge_port *p;
900 u16 pvid = br->default_pvid;
901
902 /* Disable default_pvid on all ports where it is still
903 * configured.
904 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200905 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400906 br_vlan_delete(br, pvid);
907
908 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200909 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400910 nbp_vlan_delete(p, pvid);
911 }
912
913 br->default_pvid = 0;
914}
915
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200916int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400917{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200918 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200919 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400920 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300921 unsigned long *changed;
922 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400923 u16 old_pvid;
924 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400925
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200926 if (!pvid) {
927 br_vlan_disable_default_pvid(br);
928 return 0;
929 }
930
Andy Shevchenko459479d2018-08-30 13:33:18 +0300931 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400932 if (!changed)
933 return -ENOMEM;
934
935 old_pvid = br->default_pvid;
936
937 /* Update default_pvid config only if we do not conflict with
938 * user configuration.
939 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200940 vg = br_vlan_group(br);
941 pvent = br_vlan_find(vg, pvid);
942 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200943 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400944 err = br_vlan_add(br, pvid,
945 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200946 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300947 BRIDGE_VLAN_INFO_BRENTRY,
948 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400949 if (err)
950 goto out;
951 br_vlan_delete(br, old_pvid);
952 set_bit(0, changed);
953 }
954
955 list_for_each_entry(p, &br->port_list, list) {
956 /* Update default_pvid config only if we do not conflict with
957 * user configuration.
958 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200959 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400960 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200961 !vlan_default_pvid(vg, old_pvid)) ||
962 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400963 continue;
964
965 err = nbp_vlan_add(p, pvid,
966 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300967 BRIDGE_VLAN_INFO_UNTAGGED,
968 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400969 if (err)
970 goto err_port;
971 nbp_vlan_delete(p, old_pvid);
972 set_bit(p->port_no, changed);
973 }
974
975 br->default_pvid = pvid;
976
977out:
Andy Shevchenko459479d2018-08-30 13:33:18 +0300978 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400979 return err;
980
981err_port:
982 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
983 if (!test_bit(p->port_no, changed))
984 continue;
985
986 if (old_pvid)
987 nbp_vlan_add(p, old_pvid,
988 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300989 BRIDGE_VLAN_INFO_UNTAGGED,
990 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400991 nbp_vlan_delete(p, pvid);
992 }
993
994 if (test_bit(0, changed)) {
995 if (old_pvid)
996 br_vlan_add(br, old_pvid,
997 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200998 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300999 BRIDGE_VLAN_INFO_BRENTRY,
1000 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001001 br_vlan_delete(br, pvid);
1002 }
1003 goto out;
1004}
1005
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001006int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1007{
1008 u16 pvid = val;
1009 int err = 0;
1010
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001011 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001012 return -EINVAL;
1013
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001014 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001015 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001016
1017 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001018 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001019 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1020 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001021 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001022 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001023 err = __br_vlan_set_default_pvid(br, pvid);
Xin Long047831a2016-04-09 00:03:31 +08001024out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001025 return err;
1026}
1027
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001028int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001029{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001030 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001031 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001032 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001033
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001034 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1035 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001036 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001037 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001038 if (ret)
1039 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001040 ret = vlan_tunnel_init(vg);
1041 if (ret)
1042 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001043 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001044 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001045 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001046 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001047 ret = br_vlan_add(br, 1,
1048 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001049 BRIDGE_VLAN_INFO_BRENTRY, &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001050 if (ret)
1051 goto err_vlan_add;
1052
1053out:
1054 return ret;
1055
1056err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -08001057 vlan_tunnel_deinit(vg);
1058err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001059 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001060err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001061 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001062
1063 goto out;
1064}
1065
1066int nbp_vlan_init(struct net_bridge_port *p)
1067{
Elad Raz404cdbf2016-01-06 13:01:07 +01001068 struct switchdev_attr attr = {
1069 .orig_dev = p->br->dev,
1070 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1071 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001072 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001073 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001074 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001075 int ret = -ENOMEM;
1076
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001077 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1078 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001079 goto out;
1080
Elad Raz404cdbf2016-01-06 13:01:07 +01001081 ret = switchdev_port_attr_set(p->dev, &attr);
1082 if (ret && ret != -EOPNOTSUPP)
1083 goto err_vlan_enabled;
1084
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001085 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001086 if (ret)
1087 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001088 ret = vlan_tunnel_init(vg);
1089 if (ret)
1090 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001091 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001092 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001093 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001094 bool changed;
1095
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001096 ret = nbp_vlan_add(p, p->br->default_pvid,
1097 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001098 BRIDGE_VLAN_INFO_UNTAGGED,
1099 &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001100 if (ret)
1101 goto err_vlan_add;
1102 }
1103out:
1104 return ret;
1105
1106err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001107 RCU_INIT_POINTER(p->vlgrp, NULL);
1108 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001109 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001110err_tunnel_init:
1111 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001112err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001113err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001114 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001115
1116 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001117}
1118
Toshiaki Makita8adff412013-10-16 17:07:13 +09001119/* Must be protected by RTNL.
1120 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001121 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001122 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001123int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1124 bool *changed)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001125{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001126 struct net_bridge_vlan *vlan;
1127 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001128
1129 ASSERT_RTNL();
1130
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001131 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001132 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001133 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001134 /* Pass the flags to the hardware bridge */
Petr Machatad66e4342018-05-30 02:56:03 +02001135 ret = br_switchdev_port_vlan_add(port->dev, vid, flags);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001136 if (ret && ret != -EOPNOTSUPP)
1137 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001138 *changed = __vlan_add_flags(vlan, flags);
1139
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001140 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001141 }
1142
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001143 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1144 if (!vlan)
1145 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001146
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001147 vlan->vid = vid;
1148 vlan->port = port;
1149 ret = __vlan_add(vlan, flags);
1150 if (ret)
1151 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001152 else
1153 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001154
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001155 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001156}
1157
Toshiaki Makita8adff412013-10-16 17:07:13 +09001158/* Must be protected by RTNL.
1159 * Must be called with vid in range from 1 to 4094 inclusive.
1160 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001161int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1162{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001163 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001164
1165 ASSERT_RTNL();
1166
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001167 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001168 if (!v)
1169 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001170 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001171 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001172
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001173 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001174}
1175
1176void nbp_vlan_flush(struct net_bridge_port *port)
1177{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001178 struct net_bridge_vlan_group *vg;
1179
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001180 ASSERT_RTNL();
1181
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001182 vg = nbp_vlan_group(port);
1183 __vlan_flush(vg);
1184 RCU_INIT_POINTER(port->vlgrp, NULL);
1185 synchronize_rcu();
1186 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001187}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001188
1189void br_vlan_get_stats(const struct net_bridge_vlan *v,
1190 struct br_vlan_stats *stats)
1191{
1192 int i;
1193
1194 memset(stats, 0, sizeof(*stats));
1195 for_each_possible_cpu(i) {
1196 u64 rxpackets, rxbytes, txpackets, txbytes;
1197 struct br_vlan_stats *cpu_stats;
1198 unsigned int start;
1199
1200 cpu_stats = per_cpu_ptr(v->stats, i);
1201 do {
1202 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1203 rxpackets = cpu_stats->rx_packets;
1204 rxbytes = cpu_stats->rx_bytes;
1205 txbytes = cpu_stats->tx_bytes;
1206 txpackets = cpu_stats->tx_packets;
1207 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1208
1209 stats->rx_packets += rxpackets;
1210 stats->rx_bytes += rxbytes;
1211 stats->tx_bytes += txbytes;
1212 stats->tx_packets += txpackets;
1213 }
1214}
Petr Machata4d4fd362018-04-29 10:56:08 +03001215
1216int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1217{
1218 struct net_bridge_vlan_group *vg;
1219
1220 ASSERT_RTNL();
1221 if (netif_is_bridge_master(dev))
1222 vg = br_vlan_group(netdev_priv(dev));
1223 else
1224 return -EINVAL;
1225
1226 *p_pvid = br_get_pvid(vg);
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1230
1231int br_vlan_get_info(const struct net_device *dev, u16 vid,
1232 struct bridge_vlan_info *p_vinfo)
1233{
1234 struct net_bridge_vlan_group *vg;
1235 struct net_bridge_vlan *v;
1236 struct net_bridge_port *p;
1237
1238 ASSERT_RTNL();
1239 p = br_port_get_check_rtnl(dev);
1240 if (p)
1241 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001242 else if (netif_is_bridge_master(dev))
1243 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001244 else
1245 return -EINVAL;
1246
1247 v = br_vlan_find(vg, vid);
1248 if (!v)
1249 return -ENOENT;
1250
1251 p_vinfo->vid = vid;
1252 p_vinfo->flags = v->flags;
1253 return 0;
1254}
1255EXPORT_SYMBOL_GPL(br_vlan_get_info);