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 tagged.
151 br_vlan_get_tag(skb, &vid);
152 if (test_bit(vid, pv->untagged_bitmap))
153 skb = br_vlan_untag(skb);
159 /* Called under RCU */
160 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
161 struct sk_buff *skb, u16 *vid)
165 /* If VLAN filtering is disabled on the bridge, all packets are
168 if (!br->vlan_enabled)
171 /* If there are no vlan in the permitted list, all packets are
177 err = br_vlan_get_tag(skb, vid);
179 u16 pvid = br_get_pvid(v);
181 /* Frame had a tag with VID 0 or did not have a tag.
182 * See if pvid is set on this port. That tells us which
183 * vlan untagged or priority-tagged traffic belongs to.
185 if (pvid == VLAN_N_VID)
188 /* PVID is set on this port. Any untagged or priority-tagged
189 * ingress frame is considered to belong to this vlan.
193 /* Untagged Frame. */
194 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
196 /* Priority-tagged Frame.
197 * At this point, We know that skb->vlan_tci had
198 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
199 * We update only VID field and preserve PCP field.
201 skb->vlan_tci |= pvid;
206 /* Frame had a valid vlan tag. See if vlan is allowed */
207 if (test_bit(*vid, v->vlan_bitmap))
213 /* Called under RCU. */
214 bool br_allowed_egress(struct net_bridge *br,
215 const struct net_port_vlans *v,
216 const struct sk_buff *skb)
220 if (!br->vlan_enabled)
226 br_vlan_get_tag(skb, &vid);
227 if (test_bit(vid, v->vlan_bitmap))
233 /* Must be protected by RTNL.
234 * Must be called with vid in range from 1 to 4094 inclusive.
236 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
238 struct net_port_vlans *pv = NULL;
243 pv = rtnl_dereference(br->vlan_info);
245 return __vlan_add(pv, vid, flags);
247 /* Create port vlan infomration
249 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
254 err = __vlan_add(pv, vid, flags);
258 rcu_assign_pointer(br->vlan_info, pv);
265 /* Must be protected by RTNL.
266 * Must be called with vid in range from 1 to 4094 inclusive.
268 int br_vlan_delete(struct net_bridge *br, u16 vid)
270 struct net_port_vlans *pv;
274 pv = rtnl_dereference(br->vlan_info);
278 spin_lock_bh(&br->hash_lock);
279 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
280 spin_unlock_bh(&br->hash_lock);
286 void br_vlan_flush(struct net_bridge *br)
288 struct net_port_vlans *pv;
291 pv = rtnl_dereference(br->vlan_info);
298 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
301 return restart_syscall();
303 if (br->vlan_enabled == val)
306 br->vlan_enabled = val;
313 /* Must be protected by RTNL.
314 * Must be called with vid in range from 1 to 4094 inclusive.
316 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
318 struct net_port_vlans *pv = NULL;
323 pv = rtnl_dereference(port->vlan_info);
325 return __vlan_add(pv, vid, flags);
327 /* Create port vlan infomration
329 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
335 pv->port_idx = port->port_no;
336 pv->parent.port = port;
337 err = __vlan_add(pv, vid, flags);
341 rcu_assign_pointer(port->vlan_info, pv);
349 /* Must be protected by RTNL.
350 * Must be called with vid in range from 1 to 4094 inclusive.
352 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
354 struct net_port_vlans *pv;
358 pv = rtnl_dereference(port->vlan_info);
362 spin_lock_bh(&port->br->hash_lock);
363 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
364 spin_unlock_bh(&port->br->hash_lock);
366 return __vlan_del(pv, vid);
369 void nbp_vlan_flush(struct net_bridge_port *port)
371 struct net_port_vlans *pv;
376 pv = rtnl_dereference(port->vlan_info);
380 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
381 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
386 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
388 struct net_port_vlans *pv;
392 pv = rcu_dereference(port->vlan_info);
397 if (test_bit(vid, pv->vlan_bitmap))