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 struct sk_buff *br_handle_vlan(struct net_bridge *br,
123 const struct net_port_vlans *pv,
128 if (!br->vlan_enabled)
131 /* At this point, we know that the frame was filtered and contains
132 * a valid vlan id. If the vlan id is set in the untagged bitmap,
133 * send untagged; otherwise, send tagged.
135 br_vlan_get_tag(skb, &vid);
136 if (test_bit(vid, pv->untagged_bitmap))
143 /* Called under RCU */
144 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
145 struct sk_buff *skb, u16 *vid)
149 /* If VLAN filtering is disabled on the bridge, all packets are
152 if (!br->vlan_enabled)
155 /* If there are no vlan in the permitted list, all packets are
161 /* If vlan tx offload is disabled on bridge device and frame was
162 * sent from vlan device on the bridge device, it does not have
163 * HW accelerated vlan tag.
165 if (unlikely(!vlan_tx_tag_present(skb) &&
166 (skb->protocol == htons(ETH_P_8021Q) ||
167 skb->protocol == htons(ETH_P_8021AD)))) {
168 skb = vlan_untag(skb);
173 err = br_vlan_get_tag(skb, vid);
175 u16 pvid = br_get_pvid(v);
177 /* Frame had a tag with VID 0 or did not have a tag.
178 * See if pvid is set on this port. That tells us which
179 * vlan untagged or priority-tagged traffic belongs to.
181 if (pvid == VLAN_N_VID)
184 /* PVID is set on this port. Any untagged or priority-tagged
185 * ingress frame is considered to belong to this vlan.
189 /* Untagged Frame. */
190 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
192 /* Priority-tagged Frame.
193 * At this point, We know that skb->vlan_tci had
194 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
195 * We update only VID field and preserve PCP field.
197 skb->vlan_tci |= pvid;
202 /* Frame had a valid vlan tag. See if vlan is allowed */
203 if (test_bit(*vid, v->vlan_bitmap))
209 /* Called under RCU. */
210 bool br_allowed_egress(struct net_bridge *br,
211 const struct net_port_vlans *v,
212 const struct sk_buff *skb)
216 if (!br->vlan_enabled)
222 br_vlan_get_tag(skb, &vid);
223 if (test_bit(vid, v->vlan_bitmap))
229 /* Must be protected by RTNL.
230 * Must be called with vid in range from 1 to 4094 inclusive.
232 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
234 struct net_port_vlans *pv = NULL;
239 pv = rtnl_dereference(br->vlan_info);
241 return __vlan_add(pv, vid, flags);
243 /* Create port vlan infomration
245 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
250 err = __vlan_add(pv, vid, flags);
254 rcu_assign_pointer(br->vlan_info, pv);
261 /* Must be protected by RTNL.
262 * Must be called with vid in range from 1 to 4094 inclusive.
264 int br_vlan_delete(struct net_bridge *br, u16 vid)
266 struct net_port_vlans *pv;
270 pv = rtnl_dereference(br->vlan_info);
274 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
280 void br_vlan_flush(struct net_bridge *br)
282 struct net_port_vlans *pv;
285 pv = rtnl_dereference(br->vlan_info);
292 bool br_vlan_find(struct net_bridge *br, u16 vid)
294 struct net_port_vlans *pv;
298 pv = rcu_dereference(br->vlan_info);
303 if (test_bit(vid, pv->vlan_bitmap))
311 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
314 return restart_syscall();
316 if (br->vlan_enabled == val)
319 br->vlan_enabled = val;
326 /* Must be protected by RTNL.
327 * Must be called with vid in range from 1 to 4094 inclusive.
329 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
331 struct net_port_vlans *pv = NULL;
336 pv = rtnl_dereference(port->vlan_info);
338 return __vlan_add(pv, vid, flags);
340 /* Create port vlan infomration
342 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
348 pv->port_idx = port->port_no;
349 pv->parent.port = port;
350 err = __vlan_add(pv, vid, flags);
354 rcu_assign_pointer(port->vlan_info, pv);
362 /* Must be protected by RTNL.
363 * Must be called with vid in range from 1 to 4094 inclusive.
365 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
367 struct net_port_vlans *pv;
371 pv = rtnl_dereference(port->vlan_info);
375 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
377 return __vlan_del(pv, vid);
380 void nbp_vlan_flush(struct net_bridge_port *port)
382 struct net_port_vlans *pv;
387 pv = rtnl_dereference(port->vlan_info);
391 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
392 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
397 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
399 struct net_port_vlans *pv;
403 pv = rcu_dereference(port->vlan_info);
408 if (test_bit(vid, pv->vlan_bitmap))