ip6opt.c revision 121472
1/* $KAME: ip6opt.c,v 1.13 2003/06/06 10:08:20 suz 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/cdefs.h> 33__FBSDID("$FreeBSD: head/lib/libc/net/ip6opt.c 121472 2003-10-24 18:26:30Z ume $"); 34 35#include <sys/param.h> 36#include <sys/types.h> 37#include <sys/socket.h> 38 39#include <netinet/in.h> 40#include <netinet/ip6.h> 41 42#include <string.h> 43#include <stdio.h> 44 45static int ip6optlen(u_int8_t *opt, u_int8_t *lim); 46static void inet6_insert_padopt(u_char *p, int len); 47 48/* 49 * This function returns the number of bytes required to hold an option 50 * when it is stored as ancillary data, including the cmsghdr structure 51 * at the beginning, and any padding at the end (to make its size a 52 * multiple of 8 bytes). The argument is the size of the structure 53 * defining the option, which must include any pad bytes at the 54 * beginning (the value y in the alignment term "xn + y"), the type 55 * byte, the length byte, and the option data. 56 */ 57int 58inet6_option_space(nbytes) 59 int nbytes; 60{ 61 nbytes += 2; /* we need space for nxt-hdr and length fields */ 62 return(CMSG_SPACE((nbytes + 7) & ~7)); 63} 64 65/* 66 * This function is called once per ancillary data object that will 67 * contain either Hop-by-Hop or Destination options. It returns 0 on 68 * success or -1 on an error. 69 */ 70int 71inet6_option_init(bp, cmsgp, type) 72 void *bp; 73 struct cmsghdr **cmsgp; 74 int type; 75{ 76 struct cmsghdr *ch = (struct cmsghdr *)bp; 77 78 /* argument validation */ 79 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) 80 return(-1); 81 82 ch->cmsg_level = IPPROTO_IPV6; 83 ch->cmsg_type = type; 84 ch->cmsg_len = CMSG_LEN(0); 85 86 *cmsgp = ch; 87 return(0); 88} 89 90/* 91 * This function appends a Hop-by-Hop option or a Destination option 92 * into an ancillary data object that has been initialized by 93 * inet6_option_init(). This function returns 0 if it succeeds or -1 on 94 * an error. 95 * multx is the value x in the alignment term "xn + y" described 96 * earlier. It must have a value of 1, 2, 4, or 8. 97 * plusy is the value y in the alignment term "xn + y" described 98 * earlier. It must have a value between 0 and 7, inclusive. 99 */ 100int 101inet6_option_append(cmsg, typep, multx, plusy) 102 struct cmsghdr *cmsg; 103 const u_int8_t *typep; 104 int multx; 105 int plusy; 106{ 107 int padlen, optlen, off; 108 u_char *bp = (u_char *)cmsg + cmsg->cmsg_len; 109 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); 110 111 /* argument validation */ 112 if (multx != 1 && multx != 2 && multx != 4 && multx != 8) 113 return(-1); 114 if (plusy < 0 || plusy > 7) 115 return(-1); 116 if (typep[0] > 255) 117 return(-1); 118 119 /* 120 * If this is the first option, allocate space for the 121 * first 2 bytes(for next header and length fields) of 122 * the option header. 123 */ 124 if (bp == (u_char *)eh) { 125 bp += 2; 126 cmsg->cmsg_len += 2; 127 } 128 129 /* calculate pad length before the option. */ 130 off = bp - (u_char *)eh; 131 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - 132 (off % multx); 133 padlen += plusy; 134 padlen %= multx; /* keep the pad as short as possible */ 135 /* insert padding */ 136 inet6_insert_padopt(bp, padlen); 137 cmsg->cmsg_len += padlen; 138 bp += padlen; 139 140 /* copy the option */ 141 if (typep[0] == IP6OPT_PAD1) 142 optlen = 1; 143 else 144 optlen = typep[1] + 2; 145 memcpy(bp, typep, optlen); 146 bp += optlen; 147 cmsg->cmsg_len += optlen; 148 149 /* calculate pad length after the option and insert the padding */ 150 off = bp - (u_char *)eh; 151 padlen = ((off + 7) & ~7) - off; 152 inet6_insert_padopt(bp, padlen); 153 bp += padlen; 154 cmsg->cmsg_len += padlen; 155 156 /* update the length field of the ip6 option header */ 157 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; 158 159 return(0); 160} 161 162/* 163 * This function appends a Hop-by-Hop option or a Destination option 164 * into an ancillary data object that has been initialized by 165 * inet6_option_init(). This function returns a pointer to the 8-bit 166 * option type field that starts the option on success, or NULL on an 167 * error. 168 * The difference between this function and inet6_option_append() is 169 * that the latter copies the contents of a previously built option into 170 * the ancillary data object while the current function returns a 171 * pointer to the space in the data object where the option's TLV must 172 * then be built by the caller. 173 * 174 */ 175u_int8_t * 176inet6_option_alloc(cmsg, datalen, multx, plusy) 177 struct cmsghdr *cmsg; 178 int datalen; 179 int multx; 180 int plusy; 181{ 182 int padlen, off; 183 u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len; 184 u_int8_t *retval; 185 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); 186 187 /* argument validation */ 188 if (multx != 1 && multx != 2 && multx != 4 && multx != 8) 189 return(NULL); 190 if (plusy < 0 || plusy > 7) 191 return(NULL); 192 193 /* 194 * If this is the first option, allocate space for the 195 * first 2 bytes(for next header and length fields) of 196 * the option header. 197 */ 198 if (bp == (u_char *)eh) { 199 bp += 2; 200 cmsg->cmsg_len += 2; 201 } 202 203 /* calculate pad length before the option. */ 204 off = bp - (u_char *)eh; 205 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - 206 (off % multx); 207 padlen += plusy; 208 padlen %= multx; /* keep the pad as short as possible */ 209 /* insert padding */ 210 inet6_insert_padopt(bp, padlen); 211 cmsg->cmsg_len += padlen; 212 bp += padlen; 213 214 /* keep space to store specified length of data */ 215 retval = bp; 216 bp += datalen; 217 cmsg->cmsg_len += datalen; 218 219 /* calculate pad length after the option and insert the padding */ 220 off = bp - (u_char *)eh; 221 padlen = ((off + 7) & ~7) - off; 222 inet6_insert_padopt(bp, padlen); 223 bp += padlen; 224 cmsg->cmsg_len += padlen; 225 226 /* update the length field of the ip6 option header */ 227 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; 228 229 return(retval); 230} 231 232/* 233 * This function processes the next Hop-by-Hop option or Destination 234 * option in an ancillary data object. If another option remains to be 235 * processed, the return value of the function is 0 and *tptrp points to 236 * the 8-bit option type field (which is followed by the 8-bit option 237 * data length, followed by the option data). If no more options remain 238 * to be processed, the return value is -1 and *tptrp is NULL. If an 239 * error occurs, the return value is -1 and *tptrp is not NULL. 240 * (RFC 2292, 6.3.5) 241 */ 242int 243inet6_option_next(cmsg, tptrp) 244 const struct cmsghdr *cmsg; 245 u_int8_t **tptrp; 246{ 247 struct ip6_ext *ip6e; 248 int hdrlen, optlen; 249 u_int8_t *lim; 250 251 if (cmsg->cmsg_level != IPPROTO_IPV6 || 252 (cmsg->cmsg_type != IPV6_HOPOPTS && 253 cmsg->cmsg_type != IPV6_DSTOPTS)) 254 return(-1); 255 256 /* message length validation */ 257 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) 258 return(-1); 259 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); 260 hdrlen = (ip6e->ip6e_len + 1) << 3; 261 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) 262 return(-1); 263 264 /* 265 * If the caller does not specify the starting point, 266 * simply return the 1st option. 267 * Otherwise, search the option list for the next option. 268 */ 269 lim = (u_int8_t *)ip6e + hdrlen; 270 if (*tptrp == NULL) 271 *tptrp = (u_int8_t *)(ip6e + 1); 272 else { 273 if ((optlen = ip6optlen(*tptrp, lim)) == 0) 274 return(-1); 275 276 *tptrp = *tptrp + optlen; 277 } 278 if (*tptrp >= lim) { /* there is no option */ 279 *tptrp = NULL; 280 return(-1); 281 } 282 /* 283 * Finally, checks if the next option is safely stored in the 284 * cmsg data. 285 */ 286 if (ip6optlen(*tptrp, lim) == 0) 287 return(-1); 288 else 289 return(0); 290} 291 292/* 293 * This function is similar to the inet6_option_next() function, 294 * except this function lets the caller specify the option type to be 295 * searched for, instead of always returning the next option in the 296 * ancillary data object. 297 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think 298 * it's a typo. The variable should be type of u_int8_t **. 299 */ 300int 301inet6_option_find(cmsg, tptrp, type) 302 const struct cmsghdr *cmsg; 303 u_int8_t **tptrp; 304 int type; 305{ 306 struct ip6_ext *ip6e; 307 int hdrlen, optlen; 308 u_int8_t *optp, *lim; 309 310 if (cmsg->cmsg_level != IPPROTO_IPV6 || 311 (cmsg->cmsg_type != IPV6_HOPOPTS && 312 cmsg->cmsg_type != IPV6_DSTOPTS)) 313 return(-1); 314 315 /* message length validation */ 316 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) 317 return(-1); 318 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); 319 hdrlen = (ip6e->ip6e_len + 1) << 3; 320 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) 321 return(-1); 322 323 /* 324 * If the caller does not specify the starting point, 325 * search from the beginning of the option list. 326 * Otherwise, search from *the next option* of the specified point. 327 */ 328 lim = (u_int8_t *)ip6e + hdrlen; 329 if (*tptrp == NULL) 330 *tptrp = (u_int8_t *)(ip6e + 1); 331 else { 332 if ((optlen = ip6optlen(*tptrp, lim)) == 0) 333 return(-1); 334 335 *tptrp = *tptrp + optlen; 336 } 337 for (optp = *tptrp; optp < lim; optp += optlen) { 338 if (*optp == type) { 339 *tptrp = optp; 340 return(0); 341 } 342 if ((optlen = ip6optlen(optp, lim)) == 0) 343 return(-1); 344 } 345 346 /* search failed */ 347 *tptrp = NULL; 348 return(-1); 349} 350 351/* 352 * Calculate the length of a given IPv6 option. Also checks 353 * if the option is safely stored in user's buffer according to the 354 * calculated length and the limitation of the buffer. 355 */ 356static int 357ip6optlen(opt, lim) 358 u_int8_t *opt, *lim; 359{ 360 int optlen; 361 362 if (*opt == IP6OPT_PAD1) 363 optlen = 1; 364 else { 365 /* is there enough space to store type and len? */ 366 if (opt + 2 > lim) 367 return(0); 368 optlen = *(opt + 1) + 2; 369 } 370 if (opt + optlen <= lim) 371 return(optlen); 372 373 return(0); 374} 375 376static void 377inet6_insert_padopt(u_char *p, int len) 378{ 379 switch(len) { 380 case 0: 381 return; 382 case 1: 383 p[0] = IP6OPT_PAD1; 384 return; 385 default: 386 p[0] = IP6OPT_PADN; 387 p[1] = len - 2; 388 memset(&p[2], 0, len - 2); 389 return; 390 } 391} 392 393/* 394 * The following functions are defined in a successor of RFC2292, aka 395 * rfc2292bis. 396 */ 397 398int 399inet6_opt_init(void *extbuf, socklen_t extlen) 400{ 401 struct ip6_ext *ext = (struct ip6_ext *)extbuf; 402 403 if (extlen < 0 || (extlen % 8)) 404 return(-1); 405 406 if (ext) { 407 if (extlen == 0) 408 return(-1); 409 ext->ip6e_len = (extlen >> 3) - 1; 410 } 411 412 return(2); /* sizeof the next and the length fields */ 413} 414 415int 416inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type, 417 socklen_t len, u_int8_t align, void **databufp) 418{ 419 int currentlen = offset, padlen = 0; 420 421 /* 422 * The option type must have a value from 2 to 255, inclusive. 423 * (0 and 1 are reserved for the Pad1 and PadN options, respectively.) 424 */ 425 if (type < 2 || type > 255) 426 return(-1); 427 428 /* 429 * The option data length must have a value between 0 and 255, 430 * inclusive, and is the length of the option data that follows. 431 */ 432 if (len < 0 || len > 255) 433 return(-1); 434 435 /* 436 * The align parameter must have a value of 1, 2, 4, or 8. 437 * The align value can not exceed the value of len. 438 */ 439 if (align != 1 && align != 2 && align != 4 && align != 8) 440 return(-1); 441 if (align > len) 442 return(-1); 443 444 /* Calculate the padding length. */ 445 currentlen += 2 + len; /* 2 means "type + len" */ 446 if (currentlen % align) 447 padlen = align - (currentlen % align); 448 449 /* The option must fit in the extension header buffer. */ 450 currentlen += padlen; 451 if (extlen && /* XXX: right? */ 452 currentlen > extlen) 453 return(-1); 454 455 if (extbuf) { 456 u_int8_t *optp = (u_int8_t *)extbuf + offset; 457 458 if (padlen == 1) { 459 /* insert a Pad1 option */ 460 *optp = IP6OPT_PAD1; 461 optp++; 462 } 463 else if (padlen > 0) { 464 /* insert a PadN option for alignment */ 465 *optp++ = IP6OPT_PADN; 466 *optp++ = padlen - 2; 467 memset(optp, 0, padlen - 2); 468 optp += (padlen - 2); 469 } 470 471 *optp++ = type; 472 *optp++ = len; 473 474 *databufp = optp; 475 } 476 477 return(currentlen); 478} 479 480int 481inet6_opt_finish(void *extbuf, socklen_t extlen, int offset) 482{ 483 int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;; 484 485 if (extbuf) { 486 u_int8_t *padp; 487 int padlen = updatelen - offset; 488 489 if (updatelen > extlen) 490 return(-1); 491 492 padp = (u_int8_t *)extbuf + offset; 493 if (padlen == 1) 494 *padp = IP6OPT_PAD1; 495 else if (padlen > 0) { 496 *padp++ = IP6OPT_PADN; 497 *padp++ = (padlen - 2); 498 memset(padp, 0, padlen - 2); 499 } 500 } 501 502 return(updatelen); 503} 504 505int 506inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen) 507{ 508 509 memcpy((u_int8_t *)databuf + offset, val, vallen); 510 return(offset + vallen); 511} 512 513int 514inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep, 515 size_t *lenp, void **databufp) 516{ 517 u_int8_t *optp, *lim; 518 int optlen; 519 520 /* Validate extlen. XXX: is the variable really necessary?? */ 521 if (extlen == 0 || (extlen % 8)) 522 return(-1); 523 lim = (u_int8_t *)extbuf + extlen; 524 525 /* 526 * If this is the first time this function called for this options 527 * header, simply return the 1st option. 528 * Otherwise, search the option list for the next option. 529 */ 530 if (offset == 0) { 531 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1); 532 } 533 else 534 optp = (u_int8_t *)extbuf + offset; 535 536 /* Find the next option skipping any padding options. */ 537 while(optp < lim) { 538 switch(*optp) { 539 case IP6OPT_PAD1: 540 optp++; 541 break; 542 case IP6OPT_PADN: 543 if ((optlen = ip6optlen(optp, lim)) == 0) 544 goto optend; 545 optp += optlen; 546 break; 547 default: /* found */ 548 if ((optlen = ip6optlen(optp, lim)) == 0) 549 goto optend; 550 *typep = *optp; 551 *lenp = optlen - 2; 552 *databufp = optp + 2; 553 return(optp + optlen - (u_int8_t *)extbuf); 554 } 555 } 556 557 optend: 558 *databufp = NULL; /* for safety */ 559 return(-1); 560} 561 562int 563inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type, 564 socklen_t *lenp, void **databufp) 565{ 566 u_int8_t *optp, *lim; 567 int optlen; 568 569 /* Validate extlen. XXX: is the variable really necessary?? */ 570 if (extlen == 0 || (extlen % 8)) 571 return(-1); 572 lim = (u_int8_t *)extbuf + extlen; 573 574 /* 575 * If this is the first time this function called for this options 576 * header, simply return the 1st option. 577 * Otherwise, search the option list for the next option. 578 */ 579 if (offset == 0) { 580 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1); 581 } 582 else 583 optp = (u_int8_t *)extbuf + offset; 584 585 /* Find the specified option */ 586 while(optp < lim) { 587 if ((optlen = ip6optlen(optp, lim)) == 0) 588 goto optend; 589 590 if (*optp == type) { /* found */ 591 *lenp = optlen - 2; 592 *databufp = optp + 2; 593 return(optp + optlen - (u_int8_t *)extbuf); 594 } 595 596 optp += optlen; 597 } 598 599 optend: 600 *databufp = NULL; /* for safety */ 601 return(-1); 602} 603 604int 605inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen) 606{ 607 608 /* we can't assume alignment here */ 609 memcpy(val, (u_int8_t *)databuf + offset, vallen); 610 611 return(offset + vallen); 612} 613