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