blob: d4cb129557f48ff2c91f898ff460fea0a0e59b6e [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 = {
Vivien Didelotab069002015-09-29 12:07:17 -040086 .flags = flags,
87 .vid_begin = vid,
88 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -070089 };
90
Jiri Pirko57d80832015-10-01 11:03:41 +020091 err = switchdev_port_obj_add(dev, SWITCHDEV_OBJ_ID_PORT_VLAN,
92 &v);
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 Aleksandrov2594e9062015-09-25 19:00:11 +0200114 list_add(&v->vlist, hpos);
115}
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{
119 list_del(&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 = {
Vivien Didelotab069002015-09-29 12:07:17 -0400136 .vid_begin = vid,
137 .vid_end = vid,
Scott Feldman7f109532015-06-12 17:39:50 -0700138 };
139
Jiri Pirko57d80832015-10-01 11:03:41 +0200140 err = switchdev_port_obj_del(dev, SWITCHDEV_OBJ_ID_PORT_VLAN,
141 &v);
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 Aleksandrov2594e9062015-09-25 19:00:11 +0200149/* This is the shared VLAN add function which works for both ports and bridge
150 * devices. There are four possible calls to this function in terms of the
151 * vlan entry type:
152 * 1. vlan is being added on a port (no master flags, global entry exists)
153 * 2. vlan is being added on a bridge (both master and brvlan flags)
154 * 3. vlan is being added on a port, but a global entry didn't exist which
155 * is being created right now (master flag set, brvlan flag unset), the
156 * global entry is used for global per-vlan features, but not for filtering
157 * 4. same as 3 but with both master and brvlan flags set so the entry
158 * will be used for filtering in both the port and the bridge
159 */
160static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000161{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200162 struct net_bridge_vlan *masterv = NULL;
163 struct net_bridge_port *p = NULL;
164 struct rhashtable *tbl;
165 struct net_device *dev;
166 struct net_bridge *br;
167 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000168
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200169 if (br_vlan_is_master(v)) {
170 br = v->br;
171 dev = br->dev;
172 tbl = &br->vlgrp->vlan_hash;
173 } else {
174 p = v->port;
175 br = p->br;
176 dev = p->dev;
177 tbl = &p->vlgrp->vlan_hash;
178 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000179
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200180 if (p) {
181 u16 master_flags = flags;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400182
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200183 /* Add VLAN to the device filter if it is supported.
184 * This ensures tagged traffic enters the bridge when
185 * promiscuous mode is disabled by br_manage_promisc().
186 */
187 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400188 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200189 goto out;
190
191 /* need to work on the master vlan too */
192 if (flags & BRIDGE_VLAN_INFO_MASTER) {
193 master_flags |= BRIDGE_VLAN_INFO_BRENTRY;
194 err = br_vlan_add(br, v->vid, master_flags);
195 if (err)
196 goto out_filt;
197 }
198
199 masterv = br_vlan_find(br->vlgrp, v->vid);
200 if (!masterv) {
201 /* missing global ctx, create it now */
Nikolay Aleksandrov248234c2015-09-30 20:16:55 +0200202 err = br_vlan_add(br, v->vid, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200203 if (err)
204 goto out_filt;
205 masterv = br_vlan_find(br->vlgrp, v->vid);
206 WARN_ON(!masterv);
207 }
208 atomic_inc(&masterv->refcnt);
209 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900210 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000211
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200212 /* Add the dev mac only if it's a usable vlan */
213 if (br_vlan_should_use(v)) {
214 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
215 if (err) {
216 br_err(br, "failed insert local address into bridge forwarding table\n");
217 goto out_filt;
218 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000219 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200220
221 err = rhashtable_lookup_insert_fast(tbl, &v->vnode, br_vlan_rht_params);
222 if (err)
223 goto out_fdb_insert;
224
225 __vlan_add_list(v);
226 __vlan_add_flags(v, flags);
227 if (br_vlan_is_master(v)) {
228 if (br_vlan_is_brentry(v))
229 br->vlgrp->num_vlans++;
230 } else {
231 p->vlgrp->num_vlans++;
232 }
233out:
234 return err;
235
236out_fdb_insert:
237 br_fdb_find_delete_local(br, p, br->dev->dev_addr, v->vid);
238
239out_filt:
240 if (p) {
241 __vlan_vid_del(dev, br, v->vid);
242 if (masterv) {
243 atomic_dec(&masterv->refcnt);
244 v->brvlan = NULL;
245 }
246 }
247
248 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000249}
250
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200251static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000252{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200253 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200254 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200255 struct net_bridge_port *p = NULL;
256 struct net_bridge *br;
257 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200258
259 if (br_vlan_is_master(v)) {
260 br = v->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200261 vg = v->br->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200262 } else {
263 p = v->port;
264 br = p->br;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200265 vg = v->port->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200266 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200267 }
268
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200269 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200270 if (p) {
271 err = __vlan_vid_del(p->dev, p->br, v->vid);
272 if (err)
273 goto out;
274 }
275
276 if (br_vlan_is_master(v)) {
277 if (br_vlan_is_brentry(v)) {
278 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
279 br->vlgrp->num_vlans--;
280 }
281 } else {
282 p->vlgrp->num_vlans--;
283 }
284
285 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200286 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
287 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 __vlan_del_list(v);
289 kfree_rcu(v, rcu);
290 }
291
292 if (atomic_dec_and_test(&masterv->refcnt)) {
293 rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash,
294 &masterv->vnode, br_vlan_rht_params);
295 __vlan_del_list(masterv);
296 kfree_rcu(masterv, rcu);
297 }
298out:
299 return err;
300}
301
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200302static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200303{
304 struct net_bridge_vlan *vlan, *tmp;
305
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200306 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200307 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
308 __vlan_del(vlan);
309 rhashtable_destroy(&vlgrp->vlan_hash);
310 kfree(vlgrp);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000311}
312
Vlad Yasevich78851982013-02-13 12:00:14 +0000313struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200314 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000315 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000316{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200317 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000318 u16 vid;
319
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400320 /* If this packet was not filtered at input, let it pass */
321 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000322 goto out;
323
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324 /* At this point, we know that the frame was filtered and contains
325 * a valid vlan id. If the vlan id has untagged flag set,
326 * send untagged; otherwise, send tagged.
327 */
328 br_vlan_get_tag(skb, &vid);
329 v = br_vlan_find(vg, vid);
330 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400331 * only exception is the bridge is set in promisc mode and the
332 * packet is destined for the bridge device. In this case
333 * pass the packet as is.
334 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200335 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400336 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
337 goto out;
338 } else {
339 kfree_skb(skb);
340 return NULL;
341 }
342 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200343 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900344 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000345
346out:
347 return skb;
348}
349
350/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200351static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200352 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000353{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900355 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000356
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400357 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900358 /* If vlan tx offload is disabled on bridge device and frame was
359 * sent from vlan device on the bridge device, it does not have
360 * HW accelerated vlan tag.
361 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100362 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900363 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400364 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900365 if (unlikely(!skb))
366 return false;
367 }
368
Toshiaki Makita8580e212014-06-10 20:59:23 +0900369 if (!br_vlan_get_tag(skb, vid)) {
370 /* Tagged frame */
371 if (skb->vlan_proto != proto) {
372 /* Protocol-mismatch, empty out vlan_tci for new tag */
373 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100374 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100375 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900376 if (unlikely(!skb))
377 return false;
378
379 skb_pull(skb, ETH_HLEN);
380 skb_reset_mac_len(skb);
381 *vid = 0;
382 tagged = false;
383 } else {
384 tagged = true;
385 }
386 } else {
387 /* Untagged frame */
388 tagged = false;
389 }
390
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900391 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200392 u16 pvid = br_get_pvid(vg);
393
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900394 /* Frame had a tag with VID 0 or did not have a tag.
395 * See if pvid is set on this port. That tells us which
396 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000397 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400398 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900399 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000400
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900401 /* PVID is set on this port. Any untagged or priority-tagged
402 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000403 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900404 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900405 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900406 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900407 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900408 else
409 /* Priority-tagged Frame.
410 * At this point, We know that skb->vlan_tci had
411 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
412 * We update only VID field and preserve PCP field.
413 */
414 skb->vlan_tci |= pvid;
415
Vlad Yasevich78851982013-02-13 12:00:14 +0000416 return true;
417 }
418
419 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200420 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200421 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000422 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900423drop:
424 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000425 return false;
426}
427
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200428bool br_allowed_ingress(const struct net_bridge *br,
429 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
430 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200431{
432 /* If VLAN filtering is disabled on the bridge, all packets are
433 * permitted.
434 */
435 if (!br->vlan_enabled) {
436 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
437 return true;
438 }
439
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200440 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200441}
442
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000443/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200444bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000445 const struct sk_buff *skb)
446{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200447 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000448 u16 vid;
449
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400450 /* If this packet was not filtered at input, let it pass */
451 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000452 return true;
453
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000454 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200455 v = br_vlan_find(vg, vid);
456 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000457 return true;
458
459 return false;
460}
461
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900462/* Called under RCU */
463bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
464{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200465 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900466 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900467
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400468 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400469 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900470 return true;
471
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200472 vg = p->vlgrp;
473 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900474 return false;
475
Toshiaki Makita8580e212014-06-10 20:59:23 +0900476 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
477 *vid = 0;
478
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900479 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200480 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400481 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900482 return false;
483
484 return true;
485 }
486
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200487 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900488 return true;
489
490 return false;
491}
492
Toshiaki Makita8adff412013-10-16 17:07:13 +0900493/* Must be protected by RTNL.
494 * Must be called with vid in range from 1 to 4094 inclusive.
495 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000496int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000497{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200498 struct net_bridge_vlan *vlan;
499 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000500
501 ASSERT_RTNL();
502
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200503 vlan = br_vlan_find(br->vlgrp, vid);
504 if (vlan) {
505 if (!br_vlan_is_brentry(vlan)) {
506 /* Trying to change flags of non-existent bridge vlan */
507 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
508 return -EINVAL;
509 /* It was only kept for port vlans, now make it real */
510 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
511 vlan->vid);
512 if (ret) {
513 br_err(br, "failed insert local address into bridge forwarding table\n");
514 return ret;
515 }
516 atomic_inc(&vlan->refcnt);
517 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
518 br->vlgrp->num_vlans++;
519 }
520 __vlan_add_flags(vlan, flags);
521 return 0;
522 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000523
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200524 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
525 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000526 return -ENOMEM;
527
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200528 vlan->vid = vid;
529 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
530 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
531 vlan->br = br;
532 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
533 atomic_set(&vlan->refcnt, 1);
534 ret = __vlan_add(vlan, flags);
535 if (ret)
536 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000537
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200538 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000539}
540
Toshiaki Makita8adff412013-10-16 17:07:13 +0900541/* Must be protected by RTNL.
542 * Must be called with vid in range from 1 to 4094 inclusive.
543 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000544int br_vlan_delete(struct net_bridge *br, u16 vid)
545{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200546 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000547
548 ASSERT_RTNL();
549
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200550 v = br_vlan_find(br->vlgrp, vid);
551 if (!v || !br_vlan_is_brentry(v))
552 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000553
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900554 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000555
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200556 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000557}
558
559void br_vlan_flush(struct net_bridge *br)
560{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000561 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000562
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200563 __vlan_flush(br_vlan_group(br));
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000564}
565
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200566struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900567{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200568 if (!vg)
569 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900570
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200571 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900572}
573
Toshiaki Makita204177f2014-06-10 20:59:25 +0900574/* Must be protected by RTNL. */
575static void recalculate_group_addr(struct net_bridge *br)
576{
577 if (br->group_addr_set)
578 return;
579
580 spin_lock_bh(&br->lock);
581 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
582 /* Bridge Group Address */
583 br->group_addr[5] = 0x00;
584 } else { /* vlan_enabled && ETH_P_8021AD */
585 /* Provider Bridge Group Address */
586 br->group_addr[5] = 0x08;
587 }
588 spin_unlock_bh(&br->lock);
589}
590
591/* Must be protected by RTNL. */
592void br_recalculate_fwd_mask(struct net_bridge *br)
593{
594 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
595 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
596 else /* vlan_enabled && ETH_P_8021AD */
597 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
598 ~(1u << br->group_addr[5]);
599}
600
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300601int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000602{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000603 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300604 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000605
606 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400607 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900608 recalculate_group_addr(br);
609 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000610
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300611 return 0;
612}
613
614int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
615{
616 if (!rtnl_trylock())
617 return restart_syscall();
618
619 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000620 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300621
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000622 return 0;
623}
624
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900625int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900626{
627 int err = 0;
628 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200629 struct net_bridge_vlan *vlan;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900630 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900631
Toshiaki Makita204177f2014-06-10 20:59:25 +0900632 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900633 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900634
635 /* Add VLANs for the new proto to the device filter. */
636 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200637 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
638 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900639 if (err)
640 goto err_filt;
641 }
642 }
643
644 oldproto = br->vlan_proto;
645 br->vlan_proto = proto;
646
647 recalculate_group_addr(br);
648 br_recalculate_fwd_mask(br);
649
650 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200651 list_for_each_entry(p, &br->port_list, list)
652 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
653 vlan_vid_del(p->dev, oldproto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900654
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900655 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900656
657err_filt:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200658 list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
659 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900660
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200661 list_for_each_entry_continue_reverse(p, &br->port_list, list)
662 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
663 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900664
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900665 return err;
666}
667
668int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
669{
670 int err;
671
672 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
673 return -EPROTONOSUPPORT;
674
675 if (!rtnl_trylock())
676 return restart_syscall();
677
678 err = __br_vlan_set_proto(br, htons(val));
679 rtnl_unlock();
680
681 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900682}
683
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200684static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400685{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200686 struct net_bridge_vlan *v;
687
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200688 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200689 return false;
690
691 v = br_vlan_lookup(&vg->vlan_hash, vid);
692 if (v && br_vlan_should_use(v) &&
693 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
694 return true;
695
696 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400697}
698
699static void br_vlan_disable_default_pvid(struct net_bridge *br)
700{
701 struct net_bridge_port *p;
702 u16 pvid = br->default_pvid;
703
704 /* Disable default_pvid on all ports where it is still
705 * configured.
706 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200707 if (vlan_default_pvid(br->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400708 br_vlan_delete(br, pvid);
709
710 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200711 if (vlan_default_pvid(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400712 nbp_vlan_delete(p, pvid);
713 }
714
715 br->default_pvid = 0;
716}
717
718static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
719{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200720 const struct net_bridge_vlan *pvent;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400721 struct net_bridge_port *p;
722 u16 old_pvid;
723 int err = 0;
724 unsigned long *changed;
725
726 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
727 GFP_KERNEL);
728 if (!changed)
729 return -ENOMEM;
730
731 old_pvid = br->default_pvid;
732
733 /* Update default_pvid config only if we do not conflict with
734 * user configuration.
735 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200736 pvent = br_vlan_find(br->vlgrp, pvid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200737 if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200738 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400739 err = br_vlan_add(br, pvid,
740 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200741 BRIDGE_VLAN_INFO_UNTAGGED |
742 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400743 if (err)
744 goto out;
745 br_vlan_delete(br, old_pvid);
746 set_bit(0, changed);
747 }
748
749 list_for_each_entry(p, &br->port_list, list) {
750 /* Update default_pvid config only if we do not conflict with
751 * user configuration.
752 */
753 if ((old_pvid &&
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200754 !vlan_default_pvid(p->vlgrp, old_pvid)) ||
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200755 br_vlan_find(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400756 continue;
757
758 err = nbp_vlan_add(p, pvid,
759 BRIDGE_VLAN_INFO_PVID |
760 BRIDGE_VLAN_INFO_UNTAGGED);
761 if (err)
762 goto err_port;
763 nbp_vlan_delete(p, old_pvid);
764 set_bit(p->port_no, changed);
765 }
766
767 br->default_pvid = pvid;
768
769out:
770 kfree(changed);
771 return err;
772
773err_port:
774 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
775 if (!test_bit(p->port_no, changed))
776 continue;
777
778 if (old_pvid)
779 nbp_vlan_add(p, old_pvid,
780 BRIDGE_VLAN_INFO_PVID |
781 BRIDGE_VLAN_INFO_UNTAGGED);
782 nbp_vlan_delete(p, pvid);
783 }
784
785 if (test_bit(0, changed)) {
786 if (old_pvid)
787 br_vlan_add(br, old_pvid,
788 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200789 BRIDGE_VLAN_INFO_UNTAGGED |
790 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400791 br_vlan_delete(br, pvid);
792 }
793 goto out;
794}
795
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400796int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
797{
798 u16 pvid = val;
799 int err = 0;
800
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400801 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400802 return -EINVAL;
803
804 if (!rtnl_trylock())
805 return restart_syscall();
806
807 if (pvid == br->default_pvid)
808 goto unlock;
809
810 /* Only allow default pvid change when filtering is disabled */
811 if (br->vlan_enabled) {
812 pr_info_once("Please disable vlan filtering to change default_pvid\n");
813 err = -EPERM;
814 goto unlock;
815 }
816
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400817 if (!pvid)
818 br_vlan_disable_default_pvid(br);
819 else
820 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400821
822unlock:
823 rtnl_unlock();
824 return err;
825}
826
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400827int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900828{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200829 int ret = -ENOMEM;
830
831 br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
832 if (!br->vlgrp)
833 goto out;
834 ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
835 if (ret)
836 goto err_rhtbl;
837 INIT_LIST_HEAD(&br->vlgrp->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900838 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400839 br->default_pvid = 1;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200840 ret = br_vlan_add(br, 1,
841 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
842 BRIDGE_VLAN_INFO_BRENTRY);
843 if (ret)
844 goto err_vlan_add;
845
846out:
847 return ret;
848
849err_vlan_add:
850 rhashtable_destroy(&br->vlgrp->vlan_hash);
851err_rhtbl:
852 kfree(br->vlgrp);
853
854 goto out;
855}
856
857int nbp_vlan_init(struct net_bridge_port *p)
858{
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200859 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200860 int ret = -ENOMEM;
861
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200862 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
863 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200864 goto out;
865
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200866 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200867 if (ret)
868 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200869 INIT_LIST_HEAD(&vg->vlan_list);
870 /* Make sure everything's committed before publishing vg */
871 smp_wmb();
872 p->vlgrp = vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200873 if (p->br->default_pvid) {
874 ret = nbp_vlan_add(p, p->br->default_pvid,
875 BRIDGE_VLAN_INFO_PVID |
876 BRIDGE_VLAN_INFO_UNTAGGED);
877 if (ret)
878 goto err_vlan_add;
879 }
880out:
881 return ret;
882
883err_vlan_add:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200884 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200885err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200886 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200887
888 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900889}
890
Toshiaki Makita8adff412013-10-16 17:07:13 +0900891/* Must be protected by RTNL.
892 * Must be called with vid in range from 1 to 4094 inclusive.
893 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000894int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000895{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200896 struct net_bridge_vlan *vlan;
897 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000898
899 ASSERT_RTNL();
900
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200901 vlan = br_vlan_find(port->vlgrp, vid);
902 if (vlan) {
903 __vlan_add_flags(vlan, flags);
904 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000905 }
906
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200907 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
908 if (!vlan)
909 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000910
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200911 vlan->vid = vid;
912 vlan->port = port;
913 ret = __vlan_add(vlan, flags);
914 if (ret)
915 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000916
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200917 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000918}
919
Toshiaki Makita8adff412013-10-16 17:07:13 +0900920/* Must be protected by RTNL.
921 * Must be called with vid in range from 1 to 4094 inclusive.
922 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000923int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
924{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200925 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000926
927 ASSERT_RTNL();
928
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200929 v = br_vlan_find(port->vlgrp, vid);
930 if (!v)
931 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900932 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700933 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000934
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200935 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000936}
937
938void nbp_vlan_flush(struct net_bridge_port *port)
939{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200940 struct net_bridge_vlan *vlan;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000941
942 ASSERT_RTNL();
943
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200944 list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
945 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000946
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200947 __vlan_flush(nbp_vlan_group(port));
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400948}