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
, br
->vlan_proto
, 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
, br
->vlan_proto
, 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 struct net_bridge_port
*p
= v
->parent
.port
;
97 vlan_vid_del(p
->dev
, p
->br
->vlan_proto
, vid
);
100 clear_bit(vid
, v
->vlan_bitmap
);
102 if (bitmap_empty(v
->vlan_bitmap
, VLAN_N_VID
)) {
104 RCU_INIT_POINTER(v
->parent
.port
->vlan_info
, NULL
);
106 RCU_INIT_POINTER(v
->parent
.br
->vlan_info
, NULL
);
112 static void __vlan_flush(struct net_port_vlans
*v
)
116 bitmap_zero(v
->vlan_bitmap
, VLAN_N_VID
);
118 RCU_INIT_POINTER(v
->parent
.port
->vlan_info
, NULL
);
120 RCU_INIT_POINTER(v
->parent
.br
->vlan_info
, NULL
);
124 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
125 const struct net_port_vlans
*pv
,
130 /* If this packet was not filtered at input, let it pass */
131 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
134 /* Vlan filter table must be configured at this point. The
135 * only exception is the bridge is set in promisc mode and the
136 * packet is destined for the bridge device. In this case
137 * pass the packet as is.
140 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
148 /* At this point, we know that the frame was filtered and contains
149 * a valid vlan id. If the vlan id is set in the untagged bitmap,
150 * send untagged; otherwise, send tagged.
152 br_vlan_get_tag(skb
, &vid
);
153 if (test_bit(vid
, pv
->untagged_bitmap
))
160 /* Called under RCU */
161 bool br_allowed_ingress(struct net_bridge
*br
, struct net_port_vlans
*v
,
162 struct sk_buff
*skb
, u16
*vid
)
167 /* If VLAN filtering is disabled on the bridge, all packets are
170 if (!br
->vlan_enabled
) {
171 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
175 /* If there are no vlan in the permitted list, all packets are
181 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
182 proto
= br
->vlan_proto
;
184 /* If vlan tx offload is disabled on bridge device and frame was
185 * sent from vlan device on the bridge device, it does not have
186 * HW accelerated vlan tag.
188 if (unlikely(!vlan_tx_tag_present(skb
) &&
189 skb
->protocol
== proto
)) {
190 skb
= skb_vlan_untag(skb
);
195 if (!br_vlan_get_tag(skb
, vid
)) {
197 if (skb
->vlan_proto
!= proto
) {
198 /* Protocol-mismatch, empty out vlan_tci for new tag */
199 skb_push(skb
, ETH_HLEN
);
200 skb
= __vlan_put_tag(skb
, skb
->vlan_proto
,
201 vlan_tx_tag_get(skb
));
205 skb_pull(skb
, ETH_HLEN
);
206 skb_reset_mac_len(skb
);
218 u16 pvid
= br_get_pvid(v
);
220 /* Frame had a tag with VID 0 or did not have a tag.
221 * See if pvid is set on this port. That tells us which
222 * vlan untagged or priority-tagged traffic belongs to.
224 if (pvid
== VLAN_N_VID
)
227 /* PVID is set on this port. Any untagged or priority-tagged
228 * ingress frame is considered to belong to this vlan.
232 /* Untagged Frame. */
233 __vlan_hwaccel_put_tag(skb
, proto
, pvid
);
235 /* Priority-tagged Frame.
236 * At this point, We know that skb->vlan_tci had
237 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
238 * We update only VID field and preserve PCP field.
240 skb
->vlan_tci
|= pvid
;
245 /* Frame had a valid vlan tag. See if vlan is allowed */
246 if (test_bit(*vid
, v
->vlan_bitmap
))
253 /* Called under RCU. */
254 bool br_allowed_egress(struct net_bridge
*br
,
255 const struct net_port_vlans
*v
,
256 const struct sk_buff
*skb
)
260 /* If this packet was not filtered at input, let it pass */
261 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
267 br_vlan_get_tag(skb
, &vid
);
268 if (test_bit(vid
, v
->vlan_bitmap
))
274 /* Called under RCU */
275 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
277 struct net_bridge
*br
= p
->br
;
278 struct net_port_vlans
*v
;
280 /* If filtering was disabled at input, let it pass. */
281 if (!br
->vlan_enabled
)
284 v
= rcu_dereference(p
->vlan_info
);
288 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
292 *vid
= br_get_pvid(v
);
293 if (*vid
== VLAN_N_VID
)
299 if (test_bit(*vid
, v
->vlan_bitmap
))
305 /* Must be protected by RTNL.
306 * Must be called with vid in range from 1 to 4094 inclusive.
308 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
310 struct net_port_vlans
*pv
= NULL
;
315 pv
= rtnl_dereference(br
->vlan_info
);
317 return __vlan_add(pv
, vid
, flags
);
319 /* Create port vlan infomration
321 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
326 err
= __vlan_add(pv
, vid
, flags
);
330 rcu_assign_pointer(br
->vlan_info
, pv
);
337 /* Must be protected by RTNL.
338 * Must be called with vid in range from 1 to 4094 inclusive.
340 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
342 struct net_port_vlans
*pv
;
346 pv
= rtnl_dereference(br
->vlan_info
);
350 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
356 void br_vlan_flush(struct net_bridge
*br
)
358 struct net_port_vlans
*pv
;
361 pv
= rtnl_dereference(br
->vlan_info
);
368 bool br_vlan_find(struct net_bridge
*br
, u16 vid
)
370 struct net_port_vlans
*pv
;
374 pv
= rcu_dereference(br
->vlan_info
);
379 if (test_bit(vid
, pv
->vlan_bitmap
))
387 /* Must be protected by RTNL. */
388 static void recalculate_group_addr(struct net_bridge
*br
)
390 if (br
->group_addr_set
)
393 spin_lock_bh(&br
->lock
);
394 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
)) {
395 /* Bridge Group Address */
396 br
->group_addr
[5] = 0x00;
397 } else { /* vlan_enabled && ETH_P_8021AD */
398 /* Provider Bridge Group Address */
399 br
->group_addr
[5] = 0x08;
401 spin_unlock_bh(&br
->lock
);
404 /* Must be protected by RTNL. */
405 void br_recalculate_fwd_mask(struct net_bridge
*br
)
407 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
))
408 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
409 else /* vlan_enabled && ETH_P_8021AD */
410 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
411 ~(1u << br
->group_addr
[5]);
414 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
417 return restart_syscall();
419 if (br
->vlan_enabled
== val
)
422 br
->vlan_enabled
= val
;
423 br_manage_promisc(br
);
424 recalculate_group_addr(br
);
425 br_recalculate_fwd_mask(br
);
432 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
435 struct net_bridge_port
*p
;
436 struct net_port_vlans
*pv
;
437 __be16 proto
, oldproto
;
440 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
441 return -EPROTONOSUPPORT
;
444 return restart_syscall();
447 if (br
->vlan_proto
== proto
)
450 /* Add VLANs for the new proto to the device filter. */
451 list_for_each_entry(p
, &br
->port_list
, list
) {
452 pv
= rtnl_dereference(p
->vlan_info
);
456 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
457 err
= vlan_vid_add(p
->dev
, proto
, vid
);
463 oldproto
= br
->vlan_proto
;
464 br
->vlan_proto
= proto
;
466 recalculate_group_addr(br
);
467 br_recalculate_fwd_mask(br
);
469 /* Delete VLANs for the old proto from the device filter. */
470 list_for_each_entry(p
, &br
->port_list
, list
) {
471 pv
= rtnl_dereference(p
->vlan_info
);
475 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
476 vlan_vid_del(p
->dev
, oldproto
, vid
);
485 for_each_set_bit(vid
, pv
->vlan_bitmap
, errvid
)
486 vlan_vid_del(p
->dev
, proto
, vid
);
488 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
489 pv
= rtnl_dereference(p
->vlan_info
);
493 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
494 vlan_vid_del(p
->dev
, proto
, vid
);
500 void br_vlan_init(struct net_bridge
*br
)
502 br
->vlan_proto
= htons(ETH_P_8021Q
);
505 /* Must be protected by RTNL.
506 * Must be called with vid in range from 1 to 4094 inclusive.
508 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
510 struct net_port_vlans
*pv
= NULL
;
515 pv
= rtnl_dereference(port
->vlan_info
);
517 return __vlan_add(pv
, vid
, flags
);
519 /* Create port vlan infomration
521 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
527 pv
->port_idx
= port
->port_no
;
528 pv
->parent
.port
= port
;
529 err
= __vlan_add(pv
, vid
, flags
);
533 rcu_assign_pointer(port
->vlan_info
, pv
);
541 /* Must be protected by RTNL.
542 * Must be called with vid in range from 1 to 4094 inclusive.
544 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
546 struct net_port_vlans
*pv
;
550 pv
= rtnl_dereference(port
->vlan_info
);
554 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
556 return __vlan_del(pv
, vid
);
559 void nbp_vlan_flush(struct net_bridge_port
*port
)
561 struct net_port_vlans
*pv
;
566 pv
= rtnl_dereference(port
->vlan_info
);
570 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
571 vlan_vid_del(port
->dev
, port
->br
->vlan_proto
, vid
);
576 bool nbp_vlan_find(struct net_bridge_port
*port
, u16 vid
)
578 struct net_port_vlans
*pv
;
582 pv
= rcu_dereference(port
->vlan_info
);
587 if (test_bit(vid
, pv
->vlan_bitmap
))