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 int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
28 struct net_bridge_port *p = NULL;
29 struct net_bridge *br;
30 struct net_device *dev;
33 if (test_bit(vid, v->vlan_bitmap)) {
34 if (flags & BRIDGE_VLAN_INFO_PVID)
35 __vlan_add_pvid(v, vid);
49 if (p && (dev->features & NETIF_F_HW_VLAN_FILTER)) {
50 /* Add VLAN to the device filter if it is supported.
51 * Stricly speaking, this is not necessary now, since
52 * devices are made promiscuous by the bridge, but if
53 * that ever changes this code will allow tagged
54 * traffic to enter the bridge.
56 err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
61 err = br_fdb_insert(br, p, dev->dev_addr, vid);
63 br_err(br, "failed insert local address into bridge "
64 "forwarding table\n");
70 set_bit(vid, v->vlan_bitmap);
72 if (flags & BRIDGE_VLAN_INFO_PVID)
73 __vlan_add_pvid(v, vid);
78 if (p && (dev->features & NETIF_F_HW_VLAN_FILTER))
79 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
83 static int __vlan_del(struct net_port_vlans *v, u16 vid)
85 if (!test_bit(vid, v->vlan_bitmap))
88 __vlan_delete_pvid(v, vid);
90 if (v->port_idx && vid) {
91 struct net_device *dev = v->parent.port->dev;
93 if (dev->features & NETIF_F_HW_VLAN_FILTER)
94 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
97 clear_bit(vid, v->vlan_bitmap);
99 if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
101 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
103 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
109 static void __vlan_flush(struct net_port_vlans *v)
113 bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
115 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
117 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
121 /* Strip the tag from the packet. Will return skb with tci set 0. */
122 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
124 if (skb->protocol != htons(ETH_P_8021Q)) {
130 skb = vlan_untag(skb);
137 struct sk_buff *br_handle_vlan(struct net_bridge *br,
138 const struct net_port_vlans *pv,
143 if (!br->vlan_enabled)
146 /* At this point, we know that the frame was filtered and contains
147 * a valid vlan id. If the vlan id matches the pvid of current port
148 * send untagged; otherwise, send taged.
150 br_vlan_get_tag(skb, &vid);
151 if (vid == br_get_pvid(pv))
152 skb = br_vlan_untag(skb);
154 /* Egress policy says "send tagged". If output device
155 * is the bridge, we need to add the VLAN header
156 * ourselves since we'll be going through the RX path.
157 * Sending to ports puts the frame on the TX path and
158 * we let dev_hard_start_xmit() add the header.
160 if (skb->protocol != htons(ETH_P_8021Q) &&
162 /* vlan_put_tag expects skb->data to point to
165 skb_push(skb, ETH_HLEN);
166 skb = __vlan_put_tag(skb, skb->vlan_tci);
169 /* put skb->data back to where it was */
170 skb_pull(skb, ETH_HLEN);
179 /* Called under RCU */
180 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
181 struct sk_buff *skb, u16 *vid)
183 /* If VLAN filtering is disabled on the bridge, all packets are
186 if (!br->vlan_enabled)
189 /* If there are no vlan in the permitted list, all packets are
195 if (br_vlan_get_tag(skb, vid)) {
196 u16 pvid = br_get_pvid(v);
198 /* Frame did not have a tag. See if pvid is set
199 * on this port. That tells us which vlan untagged
200 * traffic belongs to.
202 if (pvid == VLAN_N_VID)
205 /* PVID is set on this port. Any untagged ingress
206 * frame is considered to belong to this vlan.
208 __vlan_hwaccel_put_tag(skb, pvid);
212 /* Frame had a valid vlan tag. See if vlan is allowed */
213 if (test_bit(*vid, v->vlan_bitmap))
219 /* Called under RCU. */
220 bool br_allowed_egress(struct net_bridge *br,
221 const struct net_port_vlans *v,
222 const struct sk_buff *skb)
226 if (!br->vlan_enabled)
232 br_vlan_get_tag(skb, &vid);
233 if (test_bit(vid, v->vlan_bitmap))
239 /* Must be protected by RTNL */
240 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
242 struct net_port_vlans *pv = NULL;
247 pv = rtnl_dereference(br->vlan_info);
249 return __vlan_add(pv, vid, flags);
251 /* Create port vlan infomration
253 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
258 err = __vlan_add(pv, vid, flags);
262 rcu_assign_pointer(br->vlan_info, pv);
269 /* Must be protected by RTNL */
270 int br_vlan_delete(struct net_bridge *br, u16 vid)
272 struct net_port_vlans *pv;
276 pv = rtnl_dereference(br->vlan_info);
281 /* If the VID !=0 remove fdb for this vid. VID 0 is special
282 * in that it's the default and is always there in the fdb.
284 spin_lock_bh(&br->hash_lock);
285 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
286 spin_unlock_bh(&br->hash_lock);
293 void br_vlan_flush(struct net_bridge *br)
295 struct net_port_vlans *pv;
298 pv = rtnl_dereference(br->vlan_info);
305 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
308 return restart_syscall();
310 if (br->vlan_enabled == val)
313 br->vlan_enabled = val;
320 /* Must be protected by RTNL */
321 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
323 struct net_port_vlans *pv = NULL;
328 pv = rtnl_dereference(port->vlan_info);
330 return __vlan_add(pv, vid, flags);
332 /* Create port vlan infomration
334 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
340 pv->port_idx = port->port_no;
341 pv->parent.port = port;
342 err = __vlan_add(pv, vid, flags);
346 rcu_assign_pointer(port->vlan_info, pv);
354 /* Must be protected by RTNL */
355 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
357 struct net_port_vlans *pv;
361 pv = rtnl_dereference(port->vlan_info);
366 /* If the VID !=0 remove fdb for this vid. VID 0 is special
367 * in that it's the default and is always there in the fdb.
369 spin_lock_bh(&port->br->hash_lock);
370 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
371 spin_unlock_bh(&port->br->hash_lock);
374 return __vlan_del(pv, vid);
377 void nbp_vlan_flush(struct net_bridge_port *port)
379 struct net_port_vlans *pv;
383 pv = rtnl_dereference(port->vlan_info);
390 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
392 struct net_port_vlans *pv;
396 pv = rcu_dereference(port->vlan_info);
401 if (test_bit(vid, pv->vlan_bitmap))