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
, htons(ETH_P_8021Q
), 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
, htons(ETH_P_8021Q
), 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 vlan_vid_del(v
->parent
.port
->dev
, htons(ETH_P_8021Q
), vid
);
98 clear_bit(vid
, v
->vlan_bitmap
);
100 if (bitmap_empty(v
->vlan_bitmap
, VLAN_N_VID
)) {
102 RCU_INIT_POINTER(v
->parent
.port
->vlan_info
, NULL
);
104 RCU_INIT_POINTER(v
->parent
.br
->vlan_info
, NULL
);
110 static void __vlan_flush(struct net_port_vlans
*v
)
114 bitmap_zero(v
->vlan_bitmap
, VLAN_N_VID
);
116 RCU_INIT_POINTER(v
->parent
.port
->vlan_info
, NULL
);
118 RCU_INIT_POINTER(v
->parent
.br
->vlan_info
, NULL
);
122 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
123 const struct net_port_vlans
*pv
,
128 if (!br
->vlan_enabled
)
131 /* Vlan filter table must be configured at this point. The
132 * only exception is the bridge is set in promisc mode and the
133 * packet is destined for the bridge device. In this case
134 * pass the packet as is.
137 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
145 /* At this point, we know that the frame was filtered and contains
146 * a valid vlan id. If the vlan id is set in the untagged bitmap,
147 * send untagged; otherwise, send tagged.
149 br_vlan_get_tag(skb
, &vid
);
150 if (test_bit(vid
, pv
->untagged_bitmap
))
157 /* Called under RCU */
158 bool br_allowed_ingress(struct net_bridge
*br
, struct net_port_vlans
*v
,
159 struct sk_buff
*skb
, u16
*vid
)
163 /* If VLAN filtering is disabled on the bridge, all packets are
166 if (!br
->vlan_enabled
)
169 /* If there are no vlan in the permitted list, all packets are
175 /* If vlan tx offload is disabled on bridge device and frame was
176 * sent from vlan device on the bridge device, it does not have
177 * HW accelerated vlan tag.
179 if (unlikely(!vlan_tx_tag_present(skb
) &&
180 (skb
->protocol
== htons(ETH_P_8021Q
) ||
181 skb
->protocol
== htons(ETH_P_8021AD
)))) {
182 skb
= vlan_untag(skb
);
187 err
= br_vlan_get_tag(skb
, vid
);
189 u16 pvid
= br_get_pvid(v
);
191 /* Frame had a tag with VID 0 or did not have a tag.
192 * See if pvid is set on this port. That tells us which
193 * vlan untagged or priority-tagged traffic belongs to.
195 if (pvid
== VLAN_N_VID
)
198 /* PVID is set on this port. Any untagged or priority-tagged
199 * ingress frame is considered to belong to this vlan.
203 /* Untagged Frame. */
204 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), pvid
);
206 /* Priority-tagged Frame.
207 * At this point, We know that skb->vlan_tci had
208 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
209 * We update only VID field and preserve PCP field.
211 skb
->vlan_tci
|= pvid
;
216 /* Frame had a valid vlan tag. See if vlan is allowed */
217 if (test_bit(*vid
, v
->vlan_bitmap
))
224 /* Called under RCU. */
225 bool br_allowed_egress(struct net_bridge
*br
,
226 const struct net_port_vlans
*v
,
227 const struct sk_buff
*skb
)
231 if (!br
->vlan_enabled
)
237 br_vlan_get_tag(skb
, &vid
);
238 if (test_bit(vid
, v
->vlan_bitmap
))
244 /* Called under RCU */
245 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
247 struct net_bridge
*br
= p
->br
;
248 struct net_port_vlans
*v
;
250 if (!br
->vlan_enabled
)
253 v
= rcu_dereference(p
->vlan_info
);
257 br_vlan_get_tag(skb
, vid
);
259 *vid
= br_get_pvid(v
);
260 if (*vid
== VLAN_N_VID
)
266 if (test_bit(*vid
, v
->vlan_bitmap
))
272 /* Must be protected by RTNL.
273 * Must be called with vid in range from 1 to 4094 inclusive.
275 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
277 struct net_port_vlans
*pv
= NULL
;
282 pv
= rtnl_dereference(br
->vlan_info
);
284 return __vlan_add(pv
, vid
, flags
);
286 /* Create port vlan infomration
288 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
293 err
= __vlan_add(pv
, vid
, flags
);
297 rcu_assign_pointer(br
->vlan_info
, pv
);
304 /* Must be protected by RTNL.
305 * Must be called with vid in range from 1 to 4094 inclusive.
307 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
309 struct net_port_vlans
*pv
;
313 pv
= rtnl_dereference(br
->vlan_info
);
317 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
323 void br_vlan_flush(struct net_bridge
*br
)
325 struct net_port_vlans
*pv
;
328 pv
= rtnl_dereference(br
->vlan_info
);
335 bool br_vlan_find(struct net_bridge
*br
, u16 vid
)
337 struct net_port_vlans
*pv
;
341 pv
= rcu_dereference(br
->vlan_info
);
346 if (test_bit(vid
, pv
->vlan_bitmap
))
354 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
357 return restart_syscall();
359 if (br
->vlan_enabled
== val
)
362 br
->vlan_enabled
= val
;
369 /* Must be protected by RTNL.
370 * Must be called with vid in range from 1 to 4094 inclusive.
372 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
374 struct net_port_vlans
*pv
= NULL
;
379 pv
= rtnl_dereference(port
->vlan_info
);
381 return __vlan_add(pv
, vid
, flags
);
383 /* Create port vlan infomration
385 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
391 pv
->port_idx
= port
->port_no
;
392 pv
->parent
.port
= port
;
393 err
= __vlan_add(pv
, vid
, flags
);
397 rcu_assign_pointer(port
->vlan_info
, pv
);
405 /* Must be protected by RTNL.
406 * Must be called with vid in range from 1 to 4094 inclusive.
408 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
410 struct net_port_vlans
*pv
;
414 pv
= rtnl_dereference(port
->vlan_info
);
418 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
420 return __vlan_del(pv
, vid
);
423 void nbp_vlan_flush(struct net_bridge_port
*port
)
425 struct net_port_vlans
*pv
;
430 pv
= rtnl_dereference(port
->vlan_info
);
434 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
435 vlan_vid_del(port
->dev
, htons(ETH_P_8021Q
), vid
);
440 bool nbp_vlan_find(struct net_bridge_port
*port
, u16 vid
)
442 struct net_port_vlans
*pv
;
446 pv
= rcu_dereference(port
->vlan_info
);
451 if (test_bit(vid
, pv
->vlan_bitmap
))