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 const struct net_device_ops
*ops
;
38 struct net_bridge_port
*p
= NULL
;
39 struct net_bridge
*br
;
40 struct net_device
*dev
;
43 if (test_bit(vid
, v
->vlan_bitmap
)) {
44 __vlan_add_flags(v
, vid
, flags
);
57 ops
= dev
->netdev_ops
;
59 if (p
&& (dev
->features
& NETIF_F_HW_VLAN_CTAG_FILTER
)) {
60 /* Add VLAN to the device filter if it is supported.
61 * Stricly speaking, this is not necessary now, since
62 * devices are made promiscuous by the bridge, but if
63 * that ever changes this code will allow tagged
64 * traffic to enter the bridge.
66 err
= ops
->ndo_vlan_rx_add_vid(dev
, htons(ETH_P_8021Q
),
72 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, vid
);
74 br_err(br
, "failed insert local address into bridge "
75 "forwarding table\n");
81 set_bit(vid
, v
->vlan_bitmap
);
83 __vlan_add_flags(v
, vid
, flags
);
88 if (p
&& (dev
->features
& NETIF_F_HW_VLAN_CTAG_FILTER
))
89 ops
->ndo_vlan_rx_kill_vid(dev
, htons(ETH_P_8021Q
), vid
);
93 static int __vlan_del(struct net_port_vlans
*v
, u16 vid
)
95 if (!test_bit(vid
, v
->vlan_bitmap
))
98 __vlan_delete_pvid(v
, vid
);
99 clear_bit(vid
, v
->untagged_bitmap
);
101 if (v
->port_idx
&& vid
) {
102 struct net_device
*dev
= v
->parent
.port
->dev
;
103 const struct net_device_ops
*ops
= dev
->netdev_ops
;
105 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_FILTER
)
106 ops
->ndo_vlan_rx_kill_vid(dev
, htons(ETH_P_8021Q
), vid
);
109 clear_bit(vid
, v
->vlan_bitmap
);
111 if (bitmap_empty(v
->vlan_bitmap
, VLAN_N_VID
)) {
113 rcu_assign_pointer(v
->parent
.port
->vlan_info
, NULL
);
115 rcu_assign_pointer(v
->parent
.br
->vlan_info
, NULL
);
121 static void __vlan_flush(struct net_port_vlans
*v
)
125 bitmap_zero(v
->vlan_bitmap
, VLAN_N_VID
);
127 rcu_assign_pointer(v
->parent
.port
->vlan_info
, NULL
);
129 rcu_assign_pointer(v
->parent
.br
->vlan_info
, NULL
);
133 /* Strip the tag from the packet. Will return skb with tci set 0. */
134 static struct sk_buff
*br_vlan_untag(struct sk_buff
*skb
)
136 if (skb
->protocol
!= htons(ETH_P_8021Q
)) {
142 skb
= vlan_untag(skb
);
149 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
150 const struct net_port_vlans
*pv
,
155 if (!br
->vlan_enabled
)
158 /* At this point, we know that the frame was filtered and contains
159 * a valid vlan id. If the vlan id is set in the untagged bitmap,
160 * send untagged; otherwise, send taged.
162 br_vlan_get_tag(skb
, &vid
);
163 if (test_bit(vid
, pv
->untagged_bitmap
))
164 skb
= br_vlan_untag(skb
);
166 /* Egress policy says "send tagged". If output device
167 * is the bridge, we need to add the VLAN header
168 * ourselves since we'll be going through the RX path.
169 * Sending to ports puts the frame on the TX path and
170 * we let dev_hard_start_xmit() add the header.
172 if (skb
->protocol
!= htons(ETH_P_8021Q
) &&
174 /* vlan_put_tag expects skb->data to point to
177 skb_push(skb
, ETH_HLEN
);
178 skb
= __vlan_put_tag(skb
, skb
->vlan_proto
, skb
->vlan_tci
);
181 /* put skb->data back to where it was */
182 skb_pull(skb
, ETH_HLEN
);
191 /* Called under RCU */
192 bool br_allowed_ingress(struct net_bridge
*br
, struct net_port_vlans
*v
,
193 struct sk_buff
*skb
, u16
*vid
)
195 /* If VLAN filtering is disabled on the bridge, all packets are
198 if (!br
->vlan_enabled
)
201 /* If there are no vlan in the permitted list, all packets are
207 if (br_vlan_get_tag(skb
, vid
)) {
208 u16 pvid
= br_get_pvid(v
);
210 /* Frame did not have a tag. See if pvid is set
211 * on this port. That tells us which vlan untagged
212 * traffic belongs to.
214 if (pvid
== VLAN_N_VID
)
217 /* PVID is set on this port. Any untagged ingress
218 * frame is considered to belong to this vlan.
220 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), pvid
);
224 /* Frame had a valid vlan tag. See if vlan is allowed */
225 if (test_bit(*vid
, v
->vlan_bitmap
))
232 /* Called under RCU. */
233 bool br_allowed_egress(struct net_bridge
*br
,
234 const struct net_port_vlans
*v
,
235 const struct sk_buff
*skb
)
239 if (!br
->vlan_enabled
)
245 br_vlan_get_tag(skb
, &vid
);
246 if (test_bit(vid
, v
->vlan_bitmap
))
252 /* Must be protected by RTNL */
253 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
255 struct net_port_vlans
*pv
= NULL
;
260 pv
= rtnl_dereference(br
->vlan_info
);
262 return __vlan_add(pv
, vid
, flags
);
264 /* Create port vlan infomration
266 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
271 err
= __vlan_add(pv
, vid
, flags
);
275 rcu_assign_pointer(br
->vlan_info
, pv
);
282 /* Must be protected by RTNL */
283 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
285 struct net_port_vlans
*pv
;
289 pv
= rtnl_dereference(br
->vlan_info
);
294 /* If the VID !=0 remove fdb for this vid. VID 0 is special
295 * in that it's the default and is always there in the fdb.
297 spin_lock_bh(&br
->hash_lock
);
298 fdb_delete_by_addr(br
, br
->dev
->dev_addr
, vid
);
299 spin_unlock_bh(&br
->hash_lock
);
306 void br_vlan_flush(struct net_bridge
*br
)
308 struct net_port_vlans
*pv
;
311 pv
= rtnl_dereference(br
->vlan_info
);
318 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
321 return restart_syscall();
323 if (br
->vlan_enabled
== val
)
326 br
->vlan_enabled
= val
;
333 /* Must be protected by RTNL */
334 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
336 struct net_port_vlans
*pv
= NULL
;
341 pv
= rtnl_dereference(port
->vlan_info
);
343 return __vlan_add(pv
, vid
, flags
);
345 /* Create port vlan infomration
347 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
353 pv
->port_idx
= port
->port_no
;
354 pv
->parent
.port
= port
;
355 err
= __vlan_add(pv
, vid
, flags
);
359 rcu_assign_pointer(port
->vlan_info
, pv
);
367 /* Must be protected by RTNL */
368 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
370 struct net_port_vlans
*pv
;
374 pv
= rtnl_dereference(port
->vlan_info
);
379 /* If the VID !=0 remove fdb for this vid. VID 0 is special
380 * in that it's the default and is always there in the fdb.
382 spin_lock_bh(&port
->br
->hash_lock
);
383 fdb_delete_by_addr(port
->br
, port
->dev
->dev_addr
, vid
);
384 spin_unlock_bh(&port
->br
->hash_lock
);
387 return __vlan_del(pv
, vid
);
390 void nbp_vlan_flush(struct net_bridge_port
*port
)
392 struct net_port_vlans
*pv
;
396 pv
= rtnl_dereference(port
->vlan_info
);
403 bool nbp_vlan_find(struct net_bridge_port
*port
, u16 vid
)
405 struct net_port_vlans
*pv
;
409 pv
= rcu_dereference(port
->vlan_info
);
414 if (test_bit(vid
, pv
->vlan_bitmap
))