blob: 53f0990eab58e08a3da9160a27ee0834bd0f0272 [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{
Patrick McHardy80d5c362013-04-19 02:04:28 +000037 const struct net_device_ops *ops;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000038 struct net_bridge_port *p = NULL;
39 struct net_bridge *br;
40 struct net_device *dev;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000041 int err;
42
Vlad Yasevich552406c2013-02-13 12:00:15 +000043 if (test_bit(vid, v->vlan_bitmap)) {
Vlad Yasevich35e03f32013-02-13 12:00:20 +000044 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +000045 return 0;
46 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000047
Toshiaki Makita8adff412013-10-16 17:07:13 +090048 if (v->port_idx) {
49 p = v->parent.port;
50 br = p->br;
51 dev = p->dev;
52 } else {
53 br = v->parent.br;
54 dev = br->dev;
55 }
56 ops = dev->netdev_ops;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000057
Toshiaki Makita8adff412013-10-16 17:07:13 +090058 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
59 /* Add VLAN to the device filter if it is supported.
60 * Stricly speaking, this is not necessary now, since
61 * devices are made promiscuous by the bridge, but if
62 * that ever changes this code will allow tagged
63 * traffic to enter the bridge.
64 */
65 err = ops->ndo_vlan_rx_add_vid(dev, htons(ETH_P_8021Q),
66 vid);
67 if (err)
68 return err;
69 }
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000070
Toshiaki Makita8adff412013-10-16 17:07:13 +090071 err = br_fdb_insert(br, p, dev->dev_addr, vid);
72 if (err) {
73 br_err(br, "failed insert local address into bridge "
74 "forwarding table\n");
75 goto out_filt;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000076 }
77
78 set_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +000079 v->num_vlans++;
Vlad Yasevich35e03f32013-02-13 12:00:20 +000080 __vlan_add_flags(v, vid, flags);
Vlad Yasevich552406c2013-02-13 12:00:15 +000081
Vlad Yasevich243a2e62013-02-13 12:00:09 +000082 return 0;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000083
84out_filt:
Patrick McHardyf6469682013-04-19 02:04:27 +000085 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
Patrick McHardy80d5c362013-04-19 02:04:28 +000086 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000087 return err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000088}
89
90static int __vlan_del(struct net_port_vlans *v, u16 vid)
91{
92 if (!test_bit(vid, v->vlan_bitmap))
93 return -EINVAL;
94
Vlad Yasevich552406c2013-02-13 12:00:15 +000095 __vlan_delete_pvid(v, vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000096 clear_bit(vid, v->untagged_bitmap);
Vlad Yasevich552406c2013-02-13 12:00:15 +000097
Toshiaki Makita8adff412013-10-16 17:07:13 +090098 if (v->port_idx) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +000099 struct net_device *dev = v->parent.port->dev;
Patrick McHardy80d5c362013-04-19 02:04:28 +0000100 const struct net_device_ops *ops = dev->netdev_ops;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101
Patrick McHardyf6469682013-04-19 02:04:27 +0000102 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
Patrick McHardy80d5c362013-04-19 02:04:28 +0000103 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000104 }
105
106 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000107 v->num_vlans--;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900108 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000109 if (v->port_idx)
110 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
111 else
112 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
113 kfree_rcu(v, rcu);
114 }
115 return 0;
116}
117
118static void __vlan_flush(struct net_port_vlans *v)
119{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000120 smp_wmb();
121 v->pvid = 0;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900122 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000123 if (v->port_idx)
124 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
125 else
126 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
127 kfree_rcu(v, rcu);
128}
129
Vlad Yasevich78851982013-02-13 12:00:14 +0000130/* Strip the tag from the packet. Will return skb with tci set 0. */
131static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
132{
133 if (skb->protocol != htons(ETH_P_8021Q)) {
134 skb->vlan_tci = 0;
135 return skb;
136 }
137
138 skb->vlan_tci = 0;
139 skb = vlan_untag(skb);
140 if (skb)
141 skb->vlan_tci = 0;
142
143 return skb;
144}
145
146struct sk_buff *br_handle_vlan(struct net_bridge *br,
147 const struct net_port_vlans *pv,
148 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000149{
150 u16 vid;
151
Vlad Yasevich78851982013-02-13 12:00:14 +0000152 if (!br->vlan_enabled)
153 goto out;
154
155 /* At this point, we know that the frame was filtered and contains
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000156 * a valid vlan id. If the vlan id is set in the untagged bitmap,
Vlad Yasevich78851982013-02-13 12:00:14 +0000157 * send untagged; otherwise, send taged.
158 */
159 br_vlan_get_tag(skb, &vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +0000160 if (test_bit(vid, pv->untagged_bitmap))
Vlad Yasevich78851982013-02-13 12:00:14 +0000161 skb = br_vlan_untag(skb);
162 else {
163 /* Egress policy says "send tagged". If output device
164 * is the bridge, we need to add the VLAN header
165 * ourselves since we'll be going through the RX path.
166 * Sending to ports puts the frame on the TX path and
167 * we let dev_hard_start_xmit() add the header.
168 */
169 if (skb->protocol != htons(ETH_P_8021Q) &&
170 pv->port_idx == 0) {
171 /* vlan_put_tag expects skb->data to point to
172 * mac header.
173 */
174 skb_push(skb, ETH_HLEN);
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000175 skb = __vlan_put_tag(skb, skb->vlan_proto, skb->vlan_tci);
Vlad Yasevich78851982013-02-13 12:00:14 +0000176 if (!skb)
177 goto out;
178 /* put skb->data back to where it was */
179 skb_pull(skb, ETH_HLEN);
180 skb->vlan_tci = 0;
181 }
182 }
183
184out:
185 return skb;
186}
187
188/* Called under RCU */
189bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
190 struct sk_buff *skb, u16 *vid)
191{
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900192 int err;
193
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000194 /* If VLAN filtering is disabled on the bridge, all packets are
195 * permitted.
196 */
197 if (!br->vlan_enabled)
198 return true;
199
200 /* If there are no vlan in the permitted list, all packets are
201 * rejected.
202 */
203 if (!v)
204 return false;
205
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900206 err = br_vlan_get_tag(skb, vid);
207 if (!*vid) {
Vlad Yasevich78851982013-02-13 12:00:14 +0000208 u16 pvid = br_get_pvid(v);
209
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900210 /* Frame had a tag with VID 0 or did not have a tag.
211 * See if pvid is set on this port. That tells us which
212 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000213 */
214 if (pvid == VLAN_N_VID)
215 return false;
216
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900217 /* PVID is set on this port. Any untagged or priority-tagged
218 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000219 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900220 *vid = pvid;
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900221 if (likely(err))
222 /* Untagged Frame. */
223 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
224 else
225 /* Priority-tagged Frame.
226 * At this point, We know that skb->vlan_tci had
227 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
228 * We update only VID field and preserve PCP field.
229 */
230 skb->vlan_tci |= pvid;
231
Vlad Yasevich78851982013-02-13 12:00:14 +0000232 return true;
233 }
234
235 /* Frame had a valid vlan tag. See if vlan is allowed */
236 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000237 return true;
238
239 return false;
240}
241
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000242/* Called under RCU. */
243bool br_allowed_egress(struct net_bridge *br,
244 const struct net_port_vlans *v,
245 const struct sk_buff *skb)
246{
247 u16 vid;
248
249 if (!br->vlan_enabled)
250 return true;
251
252 if (!v)
253 return false;
254
255 br_vlan_get_tag(skb, &vid);
256 if (test_bit(vid, v->vlan_bitmap))
257 return true;
258
259 return false;
260}
261
Toshiaki Makita8adff412013-10-16 17:07:13 +0900262/* Must be protected by RTNL.
263 * Must be called with vid in range from 1 to 4094 inclusive.
264 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000265int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000266{
267 struct net_port_vlans *pv = NULL;
268 int err;
269
270 ASSERT_RTNL();
271
272 pv = rtnl_dereference(br->vlan_info);
273 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000274 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000275
276 /* Create port vlan infomration
277 */
278 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
279 if (!pv)
280 return -ENOMEM;
281
282 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000283 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000284 if (err)
285 goto out;
286
287 rcu_assign_pointer(br->vlan_info, pv);
288 return 0;
289out:
290 kfree(pv);
291 return err;
292}
293
Toshiaki Makita8adff412013-10-16 17:07:13 +0900294/* Must be protected by RTNL.
295 * Must be called with vid in range from 1 to 4094 inclusive.
296 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000297int br_vlan_delete(struct net_bridge *br, u16 vid)
298{
299 struct net_port_vlans *pv;
300
301 ASSERT_RTNL();
302
303 pv = rtnl_dereference(br->vlan_info);
304 if (!pv)
305 return -EINVAL;
306
Toshiaki Makita8adff412013-10-16 17:07:13 +0900307 spin_lock_bh(&br->hash_lock);
308 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
309 spin_unlock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000310
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000311 __vlan_del(pv, vid);
312 return 0;
313}
314
315void br_vlan_flush(struct net_bridge *br)
316{
317 struct net_port_vlans *pv;
318
319 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000320 pv = rtnl_dereference(br->vlan_info);
321 if (!pv)
322 return;
323
324 __vlan_flush(pv);
325}
326
327int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
328{
329 if (!rtnl_trylock())
330 return restart_syscall();
331
332 if (br->vlan_enabled == val)
333 goto unlock;
334
335 br->vlan_enabled = val;
336
337unlock:
338 rtnl_unlock();
339 return 0;
340}
341
Toshiaki Makita8adff412013-10-16 17:07:13 +0900342/* Must be protected by RTNL.
343 * Must be called with vid in range from 1 to 4094 inclusive.
344 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000345int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000346{
347 struct net_port_vlans *pv = NULL;
348 int err;
349
350 ASSERT_RTNL();
351
352 pv = rtnl_dereference(port->vlan_info);
353 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000354 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000355
356 /* Create port vlan infomration
357 */
358 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
359 if (!pv) {
360 err = -ENOMEM;
361 goto clean_up;
362 }
363
364 pv->port_idx = port->port_no;
365 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000366 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000367 if (err)
368 goto clean_up;
369
370 rcu_assign_pointer(port->vlan_info, pv);
371 return 0;
372
373clean_up:
374 kfree(pv);
375 return err;
376}
377
Toshiaki Makita8adff412013-10-16 17:07:13 +0900378/* Must be protected by RTNL.
379 * Must be called with vid in range from 1 to 4094 inclusive.
380 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000381int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
382{
383 struct net_port_vlans *pv;
384
385 ASSERT_RTNL();
386
387 pv = rtnl_dereference(port->vlan_info);
388 if (!pv)
389 return -EINVAL;
390
Toshiaki Makita8adff412013-10-16 17:07:13 +0900391 spin_lock_bh(&port->br->hash_lock);
392 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
393 spin_unlock_bh(&port->br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000394
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000395 return __vlan_del(pv, vid);
396}
397
398void nbp_vlan_flush(struct net_bridge_port *port)
399{
400 struct net_port_vlans *pv;
401
402 ASSERT_RTNL();
403
404 pv = rtnl_dereference(port->vlan_info);
405 if (!pv)
406 return;
407
408 __vlan_flush(pv);
409}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000410
411bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
412{
413 struct net_port_vlans *pv;
414 bool found = false;
415
416 rcu_read_lock();
417 pv = rcu_dereference(port->vlan_info);
418
419 if (!pv)
420 goto out;
421
422 if (test_bit(vid, pv->vlan_bitmap))
423 found = true;
424
425out:
426 rcu_read_unlock();
427 return found;
428}