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 default: 441 s = "<unknown error>"; 442 break; 443 } 444 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 445 return (buf); 446 } 447 448 /* 449 * Convert a unix errno to a dladm_status_t. 450 * We only convert errnos that are likely to be encountered. All others 451 * are mapped to DLADM_STATUS_FAILED. 452 */ 453 dladm_status_t 454 dladm_errno2status(int err) 455 { 456 switch (err) { 457 case 0: 458 return (DLADM_STATUS_OK); 459 case EINVAL: 460 return (DLADM_STATUS_BADARG); 461 case EEXIST: 462 return (DLADM_STATUS_EXIST); 463 case ENOENT: 464 return (DLADM_STATUS_NOTFOUND); 465 case ENOSPC: 466 return (DLADM_STATUS_TOOSMALL); 467 case ENOMEM: 468 return (DLADM_STATUS_NOMEM); 469 case ENOTSUP: 470 return (DLADM_STATUS_NOTSUP); 471 case ENETDOWN: 472 return (DLADM_STATUS_NONOTIF); 473 case EACCES: 474 case EPERM: 475 return (DLADM_STATUS_DENIED); 476 case EIO: 477 return (DLADM_STATUS_IOERR); 478 case EBUSY: 479 return (DLADM_STATUS_LINKBUSY); 480 case EAGAIN: 481 return (DLADM_STATUS_TRYAGAIN); 482 case ENOTEMPTY: 483 return (DLADM_STATUS_FLOW_EXISTS); 484 case EOPNOTSUPP: 485 return (DLADM_STATUS_FLOW_INCOMPATIBLE); 486 case EALREADY: 487 return (DLADM_STATUS_FLOW_IDENTICAL); 488 case EADDRINUSE: 489 return (DLADM_STATUS_ADDRINUSE); 490 default: 491 return (DLADM_STATUS_FAILED); 492 } 493 } 494 495 boolean_t 496 dladm_str2interval(char *oarg, uint32_t *interval) 497 { 498 int val; 499 char *endp = NULL; 500 501 errno = 0; 502 val = strtol(oarg, &endp, 10); 503 if (errno != 0 || val <= 0 || *endp != '\0') 504 return (B_FALSE); 505 506 *interval = val; 507 508 return (B_TRUE); 509 } 510 511 dladm_status_t 512 dladm_str2bw(char *oarg, uint64_t *bw) 513 { 514 char *endp = NULL; 515 int64_t n; 516 int mult = 1; 517 518 n = strtoull(oarg, &endp, 10); 519 520 if ((errno != 0) || (strlen(endp) > 1)) 521 return (DLADM_STATUS_BADARG); 522 523 if (n < 0) 524 return (DLADM_STATUS_BADVAL); 525 526 switch (*endp) { 527 case 'k': 528 case 'K': 529 mult = 1000; 530 break; 531 case 'm': 532 case 'M': 533 case '\0': 534 mult = 1000000; 535 break; 536 case 'g': 537 case 'G': 538 mult = 1000000000; 539 break; 540 case '%': 541 /* 542 * percentages not supported for now, 543 * see RFE 6540675 544 */ 545 return (DLADM_STATUS_NOTSUP); 546 default: 547 return (DLADM_STATUS_BADVAL); 548 } 549 550 *bw = n * mult; 551 552 /* check for overflow */ 553 if (*bw / mult != n) 554 return (DLADM_STATUS_BADARG); 555 556 return (DLADM_STATUS_OK); 557 } 558 559 /* 560 * Convert bandwidth in bps to a string in Mbps. For values greater 561 * than 1Mbps or 1000000, print a whole Mbps value. For values that 562 * have fractional Mbps in whole Kbps, print the bandwidth in a manner 563 * similar to a floating point format. 564 * 565 * bps string 566 * 0 0 567 * 100 0 568 * 2000 0.002 569 * 431000 0.431 570 * 1000000 1 571 * 1030000 1.030 572 * 100000000 100 573 */ 574 const char * 575 dladm_bw2str(int64_t bw, char *buf) 576 { 577 int kbps, mbps; 578 579 kbps = (bw%1000000)/1000; 580 mbps = bw/1000000; 581 if (kbps != 0) { 582 (void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps, kbps); 583 } else { 584 (void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps); 585 } 586 587 return (buf); 588 } 589 590 #define LOCK_DB_PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 591 592 static int 593 i_dladm_lock_db(const char *lock_file, short type) 594 { 595 int lock_fd; 596 struct flock lock; 597 598 if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC, 599 LOCK_DB_PERMS)) < 0) 600 return (-1); 601 602 lock.l_type = type; 603 lock.l_whence = SEEK_SET; 604 lock.l_start = 0; 605 lock.l_len = 0; 606 607 if (fcntl(lock_fd, F_SETLKW, &lock) < 0) { 608 int err = errno; 609 610 (void) close(lock_fd); 611 (void) unlink(lock_file); 612 errno = err; 613 return (-1); 614 } 615 return (lock_fd); 616 } 617 618 static void 619 i_dladm_unlock_db(const char *lock_file, int fd) 620 { 621 struct flock lock; 622 623 if (fd < 0) 624 return; 625 626 lock.l_type = F_UNLCK; 627 lock.l_whence = SEEK_SET; 628 lock.l_start = 0; 629 lock.l_len = 0; 630 631 (void) fcntl(fd, F_SETLKW, &lock); 632 (void) close(fd); 633 (void) unlink(lock_file); 634 } 635 636 /* 637 * Given a link class, returns its class string. 638 */ 639 const char * 640 dladm_class2str(datalink_class_t class, char *buf) 641 { 642 const char *s; 643 644 switch (class) { 645 case DATALINK_CLASS_PHYS: 646 s = "phys"; 647 break; 648 case DATALINK_CLASS_VLAN: 649 s = "vlan"; 650 break; 651 case DATALINK_CLASS_AGGR: 652 s = "aggr"; 653 break; 654 case DATALINK_CLASS_VNIC: 655 s = "vnic"; 656 break; 657 case DATALINK_CLASS_ETHERSTUB: 658 s = "etherstub"; 659 break; 660 case DATALINK_CLASS_IPTUN: 661 s = "iptun"; 662 break; 663 case DATALINK_CLASS_SIMNET: 664 s = "simnet"; 665 break; 666 case DATALINK_CLASS_BRIDGE: 667 s = "bridge"; 668 break; 669 case DATALINK_CLASS_PART: 670 s = "part"; 671 break; 672 default: 673 s = "unknown"; 674 break; 675 } 676 677 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 678 return (buf); 679 } 680 681 /* 682 * Given a physical link media type, returns its media type string. 683 */ 684 const char * 685 dladm_media2str(uint32_t media, char *buf) 686 { 687 const char *s = "--"; 688 media_type_t *mt; 689 int idx; 690 691 for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 692 mt = media_type_table + idx; 693 if (mt->media_type == media) { 694 s = mt->media_type_str; 695 break; 696 } 697 } 698 699 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 700 return (buf); 701 } 702 703 /* 704 * Given a physical link media type string, returns its media type constant. 705 */ 706 uint32_t 707 dladm_str2media(const char *buf) 708 { 709 media_type_t *mt; 710 int idx; 711 712 for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 713 mt = media_type_table + idx; 714 if (strcasecmp(buf, mt->media_type_str) == 0) 715 return (mt->media_type); 716 } 717 718 return (DL_OTHER); 719 } 720 721 dladm_status_t 722 i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms, 723 dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *), 724 void *arg, boolean_t writeop) 725 { 726 dladm_status_t status = DLADM_STATUS_OK; 727 FILE *fp, *nfp = NULL; 728 char lock[MAXPATHLEN]; 729 char file[MAXPATHLEN]; 730 char newfile[MAXPATHLEN]; 731 char *db_basename; 732 int nfd, lock_fd; 733 734 /* 735 * If we are called from a boot script such as net-physical, 736 * it's quite likely that the root fs is still not writable. 737 * For this case, it's ok for the lock creation to fail since 738 * no one else could be accessing our configuration file. 739 */ 740 db_basename = strrchr(db_file, '/'); 741 if (db_basename == NULL || db_basename[1] == '\0') 742 return (dladm_errno2status(EINVAL)); 743 db_basename++; 744 (void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename); 745 if ((lock_fd = i_dladm_lock_db 746 (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS) 747 return (dladm_errno2status(errno)); 748 749 (void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file); 750 if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) { 751 int err = errno; 752 753 i_dladm_unlock_db(lock, lock_fd); 754 if (err == ENOENT) 755 return (DLADM_STATUS_DBNOTFOUND); 756 757 return (dladm_errno2status(err)); 758 } 759 760 if (writeop) { 761 (void) snprintf(newfile, MAXPATHLEN, "%s/%s.new", 762 dladm_rootdir, db_file); 763 if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, 764 db_perms)) < 0) { 765 (void) fclose(fp); 766 i_dladm_unlock_db(lock, lock_fd); 767 return (dladm_errno2status(errno)); 768 } 769 770 if ((nfp = fdopen(nfd, "w")) == NULL) { 771 (void) close(nfd); 772 (void) fclose(fp); 773 (void) unlink(newfile); 774 i_dladm_unlock_db(lock, lock_fd); 775 return (dladm_errno2status(errno)); 776 } 777 } 778 status = (*process_db)(handle, arg, fp, nfp); 779 if (!writeop || status != DLADM_STATUS_OK) 780 goto done; 781 782 /* Set permissions on file to db_perms */ 783 if (fchmod(nfd, db_perms) < 0) { 784 status = dladm_errno2status(errno); 785 goto done; 786 } 787 788 /* 789 * Configuration files need to be owned by the 'dladm' user and 790 * 'netadm' group. 791 */ 792 if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) { 793 status = dladm_errno2status(errno); 794 goto done; 795 } 796 797 if (fflush(nfp) == EOF) { 798 status = dladm_errno2status(errno); 799 goto done; 800 } 801 (void) fclose(fp); 802 (void) fclose(nfp); 803 804 if (rename(newfile, file) < 0) { 805 (void) unlink(newfile); 806 i_dladm_unlock_db(lock, lock_fd); 807 return (dladm_errno2status(errno)); 808 } 809 810 i_dladm_unlock_db(lock, lock_fd); 811 return (DLADM_STATUS_OK); 812 813 done: 814 if (nfp != NULL) { 815 (void) fclose(nfp); 816 if (status != DLADM_STATUS_OK) 817 (void) unlink(newfile); 818 } 819 (void) fclose(fp); 820 i_dladm_unlock_db(lock, lock_fd); 821 return (status); 822 } 823 824 dladm_status_t 825 dladm_set_rootdir(const char *rootdir) 826 { 827 DIR *dp; 828 829 if (rootdir == NULL || *rootdir != '/' || 830 (dp = opendir(rootdir)) == NULL) 831 return (DLADM_STATUS_BADARG); 832 833 (void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN); 834 (void) closedir(dp); 835 return (DLADM_STATUS_OK); 836 } 837 838 boolean_t 839 dladm_valid_linkname(const char *link) 840 { 841 size_t len = strlen(link); 842 const char *cp; 843 int nd = 0; 844 845 if (len >= MAXLINKNAMELEN) 846 return (B_FALSE); 847 848 /* Link name cannot start with a digit */ 849 if (isdigit(link[0])) 850 return (B_FALSE); 851 /* Link name must end with a number without leading zeroes */ 852 cp = link + len - 1; 853 while (isdigit(*cp)) { 854 cp--; 855 nd++; 856 } 857 if (nd == 0 || (nd > 1 && *(cp + 1) == '0')) 858 return (B_FALSE); 859 860 /* 861 * The legal characters in a link name are: 862 * alphanumeric (a-z, A-Z, 0-9), underscore ('_'), and '.'. 863 */ 864 for (cp = link; *cp != '\0'; cp++) { 865 if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.')) 866 return (B_FALSE); 867 } 868 869 return (B_TRUE); 870 } 871 872 /* 873 * Convert priority string to a value. 874 */ 875 dladm_status_t 876 dladm_str2pri(char *token, mac_priority_level_t *pri) 877 { 878 if (strlen(token) == strlen("low") && 879 strncasecmp(token, "low", strlen("low")) == 0) { 880 *pri = MPL_LOW; 881 } else if (strlen(token) == strlen("medium") && 882 strncasecmp(token, "medium", strlen("medium")) == 0) { 883 *pri = MPL_MEDIUM; 884 } else if (strlen(token) == strlen("high") && 885 strncasecmp(token, "high", strlen("high")) == 0) { 886 *pri = MPL_HIGH; 887 } else { 888 return (DLADM_STATUS_BADVAL); 889 } 890 return (DLADM_STATUS_OK); 891 } 892 893 /* 894 * Convert priority value to a string. 895 */ 896 const char * 897 dladm_pri2str(mac_priority_level_t pri, char *buf) 898 { 899 const char *s; 900 901 switch (pri) { 902 case MPL_LOW: 903 s = "low"; 904 break; 905 case MPL_MEDIUM: 906 s = "medium"; 907 break; 908 case MPL_HIGH: 909 s = "high"; 910 break; 911 default: 912 s = "--"; 913 break; 914 } 915 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 916 return (buf); 917 } 918 919 /* 920 * Convert protect string to a value. 921 */ 922 dladm_status_t 923 dladm_str2protect(char *token, uint32_t *ptype) 924 { 925 link_protect_t *lp; 926 int i; 927 928 for (i = 0; i < LPTYPES; i++) { 929 lp = &link_protect_types[i]; 930 if (strcmp(token, lp->lp_name) == 0) { 931 *ptype = lp->lp_type; 932 return (DLADM_STATUS_OK); 933 } 934 } 935 return (DLADM_STATUS_BADVAL); 936 } 937 938 /* 939 * Convert protect value to a string. 940 */ 941 const char * 942 dladm_protect2str(uint32_t ptype, char *buf) 943 { 944 const char *s = "--"; 945 link_protect_t *lp; 946 int i; 947 948 for (i = 0; i < LPTYPES; i++) { 949 lp = &link_protect_types[i]; 950 if (lp->lp_type == ptype) { 951 s = lp->lp_name; 952 break; 953 } 954 } 955 (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 956 return (buf); 957 } 958 959 /* 960 * Convert an IPv4 address to/from a string. 961 */ 962 const char * 963 dladm_ipv4addr2str(void *addr, char *buf) 964 { 965 if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL) 966 buf[0] = '\0'; 967 968 return (buf); 969 } 970 971 dladm_status_t 972 dladm_str2ipv4addr(char *token, void *addr) 973 { 974 return (inet_pton(AF_INET, token, addr) == 1 ? 975 DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 976 } 977 978 const char * 979 dladm_ipv6addr2str(void *addr, char *buf) 980 { 981 if (inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN) == NULL) 982 buf[0] = '\0'; 983 984 return (buf); 985 } 986 987 dladm_status_t 988 dladm_str2ipv6addr(char *token, void *addr) 989 { 990 return (inet_pton(AF_INET6, token, addr) == 1 ? 991 DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 992 } 993 994 /* 995 * Find the set bits in a mask. 996 * This is used for expanding a bitmask into individual sub-masks 997 * which can be used for further processing. 998 */ 999 void 1000 dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt) 1001 { 1002 int i, c = 0; 1003 1004 for (i = 0; i < 32; i++) { 1005 if (((1 << i) & mask) != 0) 1006 list[c++] = 1 << i; 1007 } 1008 *cnt = c; 1009 } 1010 1011 void 1012 dladm_free_args(dladm_arg_list_t *list) 1013 { 1014 if (list != NULL) { 1015 free(list->al_buf); 1016 free(list); 1017 } 1018 } 1019 1020 dladm_status_t 1021 dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues) 1022 { 1023 dladm_arg_list_t *list; 1024 dladm_arg_info_t *aip; 1025 char *buf, *curr; 1026 int len, i; 1027 1028 if (str == NULL) 1029 return (DLADM_STATUS_BADVAL); 1030 1031 if (str[0] == '\0') 1032 return (DLADM_STATUS_OK); 1033 1034 list = malloc(sizeof (dladm_arg_list_t)); 1035 if (list == NULL) 1036 return (dladm_errno2status(errno)); 1037 1038 list->al_count = 0; 1039 list->al_buf = buf = strdup(str); 1040 if (buf == NULL) 1041 return (dladm_errno2status(errno)); 1042 1043 curr = buf; 1044 len = strlen(buf); 1045 aip = NULL; 1046 for (i = 0; i < len; i++) { 1047 char c = buf[i]; 1048 boolean_t match = (c == '=' || c == ','); 1049 1050 if (!match && i != len - 1) 1051 continue; 1052 1053 if (match) { 1054 buf[i] = '\0'; 1055 if (*curr == '\0') 1056 goto fail; 1057 } 1058 1059 if (aip != NULL && c != '=') { 1060 if (aip->ai_count > DLADM_MAX_ARG_VALS) 1061 goto fail; 1062 1063 if (novalues) 1064 goto fail; 1065 1066 aip->ai_val[aip->ai_count] = curr; 1067 aip->ai_count++; 1068 } else { 1069 if (list->al_count > DLADM_MAX_ARG_VALS) 1070 goto fail; 1071 1072 aip = &list->al_info[list->al_count]; 1073 aip->ai_name = curr; 1074 aip->ai_count = 0; 1075 list->al_count++; 1076 if (c == ',') 1077 aip = NULL; 1078 } 1079 curr = buf + i + 1; 1080 } 1081 1082 *listp = list; 1083 return (DLADM_STATUS_OK); 1084 1085 fail: 1086 dladm_free_args(list); 1087 return (DLADM_STATUS_FAILED); 1088 } 1089 1090 /* 1091 * mac_propval_range_t functions. Currently implemented for only 1092 * ranges of uint32_t elements, but can be expanded as required. 1093 */ 1094 /* 1095 * Convert an array of strings (which can be ranges or individual 1096 * elements) into a single mac_propval_range_t structure which 1097 * is allocated here but should be freed by the caller. 1098 */ 1099 dladm_status_t 1100 dladm_strs2range(char **prop_val, uint_t val_cnt, mac_propval_type_t type, 1101 mac_propval_range_t **range) 1102 { 1103 int i; 1104 char *endp; 1105 mac_propval_range_t *rangep; 1106 dladm_status_t status = DLADM_STATUS_OK; 1107 1108 switch (type) { 1109 case MAC_PROPVAL_UINT32: { 1110 mac_propval_uint32_range_t *ur; 1111 1112 /* Allocate range structure */ 1113 rangep = malloc(sizeof (mac_propval_range_t) + 1114 (val_cnt-1)*(sizeof (mac_propval_uint32_range_t))); 1115 if (rangep == NULL) 1116 return (DLADM_STATUS_NOMEM); 1117 1118 rangep->mpr_count = 0; 1119 ur = &rangep->mpr_range_uint32[0]; 1120 for (i = 0; i < val_cnt; i++, ur++) { 1121 errno = 0; 1122 if (strchr(prop_val[i], '-') == NULL) { 1123 /* single element */ 1124 ur->mpur_min = ur->mpur_max = 1125 strtol(prop_val[i], &endp, 10); 1126 if ((endp != NULL) && (*endp != '\0')) { 1127 return (DLADM_STATUS_BADRANGE); 1128 } 1129 } else { 1130 /* range of elements */ 1131 ur->mpur_min = strtol(prop_val[i], &endp, 10); 1132 if (*endp++ != '-') 1133 return (DLADM_STATUS_BADRANGE); 1134 ur->mpur_max = strtol(endp, &endp, 10); 1135 if (endp != NULL && *endp != '\0' || 1136 ur->mpur_max < ur->mpur_min) 1137 return (DLADM_STATUS_BADRANGE); 1138 } 1139 rangep->mpr_count++; 1140 } 1141 break; 1142 } 1143 default: 1144 return (DLADM_STATUS_BADVAL); 1145 } 1146 1147 rangep->mpr_type = type; 1148 *range = rangep; 1149 1150 return (status); 1151 } 1152 1153 /* 1154 * Convert a mac_propval_range_t structure into an array of elements. 1155 */ 1156 dladm_status_t 1157 dladm_range2list(mac_propval_range_t *rangep, void *elem, uint_t *nelem) 1158 { 1159 int i, j, k; 1160 dladm_status_t status = DLADM_STATUS_OK; 1161 1162 switch (rangep->mpr_type) { 1163 case MAC_PROPVAL_UINT32: { 1164 mac_propval_uint32_range_t *ur; 1165 uint32_t *elem32 = elem; 1166 1167 k = 0; 1168 ur = &rangep->mpr_range_uint32[0]; 1169 for (i = 0; i < rangep->mpr_count; i++, ur++) { 1170 for (j = 0; j <= ur->mpur_max - ur->mpur_min; j++) { 1171 elem32[k++] = ur->mpur_min + j; 1172 if (k > *nelem) { 1173 status = DLADM_STATUS_TOOMANYELEMENTS; 1174 break; 1175 } 1176 } 1177 } 1178 *nelem = k; 1179 break; 1180 } 1181 default: 1182 status = DLADM_STATUS_BADVAL; 1183 break; 1184 } 1185 return (status); 1186 } 1187 1188 /* 1189 * Convert a mac_propval_range_t structure into an array of strings 1190 * of single elements or ranges. 1191 */ 1192 int 1193 dladm_range2strs(mac_propval_range_t *rangep, char **prop_val) 1194 { 1195 int i; 1196 1197 switch (rangep->mpr_type) { 1198 case MAC_PROPVAL_UINT32: { 1199 mac_propval_uint32_range_t *ur; 1200 1201 /* Write ranges and individual elements */ 1202 ur = &rangep->mpr_range_uint32[0]; 1203 for (i = 0; i < rangep->mpr_count; i++, ur++) { 1204 if (ur->mpur_min == ur->mpur_max) { 1205 /* single element */ 1206 (void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX, 1207 "%u", ur->mpur_min); 1208 } else { 1209 /* range of elements */ 1210 (void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX, 1211 "%u-%u", ur->mpur_min, ur->mpur_max); 1212 } 1213 } 1214 return (0); 1215 } 1216 default: 1217 break; 1218 } 1219 return (EINVAL); 1220 } 1221 1222 static int 1223 uint32cmp(const void *a, const void *b) 1224 { 1225 return (*(uint32_t *)a - *(uint32_t *)b); 1226 } 1227 1228 /* 1229 * Sort and convert an array of elements into a single 1230 * mac_propval_range_t structure which is allocated here but 1231 * should be freed by the caller. 1232 */ 1233 dladm_status_t 1234 dladm_list2range(void *elem, uint_t nelem, mac_propval_type_t type, 1235 mac_propval_range_t **range) 1236 { 1237 int i; 1238 uint_t nr = 0; 1239 mac_propval_range_t *rangep; 1240 dladm_status_t status = DLADM_STATUS_OK; 1241 1242 switch (type) { 1243 case MAC_PROPVAL_UINT32: { 1244 mac_propval_uint32_range_t *ur; 1245 uint32_t *elem32 = elem; 1246 uint32_t *sort32; 1247 1248 /* Allocate range structure */ 1249 rangep = malloc(sizeof (mac_propval_range_t) + 1250 (nelem-1)*(sizeof (mac_propval_uint32_range_t))); 1251 if (rangep == NULL) 1252 return (DLADM_STATUS_NOMEM); 1253 1254 /* Allocate array for sorting */ 1255 sort32 = malloc(nelem * sizeof (uint32_t)); 1256 if (sort32 == NULL) { 1257 free(rangep); 1258 return (DLADM_STATUS_NOMEM); 1259 } 1260 1261 /* Copy and sort list */ 1262 for (i = 0; i < nelem; i++) 1263 sort32[i] = elem32[i]; 1264 if (nelem > 1) 1265 qsort(sort32, nelem, sizeof (uint32_t), uint32cmp); 1266 1267 /* Convert list to ranges */ 1268 ur = &rangep->mpr_range_uint32[0]; 1269 ur->mpur_min = ur->mpur_max = sort32[0]; 1270 for (i = 1; i < nelem; i++) { 1271 if (sort32[i]-sort32[i-1] == 1) { 1272 /* part of current range */ 1273 ur->mpur_max = sort32[i]; 1274 } else { 1275 /* start a new range */ 1276 nr++; ur++; 1277 ur->mpur_min = ur->mpur_max = sort32[i]; 1278 } 1279 } 1280 free(sort32); 1281 break; 1282 } 1283 default: 1284 return (DLADM_STATUS_BADRANGE); 1285 } 1286 1287 rangep->mpr_type = type; 1288 rangep->mpr_count = nr + 1; 1289 *range = rangep; 1290 1291 return (status); 1292 } 1293