blob: 5a9c44a0c306f2a03a9d7f373515097d2af49344 [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 Makitab90356ce2013-10-16 17:07:14 +0900220 if (likely(err))
221 /* Untagged Frame. */
222 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
223 else
224 /* Priority-tagged Frame.
225 * At this point, We know that skb->vlan_tci had
226 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
227 * We update only VID field and preserve PCP field.
228 */
229 skb->vlan_tci |= pvid;
230
Vlad Yasevich78851982013-02-13 12:00:14 +0000231 return true;
232 }
233
234 /* Frame had a valid vlan tag. See if vlan is allowed */
235 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000236 return true;
237
238 return false;
239}
240
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000241/* Called under RCU. */
242bool br_allowed_egress(struct net_bridge *br,
243 const struct net_port_vlans *v,
244 const struct sk_buff *skb)
245{
246 u16 vid;
247
248 if (!br->vlan_enabled)
249 return true;
250
251 if (!v)
252 return false;
253
254 br_vlan_get_tag(skb, &vid);
255 if (test_bit(vid, v->vlan_bitmap))
256 return true;
257
258 return false;
259}
260
Toshiaki Makita8adff412013-10-16 17:07:13 +0900261/* Must be protected by RTNL.
262 * Must be called with vid in range from 1 to 4094 inclusive.
263 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000264int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000265{
266 struct net_port_vlans *pv = NULL;
267 int err;
268
269 ASSERT_RTNL();
270
271 pv = rtnl_dereference(br->vlan_info);
272 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000273 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000274
275 /* Create port vlan infomration
276 */
277 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
278 if (!pv)
279 return -ENOMEM;
280
281 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000282 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000283 if (err)
284 goto out;
285
286 rcu_assign_pointer(br->vlan_info, pv);
287 return 0;
288out:
289 kfree(pv);
290 return err;
291}
292
Toshiaki Makita8adff412013-10-16 17:07:13 +0900293/* Must be protected by RTNL.
294 * Must be called with vid in range from 1 to 4094 inclusive.
295 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000296int br_vlan_delete(struct net_bridge *br, u16 vid)
297{
298 struct net_port_vlans *pv;
299
300 ASSERT_RTNL();
301
302 pv = rtnl_dereference(br->vlan_info);
303 if (!pv)
304 return -EINVAL;
305
Toshiaki Makita8adff412013-10-16 17:07:13 +0900306 spin_lock_bh(&br->hash_lock);
307 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
308 spin_unlock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000309
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000310 __vlan_del(pv, vid);
311 return 0;
312}
313
314void br_vlan_flush(struct net_bridge *br)
315{
316 struct net_port_vlans *pv;
317
318 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000319 pv = rtnl_dereference(br->vlan_info);
320 if (!pv)
321 return;
322
323 __vlan_flush(pv);
324}
325
326int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
327{
328 if (!rtnl_trylock())
329 return restart_syscall();
330
331 if (br->vlan_enabled == val)
332 goto unlock;
333
334 br->vlan_enabled = val;
335
336unlock:
337 rtnl_unlock();
338 return 0;
339}
340
Toshiaki Makita8adff412013-10-16 17:07:13 +0900341/* Must be protected by RTNL.
342 * Must be called with vid in range from 1 to 4094 inclusive.
343 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000344int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000345{
346 struct net_port_vlans *pv = NULL;
347 int err;
348
349 ASSERT_RTNL();
350
351 pv = rtnl_dereference(port->vlan_info);
352 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000353 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000354
355 /* Create port vlan infomration
356 */
357 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
358 if (!pv) {
359 err = -ENOMEM;
360 goto clean_up;
361 }
362
363 pv->port_idx = port->port_no;
364 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000365 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000366 if (err)
367 goto clean_up;
368
369 rcu_assign_pointer(port->vlan_info, pv);
370 return 0;
371
372clean_up:
373 kfree(pv);
374 return err;
375}
376
Toshiaki Makita8adff412013-10-16 17:07:13 +0900377/* Must be protected by RTNL.
378 * Must be called with vid in range from 1 to 4094 inclusive.
379 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000380int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
381{
382 struct net_port_vlans *pv;
383
384 ASSERT_RTNL();
385
386 pv = rtnl_dereference(port->vlan_info);
387 if (!pv)
388 return -EINVAL;
389
Toshiaki Makita8adff412013-10-16 17:07:13 +0900390 spin_lock_bh(&port->br->hash_lock);
391 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
392 spin_unlock_bh(&port->br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000393
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000394 return __vlan_del(pv, vid);
395}
396
397void nbp_vlan_flush(struct net_bridge_port *port)
398{
399 struct net_port_vlans *pv;
400
401 ASSERT_RTNL();
402
403 pv = rtnl_dereference(port->vlan_info);
404 if (!pv)
405 return;
406
407 __vlan_flush(pv);
408}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000409
410bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
411{
412 struct net_port_vlans *pv;
413 bool found = false;
414
415 rcu_read_lock();
416 pv = rcu_dereference(port->vlan_info);
417
418 if (!pv)
419 goto out;
420
421 if (test_bit(vid, pv->vlan_bitmap))
422 found = true;
423
424out:
425 rcu_read_unlock();
426 return found;
427}