blob: 1394da63614a1d8a325ef44d5fa17849b5d82294 [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 = {
76 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
77 .flags = flags,
78 .vid_begin = vid,
79 .vid_end = vid,
80 };
Scott Feldman7f109532015-06-12 17:39:50 -070081 int err;
82
Jiri Pirko0944d6b2015-10-09 13:54:11 +020083 /* Try switchdev op first. In case it is not supported, fallback to
84 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070085 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +020086 err = switchdev_port_obj_add(dev, &v.obj);
87 if (err == -EOPNOTSUPP)
88 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070089 return err;
90}
91
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020092static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000093{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020094 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020095 struct list_head *headp, *hpos;
96 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000097
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020098 if (br_vlan_is_master(v))
99 vg = br_vlan_group(v->br);
100 else
101 vg = nbp_vlan_group(v->port);
102
103 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200104 list_for_each_prev(hpos, headp) {
105 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
106 if (v->vid < vent->vid)
107 continue;
108 else
109 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000110 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200111 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200112}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000113
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200114static void __vlan_del_list(struct net_bridge_vlan *v)
115{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200116 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000117}
118
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400119static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
120 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700121{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200122 struct switchdev_obj_port_vlan v = {
123 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
124 .vid_begin = vid,
125 .vid_end = vid,
126 };
127 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700128
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200129 /* Try switchdev op first. In case it is not supported, fallback to
130 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700131 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 err = switchdev_port_obj_del(dev, &v.obj);
133 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700134 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200135 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700136 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400137 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700138}
139
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200140/* Returns a master vlan, if it didn't exist it gets created. In all cases a
141 * a reference is taken to the master vlan before returning.
142 */
143static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
144{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200145 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200146 struct net_bridge_vlan *masterv;
147
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200148 vg = br_vlan_group(br);
149 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200150 if (!masterv) {
151 /* missing global ctx, create it now */
152 if (br_vlan_add(br, vid, 0))
153 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200154 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200155 if (WARN_ON(!masterv))
156 return NULL;
157 }
158 atomic_inc(&masterv->refcnt);
159
160 return masterv;
161}
162
163static void br_vlan_put_master(struct net_bridge_vlan *masterv)
164{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200165 struct net_bridge_vlan_group *vg;
166
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200167 if (!br_vlan_is_master(masterv))
168 return;
169
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200170 vg = br_vlan_group(masterv->br);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200171 if (atomic_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200172 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200173 &masterv->vnode, br_vlan_rht_params);
174 __vlan_del_list(masterv);
175 kfree_rcu(masterv, rcu);
176 }
177}
178
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200179/* This is the shared VLAN add function which works for both ports and bridge
180 * devices. There are four possible calls to this function in terms of the
181 * vlan entry type:
182 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100183 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200184 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100185 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200186 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100187 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200188 * will be used for filtering in both the port and the bridge
189 */
190static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000191{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200192 struct net_bridge_vlan *masterv = NULL;
193 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200194 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200195 struct net_device *dev;
196 struct net_bridge *br;
197 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000198
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200199 if (br_vlan_is_master(v)) {
200 br = v->br;
201 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200202 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200203 } else {
204 p = v->port;
205 br = p->br;
206 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200207 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200208 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000209
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200210 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200211 /* Add VLAN to the device filter if it is supported.
212 * This ensures tagged traffic enters the bridge when
213 * promiscuous mode is disabled by br_manage_promisc().
214 */
215 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400216 if (err)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200217 goto out;
218
219 /* need to work on the master vlan too */
220 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrov2ffdf5082015-10-02 15:05:12 +0200221 err = br_vlan_add(br, v->vid, flags |
222 BRIDGE_VLAN_INFO_BRENTRY);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200223 if (err)
224 goto out_filt;
225 }
226
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200227 masterv = br_vlan_get_master(br, v->vid);
228 if (!masterv)
229 goto out_filt;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200230 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900231 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000232
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200233 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200234 if (br_vlan_should_use(v)) {
235 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
236 if (err) {
237 br_err(br, "failed insert local address into bridge forwarding table\n");
238 goto out_filt;
239 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200240 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000241 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200242
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200243 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
244 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200245 if (err)
246 goto out_fdb_insert;
247
248 __vlan_add_list(v);
249 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200250out:
251 return err;
252
253out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200254 if (br_vlan_should_use(v)) {
255 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
256 vg->num_vlans--;
257 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200258
259out_filt:
260 if (p) {
261 __vlan_vid_del(dev, br, v->vid);
262 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200263 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200264 v->brvlan = NULL;
265 }
266 }
267
268 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000269}
270
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200271static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000272{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200273 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200274 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200275 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200276 int err = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200277
278 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200279 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200280 } else {
281 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200282 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200283 masterv = v->brvlan;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200284 }
285
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200286 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200287 if (p) {
288 err = __vlan_vid_del(p->dev, p->br, v->vid);
289 if (err)
290 goto out;
291 }
292
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200293 if (br_vlan_should_use(v)) {
294 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
295 vg->num_vlans--;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200296 }
297
298 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200299 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
300 br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200301 __vlan_del_list(v);
302 kfree_rcu(v, rcu);
303 }
304
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200305 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200306out:
307 return err;
308}
309
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200310static void __vlan_group_free(struct net_bridge_vlan_group *vg)
311{
312 WARN_ON(!list_empty(&vg->vlan_list));
313 rhashtable_destroy(&vg->vlan_hash);
314 kfree(vg);
315}
316
317static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200318{
319 struct net_bridge_vlan *vlan, *tmp;
320
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200321 __vlan_delete_pvid(vg, vg->pvid);
322 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200323 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000324}
325
Vlad Yasevich78851982013-02-13 12:00:14 +0000326struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200327 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000328 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000329{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200330 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000331 u16 vid;
332
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400333 /* If this packet was not filtered at input, let it pass */
334 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000335 goto out;
336
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200337 /* At this point, we know that the frame was filtered and contains
338 * a valid vlan id. If the vlan id has untagged flag set,
339 * send untagged; otherwise, send tagged.
340 */
341 br_vlan_get_tag(skb, &vid);
342 v = br_vlan_find(vg, vid);
343 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400344 * only exception is the bridge is set in promisc mode and the
345 * packet is destined for the bridge device. In this case
346 * pass the packet as is.
347 */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200348 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400349 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
350 goto out;
351 } else {
352 kfree_skb(skb);
353 return NULL;
354 }
355 }
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200356 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900357 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000358
359out:
360 return skb;
361}
362
363/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200364static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200365 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000366{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200367 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900368 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000369
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400370 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900371 /* If vlan tx offload is disabled on bridge device and frame was
372 * sent from vlan device on the bridge device, it does not have
373 * HW accelerated vlan tag.
374 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100375 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900376 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400377 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900378 if (unlikely(!skb))
379 return false;
380 }
381
Toshiaki Makita8580e212014-06-10 20:59:23 +0900382 if (!br_vlan_get_tag(skb, vid)) {
383 /* Tagged frame */
384 if (skb->vlan_proto != proto) {
385 /* Protocol-mismatch, empty out vlan_tci for new tag */
386 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100387 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100388 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900389 if (unlikely(!skb))
390 return false;
391
392 skb_pull(skb, ETH_HLEN);
393 skb_reset_mac_len(skb);
394 *vid = 0;
395 tagged = false;
396 } else {
397 tagged = true;
398 }
399 } else {
400 /* Untagged frame */
401 tagged = false;
402 }
403
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900404 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200405 u16 pvid = br_get_pvid(vg);
406
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900407 /* Frame had a tag with VID 0 or did not have a tag.
408 * See if pvid is set on this port. That tells us which
409 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000410 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400411 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900412 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000413
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900414 /* PVID is set on this port. Any untagged or priority-tagged
415 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000416 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900417 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900418 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900419 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900420 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900421 else
422 /* Priority-tagged Frame.
423 * At this point, We know that skb->vlan_tci had
424 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
425 * We update only VID field and preserve PCP field.
426 */
427 skb->vlan_tci |= pvid;
428
Vlad Yasevich78851982013-02-13 12:00:14 +0000429 return true;
430 }
431
432 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200433 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200434 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000435 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900436drop:
437 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000438 return false;
439}
440
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200441bool br_allowed_ingress(const struct net_bridge *br,
442 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
443 u16 *vid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200444{
445 /* If VLAN filtering is disabled on the bridge, all packets are
446 * permitted.
447 */
448 if (!br->vlan_enabled) {
449 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
450 return true;
451 }
452
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200453 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200454}
455
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000456/* Called under RCU. */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200457bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000458 const struct sk_buff *skb)
459{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200460 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000461 u16 vid;
462
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400463 /* If this packet was not filtered at input, let it pass */
464 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000465 return true;
466
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000467 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200468 v = br_vlan_find(vg, vid);
469 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000470 return true;
471
472 return false;
473}
474
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900475/* Called under RCU */
476bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
477{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200478 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900479 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900480
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400481 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400482 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900483 return true;
484
Ido Schimmeleca1e002015-11-01 18:31:45 +0200485 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200486 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900487 return false;
488
Toshiaki Makita8580e212014-06-10 20:59:23 +0900489 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
490 *vid = 0;
491
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900492 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200493 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400494 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900495 return false;
496
497 return true;
498 }
499
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200500 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900501 return true;
502
503 return false;
504}
505
Toshiaki Makita8adff412013-10-16 17:07:13 +0900506/* Must be protected by RTNL.
507 * Must be called with vid in range from 1 to 4094 inclusive.
508 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000509int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000510{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200511 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200512 struct net_bridge_vlan *vlan;
513 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000514
515 ASSERT_RTNL();
516
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200517 vg = br_vlan_group(br);
518 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200519 if (vlan) {
520 if (!br_vlan_is_brentry(vlan)) {
521 /* Trying to change flags of non-existent bridge vlan */
522 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
523 return -EINVAL;
524 /* It was only kept for port vlans, now make it real */
525 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
526 vlan->vid);
527 if (ret) {
528 br_err(br, "failed insert local address into bridge forwarding table\n");
529 return ret;
530 }
531 atomic_inc(&vlan->refcnt);
532 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200533 vg->num_vlans++;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200534 }
535 __vlan_add_flags(vlan, flags);
536 return 0;
537 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000538
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200539 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
540 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000541 return -ENOMEM;
542
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200543 vlan->vid = vid;
544 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
545 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
546 vlan->br = br;
547 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
548 atomic_set(&vlan->refcnt, 1);
549 ret = __vlan_add(vlan, flags);
550 if (ret)
551 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000552
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200553 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000554}
555
Toshiaki Makita8adff412013-10-16 17:07:13 +0900556/* Must be protected by RTNL.
557 * Must be called with vid in range from 1 to 4094 inclusive.
558 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000559int br_vlan_delete(struct net_bridge *br, u16 vid)
560{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200561 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200562 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000563
564 ASSERT_RTNL();
565
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200566 vg = br_vlan_group(br);
567 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200568 if (!v || !br_vlan_is_brentry(v))
569 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000570
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900571 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700572 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000573
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200574 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000575}
576
577void br_vlan_flush(struct net_bridge *br)
578{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200579 struct net_bridge_vlan_group *vg;
580
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000581 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000582
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200583 vg = br_vlan_group(br);
584 __vlan_flush(vg);
585 RCU_INIT_POINTER(br->vlgrp, NULL);
586 synchronize_rcu();
587 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000588}
589
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200590struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900591{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200592 if (!vg)
593 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900594
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200595 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900596}
597
Toshiaki Makita204177f2014-06-10 20:59:25 +0900598/* Must be protected by RTNL. */
599static void recalculate_group_addr(struct net_bridge *br)
600{
601 if (br->group_addr_set)
602 return;
603
604 spin_lock_bh(&br->lock);
605 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
606 /* Bridge Group Address */
607 br->group_addr[5] = 0x00;
608 } else { /* vlan_enabled && ETH_P_8021AD */
609 /* Provider Bridge Group Address */
610 br->group_addr[5] = 0x08;
611 }
612 spin_unlock_bh(&br->lock);
613}
614
615/* Must be protected by RTNL. */
616void br_recalculate_fwd_mask(struct net_bridge *br)
617{
618 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
619 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
620 else /* vlan_enabled && ETH_P_8021AD */
621 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
622 ~(1u << br->group_addr[5]);
623}
624
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300625int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000626{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000627 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300628 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000629
630 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400631 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900632 recalculate_group_addr(br);
633 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000634
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300635 return 0;
636}
637
638int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
639{
640 if (!rtnl_trylock())
641 return restart_syscall();
642
643 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000644 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300645
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000646 return 0;
647}
648
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900649int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900650{
651 int err = 0;
652 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200653 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200654 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900655 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900656
Toshiaki Makita204177f2014-06-10 20:59:25 +0900657 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900658 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900659
660 /* Add VLANs for the new proto to the device filter. */
661 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200662 vg = nbp_vlan_group(p);
663 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200664 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900665 if (err)
666 goto err_filt;
667 }
668 }
669
670 oldproto = br->vlan_proto;
671 br->vlan_proto = proto;
672
673 recalculate_group_addr(br);
674 br_recalculate_fwd_mask(br);
675
676 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200677 list_for_each_entry(p, &br->port_list, list) {
678 vg = nbp_vlan_group(p);
679 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200680 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200681 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900682
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900683 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900684
685err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200686 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200687 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900688
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200689 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
690 vg = nbp_vlan_group(p);
691 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200692 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200693 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900694
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900695 return err;
696}
697
698int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
699{
700 int err;
701
702 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
703 return -EPROTONOSUPPORT;
704
705 if (!rtnl_trylock())
706 return restart_syscall();
707
708 err = __br_vlan_set_proto(br, htons(val));
709 rtnl_unlock();
710
711 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900712}
713
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200714static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400715{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200716 struct net_bridge_vlan *v;
717
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200718 if (vid != vg->pvid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200719 return false;
720
721 v = br_vlan_lookup(&vg->vlan_hash, vid);
722 if (v && br_vlan_should_use(v) &&
723 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
724 return true;
725
726 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400727}
728
729static void br_vlan_disable_default_pvid(struct net_bridge *br)
730{
731 struct net_bridge_port *p;
732 u16 pvid = br->default_pvid;
733
734 /* Disable default_pvid on all ports where it is still
735 * configured.
736 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200737 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400738 br_vlan_delete(br, pvid);
739
740 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200741 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400742 nbp_vlan_delete(p, pvid);
743 }
744
745 br->default_pvid = 0;
746}
747
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200748int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400749{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200750 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200751 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400752 struct net_bridge_port *p;
753 u16 old_pvid;
754 int err = 0;
755 unsigned long *changed;
756
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200757 if (!pvid) {
758 br_vlan_disable_default_pvid(br);
759 return 0;
760 }
761
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400762 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
763 GFP_KERNEL);
764 if (!changed)
765 return -ENOMEM;
766
767 old_pvid = br->default_pvid;
768
769 /* Update default_pvid config only if we do not conflict with
770 * user configuration.
771 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200772 vg = br_vlan_group(br);
773 pvent = br_vlan_find(vg, pvid);
774 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200775 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400776 err = br_vlan_add(br, pvid,
777 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200778 BRIDGE_VLAN_INFO_UNTAGGED |
779 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400780 if (err)
781 goto out;
782 br_vlan_delete(br, old_pvid);
783 set_bit(0, changed);
784 }
785
786 list_for_each_entry(p, &br->port_list, list) {
787 /* Update default_pvid config only if we do not conflict with
788 * user configuration.
789 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200790 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400791 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200792 !vlan_default_pvid(vg, old_pvid)) ||
793 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400794 continue;
795
796 err = nbp_vlan_add(p, pvid,
797 BRIDGE_VLAN_INFO_PVID |
798 BRIDGE_VLAN_INFO_UNTAGGED);
799 if (err)
800 goto err_port;
801 nbp_vlan_delete(p, old_pvid);
802 set_bit(p->port_no, changed);
803 }
804
805 br->default_pvid = pvid;
806
807out:
808 kfree(changed);
809 return err;
810
811err_port:
812 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
813 if (!test_bit(p->port_no, changed))
814 continue;
815
816 if (old_pvid)
817 nbp_vlan_add(p, old_pvid,
818 BRIDGE_VLAN_INFO_PVID |
819 BRIDGE_VLAN_INFO_UNTAGGED);
820 nbp_vlan_delete(p, pvid);
821 }
822
823 if (test_bit(0, changed)) {
824 if (old_pvid)
825 br_vlan_add(br, old_pvid,
826 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200827 BRIDGE_VLAN_INFO_UNTAGGED |
828 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400829 br_vlan_delete(br, pvid);
830 }
831 goto out;
832}
833
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400834int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
835{
836 u16 pvid = val;
837 int err = 0;
838
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400839 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400840 return -EINVAL;
841
842 if (!rtnl_trylock())
843 return restart_syscall();
844
845 if (pvid == br->default_pvid)
846 goto unlock;
847
848 /* Only allow default pvid change when filtering is disabled */
849 if (br->vlan_enabled) {
850 pr_info_once("Please disable vlan filtering to change default_pvid\n");
851 err = -EPERM;
852 goto unlock;
853 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200854 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400855unlock:
856 rtnl_unlock();
857 return err;
858}
859
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400860int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900861{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200862 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200863 int ret = -ENOMEM;
864
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200865 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
866 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200867 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200868 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200869 if (ret)
870 goto err_rhtbl;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200871 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900872 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400873 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200874 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200875 ret = br_vlan_add(br, 1,
876 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
877 BRIDGE_VLAN_INFO_BRENTRY);
878 if (ret)
879 goto err_vlan_add;
880
881out:
882 return ret;
883
884err_vlan_add:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200885 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200886err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200887 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200888
889 goto out;
890}
891
892int nbp_vlan_init(struct net_bridge_port *p)
893{
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200894 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200895 int ret = -ENOMEM;
896
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200897 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
898 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200899 goto out;
900
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200901 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200902 if (ret)
903 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200904 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200905 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200906 if (p->br->default_pvid) {
907 ret = nbp_vlan_add(p, p->br->default_pvid,
908 BRIDGE_VLAN_INFO_PVID |
909 BRIDGE_VLAN_INFO_UNTAGGED);
910 if (ret)
911 goto err_vlan_add;
912 }
913out:
914 return ret;
915
916err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +0100917 RCU_INIT_POINTER(p->vlgrp, NULL);
918 synchronize_rcu();
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200919 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200920err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200921 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200922
923 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900924}
925
Toshiaki Makita8adff412013-10-16 17:07:13 +0900926/* Must be protected by RTNL.
927 * Must be called with vid in range from 1 to 4094 inclusive.
928 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000929int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000930{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200931 struct net_bridge_vlan *vlan;
932 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000933
934 ASSERT_RTNL();
935
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200936 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200937 if (vlan) {
938 __vlan_add_flags(vlan, flags);
939 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000940 }
941
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200942 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
943 if (!vlan)
944 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000945
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200946 vlan->vid = vid;
947 vlan->port = port;
948 ret = __vlan_add(vlan, flags);
949 if (ret)
950 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000951
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200952 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000953}
954
Toshiaki Makita8adff412013-10-16 17:07:13 +0900955/* Must be protected by RTNL.
956 * Must be called with vid in range from 1 to 4094 inclusive.
957 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000958int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
959{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200960 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000961
962 ASSERT_RTNL();
963
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200964 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200965 if (!v)
966 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900967 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700968 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000969
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200970 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000971}
972
973void nbp_vlan_flush(struct net_bridge_port *port)
974{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200975 struct net_bridge_vlan_group *vg;
976
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000977 ASSERT_RTNL();
978
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200979 vg = nbp_vlan_group(port);
980 __vlan_flush(vg);
981 RCU_INIT_POINTER(port->vlgrp, NULL);
982 synchronize_rcu();
983 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400984}