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 __be16 vlan_proto
= skb
->vlan_proto
;
12 u16 vlan_id
= skb_vlan_tag_get_id(skb
);
13 struct net_device
*vlan_dev
;
14 struct vlan_pcpu_stats
*rx_stats
;
16 vlan_dev
= vlan_find_dev(skb
->dev
, vlan_proto
, vlan_id
);
20 skb
= *skbp
= skb_share_check(skb
, GFP_ATOMIC
);
24 if (unlikely(!(vlan_dev
->flags
& IFF_UP
))) {
31 if (unlikely(skb
->pkt_type
== PACKET_OTHERHOST
)) {
32 /* Our lower layer thinks this is not local, let's make sure.
33 * This allows the VLAN to have a different MAC than the
34 * underlying device, and still route correctly. */
35 if (ether_addr_equal_64bits(eth_hdr(skb
)->h_dest
, vlan_dev
->dev_addr
))
36 skb
->pkt_type
= PACKET_HOST
;
39 if (!(vlan_dev_priv(vlan_dev
)->flags
& VLAN_FLAG_REORDER_HDR
) &&
40 !netif_is_macvlan_port(vlan_dev
) &&
41 !netif_is_bridge_port(vlan_dev
)) {
42 unsigned int offset
= skb
->data
- skb_mac_header(skb
);
45 * vlan_insert_tag expect skb->data pointing to mac header.
46 * So change skb->data before calling it and change back to
47 * original position later
49 skb_push(skb
, offset
);
50 skb
= *skbp
= vlan_insert_tag(skb
, skb
->vlan_proto
,
54 skb_pull(skb
, offset
+ VLAN_HLEN
);
55 skb_reset_mac_len(skb
);
58 skb
->priority
= vlan_get_ingress_priority(vlan_dev
, skb
->vlan_tci
);
61 rx_stats
= this_cpu_ptr(vlan_dev_priv(vlan_dev
)->vlan_pcpu_stats
);
63 u64_stats_update_begin(&rx_stats
->syncp
);
64 rx_stats
->rx_packets
++;
65 rx_stats
->rx_bytes
+= skb
->len
;
66 if (skb
->pkt_type
== PACKET_MULTICAST
)
67 rx_stats
->rx_multicast
++;
68 u64_stats_update_end(&rx_stats
->syncp
);
73 /* Must be invoked with rcu_read_lock. */
74 struct net_device
*__vlan_find_dev_deep_rcu(struct net_device
*dev
,
75 __be16 vlan_proto
, u16 vlan_id
)
77 struct vlan_info
*vlan_info
= rcu_dereference(dev
->vlan_info
);
80 return vlan_group_get_device(&vlan_info
->grp
,
84 * Lower devices of master uppers (bonding, team) do not have
85 * grp assigned to themselves. Grp is assigned to upper device
88 struct net_device
*upper_dev
;
90 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
92 return __vlan_find_dev_deep_rcu(upper_dev
,
98 EXPORT_SYMBOL(__vlan_find_dev_deep_rcu
);
100 struct net_device
*vlan_dev_real_dev(const struct net_device
*dev
)
102 struct net_device
*ret
= vlan_dev_priv(dev
)->real_dev
;
104 while (is_vlan_dev(ret
))
105 ret
= vlan_dev_priv(ret
)->real_dev
;
109 EXPORT_SYMBOL(vlan_dev_real_dev
);
111 u16
vlan_dev_vlan_id(const struct net_device
*dev
)
113 return vlan_dev_priv(dev
)->vlan_id
;
115 EXPORT_SYMBOL(vlan_dev_vlan_id
);
117 __be16
vlan_dev_vlan_proto(const struct net_device
*dev
)
119 return vlan_dev_priv(dev
)->vlan_proto
;
121 EXPORT_SYMBOL(vlan_dev_vlan_proto
);
124 * vlan info and vid list
127 static void vlan_group_free(struct vlan_group
*grp
)
131 for (i
= 0; i
< VLAN_PROTO_NUM
; i
++)
132 for (j
= 0; j
< VLAN_GROUP_ARRAY_SPLIT_PARTS
; j
++)
133 kfree(grp
->vlan_devices_arrays
[i
][j
]);
136 static void vlan_info_free(struct vlan_info
*vlan_info
)
138 vlan_group_free(&vlan_info
->grp
);
142 static void vlan_info_rcu_free(struct rcu_head
*rcu
)
144 vlan_info_free(container_of(rcu
, struct vlan_info
, rcu
));
147 static struct vlan_info
*vlan_info_alloc(struct net_device
*dev
)
149 struct vlan_info
*vlan_info
;
151 vlan_info
= kzalloc(sizeof(struct vlan_info
), GFP_KERNEL
);
155 vlan_info
->real_dev
= dev
;
156 INIT_LIST_HEAD(&vlan_info
->vid_list
);
160 struct vlan_vid_info
{
161 struct list_head list
;
167 static bool vlan_hw_filter_capable(const struct net_device
*dev
,
168 const struct vlan_vid_info
*vid_info
)
170 if (vid_info
->proto
== htons(ETH_P_8021Q
) &&
171 dev
->features
& NETIF_F_HW_VLAN_CTAG_FILTER
)
173 if (vid_info
->proto
== htons(ETH_P_8021AD
) &&
174 dev
->features
& NETIF_F_HW_VLAN_STAG_FILTER
)
179 static struct vlan_vid_info
*vlan_vid_info_get(struct vlan_info
*vlan_info
,
180 __be16 proto
, u16 vid
)
182 struct vlan_vid_info
*vid_info
;
184 list_for_each_entry(vid_info
, &vlan_info
->vid_list
, list
) {
185 if (vid_info
->proto
== proto
&& vid_info
->vid
== vid
)
191 static struct vlan_vid_info
*vlan_vid_info_alloc(__be16 proto
, u16 vid
)
193 struct vlan_vid_info
*vid_info
;
195 vid_info
= kzalloc(sizeof(struct vlan_vid_info
), GFP_KERNEL
);
198 vid_info
->proto
= proto
;
204 static int __vlan_vid_add(struct vlan_info
*vlan_info
, __be16 proto
, u16 vid
,
205 struct vlan_vid_info
**pvid_info
)
207 struct net_device
*dev
= vlan_info
->real_dev
;
208 const struct net_device_ops
*ops
= dev
->netdev_ops
;
209 struct vlan_vid_info
*vid_info
;
212 vid_info
= vlan_vid_info_alloc(proto
, vid
);
216 if (vlan_hw_filter_capable(dev
, vid_info
)) {
217 if (netif_device_present(dev
))
218 err
= ops
->ndo_vlan_rx_add_vid(dev
, proto
, vid
);
226 list_add(&vid_info
->list
, &vlan_info
->vid_list
);
227 vlan_info
->nr_vids
++;
228 *pvid_info
= vid_info
;
232 int vlan_vid_add(struct net_device
*dev
, __be16 proto
, u16 vid
)
234 struct vlan_info
*vlan_info
;
235 struct vlan_vid_info
*vid_info
;
236 bool vlan_info_created
= false;
241 vlan_info
= rtnl_dereference(dev
->vlan_info
);
243 vlan_info
= vlan_info_alloc(dev
);
246 vlan_info_created
= true;
248 vid_info
= vlan_vid_info_get(vlan_info
, proto
, vid
);
250 err
= __vlan_vid_add(vlan_info
, proto
, vid
, &vid_info
);
252 goto out_free_vlan_info
;
254 vid_info
->refcount
++;
256 if (vlan_info_created
)
257 rcu_assign_pointer(dev
->vlan_info
, vlan_info
);
262 if (vlan_info_created
)
266 EXPORT_SYMBOL(vlan_vid_add
);
268 static void __vlan_vid_del(struct vlan_info
*vlan_info
,
269 struct vlan_vid_info
*vid_info
)
271 struct net_device
*dev
= vlan_info
->real_dev
;
272 const struct net_device_ops
*ops
= dev
->netdev_ops
;
273 __be16 proto
= vid_info
->proto
;
274 u16 vid
= vid_info
->vid
;
277 if (vlan_hw_filter_capable(dev
, vid_info
)) {
278 if (netif_device_present(dev
))
279 err
= ops
->ndo_vlan_rx_kill_vid(dev
, proto
, vid
);
283 pr_warn("failed to kill vid %04x/%d for device %s\n",
284 proto
, vid
, dev
->name
);
287 list_del(&vid_info
->list
);
289 vlan_info
->nr_vids
--;
292 void vlan_vid_del(struct net_device
*dev
, __be16 proto
, u16 vid
)
294 struct vlan_info
*vlan_info
;
295 struct vlan_vid_info
*vid_info
;
299 vlan_info
= rtnl_dereference(dev
->vlan_info
);
303 vid_info
= vlan_vid_info_get(vlan_info
, proto
, vid
);
306 vid_info
->refcount
--;
307 if (vid_info
->refcount
== 0) {
308 __vlan_vid_del(vlan_info
, vid_info
);
309 if (vlan_info
->nr_vids
== 0) {
310 RCU_INIT_POINTER(dev
->vlan_info
, NULL
);
311 call_rcu(&vlan_info
->rcu
, vlan_info_rcu_free
);
315 EXPORT_SYMBOL(vlan_vid_del
);
317 int vlan_vids_add_by_dev(struct net_device
*dev
,
318 const struct net_device
*by_dev
)
320 struct vlan_vid_info
*vid_info
;
321 struct vlan_info
*vlan_info
;
326 vlan_info
= rtnl_dereference(by_dev
->vlan_info
);
330 list_for_each_entry(vid_info
, &vlan_info
->vid_list
, list
) {
331 err
= vlan_vid_add(dev
, vid_info
->proto
, vid_info
->vid
);
338 list_for_each_entry_continue_reverse(vid_info
,
339 &vlan_info
->vid_list
,
341 vlan_vid_del(dev
, vid_info
->proto
, vid_info
->vid
);
346 EXPORT_SYMBOL(vlan_vids_add_by_dev
);
348 void vlan_vids_del_by_dev(struct net_device
*dev
,
349 const struct net_device
*by_dev
)
351 struct vlan_vid_info
*vid_info
;
352 struct vlan_info
*vlan_info
;
356 vlan_info
= rtnl_dereference(by_dev
->vlan_info
);
360 list_for_each_entry(vid_info
, &vlan_info
->vid_list
, list
)
361 vlan_vid_del(dev
, vid_info
->proto
, vid_info
->vid
);
363 EXPORT_SYMBOL(vlan_vids_del_by_dev
);
365 bool vlan_uses_dev(const struct net_device
*dev
)
367 struct vlan_info
*vlan_info
;
371 vlan_info
= rtnl_dereference(dev
->vlan_info
);
374 return vlan_info
->grp
.nr_vlan_devs
? true : false;
376 EXPORT_SYMBOL(vlan_uses_dev
);