blob: 51935270c6512529b87bb252dcc64f6be17719f1 [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{
Jiri Pirko0944d6b2015-10-09 13:54:11 +020085 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010086 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +020087 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
88 .flags = flags,
89 .vid_begin = vid,
90 .vid_end = vid,
91 };
Scott Feldman7f109532015-06-12 17:39:50 -070092 int err;
93
Jiri Pirko0944d6b2015-10-09 13:54:11 +020094 /* Try switchdev op first. In case it is not supported, fallback to
95 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070096 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +020097 err = switchdev_port_obj_add(dev, &v.obj);
98 if (err == -EOPNOTSUPP)
99 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -0700100 return err;
101}
102
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200103static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000104{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200105 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200106 struct list_head *headp, *hpos;
107 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000108
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200109 if (br_vlan_is_master(v))
110 vg = br_vlan_group(v->br);
111 else
112 vg = nbp_vlan_group(v->port);
113
114 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200115 list_for_each_prev(hpos, headp) {
116 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
117 if (v->vid < vent->vid)
118 continue;
119 else
120 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000121 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200122 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200123}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000124
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200125static void __vlan_del_list(struct net_bridge_vlan *v)
126{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200127 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000128}
129
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400130static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
131 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700132{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200133 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100134 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200135 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
136 .vid_begin = vid,
137 .vid_end = vid,
138 };
139 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700140
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200141 /* Try switchdev op first. In case it is not supported, fallback to
142 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700143 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200144 err = switchdev_port_obj_del(dev, &v.obj);
145 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700146 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200147 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700148 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400149 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700150}
151
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200152/* Returns a master vlan, if it didn't exist it gets created. In all cases a
153 * a reference is taken to the master vlan before returning.
154 */
155static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
156{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200157 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 struct net_bridge_vlan *masterv;
159
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200160 vg = br_vlan_group(br);
161 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200162 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300163 bool changed;
164
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200165 /* missing global ctx, create it now */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300166 if (br_vlan_add(br, vid, 0, &changed))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200167 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200168 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200169 if (WARN_ON(!masterv))
170 return NULL;
171 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300172 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200173
174 return masterv;
175}
176
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200177static void br_master_vlan_rcu_free(struct rcu_head *rcu)
178{
179 struct net_bridge_vlan *v;
180
181 v = container_of(rcu, struct net_bridge_vlan, rcu);
182 WARN_ON(!br_vlan_is_master(v));
183 free_percpu(v->stats);
184 v->stats = NULL;
185 kfree(v);
186}
187
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200188static void br_vlan_put_master(struct net_bridge_vlan *masterv)
189{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200190 struct net_bridge_vlan_group *vg;
191
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200192 if (!br_vlan_is_master(masterv))
193 return;
194
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200195 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300196 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200197 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200198 &masterv->vnode, br_vlan_rht_params);
199 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200200 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200201 }
202}
203
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200204/* This is the shared VLAN add function which works for both ports and bridge
205 * devices. There are four possible calls to this function in terms of the
206 * vlan entry type:
207 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100208 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200209 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100210 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200211 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100212 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200213 * will be used for filtering in both the port and the bridge
214 */
215static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000216{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 struct net_bridge_vlan *masterv = NULL;
218 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200219 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200220 struct net_device *dev;
221 struct net_bridge *br;
222 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000223
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200224 if (br_vlan_is_master(v)) {
225 br = v->br;
226 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200227 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200228 } else {
229 p = v->port;
230 br = p->br;
231 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200232 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200233 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000234
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200235 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200236 /* Add VLAN to the device filter if it is supported.
237 * This ensures tagged traffic enters the bridge when
238 * promiscuous mode is disabled by br_manage_promisc().
239 */
240 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400241 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 goto out;
243
244 /* need to work on the master vlan too */
245 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300246 bool changed;
247
248 err = br_vlan_add(br, v->vid,
249 flags | BRIDGE_VLAN_INFO_BRENTRY,
250 &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200251 if (err)
252 goto out_filt;
253 }
254
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200255 masterv = br_vlan_get_master(br, v->vid);
256 if (!masterv)
257 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200258 v->brvlan = masterv;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200259 v->stats = masterv->stats;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900260 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000261
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200262 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200263 if (br_vlan_should_use(v)) {
264 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
265 if (err) {
266 br_err(br, "failed insert local address into bridge forwarding table\n");
267 goto out_filt;
268 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200269 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000270 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200271
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200272 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
273 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200274 if (err)
275 goto out_fdb_insert;
276
277 __vlan_add_list(v);
278 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200279out:
280 return err;
281
282out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200283 if (br_vlan_should_use(v)) {
284 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
285 vg->num_vlans--;
286 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200287
288out_filt:
289 if (p) {
290 __vlan_vid_del(dev, br, v->vid);
291 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200292 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200293 v->brvlan = NULL;
294 }
295 }
296
297 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000298}
299
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200300static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000301{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200302 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200303 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200304 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306
307 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200308 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200309 } else {
310 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200311 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200312 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200313 }
314
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200315 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200316 if (p) {
317 err = __vlan_vid_del(p->dev, p->br, v->vid);
318 if (err)
319 goto out;
320 }
321
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200322 if (br_vlan_should_use(v)) {
323 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
324 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 }
326
327 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800328 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200329 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
330 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200331 __vlan_del_list(v);
332 kfree_rcu(v, rcu);
333 }
334
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200335 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336out:
337 return err;
338}
339
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200340static void __vlan_group_free(struct net_bridge_vlan_group *vg)
341{
342 WARN_ON(!list_empty(&vg->vlan_list));
343 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800344 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200345 kfree(vg);
346}
347
348static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349{
350 struct net_bridge_vlan *vlan, *tmp;
351
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200352 __vlan_delete_pvid(vg, vg->pvid);
353 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000355}
356
Vlad Yasevich78851982013-02-13 12:00:14 +0000357struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800358 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000360 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000361{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200362 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200363 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000364 u16 vid;
365
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400366 /* If this packet was not filtered at input, let it pass */
367 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000368 goto out;
369
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200370 /* At this point, we know that the frame was filtered and contains
371 * a valid vlan id. If the vlan id has untagged flag set,
372 * send untagged; otherwise, send tagged.
373 */
374 br_vlan_get_tag(skb, &vid);
375 v = br_vlan_find(vg, vid);
376 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400377 * only exception is the bridge is set in promisc mode and the
378 * packet is destined for the bridge device. In this case
379 * pass the packet as is.
380 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200381 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400382 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
383 goto out;
384 } else {
385 kfree_skb(skb);
386 return NULL;
387 }
388 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200389 if (br->vlan_stats_enabled) {
390 stats = this_cpu_ptr(v->stats);
391 u64_stats_update_begin(&stats->syncp);
392 stats->tx_bytes += skb->len;
393 stats->tx_packets++;
394 u64_stats_update_end(&stats->syncp);
395 }
396
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200397 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900398 skb->vlan_tci = 0;
Roopa Prabhu11538d02017-01-31 22:59:55 -0800399
400 if (p && (p->flags & BR_VLAN_TUNNEL) &&
401 br_handle_egress_vlan_tunnel(skb, v)) {
402 kfree_skb(skb);
403 return NULL;
404 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000405out:
406 return skb;
407}
408
409/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200410static bool __allowed_ingress(const struct net_bridge *br,
411 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200412 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000413{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200414 struct br_vlan_stats *stats;
415 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900416 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000417
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400418 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900419 /* If vlan tx offload is disabled on bridge device and frame was
420 * sent from vlan device on the bridge device, it does not have
421 * HW accelerated vlan tag.
422 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100423 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200424 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400425 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900426 if (unlikely(!skb))
427 return false;
428 }
429
Toshiaki Makita8580e212014-06-10 20:59:23 +0900430 if (!br_vlan_get_tag(skb, vid)) {
431 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200432 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900433 /* Protocol-mismatch, empty out vlan_tci for new tag */
434 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100435 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100436 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900437 if (unlikely(!skb))
438 return false;
439
440 skb_pull(skb, ETH_HLEN);
441 skb_reset_mac_len(skb);
442 *vid = 0;
443 tagged = false;
444 } else {
445 tagged = true;
446 }
447 } else {
448 /* Untagged frame */
449 tagged = false;
450 }
451
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900452 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200453 u16 pvid = br_get_pvid(vg);
454
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900455 /* Frame had a tag with VID 0 or did not have a tag.
456 * See if pvid is set on this port. That tells us which
457 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000458 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400459 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900460 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000461
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900462 /* PVID is set on this port. Any untagged or priority-tagged
463 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000464 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900465 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900466 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900467 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200468 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900469 else
470 /* Priority-tagged Frame.
471 * At this point, We know that skb->vlan_tci had
472 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
473 * We update only VID field and preserve PCP field.
474 */
475 skb->vlan_tci |= pvid;
476
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200477 /* if stats are disabled we can avoid the lookup */
478 if (!br->vlan_stats_enabled)
479 return true;
480 }
481 v = br_vlan_find(vg, *vid);
482 if (!v || !br_vlan_should_use(v))
483 goto drop;
484
485 if (br->vlan_stats_enabled) {
486 stats = this_cpu_ptr(v->stats);
487 u64_stats_update_begin(&stats->syncp);
488 stats->rx_bytes += skb->len;
489 stats->rx_packets++;
490 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000491 }
492
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200493 return true;
494
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900495drop:
496 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000497 return false;
498}
499
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200500bool br_allowed_ingress(const struct net_bridge *br,
501 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
502 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200503{
504 /* If VLAN filtering is disabled on the bridge, all packets are
505 * permitted.
506 */
507 if (!br->vlan_enabled) {
508 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
509 return true;
510 }
511
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200512 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200513}
514
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000515/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200516bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000517 const struct sk_buff *skb)
518{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200519 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000520 u16 vid;
521
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400522 /* If this packet was not filtered at input, let it pass */
523 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000524 return true;
525
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000526 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200527 v = br_vlan_find(vg, vid);
528 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000529 return true;
530
531 return false;
532}
533
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900534/* Called under RCU */
535bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
536{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200537 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900538 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900539
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400540 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400541 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900542 return true;
543
Ido Schimmeleca1e002015-11-01 18:31:45 +0200544 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200545 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900546 return false;
547
Toshiaki Makita8580e212014-06-10 20:59:23 +0900548 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
549 *vid = 0;
550
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900551 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200552 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400553 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900554 return false;
555
556 return true;
557 }
558
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200559 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900560 return true;
561
562 return false;
563}
564
Toshiaki Makita8adff412013-10-16 17:07:13 +0900565/* Must be protected by RTNL.
566 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300567 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900568 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300569int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000570{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200571 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200572 struct net_bridge_vlan *vlan;
573 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000574
575 ASSERT_RTNL();
576
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300577 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200578 vg = br_vlan_group(br);
579 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200580 if (vlan) {
581 if (!br_vlan_is_brentry(vlan)) {
582 /* Trying to change flags of non-existent bridge vlan */
583 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
584 return -EINVAL;
585 /* It was only kept for port vlans, now make it real */
586 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
587 vlan->vid);
588 if (ret) {
589 br_err(br, "failed insert local address into bridge forwarding table\n");
590 return ret;
591 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300592 refcount_inc(&vlan->refcnt);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200593 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200594 vg->num_vlans++;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300595 *changed = true;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200596 }
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300597 if (__vlan_add_flags(vlan, flags))
598 *changed = true;
599
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200600 return 0;
601 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000602
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200603 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
604 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000605 return -ENOMEM;
606
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200607 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
608 if (!vlan->stats) {
609 kfree(vlan);
610 return -ENOMEM;
611 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200612 vlan->vid = vid;
613 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
614 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
615 vlan->br = br;
616 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300617 refcount_set(&vlan->refcnt, 1);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200618 ret = __vlan_add(vlan, flags);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200619 if (ret) {
620 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200621 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300622 } else {
623 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200624 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000625
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200626 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000627}
628
Toshiaki Makita8adff412013-10-16 17:07:13 +0900629/* Must be protected by RTNL.
630 * Must be called with vid in range from 1 to 4094 inclusive.
631 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000632int br_vlan_delete(struct net_bridge *br, u16 vid)
633{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200634 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200635 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000636
637 ASSERT_RTNL();
638
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200639 vg = br_vlan_group(br);
640 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200641 if (!v || !br_vlan_is_brentry(v))
642 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000643
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900644 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700645 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000646
Roopa Prabhuefa53562017-01-31 22:59:54 -0800647 vlan_tunnel_info_del(vg, v);
648
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200649 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000650}
651
652void br_vlan_flush(struct net_bridge *br)
653{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200654 struct net_bridge_vlan_group *vg;
655
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000656 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000657
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200658 vg = br_vlan_group(br);
659 __vlan_flush(vg);
660 RCU_INIT_POINTER(br->vlgrp, NULL);
661 synchronize_rcu();
662 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000663}
664
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200665struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900666{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200667 if (!vg)
668 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900669
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200670 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900671}
672
Toshiaki Makita204177f2014-06-10 20:59:25 +0900673/* Must be protected by RTNL. */
674static void recalculate_group_addr(struct net_bridge *br)
675{
676 if (br->group_addr_set)
677 return;
678
679 spin_lock_bh(&br->lock);
680 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
681 /* Bridge Group Address */
682 br->group_addr[5] = 0x00;
683 } else { /* vlan_enabled && ETH_P_8021AD */
684 /* Provider Bridge Group Address */
685 br->group_addr[5] = 0x08;
686 }
687 spin_unlock_bh(&br->lock);
688}
689
690/* Must be protected by RTNL. */
691void br_recalculate_fwd_mask(struct net_bridge *br)
692{
693 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
694 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
695 else /* vlan_enabled && ETH_P_8021AD */
696 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
697 ~(1u << br->group_addr[5]);
698}
699
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300700int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000701{
Elad Raz6b72a772016-01-06 13:01:06 +0100702 struct switchdev_attr attr = {
703 .orig_dev = br->dev,
704 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
705 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
706 .u.vlan_filtering = val,
707 };
708 int err;
709
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000710 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300711 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000712
Elad Raz6b72a772016-01-06 13:01:06 +0100713 err = switchdev_port_attr_set(br->dev, &attr);
714 if (err && err != -EOPNOTSUPP)
715 return err;
716
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000717 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400718 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900719 recalculate_group_addr(br);
720 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000721
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300722 return 0;
723}
724
725int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
726{
Xin Long047831a2016-04-09 00:03:31 +0800727 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000728}
729
Ido Schimmel1f514452017-05-26 08:37:23 +0200730bool br_vlan_enabled(const struct net_device *dev)
731{
732 struct net_bridge *br = netdev_priv(dev);
733
734 return !!br->vlan_enabled;
735}
736EXPORT_SYMBOL_GPL(br_vlan_enabled);
737
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900738int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900739{
740 int err = 0;
741 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200742 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200743 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900744 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900745
Toshiaki Makita204177f2014-06-10 20:59:25 +0900746 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900747 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900748
749 /* Add VLANs for the new proto to the device filter. */
750 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200751 vg = nbp_vlan_group(p);
752 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200753 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900754 if (err)
755 goto err_filt;
756 }
757 }
758
759 oldproto = br->vlan_proto;
760 br->vlan_proto = proto;
761
762 recalculate_group_addr(br);
763 br_recalculate_fwd_mask(br);
764
765 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200766 list_for_each_entry(p, &br->port_list, list) {
767 vg = nbp_vlan_group(p);
768 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200769 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200770 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900771
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900772 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900773
774err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200775 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200776 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900777
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200778 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
779 vg = nbp_vlan_group(p);
780 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200781 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200782 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900783
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900784 return err;
785}
786
787int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
788{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900789 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
790 return -EPROTONOSUPPORT;
791
Xin Long047831a2016-04-09 00:03:31 +0800792 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900793}
794
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200795int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
796{
797 switch (val) {
798 case 0:
799 case 1:
800 br->vlan_stats_enabled = val;
801 break;
802 default:
803 return -EINVAL;
804 }
805
806 return 0;
807}
808
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200809static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400810{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200811 struct net_bridge_vlan *v;
812
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200813 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200814 return false;
815
816 v = br_vlan_lookup(&vg->vlan_hash, vid);
817 if (v && br_vlan_should_use(v) &&
818 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
819 return true;
820
821 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400822}
823
824static void br_vlan_disable_default_pvid(struct net_bridge *br)
825{
826 struct net_bridge_port *p;
827 u16 pvid = br->default_pvid;
828
829 /* Disable default_pvid on all ports where it is still
830 * configured.
831 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200832 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400833 br_vlan_delete(br, pvid);
834
835 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200836 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400837 nbp_vlan_delete(p, pvid);
838 }
839
840 br->default_pvid = 0;
841}
842
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200843int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400844{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200845 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200846 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400847 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300848 unsigned long *changed;
849 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400850 u16 old_pvid;
851 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400852
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200853 if (!pvid) {
854 br_vlan_disable_default_pvid(br);
855 return 0;
856 }
857
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400858 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
859 GFP_KERNEL);
860 if (!changed)
861 return -ENOMEM;
862
863 old_pvid = br->default_pvid;
864
865 /* Update default_pvid config only if we do not conflict with
866 * user configuration.
867 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200868 vg = br_vlan_group(br);
869 pvent = br_vlan_find(vg, pvid);
870 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200871 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400872 err = br_vlan_add(br, pvid,
873 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200874 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300875 BRIDGE_VLAN_INFO_BRENTRY,
876 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400877 if (err)
878 goto out;
879 br_vlan_delete(br, old_pvid);
880 set_bit(0, changed);
881 }
882
883 list_for_each_entry(p, &br->port_list, list) {
884 /* Update default_pvid config only if we do not conflict with
885 * user configuration.
886 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200887 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400888 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200889 !vlan_default_pvid(vg, old_pvid)) ||
890 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400891 continue;
892
893 err = nbp_vlan_add(p, pvid,
894 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300895 BRIDGE_VLAN_INFO_UNTAGGED,
896 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400897 if (err)
898 goto err_port;
899 nbp_vlan_delete(p, old_pvid);
900 set_bit(p->port_no, changed);
901 }
902
903 br->default_pvid = pvid;
904
905out:
906 kfree(changed);
907 return err;
908
909err_port:
910 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
911 if (!test_bit(p->port_no, changed))
912 continue;
913
914 if (old_pvid)
915 nbp_vlan_add(p, old_pvid,
916 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300917 BRIDGE_VLAN_INFO_UNTAGGED,
918 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400919 nbp_vlan_delete(p, pvid);
920 }
921
922 if (test_bit(0, changed)) {
923 if (old_pvid)
924 br_vlan_add(br, old_pvid,
925 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200926 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300927 BRIDGE_VLAN_INFO_BRENTRY,
928 &vlchange);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400929 br_vlan_delete(br, pvid);
930 }
931 goto out;
932}
933
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400934int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
935{
936 u16 pvid = val;
937 int err = 0;
938
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400939 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400940 return -EINVAL;
941
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400942 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +0800943 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400944
945 /* Only allow default pvid change when filtering is disabled */
946 if (br->vlan_enabled) {
947 pr_info_once("Please disable vlan filtering to change default_pvid\n");
948 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +0800949 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400950 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200951 err = __br_vlan_set_default_pvid(br, pvid);
Xin Long047831a2016-04-09 00:03:31 +0800952out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400953 return err;
954}
955
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400956int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900957{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200958 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200959 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300960 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200961
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200962 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
963 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200964 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200965 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200966 if (ret)
967 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800968 ret = vlan_tunnel_init(vg);
969 if (ret)
970 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200971 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900972 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400973 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200974 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200975 ret = br_vlan_add(br, 1,
976 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300977 BRIDGE_VLAN_INFO_BRENTRY, &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200978 if (ret)
979 goto err_vlan_add;
980
981out:
982 return ret;
983
984err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -0800985 vlan_tunnel_deinit(vg);
986err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200987 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200988err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200989 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200990
991 goto out;
992}
993
994int nbp_vlan_init(struct net_bridge_port *p)
995{
Elad Raz404cdbf2016-01-06 13:01:07 +0100996 struct switchdev_attr attr = {
997 .orig_dev = p->br->dev,
998 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
999 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1000 .u.vlan_filtering = p->br->vlan_enabled,
1001 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001002 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001003 int ret = -ENOMEM;
1004
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001005 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1006 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001007 goto out;
1008
Elad Raz404cdbf2016-01-06 13:01:07 +01001009 ret = switchdev_port_attr_set(p->dev, &attr);
1010 if (ret && ret != -EOPNOTSUPP)
1011 goto err_vlan_enabled;
1012
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001013 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001014 if (ret)
1015 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001016 ret = vlan_tunnel_init(vg);
1017 if (ret)
1018 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001019 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001020 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001021 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001022 bool changed;
1023
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001024 ret = nbp_vlan_add(p, p->br->default_pvid,
1025 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001026 BRIDGE_VLAN_INFO_UNTAGGED,
1027 &changed);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001028 if (ret)
1029 goto err_vlan_add;
1030 }
1031out:
1032 return ret;
1033
1034err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001035 RCU_INIT_POINTER(p->vlgrp, NULL);
1036 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001037 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001038err_tunnel_init:
1039 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001040err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001041err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001042 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001043
1044 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001045}
1046
Toshiaki Makita8adff412013-10-16 17:07:13 +09001047/* Must be protected by RTNL.
1048 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001049 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001050 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001051int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1052 bool *changed)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001053{
Ido Schimmel7fbac982016-02-18 14:01:46 +01001054 struct switchdev_obj_port_vlan v = {
1055 .obj.orig_dev = port->dev,
1056 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1057 .flags = flags,
1058 .vid_begin = vid,
1059 .vid_end = vid,
1060 };
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001061 struct net_bridge_vlan *vlan;
1062 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001063
1064 ASSERT_RTNL();
1065
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001066 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001067 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001068 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001069 /* Pass the flags to the hardware bridge */
1070 ret = switchdev_port_obj_add(port->dev, &v.obj);
1071 if (ret && ret != -EOPNOTSUPP)
1072 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001073 *changed = __vlan_add_flags(vlan, flags);
1074
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001075 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001076 }
1077
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001078 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1079 if (!vlan)
1080 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001081
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001082 vlan->vid = vid;
1083 vlan->port = port;
1084 ret = __vlan_add(vlan, flags);
1085 if (ret)
1086 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001087 else
1088 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001089
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001090 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001091}
1092
Toshiaki Makita8adff412013-10-16 17:07:13 +09001093/* Must be protected by RTNL.
1094 * Must be called with vid in range from 1 to 4094 inclusive.
1095 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001096int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1097{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001098 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001099
1100 ASSERT_RTNL();
1101
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001102 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001103 if (!v)
1104 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001105 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001106 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001107
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001108 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001109}
1110
1111void nbp_vlan_flush(struct net_bridge_port *port)
1112{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001113 struct net_bridge_vlan_group *vg;
1114
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001115 ASSERT_RTNL();
1116
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001117 vg = nbp_vlan_group(port);
1118 __vlan_flush(vg);
1119 RCU_INIT_POINTER(port->vlgrp, NULL);
1120 synchronize_rcu();
1121 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001122}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001123
1124void br_vlan_get_stats(const struct net_bridge_vlan *v,
1125 struct br_vlan_stats *stats)
1126{
1127 int i;
1128
1129 memset(stats, 0, sizeof(*stats));
1130 for_each_possible_cpu(i) {
1131 u64 rxpackets, rxbytes, txpackets, txbytes;
1132 struct br_vlan_stats *cpu_stats;
1133 unsigned int start;
1134
1135 cpu_stats = per_cpu_ptr(v->stats, i);
1136 do {
1137 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1138 rxpackets = cpu_stats->rx_packets;
1139 rxbytes = cpu_stats->rx_bytes;
1140 txbytes = cpu_stats->tx_bytes;
1141 txpackets = cpu_stats->tx_packets;
1142 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1143
1144 stats->rx_packets += rxpackets;
1145 stats->rx_bytes += rxbytes;
1146 stats->tx_bytes += txbytes;
1147 stats->tx_packets += txpackets;
1148 }
1149}