xref: /linux/include/linux/if_vlan.h (revision 469aceddfa3ed16e17ee30533fae45e90f62efd8)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * VLAN		An implementation of 802.1Q VLAN tagging.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:	Ben Greear <greearb@candelatech.com>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #ifndef _LINUX_IF_VLAN_H_
81da177e4SLinus Torvalds #define _LINUX_IF_VLAN_H_
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/netdevice.h>
110610d11bSStephen Hemminger #include <linux/etherdevice.h>
1265ac6a5fSJesse Gross #include <linux/rtnetlink.h>
13187f1882SPaul Gortmaker #include <linux/bug.h>
14607ca46eSDavid Howells #include <uapi/linux/if_vlan.h>
151da177e4SLinus Torvalds 
16a3f671b3SJoe Perches #define VLAN_HLEN	4		/* The additional bytes required by VLAN
17a3f671b3SJoe Perches 					 * (in addition to the Ethernet header)
181da177e4SLinus Torvalds 					 */
191da177e4SLinus Torvalds #define VLAN_ETH_HLEN	18		/* Total octets in header.	 */
201da177e4SLinus Torvalds #define VLAN_ETH_ZLEN	64		/* Min. octets in frame sans FCS */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds #define VLAN_ETH_DATA_LEN	1500	/* Max. octets in payload	 */
261da177e4SLinus Torvalds #define VLAN_ETH_FRAME_LEN	1518	/* Max. octets in frame sans FCS */
271da177e4SLinus Torvalds 
28*469aceddSToke Høiland-Jørgensen #define VLAN_MAX_DEPTH	8		/* Max. number of nested VLAN tags parsed */
29*469aceddSToke Høiland-Jørgensen 
30740c15d0SPatrick McHardy /*
31740c15d0SPatrick McHardy  * 	struct vlan_hdr - vlan header
32740c15d0SPatrick McHardy  * 	@h_vlan_TCI: priority and VLAN ID
33740c15d0SPatrick McHardy  *	@h_vlan_encapsulated_proto: packet type ID or len
34740c15d0SPatrick McHardy  */
35740c15d0SPatrick McHardy struct vlan_hdr {
36740c15d0SPatrick McHardy 	__be16	h_vlan_TCI;
37740c15d0SPatrick McHardy 	__be16	h_vlan_encapsulated_proto;
38740c15d0SPatrick McHardy };
39740c15d0SPatrick McHardy 
40740c15d0SPatrick McHardy /**
41740c15d0SPatrick McHardy  *	struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
42740c15d0SPatrick McHardy  *	@h_dest: destination ethernet address
43740c15d0SPatrick McHardy  *	@h_source: source ethernet address
44a7bf5804SOlaf Hering  *	@h_vlan_proto: ethernet protocol
45740c15d0SPatrick McHardy  *	@h_vlan_TCI: priority and VLAN ID
46740c15d0SPatrick McHardy  *	@h_vlan_encapsulated_proto: packet type ID or len
47740c15d0SPatrick McHardy  */
481da177e4SLinus Torvalds struct vlan_ethhdr {
49740c15d0SPatrick McHardy 	unsigned char	h_dest[ETH_ALEN];
50740c15d0SPatrick McHardy 	unsigned char	h_source[ETH_ALEN];
51740c15d0SPatrick McHardy 	__be16		h_vlan_proto;
52740c15d0SPatrick McHardy 	__be16		h_vlan_TCI;
53740c15d0SPatrick McHardy 	__be16		h_vlan_encapsulated_proto;
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <linux/skbuff.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
591da177e4SLinus Torvalds {
6098e399f8SArnaldo Carvalho de Melo 	return (struct vlan_ethhdr *)skb_mac_header(skb);
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
6305423b24SEric Dumazet #define VLAN_PRIO_MASK		0xe000 /* Priority Code Point */
6405423b24SEric Dumazet #define VLAN_PRIO_SHIFT		13
65a2e768b8SMichał Mirosław #define VLAN_CFI_MASK		0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
6605423b24SEric Dumazet #define VLAN_VID_MASK		0x0fff /* VLAN Identifier */
67b738127dSJesse Gross #define VLAN_N_VID		4096
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* found in socket.c */
70881d966bSEric W. Biederman extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
711da177e4SLinus Torvalds 
72b618aaa9SJiri Pirko static inline bool is_vlan_dev(const struct net_device *dev)
736dcbbe25SNeil Horman {
746dcbbe25SNeil Horman         return dev->priv_flags & IFF_802_1Q_VLAN;
756dcbbe25SNeil Horman }
766dcbbe25SNeil Horman 
770c4b2d37SMichał Mirosław #define skb_vlan_tag_present(__skb)	((__skb)->vlan_present)
780c4b2d37SMichał Mirosław #define skb_vlan_tag_get(__skb)		((__skb)->vlan_tci)
79df8a39deSJiri Pirko #define skb_vlan_tag_get_id(__skb)	((__skb)->vlan_tci & VLAN_VID_MASK)
80a2e768b8SMichał Mirosław #define skb_vlan_tag_get_cfi(__skb)	(!!((__skb)->vlan_tci & VLAN_CFI_MASK))
819b319148SMichał Mirosław #define skb_vlan_tag_get_prio(__skb)	(((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
821da177e4SLinus Torvalds 
839daae9bdSGal Pressman static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
849daae9bdSGal Pressman {
859daae9bdSGal Pressman 	ASSERT_RTNL();
869daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
879daae9bdSGal Pressman }
889daae9bdSGal Pressman 
899daae9bdSGal Pressman static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
909daae9bdSGal Pressman {
919daae9bdSGal Pressman 	ASSERT_RTNL();
929daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
939daae9bdSGal Pressman }
949daae9bdSGal Pressman 
959daae9bdSGal Pressman static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
969daae9bdSGal Pressman {
979daae9bdSGal Pressman 	ASSERT_RTNL();
989daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
999daae9bdSGal Pressman }
1009daae9bdSGal Pressman 
1019daae9bdSGal Pressman static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
1029daae9bdSGal Pressman {
1039daae9bdSGal Pressman 	ASSERT_RTNL();
1049daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
1059daae9bdSGal Pressman }
1069daae9bdSGal Pressman 
107e267cb96SDavid S. Miller /**
108e267cb96SDavid S. Miller  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
109e267cb96SDavid S. Miller  *	@rx_packets: number of received packets
110e267cb96SDavid S. Miller  *	@rx_bytes: number of received bytes
111e267cb96SDavid S. Miller  *	@rx_multicast: number of received multicast packets
112e267cb96SDavid S. Miller  *	@tx_packets: number of transmitted packets
113e267cb96SDavid S. Miller  *	@tx_bytes: number of transmitted bytes
114e267cb96SDavid S. Miller  *	@syncp: synchronization point for 64bit counters
115e267cb96SDavid S. Miller  *	@rx_errors: number of rx errors
116e267cb96SDavid S. Miller  *	@tx_dropped: number of tx drops
117e267cb96SDavid S. Miller  */
118e267cb96SDavid S. Miller struct vlan_pcpu_stats {
119e267cb96SDavid S. Miller 	u64			rx_packets;
120e267cb96SDavid S. Miller 	u64			rx_bytes;
121e267cb96SDavid S. Miller 	u64			rx_multicast;
122e267cb96SDavid S. Miller 	u64			tx_packets;
123e267cb96SDavid S. Miller 	u64			tx_bytes;
124e267cb96SDavid S. Miller 	struct u64_stats_sync	syncp;
125e267cb96SDavid S. Miller 	u32			rx_errors;
126e267cb96SDavid S. Miller 	u32			tx_dropped;
127e267cb96SDavid S. Miller };
128e267cb96SDavid S. Miller 
1291e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1301e85c9b6SHannes Frederic Sowa 
131f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
1321e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
133960abf68SIvan Khoronzhuk extern int vlan_for_each(struct net_device *dev,
134960abf68SIvan Khoronzhuk 			 int (*action)(struct net_device *dev, int vid,
135960abf68SIvan Khoronzhuk 				       void *arg), void *arg);
1361e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
1371e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
13871e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
1391e85c9b6SHannes Frederic Sowa 
1401e85c9b6SHannes Frederic Sowa /**
1411e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
1421e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
1431e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
1441e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
1451e85c9b6SHannes Frederic Sowa  */
1461e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
1471e85c9b6SHannes Frederic Sowa 	u32					priority;
1481e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
1491e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
1501e85c9b6SHannes Frederic Sowa };
1511e85c9b6SHannes Frederic Sowa 
152e267cb96SDavid S. Miller struct proc_dir_entry;
153e267cb96SDavid S. Miller struct netpoll;
154e267cb96SDavid S. Miller 
155e267cb96SDavid S. Miller /**
156e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
157e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
158e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
159e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
160e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
161e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
162e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
163e267cb96SDavid S. Miller  *	@flags: device flags
164e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
165e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
166e267cb96SDavid S. Miller  *	@dent: proc dir entry
167e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
168e267cb96SDavid S. Miller  */
169e267cb96SDavid S. Miller struct vlan_dev_priv {
170e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
171e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
172e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
173e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
174e267cb96SDavid S. Miller 
175e267cb96SDavid S. Miller 	__be16					vlan_proto;
176e267cb96SDavid S. Miller 	u16					vlan_id;
177e267cb96SDavid S. Miller 	u16					flags;
178e267cb96SDavid S. Miller 
179e267cb96SDavid S. Miller 	struct net_device			*real_dev;
180e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
181e267cb96SDavid S. Miller 
182e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
183e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
184e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
185e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
186e267cb96SDavid S. Miller #endif
187e267cb96SDavid S. Miller };
188e267cb96SDavid S. Miller 
189e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
190e267cb96SDavid S. Miller {
191e267cb96SDavid S. Miller 	return netdev_priv(dev);
192e267cb96SDavid S. Miller }
193e267cb96SDavid S. Miller 
194e267cb96SDavid S. Miller static inline u16
195e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
196e267cb96SDavid S. Miller {
197e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
198e267cb96SDavid S. Miller 
199e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
200e267cb96SDavid S. Miller 
201e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
202e267cb96SDavid S. Miller 	while (mp) {
203e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
204e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
205e267cb96SDavid S. Miller 					      * to mask correctly with the
206e267cb96SDavid S. Miller 					      * VLAN's TCI */
207e267cb96SDavid S. Miller 		}
208e267cb96SDavid S. Miller 		mp = mp->next;
209e267cb96SDavid S. Miller 	}
210e267cb96SDavid S. Miller 	return 0;
211e267cb96SDavid S. Miller }
212e267cb96SDavid S. Miller 
21348cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb);
2149b22ea56SPatrick McHardy 
21580d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
21680d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
21787002b03SJiri Pirko 
218348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev,
219348a1443SJiri Pirko 				const struct net_device *by_dev);
220348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev,
221348a1443SJiri Pirko 				 const struct net_device *by_dev);
2229b361c13SJiri Pirko 
2239b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev);
224e1618d46SVlad Yasevich 
2257750f403SPatrick McHardy #else
226cec9c133SJiri Pirko static inline struct net_device *
227f06c7f9fSdingtianhong __vlan_find_dev_deep_rcu(struct net_device *real_dev,
2289fae27b3SPatrick McHardy 		     __be16 vlan_proto, u16 vlan_id)
229cec9c133SJiri Pirko {
230cec9c133SJiri Pirko 	return NULL;
231cec9c133SJiri Pirko }
232cec9c133SJiri Pirko 
233960abf68SIvan Khoronzhuk static inline int
234960abf68SIvan Khoronzhuk vlan_for_each(struct net_device *dev,
235960abf68SIvan Khoronzhuk 	      int (*action)(struct net_device *dev, int vid, void *arg),
236960abf68SIvan Khoronzhuk 	      void *arg)
237960abf68SIvan Khoronzhuk {
238960abf68SIvan Khoronzhuk 	return 0;
239960abf68SIvan Khoronzhuk }
240960abf68SIvan Khoronzhuk 
24122d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
24222d1ba74SPatrick McHardy {
24322d1ba74SPatrick McHardy 	BUG();
24422d1ba74SPatrick McHardy 	return NULL;
24522d1ba74SPatrick McHardy }
24622d1ba74SPatrick McHardy 
24722d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
24822d1ba74SPatrick McHardy {
24922d1ba74SPatrick McHardy 	BUG();
25022d1ba74SPatrick McHardy 	return 0;
25122d1ba74SPatrick McHardy }
25222d1ba74SPatrick McHardy 
25371e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
25471e415e4Sdingtianhong {
25571e415e4Sdingtianhong 	BUG();
25671e415e4Sdingtianhong 	return 0;
25771e415e4Sdingtianhong }
25871e415e4Sdingtianhong 
259d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
260d3243539SEyal Perry 					       u32 skprio)
261d3243539SEyal Perry {
262d3243539SEyal Perry 	return 0;
263d3243539SEyal Perry }
264d3243539SEyal Perry 
26548cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb)
2669b22ea56SPatrick McHardy {
2673701e513SJesse Gross 	return false;
2689b22ea56SPatrick McHardy }
269e1c096e2SHerbert Xu 
2709fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
27187002b03SJiri Pirko {
27287002b03SJiri Pirko 	return 0;
27387002b03SJiri Pirko }
27487002b03SJiri Pirko 
2759fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
27687002b03SJiri Pirko {
27787002b03SJiri Pirko }
278348a1443SJiri Pirko 
279348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
280348a1443SJiri Pirko 				       const struct net_device *by_dev)
281348a1443SJiri Pirko {
282348a1443SJiri Pirko 	return 0;
283348a1443SJiri Pirko }
284348a1443SJiri Pirko 
285348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
286348a1443SJiri Pirko 					const struct net_device *by_dev)
287348a1443SJiri Pirko {
288348a1443SJiri Pirko }
2899b361c13SJiri Pirko 
2909b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
2919b361c13SJiri Pirko {
2929b361c13SJiri Pirko 	return false;
2939b361c13SJiri Pirko }
2947750f403SPatrick McHardy #endif
2951da177e4SLinus Torvalds 
296fe19c4f9SEric Garver /**
297fe19c4f9SEric Garver  * eth_type_vlan - check for valid vlan ether type.
298fe19c4f9SEric Garver  * @ethertype: ether type to check
299fe19c4f9SEric Garver  *
300fe19c4f9SEric Garver  * Returns true if the ether type is a vlan ether type.
301fe19c4f9SEric Garver  */
302fe19c4f9SEric Garver static inline bool eth_type_vlan(__be16 ethertype)
303fe19c4f9SEric Garver {
304fe19c4f9SEric Garver 	switch (ethertype) {
305fe19c4f9SEric Garver 	case htons(ETH_P_8021Q):
306fe19c4f9SEric Garver 	case htons(ETH_P_8021AD):
307fe19c4f9SEric Garver 		return true;
308fe19c4f9SEric Garver 	default:
309fe19c4f9SEric Garver 		return false;
310fe19c4f9SEric Garver 	}
311fe19c4f9SEric Garver }
312fe19c4f9SEric Garver 
31386a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
31486a9bad3SPatrick McHardy 					   __be16 proto)
31586a9bad3SPatrick McHardy {
31686a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
31786a9bad3SPatrick McHardy 		return true;
3188ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
3198ad227ffSPatrick McHardy 		return true;
32086a9bad3SPatrick McHardy 	return false;
32186a9bad3SPatrick McHardy }
32286a9bad3SPatrick McHardy 
3239bb8582eSPatrick McHardy /**
324cbe7128cSToshiaki Makita  * __vlan_insert_inner_tag - inner VLAN tag inserting
325cbe7128cSToshiaki Makita  * @skb: skbuff to tag
326cbe7128cSToshiaki Makita  * @vlan_proto: VLAN encapsulation protocol
327cbe7128cSToshiaki Makita  * @vlan_tci: VLAN TCI to insert
328cbe7128cSToshiaki Makita  * @mac_len: MAC header length including outer vlan headers
329cbe7128cSToshiaki Makita  *
330cbe7128cSToshiaki Makita  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
3318051ac76SThadeu Lima de Souza Cascardo  * Returns error if skb_cow_head fails.
332cbe7128cSToshiaki Makita  *
333cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
334cbe7128cSToshiaki Makita  */
335cbe7128cSToshiaki Makita static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
336cbe7128cSToshiaki Makita 					  __be16 vlan_proto, u16 vlan_tci,
337cbe7128cSToshiaki Makita 					  unsigned int mac_len)
338cbe7128cSToshiaki Makita {
339cbe7128cSToshiaki Makita 	struct vlan_ethhdr *veth;
340cbe7128cSToshiaki Makita 
341cbe7128cSToshiaki Makita 	if (skb_cow_head(skb, VLAN_HLEN) < 0)
342cbe7128cSToshiaki Makita 		return -ENOMEM;
343cbe7128cSToshiaki Makita 
344cbe7128cSToshiaki Makita 	skb_push(skb, VLAN_HLEN);
345cbe7128cSToshiaki Makita 
346cbe7128cSToshiaki Makita 	/* Move the mac header sans proto to the beginning of the new header. */
347c769accdSToshiaki Makita 	if (likely(mac_len > ETH_TLEN))
348cbe7128cSToshiaki Makita 		memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
349cbe7128cSToshiaki Makita 	skb->mac_header -= VLAN_HLEN;
350cbe7128cSToshiaki Makita 
351cbe7128cSToshiaki Makita 	veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
352cbe7128cSToshiaki Makita 
353cbe7128cSToshiaki Makita 	/* first, the ethernet type */
354c769accdSToshiaki Makita 	if (likely(mac_len >= ETH_TLEN)) {
355c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should already be populated, and
356c769accdSToshiaki Makita 		 * skb->data has space for h_vlan_proto
357c769accdSToshiaki Makita 		 */
358cbe7128cSToshiaki Makita 		veth->h_vlan_proto = vlan_proto;
359c769accdSToshiaki Makita 	} else {
360c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should not be populated, and
361c769accdSToshiaki Makita 		 * skb->data has no space for h_vlan_proto
362c769accdSToshiaki Makita 		 */
363c769accdSToshiaki Makita 		veth->h_vlan_encapsulated_proto = skb->protocol;
364c769accdSToshiaki Makita 	}
365cbe7128cSToshiaki Makita 
366cbe7128cSToshiaki Makita 	/* now, the TCI */
367cbe7128cSToshiaki Makita 	veth->h_vlan_TCI = htons(vlan_tci);
368cbe7128cSToshiaki Makita 
369cbe7128cSToshiaki Makita 	return 0;
370cbe7128cSToshiaki Makita }
371cbe7128cSToshiaki Makita 
372cbe7128cSToshiaki Makita /**
37315255a43SJiri Pirko  * __vlan_insert_tag - regular VLAN tag inserting
37415255a43SJiri Pirko  * @skb: skbuff to tag
37515255a43SJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
37615255a43SJiri Pirko  * @vlan_tci: VLAN TCI to insert
37715255a43SJiri Pirko  *
37815255a43SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3798051ac76SThadeu Lima de Souza Cascardo  * Returns error if skb_cow_head fails.
38015255a43SJiri Pirko  *
38115255a43SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
38215255a43SJiri Pirko  */
38315255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb,
38415255a43SJiri Pirko 				    __be16 vlan_proto, u16 vlan_tci)
38515255a43SJiri Pirko {
386cbe7128cSToshiaki Makita 	return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
387cbe7128cSToshiaki Makita }
38815255a43SJiri Pirko 
389cbe7128cSToshiaki Makita /**
390cbe7128cSToshiaki Makita  * vlan_insert_inner_tag - inner VLAN tag inserting
391cbe7128cSToshiaki Makita  * @skb: skbuff to tag
392cbe7128cSToshiaki Makita  * @vlan_proto: VLAN encapsulation protocol
393cbe7128cSToshiaki Makita  * @vlan_tci: VLAN TCI to insert
394cbe7128cSToshiaki Makita  * @mac_len: MAC header length including outer vlan headers
395cbe7128cSToshiaki Makita  *
396cbe7128cSToshiaki Makita  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
397cbe7128cSToshiaki Makita  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
398cbe7128cSToshiaki Makita  *
399cbe7128cSToshiaki Makita  * Following the skb_unshare() example, in case of error, the calling function
400cbe7128cSToshiaki Makita  * doesn't have to worry about freeing the original skb.
401cbe7128cSToshiaki Makita  *
402cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
403cbe7128cSToshiaki Makita  */
404cbe7128cSToshiaki Makita static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
405cbe7128cSToshiaki Makita 						    __be16 vlan_proto,
406cbe7128cSToshiaki Makita 						    u16 vlan_tci,
407cbe7128cSToshiaki Makita 						    unsigned int mac_len)
408cbe7128cSToshiaki Makita {
409cbe7128cSToshiaki Makita 	int err;
41015255a43SJiri Pirko 
411cbe7128cSToshiaki Makita 	err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
412cbe7128cSToshiaki Makita 	if (err) {
413cbe7128cSToshiaki Makita 		dev_kfree_skb_any(skb);
414cbe7128cSToshiaki Makita 		return NULL;
415cbe7128cSToshiaki Makita 	}
416cbe7128cSToshiaki Makita 	return skb;
41715255a43SJiri Pirko }
41815255a43SJiri Pirko 
41915255a43SJiri Pirko /**
4200b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
4211da177e4SLinus Torvalds  * @skb: skbuff to tag
42286a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
4239bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
4261da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
4291da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
4300b5c9db1SJiri Pirko  *
4310b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
4321da177e4SLinus Torvalds  */
43386a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
43486a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
4351da177e4SLinus Torvalds {
436cbe7128cSToshiaki Makita 	return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
4370b5c9db1SJiri Pirko }
4381da177e4SLinus Torvalds 
4390b5c9db1SJiri Pirko /**
44062749e2cSJiri Pirko  * vlan_insert_tag_set_proto - regular VLAN tag inserting
4410b5c9db1SJiri Pirko  * @skb: skbuff to tag
44262749e2cSJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
4430b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
4440b5c9db1SJiri Pirko  *
4450b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
4460b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
4470b5c9db1SJiri Pirko  *
4480b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
4490b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
4500b5c9db1SJiri Pirko  */
45162749e2cSJiri Pirko static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
45262749e2cSJiri Pirko 							__be16 vlan_proto,
45362749e2cSJiri Pirko 							u16 vlan_tci)
4540b5c9db1SJiri Pirko {
45586a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
4560b5c9db1SJiri Pirko 	if (skb)
45786a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
4581da177e4SLinus Torvalds 	return skb;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
461c8accd5aSMichał Mirosław /**
462c8accd5aSMichał Mirosław  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
463c8accd5aSMichał Mirosław  * @skb: skbuff to clear
464c8accd5aSMichał Mirosław  *
465c8accd5aSMichał Mirosław  * Clears the VLAN information from @skb
466c8accd5aSMichał Mirosław  */
467c8accd5aSMichał Mirosław static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
468c8accd5aSMichał Mirosław {
4690c4b2d37SMichał Mirosław 	skb->vlan_present = 0;
470c8accd5aSMichał Mirosław }
471c8accd5aSMichał Mirosław 
472e0a6b809SMichał Mirosław /**
473e0a6b809SMichał Mirosław  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
474e0a6b809SMichał Mirosław  * @dst: skbuff to copy to
475e0a6b809SMichał Mirosław  * @src: skbuff to copy from
476e0a6b809SMichał Mirosław  *
477e0a6b809SMichał Mirosław  * Copies VLAN information from @src to @dst (for branchless code)
478e0a6b809SMichał Mirosław  */
479e0a6b809SMichał Mirosław static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
480e0a6b809SMichał Mirosław {
4810c4b2d37SMichał Mirosław 	dst->vlan_present = src->vlan_present;
482e0a6b809SMichał Mirosław 	dst->vlan_proto = src->vlan_proto;
483e0a6b809SMichał Mirosław 	dst->vlan_tci = src->vlan_tci;
484e0a6b809SMichał Mirosław }
485e0a6b809SMichał Mirosław 
4865968250cSJiri Pirko /*
4875968250cSJiri Pirko  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
4885968250cSJiri Pirko  * @skb: skbuff to tag
4895968250cSJiri Pirko  *
4905968250cSJiri Pirko  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
4915968250cSJiri Pirko  *
4925968250cSJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
4935968250cSJiri Pirko  * doesn't have to worry about freeing the original skb.
4945968250cSJiri Pirko  */
4955968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
4965968250cSJiri Pirko {
4975968250cSJiri Pirko 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
498df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
4995968250cSJiri Pirko 	if (likely(skb))
500c8accd5aSMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
5015968250cSJiri Pirko 	return skb;
5025968250cSJiri Pirko }
5035968250cSJiri Pirko 
5041da177e4SLinus Torvalds /**
5051da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
5061da177e4SLinus Torvalds  * @skb: skbuff to tag
50786a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
5089bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
5091da177e4SLinus Torvalds  *
5106aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
5111da177e4SLinus Torvalds  */
512b960a0acSJiri Pirko static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
513b960a0acSJiri Pirko 					  __be16 vlan_proto, u16 vlan_tci)
5141da177e4SLinus Torvalds {
51586a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
5160c4b2d37SMichał Mirosław 	skb->vlan_tci = vlan_tci;
5170c4b2d37SMichał Mirosław 	skb->vlan_present = 1;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds /**
5211da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
5221da177e4SLinus Torvalds  * @skb: skbuff to query
523f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5241da177e4SLinus Torvalds  *
5251da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
5261da177e4SLinus Torvalds  */
5279bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
5301da177e4SLinus Torvalds 
531fe19c4f9SEric Garver 	if (!eth_type_vlan(veth->h_vlan_proto))
5321da177e4SLinus Torvalds 		return -EINVAL;
5331da177e4SLinus Torvalds 
5349bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
5351da177e4SLinus Torvalds 	return 0;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /**
5391da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
5401da177e4SLinus Torvalds  * @skb: skbuff to query
541f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5421da177e4SLinus Torvalds  *
5436aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
5441da177e4SLinus Torvalds  */
54518149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
5469bb8582eSPatrick McHardy 					 u16 *vlan_tci)
5471da177e4SLinus Torvalds {
548df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
549df8a39deSJiri Pirko 		*vlan_tci = skb_vlan_tag_get(skb);
5501da177e4SLinus Torvalds 		return 0;
5511da177e4SLinus Torvalds 	} else {
5529bb8582eSPatrick McHardy 		*vlan_tci = 0;
5531da177e4SLinus Torvalds 		return -EINVAL;
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /**
5581da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
5591da177e4SLinus Torvalds  * @skb: skbuff to query
560f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5611da177e4SLinus Torvalds  *
5621da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
5631da177e4SLinus Torvalds  */
5649bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5651da177e4SLinus Torvalds {
566f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
5679bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
5681da177e4SLinus Torvalds 	} else {
5699bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5730a85df00SHao Zheng /**
5740a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
5750a85df00SHao Zheng  * @skb: skbuff to query
576d4bcef3fSToshiaki Makita  * @type: first vlan protocol
577d4bcef3fSToshiaki Makita  * @depth: buffer to store length of eth and vlan tags in bytes
5780a85df00SHao Zheng  *
5790a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
5800a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
5810a85df00SHao Zheng  */
582*469aceddSToke Høiland-Jørgensen static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
583d4bcef3fSToshiaki Makita 					 int *depth)
5840a85df00SHao Zheng {
585*469aceddSToke Høiland-Jørgensen 	unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
5860a85df00SHao Zheng 
587d4bcef3fSToshiaki Makita 	/* if type is 802.1Q/AD then the header should already be
588d4bcef3fSToshiaki Makita 	 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
589d4bcef3fSToshiaki Makita 	 * ETH_HLEN otherwise
590d4bcef3fSToshiaki Makita 	 */
591fe19c4f9SEric Garver 	if (eth_type_vlan(type)) {
592d4bcef3fSToshiaki Makita 		if (vlan_depth) {
593d4bcef3fSToshiaki Makita 			if (WARN_ON(vlan_depth < VLAN_HLEN))
594d4bcef3fSToshiaki Makita 				return 0;
595d4bcef3fSToshiaki Makita 			vlan_depth -= VLAN_HLEN;
596d4bcef3fSToshiaki Makita 		} else {
597d4bcef3fSToshiaki Makita 			vlan_depth = ETH_HLEN;
598d4bcef3fSToshiaki Makita 		}
599d4bcef3fSToshiaki Makita 		do {
600*469aceddSToke Høiland-Jørgensen 			struct vlan_hdr vhdr, *vh;
601d4bcef3fSToshiaki Makita 
602*469aceddSToke Høiland-Jørgensen 			vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
603*469aceddSToke Høiland-Jørgensen 			if (unlikely(!vh || !--parse_depth))
604d4bcef3fSToshiaki Makita 				return 0;
605d4bcef3fSToshiaki Makita 
606d4bcef3fSToshiaki Makita 			type = vh->h_vlan_encapsulated_proto;
607d4bcef3fSToshiaki Makita 			vlan_depth += VLAN_HLEN;
608fe19c4f9SEric Garver 		} while (eth_type_vlan(type));
6090a85df00SHao Zheng 	}
6100a85df00SHao Zheng 
611d4bcef3fSToshiaki Makita 	if (depth)
612d4bcef3fSToshiaki Makita 		*depth = vlan_depth;
613d4bcef3fSToshiaki Makita 
614d4bcef3fSToshiaki Makita 	return type;
615d4bcef3fSToshiaki Makita }
616d4bcef3fSToshiaki Makita 
617d4bcef3fSToshiaki Makita /**
618d4bcef3fSToshiaki Makita  * vlan_get_protocol - get protocol EtherType.
619d4bcef3fSToshiaki Makita  * @skb: skbuff to query
620d4bcef3fSToshiaki Makita  *
621d4bcef3fSToshiaki Makita  * Returns the EtherType of the packet, regardless of whether it is
622d4bcef3fSToshiaki Makita  * vlan encapsulated (normal or hardware accelerated) or not.
623d4bcef3fSToshiaki Makita  */
624*469aceddSToke Høiland-Jørgensen static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
625d4bcef3fSToshiaki Makita {
626d4bcef3fSToshiaki Makita 	return __vlan_get_protocol(skb, skb->protocol, NULL);
6270a85df00SHao Zheng }
628396cf943SPravin B Shelar 
629*469aceddSToke Høiland-Jørgensen /* A getter for the SKB protocol field which will handle VLAN tags consistently
630*469aceddSToke Høiland-Jørgensen  * whether VLAN acceleration is enabled or not.
631*469aceddSToke Høiland-Jørgensen  */
632*469aceddSToke Høiland-Jørgensen static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
633*469aceddSToke Høiland-Jørgensen {
634*469aceddSToke Høiland-Jørgensen 	if (!skip_vlan)
635*469aceddSToke Høiland-Jørgensen 		/* VLAN acceleration strips the VLAN header from the skb and
636*469aceddSToke Høiland-Jørgensen 		 * moves it to skb->vlan_proto
637*469aceddSToke Høiland-Jørgensen 		 */
638*469aceddSToke Høiland-Jørgensen 		return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
639*469aceddSToke Høiland-Jørgensen 
640*469aceddSToke Høiland-Jørgensen 	return vlan_get_protocol(skb);
641*469aceddSToke Høiland-Jørgensen }
642*469aceddSToke Høiland-Jørgensen 
643396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
644396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
645396cf943SPravin B Shelar {
646396cf943SPravin B Shelar 	__be16 proto;
647da8c8724SCong Wang 	unsigned short *rawp;
648396cf943SPravin B Shelar 
649396cf943SPravin B Shelar 	/*
650396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
651396cf943SPravin B Shelar 	 * three protocols care about.
652396cf943SPravin B Shelar 	 */
653396cf943SPravin B Shelar 
654396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
6559545b22dSAlexander Duyck 	if (eth_proto_is_802_3(proto)) {
656396cf943SPravin B Shelar 		skb->protocol = proto;
657396cf943SPravin B Shelar 		return;
658396cf943SPravin B Shelar 	}
659396cf943SPravin B Shelar 
660da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
661da8c8724SCong Wang 	if (*rawp == 0xFFFF)
662396cf943SPravin B Shelar 		/*
663396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
664396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
665396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
666396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
667396cf943SPravin B Shelar 		 * but does for the rest.
668396cf943SPravin B Shelar 		 */
669396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
670396cf943SPravin B Shelar 	else
671396cf943SPravin B Shelar 		/*
672396cf943SPravin B Shelar 		 * Real 802.2 LLC
673396cf943SPravin B Shelar 		 */
674396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
675396cf943SPravin B Shelar }
67644a40855SVlad Yasevich 
677f5a7fb88SToshiaki Makita /**
678f5a7fb88SToshiaki Makita  * skb_vlan_tagged - check if skb is vlan tagged.
679f5a7fb88SToshiaki Makita  * @skb: skbuff to query
680f5a7fb88SToshiaki Makita  *
681f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged, regardless of whether it is hardware
682f5a7fb88SToshiaki Makita  * accelerated or not.
683f5a7fb88SToshiaki Makita  */
684f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb)
685f5a7fb88SToshiaki Makita {
686f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb) &&
687fe19c4f9SEric Garver 	    likely(!eth_type_vlan(skb->protocol)))
688f5a7fb88SToshiaki Makita 		return false;
689f5a7fb88SToshiaki Makita 
690f5a7fb88SToshiaki Makita 	return true;
691f5a7fb88SToshiaki Makita }
692f5a7fb88SToshiaki Makita 
693f5a7fb88SToshiaki Makita /**
694f5a7fb88SToshiaki Makita  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
695f5a7fb88SToshiaki Makita  * @skb: skbuff to query
696f5a7fb88SToshiaki Makita  *
697f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged with multiple vlan headers, regardless
698f5a7fb88SToshiaki Makita  * of whether it is hardware accelerated or not.
699f5a7fb88SToshiaki Makita  */
7007ce23672SToshiaki Makita static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
701f5a7fb88SToshiaki Makita {
702f5a7fb88SToshiaki Makita 	__be16 protocol = skb->protocol;
703f5a7fb88SToshiaki Makita 
704f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb)) {
705f5a7fb88SToshiaki Makita 		struct vlan_ethhdr *veh;
706f5a7fb88SToshiaki Makita 
707fe19c4f9SEric Garver 		if (likely(!eth_type_vlan(protocol)))
708f5a7fb88SToshiaki Makita 			return false;
709f5a7fb88SToshiaki Makita 
7107ce23672SToshiaki Makita 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
7117ce23672SToshiaki Makita 			return false;
7127ce23672SToshiaki Makita 
713f5a7fb88SToshiaki Makita 		veh = (struct vlan_ethhdr *)skb->data;
714f5a7fb88SToshiaki Makita 		protocol = veh->h_vlan_encapsulated_proto;
715f5a7fb88SToshiaki Makita 	}
716f5a7fb88SToshiaki Makita 
717fe19c4f9SEric Garver 	if (!eth_type_vlan(protocol))
718f5a7fb88SToshiaki Makita 		return false;
719f5a7fb88SToshiaki Makita 
720f5a7fb88SToshiaki Makita 	return true;
721f5a7fb88SToshiaki Makita }
722f5a7fb88SToshiaki Makita 
7238cb65d00SToshiaki Makita /**
7248cb65d00SToshiaki Makita  * vlan_features_check - drop unsafe features for skb with multiple tags.
7258cb65d00SToshiaki Makita  * @skb: skbuff to query
7268cb65d00SToshiaki Makita  * @features: features to be checked
7278cb65d00SToshiaki Makita  *
7288cb65d00SToshiaki Makita  * Returns features without unsafe ones if the skb has multiple tags.
7298cb65d00SToshiaki Makita  */
7307ce23672SToshiaki Makita static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
7318cb65d00SToshiaki Makita 						    netdev_features_t features)
7328cb65d00SToshiaki Makita {
73335d2f80bSVlad Yasevich 	if (skb_vlan_tagged_multi(skb)) {
73435d2f80bSVlad Yasevich 		/* In the case of multi-tagged packets, use a direct mask
73535d2f80bSVlad Yasevich 		 * instead of using netdev_interesect_features(), to make
73635d2f80bSVlad Yasevich 		 * sure that only devices supporting NETIF_F_HW_CSUM will
73735d2f80bSVlad Yasevich 		 * have checksum offloading support.
73835d2f80bSVlad Yasevich 		 */
73935d2f80bSVlad Yasevich 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
74035d2f80bSVlad Yasevich 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
74135d2f80bSVlad Yasevich 			    NETIF_F_HW_VLAN_STAG_TX;
74235d2f80bSVlad Yasevich 	}
7438cb65d00SToshiaki Makita 
7448cb65d00SToshiaki Makita 	return features;
7458cb65d00SToshiaki Makita }
7468cb65d00SToshiaki Makita 
74766e5133fSToshiaki Makita /**
74866e5133fSToshiaki Makita  * compare_vlan_header - Compare two vlan headers
74966e5133fSToshiaki Makita  * @h1: Pointer to vlan header
75066e5133fSToshiaki Makita  * @h2: Pointer to vlan header
75166e5133fSToshiaki Makita  *
75266e5133fSToshiaki Makita  * Compare two vlan headers, returns 0 if equal.
75366e5133fSToshiaki Makita  *
75466e5133fSToshiaki Makita  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
75566e5133fSToshiaki Makita  */
75666e5133fSToshiaki Makita static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
75766e5133fSToshiaki Makita 						const struct vlan_hdr *h2)
75866e5133fSToshiaki Makita {
75966e5133fSToshiaki Makita #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
76066e5133fSToshiaki Makita 	return *(u32 *)h1 ^ *(u32 *)h2;
76166e5133fSToshiaki Makita #else
76266e5133fSToshiaki Makita 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
76366e5133fSToshiaki Makita 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
76466e5133fSToshiaki Makita 		(__force u32)h2->h_vlan_encapsulated_proto);
76566e5133fSToshiaki Makita #endif
76666e5133fSToshiaki Makita }
7671da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
768