xref: /netbsd-src/lib/libc/net/ip6opt.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: ip6opt.c,v 1.4 1999/09/20 04:39:15 lukem Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <netinet/in.h>
37 #include <netinet/ip6.h>
38 
39 #include <assert.h>
40 #include <string.h>
41 #include <stdio.h>
42 
43 static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
44 static void inet6_insert_padopt(u_char *p, int len);
45 
46 /*
47  * This function returns the number of bytes required to hold an option
48  * when it is stored as ancillary data, including the cmsghdr structure
49  * at the beginning, and any padding at the end (to make its size a
50  * multiple of 8 bytes).  The argument is the size of the structure
51  * defining the option, which must include any pad bytes at the
52  * beginning (the value y in the alignment term "xn + y"), the type
53  * byte, the length byte, and the option data.
54  */
55 int
56 inet6_option_space(nbytes)
57 	int nbytes;
58 {
59 	nbytes += 2;	/* we need space for nxt-hdr and length fields */
60 	return(CMSG_SPACE((nbytes + 7) & ~7));
61 }
62 
63 /*
64  * This function is called once per ancillary data object that will
65  * contain either Hop-by-Hop or Destination options.  It returns 0 on
66  * success or -1 on an error.
67  */
68 int
69 inet6_option_init(bp, cmsgp, type)
70 	void *bp;
71 	struct cmsghdr **cmsgp;
72 	int type;
73 {
74 	register struct cmsghdr *ch;
75 
76 	_DIAGASSERT(bp != NULL);
77 	_DIAGASSERT(cmsgp != NULL);
78 
79 	ch = (struct cmsghdr *)bp;
80 
81 	/* argument validation */
82 	if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
83 		return(-1);
84 
85 	ch->cmsg_level = IPPROTO_IPV6;
86 	ch->cmsg_type = type;
87 	ch->cmsg_len = CMSG_LEN(0);
88 
89 	*cmsgp = ch;
90 	return(0);
91 }
92 
93 /*
94  * This function appends a Hop-by-Hop option or a Destination option
95  * into an ancillary data object that has been initialized by
96  * inet6_option_init().  This function returns 0 if it succeeds or -1 on
97  * an error.
98  * multx is the value x in the alignment term "xn + y" described
99  * earlier.  It must have a value of 1, 2, 4, or 8.
100  * plusy is the value y in the alignment term "xn + y" described
101  * earlier.  It must have a value between 0 and 7, inclusive.
102  */
103 int
104 inet6_option_append(cmsg, typep, multx, plusy)
105 	struct cmsghdr *cmsg;
106 	const u_int8_t *typep;
107 	int multx;
108 	int plusy;
109 {
110 	int padlen, optlen, off;
111 	register u_char *bp;
112 	struct ip6_ext *eh;
113 
114 	_DIAGASSERT(cmsg != NULL);
115 	_DIAGASSERT(typep != NULL);
116 
117 	bp = (u_char *)cmsg + cmsg->cmsg_len;
118 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
119 
120 	/* argument validation */
121 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
122 		return(-1);
123 	if (plusy < 0 || plusy > 7)
124 		return(-1);
125 	if (typep[0] > 255)
126 		return(-1);
127 
128 	/*
129 	 * If this is the first option, allocate space for the
130 	 * first 2 bytes(for next header and length fields) of
131 	 * the option header.
132 	 */
133 	if (bp == (u_char *)eh) {
134 		bp += 2;
135 		cmsg->cmsg_len += 2;
136 	}
137 
138 	/* calculate pad length before the option. */
139 	off = bp - (u_char *)eh;
140 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
141 		(off % multx);
142 	padlen += plusy;
143 	/* insert padding */
144 	inet6_insert_padopt(bp, padlen);
145 	cmsg->cmsg_len += padlen;
146 	bp += padlen;
147 
148 	/* copy the option */
149 	if (typep[0] == IP6OPT_PAD1)
150 		optlen = 1;
151 	else
152 		optlen = typep[1] + 2;
153 	memcpy(bp, typep, optlen);
154 	bp += optlen;
155 	cmsg->cmsg_len += optlen;
156 
157 	/* calculate pad length after the option and insert the padding */
158 	off = bp - (u_char *)eh;
159 	padlen = ((off + 7) & ~7) - off;
160 	inet6_insert_padopt(bp, padlen);
161 	bp += padlen;
162 	cmsg->cmsg_len += padlen;
163 
164 	/* update the length field of the ip6 option header */
165 	eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
166 
167 	return(0);
168 }
169 
170 /*
171  * This function appends a Hop-by-Hop option or a Destination option
172  * into an ancillary data object that has been initialized by
173  * inet6_option_init().  This function returns a pointer to the 8-bit
174  * option type field that starts the option on success, or NULL on an
175  * error.
176  * The difference between this function and inet6_option_append() is
177  * that the latter copies the contents of a previously built option into
178  * the ancillary data object while the current function returns a
179  * pointer to the space in the data object where the option's TLV must
180  * then be built by the caller.
181  *
182  */
183 u_int8_t *
184 inet6_option_alloc(cmsg, datalen, multx, plusy)
185 	struct cmsghdr *cmsg;
186 	int datalen;
187 	int multx;
188 	int plusy;
189 {
190 	int padlen, off;
191 	register u_int8_t *bp;
192 	u_int8_t *retval;
193 	struct ip6_ext *eh;
194 
195 	_DIAGASSERT(cmsg != NULL);
196 
197 	bp = (u_char *)cmsg + cmsg->cmsg_len;
198 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
199 
200 	/* argument validation */
201 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
202 		return(NULL);
203 	if (plusy < 0 || plusy > 7)
204 		return(NULL);
205 
206 	/*
207 	 * If this is the first option, allocate space for the
208 	 * first 2 bytes(for next header and length fields) of
209 	 * the option header.
210 	 */
211 	if (bp == (u_char *)eh) {
212 		bp += 2;
213 		cmsg->cmsg_len += 2;
214 	}
215 
216 	/* calculate pad length before the option. */
217 	off = bp - (u_char *)eh;
218 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
219 		(off % multx);
220 	padlen += plusy;
221 	/* insert padding */
222 	inet6_insert_padopt(bp, padlen);
223 	cmsg->cmsg_len += padlen;
224 	bp += padlen;
225 
226 	/* keep space to store specified length of data */
227 	retval = bp;
228 	bp += datalen;
229 	cmsg->cmsg_len += datalen;
230 
231 	/* calculate pad length after the option and insert the padding */
232 	off = bp - (u_char *)eh;
233 	padlen = ((off + 7) & ~7) - off;
234 	inet6_insert_padopt(bp, padlen);
235 	bp += padlen;
236 	cmsg->cmsg_len += padlen;
237 
238 	/* update the length field of the ip6 option header */
239 	eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
240 
241 	return(retval);
242 }
243 
244 /*
245  * This function processes the next Hop-by-Hop option or Destination
246  * option in an ancillary data object.  If another option remains to be
247  * processed, the return value of the function is 0 and *tptrp points to
248  * the 8-bit option type field (which is followed by the 8-bit option
249  * data length, followed by the option data).  If no more options remain
250  * to be processed, the return value is -1 and *tptrp is NULL.  If an
251  * error occurs, the return value is -1 and *tptrp is not NULL.
252  * (RFC 2292, 6.3.5)
253  */
254 int
255 inet6_option_next(cmsg, tptrp)
256 	const struct cmsghdr *cmsg;
257 	u_int8_t **tptrp;
258 {
259 	struct ip6_ext *ip6e;
260 	int hdrlen, optlen;
261 	u_int8_t *lim;
262 
263 	_DIAGASSERT(cmsg != NULL);
264 	_DIAGASSERT(tptrp != NULL);
265 
266 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
267 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
268 	     cmsg->cmsg_type != IPV6_DSTOPTS))
269 		return(-1);
270 
271 	/* message length validation */
272 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
273 		return(-1);
274 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
275 	hdrlen = (ip6e->ip6e_len + 1) << 3;
276 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
277 		return(-1);
278 
279 	/*
280 	 * If the caller does not specify the starting point,
281 	 * simply return the 1st option.
282 	 * Otherwise, search the option list for the next option.
283 	 */
284 	lim = (u_int8_t *)ip6e + hdrlen;
285 	if (*tptrp == NULL)
286 		*tptrp = (u_int8_t *)(ip6e + 1);
287 	else {
288 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
289 			return(-1);
290 
291 		*tptrp = *tptrp + optlen;
292 	}
293 	if (*tptrp >= lim) {	/* there is no option */
294 		*tptrp = NULL;
295 		return(-1);
296 	}
297 	/*
298 	 * Finally, checks if the next option is safely stored in the
299 	 * cmsg data.
300 	 */
301 	if (ip6optlen(*tptrp, lim) == 0)
302 		return(-1);
303 	else
304 		return(0);
305 }
306 
307 /*
308  * This function is similar to the inet6_option_next() function,
309  * except this function lets the caller specify the option type to be
310  * searched for, instead of always returning the next option in the
311  * ancillary data object.
312  * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
313  *       it's a typo. The variable should be type of u_int8_t **.
314  */
315 int
316 inet6_option_find(cmsg, tptrp, type)
317 	const struct cmsghdr *cmsg;
318 	u_int8_t **tptrp;
319 	int type;
320 {
321 	struct ip6_ext *ip6e;
322 	int hdrlen, optlen;
323 	u_int8_t *optp, *lim;
324 
325 	_DIAGASSERT(cmsg != NULL);
326 	_DIAGASSERT(tptrp != NULL);
327 
328 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
329 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
330 	     cmsg->cmsg_type != IPV6_DSTOPTS))
331 		return(-1);
332 
333 	/* message length validation */
334 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
335 		return(-1);
336 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
337 	hdrlen = (ip6e->ip6e_len + 1) << 3;
338 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
339 		return(-1);
340 
341 	/*
342 	 * If the caller does not specify the starting point,
343 	 * search from the beginning of the option list.
344 	 * Otherwise, search from *the next option* of the specified point.
345 	 */
346 	lim = (u_int8_t *)ip6e + hdrlen;
347 	if (*tptrp == NULL)
348 		*tptrp = (u_int8_t *)(ip6e + 1);
349 	else {
350 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
351 			return(-1);
352 
353 		*tptrp = *tptrp + optlen;
354 	}
355 	for (optp = *tptrp; optp < lim; optp += optlen) {
356 		if (*optp == type) {
357 			*tptrp = optp;
358 			return(0);
359 		}
360 		if ((optlen = ip6optlen(optp, lim)) == 0)
361 			return(-1);
362 	}
363 
364 	/* search failed */
365 	*tptrp = NULL;
366 	return(-1);
367 }
368 
369 /*
370  * Calculate the length of a given IPv6 option. Also checks
371  * if the option is safely stored in user's buffer according to the
372  * calculated length and the limitation of the buffer.
373  */
374 static int
375 ip6optlen(opt, lim)
376 	u_int8_t *opt, *lim;
377 {
378 	int optlen;
379 
380 	_DIAGASSERT(opt != NULL);
381 	_DIAGASSERT(lim != NULL);
382 
383 	if (*opt == IP6OPT_PAD1)
384 		optlen = 1;
385 	else {
386 		/* is there enough space to store type and len? */
387 		if (opt + 2 > lim)
388 			return(0);
389 		optlen = *(opt + 1) + 2;
390 	}
391 	if (opt + optlen <= lim)
392 		return(optlen);
393 
394 	return(0);
395 }
396 
397 static void
398 inet6_insert_padopt(u_char *p, int len)
399 {
400 
401 	_DIAGASSERT(p != NULL);
402 
403 	switch(len) {
404 	 case 0:
405 		 return;
406 	 case 1:
407 		 p[0] = IP6OPT_PAD1;
408 		 return;
409 	 default:
410 		 p[0] = IP6OPT_PADN;
411 		 p[1] = len - 2;
412 		 memset(&p[2], 0, len - 2);
413 		 return;
414 	}
415 }
416