summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsocket/inet/inet6_opt.c
blob: 42b923c5b39b2cc70cddf5271353b82200a36b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This code is conformant to RFC 3542.
 */

#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <netinet/in.h>
#include <netinet/ip6.h>

#include <stdio.h>

#define	bufpos(p) ((p) - (uint8_t *)extbuf)

/*
 * Section 10.1 RFC3542.  This function returns the size of the empty
 * extension header.  If extbuf is not NULL then it initializes its length
 * field.  If extlen is invalid then -1 is returned.
 */
int
inet6_opt_init(void *extbuf, socklen_t extlen)
{
	if (extbuf && ((extlen < 0) || (extlen % 8))) {
		return (-1);
	}

	if (extbuf) {
		*(uint8_t *)extbuf = 0;
		*((uint8_t *)extbuf + 1) = extlen/8 - 1;
	}

	return (2);
}

/*
 * Section 10.2 RFC3542.  This function appends an option to an already
 * initialized option buffer.  inet6_opt_append() returns the total length
 * after adding the option.
 */
int
inet6_opt_append(void *extbuf, socklen_t extlen, int offset, uint8_t type,
	socklen_t len, uint_t align, void **databufp)
{
	uint8_t *p;
	socklen_t endlen;
	int remainder, padbytes;

	if (align > len ||
	    (align != 1 && align != 2 && align != 4 && align != 8) ||
	    len < 0 || len > 255 || type < 2) {
		return (-1);
	}

	if (extbuf) {
		/*
		 * The length of the buffer is the minimum of the length
		 * passed in and the length stamped onto the buffer.  The
		 * length stamped onto the buffer is the number of 8 byte
		 * octets in the buffer minus 1.
		 */
		extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
	}

	remainder = (offset + 2 + len) % align;
	if (remainder == 0) {
		padbytes = 0;
	} else {
		padbytes = align - remainder;
	}

	endlen = offset + padbytes + 2 + len;
	if ((endlen > extlen) || !extbuf) {
		if (extbuf) {
			return (-1);
		} else {
			return (endlen);
		}
	}

	p = (uint8_t *)extbuf + offset;
	if (padbytes != 0) {
		/*
		 * Pad out the buffer here with pad options.  If its only
		 * one byte then there is a special TLV with no L or V, just
		 * a zero to say skip this byte.  For two bytes or more
		 * we have a special TLV with type 0 and length the number of
		 * padbytes.
		 */
		if (padbytes == 1) {
			*p = IP6OPT_PAD1;
		} else {
			*p = IP6OPT_PADN;
			*(p + 1) = padbytes - 2;
			memset(p + 2, 0, padbytes - 2);
		}
		p += padbytes;
	}

	*p++ = type;
	*p++ = len;
	if (databufp) {
		*databufp = p;
	}
	return (endlen);
}

/*
 * Section 10.3 RFC3542.  This function returns the updated total length.
 * This functions inserts pad options to complete the option header as
 * needed.
 */
int
inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
{
	uint8_t *p;
	int padbytes;

	if (extbuf) {
	/*
	 * The length of the buffer is the minimum of the length
	 * passed in and the length stamped onto the buffer.  The
	 * length stamped onto the buffer is the number of 8 byte
	 * octets in the buffer minus 1.
	 */
		extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
	}

	padbytes = 8 - (offset % 8);
	if (padbytes == 8)
		padbytes = 0;

	if ((offset + padbytes > extlen) || !extbuf) {
		if (extbuf) {
			return (-1);
		} else {
			return (offset + padbytes);
		}
	}

	p = (uint8_t *)extbuf + offset;
	if (padbytes != 0) {
		/*
		 * Pad out the buffer here with pad options.  If its only
		 * one byte then there is a special TLV with no L or V, just
		 * a zero to say skip this byte.  For two bytes or more
		 * we have a special TLV with type 0 and length the number of
		 * padbytes.
		 */
		if (padbytes == 1) {
			*p = IP6OPT_PAD1;
		} else {
			*p = IP6OPT_PADN;
			*(p + 1) = padbytes - 2;
			memset(p + 2, 0, padbytes - 2);
		}
		p += padbytes;
	}

	return (offset + padbytes);
}

/*
 * Section 10.4 RFC3542.  Ths function takes a pointer to the data as
 * returned by inet6_opt_append and inserts the data.
 */
int
inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
{
	memcpy((uint8_t *)databuf + offset, val, vallen);
	return (offset + vallen);
}

/*
 * Section 10.5 RFC 3542.  Starting walking the option header offset into the
 * header.  Returns where we left off.  You take the output of this function
 * and pass it back in as offset to iterate. -1 is returned on error.
 *
 * We use the fact that the first unsigned 8 bit quantity in the option
 * header is the type and the second is the length.
 */
int
inet6_opt_next(void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
	socklen_t *lenp, void **databufp)
{
	uint8_t *p;
	uint8_t *end;

	/*
	 * The length of the buffer is the minimum of the length
	 * passed in and the length stamped onto the buffer.  The
	 * length stamped onto the buffer is the number of 8 byte
	 * octets in the buffer minus 1.
	 */
	extlen = MIN(extlen, (*((uint8_t *)extbuf + 1) + 1) * 8);
	end = (uint8_t *)extbuf + extlen;
	if (offset == 0) {
		offset = 2;
	}

	/* assumption: IP6OPT_PAD1 == 0 and IP6OPT_PADN == 1 */
	p = (uint8_t *)extbuf + offset;
	while (*p == IP6OPT_PAD1 || *p == IP6OPT_PADN) {
		switch (*p) {
		case IP6OPT_PAD1:
			p++;
			break;
		case IP6OPT_PADN:
			/* *(p + 1) is the length of the option. */
			if (p + 2 + *(p + 1) >= end)
				return (-1);
			p += *(p + 1) + 2;
			break;
		}
	}

	/* type, len, and data must fit... */
	if ((p + 2 >= end) || (p + 2 + *(p + 1) > end)) {
		return (-1);
	}

	if (typep) {
		*typep = *p;
	}
	if (lenp) {
		*lenp = *(p + 1);
	}
	if (databufp) {
		*databufp = p + 2;
	}

	return ((p - (uint8_t *)extbuf) + 2 + *lenp);
}

/*
 * Section 10.6 RFC 3542.  Starting walking the option header offset into the
 * header.  Returns where we left off.  You take the output of this function
 * and pass it back in as offset to iterate. -1 is returned on error.
 *
 * We use the fact that the first unsigned 8 bit quantity in the option
 * header is the type and the second is the length.
 */
int
inet6_opt_find(void *extbuf, socklen_t extlen, int offset, uint8_t type,
	socklen_t *lenp, void **databufp)
{
	uint8_t newtype;

	do {
		offset = inet6_opt_next(extbuf, extlen, offset, &newtype, lenp,
		    databufp);

		if (offset == -1)
			return (-1);
	} while (newtype != type);

	/* value to feed back into inet6_opt_find() as offset */
	return (offset);
}

/*
 * Section 10.7 RFC 3542.  databuf should be a pointer as returned by
 * inet6_opt_next or inet6_opt_find.  The data is extracted from the option
 * at that point.
 */
int
inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
{
	memcpy(val, (uint8_t *)databuf + offset, vallen);
	return (offset + vallen);
}