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 if (p
&& (dev
->features
& NETIF_F_HW_VLAN_FILTER
)) {
58 /* Add VLAN to the device filter if it is supported.
59 * Stricly speaking, this is not necessary now, since
60 * devices are made promiscuous by the bridge, but if
61 * that ever changes this code will allow tagged
62 * traffic to enter the bridge.
64 err
= dev
->netdev_ops
->ndo_vlan_rx_add_vid(dev
, vid
);
69 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, vid
);
71 br_err(br
, "failed insert local address into bridge "
72 "forwarding table\n");
78 set_bit(vid
, v
->vlan_bitmap
);
80 __vlan_add_flags(v
, vid
, flags
);
85 if (p
&& (dev
->features
& NETIF_F_HW_VLAN_FILTER
))
86 dev
->netdev_ops
->ndo_vlan_rx_kill_vid(dev
, vid
);
90 static int __vlan_del(struct net_port_vlans
*v
, u16 vid
)
92 if (!test_bit(vid
, v
->vlan_bitmap
))
95 __vlan_delete_pvid(v
, vid
);
96 clear_bit(vid
, v
->untagged_bitmap
);
98 if (v
->port_idx
&& vid
) {
99 struct net_device
*dev
= v
->parent
.port
->dev
;
101 if (dev
->features
& NETIF_F_HW_VLAN_FILTER
)
102 dev
->netdev_ops
->ndo_vlan_rx_kill_vid(dev
, vid
);
105 clear_bit(vid
, v
->vlan_bitmap
);
107 if (bitmap_empty(v
->vlan_bitmap
, BR_VLAN_BITMAP_LEN
)) {
109 rcu_assign_pointer(v
->parent
.port
->vlan_info
, NULL
);
111 rcu_assign_pointer(v
->parent
.br
->vlan_info
, NULL
);
117 static void __vlan_flush(struct net_port_vlans
*v
)
121 bitmap_zero(v
->vlan_bitmap
, BR_VLAN_BITMAP_LEN
);
123 rcu_assign_pointer(v
->parent
.port
->vlan_info
, NULL
);
125 rcu_assign_pointer(v
->parent
.br
->vlan_info
, NULL
);
129 /* Strip the tag from the packet. Will return skb with tci set 0. */
130 static struct sk_buff
*br_vlan_untag(struct sk_buff
*skb
)
132 if (skb
->protocol
!= htons(ETH_P_8021Q
)) {
138 skb
= vlan_untag(skb
);
145 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
146 const struct net_port_vlans
*pv
,
151 if (!br
->vlan_enabled
)
154 /* At this point, we know that the frame was filtered and contains
155 * a valid vlan id. If the vlan id is set in the untagged bitmap,
156 * send untagged; otherwise, send taged.
158 br_vlan_get_tag(skb
, &vid
);
159 if (test_bit(vid
, pv
->untagged_bitmap
))
160 skb
= br_vlan_untag(skb
);
162 /* Egress policy says "send tagged". If output device
163 * is the bridge, we need to add the VLAN header
164 * ourselves since we'll be going through the RX path.
165 * Sending to ports puts the frame on the TX path and
166 * we let dev_hard_start_xmit() add the header.
168 if (skb
->protocol
!= htons(ETH_P_8021Q
) &&
170 /* vlan_put_tag expects skb->data to point to
173 skb_push(skb
, ETH_HLEN
);
174 skb
= __vlan_put_tag(skb
, skb
->vlan_tci
);
177 /* put skb->data back to where it was */
178 skb_pull(skb
, ETH_HLEN
);
187 /* Called under RCU */
188 bool br_allowed_ingress(struct net_bridge
*br
, struct net_port_vlans
*v
,
189 struct sk_buff
*skb
, u16
*vid
)
191 /* If VLAN filtering is disabled on the bridge, all packets are
194 if (!br
->vlan_enabled
)
197 /* If there are no vlan in the permitted list, all packets are
203 if (br_vlan_get_tag(skb
, vid
)) {
204 u16 pvid
= br_get_pvid(v
);
206 /* Frame did not have a tag. See if pvid is set
207 * on this port. That tells us which vlan untagged
208 * traffic belongs to.
210 if (pvid
== VLAN_N_VID
)
213 /* PVID is set on this port. Any untagged ingress
214 * frame is considered to belong to this vlan.
216 __vlan_hwaccel_put_tag(skb
, pvid
);
220 /* Frame had a valid vlan tag. See if vlan is allowed */
221 if (test_bit(*vid
, v
->vlan_bitmap
))
227 /* Called under RCU. */
228 bool br_allowed_egress(struct net_bridge
*br
,
229 const struct net_port_vlans
*v
,
230 const struct sk_buff
*skb
)
234 if (!br
->vlan_enabled
)
240 br_vlan_get_tag(skb
, &vid
);
241 if (test_bit(vid
, v
->vlan_bitmap
))
247 /* Must be protected by RTNL */
248 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
250 struct net_port_vlans
*pv
= NULL
;
255 pv
= rtnl_dereference(br
->vlan_info
);
257 return __vlan_add(pv
, vid
, flags
);
259 /* Create port vlan infomration
261 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
266 err
= __vlan_add(pv
, vid
, flags
);
270 rcu_assign_pointer(br
->vlan_info
, pv
);
277 /* Must be protected by RTNL */
278 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
280 struct net_port_vlans
*pv
;
284 pv
= rtnl_dereference(br
->vlan_info
);
289 /* If the VID !=0 remove fdb for this vid. VID 0 is special
290 * in that it's the default and is always there in the fdb.
292 spin_lock_bh(&br
->hash_lock
);
293 fdb_delete_by_addr(br
, br
->dev
->dev_addr
, vid
);
294 spin_unlock_bh(&br
->hash_lock
);
301 void br_vlan_flush(struct net_bridge
*br
)
303 struct net_port_vlans
*pv
;
306 pv
= rtnl_dereference(br
->vlan_info
);
313 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
316 return restart_syscall();
318 if (br
->vlan_enabled
== val
)
321 br
->vlan_enabled
= val
;
328 /* Must be protected by RTNL */
329 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
331 struct net_port_vlans
*pv
= NULL
;
336 pv
= rtnl_dereference(port
->vlan_info
);
338 return __vlan_add(pv
, vid
, flags
);
340 /* Create port vlan infomration
342 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
348 pv
->port_idx
= port
->port_no
;
349 pv
->parent
.port
= port
;
350 err
= __vlan_add(pv
, vid
, flags
);
354 rcu_assign_pointer(port
->vlan_info
, pv
);
362 /* Must be protected by RTNL */
363 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
365 struct net_port_vlans
*pv
;
369 pv
= rtnl_dereference(port
->vlan_info
);
374 /* If the VID !=0 remove fdb for this vid. VID 0 is special
375 * in that it's the default and is always there in the fdb.
377 spin_lock_bh(&port
->br
->hash_lock
);
378 fdb_delete_by_addr(port
->br
, port
->dev
->dev_addr
, vid
);
379 spin_unlock_bh(&port
->br
->hash_lock
);
382 return __vlan_del(pv
, vid
);
385 void nbp_vlan_flush(struct net_bridge_port
*port
)
387 struct net_port_vlans
*pv
;
391 pv
= rtnl_dereference(port
->vlan_info
);
398 bool nbp_vlan_find(struct net_bridge_port
*port
, u16 vid
)
400 struct net_port_vlans
*pv
;
404 pv
= rcu_dereference(port
->vlan_info
);
409 if (test_bit(vid
, pv
->vlan_bitmap
))