xref: /freebsd/sys/net/if_vlan_var.h (revision 7ced9c2f66f705eaf6bb3e7d69db4517e329185f)
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 
352cc2df49SGarrett Wollman struct	ether_vlan_header {
362cc2df49SGarrett Wollman 	u_char	evl_dhost[ETHER_ADDR_LEN];
372cc2df49SGarrett Wollman 	u_char	evl_shost[ETHER_ADDR_LEN];
382cc2df49SGarrett Wollman 	u_int16_t evl_encap_proto;
392cc2df49SGarrett Wollman 	u_int16_t evl_tag;
402cc2df49SGarrett Wollman 	u_int16_t evl_proto;
412cc2df49SGarrett Wollman };
422cc2df49SGarrett Wollman 
43fb88a3e0SBill Paul #define	EVL_VLID_MASK		0x0FFF
4412c34560SBruce M Simpson #define	EVL_PRI_MASK		0xE000
45fb88a3e0SBill Paul #define	EVL_VLANOFTAG(tag)	((tag) & EVL_VLID_MASK)
462cc2df49SGarrett Wollman #define	EVL_PRIOFTAG(tag)	(((tag) >> 13) & 7)
4712c34560SBruce M Simpson #define	EVL_CFIOFTAG(tag)	(((tag) >> 12) & 1)
4812c34560SBruce M Simpson #define	EVL_MAKETAG(vlid, pri, cfi)					\
4912c34560SBruce M Simpson 	((((((pri) & 7) << 1) | ((cfi) & 1)) << 12) | ((vlid) & EVL_VLID_MASK))
5012c34560SBruce M Simpson 
5112c34560SBruce M Simpson /* Set the VLAN ID in an mbuf packet header non-destructively. */
5212c34560SBruce M Simpson #define EVL_APPLY_VLID(m, vlid)						\
5312c34560SBruce M Simpson 	do {								\
5412c34560SBruce M Simpson 		if ((m)->m_flags & M_VLANTAG) {				\
5512c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag &= EVL_VLID_MASK;	\
5612c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag |= (vlid);		\
5712c34560SBruce M Simpson 		} else {						\
5812c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag = (vlid);		\
5912c34560SBruce M Simpson 			(m)->m_flags |= M_VLANTAG;			\
6012c34560SBruce M Simpson 		}							\
6112c34560SBruce M Simpson 	} while (0)
6212c34560SBruce M Simpson 
6312c34560SBruce M Simpson /* Set the priority ID in an mbuf packet header non-destructively. */
6412c34560SBruce M Simpson #define EVL_APPLY_PRI(m, pri)						\
6512c34560SBruce M Simpson 	do {								\
6612c34560SBruce M Simpson 		if ((m)->m_flags & M_VLANTAG) {				\
6712c34560SBruce M Simpson 			uint16_t __vlantag = (m)->m_pkthdr.ether_vtag;	\
6812c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag |= EVL_MAKETAG(	\
6912c34560SBruce M Simpson 			    EVL_VLANOFTAG(__vlantag), (pri),		\
7012c34560SBruce M Simpson 			    EVL_CFIOFTAG(__vlantag));			\
7112c34560SBruce M Simpson 		} else {						\
7212c34560SBruce M Simpson 			(m)->m_pkthdr.ether_vtag =			\
7312c34560SBruce M Simpson 			    EVL_MAKETAG(0, (pri), 0);			\
7412c34560SBruce M Simpson 			(m)->m_flags |= M_VLANTAG;			\
7512c34560SBruce M Simpson 		}							\
7612c34560SBruce M Simpson 	} while (0)
772cc2df49SGarrett Wollman 
782cc2df49SGarrett Wollman /* sysctl(3) tags, for compatibility purposes */
792cc2df49SGarrett Wollman #define	VLANCTL_PROTO	1
802cc2df49SGarrett Wollman #define	VLANCTL_MAX	2
812cc2df49SGarrett Wollman 
822cc2df49SGarrett Wollman /*
832cc2df49SGarrett Wollman  * Configuration structure for SIOCSETVLAN and SIOCGETVLAN ioctls.
842cc2df49SGarrett Wollman  */
852cc2df49SGarrett Wollman struct	vlanreq {
862cc2df49SGarrett Wollman 	char	vlr_parent[IFNAMSIZ];
872cc2df49SGarrett Wollman 	u_short	vlr_tag;
882cc2df49SGarrett Wollman };
892cc2df49SGarrett Wollman #define	SIOCSETVLAN	SIOCSIFGENERIC
902cc2df49SGarrett Wollman #define	SIOCGETVLAN	SIOCGIFGENERIC
912cc2df49SGarrett Wollman 
92a3814acfSSam Leffler #ifdef _KERNEL
93a3814acfSSam Leffler /*
94a3814acfSSam Leffler  * Drivers that are capable of adding and removing the VLAN header
95a3814acfSSam Leffler  * in hardware indicate they support this by marking IFCAP_VLAN_HWTAGGING
96a4e16dddSYaroslav Tykhiy  * in if_capabilities.  Drivers for hardware that is capable
97a3814acfSSam Leffler  * of handling larger MTU's that may include a software-appended
98a4e16dddSYaroslav Tykhiy  * VLAN header w/o lowering the normal MTU should mark IFCAP_VLAN_MTU
99a4e16dddSYaroslav Tykhiy  * in if_capabilities; this notifies the VLAN code it can leave the
100a3814acfSSam Leffler  * MTU on the vlan interface at the normal setting.
101a3814acfSSam Leffler  */
102a3814acfSSam Leffler 
103a3814acfSSam Leffler /*
10478ba57b9SAndre Oppermann  * VLAN tags are stored in host byte order.  Byte swapping may be
10578ba57b9SAndre Oppermann  * necessary.
106a3814acfSSam Leffler  *
10778ba57b9SAndre Oppermann  * Drivers that support hardware VLAN tag stripping fill in the
10878ba57b9SAndre Oppermann  * received VLAN tag (containing both vlan and priority information)
10978ba57b9SAndre Oppermann  * into the ether_vtag mbuf packet header field:
110a3814acfSSam Leffler  *
1117983103aSRobert Watson  *	m->m_pkthdr.ether_vtag = vtag;		// ntohs()?
11278ba57b9SAndre Oppermann  *	m->m_flags |= M_VLANTAG;
113a3814acfSSam Leffler  *
11478ba57b9SAndre Oppermann  * to mark the packet m with the specified VLAN tag.
11578ba57b9SAndre Oppermann  *
11678ba57b9SAndre Oppermann  * On output the driver should check the mbuf for the M_VLANTAG
11778ba57b9SAndre Oppermann  * flag to see if a VLAN tag is present and valid:
11878ba57b9SAndre Oppermann  *
11978ba57b9SAndre Oppermann  *	if (m->m_flags & M_VLANTAG) {
12078ba57b9SAndre Oppermann  *		... = m->m_pkthdr.ether_vtag;	// htons()?
121a3814acfSSam Leffler  *		... pass tag to hardware ...
122a3814acfSSam Leffler  *	}
123a3814acfSSam Leffler  *
124a3814acfSSam Leffler  * Note that a driver must indicate it supports hardware VLAN
12578ba57b9SAndre Oppermann  * stripping/insertion by marking IFCAP_VLAN_HWTAGGING in
12678ba57b9SAndre Oppermann  * if_capabilities.
127a3814acfSSam Leffler  */
128a3814acfSSam Leffler 
12975ee267cSGleb Smirnoff #define	VLAN_CAPABILITIES(_ifp) do {				\
13075ee267cSGleb Smirnoff 	if ((_ifp)->if_vlantrunk != NULL) 			\
13175ee267cSGleb Smirnoff 		(*vlan_trunk_cap_p)(_ifp);			\
13275ee267cSGleb Smirnoff } while (0)
13375ee267cSGleb Smirnoff 
134e4cd31ddSJeff Roberson #define	VLAN_TRUNKDEV(_ifp)					\
135e4cd31ddSJeff Roberson 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_trunkdev_p)((_ifp)) : NULL
1367983103aSRobert Watson #define	VLAN_TAG(_ifp, _vid)					\
1377983103aSRobert Watson 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_tag_p)((_ifp), (_vid)) : EINVAL
138e4cd31ddSJeff Roberson #define	VLAN_COOKIE(_ifp)					\
139e4cd31ddSJeff Roberson 	(_ifp)->if_type == IFT_L2VLAN ? (*vlan_cookie_p)((_ifp)) : NULL
140e4cd31ddSJeff Roberson #define	VLAN_SETCOOKIE(_ifp, _cookie)				\
141e4cd31ddSJeff Roberson 	(_ifp)->if_type == IFT_L2VLAN ?				\
142e4cd31ddSJeff Roberson 	    (*vlan_setcookie_p)((_ifp), (_cookie)) : EINVAL
1437983103aSRobert Watson #define	VLAN_DEVAT(_ifp, _vid)					\
1447983103aSRobert Watson 	(_ifp)->if_vlantrunk != NULL ? (*vlan_devat_p)((_ifp), (_vid)) : NULL
145e4cd31ddSJeff Roberson 
14675ee267cSGleb Smirnoff extern	void (*vlan_trunk_cap_p)(struct ifnet *);
147e4cd31ddSJeff Roberson extern	struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
148e4cd31ddSJeff Roberson extern	struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
149e4cd31ddSJeff Roberson extern	int (*vlan_tag_p)(struct ifnet *, uint16_t *);
150e4cd31ddSJeff Roberson extern	int (*vlan_setcookie_p)(struct ifnet *, void *);
151e4cd31ddSJeff Roberson extern	void *(*vlan_cookie_p)(struct ifnet *);
152e4cd31ddSJeff Roberson 
153*7ced9c2fSGleb Smirnoff #ifdef SYS_EVENTHANDLER_H
154*7ced9c2fSGleb Smirnoff /* VLAN state change events */
155*7ced9c2fSGleb Smirnoff typedef void (*vlan_config_fn)(void *, struct ifnet *, uint16_t);
156*7ced9c2fSGleb Smirnoff typedef void (*vlan_unconfig_fn)(void *, struct ifnet *, uint16_t);
157*7ced9c2fSGleb Smirnoff EVENTHANDLER_DECLARE(vlan_config, vlan_config_fn);
158*7ced9c2fSGleb Smirnoff EVENTHANDLER_DECLARE(vlan_unconfig, vlan_unconfig_fn);
159*7ced9c2fSGleb Smirnoff #endif /* SYS_EVENTHANDLER_H */
160*7ced9c2fSGleb Smirnoff 
161a3814acfSSam Leffler #endif /* _KERNEL */
162a3814acfSSam Leffler 
1632cc2df49SGarrett Wollman #endif /* _NET_IF_VLAN_VAR_H_ */
164