xref: /freebsd/sys/net/if_vlan_var.h (revision 113af4fd2bcb464307daf8ca053dc728db8da61d)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32cc2df49SGarrett Wollman  *
42cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
52cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
62cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
72cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
82cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
92cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
102cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
112cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
122cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
132cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
142cc2df49SGarrett Wollman  * warranty.
152cc2df49SGarrett Wollman  *
162cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
172cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
182cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
202cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
232cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
242cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272cc2df49SGarrett Wollman  * SUCH DAMAGE.
282cc2df49SGarrett Wollman  *
29c3aac50fSPeter Wemm  * $FreeBSD$
302cc2df49SGarrett Wollman  */
312cc2df49SGarrett Wollman 
322cc2df49SGarrett Wollman #ifndef _NET_IF_VLAN_VAR_H_
332cc2df49SGarrett Wollman #define	_NET_IF_VLAN_VAR_H_	1
342cc2df49SGarrett Wollman 
359ef8cd0bSKristof Provost #include <sys/mbuf.h>
369ef8cd0bSKristof Provost 
3712c34560SBruce M Simpson /* Set the VLAN ID in an mbuf packet header non-destructively. */
3812c34560SBruce M Simpson #define EVL_APPLY_VLID(m, vlid)						\
3912c34560SBruce M Simpson 	do {								\
4012c34560SBruce M Simpson 		if ((m)->m_flags & M_VLANTAG) {				\
4112c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag &= EVL_VLID_MASK;	\
4212c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag |= (vlid);		\
4312c34560SBruce M Simpson 		} else {						\
4412c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag = (vlid);		\
4512c34560SBruce M Simpson 			(m)->m_flags |= M_VLANTAG;			\
4612c34560SBruce M Simpson 		}							\
4712c34560SBruce M Simpson 	} while (0)
4812c34560SBruce M Simpson 
4912c34560SBruce M Simpson /* Set the priority ID in an mbuf packet header non-destructively. */
5012c34560SBruce M Simpson #define EVL_APPLY_PRI(m, pri)						\
5112c34560SBruce M Simpson 	do {								\
5212c34560SBruce M Simpson 		if ((m)->m_flags & M_VLANTAG) {				\
5312c34560SBruce M Simpson 			uint16_t __vlantag = (m)->m_pkthdr.ether_vtag;	\
5412c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag |= EVL_MAKETAG(	\
5512c34560SBruce M Simpson 			    EVL_VLANOFTAG(__vlantag), (pri),		\
5612c34560SBruce M Simpson 			    EVL_CFIOFTAG(__vlantag));			\
5712c34560SBruce M Simpson 		} else {						\
5812c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag =			\
5912c34560SBruce M Simpson 			    EVL_MAKETAG(0, (pri), 0);			\
6012c34560SBruce M Simpson 			(m)->m_flags |= M_VLANTAG;			\
6112c34560SBruce M Simpson 		}							\
6212c34560SBruce M Simpson 	} while (0)
632cc2df49SGarrett Wollman 
642cc2df49SGarrett Wollman /* sysctl(3) tags, for compatibility purposes */
652cc2df49SGarrett Wollman #define	VLANCTL_PROTO	1
662cc2df49SGarrett Wollman #define	VLANCTL_MAX	2
672cc2df49SGarrett Wollman 
682cc2df49SGarrett Wollman /*
692cc2df49SGarrett Wollman  * Configuration structure for SIOCSETVLAN and SIOCGETVLAN ioctls.
702cc2df49SGarrett Wollman  */
712cc2df49SGarrett Wollman struct	vlanreq {
722cc2df49SGarrett Wollman 	char	vlr_parent[IFNAMSIZ];
732cc2df49SGarrett Wollman 	u_short	vlr_tag;
74c7cffd65SAlexander V. Chernikov 	u_short	vlr_proto;
752cc2df49SGarrett Wollman };
762cc2df49SGarrett Wollman #define	SIOCSETVLAN	SIOCSIFGENERIC
772cc2df49SGarrett Wollman #define	SIOCGETVLAN	SIOCGIFGENERIC
782cc2df49SGarrett Wollman 
79f1379734SKonstantin Belousov #define	SIOCGVLANPCP	SIOCGLANPCP	/* Get VLAN PCP */
80f1379734SKonstantin Belousov #define	SIOCSVLANPCP	SIOCSLANPCP	/* Set VLAN PCP */
812ccbbd06SMarcelo Araujo 
82a3814acfSSam Leffler #ifdef _KERNEL
83a3814acfSSam Leffler /*
84a3814acfSSam Leffler  * Drivers that are capable of adding and removing the VLAN header
85a3814acfSSam Leffler  * in hardware indicate they support this by marking IFCAP_VLAN_HWTAGGING
86a4e16dddSYaroslav Tykhiy  * in if_capabilities.  Drivers for hardware that is capable
87a3814acfSSam Leffler  * of handling larger MTU's that may include a software-appended
88a4e16dddSYaroslav Tykhiy  * VLAN header w/o lowering the normal MTU should mark IFCAP_VLAN_MTU
89a4e16dddSYaroslav Tykhiy  * in if_capabilities; this notifies the VLAN code it can leave the
90a3814acfSSam Leffler  * MTU on the vlan interface at the normal setting.
91a3814acfSSam Leffler  */
92a3814acfSSam Leffler 
93a3814acfSSam Leffler /*
9478ba57b9SAndre Oppermann  * VLAN tags are stored in host byte order.  Byte swapping may be
9578ba57b9SAndre Oppermann  * necessary.
96a3814acfSSam Leffler  *
9778ba57b9SAndre Oppermann  * Drivers that support hardware VLAN tag stripping fill in the
9878ba57b9SAndre Oppermann  * received VLAN tag (containing both vlan and priority information)
9978ba57b9SAndre Oppermann  * into the ether_vtag mbuf packet header field:
100a3814acfSSam Leffler  *
1017983103aSRobert Watson  *	m->m_pkthdr.ether_vtag = vtag;		// ntohs()?
10278ba57b9SAndre Oppermann  *	m->m_flags |= M_VLANTAG;
103a3814acfSSam Leffler  *
10478ba57b9SAndre Oppermann  * to mark the packet m with the specified VLAN tag.
10578ba57b9SAndre Oppermann  *
10678ba57b9SAndre Oppermann  * On output the driver should check the mbuf for the M_VLANTAG
10778ba57b9SAndre Oppermann  * flag to see if a VLAN tag is present and valid:
10878ba57b9SAndre Oppermann  *
10978ba57b9SAndre Oppermann  *	if (m->m_flags & M_VLANTAG) {
11078ba57b9SAndre Oppermann  *		... = m->m_pkthdr.ether_vtag;	// htons()?
111a3814acfSSam Leffler  *		... pass tag to hardware ...
112a3814acfSSam Leffler  *	}
113a3814acfSSam Leffler  *
114a3814acfSSam Leffler  * Note that a driver must indicate it supports hardware VLAN
11578ba57b9SAndre Oppermann  * stripping/insertion by marking IFCAP_VLAN_HWTAGGING in
11678ba57b9SAndre Oppermann  * if_capabilities.
117a3814acfSSam Leffler  */
118a3814acfSSam Leffler 
1192ccbbd06SMarcelo Araujo /*
1202ccbbd06SMarcelo Araujo  * The 802.1q code may also tag mbufs with the PCP (priority) field for use in
1212ccbbd06SMarcelo Araujo  * other layers of the stack, in which case an m_tag will be used.  This is
1222ccbbd06SMarcelo Araujo  * semantically quite different from use of the ether_vtag field, which is
1232ccbbd06SMarcelo Araujo  * defined only between the device driver and VLAN layer.
1242ccbbd06SMarcelo Araujo  */
1252ccbbd06SMarcelo Araujo #define	MTAG_8021Q		1326104895
1262ccbbd06SMarcelo Araujo #define	MTAG_8021Q_PCP_IN	0		/* Input priority. */
1272ccbbd06SMarcelo Araujo #define	MTAG_8021Q_PCP_OUT	1		/* Output priority. */
1282ccbbd06SMarcelo Araujo 
1299ef8cd0bSKristof Provost #define	VLAN_PCP_MAX		7
1309ef8cd0bSKristof Provost 
131c7cffd65SAlexander V. Chernikov /*
132c7cffd65SAlexander V. Chernikov  * 802.1q full tag. Proto and vid are stored in host byte order.
133c7cffd65SAlexander V. Chernikov  */
134c7cffd65SAlexander V. Chernikov struct ether_8021q_tag {
135c7cffd65SAlexander V. Chernikov 	uint16_t proto;
136c7cffd65SAlexander V. Chernikov 	uint16_t vid;
137c7cffd65SAlexander V. Chernikov 	uint8_t  pcp;
138c7cffd65SAlexander V. Chernikov };
139c7cffd65SAlexander V. Chernikov 
14075ee267cSGleb Smirnoff #define	VLAN_CAPABILITIES(_ifp) do {				\
14175ee267cSGleb Smirnoff 	if ((_ifp)->if_vlantrunk != NULL) 			\
14275ee267cSGleb Smirnoff 		(*vlan_trunk_cap_p)(_ifp);			\
14375ee267cSGleb Smirnoff } while (0)
14475ee267cSGleb Smirnoff 
145e4cd31ddSJeff Roberson #define	VLAN_TRUNKDEV(_ifp)					\
146*113af4fdSJustin Hibbits 	(if_gettype(_ifp) == IFT_L2VLAN ? (*vlan_trunkdev_p)((_ifp)) : NULL)
1477983103aSRobert Watson #define	VLAN_TAG(_ifp, _vid)					\
148*113af4fdSJustin Hibbits 	(if_gettype(_ifp) == IFT_L2VLAN ? (*vlan_tag_p)((_ifp), (_vid)) : EINVAL)
14932d2623aSNavdeep Parhar #define	VLAN_PCP(_ifp, _pcp)					\
150*113af4fdSJustin Hibbits 	(if_gettype(_ifp) == IFT_L2VLAN ? (*vlan_pcp_p)((_ifp), (_pcp)) : EINVAL)
151e4cd31ddSJeff Roberson #define	VLAN_COOKIE(_ifp)					\
152*113af4fdSJustin Hibbits 	(if_gettype(_ifp) == IFT_L2VLAN ? (*vlan_cookie_p)((_ifp)) : NULL)
153e4cd31ddSJeff Roberson #define	VLAN_SETCOOKIE(_ifp, _cookie)				\
154*113af4fdSJustin Hibbits 	(if_gettype(_ifp) == IFT_L2VLAN ?			\
155b58e7aacSHans Petter Selasky 	    (*vlan_setcookie_p)((_ifp), (_cookie)) : EINVAL)
1567983103aSRobert Watson #define	VLAN_DEVAT(_ifp, _vid)					\
157b58e7aacSHans Petter Selasky 	((_ifp)->if_vlantrunk != NULL ? (*vlan_devat_p)((_ifp), (_vid)) : NULL)
158e4cd31ddSJeff Roberson 
15975ee267cSGleb Smirnoff extern	void (*vlan_trunk_cap_p)(struct ifnet *);
160e4cd31ddSJeff Roberson extern	struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
161e4cd31ddSJeff Roberson extern	struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
162e4cd31ddSJeff Roberson extern	int (*vlan_tag_p)(struct ifnet *, uint16_t *);
16332d2623aSNavdeep Parhar extern	int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
164e4cd31ddSJeff Roberson extern	int (*vlan_setcookie_p)(struct ifnet *, void *);
165e4cd31ddSJeff Roberson extern	void *(*vlan_cookie_p)(struct ifnet *);
166e4cd31ddSJeff Roberson 
167e2e050c8SConrad Meyer #include <sys/_eventhandler.h>
168e2e050c8SConrad Meyer 
1697ced9c2fSGleb Smirnoff /* VLAN state change events */
1707ced9c2fSGleb Smirnoff typedef void (*vlan_config_fn)(void *, struct ifnet *, uint16_t);
1717ced9c2fSGleb Smirnoff typedef void (*vlan_unconfig_fn)(void *, struct ifnet *, uint16_t);
1727ced9c2fSGleb Smirnoff EVENTHANDLER_DECLARE(vlan_config, vlan_config_fn);
1737ced9c2fSGleb Smirnoff EVENTHANDLER_DECLARE(vlan_unconfig, vlan_unconfig_fn);
1747ced9c2fSGleb Smirnoff 
1759ef8cd0bSKristof Provost static inline int
1769ef8cd0bSKristof Provost vlan_set_pcp(struct mbuf *m, uint8_t prio)
1779ef8cd0bSKristof Provost {
1789ef8cd0bSKristof Provost 	struct m_tag *mtag;
1799ef8cd0bSKristof Provost 
1809ef8cd0bSKristof Provost 	KASSERT(prio <= VLAN_PCP_MAX,
1819ef8cd0bSKristof Provost 	    ("%s with invalid pcp", __func__));
1829ef8cd0bSKristof Provost 
1839ef8cd0bSKristof Provost 	mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL);
1849ef8cd0bSKristof Provost 	if (mtag == NULL) {
1859ef8cd0bSKristof Provost 		mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_OUT,
1869ef8cd0bSKristof Provost 		    sizeof(uint8_t), M_NOWAIT);
1879ef8cd0bSKristof Provost 		if (mtag == NULL)
1889ef8cd0bSKristof Provost 			return (ENOMEM);
1899ef8cd0bSKristof Provost 		m_tag_prepend(m, mtag);
1909ef8cd0bSKristof Provost 	}
1919ef8cd0bSKristof Provost 
1929ef8cd0bSKristof Provost 	*(uint8_t *)(mtag + 1) = prio;
1939ef8cd0bSKristof Provost 
1949ef8cd0bSKristof Provost 	return (0);
1959ef8cd0bSKristof Provost }
1969ef8cd0bSKristof Provost 
197a3814acfSSam Leffler #endif /* _KERNEL */
198a3814acfSSam Leffler 
1992cc2df49SGarrett Wollman #endif /* _NET_IF_VLAN_VAR_H_ */
200