blob: 21b6d217872b88cc6205b9025a215a587d04655a [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{
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000192 /* If VLAN filtering is disabled on the bridge, all packets are
193 * permitted.
194 */
195 if (!br->vlan_enabled)
196 return true;
197
198 /* If there are no vlan in the permitted list, all packets are
199 * rejected.
200 */
201 if (!v)
202 return false;
203
Vlad Yasevich78851982013-02-13 12:00:14 +0000204 if (br_vlan_get_tag(skb, vid)) {
205 u16 pvid = br_get_pvid(v);
206
207 /* Frame did not have a tag. See if pvid is set
208 * on this port. That tells us which vlan untagged
209 * traffic belongs to.
210 */
211 if (pvid == VLAN_N_VID)
212 return false;
213
214 /* PVID is set on this port. Any untagged ingress
215 * frame is considered to belong to this vlan.
216 */
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000217 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
Vlad Yasevich78851982013-02-13 12:00:14 +0000218 return true;
219 }
220
221 /* Frame had a valid vlan tag. See if vlan is allowed */
222 if (test_bit(*vid, v->vlan_bitmap))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000223 return true;
224
225 return false;
226}
227
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000228/* Called under RCU. */
229bool br_allowed_egress(struct net_bridge *br,
230 const struct net_port_vlans *v,
231 const struct sk_buff *skb)
232{
233 u16 vid;
234
235 if (!br->vlan_enabled)
236 return true;
237
238 if (!v)
239 return false;
240
241 br_vlan_get_tag(skb, &vid);
242 if (test_bit(vid, v->vlan_bitmap))
243 return true;
244
245 return false;
246}
247
Toshiaki Makita8adff412013-10-16 17:07:13 +0900248/* Must be protected by RTNL.
249 * Must be called with vid in range from 1 to 4094 inclusive.
250 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000251int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000252{
253 struct net_port_vlans *pv = NULL;
254 int err;
255
256 ASSERT_RTNL();
257
258 pv = rtnl_dereference(br->vlan_info);
259 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000260 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000261
262 /* Create port vlan infomration
263 */
264 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
265 if (!pv)
266 return -ENOMEM;
267
268 pv->parent.br = br;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000269 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000270 if (err)
271 goto out;
272
273 rcu_assign_pointer(br->vlan_info, pv);
274 return 0;
275out:
276 kfree(pv);
277 return err;
278}
279
Toshiaki Makita8adff412013-10-16 17:07:13 +0900280/* Must be protected by RTNL.
281 * Must be called with vid in range from 1 to 4094 inclusive.
282 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000283int br_vlan_delete(struct net_bridge *br, u16 vid)
284{
285 struct net_port_vlans *pv;
286
287 ASSERT_RTNL();
288
289 pv = rtnl_dereference(br->vlan_info);
290 if (!pv)
291 return -EINVAL;
292
Toshiaki Makita8adff412013-10-16 17:07:13 +0900293 spin_lock_bh(&br->hash_lock);
294 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
295 spin_unlock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000296
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000297 __vlan_del(pv, vid);
298 return 0;
299}
300
301void br_vlan_flush(struct net_bridge *br)
302{
303 struct net_port_vlans *pv;
304
305 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000306 pv = rtnl_dereference(br->vlan_info);
307 if (!pv)
308 return;
309
310 __vlan_flush(pv);
311}
312
313int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
314{
315 if (!rtnl_trylock())
316 return restart_syscall();
317
318 if (br->vlan_enabled == val)
319 goto unlock;
320
321 br->vlan_enabled = val;
322
323unlock:
324 rtnl_unlock();
325 return 0;
326}
327
Toshiaki Makita8adff412013-10-16 17:07:13 +0900328/* Must be protected by RTNL.
329 * Must be called with vid in range from 1 to 4094 inclusive.
330 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000331int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000332{
333 struct net_port_vlans *pv = NULL;
334 int err;
335
336 ASSERT_RTNL();
337
338 pv = rtnl_dereference(port->vlan_info);
339 if (pv)
Vlad Yasevich552406c2013-02-13 12:00:15 +0000340 return __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000341
342 /* Create port vlan infomration
343 */
344 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
345 if (!pv) {
346 err = -ENOMEM;
347 goto clean_up;
348 }
349
350 pv->port_idx = port->port_no;
351 pv->parent.port = port;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000352 err = __vlan_add(pv, vid, flags);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000353 if (err)
354 goto clean_up;
355
356 rcu_assign_pointer(port->vlan_info, pv);
357 return 0;
358
359clean_up:
360 kfree(pv);
361 return err;
362}
363
Toshiaki Makita8adff412013-10-16 17:07:13 +0900364/* Must be protected by RTNL.
365 * Must be called with vid in range from 1 to 4094 inclusive.
366 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000367int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
368{
369 struct net_port_vlans *pv;
370
371 ASSERT_RTNL();
372
373 pv = rtnl_dereference(port->vlan_info);
374 if (!pv)
375 return -EINVAL;
376
Toshiaki Makita8adff412013-10-16 17:07:13 +0900377 spin_lock_bh(&port->br->hash_lock);
378 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
379 spin_unlock_bh(&port->br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000380
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000381 return __vlan_del(pv, vid);
382}
383
384void nbp_vlan_flush(struct net_bridge_port *port)
385{
386 struct net_port_vlans *pv;
387
388 ASSERT_RTNL();
389
390 pv = rtnl_dereference(port->vlan_info);
391 if (!pv)
392 return;
393
394 __vlan_flush(pv);
395}
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000396
397bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
398{
399 struct net_port_vlans *pv;
400 bool found = false;
401
402 rcu_read_lock();
403 pv = rcu_dereference(port->vlan_info);
404
405 if (!pv)
406 goto out;
407
408 if (test_bit(vid, pv->vlan_bitmap))
409 found = true;
410
411out:
412 rcu_read_unlock();
413 return found;
414}