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);
56 ops = dev->netdev_ops;
58 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
59 /* Add VLAN to the device filter if it is supported.
60 * Stricly speaking, this is not necessary now, since
61 * devices are made promiscuous by the bridge, but if
62 * that ever changes this code will allow tagged
63 * traffic to enter the bridge.
65 err = ops->ndo_vlan_rx_add_vid(dev, htons(ETH_P_8021Q),
71 err = br_fdb_insert(br, p, dev->dev_addr, vid);
73 br_err(br, "failed insert local address into bridge "
74 "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_CTAG_FILTER))
86 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), 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);
99 struct net_device *dev = v->parent.port->dev;
100 const struct net_device_ops *ops = dev->netdev_ops;
102 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
103 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
106 clear_bit(vid, v->vlan_bitmap);
108 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
110 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
112 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
118 static void __vlan_flush(struct net_port_vlans *v)
122 bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
124 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
126 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
130 /* Strip the tag from the packet. Will return skb with tci set 0. */
131 static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
133 if (skb->protocol != htons(ETH_P_8021Q)) {
139 skb = vlan_untag(skb);
146 struct sk_buff *br_handle_vlan(struct net_bridge *br,
147 const struct net_port_vlans *pv,
152 if (!br->vlan_enabled)
155 /* At this point, we know that the frame was filtered and contains
156 * a valid vlan id. If the vlan id is set in the untagged bitmap,
157 * send untagged; otherwise, send taged.
159 br_vlan_get_tag(skb, &vid);
160 if (test_bit(vid, pv->untagged_bitmap))
161 skb = br_vlan_untag(skb);
163 /* Egress policy says "send tagged". If output device
164 * is the bridge, we need to add the VLAN header
165 * ourselves since we'll be going through the RX path.
166 * Sending to ports puts the frame on the TX path and
167 * we let dev_hard_start_xmit() add the header.
169 if (skb->protocol != htons(ETH_P_8021Q) &&
171 /* vlan_put_tag expects skb->data to point to
174 skb_push(skb, ETH_HLEN);
175 skb = __vlan_put_tag(skb, skb->vlan_proto, skb->vlan_tci);
178 /* put skb->data back to where it was */
179 skb_pull(skb, ETH_HLEN);
188 /* Called under RCU */
189 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
190 struct sk_buff *skb, u16 *vid)
194 /* If VLAN filtering is disabled on the bridge, all packets are
197 if (!br->vlan_enabled)
200 /* If there are no vlan in the permitted list, all packets are
206 err = br_vlan_get_tag(skb, vid);
208 u16 pvid = br_get_pvid(v);
210 /* Frame had a tag with VID 0 or did not have a tag.
211 * See if pvid is set on this port. That tells us which
212 * vlan untagged or priority-tagged traffic belongs to.
214 if (pvid == VLAN_N_VID)
217 /* PVID is set on this port. Any untagged or priority-tagged
218 * ingress frame is considered to belong to this vlan.
221 /* Untagged Frame. */
222 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
224 /* Priority-tagged Frame.
225 * At this point, We know that skb->vlan_tci had
226 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
227 * We update only VID field and preserve PCP field.
229 skb->vlan_tci |= pvid;
234 /* Frame had a valid vlan tag. See if vlan is allowed */
235 if (test_bit(*vid, v->vlan_bitmap))
241 /* Called under RCU. */
242 bool br_allowed_egress(struct net_bridge *br,
243 const struct net_port_vlans *v,
244 const struct sk_buff *skb)
248 if (!br->vlan_enabled)
254 br_vlan_get_tag(skb, &vid);
255 if (test_bit(vid, v->vlan_bitmap))
261 /* Must be protected by RTNL.
262 * Must be called with vid in range from 1 to 4094 inclusive.
264 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
266 struct net_port_vlans *pv = NULL;
271 pv = rtnl_dereference(br->vlan_info);
273 return __vlan_add(pv, vid, flags);
275 /* Create port vlan infomration
277 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
282 err = __vlan_add(pv, vid, flags);
286 rcu_assign_pointer(br->vlan_info, pv);
293 /* Must be protected by RTNL.
294 * Must be called with vid in range from 1 to 4094 inclusive.
296 int br_vlan_delete(struct net_bridge *br, u16 vid)
298 struct net_port_vlans *pv;
302 pv = rtnl_dereference(br->vlan_info);
306 spin_lock_bh(&br->hash_lock);
307 fdb_delete_by_addr(br, br->dev->dev_addr, vid);
308 spin_unlock_bh(&br->hash_lock);
314 void br_vlan_flush(struct net_bridge *br)
316 struct net_port_vlans *pv;
319 pv = rtnl_dereference(br->vlan_info);
326 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
329 return restart_syscall();
331 if (br->vlan_enabled == val)
334 br->vlan_enabled = val;
341 /* Must be protected by RTNL.
342 * Must be called with vid in range from 1 to 4094 inclusive.
344 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
346 struct net_port_vlans *pv = NULL;
351 pv = rtnl_dereference(port->vlan_info);
353 return __vlan_add(pv, vid, flags);
355 /* Create port vlan infomration
357 pv = kzalloc(sizeof(*pv), GFP_KERNEL);
363 pv->port_idx = port->port_no;
364 pv->parent.port = port;
365 err = __vlan_add(pv, vid, flags);
369 rcu_assign_pointer(port->vlan_info, pv);
377 /* Must be protected by RTNL.
378 * Must be called with vid in range from 1 to 4094 inclusive.
380 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
382 struct net_port_vlans *pv;
386 pv = rtnl_dereference(port->vlan_info);
390 spin_lock_bh(&port->br->hash_lock);
391 fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
392 spin_unlock_bh(&port->br->hash_lock);
394 return __vlan_del(pv, vid);
397 void nbp_vlan_flush(struct net_bridge_port *port)
399 struct net_port_vlans *pv;
403 pv = rtnl_dereference(port->vlan_info);
410 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
412 struct net_port_vlans *pv;
416 pv = rcu_dereference(port->vlan_info);
421 if (test_bit(vid, pv->vlan_bitmap))