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 int __vlan_add(struct net_port_vlans *v, u16 vid)
12 if (test_bit(vid, v->vlan_bitmap))
15 if (v->port_idx && vid) {
16 struct net_device *dev = v->parent.port->dev;
18 /* Add VLAN to the device filter if it is supported.
19 * Stricly speaking, this is not necessary now, since devices
20 * are made promiscuous by the bridge, but if that ever changes
21 * this code will allow tagged traffic to enter the bridge.
23 if (dev->features & NETIF_F_HW_VLAN_FILTER) {
24 err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
30 set_bit(vid, v->vlan_bitmap);
35 static int __vlan_del(struct net_port_vlans *v, u16 vid)
37 if (!test_bit(vid, v->vlan_bitmap))
40 if (v->port_idx && vid) {
41 struct net_device *dev = v->parent.port->dev;
43 if (dev->features & NETIF_F_HW_VLAN_FILTER)
44 dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
47 clear_bit(vid, v->vlan_bitmap);
49 if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
51 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
53 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
59 static void __vlan_flush(struct net_port_vlans *v)
61 bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
63 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
65 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
69 /* Strip the tag from the packet. Will return skb with tci set 0. */
70 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
72 if (skb->protocol != htons(ETH_P_8021Q)) {
78 skb = vlan_untag(skb);
85 struct sk_buff *br_handle_vlan(struct net_bridge *br,
86 const struct net_port_vlans *pv,
91 if (!br->vlan_enabled)
94 /* At this point, we know that the frame was filtered and contains
95 * a valid vlan id. If the vlan id matches the pvid of current port
96 * send untagged; otherwise, send taged.
98 br_vlan_get_tag(skb, &vid);
99 if (vid == br_get_pvid(pv))
100 skb = br_vlan_untag(skb);
102 /* Egress policy says "send tagged". If output device
103 * is the bridge, we need to add the VLAN header
104 * ourselves since we'll be going through the RX path.
105 * Sending to ports puts the frame on the TX path and
106 * we let dev_hard_start_xmit() add the header.
108 if (skb->protocol != htons(ETH_P_8021Q) &&
110 /* vlan_put_tag expects skb->data to point to
113 skb_push(skb, ETH_HLEN);
114 skb = __vlan_put_tag(skb, skb->vlan_tci);
117 /* put skb->data back to where it was */
118 skb_pull(skb, ETH_HLEN);
127 /* Called under RCU */
128 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
129 struct sk_buff *skb, u16 *vid)
131 /* If VLAN filtering is disabled on the bridge, all packets are
134 if (!br->vlan_enabled)
137 /* If there are no vlan in the permitted list, all packets are
143 if (br_vlan_get_tag(skb, vid)) {
144 u16 pvid = br_get_pvid(v);
146 /* Frame did not have a tag. See if pvid is set
147 * on this port. That tells us which vlan untagged
148 * traffic belongs to.
150 if (pvid == VLAN_N_VID)
153 /* PVID is set on this port. Any untagged ingress
154 * frame is considered to belong to this vlan.
156 __vlan_hwaccel_put_tag(skb, pvid);
160 /* Frame had a valid vlan tag. See if vlan is allowed */
161 if (test_bit(*vid, v->vlan_bitmap))
167 /* Called under RCU. */
168 bool br_allowed_egress(struct net_bridge *br,
169 const struct net_port_vlans *v,
170 const struct sk_buff *skb)
174 if (!br->vlan_enabled)
180 br_vlan_get_tag(skb, &vid);
181 if (test_bit(vid, v->vlan_bitmap))
187 /* Must be protected by RTNL */
188 int br_vlan_add(struct net_bridge *br, u16 vid)
190 struct net_port_vlans *pv = NULL;
195 pv = rtnl_dereference(br->vlan_info);
197 return __vlan_add(pv, vid);
199 /* Create port vlan infomration
201 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
206 err = __vlan_add(pv, vid);
210 rcu_assign_pointer(br->vlan_info, pv);
217 /* Must be protected by RTNL */
218 int br_vlan_delete(struct net_bridge *br, u16 vid)
220 struct net_port_vlans *pv;
224 pv = rtnl_dereference(br->vlan_info);
232 void br_vlan_flush(struct net_bridge *br)
234 struct net_port_vlans *pv;
238 pv = rtnl_dereference(br->vlan_info);
245 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
248 return restart_syscall();
250 if (br->vlan_enabled == val)
253 br->vlan_enabled = val;
260 /* Must be protected by RTNL */
261 int nbp_vlan_add(struct net_bridge_port *port, u16 vid)
263 struct net_port_vlans *pv = NULL;
268 pv = rtnl_dereference(port->vlan_info);
270 return __vlan_add(pv, vid);
272 /* Create port vlan infomration
274 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
280 pv->port_idx = port->port_no;
281 pv->parent.port = port;
282 err = __vlan_add(pv, vid);
286 rcu_assign_pointer(port->vlan_info, pv);
294 /* Must be protected by RTNL */
295 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
297 struct net_port_vlans *pv;
301 pv = rtnl_dereference(port->vlan_info);
305 return __vlan_del(pv, vid);
308 void nbp_vlan_flush(struct net_bridge_port *port)
310 struct net_port_vlans *pv;
314 pv = rtnl_dereference(port->vlan_info);