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