blob: 3cef6892c0bbdb03b0807e3fa48711b44e676020 [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
Vlad Yasevich552406c2013-02-13 12:00:15 +00009static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
10{
11 if (v->pvid == vid)
12 return;
13
14 smp_wmb();
15 v->pvid = vid;
16}
17
18static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
19{
20 if (v->pvid != vid)
21 return;
22
23 smp_wmb();
24 v->pvid = 0;
25}
26
Vlad Yasevich35e03f32013-02-13 12:00:20 +000027static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
28{
29 if (flags & BRIDGE_VLAN_INFO_PVID)
30 __vlan_add_pvid(v, vid);
Vlad Yasevich635126b2014-09-12 16:26:17 -040031 else
32 __vlan_delete_pvid(v, vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000033
34 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
35 set_bit(vid, v->untagged_bitmap);
Vlad Yasevich635126b2014-09-12 16:26:17 -040036 else
37 clear_bit(vid, v->untagged_bitmap);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000038}
39
Scott Feldman7f109532015-06-12 17:39:50 -070040static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
41 u16 vid, u16 flags)
42{
43 const struct net_device_ops *ops = dev->netdev_ops;
44 int err;
45
46 /* If driver uses VLAN ndo ops, use 8021q to install vid
47 * on device, otherwise try switchdev ops to install vid.
48 */
49
50 if (ops->ndo_vlan_rx_add_vid) {
51 err = vlan_vid_add(dev, br->vlan_proto, vid);
52 } else {
53 struct switchdev_obj vlan_obj = {
54 .id = SWITCHDEV_OBJ_PORT_VLAN,
55 .u.vlan = {
56 .flags = flags,
Scott Feldman3e3a78b2015-06-22 00:27:16 -070057 .vid_begin = vid,
Scott Feldman7f109532015-06-12 17:39:50 -070058 .vid_end = vid,
59 },
60 };
61
62 err = switchdev_port_obj_add(dev, &vlan_obj);
63 if (err == -EOPNOTSUPP)
64 err = 0;
65 }
66
67 return err;
68}
69
Vlad Yasevich552406c2013-02-13 12:00:15 +000070static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000071{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000072 struct net_bridge_port *p = NULL;
73 struct net_bridge *br;
74 struct net_device *dev;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000075 int err;
76
Vlad Yasevich552406c2013-02-13 12:00:15 +000077 if (test_bit(vid, v->vlan_bitmap)) {
Vlad Yasevich35e03f32013-02-13 12:00:20 +000078 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +000079 return 0;
80 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000081
Toshiaki Makita8adff412013-10-16 17:07:13 +090082 if (v->port_idx) {
83 p = v->parent.port;
84 br = p->br;
85 dev = p->dev;
86 } else {
87 br = v->parent.br;
88 dev = br->dev;
89 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000090
Toshiaki Makita19236832013-11-13 17:26:12 +090091 if (p) {
Toshiaki Makita8adff412013-10-16 17:07:13 +090092 /* Add VLAN to the device filter if it is supported.
Toshiaki Makitafdb0a662014-08-05 15:57:15 +090093 * This ensures tagged traffic enters the bridge when
94 * promiscuous mode is disabled by br_manage_promisc().
Toshiaki Makita8adff412013-10-16 17:07:13 +090095 */
Scott Feldman7f109532015-06-12 17:39:50 -070096 err = __vlan_vid_add(dev, br, vid, flags);
Toshiaki Makita8adff412013-10-16 17:07:13 +090097 if (err)
98 return err;
99 }
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000100
Toshiaki Makita8adff412013-10-16 17:07:13 +0900101 err = br_fdb_insert(br, p, dev->dev_addr, vid);
102 if (err) {
103 br_err(br, "failed insert local address into bridge "
104 "forwarding table\n");
105 goto out_filt;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000106 }
107
108 set_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000109 v->num_vlans++;
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000110 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +0000111
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000112 return 0;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000113
114out_filt:
Toshiaki Makita19236832013-11-13 17:26:12 +0900115 if (p)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900116 vlan_vid_del(dev, br->vlan_proto, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000117 return err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000118}
119
Scott Feldman7f109532015-06-12 17:39:50 -0700120static void __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
121 u16 vid)
122{
123 const struct net_device_ops *ops = dev->netdev_ops;
124
125 /* If driver uses VLAN ndo ops, use 8021q to delete vid
126 * on device, otherwise try switchdev ops to delete vid.
127 */
128
129 if (ops->ndo_vlan_rx_kill_vid) {
130 vlan_vid_del(dev, br->vlan_proto, vid);
131 } else {
132 struct switchdev_obj vlan_obj = {
133 .id = SWITCHDEV_OBJ_PORT_VLAN,
134 .u.vlan = {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700135 .vid_begin = vid,
Scott Feldman7f109532015-06-12 17:39:50 -0700136 .vid_end = vid,
137 },
138 };
139
140 switchdev_port_obj_del(dev, &vlan_obj);
141 }
142}
143
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000144static int __vlan_del(struct net_port_vlans *v, u16 vid)
145{
146 if (!test_bit(vid, v->vlan_bitmap))
147 return -EINVAL;
148
Vlad Yasevich552406c2013-02-13 12:00:15 +0000149 __vlan_delete_pvid(v, vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000150 clear_bit(vid, v->untagged_bitmap);
Vlad Yasevich552406c2013-02-13 12:00:15 +0000151
Toshiaki Makita8580e212014-06-10 20:59:23 +0900152 if (v->port_idx) {
153 struct net_bridge_port *p = v->parent.port;
Scott Feldman7f109532015-06-12 17:39:50 -0700154 __vlan_vid_del(p->dev, p->br, vid);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900155 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000156
157 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000158 v->num_vlans--;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900159 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000160 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530161 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000162 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530163 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000164 kfree_rcu(v, rcu);
165 }
166 return 0;
167}
168
169static void __vlan_flush(struct net_port_vlans *v)
170{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000171 smp_wmb();
172 v->pvid = 0;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900173 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000174 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530175 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000176 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530177 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000178 kfree_rcu(v, rcu);
179}
180
Vlad Yasevich78851982013-02-13 12:00:14 +0000181struct sk_buff *br_handle_vlan(struct net_bridge *br,
182 const struct net_port_vlans *pv,
183 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000184{
185 u16 vid;
186
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400187 /* If this packet was not filtered at input, let it pass */
188 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000189 goto out;
190
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400191 /* Vlan filter table must be configured at this point. The
192 * only exception is the bridge is set in promisc mode and the
193 * packet is destined for the bridge device. In this case
194 * pass the packet as is.
195 */
196 if (!pv) {
197 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
198 goto out;
199 } else {
200 kfree_skb(skb);
201 return NULL;
202 }
203 }
204
Vlad Yasevich78851982013-02-13 12:00:14 +0000205 /* At this point, we know that the frame was filtered and contains
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000206 * a valid vlan id. If the vlan id is set in the untagged bitmap,
tanxiaojun1a81a2e2013-12-16 21:32:46 +0800207 * send untagged; otherwise, send tagged.
Vlad Yasevich78851982013-02-13 12:00:14 +0000208 */
209 br_vlan_get_tag(skb, &vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000210 if (test_bit(vid, pv->untagged_bitmap))
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900211 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000212
213out:
214 return skb;
215}
216
217/* Called under RCU */
218bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
219 struct sk_buff *skb, u16 *vid)
220{
Toshiaki Makita8580e212014-06-10 20:59:23 +0900221 bool tagged;
222 __be16 proto;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900223
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000224 /* If VLAN filtering is disabled on the bridge, all packets are
225 * permitted.
226 */
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400227 if (!br->vlan_enabled) {
228 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000229 return true;
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400230 }
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000231
232 /* If there are no vlan in the permitted list, all packets are
233 * rejected.
234 */
235 if (!v)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900236 goto drop;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000237
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400238 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900239 proto = br->vlan_proto;
240
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900241 /* If vlan tx offload is disabled on bridge device and frame was
242 * sent from vlan device on the bridge device, it does not have
243 * HW accelerated vlan tag.
244 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100245 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900246 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400247 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900248 if (unlikely(!skb))
249 return false;
250 }
251
Toshiaki Makita8580e212014-06-10 20:59:23 +0900252 if (!br_vlan_get_tag(skb, vid)) {
253 /* Tagged frame */
254 if (skb->vlan_proto != proto) {
255 /* Protocol-mismatch, empty out vlan_tci for new tag */
256 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100257 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100258 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900259 if (unlikely(!skb))
260 return false;
261
262 skb_pull(skb, ETH_HLEN);
263 skb_reset_mac_len(skb);
264 *vid = 0;
265 tagged = false;
266 } else {
267 tagged = true;
268 }
269 } else {
270 /* Untagged frame */
271 tagged = false;
272 }
273
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900274 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000275 u16 pvid = br_get_pvid(v);
276
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900277 /* Frame had a tag with VID 0 or did not have a tag.
278 * See if pvid is set on this port. That tells us which
279 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000280 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400281 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900282 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000283
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900284 /* PVID is set on this port. Any untagged or priority-tagged
285 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000286 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900287 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900288 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900289 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900290 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900291 else
292 /* Priority-tagged Frame.
293 * At this point, We know that skb->vlan_tci had
294 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
295 * We update only VID field and preserve PCP field.
296 */
297 skb->vlan_tci |= pvid;
298
Vlad Yasevich78851982013-02-13 12:00:14 +0000299 return true;
300 }
301
302 /* Frame had a valid vlan tag. See if vlan is allowed */
303 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000304 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900305drop:
306 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000307 return false;
308}
309
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000310/* Called under RCU. */
311bool br_allowed_egress(struct net_bridge *br,
312 const struct net_port_vlans *v,
313 const struct sk_buff *skb)
314{
315 u16 vid;
316
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400317 /* If this packet was not filtered at input, let it pass */
318 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000319 return true;
320
321 if (!v)
322 return false;
323
324 br_vlan_get_tag(skb, &vid);
325 if (test_bit(vid, v->vlan_bitmap))
326 return true;
327
328 return false;
329}
330
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900331/* Called under RCU */
332bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
333{
334 struct net_bridge *br = p->br;
335 struct net_port_vlans *v;
336
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400337 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400338 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900339 return true;
340
341 v = rcu_dereference(p->vlan_info);
342 if (!v)
343 return false;
344
Toshiaki Makita8580e212014-06-10 20:59:23 +0900345 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
346 *vid = 0;
347
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900348 if (!*vid) {
349 *vid = br_get_pvid(v);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400350 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900351 return false;
352
353 return true;
354 }
355
356 if (test_bit(*vid, v->vlan_bitmap))
357 return true;
358
359 return false;
360}
361
Toshiaki Makita8adff412013-10-16 17:07:13 +0900362/* Must be protected by RTNL.
363 * Must be called with vid in range from 1 to 4094 inclusive.
364 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000365int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000366{
367 struct net_port_vlans *pv = NULL;
368 int err;
369
370 ASSERT_RTNL();
371
372 pv = rtnl_dereference(br->vlan_info);
373 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000374 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000375
376 /* Create port vlan infomration
377 */
378 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
379 if (!pv)
380 return -ENOMEM;
381
382 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000383 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000384 if (err)
385 goto out;
386
387 rcu_assign_pointer(br->vlan_info, pv);
388 return 0;
389out:
390 kfree(pv);
391 return err;
392}
393
Toshiaki Makita8adff412013-10-16 17:07:13 +0900394/* Must be protected by RTNL.
395 * Must be called with vid in range from 1 to 4094 inclusive.
396 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000397int br_vlan_delete(struct net_bridge *br, u16 vid)
398{
399 struct net_port_vlans *pv;
400
401 ASSERT_RTNL();
402
403 pv = rtnl_dereference(br->vlan_info);
404 if (!pv)
405 return -EINVAL;
406
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900407 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000408
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000409 __vlan_del(pv, vid);
410 return 0;
411}
412
413void br_vlan_flush(struct net_bridge *br)
414{
415 struct net_port_vlans *pv;
416
417 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000418 pv = rtnl_dereference(br->vlan_info);
419 if (!pv)
420 return;
421
422 __vlan_flush(pv);
423}
424
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900425bool br_vlan_find(struct net_bridge *br, u16 vid)
426{
427 struct net_port_vlans *pv;
428 bool found = false;
429
430 rcu_read_lock();
431 pv = rcu_dereference(br->vlan_info);
432
433 if (!pv)
434 goto out;
435
436 if (test_bit(vid, pv->vlan_bitmap))
437 found = true;
438
439out:
440 rcu_read_unlock();
441 return found;
442}
443
Toshiaki Makita204177f2014-06-10 20:59:25 +0900444/* Must be protected by RTNL. */
445static void recalculate_group_addr(struct net_bridge *br)
446{
447 if (br->group_addr_set)
448 return;
449
450 spin_lock_bh(&br->lock);
451 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
452 /* Bridge Group Address */
453 br->group_addr[5] = 0x00;
454 } else { /* vlan_enabled && ETH_P_8021AD */
455 /* Provider Bridge Group Address */
456 br->group_addr[5] = 0x08;
457 }
458 spin_unlock_bh(&br->lock);
459}
460
461/* Must be protected by RTNL. */
462void br_recalculate_fwd_mask(struct net_bridge *br)
463{
464 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
465 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
466 else /* vlan_enabled && ETH_P_8021AD */
467 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
468 ~(1u << br->group_addr[5]);
469}
470
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300471int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000472{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000473 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300474 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000475
476 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400477 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900478 recalculate_group_addr(br);
479 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000480
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300481 return 0;
482}
483
484int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
485{
486 if (!rtnl_trylock())
487 return restart_syscall();
488
489 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000490 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300491
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000492 return 0;
493}
494
Toshiaki Makita204177f2014-06-10 20:59:25 +0900495int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
496{
497 int err = 0;
498 struct net_bridge_port *p;
499 struct net_port_vlans *pv;
500 __be16 proto, oldproto;
501 u16 vid, errvid;
502
503 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
504 return -EPROTONOSUPPORT;
505
506 if (!rtnl_trylock())
507 return restart_syscall();
508
509 proto = htons(val);
510 if (br->vlan_proto == proto)
511 goto unlock;
512
513 /* Add VLANs for the new proto to the device filter. */
514 list_for_each_entry(p, &br->port_list, list) {
515 pv = rtnl_dereference(p->vlan_info);
516 if (!pv)
517 continue;
518
519 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
520 err = vlan_vid_add(p->dev, proto, vid);
521 if (err)
522 goto err_filt;
523 }
524 }
525
526 oldproto = br->vlan_proto;
527 br->vlan_proto = proto;
528
529 recalculate_group_addr(br);
530 br_recalculate_fwd_mask(br);
531
532 /* Delete VLANs for the old proto from the device filter. */
533 list_for_each_entry(p, &br->port_list, list) {
534 pv = rtnl_dereference(p->vlan_info);
535 if (!pv)
536 continue;
537
538 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
539 vlan_vid_del(p->dev, oldproto, vid);
540 }
541
542unlock:
543 rtnl_unlock();
544 return err;
545
546err_filt:
547 errvid = vid;
548 for_each_set_bit(vid, pv->vlan_bitmap, errvid)
549 vlan_vid_del(p->dev, proto, vid);
550
551 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
552 pv = rtnl_dereference(p->vlan_info);
553 if (!pv)
554 continue;
555
556 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
557 vlan_vid_del(p->dev, proto, vid);
558 }
559
560 goto unlock;
561}
562
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400563static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid)
564{
565 return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap);
566}
567
568static void br_vlan_disable_default_pvid(struct net_bridge *br)
569{
570 struct net_bridge_port *p;
571 u16 pvid = br->default_pvid;
572
573 /* Disable default_pvid on all ports where it is still
574 * configured.
575 */
576 if (vlan_default_pvid(br_get_vlan_info(br), pvid))
577 br_vlan_delete(br, pvid);
578
579 list_for_each_entry(p, &br->port_list, list) {
580 if (vlan_default_pvid(nbp_get_vlan_info(p), pvid))
581 nbp_vlan_delete(p, pvid);
582 }
583
584 br->default_pvid = 0;
585}
586
587static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
588{
589 struct net_bridge_port *p;
590 u16 old_pvid;
591 int err = 0;
592 unsigned long *changed;
593
594 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
595 GFP_KERNEL);
596 if (!changed)
597 return -ENOMEM;
598
599 old_pvid = br->default_pvid;
600
601 /* Update default_pvid config only if we do not conflict with
602 * user configuration.
603 */
604 if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) &&
605 !br_vlan_find(br, pvid)) {
606 err = br_vlan_add(br, pvid,
607 BRIDGE_VLAN_INFO_PVID |
608 BRIDGE_VLAN_INFO_UNTAGGED);
609 if (err)
610 goto out;
611 br_vlan_delete(br, old_pvid);
612 set_bit(0, changed);
613 }
614
615 list_for_each_entry(p, &br->port_list, list) {
616 /* Update default_pvid config only if we do not conflict with
617 * user configuration.
618 */
619 if ((old_pvid &&
620 !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) ||
621 nbp_vlan_find(p, pvid))
622 continue;
623
624 err = nbp_vlan_add(p, pvid,
625 BRIDGE_VLAN_INFO_PVID |
626 BRIDGE_VLAN_INFO_UNTAGGED);
627 if (err)
628 goto err_port;
629 nbp_vlan_delete(p, old_pvid);
630 set_bit(p->port_no, changed);
631 }
632
633 br->default_pvid = pvid;
634
635out:
636 kfree(changed);
637 return err;
638
639err_port:
640 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
641 if (!test_bit(p->port_no, changed))
642 continue;
643
644 if (old_pvid)
645 nbp_vlan_add(p, old_pvid,
646 BRIDGE_VLAN_INFO_PVID |
647 BRIDGE_VLAN_INFO_UNTAGGED);
648 nbp_vlan_delete(p, pvid);
649 }
650
651 if (test_bit(0, changed)) {
652 if (old_pvid)
653 br_vlan_add(br, old_pvid,
654 BRIDGE_VLAN_INFO_PVID |
655 BRIDGE_VLAN_INFO_UNTAGGED);
656 br_vlan_delete(br, pvid);
657 }
658 goto out;
659}
660
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400661int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
662{
663 u16 pvid = val;
664 int err = 0;
665
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400666 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400667 return -EINVAL;
668
669 if (!rtnl_trylock())
670 return restart_syscall();
671
672 if (pvid == br->default_pvid)
673 goto unlock;
674
675 /* Only allow default pvid change when filtering is disabled */
676 if (br->vlan_enabled) {
677 pr_info_once("Please disable vlan filtering to change default_pvid\n");
678 err = -EPERM;
679 goto unlock;
680 }
681
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400682 if (!pvid)
683 br_vlan_disable_default_pvid(br);
684 else
685 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400686
687unlock:
688 rtnl_unlock();
689 return err;
690}
691
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400692int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900693{
694 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400695 br->default_pvid = 1;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400696 return br_vlan_add(br, 1,
697 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900698}
699
Toshiaki Makita8adff412013-10-16 17:07:13 +0900700/* Must be protected by RTNL.
701 * Must be called with vid in range from 1 to 4094 inclusive.
702 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000703int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000704{
705 struct net_port_vlans *pv = NULL;
706 int err;
707
708 ASSERT_RTNL();
709
710 pv = rtnl_dereference(port->vlan_info);
711 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000712 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000713
714 /* Create port vlan infomration
715 */
716 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
717 if (!pv) {
718 err = -ENOMEM;
719 goto clean_up;
720 }
721
722 pv->port_idx = port->port_no;
723 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000724 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000725 if (err)
726 goto clean_up;
727
728 rcu_assign_pointer(port->vlan_info, pv);
729 return 0;
730
731clean_up:
732 kfree(pv);
733 return err;
734}
735
Toshiaki Makita8adff412013-10-16 17:07:13 +0900736/* Must be protected by RTNL.
737 * Must be called with vid in range from 1 to 4094 inclusive.
738 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000739int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
740{
741 struct net_port_vlans *pv;
742
743 ASSERT_RTNL();
744
745 pv = rtnl_dereference(port->vlan_info);
746 if (!pv)
747 return -EINVAL;
748
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900749 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700750 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000751
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000752 return __vlan_del(pv, vid);
753}
754
755void nbp_vlan_flush(struct net_bridge_port *port)
756{
757 struct net_port_vlans *pv;
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900758 u16 vid;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000759
760 ASSERT_RTNL();
761
762 pv = rtnl_dereference(port->vlan_info);
763 if (!pv)
764 return;
765
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900766 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900767 vlan_vid_del(port->dev, port->br->vlan_proto, vid);
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900768
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000769 __vlan_flush(pv);
770}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000771
772bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
773{
774 struct net_port_vlans *pv;
775 bool found = false;
776
777 rcu_read_lock();
778 pv = rcu_dereference(port->vlan_info);
779
780 if (!pv)
781 goto out;
782
783 if (test_bit(vid, pv->vlan_bitmap))
784 found = true;
785
786out:
787 rcu_read_unlock();
788 return found;
789}
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400790
791int nbp_vlan_init(struct net_bridge_port *p)
792{
793 return p->br->default_pvid ?
794 nbp_vlan_add(p, p->br->default_pvid,
795 BRIDGE_VLAN_INFO_PVID |
796 BRIDGE_VLAN_INFO_UNTAGGED) :
797 0;
798}