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