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 /* Vlan filter table must be configured at this point. The
132 * only exception is the bridge is set in promisc mode and the
133 * packet is destined for the bridge device. In this case
134 * pass the packet as is.
137 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
145 /* At this point, we know that the frame was filtered and contains
146 * a valid vlan id. If the vlan id is set in the untagged bitmap,
147 * send untagged; otherwise, send tagged.
149 br_vlan_get_tag(skb, &vid);
150 if (test_bit(vid, pv->untagged_bitmap))
157 /* Called under RCU */
158 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
159 struct sk_buff *skb, u16 *vid)
163 /* If VLAN filtering is disabled on the bridge, all packets are
166 if (!br->vlan_enabled)
169 /* If there are no vlan in the permitted list, all packets are
175 /* If vlan tx offload is disabled on bridge device and frame was
176 * sent from vlan device on the bridge device, it does not have
177 * HW accelerated vlan tag.
179 if (unlikely(!vlan_tx_tag_present(skb) &&
180 (skb->protocol == htons(ETH_P_8021Q) ||
181 skb->protocol == htons(ETH_P_8021AD)))) {
182 skb = vlan_untag(skb);
187 err = br_vlan_get_tag(skb, vid);
189 u16 pvid = br_get_pvid(v);
191 /* Frame had a tag with VID 0 or did not have a tag.
192 * See if pvid is set on this port. That tells us which
193 * vlan untagged or priority-tagged traffic belongs to.
195 if (pvid == VLAN_N_VID)
198 /* PVID is set on this port. Any untagged or priority-tagged
199 * ingress frame is considered to belong to this vlan.
203 /* Untagged Frame. */
204 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
206 /* Priority-tagged Frame.
207 * At this point, We know that skb->vlan_tci had
208 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
209 * We update only VID field and preserve PCP field.
211 skb->vlan_tci |= pvid;
216 /* Frame had a valid vlan tag. See if vlan is allowed */
217 if (test_bit(*vid, v->vlan_bitmap))
223 /* Called under RCU. */
224 bool br_allowed_egress(struct net_bridge *br,
225 const struct net_port_vlans *v,
226 const struct sk_buff *skb)
230 if (!br->vlan_enabled)
236 br_vlan_get_tag(skb, &vid);
237 if (test_bit(vid, v->vlan_bitmap))
243 /* Must be protected by RTNL.
244 * Must be called with vid in range from 1 to 4094 inclusive.
246 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
248 struct net_port_vlans *pv = NULL;
253 pv = rtnl_dereference(br->vlan_info);
255 return __vlan_add(pv, vid, flags);
257 /* Create port vlan infomration
259 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
264 err = __vlan_add(pv, vid, flags);
268 rcu_assign_pointer(br->vlan_info, pv);
275 /* Must be protected by RTNL.
276 * Must be called with vid in range from 1 to 4094 inclusive.
278 int br_vlan_delete(struct net_bridge *br, u16 vid)
280 struct net_port_vlans *pv;
284 pv = rtnl_dereference(br->vlan_info);
288 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
294 void br_vlan_flush(struct net_bridge *br)
296 struct net_port_vlans *pv;
299 pv = rtnl_dereference(br->vlan_info);
306 bool br_vlan_find(struct net_bridge *br, u16 vid)
308 struct net_port_vlans *pv;
312 pv = rcu_dereference(br->vlan_info);
317 if (test_bit(vid, pv->vlan_bitmap))
325 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
328 return restart_syscall();
330 if (br->vlan_enabled == val)
333 br->vlan_enabled = val;
340 /* Must be protected by RTNL.
341 * Must be called with vid in range from 1 to 4094 inclusive.
343 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
345 struct net_port_vlans *pv = NULL;
350 pv = rtnl_dereference(port->vlan_info);
352 return __vlan_add(pv, vid, flags);
354 /* Create port vlan infomration
356 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
362 pv->port_idx = port->port_no;
363 pv->parent.port = port;
364 err = __vlan_add(pv, vid, flags);
368 rcu_assign_pointer(port->vlan_info, pv);
376 /* Must be protected by RTNL.
377 * Must be called with vid in range from 1 to 4094 inclusive.
379 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
381 struct net_port_vlans *pv;
385 pv = rtnl_dereference(port->vlan_info);
389 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
391 return __vlan_del(pv, vid);
394 void nbp_vlan_flush(struct net_bridge_port *port)
396 struct net_port_vlans *pv;
401 pv = rtnl_dereference(port->vlan_info);
405 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
406 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
411 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
413 struct net_port_vlans *pv;
417 pv = rcu_dereference(port->vlan_info);
422 if (test_bit(vid, pv->vlan_bitmap))