1 #include <linux/skbuff.h>
2 #include <linux/netdevice.h>
3 #include <linux/if_vlan.h>
4 #include <linux/netpoll.h>
5 #include <linux/export.h>
8 bool vlan_do_receive(struct sk_buff **skbp)
10 struct sk_buff *skb = *skbp;
11 u16 vlan_id = skb->vlan_tci & VLAN_VID_MASK;
12 struct net_device *vlan_dev;
13 struct vlan_pcpu_stats *rx_stats;
15 vlan_dev = vlan_find_dev(skb->dev, vlan_id);
19 skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
24 if (skb->pkt_type == PACKET_OTHERHOST) {
25 /* Our lower layer thinks this is not local, let's make sure.
26 * This allows the VLAN to have a different MAC than the
27 * underlying device, and still route correctly. */
28 if (ether_addr_equal(eth_hdr(skb)->h_dest, vlan_dev->dev_addr))
29 skb->pkt_type = PACKET_HOST;
32 if (!(vlan_dev_priv(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR)) {
33 unsigned int offset = skb->data - skb_mac_header(skb);
36 * vlan_insert_tag expect skb->data pointing to mac header.
37 * So change skb->data before calling it and change back to
38 * original position later
40 skb_push(skb, offset);
41 skb = *skbp = vlan_insert_tag(skb, skb->vlan_tci);
44 skb_pull(skb, offset + VLAN_HLEN);
45 skb_reset_mac_len(skb);
48 skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
51 rx_stats = this_cpu_ptr(vlan_dev_priv(vlan_dev)->vlan_pcpu_stats);
53 u64_stats_update_begin(&rx_stats->syncp);
54 rx_stats->rx_packets++;
55 rx_stats->rx_bytes += skb->len;
56 if (skb->pkt_type == PACKET_MULTICAST)
57 rx_stats->rx_multicast++;
58 u64_stats_update_end(&rx_stats->syncp);
63 /* Must be invoked with rcu_read_lock or with RTNL. */
64 struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
67 struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
70 return vlan_group_get_device(&vlan_info->grp, vlan_id);
73 * Bonding slaves do not have grp assigned to themselves.
74 * Grp is assigned to bonding master instead.
76 if (netif_is_bond_slave(real_dev))
77 return __vlan_find_dev_deep(real_dev->master, vlan_id);
82 EXPORT_SYMBOL(__vlan_find_dev_deep);
84 struct net_device *vlan_dev_real_dev(const struct net_device *dev)
86 return vlan_dev_priv(dev)->real_dev;
88 EXPORT_SYMBOL(vlan_dev_real_dev);
90 u16 vlan_dev_vlan_id(const struct net_device *dev)
92 return vlan_dev_priv(dev)->vlan_id;
94 EXPORT_SYMBOL(vlan_dev_vlan_id);
96 static struct sk_buff *vlan_reorder_header(struct sk_buff *skb)
98 if (skb_cow(skb, skb_headroom(skb)) < 0)
100 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
101 skb->mac_header += VLAN_HLEN;
105 struct sk_buff *vlan_untag(struct sk_buff *skb)
107 struct vlan_hdr *vhdr;
110 if (unlikely(vlan_tx_tag_present(skb))) {
111 /* vlan_tci is already set-up so leave this for another time */
115 skb = skb_share_check(skb, GFP_ATOMIC);
119 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
122 vhdr = (struct vlan_hdr *) skb->data;
123 vlan_tci = ntohs(vhdr->h_vlan_TCI);
124 __vlan_hwaccel_put_tag(skb, vlan_tci);
126 skb_pull_rcsum(skb, VLAN_HLEN);
127 vlan_set_encap_proto(skb, vhdr);
129 skb = vlan_reorder_header(skb);
133 skb_reset_network_header(skb);
134 skb_reset_transport_header(skb);
135 skb_reset_mac_len(skb);
146 * vlan info and vid list
149 static void vlan_group_free(struct vlan_group *grp)
153 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
154 kfree(grp->vlan_devices_arrays[i]);
157 static void vlan_info_free(struct vlan_info *vlan_info)
159 vlan_group_free(&vlan_info->grp);
163 static void vlan_info_rcu_free(struct rcu_head *rcu)
165 vlan_info_free(container_of(rcu, struct vlan_info, rcu));
168 static struct vlan_info *vlan_info_alloc(struct net_device *dev)
170 struct vlan_info *vlan_info;
172 vlan_info = kzalloc(sizeof(struct vlan_info), GFP_KERNEL);
176 vlan_info->real_dev = dev;
177 INIT_LIST_HEAD(&vlan_info->vid_list);
181 struct vlan_vid_info {
182 struct list_head list;
187 static struct vlan_vid_info *vlan_vid_info_get(struct vlan_info *vlan_info,
190 struct vlan_vid_info *vid_info;
192 list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
193 if (vid_info->vid == vid)
199 static struct vlan_vid_info *vlan_vid_info_alloc(unsigned short vid)
201 struct vlan_vid_info *vid_info;
203 vid_info = kzalloc(sizeof(struct vlan_vid_info), GFP_KERNEL);
211 static int __vlan_vid_add(struct vlan_info *vlan_info, unsigned short vid,
212 struct vlan_vid_info **pvid_info)
214 struct net_device *dev = vlan_info->real_dev;
215 const struct net_device_ops *ops = dev->netdev_ops;
216 struct vlan_vid_info *vid_info;
219 vid_info = vlan_vid_info_alloc(vid);
223 if ((dev->features & NETIF_F_HW_VLAN_FILTER) &&
224 ops->ndo_vlan_rx_add_vid) {
225 err = ops->ndo_vlan_rx_add_vid(dev, vid);
231 list_add(&vid_info->list, &vlan_info->vid_list);
232 vlan_info->nr_vids++;
233 *pvid_info = vid_info;
237 int vlan_vid_add(struct net_device *dev, unsigned short vid)
239 struct vlan_info *vlan_info;
240 struct vlan_vid_info *vid_info;
241 bool vlan_info_created = false;
246 vlan_info = rtnl_dereference(dev->vlan_info);
248 vlan_info = vlan_info_alloc(dev);
251 vlan_info_created = true;
253 vid_info = vlan_vid_info_get(vlan_info, vid);
255 err = __vlan_vid_add(vlan_info, vid, &vid_info);
257 goto out_free_vlan_info;
259 vid_info->refcount++;
261 if (vlan_info_created)
262 rcu_assign_pointer(dev->vlan_info, vlan_info);
267 if (vlan_info_created)
271 EXPORT_SYMBOL(vlan_vid_add);
273 static void __vlan_vid_del(struct vlan_info *vlan_info,
274 struct vlan_vid_info *vid_info)
276 struct net_device *dev = vlan_info->real_dev;
277 const struct net_device_ops *ops = dev->netdev_ops;
278 unsigned short vid = vid_info->vid;
281 if ((dev->features & NETIF_F_HW_VLAN_FILTER) &&
282 ops->ndo_vlan_rx_kill_vid) {
283 err = ops->ndo_vlan_rx_kill_vid(dev, vid);
285 pr_warn("failed to kill vid %d for device %s\n",
289 list_del(&vid_info->list);
291 vlan_info->nr_vids--;
294 void vlan_vid_del(struct net_device *dev, unsigned short vid)
296 struct vlan_info *vlan_info;
297 struct vlan_vid_info *vid_info;
301 vlan_info = rtnl_dereference(dev->vlan_info);
305 vid_info = vlan_vid_info_get(vlan_info, vid);
308 vid_info->refcount--;
309 if (vid_info->refcount == 0) {
310 __vlan_vid_del(vlan_info, vid_info);
311 if (vlan_info->nr_vids == 0) {
312 RCU_INIT_POINTER(dev->vlan_info, NULL);
313 call_rcu(&vlan_info->rcu, vlan_info_rcu_free);
317 EXPORT_SYMBOL(vlan_vid_del);
319 int vlan_vids_add_by_dev(struct net_device *dev,
320 const struct net_device *by_dev)
322 struct vlan_vid_info *vid_info;
323 struct vlan_info *vlan_info;
328 vlan_info = rtnl_dereference(by_dev->vlan_info);
332 list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
333 err = vlan_vid_add(dev, vid_info->vid);
340 list_for_each_entry_continue_reverse(vid_info,
341 &vlan_info->vid_list,
343 vlan_vid_del(dev, vid_info->vid);
348 EXPORT_SYMBOL(vlan_vids_add_by_dev);
350 void vlan_vids_del_by_dev(struct net_device *dev,
351 const struct net_device *by_dev)
353 struct vlan_vid_info *vid_info;
354 struct vlan_info *vlan_info;
358 vlan_info = rtnl_dereference(by_dev->vlan_info);
362 list_for_each_entry(vid_info, &vlan_info->vid_list, list)
363 vlan_vid_del(dev, vid_info->vid);
365 EXPORT_SYMBOL(vlan_vids_del_by_dev);
367 bool vlan_uses_dev(const struct net_device *dev)
369 return rtnl_dereference(dev->vlan_info) ? true : false;
371 EXPORT_SYMBOL(vlan_uses_dev);