xref: /onnv-gate/usr/src/lib/libsocket/inet/inet6_opt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This code is conformant to RFC 3542.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <assert.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/socket.h>
37*0Sstevel@tonic-gate #include <sys/sysmacros.h>
38*0Sstevel@tonic-gate #include <netinet/in.h>
39*0Sstevel@tonic-gate #include <netinet/ip6.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <stdio.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #define	bufpos(p) ((p) - (uint8_t *)extbuf)
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * Section 10.1 RFC3542.  This function returns the size of the empty
47*0Sstevel@tonic-gate  * extension header.  If extbuf is not NULL then it initializes its length
48*0Sstevel@tonic-gate  * field.  If extlen is invalid then -1 is returned.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate int
inet6_opt_init(void * extbuf,socklen_t extlen)51*0Sstevel@tonic-gate inet6_opt_init(void *extbuf, socklen_t extlen)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	if (extbuf && ((extlen < 0) || (extlen % 8))) {
54*0Sstevel@tonic-gate 		return (-1);
55*0Sstevel@tonic-gate 	}
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (extbuf) {
58*0Sstevel@tonic-gate 		*(uint8_t *)extbuf = 0;
59*0Sstevel@tonic-gate 		*((uint8_t *)extbuf + 1) = extlen/8 - 1;
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	return (2);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Section 10.2 RFC3542.  This function appends an option to an already
67*0Sstevel@tonic-gate  * initialized option buffer.  inet6_opt_append() returns the total length
68*0Sstevel@tonic-gate  * after adding the option.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate int
inet6_opt_append(void * extbuf,socklen_t extlen,int offset,uint8_t type,socklen_t len,uint_t align,void ** databufp)71*0Sstevel@tonic-gate inet6_opt_append(void *extbuf, socklen_t extlen, int offset, uint8_t type,
72*0Sstevel@tonic-gate 	socklen_t len, uint_t align, void **databufp)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	uint8_t *p;
75*0Sstevel@tonic-gate 	socklen_t endlen;
76*0Sstevel@tonic-gate 	int remainder, padbytes;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (align > len ||
79*0Sstevel@tonic-gate 	    (align != 1 && align != 2 && align != 4 && align != 8) ||
80*0Sstevel@tonic-gate 	    len < 0 || len > 255 || type < 2) {
81*0Sstevel@tonic-gate 		return (-1);
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	if (extbuf) {
85*0Sstevel@tonic-gate 		/*
86*0Sstevel@tonic-gate 		 * The length of the buffer is the minimum of the length
87*0Sstevel@tonic-gate 		 * passed in and the length stamped onto the buffer.  The
88*0Sstevel@tonic-gate 		 * length stamped onto the buffer is the number of 8 byte
89*0Sstevel@tonic-gate 		 * octets in the buffer minus 1.
90*0Sstevel@tonic-gate 		 */
91*0Sstevel@tonic-gate 		extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	remainder = (offset + 2 + len) % align;
95*0Sstevel@tonic-gate 	if (remainder == 0) {
96*0Sstevel@tonic-gate 		padbytes = 0;
97*0Sstevel@tonic-gate 	} else {
98*0Sstevel@tonic-gate 		padbytes = align - remainder;
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	endlen = offset + padbytes + 2 + len;
102*0Sstevel@tonic-gate 	if ((endlen > extlen) || !extbuf) {
103*0Sstevel@tonic-gate 		if (extbuf) {
104*0Sstevel@tonic-gate 			return (-1);
105*0Sstevel@tonic-gate 		} else {
106*0Sstevel@tonic-gate 			return (endlen);
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	p = (uint8_t *)extbuf + offset;
111*0Sstevel@tonic-gate 	if (padbytes != 0) {
112*0Sstevel@tonic-gate 		/*
113*0Sstevel@tonic-gate 		 * Pad out the buffer here with pad options.  If its only
114*0Sstevel@tonic-gate 		 * one byte then there is a special TLV with no L or V, just
115*0Sstevel@tonic-gate 		 * a zero to say skip this byte.  For two bytes or more
116*0Sstevel@tonic-gate 		 * we have a special TLV with type 0 and length the number of
117*0Sstevel@tonic-gate 		 * padbytes.
118*0Sstevel@tonic-gate 		 */
119*0Sstevel@tonic-gate 		if (padbytes == 1) {
120*0Sstevel@tonic-gate 			*p = IP6OPT_PAD1;
121*0Sstevel@tonic-gate 		} else {
122*0Sstevel@tonic-gate 			*p = IP6OPT_PADN;
123*0Sstevel@tonic-gate 			*(p + 1) = padbytes - 2;
124*0Sstevel@tonic-gate 			memset(p + 2, 0, padbytes - 2);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		p += padbytes;
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	*p++ = type;
130*0Sstevel@tonic-gate 	*p++ = len;
131*0Sstevel@tonic-gate 	if (databufp) {
132*0Sstevel@tonic-gate 		*databufp = p;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	return (endlen);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate  * Section 10.3 RFC3542.  This function returns the updated total length.
139*0Sstevel@tonic-gate  * This functions inserts pad options to complete the option header as
140*0Sstevel@tonic-gate  * needed.
141*0Sstevel@tonic-gate  */
142*0Sstevel@tonic-gate int
inet6_opt_finish(void * extbuf,socklen_t extlen,int offset)143*0Sstevel@tonic-gate inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	uint8_t *p;
146*0Sstevel@tonic-gate 	int padbytes;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (extbuf) {
149*0Sstevel@tonic-gate 	/*
150*0Sstevel@tonic-gate 	 * The length of the buffer is the minimum of the length
151*0Sstevel@tonic-gate 	 * passed in and the length stamped onto the buffer.  The
152*0Sstevel@tonic-gate 	 * length stamped onto the buffer is the number of 8 byte
153*0Sstevel@tonic-gate 	 * octets in the buffer minus 1.
154*0Sstevel@tonic-gate 	 */
155*0Sstevel@tonic-gate 		extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	padbytes = 8 - (offset % 8);
159*0Sstevel@tonic-gate 	if (padbytes == 8)
160*0Sstevel@tonic-gate 		padbytes = 0;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	if ((offset + padbytes > extlen) || !extbuf) {
163*0Sstevel@tonic-gate 		if (extbuf) {
164*0Sstevel@tonic-gate 			return (-1);
165*0Sstevel@tonic-gate 		} else {
166*0Sstevel@tonic-gate 			return (offset + padbytes);
167*0Sstevel@tonic-gate 		}
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	p = (uint8_t *)extbuf + offset;
171*0Sstevel@tonic-gate 	if (padbytes != 0) {
172*0Sstevel@tonic-gate 		/*
173*0Sstevel@tonic-gate 		 * Pad out the buffer here with pad options.  If its only
174*0Sstevel@tonic-gate 		 * one byte then there is a special TLV with no L or V, just
175*0Sstevel@tonic-gate 		 * a zero to say skip this byte.  For two bytes or more
176*0Sstevel@tonic-gate 		 * we have a special TLV with type 0 and length the number of
177*0Sstevel@tonic-gate 		 * padbytes.
178*0Sstevel@tonic-gate 		 */
179*0Sstevel@tonic-gate 		if (padbytes == 1) {
180*0Sstevel@tonic-gate 			*p = IP6OPT_PAD1;
181*0Sstevel@tonic-gate 		} else {
182*0Sstevel@tonic-gate 			*p = IP6OPT_PADN;
183*0Sstevel@tonic-gate 			*(p + 1) = padbytes - 2;
184*0Sstevel@tonic-gate 			memset(p + 2, 0, padbytes - 2);
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 		p += padbytes;
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	return (offset + padbytes);
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate /*
193*0Sstevel@tonic-gate  * Section 10.4 RFC3542.  Ths function takes a pointer to the data as
194*0Sstevel@tonic-gate  * returned by inet6_opt_append and inserts the data.
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate int
inet6_opt_set_val(void * databuf,int offset,void * val,socklen_t vallen)197*0Sstevel@tonic-gate inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	memcpy((uint8_t *)databuf + offset, val, vallen);
200*0Sstevel@tonic-gate 	return (offset + vallen);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate  * Section 10.5 RFC 3542.  Starting walking the option header offset into the
205*0Sstevel@tonic-gate  * header.  Returns where we left off.  You take the output of this function
206*0Sstevel@tonic-gate  * and pass it back in as offset to iterate. -1 is returned on error.
207*0Sstevel@tonic-gate  *
208*0Sstevel@tonic-gate  * We use the fact that the first unsigned 8 bit quantity in the option
209*0Sstevel@tonic-gate  * header is the type and the second is the length.
210*0Sstevel@tonic-gate  */
211*0Sstevel@tonic-gate int
inet6_opt_next(void * extbuf,socklen_t extlen,int offset,uint8_t * typep,socklen_t * lenp,void ** databufp)212*0Sstevel@tonic-gate inet6_opt_next(void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
213*0Sstevel@tonic-gate 	socklen_t *lenp, void **databufp)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	uint8_t *p;
216*0Sstevel@tonic-gate 	uint8_t *end;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/*
219*0Sstevel@tonic-gate 	 * The length of the buffer is the minimum of the length
220*0Sstevel@tonic-gate 	 * passed in and the length stamped onto the buffer.  The
221*0Sstevel@tonic-gate 	 * length stamped onto the buffer is the number of 8 byte
222*0Sstevel@tonic-gate 	 * octets in the buffer minus 1.
223*0Sstevel@tonic-gate 	 */
224*0Sstevel@tonic-gate 	extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
225*0Sstevel@tonic-gate 	end = (uint8_t *)extbuf + extlen;
226*0Sstevel@tonic-gate 	if (offset == 0) {
227*0Sstevel@tonic-gate 		offset = 2;
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	/* assumption: IP6OPT_PAD1 == 0 and IP6OPT_PADN == 1 */
231*0Sstevel@tonic-gate 	p = (uint8_t *)extbuf + offset;
232*0Sstevel@tonic-gate 	while (*p == IP6OPT_PAD1 || *p == IP6OPT_PADN) {
233*0Sstevel@tonic-gate 		switch (*p) {
234*0Sstevel@tonic-gate 		case IP6OPT_PAD1:
235*0Sstevel@tonic-gate 			p++;
236*0Sstevel@tonic-gate 			break;
237*0Sstevel@tonic-gate 		case IP6OPT_PADN:
238*0Sstevel@tonic-gate 			/* *(p + 1) is the length of the option. */
239*0Sstevel@tonic-gate 			if (p + 2 + *(p + 1) >= end)
240*0Sstevel@tonic-gate 				return (-1);
241*0Sstevel@tonic-gate 			p += *(p + 1) + 2;
242*0Sstevel@tonic-gate 			break;
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/* type, len, and data must fit... */
247*0Sstevel@tonic-gate 	if ((p + 2 >= end) || (p + 2 + *(p + 1) > end)) {
248*0Sstevel@tonic-gate 		return (-1);
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (typep) {
252*0Sstevel@tonic-gate 		*typep = *p;
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	if (lenp) {
255*0Sstevel@tonic-gate 		*lenp = *(p + 1);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 	if (databufp) {
258*0Sstevel@tonic-gate 		*databufp = p + 2;
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	return ((p - (uint8_t *)extbuf) + 2 + *lenp);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate  * Section 10.6 RFC 3542.  Starting walking the option header offset into the
266*0Sstevel@tonic-gate  * header.  Returns where we left off.  You take the output of this function
267*0Sstevel@tonic-gate  * and pass it back in as offset to iterate. -1 is returned on error.
268*0Sstevel@tonic-gate  *
269*0Sstevel@tonic-gate  * We use the fact that the first unsigned 8 bit quantity in the option
270*0Sstevel@tonic-gate  * header is the type and the second is the length.
271*0Sstevel@tonic-gate  */
272*0Sstevel@tonic-gate int
inet6_opt_find(void * extbuf,socklen_t extlen,int offset,uint8_t type,socklen_t * lenp,void ** databufp)273*0Sstevel@tonic-gate inet6_opt_find(void *extbuf, socklen_t extlen, int offset, uint8_t type,
274*0Sstevel@tonic-gate 	socklen_t *lenp, void **databufp)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	uint8_t newtype;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	do {
279*0Sstevel@tonic-gate 		offset = inet6_opt_next(extbuf, extlen, offset, &newtype, lenp,
280*0Sstevel@tonic-gate 		    databufp);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 		if (offset == -1)
283*0Sstevel@tonic-gate 			return (-1);
284*0Sstevel@tonic-gate 	} while (newtype != type);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/* value to feed back into inet6_opt_find() as offset */
287*0Sstevel@tonic-gate 	return (offset);
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate  * Section 10.7 RFC 3542.  databuf should be a pointer as returned by
292*0Sstevel@tonic-gate  * inet6_opt_next or inet6_opt_find.  The data is extracted from the option
293*0Sstevel@tonic-gate  * at that point.
294*0Sstevel@tonic-gate  */
295*0Sstevel@tonic-gate int
inet6_opt_get_val(void * databuf,int offset,void * val,socklen_t vallen)296*0Sstevel@tonic-gate inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
297*0Sstevel@tonic-gate {
298*0Sstevel@tonic-gate 	memcpy(val, (uint8_t *)databuf + offset, vallen);
299*0Sstevel@tonic-gate 	return (offset + vallen);
300*0Sstevel@tonic-gate }
301