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