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