blob: af5ebd18d7059f2d0cc289e226c544f210309dc2 [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,
Vlad Yasevich78851982013-02-13 12:00:14 +0000149 * send untagged; otherwise, send taged.
150 */
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);
154 else {
155 /* Egress policy says "send tagged". If output device
156 * is the bridge, we need to add the VLAN header
157 * ourselves since we'll be going through the RX path.
158 * Sending to ports puts the frame on the TX path and
159 * we let dev_hard_start_xmit() add the header.
160 */
161 if (skb->protocol != htons(ETH_P_8021Q) &&
162 pv->port_idx == 0) {
163 /* vlan_put_tag expects skb->data to point to
164 * mac header.
165 */
166 skb_push(skb, ETH_HLEN);
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000167 skb = __vlan_put_tag(skb, skb->vlan_proto, skb->vlan_tci);
Vlad Yasevich78851982013-02-13 12:00:14 +0000168 if (!skb)
169 goto out;
170 /* put skb->data back to where it was */
171 skb_pull(skb, ETH_HLEN);
172 skb->vlan_tci = 0;
173 }
174 }
175
176out:
177 return skb;
178}
179
180/* Called under RCU */
181bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
182 struct sk_buff *skb, u16 *vid)
183{
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900184 int err;
185
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000186 /* If VLAN filtering is disabled on the bridge, all packets are
187 * permitted.
188 */
189 if (!br->vlan_enabled)
190 return true;
191
192 /* If there are no vlan in the permitted list, all packets are
193 * rejected.
194 */
195 if (!v)
196 return false;
197
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900198 err = br_vlan_get_tag(skb, vid);
199 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000200 u16 pvid = br_get_pvid(v);
201
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900202 /* Frame had a tag with VID 0 or did not have a tag.
203 * See if pvid is set on this port. That tells us which
204 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000205 */
206 if (pvid == VLAN_N_VID)
207 return false;
208
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900209 /* PVID is set on this port. Any untagged or priority-tagged
210 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000211 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900212 *vid = pvid;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900213 if (likely(err))
214 /* Untagged Frame. */
215 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
216 else
217 /* Priority-tagged Frame.
218 * At this point, We know that skb->vlan_tci had
219 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
220 * We update only VID field and preserve PCP field.
221 */
222 skb->vlan_tci |= pvid;
223
Vlad Yasevich78851982013-02-13 12:00:14 +0000224 return true;
225 }
226
227 /* Frame had a valid vlan tag. See if vlan is allowed */
228 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000229 return true;
230
231 return false;
232}
233
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000234/* Called under RCU. */
235bool br_allowed_egress(struct net_bridge *br,
236 const struct net_port_vlans *v,
237 const struct sk_buff *skb)
238{
239 u16 vid;
240
241 if (!br->vlan_enabled)
242 return true;
243
244 if (!v)
245 return false;
246
247 br_vlan_get_tag(skb, &vid);
248 if (test_bit(vid, v->vlan_bitmap))
249 return true;
250
251 return false;
252}
253
Toshiaki Makita8adff412013-10-16 17:07:13 +0900254/* Must be protected by RTNL.
255 * Must be called with vid in range from 1 to 4094 inclusive.
256 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000257int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000258{
259 struct net_port_vlans *pv = NULL;
260 int err;
261
262 ASSERT_RTNL();
263
264 pv = rtnl_dereference(br->vlan_info);
265 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000266 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000267
268 /* Create port vlan infomration
269 */
270 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
271 if (!pv)
272 return -ENOMEM;
273
274 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000275 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000276 if (err)
277 goto out;
278
279 rcu_assign_pointer(br->vlan_info, pv);
280 return 0;
281out:
282 kfree(pv);
283 return err;
284}
285
Toshiaki Makita8adff412013-10-16 17:07:13 +0900286/* Must be protected by RTNL.
287 * Must be called with vid in range from 1 to 4094 inclusive.
288 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000289int br_vlan_delete(struct net_bridge *br, u16 vid)
290{
291 struct net_port_vlans *pv;
292
293 ASSERT_RTNL();
294
295 pv = rtnl_dereference(br->vlan_info);
296 if (!pv)
297 return -EINVAL;
298
Toshiaki Makita8adff412013-10-16 17:07:13 +0900299 spin_lock_bh(&br->hash_lock);
300 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
301 spin_unlock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000302
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000303 __vlan_del(pv, vid);
304 return 0;
305}
306
307void br_vlan_flush(struct net_bridge *br)
308{
309 struct net_port_vlans *pv;
310
311 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000312 pv = rtnl_dereference(br->vlan_info);
313 if (!pv)
314 return;
315
316 __vlan_flush(pv);
317}
318
319int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
320{
321 if (!rtnl_trylock())
322 return restart_syscall();
323
324 if (br->vlan_enabled == val)
325 goto unlock;
326
327 br->vlan_enabled = val;
328
329unlock:
330 rtnl_unlock();
331 return 0;
332}
333
Toshiaki Makita8adff412013-10-16 17:07:13 +0900334/* Must be protected by RTNL.
335 * Must be called with vid in range from 1 to 4094 inclusive.
336 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000337int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000338{
339 struct net_port_vlans *pv = NULL;
340 int err;
341
342 ASSERT_RTNL();
343
344 pv = rtnl_dereference(port->vlan_info);
345 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000346 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000347
348 /* Create port vlan infomration
349 */
350 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
351 if (!pv) {
352 err = -ENOMEM;
353 goto clean_up;
354 }
355
356 pv->port_idx = port->port_no;
357 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000358 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000359 if (err)
360 goto clean_up;
361
362 rcu_assign_pointer(port->vlan_info, pv);
363 return 0;
364
365clean_up:
366 kfree(pv);
367 return err;
368}
369
Toshiaki Makita8adff412013-10-16 17:07:13 +0900370/* Must be protected by RTNL.
371 * Must be called with vid in range from 1 to 4094 inclusive.
372 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000373int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
374{
375 struct net_port_vlans *pv;
376
377 ASSERT_RTNL();
378
379 pv = rtnl_dereference(port->vlan_info);
380 if (!pv)
381 return -EINVAL;
382
Toshiaki Makita8adff412013-10-16 17:07:13 +0900383 spin_lock_bh(&port->br->hash_lock);
384 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
385 spin_unlock_bh(&port->br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000386
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000387 return __vlan_del(pv, vid);
388}
389
390void nbp_vlan_flush(struct net_bridge_port *port)
391{
392 struct net_port_vlans *pv;
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900393 u16 vid;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000394
395 ASSERT_RTNL();
396
397 pv = rtnl_dereference(port->vlan_info);
398 if (!pv)
399 return;
400
Toshiaki Makitadbbaf942013-11-13 17:26:13 +0900401 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
402 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
403
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000404 __vlan_flush(pv);
405}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000406
407bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
408{
409 struct net_port_vlans *pv;
410 bool found = false;
411
412 rcu_read_lock();
413 pv = rcu_dereference(port->vlan_info);
414
415 if (!pv)
416 goto out;
417
418 if (test_bit(vid, pv->vlan_bitmap))
419 found = true;
420
421out:
422 rcu_read_unlock();
423 return found;
424}