blob: b6de4f45716184d04f84886f8fdb0481c31012ad [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"
8
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02009static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
10 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000011{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020012 const struct net_bridge_vlan *vle = ptr;
13 u16 vid = *(u16 *)arg->key;
14
15 return vle->vid != vid;
16}
17
18static const struct rhashtable_params br_vlan_rht_params = {
19 .head_offset = offsetof(struct net_bridge_vlan, vnode),
20 .key_offset = offsetof(struct net_bridge_vlan, vid),
21 .key_len = sizeof(u16),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020022 .nelem_hint = 3,
23 .locks_mul = 1,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020024 .max_size = VLAN_N_VID,
25 .obj_cmpfn = br_vlan_cmp,
26 .automatic_shrinking = true,
27};
28
29static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
30{
31 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
32}
33
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020034static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020035{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020036 if (vg->pvid == vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000037 return;
38
39 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020040 vg->pvid = vid;
Vlad Yasevich552406c2013-02-13 12:00:15 +000041}
42
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020043static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000044{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020045 if (vg->pvid != vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000046 return;
47
48 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020049 vg->pvid = 0;
Vlad Yasevich552406c2013-02-13 12:00:15 +000050}
51
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020052static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000053{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020054 struct net_bridge_vlan_group *vg;
55
56 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020057 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020058 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020059 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020060
61 if (flags & BRIDGE_VLAN_INFO_PVID)
62 __vlan_add_pvid(vg, v->vid);
63 else
64 __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000065
66 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020067 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040068 else
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020069 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich35e03f32013-02-13 12:00:20 +000070}
71
Scott Feldman7f109532015-06-12 17:39:50 -070072static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
73 u16 vid, u16 flags)
74{
Jiri Pirko0944d6b2015-10-09 13:54:11 +020075 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010076 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +020077 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
78 .flags = flags,
79 .vid_begin = vid,
80 .vid_end = vid,
81 };
Scott Feldman7f109532015-06-12 17:39:50 -070082 int err;
83
Jiri Pirko0944d6b2015-10-09 13:54:11 +020084 /* Try switchdev op first. In case it is not supported, fallback to
85 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070086 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +020087 err = switchdev_port_obj_add(dev, &v.obj);
88 if (err == -EOPNOTSUPP)
89 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070090 return err;
91}
92
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020093static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000094{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020095 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020096 struct list_head *headp, *hpos;
97 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000098
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020099 if (br_vlan_is_master(v))
100 vg = br_vlan_group(v->br);
101 else
102 vg = nbp_vlan_group(v->port);
103
104 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200105 list_for_each_prev(hpos, headp) {
106 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
107 if (v->vid < vent->vid)
108 continue;
109 else
110 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000111 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200112 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200113}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000114
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200115static void __vlan_del_list(struct net_bridge_vlan *v)
116{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200117 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000118}
119
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400120static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
121 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700122{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200123 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100124 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200125 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
126 .vid_begin = vid,
127 .vid_end = vid,
128 };
129 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700130
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200131 /* Try switchdev op first. In case it is not supported, fallback to
132 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700133 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200134 err = switchdev_port_obj_del(dev, &v.obj);
135 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700136 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200137 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700138 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400139 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700140}
141
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200142/* Returns a master vlan, if it didn't exist it gets created. In all cases a
143 * a reference is taken to the master vlan before returning.
144 */
145static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
146{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200147 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200148 struct net_bridge_vlan *masterv;
149
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200150 vg = br_vlan_group(br);
151 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200152 if (!masterv) {
153 /* missing global ctx, create it now */
154 if (br_vlan_add(br, vid, 0))
155 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200156 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200157 if (WARN_ON(!masterv))
158 return NULL;
159 }
160 atomic_inc(&masterv->refcnt);
161
162 return masterv;
163}
164
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200165static void br_master_vlan_rcu_free(struct rcu_head *rcu)
166{
167 struct net_bridge_vlan *v;
168
169 v = container_of(rcu, struct net_bridge_vlan, rcu);
170 WARN_ON(!br_vlan_is_master(v));
171 free_percpu(v->stats);
172 v->stats = NULL;
173 kfree(v);
174}
175
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200176static void br_vlan_put_master(struct net_bridge_vlan *masterv)
177{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200178 struct net_bridge_vlan_group *vg;
179
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200180 if (!br_vlan_is_master(masterv))
181 return;
182
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200183 vg = br_vlan_group(masterv->br);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200184 if (atomic_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200185 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200186 &masterv->vnode, br_vlan_rht_params);
187 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200188 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200189 }
190}
191
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200192/* This is the shared VLAN add function which works for both ports and bridge
193 * devices. There are four possible calls to this function in terms of the
194 * vlan entry type:
195 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100196 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200197 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100198 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200199 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100200 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200201 * will be used for filtering in both the port and the bridge
202 */
203static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000204{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200205 struct net_bridge_vlan *masterv = NULL;
206 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200207 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200208 struct net_device *dev;
209 struct net_bridge *br;
210 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000211
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200212 if (br_vlan_is_master(v)) {
213 br = v->br;
214 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200215 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200216 } else {
217 p = v->port;
218 br = p->br;
219 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200220 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200221 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000222
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200223 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200224 /* Add VLAN to the device filter if it is supported.
225 * This ensures tagged traffic enters the bridge when
226 * promiscuous mode is disabled by br_manage_promisc().
227 */
228 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400229 if (err)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200230 goto out;
231
232 /* need to work on the master vlan too */
233 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrov2ffdf5082015-10-02 15:05:12 +0200234 err = br_vlan_add(br, v->vid, flags |
235 BRIDGE_VLAN_INFO_BRENTRY);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200236 if (err)
237 goto out_filt;
238 }
239
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200240 masterv = br_vlan_get_master(br, v->vid);
241 if (!masterv)
242 goto out_filt;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200243 v->brvlan = masterv;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200244 v->stats = masterv->stats;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900245 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000246
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200247 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200248 if (br_vlan_should_use(v)) {
249 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
250 if (err) {
251 br_err(br, "failed insert local address into bridge forwarding table\n");
252 goto out_filt;
253 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200254 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000255 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200256
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200257 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
258 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200259 if (err)
260 goto out_fdb_insert;
261
262 __vlan_add_list(v);
263 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200264out:
265 return err;
266
267out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200268 if (br_vlan_should_use(v)) {
269 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
270 vg->num_vlans--;
271 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200272
273out_filt:
274 if (p) {
275 __vlan_vid_del(dev, br, v->vid);
276 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200277 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200278 v->brvlan = NULL;
279 }
280 }
281
282 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000283}
284
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200285static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000286{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200287 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200288 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200289 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200290 int err = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200291
292 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200293 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200294 } else {
295 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200296 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200297 masterv = v->brvlan;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200298 }
299
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200300 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200301 if (p) {
302 err = __vlan_vid_del(p->dev, p->br, v->vid);
303 if (err)
304 goto out;
305 }
306
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200307 if (br_vlan_should_use(v)) {
308 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
309 vg->num_vlans--;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200310 }
311
312 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200313 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
314 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200315 __vlan_del_list(v);
316 kfree_rcu(v, rcu);
317 }
318
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200319 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200320out:
321 return err;
322}
323
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200324static void __vlan_group_free(struct net_bridge_vlan_group *vg)
325{
326 WARN_ON(!list_empty(&vg->vlan_list));
327 rhashtable_destroy(&vg->vlan_hash);
328 kfree(vg);
329}
330
331static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200332{
333 struct net_bridge_vlan *vlan, *tmp;
334
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200335 __vlan_delete_pvid(vg, vg->pvid);
336 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200337 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000338}
339
Vlad Yasevich78851982013-02-13 12:00:14 +0000340struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200341 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000342 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000343{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200344 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200345 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000346 u16 vid;
347
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400348 /* If this packet was not filtered at input, let it pass */
349 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000350 goto out;
351
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200352 /* At this point, we know that the frame was filtered and contains
353 * a valid vlan id. If the vlan id has untagged flag set,
354 * send untagged; otherwise, send tagged.
355 */
356 br_vlan_get_tag(skb, &vid);
357 v = br_vlan_find(vg, vid);
358 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400359 * only exception is the bridge is set in promisc mode and the
360 * packet is destined for the bridge device. In this case
361 * pass the packet as is.
362 */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200363 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400364 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
365 goto out;
366 } else {
367 kfree_skb(skb);
368 return NULL;
369 }
370 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200371 if (br->vlan_stats_enabled) {
372 stats = this_cpu_ptr(v->stats);
373 u64_stats_update_begin(&stats->syncp);
374 stats->tx_bytes += skb->len;
375 stats->tx_packets++;
376 u64_stats_update_end(&stats->syncp);
377 }
378
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200379 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900380 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000381out:
382 return skb;
383}
384
385/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200386static bool __allowed_ingress(const struct net_bridge *br,
387 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200388 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000389{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200390 struct br_vlan_stats *stats;
391 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900392 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000393
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400394 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900395 /* If vlan tx offload is disabled on bridge device and frame was
396 * sent from vlan device on the bridge device, it does not have
397 * HW accelerated vlan tag.
398 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100399 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200400 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400401 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900402 if (unlikely(!skb))
403 return false;
404 }
405
Toshiaki Makita8580e212014-06-10 20:59:23 +0900406 if (!br_vlan_get_tag(skb, vid)) {
407 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200408 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900409 /* Protocol-mismatch, empty out vlan_tci for new tag */
410 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100411 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100412 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900413 if (unlikely(!skb))
414 return false;
415
416 skb_pull(skb, ETH_HLEN);
417 skb_reset_mac_len(skb);
418 *vid = 0;
419 tagged = false;
420 } else {
421 tagged = true;
422 }
423 } else {
424 /* Untagged frame */
425 tagged = false;
426 }
427
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900428 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200429 u16 pvid = br_get_pvid(vg);
430
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900431 /* Frame had a tag with VID 0 or did not have a tag.
432 * See if pvid is set on this port. That tells us which
433 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000434 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400435 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900436 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000437
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900438 /* PVID is set on this port. Any untagged or priority-tagged
439 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000440 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900441 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900442 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900443 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200444 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900445 else
446 /* Priority-tagged Frame.
447 * At this point, We know that skb->vlan_tci had
448 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
449 * We update only VID field and preserve PCP field.
450 */
451 skb->vlan_tci |= pvid;
452
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200453 /* if stats are disabled we can avoid the lookup */
454 if (!br->vlan_stats_enabled)
455 return true;
456 }
457 v = br_vlan_find(vg, *vid);
458 if (!v || !br_vlan_should_use(v))
459 goto drop;
460
461 if (br->vlan_stats_enabled) {
462 stats = this_cpu_ptr(v->stats);
463 u64_stats_update_begin(&stats->syncp);
464 stats->rx_bytes += skb->len;
465 stats->rx_packets++;
466 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000467 }
468
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200469 return true;
470
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900471drop:
472 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000473 return false;
474}
475
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200476bool br_allowed_ingress(const struct net_bridge *br,
477 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
478 u16 *vid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200479{
480 /* If VLAN filtering is disabled on the bridge, all packets are
481 * permitted.
482 */
483 if (!br->vlan_enabled) {
484 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
485 return true;
486 }
487
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200488 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200489}
490
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000491/* Called under RCU. */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200492bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000493 const struct sk_buff *skb)
494{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200495 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000496 u16 vid;
497
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400498 /* If this packet was not filtered at input, let it pass */
499 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000500 return true;
501
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000502 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200503 v = br_vlan_find(vg, vid);
504 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000505 return true;
506
507 return false;
508}
509
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900510/* Called under RCU */
511bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
512{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200513 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900514 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900515
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400516 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400517 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900518 return true;
519
Ido Schimmeleca1e002015-11-01 18:31:45 +0200520 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200521 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900522 return false;
523
Toshiaki Makita8580e212014-06-10 20:59:23 +0900524 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
525 *vid = 0;
526
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900527 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200528 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400529 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900530 return false;
531
532 return true;
533 }
534
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200535 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900536 return true;
537
538 return false;
539}
540
Toshiaki Makita8adff412013-10-16 17:07:13 +0900541/* Must be protected by RTNL.
542 * Must be called with vid in range from 1 to 4094 inclusive.
543 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000544int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000545{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200546 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200547 struct net_bridge_vlan *vlan;
548 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000549
550 ASSERT_RTNL();
551
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200552 vg = br_vlan_group(br);
553 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200554 if (vlan) {
555 if (!br_vlan_is_brentry(vlan)) {
556 /* Trying to change flags of non-existent bridge vlan */
557 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
558 return -EINVAL;
559 /* It was only kept for port vlans, now make it real */
560 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
561 vlan->vid);
562 if (ret) {
563 br_err(br, "failed insert local address into bridge forwarding table\n");
564 return ret;
565 }
566 atomic_inc(&vlan->refcnt);
567 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200568 vg->num_vlans++;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200569 }
570 __vlan_add_flags(vlan, flags);
571 return 0;
572 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000573
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200574 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
575 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000576 return -ENOMEM;
577
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200578 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
579 if (!vlan->stats) {
580 kfree(vlan);
581 return -ENOMEM;
582 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200583 vlan->vid = vid;
584 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
585 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
586 vlan->br = br;
587 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
588 atomic_set(&vlan->refcnt, 1);
589 ret = __vlan_add(vlan, flags);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200590 if (ret) {
591 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200592 kfree(vlan);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200593 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000594
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200595 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000596}
597
Toshiaki Makita8adff412013-10-16 17:07:13 +0900598/* Must be protected by RTNL.
599 * Must be called with vid in range from 1 to 4094 inclusive.
600 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000601int br_vlan_delete(struct net_bridge *br, u16 vid)
602{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200603 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200604 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000605
606 ASSERT_RTNL();
607
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200608 vg = br_vlan_group(br);
609 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200610 if (!v || !br_vlan_is_brentry(v))
611 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000612
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900613 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700614 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000615
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200616 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000617}
618
619void br_vlan_flush(struct net_bridge *br)
620{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200621 struct net_bridge_vlan_group *vg;
622
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000623 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000624
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200625 vg = br_vlan_group(br);
626 __vlan_flush(vg);
627 RCU_INIT_POINTER(br->vlgrp, NULL);
628 synchronize_rcu();
629 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000630}
631
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200632struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900633{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200634 if (!vg)
635 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900636
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200637 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900638}
639
Toshiaki Makita204177f2014-06-10 20:59:25 +0900640/* Must be protected by RTNL. */
641static void recalculate_group_addr(struct net_bridge *br)
642{
643 if (br->group_addr_set)
644 return;
645
646 spin_lock_bh(&br->lock);
647 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
648 /* Bridge Group Address */
649 br->group_addr[5] = 0x00;
650 } else { /* vlan_enabled && ETH_P_8021AD */
651 /* Provider Bridge Group Address */
652 br->group_addr[5] = 0x08;
653 }
654 spin_unlock_bh(&br->lock);
655}
656
657/* Must be protected by RTNL. */
658void br_recalculate_fwd_mask(struct net_bridge *br)
659{
660 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
661 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
662 else /* vlan_enabled && ETH_P_8021AD */
663 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
664 ~(1u << br->group_addr[5]);
665}
666
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300667int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000668{
Elad Raz6b72a772016-01-06 13:01:06 +0100669 struct switchdev_attr attr = {
670 .orig_dev = br->dev,
671 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
672 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
673 .u.vlan_filtering = val,
674 };
675 int err;
676
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000677 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300678 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000679
Elad Raz6b72a772016-01-06 13:01:06 +0100680 err = switchdev_port_attr_set(br->dev, &attr);
681 if (err && err != -EOPNOTSUPP)
682 return err;
683
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000684 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400685 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900686 recalculate_group_addr(br);
687 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000688
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300689 return 0;
690}
691
692int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
693{
Xin Long047831a2016-04-09 00:03:31 +0800694 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000695}
696
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900697int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900698{
699 int err = 0;
700 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200701 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200702 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900703 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900704
Toshiaki Makita204177f2014-06-10 20:59:25 +0900705 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900706 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900707
708 /* Add VLANs for the new proto to the device filter. */
709 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200710 vg = nbp_vlan_group(p);
711 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200712 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900713 if (err)
714 goto err_filt;
715 }
716 }
717
718 oldproto = br->vlan_proto;
719 br->vlan_proto = proto;
720
721 recalculate_group_addr(br);
722 br_recalculate_fwd_mask(br);
723
724 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200725 list_for_each_entry(p, &br->port_list, list) {
726 vg = nbp_vlan_group(p);
727 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200728 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200729 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900730
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900731 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900732
733err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200734 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200735 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900736
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200737 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
738 vg = nbp_vlan_group(p);
739 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200740 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200741 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900742
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900743 return err;
744}
745
746int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
747{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900748 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
749 return -EPROTONOSUPPORT;
750
Xin Long047831a2016-04-09 00:03:31 +0800751 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900752}
753
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200754int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
755{
756 switch (val) {
757 case 0:
758 case 1:
759 br->vlan_stats_enabled = val;
760 break;
761 default:
762 return -EINVAL;
763 }
764
765 return 0;
766}
767
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200768static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400769{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200770 struct net_bridge_vlan *v;
771
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200772 if (vid != vg->pvid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200773 return false;
774
775 v = br_vlan_lookup(&vg->vlan_hash, vid);
776 if (v && br_vlan_should_use(v) &&
777 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
778 return true;
779
780 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400781}
782
783static void br_vlan_disable_default_pvid(struct net_bridge *br)
784{
785 struct net_bridge_port *p;
786 u16 pvid = br->default_pvid;
787
788 /* Disable default_pvid on all ports where it is still
789 * configured.
790 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200791 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400792 br_vlan_delete(br, pvid);
793
794 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200795 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400796 nbp_vlan_delete(p, pvid);
797 }
798
799 br->default_pvid = 0;
800}
801
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200802int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400803{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200804 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200805 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400806 struct net_bridge_port *p;
807 u16 old_pvid;
808 int err = 0;
809 unsigned long *changed;
810
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200811 if (!pvid) {
812 br_vlan_disable_default_pvid(br);
813 return 0;
814 }
815
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400816 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
817 GFP_KERNEL);
818 if (!changed)
819 return -ENOMEM;
820
821 old_pvid = br->default_pvid;
822
823 /* Update default_pvid config only if we do not conflict with
824 * user configuration.
825 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200826 vg = br_vlan_group(br);
827 pvent = br_vlan_find(vg, pvid);
828 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200829 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400830 err = br_vlan_add(br, pvid,
831 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200832 BRIDGE_VLAN_INFO_UNTAGGED |
833 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400834 if (err)
835 goto out;
836 br_vlan_delete(br, old_pvid);
837 set_bit(0, changed);
838 }
839
840 list_for_each_entry(p, &br->port_list, list) {
841 /* Update default_pvid config only if we do not conflict with
842 * user configuration.
843 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200844 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400845 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200846 !vlan_default_pvid(vg, old_pvid)) ||
847 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400848 continue;
849
850 err = nbp_vlan_add(p, pvid,
851 BRIDGE_VLAN_INFO_PVID |
852 BRIDGE_VLAN_INFO_UNTAGGED);
853 if (err)
854 goto err_port;
855 nbp_vlan_delete(p, old_pvid);
856 set_bit(p->port_no, changed);
857 }
858
859 br->default_pvid = pvid;
860
861out:
862 kfree(changed);
863 return err;
864
865err_port:
866 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
867 if (!test_bit(p->port_no, changed))
868 continue;
869
870 if (old_pvid)
871 nbp_vlan_add(p, old_pvid,
872 BRIDGE_VLAN_INFO_PVID |
873 BRIDGE_VLAN_INFO_UNTAGGED);
874 nbp_vlan_delete(p, pvid);
875 }
876
877 if (test_bit(0, changed)) {
878 if (old_pvid)
879 br_vlan_add(br, old_pvid,
880 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200881 BRIDGE_VLAN_INFO_UNTAGGED |
882 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400883 br_vlan_delete(br, pvid);
884 }
885 goto out;
886}
887
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400888int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
889{
890 u16 pvid = val;
891 int err = 0;
892
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400893 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400894 return -EINVAL;
895
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400896 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +0800897 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400898
899 /* Only allow default pvid change when filtering is disabled */
900 if (br->vlan_enabled) {
901 pr_info_once("Please disable vlan filtering to change default_pvid\n");
902 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +0800903 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400904 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200905 err = __br_vlan_set_default_pvid(br, pvid);
Xin Long047831a2016-04-09 00:03:31 +0800906out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400907 return err;
908}
909
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400910int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900911{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200912 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200913 int ret = -ENOMEM;
914
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200915 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
916 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200917 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200918 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200919 if (ret)
920 goto err_rhtbl;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200921 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900922 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400923 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200924 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200925 ret = br_vlan_add(br, 1,
926 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
927 BRIDGE_VLAN_INFO_BRENTRY);
928 if (ret)
929 goto err_vlan_add;
930
931out:
932 return ret;
933
934err_vlan_add:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200935 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200936err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200937 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200938
939 goto out;
940}
941
942int nbp_vlan_init(struct net_bridge_port *p)
943{
Elad Raz404cdbf2016-01-06 13:01:07 +0100944 struct switchdev_attr attr = {
945 .orig_dev = p->br->dev,
946 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
947 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
948 .u.vlan_filtering = p->br->vlan_enabled,
949 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200950 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200951 int ret = -ENOMEM;
952
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200953 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
954 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200955 goto out;
956
Elad Raz404cdbf2016-01-06 13:01:07 +0100957 ret = switchdev_port_attr_set(p->dev, &attr);
958 if (ret && ret != -EOPNOTSUPP)
959 goto err_vlan_enabled;
960
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200961 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200962 if (ret)
963 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200964 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200965 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200966 if (p->br->default_pvid) {
967 ret = nbp_vlan_add(p, p->br->default_pvid,
968 BRIDGE_VLAN_INFO_PVID |
969 BRIDGE_VLAN_INFO_UNTAGGED);
970 if (ret)
971 goto err_vlan_add;
972 }
973out:
974 return ret;
975
976err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +0100977 RCU_INIT_POINTER(p->vlgrp, NULL);
978 synchronize_rcu();
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200979 rhashtable_destroy(&vg->vlan_hash);
Elad Raz404cdbf2016-01-06 13:01:07 +0100980err_vlan_enabled:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200981err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200982 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200983
984 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900985}
986
Toshiaki Makita8adff412013-10-16 17:07:13 +0900987/* Must be protected by RTNL.
988 * Must be called with vid in range from 1 to 4094 inclusive.
989 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000990int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000991{
Ido Schimmel7fbac982016-02-18 14:01:46 +0100992 struct switchdev_obj_port_vlan v = {
993 .obj.orig_dev = port->dev,
994 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
995 .flags = flags,
996 .vid_begin = vid,
997 .vid_end = vid,
998 };
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200999 struct net_bridge_vlan *vlan;
1000 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001001
1002 ASSERT_RTNL();
1003
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001004 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001005 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001006 /* Pass the flags to the hardware bridge */
1007 ret = switchdev_port_obj_add(port->dev, &v.obj);
1008 if (ret && ret != -EOPNOTSUPP)
1009 return ret;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001010 __vlan_add_flags(vlan, flags);
1011 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001012 }
1013
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001014 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1015 if (!vlan)
1016 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001017
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001018 vlan->vid = vid;
1019 vlan->port = port;
1020 ret = __vlan_add(vlan, flags);
1021 if (ret)
1022 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001023
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001024 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001025}
1026
Toshiaki Makita8adff412013-10-16 17:07:13 +09001027/* Must be protected by RTNL.
1028 * Must be called with vid in range from 1 to 4094 inclusive.
1029 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001030int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1031{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001032 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001033
1034 ASSERT_RTNL();
1035
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001036 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001037 if (!v)
1038 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001039 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001040 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001041
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001042 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001043}
1044
1045void nbp_vlan_flush(struct net_bridge_port *port)
1046{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001047 struct net_bridge_vlan_group *vg;
1048
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001049 ASSERT_RTNL();
1050
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001051 vg = nbp_vlan_group(port);
1052 __vlan_flush(vg);
1053 RCU_INIT_POINTER(port->vlgrp, NULL);
1054 synchronize_rcu();
1055 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001056}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001057
1058void br_vlan_get_stats(const struct net_bridge_vlan *v,
1059 struct br_vlan_stats *stats)
1060{
1061 int i;
1062
1063 memset(stats, 0, sizeof(*stats));
1064 for_each_possible_cpu(i) {
1065 u64 rxpackets, rxbytes, txpackets, txbytes;
1066 struct br_vlan_stats *cpu_stats;
1067 unsigned int start;
1068
1069 cpu_stats = per_cpu_ptr(v->stats, i);
1070 do {
1071 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1072 rxpackets = cpu_stats->rx_packets;
1073 rxbytes = cpu_stats->rx_bytes;
1074 txbytes = cpu_stats->tx_bytes;
1075 txpackets = cpu_stats->tx_packets;
1076 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1077
1078 stats->rx_packets += rxpackets;
1079 stats->rx_bytes += rxbytes;
1080 stats->tx_bytes += txbytes;
1081 stats->tx_packets += txpackets;
1082 }
1083}