xref: /linux/include/linux/if_vlan.h (revision 1e85c9b66d86a776e78c332d5d32ae370ab84d3f)
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 
766dcbbe25SNeil Horman static inline int 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 
107*1e85c9b6SHannes Frederic Sowa #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
108*1e85c9b6SHannes Frederic Sowa 
109*1e85c9b6SHannes Frederic Sowa extern struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
110*1e85c9b6SHannes Frederic Sowa 					       __be16 vlan_proto, u16 vlan_id);
111*1e85c9b6SHannes Frederic Sowa extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
112*1e85c9b6SHannes Frederic Sowa extern u16 vlan_dev_vlan_id(const struct net_device *dev);
113*1e85c9b6SHannes Frederic Sowa 
114*1e85c9b6SHannes Frederic Sowa /**
115*1e85c9b6SHannes Frederic Sowa  *	struct vlan_priority_tci_mapping - vlan egress priority mappings
116*1e85c9b6SHannes Frederic Sowa  *	@priority: skb priority
117*1e85c9b6SHannes Frederic Sowa  *	@vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
118*1e85c9b6SHannes Frederic Sowa  *	@next: pointer to next struct
119*1e85c9b6SHannes Frederic Sowa  */
120*1e85c9b6SHannes Frederic Sowa struct vlan_priority_tci_mapping {
121*1e85c9b6SHannes Frederic Sowa 	u32					priority;
122*1e85c9b6SHannes Frederic Sowa 	u16					vlan_qos;
123*1e85c9b6SHannes Frederic Sowa 	struct vlan_priority_tci_mapping	*next;
124*1e85c9b6SHannes Frederic Sowa };
125*1e85c9b6SHannes Frederic Sowa 
126e267cb96SDavid S. Miller struct proc_dir_entry;
127e267cb96SDavid S. Miller struct netpoll;
128e267cb96SDavid S. Miller 
129e267cb96SDavid S. Miller /**
130e267cb96SDavid S. Miller  *	struct vlan_dev_priv - VLAN private device data
131e267cb96SDavid S. Miller  *	@nr_ingress_mappings: number of ingress priority mappings
132e267cb96SDavid S. Miller  *	@ingress_priority_map: ingress priority mappings
133e267cb96SDavid S. Miller  *	@nr_egress_mappings: number of egress priority mappings
134e267cb96SDavid S. Miller  *	@egress_priority_map: hash of egress priority mappings
135e267cb96SDavid S. Miller  *	@vlan_proto: VLAN encapsulation protocol
136e267cb96SDavid S. Miller  *	@vlan_id: VLAN identifier
137e267cb96SDavid S. Miller  *	@flags: device flags
138e267cb96SDavid S. Miller  *	@real_dev: underlying netdevice
139e267cb96SDavid S. Miller  *	@real_dev_addr: address of underlying netdevice
140e267cb96SDavid S. Miller  *	@dent: proc dir entry
141e267cb96SDavid S. Miller  *	@vlan_pcpu_stats: ptr to percpu rx stats
142e267cb96SDavid S. Miller  */
143e267cb96SDavid S. Miller struct vlan_dev_priv {
144e267cb96SDavid S. Miller 	unsigned int				nr_ingress_mappings;
145e267cb96SDavid S. Miller 	u32					ingress_priority_map[8];
146e267cb96SDavid S. Miller 	unsigned int				nr_egress_mappings;
147e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping	*egress_priority_map[16];
148e267cb96SDavid S. Miller 
149e267cb96SDavid S. Miller 	__be16					vlan_proto;
150e267cb96SDavid S. Miller 	u16					vlan_id;
151e267cb96SDavid S. Miller 	u16					flags;
152e267cb96SDavid S. Miller 
153e267cb96SDavid S. Miller 	struct net_device			*real_dev;
154e267cb96SDavid S. Miller 	unsigned char				real_dev_addr[ETH_ALEN];
155e267cb96SDavid S. Miller 
156e267cb96SDavid S. Miller 	struct proc_dir_entry			*dent;
157e267cb96SDavid S. Miller 	struct vlan_pcpu_stats __percpu		*vlan_pcpu_stats;
158e267cb96SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
159e267cb96SDavid S. Miller 	struct netpoll				*netpoll;
160e267cb96SDavid S. Miller #endif
161e267cb96SDavid S. Miller };
162e267cb96SDavid S. Miller 
163e267cb96SDavid S. Miller static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
164e267cb96SDavid S. Miller {
165e267cb96SDavid S. Miller 	return netdev_priv(dev);
166e267cb96SDavid S. Miller }
167e267cb96SDavid S. Miller 
168e267cb96SDavid S. Miller static inline u16
169e267cb96SDavid S. Miller vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
170e267cb96SDavid S. Miller {
171e267cb96SDavid S. Miller 	struct vlan_priority_tci_mapping *mp;
172e267cb96SDavid S. Miller 
173e267cb96SDavid S. Miller 	smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
174e267cb96SDavid S. Miller 
175e267cb96SDavid S. Miller 	mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
176e267cb96SDavid S. Miller 	while (mp) {
177e267cb96SDavid S. Miller 		if (mp->priority == skprio) {
178e267cb96SDavid S. Miller 			return mp->vlan_qos; /* This should already be shifted
179e267cb96SDavid S. Miller 					      * to mask correctly with the
180e267cb96SDavid S. Miller 					      * VLAN's TCI */
181e267cb96SDavid S. Miller 		}
182e267cb96SDavid S. Miller 		mp = mp->next;
183e267cb96SDavid S. Miller 	}
184e267cb96SDavid S. Miller 	return 0;
185e267cb96SDavid S. Miller }
186e267cb96SDavid S. Miller 
18748cc32d3SFlorian Zumbiehl extern bool vlan_do_receive(struct sk_buff **skb);
188bcc6d479SJiri Pirko extern struct sk_buff *vlan_untag(struct sk_buff *skb);
1899b22ea56SPatrick McHardy 
19080d5c368SPatrick McHardy extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
19180d5c368SPatrick McHardy extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
19287002b03SJiri Pirko 
193348a1443SJiri Pirko extern int vlan_vids_add_by_dev(struct net_device *dev,
194348a1443SJiri Pirko 				const struct net_device *by_dev);
195348a1443SJiri Pirko extern void vlan_vids_del_by_dev(struct net_device *dev,
196348a1443SJiri Pirko 				 const struct net_device *by_dev);
1979b361c13SJiri Pirko 
1989b361c13SJiri Pirko extern bool vlan_uses_dev(const struct net_device *dev);
1997750f403SPatrick McHardy #else
200cec9c133SJiri Pirko static inline struct net_device *
2019fae27b3SPatrick McHardy __vlan_find_dev_deep(struct net_device *real_dev,
2029fae27b3SPatrick McHardy 		     __be16 vlan_proto, u16 vlan_id)
203cec9c133SJiri Pirko {
204cec9c133SJiri Pirko 	return NULL;
205cec9c133SJiri Pirko }
206cec9c133SJiri Pirko 
20722d1ba74SPatrick McHardy static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
20822d1ba74SPatrick McHardy {
20922d1ba74SPatrick McHardy 	BUG();
21022d1ba74SPatrick McHardy 	return NULL;
21122d1ba74SPatrick McHardy }
21222d1ba74SPatrick McHardy 
21322d1ba74SPatrick McHardy static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
21422d1ba74SPatrick McHardy {
21522d1ba74SPatrick McHardy 	BUG();
21622d1ba74SPatrick McHardy 	return 0;
21722d1ba74SPatrick McHardy }
21822d1ba74SPatrick McHardy 
219d3243539SEyal Perry static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
220d3243539SEyal Perry 					       u32 skprio)
221d3243539SEyal Perry {
222d3243539SEyal Perry 	return 0;
223d3243539SEyal Perry }
224d3243539SEyal Perry 
22548cc32d3SFlorian Zumbiehl static inline bool vlan_do_receive(struct sk_buff **skb)
2269b22ea56SPatrick McHardy {
2273701e513SJesse Gross 	return false;
2289b22ea56SPatrick McHardy }
229e1c096e2SHerbert Xu 
2306139e75fSDavid S. Miller static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
231bcc6d479SJiri Pirko {
232bcc6d479SJiri Pirko 	return skb;
233bcc6d479SJiri Pirko }
23487002b03SJiri Pirko 
2359fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
23687002b03SJiri Pirko {
23787002b03SJiri Pirko 	return 0;
23887002b03SJiri Pirko }
23987002b03SJiri Pirko 
2409fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
24187002b03SJiri Pirko {
24287002b03SJiri Pirko }
243348a1443SJiri Pirko 
244348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
245348a1443SJiri Pirko 				       const struct net_device *by_dev)
246348a1443SJiri Pirko {
247348a1443SJiri Pirko 	return 0;
248348a1443SJiri Pirko }
249348a1443SJiri Pirko 
250348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
251348a1443SJiri Pirko 					const struct net_device *by_dev)
252348a1443SJiri Pirko {
253348a1443SJiri Pirko }
2549b361c13SJiri Pirko 
2559b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
2569b361c13SJiri Pirko {
2579b361c13SJiri Pirko 	return false;
2589b361c13SJiri Pirko }
2597750f403SPatrick McHardy #endif
2601da177e4SLinus Torvalds 
26186a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
26286a9bad3SPatrick McHardy 					   __be16 proto)
26386a9bad3SPatrick McHardy {
26486a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
26586a9bad3SPatrick McHardy 		return true;
2668ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
2678ad227ffSPatrick McHardy 		return true;
26886a9bad3SPatrick McHardy 	return false;
26986a9bad3SPatrick McHardy }
27086a9bad3SPatrick McHardy 
2719bb8582eSPatrick McHardy /**
2720b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
2731da177e4SLinus Torvalds  * @skb: skbuff to tag
27486a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
2759bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
2781da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
2791da177e4SLinus Torvalds  *
2801da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
2811da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
2820b5c9db1SJiri Pirko  *
2830b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
2841da177e4SLinus Torvalds  */
28586a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
28686a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct vlan_ethhdr *veth;
2891da177e4SLinus Torvalds 
29011a100f8SPatrick McHardy 	if (skb_cow_head(skb, VLAN_HLEN) < 0) {
29111a100f8SPatrick McHardy 		kfree_skb(skb);
2921da177e4SLinus Torvalds 		return NULL;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* Move the mac addresses to the beginning of the new header. */
297a3f671b3SJoe Perches 	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
298e4dd6188SJarek Poplawski 	skb->mac_header -= VLAN_HLEN;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* first, the ethernet type */
30186a9bad3SPatrick McHardy 	veth->h_vlan_proto = vlan_proto;
3021da177e4SLinus Torvalds 
3039bb8582eSPatrick McHardy 	/* now, the TCI */
3049bb8582eSPatrick McHardy 	veth->h_vlan_TCI = htons(vlan_tci);
3051da177e4SLinus Torvalds 
3060b5c9db1SJiri Pirko 	return skb;
3070b5c9db1SJiri Pirko }
3081da177e4SLinus Torvalds 
3090b5c9db1SJiri Pirko /**
3100b5c9db1SJiri Pirko  * __vlan_put_tag - regular VLAN tag inserting
3110b5c9db1SJiri Pirko  * @skb: skbuff to tag
3120b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
3130b5c9db1SJiri Pirko  *
3140b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3150b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
3160b5c9db1SJiri Pirko  *
3170b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
3180b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
3190b5c9db1SJiri Pirko  */
32086a9bad3SPatrick McHardy static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb,
32186a9bad3SPatrick McHardy 					     __be16 vlan_proto, u16 vlan_tci)
3220b5c9db1SJiri Pirko {
32386a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
3240b5c9db1SJiri Pirko 	if (skb)
32586a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
3261da177e4SLinus Torvalds 	return skb;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /**
3301da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
3311da177e4SLinus Torvalds  * @skb: skbuff to tag
33286a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
3339bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3341da177e4SLinus Torvalds  *
3356aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
3361da177e4SLinus Torvalds  */
3379bb8582eSPatrick McHardy static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
33886a9bad3SPatrick McHardy 						     __be16 vlan_proto,
3399bb8582eSPatrick McHardy 						     u16 vlan_tci)
3401da177e4SLinus Torvalds {
34186a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
34205423b24SEric Dumazet 	skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
3431da177e4SLinus Torvalds 	return skb;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /**
3471da177e4SLinus Torvalds  * vlan_put_tag - inserts VLAN tag according to device features
3481da177e4SLinus Torvalds  * @skb: skbuff to tag
3499bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3501da177e4SLinus Torvalds  *
3511da177e4SLinus Torvalds  * Assumes skb->dev is the target that will xmit this frame.
3521da177e4SLinus Torvalds  * Returns a VLAN tagged skb.
3531da177e4SLinus Torvalds  */
35486a9bad3SPatrick McHardy static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb,
35586a9bad3SPatrick McHardy 					   __be16 vlan_proto, u16 vlan_tci)
3561da177e4SLinus Torvalds {
35786a9bad3SPatrick McHardy 	if (vlan_hw_offload_capable(skb->dev->features, vlan_proto)) {
35886a9bad3SPatrick McHardy 		return __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
3591da177e4SLinus Torvalds 	} else {
36086a9bad3SPatrick McHardy 		return __vlan_put_tag(skb, vlan_proto, vlan_tci);
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /**
3651da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
3661da177e4SLinus Torvalds  * @skb: skbuff to query
3679bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
3681da177e4SLinus Torvalds  *
3691da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
3701da177e4SLinus Torvalds  */
3719bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
3741da177e4SLinus Torvalds 
3758ad227ffSPatrick McHardy 	if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
3768ad227ffSPatrick McHardy 	    veth->h_vlan_proto != htons(ETH_P_8021AD))
3771da177e4SLinus Torvalds 		return -EINVAL;
3781da177e4SLinus Torvalds 
3799bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
3801da177e4SLinus Torvalds 	return 0;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /**
3841da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
3851da177e4SLinus Torvalds  * @skb: skbuff to query
3869bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
3871da177e4SLinus Torvalds  *
3886aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
3891da177e4SLinus Torvalds  */
39018149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
3919bb8582eSPatrick McHardy 					 u16 *vlan_tci)
3921da177e4SLinus Torvalds {
3936aa895b0SPatrick McHardy 	if (vlan_tx_tag_present(skb)) {
39405423b24SEric Dumazet 		*vlan_tci = vlan_tx_tag_get(skb);
3951da177e4SLinus Torvalds 		return 0;
3961da177e4SLinus Torvalds 	} else {
3979bb8582eSPatrick McHardy 		*vlan_tci = 0;
3981da177e4SLinus Torvalds 		return -EINVAL;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /**
4051da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
4061da177e4SLinus Torvalds  * @skb: skbuff to query
4079bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
4081da177e4SLinus Torvalds  *
4091da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
4101da177e4SLinus Torvalds  */
4119bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
4121da177e4SLinus Torvalds {
413f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
4149bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
4151da177e4SLinus Torvalds 	} else {
4169bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4200a85df00SHao Zheng /**
4210a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
4220a85df00SHao Zheng  * @skb: skbuff to query
4230a85df00SHao Zheng  *
4240a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
4250a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
4260a85df00SHao Zheng  */
4270a85df00SHao Zheng static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
4280a85df00SHao Zheng {
4290a85df00SHao Zheng 	__be16 protocol = 0;
4300a85df00SHao Zheng 
4310a85df00SHao Zheng 	if (vlan_tx_tag_present(skb) ||
4320a85df00SHao Zheng 	     skb->protocol != cpu_to_be16(ETH_P_8021Q))
4330a85df00SHao Zheng 		protocol = skb->protocol;
4340a85df00SHao Zheng 	else {
4350a85df00SHao Zheng 		__be16 proto, *protop;
4360a85df00SHao Zheng 		protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
4370a85df00SHao Zheng 						h_vlan_encapsulated_proto),
4380a85df00SHao Zheng 						sizeof(proto), &proto);
4390a85df00SHao Zheng 		if (likely(protop))
4400a85df00SHao Zheng 			protocol = *protop;
4410a85df00SHao Zheng 	}
4420a85df00SHao Zheng 
4430a85df00SHao Zheng 	return protocol;
4440a85df00SHao Zheng }
445396cf943SPravin B Shelar 
446396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
447396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
448396cf943SPravin B Shelar {
449396cf943SPravin B Shelar 	__be16 proto;
450da8c8724SCong Wang 	unsigned short *rawp;
451396cf943SPravin B Shelar 
452396cf943SPravin B Shelar 	/*
453396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
454396cf943SPravin B Shelar 	 * three protocols care about.
455396cf943SPravin B Shelar 	 */
456396cf943SPravin B Shelar 
457396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
458e5c5d22eSSimon Horman 	if (ntohs(proto) >= ETH_P_802_3_MIN) {
459396cf943SPravin B Shelar 		skb->protocol = proto;
460396cf943SPravin B Shelar 		return;
461396cf943SPravin B Shelar 	}
462396cf943SPravin B Shelar 
463da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
464da8c8724SCong Wang 	if (*rawp == 0xFFFF)
465396cf943SPravin B Shelar 		/*
466396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
467396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
468396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
469396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
470396cf943SPravin B Shelar 		 * but does for the rest.
471396cf943SPravin B Shelar 		 */
472396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
473396cf943SPravin B Shelar 	else
474396cf943SPravin B Shelar 		/*
475396cf943SPravin B Shelar 		 * Real 802.2 LLC
476396cf943SPravin B Shelar 		 */
477396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
478396cf943SPravin B Shelar }
4791da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
480