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 /* If vlan tx offload is disabled on bridge device and frame was
178 * sent from vlan device on the bridge device, it does not have
179 * HW accelerated vlan tag.
181 if (unlikely(!vlan_tx_tag_present(skb) &&
182 (skb->protocol == htons(ETH_P_8021Q) ||
183 skb->protocol == htons(ETH_P_8021AD)))) {
184 skb = vlan_untag(skb);
189 err = br_vlan_get_tag(skb, vid);
191 u16 pvid = br_get_pvid(v);
193 /* Frame had a tag with VID 0 or did not have a tag.
194 * See if pvid is set on this port. That tells us which
195 * vlan untagged or priority-tagged traffic belongs to.
197 if (pvid == VLAN_N_VID)
200 /* PVID is set on this port. Any untagged or priority-tagged
201 * ingress frame is considered to belong to this vlan.
205 /* Untagged Frame. */
206 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
208 /* Priority-tagged Frame.
209 * At this point, We know that skb->vlan_tci had
210 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
211 * We update only VID field and preserve PCP field.
213 skb->vlan_tci |= pvid;
218 /* Frame had a valid vlan tag. See if vlan is allowed */
219 if (test_bit(*vid, v->vlan_bitmap))
225 /* Called under RCU. */
226 bool br_allowed_egress(struct net_bridge *br,
227 const struct net_port_vlans *v,
228 const struct sk_buff *skb)
232 if (!br->vlan_enabled)
238 br_vlan_get_tag(skb, &vid);
239 if (test_bit(vid, v->vlan_bitmap))
245 /* Must be protected by RTNL.
246 * Must be called with vid in range from 1 to 4094 inclusive.
248 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
250 struct net_port_vlans *pv = NULL;
255 pv = rtnl_dereference(br->vlan_info);
257 return __vlan_add(pv, vid, flags);
259 /* Create port vlan infomration
261 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
266 err = __vlan_add(pv, vid, flags);
270 rcu_assign_pointer(br->vlan_info, pv);
277 /* Must be protected by RTNL.
278 * Must be called with vid in range from 1 to 4094 inclusive.
280 int br_vlan_delete(struct net_bridge *br, u16 vid)
282 struct net_port_vlans *pv;
286 pv = rtnl_dereference(br->vlan_info);
290 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
296 void br_vlan_flush(struct net_bridge *br)
298 struct net_port_vlans *pv;
301 pv = rtnl_dereference(br->vlan_info);
308 bool br_vlan_find(struct net_bridge *br, u16 vid)
310 struct net_port_vlans *pv;
314 pv = rcu_dereference(br->vlan_info);
319 if (test_bit(vid, pv->vlan_bitmap))
327 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
330 return restart_syscall();
332 if (br->vlan_enabled == val)
335 br->vlan_enabled = val;
342 /* Must be protected by RTNL.
343 * Must be called with vid in range from 1 to 4094 inclusive.
345 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
347 struct net_port_vlans *pv = NULL;
352 pv = rtnl_dereference(port->vlan_info);
354 return __vlan_add(pv, vid, flags);
356 /* Create port vlan infomration
358 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
364 pv->port_idx = port->port_no;
365 pv->parent.port = port;
366 err = __vlan_add(pv, vid, flags);
370 rcu_assign_pointer(port->vlan_info, pv);
378 /* Must be protected by RTNL.
379 * Must be called with vid in range from 1 to 4094 inclusive.
381 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
383 struct net_port_vlans *pv;
387 pv = rtnl_dereference(port->vlan_info);
391 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
393 return __vlan_del(pv, vid);
396 void nbp_vlan_flush(struct net_bridge_port *port)
398 struct net_port_vlans *pv;
403 pv = rtnl_dereference(port->vlan_info);
407 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
408 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
413 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
415 struct net_port_vlans *pv;
419 pv = rcu_dereference(port->vlan_info);
424 if (test_bit(vid, pv->vlan_bitmap))