blob: 233ec1c6e9db4393c378902d92158accb7715ced [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 Makita19236832013-11-13 17:26:12 +090063 err = vlan_vid_add(dev, htons(ETH_P_8021Q), 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)
83 vlan_vid_del(dev, htons(ETH_P_8021Q), 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 Makita19236832013-11-13 17:26:12 +090095 if (v->port_idx)
96 vlan_vid_del(v->parent.port->dev, htons(ETH_P_8021Q), vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +000097
98 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +000099 v->num_vlans--;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900100 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101 if (v->port_idx)
102 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
103 else
104 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
105 kfree_rcu(v, rcu);
106 }
107 return 0;
108}
109
110static void __vlan_flush(struct net_port_vlans *v)
111{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000112 smp_wmb();
113 v->pvid = 0;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900114 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000115 if (v->port_idx)
116 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
117 else
118 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
119 kfree_rcu(v, rcu);
120}
121
Vlad Yasevich78851982013-02-13 12:00:14 +0000122/* Strip the tag from the packet. Will return skb with tci set 0. */
123static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
124{
125 if (skb->protocol != htons(ETH_P_8021Q)) {
126 skb->vlan_tci = 0;
127 return skb;
128 }
129
130 skb->vlan_tci = 0;
131 skb = vlan_untag(skb);
132 if (skb)
133 skb->vlan_tci = 0;
134
135 return skb;
136}
137
138struct sk_buff *br_handle_vlan(struct net_bridge *br,
139 const struct net_port_vlans *pv,
140 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000141{
142 u16 vid;
143
Vlad Yasevich78851982013-02-13 12:00:14 +0000144 if (!br->vlan_enabled)
145 goto out;
146
147 /* 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))
Vlad Yasevich78851982013-02-13 12:00:14 +0000153 skb = br_vlan_untag(skb);
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 Makitab90356ce2013-10-16 17:07:14 +0900163 int err;
164
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000165 /* If VLAN filtering is disabled on the bridge, all packets are
166 * permitted.
167 */
168 if (!br->vlan_enabled)
169 return true;
170
171 /* If there are no vlan in the permitted list, all packets are
172 * rejected.
173 */
174 if (!v)
175 return false;
176
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900177 err = br_vlan_get_tag(skb, vid);
178 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000179 u16 pvid = br_get_pvid(v);
180
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900181 /* Frame had a tag with VID 0 or did not have a tag.
182 * See if pvid is set on this port. That tells us which
183 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000184 */
185 if (pvid == VLAN_N_VID)
186 return false;
187
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900188 /* PVID is set on this port. Any untagged or priority-tagged
189 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000190 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900191 *vid = pvid;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900192 if (likely(err))
193 /* Untagged Frame. */
194 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
195 else
196 /* Priority-tagged Frame.
197 * At this point, We know that skb->vlan_tci had
198 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
199 * We update only VID field and preserve PCP field.
200 */
201 skb->vlan_tci |= pvid;
202
Vlad Yasevich78851982013-02-13 12:00:14 +0000203 return true;
204 }
205
206 /* Frame had a valid vlan tag. See if vlan is allowed */
207 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000208 return true;
209
210 return false;
211}
212
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000213/* Called under RCU. */
214bool br_allowed_egress(struct net_bridge *br,
215 const struct net_port_vlans *v,
216 const struct sk_buff *skb)
217{
218 u16 vid;
219
220 if (!br->vlan_enabled)
221 return true;
222
223 if (!v)
224 return false;
225
226 br_vlan_get_tag(skb, &vid);
227 if (test_bit(vid, v->vlan_bitmap))
228 return true;
229
230 return false;
231}
232
Toshiaki Makita8adff412013-10-16 17:07:13 +0900233/* Must be protected by RTNL.
234 * Must be called with vid in range from 1 to 4094 inclusive.
235 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000236int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000237{
238 struct net_port_vlans *pv = NULL;
239 int err;
240
241 ASSERT_RTNL();
242
243 pv = rtnl_dereference(br->vlan_info);
244 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000245 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000246
247 /* Create port vlan infomration
248 */
249 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
250 if (!pv)
251 return -ENOMEM;
252
253 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000254 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000255 if (err)
256 goto out;
257
258 rcu_assign_pointer(br->vlan_info, pv);
259 return 0;
260out:
261 kfree(pv);
262 return err;
263}
264
Toshiaki Makita8adff412013-10-16 17:07:13 +0900265/* Must be protected by RTNL.
266 * Must be called with vid in range from 1 to 4094 inclusive.
267 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000268int br_vlan_delete(struct net_bridge *br, u16 vid)
269{
270 struct net_port_vlans *pv;
271
272 ASSERT_RTNL();
273
274 pv = rtnl_dereference(br->vlan_info);
275 if (!pv)
276 return -EINVAL;
277
Toshiaki Makita8adff412013-10-16 17:07:13 +0900278 spin_lock_bh(&br->hash_lock);
279 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
280 spin_unlock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000281
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000282 __vlan_del(pv, vid);
283 return 0;
284}
285
286void br_vlan_flush(struct net_bridge *br)
287{
288 struct net_port_vlans *pv;
289
290 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000291 pv = rtnl_dereference(br->vlan_info);
292 if (!pv)
293 return;
294
295 __vlan_flush(pv);
296}
297
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900298bool br_vlan_find(struct net_bridge *br, u16 vid)
299{
300 struct net_port_vlans *pv;
301 bool found = false;
302
303 rcu_read_lock();
304 pv = rcu_dereference(br->vlan_info);
305
306 if (!pv)
307 goto out;
308
309 if (test_bit(vid, pv->vlan_bitmap))
310 found = true;
311
312out:
313 rcu_read_unlock();
314 return found;
315}
316
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000317int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
318{
319 if (!rtnl_trylock())
320 return restart_syscall();
321
322 if (br->vlan_enabled == val)
323 goto unlock;
324
325 br->vlan_enabled = val;
326
327unlock:
328 rtnl_unlock();
329 return 0;
330}
331
Toshiaki Makita8adff412013-10-16 17:07:13 +0900332/* Must be protected by RTNL.
333 * Must be called with vid in range from 1 to 4094 inclusive.
334 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000335int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000336{
337 struct net_port_vlans *pv = NULL;
338 int err;
339
340 ASSERT_RTNL();
341
342 pv = rtnl_dereference(port->vlan_info);
343 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000344 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000345
346 /* Create port vlan infomration
347 */
348 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
349 if (!pv) {
350 err = -ENOMEM;
351 goto clean_up;
352 }
353
354 pv->port_idx = port->port_no;
355 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000356 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000357 if (err)
358 goto clean_up;
359
360 rcu_assign_pointer(port->vlan_info, pv);
361 return 0;
362
363clean_up:
364 kfree(pv);
365 return err;
366}
367
Toshiaki Makita8adff412013-10-16 17:07:13 +0900368/* Must be protected by RTNL.
369 * Must be called with vid in range from 1 to 4094 inclusive.
370 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000371int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
372{
373 struct net_port_vlans *pv;
374
375 ASSERT_RTNL();
376
377 pv = rtnl_dereference(port->vlan_info);
378 if (!pv)
379 return -EINVAL;
380
Toshiaki Makita8adff412013-10-16 17:07:13 +0900381 spin_lock_bh(&port->br->hash_lock);
382 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
383 spin_unlock_bh(&port->br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000384
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000385 return __vlan_del(pv, vid);
386}
387
388void nbp_vlan_flush(struct net_bridge_port *port)
389{
390 struct net_port_vlans *pv;
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900391 u16 vid;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000392
393 ASSERT_RTNL();
394
395 pv = rtnl_dereference(port->vlan_info);
396 if (!pv)
397 return;
398
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900399 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
400 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
401
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000402 __vlan_flush(pv);
403}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000404
405bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
406{
407 struct net_port_vlans *pv;
408 bool found = false;
409
410 rcu_read_lock();
411 pv = rcu_dereference(port->vlan_info);
412
413 if (!pv)
414 goto out;
415
416 if (test_bit(vid, pv->vlan_bitmap))
417 found = true;
418
419out:
420 rcu_read_unlock();
421 return found;
422}