xref: /linux/include/linux/if_vlan.h (revision 35d2f80b07bbe03fb358afb0bdeff7437a7d67ff)
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)
84f6a66927SHadar Hen Zion #define skb_vlan_tag_get_prio(__skb)	((__skb)->vlan_tci & VLAN_PRIO_MASK)
851da177e4SLinus Torvalds 
86e267cb96SDavid S. Miller /**
87e267cb96SDavid S. Miller  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
88e267cb96SDavid S. Miller  *	@rx_packets: number of received packets
89e267cb96SDavid S. Miller  *	@rx_bytes: number of received bytes
90e267cb96SDavid S. Miller  *	@rx_multicast: number of received multicast packets
91e267cb96SDavid S. Miller  *	@tx_packets: number of transmitted packets
92e267cb96SDavid S. Miller  *	@tx_bytes: number of transmitted bytes
93e267cb96SDavid S. Miller  *	@syncp: synchronization point for 64bit counters
94e267cb96SDavid S. Miller  *	@rx_errors: number of rx errors
95e267cb96SDavid S. Miller  *	@tx_dropped: number of tx drops
96e267cb96SDavid S. Miller  */
97e267cb96SDavid S. Miller struct vlan_pcpu_stats {
98e267cb96SDavid S. Miller 	u64			rx_packets;
99e267cb96SDavid S. Miller 	u64			rx_bytes;
100e267cb96SDavid S. Miller 	u64			rx_multicast;
101e267cb96SDavid S. Miller 	u64			tx_packets;
102e267cb96SDavid S. Miller 	u64			tx_bytes;
103e267cb96SDavid S. Miller 	struct u64_stats_sync	syncp;
104e267cb96SDavid S. Miller 	u32			rx_errors;
105e267cb96SDavid S. Miller 	u32			tx_dropped;
106e267cb96SDavid S. Miller };
107e267cb96SDavid S. Miller 
1081e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1091e85c9b6SHannes Frederic Sowa 
110f06c7f9fSdingtianhong extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
1111e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
1121e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
1131e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
11471e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
1151e85c9b6SHannes Frederic Sowa 
1161e85c9b6SHannes Frederic Sowa /**
1171e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
1181e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
1191e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
1201e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
1211e85c9b6SHannes Frederic Sowa  */
1221e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
1231e85c9b6SHannes Frederic Sowa 	u32					priority;
1241e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
1251e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
1261e85c9b6SHannes Frederic Sowa };
1271e85c9b6SHannes Frederic Sowa 
128e267cb96SDavid S. Miller struct proc_dir_entry;
129e267cb96SDavid S. Miller struct netpoll;
130e267cb96SDavid S. Miller 
131e267cb96SDavid S. Miller /**
132e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
133e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
134e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
135e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
136e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
137e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
138e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
139e267cb96SDavid S. Miller  *	@flags: device flags
140e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
141e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
142e267cb96SDavid S. Miller  *	@dent: proc dir entry
143e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
144e267cb96SDavid S. Miller  */
145e267cb96SDavid S. Miller struct vlan_dev_priv {
146e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
147e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
148e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
149e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
150e267cb96SDavid S. Miller 
151e267cb96SDavid S. Miller 	__be16					vlan_proto;
152e267cb96SDavid S. Miller 	u16					vlan_id;
153e267cb96SDavid S. Miller 	u16					flags;
154e267cb96SDavid S. Miller 
155e267cb96SDavid S. Miller 	struct net_device			*real_dev;
156e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
157e267cb96SDavid S. Miller 
158e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
159e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
160e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
161e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
162e267cb96SDavid S. Miller #endif
163d38569abSVlad Yasevich 	unsigned int				nest_level;
164e267cb96SDavid S. Miller };
165e267cb96SDavid S. Miller 
166e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
167e267cb96SDavid S. Miller {
168e267cb96SDavid S. Miller 	return netdev_priv(dev);
169e267cb96SDavid S. Miller }
170e267cb96SDavid S. Miller 
171e267cb96SDavid S. Miller static inline u16
172e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
173e267cb96SDavid S. Miller {
174e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
175e267cb96SDavid S. Miller 
176e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
177e267cb96SDavid S. Miller 
178e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
179e267cb96SDavid S. Miller 	while (mp) {
180e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
181e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
182e267cb96SDavid S. Miller 					      * to mask correctly with the
183e267cb96SDavid S. Miller 					      * VLAN's TCI */
184e267cb96SDavid S. Miller 		}
185e267cb96SDavid S. Miller 		mp = mp->next;
186e267cb96SDavid S. Miller 	}
187e267cb96SDavid S. Miller 	return 0;
188e267cb96SDavid S. Miller }
189e267cb96SDavid S. Miller 
19048cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb);
1919b22ea56SPatrick McHardy 
19280d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
19380d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
19487002b03SJiri Pirko 
195348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev,
196348a1443SJiri Pirko 				const struct net_device *by_dev);
197348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev,
198348a1443SJiri Pirko 				 const struct net_device *by_dev);
1999b361c13SJiri Pirko 
2009b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev);
201e1618d46SVlad Yasevich 
202e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
203e1618d46SVlad Yasevich {
204e1618d46SVlad Yasevich 	BUG_ON(!is_vlan_dev(dev));
205e1618d46SVlad Yasevich 	return vlan_dev_priv(dev)->nest_level;
206e1618d46SVlad Yasevich }
2077750f403SPatrick McHardy #else
208cec9c133SJiri Pirko static inline struct net_device *
209f06c7f9fSdingtianhong __vlan_find_dev_deep_rcu(struct net_device *real_dev,
2109fae27b3SPatrick McHardy 		     __be16 vlan_proto, u16 vlan_id)
211cec9c133SJiri Pirko {
212cec9c133SJiri Pirko 	return NULL;
213cec9c133SJiri Pirko }
214cec9c133SJiri Pirko 
21522d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
21622d1ba74SPatrick McHardy {
21722d1ba74SPatrick McHardy 	BUG();
21822d1ba74SPatrick McHardy 	return NULL;
21922d1ba74SPatrick McHardy }
22022d1ba74SPatrick McHardy 
22122d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
22222d1ba74SPatrick McHardy {
22322d1ba74SPatrick McHardy 	BUG();
22422d1ba74SPatrick McHardy 	return 0;
22522d1ba74SPatrick McHardy }
22622d1ba74SPatrick McHardy 
22771e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
22871e415e4Sdingtianhong {
22971e415e4Sdingtianhong 	BUG();
23071e415e4Sdingtianhong 	return 0;
23171e415e4Sdingtianhong }
23271e415e4Sdingtianhong 
233d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
234d3243539SEyal Perry 					       u32 skprio)
235d3243539SEyal Perry {
236d3243539SEyal Perry 	return 0;
237d3243539SEyal Perry }
238d3243539SEyal Perry 
23948cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb)
2409b22ea56SPatrick McHardy {
2413701e513SJesse Gross 	return false;
2429b22ea56SPatrick McHardy }
243e1c096e2SHerbert Xu 
2449fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
24587002b03SJiri Pirko {
24687002b03SJiri Pirko 	return 0;
24787002b03SJiri Pirko }
24887002b03SJiri Pirko 
2499fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
25087002b03SJiri Pirko {
25187002b03SJiri Pirko }
252348a1443SJiri Pirko 
253348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
254348a1443SJiri Pirko 				       const struct net_device *by_dev)
255348a1443SJiri Pirko {
256348a1443SJiri Pirko 	return 0;
257348a1443SJiri Pirko }
258348a1443SJiri Pirko 
259348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
260348a1443SJiri Pirko 					const struct net_device *by_dev)
261348a1443SJiri Pirko {
262348a1443SJiri Pirko }
2639b361c13SJiri Pirko 
2649b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
2659b361c13SJiri Pirko {
2669b361c13SJiri Pirko 	return false;
2679b361c13SJiri Pirko }
268e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
269e1618d46SVlad Yasevich {
270e1618d46SVlad Yasevich 	BUG();
271e1618d46SVlad Yasevich 	return 0;
272e1618d46SVlad Yasevich }
2737750f403SPatrick McHardy #endif
2741da177e4SLinus Torvalds 
275fe19c4f9SEric Garver /**
276fe19c4f9SEric Garver  * eth_type_vlan - check for valid vlan ether type.
277fe19c4f9SEric Garver  * @ethertype: ether type to check
278fe19c4f9SEric Garver  *
279fe19c4f9SEric Garver  * Returns true if the ether type is a vlan ether type.
280fe19c4f9SEric Garver  */
281fe19c4f9SEric Garver static inline bool eth_type_vlan(__be16 ethertype)
282fe19c4f9SEric Garver {
283fe19c4f9SEric Garver 	switch (ethertype) {
284fe19c4f9SEric Garver 	case htons(ETH_P_8021Q):
285fe19c4f9SEric Garver 	case htons(ETH_P_8021AD):
286fe19c4f9SEric Garver 		return true;
287fe19c4f9SEric Garver 	default:
288fe19c4f9SEric Garver 		return false;
289fe19c4f9SEric Garver 	}
290fe19c4f9SEric Garver }
291fe19c4f9SEric Garver 
29286a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
29386a9bad3SPatrick McHardy 					   __be16 proto)
29486a9bad3SPatrick McHardy {
29586a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
29686a9bad3SPatrick McHardy 		return true;
2978ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
2988ad227ffSPatrick McHardy 		return true;
29986a9bad3SPatrick McHardy 	return false;
30086a9bad3SPatrick McHardy }
30186a9bad3SPatrick McHardy 
3029bb8582eSPatrick McHardy /**
30315255a43SJiri Pirko  * __vlan_insert_tag - regular VLAN tag inserting
30415255a43SJiri Pirko  * @skb: skbuff to tag
30515255a43SJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
30615255a43SJiri Pirko  * @vlan_tci: VLAN TCI to insert
30715255a43SJiri Pirko  *
30815255a43SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
30915255a43SJiri Pirko  * Returns error if skb_cow_head failes.
31015255a43SJiri Pirko  *
31115255a43SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
31215255a43SJiri Pirko  */
31315255a43SJiri Pirko static inline int __vlan_insert_tag(struct sk_buff *skb,
31415255a43SJiri Pirko 				    __be16 vlan_proto, u16 vlan_tci)
31515255a43SJiri Pirko {
31615255a43SJiri Pirko 	struct vlan_ethhdr *veth;
31715255a43SJiri Pirko 
31815255a43SJiri Pirko 	if (skb_cow_head(skb, VLAN_HLEN) < 0)
31915255a43SJiri Pirko 		return -ENOMEM;
32015255a43SJiri Pirko 
32115255a43SJiri Pirko 	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
32215255a43SJiri Pirko 
32315255a43SJiri Pirko 	/* Move the mac addresses to the beginning of the new header. */
32415255a43SJiri Pirko 	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
32515255a43SJiri Pirko 	skb->mac_header -= VLAN_HLEN;
32615255a43SJiri Pirko 
32715255a43SJiri Pirko 	/* first, the ethernet type */
32815255a43SJiri Pirko 	veth->h_vlan_proto = vlan_proto;
32915255a43SJiri Pirko 
33015255a43SJiri Pirko 	/* now, the TCI */
33115255a43SJiri Pirko 	veth->h_vlan_TCI = htons(vlan_tci);
33215255a43SJiri Pirko 
33315255a43SJiri Pirko 	return 0;
33415255a43SJiri Pirko }
33515255a43SJiri Pirko 
33615255a43SJiri Pirko /**
3370b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
3381da177e4SLinus Torvalds  * @skb: skbuff to tag
33986a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
3409bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3411da177e4SLinus Torvalds  *
3421da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
3431da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
3461da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
3470b5c9db1SJiri Pirko  *
3480b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
3491da177e4SLinus Torvalds  */
35086a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
35186a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
3521da177e4SLinus Torvalds {
35315255a43SJiri Pirko 	int err;
3541da177e4SLinus Torvalds 
35515255a43SJiri Pirko 	err = __vlan_insert_tag(skb, vlan_proto, vlan_tci);
35615255a43SJiri Pirko 	if (err) {
357fb7c03dfSEric W. Biederman 		dev_kfree_skb_any(skb);
3581da177e4SLinus Torvalds 		return NULL;
3591da177e4SLinus Torvalds 	}
3600b5c9db1SJiri Pirko 	return skb;
3610b5c9db1SJiri Pirko }
3621da177e4SLinus Torvalds 
3630b5c9db1SJiri Pirko /**
36462749e2cSJiri Pirko  * vlan_insert_tag_set_proto - regular VLAN tag inserting
3650b5c9db1SJiri Pirko  * @skb: skbuff to tag
36662749e2cSJiri Pirko  * @vlan_proto: VLAN encapsulation protocol
3670b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
3680b5c9db1SJiri Pirko  *
3690b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3700b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
3710b5c9db1SJiri Pirko  *
3720b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
3730b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
3740b5c9db1SJiri Pirko  */
37562749e2cSJiri Pirko static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
37662749e2cSJiri Pirko 							__be16 vlan_proto,
37762749e2cSJiri Pirko 							u16 vlan_tci)
3780b5c9db1SJiri Pirko {
37986a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
3800b5c9db1SJiri Pirko 	if (skb)
38186a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
3821da177e4SLinus Torvalds 	return skb;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3855968250cSJiri Pirko /*
3865968250cSJiri Pirko  * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
3875968250cSJiri Pirko  * @skb: skbuff to tag
3885968250cSJiri Pirko  *
3895968250cSJiri Pirko  * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
3905968250cSJiri Pirko  *
3915968250cSJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
3925968250cSJiri Pirko  * doesn't have to worry about freeing the original skb.
3935968250cSJiri Pirko  */
3945968250cSJiri Pirko static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
3955968250cSJiri Pirko {
3965968250cSJiri Pirko 	skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
397df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
3985968250cSJiri Pirko 	if (likely(skb))
3995968250cSJiri Pirko 		skb->vlan_tci = 0;
4005968250cSJiri Pirko 	return skb;
4015968250cSJiri Pirko }
4025968250cSJiri Pirko 
4031da177e4SLinus Torvalds /**
4041da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
4051da177e4SLinus Torvalds  * @skb: skbuff to tag
40686a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
4079bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
4081da177e4SLinus Torvalds  *
4096aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
4101da177e4SLinus Torvalds  */
411b960a0acSJiri Pirko static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
412b960a0acSJiri Pirko 					  __be16 vlan_proto, u16 vlan_tci)
4131da177e4SLinus Torvalds {
41486a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
41505423b24SEric Dumazet 	skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /**
4191da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
4201da177e4SLinus Torvalds  * @skb: skbuff to query
421f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
4221da177e4SLinus Torvalds  *
4231da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
4241da177e4SLinus Torvalds  */
4259bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
4281da177e4SLinus Torvalds 
429fe19c4f9SEric Garver 	if (!eth_type_vlan(veth->h_vlan_proto))
4301da177e4SLinus Torvalds 		return -EINVAL;
4311da177e4SLinus Torvalds 
4329bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
4331da177e4SLinus Torvalds 	return 0;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /**
4371da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
4381da177e4SLinus Torvalds  * @skb: skbuff to query
439f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
4401da177e4SLinus Torvalds  *
4416aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
4421da177e4SLinus Torvalds  */
44318149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
4449bb8582eSPatrick McHardy 					 u16 *vlan_tci)
4451da177e4SLinus Torvalds {
446df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
447df8a39deSJiri Pirko 		*vlan_tci = skb_vlan_tag_get(skb);
4481da177e4SLinus Torvalds 		return 0;
4491da177e4SLinus Torvalds 	} else {
4509bb8582eSPatrick McHardy 		*vlan_tci = 0;
4511da177e4SLinus Torvalds 		return -EINVAL;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /**
4581da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
4591da177e4SLinus Torvalds  * @skb: skbuff to query
460f4fb874cSVivien Didelot  * @vlan_tci: buffer to store value
4611da177e4SLinus Torvalds  *
4621da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
4631da177e4SLinus Torvalds  */
4649bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
4651da177e4SLinus Torvalds {
466f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
4679bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
4681da177e4SLinus Torvalds 	} else {
4699bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4730a85df00SHao Zheng /**
4740a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
4750a85df00SHao Zheng  * @skb: skbuff to query
476d4bcef3fSToshiaki Makita  * @type: first vlan protocol
477d4bcef3fSToshiaki Makita  * @depth: buffer to store length of eth and vlan tags in bytes
4780a85df00SHao Zheng  *
4790a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
4800a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
4810a85df00SHao Zheng  */
482d4bcef3fSToshiaki Makita static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
483d4bcef3fSToshiaki Makita 					 int *depth)
4840a85df00SHao Zheng {
485d4bcef3fSToshiaki Makita 	unsigned int vlan_depth = skb->mac_len;
4860a85df00SHao Zheng 
487d4bcef3fSToshiaki Makita 	/* if type is 802.1Q/AD then the header should already be
488d4bcef3fSToshiaki Makita 	 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
489d4bcef3fSToshiaki Makita 	 * ETH_HLEN otherwise
490d4bcef3fSToshiaki Makita 	 */
491fe19c4f9SEric Garver 	if (eth_type_vlan(type)) {
492d4bcef3fSToshiaki Makita 		if (vlan_depth) {
493d4bcef3fSToshiaki Makita 			if (WARN_ON(vlan_depth < VLAN_HLEN))
494d4bcef3fSToshiaki Makita 				return 0;
495d4bcef3fSToshiaki Makita 			vlan_depth -= VLAN_HLEN;
496d4bcef3fSToshiaki Makita 		} else {
497d4bcef3fSToshiaki Makita 			vlan_depth = ETH_HLEN;
498d4bcef3fSToshiaki Makita 		}
499d4bcef3fSToshiaki Makita 		do {
500d4bcef3fSToshiaki Makita 			struct vlan_hdr *vh;
501d4bcef3fSToshiaki Makita 
502d4bcef3fSToshiaki Makita 			if (unlikely(!pskb_may_pull(skb,
503d4bcef3fSToshiaki Makita 						    vlan_depth + VLAN_HLEN)))
504d4bcef3fSToshiaki Makita 				return 0;
505d4bcef3fSToshiaki Makita 
506d4bcef3fSToshiaki Makita 			vh = (struct vlan_hdr *)(skb->data + vlan_depth);
507d4bcef3fSToshiaki Makita 			type = vh->h_vlan_encapsulated_proto;
508d4bcef3fSToshiaki Makita 			vlan_depth += VLAN_HLEN;
509fe19c4f9SEric Garver 		} while (eth_type_vlan(type));
5100a85df00SHao Zheng 	}
5110a85df00SHao Zheng 
512d4bcef3fSToshiaki Makita 	if (depth)
513d4bcef3fSToshiaki Makita 		*depth = vlan_depth;
514d4bcef3fSToshiaki Makita 
515d4bcef3fSToshiaki Makita 	return type;
516d4bcef3fSToshiaki Makita }
517d4bcef3fSToshiaki Makita 
518d4bcef3fSToshiaki Makita /**
519d4bcef3fSToshiaki Makita  * vlan_get_protocol - get protocol EtherType.
520d4bcef3fSToshiaki Makita  * @skb: skbuff to query
521d4bcef3fSToshiaki Makita  *
522d4bcef3fSToshiaki Makita  * Returns the EtherType of the packet, regardless of whether it is
523d4bcef3fSToshiaki Makita  * vlan encapsulated (normal or hardware accelerated) or not.
524d4bcef3fSToshiaki Makita  */
525d4bcef3fSToshiaki Makita static inline __be16 vlan_get_protocol(struct sk_buff *skb)
526d4bcef3fSToshiaki Makita {
527d4bcef3fSToshiaki Makita 	return __vlan_get_protocol(skb, skb->protocol, NULL);
5280a85df00SHao Zheng }
529396cf943SPravin B Shelar 
530396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
531396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
532396cf943SPravin B Shelar {
533396cf943SPravin B Shelar 	__be16 proto;
534da8c8724SCong Wang 	unsigned short *rawp;
535396cf943SPravin B Shelar 
536396cf943SPravin B Shelar 	/*
537396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
538396cf943SPravin B Shelar 	 * three protocols care about.
539396cf943SPravin B Shelar 	 */
540396cf943SPravin B Shelar 
541396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
5429545b22dSAlexander Duyck 	if (eth_proto_is_802_3(proto)) {
543396cf943SPravin B Shelar 		skb->protocol = proto;
544396cf943SPravin B Shelar 		return;
545396cf943SPravin B Shelar 	}
546396cf943SPravin B Shelar 
547da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
548da8c8724SCong Wang 	if (*rawp == 0xFFFF)
549396cf943SPravin B Shelar 		/*
550396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
551396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
552396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
553396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
554396cf943SPravin B Shelar 		 * but does for the rest.
555396cf943SPravin B Shelar 		 */
556396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
557396cf943SPravin B Shelar 	else
558396cf943SPravin B Shelar 		/*
559396cf943SPravin B Shelar 		 * Real 802.2 LLC
560396cf943SPravin B Shelar 		 */
561396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
562396cf943SPravin B Shelar }
56344a40855SVlad Yasevich 
564f5a7fb88SToshiaki Makita /**
565f5a7fb88SToshiaki Makita  * skb_vlan_tagged - check if skb is vlan tagged.
566f5a7fb88SToshiaki Makita  * @skb: skbuff to query
567f5a7fb88SToshiaki Makita  *
568f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged, regardless of whether it is hardware
569f5a7fb88SToshiaki Makita  * accelerated or not.
570f5a7fb88SToshiaki Makita  */
571f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged(const struct sk_buff *skb)
572f5a7fb88SToshiaki Makita {
573f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb) &&
574fe19c4f9SEric Garver 	    likely(!eth_type_vlan(skb->protocol)))
575f5a7fb88SToshiaki Makita 		return false;
576f5a7fb88SToshiaki Makita 
577f5a7fb88SToshiaki Makita 	return true;
578f5a7fb88SToshiaki Makita }
579f5a7fb88SToshiaki Makita 
580f5a7fb88SToshiaki Makita /**
581f5a7fb88SToshiaki Makita  * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
582f5a7fb88SToshiaki Makita  * @skb: skbuff to query
583f5a7fb88SToshiaki Makita  *
584f5a7fb88SToshiaki Makita  * Returns true if the skb is tagged with multiple vlan headers, regardless
585f5a7fb88SToshiaki Makita  * of whether it is hardware accelerated or not.
586f5a7fb88SToshiaki Makita  */
587f5a7fb88SToshiaki Makita static inline bool skb_vlan_tagged_multi(const struct sk_buff *skb)
588f5a7fb88SToshiaki Makita {
589f5a7fb88SToshiaki Makita 	__be16 protocol = skb->protocol;
590f5a7fb88SToshiaki Makita 
591f5a7fb88SToshiaki Makita 	if (!skb_vlan_tag_present(skb)) {
592f5a7fb88SToshiaki Makita 		struct vlan_ethhdr *veh;
593f5a7fb88SToshiaki Makita 
594fe19c4f9SEric Garver 		if (likely(!eth_type_vlan(protocol)))
595f5a7fb88SToshiaki Makita 			return false;
596f5a7fb88SToshiaki Makita 
597f5a7fb88SToshiaki Makita 		veh = (struct vlan_ethhdr *)skb->data;
598f5a7fb88SToshiaki Makita 		protocol = veh->h_vlan_encapsulated_proto;
599f5a7fb88SToshiaki Makita 	}
600f5a7fb88SToshiaki Makita 
601fe19c4f9SEric Garver 	if (!eth_type_vlan(protocol))
602f5a7fb88SToshiaki Makita 		return false;
603f5a7fb88SToshiaki Makita 
604f5a7fb88SToshiaki Makita 	return true;
605f5a7fb88SToshiaki Makita }
606f5a7fb88SToshiaki Makita 
6078cb65d00SToshiaki Makita /**
6088cb65d00SToshiaki Makita  * vlan_features_check - drop unsafe features for skb with multiple tags.
6098cb65d00SToshiaki Makita  * @skb: skbuff to query
6108cb65d00SToshiaki Makita  * @features: features to be checked
6118cb65d00SToshiaki Makita  *
6128cb65d00SToshiaki Makita  * Returns features without unsafe ones if the skb has multiple tags.
6138cb65d00SToshiaki Makita  */
6148cb65d00SToshiaki Makita static inline netdev_features_t vlan_features_check(const struct sk_buff *skb,
6158cb65d00SToshiaki Makita 						    netdev_features_t features)
6168cb65d00SToshiaki Makita {
617*35d2f80bSVlad Yasevich 	if (skb_vlan_tagged_multi(skb)) {
618*35d2f80bSVlad Yasevich 		/* In the case of multi-tagged packets, use a direct mask
619*35d2f80bSVlad Yasevich 		 * instead of using netdev_interesect_features(), to make
620*35d2f80bSVlad Yasevich 		 * sure that only devices supporting NETIF_F_HW_CSUM will
621*35d2f80bSVlad Yasevich 		 * have checksum offloading support.
622*35d2f80bSVlad Yasevich 		 */
623*35d2f80bSVlad Yasevich 		features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
624*35d2f80bSVlad Yasevich 			    NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
625*35d2f80bSVlad Yasevich 			    NETIF_F_HW_VLAN_STAG_TX;
626*35d2f80bSVlad Yasevich 	}
6278cb65d00SToshiaki Makita 
6288cb65d00SToshiaki Makita 	return features;
6298cb65d00SToshiaki Makita }
6308cb65d00SToshiaki Makita 
63166e5133fSToshiaki Makita /**
63266e5133fSToshiaki Makita  * compare_vlan_header - Compare two vlan headers
63366e5133fSToshiaki Makita  * @h1: Pointer to vlan header
63466e5133fSToshiaki Makita  * @h2: Pointer to vlan header
63566e5133fSToshiaki Makita  *
63666e5133fSToshiaki Makita  * Compare two vlan headers, returns 0 if equal.
63766e5133fSToshiaki Makita  *
63866e5133fSToshiaki Makita  * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
63966e5133fSToshiaki Makita  */
64066e5133fSToshiaki Makita static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
64166e5133fSToshiaki Makita 						const struct vlan_hdr *h2)
64266e5133fSToshiaki Makita {
64366e5133fSToshiaki Makita #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
64466e5133fSToshiaki Makita 	return *(u32 *)h1 ^ *(u32 *)h2;
64566e5133fSToshiaki Makita #else
64666e5133fSToshiaki Makita 	return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
64766e5133fSToshiaki Makita 	       ((__force u32)h1->h_vlan_encapsulated_proto ^
64866e5133fSToshiaki Makita 		(__force u32)h2->h_vlan_encapsulated_proto);
64966e5133fSToshiaki Makita #endif
65066e5133fSToshiaki Makita }
6511da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
652