xref: /linux/include/linux/if_vlan.h (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
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 
28469aceddSToke Høiland-Jørgensen #define VLAN_MAX_DEPTH	8		/* Max. number of nested VLAN tags parsed */
29469aceddSToke 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 {
496d5c900eSKees Cook 	struct_group(addrs,
50740c15d0SPatrick McHardy 		unsigned char	h_dest[ETH_ALEN];
51740c15d0SPatrick McHardy 		unsigned char	h_source[ETH_ALEN];
526d5c900eSKees Cook 	);
53740c15d0SPatrick McHardy 	__be16		h_vlan_proto;
54740c15d0SPatrick McHardy 	__be16		h_vlan_TCI;
55740c15d0SPatrick McHardy 	__be16		h_vlan_encapsulated_proto;
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <linux/skbuff.h>
591da177e4SLinus Torvalds 
vlan_eth_hdr(const struct sk_buff * skb)601da177e4SLinus Torvalds static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
611da177e4SLinus Torvalds {
6298e399f8SArnaldo Carvalho de Melo 	return (struct vlan_ethhdr *)skb_mac_header(skb);
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651f5020acSVladimir Oltean /* Prefer this version in TX path, instead of
661f5020acSVladimir Oltean  * skb_reset_mac_header() + vlan_eth_hdr()
671f5020acSVladimir Oltean  */
skb_vlan_eth_hdr(const struct sk_buff * skb)681f5020acSVladimir Oltean static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
691f5020acSVladimir Oltean {
701f5020acSVladimir Oltean 	return (struct vlan_ethhdr *)skb->data;
711f5020acSVladimir Oltean }
721f5020acSVladimir Oltean 
7305423b24SEric Dumazet #define VLAN_PRIO_MASK		0xe000 /* Priority Code Point */
7405423b24SEric Dumazet #define VLAN_PRIO_SHIFT		13
75a2e768b8SMichał Mirosław #define VLAN_CFI_MASK		0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
7605423b24SEric Dumazet #define VLAN_VID_MASK		0x0fff /* VLAN Identifier */
77b738127dSJesse Gross #define VLAN_N_VID		4096
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* found in socket.c */
80881d966bSEric W. Biederman extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
811da177e4SLinus Torvalds 
is_vlan_dev(const struct net_device * dev)82b618aaa9SJiri Pirko static inline bool is_vlan_dev(const struct net_device *dev)
836dcbbe25SNeil Horman {
846dcbbe25SNeil Horman         return dev->priv_flags & IFF_802_1Q_VLAN;
856dcbbe25SNeil Horman }
866dcbbe25SNeil Horman 
87354259faSEric Dumazet #define skb_vlan_tag_present(__skb)	(!!(__skb)->vlan_all)
880c4b2d37SMichał Mirosław #define skb_vlan_tag_get(__skb)		((__skb)->vlan_tci)
89df8a39deSJiri Pirko #define skb_vlan_tag_get_id(__skb)	((__skb)->vlan_tci & VLAN_VID_MASK)
90a2e768b8SMichał Mirosław #define skb_vlan_tag_get_cfi(__skb)	(!!((__skb)->vlan_tci & VLAN_CFI_MASK))
919b319148SMichał Mirosław #define skb_vlan_tag_get_prio(__skb)	(((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
921da177e4SLinus Torvalds 
vlan_get_rx_ctag_filter_info(struct net_device * dev)939daae9bdSGal Pressman static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
949daae9bdSGal Pressman {
959daae9bdSGal Pressman 	ASSERT_RTNL();
969daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
979daae9bdSGal Pressman }
989daae9bdSGal Pressman 
vlan_drop_rx_ctag_filter_info(struct net_device * dev)999daae9bdSGal Pressman static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
1009daae9bdSGal Pressman {
1019daae9bdSGal Pressman 	ASSERT_RTNL();
1029daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
1039daae9bdSGal Pressman }
1049daae9bdSGal Pressman 
vlan_get_rx_stag_filter_info(struct net_device * dev)1059daae9bdSGal Pressman static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
1069daae9bdSGal Pressman {
1079daae9bdSGal Pressman 	ASSERT_RTNL();
1089daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
1099daae9bdSGal Pressman }
1109daae9bdSGal Pressman 
vlan_drop_rx_stag_filter_info(struct net_device * dev)1119daae9bdSGal Pressman static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
1129daae9bdSGal Pressman {
1139daae9bdSGal Pressman 	ASSERT_RTNL();
1149daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
1159daae9bdSGal Pressman }
1169daae9bdSGal Pressman 
117e267cb96SDavid S. Miller /**
118e267cb96SDavid S. Miller  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
119e267cb96SDavid S. Miller  *	@rx_packets: number of received packets
120e267cb96SDavid S. Miller  *	@rx_bytes: number of received bytes
121e267cb96SDavid S. Miller  *	@rx_multicast: number of received multicast packets
122e267cb96SDavid S. Miller  *	@tx_packets: number of transmitted packets
123e267cb96SDavid S. Miller  *	@tx_bytes: number of transmitted bytes
124e267cb96SDavid S. Miller  *	@syncp: synchronization point for 64bit counters
125e267cb96SDavid S. Miller  *	@rx_errors: number of rx errors
126e267cb96SDavid S. Miller  *	@tx_dropped: number of tx drops
127e267cb96SDavid S. Miller  */
128e267cb96SDavid S. Miller struct vlan_pcpu_stats {
12909cca53cSEric Dumazet 	u64_stats_t		rx_packets;
13009cca53cSEric Dumazet 	u64_stats_t		rx_bytes;
13109cca53cSEric Dumazet 	u64_stats_t		rx_multicast;
13209cca53cSEric Dumazet 	u64_stats_t		tx_packets;
13309cca53cSEric Dumazet 	u64_stats_t		tx_bytes;
134e267cb96SDavid S. Miller 	struct u64_stats_sync	syncp;
135e267cb96SDavid S. Miller 	u32			rx_errors;
136e267cb96SDavid S. Miller 	u32			tx_dropped;
137e267cb96SDavid S. Miller };
138e267cb96SDavid S. Miller 
1391e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1401e85c9b6SHannes Frederic Sowa 
141f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
1421e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
143960abf68SIvan Khoronzhuk extern int vlan_for_each(struct net_device *dev,
144960abf68SIvan Khoronzhuk 			 int (*action)(struct net_device *dev, int vid,
145960abf68SIvan Khoronzhuk 				       void *arg), void *arg);
1461e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
1471e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
14871e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
1491e85c9b6SHannes Frederic Sowa 
1501e85c9b6SHannes Frederic Sowa /**
1511e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
1521e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
1531e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
1541e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
1551e85c9b6SHannes Frederic Sowa  */
1561e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
1571e85c9b6SHannes Frederic Sowa 	u32					priority;
1581e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
1591e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
1601e85c9b6SHannes Frederic Sowa };
1611e85c9b6SHannes Frederic Sowa 
162e267cb96SDavid S. Miller struct proc_dir_entry;
163e267cb96SDavid S. Miller struct netpoll;
164e267cb96SDavid S. Miller 
165e267cb96SDavid S. Miller /**
166e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
167e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
168e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
169e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
170e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
171e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
172e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
173e267cb96SDavid S. Miller  *	@flags: device flags
174e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
17519c9ebf6SEric Dumazet  *	@dev_tracker: refcount tracker for @real_dev reference
176e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
177e267cb96SDavid S. Miller  *	@dent: proc dir entry
178e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
179e267cb96SDavid S. Miller  *	@netpoll: netpoll instance "propagated" down to @real_dev
180e267cb96SDavid S. Miller  */
181e267cb96SDavid S. Miller struct vlan_dev_priv {
182e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
183e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
184e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
185e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
186e267cb96SDavid S. Miller 
187e267cb96SDavid S. Miller 	__be16					vlan_proto;
188e267cb96SDavid S. Miller 	u16					vlan_id;
189e267cb96SDavid S. Miller 	u16					flags;
190e267cb96SDavid S. Miller 
19119c9ebf6SEric Dumazet 	struct net_device			*real_dev;
19219c9ebf6SEric Dumazet 	netdevice_tracker			dev_tracker;
193e267cb96SDavid S. Miller 
194e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
195e267cb96SDavid S. Miller 
196e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
197e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
198e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
199e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
200e267cb96SDavid S. Miller #endif
201e267cb96SDavid S. Miller };
202e267cb96SDavid S. Miller 
vlan_dev_priv(const struct net_device * dev)203e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
204e267cb96SDavid S. Miller {
205e267cb96SDavid S. Miller 	return netdev_priv(dev);
206e267cb96SDavid S. Miller }
207e267cb96SDavid S. Miller 
208e267cb96SDavid S. Miller static inline u16
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)209e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
210e267cb96SDavid S. Miller {
211e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
212e267cb96SDavid S. Miller 
213e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
214e267cb96SDavid S. Miller 
215e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
216e267cb96SDavid S. Miller 	while (mp) {
217e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
218e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
219e267cb96SDavid S. Miller 					      * to mask correctly with the
220e267cb96SDavid S. Miller 					      * VLAN's TCI */
221e267cb96SDavid S. Miller 		}
222e267cb96SDavid S. Miller 		mp = mp->next;
223e267cb96SDavid S. Miller 	}
224e267cb96SDavid S. Miller 	return 0;
225e267cb96SDavid S. Miller }
22648cc32d3SFlorian Zumbiehl 
2279b22ea56SPatrick McHardy extern bool vlan_do_receive(struct sk_buff **skb);
22880d5c368SPatrick McHardy 
22980d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
23087002b03SJiri Pirko extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
231348a1443SJiri Pirko 
232348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev,
233348a1443SJiri Pirko 				const struct net_device *by_dev);
234348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev,
2359b361c13SJiri Pirko 				 const struct net_device *by_dev);
2369b361c13SJiri Pirko 
237e1618d46SVlad Yasevich extern bool vlan_uses_dev(const struct net_device *dev);
2387750f403SPatrick McHardy 
239cec9c133SJiri Pirko #else
240f06c7f9fSdingtianhong static inline struct net_device *
__vlan_find_dev_deep_rcu(struct net_device * real_dev,__be16 vlan_proto,u16 vlan_id)2419fae27b3SPatrick McHardy __vlan_find_dev_deep_rcu(struct net_device *real_dev,
242cec9c133SJiri Pirko 		     __be16 vlan_proto, u16 vlan_id)
243cec9c133SJiri Pirko {
244cec9c133SJiri Pirko 	return NULL;
245cec9c133SJiri Pirko }
246960abf68SIvan Khoronzhuk 
247960abf68SIvan Khoronzhuk static inline int
vlan_for_each(struct net_device * dev,int (* action)(struct net_device * dev,int vid,void * arg),void * arg)248960abf68SIvan Khoronzhuk vlan_for_each(struct net_device *dev,
249960abf68SIvan Khoronzhuk 	      int (*action)(struct net_device *dev, int vid, void *arg),
250960abf68SIvan Khoronzhuk 	      void *arg)
251960abf68SIvan Khoronzhuk {
252960abf68SIvan Khoronzhuk 	return 0;
253960abf68SIvan Khoronzhuk }
25422d1ba74SPatrick McHardy 
vlan_dev_real_dev(const struct net_device * dev)25522d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
25622d1ba74SPatrick McHardy {
25722d1ba74SPatrick McHardy 	BUG();
25822d1ba74SPatrick McHardy 	return NULL;
25922d1ba74SPatrick McHardy }
26022d1ba74SPatrick McHardy 
vlan_dev_vlan_id(const struct net_device * dev)26122d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
26222d1ba74SPatrick McHardy {
26322d1ba74SPatrick McHardy 	BUG();
26422d1ba74SPatrick McHardy 	return 0;
26522d1ba74SPatrick McHardy }
26671e415e4Sdingtianhong 
vlan_dev_vlan_proto(const struct net_device * dev)26771e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
26871e415e4Sdingtianhong {
26971e415e4Sdingtianhong 	BUG();
27071e415e4Sdingtianhong 	return 0;
27171e415e4Sdingtianhong }
272d3243539SEyal Perry 
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)273d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
274d3243539SEyal Perry 					       u32 skprio)
275d3243539SEyal Perry {
276d3243539SEyal Perry 	return 0;
277d3243539SEyal Perry }
27848cc32d3SFlorian Zumbiehl 
vlan_do_receive(struct sk_buff ** skb)2799b22ea56SPatrick McHardy static inline bool vlan_do_receive(struct sk_buff **skb)
2803701e513SJesse Gross {
2819b22ea56SPatrick McHardy 	return false;
282e1c096e2SHerbert Xu }
2839fae27b3SPatrick McHardy 
vlan_vid_add(struct net_device * dev,__be16 proto,u16 vid)28487002b03SJiri Pirko static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
28587002b03SJiri Pirko {
28687002b03SJiri Pirko 	return 0;
28787002b03SJiri Pirko }
2889fae27b3SPatrick McHardy 
vlan_vid_del(struct net_device * dev,__be16 proto,u16 vid)28987002b03SJiri Pirko static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
29087002b03SJiri Pirko {
291348a1443SJiri Pirko }
292348a1443SJiri Pirko 
vlan_vids_add_by_dev(struct net_device * dev,const struct net_device * by_dev)293348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
294348a1443SJiri Pirko 				       const struct net_device *by_dev)
295348a1443SJiri Pirko {
296348a1443SJiri Pirko 	return 0;
297348a1443SJiri Pirko }
298348a1443SJiri Pirko 
vlan_vids_del_by_dev(struct net_device * dev,const struct net_device * by_dev)299348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
300348a1443SJiri Pirko 					const struct net_device *by_dev)
301348a1443SJiri Pirko {
3029b361c13SJiri Pirko }
3039b361c13SJiri Pirko 
vlan_uses_dev(const struct net_device * dev)3049b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
3059b361c13SJiri Pirko {
3069b361c13SJiri Pirko 	return false;
3077750f403SPatrick McHardy }
3081da177e4SLinus Torvalds #endif
309fe19c4f9SEric Garver 
310fe19c4f9SEric Garver /**
311fe19c4f9SEric Garver  * eth_type_vlan - check for valid vlan ether type.
312fe19c4f9SEric Garver  * @ethertype: ether type to check
313fe19c4f9SEric Garver  *
314fe19c4f9SEric Garver  * Returns: true if the ether type is a vlan ether type.
315fe19c4f9SEric Garver  */
eth_type_vlan(__be16 ethertype)316fe19c4f9SEric Garver static inline bool eth_type_vlan(__be16 ethertype)
317fe19c4f9SEric Garver {
318fe19c4f9SEric Garver 	switch (ethertype) {
319fe19c4f9SEric Garver 	case htons(ETH_P_8021Q):
320fe19c4f9SEric Garver 	case htons(ETH_P_8021AD):
321fe19c4f9SEric Garver 		return true;
322fe19c4f9SEric Garver 	default:
323fe19c4f9SEric Garver 		return false;
324fe19c4f9SEric Garver 	}
325fe19c4f9SEric Garver }
32686a9bad3SPatrick McHardy 
vlan_hw_offload_capable(netdev_features_t features,__be16 proto)32786a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
32886a9bad3SPatrick McHardy 					   __be16 proto)
32986a9bad3SPatrick McHardy {
33086a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
3318ad227ffSPatrick McHardy 		return true;
3328ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
33386a9bad3SPatrick McHardy 		return true;
33486a9bad3SPatrick McHardy 	return false;
33586a9bad3SPatrick McHardy }
3369bb8582eSPatrick McHardy 
337cbe7128cSToshiaki Makita /**
338cbe7128cSToshiaki Makita  * __vlan_insert_inner_tag - inner VLAN tag inserting
339cbe7128cSToshiaki Makita  * @skb: skbuff to tag
340cbe7128cSToshiaki Makita  * @vlan_proto: VLAN encapsulation protocol
341cbe7128cSToshiaki Makita  * @vlan_tci: VLAN TCI to insert
342cbe7128cSToshiaki Makita  * @mac_len: MAC header length including outer vlan headers
343cbe7128cSToshiaki Makita  *
3448051ac76SThadeu Lima de Souza Cascardo  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
345cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
346cbe7128cSToshiaki Makita  *
347cbe7128cSToshiaki Makita  * Returns: error if skb_cow_head fails.
348cbe7128cSToshiaki Makita  */
__vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)349cbe7128cSToshiaki Makita static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
350cbe7128cSToshiaki Makita 					  __be16 vlan_proto, u16 vlan_tci,
351cbe7128cSToshiaki Makita 					  unsigned int mac_len)
352cbe7128cSToshiaki Makita {
353cbe7128cSToshiaki Makita 	struct vlan_ethhdr *veth;
354cbe7128cSToshiaki Makita 
355cbe7128cSToshiaki Makita 	if (skb_cow_head(skb, VLAN_HLEN) < 0)
356cbe7128cSToshiaki Makita 		return -ENOMEM;
357cbe7128cSToshiaki Makita 
358cbe7128cSToshiaki Makita 	skb_push(skb, VLAN_HLEN);
359cbe7128cSToshiaki Makita 
360c769accdSToshiaki Makita 	/* Move the mac header sans proto to the beginning of the new header. */
361cbe7128cSToshiaki Makita 	if (likely(mac_len > ETH_TLEN))
362f90615adSVladimir Oltean 		memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
363cbe7128cSToshiaki Makita 	if (skb_mac_header_was_set(skb))
364cbe7128cSToshiaki Makita 		skb->mac_header -= VLAN_HLEN;
365cbe7128cSToshiaki Makita 
366cbe7128cSToshiaki Makita 	veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
367cbe7128cSToshiaki Makita 
368c769accdSToshiaki Makita 	/* first, the ethernet type */
369c769accdSToshiaki Makita 	if (likely(mac_len >= ETH_TLEN)) {
370c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should already be populated, and
371c769accdSToshiaki Makita 		 * skb->data has space for h_vlan_proto
372cbe7128cSToshiaki Makita 		 */
373c769accdSToshiaki Makita 		veth->h_vlan_proto = vlan_proto;
374c769accdSToshiaki Makita 	} else {
375c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should not be populated, and
376c769accdSToshiaki Makita 		 * skb->data has no space for h_vlan_proto
377c769accdSToshiaki Makita 		 */
378c769accdSToshiaki Makita 		veth->h_vlan_encapsulated_proto = skb->protocol;
379cbe7128cSToshiaki Makita 	}
380cbe7128cSToshiaki Makita 
381cbe7128cSToshiaki Makita 	/* now, the TCI */
382cbe7128cSToshiaki Makita 	veth->h_vlan_TCI = htons(vlan_tci);
383cbe7128cSToshiaki Makita 
384cbe7128cSToshiaki Makita 	return 0;
385cbe7128cSToshiaki Makita }
386cbe7128cSToshiaki Makita 
38715255a43SJiri Pirko /**
38815255a43SJiri Pirko  * __vlan_insert_tag - regular VLAN tag inserting
38915255a43SJiri Pirko  * @skb: skbuff to tag
39015255a43SJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
39115255a43SJiri Pirko  * @vlan_tci: VLAN TCI to insert
39215255a43SJiri Pirko  *
3938051ac76SThadeu Lima de Souza Cascardo  * Inserts the VLAN tag into @skb as part of the payload
39415255a43SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
39515255a43SJiri Pirko  *
39615255a43SJiri Pirko  * Returns: error if skb_cow_head fails.
39715255a43SJiri Pirko  */
__vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)39815255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb,
39915255a43SJiri Pirko 				    __be16 vlan_proto, u16 vlan_tci)
400cbe7128cSToshiaki Makita {
401cbe7128cSToshiaki Makita 	return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
40215255a43SJiri Pirko }
403cbe7128cSToshiaki Makita 
404cbe7128cSToshiaki Makita /**
405cbe7128cSToshiaki Makita  * vlan_insert_inner_tag - inner VLAN tag inserting
406cbe7128cSToshiaki Makita  * @skb: skbuff to tag
407cbe7128cSToshiaki Makita  * @vlan_proto: VLAN encapsulation protocol
408cbe7128cSToshiaki Makita  * @vlan_tci: VLAN TCI to insert
409cbe7128cSToshiaki Makita  * @mac_len: MAC header length including outer vlan headers
410cbe7128cSToshiaki Makita  *
4117740bb88SEric Dumazet  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
412cbe7128cSToshiaki Makita  * Returns a VLAN tagged skb. This might change skb->head.
413cbe7128cSToshiaki Makita  *
414cbe7128cSToshiaki Makita  * Following the skb_unshare() example, in case of error, the calling function
415cbe7128cSToshiaki Makita  * doesn't have to worry about freeing the original skb.
416cbe7128cSToshiaki Makita  *
417cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
418cbe7128cSToshiaki Makita  *
419cbe7128cSToshiaki Makita  * Return: modified @skb on success, NULL on error (@skb is freed).
420cbe7128cSToshiaki Makita  */
vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)421cbe7128cSToshiaki Makita static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
422cbe7128cSToshiaki Makita 						    __be16 vlan_proto,
423cbe7128cSToshiaki Makita 						    u16 vlan_tci,
42415255a43SJiri Pirko 						    unsigned int mac_len)
425cbe7128cSToshiaki Makita {
426cbe7128cSToshiaki Makita 	int err;
427cbe7128cSToshiaki Makita 
428cbe7128cSToshiaki Makita 	err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
429cbe7128cSToshiaki Makita 	if (err) {
430cbe7128cSToshiaki Makita 		dev_kfree_skb_any(skb);
43115255a43SJiri Pirko 		return NULL;
43215255a43SJiri Pirko 	}
43315255a43SJiri Pirko 	return skb;
4340b5c9db1SJiri Pirko }
4351da177e4SLinus Torvalds 
43686a9bad3SPatrick McHardy /**
4379bb8582eSPatrick McHardy  * vlan_insert_tag - regular VLAN tag inserting
4381da177e4SLinus Torvalds  * @skb: skbuff to tag
4391da177e4SLinus Torvalds  * @vlan_proto: VLAN encapsulation protocol
4407740bb88SEric Dumazet  * @vlan_tci: VLAN TCI to insert
4411da177e4SLinus Torvalds  *
4421da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
4431da177e4SLinus Torvalds  * Returns a VLAN tagged skb. This might change skb->head.
4440b5c9db1SJiri Pirko  *
4450b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
4461da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
44786a9bad3SPatrick McHardy  *
44886a9bad3SPatrick McHardy  * Does not change skb->protocol so this function can be used during receive.
4491da177e4SLinus Torvalds  *
450cbe7128cSToshiaki Makita  * Return: modified @skb on success, NULL on error (@skb is freed).
4510b5c9db1SJiri Pirko  */
vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)4521da177e4SLinus Torvalds static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
4530b5c9db1SJiri Pirko 					      __be16 vlan_proto, u16 vlan_tci)
45462749e2cSJiri Pirko {
4550b5c9db1SJiri Pirko 	return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
45662749e2cSJiri Pirko }
4570b5c9db1SJiri Pirko 
4580b5c9db1SJiri Pirko /**
4590b5c9db1SJiri Pirko  * vlan_insert_tag_set_proto - regular VLAN tag inserting
4607740bb88SEric Dumazet  * @skb: skbuff to tag
4610b5c9db1SJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
4620b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
4630b5c9db1SJiri Pirko  *
4640b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
46562749e2cSJiri Pirko  * Returns a VLAN tagged skb. This might change skb->head.
46662749e2cSJiri Pirko  *
46762749e2cSJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
4680b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
46986a9bad3SPatrick McHardy  *
4700b5c9db1SJiri Pirko  * Return: modified @skb on success, NULL on error (@skb is freed).
47186a9bad3SPatrick McHardy  */
vlan_insert_tag_set_proto(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)4721da177e4SLinus Torvalds static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
4731da177e4SLinus Torvalds 							__be16 vlan_proto,
4741da177e4SLinus Torvalds 							u16 vlan_tci)
475c8accd5aSMichał Mirosław {
476c8accd5aSMichał Mirosław 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
477c8accd5aSMichał Mirosław 	if (skb)
478c8accd5aSMichał Mirosław 		skb->protocol = vlan_proto;
479c8accd5aSMichał Mirosław 	return skb;
480c8accd5aSMichał Mirosław }
481c8accd5aSMichał Mirosław 
482c8accd5aSMichał Mirosław /**
483354259faSEric Dumazet  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
484c8accd5aSMichał Mirosław  * @skb: skbuff to clear
485c8accd5aSMichał Mirosław  *
486e0a6b809SMichał Mirosław  * Clears the VLAN information from @skb
487e0a6b809SMichał Mirosław  */
__vlan_hwaccel_clear_tag(struct sk_buff * skb)488e0a6b809SMichał Mirosław static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
489e0a6b809SMichał Mirosław {
490e0a6b809SMichał Mirosław 	skb->vlan_all = 0;
491e0a6b809SMichał Mirosław }
492e0a6b809SMichał Mirosław 
493e0a6b809SMichał Mirosław /**
494e0a6b809SMichał Mirosław  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
495354259faSEric Dumazet  * @dst: skbuff to copy to
496e0a6b809SMichał Mirosław  * @src: skbuff to copy from
497e0a6b809SMichał Mirosław  *
4985968250cSJiri Pirko  * Copies VLAN information from @src to @dst (for branchless code)
4995968250cSJiri Pirko  */
__vlan_hwaccel_copy_tag(struct sk_buff * dst,const struct sk_buff * src)5005968250cSJiri Pirko static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
5015968250cSJiri Pirko {
5025968250cSJiri Pirko 	dst->vlan_all = src->vlan_all;
5035968250cSJiri Pirko }
5045968250cSJiri Pirko 
5055968250cSJiri Pirko /*
5065968250cSJiri Pirko  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
5075968250cSJiri Pirko  * @skb: skbuff to tag
5085968250cSJiri Pirko  *
5095968250cSJiri Pirko  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
510df8a39deSJiri Pirko  *
5115968250cSJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
512c8accd5aSMichał Mirosław  * doesn't have to worry about freeing the original skb.
5135968250cSJiri Pirko  */
__vlan_hwaccel_push_inside(struct sk_buff * skb)5145968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
5155968250cSJiri Pirko {
5161da177e4SLinus Torvalds 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
5171da177e4SLinus Torvalds 					skb_vlan_tag_get(skb));
5181da177e4SLinus Torvalds 	if (likely(skb))
51986a9bad3SPatrick McHardy 		__vlan_hwaccel_clear_tag(skb);
5209bb8582eSPatrick McHardy 	return skb;
5211da177e4SLinus Torvalds }
5226aa895b0SPatrick McHardy 
5231da177e4SLinus Torvalds /**
524b960a0acSJiri Pirko  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
525b960a0acSJiri Pirko  * @skb: skbuff to tag
5261da177e4SLinus Torvalds  * @vlan_proto: VLAN encapsulation protocol
52786a9bad3SPatrick McHardy  * @vlan_tci: VLAN TCI to insert
5280c4b2d37SMichał Mirosław  *
5291da177e4SLinus Torvalds  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
5301da177e4SLinus Torvalds  */
__vlan_hwaccel_put_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)5311da177e4SLinus Torvalds static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
5321da177e4SLinus Torvalds 					  __be16 vlan_proto, u16 vlan_tci)
5331da177e4SLinus Torvalds {
534f4fb874cSVivien Didelot 	skb->vlan_proto = vlan_proto;
5351da177e4SLinus Torvalds 	skb->vlan_tci = vlan_tci;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5389bb8582eSPatrick McHardy /**
5391da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
5401f5020acSVladimir Oltean  * @skb: skbuff to query
5411da177e4SLinus Torvalds  * @vlan_tci: buffer to store value
542fe19c4f9SEric Garver  *
543537fec07SLarysa Zaremba  * Returns: error if the skb is not of VLAN type
5441da177e4SLinus Torvalds  */
__vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)5459bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	if (!eth_type_vlan(veth->h_vlan_proto))
5501da177e4SLinus Torvalds 		return -ENODATA;
5511da177e4SLinus Torvalds 
552f4fb874cSVivien Didelot 	*vlan_tci = ntohs(veth->h_vlan_TCI);
5531da177e4SLinus Torvalds 	return 0;
5546aa895b0SPatrick McHardy }
5551da177e4SLinus Torvalds 
55618149935SPatrick McHardy /**
5579bb8582eSPatrick McHardy  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
5581da177e4SLinus Torvalds  * @skb: skbuff to query
559df8a39deSJiri Pirko  * @vlan_tci: buffer to store value
560df8a39deSJiri Pirko  *
5611da177e4SLinus Torvalds  * Returns: error if @skb->vlan_tci is not set correctly
5621da177e4SLinus Torvalds  */
__vlan_hwaccel_get_tag(const struct sk_buff * skb,u16 * vlan_tci)5639bb8582eSPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
564537fec07SLarysa Zaremba 					 u16 *vlan_tci)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	if (skb_vlan_tag_present(skb)) {
5671da177e4SLinus Torvalds 		*vlan_tci = skb_vlan_tag_get(skb);
5681da177e4SLinus Torvalds 		return 0;
5691da177e4SLinus Torvalds 	} else {
5701da177e4SLinus Torvalds 		*vlan_tci = 0;
571f4fb874cSVivien Didelot 		return -ENODATA;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5759bb8582eSPatrick McHardy /**
5761da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
577f646968fSPatrick McHardy  * @skb: skbuff to query
5789bb8582eSPatrick McHardy  * @vlan_tci: buffer to store value
5791da177e4SLinus Torvalds  *
5809bb8582eSPatrick McHardy  * Returns: error if the skb is not VLAN tagged
5811da177e4SLinus Torvalds  */
vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)5821da177e4SLinus Torvalds static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5831da177e4SLinus Torvalds {
5840a85df00SHao Zheng 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
5850a85df00SHao Zheng 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
5860a85df00SHao Zheng 	} else {
587d4bcef3fSToshiaki Makita 		return __vlan_get_tag(skb, vlan_tci);
588*f91a5b80SEric Dumazet 	}
589d4bcef3fSToshiaki Makita }
5900a85df00SHao Zheng 
5910a85df00SHao Zheng /**
5920a85df00SHao Zheng  * __vlan_get_protocol_offset() - get protocol EtherType.
5930a85df00SHao Zheng  * @skb: skbuff to query
594*f91a5b80SEric Dumazet  * @type: first vlan protocol
595*f91a5b80SEric Dumazet  * @mac_offset: MAC offset
596*f91a5b80SEric Dumazet  * @depth: buffer to store length of eth and vlan tags in bytes
597d4bcef3fSToshiaki Makita  *
5980a85df00SHao Zheng  * Returns: the EtherType of the packet, regardless of whether it is
599469aceddSToke Høiland-Jørgensen  * vlan encapsulated (normal or hardware accelerated) or not.
6000a85df00SHao Zheng  */
__vlan_get_protocol_offset(const struct sk_buff * skb,__be16 type,int mac_offset,int * depth)601d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol_offset(const struct sk_buff *skb,
602d4bcef3fSToshiaki Makita 						__be16 type,
603d4bcef3fSToshiaki Makita 						int mac_offset,
604d4bcef3fSToshiaki Makita 						int *depth)
605fe19c4f9SEric Garver {
606d4bcef3fSToshiaki Makita 	unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
607d4bcef3fSToshiaki Makita 
608d4bcef3fSToshiaki Makita 	/* if type is 802.1Q/AD then the header should already be
609d4bcef3fSToshiaki Makita 	 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
610d4bcef3fSToshiaki Makita 	 * ETH_HLEN otherwise
611d4bcef3fSToshiaki Makita 	 */
612d4bcef3fSToshiaki Makita 	if (eth_type_vlan(type)) {
613d4bcef3fSToshiaki Makita 		if (vlan_depth) {
614469aceddSToke Høiland-Jørgensen 			if (WARN_ON(vlan_depth < VLAN_HLEN))
615d4bcef3fSToshiaki Makita 				return 0;
616*f91a5b80SEric Dumazet 			vlan_depth -= VLAN_HLEN;
617*f91a5b80SEric Dumazet 		} else {
618469aceddSToke Høiland-Jørgensen 			vlan_depth = ETH_HLEN;
619d4bcef3fSToshiaki Makita 		}
620d4bcef3fSToshiaki Makita 		do {
621d4bcef3fSToshiaki Makita 			struct vlan_hdr vhdr, *vh;
622d4bcef3fSToshiaki Makita 
623fe19c4f9SEric Garver 			vh = skb_header_pointer(skb, mac_offset + vlan_depth,
6240a85df00SHao Zheng 						sizeof(vhdr), &vhdr);
6250a85df00SHao Zheng 			if (unlikely(!vh || !--parse_depth))
626d4bcef3fSToshiaki Makita 				return 0;
627d4bcef3fSToshiaki Makita 
628d4bcef3fSToshiaki Makita 			type = vh->h_vlan_encapsulated_proto;
629d4bcef3fSToshiaki Makita 			vlan_depth += VLAN_HLEN;
630d4bcef3fSToshiaki Makita 		} while (eth_type_vlan(type));
631d4bcef3fSToshiaki Makita 	}
632*f91a5b80SEric Dumazet 
633*f91a5b80SEric Dumazet 	if (depth)
634*f91a5b80SEric Dumazet 		*depth = vlan_depth;
635*f91a5b80SEric Dumazet 
636*f91a5b80SEric Dumazet 	return type;
637*f91a5b80SEric Dumazet }
638d4bcef3fSToshiaki Makita 
__vlan_get_protocol(const struct sk_buff * skb,__be16 type,int * depth)639d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
640d4bcef3fSToshiaki Makita 					 int *depth)
641d4bcef3fSToshiaki Makita {
642d4bcef3fSToshiaki Makita 	return __vlan_get_protocol_offset(skb, type, 0, depth);
643d4bcef3fSToshiaki Makita }
644d4bcef3fSToshiaki Makita 
645469aceddSToke Høiland-Jørgensen /**
646d4bcef3fSToshiaki Makita  * vlan_get_protocol - get protocol EtherType.
647d4bcef3fSToshiaki Makita  * @skb: skbuff to query
6480a85df00SHao Zheng  *
649396cf943SPravin B Shelar  * Returns: the EtherType of the packet, regardless of whether it is
6504063384eSEric Dumazet  * vlan encapsulated (normal or hardware accelerated) or not.
6514063384eSEric Dumazet  */
vlan_get_protocol(const struct sk_buff * skb)6524063384eSEric Dumazet static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
6534063384eSEric Dumazet {
6544063384eSEric Dumazet 	return __vlan_get_protocol(skb, skb->protocol, NULL);
6554063384eSEric Dumazet }
6564063384eSEric Dumazet 
6574063384eSEric Dumazet /* This version of __vlan_get_protocol() also pulls mac header in skb->head */
vlan_get_protocol_and_depth(struct sk_buff * skb,__be16 type,int * depth)6584063384eSEric Dumazet static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
6594063384eSEric Dumazet 						 __be16 type, int *depth)
6604063384eSEric Dumazet {
6614063384eSEric Dumazet 	int maclen;
6624063384eSEric Dumazet 
6634063384eSEric Dumazet 	type = __vlan_get_protocol(skb, type, &maclen);
6644063384eSEric Dumazet 
6654063384eSEric Dumazet 	if (type) {
6664063384eSEric Dumazet 		if (!pskb_may_pull(skb, maclen))
667469aceddSToke Høiland-Jørgensen 			type = 0;
668469aceddSToke Høiland-Jørgensen 		else if (depth)
669469aceddSToke Høiland-Jørgensen 			*depth = maclen;
670469aceddSToke Høiland-Jørgensen 	}
671469aceddSToke Høiland-Jørgensen 	return type;
672469aceddSToke Høiland-Jørgensen }
673469aceddSToke Høiland-Jørgensen 
674469aceddSToke Høiland-Jørgensen /* A getter for the SKB protocol field which will handle VLAN tags consistently
675469aceddSToke Høiland-Jørgensen  * whether VLAN acceleration is enabled or not.
676469aceddSToke Høiland-Jørgensen  */
skb_protocol(const struct sk_buff * skb,bool skip_vlan)677469aceddSToke Høiland-Jørgensen static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
678469aceddSToke Høiland-Jørgensen {
679469aceddSToke Høiland-Jørgensen 	if (!skip_vlan)
680469aceddSToke Høiland-Jørgensen 		/* VLAN acceleration strips the VLAN header from the skb and
681396cf943SPravin B Shelar 		 * moves it to skb->vlan_proto
682396cf943SPravin B Shelar 		 */
683396cf943SPravin B Shelar 		return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
684396cf943SPravin B Shelar 
685da8c8724SCong Wang 	return vlan_get_protocol(skb);
686396cf943SPravin B Shelar }
687396cf943SPravin B Shelar 
vlan_set_encap_proto(struct sk_buff * skb,struct vlan_hdr * vhdr)688396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
689396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
690396cf943SPravin B Shelar {
691396cf943SPravin B Shelar 	__be16 proto;
692396cf943SPravin B Shelar 	unsigned short *rawp;
6939545b22dSAlexander Duyck 
694396cf943SPravin B Shelar 	/*
695396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
696396cf943SPravin B Shelar 	 * three protocols care about.
697396cf943SPravin B Shelar 	 */
698da8c8724SCong Wang 
699da8c8724SCong Wang 	proto = vhdr->h_vlan_encapsulated_proto;
700396cf943SPravin B Shelar 	if (eth_proto_is_802_3(proto)) {
701396cf943SPravin B Shelar 		skb->protocol = proto;
702396cf943SPravin B Shelar 		return;
703396cf943SPravin B Shelar 	}
704396cf943SPravin B Shelar 
705396cf943SPravin B Shelar 	rawp = (unsigned short *)(vhdr + 1);
706396cf943SPravin B Shelar 	if (*rawp == 0xFFFF)
707396cf943SPravin B Shelar 		/*
708396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
709396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
710396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
711396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
712396cf943SPravin B Shelar 		 * but does for the rest.
713396cf943SPravin B Shelar 		 */
71444a40855SVlad Yasevich 		skb->protocol = htons(ETH_P_802_3);
715f5a7fb88SToshiaki Makita 	else
7160bcf2e4aSVladimir Oltean 		/*
7170bcf2e4aSVladimir Oltean 		 * Real 802.2 LLC
7180bcf2e4aSVladimir Oltean 		 */
7190bcf2e4aSVladimir Oltean 		skb->protocol = htons(ETH_P_802_2);
7200bcf2e4aSVladimir Oltean }
7210bcf2e4aSVladimir Oltean 
7220bcf2e4aSVladimir Oltean /**
7230bcf2e4aSVladimir Oltean  * vlan_remove_tag - remove outer VLAN tag from payload
7240bcf2e4aSVladimir Oltean  * @skb: skbuff to remove tag from
7250bcf2e4aSVladimir Oltean  * @vlan_tci: buffer to store value
7260bcf2e4aSVladimir Oltean  *
7270bcf2e4aSVladimir Oltean  * Expects the skb to contain a VLAN tag in the payload, and to have skb->data
7280bcf2e4aSVladimir Oltean  * pointing at the MAC header.
7290bcf2e4aSVladimir Oltean  *
7300bcf2e4aSVladimir Oltean  * Returns: a new pointer to skb->data, or NULL on failure to pull.
7310bcf2e4aSVladimir Oltean  */
vlan_remove_tag(struct sk_buff * skb,u16 * vlan_tci)7320bcf2e4aSVladimir Oltean static inline void *vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci)
7330bcf2e4aSVladimir Oltean {
7340bcf2e4aSVladimir Oltean 	struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
7350bcf2e4aSVladimir Oltean 
7360bcf2e4aSVladimir Oltean 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
737f5a7fb88SToshiaki Makita 
738f5a7fb88SToshiaki Makita 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
739f5a7fb88SToshiaki Makita 	vlan_set_encap_proto(skb, vhdr);
740f5a7fb88SToshiaki Makita 	return __skb_pull(skb, VLAN_HLEN);
741f5a7fb88SToshiaki Makita }
742f5a7fb88SToshiaki Makita 
743f5a7fb88SToshiaki Makita /**
744f5a7fb88SToshiaki Makita  * skb_vlan_tagged - check if skb is vlan tagged.
745f5a7fb88SToshiaki Makita  * @skb: skbuff to query
746fe19c4f9SEric Garver  *
747f5a7fb88SToshiaki Makita  * Returns: true if the skb is tagged, regardless of whether it is hardware
748f5a7fb88SToshiaki Makita  * accelerated or not.
749f5a7fb88SToshiaki Makita  */
skb_vlan_tagged(const struct sk_buff * skb)750f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb)
751f5a7fb88SToshiaki Makita {
752f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb) &&
753f5a7fb88SToshiaki Makita 	    likely(!eth_type_vlan(skb->protocol)))
754f5a7fb88SToshiaki Makita 		return false;
755f5a7fb88SToshiaki Makita 
756f5a7fb88SToshiaki Makita 	return true;
757f5a7fb88SToshiaki Makita }
758f5a7fb88SToshiaki Makita 
7597ce23672SToshiaki Makita /**
760f5a7fb88SToshiaki Makita  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
761f5a7fb88SToshiaki Makita  * @skb: skbuff to query
762f5a7fb88SToshiaki Makita  *
763f5a7fb88SToshiaki Makita  * Returns: true if the skb is tagged with multiple vlan headers, regardless
764f5a7fb88SToshiaki Makita  * of whether it is hardware accelerated or not.
765f5a7fb88SToshiaki Makita  */
skb_vlan_tagged_multi(struct sk_buff * skb)766fe19c4f9SEric Garver static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
767f5a7fb88SToshiaki Makita {
768f5a7fb88SToshiaki Makita 	__be16 protocol = skb->protocol;
7697ce23672SToshiaki Makita 
7707ce23672SToshiaki Makita 	if (!skb_vlan_tag_present(skb)) {
7717ce23672SToshiaki Makita 		struct vlan_ethhdr *veh;
7721f5020acSVladimir Oltean 
773f5a7fb88SToshiaki Makita 		if (likely(!eth_type_vlan(protocol)))
774f5a7fb88SToshiaki Makita 			return false;
775f5a7fb88SToshiaki Makita 
776fe19c4f9SEric Garver 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
777f5a7fb88SToshiaki Makita 			return false;
778f5a7fb88SToshiaki Makita 
779f5a7fb88SToshiaki Makita 		veh = skb_vlan_eth_hdr(skb);
780f5a7fb88SToshiaki Makita 		protocol = veh->h_vlan_encapsulated_proto;
781f5a7fb88SToshiaki Makita 	}
7828cb65d00SToshiaki Makita 
7838cb65d00SToshiaki Makita 	if (!eth_type_vlan(protocol))
7848cb65d00SToshiaki Makita 		return false;
7858cb65d00SToshiaki Makita 
7868cb65d00SToshiaki Makita 	return true;
7878cb65d00SToshiaki Makita }
7888cb65d00SToshiaki Makita 
7897ce23672SToshiaki Makita /**
7908cb65d00SToshiaki Makita  * vlan_features_check - drop unsafe features for skb with multiple tags.
7918cb65d00SToshiaki Makita  * @skb: skbuff to query
79235d2f80bSVlad Yasevich  * @features: features to be checked
79335d2f80bSVlad Yasevich  *
79435d2f80bSVlad Yasevich  * Returns: features without unsafe ones if the skb has multiple tags.
79535d2f80bSVlad Yasevich  */
vlan_features_check(struct sk_buff * skb,netdev_features_t features)79635d2f80bSVlad Yasevich static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
79735d2f80bSVlad Yasevich 						    netdev_features_t features)
79835d2f80bSVlad Yasevich {
79935d2f80bSVlad Yasevich 	if (skb_vlan_tagged_multi(skb)) {
80035d2f80bSVlad Yasevich 		/* In the case of multi-tagged packets, use a direct mask
80135d2f80bSVlad Yasevich 		 * instead of using netdev_interesect_features(), to make
8028cb65d00SToshiaki Makita 		 * sure that only devices supporting NETIF_F_HW_CSUM will
8038cb65d00SToshiaki Makita 		 * have checksum offloading support.
8048cb65d00SToshiaki Makita 		 */
8058cb65d00SToshiaki Makita 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
80666e5133fSToshiaki Makita 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
80766e5133fSToshiaki Makita 			    NETIF_F_HW_VLAN_STAG_TX;
80866e5133fSToshiaki Makita 	}
80966e5133fSToshiaki Makita 
81066e5133fSToshiaki Makita 	return features;
81166e5133fSToshiaki Makita }
81266e5133fSToshiaki Makita 
81366e5133fSToshiaki Makita /**
81466e5133fSToshiaki Makita  * compare_vlan_header - Compare two vlan headers
81566e5133fSToshiaki Makita  * @h1: Pointer to vlan header
81666e5133fSToshiaki Makita  * @h2: Pointer to vlan header
81766e5133fSToshiaki Makita  *
81866e5133fSToshiaki Makita  * Compare two vlan headers.
81966e5133fSToshiaki Makita  *
82066e5133fSToshiaki Makita  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
82166e5133fSToshiaki Makita  *
82266e5133fSToshiaki Makita  * Return: 0 if equal, arbitrary non-zero value if not equal.
82366e5133fSToshiaki Makita  */
compare_vlan_header(const struct vlan_hdr * h1,const struct vlan_hdr * h2)82466e5133fSToshiaki Makita static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
82566e5133fSToshiaki Makita 						const struct vlan_hdr *h2)
8261da177e4SLinus Torvalds {
827 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
828 	return *(u32 *)h1 ^ *(u32 *)h2;
829 #else
830 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
831 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
832 		(__force u32)h2->h_vlan_encapsulated_proto);
833 #endif
834 }
835 #endif /* !(_LINUX_IF_VLAN_H_) */
836