blob: 5f5a02b49a99617918c8f76ecf7858920152ba53 [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
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400120static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
121 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700122{
123 const struct net_device_ops *ops = dev->netdev_ops;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400124 int err = 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700125
126 /* If driver uses VLAN ndo ops, use 8021q to delete vid
127 * on device, otherwise try switchdev ops to delete vid.
128 */
129
130 if (ops->ndo_vlan_rx_kill_vid) {
131 vlan_vid_del(dev, br->vlan_proto, vid);
132 } else {
133 struct switchdev_obj vlan_obj = {
134 .id = SWITCHDEV_OBJ_PORT_VLAN,
135 .u.vlan = {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700136 .vid_begin = vid,
Scott Feldman7f109532015-06-12 17:39:50 -0700137 .vid_end = vid,
138 },
139 };
140
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400141 err = switchdev_port_obj_del(dev, &vlan_obj);
142 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
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000149static int __vlan_del(struct net_port_vlans *v, u16 vid)
150{
151 if (!test_bit(vid, v->vlan_bitmap))
152 return -EINVAL;
153
Vlad Yasevich552406c2013-02-13 12:00:15 +0000154 __vlan_delete_pvid(v, vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000155 clear_bit(vid, v->untagged_bitmap);
Vlad Yasevich552406c2013-02-13 12:00:15 +0000156
Toshiaki Makita8580e212014-06-10 20:59:23 +0900157 if (v->port_idx) {
158 struct net_bridge_port *p = v->parent.port;
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400159 int err;
160
161 err = __vlan_vid_del(p->dev, p->br, vid);
162 if (err)
163 return err;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900164 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000165
166 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000167 v->num_vlans--;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900168 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000169 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530170 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000171 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530172 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000173 kfree_rcu(v, rcu);
174 }
175 return 0;
176}
177
178static void __vlan_flush(struct net_port_vlans *v)
179{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000180 smp_wmb();
181 v->pvid = 0;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900182 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000183 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530184 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000185 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530186 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000187 kfree_rcu(v, rcu);
188}
189
Vlad Yasevich78851982013-02-13 12:00:14 +0000190struct sk_buff *br_handle_vlan(struct net_bridge *br,
191 const struct net_port_vlans *pv,
192 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000193{
194 u16 vid;
195
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400196 /* If this packet was not filtered at input, let it pass */
197 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000198 goto out;
199
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400200 /* Vlan filter table must be configured at this point. The
201 * only exception is the bridge is set in promisc mode and the
202 * packet is destined for the bridge device. In this case
203 * pass the packet as is.
204 */
205 if (!pv) {
206 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
207 goto out;
208 } else {
209 kfree_skb(skb);
210 return NULL;
211 }
212 }
213
Vlad Yasevich78851982013-02-13 12:00:14 +0000214 /* At this point, we know that the frame was filtered and contains
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000215 * a valid vlan id. If the vlan id is set in the untagged bitmap,
tanxiaojun1a81a2e2013-12-16 21:32:46 +0800216 * send untagged; otherwise, send tagged.
Vlad Yasevich78851982013-02-13 12:00:14 +0000217 */
218 br_vlan_get_tag(skb, &vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000219 if (test_bit(vid, pv->untagged_bitmap))
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900220 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000221
222out:
223 return skb;
224}
225
226/* Called under RCU */
227bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
228 struct sk_buff *skb, u16 *vid)
229{
Toshiaki Makita8580e212014-06-10 20:59:23 +0900230 bool tagged;
231 __be16 proto;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900232
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000233 /* If VLAN filtering is disabled on the bridge, all packets are
234 * permitted.
235 */
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400236 if (!br->vlan_enabled) {
237 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000238 return true;
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400239 }
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000240
241 /* If there are no vlan in the permitted list, all packets are
242 * rejected.
243 */
244 if (!v)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900245 goto drop;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000246
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400247 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900248 proto = br->vlan_proto;
249
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900250 /* If vlan tx offload is disabled on bridge device and frame was
251 * sent from vlan device on the bridge device, it does not have
252 * HW accelerated vlan tag.
253 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100254 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900255 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400256 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900257 if (unlikely(!skb))
258 return false;
259 }
260
Toshiaki Makita8580e212014-06-10 20:59:23 +0900261 if (!br_vlan_get_tag(skb, vid)) {
262 /* Tagged frame */
263 if (skb->vlan_proto != proto) {
264 /* Protocol-mismatch, empty out vlan_tci for new tag */
265 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100266 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100267 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900268 if (unlikely(!skb))
269 return false;
270
271 skb_pull(skb, ETH_HLEN);
272 skb_reset_mac_len(skb);
273 *vid = 0;
274 tagged = false;
275 } else {
276 tagged = true;
277 }
278 } else {
279 /* Untagged frame */
280 tagged = false;
281 }
282
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900283 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000284 u16 pvid = br_get_pvid(v);
285
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900286 /* Frame had a tag with VID 0 or did not have a tag.
287 * See if pvid is set on this port. That tells us which
288 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000289 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400290 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900291 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000292
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900293 /* PVID is set on this port. Any untagged or priority-tagged
294 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000295 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900296 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900297 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900298 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900299 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900300 else
301 /* Priority-tagged Frame.
302 * At this point, We know that skb->vlan_tci had
303 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
304 * We update only VID field and preserve PCP field.
305 */
306 skb->vlan_tci |= pvid;
307
Vlad Yasevich78851982013-02-13 12:00:14 +0000308 return true;
309 }
310
311 /* Frame had a valid vlan tag. See if vlan is allowed */
312 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000313 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900314drop:
315 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000316 return false;
317}
318
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000319/* Called under RCU. */
320bool br_allowed_egress(struct net_bridge *br,
321 const struct net_port_vlans *v,
322 const struct sk_buff *skb)
323{
324 u16 vid;
325
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400326 /* If this packet was not filtered at input, let it pass */
327 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000328 return true;
329
330 if (!v)
331 return false;
332
333 br_vlan_get_tag(skb, &vid);
334 if (test_bit(vid, v->vlan_bitmap))
335 return true;
336
337 return false;
338}
339
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900340/* Called under RCU */
341bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
342{
343 struct net_bridge *br = p->br;
344 struct net_port_vlans *v;
345
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400346 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400347 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900348 return true;
349
350 v = rcu_dereference(p->vlan_info);
351 if (!v)
352 return false;
353
Toshiaki Makita8580e212014-06-10 20:59:23 +0900354 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
355 *vid = 0;
356
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900357 if (!*vid) {
358 *vid = br_get_pvid(v);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400359 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900360 return false;
361
362 return true;
363 }
364
365 if (test_bit(*vid, v->vlan_bitmap))
366 return true;
367
368 return false;
369}
370
Toshiaki Makita8adff412013-10-16 17:07:13 +0900371/* Must be protected by RTNL.
372 * Must be called with vid in range from 1 to 4094 inclusive.
373 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000374int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000375{
376 struct net_port_vlans *pv = NULL;
377 int err;
378
379 ASSERT_RTNL();
380
381 pv = rtnl_dereference(br->vlan_info);
382 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000383 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000384
385 /* Create port vlan infomration
386 */
387 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
388 if (!pv)
389 return -ENOMEM;
390
391 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000392 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000393 if (err)
394 goto out;
395
396 rcu_assign_pointer(br->vlan_info, pv);
397 return 0;
398out:
399 kfree(pv);
400 return err;
401}
402
Toshiaki Makita8adff412013-10-16 17:07:13 +0900403/* Must be protected by RTNL.
404 * Must be called with vid in range from 1 to 4094 inclusive.
405 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000406int br_vlan_delete(struct net_bridge *br, u16 vid)
407{
408 struct net_port_vlans *pv;
409
410 ASSERT_RTNL();
411
412 pv = rtnl_dereference(br->vlan_info);
413 if (!pv)
414 return -EINVAL;
415
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900416 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000417
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000418 __vlan_del(pv, vid);
419 return 0;
420}
421
422void br_vlan_flush(struct net_bridge *br)
423{
424 struct net_port_vlans *pv;
425
426 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000427 pv = rtnl_dereference(br->vlan_info);
428 if (!pv)
429 return;
430
431 __vlan_flush(pv);
432}
433
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900434bool br_vlan_find(struct net_bridge *br, u16 vid)
435{
436 struct net_port_vlans *pv;
437 bool found = false;
438
439 rcu_read_lock();
440 pv = rcu_dereference(br->vlan_info);
441
442 if (!pv)
443 goto out;
444
445 if (test_bit(vid, pv->vlan_bitmap))
446 found = true;
447
448out:
449 rcu_read_unlock();
450 return found;
451}
452
Toshiaki Makita204177f2014-06-10 20:59:25 +0900453/* Must be protected by RTNL. */
454static void recalculate_group_addr(struct net_bridge *br)
455{
456 if (br->group_addr_set)
457 return;
458
459 spin_lock_bh(&br->lock);
460 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
461 /* Bridge Group Address */
462 br->group_addr[5] = 0x00;
463 } else { /* vlan_enabled && ETH_P_8021AD */
464 /* Provider Bridge Group Address */
465 br->group_addr[5] = 0x08;
466 }
467 spin_unlock_bh(&br->lock);
468}
469
470/* Must be protected by RTNL. */
471void br_recalculate_fwd_mask(struct net_bridge *br)
472{
473 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
474 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
475 else /* vlan_enabled && ETH_P_8021AD */
476 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
477 ~(1u << br->group_addr[5]);
478}
479
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300480int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000481{
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000482 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300483 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000484
485 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400486 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900487 recalculate_group_addr(br);
488 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000489
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300490 return 0;
491}
492
493int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
494{
495 if (!rtnl_trylock())
496 return restart_syscall();
497
498 __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000499 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300500
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000501 return 0;
502}
503
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900504int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900505{
506 int err = 0;
507 struct net_bridge_port *p;
508 struct net_port_vlans *pv;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900509 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900510 u16 vid, errvid;
511
Toshiaki Makita204177f2014-06-10 20:59:25 +0900512 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900513 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900514
515 /* Add VLANs for the new proto to the device filter. */
516 list_for_each_entry(p, &br->port_list, list) {
517 pv = rtnl_dereference(p->vlan_info);
518 if (!pv)
519 continue;
520
521 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
522 err = vlan_vid_add(p->dev, proto, vid);
523 if (err)
524 goto err_filt;
525 }
526 }
527
528 oldproto = br->vlan_proto;
529 br->vlan_proto = proto;
530
531 recalculate_group_addr(br);
532 br_recalculate_fwd_mask(br);
533
534 /* Delete VLANs for the old proto from the device filter. */
535 list_for_each_entry(p, &br->port_list, list) {
536 pv = rtnl_dereference(p->vlan_info);
537 if (!pv)
538 continue;
539
540 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
541 vlan_vid_del(p->dev, oldproto, vid);
542 }
543
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900544 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900545
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
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900560 return err;
561}
562
563int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
564{
565 int err;
566
567 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
568 return -EPROTONOSUPPORT;
569
570 if (!rtnl_trylock())
571 return restart_syscall();
572
573 err = __br_vlan_set_proto(br, htons(val));
574 rtnl_unlock();
575
576 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900577}
578
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400579static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid)
580{
581 return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap);
582}
583
584static void br_vlan_disable_default_pvid(struct net_bridge *br)
585{
586 struct net_bridge_port *p;
587 u16 pvid = br->default_pvid;
588
589 /* Disable default_pvid on all ports where it is still
590 * configured.
591 */
592 if (vlan_default_pvid(br_get_vlan_info(br), pvid))
593 br_vlan_delete(br, pvid);
594
595 list_for_each_entry(p, &br->port_list, list) {
596 if (vlan_default_pvid(nbp_get_vlan_info(p), pvid))
597 nbp_vlan_delete(p, pvid);
598 }
599
600 br->default_pvid = 0;
601}
602
603static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
604{
605 struct net_bridge_port *p;
606 u16 old_pvid;
607 int err = 0;
608 unsigned long *changed;
609
610 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
611 GFP_KERNEL);
612 if (!changed)
613 return -ENOMEM;
614
615 old_pvid = br->default_pvid;
616
617 /* Update default_pvid config only if we do not conflict with
618 * user configuration.
619 */
620 if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) &&
621 !br_vlan_find(br, pvid)) {
622 err = br_vlan_add(br, pvid,
623 BRIDGE_VLAN_INFO_PVID |
624 BRIDGE_VLAN_INFO_UNTAGGED);
625 if (err)
626 goto out;
627 br_vlan_delete(br, old_pvid);
628 set_bit(0, changed);
629 }
630
631 list_for_each_entry(p, &br->port_list, list) {
632 /* Update default_pvid config only if we do not conflict with
633 * user configuration.
634 */
635 if ((old_pvid &&
636 !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) ||
637 nbp_vlan_find(p, pvid))
638 continue;
639
640 err = nbp_vlan_add(p, pvid,
641 BRIDGE_VLAN_INFO_PVID |
642 BRIDGE_VLAN_INFO_UNTAGGED);
643 if (err)
644 goto err_port;
645 nbp_vlan_delete(p, old_pvid);
646 set_bit(p->port_no, changed);
647 }
648
649 br->default_pvid = pvid;
650
651out:
652 kfree(changed);
653 return err;
654
655err_port:
656 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
657 if (!test_bit(p->port_no, changed))
658 continue;
659
660 if (old_pvid)
661 nbp_vlan_add(p, old_pvid,
662 BRIDGE_VLAN_INFO_PVID |
663 BRIDGE_VLAN_INFO_UNTAGGED);
664 nbp_vlan_delete(p, pvid);
665 }
666
667 if (test_bit(0, changed)) {
668 if (old_pvid)
669 br_vlan_add(br, old_pvid,
670 BRIDGE_VLAN_INFO_PVID |
671 BRIDGE_VLAN_INFO_UNTAGGED);
672 br_vlan_delete(br, pvid);
673 }
674 goto out;
675}
676
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400677int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
678{
679 u16 pvid = val;
680 int err = 0;
681
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400682 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400683 return -EINVAL;
684
685 if (!rtnl_trylock())
686 return restart_syscall();
687
688 if (pvid == br->default_pvid)
689 goto unlock;
690
691 /* Only allow default pvid change when filtering is disabled */
692 if (br->vlan_enabled) {
693 pr_info_once("Please disable vlan filtering to change default_pvid\n");
694 err = -EPERM;
695 goto unlock;
696 }
697
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400698 if (!pvid)
699 br_vlan_disable_default_pvid(br);
700 else
701 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400702
703unlock:
704 rtnl_unlock();
705 return err;
706}
707
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400708int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900709{
710 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400711 br->default_pvid = 1;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400712 return br_vlan_add(br, 1,
713 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900714}
715
Toshiaki Makita8adff412013-10-16 17:07:13 +0900716/* Must be protected by RTNL.
717 * Must be called with vid in range from 1 to 4094 inclusive.
718 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000719int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000720{
721 struct net_port_vlans *pv = NULL;
722 int err;
723
724 ASSERT_RTNL();
725
726 pv = rtnl_dereference(port->vlan_info);
727 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000728 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000729
730 /* Create port vlan infomration
731 */
732 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
733 if (!pv) {
734 err = -ENOMEM;
735 goto clean_up;
736 }
737
738 pv->port_idx = port->port_no;
739 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000740 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000741 if (err)
742 goto clean_up;
743
744 rcu_assign_pointer(port->vlan_info, pv);
745 return 0;
746
747clean_up:
748 kfree(pv);
749 return err;
750}
751
Toshiaki Makita8adff412013-10-16 17:07:13 +0900752/* Must be protected by RTNL.
753 * Must be called with vid in range from 1 to 4094 inclusive.
754 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000755int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
756{
757 struct net_port_vlans *pv;
758
759 ASSERT_RTNL();
760
761 pv = rtnl_dereference(port->vlan_info);
762 if (!pv)
763 return -EINVAL;
764
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900765 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700766 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000767
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000768 return __vlan_del(pv, vid);
769}
770
771void nbp_vlan_flush(struct net_bridge_port *port)
772{
773 struct net_port_vlans *pv;
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900774 u16 vid;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000775
776 ASSERT_RTNL();
777
778 pv = rtnl_dereference(port->vlan_info);
779 if (!pv)
780 return;
781
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900782 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900783 vlan_vid_del(port->dev, port->br->vlan_proto, vid);
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900784
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000785 __vlan_flush(pv);
786}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000787
788bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
789{
790 struct net_port_vlans *pv;
791 bool found = false;
792
793 rcu_read_lock();
794 pv = rcu_dereference(port->vlan_info);
795
796 if (!pv)
797 goto out;
798
799 if (test_bit(vid, pv->vlan_bitmap))
800 found = true;
801
802out:
803 rcu_read_unlock();
804 return found;
805}
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400806
807int nbp_vlan_init(struct net_bridge_port *p)
808{
809 return p->br->default_pvid ?
810 nbp_vlan_add(p, p->br->default_pvid,
811 BRIDGE_VLAN_INFO_PVID |
812 BRIDGE_VLAN_INFO_UNTAGGED) :
813 0;
814}