blob: 6e41fba47e978413330a585286a53fc0178c6a3b [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))
57 vg = v->br->vlgrp;
58 else
59 vg = v->port->vlgrp;
60
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{
75 const struct net_device_ops *ops = dev->netdev_ops;
76 int err;
77
78 /* If driver uses VLAN ndo ops, use 8021q to install vid
79 * on device, otherwise try switchdev ops to install vid.
80 */
81
82 if (ops->ndo_vlan_rx_add_vid) {
83 err = vlan_vid_add(dev, br->vlan_proto, vid);
84 } else {
Jiri Pirko8f24f302015-10-01 11:03:43 +020085 struct switchdev_obj_port_vlan v = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +020086 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Vivien Didelotab069002015-09-29 12:07:17 -040087 .flags = flags,
88 .vid_begin = vid,
89 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -070090 };
91
Jiri Pirko9e8f4a52015-10-01 11:03:46 +020092 err = switchdev_port_obj_add(dev, &v.obj);
Scott Feldman7f109532015-06-12 17:39:50 -070093 if (err == -EOPNOTSUPP)
94 err = 0;
95 }
96
97 return err;
98}
99
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200100static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200102 struct list_head *headp, *hpos;
103 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000104
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200105 headp = br_vlan_is_master(v) ? &v->br->vlgrp->vlan_list :
106 &v->port->vlgrp->vlan_list;
107 list_for_each_prev(hpos, headp) {
108 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
109 if (v->vid < vent->vid)
110 continue;
111 else
112 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000113 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200114 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200115}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000116
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200117static void __vlan_del_list(struct net_bridge_vlan *v)
118{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200119 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000120}
121
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400122static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
123 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700124{
125 const struct net_device_ops *ops = dev->netdev_ops;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400126 int err = 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700127
128 /* If driver uses VLAN ndo ops, use 8021q to delete vid
129 * on device, otherwise try switchdev ops to delete vid.
130 */
131
132 if (ops->ndo_vlan_rx_kill_vid) {
133 vlan_vid_del(dev, br->vlan_proto, vid);
134 } else {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200135 struct switchdev_obj_port_vlan v = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200136 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Vivien Didelotab069002015-09-29 12:07:17 -0400137 .vid_begin = vid,
138 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -0700139 };
140
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200141 err = switchdev_port_obj_del(dev, &v.obj);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400142 if (err == -EOPNOTSUPP)
143 err = 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700144 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400145
146 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700147}
148
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200149/* Returns a master vlan, if it didn't exist it gets created. In all cases a
150 * a reference is taken to the master vlan before returning.
151 */
152static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
153{
154 struct net_bridge_vlan *masterv;
155
156 masterv = br_vlan_find(br->vlgrp, vid);
157 if (!masterv) {
158 /* missing global ctx, create it now */
159 if (br_vlan_add(br, vid, 0))
160 return NULL;
161 masterv = br_vlan_find(br->vlgrp, vid);
162 if (WARN_ON(!masterv))
163 return NULL;
164 }
165 atomic_inc(&masterv->refcnt);
166
167 return masterv;
168}
169
170static void br_vlan_put_master(struct net_bridge_vlan *masterv)
171{
172 if (!br_vlan_is_master(masterv))
173 return;
174
175 if (atomic_dec_and_test(&masterv->refcnt)) {
176 rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash,
177 &masterv->vnode, br_vlan_rht_params);
178 __vlan_del_list(masterv);
179 kfree_rcu(masterv, rcu);
180 }
181}
182
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200183/* This is the shared VLAN add function which works for both ports and bridge
184 * devices. There are four possible calls to this function in terms of the
185 * vlan entry type:
186 * 1. vlan is being added on a port (no master flags, global entry exists)
187 * 2. vlan is being added on a bridge (both master and brvlan flags)
188 * 3. vlan is being added on a port, but a global entry didn't exist which
189 * is being created right now (master flag set, brvlan flag unset), the
190 * global entry is used for global per-vlan features, but not for filtering
191 * 4. same as 3 but with both master and brvlan flags set so the entry
192 * will be used for filtering in both the port and the bridge
193 */
194static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000195{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200196 struct net_bridge_vlan *masterv = NULL;
197 struct net_bridge_port *p = NULL;
198 struct rhashtable *tbl;
199 struct net_device *dev;
200 struct net_bridge *br;
201 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000202
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200203 if (br_vlan_is_master(v)) {
204 br = v->br;
205 dev = br->dev;
206 tbl = &br->vlgrp->vlan_hash;
207 } else {
208 p = v->port;
209 br = p->br;
210 dev = p->dev;
211 tbl = &p->vlgrp->vlan_hash;
212 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000213
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200214 if (p) {
215 u16 master_flags = flags;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400216
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 /* Add VLAN to the device filter if it is supported.
218 * This ensures tagged traffic enters the bridge when
219 * promiscuous mode is disabled by br_manage_promisc().
220 */
221 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400222 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200223 goto out;
224
225 /* need to work on the master vlan too */
226 if (flags & BRIDGE_VLAN_INFO_MASTER) {
227 master_flags |= BRIDGE_VLAN_INFO_BRENTRY;
228 err = br_vlan_add(br, v->vid, master_flags);
229 if (err)
230 goto out_filt;
231 }
232
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200233 masterv = br_vlan_get_master(br, v->vid);
234 if (!masterv)
235 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200236 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900237 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000238
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200239 /* Add the dev mac only if it's a usable vlan */
240 if (br_vlan_should_use(v)) {
241 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
242 if (err) {
243 br_err(br, "failed insert local address into bridge forwarding table\n");
244 goto out_filt;
245 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000246 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200247
248 err = rhashtable_lookup_insert_fast(tbl, &v->vnode, br_vlan_rht_params);
249 if (err)
250 goto out_fdb_insert;
251
252 __vlan_add_list(v);
253 __vlan_add_flags(v, flags);
254 if (br_vlan_is_master(v)) {
255 if (br_vlan_is_brentry(v))
256 br->vlgrp->num_vlans++;
257 } else {
258 p->vlgrp->num_vlans++;
259 }
260out:
261 return err;
262
263out_fdb_insert:
264 br_fdb_find_delete_local(br, p, br->dev->dev_addr, v->vid);
265
266out_filt:
267 if (p) {
268 __vlan_vid_del(dev, br, v->vid);
269 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200270 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200271 v->brvlan = NULL;
272 }
273 }
274
275 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000276}
277
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200278static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000279{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200280 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200281 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200282 struct net_bridge_port *p = NULL;
283 struct net_bridge *br;
284 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200285
286 if (br_vlan_is_master(v)) {
287 br = v->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200288 vg = v->br->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200289 } else {
290 p = v->port;
291 br = p->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200292 vg = v->port->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200293 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200294 }
295
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200296 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200297 if (p) {
298 err = __vlan_vid_del(p->dev, p->br, v->vid);
299 if (err)
300 goto out;
301 }
302
303 if (br_vlan_is_master(v)) {
304 if (br_vlan_is_brentry(v)) {
305 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
306 br->vlgrp->num_vlans--;
307 }
308 } else {
309 p->vlgrp->num_vlans--;
310 }
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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +0200320out:
321 return err;
322}
323
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200324static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325{
326 struct net_bridge_vlan *vlan, *tmp;
327
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200328 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
330 __vlan_del(vlan);
331 rhashtable_destroy(&vlgrp->vlan_hash);
332 kfree(vlgrp);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000333}
334
Vlad Yasevich78851982013-02-13 12:00:14 +0000335struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000337 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000338{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200339 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000340 u16 vid;
341
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400342 /* If this packet was not filtered at input, let it pass */
343 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000344 goto out;
345
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 /* At this point, we know that the frame was filtered and contains
347 * a valid vlan id. If the vlan id has untagged flag set,
348 * send untagged; otherwise, send tagged.
349 */
350 br_vlan_get_tag(skb, &vid);
351 v = br_vlan_find(vg, vid);
352 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400353 * only exception is the bridge is set in promisc mode and the
354 * packet is destined for the bridge device. In this case
355 * pass the packet as is.
356 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200357 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400358 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
359 goto out;
360 } else {
361 kfree_skb(skb);
362 return NULL;
363 }
364 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200365 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900366 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000367
368out:
369 return skb;
370}
371
372/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200373static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200374 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000375{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200376 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900377 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000378
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400379 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900380 /* If vlan tx offload is disabled on bridge device and frame was
381 * sent from vlan device on the bridge device, it does not have
382 * HW accelerated vlan tag.
383 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100384 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900385 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400386 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900387 if (unlikely(!skb))
388 return false;
389 }
390
Toshiaki Makita8580e212014-06-10 20:59:23 +0900391 if (!br_vlan_get_tag(skb, vid)) {
392 /* Tagged frame */
393 if (skb->vlan_proto != proto) {
394 /* Protocol-mismatch, empty out vlan_tci for new tag */
395 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100396 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100397 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900398 if (unlikely(!skb))
399 return false;
400
401 skb_pull(skb, ETH_HLEN);
402 skb_reset_mac_len(skb);
403 *vid = 0;
404 tagged = false;
405 } else {
406 tagged = true;
407 }
408 } else {
409 /* Untagged frame */
410 tagged = false;
411 }
412
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900413 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200414 u16 pvid = br_get_pvid(vg);
415
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900416 /* Frame had a tag with VID 0 or did not have a tag.
417 * See if pvid is set on this port. That tells us which
418 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000419 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400420 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900421 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000422
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900423 /* PVID is set on this port. Any untagged or priority-tagged
424 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000425 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900426 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900427 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900428 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900429 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900430 else
431 /* Priority-tagged Frame.
432 * At this point, We know that skb->vlan_tci had
433 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
434 * We update only VID field and preserve PCP field.
435 */
436 skb->vlan_tci |= pvid;
437
Vlad Yasevich78851982013-02-13 12:00:14 +0000438 return true;
439 }
440
441 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200442 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200443 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000444 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900445drop:
446 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000447 return false;
448}
449
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200450bool br_allowed_ingress(const struct net_bridge *br,
451 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
452 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200453{
454 /* If VLAN filtering is disabled on the bridge, all packets are
455 * permitted.
456 */
457 if (!br->vlan_enabled) {
458 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
459 return true;
460 }
461
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200462 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200463}
464
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000465/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200466bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000467 const struct sk_buff *skb)
468{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200469 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000470 u16 vid;
471
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400472 /* If this packet was not filtered at input, let it pass */
473 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000474 return true;
475
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000476 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200477 v = br_vlan_find(vg, vid);
478 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000479 return true;
480
481 return false;
482}
483
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900484/* Called under RCU */
485bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
486{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200487 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900488 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900489
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400490 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400491 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900492 return true;
493
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200494 vg = p->vlgrp;
495 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900496 return false;
497
Toshiaki Makita8580e212014-06-10 20:59:23 +0900498 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
499 *vid = 0;
500
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900501 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200502 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400503 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900504 return false;
505
506 return true;
507 }
508
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200509 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900510 return true;
511
512 return false;
513}
514
Toshiaki Makita8adff412013-10-16 17:07:13 +0900515/* Must be protected by RTNL.
516 * Must be called with vid in range from 1 to 4094 inclusive.
517 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000518int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000519{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200520 struct net_bridge_vlan *vlan;
521 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000522
523 ASSERT_RTNL();
524
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200525 vlan = br_vlan_find(br->vlgrp, vid);
526 if (vlan) {
527 if (!br_vlan_is_brentry(vlan)) {
528 /* Trying to change flags of non-existent bridge vlan */
529 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
530 return -EINVAL;
531 /* It was only kept for port vlans, now make it real */
532 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
533 vlan->vid);
534 if (ret) {
535 br_err(br, "failed insert local address into bridge forwarding table\n");
536 return ret;
537 }
538 atomic_inc(&vlan->refcnt);
539 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
540 br->vlgrp->num_vlans++;
541 }
542 __vlan_add_flags(vlan, flags);
543 return 0;
544 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000545
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200546 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
547 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000548 return -ENOMEM;
549
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200550 vlan->vid = vid;
551 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
552 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
553 vlan->br = br;
554 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
555 atomic_set(&vlan->refcnt, 1);
556 ret = __vlan_add(vlan, flags);
557 if (ret)
558 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000559
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200560 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000561}
562
Toshiaki Makita8adff412013-10-16 17:07:13 +0900563/* Must be protected by RTNL.
564 * Must be called with vid in range from 1 to 4094 inclusive.
565 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000566int br_vlan_delete(struct net_bridge *br, u16 vid)
567{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200568 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000569
570 ASSERT_RTNL();
571
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200572 v = br_vlan_find(br->vlgrp, vid);
573 if (!v || !br_vlan_is_brentry(v))
574 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000575
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900576 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000577
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200578 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000579}
580
581void br_vlan_flush(struct net_bridge *br)
582{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000583 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000584
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200585 __vlan_flush(br_vlan_group(br));
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000586}
587
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200588struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900589{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200590 if (!vg)
591 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900592
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200593 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900594}
595
Toshiaki Makita204177f2014-06-10 20:59:25 +0900596/* Must be protected by RTNL. */
597static void recalculate_group_addr(struct net_bridge *br)
598{
599 if (br->group_addr_set)
600 return;
601
602 spin_lock_bh(&br->lock);
603 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
604 /* Bridge Group Address */
605 br->group_addr[5] = 0x00;
606 } else { /* vlan_enabled && ETH_P_8021AD */
607 /* Provider Bridge Group Address */
608 br->group_addr[5] = 0x08;
609 }
610 spin_unlock_bh(&br->lock);
611}
612
613/* Must be protected by RTNL. */
614void br_recalculate_fwd_mask(struct net_bridge *br)
615{
616 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
617 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
618 else /* vlan_enabled && ETH_P_8021AD */
619 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
620 ~(1u << br->group_addr[5]);
621}
622
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300623int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000624{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000625 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300626 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000627
628 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400629 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900630 recalculate_group_addr(br);
631 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000632
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300633 return 0;
634}
635
636int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
637{
638 if (!rtnl_trylock())
639 return restart_syscall();
640
641 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000642 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300643
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000644 return 0;
645}
646
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900647int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900648{
649 int err = 0;
650 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200651 struct net_bridge_vlan *vlan;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900652 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900653
Toshiaki Makita204177f2014-06-10 20:59:25 +0900654 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900655 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900656
657 /* Add VLANs for the new proto to the device filter. */
658 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200659 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
660 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900661 if (err)
662 goto err_filt;
663 }
664 }
665
666 oldproto = br->vlan_proto;
667 br->vlan_proto = proto;
668
669 recalculate_group_addr(br);
670 br_recalculate_fwd_mask(br);
671
672 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200673 list_for_each_entry(p, &br->port_list, list)
674 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
675 vlan_vid_del(p->dev, oldproto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900676
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900677 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900678
679err_filt:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200680 list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
681 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900682
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200683 list_for_each_entry_continue_reverse(p, &br->port_list, list)
684 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
685 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900686
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900687 return err;
688}
689
690int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
691{
692 int err;
693
694 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
695 return -EPROTONOSUPPORT;
696
697 if (!rtnl_trylock())
698 return restart_syscall();
699
700 err = __br_vlan_set_proto(br, htons(val));
701 rtnl_unlock();
702
703 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900704}
705
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200706static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400707{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200708 struct net_bridge_vlan *v;
709
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200710 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200711 return false;
712
713 v = br_vlan_lookup(&vg->vlan_hash, vid);
714 if (v && br_vlan_should_use(v) &&
715 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
716 return true;
717
718 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400719}
720
721static void br_vlan_disable_default_pvid(struct net_bridge *br)
722{
723 struct net_bridge_port *p;
724 u16 pvid = br->default_pvid;
725
726 /* Disable default_pvid on all ports where it is still
727 * configured.
728 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200729 if (vlan_default_pvid(br->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400730 br_vlan_delete(br, pvid);
731
732 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200733 if (vlan_default_pvid(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400734 nbp_vlan_delete(p, pvid);
735 }
736
737 br->default_pvid = 0;
738}
739
740static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
741{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200742 const struct net_bridge_vlan *pvent;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400743 struct net_bridge_port *p;
744 u16 old_pvid;
745 int err = 0;
746 unsigned long *changed;
747
748 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
749 GFP_KERNEL);
750 if (!changed)
751 return -ENOMEM;
752
753 old_pvid = br->default_pvid;
754
755 /* Update default_pvid config only if we do not conflict with
756 * user configuration.
757 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200758 pvent = br_vlan_find(br->vlgrp, pvid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200759 if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200760 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400761 err = br_vlan_add(br, pvid,
762 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200763 BRIDGE_VLAN_INFO_UNTAGGED |
764 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400765 if (err)
766 goto out;
767 br_vlan_delete(br, old_pvid);
768 set_bit(0, changed);
769 }
770
771 list_for_each_entry(p, &br->port_list, list) {
772 /* Update default_pvid config only if we do not conflict with
773 * user configuration.
774 */
775 if ((old_pvid &&
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200776 !vlan_default_pvid(p->vlgrp, old_pvid)) ||
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200777 br_vlan_find(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400778 continue;
779
780 err = nbp_vlan_add(p, pvid,
781 BRIDGE_VLAN_INFO_PVID |
782 BRIDGE_VLAN_INFO_UNTAGGED);
783 if (err)
784 goto err_port;
785 nbp_vlan_delete(p, old_pvid);
786 set_bit(p->port_no, changed);
787 }
788
789 br->default_pvid = pvid;
790
791out:
792 kfree(changed);
793 return err;
794
795err_port:
796 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
797 if (!test_bit(p->port_no, changed))
798 continue;
799
800 if (old_pvid)
801 nbp_vlan_add(p, old_pvid,
802 BRIDGE_VLAN_INFO_PVID |
803 BRIDGE_VLAN_INFO_UNTAGGED);
804 nbp_vlan_delete(p, pvid);
805 }
806
807 if (test_bit(0, changed)) {
808 if (old_pvid)
809 br_vlan_add(br, old_pvid,
810 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200811 BRIDGE_VLAN_INFO_UNTAGGED |
812 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400813 br_vlan_delete(br, pvid);
814 }
815 goto out;
816}
817
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400818int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
819{
820 u16 pvid = val;
821 int err = 0;
822
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400823 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400824 return -EINVAL;
825
826 if (!rtnl_trylock())
827 return restart_syscall();
828
829 if (pvid == br->default_pvid)
830 goto unlock;
831
832 /* Only allow default pvid change when filtering is disabled */
833 if (br->vlan_enabled) {
834 pr_info_once("Please disable vlan filtering to change default_pvid\n");
835 err = -EPERM;
836 goto unlock;
837 }
838
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400839 if (!pvid)
840 br_vlan_disable_default_pvid(br);
841 else
842 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400843
844unlock:
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 Aleksandrov2594e9062015-09-25 19:00:11 +0200851 int ret = -ENOMEM;
852
853 br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
854 if (!br->vlgrp)
855 goto out;
856 ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
857 if (ret)
858 goto err_rhtbl;
859 INIT_LIST_HEAD(&br->vlgrp->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900860 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400861 br->default_pvid = 1;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200862 ret = br_vlan_add(br, 1,
863 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
864 BRIDGE_VLAN_INFO_BRENTRY);
865 if (ret)
866 goto err_vlan_add;
867
868out:
869 return ret;
870
871err_vlan_add:
872 rhashtable_destroy(&br->vlgrp->vlan_hash);
873err_rhtbl:
874 kfree(br->vlgrp);
875
876 goto out;
877}
878
879int nbp_vlan_init(struct net_bridge_port *p)
880{
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200881 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200882 int ret = -ENOMEM;
883
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200884 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
885 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200886 goto out;
887
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200888 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200889 if (ret)
890 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200891 INIT_LIST_HEAD(&vg->vlan_list);
892 /* Make sure everything's committed before publishing vg */
893 smp_wmb();
894 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 Aleksandrov2594e9062015-09-25 19:00:11 +0200923 vlan = br_vlan_find(port->vlgrp, vid);
924 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 Aleksandrov2594e9062015-09-25 19:00:11 +0200951 v = br_vlan_find(port->vlgrp, vid);
952 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 Aleksandrov2594e9062015-09-25 19:00:11 +0200962 struct net_bridge_vlan *vlan;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000963
964 ASSERT_RTNL();
965
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200966 list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
967 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000968
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200969 __vlan_flush(nbp_vlan_group(port));
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400970}