xref: /linux/include/linux/if_vlan.h (revision e1618d461ca18d40f9c3ef70598abb72e75d27ae)
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);
201*e1618d46SVlad Yasevich 
202*e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
203*e1618d46SVlad Yasevich {
204*e1618d46SVlad Yasevich 	BUG_ON(!is_vlan_dev(dev));
205*e1618d46SVlad Yasevich 	return vlan_dev_priv(dev)->nest_level;
206*e1618d46SVlad Yasevich }
2077750f403SPatrick McHardy #else
208cec9c133SJiri Pirko static inline struct net_device *
2099fae27b3SPatrick McHardy __vlan_find_dev_deep(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 
2446139e75fSDavid S. Miller static inline struct sk_buff *vlan_untag(struct sk_buff *skb)
245bcc6d479SJiri Pirko {
246bcc6d479SJiri Pirko 	return skb;
247bcc6d479SJiri Pirko }
24887002b03SJiri Pirko 
2499fae27b3SPatrick McHardy static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
25087002b03SJiri Pirko {
25187002b03SJiri Pirko 	return 0;
25287002b03SJiri Pirko }
25387002b03SJiri Pirko 
2549fae27b3SPatrick McHardy static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
25587002b03SJiri Pirko {
25687002b03SJiri Pirko }
257348a1443SJiri Pirko 
258348a1443SJiri Pirko static inline int vlan_vids_add_by_dev(struct net_device *dev,
259348a1443SJiri Pirko 				       const struct net_device *by_dev)
260348a1443SJiri Pirko {
261348a1443SJiri Pirko 	return 0;
262348a1443SJiri Pirko }
263348a1443SJiri Pirko 
264348a1443SJiri Pirko static inline void vlan_vids_del_by_dev(struct net_device *dev,
265348a1443SJiri Pirko 					const struct net_device *by_dev)
266348a1443SJiri Pirko {
267348a1443SJiri Pirko }
2689b361c13SJiri Pirko 
2699b361c13SJiri Pirko static inline bool vlan_uses_dev(const struct net_device *dev)
2709b361c13SJiri Pirko {
2719b361c13SJiri Pirko 	return false;
2729b361c13SJiri Pirko }
273*e1618d46SVlad Yasevich static inline int vlan_get_encap_level(struct net_device *dev)
274*e1618d46SVlad Yasevich {
275*e1618d46SVlad Yasevich 	BUG();
276*e1618d46SVlad Yasevich 	return 0;
277*e1618d46SVlad Yasevich }
2787750f403SPatrick McHardy #endif
2791da177e4SLinus Torvalds 
28086a9bad3SPatrick McHardy static inline bool vlan_hw_offload_capable(netdev_features_t features,
28186a9bad3SPatrick McHardy 					   __be16 proto)
28286a9bad3SPatrick McHardy {
28386a9bad3SPatrick McHardy 	if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
28486a9bad3SPatrick McHardy 		return true;
2858ad227ffSPatrick McHardy 	if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
2868ad227ffSPatrick McHardy 		return true;
28786a9bad3SPatrick McHardy 	return false;
28886a9bad3SPatrick McHardy }
28986a9bad3SPatrick McHardy 
2909bb8582eSPatrick McHardy /**
2910b5c9db1SJiri Pirko  * vlan_insert_tag - regular VLAN tag inserting
2921da177e4SLinus Torvalds  * @skb: skbuff to tag
29386a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
2949bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  * Inserts the VLAN tag into @skb as part of the payload
2971da177e4SLinus Torvalds  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  * Following the skb_unshare() example, in case of error, the calling function
3001da177e4SLinus Torvalds  * doesn't have to worry about freeing the original skb.
3010b5c9db1SJiri Pirko  *
3020b5c9db1SJiri Pirko  * Does not change skb->protocol so this function can be used during receive.
3031da177e4SLinus Torvalds  */
30486a9bad3SPatrick McHardy static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
30586a9bad3SPatrick McHardy 					      __be16 vlan_proto, u16 vlan_tci)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	struct vlan_ethhdr *veth;
3081da177e4SLinus Torvalds 
30911a100f8SPatrick McHardy 	if (skb_cow_head(skb, VLAN_HLEN) < 0) {
310fb7c03dfSEric W. Biederman 		dev_kfree_skb_any(skb);
3111da177e4SLinus Torvalds 		return NULL;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Move the mac addresses to the beginning of the new header. */
316a3f671b3SJoe Perches 	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
317e4dd6188SJarek Poplawski 	skb->mac_header -= VLAN_HLEN;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* first, the ethernet type */
32086a9bad3SPatrick McHardy 	veth->h_vlan_proto = vlan_proto;
3211da177e4SLinus Torvalds 
3229bb8582eSPatrick McHardy 	/* now, the TCI */
3239bb8582eSPatrick McHardy 	veth->h_vlan_TCI = htons(vlan_tci);
3241da177e4SLinus Torvalds 
3250b5c9db1SJiri Pirko 	return skb;
3260b5c9db1SJiri Pirko }
3271da177e4SLinus Torvalds 
3280b5c9db1SJiri Pirko /**
3290b5c9db1SJiri Pirko  * __vlan_put_tag - regular VLAN tag inserting
3300b5c9db1SJiri Pirko  * @skb: skbuff to tag
3310b5c9db1SJiri Pirko  * @vlan_tci: VLAN TCI to insert
3320b5c9db1SJiri Pirko  *
3330b5c9db1SJiri Pirko  * Inserts the VLAN tag into @skb as part of the payload
3340b5c9db1SJiri Pirko  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
3350b5c9db1SJiri Pirko  *
3360b5c9db1SJiri Pirko  * Following the skb_unshare() example, in case of error, the calling function
3370b5c9db1SJiri Pirko  * doesn't have to worry about freeing the original skb.
3380b5c9db1SJiri Pirko  */
33986a9bad3SPatrick McHardy static inline struct sk_buff *__vlan_put_tag(struct sk_buff *skb,
34086a9bad3SPatrick McHardy 					     __be16 vlan_proto, u16 vlan_tci)
3410b5c9db1SJiri Pirko {
34286a9bad3SPatrick McHardy 	skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
3430b5c9db1SJiri Pirko 	if (skb)
34486a9bad3SPatrick McHardy 		skb->protocol = vlan_proto;
3451da177e4SLinus Torvalds 	return skb;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /**
3491da177e4SLinus Torvalds  * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
3501da177e4SLinus Torvalds  * @skb: skbuff to tag
35186a9bad3SPatrick McHardy  * @vlan_proto: VLAN encapsulation protocol
3529bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3531da177e4SLinus Torvalds  *
3546aa895b0SPatrick McHardy  * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
3551da177e4SLinus Torvalds  */
3569bb8582eSPatrick McHardy static inline struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb,
35786a9bad3SPatrick McHardy 						     __be16 vlan_proto,
3589bb8582eSPatrick McHardy 						     u16 vlan_tci)
3591da177e4SLinus Torvalds {
36086a9bad3SPatrick McHardy 	skb->vlan_proto = vlan_proto;
36105423b24SEric Dumazet 	skb->vlan_tci = VLAN_TAG_PRESENT | vlan_tci;
3621da177e4SLinus Torvalds 	return skb;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /**
3661da177e4SLinus Torvalds  * vlan_put_tag - inserts VLAN tag according to device features
3671da177e4SLinus Torvalds  * @skb: skbuff to tag
3689bb8582eSPatrick McHardy  * @vlan_tci: VLAN TCI to insert
3691da177e4SLinus Torvalds  *
3701da177e4SLinus Torvalds  * Assumes skb->dev is the target that will xmit this frame.
3711da177e4SLinus Torvalds  * Returns a VLAN tagged skb.
3721da177e4SLinus Torvalds  */
37386a9bad3SPatrick McHardy static inline struct sk_buff *vlan_put_tag(struct sk_buff *skb,
37486a9bad3SPatrick McHardy 					   __be16 vlan_proto, u16 vlan_tci)
3751da177e4SLinus Torvalds {
37686a9bad3SPatrick McHardy 	if (vlan_hw_offload_capable(skb->dev->features, vlan_proto)) {
37786a9bad3SPatrick McHardy 		return __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
3781da177e4SLinus Torvalds 	} else {
37986a9bad3SPatrick McHardy 		return __vlan_put_tag(skb, vlan_proto, vlan_tci);
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /**
3841da177e4SLinus Torvalds  * __vlan_get_tag - get the VLAN ID that is part of the payload
3851da177e4SLinus Torvalds  * @skb: skbuff to query
3869bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
3871da177e4SLinus Torvalds  *
3881da177e4SLinus Torvalds  * Returns error if the skb is not of VLAN type
3891da177e4SLinus Torvalds  */
3909bb8582eSPatrick McHardy static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
3931da177e4SLinus Torvalds 
3948ad227ffSPatrick McHardy 	if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
3958ad227ffSPatrick McHardy 	    veth->h_vlan_proto != htons(ETH_P_8021AD))
3961da177e4SLinus Torvalds 		return -EINVAL;
3971da177e4SLinus Torvalds 
3989bb8582eSPatrick McHardy 	*vlan_tci = ntohs(veth->h_vlan_TCI);
3991da177e4SLinus Torvalds 	return 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /**
4031da177e4SLinus Torvalds  * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
4041da177e4SLinus Torvalds  * @skb: skbuff to query
4059bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
4061da177e4SLinus Torvalds  *
4076aa895b0SPatrick McHardy  * Returns error if @skb->vlan_tci is not set correctly
4081da177e4SLinus Torvalds  */
40918149935SPatrick McHardy static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
4109bb8582eSPatrick McHardy 					 u16 *vlan_tci)
4111da177e4SLinus Torvalds {
4126aa895b0SPatrick McHardy 	if (vlan_tx_tag_present(skb)) {
41305423b24SEric Dumazet 		*vlan_tci = vlan_tx_tag_get(skb);
4141da177e4SLinus Torvalds 		return 0;
4151da177e4SLinus Torvalds 	} else {
4169bb8582eSPatrick McHardy 		*vlan_tci = 0;
4171da177e4SLinus Torvalds 		return -EINVAL;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds #define HAVE_VLAN_GET_TAG
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /**
4241da177e4SLinus Torvalds  * vlan_get_tag - get the VLAN ID from the skb
4251da177e4SLinus Torvalds  * @skb: skbuff to query
4269bb8582eSPatrick McHardy  * @vlan_tci: buffer to store vlaue
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  * Returns error if the skb is not VLAN tagged
4291da177e4SLinus Torvalds  */
4309bb8582eSPatrick McHardy static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
4311da177e4SLinus Torvalds {
432f646968fSPatrick McHardy 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
4339bb8582eSPatrick McHardy 		return __vlan_hwaccel_get_tag(skb, vlan_tci);
4341da177e4SLinus Torvalds 	} else {
4359bb8582eSPatrick McHardy 		return __vlan_get_tag(skb, vlan_tci);
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4390a85df00SHao Zheng /**
4400a85df00SHao Zheng  * vlan_get_protocol - get protocol EtherType.
4410a85df00SHao Zheng  * @skb: skbuff to query
4420a85df00SHao Zheng  *
4430a85df00SHao Zheng  * Returns the EtherType of the packet, regardless of whether it is
4440a85df00SHao Zheng  * vlan encapsulated (normal or hardware accelerated) or not.
4450a85df00SHao Zheng  */
4460a85df00SHao Zheng static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
4470a85df00SHao Zheng {
4480a85df00SHao Zheng 	__be16 protocol = 0;
4490a85df00SHao Zheng 
4500a85df00SHao Zheng 	if (vlan_tx_tag_present(skb) ||
4510a85df00SHao Zheng 	     skb->protocol != cpu_to_be16(ETH_P_8021Q))
4520a85df00SHao Zheng 		protocol = skb->protocol;
4530a85df00SHao Zheng 	else {
4540a85df00SHao Zheng 		__be16 proto, *protop;
4550a85df00SHao Zheng 		protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr,
4560a85df00SHao Zheng 						h_vlan_encapsulated_proto),
4570a85df00SHao Zheng 						sizeof(proto), &proto);
4580a85df00SHao Zheng 		if (likely(protop))
4590a85df00SHao Zheng 			protocol = *protop;
4600a85df00SHao Zheng 	}
4610a85df00SHao Zheng 
4620a85df00SHao Zheng 	return protocol;
4630a85df00SHao Zheng }
464396cf943SPravin B Shelar 
465396cf943SPravin B Shelar static inline void vlan_set_encap_proto(struct sk_buff *skb,
466396cf943SPravin B Shelar 					struct vlan_hdr *vhdr)
467396cf943SPravin B Shelar {
468396cf943SPravin B Shelar 	__be16 proto;
469da8c8724SCong Wang 	unsigned short *rawp;
470396cf943SPravin B Shelar 
471396cf943SPravin B Shelar 	/*
472396cf943SPravin B Shelar 	 * Was a VLAN packet, grab the encapsulated protocol, which the layer
473396cf943SPravin B Shelar 	 * three protocols care about.
474396cf943SPravin B Shelar 	 */
475396cf943SPravin B Shelar 
476396cf943SPravin B Shelar 	proto = vhdr->h_vlan_encapsulated_proto;
477e5c5d22eSSimon Horman 	if (ntohs(proto) >= ETH_P_802_3_MIN) {
478396cf943SPravin B Shelar 		skb->protocol = proto;
479396cf943SPravin B Shelar 		return;
480396cf943SPravin B Shelar 	}
481396cf943SPravin B Shelar 
482da8c8724SCong Wang 	rawp = (unsigned short *)(vhdr + 1);
483da8c8724SCong Wang 	if (*rawp == 0xFFFF)
484396cf943SPravin B Shelar 		/*
485396cf943SPravin B Shelar 		 * This is a magic hack to spot IPX packets. Older Novell
486396cf943SPravin B Shelar 		 * breaks the protocol design and runs IPX over 802.3 without
487396cf943SPravin B Shelar 		 * an 802.2 LLC layer. We look for FFFF which isn't a used
488396cf943SPravin B Shelar 		 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
489396cf943SPravin B Shelar 		 * but does for the rest.
490396cf943SPravin B Shelar 		 */
491396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_3);
492396cf943SPravin B Shelar 	else
493396cf943SPravin B Shelar 		/*
494396cf943SPravin B Shelar 		 * Real 802.2 LLC
495396cf943SPravin B Shelar 		 */
496396cf943SPravin B Shelar 		skb->protocol = htons(ETH_P_802_2);
497396cf943SPravin B Shelar }
49844a40855SVlad Yasevich 
4991da177e4SLinus Torvalds #endif /* !(_LINUX_IF_VLAN_H_) */
500