xref: /linux/include/linux/if_vlan.h (revision 960abf68d2023f0d0b08c6f5d05971630496cfb0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * VLAN		An implementation of 802.1Q VLAN tagging.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Authors:	Ben Greear <greearb@candelatech.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds #ifndef _LINUX_IF_VLAN_H_
131da177e4SLinus Torvalds #define _LINUX_IF_VLAN_H_
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/netdevice.h>
160610d11bSStephen Hemminger #include <linux/etherdevice.h>
1765ac6a5fSJesse Gross #include <linux/rtnetlink.h>
18187f1882SPaul Gortmaker #include <linux/bug.h>
19607ca46eSDavid Howells #include <uapi/linux/if_vlan.h>
201da177e4SLinus Torvalds 
21a3f671b3SJoe Perches #define VLAN_HLEN	4		/* The additional bytes required by VLAN
22a3f671b3SJoe Perches 					 * (in addition to the Ethernet header)
231da177e4SLinus Torvalds 					 */
241da177e4SLinus Torvalds #define VLAN_ETH_HLEN	18		/* Total octets in header.	 */
251da177e4SLinus Torvalds #define VLAN_ETH_ZLEN	64		/* Min. octets in frame sans FCS */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds #define VLAN_ETH_DATA_LEN	1500	/* Max. octets in payload	 */
311da177e4SLinus Torvalds #define VLAN_ETH_FRAME_LEN	1518	/* Max. octets in frame sans FCS */
321da177e4SLinus Torvalds 
33740c15d0SPatrick McHardy /*
34740c15d0SPatrick McHardy  * 	struct vlan_hdr - vlan header
35740c15d0SPatrick McHardy  * 	@h_vlan_TCI: priority and VLAN ID
36740c15d0SPatrick McHardy  *	@h_vlan_encapsulated_proto: packet type ID or len
37740c15d0SPatrick McHardy  */
38740c15d0SPatrick McHardy struct vlan_hdr {
39740c15d0SPatrick McHardy 	__be16	h_vlan_TCI;
40740c15d0SPatrick McHardy 	__be16	h_vlan_encapsulated_proto;
41740c15d0SPatrick McHardy };
42740c15d0SPatrick McHardy 
43740c15d0SPatrick McHardy /**
44740c15d0SPatrick McHardy  *	struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
45740c15d0SPatrick McHardy  *	@h_dest: destination ethernet address
46740c15d0SPatrick McHardy  *	@h_source: source ethernet address
47a7bf5804SOlaf Hering  *	@h_vlan_proto: ethernet protocol
48740c15d0SPatrick McHardy  *	@h_vlan_TCI: priority and VLAN ID
49740c15d0SPatrick McHardy  *	@h_vlan_encapsulated_proto: packet type ID or len
50740c15d0SPatrick McHardy  */
511da177e4SLinus Torvalds struct vlan_ethhdr {
52740c15d0SPatrick McHardy 	unsigned char	h_dest[ETH_ALEN];
53740c15d0SPatrick McHardy 	unsigned char	h_source[ETH_ALEN];
54740c15d0SPatrick McHardy 	__be16		h_vlan_proto;
55740c15d0SPatrick McHardy 	__be16		h_vlan_TCI;
56740c15d0SPatrick McHardy 	__be16		h_vlan_encapsulated_proto;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <linux/skbuff.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
621da177e4SLinus Torvalds {
6398e399f8SArnaldo Carvalho de Melo 	return (struct vlan_ethhdr *)skb_mac_header(skb);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
6605423b24SEric Dumazet #define VLAN_PRIO_MASK		0xe000 /* Priority Code Point */
6705423b24SEric Dumazet #define VLAN_PRIO_SHIFT		13
6805423b24SEric Dumazet #define VLAN_CFI_MASK		0x1000 /* Canonical Format Indicator */
6905423b24SEric Dumazet #define VLAN_TAG_PRESENT	VLAN_CFI_MASK
7005423b24SEric Dumazet #define VLAN_VID_MASK		0x0fff /* VLAN Identifier */
71b738127dSJesse Gross #define VLAN_N_VID		4096
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* found in socket.c */
74881d966bSEric W. Biederman extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
751da177e4SLinus Torvalds 
76b618aaa9SJiri Pirko static inline bool is_vlan_dev(const struct net_device *dev)
776dcbbe25SNeil Horman {
786dcbbe25SNeil Horman         return dev->priv_flags & IFF_802_1Q_VLAN;
796dcbbe25SNeil Horman }
806dcbbe25SNeil Horman 
81df8a39deSJiri Pirko #define skb_vlan_tag_present(__skb)	((__skb)->vlan_tci & VLAN_TAG_PRESENT)
82df8a39deSJiri Pirko #define skb_vlan_tag_get(__skb)		((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
83df8a39deSJiri Pirko #define skb_vlan_tag_get_id(__skb)	((__skb)->vlan_tci & VLAN_VID_MASK)
849b319148SMichał Mirosław #define skb_vlan_tag_get_prio(__skb)	(((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
851da177e4SLinus Torvalds 
869daae9bdSGal Pressman static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
879daae9bdSGal Pressman {
889daae9bdSGal Pressman 	ASSERT_RTNL();
899daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
909daae9bdSGal Pressman }
919daae9bdSGal Pressman 
929daae9bdSGal Pressman static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
939daae9bdSGal Pressman {
949daae9bdSGal Pressman 	ASSERT_RTNL();
959daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
969daae9bdSGal Pressman }
979daae9bdSGal Pressman 
989daae9bdSGal Pressman static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
999daae9bdSGal Pressman {
1009daae9bdSGal Pressman 	ASSERT_RTNL();
1019daae9bdSGal Pressman 	return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
1029daae9bdSGal Pressman }
1039daae9bdSGal Pressman 
1049daae9bdSGal Pressman static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
1059daae9bdSGal Pressman {
1069daae9bdSGal Pressman 	ASSERT_RTNL();
1079daae9bdSGal Pressman 	call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
1089daae9bdSGal Pressman }
1099daae9bdSGal Pressman 
110e267cb96SDavid S. Miller /**
111e267cb96SDavid S. Miller  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
112e267cb96SDavid S. Miller  *	@rx_packets: number of received packets
113e267cb96SDavid S. Miller  *	@rx_bytes: number of received bytes
114e267cb96SDavid S. Miller  *	@rx_multicast: number of received multicast packets
115e267cb96SDavid S. Miller  *	@tx_packets: number of transmitted packets
116e267cb96SDavid S. Miller  *	@tx_bytes: number of transmitted bytes
117e267cb96SDavid S. Miller  *	@syncp: synchronization point for 64bit counters
118e267cb96SDavid S. Miller  *	@rx_errors: number of rx errors
119e267cb96SDavid S. Miller  *	@tx_dropped: number of tx drops
120e267cb96SDavid S. Miller  */
121e267cb96SDavid S. Miller struct vlan_pcpu_stats {
122e267cb96SDavid S. Miller 	u64			rx_packets;
123e267cb96SDavid S. Miller 	u64			rx_bytes;
124e267cb96SDavid S. Miller 	u64			rx_multicast;
125e267cb96SDavid S. Miller 	u64			tx_packets;
126e267cb96SDavid S. Miller 	u64			tx_bytes;
127e267cb96SDavid S. Miller 	struct u64_stats_sync	syncp;
128e267cb96SDavid S. Miller 	u32			rx_errors;
129e267cb96SDavid S. Miller 	u32			tx_dropped;
130e267cb96SDavid S. Miller };
131e267cb96SDavid S. Miller 
1321e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1331e85c9b6SHannes Frederic Sowa 
134f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
1351e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
136*960abf68SIvan Khoronzhuk extern int vlan_for_each(struct net_device *dev,
137*960abf68SIvan Khoronzhuk 			 int (*action)(struct net_device *dev, int vid,
138*960abf68SIvan Khoronzhuk 				       void *arg), void *arg);
1391e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
1401e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
14171e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
1421e85c9b6SHannes Frederic Sowa 
1431e85c9b6SHannes Frederic Sowa /**
1441e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
1451e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
1461e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
1471e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
1481e85c9b6SHannes Frederic Sowa  */
1491e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
1501e85c9b6SHannes Frederic Sowa 	u32					priority;
1511e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
1521e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
1531e85c9b6SHannes Frederic Sowa };
1541e85c9b6SHannes Frederic Sowa 
155e267cb96SDavid S. Miller struct proc_dir_entry;
156e267cb96SDavid S. Miller struct netpoll;
157e267cb96SDavid S. Miller 
158e267cb96SDavid S. Miller /**
159e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
160e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
161e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
162e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
163e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
164e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
165e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
166e267cb96SDavid S. Miller  *	@flags: device flags
167e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
168e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
169e267cb96SDavid S. Miller  *	@dent: proc dir entry
170e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
171e267cb96SDavid S. Miller  */
172e267cb96SDavid S. Miller struct vlan_dev_priv {
173e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
174e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
175e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
176e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
177e267cb96SDavid S. Miller 
178e267cb96SDavid S. Miller 	__be16					vlan_proto;
179e267cb96SDavid S. Miller 	u16					vlan_id;
180e267cb96SDavid S. Miller 	u16					flags;
181e267cb96SDavid S. Miller 
182e267cb96SDavid S. Miller 	struct net_device			*real_dev;
183e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
184e267cb96SDavid S. Miller 
185e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
186e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
187e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
188e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
189e267cb96SDavid S. Miller #endif
190d38569abSVlad Yasevich 	unsigned int				nest_level;
191e267cb96SDavid S. Miller };
192e267cb96SDavid S. Miller 
193e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
194e267cb96SDavid S. Miller {
195e267cb96SDavid S. Miller 	return netdev_priv(dev);
196e267cb96SDavid S. Miller }
197e267cb96SDavid S. Miller 
198e267cb96SDavid S. Miller static inline u16
199e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
200e267cb96SDavid S. Miller {
201e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
202e267cb96SDavid S. Miller 
203e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
204e267cb96SDavid S. Miller 
205e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
206e267cb96SDavid S. Miller 	while (mp) {
207e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
208e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
209e267cb96SDavid S. Miller 					      * to mask correctly with the
210e267cb96SDavid S. Miller 					      * VLAN's TCI */
211e267cb96SDavid S. Miller 		}
212e267cb96SDavid S. Miller 		mp = mp->next;
213e267cb96SDavid S. Miller 	}
214e267cb96SDavid S. Miller 	return 0;
215e267cb96SDavid S. Miller }
216e267cb96SDavid S. Miller 
21748cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb);
2189b22ea56SPatrick McHardy 
21980d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
22080d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
22187002b03SJiri Pirko 
222348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev,
223348a1443SJiri Pirko 				const struct net_device *by_dev);
224348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev,
225348a1443SJiri Pirko 				 const struct net_device *by_dev);
2269b361c13SJiri Pirko 
2279b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev);
228e1618d46SVlad Yasevich 
229e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
230e1618d46SVlad Yasevich {
231e1618d46SVlad Yasevich 	BUG_ON(!is_vlan_dev(dev));
232e1618d46SVlad Yasevich 	return vlan_dev_priv(dev)->nest_level;
233e1618d46SVlad Yasevich }
2347750f403SPatrick McHardy #else
235cec9c133SJiri Pirko static inline struct net_device *
236f06c7f9fSdingtianhong __vlan_find_dev_deep_rcu(struct net_device *real_dev,
2379fae27b3SPatrick McHardy 		     __be16 vlan_proto, u16 vlan_id)
238cec9c133SJiri Pirko {
239cec9c133SJiri Pirko 	return NULL;
240cec9c133SJiri Pirko }
241cec9c133SJiri Pirko 
242*960abf68SIvan Khoronzhuk static inline int
243*960abf68SIvan Khoronzhuk vlan_for_each(struct net_device *dev,
244*960abf68SIvan Khoronzhuk 	      int (*action)(struct net_device *dev, int vid, void *arg),
245*960abf68SIvan Khoronzhuk 	      void *arg)
246*960abf68SIvan Khoronzhuk {
247*960abf68SIvan Khoronzhuk 	return 0;
248*960abf68SIvan Khoronzhuk }
249*960abf68SIvan Khoronzhuk 
25022d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
25122d1ba74SPatrick McHardy {
25222d1ba74SPatrick McHardy 	BUG();
25322d1ba74SPatrick McHardy 	return NULL;
25422d1ba74SPatrick McHardy }
25522d1ba74SPatrick McHardy 
25622d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
25722d1ba74SPatrick McHardy {
25822d1ba74SPatrick McHardy 	BUG();
25922d1ba74SPatrick McHardy 	return 0;
26022d1ba74SPatrick McHardy }
26122d1ba74SPatrick McHardy 
26271e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
26371e415e4Sdingtianhong {
26471e415e4Sdingtianhong 	BUG();
26571e415e4Sdingtianhong 	return 0;
26671e415e4Sdingtianhong }
26771e415e4Sdingtianhong 
268d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
269d3243539SEyal Perry 					       u32 skprio)
270d3243539SEyal Perry {
271d3243539SEyal Perry 	return 0;
272d3243539SEyal Perry }
273d3243539SEyal Perry 
27448cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb)
2759b22ea56SPatrick McHardy {
2763701e513SJesse Gross 	return false;
2779b22ea56SPatrick McHardy }
278e1c096e2SHerbert Xu 
2799fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
28087002b03SJiri Pirko {
28187002b03SJiri Pirko 	return 0;
28287002b03SJiri Pirko }
28387002b03SJiri Pirko 
2849fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
28587002b03SJiri Pirko {
28687002b03SJiri Pirko }
287348a1443SJiri Pirko 
288348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
289348a1443SJiri Pirko 				       const struct net_device *by_dev)
290348a1443SJiri Pirko {
291348a1443SJiri Pirko 	return 0;
292348a1443SJiri Pirko }
293348a1443SJiri Pirko 
294348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
295348a1443SJiri Pirko 					const struct net_device *by_dev)
296348a1443SJiri Pirko {
297348a1443SJiri Pirko }
2989b361c13SJiri Pirko 
2999b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
3009b361c13SJiri Pirko {
3019b361c13SJiri Pirko 	return false;
3029b361c13SJiri Pirko }
303e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
304e1618d46SVlad Yasevich {
305e1618d46SVlad Yasevich 	BUG();
306e1618d46SVlad Yasevich 	return 0;
307e1618d46SVlad Yasevich }
3087750f403SPatrick McHardy #endif
3091da177e4SLinus Torvalds 
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  */
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 }
326fe19c4f9SEric Garver 
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)
33186a9bad3SPatrick McHardy 		return true;
3328ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
3338ad227ffSPatrick McHardy 		return true;
33486a9bad3SPatrick McHardy 	return false;
33586a9bad3SPatrick McHardy }
33686a9bad3SPatrick McHardy 
3379bb8582eSPatrick McHardy /**
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  *
344cbe7128cSToshiaki Makita  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
3458051ac76SThadeu Lima de Souza Cascardo  * Returns error if skb_cow_head fails.
346cbe7128cSToshiaki Makita  *
347cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
348cbe7128cSToshiaki Makita  */
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 
360cbe7128cSToshiaki Makita 	/* Move the mac header sans proto to the beginning of the new header. */
361c769accdSToshiaki Makita 	if (likely(mac_len > ETH_TLEN))
362cbe7128cSToshiaki Makita 		memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
363cbe7128cSToshiaki Makita 	skb->mac_header -= VLAN_HLEN;
364cbe7128cSToshiaki Makita 
365cbe7128cSToshiaki Makita 	veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
366cbe7128cSToshiaki Makita 
367cbe7128cSToshiaki Makita 	/* first, the ethernet type */
368c769accdSToshiaki Makita 	if (likely(mac_len >= ETH_TLEN)) {
369c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should already be populated, and
370c769accdSToshiaki Makita 		 * skb->data has space for h_vlan_proto
371c769accdSToshiaki Makita 		 */
372cbe7128cSToshiaki Makita 		veth->h_vlan_proto = vlan_proto;
373c769accdSToshiaki Makita 	} else {
374c769accdSToshiaki Makita 		/* h_vlan_encapsulated_proto should not be populated, and
375c769accdSToshiaki Makita 		 * skb->data has no space for h_vlan_proto
376c769accdSToshiaki Makita 		 */
377c769accdSToshiaki Makita 		veth->h_vlan_encapsulated_proto = skb->protocol;
378c769accdSToshiaki Makita 	}
379cbe7128cSToshiaki Makita 
380cbe7128cSToshiaki Makita 	/* now, the TCI */
381cbe7128cSToshiaki Makita 	veth->h_vlan_TCI = htons(vlan_tci);
382cbe7128cSToshiaki Makita 
383cbe7128cSToshiaki Makita 	return 0;
384cbe7128cSToshiaki Makita }
385cbe7128cSToshiaki Makita 
386cbe7128cSToshiaki Makita /**
38715255a43SJiri Pirko  * __vlan_insert_tag - regular VLAN tag inserting
38815255a43SJiri Pirko  * @skb: skbuff to tag
38915255a43SJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
39015255a43SJiri Pirko  * @vlan_tci: VLAN TCI to insert
39115255a43SJiri Pirko  *
39215255a43SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3938051ac76SThadeu Lima de Souza Cascardo  * Returns error if skb_cow_head fails.
39415255a43SJiri Pirko  *
39515255a43SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
39615255a43SJiri Pirko  */
39715255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb,
39815255a43SJiri Pirko 				    __be16 vlan_proto, u16 vlan_tci)
39915255a43SJiri Pirko {
400cbe7128cSToshiaki Makita 	return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
401cbe7128cSToshiaki Makita }
40215255a43SJiri Pirko 
403cbe7128cSToshiaki Makita /**
404cbe7128cSToshiaki Makita  * vlan_insert_inner_tag - inner VLAN tag inserting
405cbe7128cSToshiaki Makita  * @skb: skbuff to tag
406cbe7128cSToshiaki Makita  * @vlan_proto: VLAN encapsulation protocol
407cbe7128cSToshiaki Makita  * @vlan_tci: VLAN TCI to insert
408cbe7128cSToshiaki Makita  * @mac_len: MAC header length including outer vlan headers
409cbe7128cSToshiaki Makita  *
410cbe7128cSToshiaki Makita  * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
411cbe7128cSToshiaki Makita  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
412cbe7128cSToshiaki Makita  *
413cbe7128cSToshiaki Makita  * Following the skb_unshare() example, in case of error, the calling function
414cbe7128cSToshiaki Makita  * doesn't have to worry about freeing the original skb.
415cbe7128cSToshiaki Makita  *
416cbe7128cSToshiaki Makita  * Does not change skb->protocol so this function can be used during receive.
417cbe7128cSToshiaki Makita  */
418cbe7128cSToshiaki Makita static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
419cbe7128cSToshiaki Makita 						    __be16 vlan_proto,
420cbe7128cSToshiaki Makita 						    u16 vlan_tci,
421cbe7128cSToshiaki Makita 						    unsigned int mac_len)
422cbe7128cSToshiaki Makita {
423cbe7128cSToshiaki Makita 	int err;
42415255a43SJiri Pirko 
425cbe7128cSToshiaki Makita 	err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
426cbe7128cSToshiaki Makita 	if (err) {
427cbe7128cSToshiaki Makita 		dev_kfree_skb_any(skb);
428cbe7128cSToshiaki Makita 		return NULL;
429cbe7128cSToshiaki Makita 	}
430cbe7128cSToshiaki Makita 	return skb;
43115255a43SJiri Pirko }
43215255a43SJiri Pirko 
43315255a43SJiri Pirko /**
4340b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
4351da177e4SLinus Torvalds  * @skb: skbuff to tag
43686a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
4379bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
4401da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
4411da177e4SLinus Torvalds  *
4421da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
4431da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
4440b5c9db1SJiri Pirko  *
4450b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
4461da177e4SLinus Torvalds  */
44786a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
44886a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
4491da177e4SLinus Torvalds {
450cbe7128cSToshiaki Makita 	return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
4510b5c9db1SJiri Pirko }
4521da177e4SLinus Torvalds 
4530b5c9db1SJiri Pirko /**
45462749e2cSJiri Pirko  * vlan_insert_tag_set_proto - regular VLAN tag inserting
4550b5c9db1SJiri Pirko  * @skb: skbuff to tag
45662749e2cSJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
4570b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
4580b5c9db1SJiri Pirko  *
4590b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
4600b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
4610b5c9db1SJiri Pirko  *
4620b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
4630b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
4640b5c9db1SJiri Pirko  */
46562749e2cSJiri Pirko static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
46662749e2cSJiri Pirko 							__be16 vlan_proto,
46762749e2cSJiri Pirko 							u16 vlan_tci)
4680b5c9db1SJiri Pirko {
46986a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
4700b5c9db1SJiri Pirko 	if (skb)
47186a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
4721da177e4SLinus Torvalds 	return skb;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
475c8accd5aSMichał Mirosław /**
476c8accd5aSMichał Mirosław  * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
477c8accd5aSMichał Mirosław  * @skb: skbuff to clear
478c8accd5aSMichał Mirosław  *
479c8accd5aSMichał Mirosław  * Clears the VLAN information from @skb
480c8accd5aSMichał Mirosław  */
481c8accd5aSMichał Mirosław static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
482c8accd5aSMichał Mirosław {
483c8accd5aSMichał Mirosław 	skb->vlan_tci = 0;
484c8accd5aSMichał Mirosław }
485c8accd5aSMichał Mirosław 
486e0a6b809SMichał Mirosław /**
487e0a6b809SMichał Mirosław  * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
488e0a6b809SMichał Mirosław  * @dst: skbuff to copy to
489e0a6b809SMichał Mirosław  * @src: skbuff to copy from
490e0a6b809SMichał Mirosław  *
491e0a6b809SMichał Mirosław  * Copies VLAN information from @src to @dst (for branchless code)
492e0a6b809SMichał Mirosław  */
493e0a6b809SMichał Mirosław static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
494e0a6b809SMichał Mirosław {
495e0a6b809SMichał Mirosław 	dst->vlan_proto = src->vlan_proto;
496e0a6b809SMichał Mirosław 	dst->vlan_tci = src->vlan_tci;
497e0a6b809SMichał Mirosław }
498e0a6b809SMichał Mirosław 
4995968250cSJiri Pirko /*
5005968250cSJiri Pirko  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
5015968250cSJiri Pirko  * @skb: skbuff to tag
5025968250cSJiri Pirko  *
5035968250cSJiri Pirko  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
5045968250cSJiri Pirko  *
5055968250cSJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
5065968250cSJiri Pirko  * doesn't have to worry about freeing the original skb.
5075968250cSJiri Pirko  */
5085968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
5095968250cSJiri Pirko {
5105968250cSJiri Pirko 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
511df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
5125968250cSJiri Pirko 	if (likely(skb))
513c8accd5aSMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
5145968250cSJiri Pirko 	return skb;
5155968250cSJiri Pirko }
5165968250cSJiri Pirko 
5171da177e4SLinus Torvalds /**
5181da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
5191da177e4SLinus Torvalds  * @skb: skbuff to tag
52086a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
5219bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
5221da177e4SLinus Torvalds  *
5236aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
5241da177e4SLinus Torvalds  */
525b960a0acSJiri Pirko static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
526b960a0acSJiri Pirko 					  __be16 vlan_proto, u16 vlan_tci)
5271da177e4SLinus Torvalds {
52886a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
52905423b24SEric Dumazet 	skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds /**
5331da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
5341da177e4SLinus Torvalds  * @skb: skbuff to query
535f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5361da177e4SLinus Torvalds  *
5371da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
5381da177e4SLinus Torvalds  */
5399bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
5421da177e4SLinus Torvalds 
543fe19c4f9SEric Garver 	if (!eth_type_vlan(veth->h_vlan_proto))
5441da177e4SLinus Torvalds 		return -EINVAL;
5451da177e4SLinus Torvalds 
5469bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
5471da177e4SLinus Torvalds 	return 0;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
5521da177e4SLinus Torvalds  * @skb: skbuff to query
553f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5541da177e4SLinus Torvalds  *
5556aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
5561da177e4SLinus Torvalds  */
55718149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
5589bb8582eSPatrick McHardy 					 u16 *vlan_tci)
5591da177e4SLinus Torvalds {
560df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
561df8a39deSJiri Pirko 		*vlan_tci = skb_vlan_tag_get(skb);
5621da177e4SLinus Torvalds 		return 0;
5631da177e4SLinus Torvalds 	} else {
5649bb8582eSPatrick McHardy 		*vlan_tci = 0;
5651da177e4SLinus Torvalds 		return -EINVAL;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /**
5701da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
5711da177e4SLinus Torvalds  * @skb: skbuff to query
572f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
5731da177e4SLinus Torvalds  *
5741da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
5751da177e4SLinus Torvalds  */
5769bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
5771da177e4SLinus Torvalds {
578f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
5799bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
5801da177e4SLinus Torvalds 	} else {
5819bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
5850a85df00SHao Zheng /**
5860a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
5870a85df00SHao Zheng  * @skb: skbuff to query
588d4bcef3fSToshiaki Makita  * @type: first vlan protocol
589d4bcef3fSToshiaki Makita  * @depth: buffer to store length of eth and vlan tags in bytes
5900a85df00SHao Zheng  *
5910a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
5920a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
5930a85df00SHao Zheng  */
594d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
595d4bcef3fSToshiaki Makita 					 int *depth)
5960a85df00SHao Zheng {
597d4bcef3fSToshiaki Makita 	unsigned int vlan_depth = skb->mac_len;
5980a85df00SHao Zheng 
599d4bcef3fSToshiaki Makita 	/* if type is 802.1Q/AD then the header should already be
600d4bcef3fSToshiaki Makita 	 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
601d4bcef3fSToshiaki Makita 	 * ETH_HLEN otherwise
602d4bcef3fSToshiaki Makita 	 */
603fe19c4f9SEric Garver 	if (eth_type_vlan(type)) {
604d4bcef3fSToshiaki Makita 		if (vlan_depth) {
605d4bcef3fSToshiaki Makita 			if (WARN_ON(vlan_depth < VLAN_HLEN))
606d4bcef3fSToshiaki Makita 				return 0;
607d4bcef3fSToshiaki Makita 			vlan_depth -= VLAN_HLEN;
608d4bcef3fSToshiaki Makita 		} else {
609d4bcef3fSToshiaki Makita 			vlan_depth = ETH_HLEN;
610d4bcef3fSToshiaki Makita 		}
611d4bcef3fSToshiaki Makita 		do {
612d4bcef3fSToshiaki Makita 			struct vlan_hdr *vh;
613d4bcef3fSToshiaki Makita 
614d4bcef3fSToshiaki Makita 			if (unlikely(!pskb_may_pull(skb,
615d4bcef3fSToshiaki Makita 						    vlan_depth + VLAN_HLEN)))
616d4bcef3fSToshiaki Makita 				return 0;
617d4bcef3fSToshiaki Makita 
618d4bcef3fSToshiaki Makita 			vh = (struct vlan_hdr *)(skb->data + vlan_depth);
619d4bcef3fSToshiaki Makita 			type = vh->h_vlan_encapsulated_proto;
620d4bcef3fSToshiaki Makita 			vlan_depth += VLAN_HLEN;
621fe19c4f9SEric Garver 		} while (eth_type_vlan(type));
6220a85df00SHao Zheng 	}
6230a85df00SHao Zheng 
624d4bcef3fSToshiaki Makita 	if (depth)
625d4bcef3fSToshiaki Makita 		*depth = vlan_depth;
626d4bcef3fSToshiaki Makita 
627d4bcef3fSToshiaki Makita 	return type;
628d4bcef3fSToshiaki Makita }
629d4bcef3fSToshiaki Makita 
630d4bcef3fSToshiaki Makita /**
631d4bcef3fSToshiaki Makita  * vlan_get_protocol - get protocol EtherType.
632d4bcef3fSToshiaki Makita  * @skb: skbuff to query
633d4bcef3fSToshiaki Makita  *
634d4bcef3fSToshiaki Makita  * Returns the EtherType of the packet, regardless of whether it is
635d4bcef3fSToshiaki Makita  * vlan encapsulated (normal or hardware accelerated) or not.
636d4bcef3fSToshiaki Makita  */
637d4bcef3fSToshiaki Makita static inline __be16 vlan_get_protocol(struct sk_buff *skb)
638d4bcef3fSToshiaki Makita {
639d4bcef3fSToshiaki Makita 	return __vlan_get_protocol(skb, skb->protocol, NULL);
6400a85df00SHao Zheng }
641396cf943SPravin B Shelar 
642396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
643396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
644396cf943SPravin B Shelar {
645396cf943SPravin B Shelar 	__be16 proto;
646da8c8724SCong Wang 	unsigned short *rawp;
647396cf943SPravin B Shelar 
648396cf943SPravin B Shelar 	/*
649396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
650396cf943SPravin B Shelar 	 * three protocols care about.
651396cf943SPravin B Shelar 	 */
652396cf943SPravin B Shelar 
653396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
6549545b22dSAlexander Duyck 	if (eth_proto_is_802_3(proto)) {
655396cf943SPravin B Shelar 		skb->protocol = proto;
656396cf943SPravin B Shelar 		return;
657396cf943SPravin B Shelar 	}
658396cf943SPravin B Shelar 
659da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
660da8c8724SCong Wang 	if (*rawp == 0xFFFF)
661396cf943SPravin B Shelar 		/*
662396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
663396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
664396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
665396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
666396cf943SPravin B Shelar 		 * but does for the rest.
667396cf943SPravin B Shelar 		 */
668396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
669396cf943SPravin B Shelar 	else
670396cf943SPravin B Shelar 		/*
671396cf943SPravin B Shelar 		 * Real 802.2 LLC
672396cf943SPravin B Shelar 		 */
673396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
674396cf943SPravin B Shelar }
67544a40855SVlad Yasevich 
676f5a7fb88SToshiaki Makita /**
677f5a7fb88SToshiaki Makita  * skb_vlan_tagged - check if skb is vlan tagged.
678f5a7fb88SToshiaki Makita  * @skb: skbuff to query
679f5a7fb88SToshiaki Makita  *
680f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged, regardless of whether it is hardware
681f5a7fb88SToshiaki Makita  * accelerated or not.
682f5a7fb88SToshiaki Makita  */
683f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb)
684f5a7fb88SToshiaki Makita {
685f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb) &&
686fe19c4f9SEric Garver 	    likely(!eth_type_vlan(skb->protocol)))
687f5a7fb88SToshiaki Makita 		return false;
688f5a7fb88SToshiaki Makita 
689f5a7fb88SToshiaki Makita 	return true;
690f5a7fb88SToshiaki Makita }
691f5a7fb88SToshiaki Makita 
692f5a7fb88SToshiaki Makita /**
693f5a7fb88SToshiaki Makita  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
694f5a7fb88SToshiaki Makita  * @skb: skbuff to query
695f5a7fb88SToshiaki Makita  *
696f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged with multiple vlan headers, regardless
697f5a7fb88SToshiaki Makita  * of whether it is hardware accelerated or not.
698f5a7fb88SToshiaki Makita  */
6997ce23672SToshiaki Makita static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
700f5a7fb88SToshiaki Makita {
701f5a7fb88SToshiaki Makita 	__be16 protocol = skb->protocol;
702f5a7fb88SToshiaki Makita 
703f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb)) {
704f5a7fb88SToshiaki Makita 		struct vlan_ethhdr *veh;
705f5a7fb88SToshiaki Makita 
706fe19c4f9SEric Garver 		if (likely(!eth_type_vlan(protocol)))
707f5a7fb88SToshiaki Makita 			return false;
708f5a7fb88SToshiaki Makita 
7097ce23672SToshiaki Makita 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
7107ce23672SToshiaki Makita 			return false;
7117ce23672SToshiaki Makita 
712f5a7fb88SToshiaki Makita 		veh = (struct vlan_ethhdr *)skb->data;
713f5a7fb88SToshiaki Makita 		protocol = veh->h_vlan_encapsulated_proto;
714f5a7fb88SToshiaki Makita 	}
715f5a7fb88SToshiaki Makita 
716fe19c4f9SEric Garver 	if (!eth_type_vlan(protocol))
717f5a7fb88SToshiaki Makita 		return false;
718f5a7fb88SToshiaki Makita 
719f5a7fb88SToshiaki Makita 	return true;
720f5a7fb88SToshiaki Makita }
721f5a7fb88SToshiaki Makita 
7228cb65d00SToshiaki Makita /**
7238cb65d00SToshiaki Makita  * vlan_features_check - drop unsafe features for skb with multiple tags.
7248cb65d00SToshiaki Makita  * @skb: skbuff to query
7258cb65d00SToshiaki Makita  * @features: features to be checked
7268cb65d00SToshiaki Makita  *
7278cb65d00SToshiaki Makita  * Returns features without unsafe ones if the skb has multiple tags.
7288cb65d00SToshiaki Makita  */
7297ce23672SToshiaki Makita static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
7308cb65d00SToshiaki Makita 						    netdev_features_t features)
7318cb65d00SToshiaki Makita {
73235d2f80bSVlad Yasevich 	if (skb_vlan_tagged_multi(skb)) {
73335d2f80bSVlad Yasevich 		/* In the case of multi-tagged packets, use a direct mask
73435d2f80bSVlad Yasevich 		 * instead of using netdev_interesect_features(), to make
73535d2f80bSVlad Yasevich 		 * sure that only devices supporting NETIF_F_HW_CSUM will
73635d2f80bSVlad Yasevich 		 * have checksum offloading support.
73735d2f80bSVlad Yasevich 		 */
73835d2f80bSVlad Yasevich 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
73935d2f80bSVlad Yasevich 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
74035d2f80bSVlad Yasevich 			    NETIF_F_HW_VLAN_STAG_TX;
74135d2f80bSVlad Yasevich 	}
7428cb65d00SToshiaki Makita 
7438cb65d00SToshiaki Makita 	return features;
7448cb65d00SToshiaki Makita }
7458cb65d00SToshiaki Makita 
74666e5133fSToshiaki Makita /**
74766e5133fSToshiaki Makita  * compare_vlan_header - Compare two vlan headers
74866e5133fSToshiaki Makita  * @h1: Pointer to vlan header
74966e5133fSToshiaki Makita  * @h2: Pointer to vlan header
75066e5133fSToshiaki Makita  *
75166e5133fSToshiaki Makita  * Compare two vlan headers, returns 0 if equal.
75266e5133fSToshiaki Makita  *
75366e5133fSToshiaki Makita  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
75466e5133fSToshiaki Makita  */
75566e5133fSToshiaki Makita static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
75666e5133fSToshiaki Makita 						const struct vlan_hdr *h2)
75766e5133fSToshiaki Makita {
75866e5133fSToshiaki Makita #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
75966e5133fSToshiaki Makita 	return *(u32 *)h1 ^ *(u32 *)h2;
76066e5133fSToshiaki Makita #else
76166e5133fSToshiaki Makita 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
76266e5133fSToshiaki Makita 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
76366e5133fSToshiaki Makita 		(__force u32)h2->h_vlan_encapsulated_proto);
76466e5133fSToshiaki Makita #endif
76566e5133fSToshiaki Makita }
7661da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
767