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