xref: /linux/include/linux/if_vlan.h (revision 04ca6fe63d63bafec1e97c187c108f2ddf2328ce)
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 
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  */
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 
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 
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 
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 
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  *	@uninit: the promisc/allmulti references on @real_dev were dropped
172  *	@real_dev: underlying netdevice
173  *	@dev_tracker: refcount tracker for @real_dev reference
174  *	@real_dev_addr: address of underlying netdevice
175  *	@dent: proc dir entry
176  *	@vlan_pcpu_stats: ptr to percpu rx stats
177  *	@netpoll: netpoll instance "propagated" down to @real_dev
178  */
179 struct vlan_dev_priv {
180 	unsigned int				nr_ingress_mappings;
181 	u32					ingress_priority_map[8];
182 	unsigned int				nr_egress_mappings;
183 	struct vlan_priority_tci_mapping __rcu	*egress_priority_map[16];
184 
185 	__be16					vlan_proto;
186 	u16					vlan_id;
187 	u16					flags;
188 	bool					uninit;
189 
190 	struct net_device			*real_dev;
191 	netdevice_tracker			dev_tracker;
192 
193 	unsigned char				real_dev_addr[ETH_ALEN];
194 
195 	struct proc_dir_entry			*dent;
196 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
197 #ifdef CONFIG_NET_POLL_CONTROLLER
198 	struct netpoll				*netpoll;
199 #endif
200 };
201 
202 static inline bool is_vlan_dev(const struct net_device *dev)
203 {
204 	return dev->priv_flags & IFF_802_1Q_VLAN;
205 }
206 
207 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
208 {
209 	return netdev_priv(dev);
210 }
211 
212 static inline u16
213 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
214 {
215 	struct vlan_priority_tci_mapping *mp;
216 	u16 vlan_qos = 0;
217 
218 	rcu_read_lock();
219 
220 	mp = rcu_dereference(vlan_dev_priv(dev)->egress_priority_map[skprio & 0xF]);
221 	while (mp) {
222 		if (mp->priority == skprio) {
223 			vlan_qos = READ_ONCE(mp->vlan_qos);
224 			break;
225 		}
226 		mp = rcu_dereference(mp->next);
227 	}
228 	rcu_read_unlock();
229 
230 	/* This should already be shifted to mask correctly with
231 	 * the VLAN's TCI.
232 	 */
233 	return vlan_qos;
234 }
235 
236 extern bool vlan_do_receive(struct sk_buff **skb);
237 
238 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
239 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
240 
241 extern int vlan_vids_add_by_dev(struct net_device *dev,
242 				const struct net_device *by_dev);
243 extern void vlan_vids_del_by_dev(struct net_device *dev,
244 				 const struct net_device *by_dev);
245 
246 extern bool vlan_uses_dev(const struct net_device *dev);
247 
248 #else
249 static inline bool is_vlan_dev(const struct net_device *dev)
250 {
251 	return false;
252 }
253 
254 static inline struct net_device *
255 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
256 		     __be16 vlan_proto, u16 vlan_id)
257 {
258 	return NULL;
259 }
260 
261 static inline int
262 vlan_for_each(struct net_device *dev,
263 	      int (*action)(struct net_device *dev, int vid, void *arg),
264 	      void *arg)
265 {
266 	return 0;
267 }
268 
269 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
270 {
271 	WARN_ON_ONCE(1);
272 	return NULL;
273 }
274 
275 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
276 {
277 	WARN_ON_ONCE(1);
278 	return 0;
279 }
280 
281 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
282 {
283 	WARN_ON_ONCE(1);
284 	return 0;
285 }
286 
287 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
288 					       u32 skprio)
289 {
290 	return 0;
291 }
292 
293 static inline bool vlan_do_receive(struct sk_buff **skb)
294 {
295 	return false;
296 }
297 
298 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
299 {
300 	return 0;
301 }
302 
303 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
304 {
305 }
306 
307 static inline int vlan_vids_add_by_dev(struct net_device *dev,
308 				       const struct net_device *by_dev)
309 {
310 	return 0;
311 }
312 
313 static inline void vlan_vids_del_by_dev(struct net_device *dev,
314 					const struct net_device *by_dev)
315 {
316 }
317 
318 static inline bool vlan_uses_dev(const struct net_device *dev)
319 {
320 	return false;
321 }
322 #endif
323 
324 /**
325  * eth_type_vlan - check for valid vlan ether type.
326  * @ethertype: ether type to check
327  *
328  * Returns: true if the ether type is a vlan ether type.
329  */
330 static inline bool eth_type_vlan(__be16 ethertype)
331 {
332 	switch (ethertype) {
333 	case htons(ETH_P_8021Q):
334 	case htons(ETH_P_8021AD):
335 		return true;
336 	default:
337 		return false;
338 	}
339 }
340 
341 static inline bool vlan_hw_offload_capable(netdev_features_t features,
342 					   __be16 proto)
343 {
344 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
345 		return true;
346 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
347 		return true;
348 	return false;
349 }
350 
351 /**
352  * __vlan_insert_inner_tag - inner VLAN tag inserting
353  * @skb: skbuff to tag
354  * @vlan_proto: VLAN encapsulation protocol
355  * @vlan_tci: VLAN TCI to insert
356  * @mac_len: MAC header length including outer vlan headers
357  *
358  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
359  * Does not change skb->protocol so this function can be used during receive.
360  *
361  * Returns: error if skb_cow_head fails.
362  */
363 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
364 					  __be16 vlan_proto, u16 vlan_tci,
365 					  unsigned int mac_len)
366 {
367 	const u8 meta_len = mac_len > ETH_TLEN ? skb_metadata_len(skb) : 0;
368 	struct vlan_ethhdr *veth;
369 
370 	if (unlikely(!pskb_may_pull(skb, mac_len)))
371 		return -EINVAL;
372 
373 	if (skb_cow_head(skb, meta_len + VLAN_HLEN) < 0)
374 		return -ENOMEM;
375 
376 	skb_push(skb, VLAN_HLEN);
377 
378 	/* Move the mac header sans proto to the beginning of the new header. */
379 	if (likely(mac_len > ETH_TLEN))
380 		skb_postpush_data_move(skb, VLAN_HLEN, mac_len - ETH_TLEN);
381 	if (skb_mac_header_was_set(skb))
382 		skb->mac_header -= VLAN_HLEN;
383 
384 	veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
385 
386 	/* first, the ethernet type */
387 	if (likely(mac_len >= ETH_TLEN)) {
388 		/* h_vlan_encapsulated_proto should already be populated, and
389 		 * skb->data has space for h_vlan_proto
390 		 */
391 		veth->h_vlan_proto = vlan_proto;
392 	} else {
393 		/* h_vlan_encapsulated_proto should not be populated, and
394 		 * skb->data has no space for h_vlan_proto
395 		 */
396 		veth->h_vlan_encapsulated_proto = skb->protocol;
397 	}
398 
399 	/* now, the TCI */
400 	veth->h_vlan_TCI = htons(vlan_tci);
401 
402 	return 0;
403 }
404 
405 /**
406  * __vlan_insert_tag - regular VLAN tag inserting
407  * @skb: skbuff to tag
408  * @vlan_proto: VLAN encapsulation protocol
409  * @vlan_tci: VLAN TCI to insert
410  *
411  * Inserts the VLAN tag into @skb as part of the payload
412  * Does not change skb->protocol so this function can be used during receive.
413  *
414  * Returns: error if skb_cow_head fails.
415  */
416 static inline int __vlan_insert_tag(struct sk_buff *skb,
417 				    __be16 vlan_proto, u16 vlan_tci)
418 {
419 	return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
420 }
421 
422 /**
423  * vlan_insert_inner_tag - inner VLAN tag inserting
424  * @skb: skbuff to tag
425  * @vlan_proto: VLAN encapsulation protocol
426  * @vlan_tci: VLAN TCI to insert
427  * @mac_len: MAC header length including outer vlan headers
428  *
429  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
430  * Returns a VLAN tagged skb. This might change skb->head.
431  *
432  * Following the skb_unclone_full() example, in case of error, the calling
433  * function doesn't have to worry about freeing the original skb.
434  *
435  * Does not change skb->protocol so this function can be used during receive.
436  *
437  * Return: modified @skb on success, NULL on error (@skb is freed).
438  */
439 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
440 						    __be16 vlan_proto,
441 						    u16 vlan_tci,
442 						    unsigned int mac_len)
443 {
444 	int err;
445 
446 	err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
447 	if (err) {
448 		dev_kfree_skb_any(skb);
449 		return NULL;
450 	}
451 	return skb;
452 }
453 
454 /**
455  * vlan_insert_tag - regular VLAN tag inserting
456  * @skb: skbuff to tag
457  * @vlan_proto: VLAN encapsulation protocol
458  * @vlan_tci: VLAN TCI to insert
459  *
460  * Inserts the VLAN tag into @skb as part of the payload
461  * Returns a VLAN tagged skb. This might change skb->head.
462  *
463  * Following the skb_unclone_full() example, in case of error, the calling
464  * function doesn't have to worry about freeing the original skb.
465  *
466  * Does not change skb->protocol so this function can be used during receive.
467  *
468  * Return: modified @skb on success, NULL on error (@skb is freed).
469  */
470 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
471 					      __be16 vlan_proto, u16 vlan_tci)
472 {
473 	return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
474 }
475 
476 /**
477  * vlan_insert_tag_set_proto - regular VLAN tag inserting
478  * @skb: skbuff to tag
479  * @vlan_proto: VLAN encapsulation protocol
480  * @vlan_tci: VLAN TCI to insert
481  *
482  * Inserts the VLAN tag into @skb as part of the payload
483  * Returns a VLAN tagged skb. This might change skb->head.
484  *
485  * Following the skb_unclone_full() example, in case of error, the calling
486  * function doesn't have to worry about freeing the original skb.
487  *
488  * Return: modified @skb on success, NULL on error (@skb is freed).
489  */
490 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
491 							__be16 vlan_proto,
492 							u16 vlan_tci)
493 {
494 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
495 	if (skb)
496 		skb->protocol = vlan_proto;
497 	return skb;
498 }
499 
500 /**
501  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
502  * @skb: skbuff to clear
503  *
504  * Clears the VLAN information from @skb
505  */
506 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
507 {
508 	skb->vlan_all = 0;
509 }
510 
511 /**
512  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
513  * @dst: skbuff to copy to
514  * @src: skbuff to copy from
515  *
516  * Copies VLAN information from @src to @dst (for branchless code)
517  */
518 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
519 {
520 	dst->vlan_all = src->vlan_all;
521 }
522 
523 /*
524  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
525  * @skb: skbuff to tag
526  *
527  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
528  *
529  * Following the skb_unclone_full() example, in case of error, the calling
530  * function doesn't have to worry about freeing the original skb.
531  */
532 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
533 {
534 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
535 					skb_vlan_tag_get(skb));
536 	if (likely(skb))
537 		__vlan_hwaccel_clear_tag(skb);
538 	return skb;
539 }
540 
541 /**
542  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
543  * @skb: skbuff to tag
544  * @vlan_proto: VLAN encapsulation protocol
545  * @vlan_tci: VLAN TCI to insert
546  *
547  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
548  */
549 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
550 					  __be16 vlan_proto, u16 vlan_tci)
551 {
552 	skb->vlan_proto = vlan_proto;
553 	skb->vlan_tci = vlan_tci;
554 }
555 
556 /**
557  * __vlan_get_tag - get the VLAN ID that is part of the payload
558  * @skb: skbuff to query
559  * @vlan_tci: buffer to store value
560  *
561  * Returns: error if the skb is not of VLAN type
562  */
563 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
564 {
565 	struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
566 
567 	if (!eth_type_vlan(veth->h_vlan_proto))
568 		return -ENODATA;
569 
570 	*vlan_tci = ntohs(veth->h_vlan_TCI);
571 	return 0;
572 }
573 
574 /**
575  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
576  * @skb: skbuff to query
577  * @vlan_tci: buffer to store value
578  *
579  * Returns: error if @skb->vlan_tci is not set correctly
580  */
581 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
582 					 u16 *vlan_tci)
583 {
584 	if (skb_vlan_tag_present(skb)) {
585 		*vlan_tci = skb_vlan_tag_get(skb);
586 		return 0;
587 	} else {
588 		*vlan_tci = 0;
589 		return -ENODATA;
590 	}
591 }
592 
593 /**
594  * vlan_get_tag - get the VLAN ID from the skb
595  * @skb: skbuff to query
596  * @vlan_tci: buffer to store value
597  *
598  * Returns: error if the skb is not VLAN tagged
599  */
600 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
601 {
602 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
603 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
604 	} else {
605 		return __vlan_get_tag(skb, vlan_tci);
606 	}
607 }
608 
609 struct vlan_type_depth {
610 	__be16 type;
611 	u16 depth;
612 };
613 
614 struct vlan_type_depth __vlan_get_protocol_offset(const struct sk_buff *skb,
615 						  __be16 type,
616 						  int mac_offset);
617 
618 /**
619  * vlan_get_protocol_offset_inline() - get protocol EtherType.
620  * @skb: skbuff to query
621  * @type: first vlan protocol
622  * @mac_offset: MAC offset
623  * @depth: buffer to store length of eth and vlan tags in bytes
624  *
625  * Returns: the EtherType of the packet, regardless of whether it is
626  * vlan encapsulated (normal or hardware accelerated) or not.
627  */
628 static inline
629 __be16 vlan_get_protocol_offset_inline(const struct sk_buff *skb,
630 				       __be16 type,
631 				       int mac_offset,
632 				       int *depth)
633 {
634 	if (eth_type_vlan(type)) {
635 		struct vlan_type_depth res;
636 
637 		res = __vlan_get_protocol_offset(skb, type, mac_offset);
638 
639 		if (depth && res.type)
640 			*depth = res.depth;
641 		return res.type;
642 	}
643 
644 	if (depth)
645 		*depth = skb->mac_len;
646 
647 	return type;
648 }
649 
650 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
651 					 int *depth)
652 {
653 	return vlan_get_protocol_offset_inline(skb, type, 0, depth);
654 }
655 
656 /**
657  * vlan_get_protocol - get protocol EtherType.
658  * @skb: skbuff to query
659  *
660  * Returns: the EtherType of the packet, regardless of whether it is
661  * vlan encapsulated (normal or hardware accelerated) or not.
662  */
663 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
664 {
665 	return __vlan_get_protocol(skb, skb->protocol, NULL);
666 }
667 
668 /* This version of __vlan_get_protocol() also pulls mac header in skb->head */
669 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
670 						 __be16 type, int *depth)
671 {
672 	int maclen;
673 
674 	type = __vlan_get_protocol(skb, type, &maclen);
675 
676 	if (type) {
677 		if (!pskb_may_pull(skb, maclen))
678 			type = 0;
679 		else if (depth)
680 			*depth = maclen;
681 	}
682 	return type;
683 }
684 
685 /* A getter for the SKB protocol field which will handle VLAN tags consistently
686  * whether VLAN acceleration is enabled or not.
687  */
688 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
689 {
690 	if (!skip_vlan)
691 		/* VLAN acceleration strips the VLAN header from the skb and
692 		 * moves it to skb->vlan_proto
693 		 */
694 		return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
695 
696 	return vlan_get_protocol(skb);
697 }
698 
699 static inline void vlan_set_encap_proto(struct sk_buff *skb,
700 					struct vlan_hdr *vhdr)
701 {
702 	__be16 proto;
703 
704 	/*
705 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
706 	 * three protocols care about.
707 	 */
708 
709 	proto = vhdr->h_vlan_encapsulated_proto;
710 	if (eth_proto_is_802_3(proto)) {
711 		skb->protocol = proto;
712 		return;
713 	}
714 
715 	/* No ethertype: this is an 802.2 LLC frame (length field). */
716 	skb->protocol = htons(ETH_P_802_2);
717 }
718 
719 /**
720  * vlan_remove_tag - remove outer VLAN tag from payload
721  * @skb: skbuff to remove tag from
722  * @vlan_tci: buffer to store value
723  *
724  * Expects the skb to contain a VLAN tag in the payload, and to have skb->data
725  * pointing at the MAC header.
726  */
727 static inline void vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci)
728 {
729 	struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
730 
731 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
732 
733 	vlan_set_encap_proto(skb, vhdr);
734 	__skb_pull(skb, VLAN_HLEN);
735 	skb_postpull_data_move(skb, VLAN_HLEN, 2 * ETH_ALEN);
736 }
737 
738 /**
739  * skb_vlan_tagged - check if skb is vlan tagged.
740  * @skb: skbuff to query
741  *
742  * Returns: true if the skb is tagged, regardless of whether it is hardware
743  * accelerated or not.
744  */
745 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
746 {
747 	if (!skb_vlan_tag_present(skb) &&
748 	    likely(!eth_type_vlan(skb->protocol)))
749 		return false;
750 
751 	return true;
752 }
753 
754 /**
755  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
756  * @skb: skbuff to query
757  *
758  * Returns: true if the skb is tagged with multiple vlan headers, regardless
759  * of whether it is hardware accelerated or not.
760  */
761 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
762 {
763 	__be16 protocol = skb->protocol;
764 
765 	if (!skb_vlan_tag_present(skb)) {
766 		struct vlan_ethhdr *veh;
767 
768 		if (likely(!eth_type_vlan(protocol)))
769 			return false;
770 
771 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
772 			return false;
773 
774 		veh = skb_vlan_eth_hdr(skb);
775 		protocol = veh->h_vlan_encapsulated_proto;
776 	}
777 
778 	if (!eth_type_vlan(protocol))
779 		return false;
780 
781 	return true;
782 }
783 
784 /**
785  * vlan_features_check - drop unsafe features for skb with multiple tags.
786  * @skb: skbuff to query
787  * @features: features to be checked
788  *
789  * Returns: features without unsafe ones if the skb has multiple tags.
790  */
791 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
792 						    netdev_features_t features)
793 {
794 	if (skb_vlan_tagged_multi(skb)) {
795 		/* In the case of multi-tagged packets, use a direct mask
796 		 * instead of using netdev_interesect_features(), to make
797 		 * sure that only devices supporting NETIF_F_HW_CSUM will
798 		 * have checksum offloading support.
799 		 */
800 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
801 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
802 			    NETIF_F_HW_VLAN_STAG_TX;
803 	}
804 
805 	return features;
806 }
807 
808 /**
809  * compare_vlan_header - Compare two vlan headers
810  * @h1: Pointer to vlan header
811  * @h2: Pointer to vlan header
812  *
813  * Compare two vlan headers.
814  *
815  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
816  *
817  * Return: 0 if equal, arbitrary non-zero value if not equal.
818  */
819 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
820 						const struct vlan_hdr *h2)
821 {
822 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
823 	return *(u32 *)h1 ^ *(u32 *)h2;
824 #else
825 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
826 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
827 		(__force u32)h2->h_vlan_encapsulated_proto);
828 #endif
829 }
830 #endif /* !(_LINUX_IF_VLAN_H_) */
831