xref: /linux/include/linux/if_vlan.h (revision 44a4085538c844e79d6ee6bcf46fabf7c57a9a38)
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 
76d38569abSVlad Yasevich static inline bool is_vlan_dev(struct net_device *dev)
776dcbbe25SNeil Horman {
786dcbbe25SNeil Horman         return dev->priv_flags & IFF_802_1Q_VLAN;
796dcbbe25SNeil Horman }
806dcbbe25SNeil Horman 
8105423b24SEric Dumazet #define vlan_tx_tag_present(__skb)	((__skb)->vlan_tci & VLAN_TAG_PRESENT)
8205423b24SEric Dumazet #define vlan_tx_tag_get(__skb)		((__skb)->vlan_tci & ~VLAN_TAG_PRESENT)
83d4b812deSEric Dumazet #define vlan_tx_tag_get_id(__skb)	((__skb)->vlan_tci & VLAN_VID_MASK)
841da177e4SLinus Torvalds 
85e267cb96SDavid S. Miller /**
86e267cb96SDavid S. Miller  *	struct vlan_pcpu_stats - VLAN percpu rx/tx stats
87e267cb96SDavid S. Miller  *	@rx_packets: number of received packets
88e267cb96SDavid S. Miller  *	@rx_bytes: number of received bytes
89e267cb96SDavid S. Miller  *	@rx_multicast: number of received multicast packets
90e267cb96SDavid S. Miller  *	@tx_packets: number of transmitted packets
91e267cb96SDavid S. Miller  *	@tx_bytes: number of transmitted bytes
92e267cb96SDavid S. Miller  *	@syncp: synchronization point for 64bit counters
93e267cb96SDavid S. Miller  *	@rx_errors: number of rx errors
94e267cb96SDavid S. Miller  *	@tx_dropped: number of tx drops
95e267cb96SDavid S. Miller  */
96e267cb96SDavid S. Miller struct vlan_pcpu_stats {
97e267cb96SDavid S. Miller 	u64			rx_packets;
98e267cb96SDavid S. Miller 	u64			rx_bytes;
99e267cb96SDavid S. Miller 	u64			rx_multicast;
100e267cb96SDavid S. Miller 	u64			tx_packets;
101e267cb96SDavid S. Miller 	u64			tx_bytes;
102e267cb96SDavid S. Miller 	struct u64_stats_sync	syncp;
103e267cb96SDavid S. Miller 	u32			rx_errors;
104e267cb96SDavid S. Miller 	u32			tx_dropped;
105e267cb96SDavid S. Miller };
106e267cb96SDavid S. Miller 
1071e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1081e85c9b6SHannes Frederic Sowa 
1091e85c9b6SHannes Frederic Sowa extern struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
1101e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
1111e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
1121e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
11371e415e4Sdingtianhong extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
1141e85c9b6SHannes Frederic Sowa 
1151e85c9b6SHannes Frederic Sowa /**
1161e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
1171e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
1181e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
1191e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
1201e85c9b6SHannes Frederic Sowa  */
1211e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
1221e85c9b6SHannes Frederic Sowa 	u32					priority;
1231e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
1241e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
1251e85c9b6SHannes Frederic Sowa };
1261e85c9b6SHannes Frederic Sowa 
127e267cb96SDavid S. Miller struct proc_dir_entry;
128e267cb96SDavid S. Miller struct netpoll;
129e267cb96SDavid S. Miller 
130e267cb96SDavid S. Miller /**
131e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
132e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
133e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
134e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
135e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
136e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
137e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
138e267cb96SDavid S. Miller  *	@flags: device flags
139e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
140e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
141e267cb96SDavid S. Miller  *	@dent: proc dir entry
142e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
143e267cb96SDavid S. Miller  */
144e267cb96SDavid S. Miller struct vlan_dev_priv {
145e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
146e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
147e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
148e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
149e267cb96SDavid S. Miller 
150e267cb96SDavid S. Miller 	__be16					vlan_proto;
151e267cb96SDavid S. Miller 	u16					vlan_id;
152e267cb96SDavid S. Miller 	u16					flags;
153e267cb96SDavid S. Miller 
154e267cb96SDavid S. Miller 	struct net_device			*real_dev;
155e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
156e267cb96SDavid S. Miller 
157e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
158e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
159e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
160e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
161e267cb96SDavid S. Miller #endif
162d38569abSVlad Yasevich 	unsigned int				nest_level;
163e267cb96SDavid S. Miller };
164e267cb96SDavid S. Miller 
165e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
166e267cb96SDavid S. Miller {
167e267cb96SDavid S. Miller 	return netdev_priv(dev);
168e267cb96SDavid S. Miller }
169e267cb96SDavid S. Miller 
170e267cb96SDavid S. Miller static inline u16
171e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
172e267cb96SDavid S. Miller {
173e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
174e267cb96SDavid S. Miller 
175e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
176e267cb96SDavid S. Miller 
177e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
178e267cb96SDavid S. Miller 	while (mp) {
179e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
180e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
181e267cb96SDavid S. Miller 					      * to mask correctly with the
182e267cb96SDavid S. Miller 					      * VLAN's TCI */
183e267cb96SDavid S. Miller 		}
184e267cb96SDavid S. Miller 		mp = mp->next;
185e267cb96SDavid S. Miller 	}
186e267cb96SDavid S. Miller 	return 0;
187e267cb96SDavid S. Miller }
188e267cb96SDavid S. Miller 
18948cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb);
190bcc6d479SJiri Pirko extern struct sk_buff *vlan_untag(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);
2017750f403SPatrick McHardy #else
202cec9c133SJiri Pirko static inline struct net_device *
2039fae27b3SPatrick McHardy __vlan_find_dev_deep(struct net_device *real_dev,
2049fae27b3SPatrick McHardy 		     __be16 vlan_proto, u16 vlan_id)
205cec9c133SJiri Pirko {
206cec9c133SJiri Pirko 	return NULL;
207cec9c133SJiri Pirko }
208cec9c133SJiri Pirko 
20922d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
21022d1ba74SPatrick McHardy {
21122d1ba74SPatrick McHardy 	BUG();
21222d1ba74SPatrick McHardy 	return NULL;
21322d1ba74SPatrick McHardy }
21422d1ba74SPatrick McHardy 
21522d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
21622d1ba74SPatrick McHardy {
21722d1ba74SPatrick McHardy 	BUG();
21822d1ba74SPatrick McHardy 	return 0;
21922d1ba74SPatrick McHardy }
22022d1ba74SPatrick McHardy 
22171e415e4Sdingtianhong static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
22271e415e4Sdingtianhong {
22371e415e4Sdingtianhong 	BUG();
22471e415e4Sdingtianhong 	return 0;
22571e415e4Sdingtianhong }
22671e415e4Sdingtianhong 
227d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
228d3243539SEyal Perry 					       u32 skprio)
229d3243539SEyal Perry {
230d3243539SEyal Perry 	return 0;
231d3243539SEyal Perry }
232d3243539SEyal Perry 
23348cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb)
2349b22ea56SPatrick McHardy {
2353701e513SJesse Gross 	return false;
2369b22ea56SPatrick McHardy }
237e1c096e2SHerbert Xu 
2386139e75fSDavid S. Miller static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
239bcc6d479SJiri Pirko {
240bcc6d479SJiri Pirko 	return skb;
241bcc6d479SJiri Pirko }
24287002b03SJiri Pirko 
2439fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
24487002b03SJiri Pirko {
24587002b03SJiri Pirko 	return 0;
24687002b03SJiri Pirko }
24787002b03SJiri Pirko 
2489fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
24987002b03SJiri Pirko {
25087002b03SJiri Pirko }
251348a1443SJiri Pirko 
252348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
253348a1443SJiri Pirko 				       const struct net_device *by_dev)
254348a1443SJiri Pirko {
255348a1443SJiri Pirko 	return 0;
256348a1443SJiri Pirko }
257348a1443SJiri Pirko 
258348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
259348a1443SJiri Pirko 					const struct net_device *by_dev)
260348a1443SJiri Pirko {
261348a1443SJiri Pirko }
2629b361c13SJiri Pirko 
2639b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
2649b361c13SJiri Pirko {
2659b361c13SJiri Pirko 	return false;
2669b361c13SJiri Pirko }
2677750f403SPatrick McHardy #endif
2681da177e4SLinus Torvalds 
26986a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
27086a9bad3SPatrick McHardy 					   __be16 proto)
27186a9bad3SPatrick McHardy {
27286a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
27386a9bad3SPatrick McHardy 		return true;
2748ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
2758ad227ffSPatrick McHardy 		return true;
27686a9bad3SPatrick McHardy 	return false;
27786a9bad3SPatrick McHardy }
27886a9bad3SPatrick McHardy 
2799bb8582eSPatrick McHardy /**
2800b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
2811da177e4SLinus Torvalds  * @skb: skbuff to tag
28286a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
2839bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
2861da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
2891da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
2900b5c9db1SJiri Pirko  *
2910b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
2921da177e4SLinus Torvalds  */
29386a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
29486a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	struct vlan_ethhdr *veth;
2971da177e4SLinus Torvalds 
29811a100f8SPatrick McHardy 	if (skb_cow_head(skb, VLAN_HLEN) < 0) {
299fb7c03dfSEric W. Biederman 		dev_kfree_skb_any(skb);
3001da177e4SLinus Torvalds 		return NULL;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	/* Move the mac addresses to the beginning of the new header. */
305a3f671b3SJoe Perches 	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
306e4dd6188SJarek Poplawski 	skb->mac_header -= VLAN_HLEN;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/* first, the ethernet type */
30986a9bad3SPatrick McHardy 	veth->h_vlan_proto = vlan_proto;
3101da177e4SLinus Torvalds 
3119bb8582eSPatrick McHardy 	/* now, the TCI */
3129bb8582eSPatrick McHardy 	veth->h_vlan_TCI = htons(vlan_tci);
3131da177e4SLinus Torvalds 
3140b5c9db1SJiri Pirko 	return skb;
3150b5c9db1SJiri Pirko }
3161da177e4SLinus Torvalds 
3170b5c9db1SJiri Pirko /**
3180b5c9db1SJiri Pirko  * __vlan_put_tag - regular VLAN tag inserting
3190b5c9db1SJiri Pirko  * @skb: skbuff to tag
3200b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
3210b5c9db1SJiri Pirko  *
3220b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3230b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
3240b5c9db1SJiri Pirko  *
3250b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
3260b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
3270b5c9db1SJiri Pirko  */
32886a9bad3SPatrick McHardy static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb,
32986a9bad3SPatrick McHardy 					     __be16 vlan_proto, u16 vlan_tci)
3300b5c9db1SJiri Pirko {
33186a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
3320b5c9db1SJiri Pirko 	if (skb)
33386a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
3341da177e4SLinus Torvalds 	return skb;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /**
3381da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
3391da177e4SLinus Torvalds  * @skb: skbuff to tag
34086a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
3419bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3421da177e4SLinus Torvalds  *
3436aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
3441da177e4SLinus Torvalds  */
3459bb8582eSPatrick McHardy static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
34686a9bad3SPatrick McHardy 						     __be16 vlan_proto,
3479bb8582eSPatrick McHardy 						     u16 vlan_tci)
3481da177e4SLinus Torvalds {
34986a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
35005423b24SEric Dumazet 	skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
3511da177e4SLinus Torvalds 	return skb;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /**
3551da177e4SLinus Torvalds  * vlan_put_tag - inserts VLAN tag according to device features
3561da177e4SLinus Torvalds  * @skb: skbuff to tag
3579bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3581da177e4SLinus Torvalds  *
3591da177e4SLinus Torvalds  * Assumes skb->dev is the target that will xmit this frame.
3601da177e4SLinus Torvalds  * Returns a VLAN tagged skb.
3611da177e4SLinus Torvalds  */
36286a9bad3SPatrick McHardy static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb,
36386a9bad3SPatrick McHardy 					   __be16 vlan_proto, u16 vlan_tci)
3641da177e4SLinus Torvalds {
36586a9bad3SPatrick McHardy 	if (vlan_hw_offload_capable(skb->dev->features, vlan_proto)) {
36686a9bad3SPatrick McHardy 		return __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
3671da177e4SLinus Torvalds 	} else {
36886a9bad3SPatrick McHardy 		return __vlan_put_tag(skb, vlan_proto, vlan_tci);
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /**
3731da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
3741da177e4SLinus Torvalds  * @skb: skbuff to query
3759bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
3761da177e4SLinus Torvalds  *
3771da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
3781da177e4SLinus Torvalds  */
3799bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
3821da177e4SLinus Torvalds 
3838ad227ffSPatrick McHardy 	if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
3848ad227ffSPatrick McHardy 	    veth->h_vlan_proto != htons(ETH_P_8021AD))
3851da177e4SLinus Torvalds 		return -EINVAL;
3861da177e4SLinus Torvalds 
3879bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
3881da177e4SLinus Torvalds 	return 0;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /**
3921da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
3931da177e4SLinus Torvalds  * @skb: skbuff to query
3949bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
3951da177e4SLinus Torvalds  *
3966aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
3971da177e4SLinus Torvalds  */
39818149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
3999bb8582eSPatrick McHardy 					 u16 *vlan_tci)
4001da177e4SLinus Torvalds {
4016aa895b0SPatrick McHardy 	if (vlan_tx_tag_present(skb)) {
40205423b24SEric Dumazet 		*vlan_tci = vlan_tx_tag_get(skb);
4031da177e4SLinus Torvalds 		return 0;
4041da177e4SLinus Torvalds 	} else {
4059bb8582eSPatrick McHardy 		*vlan_tci = 0;
4061da177e4SLinus Torvalds 		return -EINVAL;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /**
4131da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
4141da177e4SLinus Torvalds  * @skb: skbuff to query
4159bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
4181da177e4SLinus Torvalds  */
4199bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
4201da177e4SLinus Torvalds {
421f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
4229bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
4231da177e4SLinus Torvalds 	} else {
4249bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4280a85df00SHao Zheng /**
4290a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
4300a85df00SHao Zheng  * @skb: skbuff to query
4310a85df00SHao Zheng  *
4320a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
4330a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
4340a85df00SHao Zheng  */
4350a85df00SHao Zheng static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
4360a85df00SHao Zheng {
4370a85df00SHao Zheng 	__be16 protocol = 0;
4380a85df00SHao Zheng 
4390a85df00SHao Zheng 	if (vlan_tx_tag_present(skb) ||
4400a85df00SHao Zheng 	     skb->protocol != cpu_to_be16(ETH_P_8021Q))
4410a85df00SHao Zheng 		protocol = skb->protocol;
4420a85df00SHao Zheng 	else {
4430a85df00SHao Zheng 		__be16 proto, *protop;
4440a85df00SHao Zheng 		protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
4450a85df00SHao Zheng 						h_vlan_encapsulated_proto),
4460a85df00SHao Zheng 						sizeof(proto), &proto);
4470a85df00SHao Zheng 		if (likely(protop))
4480a85df00SHao Zheng 			protocol = *protop;
4490a85df00SHao Zheng 	}
4500a85df00SHao Zheng 
4510a85df00SHao Zheng 	return protocol;
4520a85df00SHao Zheng }
453396cf943SPravin B Shelar 
454396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
455396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
456396cf943SPravin B Shelar {
457396cf943SPravin B Shelar 	__be16 proto;
458da8c8724SCong Wang 	unsigned short *rawp;
459396cf943SPravin B Shelar 
460396cf943SPravin B Shelar 	/*
461396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
462396cf943SPravin B Shelar 	 * three protocols care about.
463396cf943SPravin B Shelar 	 */
464396cf943SPravin B Shelar 
465396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
466e5c5d22eSSimon Horman 	if (ntohs(proto) >= ETH_P_802_3_MIN) {
467396cf943SPravin B Shelar 		skb->protocol = proto;
468396cf943SPravin B Shelar 		return;
469396cf943SPravin B Shelar 	}
470396cf943SPravin B Shelar 
471da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
472da8c8724SCong Wang 	if (*rawp == 0xFFFF)
473396cf943SPravin B Shelar 		/*
474396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
475396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
476396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
477396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
478396cf943SPravin B Shelar 		 * but does for the rest.
479396cf943SPravin B Shelar 		 */
480396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
481396cf943SPravin B Shelar 	else
482396cf943SPravin B Shelar 		/*
483396cf943SPravin B Shelar 		 * Real 802.2 LLC
484396cf943SPravin B Shelar 		 */
485396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
486396cf943SPravin B Shelar }
487*44a40855SVlad Yasevich 
488*44a40855SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
489*44a40855SVlad Yasevich {
490*44a40855SVlad Yasevich 	BUG_ON(!is_vlan_dev(dev));
491*44a40855SVlad Yasevich 	return vlan_dev_priv(dev)->nest_level;
492*44a40855SVlad Yasevich }
4931da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
494