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, br->vlan_proto, 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, br->vlan_proto, 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 struct net_bridge_port *p = v->parent.port;
97 vlan_vid_del(p->dev, p->br->vlan_proto, vid);
100 clear_bit(vid, v->vlan_bitmap);
102 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
104 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
106 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
112 static void __vlan_flush(struct net_port_vlans *v)
116 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
118 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
120 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
124 struct sk_buff *br_handle_vlan(struct net_bridge *br,
125 const struct net_port_vlans *pv,
130 if (!br->vlan_enabled)
133 /* Vlan filter table must be configured at this point. The
134 * only exception is the bridge is set in promisc mode and the
135 * packet is destined for the bridge device. In this case
136 * pass the packet as is.
139 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
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))
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)
166 /* If VLAN filtering is disabled on the bridge, all packets are
169 if (!br->vlan_enabled)
172 /* If there are no vlan in the permitted list, all packets are
178 proto = br->vlan_proto;
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 == proto)) {
186 skb = vlan_untag(skb);
191 if (!br_vlan_get_tag(skb, vid)) {
193 if (skb->vlan_proto != proto) {
194 /* Protocol-mismatch, empty out vlan_tci for new tag */
195 skb_push(skb, ETH_HLEN);
196 skb = __vlan_put_tag(skb, skb->vlan_proto,
197 vlan_tx_tag_get(skb));
201 skb_pull(skb, ETH_HLEN);
202 skb_reset_mac_len(skb);
214 u16 pvid = br_get_pvid(v);
216 /* Frame had a tag with VID 0 or did not have a tag.
217 * See if pvid is set on this port. That tells us which
218 * vlan untagged or priority-tagged traffic belongs to.
220 if (pvid == VLAN_N_VID)
223 /* PVID is set on this port. Any untagged or priority-tagged
224 * ingress frame is considered to belong to this vlan.
228 /* Untagged Frame. */
229 __vlan_hwaccel_put_tag(skb, proto, pvid);
231 /* Priority-tagged Frame.
232 * At this point, We know that skb->vlan_tci had
233 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
234 * We update only VID field and preserve PCP field.
236 skb->vlan_tci |= pvid;
241 /* Frame had a valid vlan tag. See if vlan is allowed */
242 if (test_bit(*vid, v->vlan_bitmap))
249 /* Called under RCU. */
250 bool br_allowed_egress(struct net_bridge *br,
251 const struct net_port_vlans *v,
252 const struct sk_buff *skb)
256 if (!br->vlan_enabled)
262 br_vlan_get_tag(skb, &vid);
263 if (test_bit(vid, v->vlan_bitmap))
269 /* Called under RCU */
270 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
272 struct net_bridge *br = p->br;
273 struct net_port_vlans *v;
275 if (!br->vlan_enabled)
278 v = rcu_dereference(p->vlan_info);
282 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
286 *vid = br_get_pvid(v);
287 if (*vid == VLAN_N_VID)
293 if (test_bit(*vid, v->vlan_bitmap))
299 /* Must be protected by RTNL.
300 * Must be called with vid in range from 1 to 4094 inclusive.
302 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
304 struct net_port_vlans *pv = NULL;
309 pv = rtnl_dereference(br->vlan_info);
311 return __vlan_add(pv, vid, flags);
313 /* Create port vlan infomration
315 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
320 err = __vlan_add(pv, vid, flags);
324 rcu_assign_pointer(br->vlan_info, pv);
331 /* Must be protected by RTNL.
332 * Must be called with vid in range from 1 to 4094 inclusive.
334 int br_vlan_delete(struct net_bridge *br, u16 vid)
336 struct net_port_vlans *pv;
340 pv = rtnl_dereference(br->vlan_info);
344 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
350 void br_vlan_flush(struct net_bridge *br)
352 struct net_port_vlans *pv;
355 pv = rtnl_dereference(br->vlan_info);
362 bool br_vlan_find(struct net_bridge *br, u16 vid)
364 struct net_port_vlans *pv;
368 pv = rcu_dereference(br->vlan_info);
373 if (test_bit(vid, pv->vlan_bitmap))
381 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
384 return restart_syscall();
386 if (br->vlan_enabled == val)
389 br->vlan_enabled = val;
390 br_manage_promisc(br);
397 void br_vlan_init(struct net_bridge *br)
399 br->vlan_proto = htons(ETH_P_8021Q);
402 /* Must be protected by RTNL.
403 * Must be called with vid in range from 1 to 4094 inclusive.
405 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
407 struct net_port_vlans *pv = NULL;
412 pv = rtnl_dereference(port->vlan_info);
414 return __vlan_add(pv, vid, flags);
416 /* Create port vlan infomration
418 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
424 pv->port_idx = port->port_no;
425 pv->parent.port = port;
426 err = __vlan_add(pv, vid, flags);
430 rcu_assign_pointer(port->vlan_info, pv);
438 /* Must be protected by RTNL.
439 * Must be called with vid in range from 1 to 4094 inclusive.
441 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
443 struct net_port_vlans *pv;
447 pv = rtnl_dereference(port->vlan_info);
451 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
453 return __vlan_del(pv, vid);
456 void nbp_vlan_flush(struct net_bridge_port *port)
458 struct net_port_vlans *pv;
463 pv = rtnl_dereference(port->vlan_info);
467 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
468 vlan_vid_del(port->dev, port->br->vlan_proto, vid);
473 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
475 struct net_port_vlans *pv;
479 pv = rcu_dereference(port->vlan_info);
484 if (test_bit(vid, pv->vlan_bitmap))