xref: /linux/include/linux/if_vlan.h (revision f2c53ea949c5048f96b3dbb5a5ee7131ce4ff2de)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * VLAN		An implementation of 802.1Q VLAN tagging.
4  *
5  * Authors:	Ben Greear <greearb@candelatech.com>
6  */
7 #ifndef _LINUX_IF_VLAN_H_
8 #define _LINUX_IF_VLAN_H_
9 
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/rtnetlink.h>
13 #include <linux/bug.h>
14 #include <uapi/linux/if_vlan.h>
15 
16 #define VLAN_HLEN	4		/* The additional bytes required by VLAN
17 					 * (in addition to the Ethernet header)
18 					 */
19 #define VLAN_ETH_HLEN	18		/* Total octets in header.	 */
20 #define VLAN_ETH_ZLEN	64		/* Min. octets in frame sans FCS */
21 
22 /*
23  * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
24  */
25 #define VLAN_ETH_DATA_LEN	1500	/* Max. octets in payload	 */
26 #define VLAN_ETH_FRAME_LEN	1518	/* Max. octets in frame sans FCS */
27 
28 #define VLAN_MAX_DEPTH	8		/* Max. number of nested VLAN tags parsed */
29 
30 /*
31  * 	struct vlan_hdr - vlan header
32  * 	@h_vlan_TCI: priority and VLAN ID
33  *	@h_vlan_encapsulated_proto: packet type ID or len
34  */
35 struct vlan_hdr {
36 	__be16	h_vlan_TCI;
37 	__be16	h_vlan_encapsulated_proto;
38 };
39 
40 /**
41  *	struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
42  *	@h_dest: destination ethernet address
43  *	@h_source: source ethernet address
44  *	@h_vlan_proto: ethernet protocol
45  *	@h_vlan_TCI: priority and VLAN ID
46  *	@h_vlan_encapsulated_proto: packet type ID or len
47  */
48 struct vlan_ethhdr {
49 	struct_group(addrs,
50 		unsigned char	h_dest[ETH_ALEN];
51 		unsigned char	h_source[ETH_ALEN];
52 	);
53 	__be16		h_vlan_proto;
54 	__be16		h_vlan_TCI;
55 	__be16		h_vlan_encapsulated_proto;
56 };
57 
58 #include <linux/skbuff.h>
59 
vlan_eth_hdr(const struct sk_buff * skb)60 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
61 {
62 	return (struct vlan_ethhdr *)skb_mac_header(skb);
63 }
64 
65 /* Prefer this version in TX path, instead of
66  * skb_reset_mac_header() + vlan_eth_hdr()
67  */
skb_vlan_eth_hdr(const struct sk_buff * skb)68 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
69 {
70 	return (struct vlan_ethhdr *)skb->data;
71 }
72 
73 #define VLAN_PRIO_MASK		0xe000 /* Priority Code Point */
74 #define VLAN_PRIO_SHIFT		13
75 #define VLAN_CFI_MASK		0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
76 #define VLAN_VID_MASK		0x0fff /* VLAN Identifier */
77 #define VLAN_N_VID		4096
78 
79 /* found in socket.c */
80 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
81 
82 #define skb_vlan_tag_present(__skb)	(!!(__skb)->vlan_all)
83 #define skb_vlan_tag_get(__skb)		((__skb)->vlan_tci)
84 #define skb_vlan_tag_get_id(__skb)	((__skb)->vlan_tci & VLAN_VID_MASK)
85 #define skb_vlan_tag_get_cfi(__skb)	(!!((__skb)->vlan_tci & VLAN_CFI_MASK))
86 #define skb_vlan_tag_get_prio(__skb)	(((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
87 
vlan_get_rx_ctag_filter_info(struct net_device * dev)88 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
89 {
90 	ASSERT_RTNL();
91 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
92 }
93 
vlan_drop_rx_ctag_filter_info(struct net_device * dev)94 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
95 {
96 	ASSERT_RTNL();
97 	call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
98 }
99 
vlan_get_rx_stag_filter_info(struct net_device * dev)100 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
101 {
102 	ASSERT_RTNL();
103 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
104 }
105 
vlan_drop_rx_stag_filter_info(struct net_device * dev)106 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
107 {
108 	ASSERT_RTNL();
109 	call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
110 }
111 
112 /**
113  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
114  *	@rx_packets: number of received packets
115  *	@rx_bytes: number of received bytes
116  *	@rx_multicast: number of received multicast packets
117  *	@tx_packets: number of transmitted packets
118  *	@tx_bytes: number of transmitted bytes
119  *	@syncp: synchronization point for 64bit counters
120  *	@rx_errors: number of rx errors
121  *	@tx_dropped: number of tx drops
122  */
123 struct vlan_pcpu_stats {
124 	u64_stats_t		rx_packets;
125 	u64_stats_t		rx_bytes;
126 	u64_stats_t		rx_multicast;
127 	u64_stats_t		tx_packets;
128 	u64_stats_t		tx_bytes;
129 	struct u64_stats_sync	syncp;
130 	u32			rx_errors;
131 	u32			tx_dropped;
132 };
133 
134 #if IS_ENABLED(CONFIG_VLAN_8021Q)
135 
136 extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
137 					       __be16 vlan_proto, u16 vlan_id);
138 extern int vlan_for_each(struct net_device *dev,
139 			 int (*action)(struct net_device *dev, int vid,
140 				       void *arg), void *arg);
141 extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
142 extern u16 vlan_dev_vlan_id(const struct net_device *dev);
143 extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
144 
145 /**
146  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
147  *	@priority: skb priority
148  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
149  *	@next: pointer to next struct
150  *	@rcu: used for deferred freeing of mapping nodes
151  */
152 struct vlan_priority_tci_mapping {
153 	u32					priority;
154 	u16					vlan_qos;
155 	struct vlan_priority_tci_mapping __rcu	*next;
156 	struct rcu_head			rcu;
157 };
158 
159 struct proc_dir_entry;
160 struct netpoll;
161 
162 /**
163  *	struct vlan_dev_priv - VLAN private device data
164  *	@nr_ingress_mappings: number of ingress priority mappings
165  *	@ingress_priority_map: ingress priority mappings
166  *	@nr_egress_mappings: number of egress priority mappings
167  *	@egress_priority_map: hash of egress priority mappings
168  *	@vlan_proto: VLAN encapsulation protocol
169  *	@vlan_id: VLAN identifier
170  *	@flags: device flags
171  *	@real_dev: underlying netdevice
172  *	@dev_tracker: refcount tracker for @real_dev reference
173  *	@real_dev_addr: address of underlying netdevice
174  *	@dent: proc dir entry
175  *	@vlan_pcpu_stats: ptr to percpu rx stats
176  *	@netpoll: netpoll instance "propagated" down to @real_dev
177  */
178 struct vlan_dev_priv {
179 	unsigned int				nr_ingress_mappings;
180 	u32					ingress_priority_map[8];
181 	unsigned int				nr_egress_mappings;
182 	struct vlan_priority_tci_mapping __rcu	*egress_priority_map[16];
183 
184 	__be16					vlan_proto;
185 	u16					vlan_id;
186 	u16					flags;
187 
188 	struct net_device			*real_dev;
189 	netdevice_tracker			dev_tracker;
190 
191 	unsigned char				real_dev_addr[ETH_ALEN];
192 
193 	struct proc_dir_entry			*dent;
194 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
195 #ifdef CONFIG_NET_POLL_CONTROLLER
196 	struct netpoll				*netpoll;
197 #endif
198 };
199 
is_vlan_dev(const struct net_device * dev)200 static inline bool is_vlan_dev(const struct net_device *dev)
201 {
202 	return dev->priv_flags & IFF_802_1Q_VLAN;
203 }
204 
vlan_dev_priv(const struct net_device * dev)205 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
206 {
207 	return netdev_priv(dev);
208 }
209 
210 static inline u16
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)211 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
212 {
213 	struct vlan_priority_tci_mapping *mp;
214 	u16 vlan_qos = 0;
215 
216 	rcu_read_lock();
217 
218 	mp = rcu_dereference(vlan_dev_priv(dev)->egress_priority_map[skprio & 0xF]);
219 	while (mp) {
220 		if (mp->priority == skprio) {
221 			vlan_qos = READ_ONCE(mp->vlan_qos);
222 			break;
223 		}
224 		mp = rcu_dereference(mp->next);
225 	}
226 	rcu_read_unlock();
227 
228 	/* This should already be shifted to mask correctly with
229 	 * the VLAN's TCI.
230 	 */
231 	return vlan_qos;
232 }
233 
234 extern bool vlan_do_receive(struct sk_buff **skb);
235 
236 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
237 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
238 
239 extern int vlan_vids_add_by_dev(struct net_device *dev,
240 				const struct net_device *by_dev);
241 extern void vlan_vids_del_by_dev(struct net_device *dev,
242 				 const struct net_device *by_dev);
243 
244 extern bool vlan_uses_dev(const struct net_device *dev);
245 
246 #else
is_vlan_dev(const struct net_device * dev)247 static inline bool is_vlan_dev(const struct net_device *dev)
248 {
249 	return false;
250 }
251 
252 static inline struct net_device *
__vlan_find_dev_deep_rcu(struct net_device * real_dev,__be16 vlan_proto,u16 vlan_id)253 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
254 		     __be16 vlan_proto, u16 vlan_id)
255 {
256 	return NULL;
257 }
258 
259 static inline int
vlan_for_each(struct net_device * dev,int (* action)(struct net_device * dev,int vid,void * arg),void * arg)260 vlan_for_each(struct net_device *dev,
261 	      int (*action)(struct net_device *dev, int vid, void *arg),
262 	      void *arg)
263 {
264 	return 0;
265 }
266 
vlan_dev_real_dev(const struct net_device * dev)267 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
268 {
269 	WARN_ON_ONCE(1);
270 	return NULL;
271 }
272 
vlan_dev_vlan_id(const struct net_device * dev)273 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
274 {
275 	WARN_ON_ONCE(1);
276 	return 0;
277 }
278 
vlan_dev_vlan_proto(const struct net_device * dev)279 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
280 {
281 	WARN_ON_ONCE(1);
282 	return 0;
283 }
284 
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)285 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
286 					       u32 skprio)
287 {
288 	return 0;
289 }
290 
vlan_do_receive(struct sk_buff ** skb)291 static inline bool vlan_do_receive(struct sk_buff **skb)
292 {
293 	return false;
294 }
295 
vlan_vid_add(struct net_device * dev,__be16 proto,u16 vid)296 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
297 {
298 	return 0;
299 }
300 
vlan_vid_del(struct net_device * dev,__be16 proto,u16 vid)301 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
302 {
303 }
304 
vlan_vids_add_by_dev(struct net_device * dev,const struct net_device * by_dev)305 static inline int vlan_vids_add_by_dev(struct net_device *dev,
306 				       const struct net_device *by_dev)
307 {
308 	return 0;
309 }
310 
vlan_vids_del_by_dev(struct net_device * dev,const struct net_device * by_dev)311 static inline void vlan_vids_del_by_dev(struct net_device *dev,
312 					const struct net_device *by_dev)
313 {
314 }
315 
vlan_uses_dev(const struct net_device * dev)316 static inline bool vlan_uses_dev(const struct net_device *dev)
317 {
318 	return false;
319 }
320 #endif
321 
322 /**
323  * eth_type_vlan - check for valid vlan ether type.
324  * @ethertype: ether type to check
325  *
326  * Returns: true if the ether type is a vlan ether type.
327  */
eth_type_vlan(__be16 ethertype)328 static inline bool eth_type_vlan(__be16 ethertype)
329 {
330 	switch (ethertype) {
331 	case htons(ETH_P_8021Q):
332 	case htons(ETH_P_8021AD):
333 		return true;
334 	default:
335 		return false;
336 	}
337 }
338 
vlan_hw_offload_capable(netdev_features_t features,__be16 proto)339 static inline bool vlan_hw_offload_capable(netdev_features_t features,
340 					   __be16 proto)
341 {
342 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
343 		return true;
344 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
345 		return true;
346 	return false;
347 }
348 
349 /**
350  * __vlan_insert_inner_tag - inner VLAN tag inserting
351  * @skb: skbuff to tag
352  * @vlan_proto: VLAN encapsulation protocol
353  * @vlan_tci: VLAN TCI to insert
354  * @mac_len: MAC header length including outer vlan headers
355  *
356  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
357  * Does not change skb->protocol so this function can be used during receive.
358  *
359  * Returns: error if skb_cow_head fails.
360  */
__vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)361 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
362 					  __be16 vlan_proto, u16 vlan_tci,
363 					  unsigned int mac_len)
364 {
365 	const u8 meta_len = mac_len > ETH_TLEN ? skb_metadata_len(skb) : 0;
366 	struct vlan_ethhdr *veth;
367 
368 	if (unlikely(!pskb_may_pull(skb, mac_len)))
369 		return -EINVAL;
370 
371 	if (skb_cow_head(skb, meta_len + VLAN_HLEN) < 0)
372 		return -ENOMEM;
373 
374 	skb_push(skb, VLAN_HLEN);
375 
376 	/* Move the mac header sans proto to the beginning of the new header. */
377 	if (likely(mac_len > ETH_TLEN))
378 		skb_postpush_data_move(skb, VLAN_HLEN, mac_len - ETH_TLEN);
379 	if (skb_mac_header_was_set(skb))
380 		skb->mac_header -= VLAN_HLEN;
381 
382 	veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
383 
384 	/* first, the ethernet type */
385 	if (likely(mac_len >= ETH_TLEN)) {
386 		/* h_vlan_encapsulated_proto should already be populated, and
387 		 * skb->data has space for h_vlan_proto
388 		 */
389 		veth->h_vlan_proto = vlan_proto;
390 	} else {
391 		/* h_vlan_encapsulated_proto should not be populated, and
392 		 * skb->data has no space for h_vlan_proto
393 		 */
394 		veth->h_vlan_encapsulated_proto = skb->protocol;
395 	}
396 
397 	/* now, the TCI */
398 	veth->h_vlan_TCI = htons(vlan_tci);
399 
400 	return 0;
401 }
402 
403 /**
404  * __vlan_insert_tag - regular VLAN tag inserting
405  * @skb: skbuff to tag
406  * @vlan_proto: VLAN encapsulation protocol
407  * @vlan_tci: VLAN TCI to insert
408  *
409  * Inserts the VLAN tag into @skb as part of the payload
410  * Does not change skb->protocol so this function can be used during receive.
411  *
412  * Returns: error if skb_cow_head fails.
413  */
__vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)414 static inline int __vlan_insert_tag(struct sk_buff *skb,
415 				    __be16 vlan_proto, u16 vlan_tci)
416 {
417 	return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
418 }
419 
420 /**
421  * vlan_insert_inner_tag - inner VLAN tag inserting
422  * @skb: skbuff to tag
423  * @vlan_proto: VLAN encapsulation protocol
424  * @vlan_tci: VLAN TCI to insert
425  * @mac_len: MAC header length including outer vlan headers
426  *
427  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
428  * Returns a VLAN tagged skb. This might change skb->head.
429  *
430  * Following the skb_unshare() example, in case of error, the calling function
431  * doesn't have to worry about freeing the original skb.
432  *
433  * Does not change skb->protocol so this function can be used during receive.
434  *
435  * Return: modified @skb on success, NULL on error (@skb is freed).
436  */
vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)437 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
438 						    __be16 vlan_proto,
439 						    u16 vlan_tci,
440 						    unsigned int mac_len)
441 {
442 	int err;
443 
444 	err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
445 	if (err) {
446 		dev_kfree_skb_any(skb);
447 		return NULL;
448 	}
449 	return skb;
450 }
451 
452 /**
453  * vlan_insert_tag - regular VLAN tag inserting
454  * @skb: skbuff to tag
455  * @vlan_proto: VLAN encapsulation protocol
456  * @vlan_tci: VLAN TCI to insert
457  *
458  * Inserts the VLAN tag into @skb as part of the payload
459  * Returns a VLAN tagged skb. This might change skb->head.
460  *
461  * Following the skb_unshare() example, in case of error, the calling function
462  * doesn't have to worry about freeing the original skb.
463  *
464  * Does not change skb->protocol so this function can be used during receive.
465  *
466  * Return: modified @skb on success, NULL on error (@skb is freed).
467  */
vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)468 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
469 					      __be16 vlan_proto, u16 vlan_tci)
470 {
471 	return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
472 }
473 
474 /**
475  * vlan_insert_tag_set_proto - regular VLAN tag inserting
476  * @skb: skbuff to tag
477  * @vlan_proto: VLAN encapsulation protocol
478  * @vlan_tci: VLAN TCI to insert
479  *
480  * Inserts the VLAN tag into @skb as part of the payload
481  * Returns a VLAN tagged skb. This might change skb->head.
482  *
483  * Following the skb_unshare() example, in case of error, the calling function
484  * doesn't have to worry about freeing the original skb.
485  *
486  * Return: modified @skb on success, NULL on error (@skb is freed).
487  */
vlan_insert_tag_set_proto(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)488 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
489 							__be16 vlan_proto,
490 							u16 vlan_tci)
491 {
492 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
493 	if (skb)
494 		skb->protocol = vlan_proto;
495 	return skb;
496 }
497 
498 /**
499  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
500  * @skb: skbuff to clear
501  *
502  * Clears the VLAN information from @skb
503  */
__vlan_hwaccel_clear_tag(struct sk_buff * skb)504 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
505 {
506 	skb->vlan_all = 0;
507 }
508 
509 /**
510  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
511  * @dst: skbuff to copy to
512  * @src: skbuff to copy from
513  *
514  * Copies VLAN information from @src to @dst (for branchless code)
515  */
__vlan_hwaccel_copy_tag(struct sk_buff * dst,const struct sk_buff * src)516 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
517 {
518 	dst->vlan_all = src->vlan_all;
519 }
520 
521 /*
522  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
523  * @skb: skbuff to tag
524  *
525  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
526  *
527  * Following the skb_unshare() example, in case of error, the calling function
528  * doesn't have to worry about freeing the original skb.
529  */
__vlan_hwaccel_push_inside(struct sk_buff * skb)530 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
531 {
532 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
533 					skb_vlan_tag_get(skb));
534 	if (likely(skb))
535 		__vlan_hwaccel_clear_tag(skb);
536 	return skb;
537 }
538 
539 /**
540  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
541  * @skb: skbuff to tag
542  * @vlan_proto: VLAN encapsulation protocol
543  * @vlan_tci: VLAN TCI to insert
544  *
545  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
546  */
__vlan_hwaccel_put_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)547 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
548 					  __be16 vlan_proto, u16 vlan_tci)
549 {
550 	skb->vlan_proto = vlan_proto;
551 	skb->vlan_tci = vlan_tci;
552 }
553 
554 /**
555  * __vlan_get_tag - get the VLAN ID that is part of the payload
556  * @skb: skbuff to query
557  * @vlan_tci: buffer to store value
558  *
559  * Returns: error if the skb is not of VLAN type
560  */
__vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)561 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
562 {
563 	struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
564 
565 	if (!eth_type_vlan(veth->h_vlan_proto))
566 		return -ENODATA;
567 
568 	*vlan_tci = ntohs(veth->h_vlan_TCI);
569 	return 0;
570 }
571 
572 /**
573  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
574  * @skb: skbuff to query
575  * @vlan_tci: buffer to store value
576  *
577  * Returns: error if @skb->vlan_tci is not set correctly
578  */
__vlan_hwaccel_get_tag(const struct sk_buff * skb,u16 * vlan_tci)579 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
580 					 u16 *vlan_tci)
581 {
582 	if (skb_vlan_tag_present(skb)) {
583 		*vlan_tci = skb_vlan_tag_get(skb);
584 		return 0;
585 	} else {
586 		*vlan_tci = 0;
587 		return -ENODATA;
588 	}
589 }
590 
591 /**
592  * vlan_get_tag - get the VLAN ID from the skb
593  * @skb: skbuff to query
594  * @vlan_tci: buffer to store value
595  *
596  * Returns: error if the skb is not VLAN tagged
597  */
vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)598 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
599 {
600 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
601 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
602 	} else {
603 		return __vlan_get_tag(skb, vlan_tci);
604 	}
605 }
606 
607 struct vlan_type_depth {
608 	__be16 type;
609 	u16 depth;
610 };
611 
612 struct vlan_type_depth __vlan_get_protocol_offset(const struct sk_buff *skb,
613 						  __be16 type,
614 						  int mac_offset);
615 
616 /**
617  * vlan_get_protocol_offset_inline() - get protocol EtherType.
618  * @skb: skbuff to query
619  * @type: first vlan protocol
620  * @mac_offset: MAC offset
621  * @depth: buffer to store length of eth and vlan tags in bytes
622  *
623  * Returns: the EtherType of the packet, regardless of whether it is
624  * vlan encapsulated (normal or hardware accelerated) or not.
625  */
626 static inline
vlan_get_protocol_offset_inline(const struct sk_buff * skb,__be16 type,int mac_offset,int * depth)627 __be16 vlan_get_protocol_offset_inline(const struct sk_buff *skb,
628 				       __be16 type,
629 				       int mac_offset,
630 				       int *depth)
631 {
632 	if (eth_type_vlan(type)) {
633 		struct vlan_type_depth res;
634 
635 		res = __vlan_get_protocol_offset(skb, type, mac_offset);
636 
637 		if (depth && res.type)
638 			*depth = res.depth;
639 		return res.type;
640 	}
641 
642 	if (depth)
643 		*depth = skb->mac_len;
644 
645 	return type;
646 }
647 
__vlan_get_protocol(const struct sk_buff * skb,__be16 type,int * depth)648 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
649 					 int *depth)
650 {
651 	return vlan_get_protocol_offset_inline(skb, type, 0, depth);
652 }
653 
654 /**
655  * vlan_get_protocol - get protocol EtherType.
656  * @skb: skbuff to query
657  *
658  * Returns: the EtherType of the packet, regardless of whether it is
659  * vlan encapsulated (normal or hardware accelerated) or not.
660  */
vlan_get_protocol(const struct sk_buff * skb)661 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
662 {
663 	return __vlan_get_protocol(skb, skb->protocol, NULL);
664 }
665 
666 /* This version of __vlan_get_protocol() also pulls mac header in skb->head */
vlan_get_protocol_and_depth(struct sk_buff * skb,__be16 type,int * depth)667 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
668 						 __be16 type, int *depth)
669 {
670 	int maclen;
671 
672 	type = __vlan_get_protocol(skb, type, &maclen);
673 
674 	if (type) {
675 		if (!pskb_may_pull(skb, maclen))
676 			type = 0;
677 		else if (depth)
678 			*depth = maclen;
679 	}
680 	return type;
681 }
682 
683 /* A getter for the SKB protocol field which will handle VLAN tags consistently
684  * whether VLAN acceleration is enabled or not.
685  */
skb_protocol(const struct sk_buff * skb,bool skip_vlan)686 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
687 {
688 	if (!skip_vlan)
689 		/* VLAN acceleration strips the VLAN header from the skb and
690 		 * moves it to skb->vlan_proto
691 		 */
692 		return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
693 
694 	return vlan_get_protocol(skb);
695 }
696 
vlan_set_encap_proto(struct sk_buff * skb,struct vlan_hdr * vhdr)697 static inline void vlan_set_encap_proto(struct sk_buff *skb,
698 					struct vlan_hdr *vhdr)
699 {
700 	__be16 proto;
701 	unsigned short *rawp;
702 
703 	/*
704 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
705 	 * three protocols care about.
706 	 */
707 
708 	proto = vhdr->h_vlan_encapsulated_proto;
709 	if (eth_proto_is_802_3(proto)) {
710 		skb->protocol = proto;
711 		return;
712 	}
713 
714 	rawp = (unsigned short *)(vhdr + 1);
715 	if (*rawp == 0xFFFF)
716 		/*
717 		 * This is a magic hack to spot IPX packets. Older Novell
718 		 * breaks the protocol design and runs IPX over 802.3 without
719 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
720 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
721 		 * but does for the rest.
722 		 */
723 		skb->protocol = htons(ETH_P_802_3);
724 	else
725 		/*
726 		 * Real 802.2 LLC
727 		 */
728 		skb->protocol = htons(ETH_P_802_2);
729 }
730 
731 /**
732  * vlan_remove_tag - remove outer VLAN tag from payload
733  * @skb: skbuff to remove tag from
734  * @vlan_tci: buffer to store value
735  *
736  * Expects the skb to contain a VLAN tag in the payload, and to have skb->data
737  * pointing at the MAC header.
738  */
vlan_remove_tag(struct sk_buff * skb,u16 * vlan_tci)739 static inline void vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci)
740 {
741 	struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
742 
743 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
744 
745 	vlan_set_encap_proto(skb, vhdr);
746 	__skb_pull(skb, VLAN_HLEN);
747 	skb_postpull_data_move(skb, VLAN_HLEN, 2 * ETH_ALEN);
748 }
749 
750 /**
751  * skb_vlan_tagged - check if skb is vlan tagged.
752  * @skb: skbuff to query
753  *
754  * Returns: true if the skb is tagged, regardless of whether it is hardware
755  * accelerated or not.
756  */
skb_vlan_tagged(const struct sk_buff * skb)757 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
758 {
759 	if (!skb_vlan_tag_present(skb) &&
760 	    likely(!eth_type_vlan(skb->protocol)))
761 		return false;
762 
763 	return true;
764 }
765 
766 /**
767  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
768  * @skb: skbuff to query
769  *
770  * Returns: true if the skb is tagged with multiple vlan headers, regardless
771  * of whether it is hardware accelerated or not.
772  */
skb_vlan_tagged_multi(struct sk_buff * skb)773 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
774 {
775 	__be16 protocol = skb->protocol;
776 
777 	if (!skb_vlan_tag_present(skb)) {
778 		struct vlan_ethhdr *veh;
779 
780 		if (likely(!eth_type_vlan(protocol)))
781 			return false;
782 
783 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
784 			return false;
785 
786 		veh = skb_vlan_eth_hdr(skb);
787 		protocol = veh->h_vlan_encapsulated_proto;
788 	}
789 
790 	if (!eth_type_vlan(protocol))
791 		return false;
792 
793 	return true;
794 }
795 
796 /**
797  * vlan_features_check - drop unsafe features for skb with multiple tags.
798  * @skb: skbuff to query
799  * @features: features to be checked
800  *
801  * Returns: features without unsafe ones if the skb has multiple tags.
802  */
vlan_features_check(struct sk_buff * skb,netdev_features_t features)803 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
804 						    netdev_features_t features)
805 {
806 	if (skb_vlan_tagged_multi(skb)) {
807 		/* In the case of multi-tagged packets, use a direct mask
808 		 * instead of using netdev_interesect_features(), to make
809 		 * sure that only devices supporting NETIF_F_HW_CSUM will
810 		 * have checksum offloading support.
811 		 */
812 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
813 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
814 			    NETIF_F_HW_VLAN_STAG_TX;
815 	}
816 
817 	return features;
818 }
819 
820 /**
821  * compare_vlan_header - Compare two vlan headers
822  * @h1: Pointer to vlan header
823  * @h2: Pointer to vlan header
824  *
825  * Compare two vlan headers.
826  *
827  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
828  *
829  * Return: 0 if equal, arbitrary non-zero value if not equal.
830  */
compare_vlan_header(const struct vlan_hdr * h1,const struct vlan_hdr * h2)831 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
832 						const struct vlan_hdr *h2)
833 {
834 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
835 	return *(u32 *)h1 ^ *(u32 *)h2;
836 #else
837 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
838 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
839 		(__force u32)h2->h_vlan_encapsulated_proto);
840 #endif
841 }
842 #endif /* !(_LINUX_IF_VLAN_H_) */
843