blob: ffaa6d986505c941d2b4690b976bccf66b01cb6c [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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +020067 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040068 else
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200112}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000113
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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)
183 * 2. vlan is being added on a bridge (both master and brvlan flags)
184 * 3. vlan is being added on a port, but a global entry didn't exist which
185 * is being created right now (master flag set, brvlan flag unset), the
186 * global entry is used for global per-vlan features, but not for filtering
187 * 4. same as 3 but with both master and brvlan flags set so the entry
188 * 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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200208 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000209
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210 if (p) {
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200271static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000272{
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200275 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200276 int err = 0;
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200283 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200284 }
285
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200286 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200306out:
307 return err;
308}
309
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200310static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200311{
312 struct net_bridge_vlan *vlan, *tmp;
313
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200314 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200315 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
316 __vlan_del(vlan);
317 rhashtable_destroy(&vlgrp->vlan_hash);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200318 kfree_rcu(vlgrp, rcu);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000319}
320
Vlad Yasevich78851982013-02-13 12:00:14 +0000321struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200322 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000323 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000324{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000326 u16 vid;
327
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400328 /* If this packet was not filtered at input, let it pass */
329 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000330 goto out;
331
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 /* At this point, we know that the frame was filtered and contains
333 * a valid vlan id. If the vlan id has untagged flag set,
334 * send untagged; otherwise, send tagged.
335 */
336 br_vlan_get_tag(skb, &vid);
337 v = br_vlan_find(vg, vid);
338 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400339 * only exception is the bridge is set in promisc mode and the
340 * packet is destined for the bridge device. In this case
341 * pass the packet as is.
342 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200343 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400344 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
345 goto out;
346 } else {
347 kfree_skb(skb);
348 return NULL;
349 }
350 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900352 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000353
354out:
355 return skb;
356}
357
358/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200359static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200360 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000361{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200362 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900363 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000364
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400365 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900366 /* If vlan tx offload is disabled on bridge device and frame was
367 * sent from vlan device on the bridge device, it does not have
368 * HW accelerated vlan tag.
369 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100370 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900371 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400372 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900373 if (unlikely(!skb))
374 return false;
375 }
376
Toshiaki Makita8580e212014-06-10 20:59:23 +0900377 if (!br_vlan_get_tag(skb, vid)) {
378 /* Tagged frame */
379 if (skb->vlan_proto != proto) {
380 /* Protocol-mismatch, empty out vlan_tci for new tag */
381 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100382 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100383 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900384 if (unlikely(!skb))
385 return false;
386
387 skb_pull(skb, ETH_HLEN);
388 skb_reset_mac_len(skb);
389 *vid = 0;
390 tagged = false;
391 } else {
392 tagged = true;
393 }
394 } else {
395 /* Untagged frame */
396 tagged = false;
397 }
398
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900399 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200400 u16 pvid = br_get_pvid(vg);
401
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900402 /* Frame had a tag with VID 0 or did not have a tag.
403 * See if pvid is set on this port. That tells us which
404 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000405 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400406 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900407 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000408
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900409 /* PVID is set on this port. Any untagged or priority-tagged
410 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000411 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900412 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900413 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900414 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900415 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900416 else
417 /* Priority-tagged Frame.
418 * At this point, We know that skb->vlan_tci had
419 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
420 * We update only VID field and preserve PCP field.
421 */
422 skb->vlan_tci |= pvid;
423
Vlad Yasevich78851982013-02-13 12:00:14 +0000424 return true;
425 }
426
427 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200428 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200429 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000430 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900431drop:
432 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000433 return false;
434}
435
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200436bool br_allowed_ingress(const struct net_bridge *br,
437 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
438 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200439{
440 /* If VLAN filtering is disabled on the bridge, all packets are
441 * permitted.
442 */
443 if (!br->vlan_enabled) {
444 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
445 return true;
446 }
447
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200448 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200449}
450
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000451/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200452bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000453 const struct sk_buff *skb)
454{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200455 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000456 u16 vid;
457
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400458 /* If this packet was not filtered at input, let it pass */
459 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000460 return true;
461
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000462 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200463 v = br_vlan_find(vg, vid);
464 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000465 return true;
466
467 return false;
468}
469
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900470/* Called under RCU */
471bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
472{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200473 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900474 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900475
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400476 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400477 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900478 return true;
479
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200480 vg = nbp_vlan_group(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200481 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900482 return false;
483
Toshiaki Makita8580e212014-06-10 20:59:23 +0900484 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
485 *vid = 0;
486
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900487 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200488 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400489 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900490 return false;
491
492 return true;
493 }
494
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200495 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900496 return true;
497
498 return false;
499}
500
Toshiaki Makita8adff412013-10-16 17:07:13 +0900501/* Must be protected by RTNL.
502 * Must be called with vid in range from 1 to 4094 inclusive.
503 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000504int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000505{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200506 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200507 struct net_bridge_vlan *vlan;
508 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000509
510 ASSERT_RTNL();
511
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200512 vg = br_vlan_group(br);
513 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200514 if (vlan) {
515 if (!br_vlan_is_brentry(vlan)) {
516 /* Trying to change flags of non-existent bridge vlan */
517 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
518 return -EINVAL;
519 /* It was only kept for port vlans, now make it real */
520 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
521 vlan->vid);
522 if (ret) {
523 br_err(br, "failed insert local address into bridge forwarding table\n");
524 return ret;
525 }
526 atomic_inc(&vlan->refcnt);
527 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200528 vg->num_vlans++;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200529 }
530 __vlan_add_flags(vlan, flags);
531 return 0;
532 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000533
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200534 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
535 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000536 return -ENOMEM;
537
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200538 vlan->vid = vid;
539 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
540 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
541 vlan->br = br;
542 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
543 atomic_set(&vlan->refcnt, 1);
544 ret = __vlan_add(vlan, flags);
545 if (ret)
546 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000547
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200548 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000549}
550
Toshiaki Makita8adff412013-10-16 17:07:13 +0900551/* Must be protected by RTNL.
552 * Must be called with vid in range from 1 to 4094 inclusive.
553 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000554int br_vlan_delete(struct net_bridge *br, u16 vid)
555{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200556 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200557 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000558
559 ASSERT_RTNL();
560
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200561 vg = br_vlan_group(br);
562 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200563 if (!v || !br_vlan_is_brentry(v))
564 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000565
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900566 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700567 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000568
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200569 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000570}
571
572void br_vlan_flush(struct net_bridge *br)
573{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000574 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000575
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200576 __vlan_flush(br_vlan_group(br));
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000577}
578
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200579struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900580{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200581 if (!vg)
582 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900583
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200584 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900585}
586
Toshiaki Makita204177f2014-06-10 20:59:25 +0900587/* Must be protected by RTNL. */
588static void recalculate_group_addr(struct net_bridge *br)
589{
590 if (br->group_addr_set)
591 return;
592
593 spin_lock_bh(&br->lock);
594 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
595 /* Bridge Group Address */
596 br->group_addr[5] = 0x00;
597 } else { /* vlan_enabled && ETH_P_8021AD */
598 /* Provider Bridge Group Address */
599 br->group_addr[5] = 0x08;
600 }
601 spin_unlock_bh(&br->lock);
602}
603
604/* Must be protected by RTNL. */
605void br_recalculate_fwd_mask(struct net_bridge *br)
606{
607 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
608 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
609 else /* vlan_enabled && ETH_P_8021AD */
610 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
611 ~(1u << br->group_addr[5]);
612}
613
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300614int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000615{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000616 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300617 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000618
619 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400620 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900621 recalculate_group_addr(br);
622 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000623
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300624 return 0;
625}
626
627int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
628{
629 if (!rtnl_trylock())
630 return restart_syscall();
631
632 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000633 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300634
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000635 return 0;
636}
637
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900638int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900639{
640 int err = 0;
641 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200642 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200643 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900644 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900645
Toshiaki Makita204177f2014-06-10 20:59:25 +0900646 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900647 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900648
649 /* Add VLANs for the new proto to the device filter. */
650 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200651 vg = nbp_vlan_group(p);
652 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200653 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900654 if (err)
655 goto err_filt;
656 }
657 }
658
659 oldproto = br->vlan_proto;
660 br->vlan_proto = proto;
661
662 recalculate_group_addr(br);
663 br_recalculate_fwd_mask(br);
664
665 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200666 list_for_each_entry(p, &br->port_list, list) {
667 vg = nbp_vlan_group(p);
668 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200669 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200670 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900671
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900672 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900673
674err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200675 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200676 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900677
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200678 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
679 vg = nbp_vlan_group(p);
680 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200681 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200682 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900683
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900684 return err;
685}
686
687int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
688{
689 int err;
690
691 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
692 return -EPROTONOSUPPORT;
693
694 if (!rtnl_trylock())
695 return restart_syscall();
696
697 err = __br_vlan_set_proto(br, htons(val));
698 rtnl_unlock();
699
700 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900701}
702
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200703static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400704{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200705 struct net_bridge_vlan *v;
706
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200707 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200708 return false;
709
710 v = br_vlan_lookup(&vg->vlan_hash, vid);
711 if (v && br_vlan_should_use(v) &&
712 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
713 return true;
714
715 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400716}
717
718static void br_vlan_disable_default_pvid(struct net_bridge *br)
719{
720 struct net_bridge_port *p;
721 u16 pvid = br->default_pvid;
722
723 /* Disable default_pvid on all ports where it is still
724 * configured.
725 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200726 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400727 br_vlan_delete(br, pvid);
728
729 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200730 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400731 nbp_vlan_delete(p, pvid);
732 }
733
734 br->default_pvid = 0;
735}
736
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200737int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400738{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200739 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200740 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400741 struct net_bridge_port *p;
742 u16 old_pvid;
743 int err = 0;
744 unsigned long *changed;
745
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200746 if (!pvid) {
747 br_vlan_disable_default_pvid(br);
748 return 0;
749 }
750
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400751 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
752 GFP_KERNEL);
753 if (!changed)
754 return -ENOMEM;
755
756 old_pvid = br->default_pvid;
757
758 /* Update default_pvid config only if we do not conflict with
759 * user configuration.
760 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200761 vg = br_vlan_group(br);
762 pvent = br_vlan_find(vg, pvid);
763 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200764 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400765 err = br_vlan_add(br, pvid,
766 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200767 BRIDGE_VLAN_INFO_UNTAGGED |
768 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400769 if (err)
770 goto out;
771 br_vlan_delete(br, old_pvid);
772 set_bit(0, changed);
773 }
774
775 list_for_each_entry(p, &br->port_list, list) {
776 /* Update default_pvid config only if we do not conflict with
777 * user configuration.
778 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200779 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400780 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200781 !vlan_default_pvid(vg, old_pvid)) ||
782 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400783 continue;
784
785 err = nbp_vlan_add(p, pvid,
786 BRIDGE_VLAN_INFO_PVID |
787 BRIDGE_VLAN_INFO_UNTAGGED);
788 if (err)
789 goto err_port;
790 nbp_vlan_delete(p, old_pvid);
791 set_bit(p->port_no, changed);
792 }
793
794 br->default_pvid = pvid;
795
796out:
797 kfree(changed);
798 return err;
799
800err_port:
801 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
802 if (!test_bit(p->port_no, changed))
803 continue;
804
805 if (old_pvid)
806 nbp_vlan_add(p, old_pvid,
807 BRIDGE_VLAN_INFO_PVID |
808 BRIDGE_VLAN_INFO_UNTAGGED);
809 nbp_vlan_delete(p, pvid);
810 }
811
812 if (test_bit(0, changed)) {
813 if (old_pvid)
814 br_vlan_add(br, old_pvid,
815 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200816 BRIDGE_VLAN_INFO_UNTAGGED |
817 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400818 br_vlan_delete(br, pvid);
819 }
820 goto out;
821}
822
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400823int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
824{
825 u16 pvid = val;
826 int err = 0;
827
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400828 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400829 return -EINVAL;
830
831 if (!rtnl_trylock())
832 return restart_syscall();
833
834 if (pvid == br->default_pvid)
835 goto unlock;
836
837 /* Only allow default pvid change when filtering is disabled */
838 if (br->vlan_enabled) {
839 pr_info_once("Please disable vlan filtering to change default_pvid\n");
840 err = -EPERM;
841 goto unlock;
842 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200843 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400844unlock:
845 rtnl_unlock();
846 return err;
847}
848
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400849int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900850{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200851 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200852 int ret = -ENOMEM;
853
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200854 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
855 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200856 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200857 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200858 if (ret)
859 goto err_rhtbl;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200860 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900861 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400862 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200863 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200864 ret = br_vlan_add(br, 1,
865 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
866 BRIDGE_VLAN_INFO_BRENTRY);
867 if (ret)
868 goto err_vlan_add;
869
870out:
871 return ret;
872
873err_vlan_add:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200874 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200875err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200876 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200877
878 goto out;
879}
880
881int nbp_vlan_init(struct net_bridge_port *p)
882{
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200883 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200884 int ret = -ENOMEM;
885
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200886 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
887 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200888 goto out;
889
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200890 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200891 if (ret)
892 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200893 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200894 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200895 if (p->br->default_pvid) {
896 ret = nbp_vlan_add(p, p->br->default_pvid,
897 BRIDGE_VLAN_INFO_PVID |
898 BRIDGE_VLAN_INFO_UNTAGGED);
899 if (ret)
900 goto err_vlan_add;
901 }
902out:
903 return ret;
904
905err_vlan_add:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200906 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200907err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200908 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200909
910 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900911}
912
Toshiaki Makita8adff412013-10-16 17:07:13 +0900913/* Must be protected by RTNL.
914 * Must be called with vid in range from 1 to 4094 inclusive.
915 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000916int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000917{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200918 struct net_bridge_vlan *vlan;
919 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000920
921 ASSERT_RTNL();
922
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200923 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200924 if (vlan) {
925 __vlan_add_flags(vlan, flags);
926 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000927 }
928
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200929 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
930 if (!vlan)
931 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000932
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200933 vlan->vid = vid;
934 vlan->port = port;
935 ret = __vlan_add(vlan, flags);
936 if (ret)
937 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000938
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200939 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000940}
941
Toshiaki Makita8adff412013-10-16 17:07:13 +0900942/* Must be protected by RTNL.
943 * Must be called with vid in range from 1 to 4094 inclusive.
944 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000945int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
946{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200947 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000948
949 ASSERT_RTNL();
950
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200951 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200952 if (!v)
953 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900954 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700955 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000956
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200957 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000958}
959
960void nbp_vlan_flush(struct net_bridge_port *port)
961{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200962 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200963 struct net_bridge_vlan *vlan;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000964
965 ASSERT_RTNL();
966
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200967 vg = nbp_vlan_group(port);
968 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200969 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000970
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200971 __vlan_flush(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400972}