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