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 this packet was not filtered at input, let it pass */
129 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
132 /* Vlan filter table must be configured at this point. The
133 * only exception is the bridge is set in promisc mode and the
134 * packet is destined for the bridge device. In this case
135 * pass the packet as is.
138 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
146 /* At this point, we know that the frame was filtered and contains
147 * a valid vlan id. If the vlan id is set in the untagged bitmap,
148 * send untagged; otherwise, send tagged.
150 br_vlan_get_tag(skb, &vid);
151 if (test_bit(vid, pv->untagged_bitmap))
158 /* Called under RCU */
159 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
160 struct sk_buff *skb, u16 *vid)
164 /* If VLAN filtering is disabled on the bridge, all packets are
167 if (!br->vlan_enabled) {
168 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
172 /* If there are no vlan in the permitted list, all packets are
178 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
180 /* If vlan tx offload is disabled on bridge device and frame was
181 * sent from vlan device on the bridge device, it does not have
182 * HW accelerated vlan tag.
184 if (unlikely(!vlan_tx_tag_present(skb) &&
185 (skb->protocol == htons(ETH_P_8021Q) ||
186 skb->protocol == htons(ETH_P_8021AD)))) {
187 skb = skb_vlan_untag(skb);
192 err = br_vlan_get_tag(skb, vid);
194 u16 pvid = br_get_pvid(v);
196 /* Frame had a tag with VID 0 or did not have a tag.
197 * See if pvid is set on this port. That tells us which
198 * vlan untagged or priority-tagged traffic belongs to.
200 if (pvid == VLAN_N_VID)
203 /* PVID is set on this port. Any untagged or priority-tagged
204 * ingress frame is considered to belong to this vlan.
208 /* Untagged Frame. */
209 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
211 /* Priority-tagged Frame.
212 * At this point, We know that skb->vlan_tci had
213 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
214 * We update only VID field and preserve PCP field.
216 skb->vlan_tci |= pvid;
221 /* Frame had a valid vlan tag. See if vlan is allowed */
222 if (test_bit(*vid, v->vlan_bitmap))
229 /* Called under RCU. */
230 bool br_allowed_egress(struct net_bridge *br,
231 const struct net_port_vlans *v,
232 const struct sk_buff *skb)
236 /* If this packet was not filtered at input, let it pass */
237 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
243 br_vlan_get_tag(skb, &vid);
244 if (test_bit(vid, v->vlan_bitmap))
250 /* Called under RCU */
251 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
253 struct net_bridge *br = p->br;
254 struct net_port_vlans *v;
256 /* If filtering was disabled at input, let it pass. */
257 if (!br->vlan_enabled)
260 v = rcu_dereference(p->vlan_info);
264 br_vlan_get_tag(skb, vid);
266 *vid = br_get_pvid(v);
267 if (*vid == VLAN_N_VID)
273 if (test_bit(*vid, v->vlan_bitmap))
279 /* Must be protected by RTNL.
280 * Must be called with vid in range from 1 to 4094 inclusive.
282 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
284 struct net_port_vlans *pv = NULL;
289 pv = rtnl_dereference(br->vlan_info);
291 return __vlan_add(pv, vid, flags);
293 /* Create port vlan infomration
295 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
300 err = __vlan_add(pv, vid, flags);
304 rcu_assign_pointer(br->vlan_info, pv);
311 /* Must be protected by RTNL.
312 * Must be called with vid in range from 1 to 4094 inclusive.
314 int br_vlan_delete(struct net_bridge *br, u16 vid)
316 struct net_port_vlans *pv;
320 pv = rtnl_dereference(br->vlan_info);
324 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
330 void br_vlan_flush(struct net_bridge *br)
332 struct net_port_vlans *pv;
335 pv = rtnl_dereference(br->vlan_info);
342 bool br_vlan_find(struct net_bridge *br, u16 vid)
344 struct net_port_vlans *pv;
348 pv = rcu_dereference(br->vlan_info);
353 if (test_bit(vid, pv->vlan_bitmap))
361 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
364 return restart_syscall();
366 if (br->vlan_enabled == val)
369 br->vlan_enabled = val;
376 /* Must be protected by RTNL.
377 * Must be called with vid in range from 1 to 4094 inclusive.
379 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
381 struct net_port_vlans *pv = NULL;
386 pv = rtnl_dereference(port->vlan_info);
388 return __vlan_add(pv, vid, flags);
390 /* Create port vlan infomration
392 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
398 pv->port_idx = port->port_no;
399 pv->parent.port = port;
400 err = __vlan_add(pv, vid, flags);
404 rcu_assign_pointer(port->vlan_info, pv);
412 /* Must be protected by RTNL.
413 * Must be called with vid in range from 1 to 4094 inclusive.
415 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
417 struct net_port_vlans *pv;
421 pv = rtnl_dereference(port->vlan_info);
425 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
427 return __vlan_del(pv, vid);
430 void nbp_vlan_flush(struct net_bridge_port *port)
432 struct net_port_vlans *pv;
437 pv = rtnl_dereference(port->vlan_info);
441 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
442 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
447 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
449 struct net_port_vlans *pv;
453 pv = rcu_dereference(port->vlan_info);
458 if (test_bit(vid, pv->vlan_bitmap))