blob: 63bd98137a42cbef6d1813dcce48218bdd43d248 [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>
5
6#include "br_private.h"
7
Vlad Yasevich552406c2013-02-13 12:00:15 +00008static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
9{
10 if (v->pvid == vid)
11 return;
12
13 smp_wmb();
14 v->pvid = vid;
15}
16
17static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
18{
19 if (v->pvid != vid)
20 return;
21
22 smp_wmb();
23 v->pvid = 0;
24}
25
Vlad Yasevich35e03f32013-02-13 12:00:20 +000026static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
27{
28 if (flags & BRIDGE_VLAN_INFO_PVID)
29 __vlan_add_pvid(v, vid);
30
31 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
32 set_bit(vid, v->untagged_bitmap);
33}
34
Vlad Yasevich552406c2013-02-13 12:00:15 +000035static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000036{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000037 struct net_bridge_port *p = NULL;
38 struct net_bridge *br;
39 struct net_device *dev;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000040 int err;
41
Vlad Yasevich552406c2013-02-13 12:00:15 +000042 if (test_bit(vid, v->vlan_bitmap)) {
Vlad Yasevich35e03f32013-02-13 12:00:20 +000043 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +000044 return 0;
45 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000046
Toshiaki Makita8adff412013-10-16 17:07:13 +090047 if (v->port_idx) {
48 p = v->parent.port;
49 br = p->br;
50 dev = p->dev;
51 } else {
52 br = v->parent.br;
53 dev = br->dev;
54 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000055
Toshiaki Makita19236832013-11-13 17:26:12 +090056 if (p) {
Toshiaki Makita8adff412013-10-16 17:07:13 +090057 /* Add VLAN to the device filter if it is supported.
58 * Stricly speaking, this is not necessary now, since
59 * devices are made promiscuous by the bridge, but if
60 * that ever changes this code will allow tagged
61 * traffic to enter the bridge.
62 */
Toshiaki Makita8580e212014-06-10 20:59:23 +090063 err = vlan_vid_add(dev, br->vlan_proto, vid);
Toshiaki Makita8adff412013-10-16 17:07:13 +090064 if (err)
65 return err;
66 }
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000067
Toshiaki Makita8adff412013-10-16 17:07:13 +090068 err = br_fdb_insert(br, p, dev->dev_addr, vid);
69 if (err) {
70 br_err(br, "failed insert local address into bridge "
71 "forwarding table\n");
72 goto out_filt;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000073 }
74
75 set_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +000076 v->num_vlans++;
Vlad Yasevich35e03f32013-02-13 12:00:20 +000077 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +000078
Vlad Yasevich243a2e62013-02-13 12:00:09 +000079 return 0;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000080
81out_filt:
Toshiaki Makita19236832013-11-13 17:26:12 +090082 if (p)
Toshiaki Makita8580e212014-06-10 20:59:23 +090083 vlan_vid_del(dev, br->vlan_proto, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000084 return err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000085}
86
87static int __vlan_del(struct net_port_vlans *v, u16 vid)
88{
89 if (!test_bit(vid, v->vlan_bitmap))
90 return -EINVAL;
91
Vlad Yasevich552406c2013-02-13 12:00:15 +000092 __vlan_delete_pvid(v, vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000093 clear_bit(vid, v->untagged_bitmap);
Vlad Yasevich552406c2013-02-13 12:00:15 +000094
Toshiaki Makita8580e212014-06-10 20:59:23 +090095 if (v->port_idx) {
96 struct net_bridge_port *p = v->parent.port;
97 vlan_vid_del(p->dev, p->br->vlan_proto, vid);
98 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000099
100 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000101 v->num_vlans--;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900102 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000103 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530104 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000105 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530106 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000107 kfree_rcu(v, rcu);
108 }
109 return 0;
110}
111
112static void __vlan_flush(struct net_port_vlans *v)
113{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000114 smp_wmb();
115 v->pvid = 0;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900116 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000117 if (v->port_idx)
Monam Agarwalcd187212014-03-24 00:41:13 +0530118 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000119 else
Monam Agarwalcd187212014-03-24 00:41:13 +0530120 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000121 kfree_rcu(v, rcu);
122}
123
Vlad Yasevich78851982013-02-13 12:00:14 +0000124struct sk_buff *br_handle_vlan(struct net_bridge *br,
125 const struct net_port_vlans *pv,
126 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000127{
128 u16 vid;
129
Vlad Yasevich78851982013-02-13 12:00:14 +0000130 if (!br->vlan_enabled)
131 goto out;
132
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400133 /* Vlan filter table must be configured at this point. The
134 * only exception is the bridge is set in promisc mode and the
135 * packet is destined for the bridge device. In this case
136 * pass the packet as is.
137 */
138 if (!pv) {
139 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
140 goto out;
141 } else {
142 kfree_skb(skb);
143 return NULL;
144 }
145 }
146
Vlad Yasevich78851982013-02-13 12:00:14 +0000147 /* At this point, we know that the frame was filtered and contains
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000148 * a valid vlan id. If the vlan id is set in the untagged bitmap,
tanxiaojun1a81a2e2013-12-16 21:32:46 +0800149 * send untagged; otherwise, send tagged.
Vlad Yasevich78851982013-02-13 12:00:14 +0000150 */
151 br_vlan_get_tag(skb, &vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000152 if (test_bit(vid, pv->untagged_bitmap))
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900153 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000154
155out:
156 return skb;
157}
158
159/* Called under RCU */
160bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
161 struct sk_buff *skb, u16 *vid)
162{
Toshiaki Makita8580e212014-06-10 20:59:23 +0900163 bool tagged;
164 __be16 proto;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900165
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000166 /* If VLAN filtering is disabled on the bridge, all packets are
167 * permitted.
168 */
169 if (!br->vlan_enabled)
170 return true;
171
172 /* If there are no vlan in the permitted list, all packets are
173 * rejected.
174 */
175 if (!v)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900176 goto drop;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000177
Toshiaki Makita8580e212014-06-10 20:59:23 +0900178 proto = br->vlan_proto;
179
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900180 /* If vlan tx offload is disabled on bridge device and frame was
181 * sent from vlan device on the bridge device, it does not have
182 * HW accelerated vlan tag.
183 */
184 if (unlikely(!vlan_tx_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900185 skb->protocol == proto)) {
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900186 skb = vlan_untag(skb);
187 if (unlikely(!skb))
188 return false;
189 }
190
Toshiaki Makita8580e212014-06-10 20:59:23 +0900191 if (!br_vlan_get_tag(skb, vid)) {
192 /* Tagged frame */
193 if (skb->vlan_proto != proto) {
194 /* Protocol-mismatch, empty out vlan_tci for new tag */
195 skb_push(skb, ETH_HLEN);
196 skb = __vlan_put_tag(skb, skb->vlan_proto,
197 vlan_tx_tag_get(skb));
198 if (unlikely(!skb))
199 return false;
200
201 skb_pull(skb, ETH_HLEN);
202 skb_reset_mac_len(skb);
203 *vid = 0;
204 tagged = false;
205 } else {
206 tagged = true;
207 }
208 } else {
209 /* Untagged frame */
210 tagged = false;
211 }
212
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900213 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000214 u16 pvid = br_get_pvid(v);
215
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900216 /* Frame had a tag with VID 0 or did not have a tag.
217 * See if pvid is set on this port. That tells us which
218 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000219 */
220 if (pvid == VLAN_N_VID)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900221 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000222
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900223 /* PVID is set on this port. Any untagged or priority-tagged
224 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000225 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900226 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900227 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900228 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900229 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900230 else
231 /* Priority-tagged Frame.
232 * At this point, We know that skb->vlan_tci had
233 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
234 * We update only VID field and preserve PCP field.
235 */
236 skb->vlan_tci |= pvid;
237
Vlad Yasevich78851982013-02-13 12:00:14 +0000238 return true;
239 }
240
241 /* Frame had a valid vlan tag. See if vlan is allowed */
242 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000243 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900244drop:
245 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000246 return false;
247}
248
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000249/* Called under RCU. */
250bool br_allowed_egress(struct net_bridge *br,
251 const struct net_port_vlans *v,
252 const struct sk_buff *skb)
253{
254 u16 vid;
255
256 if (!br->vlan_enabled)
257 return true;
258
259 if (!v)
260 return false;
261
262 br_vlan_get_tag(skb, &vid);
263 if (test_bit(vid, v->vlan_bitmap))
264 return true;
265
266 return false;
267}
268
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900269/* Called under RCU */
270bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
271{
272 struct net_bridge *br = p->br;
273 struct net_port_vlans *v;
274
275 if (!br->vlan_enabled)
276 return true;
277
278 v = rcu_dereference(p->vlan_info);
279 if (!v)
280 return false;
281
Toshiaki Makita8580e212014-06-10 20:59:23 +0900282 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
283 *vid = 0;
284
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900285 if (!*vid) {
286 *vid = br_get_pvid(v);
287 if (*vid == VLAN_N_VID)
288 return false;
289
290 return true;
291 }
292
293 if (test_bit(*vid, v->vlan_bitmap))
294 return true;
295
296 return false;
297}
298
Toshiaki Makita8adff412013-10-16 17:07:13 +0900299/* Must be protected by RTNL.
300 * Must be called with vid in range from 1 to 4094 inclusive.
301 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000302int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000303{
304 struct net_port_vlans *pv = NULL;
305 int err;
306
307 ASSERT_RTNL();
308
309 pv = rtnl_dereference(br->vlan_info);
310 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000311 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000312
313 /* Create port vlan infomration
314 */
315 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
316 if (!pv)
317 return -ENOMEM;
318
319 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000320 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000321 if (err)
322 goto out;
323
324 rcu_assign_pointer(br->vlan_info, pv);
325 return 0;
326out:
327 kfree(pv);
328 return err;
329}
330
Toshiaki Makita8adff412013-10-16 17:07:13 +0900331/* Must be protected by RTNL.
332 * Must be called with vid in range from 1 to 4094 inclusive.
333 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000334int br_vlan_delete(struct net_bridge *br, u16 vid)
335{
336 struct net_port_vlans *pv;
337
338 ASSERT_RTNL();
339
340 pv = rtnl_dereference(br->vlan_info);
341 if (!pv)
342 return -EINVAL;
343
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900344 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000345
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000346 __vlan_del(pv, vid);
347 return 0;
348}
349
350void br_vlan_flush(struct net_bridge *br)
351{
352 struct net_port_vlans *pv;
353
354 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000355 pv = rtnl_dereference(br->vlan_info);
356 if (!pv)
357 return;
358
359 __vlan_flush(pv);
360}
361
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900362bool br_vlan_find(struct net_bridge *br, u16 vid)
363{
364 struct net_port_vlans *pv;
365 bool found = false;
366
367 rcu_read_lock();
368 pv = rcu_dereference(br->vlan_info);
369
370 if (!pv)
371 goto out;
372
373 if (test_bit(vid, pv->vlan_bitmap))
374 found = true;
375
376out:
377 rcu_read_unlock();
378 return found;
379}
380
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000381int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
382{
383 if (!rtnl_trylock())
384 return restart_syscall();
385
386 if (br->vlan_enabled == val)
387 goto unlock;
388
389 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400390 br_manage_promisc(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000391
392unlock:
393 rtnl_unlock();
394 return 0;
395}
396
Toshiaki Makita8580e212014-06-10 20:59:23 +0900397void br_vlan_init(struct net_bridge *br)
398{
399 br->vlan_proto = htons(ETH_P_8021Q);
400}
401
Toshiaki Makita8adff412013-10-16 17:07:13 +0900402/* Must be protected by RTNL.
403 * Must be called with vid in range from 1 to 4094 inclusive.
404 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000405int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000406{
407 struct net_port_vlans *pv = NULL;
408 int err;
409
410 ASSERT_RTNL();
411
412 pv = rtnl_dereference(port->vlan_info);
413 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000414 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000415
416 /* Create port vlan infomration
417 */
418 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
419 if (!pv) {
420 err = -ENOMEM;
421 goto clean_up;
422 }
423
424 pv->port_idx = port->port_no;
425 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000426 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000427 if (err)
428 goto clean_up;
429
430 rcu_assign_pointer(port->vlan_info, pv);
431 return 0;
432
433clean_up:
434 kfree(pv);
435 return err;
436}
437
Toshiaki Makita8adff412013-10-16 17:07:13 +0900438/* Must be protected by RTNL.
439 * Must be called with vid in range from 1 to 4094 inclusive.
440 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000441int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
442{
443 struct net_port_vlans *pv;
444
445 ASSERT_RTNL();
446
447 pv = rtnl_dereference(port->vlan_info);
448 if (!pv)
449 return -EINVAL;
450
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900451 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000452
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000453 return __vlan_del(pv, vid);
454}
455
456void nbp_vlan_flush(struct net_bridge_port *port)
457{
458 struct net_port_vlans *pv;
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900459 u16 vid;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000460
461 ASSERT_RTNL();
462
463 pv = rtnl_dereference(port->vlan_info);
464 if (!pv)
465 return;
466
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900467 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900468 vlan_vid_del(port->dev, port->br->vlan_proto, vid);
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900469
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000470 __vlan_flush(pv);
471}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000472
473bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
474{
475 struct net_port_vlans *pv;
476 bool found = false;
477
478 rcu_read_lock();
479 pv = rcu_dereference(port->vlan_info);
480
481 if (!pv)
482 goto out;
483
484 if (test_bit(vid, pv->vlan_bitmap))
485 found = true;
486
487out:
488 rcu_read_unlock();
489 return found;
490}