blob: 90ac4b0c55c13f7108f542e86f102ec68133cbf6 [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 {
Vivien Didelotab069002015-09-29 12:07:17 -040085 struct switchdev_obj_vlan v = {
86 .flags = flags,
87 .vid_begin = vid,
88 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -070089 };
90
Vivien Didelotab069002015-09-29 12:07:17 -040091 err = switchdev_port_obj_add(dev, SWITCHDEV_OBJ_PORT_VLAN, &v);
Scott Feldman7f109532015-06-12 17:39:50 -070092 if (err == -EOPNOTSUPP)
93 err = 0;
94 }
95
96 return err;
97}
98
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020099static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000100{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200101 struct list_head *headp, *hpos;
102 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000103
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200104 headp = br_vlan_is_master(v) ? &v->br->vlgrp->vlan_list :
105 &v->port->vlgrp->vlan_list;
106 list_for_each_prev(hpos, headp) {
107 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
108 if (v->vid < vent->vid)
109 continue;
110 else
111 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000112 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200113 list_add(&v->vlist, hpos);
114}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000115
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200116static void __vlan_del_list(struct net_bridge_vlan *v)
117{
118 list_del(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000119}
120
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400121static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
122 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700123{
124 const struct net_device_ops *ops = dev->netdev_ops;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400125 int err = 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700126
127 /* If driver uses VLAN ndo ops, use 8021q to delete vid
128 * on device, otherwise try switchdev ops to delete vid.
129 */
130
131 if (ops->ndo_vlan_rx_kill_vid) {
132 vlan_vid_del(dev, br->vlan_proto, vid);
133 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400134 struct switchdev_obj_vlan v = {
135 .vid_begin = vid,
136 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -0700137 };
138
Vivien Didelotab069002015-09-29 12:07:17 -0400139 err = switchdev_port_obj_del(dev, SWITCHDEV_OBJ_PORT_VLAN, &v);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400140 if (err == -EOPNOTSUPP)
141 err = 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700142 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400143
144 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700145}
146
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200147/* This is the shared VLAN add function which works for both ports and bridge
148 * devices. There are four possible calls to this function in terms of the
149 * vlan entry type:
150 * 1. vlan is being added on a port (no master flags, global entry exists)
151 * 2. vlan is being added on a bridge (both master and brvlan flags)
152 * 3. vlan is being added on a port, but a global entry didn't exist which
153 * is being created right now (master flag set, brvlan flag unset), the
154 * global entry is used for global per-vlan features, but not for filtering
155 * 4. same as 3 but with both master and brvlan flags set so the entry
156 * will be used for filtering in both the port and the bridge
157 */
158static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000159{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200160 struct net_bridge_vlan *masterv = NULL;
161 struct net_bridge_port *p = NULL;
162 struct rhashtable *tbl;
163 struct net_device *dev;
164 struct net_bridge *br;
165 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000166
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200167 if (br_vlan_is_master(v)) {
168 br = v->br;
169 dev = br->dev;
170 tbl = &br->vlgrp->vlan_hash;
171 } else {
172 p = v->port;
173 br = p->br;
174 dev = p->dev;
175 tbl = &p->vlgrp->vlan_hash;
176 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000177
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200178 if (p) {
179 u16 master_flags = flags;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400180
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200181 /* Add VLAN to the device filter if it is supported.
182 * This ensures tagged traffic enters the bridge when
183 * promiscuous mode is disabled by br_manage_promisc().
184 */
185 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400186 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200187 goto out;
188
189 /* need to work on the master vlan too */
190 if (flags & BRIDGE_VLAN_INFO_MASTER) {
191 master_flags |= BRIDGE_VLAN_INFO_BRENTRY;
192 err = br_vlan_add(br, v->vid, master_flags);
193 if (err)
194 goto out_filt;
195 }
196
197 masterv = br_vlan_find(br->vlgrp, v->vid);
198 if (!masterv) {
199 /* missing global ctx, create it now */
200 err = br_vlan_add(br, v->vid, master_flags);
201 if (err)
202 goto out_filt;
203 masterv = br_vlan_find(br->vlgrp, v->vid);
204 WARN_ON(!masterv);
205 }
206 atomic_inc(&masterv->refcnt);
207 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900208 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000209
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210 /* Add the dev mac only if it's a usable vlan */
211 if (br_vlan_should_use(v)) {
212 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
213 if (err) {
214 br_err(br, "failed insert local address into bridge forwarding table\n");
215 goto out_filt;
216 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000217 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200218
219 err = rhashtable_lookup_insert_fast(tbl, &v->vnode, br_vlan_rht_params);
220 if (err)
221 goto out_fdb_insert;
222
223 __vlan_add_list(v);
224 __vlan_add_flags(v, flags);
225 if (br_vlan_is_master(v)) {
226 if (br_vlan_is_brentry(v))
227 br->vlgrp->num_vlans++;
228 } else {
229 p->vlgrp->num_vlans++;
230 }
231out:
232 return err;
233
234out_fdb_insert:
235 br_fdb_find_delete_local(br, p, br->dev->dev_addr, v->vid);
236
237out_filt:
238 if (p) {
239 __vlan_vid_del(dev, br, v->vid);
240 if (masterv) {
241 atomic_dec(&masterv->refcnt);
242 v->brvlan = NULL;
243 }
244 }
245
246 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000247}
248
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200249static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000250{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200251 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200252 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200253 struct net_bridge_port *p = NULL;
254 struct net_bridge *br;
255 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200256
257 if (br_vlan_is_master(v)) {
258 br = v->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200259 vg = v->br->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200260 } else {
261 p = v->port;
262 br = p->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200263 vg = v->port->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200264 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200265 }
266
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200267 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200268 if (p) {
269 err = __vlan_vid_del(p->dev, p->br, v->vid);
270 if (err)
271 goto out;
272 }
273
274 if (br_vlan_is_master(v)) {
275 if (br_vlan_is_brentry(v)) {
276 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
277 br->vlgrp->num_vlans--;
278 }
279 } else {
280 p->vlgrp->num_vlans--;
281 }
282
283 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200284 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
285 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200286 __vlan_del_list(v);
287 kfree_rcu(v, rcu);
288 }
289
290 if (atomic_dec_and_test(&masterv->refcnt)) {
291 rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash,
292 &masterv->vnode, br_vlan_rht_params);
293 __vlan_del_list(masterv);
294 kfree_rcu(masterv, rcu);
295 }
296out:
297 return err;
298}
299
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200300static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200301{
302 struct net_bridge_vlan *vlan, *tmp;
303
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200304 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
306 __vlan_del(vlan);
307 rhashtable_destroy(&vlgrp->vlan_hash);
308 kfree(vlgrp);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000309}
310
Vlad Yasevich78851982013-02-13 12:00:14 +0000311struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200312 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000313 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000314{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200315 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000316 u16 vid;
317
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400318 /* If this packet was not filtered at input, let it pass */
319 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000320 goto out;
321
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200322 /* At this point, we know that the frame was filtered and contains
323 * a valid vlan id. If the vlan id has untagged flag set,
324 * send untagged; otherwise, send tagged.
325 */
326 br_vlan_get_tag(skb, &vid);
327 v = br_vlan_find(vg, vid);
328 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400329 * only exception is the bridge is set in promisc mode and the
330 * packet is destined for the bridge device. In this case
331 * pass the packet as is.
332 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400334 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
335 goto out;
336 } else {
337 kfree_skb(skb);
338 return NULL;
339 }
340 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200341 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900342 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000343
344out:
345 return skb;
346}
347
348/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200349static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000351{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200352 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900353 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000354
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400355 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900356 /* If vlan tx offload is disabled on bridge device and frame was
357 * sent from vlan device on the bridge device, it does not have
358 * HW accelerated vlan tag.
359 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100360 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900361 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400362 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900363 if (unlikely(!skb))
364 return false;
365 }
366
Toshiaki Makita8580e212014-06-10 20:59:23 +0900367 if (!br_vlan_get_tag(skb, vid)) {
368 /* Tagged frame */
369 if (skb->vlan_proto != proto) {
370 /* Protocol-mismatch, empty out vlan_tci for new tag */
371 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100372 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100373 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900374 if (unlikely(!skb))
375 return false;
376
377 skb_pull(skb, ETH_HLEN);
378 skb_reset_mac_len(skb);
379 *vid = 0;
380 tagged = false;
381 } else {
382 tagged = true;
383 }
384 } else {
385 /* Untagged frame */
386 tagged = false;
387 }
388
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900389 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200390 u16 pvid = br_get_pvid(vg);
391
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900392 /* Frame had a tag with VID 0 or did not have a tag.
393 * See if pvid is set on this port. That tells us which
394 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000395 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400396 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900397 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000398
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900399 /* PVID is set on this port. Any untagged or priority-tagged
400 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000401 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900402 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900403 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900404 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900405 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900406 else
407 /* Priority-tagged Frame.
408 * At this point, We know that skb->vlan_tci had
409 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
410 * We update only VID field and preserve PCP field.
411 */
412 skb->vlan_tci |= pvid;
413
Vlad Yasevich78851982013-02-13 12:00:14 +0000414 return true;
415 }
416
417 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200418 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200419 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000420 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900421drop:
422 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000423 return false;
424}
425
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200426bool br_allowed_ingress(const struct net_bridge *br,
427 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
428 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200429{
430 /* If VLAN filtering is disabled on the bridge, all packets are
431 * permitted.
432 */
433 if (!br->vlan_enabled) {
434 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
435 return true;
436 }
437
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200438 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200439}
440
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000441/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200442bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000443 const struct sk_buff *skb)
444{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200445 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000446 u16 vid;
447
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400448 /* If this packet was not filtered at input, let it pass */
449 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000450 return true;
451
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000452 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200453 v = br_vlan_find(vg, vid);
454 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000455 return true;
456
457 return false;
458}
459
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900460/* Called under RCU */
461bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
462{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200463 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900464 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900465
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400466 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400467 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900468 return true;
469
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200470 vg = p->vlgrp;
471 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900472 return false;
473
Toshiaki Makita8580e212014-06-10 20:59:23 +0900474 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
475 *vid = 0;
476
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900477 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200478 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400479 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900480 return false;
481
482 return true;
483 }
484
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200485 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900486 return true;
487
488 return false;
489}
490
Toshiaki Makita8adff412013-10-16 17:07:13 +0900491/* Must be protected by RTNL.
492 * Must be called with vid in range from 1 to 4094 inclusive.
493 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000494int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000495{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200496 struct net_bridge_vlan *vlan;
497 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000498
499 ASSERT_RTNL();
500
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200501 vlan = br_vlan_find(br->vlgrp, vid);
502 if (vlan) {
503 if (!br_vlan_is_brentry(vlan)) {
504 /* Trying to change flags of non-existent bridge vlan */
505 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
506 return -EINVAL;
507 /* It was only kept for port vlans, now make it real */
508 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
509 vlan->vid);
510 if (ret) {
511 br_err(br, "failed insert local address into bridge forwarding table\n");
512 return ret;
513 }
514 atomic_inc(&vlan->refcnt);
515 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
516 br->vlgrp->num_vlans++;
517 }
518 __vlan_add_flags(vlan, flags);
519 return 0;
520 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000521
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200522 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
523 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000524 return -ENOMEM;
525
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200526 vlan->vid = vid;
527 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
528 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
529 vlan->br = br;
530 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
531 atomic_set(&vlan->refcnt, 1);
532 ret = __vlan_add(vlan, flags);
533 if (ret)
534 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000535
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000537}
538
Toshiaki Makita8adff412013-10-16 17:07:13 +0900539/* Must be protected by RTNL.
540 * Must be called with vid in range from 1 to 4094 inclusive.
541 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000542int br_vlan_delete(struct net_bridge *br, u16 vid)
543{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200544 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000545
546 ASSERT_RTNL();
547
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200548 v = br_vlan_find(br->vlgrp, vid);
549 if (!v || !br_vlan_is_brentry(v))
550 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000551
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900552 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000553
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200554 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000555}
556
557void br_vlan_flush(struct net_bridge *br)
558{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000559 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000560
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200561 __vlan_flush(br_vlan_group(br));
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000562}
563
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200564struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900565{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200566 if (!vg)
567 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900568
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200569 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900570}
571
Toshiaki Makita204177f2014-06-10 20:59:25 +0900572/* Must be protected by RTNL. */
573static void recalculate_group_addr(struct net_bridge *br)
574{
575 if (br->group_addr_set)
576 return;
577
578 spin_lock_bh(&br->lock);
579 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
580 /* Bridge Group Address */
581 br->group_addr[5] = 0x00;
582 } else { /* vlan_enabled && ETH_P_8021AD */
583 /* Provider Bridge Group Address */
584 br->group_addr[5] = 0x08;
585 }
586 spin_unlock_bh(&br->lock);
587}
588
589/* Must be protected by RTNL. */
590void br_recalculate_fwd_mask(struct net_bridge *br)
591{
592 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
593 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
594 else /* vlan_enabled && ETH_P_8021AD */
595 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
596 ~(1u << br->group_addr[5]);
597}
598
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300599int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000600{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000601 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300602 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000603
604 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400605 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900606 recalculate_group_addr(br);
607 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000608
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300609 return 0;
610}
611
612int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
613{
614 if (!rtnl_trylock())
615 return restart_syscall();
616
617 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000618 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300619
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000620 return 0;
621}
622
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900623int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900624{
625 int err = 0;
626 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200627 struct net_bridge_vlan *vlan;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900628 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900629
Toshiaki Makita204177f2014-06-10 20:59:25 +0900630 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900631 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900632
633 /* Add VLANs for the new proto to the device filter. */
634 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200635 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
636 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900637 if (err)
638 goto err_filt;
639 }
640 }
641
642 oldproto = br->vlan_proto;
643 br->vlan_proto = proto;
644
645 recalculate_group_addr(br);
646 br_recalculate_fwd_mask(br);
647
648 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200649 list_for_each_entry(p, &br->port_list, list)
650 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
651 vlan_vid_del(p->dev, oldproto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900652
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900653 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900654
655err_filt:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200656 list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
657 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900658
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200659 list_for_each_entry_continue_reverse(p, &br->port_list, list)
660 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
661 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900662
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900663 return err;
664}
665
666int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
667{
668 int err;
669
670 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
671 return -EPROTONOSUPPORT;
672
673 if (!rtnl_trylock())
674 return restart_syscall();
675
676 err = __br_vlan_set_proto(br, htons(val));
677 rtnl_unlock();
678
679 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900680}
681
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200682static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400683{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200684 struct net_bridge_vlan *v;
685
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200686 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200687 return false;
688
689 v = br_vlan_lookup(&vg->vlan_hash, vid);
690 if (v && br_vlan_should_use(v) &&
691 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
692 return true;
693
694 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400695}
696
697static void br_vlan_disable_default_pvid(struct net_bridge *br)
698{
699 struct net_bridge_port *p;
700 u16 pvid = br->default_pvid;
701
702 /* Disable default_pvid on all ports where it is still
703 * configured.
704 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200705 if (vlan_default_pvid(br->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400706 br_vlan_delete(br, pvid);
707
708 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200709 if (vlan_default_pvid(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400710 nbp_vlan_delete(p, pvid);
711 }
712
713 br->default_pvid = 0;
714}
715
716static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
717{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200718 const struct net_bridge_vlan *pvent;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400719 struct net_bridge_port *p;
720 u16 old_pvid;
721 int err = 0;
722 unsigned long *changed;
723
724 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
725 GFP_KERNEL);
726 if (!changed)
727 return -ENOMEM;
728
729 old_pvid = br->default_pvid;
730
731 /* Update default_pvid config only if we do not conflict with
732 * user configuration.
733 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200734 pvent = br_vlan_find(br->vlgrp, pvid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200735 if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200736 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400737 err = br_vlan_add(br, pvid,
738 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200739 BRIDGE_VLAN_INFO_UNTAGGED |
740 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400741 if (err)
742 goto out;
743 br_vlan_delete(br, old_pvid);
744 set_bit(0, changed);
745 }
746
747 list_for_each_entry(p, &br->port_list, list) {
748 /* Update default_pvid config only if we do not conflict with
749 * user configuration.
750 */
751 if ((old_pvid &&
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200752 !vlan_default_pvid(p->vlgrp, old_pvid)) ||
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200753 br_vlan_find(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400754 continue;
755
756 err = nbp_vlan_add(p, pvid,
757 BRIDGE_VLAN_INFO_PVID |
758 BRIDGE_VLAN_INFO_UNTAGGED);
759 if (err)
760 goto err_port;
761 nbp_vlan_delete(p, old_pvid);
762 set_bit(p->port_no, changed);
763 }
764
765 br->default_pvid = pvid;
766
767out:
768 kfree(changed);
769 return err;
770
771err_port:
772 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
773 if (!test_bit(p->port_no, changed))
774 continue;
775
776 if (old_pvid)
777 nbp_vlan_add(p, old_pvid,
778 BRIDGE_VLAN_INFO_PVID |
779 BRIDGE_VLAN_INFO_UNTAGGED);
780 nbp_vlan_delete(p, pvid);
781 }
782
783 if (test_bit(0, changed)) {
784 if (old_pvid)
785 br_vlan_add(br, old_pvid,
786 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200787 BRIDGE_VLAN_INFO_UNTAGGED |
788 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400789 br_vlan_delete(br, pvid);
790 }
791 goto out;
792}
793
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400794int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
795{
796 u16 pvid = val;
797 int err = 0;
798
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400799 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400800 return -EINVAL;
801
802 if (!rtnl_trylock())
803 return restart_syscall();
804
805 if (pvid == br->default_pvid)
806 goto unlock;
807
808 /* Only allow default pvid change when filtering is disabled */
809 if (br->vlan_enabled) {
810 pr_info_once("Please disable vlan filtering to change default_pvid\n");
811 err = -EPERM;
812 goto unlock;
813 }
814
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400815 if (!pvid)
816 br_vlan_disable_default_pvid(br);
817 else
818 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400819
820unlock:
821 rtnl_unlock();
822 return err;
823}
824
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400825int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900826{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200827 int ret = -ENOMEM;
828
829 br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
830 if (!br->vlgrp)
831 goto out;
832 ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
833 if (ret)
834 goto err_rhtbl;
835 INIT_LIST_HEAD(&br->vlgrp->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900836 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400837 br->default_pvid = 1;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200838 ret = br_vlan_add(br, 1,
839 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
840 BRIDGE_VLAN_INFO_BRENTRY);
841 if (ret)
842 goto err_vlan_add;
843
844out:
845 return ret;
846
847err_vlan_add:
848 rhashtable_destroy(&br->vlgrp->vlan_hash);
849err_rhtbl:
850 kfree(br->vlgrp);
851
852 goto out;
853}
854
855int nbp_vlan_init(struct net_bridge_port *p)
856{
857 int ret = -ENOMEM;
858
859 p->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
860 if (!p->vlgrp)
861 goto out;
862
863 ret = rhashtable_init(&p->vlgrp->vlan_hash, &br_vlan_rht_params);
864 if (ret)
865 goto err_rhtbl;
866 INIT_LIST_HEAD(&p->vlgrp->vlan_list);
867 if (p->br->default_pvid) {
868 ret = nbp_vlan_add(p, p->br->default_pvid,
869 BRIDGE_VLAN_INFO_PVID |
870 BRIDGE_VLAN_INFO_UNTAGGED);
871 if (ret)
872 goto err_vlan_add;
873 }
874out:
875 return ret;
876
877err_vlan_add:
878 rhashtable_destroy(&p->vlgrp->vlan_hash);
879err_rhtbl:
880 kfree(p->vlgrp);
881
882 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900883}
884
Toshiaki Makita8adff412013-10-16 17:07:13 +0900885/* Must be protected by RTNL.
886 * Must be called with vid in range from 1 to 4094 inclusive.
887 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000888int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000889{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200890 struct net_bridge_vlan *vlan;
891 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000892
893 ASSERT_RTNL();
894
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200895 vlan = br_vlan_find(port->vlgrp, vid);
896 if (vlan) {
897 __vlan_add_flags(vlan, flags);
898 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000899 }
900
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200901 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
902 if (!vlan)
903 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000904
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200905 vlan->vid = vid;
906 vlan->port = port;
907 ret = __vlan_add(vlan, flags);
908 if (ret)
909 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000910
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200911 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000912}
913
Toshiaki Makita8adff412013-10-16 17:07:13 +0900914/* Must be protected by RTNL.
915 * Must be called with vid in range from 1 to 4094 inclusive.
916 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000917int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
918{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200919 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000920
921 ASSERT_RTNL();
922
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200923 v = br_vlan_find(port->vlgrp, vid);
924 if (!v)
925 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900926 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700927 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000928
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200929 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000930}
931
932void nbp_vlan_flush(struct net_bridge_port *port)
933{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200934 struct net_bridge_vlan *vlan;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000935
936 ASSERT_RTNL();
937
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200938 list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
939 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000940
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200941 __vlan_flush(nbp_vlan_group(port));
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400942}