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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #include <unistd.h> 26 #include <errno.h> 27 #include <ctype.h> 28 #include <fcntl.h> 29 #include <strings.h> 30 #include <dirent.h> 31 #include <stdlib.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 34 #include <sys/param.h> 35 #include <sys/stat.h> 36 #include <sys/dld.h> 37 #include <sys/dld_ioc.h> 38 #include <libdladm_impl.h> 39 #include <libintl.h> 40 #include <libdlpi.h> 41 #include <libdllink.h> 42 43 static char dladm_rootdir[MAXPATHLEN] = "/"; 44 45 typedef struct media_type_desc { 46 uint32_t media_type; 47 #define MAX_MEDIA_TYPE_STRING 32 48 const char media_type_str[MAX_MEDIA_TYPE_STRING]; 49 } media_type_t; 50 51 static media_type_t media_type_table[] = { 52 { DL_ETHER, "Ethernet" }, 53 { DL_WIFI, "WiFi" }, 54 { DL_IB, "Infiniband" }, 55 { DL_IPV4, "IPv4Tunnel" }, 56 { DL_IPV6, "IPv6Tunnel" }, 57 { DL_6TO4, "6to4Tunnel" }, 58 { DL_CSMACD, "CSMA/CD" }, 59 { DL_TPB, "TokenBus" }, 60 { DL_TPR, "TokenRing" }, 61 { DL_METRO, "MetroNet" }, 62 { DL_HDLC, "HDLC" }, 63 { DL_CHAR, "SyncCharacter" }, 64 { DL_CTCA, "CTCA" }, 65 { DL_FDDI, "FDDI" }, 66 { DL_FC, "FiberChannel" }, 67 { DL_ATM, "ATM" }, 68 { DL_IPATM, "ATM(ClassicIP)" }, 69 { DL_X25, "X.25" }, 70 { DL_IPX25, "X.25(ClassicIP)" }, 71 { DL_ISDN, "ISDN" }, 72 { DL_HIPPI, "HIPPI" }, 73 { DL_100VG, "100BaseVGEthernet" }, 74 { DL_100VGTPR, "100BaseVGTokenRing" }, 75 { DL_ETH_CSMA, "IEEE802.3" }, 76 { DL_100BT, "100BaseT" }, 77 { DL_FRAME, "FrameRelay" }, 78 { DL_MPFRAME, "MPFrameRelay" }, 79 { DL_ASYNC, "AsyncCharacter" }, 80 { DL_IPNET, "IPNET" }, 81 { DL_OTHER, "Other" } 82 }; 83 #define MEDIATYPECOUNT (sizeof (media_type_table) / sizeof (media_type_t)) 84 85 typedef struct { 86 uint32_t lp_type; 87 char *lp_name; 88 } link_protect_t; 89 90 static link_protect_t link_protect_types[] = { 91 { MPT_MACNOSPOOF, "mac-nospoof" }, 92 { MPT_RESTRICTED, "restricted" }, 93 { MPT_IPNOSPOOF, "ip-nospoof" }, 94 { MPT_DHCPNOSPOOF, "dhcp-nospoof" } 95 }; 96 #define LPTYPES (sizeof (link_protect_types) / sizeof (link_protect_t)) 97 98 dladm_status_t 99 dladm_open(dladm_handle_t *handle) 100 { 101 int dld_fd; 102 103 if (handle == NULL) 104 return (DLADM_STATUS_BADARG); 105 106 if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 107 return (dladm_errno2status(errno)); 108 109 /* 110 * Don't open DLMGMT_DOOR now. dlmgmtd(1M) is not able to 111 * open the door when the dladm handle is opened because the 112 * door hasn't been created yet at that time. Thus, we must 113 * open it on-demand in dladm_door_fd(). Move the open() 114 * to dladm_door_fd() for all cases. 115 */ 116 117 if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) { 118 (void) close(dld_fd); 119 return (DLADM_STATUS_NOMEM); 120 } 121 122 (*handle)->dld_fd = dld_fd; 123 (*handle)->door_fd = -1; 124 125 return (DLADM_STATUS_OK); 126 } 127 128 void 129 dladm_close(dladm_handle_t handle) 130 { 131 if (handle != NULL) { 132 (void) close(handle->dld_fd); 133 if (handle->door_fd != -1) 134 (void) close(handle->door_fd); 135 free(handle); 136 } 137 } 138 139 int 140 dladm_dld_fd(dladm_handle_t handle) 141 { 142 return (handle->dld_fd); 143 } 144 145 /* 146 * If DLMGMT_DOOR hasn't been opened in the handle yet, open it. 147 */ 148 dladm_status_t 149 dladm_door_fd(dladm_handle_t handle, int *door_fd) 150 { 151 int fd; 152 153 if (handle->door_fd == -1) { 154 if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0) 155 return (dladm_errno2status(errno)); 156 handle->door_fd = fd; 157 } 158 *door_fd = handle->door_fd; 159 160 return (DLADM_STATUS_OK); 161 } 162 163 const char * 164 dladm_status2str(dladm_status_t status, char *buf) 165 { 166 const char *s; 167 168 switch (status) { 169 case DLADM_STATUS_OK: 170 s = "ok"; 171 break; 172 case DLADM_STATUS_BADARG: 173 s = "invalid argument"; 174 break; 175 case DLADM_STATUS_FAILED: 176 s = "operation failed"; 177 break; 178 case DLADM_STATUS_TOOSMALL: 179 s = "buffer size too small"; 180 break; 181 case DLADM_STATUS_NOTSUP: 182 s = "operation not supported"; 183 break; 184 case DLADM_STATUS_NOTFOUND: 185 s = "object not found"; 186 break; 187 case DLADM_STATUS_BADVAL: 188 s = "invalid value"; 189 break; 190 case DLADM_STATUS_NOMEM: 191 s = "insufficient memory"; 192 break; 193 case DLADM_STATUS_EXIST: 194 s = "object already exists"; 195 break; 196 case DLADM_STATUS_LINKINVAL: 197 s = "invalid link"; 198 break; 199 case DLADM_STATUS_PROPRDONLY: 200 s = "read-only property"; 201 break; 202 case DLADM_STATUS_BADVALCNT: 203 s = "invalid number of values"; 204 break; 205 case DLADM_STATUS_DBNOTFOUND: 206 s = "database not found"; 207 break; 208 case DLADM_STATUS_DENIED: 209 s = "permission denied"; 210 break; 211 case DLADM_STATUS_IOERR: 212 s = "I/O error"; 213 break; 214 case DLADM_STATUS_TEMPONLY: 215 s = "change cannot be persistent"; 216 break; 217 case DLADM_STATUS_TIMEDOUT: 218 s = "operation timed out"; 219 break; 220 case DLADM_STATUS_ISCONN: 221 s = "already connected"; 222 break; 223 case DLADM_STATUS_NOTCONN: 224 s = "not connected"; 225 break; 226 case DLADM_STATUS_REPOSITORYINVAL: 227 s = "invalid configuration repository"; 228 break; 229 case DLADM_STATUS_MACADDRINVAL: 230 s = "invalid MAC address"; 231 break; 232 case DLADM_STATUS_KEYINVAL: 233 s = "invalid key"; 234 break; 235 case DLADM_STATUS_INVALIDMACADDRLEN: 236 s = "invalid MAC address length"; 237 break; 238 case DLADM_STATUS_INVALIDMACADDRTYPE: 239 s = "invalid MAC address type"; 240 break; 241 case DLADM_STATUS_LINKBUSY: 242 s = "link busy"; 243 break; 244 case DLADM_STATUS_VIDINVAL: 245 s = "invalid VLAN identifier"; 246 break; 247 case DLADM_STATUS_TRYAGAIN: 248 s = "try again later"; 249 break; 250 case DLADM_STATUS_NONOTIF: 251 s = "link notification is not supported"; 252 break; 253 case DLADM_STATUS_BADTIMEVAL: 254 s = "invalid time range"; 255 break; 256 case DLADM_STATUS_INVALIDMACADDR: 257 s = "invalid MAC address value"; 258 break; 259 case DLADM_STATUS_INVALIDMACADDRNIC: 260 s = "MAC address reserved for use by underlying data-link"; 261 break; 262 case DLADM_STATUS_INVALIDMACADDRINUSE: 263 s = "MAC address is already in use"; 264 break; 265 case DLADM_STATUS_MACFACTORYSLOTINVALID: 266 s = "invalid factory MAC address slot"; 267 break; 268 case DLADM_STATUS_MACFACTORYSLOTUSED: 269 s = "factory MAC address slot already used"; 270 break; 271 case DLADM_STATUS_MACFACTORYSLOTALLUSED: 272 s = "all factory MAC address slots are in use"; 273 break; 274 case DLADM_STATUS_MACFACTORYNOTSUP: 275 s = "factory MAC address slots not supported"; 276 break; 277 case DLADM_STATUS_INVALIDMACPREFIX: 278 s = "Invalid MAC address prefix value"; 279 break; 280 case DLADM_STATUS_INVALIDMACPREFIXLEN: 281 s = "Invalid MAC address prefix length"; 282 break; 283 case DLADM_STATUS_BADCPUID: 284 s = "non-existent processor ID"; 285 break; 286 case DLADM_STATUS_CPUERR: 287 s = "could not determine processor status"; 288 break; 289 case DLADM_STATUS_CPUNOTONLINE: 290 s = "processor not online"; 291 break; 292 case DLADM_STATUS_TOOMANYELEMENTS: 293 s = "too many elements specified"; 294 break; 295 case DLADM_STATUS_BADRANGE: 296 s = "invalid range"; 297 break; 298 case DLADM_STATUS_DB_NOTFOUND: 299 s = "database not found"; 300 break; 301 case DLADM_STATUS_DB_PARSE_ERR: 302 s = "database parse error"; 303 break; 304 case DLADM_STATUS_PROP_PARSE_ERR: 305 s = "property parse error"; 306 break; 307 case DLADM_STATUS_ATTR_PARSE_ERR: 308 s = "attribute parse error"; 309 break; 310 case DLADM_STATUS_FLOW_DB_ERR: 311 s = "flow database error"; 312 break; 313 case DLADM_STATUS_FLOW_DB_OPEN_ERR: 314 s = "flow database open error"; 315 break; 316 case DLADM_STATUS_FLOW_DB_PARSE_ERR: 317 s = "flow database parse error"; 318 break; 319 case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR: 320 s = "flow property database parse error"; 321 break; 322 case DLADM_STATUS_FLOW_ADD_ERR: 323 s = "flow add error"; 324 break; 325 case DLADM_STATUS_FLOW_WALK_ERR: 326 s = "flow walk error"; 327 break; 328 case DLADM_STATUS_FLOW_IDENTICAL: 329 s = "a flow with identical attributes exists"; 330 break; 331 case DLADM_STATUS_FLOW_INCOMPATIBLE: 332 s = "flow(s) with incompatible attributes exists"; 333 break; 334 case DLADM_STATUS_FLOW_EXISTS: 335 s = "link still has flows"; 336 break; 337 case DLADM_STATUS_PERSIST_FLOW_EXISTS: 338 s = "persistent flow with the same name exists"; 339 break; 340 case DLADM_STATUS_INVALID_IP: 341 s = "invalid IP address"; 342 break; 343 case DLADM_STATUS_INVALID_PREFIXLEN: 344 s = "invalid IP prefix length"; 345 break; 346 case DLADM_STATUS_INVALID_PROTOCOL: 347 s = "invalid IP protocol"; 348 break; 349 case DLADM_STATUS_INVALID_PORT: 350 s = "invalid port number"; 351 break; 352 case DLADM_STATUS_INVALID_DSF: 353 s = "invalid dsfield"; 354 break; 355 case DLADM_STATUS_INVALID_DSFMASK: 356 s = "invalid dsfield mask"; 357 break; 358 case DLADM_STATUS_INVALID_MACMARGIN: 359 s = "MTU check failed, use lower MTU or -f option"; 360 break; 361 case DLADM_STATUS_BADPROP: 362 s = "invalid property"; 363 break; 364 case DLADM_STATUS_MINMAXBW: 365 s = "minimum value for maxbw is 1200K"; 366 break; 367 case DLADM_STATUS_NO_HWRINGS: 368 s = "request hw rings failed"; 369 break; 370 case DLADM_STATUS_PERMONLY: 371 s = "change must be persistent"; 372 break; 373 case DLADM_STATUS_OPTMISSING: 374 s = "optional software not installed"; 375 break; 376 case DLADM_STATUS_IPTUNTYPE: 377 s = "invalid IP tunnel type"; 378 break; 379 case DLADM_STATUS_IPTUNTYPEREQD: 380 s = "IP tunnel type required"; 381 break; 382 case DLADM_STATUS_BADIPTUNLADDR: 383 s = "invalid local IP tunnel address"; 384 break; 385 case DLADM_STATUS_BADIPTUNRADDR: 386 s = "invalid remote IP tunnel address"; 387 break; 388 case DLADM_STATUS_ADDRINUSE: 389 s = "address already in use"; 390 break; 391 case DLADM_STATUS_POOLCPU: 392 s = "pool and cpus property are mutually exclusive"; 393 break; 394 case DLADM_STATUS_INVALID_PORT_INSTANCE: 395 s = "invalid IB phys link"; 396 break; 397 case DLADM_STATUS_PORT_IS_DOWN: 398 s = "port is down"; 399 break; 400 case DLADM_STATUS_PARTITION_EXISTS: 401 s = "partition already exists"; 402 break; 403 case DLADM_STATUS_PKEY_NOT_PRESENT: 404 s = "PKEY is not present on the port"; 405 break; 406 case DLADM_STATUS_INVALID_PKEY: 407 s = "invalid PKEY"; 408 break; 409 case DLADM_STATUS_NO_IB_HW_RESOURCE: 410 s = "IB internal resource not available"; 411 break; 412 case DLADM_STATUS_INVALID_PKEY_TBL_SIZE: 413 s = "invalid PKEY table size"; 414 break; 415 case DLADM_STATUS_PORT_NOPROTO: 416 s = "local or remote port requires transport"; 417 break; 418 default: 419 s = "<unknown error>"; 420 break; 421 } 422 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 423 return (buf); 424 } 425 426 /* 427 * Convert a unix errno to a dladm_status_t. 428 * We only convert errnos that are likely to be encountered. All others 429 * are mapped to DLADM_STATUS_FAILED. 430 */ 431 dladm_status_t 432 dladm_errno2status(int err) 433 { 434 switch (err) { 435 case 0: 436 return (DLADM_STATUS_OK); 437 case EINVAL: 438 return (DLADM_STATUS_BADARG); 439 case EEXIST: 440 return (DLADM_STATUS_EXIST); 441 case ENOENT: 442 return (DLADM_STATUS_NOTFOUND); 443 case ENOSPC: 444 return (DLADM_STATUS_TOOSMALL); 445 case ENOMEM: 446 return (DLADM_STATUS_NOMEM); 447 case ENOTSUP: 448 return (DLADM_STATUS_NOTSUP); 449 case ENETDOWN: 450 return (DLADM_STATUS_NONOTIF); 451 case EACCES: 452 case EPERM: 453 return (DLADM_STATUS_DENIED); 454 case EIO: 455 return (DLADM_STATUS_IOERR); 456 case EBUSY: 457 return (DLADM_STATUS_LINKBUSY); 458 case EAGAIN: 459 return (DLADM_STATUS_TRYAGAIN); 460 case ENOTEMPTY: 461 return (DLADM_STATUS_FLOW_EXISTS); 462 case EOPNOTSUPP: 463 return (DLADM_STATUS_FLOW_INCOMPATIBLE); 464 case EALREADY: 465 return (DLADM_STATUS_FLOW_IDENTICAL); 466 case EADDRINUSE: 467 return (DLADM_STATUS_ADDRINUSE); 468 default: 469 return (DLADM_STATUS_FAILED); 470 } 471 } 472 473 boolean_t 474 dladm_str2interval(char *oarg, uint32_t *interval) 475 { 476 int val; 477 char *endp = NULL; 478 479 errno = 0; 480 val = strtol(oarg, &endp, 10); 481 if (errno != 0 || val <= 0 || *endp != '\0') 482 return (B_FALSE); 483 484 *interval = val; 485 486 return (B_TRUE); 487 } 488 489 dladm_status_t 490 dladm_str2bw(char *oarg, uint64_t *bw) 491 { 492 char *endp = NULL; 493 int64_t n; 494 int mult = 1; 495 496 n = strtoull(oarg, &endp, 10); 497 498 if ((errno != 0) || (strlen(endp) > 1)) 499 return (DLADM_STATUS_BADARG); 500 501 if (n < 0) 502 return (DLADM_STATUS_BADVAL); 503 504 switch (*endp) { 505 case 'k': 506 case 'K': 507 mult = 1000; 508 break; 509 case 'm': 510 case 'M': 511 case '\0': 512 mult = 1000000; 513 break; 514 case 'g': 515 case 'G': 516 mult = 1000000000; 517 break; 518 case '%': 519 /* 520 * percentages not supported for now, 521 * see RFE 6540675 522 */ 523 return (DLADM_STATUS_NOTSUP); 524 default: 525 return (DLADM_STATUS_BADVAL); 526 } 527 528 *bw = n * mult; 529 530 /* check for overflow */ 531 if (*bw / mult != n) 532 return (DLADM_STATUS_BADARG); 533 534 return (DLADM_STATUS_OK); 535 } 536 537 /* 538 * Convert bandwidth in bps to a string in mpbs. For values greater 539 * than 1mbps or 1000000, print a whole mbps value. For values that 540 * have fractional Mbps in whole Kbps , print the bandwidth in a manner 541 * simlilar to a floating point format. 542 * 543 * bps string 544 * 0 0 545 * 100 0 546 * 2000 0.002 547 * 431000 0.431 548 * 1000000 1 549 * 1030000 1.030 550 * 100000000 100 551 */ 552 const char * 553 dladm_bw2str(int64_t bw, char *buf) 554 { 555 int kbps, mbps; 556 557 kbps = (bw%1000000)/1000; 558 mbps = bw/1000000; 559 if (kbps != 0) { 560 if (mbps == 0) 561 (void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps); 562 else 563 (void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps, 564 kbps); 565 } else { 566 (void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps); 567 } 568 569 return (buf); 570 } 571 572 #define LOCK_DB_PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 573 574 static int 575 i_dladm_lock_db(const char *lock_file, short type) 576 { 577 int lock_fd; 578 struct flock lock; 579 580 if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC, 581 LOCK_DB_PERMS)) < 0) 582 return (-1); 583 584 lock.l_type = type; 585 lock.l_whence = SEEK_SET; 586 lock.l_start = 0; 587 lock.l_len = 0; 588 589 if (fcntl(lock_fd, F_SETLKW, &lock) < 0) { 590 int err = errno; 591 592 (void) close(lock_fd); 593 (void) unlink(lock_file); 594 errno = err; 595 return (-1); 596 } 597 return (lock_fd); 598 } 599 600 static void 601 i_dladm_unlock_db(const char *lock_file, int fd) 602 { 603 struct flock lock; 604 605 if (fd < 0) 606 return; 607 608 lock.l_type = F_UNLCK; 609 lock.l_whence = SEEK_SET; 610 lock.l_start = 0; 611 lock.l_len = 0; 612 613 (void) fcntl(fd, F_SETLKW, &lock); 614 (void) close(fd); 615 (void) unlink(lock_file); 616 } 617 618 /* 619 * Given a link class, returns its class string. 620 */ 621 const char * 622 dladm_class2str(datalink_class_t class, char *buf) 623 { 624 const char *s; 625 626 switch (class) { 627 case DATALINK_CLASS_PHYS: 628 s = "phys"; 629 break; 630 case DATALINK_CLASS_VLAN: 631 s = "vlan"; 632 break; 633 case DATALINK_CLASS_AGGR: 634 s = "aggr"; 635 break; 636 case DATALINK_CLASS_VNIC: 637 s = "vnic"; 638 break; 639 case DATALINK_CLASS_ETHERSTUB: 640 s = "etherstub"; 641 break; 642 case DATALINK_CLASS_IPTUN: 643 s = "iptun"; 644 break; 645 case DATALINK_CLASS_SIMNET: 646 s = "simnet"; 647 break; 648 case DATALINK_CLASS_BRIDGE: 649 s = "bridge"; 650 break; 651 case DATALINK_CLASS_PART: 652 s = "part"; 653 break; 654 default: 655 s = "unknown"; 656 break; 657 } 658 659 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 660 return (buf); 661 } 662 663 /* 664 * Given a physical link media type, returns its media type string. 665 */ 666 const char * 667 dladm_media2str(uint32_t media, char *buf) 668 { 669 const char *s = "--"; 670 media_type_t *mt; 671 int idx; 672 673 for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 674 mt = media_type_table + idx; 675 if (mt->media_type == media) { 676 s = mt->media_type_str; 677 break; 678 } 679 } 680 681 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 682 return (buf); 683 } 684 685 /* 686 * Given a physical link media type string, returns its media type constant. 687 */ 688 uint32_t 689 dladm_str2media(const char *buf) 690 { 691 media_type_t *mt; 692 int idx; 693 694 for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 695 mt = media_type_table + idx; 696 if (strcasecmp(buf, mt->media_type_str) == 0) 697 return (mt->media_type); 698 } 699 700 return (DL_OTHER); 701 } 702 703 dladm_status_t 704 i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms, 705 dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *), 706 void *arg, boolean_t writeop) 707 { 708 dladm_status_t status = DLADM_STATUS_OK; 709 FILE *fp, *nfp = NULL; 710 char lock[MAXPATHLEN]; 711 char file[MAXPATHLEN]; 712 char newfile[MAXPATHLEN]; 713 char *db_basename; 714 int nfd, lock_fd; 715 716 /* 717 * If we are called from a boot script such as net-physical, 718 * it's quite likely that the root fs is still not writable. 719 * For this case, it's ok for the lock creation to fail since 720 * no one else could be accessing our configuration file. 721 */ 722 db_basename = strrchr(db_file, '/'); 723 if (db_basename == NULL || db_basename[1] == '\0') 724 return (dladm_errno2status(EINVAL)); 725 db_basename++; 726 (void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename); 727 if ((lock_fd = i_dladm_lock_db 728 (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS) 729 return (dladm_errno2status(errno)); 730 731 (void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file); 732 if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) { 733 int err = errno; 734 735 i_dladm_unlock_db(lock, lock_fd); 736 if (err == ENOENT) 737 return (DLADM_STATUS_DBNOTFOUND); 738 739 return (dladm_errno2status(err)); 740 } 741 742 if (writeop) { 743 (void) snprintf(newfile, MAXPATHLEN, "%s/%s.new", 744 dladm_rootdir, db_file); 745 if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, 746 db_perms)) < 0) { 747 (void) fclose(fp); 748 i_dladm_unlock_db(lock, lock_fd); 749 return (dladm_errno2status(errno)); 750 } 751 752 if ((nfp = fdopen(nfd, "w")) == NULL) { 753 (void) close(nfd); 754 (void) fclose(fp); 755 (void) unlink(newfile); 756 i_dladm_unlock_db(lock, lock_fd); 757 return (dladm_errno2status(errno)); 758 } 759 } 760 status = (*process_db)(handle, arg, fp, nfp); 761 if (!writeop || status != DLADM_STATUS_OK) 762 goto done; 763 764 /* Set permissions on file to db_perms */ 765 if (fchmod(nfd, db_perms) < 0) { 766 status = dladm_errno2status(errno); 767 goto done; 768 } 769 770 /* 771 * Configuration files need to be owned by the 'dladm' user and 772 * 'netadm' group. 773 */ 774 if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) { 775 status = dladm_errno2status(errno); 776 goto done; 777 } 778 779 if (fflush(nfp) == EOF) { 780 status = dladm_errno2status(errno); 781 goto done; 782 } 783 (void) fclose(fp); 784 (void) fclose(nfp); 785 786 if (rename(newfile, file) < 0) { 787 (void) unlink(newfile); 788 i_dladm_unlock_db(lock, lock_fd); 789 return (dladm_errno2status(errno)); 790 } 791 792 i_dladm_unlock_db(lock, lock_fd); 793 return (DLADM_STATUS_OK); 794 795 done: 796 if (nfp != NULL) { 797 (void) fclose(nfp); 798 if (status != DLADM_STATUS_OK) 799 (void) unlink(newfile); 800 } 801 (void) fclose(fp); 802 i_dladm_unlock_db(lock, lock_fd); 803 return (status); 804 } 805 806 dladm_status_t 807 dladm_set_rootdir(const char *rootdir) 808 { 809 DIR *dp; 810 811 if (rootdir == NULL || *rootdir != '/' || 812 (dp = opendir(rootdir)) == NULL) 813 return (DLADM_STATUS_BADARG); 814 815 (void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN); 816 (void) closedir(dp); 817 return (DLADM_STATUS_OK); 818 } 819 820 boolean_t 821 dladm_valid_linkname(const char *link) 822 { 823 size_t len = strlen(link); 824 const char *cp; 825 826 if (len >= MAXLINKNAMELEN) 827 return (B_FALSE); 828 829 /* 830 * The link name cannot start with a digit and must end with a digit. 831 */ 832 if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0)) 833 return (B_FALSE); 834 835 /* 836 * The legal characters in a link name are: 837 * alphanumeric (a-z, A-Z, 0-9), underscore ('_'), and '.'. 838 */ 839 for (cp = link; *cp != '\0'; cp++) { 840 if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.')) 841 return (B_FALSE); 842 } 843 844 return (B_TRUE); 845 } 846 847 /* 848 * Convert priority string to a value. 849 */ 850 dladm_status_t 851 dladm_str2pri(char *token, mac_priority_level_t *pri) 852 { 853 if (strlen(token) == strlen("low") && 854 strncasecmp(token, "low", strlen("low")) == 0) { 855 *pri = MPL_LOW; 856 } else if (strlen(token) == strlen("medium") && 857 strncasecmp(token, "medium", strlen("medium")) == 0) { 858 *pri = MPL_MEDIUM; 859 } else if (strlen(token) == strlen("high") && 860 strncasecmp(token, "high", strlen("high")) == 0) { 861 *pri = MPL_HIGH; 862 } else { 863 return (DLADM_STATUS_BADVAL); 864 } 865 return (DLADM_STATUS_OK); 866 } 867 868 /* 869 * Convert priority value to a string. 870 */ 871 const char * 872 dladm_pri2str(mac_priority_level_t pri, char *buf) 873 { 874 const char *s; 875 876 switch (pri) { 877 case MPL_LOW: 878 s = "low"; 879 break; 880 case MPL_MEDIUM: 881 s = "medium"; 882 break; 883 case MPL_HIGH: 884 s = "high"; 885 break; 886 default: 887 s = "--"; 888 break; 889 } 890 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 891 return (buf); 892 } 893 894 /* 895 * Convert protect string to a value. 896 */ 897 dladm_status_t 898 dladm_str2protect(char *token, uint32_t *ptype) 899 { 900 link_protect_t *lp; 901 int i; 902 903 for (i = 0; i < LPTYPES; i++) { 904 lp = &link_protect_types[i]; 905 if (strcmp(token, lp->lp_name) == 0) { 906 *ptype = lp->lp_type; 907 return (DLADM_STATUS_OK); 908 } 909 } 910 return (DLADM_STATUS_BADVAL); 911 } 912 913 /* 914 * Convert protect value to a string. 915 */ 916 const char * 917 dladm_protect2str(uint32_t ptype, char *buf) 918 { 919 const char *s = "--"; 920 link_protect_t *lp; 921 int i; 922 923 for (i = 0; i < LPTYPES; i++) { 924 lp = &link_protect_types[i]; 925 if (lp->lp_type == ptype) { 926 s = lp->lp_name; 927 break; 928 } 929 } 930 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 931 return (buf); 932 } 933 934 /* 935 * Convert an IPv4 address to/from a string. 936 */ 937 const char * 938 dladm_ipv4addr2str(void *addr, char *buf) 939 { 940 if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL) 941 buf[0] = '\0'; 942 943 return (buf); 944 } 945 946 dladm_status_t 947 dladm_str2ipv4addr(char *token, void *addr) 948 { 949 return (inet_pton(AF_INET, token, addr) == 1 ? 950 DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 951 } 952 953 const char * 954 dladm_ipv6addr2str(void *addr, char *buf) 955 { 956 if (inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN) == NULL) 957 buf[0] = '\0'; 958 959 return (buf); 960 } 961 962 dladm_status_t 963 dladm_str2ipv6addr(char *token, void *addr) 964 { 965 return (inet_pton(AF_INET6, token, addr) == 1 ? 966 DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 967 } 968 969 /* 970 * Find the set bits in a mask. 971 * This is used for expanding a bitmask into individual sub-masks 972 * which can be used for further processing. 973 */ 974 void 975 dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt) 976 { 977 int i, c = 0; 978 979 for (i = 0; i < 32; i++) { 980 if (((1 << i) & mask) != 0) 981 list[c++] = 1 << i; 982 } 983 *cnt = c; 984 } 985 986 void 987 dladm_free_args(dladm_arg_list_t *list) 988 { 989 if (list != NULL) { 990 free(list->al_buf); 991 free(list); 992 } 993 } 994 995 dladm_status_t 996 dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues) 997 { 998 dladm_arg_list_t *list; 999 dladm_arg_info_t *aip; 1000 char *buf, *curr; 1001 int len, i; 1002 1003 if (str == NULL) 1004 return (DLADM_STATUS_BADVAL); 1005 1006 if (str[0] == '\0') 1007 return (DLADM_STATUS_OK); 1008 1009 list = malloc(sizeof (dladm_arg_list_t)); 1010 if (list == NULL) 1011 return (dladm_errno2status(errno)); 1012 1013 list->al_count = 0; 1014 list->al_buf = buf = strdup(str); 1015 if (buf == NULL) 1016 return (dladm_errno2status(errno)); 1017 1018 curr = buf; 1019 len = strlen(buf); 1020 aip = NULL; 1021 for (i = 0; i < len; i++) { 1022 char c = buf[i]; 1023 boolean_t match = (c == '=' || c == ','); 1024 1025 if (!match && i != len - 1) 1026 continue; 1027 1028 if (match) { 1029 buf[i] = '\0'; 1030 if (*curr == '\0') 1031 goto fail; 1032 } 1033 1034 if (aip != NULL && c != '=') { 1035 if (aip->ai_count > DLADM_MAX_ARG_VALS) 1036 goto fail; 1037 1038 if (novalues) 1039 goto fail; 1040 1041 aip->ai_val[aip->ai_count] = curr; 1042 aip->ai_count++; 1043 } else { 1044 if (list->al_count > DLADM_MAX_ARG_VALS) 1045 goto fail; 1046 1047 aip = &list->al_info[list->al_count]; 1048 aip->ai_name = curr; 1049 aip->ai_count = 0; 1050 list->al_count++; 1051 if (c == ',') 1052 aip = NULL; 1053 } 1054 curr = buf + i + 1; 1055 } 1056 1057 *listp = list; 1058 return (DLADM_STATUS_OK); 1059 1060 fail: 1061 dladm_free_args(list); 1062 return (DLADM_STATUS_FAILED); 1063 } 1064 1065 /* 1066 * mac_propval_range_t functions. Currently implemented for only 1067 * ranges of uint32_t elements, but can be expanded as required. 1068 */ 1069 /* 1070 * Convert an array of strings (which can be ranges or individual 1071 * elements) into a single mac_propval_range_t structure which 1072 * allocated here but should be freed by the caller. 1073 */ 1074 dladm_status_t 1075 dladm_strs2range(char **prop_val, uint_t val_cnt, 1076 mac_propval_type_t type, mac_propval_range_t **range) 1077 { 1078 int i; 1079 char *endp; 1080 mac_propval_range_t *rangep; 1081 dladm_status_t status = DLADM_STATUS_OK; 1082 1083 switch (type) { 1084 case MAC_PROPVAL_UINT32: { 1085 mac_propval_uint32_range_t *ur; 1086 1087 /* Allocate range structure */ 1088 rangep = malloc(sizeof (mac_propval_range_t) + 1089 (val_cnt-1)*(sizeof (mac_propval_uint32_range_t))); 1090 if (rangep == NULL) 1091 return (DLADM_STATUS_NOMEM); 1092 1093 rangep->mpr_count = 0; 1094 ur = &rangep->mpr_range_uint32[0]; 1095 for (i = 0; i < val_cnt; i++, ur++) { 1096 errno = 0; 1097 if (strchr(prop_val[i], '-') == NULL) { 1098 /* single element */ 1099 ur->mpur_min = ur->mpur_max = 1100 strtol(prop_val[i], &endp, 10); 1101 if ((endp != NULL) && (*endp != '\0')) { 1102 return (DLADM_STATUS_BADRANGE); 1103 } 1104 } else { 1105 /* range of elements */ 1106 ur->mpur_min = strtol(prop_val[i], &endp, 10); 1107 if (*endp++ != '-') 1108 return (DLADM_STATUS_BADRANGE); 1109 ur->mpur_max = strtol(endp, &endp, 10); 1110 if (endp != NULL && *endp != '\0' || 1111 ur->mpur_max < ur->mpur_min) 1112 return (DLADM_STATUS_BADRANGE); 1113 } 1114 rangep->mpr_count++; 1115 } 1116 break; 1117 } 1118 default: 1119 return (DLADM_STATUS_BADVAL); 1120 } 1121 1122 rangep->mpr_type = type; 1123 *range = rangep; 1124 1125 return (status); 1126 } 1127 1128 /* 1129 * Convert a mac_propval_range_t structure into an array of elements. 1130 */ 1131 dladm_status_t 1132 dladm_range2list(mac_propval_range_t *rangep, void *elem, uint_t *nelem) 1133 { 1134 int i, j, k; 1135 dladm_status_t status = DLADM_STATUS_OK; 1136 1137 switch (rangep->mpr_type) { 1138 case MAC_PROPVAL_UINT32: { 1139 mac_propval_uint32_range_t *ur; 1140 uint32_t *elem32 = elem; 1141 1142 k = 0; 1143 ur = &rangep->mpr_range_uint32[0]; 1144 for (i = 0; i < rangep->mpr_count; i++, ur++) { 1145 for (j = 0; j <= ur->mpur_max - ur->mpur_min; j++) { 1146 elem32[k++] = ur->mpur_min + j; 1147 if (k > *nelem) { 1148 status = DLADM_STATUS_TOOMANYELEMENTS; 1149 break; 1150 } 1151 } 1152 } 1153 *nelem = k; 1154 break; 1155 } 1156 default: 1157 status = DLADM_STATUS_BADVAL; 1158 break; 1159 } 1160 return (status); 1161 } 1162 1163 /* 1164 * Convert a mac_propval_range_t structure into an array of strings 1165 * of single elements or ranges. 1166 */ 1167 int 1168 dladm_range2strs(mac_propval_range_t *rangep, char **prop_val) 1169 { 1170 int i; 1171 1172 switch (rangep->mpr_type) { 1173 case MAC_PROPVAL_UINT32: { 1174 mac_propval_uint32_range_t *ur; 1175 1176 /* Write ranges and individual elements */ 1177 ur = &rangep->mpr_range_uint32[0]; 1178 for (i = 0; i <= rangep->mpr_count; i++, ur++) { 1179 if (ur->mpur_min == ur->mpur_max) { 1180 /* single element */ 1181 (void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX, 1182 "%u", ur->mpur_min); 1183 } else { 1184 /* range of elements */ 1185 (void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX, 1186 "%u-%u", ur->mpur_min, ur->mpur_max); 1187 } 1188 } 1189 return (0); 1190 } 1191 default: 1192 break; 1193 } 1194 return (EINVAL); 1195 } 1196 1197 static int 1198 uint32cmp(const void *a, const void *b) 1199 { 1200 return (*(uint32_t *)a - *(uint32_t *)b); 1201 } 1202 1203 /* 1204 * Sort and convert an array of elements into a single 1205 * mac_propval_range_t structure which is allocated here but 1206 * should be freed by the caller. 1207 */ 1208 dladm_status_t 1209 dladm_list2range(void *elem, uint_t nelem, mac_propval_type_t type, 1210 mac_propval_range_t **range) 1211 { 1212 int i; 1213 uint_t nr = 0; 1214 mac_propval_range_t *rangep; 1215 dladm_status_t status = DLADM_STATUS_OK; 1216 1217 switch (type) { 1218 case MAC_PROPVAL_UINT32: { 1219 mac_propval_uint32_range_t *ur; 1220 uint32_t *elem32 = elem; 1221 uint32_t *sort32; 1222 1223 /* Allocate range structure */ 1224 rangep = malloc(sizeof (mac_propval_range_t) + 1225 (nelem-1)*(sizeof (mac_propval_uint32_range_t))); 1226 if (rangep == NULL) 1227 return (DLADM_STATUS_NOMEM); 1228 1229 /* Allocate array for sorting */ 1230 sort32 = malloc(nelem * sizeof (uint32_t)); 1231 if (sort32 == NULL) { 1232 free(rangep); 1233 return (DLADM_STATUS_NOMEM); 1234 } 1235 1236 /* Copy and sort list */ 1237 for (i = 0; i < nelem; i++) 1238 sort32[i] = elem32[i]; 1239 if (nelem > 1) 1240 qsort(sort32, nelem, sizeof (uint32_t), uint32cmp); 1241 1242 /* Convert list to ranges */ 1243 ur = &rangep->mpr_range_uint32[0]; 1244 ur->mpur_min = ur->mpur_max = sort32[0]; 1245 for (i = 1; i < nelem; i++) { 1246 if (sort32[i]-sort32[i-1] == 1) { 1247 /* part of current range */ 1248 ur->mpur_max = sort32[i]; 1249 } else { 1250 /* start a new range */ 1251 nr++; ur++; 1252 ur->mpur_min = ur->mpur_max = sort32[i]; 1253 } 1254 } 1255 free(sort32); 1256 break; 1257 } 1258 default: 1259 return (DLADM_STATUS_BADRANGE); 1260 } 1261 1262 rangep->mpr_type = type; 1263 rangep->mpr_count = nr + 1; 1264 *range = rangep; 1265 1266 return (status); 1267 } 1268