1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <sys/stropts.h> 27 #include <sys/debug.h> 28 #include <sys/isa_defs.h> 29 #include <sys/int_limits.h> 30 #include <sys/nvpair.h> 31 #include <sys/nvpair_impl.h> 32 #include <rpc/types.h> 33 #include <rpc/xdr.h> 34 35 #if defined(_KERNEL) && !defined(_BOOT) 36 #include <sys/varargs.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/sysmacros.h> 40 #else 41 #include <stdarg.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <strings.h> 45 #include <stddef.h> 46 #endif 47 48 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++ 49 50 /* 51 * nvpair.c - Provides kernel & userland interfaces for manipulating 52 * name-value pairs. 53 * 54 * Overview Diagram 55 * 56 * +--------------+ 57 * | nvlist_t | 58 * |--------------| 59 * | nvl_version | 60 * | nvl_nvflag | 61 * | nvl_priv -+-+ 62 * | nvl_flag | | 63 * | nvl_pad | | 64 * +--------------+ | 65 * V 66 * +--------------+ last i_nvp in list 67 * | nvpriv_t | +---------------------> 68 * |--------------| | 69 * +--+- nvp_list | | +------------+ 70 * | | nvp_last -+--+ + nv_alloc_t | 71 * | | nvp_curr | |------------| 72 * | | nvp_nva -+----> | nva_ops | 73 * | | nvp_stat | | nva_arg | 74 * | +--------------+ +------------+ 75 * | 76 * +-------+ 77 * V 78 * +---------------------+ +-------------------+ 79 * | i_nvp_t | +-->| i_nvp_t | +--> 80 * |---------------------| | |-------------------| | 81 * | nvi_next -+--+ | nvi_next -+--+ 82 * | nvi_prev (NULL) | <----+ nvi_prev | 83 * | . . . . . . . . . . | | . . . . . . . . . | 84 * | nvp (nvpair_t) | | nvp (nvpair_t) | 85 * | - nvp_size | | - nvp_size | 86 * | - nvp_name_sz | | - nvp_name_sz | 87 * | - nvp_value_elem | | - nvp_value_elem | 88 * | - nvp_type | | - nvp_type | 89 * | - data ... | | - data ... | 90 * +---------------------+ +-------------------+ 91 * 92 * 93 * 94 * +---------------------+ +---------------------+ 95 * | i_nvp_t | +--> +-->| i_nvp_t (last) | 96 * |---------------------| | | |---------------------| 97 * | nvi_next -+--+ ... --+ | nvi_next (NULL) | 98 * <-+- nvi_prev |<-- ... <----+ nvi_prev | 99 * | . . . . . . . . . | | . . . . . . . . . | 100 * | nvp (nvpair_t) | | nvp (nvpair_t) | 101 * | - nvp_size | | - nvp_size | 102 * | - nvp_name_sz | | - nvp_name_sz | 103 * | - nvp_value_elem | | - nvp_value_elem | 104 * | - DATA_TYPE_NVLIST | | - nvp_type | 105 * | - data (embedded) | | - data ... | 106 * | nvlist name | +---------------------+ 107 * | +--------------+ | 108 * | | nvlist_t | | 109 * | |--------------| | 110 * | | nvl_version | | 111 * | | nvl_nvflag | | 112 * | | nvl_priv --+---+----> 113 * | | nvl_flag | | 114 * | | nvl_pad | | 115 * | +--------------+ | 116 * +---------------------+ 117 * 118 * 119 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will 120 * allow value to be aligned on 8 byte boundary 121 * 122 * name_len is the length of the name string including the null terminator 123 * so it must be >= 1 124 */ 125 #define NVP_SIZE_CALC(name_len, data_len) \ 126 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len)) 127 128 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem); 129 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type, 130 uint_t nelem, const void *data); 131 132 #define NV_STAT_EMBEDDED 0x1 133 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp)) 134 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp)) 135 136 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz)) 137 #define NVPAIR2I_NVP(nvp) \ 138 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp))) 139 140 141 int 142 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...) 143 { 144 va_list valist; 145 int err = 0; 146 147 nva->nva_ops = nvo; 148 nva->nva_arg = NULL; 149 150 va_start(valist, nvo); 151 if (nva->nva_ops->nv_ao_init != NULL) 152 err = nva->nva_ops->nv_ao_init(nva, valist); 153 va_end(valist); 154 155 return (err); 156 } 157 158 void 159 nv_alloc_reset(nv_alloc_t *nva) 160 { 161 if (nva->nva_ops->nv_ao_reset != NULL) 162 nva->nva_ops->nv_ao_reset(nva); 163 } 164 165 void 166 nv_alloc_fini(nv_alloc_t *nva) 167 { 168 if (nva->nva_ops->nv_ao_fini != NULL) 169 nva->nva_ops->nv_ao_fini(nva); 170 } 171 172 nv_alloc_t * 173 nvlist_lookup_nv_alloc(nvlist_t *nvl) 174 { 175 nvpriv_t *priv; 176 177 if (nvl == NULL || 178 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 179 return (NULL); 180 181 return (priv->nvp_nva); 182 } 183 184 static void * 185 nv_mem_zalloc(nvpriv_t *nvp, size_t size) 186 { 187 nv_alloc_t *nva = nvp->nvp_nva; 188 void *buf; 189 190 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL) 191 bzero(buf, size); 192 193 return (buf); 194 } 195 196 static void 197 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size) 198 { 199 nv_alloc_t *nva = nvp->nvp_nva; 200 201 nva->nva_ops->nv_ao_free(nva, buf, size); 202 } 203 204 static void 205 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat) 206 { 207 bzero(priv, sizeof (nvpriv_t)); 208 209 priv->nvp_nva = nva; 210 priv->nvp_stat = stat; 211 } 212 213 static nvpriv_t * 214 nv_priv_alloc(nv_alloc_t *nva) 215 { 216 nvpriv_t *priv; 217 218 /* 219 * nv_mem_alloc() cannot called here because it needs the priv 220 * argument. 221 */ 222 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL) 223 return (NULL); 224 225 nv_priv_init(priv, nva, 0); 226 227 return (priv); 228 } 229 230 /* 231 * Embedded lists need their own nvpriv_t's. We create a new 232 * nvpriv_t using the parameters and allocator from the parent 233 * list's nvpriv_t. 234 */ 235 static nvpriv_t * 236 nv_priv_alloc_embedded(nvpriv_t *priv) 237 { 238 nvpriv_t *emb_priv; 239 240 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL) 241 return (NULL); 242 243 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED); 244 245 return (emb_priv); 246 } 247 248 static void 249 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv) 250 { 251 nvl->nvl_version = NV_VERSION; 252 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE); 253 nvl->nvl_priv = (uint64_t)(uintptr_t)priv; 254 nvl->nvl_flag = 0; 255 nvl->nvl_pad = 0; 256 } 257 258 uint_t 259 nvlist_nvflag(nvlist_t *nvl) 260 { 261 return (nvl->nvl_nvflag); 262 } 263 264 /* 265 * nvlist_alloc - Allocate nvlist. 266 */ 267 /*ARGSUSED1*/ 268 int 269 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag) 270 { 271 #if defined(_KERNEL) && !defined(_BOOT) 272 return (nvlist_xalloc(nvlp, nvflag, 273 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep))); 274 #else 275 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep)); 276 #endif 277 } 278 279 int 280 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva) 281 { 282 nvpriv_t *priv; 283 284 if (nvlp == NULL || nva == NULL) 285 return (EINVAL); 286 287 if ((priv = nv_priv_alloc(nva)) == NULL) 288 return (ENOMEM); 289 290 if ((*nvlp = nv_mem_zalloc(priv, 291 NV_ALIGN(sizeof (nvlist_t)))) == NULL) { 292 nv_mem_free(priv, priv, sizeof (nvpriv_t)); 293 return (ENOMEM); 294 } 295 296 nvlist_init(*nvlp, nvflag, priv); 297 298 return (0); 299 } 300 301 /* 302 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair. 303 */ 304 static nvpair_t * 305 nvp_buf_alloc(nvlist_t *nvl, size_t len) 306 { 307 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 308 i_nvp_t *buf; 309 nvpair_t *nvp; 310 size_t nvsize; 311 312 /* 313 * Allocate the buffer 314 */ 315 nvsize = len + offsetof(i_nvp_t, nvi_nvp); 316 317 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL) 318 return (NULL); 319 320 nvp = &buf->nvi_nvp; 321 nvp->nvp_size = len; 322 323 return (nvp); 324 } 325 326 /* 327 * nvp_buf_free - de-Allocate an i_nvp_t. 328 */ 329 static void 330 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp) 331 { 332 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 333 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp); 334 335 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize); 336 } 337 338 /* 339 * nvp_buf_link - link a new nv pair into the nvlist. 340 */ 341 static void 342 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp) 343 { 344 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 345 i_nvp_t *curr = NVPAIR2I_NVP(nvp); 346 347 /* Put element at end of nvlist */ 348 if (priv->nvp_list == NULL) { 349 priv->nvp_list = priv->nvp_last = curr; 350 } else { 351 curr->nvi_prev = priv->nvp_last; 352 priv->nvp_last->nvi_next = curr; 353 priv->nvp_last = curr; 354 } 355 } 356 357 /* 358 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist. 359 */ 360 static void 361 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp) 362 { 363 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 364 i_nvp_t *curr = NVPAIR2I_NVP(nvp); 365 366 /* 367 * protect nvlist_next_nvpair() against walking on freed memory. 368 */ 369 if (priv->nvp_curr == curr) 370 priv->nvp_curr = curr->nvi_next; 371 372 if (curr == priv->nvp_list) 373 priv->nvp_list = curr->nvi_next; 374 else 375 curr->nvi_prev->nvi_next = curr->nvi_next; 376 377 if (curr == priv->nvp_last) 378 priv->nvp_last = curr->nvi_prev; 379 else 380 curr->nvi_next->nvi_prev = curr->nvi_prev; 381 } 382 383 /* 384 * take a nvpair type and number of elements and make sure the are valid 385 */ 386 static int 387 i_validate_type_nelem(data_type_t type, uint_t nelem) 388 { 389 switch (type) { 390 case DATA_TYPE_BOOLEAN: 391 if (nelem != 0) 392 return (EINVAL); 393 break; 394 case DATA_TYPE_BOOLEAN_VALUE: 395 case DATA_TYPE_BYTE: 396 case DATA_TYPE_INT8: 397 case DATA_TYPE_UINT8: 398 case DATA_TYPE_INT16: 399 case DATA_TYPE_UINT16: 400 case DATA_TYPE_INT32: 401 case DATA_TYPE_UINT32: 402 case DATA_TYPE_INT64: 403 case DATA_TYPE_UINT64: 404 case DATA_TYPE_STRING: 405 case DATA_TYPE_HRTIME: 406 case DATA_TYPE_NVLIST: 407 #if !defined(_KERNEL) 408 case DATA_TYPE_DOUBLE: 409 #endif 410 if (nelem != 1) 411 return (EINVAL); 412 break; 413 case DATA_TYPE_BOOLEAN_ARRAY: 414 case DATA_TYPE_BYTE_ARRAY: 415 case DATA_TYPE_INT8_ARRAY: 416 case DATA_TYPE_UINT8_ARRAY: 417 case DATA_TYPE_INT16_ARRAY: 418 case DATA_TYPE_UINT16_ARRAY: 419 case DATA_TYPE_INT32_ARRAY: 420 case DATA_TYPE_UINT32_ARRAY: 421 case DATA_TYPE_INT64_ARRAY: 422 case DATA_TYPE_UINT64_ARRAY: 423 case DATA_TYPE_STRING_ARRAY: 424 case DATA_TYPE_NVLIST_ARRAY: 425 /* we allow arrays with 0 elements */ 426 break; 427 default: 428 return (EINVAL); 429 } 430 return (0); 431 } 432 433 /* 434 * Verify nvp_name_sz and check the name string length. 435 */ 436 static int 437 i_validate_nvpair_name(nvpair_t *nvp) 438 { 439 if ((nvp->nvp_name_sz <= 0) || 440 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0))) 441 return (EFAULT); 442 443 /* verify the name string, make sure its terminated */ 444 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0') 445 return (EFAULT); 446 447 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT); 448 } 449 450 static int 451 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data) 452 { 453 switch (type) { 454 case DATA_TYPE_BOOLEAN_VALUE: 455 if (*(boolean_t *)data != B_TRUE && 456 *(boolean_t *)data != B_FALSE) 457 return (EINVAL); 458 break; 459 case DATA_TYPE_BOOLEAN_ARRAY: { 460 int i; 461 462 for (i = 0; i < nelem; i++) 463 if (((boolean_t *)data)[i] != B_TRUE && 464 ((boolean_t *)data)[i] != B_FALSE) 465 return (EINVAL); 466 break; 467 } 468 default: 469 break; 470 } 471 472 return (0); 473 } 474 475 /* 476 * This function takes a pointer to what should be a nvpair and it's size 477 * and then verifies that all the nvpair fields make sense and can be 478 * trusted. This function is used when decoding packed nvpairs. 479 */ 480 static int 481 i_validate_nvpair(nvpair_t *nvp) 482 { 483 data_type_t type = NVP_TYPE(nvp); 484 int size1, size2; 485 486 /* verify nvp_name_sz, check the name string length */ 487 if (i_validate_nvpair_name(nvp) != 0) 488 return (EFAULT); 489 490 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0) 491 return (EFAULT); 492 493 /* 494 * verify nvp_type, nvp_value_elem, and also possibly 495 * verify string values and get the value size. 496 */ 497 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp)); 498 size1 = nvp->nvp_size - NVP_VALOFF(nvp); 499 if (size2 < 0 || size1 != NV_ALIGN(size2)) 500 return (EFAULT); 501 502 return (0); 503 } 504 505 static int 506 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl) 507 { 508 nvpriv_t *priv; 509 i_nvp_t *curr; 510 511 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL) 512 return (EINVAL); 513 514 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) { 515 nvpair_t *nvp = &curr->nvi_nvp; 516 int err; 517 518 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp), 519 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0) 520 return (err); 521 } 522 523 return (0); 524 } 525 526 /* 527 * Frees all memory allocated for an nvpair (like embedded lists) with 528 * the exception of the nvpair buffer itself. 529 */ 530 static void 531 nvpair_free(nvpair_t *nvp) 532 { 533 switch (NVP_TYPE(nvp)) { 534 case DATA_TYPE_NVLIST: 535 nvlist_free(EMBEDDED_NVL(nvp)); 536 break; 537 case DATA_TYPE_NVLIST_ARRAY: { 538 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp); 539 int i; 540 541 for (i = 0; i < NVP_NELEM(nvp); i++) 542 if (nvlp[i] != NULL) 543 nvlist_free(nvlp[i]); 544 break; 545 } 546 default: 547 break; 548 } 549 } 550 551 /* 552 * nvlist_free - free an unpacked nvlist 553 */ 554 void 555 nvlist_free(nvlist_t *nvl) 556 { 557 nvpriv_t *priv; 558 i_nvp_t *curr; 559 560 if (nvl == NULL || 561 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 562 return; 563 564 /* 565 * Unpacked nvlist are linked through i_nvp_t 566 */ 567 curr = priv->nvp_list; 568 while (curr != NULL) { 569 nvpair_t *nvp = &curr->nvi_nvp; 570 curr = curr->nvi_next; 571 572 nvpair_free(nvp); 573 nvp_buf_free(nvl, nvp); 574 } 575 576 if (!(priv->nvp_stat & NV_STAT_EMBEDDED)) 577 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t))); 578 else 579 nvl->nvl_priv = 0; 580 581 nv_mem_free(priv, priv, sizeof (nvpriv_t)); 582 } 583 584 static int 585 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp) 586 { 587 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 588 i_nvp_t *curr; 589 590 if (nvp == NULL) 591 return (0); 592 593 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) 594 if (&curr->nvi_nvp == nvp) 595 return (1); 596 597 return (0); 598 } 599 600 /* 601 * Make a copy of nvlist 602 */ 603 /*ARGSUSED1*/ 604 int 605 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag) 606 { 607 #if defined(_KERNEL) && !defined(_BOOT) 608 return (nvlist_xdup(nvl, nvlp, 609 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep))); 610 #else 611 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep)); 612 #endif 613 } 614 615 int 616 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva) 617 { 618 int err; 619 nvlist_t *ret; 620 621 if (nvl == NULL || nvlp == NULL) 622 return (EINVAL); 623 624 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0) 625 return (err); 626 627 if ((err = nvlist_copy_pairs(nvl, ret)) != 0) 628 nvlist_free(ret); 629 else 630 *nvlp = ret; 631 632 return (err); 633 } 634 635 /* 636 * Remove all with matching name 637 */ 638 int 639 nvlist_remove_all(nvlist_t *nvl, const char *name) 640 { 641 nvpriv_t *priv; 642 i_nvp_t *curr; 643 int error = ENOENT; 644 645 if (nvl == NULL || name == NULL || 646 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 647 return (EINVAL); 648 649 curr = priv->nvp_list; 650 while (curr != NULL) { 651 nvpair_t *nvp = &curr->nvi_nvp; 652 653 curr = curr->nvi_next; 654 if (strcmp(name, NVP_NAME(nvp)) != 0) 655 continue; 656 657 nvp_buf_unlink(nvl, nvp); 658 nvpair_free(nvp); 659 nvp_buf_free(nvl, nvp); 660 661 error = 0; 662 } 663 664 return (error); 665 } 666 667 /* 668 * Remove first one with matching name and type 669 */ 670 int 671 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type) 672 { 673 nvpriv_t *priv; 674 i_nvp_t *curr; 675 676 if (nvl == NULL || name == NULL || 677 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 678 return (EINVAL); 679 680 curr = priv->nvp_list; 681 while (curr != NULL) { 682 nvpair_t *nvp = &curr->nvi_nvp; 683 684 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) { 685 nvp_buf_unlink(nvl, nvp); 686 nvpair_free(nvp); 687 nvp_buf_free(nvl, nvp); 688 689 return (0); 690 } 691 curr = curr->nvi_next; 692 } 693 694 return (ENOENT); 695 } 696 697 int 698 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp) 699 { 700 if (nvl == NULL || nvp == NULL) 701 return (EINVAL); 702 703 nvp_buf_unlink(nvl, nvp); 704 nvpair_free(nvp); 705 nvp_buf_free(nvl, nvp); 706 return (0); 707 } 708 709 /* 710 * This function calculates the size of an nvpair value. 711 * 712 * The data argument controls the behavior in case of the data types 713 * DATA_TYPE_STRING and 714 * DATA_TYPE_STRING_ARRAY 715 * Is data == NULL then the size of the string(s) is excluded. 716 */ 717 static int 718 i_get_value_size(data_type_t type, const void *data, uint_t nelem) 719 { 720 uint64_t value_sz; 721 722 if (i_validate_type_nelem(type, nelem) != 0) 723 return (-1); 724 725 /* Calculate required size for holding value */ 726 switch (type) { 727 case DATA_TYPE_BOOLEAN: 728 value_sz = 0; 729 break; 730 case DATA_TYPE_BOOLEAN_VALUE: 731 value_sz = sizeof (boolean_t); 732 break; 733 case DATA_TYPE_BYTE: 734 value_sz = sizeof (uchar_t); 735 break; 736 case DATA_TYPE_INT8: 737 value_sz = sizeof (int8_t); 738 break; 739 case DATA_TYPE_UINT8: 740 value_sz = sizeof (uint8_t); 741 break; 742 case DATA_TYPE_INT16: 743 value_sz = sizeof (int16_t); 744 break; 745 case DATA_TYPE_UINT16: 746 value_sz = sizeof (uint16_t); 747 break; 748 case DATA_TYPE_INT32: 749 value_sz = sizeof (int32_t); 750 break; 751 case DATA_TYPE_UINT32: 752 value_sz = sizeof (uint32_t); 753 break; 754 case DATA_TYPE_INT64: 755 value_sz = sizeof (int64_t); 756 break; 757 case DATA_TYPE_UINT64: 758 value_sz = sizeof (uint64_t); 759 break; 760 #if !defined(_KERNEL) 761 case DATA_TYPE_DOUBLE: 762 value_sz = sizeof (double); 763 break; 764 #endif 765 case DATA_TYPE_STRING: 766 if (data == NULL) 767 value_sz = 0; 768 else 769 value_sz = strlen(data) + 1; 770 break; 771 case DATA_TYPE_BOOLEAN_ARRAY: 772 value_sz = (uint64_t)nelem * sizeof (boolean_t); 773 break; 774 case DATA_TYPE_BYTE_ARRAY: 775 value_sz = (uint64_t)nelem * sizeof (uchar_t); 776 break; 777 case DATA_TYPE_INT8_ARRAY: 778 value_sz = (uint64_t)nelem * sizeof (int8_t); 779 break; 780 case DATA_TYPE_UINT8_ARRAY: 781 value_sz = (uint64_t)nelem * sizeof (uint8_t); 782 break; 783 case DATA_TYPE_INT16_ARRAY: 784 value_sz = (uint64_t)nelem * sizeof (int16_t); 785 break; 786 case DATA_TYPE_UINT16_ARRAY: 787 value_sz = (uint64_t)nelem * sizeof (uint16_t); 788 break; 789 case DATA_TYPE_INT32_ARRAY: 790 value_sz = (uint64_t)nelem * sizeof (int32_t); 791 break; 792 case DATA_TYPE_UINT32_ARRAY: 793 value_sz = (uint64_t)nelem * sizeof (uint32_t); 794 break; 795 case DATA_TYPE_INT64_ARRAY: 796 value_sz = (uint64_t)nelem * sizeof (int64_t); 797 break; 798 case DATA_TYPE_UINT64_ARRAY: 799 value_sz = (uint64_t)nelem * sizeof (uint64_t); 800 break; 801 case DATA_TYPE_STRING_ARRAY: 802 value_sz = (uint64_t)nelem * sizeof (uint64_t); 803 804 if (data != NULL) { 805 char *const *strs = data; 806 uint_t i; 807 808 /* no alignment requirement for strings */ 809 for (i = 0; i < nelem; i++) { 810 if (strs[i] == NULL) 811 return (-1); 812 value_sz += strlen(strs[i]) + 1; 813 } 814 } 815 break; 816 case DATA_TYPE_HRTIME: 817 value_sz = sizeof (hrtime_t); 818 break; 819 case DATA_TYPE_NVLIST: 820 value_sz = NV_ALIGN(sizeof (nvlist_t)); 821 break; 822 case DATA_TYPE_NVLIST_ARRAY: 823 value_sz = (uint64_t)nelem * sizeof (uint64_t) + 824 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t)); 825 break; 826 default: 827 return (-1); 828 } 829 830 return (value_sz > INT32_MAX ? -1 : (int)value_sz); 831 } 832 833 static int 834 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl) 835 { 836 nvpriv_t *priv; 837 int err; 838 839 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t) 840 nvl->nvl_priv)) == NULL) 841 return (ENOMEM); 842 843 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv); 844 845 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) { 846 nvlist_free(emb_nvl); 847 emb_nvl->nvl_priv = 0; 848 } 849 850 return (err); 851 } 852 853 /* 854 * nvlist_add_common - Add new <name,value> pair to nvlist 855 */ 856 static int 857 nvlist_add_common(nvlist_t *nvl, const char *name, 858 data_type_t type, uint_t nelem, const void *data) 859 { 860 nvpair_t *nvp; 861 uint_t i; 862 863 int nvp_sz, name_sz, value_sz; 864 int err = 0; 865 866 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0) 867 return (EINVAL); 868 869 if (nelem != 0 && data == NULL) 870 return (EINVAL); 871 872 /* 873 * Verify type and nelem and get the value size. 874 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY 875 * is the size of the string(s) included. 876 */ 877 if ((value_sz = i_get_value_size(type, data, nelem)) < 0) 878 return (EINVAL); 879 880 if (i_validate_nvpair_value(type, nelem, data) != 0) 881 return (EINVAL); 882 883 /* 884 * If we're adding an nvlist or nvlist array, ensure that we are not 885 * adding the input nvlist to itself, which would cause recursion, 886 * and ensure that no NULL nvlist pointers are present. 887 */ 888 switch (type) { 889 case DATA_TYPE_NVLIST: 890 if (data == nvl || data == NULL) 891 return (EINVAL); 892 break; 893 case DATA_TYPE_NVLIST_ARRAY: { 894 nvlist_t **onvlp = (nvlist_t **)data; 895 for (i = 0; i < nelem; i++) { 896 if (onvlp[i] == nvl || onvlp[i] == NULL) 897 return (EINVAL); 898 } 899 break; 900 } 901 default: 902 break; 903 } 904 905 /* calculate sizes of the nvpair elements and the nvpair itself */ 906 name_sz = strlen(name) + 1; 907 908 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz); 909 910 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL) 911 return (ENOMEM); 912 913 ASSERT(nvp->nvp_size == nvp_sz); 914 nvp->nvp_name_sz = name_sz; 915 nvp->nvp_value_elem = nelem; 916 nvp->nvp_type = type; 917 bcopy(name, NVP_NAME(nvp), name_sz); 918 919 switch (type) { 920 case DATA_TYPE_BOOLEAN: 921 break; 922 case DATA_TYPE_STRING_ARRAY: { 923 char *const *strs = data; 924 char *buf = NVP_VALUE(nvp); 925 char **cstrs = (void *)buf; 926 927 /* skip pre-allocated space for pointer array */ 928 buf += nelem * sizeof (uint64_t); 929 for (i = 0; i < nelem; i++) { 930 int slen = strlen(strs[i]) + 1; 931 bcopy(strs[i], buf, slen); 932 cstrs[i] = buf; 933 buf += slen; 934 } 935 break; 936 } 937 case DATA_TYPE_NVLIST: { 938 nvlist_t *nnvl = EMBEDDED_NVL(nvp); 939 nvlist_t *onvl = (nvlist_t *)data; 940 941 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) { 942 nvp_buf_free(nvl, nvp); 943 return (err); 944 } 945 break; 946 } 947 case DATA_TYPE_NVLIST_ARRAY: { 948 nvlist_t **onvlp = (nvlist_t **)data; 949 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp); 950 nvlist_t *embedded = (nvlist_t *) 951 ((uintptr_t)nvlp + nelem * sizeof (uint64_t)); 952 953 for (i = 0; i < nelem; i++) { 954 if ((err = nvlist_copy_embedded(nvl, 955 onvlp[i], embedded)) != 0) { 956 /* 957 * Free any successfully created lists 958 */ 959 nvpair_free(nvp); 960 nvp_buf_free(nvl, nvp); 961 return (err); 962 } 963 964 nvlp[i] = embedded++; 965 } 966 break; 967 } 968 default: 969 bcopy(data, NVP_VALUE(nvp), value_sz); 970 } 971 972 /* if unique name, remove before add */ 973 if (nvl->nvl_nvflag & NV_UNIQUE_NAME) 974 (void) nvlist_remove_all(nvl, name); 975 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE) 976 (void) nvlist_remove(nvl, name, type); 977 978 nvp_buf_link(nvl, nvp); 979 980 return (0); 981 } 982 983 int 984 nvlist_add_boolean(nvlist_t *nvl, const char *name) 985 { 986 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL)); 987 } 988 989 int 990 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val) 991 { 992 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val)); 993 } 994 995 int 996 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val) 997 { 998 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val)); 999 } 1000 1001 int 1002 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val) 1003 { 1004 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val)); 1005 } 1006 1007 int 1008 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val) 1009 { 1010 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val)); 1011 } 1012 1013 int 1014 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val) 1015 { 1016 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val)); 1017 } 1018 1019 int 1020 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val) 1021 { 1022 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val)); 1023 } 1024 1025 int 1026 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val) 1027 { 1028 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val)); 1029 } 1030 1031 int 1032 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val) 1033 { 1034 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val)); 1035 } 1036 1037 int 1038 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val) 1039 { 1040 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val)); 1041 } 1042 1043 int 1044 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val) 1045 { 1046 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val)); 1047 } 1048 1049 #if !defined(_KERNEL) 1050 int 1051 nvlist_add_double(nvlist_t *nvl, const char *name, double val) 1052 { 1053 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val)); 1054 } 1055 #endif 1056 1057 int 1058 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val) 1059 { 1060 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val)); 1061 } 1062 1063 int 1064 nvlist_add_boolean_array(nvlist_t *nvl, const char *name, 1065 boolean_t *a, uint_t n) 1066 { 1067 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a)); 1068 } 1069 1070 int 1071 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n) 1072 { 1073 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a)); 1074 } 1075 1076 int 1077 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n) 1078 { 1079 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a)); 1080 } 1081 1082 int 1083 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n) 1084 { 1085 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a)); 1086 } 1087 1088 int 1089 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n) 1090 { 1091 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a)); 1092 } 1093 1094 int 1095 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n) 1096 { 1097 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a)); 1098 } 1099 1100 int 1101 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n) 1102 { 1103 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a)); 1104 } 1105 1106 int 1107 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n) 1108 { 1109 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a)); 1110 } 1111 1112 int 1113 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n) 1114 { 1115 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a)); 1116 } 1117 1118 int 1119 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n) 1120 { 1121 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a)); 1122 } 1123 1124 int 1125 nvlist_add_string_array(nvlist_t *nvl, const char *name, 1126 char *const *a, uint_t n) 1127 { 1128 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a)); 1129 } 1130 1131 int 1132 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val) 1133 { 1134 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val)); 1135 } 1136 1137 int 1138 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val) 1139 { 1140 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val)); 1141 } 1142 1143 int 1144 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n) 1145 { 1146 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a)); 1147 } 1148 1149 /* reading name-value pairs */ 1150 nvpair_t * 1151 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp) 1152 { 1153 nvpriv_t *priv; 1154 i_nvp_t *curr; 1155 1156 if (nvl == NULL || 1157 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 1158 return (NULL); 1159 1160 curr = NVPAIR2I_NVP(nvp); 1161 1162 /* 1163 * Ensure that nvp is a valid nvpair on this nvlist. 1164 * NB: nvp_curr is used only as a hint so that we don't always 1165 * have to walk the list to determine if nvp is still on the list. 1166 */ 1167 if (nvp == NULL) 1168 curr = priv->nvp_list; 1169 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp)) 1170 curr = curr->nvi_next; 1171 else 1172 curr = NULL; 1173 1174 priv->nvp_curr = curr; 1175 1176 return (curr != NULL ? &curr->nvi_nvp : NULL); 1177 } 1178 1179 nvpair_t * 1180 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp) 1181 { 1182 nvpriv_t *priv; 1183 i_nvp_t *curr; 1184 1185 if (nvl == NULL || 1186 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 1187 return (NULL); 1188 1189 curr = NVPAIR2I_NVP(nvp); 1190 1191 if (nvp == NULL) 1192 curr = priv->nvp_last; 1193 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp)) 1194 curr = curr->nvi_prev; 1195 else 1196 curr = NULL; 1197 1198 priv->nvp_curr = curr; 1199 1200 return (curr != NULL ? &curr->nvi_nvp : NULL); 1201 } 1202 1203 boolean_t 1204 nvlist_empty(nvlist_t *nvl) 1205 { 1206 nvpriv_t *priv; 1207 1208 if (nvl == NULL || 1209 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 1210 return (B_TRUE); 1211 1212 return (priv->nvp_list == NULL); 1213 } 1214 1215 char * 1216 nvpair_name(nvpair_t *nvp) 1217 { 1218 return (NVP_NAME(nvp)); 1219 } 1220 1221 data_type_t 1222 nvpair_type(nvpair_t *nvp) 1223 { 1224 return (NVP_TYPE(nvp)); 1225 } 1226 1227 int 1228 nvpair_type_is_array(nvpair_t *nvp) 1229 { 1230 data_type_t type = NVP_TYPE(nvp); 1231 1232 if ((type == DATA_TYPE_BYTE_ARRAY) || 1233 (type == DATA_TYPE_UINT8_ARRAY) || 1234 (type == DATA_TYPE_INT16_ARRAY) || 1235 (type == DATA_TYPE_UINT16_ARRAY) || 1236 (type == DATA_TYPE_INT32_ARRAY) || 1237 (type == DATA_TYPE_UINT32_ARRAY) || 1238 (type == DATA_TYPE_INT64_ARRAY) || 1239 (type == DATA_TYPE_UINT64_ARRAY) || 1240 (type == DATA_TYPE_BOOLEAN_ARRAY) || 1241 (type == DATA_TYPE_STRING_ARRAY) || 1242 (type == DATA_TYPE_NVLIST_ARRAY)) 1243 return (1); 1244 return (0); 1245 1246 } 1247 1248 static int 1249 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data) 1250 { 1251 if (nvp == NULL || nvpair_type(nvp) != type) 1252 return (EINVAL); 1253 1254 /* 1255 * For non-array types, we copy the data. 1256 * For array types (including string), we set a pointer. 1257 */ 1258 switch (type) { 1259 case DATA_TYPE_BOOLEAN: 1260 if (nelem != NULL) 1261 *nelem = 0; 1262 break; 1263 1264 case DATA_TYPE_BOOLEAN_VALUE: 1265 case DATA_TYPE_BYTE: 1266 case DATA_TYPE_INT8: 1267 case DATA_TYPE_UINT8: 1268 case DATA_TYPE_INT16: 1269 case DATA_TYPE_UINT16: 1270 case DATA_TYPE_INT32: 1271 case DATA_TYPE_UINT32: 1272 case DATA_TYPE_INT64: 1273 case DATA_TYPE_UINT64: 1274 case DATA_TYPE_HRTIME: 1275 #if !defined(_KERNEL) 1276 case DATA_TYPE_DOUBLE: 1277 #endif 1278 if (data == NULL) 1279 return (EINVAL); 1280 bcopy(NVP_VALUE(nvp), data, 1281 (size_t)i_get_value_size(type, NULL, 1)); 1282 if (nelem != NULL) 1283 *nelem = 1; 1284 break; 1285 1286 case DATA_TYPE_NVLIST: 1287 case DATA_TYPE_STRING: 1288 if (data == NULL) 1289 return (EINVAL); 1290 *(void **)data = (void *)NVP_VALUE(nvp); 1291 if (nelem != NULL) 1292 *nelem = 1; 1293 break; 1294 1295 case DATA_TYPE_BOOLEAN_ARRAY: 1296 case DATA_TYPE_BYTE_ARRAY: 1297 case DATA_TYPE_INT8_ARRAY: 1298 case DATA_TYPE_UINT8_ARRAY: 1299 case DATA_TYPE_INT16_ARRAY: 1300 case DATA_TYPE_UINT16_ARRAY: 1301 case DATA_TYPE_INT32_ARRAY: 1302 case DATA_TYPE_UINT32_ARRAY: 1303 case DATA_TYPE_INT64_ARRAY: 1304 case DATA_TYPE_UINT64_ARRAY: 1305 case DATA_TYPE_STRING_ARRAY: 1306 case DATA_TYPE_NVLIST_ARRAY: 1307 if (nelem == NULL || data == NULL) 1308 return (EINVAL); 1309 if ((*nelem = NVP_NELEM(nvp)) != 0) 1310 *(void **)data = (void *)NVP_VALUE(nvp); 1311 else 1312 *(void **)data = NULL; 1313 break; 1314 1315 default: 1316 return (ENOTSUP); 1317 } 1318 1319 return (0); 1320 } 1321 1322 static int 1323 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type, 1324 uint_t *nelem, void *data) 1325 { 1326 nvpriv_t *priv; 1327 nvpair_t *nvp; 1328 i_nvp_t *curr; 1329 1330 if (name == NULL || nvl == NULL || 1331 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 1332 return (EINVAL); 1333 1334 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE))) 1335 return (ENOTSUP); 1336 1337 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) { 1338 nvp = &curr->nvi_nvp; 1339 1340 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) 1341 return (nvpair_value_common(nvp, type, nelem, data)); 1342 } 1343 1344 return (ENOENT); 1345 } 1346 1347 int 1348 nvlist_lookup_boolean(nvlist_t *nvl, const char *name) 1349 { 1350 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL)); 1351 } 1352 1353 int 1354 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val) 1355 { 1356 return (nvlist_lookup_common(nvl, name, 1357 DATA_TYPE_BOOLEAN_VALUE, NULL, val)); 1358 } 1359 1360 int 1361 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val) 1362 { 1363 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val)); 1364 } 1365 1366 int 1367 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val) 1368 { 1369 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val)); 1370 } 1371 1372 int 1373 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val) 1374 { 1375 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val)); 1376 } 1377 1378 int 1379 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val) 1380 { 1381 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val)); 1382 } 1383 1384 int 1385 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val) 1386 { 1387 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val)); 1388 } 1389 1390 int 1391 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val) 1392 { 1393 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val)); 1394 } 1395 1396 int 1397 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val) 1398 { 1399 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val)); 1400 } 1401 1402 int 1403 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val) 1404 { 1405 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val)); 1406 } 1407 1408 int 1409 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val) 1410 { 1411 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val)); 1412 } 1413 1414 #if !defined(_KERNEL) 1415 int 1416 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val) 1417 { 1418 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val)); 1419 } 1420 #endif 1421 1422 int 1423 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val) 1424 { 1425 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val)); 1426 } 1427 1428 int 1429 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val) 1430 { 1431 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val)); 1432 } 1433 1434 int 1435 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name, 1436 boolean_t **a, uint_t *n) 1437 { 1438 return (nvlist_lookup_common(nvl, name, 1439 DATA_TYPE_BOOLEAN_ARRAY, n, a)); 1440 } 1441 1442 int 1443 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name, 1444 uchar_t **a, uint_t *n) 1445 { 1446 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a)); 1447 } 1448 1449 int 1450 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n) 1451 { 1452 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a)); 1453 } 1454 1455 int 1456 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name, 1457 uint8_t **a, uint_t *n) 1458 { 1459 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a)); 1460 } 1461 1462 int 1463 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name, 1464 int16_t **a, uint_t *n) 1465 { 1466 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a)); 1467 } 1468 1469 int 1470 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name, 1471 uint16_t **a, uint_t *n) 1472 { 1473 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a)); 1474 } 1475 1476 int 1477 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name, 1478 int32_t **a, uint_t *n) 1479 { 1480 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a)); 1481 } 1482 1483 int 1484 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name, 1485 uint32_t **a, uint_t *n) 1486 { 1487 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a)); 1488 } 1489 1490 int 1491 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name, 1492 int64_t **a, uint_t *n) 1493 { 1494 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a)); 1495 } 1496 1497 int 1498 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name, 1499 uint64_t **a, uint_t *n) 1500 { 1501 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a)); 1502 } 1503 1504 int 1505 nvlist_lookup_string_array(nvlist_t *nvl, const char *name, 1506 char ***a, uint_t *n) 1507 { 1508 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a)); 1509 } 1510 1511 int 1512 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name, 1513 nvlist_t ***a, uint_t *n) 1514 { 1515 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a)); 1516 } 1517 1518 int 1519 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val) 1520 { 1521 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val)); 1522 } 1523 1524 int 1525 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...) 1526 { 1527 va_list ap; 1528 char *name; 1529 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0); 1530 int ret = 0; 1531 1532 va_start(ap, flag); 1533 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) { 1534 data_type_t type; 1535 void *val; 1536 uint_t *nelem; 1537 1538 switch (type = va_arg(ap, data_type_t)) { 1539 case DATA_TYPE_BOOLEAN: 1540 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL); 1541 break; 1542 1543 case DATA_TYPE_BOOLEAN_VALUE: 1544 case DATA_TYPE_BYTE: 1545 case DATA_TYPE_INT8: 1546 case DATA_TYPE_UINT8: 1547 case DATA_TYPE_INT16: 1548 case DATA_TYPE_UINT16: 1549 case DATA_TYPE_INT32: 1550 case DATA_TYPE_UINT32: 1551 case DATA_TYPE_INT64: 1552 case DATA_TYPE_UINT64: 1553 case DATA_TYPE_HRTIME: 1554 case DATA_TYPE_STRING: 1555 case DATA_TYPE_NVLIST: 1556 #if !defined(_KERNEL) 1557 case DATA_TYPE_DOUBLE: 1558 #endif 1559 val = va_arg(ap, void *); 1560 ret = nvlist_lookup_common(nvl, name, type, NULL, val); 1561 break; 1562 1563 case DATA_TYPE_BYTE_ARRAY: 1564 case DATA_TYPE_BOOLEAN_ARRAY: 1565 case DATA_TYPE_INT8_ARRAY: 1566 case DATA_TYPE_UINT8_ARRAY: 1567 case DATA_TYPE_INT16_ARRAY: 1568 case DATA_TYPE_UINT16_ARRAY: 1569 case DATA_TYPE_INT32_ARRAY: 1570 case DATA_TYPE_UINT32_ARRAY: 1571 case DATA_TYPE_INT64_ARRAY: 1572 case DATA_TYPE_UINT64_ARRAY: 1573 case DATA_TYPE_STRING_ARRAY: 1574 case DATA_TYPE_NVLIST_ARRAY: 1575 val = va_arg(ap, void *); 1576 nelem = va_arg(ap, uint_t *); 1577 ret = nvlist_lookup_common(nvl, name, type, nelem, val); 1578 break; 1579 1580 default: 1581 ret = EINVAL; 1582 } 1583 1584 if (ret == ENOENT && noentok) 1585 ret = 0; 1586 } 1587 va_end(ap); 1588 1589 return (ret); 1590 } 1591 1592 /* 1593 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function 1594 * returns zero and a pointer to the matching nvpair is returned in '*ret' 1595 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate 1596 * multiple levels of embedded nvlists, with 'sep' as the separator. As an 1597 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or 1598 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience, 1599 * code also supports "a.d[3]e[1]" syntax). 1600 * 1601 * If 'ip' is non-NULL and the last name component is an array, return the 1602 * value of the "...[index]" array index in *ip. For an array reference that 1603 * is not indexed, *ip will be returned as -1. If there is a syntax error in 1604 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location 1605 * inside the 'name' string where the syntax error was detected. 1606 */ 1607 static int 1608 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep, 1609 nvpair_t **ret, int *ip, char **ep) 1610 { 1611 nvpair_t *nvp; 1612 const char *np; 1613 char *sepp; 1614 char *idxp, *idxep; 1615 nvlist_t **nva; 1616 long idx; 1617 int n; 1618 1619 if (ip) 1620 *ip = -1; /* not indexed */ 1621 if (ep) 1622 *ep = NULL; 1623 1624 if ((nvl == NULL) || (name == NULL)) 1625 return (EINVAL); 1626 1627 /* step through components of name */ 1628 for (np = name; np && *np; np = sepp) { 1629 /* ensure unique names */ 1630 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME)) 1631 return (ENOTSUP); 1632 1633 /* skip white space */ 1634 skip_whitespace(np); 1635 if (*np == 0) 1636 break; 1637 1638 /* set 'sepp' to end of current component 'np' */ 1639 if (sep) 1640 sepp = strchr(np, sep); 1641 else 1642 sepp = NULL; 1643 1644 /* find start of next "[ index ]..." */ 1645 idxp = strchr(np, '['); 1646 1647 /* if sepp comes first, set idxp to NULL */ 1648 if (sepp && idxp && (sepp < idxp)) 1649 idxp = NULL; 1650 1651 /* 1652 * At this point 'idxp' is set if there is an index 1653 * expected for the current component. 1654 */ 1655 if (idxp) { 1656 /* set 'n' to length of current 'np' name component */ 1657 n = idxp++ - np; 1658 1659 /* keep sepp up to date for *ep use as we advance */ 1660 skip_whitespace(idxp); 1661 sepp = idxp; 1662 1663 /* determine the index value */ 1664 #if defined(_KERNEL) && !defined(_BOOT) 1665 if (ddi_strtol(idxp, &idxep, 0, &idx)) 1666 goto fail; 1667 #else 1668 idx = strtol(idxp, &idxep, 0); 1669 #endif 1670 if (idxep == idxp) 1671 goto fail; 1672 1673 /* keep sepp up to date for *ep use as we advance */ 1674 sepp = idxep; 1675 1676 /* skip white space index value and check for ']' */ 1677 skip_whitespace(sepp); 1678 if (*sepp++ != ']') 1679 goto fail; 1680 1681 /* for embedded arrays, support C syntax: "a[1].b" */ 1682 skip_whitespace(sepp); 1683 if (sep && (*sepp == sep)) 1684 sepp++; 1685 } else if (sepp) { 1686 n = sepp++ - np; 1687 } else { 1688 n = strlen(np); 1689 } 1690 1691 /* trim trailing whitespace by reducing length of 'np' */ 1692 if (n == 0) 1693 goto fail; 1694 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--) 1695 ; 1696 n++; 1697 1698 /* skip whitespace, and set sepp to NULL if complete */ 1699 if (sepp) { 1700 skip_whitespace(sepp); 1701 if (*sepp == 0) 1702 sepp = NULL; 1703 } 1704 1705 /* 1706 * At this point: 1707 * o 'n' is the length of current 'np' component. 1708 * o 'idxp' is set if there was an index, and value 'idx'. 1709 * o 'sepp' is set to the beginning of the next component, 1710 * and set to NULL if we have no more components. 1711 * 1712 * Search for nvpair with matching component name. 1713 */ 1714 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL; 1715 nvp = nvlist_next_nvpair(nvl, nvp)) { 1716 1717 /* continue if no match on name */ 1718 if (strncmp(np, nvpair_name(nvp), n) || 1719 (strlen(nvpair_name(nvp)) != n)) 1720 continue; 1721 1722 /* if indexed, verify type is array oriented */ 1723 if (idxp && !nvpair_type_is_array(nvp)) 1724 goto fail; 1725 1726 /* 1727 * Full match found, return nvp and idx if this 1728 * was the last component. 1729 */ 1730 if (sepp == NULL) { 1731 if (ret) 1732 *ret = nvp; 1733 if (ip && idxp) 1734 *ip = (int)idx; /* return index */ 1735 return (0); /* found */ 1736 } 1737 1738 /* 1739 * More components: current match must be 1740 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY 1741 * to support going deeper. 1742 */ 1743 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) { 1744 nvl = EMBEDDED_NVL(nvp); 1745 break; 1746 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) { 1747 (void) nvpair_value_nvlist_array(nvp, 1748 &nva, (uint_t *)&n); 1749 if ((n < 0) || (idx >= n)) 1750 goto fail; 1751 nvl = nva[idx]; 1752 break; 1753 } 1754 1755 /* type does not support more levels */ 1756 goto fail; 1757 } 1758 if (nvp == NULL) 1759 goto fail; /* 'name' not found */ 1760 1761 /* search for match of next component in embedded 'nvl' list */ 1762 } 1763 1764 fail: if (ep && sepp) 1765 *ep = sepp; 1766 return (EINVAL); 1767 } 1768 1769 /* 1770 * Return pointer to nvpair with specified 'name'. 1771 */ 1772 int 1773 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret) 1774 { 1775 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL)); 1776 } 1777 1778 /* 1779 * Determine if named nvpair exists in nvlist (use embedded separator of '.' 1780 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed 1781 * description. 1782 */ 1783 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl, 1784 const char *name, nvpair_t **ret, int *ip, char **ep) 1785 { 1786 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep)); 1787 } 1788 1789 boolean_t 1790 nvlist_exists(nvlist_t *nvl, const char *name) 1791 { 1792 nvpriv_t *priv; 1793 nvpair_t *nvp; 1794 i_nvp_t *curr; 1795 1796 if (name == NULL || nvl == NULL || 1797 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 1798 return (B_FALSE); 1799 1800 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) { 1801 nvp = &curr->nvi_nvp; 1802 1803 if (strcmp(name, NVP_NAME(nvp)) == 0) 1804 return (B_TRUE); 1805 } 1806 1807 return (B_FALSE); 1808 } 1809 1810 int 1811 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val) 1812 { 1813 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val)); 1814 } 1815 1816 int 1817 nvpair_value_byte(nvpair_t *nvp, uchar_t *val) 1818 { 1819 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val)); 1820 } 1821 1822 int 1823 nvpair_value_int8(nvpair_t *nvp, int8_t *val) 1824 { 1825 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val)); 1826 } 1827 1828 int 1829 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val) 1830 { 1831 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val)); 1832 } 1833 1834 int 1835 nvpair_value_int16(nvpair_t *nvp, int16_t *val) 1836 { 1837 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val)); 1838 } 1839 1840 int 1841 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val) 1842 { 1843 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val)); 1844 } 1845 1846 int 1847 nvpair_value_int32(nvpair_t *nvp, int32_t *val) 1848 { 1849 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val)); 1850 } 1851 1852 int 1853 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val) 1854 { 1855 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val)); 1856 } 1857 1858 int 1859 nvpair_value_int64(nvpair_t *nvp, int64_t *val) 1860 { 1861 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val)); 1862 } 1863 1864 int 1865 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val) 1866 { 1867 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val)); 1868 } 1869 1870 #if !defined(_KERNEL) 1871 int 1872 nvpair_value_double(nvpair_t *nvp, double *val) 1873 { 1874 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val)); 1875 } 1876 #endif 1877 1878 int 1879 nvpair_value_string(nvpair_t *nvp, char **val) 1880 { 1881 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val)); 1882 } 1883 1884 int 1885 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val) 1886 { 1887 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val)); 1888 } 1889 1890 int 1891 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem) 1892 { 1893 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val)); 1894 } 1895 1896 int 1897 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem) 1898 { 1899 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val)); 1900 } 1901 1902 int 1903 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem) 1904 { 1905 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val)); 1906 } 1907 1908 int 1909 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem) 1910 { 1911 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val)); 1912 } 1913 1914 int 1915 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem) 1916 { 1917 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val)); 1918 } 1919 1920 int 1921 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem) 1922 { 1923 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val)); 1924 } 1925 1926 int 1927 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem) 1928 { 1929 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val)); 1930 } 1931 1932 int 1933 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem) 1934 { 1935 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val)); 1936 } 1937 1938 int 1939 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem) 1940 { 1941 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val)); 1942 } 1943 1944 int 1945 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem) 1946 { 1947 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val)); 1948 } 1949 1950 int 1951 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem) 1952 { 1953 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val)); 1954 } 1955 1956 int 1957 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem) 1958 { 1959 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val)); 1960 } 1961 1962 int 1963 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val) 1964 { 1965 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val)); 1966 } 1967 1968 /* 1969 * Add specified pair to the list. 1970 */ 1971 int 1972 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp) 1973 { 1974 if (nvl == NULL || nvp == NULL) 1975 return (EINVAL); 1976 1977 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp), 1978 NVP_NELEM(nvp), NVP_VALUE(nvp))); 1979 } 1980 1981 /* 1982 * Merge the supplied nvlists and put the result in dst. 1983 * The merged list will contain all names specified in both lists, 1984 * the values are taken from nvl in the case of duplicates. 1985 * Return 0 on success. 1986 */ 1987 /*ARGSUSED*/ 1988 int 1989 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag) 1990 { 1991 if (nvl == NULL || dst == NULL) 1992 return (EINVAL); 1993 1994 if (dst != nvl) 1995 return (nvlist_copy_pairs(nvl, dst)); 1996 1997 return (0); 1998 } 1999 2000 /* 2001 * Encoding related routines 2002 */ 2003 #define NVS_OP_ENCODE 0 2004 #define NVS_OP_DECODE 1 2005 #define NVS_OP_GETSIZE 2 2006 2007 typedef struct nvs_ops nvs_ops_t; 2008 2009 typedef struct { 2010 int nvs_op; 2011 const nvs_ops_t *nvs_ops; 2012 void *nvs_private; 2013 nvpriv_t *nvs_priv; 2014 } nvstream_t; 2015 2016 /* 2017 * nvs operations are: 2018 * - nvs_nvlist 2019 * encoding / decoding of a nvlist header (nvlist_t) 2020 * calculates the size used for header and end detection 2021 * 2022 * - nvs_nvpair 2023 * responsible for the first part of encoding / decoding of an nvpair 2024 * calculates the decoded size of an nvpair 2025 * 2026 * - nvs_nvp_op 2027 * second part of encoding / decoding of an nvpair 2028 * 2029 * - nvs_nvp_size 2030 * calculates the encoding size of an nvpair 2031 * 2032 * - nvs_nvl_fini 2033 * encodes the end detection mark (zeros). 2034 */ 2035 struct nvs_ops { 2036 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *); 2037 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *); 2038 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *); 2039 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *); 2040 int (*nvs_nvl_fini)(nvstream_t *); 2041 }; 2042 2043 typedef struct { 2044 char nvh_encoding; /* nvs encoding method */ 2045 char nvh_endian; /* nvs endian */ 2046 char nvh_reserved1; /* reserved for future use */ 2047 char nvh_reserved2; /* reserved for future use */ 2048 } nvs_header_t; 2049 2050 static int 2051 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl) 2052 { 2053 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 2054 i_nvp_t *curr; 2055 2056 /* 2057 * Walk nvpair in list and encode each nvpair 2058 */ 2059 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) 2060 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0) 2061 return (EFAULT); 2062 2063 return (nvs->nvs_ops->nvs_nvl_fini(nvs)); 2064 } 2065 2066 static int 2067 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl) 2068 { 2069 nvpair_t *nvp; 2070 size_t nvsize; 2071 int err; 2072 2073 /* 2074 * Get decoded size of next pair in stream, alloc 2075 * memory for nvpair_t, then decode the nvpair 2076 */ 2077 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) { 2078 if (nvsize == 0) /* end of list */ 2079 break; 2080 2081 /* make sure len makes sense */ 2082 if (nvsize < NVP_SIZE_CALC(1, 0)) 2083 return (EFAULT); 2084 2085 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL) 2086 return (ENOMEM); 2087 2088 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) { 2089 nvp_buf_free(nvl, nvp); 2090 return (err); 2091 } 2092 2093 if (i_validate_nvpair(nvp) != 0) { 2094 nvpair_free(nvp); 2095 nvp_buf_free(nvl, nvp); 2096 return (EFAULT); 2097 } 2098 2099 nvp_buf_link(nvl, nvp); 2100 } 2101 return (err); 2102 } 2103 2104 static int 2105 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen) 2106 { 2107 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv; 2108 i_nvp_t *curr; 2109 uint64_t nvsize = *buflen; 2110 size_t size; 2111 2112 /* 2113 * Get encoded size of nvpairs in nvlist 2114 */ 2115 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) { 2116 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0) 2117 return (EINVAL); 2118 2119 if ((nvsize += size) > INT32_MAX) 2120 return (EINVAL); 2121 } 2122 2123 *buflen = nvsize; 2124 return (0); 2125 } 2126 2127 static int 2128 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen) 2129 { 2130 int err; 2131 2132 if (nvl->nvl_priv == 0) 2133 return (EFAULT); 2134 2135 /* 2136 * Perform the operation, starting with header, then each nvpair 2137 */ 2138 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0) 2139 return (err); 2140 2141 switch (nvs->nvs_op) { 2142 case NVS_OP_ENCODE: 2143 err = nvs_encode_pairs(nvs, nvl); 2144 break; 2145 2146 case NVS_OP_DECODE: 2147 err = nvs_decode_pairs(nvs, nvl); 2148 break; 2149 2150 case NVS_OP_GETSIZE: 2151 err = nvs_getsize_pairs(nvs, nvl, buflen); 2152 break; 2153 2154 default: 2155 err = EINVAL; 2156 } 2157 2158 return (err); 2159 } 2160 2161 static int 2162 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded) 2163 { 2164 switch (nvs->nvs_op) { 2165 case NVS_OP_ENCODE: 2166 return (nvs_operation(nvs, embedded, NULL)); 2167 2168 case NVS_OP_DECODE: { 2169 nvpriv_t *priv; 2170 int err; 2171 2172 if (embedded->nvl_version != NV_VERSION) 2173 return (ENOTSUP); 2174 2175 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL) 2176 return (ENOMEM); 2177 2178 nvlist_init(embedded, embedded->nvl_nvflag, priv); 2179 2180 if ((err = nvs_operation(nvs, embedded, NULL)) != 0) 2181 nvlist_free(embedded); 2182 return (err); 2183 } 2184 default: 2185 break; 2186 } 2187 2188 return (EINVAL); 2189 } 2190 2191 static int 2192 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size) 2193 { 2194 size_t nelem = NVP_NELEM(nvp); 2195 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp); 2196 int i; 2197 2198 switch (nvs->nvs_op) { 2199 case NVS_OP_ENCODE: 2200 for (i = 0; i < nelem; i++) 2201 if (nvs_embedded(nvs, nvlp[i]) != 0) 2202 return (EFAULT); 2203 break; 2204 2205 case NVS_OP_DECODE: { 2206 size_t len = nelem * sizeof (uint64_t); 2207 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len); 2208 2209 bzero(nvlp, len); /* don't trust packed data */ 2210 for (i = 0; i < nelem; i++) { 2211 if (nvs_embedded(nvs, embedded) != 0) { 2212 nvpair_free(nvp); 2213 return (EFAULT); 2214 } 2215 2216 nvlp[i] = embedded++; 2217 } 2218 break; 2219 } 2220 case NVS_OP_GETSIZE: { 2221 uint64_t nvsize = 0; 2222 2223 for (i = 0; i < nelem; i++) { 2224 size_t nvp_sz = 0; 2225 2226 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0) 2227 return (EINVAL); 2228 2229 if ((nvsize += nvp_sz) > INT32_MAX) 2230 return (EINVAL); 2231 } 2232 2233 *size = nvsize; 2234 break; 2235 } 2236 default: 2237 return (EINVAL); 2238 } 2239 2240 return (0); 2241 } 2242 2243 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *); 2244 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *); 2245 2246 /* 2247 * Common routine for nvlist operations: 2248 * encode, decode, getsize (encoded size). 2249 */ 2250 static int 2251 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding, 2252 int nvs_op) 2253 { 2254 int err = 0; 2255 nvstream_t nvs; 2256 int nvl_endian; 2257 #ifdef _LITTLE_ENDIAN 2258 int host_endian = 1; 2259 #else 2260 int host_endian = 0; 2261 #endif /* _LITTLE_ENDIAN */ 2262 nvs_header_t *nvh = (void *)buf; 2263 2264 if (buflen == NULL || nvl == NULL || 2265 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL) 2266 return (EINVAL); 2267 2268 nvs.nvs_op = nvs_op; 2269 2270 /* 2271 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and 2272 * a buffer is allocated. The first 4 bytes in the buffer are 2273 * used for encoding method and host endian. 2274 */ 2275 switch (nvs_op) { 2276 case NVS_OP_ENCODE: 2277 if (buf == NULL || *buflen < sizeof (nvs_header_t)) 2278 return (EINVAL); 2279 2280 nvh->nvh_encoding = encoding; 2281 nvh->nvh_endian = nvl_endian = host_endian; 2282 nvh->nvh_reserved1 = 0; 2283 nvh->nvh_reserved2 = 0; 2284 break; 2285 2286 case NVS_OP_DECODE: 2287 if (buf == NULL || *buflen < sizeof (nvs_header_t)) 2288 return (EINVAL); 2289 2290 /* get method of encoding from first byte */ 2291 encoding = nvh->nvh_encoding; 2292 nvl_endian = nvh->nvh_endian; 2293 break; 2294 2295 case NVS_OP_GETSIZE: 2296 nvl_endian = host_endian; 2297 2298 /* 2299 * add the size for encoding 2300 */ 2301 *buflen = sizeof (nvs_header_t); 2302 break; 2303 2304 default: 2305 return (ENOTSUP); 2306 } 2307 2308 /* 2309 * Create an nvstream with proper encoding method 2310 */ 2311 switch (encoding) { 2312 case NV_ENCODE_NATIVE: 2313 /* 2314 * check endianness, in case we are unpacking 2315 * from a file 2316 */ 2317 if (nvl_endian != host_endian) 2318 return (ENOTSUP); 2319 err = nvs_native(&nvs, nvl, buf, buflen); 2320 break; 2321 case NV_ENCODE_XDR: 2322 err = nvs_xdr(&nvs, nvl, buf, buflen); 2323 break; 2324 default: 2325 err = ENOTSUP; 2326 break; 2327 } 2328 2329 return (err); 2330 } 2331 2332 int 2333 nvlist_size(nvlist_t *nvl, size_t *size, int encoding) 2334 { 2335 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE)); 2336 } 2337 2338 /* 2339 * Pack nvlist into contiguous memory 2340 */ 2341 /*ARGSUSED1*/ 2342 int 2343 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding, 2344 int kmflag) 2345 { 2346 #if defined(_KERNEL) && !defined(_BOOT) 2347 return (nvlist_xpack(nvl, bufp, buflen, encoding, 2348 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep))); 2349 #else 2350 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep)); 2351 #endif 2352 } 2353 2354 int 2355 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding, 2356 nv_alloc_t *nva) 2357 { 2358 nvpriv_t nvpriv; 2359 size_t alloc_size; 2360 char *buf; 2361 int err; 2362 2363 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL) 2364 return (EINVAL); 2365 2366 if (*bufp != NULL) 2367 return (nvlist_common(nvl, *bufp, buflen, encoding, 2368 NVS_OP_ENCODE)); 2369 2370 /* 2371 * Here is a difficult situation: 2372 * 1. The nvlist has fixed allocator properties. 2373 * All other nvlist routines (like nvlist_add_*, ...) use 2374 * these properties. 2375 * 2. When using nvlist_pack() the user can specify his own 2376 * allocator properties (e.g. by using KM_NOSLEEP). 2377 * 2378 * We use the user specified properties (2). A clearer solution 2379 * will be to remove the kmflag from nvlist_pack(), but we will 2380 * not change the interface. 2381 */ 2382 nv_priv_init(&nvpriv, nva, 0); 2383 2384 if (err = nvlist_size(nvl, &alloc_size, encoding)) 2385 return (err); 2386 2387 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL) 2388 return (ENOMEM); 2389 2390 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding, 2391 NVS_OP_ENCODE)) != 0) { 2392 nv_mem_free(&nvpriv, buf, alloc_size); 2393 } else { 2394 *buflen = alloc_size; 2395 *bufp = buf; 2396 } 2397 2398 return (err); 2399 } 2400 2401 /* 2402 * Unpack buf into an nvlist_t 2403 */ 2404 /*ARGSUSED1*/ 2405 int 2406 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag) 2407 { 2408 #if defined(_KERNEL) && !defined(_BOOT) 2409 return (nvlist_xunpack(buf, buflen, nvlp, 2410 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep))); 2411 #else 2412 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep)); 2413 #endif 2414 } 2415 2416 int 2417 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva) 2418 { 2419 nvlist_t *nvl; 2420 int err; 2421 2422 if (nvlp == NULL) 2423 return (EINVAL); 2424 2425 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0) 2426 return (err); 2427 2428 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0) 2429 nvlist_free(nvl); 2430 else 2431 *nvlp = nvl; 2432 2433 return (err); 2434 } 2435 2436 /* 2437 * Native encoding functions 2438 */ 2439 typedef struct { 2440 /* 2441 * This structure is used when decoding a packed nvpair in 2442 * the native format. n_base points to a buffer containing the 2443 * packed nvpair. n_end is a pointer to the end of the buffer. 2444 * (n_end actually points to the first byte past the end of the 2445 * buffer.) n_curr is a pointer that lies between n_base and n_end. 2446 * It points to the current data that we are decoding. 2447 * The amount of data left in the buffer is equal to n_end - n_curr. 2448 * n_flag is used to recognize a packed embedded list. 2449 */ 2450 caddr_t n_base; 2451 caddr_t n_end; 2452 caddr_t n_curr; 2453 uint_t n_flag; 2454 } nvs_native_t; 2455 2456 static int 2457 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf, 2458 size_t buflen) 2459 { 2460 switch (nvs->nvs_op) { 2461 case NVS_OP_ENCODE: 2462 case NVS_OP_DECODE: 2463 nvs->nvs_private = native; 2464 native->n_curr = native->n_base = buf; 2465 native->n_end = buf + buflen; 2466 native->n_flag = 0; 2467 return (0); 2468 2469 case NVS_OP_GETSIZE: 2470 nvs->nvs_private = native; 2471 native->n_curr = native->n_base = native->n_end = NULL; 2472 native->n_flag = 0; 2473 return (0); 2474 default: 2475 return (EINVAL); 2476 } 2477 } 2478 2479 /*ARGSUSED*/ 2480 static void 2481 nvs_native_destroy(nvstream_t *nvs) 2482 { 2483 } 2484 2485 static int 2486 native_cp(nvstream_t *nvs, void *buf, size_t size) 2487 { 2488 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2489 2490 if (native->n_curr + size > native->n_end) 2491 return (EFAULT); 2492 2493 /* 2494 * The bcopy() below eliminates alignment requirement 2495 * on the buffer (stream) and is preferred over direct access. 2496 */ 2497 switch (nvs->nvs_op) { 2498 case NVS_OP_ENCODE: 2499 bcopy(buf, native->n_curr, size); 2500 break; 2501 case NVS_OP_DECODE: 2502 bcopy(native->n_curr, buf, size); 2503 break; 2504 default: 2505 return (EINVAL); 2506 } 2507 2508 native->n_curr += size; 2509 return (0); 2510 } 2511 2512 /* 2513 * operate on nvlist_t header 2514 */ 2515 static int 2516 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size) 2517 { 2518 nvs_native_t *native = nvs->nvs_private; 2519 2520 switch (nvs->nvs_op) { 2521 case NVS_OP_ENCODE: 2522 case NVS_OP_DECODE: 2523 if (native->n_flag) 2524 return (0); /* packed embedded list */ 2525 2526 native->n_flag = 1; 2527 2528 /* copy version and nvflag of the nvlist_t */ 2529 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 || 2530 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0) 2531 return (EFAULT); 2532 2533 return (0); 2534 2535 case NVS_OP_GETSIZE: 2536 /* 2537 * if calculate for packed embedded list 2538 * 4 for end of the embedded list 2539 * else 2540 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag 2541 * and 4 for end of the entire list 2542 */ 2543 if (native->n_flag) { 2544 *size += 4; 2545 } else { 2546 native->n_flag = 1; 2547 *size += 2 * sizeof (int32_t) + 4; 2548 } 2549 2550 return (0); 2551 2552 default: 2553 return (EINVAL); 2554 } 2555 } 2556 2557 static int 2558 nvs_native_nvl_fini(nvstream_t *nvs) 2559 { 2560 if (nvs->nvs_op == NVS_OP_ENCODE) { 2561 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2562 /* 2563 * Add 4 zero bytes at end of nvlist. They are used 2564 * for end detection by the decode routine. 2565 */ 2566 if (native->n_curr + sizeof (int) > native->n_end) 2567 return (EFAULT); 2568 2569 bzero(native->n_curr, sizeof (int)); 2570 native->n_curr += sizeof (int); 2571 } 2572 2573 return (0); 2574 } 2575 2576 static int 2577 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp) 2578 { 2579 if (nvs->nvs_op == NVS_OP_ENCODE) { 2580 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2581 nvlist_t *packed = (void *) 2582 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp)); 2583 /* 2584 * Null out the pointer that is meaningless in the packed 2585 * structure. The address may not be aligned, so we have 2586 * to use bzero. 2587 */ 2588 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv)); 2589 } 2590 2591 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp))); 2592 } 2593 2594 static int 2595 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp) 2596 { 2597 if (nvs->nvs_op == NVS_OP_ENCODE) { 2598 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2599 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp); 2600 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t); 2601 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len); 2602 int i; 2603 /* 2604 * Null out pointers that are meaningless in the packed 2605 * structure. The addresses may not be aligned, so we have 2606 * to use bzero. 2607 */ 2608 bzero(value, len); 2609 2610 for (i = 0; i < NVP_NELEM(nvp); i++, packed++) 2611 /* 2612 * Null out the pointer that is meaningless in the 2613 * packed structure. The address may not be aligned, 2614 * so we have to use bzero. 2615 */ 2616 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv)); 2617 } 2618 2619 return (nvs_embedded_nvl_array(nvs, nvp, NULL)); 2620 } 2621 2622 static void 2623 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp) 2624 { 2625 switch (nvs->nvs_op) { 2626 case NVS_OP_ENCODE: { 2627 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2628 uint64_t *strp = (void *) 2629 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp)); 2630 /* 2631 * Null out pointers that are meaningless in the packed 2632 * structure. The addresses may not be aligned, so we have 2633 * to use bzero. 2634 */ 2635 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t)); 2636 break; 2637 } 2638 case NVS_OP_DECODE: { 2639 char **strp = (void *)NVP_VALUE(nvp); 2640 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t)); 2641 int i; 2642 2643 for (i = 0; i < NVP_NELEM(nvp); i++) { 2644 strp[i] = buf; 2645 buf += strlen(buf) + 1; 2646 } 2647 break; 2648 } 2649 } 2650 } 2651 2652 static int 2653 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp) 2654 { 2655 data_type_t type; 2656 int value_sz; 2657 int ret = 0; 2658 2659 /* 2660 * We do the initial bcopy of the data before we look at 2661 * the nvpair type, because when we're decoding, we won't 2662 * have the correct values for the pair until we do the bcopy. 2663 */ 2664 switch (nvs->nvs_op) { 2665 case NVS_OP_ENCODE: 2666 case NVS_OP_DECODE: 2667 if (native_cp(nvs, nvp, nvp->nvp_size) != 0) 2668 return (EFAULT); 2669 break; 2670 default: 2671 return (EINVAL); 2672 } 2673 2674 /* verify nvp_name_sz, check the name string length */ 2675 if (i_validate_nvpair_name(nvp) != 0) 2676 return (EFAULT); 2677 2678 type = NVP_TYPE(nvp); 2679 2680 /* 2681 * Verify type and nelem and get the value size. 2682 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY 2683 * is the size of the string(s) excluded. 2684 */ 2685 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0) 2686 return (EFAULT); 2687 2688 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size) 2689 return (EFAULT); 2690 2691 switch (type) { 2692 case DATA_TYPE_NVLIST: 2693 ret = nvpair_native_embedded(nvs, nvp); 2694 break; 2695 case DATA_TYPE_NVLIST_ARRAY: 2696 ret = nvpair_native_embedded_array(nvs, nvp); 2697 break; 2698 case DATA_TYPE_STRING_ARRAY: 2699 nvpair_native_string_array(nvs, nvp); 2700 break; 2701 default: 2702 break; 2703 } 2704 2705 return (ret); 2706 } 2707 2708 static int 2709 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size) 2710 { 2711 uint64_t nvp_sz = nvp->nvp_size; 2712 2713 switch (NVP_TYPE(nvp)) { 2714 case DATA_TYPE_NVLIST: { 2715 size_t nvsize = 0; 2716 2717 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0) 2718 return (EINVAL); 2719 2720 nvp_sz += nvsize; 2721 break; 2722 } 2723 case DATA_TYPE_NVLIST_ARRAY: { 2724 size_t nvsize; 2725 2726 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0) 2727 return (EINVAL); 2728 2729 nvp_sz += nvsize; 2730 break; 2731 } 2732 default: 2733 break; 2734 } 2735 2736 if (nvp_sz > INT32_MAX) 2737 return (EINVAL); 2738 2739 *size = nvp_sz; 2740 2741 return (0); 2742 } 2743 2744 static int 2745 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size) 2746 { 2747 switch (nvs->nvs_op) { 2748 case NVS_OP_ENCODE: 2749 return (nvs_native_nvp_op(nvs, nvp)); 2750 2751 case NVS_OP_DECODE: { 2752 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private; 2753 int32_t decode_len; 2754 2755 /* try to read the size value from the stream */ 2756 if (native->n_curr + sizeof (int32_t) > native->n_end) 2757 return (EFAULT); 2758 bcopy(native->n_curr, &decode_len, sizeof (int32_t)); 2759 2760 /* sanity check the size value */ 2761 if (decode_len < 0 || 2762 decode_len > native->n_end - native->n_curr) 2763 return (EFAULT); 2764 2765 *size = decode_len; 2766 2767 /* 2768 * If at the end of the stream then move the cursor 2769 * forward, otherwise nvpair_native_op() will read 2770 * the entire nvpair at the same cursor position. 2771 */ 2772 if (*size == 0) 2773 native->n_curr += sizeof (int32_t); 2774 break; 2775 } 2776 2777 default: 2778 return (EINVAL); 2779 } 2780 2781 return (0); 2782 } 2783 2784 static const nvs_ops_t nvs_native_ops = { 2785 nvs_native_nvlist, 2786 nvs_native_nvpair, 2787 nvs_native_nvp_op, 2788 nvs_native_nvp_size, 2789 nvs_native_nvl_fini 2790 }; 2791 2792 static int 2793 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen) 2794 { 2795 nvs_native_t native; 2796 int err; 2797 2798 nvs->nvs_ops = &nvs_native_ops; 2799 2800 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t), 2801 *buflen - sizeof (nvs_header_t))) != 0) 2802 return (err); 2803 2804 err = nvs_operation(nvs, nvl, buflen); 2805 2806 nvs_native_destroy(nvs); 2807 2808 return (err); 2809 } 2810 2811 /* 2812 * XDR encoding functions 2813 * 2814 * An xdr packed nvlist is encoded as: 2815 * 2816 * - encoding methode and host endian (4 bytes) 2817 * - nvl_version (4 bytes) 2818 * - nvl_nvflag (4 bytes) 2819 * 2820 * - encoded nvpairs, the format of one xdr encoded nvpair is: 2821 * - encoded size of the nvpair (4 bytes) 2822 * - decoded size of the nvpair (4 bytes) 2823 * - name string, (4 + sizeof(NV_ALIGN4(string)) 2824 * a string is coded as size (4 bytes) and data 2825 * - data type (4 bytes) 2826 * - number of elements in the nvpair (4 bytes) 2827 * - data 2828 * 2829 * - 2 zero's for end of the entire list (8 bytes) 2830 */ 2831 static int 2832 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen) 2833 { 2834 /* xdr data must be 4 byte aligned */ 2835 if ((ulong_t)buf % 4 != 0) 2836 return (EFAULT); 2837 2838 switch (nvs->nvs_op) { 2839 case NVS_OP_ENCODE: 2840 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE); 2841 nvs->nvs_private = xdr; 2842 return (0); 2843 case NVS_OP_DECODE: 2844 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE); 2845 nvs->nvs_private = xdr; 2846 return (0); 2847 case NVS_OP_GETSIZE: 2848 nvs->nvs_private = NULL; 2849 return (0); 2850 default: 2851 return (EINVAL); 2852 } 2853 } 2854 2855 static void 2856 nvs_xdr_destroy(nvstream_t *nvs) 2857 { 2858 switch (nvs->nvs_op) { 2859 case NVS_OP_ENCODE: 2860 case NVS_OP_DECODE: 2861 xdr_destroy((XDR *)nvs->nvs_private); 2862 break; 2863 default: 2864 break; 2865 } 2866 } 2867 2868 static int 2869 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size) 2870 { 2871 switch (nvs->nvs_op) { 2872 case NVS_OP_ENCODE: 2873 case NVS_OP_DECODE: { 2874 XDR *xdr = nvs->nvs_private; 2875 2876 if (!xdr_int(xdr, &nvl->nvl_version) || 2877 !xdr_u_int(xdr, &nvl->nvl_nvflag)) 2878 return (EFAULT); 2879 break; 2880 } 2881 case NVS_OP_GETSIZE: { 2882 /* 2883 * 2 * 4 for nvl_version + nvl_nvflag 2884 * and 8 for end of the entire list 2885 */ 2886 *size += 2 * 4 + 8; 2887 break; 2888 } 2889 default: 2890 return (EINVAL); 2891 } 2892 return (0); 2893 } 2894 2895 static int 2896 nvs_xdr_nvl_fini(nvstream_t *nvs) 2897 { 2898 if (nvs->nvs_op == NVS_OP_ENCODE) { 2899 XDR *xdr = nvs->nvs_private; 2900 int zero = 0; 2901 2902 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero)) 2903 return (EFAULT); 2904 } 2905 2906 return (0); 2907 } 2908 2909 /* 2910 * The format of xdr encoded nvpair is: 2911 * encode_size, decode_size, name string, data type, nelem, data 2912 */ 2913 static int 2914 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp) 2915 { 2916 data_type_t type; 2917 char *buf; 2918 char *buf_end = (char *)nvp + nvp->nvp_size; 2919 int value_sz; 2920 uint_t nelem, buflen; 2921 bool_t ret = FALSE; 2922 XDR *xdr = nvs->nvs_private; 2923 2924 ASSERT(xdr != NULL && nvp != NULL); 2925 2926 /* name string */ 2927 if ((buf = NVP_NAME(nvp)) >= buf_end) 2928 return (EFAULT); 2929 buflen = buf_end - buf; 2930 2931 if (!xdr_string(xdr, &buf, buflen - 1)) 2932 return (EFAULT); 2933 nvp->nvp_name_sz = strlen(buf) + 1; 2934 2935 /* type and nelem */ 2936 if (!xdr_int(xdr, (int *)&nvp->nvp_type) || 2937 !xdr_int(xdr, &nvp->nvp_value_elem)) 2938 return (EFAULT); 2939 2940 type = NVP_TYPE(nvp); 2941 nelem = nvp->nvp_value_elem; 2942 2943 /* 2944 * Verify type and nelem and get the value size. 2945 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY 2946 * is the size of the string(s) excluded. 2947 */ 2948 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0) 2949 return (EFAULT); 2950 2951 /* if there is no data to extract then return */ 2952 if (nelem == 0) 2953 return (0); 2954 2955 /* value */ 2956 if ((buf = NVP_VALUE(nvp)) >= buf_end) 2957 return (EFAULT); 2958 buflen = buf_end - buf; 2959 2960 if (buflen < value_sz) 2961 return (EFAULT); 2962 2963 switch (type) { 2964 case DATA_TYPE_NVLIST: 2965 if (nvs_embedded(nvs, (void *)buf) == 0) 2966 return (0); 2967 break; 2968 2969 case DATA_TYPE_NVLIST_ARRAY: 2970 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0) 2971 return (0); 2972 break; 2973 2974 case DATA_TYPE_BOOLEAN: 2975 ret = TRUE; 2976 break; 2977 2978 case DATA_TYPE_BYTE: 2979 case DATA_TYPE_INT8: 2980 case DATA_TYPE_UINT8: 2981 ret = xdr_char(xdr, buf); 2982 break; 2983 2984 case DATA_TYPE_INT16: 2985 ret = xdr_short(xdr, (void *)buf); 2986 break; 2987 2988 case DATA_TYPE_UINT16: 2989 ret = xdr_u_short(xdr, (void *)buf); 2990 break; 2991 2992 case DATA_TYPE_BOOLEAN_VALUE: 2993 case DATA_TYPE_INT32: 2994 ret = xdr_int(xdr, (void *)buf); 2995 break; 2996 2997 case DATA_TYPE_UINT32: 2998 ret = xdr_u_int(xdr, (void *)buf); 2999 break; 3000 3001 case DATA_TYPE_INT64: 3002 ret = xdr_longlong_t(xdr, (void *)buf); 3003 break; 3004 3005 case DATA_TYPE_UINT64: 3006 ret = xdr_u_longlong_t(xdr, (void *)buf); 3007 break; 3008 3009 case DATA_TYPE_HRTIME: 3010 /* 3011 * NOTE: must expose the definition of hrtime_t here 3012 */ 3013 ret = xdr_longlong_t(xdr, (void *)buf); 3014 break; 3015 #if !defined(_KERNEL) 3016 case DATA_TYPE_DOUBLE: 3017 ret = xdr_double(xdr, (void *)buf); 3018 break; 3019 #endif 3020 case DATA_TYPE_STRING: 3021 ret = xdr_string(xdr, &buf, buflen - 1); 3022 break; 3023 3024 case DATA_TYPE_BYTE_ARRAY: 3025 ret = xdr_opaque(xdr, buf, nelem); 3026 break; 3027 3028 case DATA_TYPE_INT8_ARRAY: 3029 case DATA_TYPE_UINT8_ARRAY: 3030 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t), 3031 (xdrproc_t)xdr_char); 3032 break; 3033 3034 case DATA_TYPE_INT16_ARRAY: 3035 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t), 3036 sizeof (int16_t), (xdrproc_t)xdr_short); 3037 break; 3038 3039 case DATA_TYPE_UINT16_ARRAY: 3040 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t), 3041 sizeof (uint16_t), (xdrproc_t)xdr_u_short); 3042 break; 3043 3044 case DATA_TYPE_BOOLEAN_ARRAY: 3045 case DATA_TYPE_INT32_ARRAY: 3046 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t), 3047 sizeof (int32_t), (xdrproc_t)xdr_int); 3048 break; 3049 3050 case DATA_TYPE_UINT32_ARRAY: 3051 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t), 3052 sizeof (uint32_t), (xdrproc_t)xdr_u_int); 3053 break; 3054 3055 case DATA_TYPE_INT64_ARRAY: 3056 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t), 3057 sizeof (int64_t), (xdrproc_t)xdr_longlong_t); 3058 break; 3059 3060 case DATA_TYPE_UINT64_ARRAY: 3061 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t), 3062 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t); 3063 break; 3064 3065 case DATA_TYPE_STRING_ARRAY: { 3066 size_t len = nelem * sizeof (uint64_t); 3067 char **strp = (void *)buf; 3068 int i; 3069 3070 if (nvs->nvs_op == NVS_OP_DECODE) 3071 bzero(buf, len); /* don't trust packed data */ 3072 3073 for (i = 0; i < nelem; i++) { 3074 if (buflen <= len) 3075 return (EFAULT); 3076 3077 buf += len; 3078 buflen -= len; 3079 3080 if (xdr_string(xdr, &buf, buflen - 1) != TRUE) 3081 return (EFAULT); 3082 3083 if (nvs->nvs_op == NVS_OP_DECODE) 3084 strp[i] = buf; 3085 len = strlen(buf) + 1; 3086 } 3087 ret = TRUE; 3088 break; 3089 } 3090 default: 3091 break; 3092 } 3093 3094 return (ret == TRUE ? 0 : EFAULT); 3095 } 3096 3097 static int 3098 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size) 3099 { 3100 data_type_t type = NVP_TYPE(nvp); 3101 /* 3102 * encode_size + decode_size + name string size + data type + nelem 3103 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) 3104 */ 3105 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4; 3106 3107 switch (type) { 3108 case DATA_TYPE_BOOLEAN: 3109 break; 3110 3111 case DATA_TYPE_BOOLEAN_VALUE: 3112 case DATA_TYPE_BYTE: 3113 case DATA_TYPE_INT8: 3114 case DATA_TYPE_UINT8: 3115 case DATA_TYPE_INT16: 3116 case DATA_TYPE_UINT16: 3117 case DATA_TYPE_INT32: 3118 case DATA_TYPE_UINT32: 3119 nvp_sz += 4; /* 4 is the minimum xdr unit */ 3120 break; 3121 3122 case DATA_TYPE_INT64: 3123 case DATA_TYPE_UINT64: 3124 case DATA_TYPE_HRTIME: 3125 #if !defined(_KERNEL) 3126 case DATA_TYPE_DOUBLE: 3127 #endif 3128 nvp_sz += 8; 3129 break; 3130 3131 case DATA_TYPE_STRING: 3132 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp))); 3133 break; 3134 3135 case DATA_TYPE_BYTE_ARRAY: 3136 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp)); 3137 break; 3138 3139 case DATA_TYPE_BOOLEAN_ARRAY: 3140 case DATA_TYPE_INT8_ARRAY: 3141 case DATA_TYPE_UINT8_ARRAY: 3142 case DATA_TYPE_INT16_ARRAY: 3143 case DATA_TYPE_UINT16_ARRAY: 3144 case DATA_TYPE_INT32_ARRAY: 3145 case DATA_TYPE_UINT32_ARRAY: 3146 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp); 3147 break; 3148 3149 case DATA_TYPE_INT64_ARRAY: 3150 case DATA_TYPE_UINT64_ARRAY: 3151 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp); 3152 break; 3153 3154 case DATA_TYPE_STRING_ARRAY: { 3155 int i; 3156 char **strs = (void *)NVP_VALUE(nvp); 3157 3158 for (i = 0; i < NVP_NELEM(nvp); i++) 3159 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i])); 3160 3161 break; 3162 } 3163 3164 case DATA_TYPE_NVLIST: 3165 case DATA_TYPE_NVLIST_ARRAY: { 3166 size_t nvsize = 0; 3167 int old_nvs_op = nvs->nvs_op; 3168 int err; 3169 3170 nvs->nvs_op = NVS_OP_GETSIZE; 3171 if (type == DATA_TYPE_NVLIST) 3172 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize); 3173 else 3174 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize); 3175 nvs->nvs_op = old_nvs_op; 3176 3177 if (err != 0) 3178 return (EINVAL); 3179 3180 nvp_sz += nvsize; 3181 break; 3182 } 3183 3184 default: 3185 return (EINVAL); 3186 } 3187 3188 if (nvp_sz > INT32_MAX) 3189 return (EINVAL); 3190 3191 *size = nvp_sz; 3192 3193 return (0); 3194 } 3195 3196 3197 /* 3198 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates 3199 * the largest nvpair that could be encoded in the buffer. 3200 * 3201 * See comments above nvpair_xdr_op() for the format of xdr encoding. 3202 * The size of a xdr packed nvpair without any data is 5 words. 3203 * 3204 * Using the size of the data directly as an estimate would be ok 3205 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY 3206 * then the actual nvpair has space for an array of pointers to index 3207 * the strings. These pointers are not encoded into the packed xdr buffer. 3208 * 3209 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are 3210 * of length 0, then each string is endcoded in xdr format as a single word. 3211 * Therefore when expanded to an nvpair there will be 2.25 word used for 3212 * each string. (a int64_t allocated for pointer usage, and a single char 3213 * for the null termination.) 3214 * 3215 * This is the calculation performed by the NVS_XDR_MAX_LEN macro. 3216 */ 3217 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4)) 3218 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \ 3219 0 : ((size_t)(y) - NVS_XDR_HDR_LEN)) 3220 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \ 3221 (NVS_XDR_DATA_LEN(x) * 2) + \ 3222 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4))) 3223 3224 static int 3225 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size) 3226 { 3227 XDR *xdr = nvs->nvs_private; 3228 int32_t encode_len, decode_len; 3229 3230 switch (nvs->nvs_op) { 3231 case NVS_OP_ENCODE: { 3232 size_t nvsize; 3233 3234 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0) 3235 return (EFAULT); 3236 3237 decode_len = nvp->nvp_size; 3238 encode_len = nvsize; 3239 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len)) 3240 return (EFAULT); 3241 3242 return (nvs_xdr_nvp_op(nvs, nvp)); 3243 } 3244 case NVS_OP_DECODE: { 3245 struct xdr_bytesrec bytesrec; 3246 3247 /* get the encode and decode size */ 3248 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len)) 3249 return (EFAULT); 3250 *size = decode_len; 3251 3252 /* are we at the end of the stream? */ 3253 if (*size == 0) 3254 return (0); 3255 3256 /* sanity check the size parameter */ 3257 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec)) 3258 return (EFAULT); 3259 3260 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail)) 3261 return (EFAULT); 3262 break; 3263 } 3264 3265 default: 3266 return (EINVAL); 3267 } 3268 return (0); 3269 } 3270 3271 static const struct nvs_ops nvs_xdr_ops = { 3272 nvs_xdr_nvlist, 3273 nvs_xdr_nvpair, 3274 nvs_xdr_nvp_op, 3275 nvs_xdr_nvp_size, 3276 nvs_xdr_nvl_fini 3277 }; 3278 3279 static int 3280 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen) 3281 { 3282 XDR xdr; 3283 int err; 3284 3285 nvs->nvs_ops = &nvs_xdr_ops; 3286 3287 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t), 3288 *buflen - sizeof (nvs_header_t))) != 0) 3289 return (err); 3290 3291 err = nvs_operation(nvs, nvl, buflen); 3292 3293 nvs_xdr_destroy(nvs); 3294 3295 return (err); 3296 } 3297