xref: /illumos-gate/usr/src/uts/common/io/mac/plugins/mac_ipv6.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
1*2b24ab6bSSebastien Roy /*
2*2b24ab6bSSebastien Roy  * CDDL HEADER START
3*2b24ab6bSSebastien Roy  *
4*2b24ab6bSSebastien Roy  * The contents of this file are subject to the terms of the
5*2b24ab6bSSebastien Roy  * Common Development and Distribution License (the "License").
6*2b24ab6bSSebastien Roy  * You may not use this file except in compliance with the License.
7*2b24ab6bSSebastien Roy  *
8*2b24ab6bSSebastien Roy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2b24ab6bSSebastien Roy  * or http://www.opensolaris.org/os/licensing.
10*2b24ab6bSSebastien Roy  * See the License for the specific language governing permissions
11*2b24ab6bSSebastien Roy  * and limitations under the License.
12*2b24ab6bSSebastien Roy  *
13*2b24ab6bSSebastien Roy  * When distributing Covered Code, include this CDDL HEADER in each
14*2b24ab6bSSebastien Roy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2b24ab6bSSebastien Roy  * If applicable, add the following below this CDDL HEADER, with the
16*2b24ab6bSSebastien Roy  * fields enclosed by brackets "[]" replaced with your own identifying
17*2b24ab6bSSebastien Roy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2b24ab6bSSebastien Roy  *
19*2b24ab6bSSebastien Roy  * CDDL HEADER END
20*2b24ab6bSSebastien Roy  */
21*2b24ab6bSSebastien Roy /*
22*2b24ab6bSSebastien Roy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*2b24ab6bSSebastien Roy  * Use is subject to license terms.
24*2b24ab6bSSebastien Roy  */
25*2b24ab6bSSebastien Roy 
26*2b24ab6bSSebastien Roy /*
27*2b24ab6bSSebastien Roy  * DL_IPV6 MAC Type plugin for the Nemo mac module
28*2b24ab6bSSebastien Roy  */
29*2b24ab6bSSebastien Roy 
30*2b24ab6bSSebastien Roy #include <sys/types.h>
31*2b24ab6bSSebastien Roy #include <sys/modctl.h>
32*2b24ab6bSSebastien Roy #include <sys/dlpi.h>
33*2b24ab6bSSebastien Roy #include <sys/mac.h>
34*2b24ab6bSSebastien Roy #include <sys/mac_ipv6.h>
35*2b24ab6bSSebastien Roy #include <sys/mac_ipv4_impl.h>
36*2b24ab6bSSebastien Roy #include <sys/byteorder.h>
37*2b24ab6bSSebastien Roy #include <sys/strsun.h>
38*2b24ab6bSSebastien Roy #include <netinet/ip6.h>
39*2b24ab6bSSebastien Roy #include <inet/common.h>
40*2b24ab6bSSebastien Roy #include <inet/mib2.h>
41*2b24ab6bSSebastien Roy #include <inet/ip.h>
42*2b24ab6bSSebastien Roy #include <inet/ip6.h>
43*2b24ab6bSSebastien Roy #include <inet/iptun.h>
44*2b24ab6bSSebastien Roy 
45*2b24ab6bSSebastien Roy static struct modlmisc mac_ipv6_modlmisc = {
46*2b24ab6bSSebastien Roy 	&mod_miscops,
47*2b24ab6bSSebastien Roy 	"IPv6 tunneling MAC plugin"
48*2b24ab6bSSebastien Roy };
49*2b24ab6bSSebastien Roy 
50*2b24ab6bSSebastien Roy static struct modlinkage mac_ipv6_modlinkage = {
51*2b24ab6bSSebastien Roy 	MODREV_1,
52*2b24ab6bSSebastien Roy 	&mac_ipv6_modlmisc,
53*2b24ab6bSSebastien Roy 	NULL
54*2b24ab6bSSebastien Roy };
55*2b24ab6bSSebastien Roy 
56*2b24ab6bSSebastien Roy static mactype_ops_t mac_ipv6_type_ops;
57*2b24ab6bSSebastien Roy 
58*2b24ab6bSSebastien Roy int
_init(void)59*2b24ab6bSSebastien Roy _init(void)
60*2b24ab6bSSebastien Roy {
61*2b24ab6bSSebastien Roy 	mactype_register_t *mtrp;
62*2b24ab6bSSebastien Roy 	int	err;
63*2b24ab6bSSebastien Roy 
64*2b24ab6bSSebastien Roy 	if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
65*2b24ab6bSSebastien Roy 		return (EINVAL);
66*2b24ab6bSSebastien Roy 	mtrp->mtr_ident = MAC_PLUGIN_IDENT_IPV6;
67*2b24ab6bSSebastien Roy 	mtrp->mtr_ops = &mac_ipv6_type_ops;
68*2b24ab6bSSebastien Roy 	mtrp->mtr_mactype = DL_IPV6;
69*2b24ab6bSSebastien Roy 	mtrp->mtr_nativetype = DL_IPV6;
70*2b24ab6bSSebastien Roy 	mtrp->mtr_addrlen = sizeof (in6_addr_t);
71*2b24ab6bSSebastien Roy 	if ((err = mactype_register(mtrp)) == 0) {
72*2b24ab6bSSebastien Roy 		if ((err = mod_install(&mac_ipv6_modlinkage)) != 0)
73*2b24ab6bSSebastien Roy 			(void) mactype_unregister(MAC_PLUGIN_IDENT_IPV6);
74*2b24ab6bSSebastien Roy 	}
75*2b24ab6bSSebastien Roy 	mactype_free(mtrp);
76*2b24ab6bSSebastien Roy 	return (err);
77*2b24ab6bSSebastien Roy }
78*2b24ab6bSSebastien Roy 
79*2b24ab6bSSebastien Roy int
_fini(void)80*2b24ab6bSSebastien Roy _fini(void)
81*2b24ab6bSSebastien Roy {
82*2b24ab6bSSebastien Roy 	int	err;
83*2b24ab6bSSebastien Roy 	if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IPV6)) != 0)
84*2b24ab6bSSebastien Roy 		return (err);
85*2b24ab6bSSebastien Roy 	return (mod_remove(&mac_ipv6_modlinkage));
86*2b24ab6bSSebastien Roy }
87*2b24ab6bSSebastien Roy 
88*2b24ab6bSSebastien Roy int
_info(struct modinfo * modinfop)89*2b24ab6bSSebastien Roy _info(struct modinfo *modinfop)
90*2b24ab6bSSebastien Roy {
91*2b24ab6bSSebastien Roy 	return (mod_info(&mac_ipv6_modlinkage, modinfop));
92*2b24ab6bSSebastien Roy }
93*2b24ab6bSSebastien Roy 
94*2b24ab6bSSebastien Roy 
95*2b24ab6bSSebastien Roy /*
96*2b24ab6bSSebastien Roy  * MAC Type plugin operations
97*2b24ab6bSSebastien Roy  */
98*2b24ab6bSSebastien Roy 
99*2b24ab6bSSebastien Roy /* ARGSUSED */
100*2b24ab6bSSebastien Roy int
mac_ipv6_unicst_verify(const void * addr,void * pdata)101*2b24ab6bSSebastien Roy mac_ipv6_unicst_verify(const void *addr, void *pdata)
102*2b24ab6bSSebastien Roy {
103*2b24ab6bSSebastien Roy 	const in6_addr_t *in6addr = addr;
104*2b24ab6bSSebastien Roy 	if (IN6_IS_ADDR_UNSPECIFIED(in6addr) ||
105*2b24ab6bSSebastien Roy 	    IN6_IS_ADDR_LOOPBACK(in6addr) ||
106*2b24ab6bSSebastien Roy 	    IN6_IS_ADDR_MULTICAST(in6addr) ||
107*2b24ab6bSSebastien Roy 	    IN6_IS_ADDR_V4MAPPED(in6addr) ||
108*2b24ab6bSSebastien Roy 	    IN6_IS_ADDR_V4COMPAT(in6addr)) {
109*2b24ab6bSSebastien Roy 		return (EINVAL);
110*2b24ab6bSSebastien Roy 	}
111*2b24ab6bSSebastien Roy 	return (0);
112*2b24ab6bSSebastien Roy }
113*2b24ab6bSSebastien Roy 
114*2b24ab6bSSebastien Roy /*
115*2b24ab6bSSebastien Roy  * Build an IPv6 link-layer header for tunneling.  If provided, the
116*2b24ab6bSSebastien Roy  * template header provided by the driver supplies the traffic class, flow
117*2b24ab6bSSebastien Roy  * label, hop limit, and potential options.  The template's payload length
118*2b24ab6bSSebastien Roy  * must either be 0 if there are no extension headers, or reflect the size
119*2b24ab6bSSebastien Roy  * of the extension headers if present.  The template's next header value
120*2b24ab6bSSebastien Roy  * must either be IPPROTO_NONE if no extension headers are present, or
121*2b24ab6bSSebastien Roy  * reflect the type of extension header that follows (the same is true for
122*2b24ab6bSSebastien Roy  * the field values of the extension headers themselves.)
123*2b24ab6bSSebastien Roy  */
124*2b24ab6bSSebastien Roy /* ARGSUSED */
125*2b24ab6bSSebastien Roy mblk_t *
mac_ipv6_header(const void * saddr,const void * daddr,uint32_t sap,void * pdata,mblk_t * payload,size_t extra_len)126*2b24ab6bSSebastien Roy mac_ipv6_header(const void *saddr, const void *daddr, uint32_t sap, void *pdata,
127*2b24ab6bSSebastien Roy     mblk_t *payload, size_t extra_len)
128*2b24ab6bSSebastien Roy {
129*2b24ab6bSSebastien Roy 	ip6_t	*ip6hp;
130*2b24ab6bSSebastien Roy 	ip6_t	*tmpl_ip6hp = pdata;
131*2b24ab6bSSebastien Roy 	mblk_t	*mp;
132*2b24ab6bSSebastien Roy 	size_t	hdr_len = sizeof (ip6_t);
133*2b24ab6bSSebastien Roy 	uint8_t	*nxt_proto;
134*2b24ab6bSSebastien Roy 
135*2b24ab6bSSebastien Roy 	if (!mac_ipv4_sap_verify(sap, NULL, NULL))
136*2b24ab6bSSebastien Roy 		return (NULL);
137*2b24ab6bSSebastien Roy 
138*2b24ab6bSSebastien Roy 	if (tmpl_ip6hp != NULL)
139*2b24ab6bSSebastien Roy 		hdr_len = sizeof (ip6_t) + tmpl_ip6hp->ip6_plen;
140*2b24ab6bSSebastien Roy 
141*2b24ab6bSSebastien Roy 	if ((mp = allocb(hdr_len + extra_len, BPRI_HI)) == NULL)
142*2b24ab6bSSebastien Roy 		return (NULL);
143*2b24ab6bSSebastien Roy 
144*2b24ab6bSSebastien Roy 	ip6hp = (ip6_t *)mp->b_rptr;
145*2b24ab6bSSebastien Roy 
146*2b24ab6bSSebastien Roy 	bzero(ip6hp, hdr_len + extra_len);
147*2b24ab6bSSebastien Roy 	if (tmpl_ip6hp != NULL) {
148*2b24ab6bSSebastien Roy 		bcopy(tmpl_ip6hp, ip6hp, hdr_len);
149*2b24ab6bSSebastien Roy 	} else {
150*2b24ab6bSSebastien Roy 		ip6hp->ip6_nxt = IPPROTO_NONE;
151*2b24ab6bSSebastien Roy 		ip6hp->ip6_hlim = IPTUN_DEFAULT_HOPLIMIT;
152*2b24ab6bSSebastien Roy 	}
153*2b24ab6bSSebastien Roy 
154*2b24ab6bSSebastien Roy 	ip6hp->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
155*2b24ab6bSSebastien Roy 	ip6hp->ip6_plen = 0;
156*2b24ab6bSSebastien Roy 
157*2b24ab6bSSebastien Roy 	nxt_proto = &ip6hp->ip6_nxt;
158*2b24ab6bSSebastien Roy 	if (*nxt_proto != IPPROTO_NONE) {
159*2b24ab6bSSebastien Roy 		ip6_dest_t *hdrptr = (ip6_dest_t *)(ip6hp + 1);
160*2b24ab6bSSebastien Roy 		nxt_proto = &hdrptr->ip6d_nxt;
161*2b24ab6bSSebastien Roy 		while (*nxt_proto != IPPROTO_NONE) {
162*2b24ab6bSSebastien Roy 			hdrptr = (ip6_dest_t *)((uint8_t *)hdrptr +
163*2b24ab6bSSebastien Roy 			    (8 * (hdrptr->ip6d_len + 1)));
164*2b24ab6bSSebastien Roy 			nxt_proto = &hdrptr->ip6d_nxt;
165*2b24ab6bSSebastien Roy 		}
166*2b24ab6bSSebastien Roy 	}
167*2b24ab6bSSebastien Roy 	*nxt_proto = (uint8_t)sap;
168*2b24ab6bSSebastien Roy 	bcopy(saddr, &(ip6hp->ip6_src), sizeof (in6_addr_t));
169*2b24ab6bSSebastien Roy 	bcopy(daddr, &(ip6hp->ip6_dst), sizeof (in6_addr_t));
170*2b24ab6bSSebastien Roy 
171*2b24ab6bSSebastien Roy 	mp->b_wptr += hdr_len;
172*2b24ab6bSSebastien Roy 	return (mp);
173*2b24ab6bSSebastien Roy }
174*2b24ab6bSSebastien Roy 
175*2b24ab6bSSebastien Roy /* ARGSUSED */
176*2b24ab6bSSebastien Roy int
mac_ipv6_header_info(mblk_t * mp,void * pdata,mac_header_info_t * hdr_info)177*2b24ab6bSSebastien Roy mac_ipv6_header_info(mblk_t *mp, void *pdata, mac_header_info_t *hdr_info)
178*2b24ab6bSSebastien Roy {
179*2b24ab6bSSebastien Roy 	ip6_t	*ip6hp;
180*2b24ab6bSSebastien Roy 	uint8_t	*whereptr, *endptr;
181*2b24ab6bSSebastien Roy 	uint8_t	nexthdr;
182*2b24ab6bSSebastien Roy 
183*2b24ab6bSSebastien Roy 	if (MBLKL(mp) < sizeof (ip6_t))
184*2b24ab6bSSebastien Roy 		return (EINVAL);
185*2b24ab6bSSebastien Roy 
186*2b24ab6bSSebastien Roy 	ip6hp = (ip6_t *)mp->b_rptr;
187*2b24ab6bSSebastien Roy 
188*2b24ab6bSSebastien Roy 	/*
189*2b24ab6bSSebastien Roy 	 * IPv6 tunnels don't have a concept of link-layer multicast since
190*2b24ab6bSSebastien Roy 	 * they have fixed unicast endpoints.
191*2b24ab6bSSebastien Roy 	 */
192*2b24ab6bSSebastien Roy 	if (mac_ipv6_unicst_verify(&ip6hp->ip6_dst, NULL) != 0)
193*2b24ab6bSSebastien Roy 		return (EINVAL);
194*2b24ab6bSSebastien Roy 
195*2b24ab6bSSebastien Roy 	nexthdr = ip6hp->ip6_nxt;
196*2b24ab6bSSebastien Roy 	whereptr = (uint8_t *)(ip6hp + 1);
197*2b24ab6bSSebastien Roy 	endptr = mp->b_wptr;
198*2b24ab6bSSebastien Roy 	while (nexthdr != IPPROTO_ENCAP && nexthdr != IPPROTO_IPV6) {
199*2b24ab6bSSebastien Roy 		ip6_dest_t	*exthdrptr = (ip6_dest_t *)whereptr;
200*2b24ab6bSSebastien Roy 
201*2b24ab6bSSebastien Roy 		if (whereptr + sizeof (ip6_dest_t) >= endptr)
202*2b24ab6bSSebastien Roy 			return (EINVAL);
203*2b24ab6bSSebastien Roy 
204*2b24ab6bSSebastien Roy 		nexthdr = exthdrptr->ip6d_nxt;
205*2b24ab6bSSebastien Roy 		whereptr += 8 * (exthdrptr->ip6d_len + 1);
206*2b24ab6bSSebastien Roy 
207*2b24ab6bSSebastien Roy 		if (whereptr > endptr)
208*2b24ab6bSSebastien Roy 			return (EINVAL);
209*2b24ab6bSSebastien Roy 	}
210*2b24ab6bSSebastien Roy 
211*2b24ab6bSSebastien Roy 	hdr_info->mhi_hdrsize = whereptr - mp->b_rptr;
212*2b24ab6bSSebastien Roy 	hdr_info->mhi_pktsize = 0;
213*2b24ab6bSSebastien Roy 	hdr_info->mhi_daddr = (const uint8_t *)&(ip6hp->ip6_dst);
214*2b24ab6bSSebastien Roy 	hdr_info->mhi_saddr = (const uint8_t *)&(ip6hp->ip6_src);
215*2b24ab6bSSebastien Roy 	hdr_info->mhi_bindsap = hdr_info->mhi_origsap = nexthdr;
216*2b24ab6bSSebastien Roy 	hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
217*2b24ab6bSSebastien Roy 	return (0);
218*2b24ab6bSSebastien Roy }
219*2b24ab6bSSebastien Roy 
220*2b24ab6bSSebastien Roy /*
221*2b24ab6bSSebastien Roy  * This plugin's MAC plugin data is a template IPv6 header followed by
222*2b24ab6bSSebastien Roy  * optional extension headers.  The chain of headers must be terminated by
223*2b24ab6bSSebastien Roy  * a header with a next header value of IPPROTO_NONE.  The payload length
224*2b24ab6bSSebastien Roy  * of the IPv6 header must be 0 if there are no extension headers, or must
225*2b24ab6bSSebastien Roy  * reflect the total size of extension headers present.
226*2b24ab6bSSebastien Roy  */
227*2b24ab6bSSebastien Roy boolean_t
mac_ipv6_pdata_verify(void * pdata,size_t pdata_size)228*2b24ab6bSSebastien Roy mac_ipv6_pdata_verify(void *pdata, size_t pdata_size)
229*2b24ab6bSSebastien Roy {
230*2b24ab6bSSebastien Roy 	ip6_t	*ip6hp = pdata;
231*2b24ab6bSSebastien Roy 	uint8_t	*whereptr, *endptr;
232*2b24ab6bSSebastien Roy 	uint8_t	nexthdr;
233*2b24ab6bSSebastien Roy 
234*2b24ab6bSSebastien Roy 	/*
235*2b24ab6bSSebastien Roy 	 * Since the plugin does not require plugin data, it is acceptable
236*2b24ab6bSSebastien Roy 	 * for drivers to pass in NULL plugin data as long as the plugin
237*2b24ab6bSSebastien Roy 	 * data size is consistent.
238*2b24ab6bSSebastien Roy 	 */
239*2b24ab6bSSebastien Roy 	if (pdata == NULL)
240*2b24ab6bSSebastien Roy 		return (pdata_size == 0);
241*2b24ab6bSSebastien Roy 
242*2b24ab6bSSebastien Roy 	/* First verify that we have enough data to hold an IPv6 header. */
243*2b24ab6bSSebastien Roy 	if (pdata_size < sizeof (ip6_t))
244*2b24ab6bSSebastien Roy 		return (B_FALSE);
245*2b24ab6bSSebastien Roy 	/* Make sure that pdata_size is consistent with the payload length. */
246*2b24ab6bSSebastien Roy 	if (pdata_size != sizeof (ip6_t) + ip6hp->ip6_plen)
247*2b24ab6bSSebastien Roy 		return (B_FALSE);
248*2b24ab6bSSebastien Roy 
249*2b24ab6bSSebastien Roy 	/*
250*2b24ab6bSSebastien Roy 	 * Make sure that the header chain is terminated by a header with a
251*2b24ab6bSSebastien Roy 	 * next header value of IPPROTO_NONE.
252*2b24ab6bSSebastien Roy 	 */
253*2b24ab6bSSebastien Roy 	nexthdr = ip6hp->ip6_nxt;
254*2b24ab6bSSebastien Roy 	if (nexthdr == IPPROTO_NONE)
255*2b24ab6bSSebastien Roy 		return (ip6hp->ip6_plen == 0);
256*2b24ab6bSSebastien Roy 	whereptr = (uint8_t *)(ip6hp + 1);
257*2b24ab6bSSebastien Roy 	endptr = (uint8_t *)pdata + pdata_size;
258*2b24ab6bSSebastien Roy 
259*2b24ab6bSSebastien Roy 	while (nexthdr != IPPROTO_NONE && whereptr < endptr) {
260*2b24ab6bSSebastien Roy 		ip6_dest_t *hdrptr = (ip6_dest_t *)whereptr;
261*2b24ab6bSSebastien Roy 
262*2b24ab6bSSebastien Roy 		/* make sure we're pointing at a complete header */
263*2b24ab6bSSebastien Roy 		if (whereptr + sizeof (ip6_dest_t) > endptr)
264*2b24ab6bSSebastien Roy 			break;
265*2b24ab6bSSebastien Roy 		nexthdr = hdrptr->ip6d_nxt;
266*2b24ab6bSSebastien Roy 		whereptr += 8 * (hdrptr->ip6d_len + 1);
267*2b24ab6bSSebastien Roy 	}
268*2b24ab6bSSebastien Roy 
269*2b24ab6bSSebastien Roy 	return (nexthdr == IPPROTO_NONE && whereptr == endptr);
270*2b24ab6bSSebastien Roy }
271*2b24ab6bSSebastien Roy 
272*2b24ab6bSSebastien Roy static mactype_ops_t mac_ipv6_type_ops = {
273*2b24ab6bSSebastien Roy 	MTOPS_PDATA_VERIFY,
274*2b24ab6bSSebastien Roy 	mac_ipv6_unicst_verify,
275*2b24ab6bSSebastien Roy 	mac_ipv4_multicst_verify, /* neither plugin supports multicast */
276*2b24ab6bSSebastien Roy 	mac_ipv4_sap_verify,	/* same set of legal SAP values */
277*2b24ab6bSSebastien Roy 	mac_ipv6_header,
278*2b24ab6bSSebastien Roy 	mac_ipv6_header_info,
279*2b24ab6bSSebastien Roy 	mac_ipv6_pdata_verify,
280*2b24ab6bSSebastien Roy 	NULL,
281*2b24ab6bSSebastien Roy 	NULL,
282*2b24ab6bSSebastien Roy 	NULL
283*2b24ab6bSSebastien Roy };
284