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)
17 static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
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 struct net_bridge_port *p = NULL;
38 struct net_bridge *br;
39 struct net_device *dev;
42 if (test_bit(vid, v->vlan_bitmap)) {
43 __vlan_add_flags(v, vid, flags);
57 /* Add VLAN to the device filter if it is supported.
58 * Stricly speaking, this is not necessary now, since
59 * devices are made promiscuous by the bridge, but if
60 * that ever changes this code will allow tagged
61 * traffic to enter the bridge.
63 err = vlan_vid_add(dev, htons(ETH_P_8021Q), vid);
68 err = br_fdb_insert(br, p, dev->dev_addr, vid);
70 br_err(br, "failed insert local address into bridge "
71 "forwarding table\n");
75 set_bit(vid, v->vlan_bitmap);
77 __vlan_add_flags(v, vid, flags);
83 vlan_vid_del(dev, htons(ETH_P_8021Q), vid);
87 static int __vlan_del(struct net_port_vlans *v, u16 vid)
89 if (!test_bit(vid, v->vlan_bitmap))
92 __vlan_delete_pvid(v, vid);
93 clear_bit(vid, v->untagged_bitmap);
96 vlan_vid_del(v->parent.port->dev, htons(ETH_P_8021Q), vid);
98 clear_bit(vid, v->vlan_bitmap);
100 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
102 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
104 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
110 static void __vlan_flush(struct net_port_vlans *v)
114 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
116 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
118 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
122 /* Strip the tag from the packet. Will return skb with tci set 0. */
123 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
125 if (skb->protocol != htons(ETH_P_8021Q)) {
131 skb = vlan_untag(skb);
138 struct sk_buff *br_handle_vlan(struct net_bridge *br,
139 const struct net_port_vlans *pv,
144 if (!br->vlan_enabled)
147 /* At this point, we know that the frame was filtered and contains
148 * a valid vlan id. If the vlan id is set in the untagged bitmap,
149 * send untagged; otherwise, send taged.
151 br_vlan_get_tag(skb, &vid);
152 if (test_bit(vid, pv->untagged_bitmap))
153 skb = br_vlan_untag(skb);
155 /* Egress policy says "send tagged". If output device
156 * is the bridge, we need to add the VLAN header
157 * ourselves since we'll be going through the RX path.
158 * Sending to ports puts the frame on the TX path and
159 * we let dev_hard_start_xmit() add the header.
161 if (skb->protocol != htons(ETH_P_8021Q) &&
163 /* vlan_put_tag expects skb->data to point to
166 skb_push(skb, ETH_HLEN);
167 skb = __vlan_put_tag(skb, skb->vlan_proto, skb->vlan_tci);
170 /* put skb->data back to where it was */
171 skb_pull(skb, ETH_HLEN);
180 /* Called under RCU */
181 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
182 struct sk_buff *skb, u16 *vid)
186 /* If VLAN filtering is disabled on the bridge, all packets are
189 if (!br->vlan_enabled)
192 /* If there are no vlan in the permitted list, all packets are
198 err = br_vlan_get_tag(skb, vid);
200 u16 pvid = br_get_pvid(v);
202 /* Frame had a tag with VID 0 or did not have a tag.
203 * See if pvid is set on this port. That tells us which
204 * vlan untagged or priority-tagged traffic belongs to.
206 if (pvid == VLAN_N_VID)
209 /* PVID is set on this port. Any untagged or priority-tagged
210 * ingress frame is considered to belong to this vlan.
214 /* Untagged Frame. */
215 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
217 /* Priority-tagged Frame.
218 * At this point, We know that skb->vlan_tci had
219 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
220 * We update only VID field and preserve PCP field.
222 skb->vlan_tci |= pvid;
227 /* Frame had a valid vlan tag. See if vlan is allowed */
228 if (test_bit(*vid, v->vlan_bitmap))
234 /* Called under RCU. */
235 bool br_allowed_egress(struct net_bridge *br,
236 const struct net_port_vlans *v,
237 const struct sk_buff *skb)
241 if (!br->vlan_enabled)
247 br_vlan_get_tag(skb, &vid);
248 if (test_bit(vid, v->vlan_bitmap))
254 /* Must be protected by RTNL.
255 * Must be called with vid in range from 1 to 4094 inclusive.
257 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
259 struct net_port_vlans *pv = NULL;
264 pv = rtnl_dereference(br->vlan_info);
266 return __vlan_add(pv, vid, flags);
268 /* Create port vlan infomration
270 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
275 err = __vlan_add(pv, vid, flags);
279 rcu_assign_pointer(br->vlan_info, pv);
286 /* Must be protected by RTNL.
287 * Must be called with vid in range from 1 to 4094 inclusive.
289 int br_vlan_delete(struct net_bridge *br, u16 vid)
291 struct net_port_vlans *pv;
295 pv = rtnl_dereference(br->vlan_info);
299 spin_lock_bh(&br->hash_lock);
300 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
301 spin_unlock_bh(&br->hash_lock);
307 void br_vlan_flush(struct net_bridge *br)
309 struct net_port_vlans *pv;
312 pv = rtnl_dereference(br->vlan_info);
319 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
322 return restart_syscall();
324 if (br->vlan_enabled == val)
327 br->vlan_enabled = val;
334 /* Must be protected by RTNL.
335 * Must be called with vid in range from 1 to 4094 inclusive.
337 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
339 struct net_port_vlans *pv = NULL;
344 pv = rtnl_dereference(port->vlan_info);
346 return __vlan_add(pv, vid, flags);
348 /* Create port vlan infomration
350 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
356 pv->port_idx = port->port_no;
357 pv->parent.port = port;
358 err = __vlan_add(pv, vid, flags);
362 rcu_assign_pointer(port->vlan_info, pv);
370 /* Must be protected by RTNL.
371 * Must be called with vid in range from 1 to 4094 inclusive.
373 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
375 struct net_port_vlans *pv;
379 pv = rtnl_dereference(port->vlan_info);
383 spin_lock_bh(&port->br->hash_lock);
384 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
385 spin_unlock_bh(&port->br->hash_lock);
387 return __vlan_del(pv, vid);
390 void nbp_vlan_flush(struct net_bridge_port *port)
392 struct net_port_vlans *pv;
397 pv = rtnl_dereference(port->vlan_info);
401 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
402 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
407 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
409 struct net_port_vlans *pv;
413 pv = rcu_dereference(port->vlan_info);
418 if (test_bit(vid, pv->vlan_bitmap))