Linux 3.12.28
[linux/fpc-iii.git] / net / bridge / br_vlan.c
blob12ce54c0e8ed12c1578ffca80b97cf5782c87324
1 #include <linux/kernel.h>
2 #include <linux/netdevice.h>
3 #include <linux/rtnetlink.h>
4 #include <linux/slab.h>
6 #include "br_private.h"
8 static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
10 if (v->pvid == vid)
11 return;
13 smp_wmb();
14 v->pvid = vid;
17 static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
19 if (v->pvid != vid)
20 return;
22 smp_wmb();
23 v->pvid = 0;
26 static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
28 if (flags & BRIDGE_VLAN_INFO_PVID)
29 __vlan_add_pvid(v, vid);
31 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
32 set_bit(vid, v->untagged_bitmap);
35 static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
37 const struct net_device_ops *ops;
38 struct net_bridge_port *p = NULL;
39 struct net_bridge *br;
40 struct net_device *dev;
41 int err;
43 if (test_bit(vid, v->vlan_bitmap)) {
44 __vlan_add_flags(v, vid, flags);
45 return 0;
48 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;
56 ops = dev->netdev_ops;
58 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.
65 err = ops->ndo_vlan_rx_add_vid(dev, htons(ETH_P_8021Q),
66 vid);
67 if (err)
68 return err;
71 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;
78 set_bit(vid, v->vlan_bitmap);
79 v->num_vlans++;
80 __vlan_add_flags(v, vid, flags);
82 return 0;
84 out_filt:
85 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
86 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
87 return err;
90 static int __vlan_del(struct net_port_vlans *v, u16 vid)
92 if (!test_bit(vid, v->vlan_bitmap))
93 return -EINVAL;
95 __vlan_delete_pvid(v, vid);
96 clear_bit(vid, v->untagged_bitmap);
98 if (v->port_idx) {
99 struct net_device *dev = v->parent.port->dev;
100 const struct net_device_ops *ops = dev->netdev_ops;
102 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
103 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
106 clear_bit(vid, v->vlan_bitmap);
107 v->num_vlans--;
108 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
109 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);
115 return 0;
118 static void __vlan_flush(struct net_port_vlans *v)
120 smp_wmb();
121 v->pvid = 0;
122 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
123 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);
130 /* Strip the tag from the packet. Will return skb with tci set 0. */
131 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
133 if (skb->protocol != htons(ETH_P_8021Q)) {
134 skb->vlan_tci = 0;
135 return skb;
138 skb->vlan_tci = 0;
139 skb = vlan_untag(skb);
140 if (skb)
141 skb->vlan_tci = 0;
143 return skb;
146 struct sk_buff *br_handle_vlan(struct net_bridge *br,
147 const struct net_port_vlans *pv,
148 struct sk_buff *skb)
150 u16 vid;
152 if (!br->vlan_enabled)
153 goto out;
155 /* At this point, we know that the frame was filtered and contains
156 * a valid vlan id. If the vlan id is set in the untagged bitmap,
157 * send untagged; otherwise, send taged.
159 br_vlan_get_tag(skb, &vid);
160 if (test_bit(vid, pv->untagged_bitmap))
161 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.
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.
174 skb_push(skb, ETH_HLEN);
175 skb = __vlan_put_tag(skb, skb->vlan_proto, skb->vlan_tci);
176 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;
184 out:
185 return skb;
188 /* Called under RCU */
189 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
190 struct sk_buff *skb, u16 *vid)
192 int err;
194 /* If VLAN filtering is disabled on the bridge, all packets are
195 * permitted.
197 if (!br->vlan_enabled)
198 return true;
200 /* If there are no vlan in the permitted list, all packets are
201 * rejected.
203 if (!v)
204 goto drop;
206 err = br_vlan_get_tag(skb, vid);
207 if (!*vid) {
208 u16 pvid = br_get_pvid(v);
210 /* 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.
214 if (pvid == VLAN_N_VID)
215 goto drop;
217 /* PVID is set on this port. Any untagged or priority-tagged
218 * ingress frame is considered to belong to this vlan.
220 *vid = pvid;
221 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.
230 skb->vlan_tci |= pvid;
232 return true;
235 /* Frame had a valid vlan tag. See if vlan is allowed */
236 if (test_bit(*vid, v->vlan_bitmap))
237 return true;
238 drop:
239 kfree_skb(skb);
240 return false;
243 /* Called under RCU. */
244 bool br_allowed_egress(struct net_bridge *br,
245 const struct net_port_vlans *v,
246 const struct sk_buff *skb)
248 u16 vid;
250 if (!br->vlan_enabled)
251 return true;
253 if (!v)
254 return false;
256 br_vlan_get_tag(skb, &vid);
257 if (test_bit(vid, v->vlan_bitmap))
258 return true;
260 return false;
263 /* Called under RCU */
264 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
266 struct net_bridge *br = p->br;
267 struct net_port_vlans *v;
269 if (!br->vlan_enabled)
270 return true;
272 v = rcu_dereference(p->vlan_info);
273 if (!v)
274 return false;
276 br_vlan_get_tag(skb, vid);
277 if (!*vid) {
278 *vid = br_get_pvid(v);
279 if (*vid == VLAN_N_VID)
280 return false;
282 return true;
285 if (test_bit(*vid, v->vlan_bitmap))
286 return true;
288 return false;
291 /* Must be protected by RTNL.
292 * Must be called with vid in range from 1 to 4094 inclusive.
294 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
296 struct net_port_vlans *pv = NULL;
297 int err;
299 ASSERT_RTNL();
301 pv = rtnl_dereference(br->vlan_info);
302 if (pv)
303 return __vlan_add(pv, vid, flags);
305 /* Create port vlan infomration
307 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
308 if (!pv)
309 return -ENOMEM;
311 pv->parent.br = br;
312 err = __vlan_add(pv, vid, flags);
313 if (err)
314 goto out;
316 rcu_assign_pointer(br->vlan_info, pv);
317 return 0;
318 out:
319 kfree(pv);
320 return err;
323 /* Must be protected by RTNL.
324 * Must be called with vid in range from 1 to 4094 inclusive.
326 int br_vlan_delete(struct net_bridge *br, u16 vid)
328 struct net_port_vlans *pv;
330 ASSERT_RTNL();
332 pv = rtnl_dereference(br->vlan_info);
333 if (!pv)
334 return -EINVAL;
336 spin_lock_bh(&br->hash_lock);
337 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
338 spin_unlock_bh(&br->hash_lock);
340 __vlan_del(pv, vid);
341 return 0;
344 void br_vlan_flush(struct net_bridge *br)
346 struct net_port_vlans *pv;
348 ASSERT_RTNL();
349 pv = rtnl_dereference(br->vlan_info);
350 if (!pv)
351 return;
353 __vlan_flush(pv);
356 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
358 if (!rtnl_trylock())
359 return restart_syscall();
361 if (br->vlan_enabled == val)
362 goto unlock;
364 br->vlan_enabled = val;
366 unlock:
367 rtnl_unlock();
368 return 0;
371 /* Must be protected by RTNL.
372 * Must be called with vid in range from 1 to 4094 inclusive.
374 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
376 struct net_port_vlans *pv = NULL;
377 int err;
379 ASSERT_RTNL();
381 pv = rtnl_dereference(port->vlan_info);
382 if (pv)
383 return __vlan_add(pv, vid, flags);
385 /* Create port vlan infomration
387 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
388 if (!pv) {
389 err = -ENOMEM;
390 goto clean_up;
393 pv->port_idx = port->port_no;
394 pv->parent.port = port;
395 err = __vlan_add(pv, vid, flags);
396 if (err)
397 goto clean_up;
399 rcu_assign_pointer(port->vlan_info, pv);
400 return 0;
402 clean_up:
403 kfree(pv);
404 return err;
407 /* Must be protected by RTNL.
408 * Must be called with vid in range from 1 to 4094 inclusive.
410 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
412 struct net_port_vlans *pv;
414 ASSERT_RTNL();
416 pv = rtnl_dereference(port->vlan_info);
417 if (!pv)
418 return -EINVAL;
420 spin_lock_bh(&port->br->hash_lock);
421 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
422 spin_unlock_bh(&port->br->hash_lock);
424 return __vlan_del(pv, vid);
427 void nbp_vlan_flush(struct net_bridge_port *port)
429 struct net_port_vlans *pv;
431 ASSERT_RTNL();
433 pv = rtnl_dereference(port->vlan_info);
434 if (!pv)
435 return;
437 __vlan_flush(pv);
440 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
442 struct net_port_vlans *pv;
443 bool found = false;
445 rcu_read_lock();
446 pv = rcu_dereference(port->vlan_info);
448 if (!pv)
449 goto out;
451 if (test_bit(vid, pv->vlan_bitmap))
452 found = true;
454 out:
455 rcu_read_unlock();
456 return found;