blob: 26a1a56639b26f07ca40e661f74be4890f6e4b7a [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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov77751ee2015-09-30 20:16:53 +020035static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020036{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020037 if (vg->pvid == vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000038 return;
39
40 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020041 vg->pvid = vid;
Vlad Yasevich552406c2013-02-13 12:00:15 +000042}
43
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020044static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000045{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020046 if (vg->pvid != vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000047 return;
48
49 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020050 vg->pvid = 0;
Vlad Yasevich552406c2013-02-13 12:00:15 +000051}
52
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020053static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000054{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020055 struct net_bridge_vlan_group *vg;
56
57 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020058 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020059 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020060 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020061
62 if (flags & BRIDGE_VLAN_INFO_PVID)
63 __vlan_add_pvid(vg, v->vid);
64 else
65 __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000066
67 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020068 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040069 else
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020070 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich35e03f32013-02-13 12:00:20 +000071}
72
Scott Feldman7f109532015-06-12 17:39:50 -070073static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
74 u16 vid, u16 flags)
75{
Jiri Pirko0944d6b2015-10-09 13:54:11 +020076 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010077 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +020078 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
79 .flags = flags,
80 .vid_begin = vid,
81 .vid_end = vid,
82 };
Scott Feldman7f109532015-06-12 17:39:50 -070083 int err;
84
Jiri Pirko0944d6b2015-10-09 13:54:11 +020085 /* Try switchdev op first. In case it is not supported, fallback to
86 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070087 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +020088 err = switchdev_port_obj_add(dev, &v.obj);
89 if (err == -EOPNOTSUPP)
90 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070091 return err;
92}
93
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020094static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000095{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020096 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020097 struct list_head *headp, *hpos;
98 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000099
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200100 if (br_vlan_is_master(v))
101 vg = br_vlan_group(v->br);
102 else
103 vg = nbp_vlan_group(v->port);
104
105 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200106 list_for_each_prev(hpos, headp) {
107 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
108 if (v->vid < vent->vid)
109 continue;
110 else
111 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000112 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200113 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200114}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000115
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200116static void __vlan_del_list(struct net_bridge_vlan *v)
117{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200118 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000119}
120
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400121static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
122 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700123{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200124 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100125 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200126 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
127 .vid_begin = vid,
128 .vid_end = vid,
129 };
130 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700131
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 /* Try switchdev op first. In case it is not supported, fallback to
133 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700134 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200135 err = switchdev_port_obj_del(dev, &v.obj);
136 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700137 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200138 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700139 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400140 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700141}
142
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200143/* Returns a master vlan, if it didn't exist it gets created. In all cases a
144 * a reference is taken to the master vlan before returning.
145 */
146static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
147{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200148 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149 struct net_bridge_vlan *masterv;
150
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200151 vg = br_vlan_group(br);
152 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200153 if (!masterv) {
154 /* missing global ctx, create it now */
155 if (br_vlan_add(br, vid, 0))
156 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200157 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 if (WARN_ON(!masterv))
159 return NULL;
160 }
161 atomic_inc(&masterv->refcnt);
162
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);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200185 if (atomic_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 Aleksandrov2594e902015-09-25 19:00:11 +0200193/* This is the shared VLAN add function which works for both ports and bridge
194 * devices. There are four possible calls to this function in terms of the
195 * vlan entry type:
196 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100197 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200198 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100199 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200200 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100201 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200202 * will be used for filtering in both the port and the bridge
203 */
204static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000205{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200206 struct net_bridge_vlan *masterv = NULL;
207 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200208 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200209 struct net_device *dev;
210 struct net_bridge *br;
211 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000212
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200213 if (br_vlan_is_master(v)) {
214 br = v->br;
215 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200216 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200217 } else {
218 p = v->port;
219 br = p->br;
220 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200221 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200222 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000223
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200224 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200225 /* Add VLAN to the device filter if it is supported.
226 * This ensures tagged traffic enters the bridge when
227 * promiscuous mode is disabled by br_manage_promisc().
228 */
229 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400230 if (err)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200231 goto out;
232
233 /* need to work on the master vlan too */
234 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrov2ffdf5082015-10-02 15:05:12 +0200235 err = br_vlan_add(br, v->vid, flags |
236 BRIDGE_VLAN_INFO_BRENTRY);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200237 if (err)
238 goto out_filt;
239 }
240
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200241 masterv = br_vlan_get_master(br, v->vid);
242 if (!masterv)
243 goto out_filt;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200244 v->brvlan = masterv;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200245 v->stats = masterv->stats;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900246 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000247
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200248 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200249 if (br_vlan_should_use(v)) {
250 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
251 if (err) {
252 br_err(br, "failed insert local address into bridge forwarding table\n");
253 goto out_filt;
254 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200255 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000256 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200257
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200258 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
259 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200260 if (err)
261 goto out_fdb_insert;
262
263 __vlan_add_list(v);
264 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200265out:
266 return err;
267
268out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200269 if (br_vlan_should_use(v)) {
270 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
271 vg->num_vlans--;
272 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200273
274out_filt:
275 if (p) {
276 __vlan_vid_del(dev, br, v->vid);
277 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200278 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200279 v->brvlan = NULL;
280 }
281 }
282
283 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000284}
285
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200286static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000287{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200288 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200289 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200290 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200291 int err = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200292
293 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200294 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200295 } else {
296 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200297 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200298 masterv = v->brvlan;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200299 }
300
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200301 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200302 if (p) {
303 err = __vlan_vid_del(p->dev, p->br, v->vid);
304 if (err)
305 goto out;
306 }
307
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200308 if (br_vlan_should_use(v)) {
309 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
310 vg->num_vlans--;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200311 }
312
313 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800314 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200315 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
316 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200317 __vlan_del_list(v);
318 kfree_rcu(v, rcu);
319 }
320
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200321 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200322out:
323 return err;
324}
325
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200326static void __vlan_group_free(struct net_bridge_vlan_group *vg)
327{
328 WARN_ON(!list_empty(&vg->vlan_list));
329 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800330 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200331 kfree(vg);
332}
333
334static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200335{
336 struct net_bridge_vlan *vlan, *tmp;
337
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200338 __vlan_delete_pvid(vg, vg->pvid);
339 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200340 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000341}
342
Vlad Yasevich78851982013-02-13 12:00:14 +0000343struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800344 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200345 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000346 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000347{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200348 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200349 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000350 u16 vid;
351
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400352 /* If this packet was not filtered at input, let it pass */
353 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000354 goto out;
355
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200356 /* At this point, we know that the frame was filtered and contains
357 * a valid vlan id. If the vlan id has untagged flag set,
358 * send untagged; otherwise, send tagged.
359 */
360 br_vlan_get_tag(skb, &vid);
361 v = br_vlan_find(vg, vid);
362 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400363 * only exception is the bridge is set in promisc mode and the
364 * packet is destined for the bridge device. In this case
365 * pass the packet as is.
366 */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200367 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400368 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
369 goto out;
370 } else {
371 kfree_skb(skb);
372 return NULL;
373 }
374 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200375 if (br->vlan_stats_enabled) {
376 stats = this_cpu_ptr(v->stats);
377 u64_stats_update_begin(&stats->syncp);
378 stats->tx_bytes += skb->len;
379 stats->tx_packets++;
380 u64_stats_update_end(&stats->syncp);
381 }
382
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200383 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900384 skb->vlan_tci = 0;
Roopa Prabhu11538d02017-01-31 22:59:55 -0800385
386 if (p && (p->flags & BR_VLAN_TUNNEL) &&
387 br_handle_egress_vlan_tunnel(skb, v)) {
388 kfree_skb(skb);
389 return NULL;
390 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000391out:
392 return skb;
393}
394
395/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200396static bool __allowed_ingress(const struct net_bridge *br,
397 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200398 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000399{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200400 struct br_vlan_stats *stats;
401 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900402 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000403
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400404 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900405 /* If vlan tx offload is disabled on bridge device and frame was
406 * sent from vlan device on the bridge device, it does not have
407 * HW accelerated vlan tag.
408 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100409 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200410 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400411 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900412 if (unlikely(!skb))
413 return false;
414 }
415
Toshiaki Makita8580e212014-06-10 20:59:23 +0900416 if (!br_vlan_get_tag(skb, vid)) {
417 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200418 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900419 /* Protocol-mismatch, empty out vlan_tci for new tag */
420 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100421 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100422 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900423 if (unlikely(!skb))
424 return false;
425
426 skb_pull(skb, ETH_HLEN);
427 skb_reset_mac_len(skb);
428 *vid = 0;
429 tagged = false;
430 } else {
431 tagged = true;
432 }
433 } else {
434 /* Untagged frame */
435 tagged = false;
436 }
437
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900438 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200439 u16 pvid = br_get_pvid(vg);
440
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900441 /* Frame had a tag with VID 0 or did not have a tag.
442 * See if pvid is set on this port. That tells us which
443 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000444 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400445 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900446 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000447
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900448 /* PVID is set on this port. Any untagged or priority-tagged
449 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000450 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900451 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900452 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900453 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200454 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900455 else
456 /* Priority-tagged Frame.
457 * At this point, We know that skb->vlan_tci had
458 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
459 * We update only VID field and preserve PCP field.
460 */
461 skb->vlan_tci |= pvid;
462
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200463 /* if stats are disabled we can avoid the lookup */
464 if (!br->vlan_stats_enabled)
465 return true;
466 }
467 v = br_vlan_find(vg, *vid);
468 if (!v || !br_vlan_should_use(v))
469 goto drop;
470
471 if (br->vlan_stats_enabled) {
472 stats = this_cpu_ptr(v->stats);
473 u64_stats_update_begin(&stats->syncp);
474 stats->rx_bytes += skb->len;
475 stats->rx_packets++;
476 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000477 }
478
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200479 return true;
480
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900481drop:
482 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000483 return false;
484}
485
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200486bool br_allowed_ingress(const struct net_bridge *br,
487 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
488 u16 *vid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200489{
490 /* If VLAN filtering is disabled on the bridge, all packets are
491 * permitted.
492 */
493 if (!br->vlan_enabled) {
494 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
495 return true;
496 }
497
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200498 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200499}
500
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000501/* Called under RCU. */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200502bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000503 const struct sk_buff *skb)
504{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200505 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000506 u16 vid;
507
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400508 /* If this packet was not filtered at input, let it pass */
509 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000510 return true;
511
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000512 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200513 v = br_vlan_find(vg, vid);
514 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000515 return true;
516
517 return false;
518}
519
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900520/* Called under RCU */
521bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
522{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200523 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900524 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900525
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400526 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400527 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900528 return true;
529
Ido Schimmeleca1e002015-11-01 18:31:45 +0200530 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200531 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900532 return false;
533
Toshiaki Makita8580e212014-06-10 20:59:23 +0900534 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
535 *vid = 0;
536
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900537 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200538 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400539 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900540 return false;
541
542 return true;
543 }
544
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200545 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900546 return true;
547
548 return false;
549}
550
Toshiaki Makita8adff412013-10-16 17:07:13 +0900551/* Must be protected by RTNL.
552 * Must be called with vid in range from 1 to 4094 inclusive.
553 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000554int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000555{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200556 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200557 struct net_bridge_vlan *vlan;
558 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000559
560 ASSERT_RTNL();
561
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200562 vg = br_vlan_group(br);
563 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200564 if (vlan) {
565 if (!br_vlan_is_brentry(vlan)) {
566 /* Trying to change flags of non-existent bridge vlan */
567 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
568 return -EINVAL;
569 /* It was only kept for port vlans, now make it real */
570 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
571 vlan->vid);
572 if (ret) {
573 br_err(br, "failed insert local address into bridge forwarding table\n");
574 return ret;
575 }
576 atomic_inc(&vlan->refcnt);
577 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200578 vg->num_vlans++;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200579 }
580 __vlan_add_flags(vlan, flags);
581 return 0;
582 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000583
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200584 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
585 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000586 return -ENOMEM;
587
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200588 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
589 if (!vlan->stats) {
590 kfree(vlan);
591 return -ENOMEM;
592 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200593 vlan->vid = vid;
594 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
595 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
596 vlan->br = br;
597 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
598 atomic_set(&vlan->refcnt, 1);
599 ret = __vlan_add(vlan, flags);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200600 if (ret) {
601 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200602 kfree(vlan);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200603 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000604
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200605 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000606}
607
Toshiaki Makita8adff412013-10-16 17:07:13 +0900608/* Must be protected by RTNL.
609 * Must be called with vid in range from 1 to 4094 inclusive.
610 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000611int br_vlan_delete(struct net_bridge *br, u16 vid)
612{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200613 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200614 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000615
616 ASSERT_RTNL();
617
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200618 vg = br_vlan_group(br);
619 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200620 if (!v || !br_vlan_is_brentry(v))
621 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000622
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900623 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700624 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000625
Roopa Prabhuefa53562017-01-31 22:59:54 -0800626 vlan_tunnel_info_del(vg, v);
627
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200628 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000629}
630
631void br_vlan_flush(struct net_bridge *br)
632{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200633 struct net_bridge_vlan_group *vg;
634
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000635 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000636
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200637 vg = br_vlan_group(br);
638 __vlan_flush(vg);
639 RCU_INIT_POINTER(br->vlgrp, NULL);
640 synchronize_rcu();
641 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000642}
643
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200644struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900645{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200646 if (!vg)
647 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900648
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200649 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900650}
651
Toshiaki Makita204177f2014-06-10 20:59:25 +0900652/* Must be protected by RTNL. */
653static void recalculate_group_addr(struct net_bridge *br)
654{
655 if (br->group_addr_set)
656 return;
657
658 spin_lock_bh(&br->lock);
659 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
660 /* Bridge Group Address */
661 br->group_addr[5] = 0x00;
662 } else { /* vlan_enabled && ETH_P_8021AD */
663 /* Provider Bridge Group Address */
664 br->group_addr[5] = 0x08;
665 }
666 spin_unlock_bh(&br->lock);
667}
668
669/* Must be protected by RTNL. */
670void br_recalculate_fwd_mask(struct net_bridge *br)
671{
672 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
673 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
674 else /* vlan_enabled && ETH_P_8021AD */
675 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
676 ~(1u << br->group_addr[5]);
677}
678
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300679int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000680{
Elad Raz6b72a772016-01-06 13:01:06 +0100681 struct switchdev_attr attr = {
682 .orig_dev = br->dev,
683 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
684 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
685 .u.vlan_filtering = val,
686 };
687 int err;
688
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000689 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300690 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000691
Elad Raz6b72a772016-01-06 13:01:06 +0100692 err = switchdev_port_attr_set(br->dev, &attr);
693 if (err && err != -EOPNOTSUPP)
694 return err;
695
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000696 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400697 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900698 recalculate_group_addr(br);
699 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000700
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300701 return 0;
702}
703
704int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
705{
Xin Long047831a2016-04-09 00:03:31 +0800706 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000707}
708
Ido Schimmel1f514452017-05-26 08:37:23 +0200709bool br_vlan_enabled(const struct net_device *dev)
710{
711 struct net_bridge *br = netdev_priv(dev);
712
713 return !!br->vlan_enabled;
714}
715EXPORT_SYMBOL_GPL(br_vlan_enabled);
716
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900717int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900718{
719 int err = 0;
720 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200721 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200722 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900723 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900724
Toshiaki Makita204177f2014-06-10 20:59:25 +0900725 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900726 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900727
728 /* Add VLANs for the new proto to the device filter. */
729 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200730 vg = nbp_vlan_group(p);
731 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200732 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900733 if (err)
734 goto err_filt;
735 }
736 }
737
738 oldproto = br->vlan_proto;
739 br->vlan_proto = proto;
740
741 recalculate_group_addr(br);
742 br_recalculate_fwd_mask(br);
743
744 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200745 list_for_each_entry(p, &br->port_list, list) {
746 vg = nbp_vlan_group(p);
747 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200748 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200749 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900750
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900751 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900752
753err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200754 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200755 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900756
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200757 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
758 vg = nbp_vlan_group(p);
759 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200760 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200761 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900762
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900763 return err;
764}
765
766int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
767{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900768 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
769 return -EPROTONOSUPPORT;
770
Xin Long047831a2016-04-09 00:03:31 +0800771 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900772}
773
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200774int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
775{
776 switch (val) {
777 case 0:
778 case 1:
779 br->vlan_stats_enabled = val;
780 break;
781 default:
782 return -EINVAL;
783 }
784
785 return 0;
786}
787
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200788static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400789{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200790 struct net_bridge_vlan *v;
791
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200792 if (vid != vg->pvid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200793 return false;
794
795 v = br_vlan_lookup(&vg->vlan_hash, vid);
796 if (v && br_vlan_should_use(v) &&
797 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
798 return true;
799
800 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400801}
802
803static void br_vlan_disable_default_pvid(struct net_bridge *br)
804{
805 struct net_bridge_port *p;
806 u16 pvid = br->default_pvid;
807
808 /* Disable default_pvid on all ports where it is still
809 * configured.
810 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200811 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400812 br_vlan_delete(br, pvid);
813
814 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200815 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400816 nbp_vlan_delete(p, pvid);
817 }
818
819 br->default_pvid = 0;
820}
821
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200822int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400823{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200824 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200825 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400826 struct net_bridge_port *p;
827 u16 old_pvid;
828 int err = 0;
829 unsigned long *changed;
830
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200831 if (!pvid) {
832 br_vlan_disable_default_pvid(br);
833 return 0;
834 }
835
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400836 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
837 GFP_KERNEL);
838 if (!changed)
839 return -ENOMEM;
840
841 old_pvid = br->default_pvid;
842
843 /* Update default_pvid config only if we do not conflict with
844 * user configuration.
845 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200846 vg = br_vlan_group(br);
847 pvent = br_vlan_find(vg, pvid);
848 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200849 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400850 err = br_vlan_add(br, pvid,
851 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200852 BRIDGE_VLAN_INFO_UNTAGGED |
853 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400854 if (err)
855 goto out;
856 br_vlan_delete(br, old_pvid);
857 set_bit(0, changed);
858 }
859
860 list_for_each_entry(p, &br->port_list, list) {
861 /* Update default_pvid config only if we do not conflict with
862 * user configuration.
863 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200864 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400865 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200866 !vlan_default_pvid(vg, old_pvid)) ||
867 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400868 continue;
869
870 err = nbp_vlan_add(p, pvid,
871 BRIDGE_VLAN_INFO_PVID |
872 BRIDGE_VLAN_INFO_UNTAGGED);
873 if (err)
874 goto err_port;
875 nbp_vlan_delete(p, old_pvid);
876 set_bit(p->port_no, changed);
877 }
878
879 br->default_pvid = pvid;
880
881out:
882 kfree(changed);
883 return err;
884
885err_port:
886 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
887 if (!test_bit(p->port_no, changed))
888 continue;
889
890 if (old_pvid)
891 nbp_vlan_add(p, old_pvid,
892 BRIDGE_VLAN_INFO_PVID |
893 BRIDGE_VLAN_INFO_UNTAGGED);
894 nbp_vlan_delete(p, pvid);
895 }
896
897 if (test_bit(0, changed)) {
898 if (old_pvid)
899 br_vlan_add(br, old_pvid,
900 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200901 BRIDGE_VLAN_INFO_UNTAGGED |
902 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400903 br_vlan_delete(br, pvid);
904 }
905 goto out;
906}
907
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400908int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
909{
910 u16 pvid = val;
911 int err = 0;
912
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400913 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400914 return -EINVAL;
915
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400916 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +0800917 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400918
919 /* Only allow default pvid change when filtering is disabled */
920 if (br->vlan_enabled) {
921 pr_info_once("Please disable vlan filtering to change default_pvid\n");
922 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +0800923 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400924 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200925 err = __br_vlan_set_default_pvid(br, pvid);
Xin Long047831a2016-04-09 00:03:31 +0800926out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400927 return err;
928}
929
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400930int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900931{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200932 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200933 int ret = -ENOMEM;
934
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200935 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
936 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200937 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200938 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200939 if (ret)
940 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800941 ret = vlan_tunnel_init(vg);
942 if (ret)
943 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200944 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900945 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400946 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200947 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200948 ret = br_vlan_add(br, 1,
949 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
950 BRIDGE_VLAN_INFO_BRENTRY);
951 if (ret)
952 goto err_vlan_add;
953
954out:
955 return ret;
956
957err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -0800958 vlan_tunnel_deinit(vg);
959err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200960 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200961err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200962 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200963
964 goto out;
965}
966
967int nbp_vlan_init(struct net_bridge_port *p)
968{
Elad Raz404cdbf2016-01-06 13:01:07 +0100969 struct switchdev_attr attr = {
970 .orig_dev = p->br->dev,
971 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
972 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
973 .u.vlan_filtering = p->br->vlan_enabled,
974 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200975 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200976 int ret = -ENOMEM;
977
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200978 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
979 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200980 goto out;
981
Elad Raz404cdbf2016-01-06 13:01:07 +0100982 ret = switchdev_port_attr_set(p->dev, &attr);
983 if (ret && ret != -EOPNOTSUPP)
984 goto err_vlan_enabled;
985
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200986 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200987 if (ret)
988 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800989 ret = vlan_tunnel_init(vg);
990 if (ret)
991 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200992 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200993 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200994 if (p->br->default_pvid) {
995 ret = nbp_vlan_add(p, p->br->default_pvid,
996 BRIDGE_VLAN_INFO_PVID |
997 BRIDGE_VLAN_INFO_UNTAGGED);
998 if (ret)
999 goto err_vlan_add;
1000 }
1001out:
1002 return ret;
1003
1004err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001005 RCU_INIT_POINTER(p->vlgrp, NULL);
1006 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001007 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001008err_tunnel_init:
1009 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001010err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001011err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001012 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001013
1014 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001015}
1016
Toshiaki Makita8adff412013-10-16 17:07:13 +09001017/* Must be protected by RTNL.
1018 * Must be called with vid in range from 1 to 4094 inclusive.
1019 */
Vlad Yasevich552406c2013-02-13 12:00:15 +00001020int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001021{
Ido Schimmel7fbac982016-02-18 14:01:46 +01001022 struct switchdev_obj_port_vlan v = {
1023 .obj.orig_dev = port->dev,
1024 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1025 .flags = flags,
1026 .vid_begin = vid,
1027 .vid_end = vid,
1028 };
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001029 struct net_bridge_vlan *vlan;
1030 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001031
1032 ASSERT_RTNL();
1033
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001034 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001035 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001036 /* Pass the flags to the hardware bridge */
1037 ret = switchdev_port_obj_add(port->dev, &v.obj);
1038 if (ret && ret != -EOPNOTSUPP)
1039 return ret;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001040 __vlan_add_flags(vlan, flags);
1041 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001042 }
1043
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001044 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1045 if (!vlan)
1046 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001047
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001048 vlan->vid = vid;
1049 vlan->port = port;
1050 ret = __vlan_add(vlan, flags);
1051 if (ret)
1052 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001053
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001054 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001055}
1056
Toshiaki Makita8adff412013-10-16 17:07:13 +09001057/* Must be protected by RTNL.
1058 * Must be called with vid in range from 1 to 4094 inclusive.
1059 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001060int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1061{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001062 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001063
1064 ASSERT_RTNL();
1065
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001066 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001067 if (!v)
1068 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001069 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001070 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001071
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001072 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001073}
1074
1075void nbp_vlan_flush(struct net_bridge_port *port)
1076{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001077 struct net_bridge_vlan_group *vg;
1078
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001079 ASSERT_RTNL();
1080
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001081 vg = nbp_vlan_group(port);
1082 __vlan_flush(vg);
1083 RCU_INIT_POINTER(port->vlgrp, NULL);
1084 synchronize_rcu();
1085 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001086}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001087
1088void br_vlan_get_stats(const struct net_bridge_vlan *v,
1089 struct br_vlan_stats *stats)
1090{
1091 int i;
1092
1093 memset(stats, 0, sizeof(*stats));
1094 for_each_possible_cpu(i) {
1095 u64 rxpackets, rxbytes, txpackets, txbytes;
1096 struct br_vlan_stats *cpu_stats;
1097 unsigned int start;
1098
1099 cpu_stats = per_cpu_ptr(v->stats, i);
1100 do {
1101 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1102 rxpackets = cpu_stats->rx_packets;
1103 rxbytes = cpu_stats->rx_bytes;
1104 txbytes = cpu_stats->tx_bytes;
1105 txpackets = cpu_stats->tx_packets;
1106 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1107
1108 stats->rx_packets += rxpackets;
1109 stats->rx_bytes += rxbytes;
1110 stats->tx_bytes += txbytes;
1111 stats->tx_packets += txpackets;
1112 }
1113}