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