blob: 7a95e31f782f261856aebbda185de0db01687578 [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;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200198 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200199 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;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200206 vg = br->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200207 } else {
208 p = v->port;
209 br = p->br;
210 dev = p->dev;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200211 vg = p->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200212 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000213
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200214 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 /* Add VLAN to the device filter if it is supported.
216 * This ensures tagged traffic enters the bridge when
217 * promiscuous mode is disabled by br_manage_promisc().
218 */
219 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400220 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200221 goto out;
222
223 /* need to work on the master vlan too */
224 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrov2ffdf5082015-10-02 15:05:12 +0200225 err = br_vlan_add(br, v->vid, flags |
226 BRIDGE_VLAN_INFO_BRENTRY);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200227 if (err)
228 goto out_filt;
229 }
230
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200231 masterv = br_vlan_get_master(br, v->vid);
232 if (!masterv)
233 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200234 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900235 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000236
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200237 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200238 if (br_vlan_should_use(v)) {
239 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
240 if (err) {
241 br_err(br, "failed insert local address into bridge forwarding table\n");
242 goto out_filt;
243 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200244 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000245 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200246
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200247 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
248 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200249 if (err)
250 goto out_fdb_insert;
251
252 __vlan_add_list(v);
253 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200254out:
255 return err;
256
257out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200258 if (br_vlan_should_use(v)) {
259 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
260 vg->num_vlans--;
261 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200262
263out_filt:
264 if (p) {
265 __vlan_vid_del(dev, br, v->vid);
266 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200267 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200268 v->brvlan = NULL;
269 }
270 }
271
272 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000273}
274
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200275static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000276{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200277 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200278 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200279 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200280 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200281
282 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200283 vg = v->br->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200284 } else {
285 p = v->port;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200286 vg = v->port->vlgrp;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200287 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 }
289
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200290 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200291 if (p) {
292 err = __vlan_vid_del(p->dev, p->br, v->vid);
293 if (err)
294 goto out;
295 }
296
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200297 if (br_vlan_should_use(v)) {
298 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
299 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200300 }
301
302 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200303 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
304 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305 __vlan_del_list(v);
306 kfree_rcu(v, rcu);
307 }
308
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200309 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200310out:
311 return err;
312}
313
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200314static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200315{
316 struct net_bridge_vlan *vlan, *tmp;
317
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200318 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200319 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
320 __vlan_del(vlan);
321 rhashtable_destroy(&vlgrp->vlan_hash);
322 kfree(vlgrp);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000323}
324
Vlad Yasevich78851982013-02-13 12:00:14 +0000325struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000327 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000328{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000330 u16 vid;
331
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400332 /* If this packet was not filtered at input, let it pass */
333 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000334 goto out;
335
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 /* At this point, we know that the frame was filtered and contains
337 * a valid vlan id. If the vlan id has untagged flag set,
338 * send untagged; otherwise, send tagged.
339 */
340 br_vlan_get_tag(skb, &vid);
341 v = br_vlan_find(vg, vid);
342 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400343 * only exception is the bridge is set in promisc mode and the
344 * packet is destined for the bridge device. In this case
345 * pass the packet as is.
346 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200347 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400348 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
349 goto out;
350 } else {
351 kfree_skb(skb);
352 return NULL;
353 }
354 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200355 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900356 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000357
358out:
359 return skb;
360}
361
362/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200363static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200364 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000365{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200366 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900367 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000368
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400369 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900370 /* If vlan tx offload is disabled on bridge device and frame was
371 * sent from vlan device on the bridge device, it does not have
372 * HW accelerated vlan tag.
373 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100374 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900375 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400376 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900377 if (unlikely(!skb))
378 return false;
379 }
380
Toshiaki Makita8580e212014-06-10 20:59:23 +0900381 if (!br_vlan_get_tag(skb, vid)) {
382 /* Tagged frame */
383 if (skb->vlan_proto != proto) {
384 /* Protocol-mismatch, empty out vlan_tci for new tag */
385 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100386 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100387 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900388 if (unlikely(!skb))
389 return false;
390
391 skb_pull(skb, ETH_HLEN);
392 skb_reset_mac_len(skb);
393 *vid = 0;
394 tagged = false;
395 } else {
396 tagged = true;
397 }
398 } else {
399 /* Untagged frame */
400 tagged = false;
401 }
402
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900403 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200404 u16 pvid = br_get_pvid(vg);
405
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900406 /* Frame had a tag with VID 0 or did not have a tag.
407 * See if pvid is set on this port. That tells us which
408 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000409 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400410 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900411 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000412
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900413 /* PVID is set on this port. Any untagged or priority-tagged
414 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000415 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900416 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900417 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900418 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900419 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900420 else
421 /* Priority-tagged Frame.
422 * At this point, We know that skb->vlan_tci had
423 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
424 * We update only VID field and preserve PCP field.
425 */
426 skb->vlan_tci |= pvid;
427
Vlad Yasevich78851982013-02-13 12:00:14 +0000428 return true;
429 }
430
431 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200432 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200433 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000434 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900435drop:
436 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000437 return false;
438}
439
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200440bool br_allowed_ingress(const struct net_bridge *br,
441 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
442 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200443{
444 /* If VLAN filtering is disabled on the bridge, all packets are
445 * permitted.
446 */
447 if (!br->vlan_enabled) {
448 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
449 return true;
450 }
451
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200452 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200453}
454
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000455/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200456bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000457 const struct sk_buff *skb)
458{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200459 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000460 u16 vid;
461
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400462 /* If this packet was not filtered at input, let it pass */
463 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000464 return true;
465
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000466 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200467 v = br_vlan_find(vg, vid);
468 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000469 return true;
470
471 return false;
472}
473
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900474/* Called under RCU */
475bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
476{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200477 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900478 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900479
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400480 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400481 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900482 return true;
483
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200484 vg = p->vlgrp;
485 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900486 return false;
487
Toshiaki Makita8580e212014-06-10 20:59:23 +0900488 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
489 *vid = 0;
490
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900491 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200492 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400493 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900494 return false;
495
496 return true;
497 }
498
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200499 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900500 return true;
501
502 return false;
503}
504
Toshiaki Makita8adff412013-10-16 17:07:13 +0900505/* Must be protected by RTNL.
506 * Must be called with vid in range from 1 to 4094 inclusive.
507 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000508int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000509{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200510 struct net_bridge_vlan *vlan;
511 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000512
513 ASSERT_RTNL();
514
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200515 vlan = br_vlan_find(br->vlgrp, vid);
516 if (vlan) {
517 if (!br_vlan_is_brentry(vlan)) {
518 /* Trying to change flags of non-existent bridge vlan */
519 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
520 return -EINVAL;
521 /* It was only kept for port vlans, now make it real */
522 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
523 vlan->vid);
524 if (ret) {
525 br_err(br, "failed insert local address into bridge forwarding table\n");
526 return ret;
527 }
528 atomic_inc(&vlan->refcnt);
529 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
530 br->vlgrp->num_vlans++;
531 }
532 __vlan_add_flags(vlan, flags);
533 return 0;
534 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000535
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
537 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000538 return -ENOMEM;
539
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200540 vlan->vid = vid;
541 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
542 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
543 vlan->br = br;
544 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
545 atomic_set(&vlan->refcnt, 1);
546 ret = __vlan_add(vlan, flags);
547 if (ret)
548 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000549
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200550 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000551}
552
Toshiaki Makita8adff412013-10-16 17:07:13 +0900553/* Must be protected by RTNL.
554 * Must be called with vid in range from 1 to 4094 inclusive.
555 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000556int br_vlan_delete(struct net_bridge *br, u16 vid)
557{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200558 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000559
560 ASSERT_RTNL();
561
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200562 v = br_vlan_find(br->vlgrp, vid);
563 if (!v || !br_vlan_is_brentry(v))
564 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000565
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900566 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700567 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000568
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200569 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000570}
571
572void br_vlan_flush(struct net_bridge *br)
573{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000574 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000575
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200576 __vlan_flush(br_vlan_group(br));
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000577}
578
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200579struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900580{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200581 if (!vg)
582 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900583
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200584 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900585}
586
Toshiaki Makita204177f2014-06-10 20:59:25 +0900587/* Must be protected by RTNL. */
588static void recalculate_group_addr(struct net_bridge *br)
589{
590 if (br->group_addr_set)
591 return;
592
593 spin_lock_bh(&br->lock);
594 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
595 /* Bridge Group Address */
596 br->group_addr[5] = 0x00;
597 } else { /* vlan_enabled && ETH_P_8021AD */
598 /* Provider Bridge Group Address */
599 br->group_addr[5] = 0x08;
600 }
601 spin_unlock_bh(&br->lock);
602}
603
604/* Must be protected by RTNL. */
605void br_recalculate_fwd_mask(struct net_bridge *br)
606{
607 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
608 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
609 else /* vlan_enabled && ETH_P_8021AD */
610 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
611 ~(1u << br->group_addr[5]);
612}
613
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300614int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000615{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000616 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300617 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000618
619 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400620 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900621 recalculate_group_addr(br);
622 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000623
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300624 return 0;
625}
626
627int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
628{
629 if (!rtnl_trylock())
630 return restart_syscall();
631
632 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000633 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300634
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000635 return 0;
636}
637
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900638int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900639{
640 int err = 0;
641 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200642 struct net_bridge_vlan *vlan;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900643 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900644
Toshiaki Makita204177f2014-06-10 20:59:25 +0900645 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900646 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900647
648 /* Add VLANs for the new proto to the device filter. */
649 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200650 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
651 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900652 if (err)
653 goto err_filt;
654 }
655 }
656
657 oldproto = br->vlan_proto;
658 br->vlan_proto = proto;
659
660 recalculate_group_addr(br);
661 br_recalculate_fwd_mask(br);
662
663 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200664 list_for_each_entry(p, &br->port_list, list)
665 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
666 vlan_vid_del(p->dev, oldproto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900667
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900668 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900669
670err_filt:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200671 list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
672 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900673
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200674 list_for_each_entry_continue_reverse(p, &br->port_list, list)
675 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
676 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900677
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900678 return err;
679}
680
681int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
682{
683 int err;
684
685 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
686 return -EPROTONOSUPPORT;
687
688 if (!rtnl_trylock())
689 return restart_syscall();
690
691 err = __br_vlan_set_proto(br, htons(val));
692 rtnl_unlock();
693
694 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900695}
696
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200697static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400698{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200699 struct net_bridge_vlan *v;
700
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200701 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200702 return false;
703
704 v = br_vlan_lookup(&vg->vlan_hash, vid);
705 if (v && br_vlan_should_use(v) &&
706 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
707 return true;
708
709 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400710}
711
712static void br_vlan_disable_default_pvid(struct net_bridge *br)
713{
714 struct net_bridge_port *p;
715 u16 pvid = br->default_pvid;
716
717 /* Disable default_pvid on all ports where it is still
718 * configured.
719 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200720 if (vlan_default_pvid(br->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400721 br_vlan_delete(br, pvid);
722
723 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200724 if (vlan_default_pvid(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400725 nbp_vlan_delete(p, pvid);
726 }
727
728 br->default_pvid = 0;
729}
730
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200731int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400732{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200733 const struct net_bridge_vlan *pvent;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400734 struct net_bridge_port *p;
735 u16 old_pvid;
736 int err = 0;
737 unsigned long *changed;
738
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200739 if (!pvid) {
740 br_vlan_disable_default_pvid(br);
741 return 0;
742 }
743
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400744 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
745 GFP_KERNEL);
746 if (!changed)
747 return -ENOMEM;
748
749 old_pvid = br->default_pvid;
750
751 /* Update default_pvid config only if we do not conflict with
752 * user configuration.
753 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200754 pvent = br_vlan_find(br->vlgrp, pvid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200755 if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200756 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400757 err = br_vlan_add(br, pvid,
758 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200759 BRIDGE_VLAN_INFO_UNTAGGED |
760 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400761 if (err)
762 goto out;
763 br_vlan_delete(br, old_pvid);
764 set_bit(0, changed);
765 }
766
767 list_for_each_entry(p, &br->port_list, list) {
768 /* Update default_pvid config only if we do not conflict with
769 * user configuration.
770 */
771 if ((old_pvid &&
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200772 !vlan_default_pvid(p->vlgrp, old_pvid)) ||
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200773 br_vlan_find(p->vlgrp, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400774 continue;
775
776 err = nbp_vlan_add(p, pvid,
777 BRIDGE_VLAN_INFO_PVID |
778 BRIDGE_VLAN_INFO_UNTAGGED);
779 if (err)
780 goto err_port;
781 nbp_vlan_delete(p, old_pvid);
782 set_bit(p->port_no, changed);
783 }
784
785 br->default_pvid = pvid;
786
787out:
788 kfree(changed);
789 return err;
790
791err_port:
792 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
793 if (!test_bit(p->port_no, changed))
794 continue;
795
796 if (old_pvid)
797 nbp_vlan_add(p, old_pvid,
798 BRIDGE_VLAN_INFO_PVID |
799 BRIDGE_VLAN_INFO_UNTAGGED);
800 nbp_vlan_delete(p, pvid);
801 }
802
803 if (test_bit(0, changed)) {
804 if (old_pvid)
805 br_vlan_add(br, old_pvid,
806 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200807 BRIDGE_VLAN_INFO_UNTAGGED |
808 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400809 br_vlan_delete(br, pvid);
810 }
811 goto out;
812}
813
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400814int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
815{
816 u16 pvid = val;
817 int err = 0;
818
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400819 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400820 return -EINVAL;
821
822 if (!rtnl_trylock())
823 return restart_syscall();
824
825 if (pvid == br->default_pvid)
826 goto unlock;
827
828 /* Only allow default pvid change when filtering is disabled */
829 if (br->vlan_enabled) {
830 pr_info_once("Please disable vlan filtering to change default_pvid\n");
831 err = -EPERM;
832 goto unlock;
833 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200834 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400835unlock:
836 rtnl_unlock();
837 return err;
838}
839
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400840int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900841{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200842 int ret = -ENOMEM;
843
844 br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
845 if (!br->vlgrp)
846 goto out;
847 ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
848 if (ret)
849 goto err_rhtbl;
850 INIT_LIST_HEAD(&br->vlgrp->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900851 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400852 br->default_pvid = 1;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200853 ret = br_vlan_add(br, 1,
854 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
855 BRIDGE_VLAN_INFO_BRENTRY);
856 if (ret)
857 goto err_vlan_add;
858
859out:
860 return ret;
861
862err_vlan_add:
863 rhashtable_destroy(&br->vlgrp->vlan_hash);
864err_rhtbl:
865 kfree(br->vlgrp);
866
867 goto out;
868}
869
870int nbp_vlan_init(struct net_bridge_port *p)
871{
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200872 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200873 int ret = -ENOMEM;
874
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200875 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
876 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200877 goto out;
878
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200879 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200880 if (ret)
881 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200882 INIT_LIST_HEAD(&vg->vlan_list);
883 /* Make sure everything's committed before publishing vg */
884 smp_wmb();
885 p->vlgrp = vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200886 if (p->br->default_pvid) {
887 ret = nbp_vlan_add(p, p->br->default_pvid,
888 BRIDGE_VLAN_INFO_PVID |
889 BRIDGE_VLAN_INFO_UNTAGGED);
890 if (ret)
891 goto err_vlan_add;
892 }
893out:
894 return ret;
895
896err_vlan_add:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200897 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200898err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200899 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200900
901 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900902}
903
Toshiaki Makita8adff412013-10-16 17:07:13 +0900904/* Must be protected by RTNL.
905 * Must be called with vid in range from 1 to 4094 inclusive.
906 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000907int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000908{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200909 struct net_bridge_vlan *vlan;
910 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000911
912 ASSERT_RTNL();
913
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200914 vlan = br_vlan_find(port->vlgrp, vid);
915 if (vlan) {
916 __vlan_add_flags(vlan, flags);
917 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000918 }
919
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200920 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
921 if (!vlan)
922 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000923
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200924 vlan->vid = vid;
925 vlan->port = port;
926 ret = __vlan_add(vlan, flags);
927 if (ret)
928 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000929
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200930 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000931}
932
Toshiaki Makita8adff412013-10-16 17:07:13 +0900933/* Must be protected by RTNL.
934 * Must be called with vid in range from 1 to 4094 inclusive.
935 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000936int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
937{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200938 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000939
940 ASSERT_RTNL();
941
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200942 v = br_vlan_find(port->vlgrp, vid);
943 if (!v)
944 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900945 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700946 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000947
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200948 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000949}
950
951void nbp_vlan_flush(struct net_bridge_port *port)
952{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200953 struct net_bridge_vlan *vlan;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000954
955 ASSERT_RTNL();
956
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200957 list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
958 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000959
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200960 __vlan_flush(nbp_vlan_group(port));
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400961}