xref: /freebsd/sys/net/if_vlan_var.h (revision d2bc7754a226c031b76184277e32c4d65a763f67)
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 #include <sys/mbuf.h>
36 
37 /* Set the VLAN ID in an mbuf packet header non-destructively. */
38 #define EVL_APPLY_VLID(m, vlid)						\
39 	do {								\
40 		if ((m)->m_flags & M_VLANTAG) {				\
41 			(m)->m_pkthdr.ether_vtag &= EVL_VLID_MASK;	\
42 			(m)->m_pkthdr.ether_vtag |= (vlid);		\
43 		} else {						\
44 			(m)->m_pkthdr.ether_vtag = (vlid);		\
45 			(m)->m_flags |= M_VLANTAG;			\
46 		}							\
47 	} while (0)
48 
49 /* Set the priority ID in an mbuf packet header non-destructively. */
50 #define EVL_APPLY_PRI(m, pri)						\
51 	do {								\
52 		if ((m)->m_flags & M_VLANTAG) {				\
53 			uint16_t __vlantag = (m)->m_pkthdr.ether_vtag;	\
54 			(m)->m_pkthdr.ether_vtag |= EVL_MAKETAG(	\
55 			    EVL_VLANOFTAG(__vlantag), (pri),		\
56 			    EVL_CFIOFTAG(__vlantag));			\
57 		} else {						\
58 			(m)->m_pkthdr.ether_vtag =			\
59 			    EVL_MAKETAG(0, (pri), 0);			\
60 			(m)->m_flags |= M_VLANTAG;			\
61 		}							\
62 	} while (0)
63 
64 /* sysctl(3) tags, for compatibility purposes */
65 #define	VLANCTL_PROTO	1
66 #define	VLANCTL_MAX	2
67 
68 /*
69  * Configuration structure for SIOCSETVLAN and SIOCGETVLAN ioctls.
70  */
71 struct	vlanreq {
72 	char	vlr_parent[IFNAMSIZ];
73 	u_short	vlr_tag;
74 	u_short	vlr_proto;
75 };
76 #define	SIOCSETVLAN	SIOCSIFGENERIC
77 #define	SIOCGETVLAN	SIOCGIFGENERIC
78 
79 #define	SIOCGVLANPCP	SIOCGLANPCP	/* Get VLAN PCP */
80 #define	SIOCSVLANPCP	SIOCSLANPCP	/* Set VLAN PCP */
81 
82 #ifdef _KERNEL
83 /*
84  * Drivers that are capable of adding and removing the VLAN header
85  * in hardware indicate they support this by marking IFCAP_VLAN_HWTAGGING
86  * in if_capabilities.  Drivers for hardware that is capable
87  * of handling larger MTU's that may include a software-appended
88  * VLAN header w/o lowering the normal MTU should mark IFCAP_VLAN_MTU
89  * in if_capabilities; this notifies the VLAN code it can leave the
90  * MTU on the vlan interface at the normal setting.
91  */
92 
93 /*
94  * VLAN tags are stored in host byte order.  Byte swapping may be
95  * necessary.
96  *
97  * Drivers that support hardware VLAN tag stripping fill in the
98  * received VLAN tag (containing both vlan and priority information)
99  * into the ether_vtag mbuf packet header field:
100  *
101  *	m->m_pkthdr.ether_vtag = vtag;		// ntohs()?
102  *	m->m_flags |= M_VLANTAG;
103  *
104  * to mark the packet m with the specified VLAN tag.
105  *
106  * On output the driver should check the mbuf for the M_VLANTAG
107  * flag to see if a VLAN tag is present and valid:
108  *
109  *	if (m->m_flags & M_VLANTAG) {
110  *		... = m->m_pkthdr.ether_vtag;	// htons()?
111  *		... pass tag to hardware ...
112  *	}
113  *
114  * Note that a driver must indicate it supports hardware VLAN
115  * stripping/insertion by marking IFCAP_VLAN_HWTAGGING in
116  * if_capabilities.
117  */
118 
119 /*
120  * The 802.1q code may also tag mbufs with the PCP (priority) field for use in
121  * other layers of the stack, in which case an m_tag will be used.  This is
122  * semantically quite different from use of the ether_vtag field, which is
123  * defined only between the device driver and VLAN layer.
124  */
125 #define	MTAG_8021Q		1326104895
126 #define	MTAG_8021Q_PCP_IN	0		/* Input priority. */
127 #define	MTAG_8021Q_PCP_OUT	1		/* Output priority. */
128 
129 #define	VLAN_PCP_MAX		7
130 
131 /*
132  * 802.1q full tag. Proto and vid are stored in host byte order.
133  */
134 struct ether_8021q_tag {
135 	uint16_t proto;
136 	uint16_t vid;
137 	uint8_t  pcp;
138 };
139 
140 #define	VLAN_CAPABILITIES(_ifp) do {				\
141 	if ((_ifp)->if_vlantrunk != NULL) 			\
142 		(*vlan_trunk_cap_p)(_ifp);			\
143 } while (0)
144 
145 #define	VLAN_TRUNKDEV(_ifp)					\
146 	((_ifp)->if_type == IFT_L2VLAN ? (*vlan_trunkdev_p)((_ifp)) : NULL)
147 #define	VLAN_TAG(_ifp, _vid)					\
148 	((_ifp)->if_type == IFT_L2VLAN ? (*vlan_tag_p)((_ifp), (_vid)) : EINVAL)
149 #define	VLAN_PCP(_ifp, _pcp)					\
150 	((_ifp)->if_type == IFT_L2VLAN ? (*vlan_pcp_p)((_ifp), (_pcp)) : EINVAL)
151 #define	VLAN_COOKIE(_ifp)					\
152 	((_ifp)->if_type == IFT_L2VLAN ? (*vlan_cookie_p)((_ifp)) : NULL)
153 #define	VLAN_SETCOOKIE(_ifp, _cookie)				\
154 	((_ifp)->if_type == IFT_L2VLAN ?			\
155 	    (*vlan_setcookie_p)((_ifp), (_cookie)) : EINVAL)
156 #define	VLAN_DEVAT(_ifp, _vid)					\
157 	((_ifp)->if_vlantrunk != NULL ? (*vlan_devat_p)((_ifp), (_vid)) : NULL)
158 
159 extern	void (*vlan_trunk_cap_p)(struct ifnet *);
160 extern	struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
161 extern	struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
162 extern	int (*vlan_tag_p)(struct ifnet *, uint16_t *);
163 extern	int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
164 extern	int (*vlan_setcookie_p)(struct ifnet *, void *);
165 extern	void *(*vlan_cookie_p)(struct ifnet *);
166 
167 #include <sys/_eventhandler.h>
168 
169 /* VLAN state change events */
170 typedef void (*vlan_config_fn)(void *, struct ifnet *, uint16_t);
171 typedef void (*vlan_unconfig_fn)(void *, struct ifnet *, uint16_t);
172 EVENTHANDLER_DECLARE(vlan_config, vlan_config_fn);
173 EVENTHANDLER_DECLARE(vlan_unconfig, vlan_unconfig_fn);
174 
175 static inline int
176 vlan_set_pcp(struct mbuf *m, uint8_t prio)
177 {
178 	struct m_tag *mtag;
179 
180 	KASSERT(prio <= VLAN_PCP_MAX,
181 	    ("%s with invalid pcp", __func__));
182 
183 	mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL);
184 	if (mtag == NULL) {
185 		mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_OUT,
186 		    sizeof(uint8_t), M_NOWAIT);
187 		if (mtag == NULL)
188 			return (ENOMEM);
189 		m_tag_prepend(m, mtag);
190 	}
191 
192 	*(uint8_t *)(mtag + 1) = prio;
193 
194 	return (0);
195 }
196 
197 #endif /* _KERNEL */
198 
199 #endif /* _NET_IF_VLAN_VAR_H_ */
200