blob: 9ea358fbbf789870a2b85d0fee991b5b502887a6 [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
26static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000027{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000028 struct net_bridge_port *p = NULL;
29 struct net_bridge *br;
30 struct net_device *dev;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000031 int err;
32
Vlad Yasevich552406c2013-02-13 12:00:15 +000033 if (test_bit(vid, v->vlan_bitmap)) {
34 if (flags & BRIDGE_VLAN_INFO_PVID)
35 __vlan_add_pvid(v, vid);
36 return 0;
37 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000038
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000039 if (vid) {
40 if (v->port_idx) {
41 p = v->parent.port;
42 br = p->br;
43 dev = p->dev;
44 } else {
45 br = v->parent.br;
46 dev = br->dev;
47 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +000048
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000049 if (p && (dev->features & NETIF_F_HW_VLAN_FILTER)) {
50 /* Add VLAN to the device filter if it is supported.
51 * Stricly speaking, this is not necessary now, since
52 * devices are made promiscuous by the bridge, but if
53 * that ever changes this code will allow tagged
54 * traffic to enter the bridge.
55 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +000056 err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
57 if (err)
58 return err;
59 }
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000060
61 err = br_fdb_insert(br, p, dev->dev_addr, vid);
62 if (err) {
63 br_err(br, "failed insert local address into bridge "
64 "forwarding table\n");
65 goto out_filt;
66 }
67
Vlad Yasevich243a2e62013-02-13 12:00:09 +000068 }
69
70 set_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +000071 v->num_vlans++;
Vlad Yasevich552406c2013-02-13 12:00:15 +000072 if (flags & BRIDGE_VLAN_INFO_PVID)
73 __vlan_add_pvid(v, vid);
74
Vlad Yasevich243a2e62013-02-13 12:00:09 +000075 return 0;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000076
77out_filt:
78 if (p && (dev->features & NETIF_F_HW_VLAN_FILTER))
79 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
80 return err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000081}
82
83static int __vlan_del(struct net_port_vlans *v, u16 vid)
84{
85 if (!test_bit(vid, v->vlan_bitmap))
86 return -EINVAL;
87
Vlad Yasevich552406c2013-02-13 12:00:15 +000088 __vlan_delete_pvid(v, vid);
89
Vlad Yasevich243a2e62013-02-13 12:00:09 +000090 if (v->port_idx && vid) {
91 struct net_device *dev = v->parent.port->dev;
92
93 if (dev->features & NETIF_F_HW_VLAN_FILTER)
94 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
95 }
96
97 clear_bit(vid, v->vlan_bitmap);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +000098 v->num_vlans--;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000099 if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
100 if (v->port_idx)
101 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
102 else
103 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
104 kfree_rcu(v, rcu);
105 }
106 return 0;
107}
108
109static void __vlan_flush(struct net_port_vlans *v)
110{
Vlad Yasevich552406c2013-02-13 12:00:15 +0000111 smp_wmb();
112 v->pvid = 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000113 bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
114 if (v->port_idx)
115 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
116 else
117 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
118 kfree_rcu(v, rcu);
119}
120
Vlad Yasevich78851982013-02-13 12:00:14 +0000121/* Strip the tag from the packet. Will return skb with tci set 0. */
122static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
123{
124 if (skb->protocol != htons(ETH_P_8021Q)) {
125 skb->vlan_tci = 0;
126 return skb;
127 }
128
129 skb->vlan_tci = 0;
130 skb = vlan_untag(skb);
131 if (skb)
132 skb->vlan_tci = 0;
133
134 return skb;
135}
136
137struct sk_buff *br_handle_vlan(struct net_bridge *br,
138 const struct net_port_vlans *pv,
139 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000140{
141 u16 vid;
142
Vlad Yasevich78851982013-02-13 12:00:14 +0000143 if (!br->vlan_enabled)
144 goto out;
145
146 /* At this point, we know that the frame was filtered and contains
147 * a valid vlan id. If the vlan id matches the pvid of current port
148 * send untagged; otherwise, send taged.
149 */
150 br_vlan_get_tag(skb, &vid);
151 if (vid == br_get_pvid(pv))
152 skb = br_vlan_untag(skb);
153 else {
154 /* Egress policy says "send tagged". If output device
155 * is the bridge, we need to add the VLAN header
156 * ourselves since we'll be going through the RX path.
157 * Sending to ports puts the frame on the TX path and
158 * we let dev_hard_start_xmit() add the header.
159 */
160 if (skb->protocol != htons(ETH_P_8021Q) &&
161 pv->port_idx == 0) {
162 /* vlan_put_tag expects skb->data to point to
163 * mac header.
164 */
165 skb_push(skb, ETH_HLEN);
166 skb = __vlan_put_tag(skb, skb->vlan_tci);
167 if (!skb)
168 goto out;
169 /* put skb->data back to where it was */
170 skb_pull(skb, ETH_HLEN);
171 skb->vlan_tci = 0;
172 }
173 }
174
175out:
176 return skb;
177}
178
179/* Called under RCU */
180bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
181 struct sk_buff *skb, u16 *vid)
182{
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000183 /* If VLAN filtering is disabled on the bridge, all packets are
184 * permitted.
185 */
186 if (!br->vlan_enabled)
187 return true;
188
189 /* If there are no vlan in the permitted list, all packets are
190 * rejected.
191 */
192 if (!v)
193 return false;
194
Vlad Yasevich78851982013-02-13 12:00:14 +0000195 if (br_vlan_get_tag(skb, vid)) {
196 u16 pvid = br_get_pvid(v);
197
198 /* Frame did not have a tag. See if pvid is set
199 * on this port. That tells us which vlan untagged
200 * traffic belongs to.
201 */
202 if (pvid == VLAN_N_VID)
203 return false;
204
205 /* PVID is set on this port. Any untagged ingress
206 * frame is considered to belong to this vlan.
207 */
208 __vlan_hwaccel_put_tag(skb, pvid);
209 return true;
210 }
211
212 /* Frame had a valid vlan tag. See if vlan is allowed */
213 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000214 return true;
215
216 return false;
217}
218
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000219/* Called under RCU. */
220bool br_allowed_egress(struct net_bridge *br,
221 const struct net_port_vlans *v,
222 const struct sk_buff *skb)
223{
224 u16 vid;
225
226 if (!br->vlan_enabled)
227 return true;
228
229 if (!v)
230 return false;
231
232 br_vlan_get_tag(skb, &vid);
233 if (test_bit(vid, v->vlan_bitmap))
234 return true;
235
236 return false;
237}
238
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000239/* Must be protected by RTNL */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000240int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000241{
242 struct net_port_vlans *pv = NULL;
243 int err;
244
245 ASSERT_RTNL();
246
247 pv = rtnl_dereference(br->vlan_info);
248 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000249 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000250
251 /* Create port vlan infomration
252 */
253 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
254 if (!pv)
255 return -ENOMEM;
256
257 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000258 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000259 if (err)
260 goto out;
261
262 rcu_assign_pointer(br->vlan_info, pv);
263 return 0;
264out:
265 kfree(pv);
266 return err;
267}
268
269/* Must be protected by RTNL */
270int br_vlan_delete(struct net_bridge *br, u16 vid)
271{
272 struct net_port_vlans *pv;
273
274 ASSERT_RTNL();
275
276 pv = rtnl_dereference(br->vlan_info);
277 if (!pv)
278 return -EINVAL;
279
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000280 if (vid) {
281 /* If the VID !=0 remove fdb for this vid. VID 0 is special
282 * in that it's the default and is always there in the fdb.
283 */
284 spin_lock_bh(&br->hash_lock);
285 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
286 spin_unlock_bh(&br->hash_lock);
287 }
288
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000289 __vlan_del(pv, vid);
290 return 0;
291}
292
293void br_vlan_flush(struct net_bridge *br)
294{
295 struct net_port_vlans *pv;
296
297 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000298 pv = rtnl_dereference(br->vlan_info);
299 if (!pv)
300 return;
301
302 __vlan_flush(pv);
303}
304
305int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
306{
307 if (!rtnl_trylock())
308 return restart_syscall();
309
310 if (br->vlan_enabled == val)
311 goto unlock;
312
313 br->vlan_enabled = val;
314
315unlock:
316 rtnl_unlock();
317 return 0;
318}
319
320/* Must be protected by RTNL */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000321int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000322{
323 struct net_port_vlans *pv = NULL;
324 int err;
325
326 ASSERT_RTNL();
327
328 pv = rtnl_dereference(port->vlan_info);
329 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000330 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000331
332 /* Create port vlan infomration
333 */
334 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
335 if (!pv) {
336 err = -ENOMEM;
337 goto clean_up;
338 }
339
340 pv->port_idx = port->port_no;
341 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000342 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000343 if (err)
344 goto clean_up;
345
346 rcu_assign_pointer(port->vlan_info, pv);
347 return 0;
348
349clean_up:
350 kfree(pv);
351 return err;
352}
353
354/* Must be protected by RTNL */
355int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
356{
357 struct net_port_vlans *pv;
358
359 ASSERT_RTNL();
360
361 pv = rtnl_dereference(port->vlan_info);
362 if (!pv)
363 return -EINVAL;
364
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000365 if (vid) {
366 /* If the VID !=0 remove fdb for this vid. VID 0 is special
367 * in that it's the default and is always there in the fdb.
368 */
369 spin_lock_bh(&port->br->hash_lock);
370 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
371 spin_unlock_bh(&port->br->hash_lock);
372 }
373
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000374 return __vlan_del(pv, vid);
375}
376
377void nbp_vlan_flush(struct net_bridge_port *port)
378{
379 struct net_port_vlans *pv;
380
381 ASSERT_RTNL();
382
383 pv = rtnl_dereference(port->vlan_info);
384 if (!pv)
385 return;
386
387 __vlan_flush(pv);
388}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000389
390bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
391{
392 struct net_port_vlans *pv;
393 bool found = false;
394
395 rcu_read_lock();
396 pv = rcu_dereference(port->vlan_info);
397
398 if (!pv)
399 goto out;
400
401 if (test_bit(vid, pv->vlan_bitmap))
402 found = true;
403
404out:
405 rcu_read_unlock();
406 return found;
407}