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