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)
30 if (test_bit(vid, v->vlan_bitmap)) {
31 if (flags & BRIDGE_VLAN_INFO_PVID)
32 __vlan_add_pvid(v, vid);
36 if (v->port_idx && vid) {
37 struct net_device *dev = v->parent.port->dev;
39 /* Add VLAN to the device filter if it is supported.
40 * Stricly speaking, this is not necessary now, since devices
41 * are made promiscuous by the bridge, but if that ever changes
42 * this code will allow tagged traffic to enter the bridge.
44 if (dev->features & NETIF_F_HW_VLAN_FILTER) {
45 err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
51 set_bit(vid, v->vlan_bitmap);
53 if (flags & BRIDGE_VLAN_INFO_PVID)
54 __vlan_add_pvid(v, vid);
59 static int __vlan_del(struct net_port_vlans *v, u16 vid)
61 if (!test_bit(vid, v->vlan_bitmap))
64 __vlan_delete_pvid(v, vid);
66 if (v->port_idx && vid) {
67 struct net_device *dev = v->parent.port->dev;
69 if (dev->features & NETIF_F_HW_VLAN_FILTER)
70 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
73 clear_bit(vid, v->vlan_bitmap);
75 if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
77 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
79 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
85 static void __vlan_flush(struct net_port_vlans *v)
89 bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
91 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
93 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
97 /* Strip the tag from the packet. Will return skb with tci set 0. */
98 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
100 if (skb->protocol != htons(ETH_P_8021Q)) {
106 skb = vlan_untag(skb);
113 struct sk_buff *br_handle_vlan(struct net_bridge *br,
114 const struct net_port_vlans *pv,
119 if (!br->vlan_enabled)
122 /* At this point, we know that the frame was filtered and contains
123 * a valid vlan id. If the vlan id matches the pvid of current port
124 * send untagged; otherwise, send taged.
126 br_vlan_get_tag(skb, &vid);
127 if (vid == br_get_pvid(pv))
128 skb = br_vlan_untag(skb);
130 /* Egress policy says "send tagged". If output device
131 * is the bridge, we need to add the VLAN header
132 * ourselves since we'll be going through the RX path.
133 * Sending to ports puts the frame on the TX path and
134 * we let dev_hard_start_xmit() add the header.
136 if (skb->protocol != htons(ETH_P_8021Q) &&
138 /* vlan_put_tag expects skb->data to point to
141 skb_push(skb, ETH_HLEN);
142 skb = __vlan_put_tag(skb, skb->vlan_tci);
145 /* put skb->data back to where it was */
146 skb_pull(skb, ETH_HLEN);
155 /* Called under RCU */
156 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
157 struct sk_buff *skb, u16 *vid)
159 /* If VLAN filtering is disabled on the bridge, all packets are
162 if (!br->vlan_enabled)
165 /* If there are no vlan in the permitted list, all packets are
171 if (br_vlan_get_tag(skb, vid)) {
172 u16 pvid = br_get_pvid(v);
174 /* Frame did not have a tag. See if pvid is set
175 * on this port. That tells us which vlan untagged
176 * traffic belongs to.
178 if (pvid == VLAN_N_VID)
181 /* PVID is set on this port. Any untagged ingress
182 * frame is considered to belong to this vlan.
184 __vlan_hwaccel_put_tag(skb, pvid);
188 /* Frame had a valid vlan tag. See if vlan is allowed */
189 if (test_bit(*vid, v->vlan_bitmap))
195 /* Called under RCU. */
196 bool br_allowed_egress(struct net_bridge *br,
197 const struct net_port_vlans *v,
198 const struct sk_buff *skb)
202 if (!br->vlan_enabled)
208 br_vlan_get_tag(skb, &vid);
209 if (test_bit(vid, v->vlan_bitmap))
215 /* Must be protected by RTNL */
216 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
218 struct net_port_vlans *pv = NULL;
223 pv = rtnl_dereference(br->vlan_info);
225 return __vlan_add(pv, vid, flags);
227 /* Create port vlan infomration
229 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
234 err = __vlan_add(pv, vid, flags);
238 rcu_assign_pointer(br->vlan_info, pv);
245 /* Must be protected by RTNL */
246 int br_vlan_delete(struct net_bridge *br, u16 vid)
248 struct net_port_vlans *pv;
252 pv = rtnl_dereference(br->vlan_info);
260 void br_vlan_flush(struct net_bridge *br)
262 struct net_port_vlans *pv;
265 pv = rtnl_dereference(br->vlan_info);
272 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
275 return restart_syscall();
277 if (br->vlan_enabled == val)
280 br->vlan_enabled = val;
287 /* Must be protected by RTNL */
288 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
290 struct net_port_vlans *pv = NULL;
295 pv = rtnl_dereference(port->vlan_info);
297 return __vlan_add(pv, vid, flags);
299 /* Create port vlan infomration
301 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
307 pv->port_idx = port->port_no;
308 pv->parent.port = port;
309 err = __vlan_add(pv, vid, flags);
313 rcu_assign_pointer(port->vlan_info, pv);
321 /* Must be protected by RTNL */
322 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
324 struct net_port_vlans *pv;
328 pv = rtnl_dereference(port->vlan_info);
332 return __vlan_del(pv, vid);
335 void nbp_vlan_flush(struct net_bridge_port *port)
337 struct net_port_vlans *pv;
341 pv = rtnl_dereference(port->vlan_info);