xref: /freebsd/sys/net/if_vlan_var.h (revision 7431dfd4580e850375fe5478d92ec770344db098)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _NET_IF_VLAN_VAR_H_
33 #define	_NET_IF_VLAN_VAR_H_	1
34 
35 struct	ether_vlan_header {
36 	u_char	evl_dhost[ETHER_ADDR_LEN];
37 	u_char	evl_shost[ETHER_ADDR_LEN];
38 	u_int16_t evl_encap_proto;
39 	u_int16_t evl_tag;
40 	u_int16_t evl_proto;
41 };
42 
43 #define	EVL_VLID_MASK		0x0FFF
44 #define	EVL_PRI_MASK		0xE000
45 #define	EVL_VLANOFTAG(tag)	((tag) & EVL_VLID_MASK)
46 #define	EVL_PRIOFTAG(tag)	(((tag) >> 13) & 7)
47 #define	EVL_CFIOFTAG(tag)	(((tag) >> 12) & 1)
48 #define	EVL_MAKETAG(vlid, pri, cfi)					\
49 	((((((pri) & 7) << 1) | ((cfi) & 1)) << 12) | ((vlid) & EVL_VLID_MASK))
50 
51 /* Set the VLAN ID in an mbuf packet header non-destructively. */
52 #define EVL_APPLY_VLID(m, vlid)						\
53 	do {								\
54 		if ((m)->m_flags & M_VLANTAG) {				\
55 			(m)->m_pkthdr.ether_vtag &= EVL_VLID_MASK;	\
56 			(m)->m_pkthdr.ether_vtag |= (vlid);		\
57 		} else {						\
58 			(m)->m_pkthdr.ether_vtag = (vlid);		\
59 			(m)->m_flags |= M_VLANTAG;			\
60 		}							\
61 	} while (0)
62 
63 /* Set the priority ID in an mbuf packet header non-destructively. */
64 #define EVL_APPLY_PRI(m, pri)						\
65 	do {								\
66 		if ((m)->m_flags & M_VLANTAG) {				\
67 			uint16_t __vlantag = (m)->m_pkthdr.ether_vtag;	\
68 			(m)->m_pkthdr.ether_vtag |= EVL_MAKETAG(	\
69 			    EVL_VLANOFTAG(__vlantag), (pri),		\
70 			    EVL_CFIOFTAG(__vlantag));			\
71 		} else {						\
72 			(m)->m_pkthdr.ether_vtag =			\
73 			    EVL_MAKETAG(0, (pri), 0);			\
74 			(m)->m_flags |= M_VLANTAG;			\
75 		}							\
76 	} while (0)
77 
78 /* sysctl(3) tags, for compatibility purposes */
79 #define	VLANCTL_PROTO	1
80 #define	VLANCTL_MAX	2
81 
82 /*
83  * Configuration structure for SIOCSETVLAN and SIOCGETVLAN ioctls.
84  */
85 struct	vlanreq {
86 	char	vlr_parent[IFNAMSIZ];
87 	u_short	vlr_tag;
88 };
89 #define	SIOCSETVLAN	SIOCSIFGENERIC
90 #define	SIOCGETVLAN	SIOCGIFGENERIC
91 
92 #ifdef _KERNEL
93 /*
94  * Drivers that are capable of adding and removing the VLAN header
95  * in hardware indicate they support this by marking IFCAP_VLAN_HWTAGGING
96  * in if_capabilities.  Drivers for hardware that is capable
97  * of handling larger MTU's that may include a software-appended
98  * VLAN header w/o lowering the normal MTU should mark IFCAP_VLAN_MTU
99  * in if_capabilities; this notifies the VLAN code it can leave the
100  * MTU on the vlan interface at the normal setting.
101  */
102 
103 /*
104  * VLAN tags are stored in host byte order.  Byte swapping may be
105  * necessary.
106  *
107  * Drivers that support hardware VLAN tag stripping fill in the
108  * received VLAN tag (containing both vlan and priority information)
109  * into the ether_vtag mbuf packet header field:
110  *
111  *	m->m_pkthdr.ether_vtag = vtag;		// ntohs()?
112  *	m->m_flags |= M_VLANTAG;
113  *
114  * to mark the packet m with the specified VLAN tag.
115  *
116  * On output the driver should check the mbuf for the M_VLANTAG
117  * flag to see if a VLAN tag is present and valid:
118  *
119  *	if (m->m_flags & M_VLANTAG) {
120  *		... = m->m_pkthdr.ether_vtag;	// htons()?
121  *		... pass tag to hardware ...
122  *	}
123  *
124  * Note that a driver must indicate it supports hardware VLAN
125  * stripping/insertion by marking IFCAP_VLAN_HWTAGGING in
126  * if_capabilities.
127  */
128 
129 #define	VLAN_CAPABILITIES(_ifp) do {				\
130 	if ((_ifp)->if_vlantrunk != NULL) 			\
131 		(*vlan_trunk_cap_p)(_ifp);			\
132 } while (0)
133 
134 #define	VLAN_TRUNKDEV(_ifp)					\
135 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_trunkdev_p)((_ifp)) : NULL
136 #define	VLAN_TAG(_ifp, _vid)					\
137 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_tag_p)((_ifp), (_vid)) : EINVAL
138 #define	VLAN_COOKIE(_ifp)					\
139 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_cookie_p)((_ifp)) : NULL
140 #define	VLAN_SETCOOKIE(_ifp, _cookie)				\
141 	(_ifp)->if_type == IFT_L2VLAN ?				\
142 	    (*vlan_setcookie_p)((_ifp), (_cookie)) : EINVAL
143 #define	VLAN_DEVAT(_ifp, _vid)					\
144 	(_ifp)->if_vlantrunk != NULL ? (*vlan_devat_p)((_ifp), (_vid)) : NULL
145 
146 extern	void (*vlan_trunk_cap_p)(struct ifnet *);
147 extern	struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
148 extern	struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
149 extern	int (*vlan_tag_p)(struct ifnet *, uint16_t *);
150 extern	int (*vlan_setcookie_p)(struct ifnet *, void *);
151 extern	void *(*vlan_cookie_p)(struct ifnet *);
152 
153 #ifdef _SYS_EVENTHANDLER_H_
154 /* VLAN state change events */
155 typedef void (*vlan_config_fn)(void *, struct ifnet *, uint16_t);
156 typedef void (*vlan_unconfig_fn)(void *, struct ifnet *, uint16_t);
157 EVENTHANDLER_DECLARE(vlan_config, vlan_config_fn);
158 EVENTHANDLER_DECLARE(vlan_unconfig, vlan_unconfig_fn);
159 #endif /* _SYS_EVENTHANDLER_H_ */
160 
161 #endif /* _KERNEL */
162 
163 #endif /* _NET_IF_VLAN_VAR_H_ */
164