1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2016 Joyent, Inc.
  25  * Copyright 2023 Oxide Computer Company
  26  */
  27 
  28 #include <assert.h>
  29 #include <ctype.h>
  30 #include <errno.h>
  31 #include <fcntl.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <string.h>
  35 #include <strings.h>
  36 #include <syslog.h>
  37 #include <zone.h>
  38 #include <sys/types.h>
  39 #include <sys/stat.h>
  40 #include <stropts.h>
  41 #include <sys/conf.h>
  42 #include <pthread.h>
  43 #include <unistd.h>
  44 #include <wait.h>
  45 #include <libcontract.h>
  46 #include <libcontract_priv.h>
  47 #include <sys/contract/process.h>
  48 #include <zone.h>
  49 #include "dlmgmt_impl.h"
  50 
  51 typedef enum dlmgmt_db_op {
  52         DLMGMT_DB_OP_WRITE,
  53         DLMGMT_DB_OP_DELETE,
  54         DLMGMT_DB_OP_READ
  55 } dlmgmt_db_op_t;
  56 
  57 typedef struct dlmgmt_db_req_s {
  58         struct dlmgmt_db_req_s  *ls_next;
  59         dlmgmt_db_op_t          ls_op;
  60         char                    ls_link[MAXLINKNAMELEN];
  61         datalink_id_t           ls_linkid;
  62         zoneid_t                ls_zoneid;
  63         uint32_t                ls_flags;       /* Either DLMGMT_ACTIVE or   */
  64                                                 /* DLMGMT_PERSIST, not both. */
  65 } dlmgmt_db_req_t;
  66 
  67 /*
  68  * List of pending db updates (e.g., because of a read-only filesystem).
  69  */
  70 static dlmgmt_db_req_t  *dlmgmt_db_req_head = NULL;
  71 static dlmgmt_db_req_t  *dlmgmt_db_req_tail = NULL;
  72 
  73 /*
  74  * rewrite_needed is set to B_TRUE by process_link_line() if it encounters a
  75  * line with an old format.  This will cause the file being read to be
  76  * re-written with the current format.
  77  */
  78 static boolean_t        rewrite_needed;
  79 
  80 static int              dlmgmt_db_update(dlmgmt_db_op_t, const char *,
  81                             dlmgmt_link_t *, uint32_t);
  82 static int              dlmgmt_process_db_req(dlmgmt_db_req_t *);
  83 static int              dlmgmt_process_db_onereq(dlmgmt_db_req_t *, boolean_t);
  84 static void             *dlmgmt_db_update_thread(void *);
  85 static boolean_t        process_link_line(char *, dlmgmt_link_t *);
  86 static int              process_db_write(dlmgmt_db_req_t *, FILE *, FILE *);
  87 static int              process_db_read(dlmgmt_db_req_t *, FILE *);
  88 static void             generate_link_line(dlmgmt_link_t *, boolean_t, char *);
  89 
  90 #define BUFLEN(lim, ptr)        (((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
  91 #define MAXLINELEN              1024
  92 
  93 typedef void db_walk_func_t(dlmgmt_link_t *);
  94 
  95 /*
  96  * Translator functions to go from dladm_datatype_t to character strings.
  97  * Each function takes a pointer to a buffer, the size of the buffer,
  98  * the name of the attribute, and the value to be written.  The functions
  99  * return the number of bytes written to the buffer.  If the buffer is not big
 100  * enough to hold the string representing the value, then nothing is written
 101  * and 0 is returned.
 102  */
 103 typedef size_t write_func_t(char *, size_t, char *, void *);
 104 
 105 /*
 106  * Translator functions to read from a NULL terminated string buffer into
 107  * something of the given DLADM_TYPE_*.  The functions each return the number
 108  * of bytes read from the string buffer.  If there is an error reading data
 109  * from the buffer, then 0 is returned.  It is the caller's responsibility
 110  * to free the data allocated by these functions.
 111  */
 112 typedef size_t read_func_t(char *, void **);
 113 
 114 typedef struct translator_s {
 115         const char      *type_name;
 116         write_func_t    *write_func;
 117         read_func_t     *read_func;
 118 } translator_t;
 119 
 120 /*
 121  * Translator functions, defined later but declared here so that
 122  * the translator table can be defined.
 123  */
 124 static write_func_t     write_str, write_boolean, write_uint64;
 125 static read_func_t      read_str, read_boolean, read_int64;
 126 
 127 /*
 128  * Translator table, indexed by dladm_datatype_t.
 129  */
 130 static translator_t translators[] = {
 131         { "string",     write_str,      read_str        },
 132         { "boolean",    write_boolean,  read_boolean    },
 133         { "int",        write_uint64,   read_int64      }
 134 };
 135 
 136 static size_t ntranslators = sizeof (translators) / sizeof (translator_t);
 137 
 138 #define LINK_PROPERTY_DELIMINATOR       ";"
 139 #define LINK_PROPERTY_TYPE_VALUE_SEP    ","
 140 #define BASE_PROPERTY_LENGTH(t, n) (strlen(translators[(t)].type_name) +\
 141                                     strlen(LINK_PROPERTY_TYPE_VALUE_SEP) +\
 142                                     strlen(LINK_PROPERTY_DELIMINATOR) +\
 143                                     strlen((n)))
 144 #define GENERATE_PROPERTY_STRING(buf, length, conv, name, type, val) \
 145             (snprintf((buf), (length), "%s=%s%s" conv "%s", (name), \
 146             translators[(type)].type_name, \
 147             LINK_PROPERTY_TYPE_VALUE_SEP, (val), LINK_PROPERTY_DELIMINATOR))
 148 
 149 /*
 150  * Name of the cache file to keep the active <link name, linkid> mapping
 151  */
 152 char    cachefile[MAXPATHLEN];
 153 
 154 #define DLMGMT_PERSISTENT_DB_PATH       "/etc/dladm/datalink.conf"
 155 #define DLMGMT_MAKE_FILE_DB_PATH(buffer, persistent)    \
 156         (void) snprintf((buffer), MAXPATHLEN, "%s", \
 157         (persistent) ? DLMGMT_PERSISTENT_DB_PATH : cachefile);
 158 
 159 typedef struct zopen_arg {
 160         const char      *zopen_modestr;
 161         int             *zopen_pipe;
 162         int             zopen_fd;
 163 } zopen_arg_t;
 164 
 165 typedef struct zrename_arg {
 166         const char      *zrename_newname;
 167 } zrename_arg_t;
 168 
 169 typedef union zfoparg {
 170         zopen_arg_t     zfop_openarg;
 171         zrename_arg_t   zfop_renamearg;
 172 } zfoparg_t;
 173 
 174 typedef struct zfcbarg {
 175         boolean_t       zfarg_inglobalzone; /* is callback in global zone? */
 176         zoneid_t        zfarg_finglobalzone; /* is file in global zone? */
 177         const char      *zfarg_filename;
 178         zfoparg_t       *zfarg_oparg;
 179 } zfarg_t;
 180 #define zfarg_openarg   zfarg_oparg->zfop_openarg
 181 #define zfarg_renamearg zfarg_oparg->zfop_renamearg
 182 
 183 /* zone file callback */
 184 typedef int zfcb_t(zfarg_t *);
 185 
 186 /*
 187  * Execute an operation on filename relative to zoneid's zone root.  If the
 188  * file is in the global zone, then the zfcb() callback will simply be called
 189  * directly.  If the file is in a non-global zone, then zfcb() will be called
 190  * both from the global zone's context, and from the non-global zone's context
 191  * (from a fork()'ed child that has entered the non-global zone).  This is
 192  * done to allow the callback to communicate with itself if needed (e.g. to
 193  * pass back the file descriptor of an opened file).
 194  */
 195 static int
 196 dlmgmt_zfop(const char *filename, zoneid_t zoneid, zfcb_t *zfcb,
 197     zfoparg_t *zfoparg)
 198 {
 199         int             ctfd;
 200         int             err;
 201         pid_t           childpid;
 202         siginfo_t       info;
 203         zfarg_t         zfarg;
 204         ctid_t          ct;
 205 
 206         if (zoneid != GLOBAL_ZONEID) {
 207                 /*
 208                  * We need to access a file that isn't in the global zone.
 209                  * Accessing non-global zone files from the global zone is
 210                  * unsafe (due to symlink attacks), we'll need to fork a child
 211                  * that enters the zone in question and executes the callback
 212                  * that will operate on the file.
 213                  *
 214                  * Before we proceed with this zone tango, we need to create a
 215                  * new process contract for the child, as required by
 216                  * zone_enter().
 217                  */
 218                 errno = 0;
 219                 ctfd = open64("/system/contract/process/template", O_RDWR);
 220                 if (ctfd == -1)
 221                         return (errno);
 222                 if ((err = ct_tmpl_set_critical(ctfd, 0)) != 0 ||
 223                     (err = ct_tmpl_set_informative(ctfd, 0)) != 0 ||
 224                     (err = ct_pr_tmpl_set_fatal(ctfd, CT_PR_EV_HWERR)) != 0 ||
 225                     (err = ct_pr_tmpl_set_param(ctfd, CT_PR_PGRPONLY)) != 0 ||
 226                     (err = ct_tmpl_activate(ctfd)) != 0) {
 227                         (void) close(ctfd);
 228                         return (err);
 229                 }
 230                 childpid = fork();
 231                 switch (childpid) {
 232                 case -1:
 233                         (void) ct_tmpl_clear(ctfd);
 234                         (void) close(ctfd);
 235                         return (err);
 236                 case 0:
 237                         (void) ct_tmpl_clear(ctfd);
 238                         (void) close(ctfd);
 239                         /*
 240                          * Elevate our privileges as zone_enter() requires all
 241                          * privileges.
 242                          */
 243                         if ((err = dlmgmt_elevate_privileges()) != 0)
 244                                 _exit(err);
 245                         if (zone_enter(zoneid) == -1)
 246                                 _exit(errno);
 247                         if ((err = dlmgmt_drop_privileges()) != 0)
 248                                 _exit(err);
 249                         break;
 250                 default:
 251                         if (contract_latest(&ct) == -1)
 252                                 ct = -1;
 253                         (void) ct_tmpl_clear(ctfd);
 254                         (void) close(ctfd);
 255                         if (waitid(P_PID, childpid, &info, WEXITED) == -1) {
 256                                 (void) contract_abandon_id(ct);
 257                                 return (errno);
 258                         }
 259                         (void) contract_abandon_id(ct);
 260                         if (info.si_status != 0)
 261                                 return (info.si_status);
 262                 }
 263         }
 264 
 265         zfarg.zfarg_inglobalzone = (zoneid == GLOBAL_ZONEID || childpid != 0);
 266         zfarg.zfarg_finglobalzone = (zoneid == GLOBAL_ZONEID);
 267         zfarg.zfarg_filename = filename;
 268         zfarg.zfarg_oparg = zfoparg;
 269         err = zfcb(&zfarg);
 270         if (!zfarg.zfarg_inglobalzone)
 271                 _exit(err);
 272         return (err);
 273 }
 274 
 275 static int
 276 dlmgmt_zopen_cb(zfarg_t *zfarg)
 277 {
 278         struct strrecvfd recvfd;
 279         boolean_t       newfile = B_FALSE;
 280         boolean_t       inglobalzone = zfarg->zfarg_inglobalzone;
 281         zoneid_t        finglobalzone = zfarg->zfarg_finglobalzone;
 282         const char      *filename = zfarg->zfarg_filename;
 283         const char      *modestr = zfarg->zfarg_openarg.zopen_modestr;
 284         int             *p = zfarg->zfarg_openarg.zopen_pipe;
 285         struct stat     statbuf;
 286         int             oflags;
 287         mode_t          mode;
 288         int             fd = -1;
 289         int             err;
 290 
 291         /* We only ever open a file for reading or writing, not both. */
 292         oflags = (modestr[0] == 'r') ? O_RDONLY : O_WRONLY | O_CREAT | O_TRUNC;
 293         mode = (modestr[0] == 'r') ? 0 : S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
 294 
 295         /* Open the file if we're in the same zone as the file. */
 296         if (inglobalzone == finglobalzone) {
 297                 /*
 298                  * First determine if we will be creating the file as part of
 299                  * opening it.  If so, then we'll need to ensure that it has
 300                  * the proper ownership after having opened it.
 301                  */
 302                 if (oflags & O_CREAT) {
 303                         if (stat(filename, &statbuf) == -1) {
 304                                 if (errno == ENOENT)
 305                                         newfile = B_TRUE;
 306                                 else
 307                                         return (errno);
 308                         }
 309                 }
 310                 if ((fd = open(filename, oflags, mode)) == -1)
 311                         return (errno);
 312                 if (newfile) {
 313                         if (chown(filename, UID_DLADM, GID_NETADM) == -1) {
 314                                 err = errno;
 315                                 (void) close(fd);
 316                                 return (err);
 317                         }
 318                 }
 319         }
 320 
 321         /*
 322          * If we're not in the global zone, send the file-descriptor back to
 323          * our parent in the global zone.
 324          */
 325         if (!inglobalzone) {
 326                 assert(!finglobalzone);
 327                 assert(fd != -1);
 328                 return (ioctl(p[1], I_SENDFD, fd) == -1 ? errno : 0);
 329         }
 330 
 331         /*
 332          * At this point, we know we're in the global zone.  If the file was
 333          * in a non-global zone, receive the file-descriptor from our child in
 334          * the non-global zone.
 335          */
 336         if (!finglobalzone) {
 337                 if (ioctl(p[0], I_RECVFD, &recvfd) == -1)
 338                         return (errno);
 339                 fd = recvfd.fd;
 340         }
 341 
 342         zfarg->zfarg_openarg.zopen_fd = fd;
 343         return (0);
 344 }
 345 
 346 static int
 347 dlmgmt_zunlink_cb(zfarg_t *zfarg)
 348 {
 349         if (zfarg->zfarg_inglobalzone != zfarg->zfarg_finglobalzone)
 350                 return (0);
 351         return (unlink(zfarg->zfarg_filename) == 0 ? 0 : errno);
 352 }
 353 
 354 static int
 355 dlmgmt_zrename_cb(zfarg_t *zfarg)
 356 {
 357         if (zfarg->zfarg_inglobalzone != zfarg->zfarg_finglobalzone)
 358                 return (0);
 359         return (rename(zfarg->zfarg_filename,
 360             zfarg->zfarg_renamearg.zrename_newname) == 0 ? 0 : errno);
 361 }
 362 
 363 /*
 364  * Same as fopen(3C), except that it opens the file relative to zoneid's zone
 365  * root.
 366  */
 367 static FILE *
 368 dlmgmt_zfopen(const char *filename, const char *modestr, zoneid_t zoneid,
 369     int *err)
 370 {
 371         int             p[2];
 372         zfoparg_t       zfoparg;
 373         FILE            *fp = NULL;
 374 
 375         if (zoneid != GLOBAL_ZONEID && pipe(p) == -1) {
 376                 *err = errno;
 377                 return (NULL);
 378         }
 379 
 380         zfoparg.zfop_openarg.zopen_modestr = modestr;
 381         zfoparg.zfop_openarg.zopen_pipe = p;
 382         *err = dlmgmt_zfop(filename, zoneid, dlmgmt_zopen_cb, &zfoparg);
 383         if (zoneid != GLOBAL_ZONEID) {
 384                 (void) close(p[0]);
 385                 (void) close(p[1]);
 386         }
 387         if (*err == 0) {
 388                 fp = fdopen(zfoparg.zfop_openarg.zopen_fd, modestr);
 389                 if (fp == NULL) {
 390                         *err = errno;
 391                         (void) close(zfoparg.zfop_openarg.zopen_fd);
 392                 }
 393         }
 394         return (fp);
 395 }
 396 
 397 /*
 398  * Same as rename(2), except that old and new are relative to zoneid's zone
 399  * root.
 400  */
 401 static int
 402 dlmgmt_zrename(const char *old, const char *new, zoneid_t zoneid)
 403 {
 404         zfoparg_t zfoparg;
 405 
 406         zfoparg.zfop_renamearg.zrename_newname = new;
 407         return (dlmgmt_zfop(old, zoneid, dlmgmt_zrename_cb, &zfoparg));
 408 }
 409 
 410 /*
 411  * Same as unlink(2), except that filename is relative to zoneid's zone root.
 412  */
 413 static int
 414 dlmgmt_zunlink(const char *filename, zoneid_t zoneid)
 415 {
 416         return (dlmgmt_zfop(filename, zoneid, dlmgmt_zunlink_cb, NULL));
 417 }
 418 
 419 static size_t
 420 write_str(char *buffer, size_t buffer_length, char *name, void *value)
 421 {
 422         char    *ptr = value;
 423         size_t  data_length = strnlen(ptr, buffer_length);
 424 
 425         /*
 426          * Strings are assumed to be NULL terminated.  In order to fit in
 427          * the buffer, the string's length must be less then buffer_length.
 428          * If the value is empty, there's no point in writing it, in fact,
 429          * we shouldn't even see that case.
 430          */
 431         if (data_length + BASE_PROPERTY_LENGTH(DLADM_TYPE_STR, name) ==
 432             buffer_length || data_length == 0)
 433                 return (0);
 434 
 435         /*
 436          * Since we know the string will fit in the buffer, snprintf will
 437          * always return less than buffer_length, so we can just return
 438          * whatever snprintf returns.
 439          */
 440         return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%s",
 441             name, DLADM_TYPE_STR, ptr));
 442 }
 443 
 444 static size_t
 445 write_boolean(char *buffer, size_t buffer_length, char *name, void *value)
 446 {
 447         boolean_t       *ptr = value;
 448 
 449         /*
 450          * Booleans are either zero or one, so we only need room for two
 451          * characters in the buffer.
 452          */
 453         if (buffer_length <= 1 + BASE_PROPERTY_LENGTH(DLADM_TYPE_BOOLEAN, name))
 454                 return (0);
 455 
 456         return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%d",
 457             name, DLADM_TYPE_BOOLEAN, *ptr));
 458 }
 459 
 460 static size_t
 461 write_uint64(char *buffer, size_t buffer_length, char *name, void *value)
 462 {
 463         uint64_t        *ptr = value;
 464 
 465         /*
 466          * Limit checking for uint64_t is a little trickier.
 467          */
 468         if (snprintf(NULL, 0, "%lld", *ptr)  +
 469             BASE_PROPERTY_LENGTH(DLADM_TYPE_UINT64, name) >= buffer_length)
 470                 return (0);
 471 
 472         return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%lld",
 473             name, DLADM_TYPE_UINT64, *ptr));
 474 }
 475 
 476 static size_t
 477 read_str(char *buffer, void **value)
 478 {
 479         char            *ptr = calloc(MAXLINKATTRVALLEN, sizeof (char));
 480         ssize_t         len;
 481 
 482         if (ptr == NULL || (len = strlcpy(ptr, buffer, MAXLINKATTRVALLEN))
 483             >= MAXLINKATTRVALLEN) {
 484                 free(ptr);
 485                 return (0);
 486         }
 487 
 488         *(char **)value = ptr;
 489 
 490         /* Account for NULL terminator */
 491         return (len + 1);
 492 }
 493 
 494 static size_t
 495 read_boolean(char *buffer, void **value)
 496 {
 497         boolean_t       *ptr = calloc(1, sizeof (boolean_t));
 498 
 499         if (ptr == NULL)
 500                 return (0);
 501 
 502         *ptr = atoi(buffer);
 503         *(boolean_t **)value = ptr;
 504 
 505         return (sizeof (boolean_t));
 506 }
 507 
 508 static size_t
 509 read_int64(char *buffer, void **value)
 510 {
 511         int64_t *ptr = calloc(1, sizeof (int64_t));
 512 
 513         if (ptr == NULL)
 514                 return (0);
 515 
 516         *ptr = (int64_t)atoll(buffer);
 517         *(int64_t **)value = ptr;
 518 
 519         return (sizeof (int64_t));
 520 }
 521 
 522 static dlmgmt_db_req_t *
 523 dlmgmt_db_req_alloc(dlmgmt_db_op_t op, const char *linkname,
 524     datalink_id_t linkid, zoneid_t zoneid, uint32_t flags, int *err)
 525 {
 526         dlmgmt_db_req_t *req;
 527 
 528         if ((req = calloc(1, sizeof (dlmgmt_db_req_t))) == NULL) {
 529                 *err = errno;
 530         } else {
 531                 req->ls_op = op;
 532                 if (linkname != NULL)
 533                         (void) strlcpy(req->ls_link, linkname, MAXLINKNAMELEN);
 534                 req->ls_linkid = linkid;
 535                 req->ls_zoneid = zoneid;
 536                 req->ls_flags = flags;
 537         }
 538         return (req);
 539 }
 540 
 541 /*
 542  * Update the db entry with name "entryname" using information from "linkp".
 543  */
 544 static int
 545 dlmgmt_db_update(dlmgmt_db_op_t op, const char *entryname, dlmgmt_link_t *linkp,
 546     uint32_t flags)
 547 {
 548         dlmgmt_db_req_t *req;
 549         int             err;
 550 
 551         /* It is either a persistent request or an active request, not both. */
 552         assert((flags == DLMGMT_PERSIST) || (flags == DLMGMT_ACTIVE));
 553 
 554         if ((req = dlmgmt_db_req_alloc(op, entryname, linkp->ll_linkid,
 555             linkp->ll_zoneid, flags, &err)) == NULL) {
 556                 return (err);
 557         }
 558 
 559         /*
 560          * If this is a transient link, then use the global zone cache file.
 561          * This is in order to allow recovery from a dlmgmtd failure that
 562          * leaves a zone in a 'down' state. In that state it is not possible
 563          * to read the zone's cache file (since it is always done from a sub
 564          * process running in the zone's context). As a result, datalinks would
 565          * otherwise remain stuck in the zone.
 566          */
 567         if (flags == DLMGMT_ACTIVE && linkp->ll_transient)
 568                 req->ls_zoneid = GLOBAL_ZONEID;
 569 
 570         /* If transient op and onloan, use the global zone cache file. */
 571         if (flags == DLMGMT_ACTIVE && linkp->ll_onloan)
 572                 req->ls_zoneid = GLOBAL_ZONEID;
 573 
 574         /*
 575          * If the return error is EINPROGRESS, this request is handled
 576          * asynchronously; return success.
 577          */
 578         err = dlmgmt_process_db_req(req);
 579         if (err != EINPROGRESS)
 580                 free(req);
 581         else
 582                 err = 0;
 583         return (err);
 584 }
 585 
 586 #define DLMGMT_DB_OP_STR(op)                                    \
 587         (((op) == DLMGMT_DB_OP_READ) ? "read" :                 \
 588         (((op) == DLMGMT_DB_OP_WRITE) ? "write" : "delete"))
 589 
 590 #define DLMGMT_DB_CONF_STR(flag)                                \
 591         (((flag) == DLMGMT_ACTIVE) ? "active" :                 \
 592         (((flag) == DLMGMT_PERSIST) ? "persistent" : ""))
 593 
 594 static int
 595 dlmgmt_process_db_req(dlmgmt_db_req_t *req)
 596 {
 597         pthread_t       tid;
 598         boolean_t       writeop;
 599         int             err;
 600 
 601         /*
 602          * If there are already pending "write" requests, queue this request in
 603          * the pending list.  Note that this function is called while the
 604          * dlmgmt_rw_lock is held, so it is safe to access the global variables.
 605          */
 606         writeop = (req->ls_op != DLMGMT_DB_OP_READ);
 607         if (writeop && (req->ls_flags == DLMGMT_PERSIST) &&
 608             (dlmgmt_db_req_head != NULL)) {
 609                 dlmgmt_db_req_tail->ls_next = req;
 610                 dlmgmt_db_req_tail = req;
 611                 return (EINPROGRESS);
 612         }
 613 
 614         err = dlmgmt_process_db_onereq(req, writeop);
 615         if (err != EINPROGRESS && err != 0 && err != ENOENT) {
 616                 /*
 617                  * Log the error unless the request processing is still in
 618                  * progress or if the configuration file hasn't been created
 619                  * yet (ENOENT).
 620                  */
 621                 dlmgmt_log(LOG_WARNING, "dlmgmt_process_db_onereq() %s "
 622                     "operation on %s configuration failed: %s",
 623                     DLMGMT_DB_OP_STR(req->ls_op),
 624                     DLMGMT_DB_CONF_STR(req->ls_flags), strerror(err));
 625         }
 626 
 627         if (err == EINPROGRESS) {
 628                 assert(req->ls_flags == DLMGMT_PERSIST);
 629                 assert(writeop && dlmgmt_db_req_head == NULL);
 630                 dlmgmt_db_req_tail = dlmgmt_db_req_head = req;
 631                 err = pthread_create(&tid, NULL, dlmgmt_db_update_thread, NULL);
 632                 if (err == 0)
 633                         return (EINPROGRESS);
 634         }
 635         return (err);
 636 }
 637 
 638 static int
 639 dlmgmt_process_db_onereq(dlmgmt_db_req_t *req, boolean_t writeop)
 640 {
 641         int     err = 0;
 642         FILE    *fp, *nfp = NULL;
 643         char    file[MAXPATHLEN];
 644         char    newfile[MAXPATHLEN];
 645 
 646         DLMGMT_MAKE_FILE_DB_PATH(file, (req->ls_flags == DLMGMT_PERSIST));
 647         fp = dlmgmt_zfopen(file, "r", req->ls_zoneid, &err);
 648         /*
 649          * Note that it is not an error if the file doesn't exist.  If we're
 650          * reading, we treat this case the same way as an empty file.  If
 651          * we're writing, the file will be created when we open the file for
 652          * writing below.
 653          */
 654         if (fp == NULL && !writeop)
 655                 return (err);
 656 
 657         if (writeop) {
 658                 (void) snprintf(newfile, MAXPATHLEN, "%s.new", file);
 659                 nfp = dlmgmt_zfopen(newfile, "w", req->ls_zoneid, &err);
 660                 if (nfp == NULL) {
 661                         /*
 662                          * EROFS can happen at boot when the file system is
 663                          * read-only.  Return EINPROGRESS so that the caller
 664                          * can add this request to the pending request list
 665                          * and start a retry thread.
 666                          */
 667                         err = (errno == EROFS ? EINPROGRESS : errno);
 668                         goto done;
 669                 }
 670         }
 671         if (writeop) {
 672                 if ((err = process_db_write(req, fp, nfp)) == 0)
 673                         err = dlmgmt_zrename(newfile, file, req->ls_zoneid);
 674         } else {
 675                 err = process_db_read(req, fp);
 676         }
 677 
 678 done:
 679         if (nfp != NULL) {
 680                 (void) fclose(nfp);
 681                 if (err != 0)
 682                         (void) dlmgmt_zunlink(newfile, req->ls_zoneid);
 683         }
 684         (void) fclose(fp);
 685         return (err);
 686 }
 687 
 688 /*ARGSUSED*/
 689 static void *
 690 dlmgmt_db_update_thread(void *arg)
 691 {
 692         dlmgmt_db_req_t *req;
 693 
 694         dlmgmt_table_lock(B_TRUE);
 695 
 696         assert(dlmgmt_db_req_head != NULL);
 697         while ((req = dlmgmt_db_req_head) != NULL) {
 698                 assert(req->ls_flags == DLMGMT_PERSIST);
 699                 if (dlmgmt_process_db_onereq(req, B_TRUE) == EINPROGRESS) {
 700                         /*
 701                          * The filesystem is still read only. Go to sleep and
 702                          * try again.
 703                          */
 704                         dlmgmt_table_unlock();
 705                         (void) sleep(5);
 706                         dlmgmt_table_lock(B_TRUE);
 707                         continue;
 708                 }
 709 
 710                 /*
 711                  * The filesystem is no longer read only. Continue processing
 712                  * and remove the request from the pending list.
 713                  */
 714                 dlmgmt_db_req_head = req->ls_next;
 715                 if (dlmgmt_db_req_tail == req) {
 716                         assert(dlmgmt_db_req_head == NULL);
 717                         dlmgmt_db_req_tail = NULL;
 718                 }
 719                 free(req);
 720         }
 721 
 722         dlmgmt_table_unlock();
 723         return (NULL);
 724 }
 725 
 726 static int
 727 parse_linkprops(char *buf, dlmgmt_link_t *linkp)
 728 {
 729         boolean_t               found_type = B_FALSE;
 730         dladm_datatype_t        type = DLADM_TYPE_STR;
 731         int                     i, len;
 732         char                    *curr;
 733         char                    attr_name[MAXLINKATTRLEN];
 734         size_t                  attr_buf_len = 0;
 735         void                    *attr_buf = NULL;
 736 
 737         curr = buf;
 738         len = strlen(buf);
 739         attr_name[0] = '\0';
 740         for (i = 0; i < len; i++) {
 741                 char            c = buf[i];
 742                 boolean_t       match = (c == '=' ||
 743                     (c == ',' && !found_type) || c == ';');
 744                 boolean_t       rename = B_FALSE;
 745 
 746                 /*
 747                  * Move to the next character if there is no match and
 748                  * if we have not reached the last character.
 749                  */
 750                 if (!match && i != len - 1)
 751                         continue;
 752 
 753                 if (match) {
 754                         /*
 755                          * NUL-terminate the string pointed to by 'curr'.
 756                          */
 757                         buf[i] = '\0';
 758                         if (*curr == '\0')
 759                                 goto parse_fail;
 760                 }
 761 
 762                 if (attr_name[0] != '\0' && found_type) {
 763                         /*
 764                          * We get here after we have processed the "<prop>="
 765                          * pattern. The pattern we are now interested in is
 766                          * "<val>;".
 767                          */
 768                         if (c == '=')
 769                                 goto parse_fail;
 770 
 771                         if (strcmp(attr_name, "linkid") == 0) {
 772                                 if (read_int64(curr, &attr_buf) == 0)
 773                                         goto parse_fail;
 774                                 linkp->ll_linkid =
 775                                     (datalink_class_t)*(int64_t *)attr_buf;
 776                         } else if (strcmp(attr_name, "name") == 0) {
 777                                 if (read_str(curr, &attr_buf) == 0)
 778                                         goto parse_fail;
 779                                 (void) snprintf(linkp->ll_link,
 780                                     MAXLINKNAMELEN, "%s", attr_buf);
 781                         } else if (strcmp(attr_name, "class") == 0) {
 782                                 if (read_int64(curr, &attr_buf) == 0)
 783                                         goto parse_fail;
 784                                 linkp->ll_class =
 785                                     (datalink_class_t)*(int64_t *)attr_buf;
 786                         } else if (strcmp(attr_name, "media") == 0) {
 787                                 if (read_int64(curr, &attr_buf) == 0)
 788                                         goto parse_fail;
 789                                 linkp->ll_media =
 790                                     (uint32_t)*(int64_t *)attr_buf;
 791                         } else if (strcmp(attr_name, "zone") == 0) {
 792                                 if (read_str(curr, &attr_buf) == 0)
 793                                         goto parse_fail;
 794                                 linkp->ll_zoneid = getzoneidbyname(attr_buf);
 795                                 if (linkp->ll_zoneid == -1) {
 796                                         if (errno == EFAULT)
 797                                                 abort();
 798                                         /*
 799                                          * If we can't find the zone, assign the
 800                                          * link to the GZ and mark it for being
 801                                          * renamed.
 802                                          */
 803                                         linkp->ll_zoneid = 0;
 804                                         rename = B_TRUE;
 805                                 }
 806                         } else if (strcmp(attr_name, "transient") == 0) {
 807                                 if (read_boolean(curr, &attr_buf) == 0)
 808                                         goto parse_fail;
 809                                 linkp->ll_transient = *(boolean_t *)attr_buf;
 810                         } else {
 811                                 attr_buf_len = translators[type].read_func(curr,
 812                                     &attr_buf);
 813                                 if (attr_buf_len == 0)
 814                                         goto parse_fail;
 815 
 816                                 if (linkattr_set(&(linkp->ll_head), attr_name,
 817                                     attr_buf, attr_buf_len, type) != 0) {
 818                                         free(attr_buf);
 819                                         goto parse_fail;
 820                                 }
 821                         }
 822 
 823                         free(attr_buf);
 824                         attr_name[0] = '\0';
 825                         found_type = B_FALSE;
 826                 } else if (attr_name[0] != '\0') {
 827                         /*
 828                          * Non-zero length attr_name and found_type of false
 829                          * indicates that we have not found the type for this
 830                          * attribute.  The pattern now is "<type>,<val>;", we
 831                          * want the <type> part of the pattern.
 832                          */
 833                         for (type = 0; type < ntranslators; type++) {
 834                                 if (strcmp(curr,
 835                                     translators[type].type_name) == 0) {
 836                                         found_type = B_TRUE;
 837                                         break;
 838                                 }
 839                         }
 840 
 841                         if (!found_type)
 842                                 goto parse_fail;
 843                 } else {
 844                         /*
 845                          * A zero length attr_name indicates we are looking
 846                          * at the beginning of a link attribute.
 847                          */
 848                         if (c != '=')
 849                                 goto parse_fail;
 850 
 851                         (void) snprintf(attr_name, MAXLINKATTRLEN, "%s", curr);
 852                 }
 853 
 854                 /*
 855                  * The zone that this link belongs to has died, we are
 856                  * reparenting it to the GZ and renaming it to avoid name
 857                  * collisions.
 858                  */
 859                 if (rename) {
 860                         (void) snprintf(linkp->ll_link, MAXLINKNAMELEN,
 861                             "SUNWorphan%u", (uint16_t)(gethrtime() / 1000));
 862                 }
 863 
 864                 curr = buf + i + 1;
 865         }
 866 
 867         /* Correct any erroneous IPTUN datalink class constant in the file */
 868         if (linkp->ll_class == 0x60) {
 869                 linkp->ll_class = DATALINK_CLASS_IPTUN;
 870                 rewrite_needed = B_TRUE;
 871         }
 872 
 873         return (0);
 874 
 875 parse_fail:
 876         /*
 877          * Free linkp->ll_head (link attribute list)
 878          */
 879         linkattr_destroy(linkp);
 880         return (-1);
 881 }
 882 
 883 static boolean_t
 884 process_link_line(char *buf, dlmgmt_link_t *linkp)
 885 {
 886         int     i, len, llen;
 887         char    *str, *lasts;
 888         char    tmpbuf[MAXLINELEN];
 889 
 890         bzero(linkp, sizeof (*linkp));
 891         linkp->ll_linkid = DATALINK_INVALID_LINKID;
 892 
 893         /*
 894          * Use a copy of buf for parsing so that we can do whatever we want.
 895          */
 896         (void) strlcpy(tmpbuf, buf, MAXLINELEN);
 897 
 898         /*
 899          * Skip leading spaces, blank lines, and comments.
 900          */
 901         len = strlen(tmpbuf);
 902         for (i = 0; i < len; i++) {
 903                 if (!isspace(tmpbuf[i]))
 904                         break;
 905         }
 906         if (i == len || tmpbuf[i] == '#')
 907                 return (B_TRUE);
 908 
 909         str = tmpbuf + i;
 910         /*
 911          * Find the link name and assign it to the link structure.
 912          */
 913         if (strtok_r(str, " \n\t", &lasts) == NULL)
 914                 goto fail;
 915 
 916         llen = strlen(str);
 917         /*
 918          * Note that a previous version of the persistent datalink.conf file
 919          * stored the linkid as the first field.  In that case, the name will
 920          * be obtained through parse_linkprops from a property with the format
 921          * "name=<linkname>".  If we encounter such a format, we set
 922          * rewrite_needed so that dlmgmt_db_init() can rewrite the file with
 923          * the new format after it's done reading in the data.
 924          */
 925         if (isdigit(str[0])) {
 926                 linkp->ll_linkid = atoi(str);
 927                 rewrite_needed = B_TRUE;
 928         } else {
 929                 if (strlcpy(linkp->ll_link, str, sizeof (linkp->ll_link)) >=
 930                     sizeof (linkp->ll_link))
 931                         goto fail;
 932         }
 933 
 934         str += llen + 1;
 935         if (str >= tmpbuf + len)
 936                 goto fail;
 937 
 938         /*
 939          * Now find the list of link properties.
 940          */
 941         if ((str = strtok_r(str, " \n\t", &lasts)) == NULL)
 942                 goto fail;
 943 
 944         if (parse_linkprops(str, linkp) < 0)
 945                 goto fail;
 946 
 947         return (B_TRUE);
 948 
 949 fail:
 950         /*
 951          * Delete corrupted line.
 952          */
 953         buf[0] = '\0';
 954         return (B_FALSE);
 955 }
 956 
 957 /*
 958  * Find any properties in linkp that refer to "old", and rename to "new".
 959  * Return B_TRUE if any renaming occurred.
 960  */
 961 static int
 962 dlmgmt_attr_rename(dlmgmt_link_t *linkp, const char *old, const char *new,
 963     boolean_t *renamed)
 964 {
 965         dlmgmt_linkattr_t       *attrp;
 966         char                    *newval = NULL, *pname;
 967         char                    valcp[MAXLINKATTRVALLEN];
 968         size_t                  newsize;
 969 
 970         *renamed = B_FALSE;
 971 
 972         if ((attrp = linkattr_find(linkp->ll_head, "linkover")) != NULL ||
 973             (attrp = linkattr_find(linkp->ll_head, "simnetpeer")) != NULL) {
 974                 if (strcmp(old, (char *)attrp->lp_val) == 0) {
 975                         newsize = strlen(new) + 1;
 976                         if ((newval = malloc(newsize)) == NULL)
 977                                 return (errno);
 978                         (void) strcpy(newval, new);
 979                         free(attrp->lp_val);
 980                         attrp->lp_val = newval;
 981                         attrp->lp_sz = newsize;
 982                         *renamed = B_TRUE;
 983                 }
 984                 return (0);
 985         }
 986 
 987         if ((attrp = linkattr_find(linkp->ll_head, "portnames")) == NULL)
 988                 return (0);
 989 
 990         /* <linkname>:[<linkname>:]... */
 991         if ((newval = calloc(MAXLINKATTRVALLEN, sizeof (char))) == NULL)
 992                 return (errno);
 993 
 994         bcopy(attrp->lp_val, valcp, sizeof (valcp));
 995         pname = strtok(valcp, ":");
 996         while (pname != NULL) {
 997                 if (strcmp(pname, old) == 0) {
 998                         (void) strcat(newval, new);
 999                         *renamed = B_TRUE;
1000                 } else {
1001                         (void) strcat(newval, pname);
1002                 }
1003                 (void) strcat(newval, ":");
1004                 pname = strtok(NULL, ":");
1005         }
1006         if (*renamed) {
1007                 free(attrp->lp_val);
1008                 attrp->lp_val = newval;
1009                 attrp->lp_sz = strlen(newval) + 1;
1010         } else {
1011                 free(newval);
1012         }
1013         return (0);
1014 }
1015 
1016 static int
1017 process_db_write(dlmgmt_db_req_t *req, FILE *fp, FILE *nfp)
1018 {
1019         boolean_t               done = B_FALSE;
1020         int                     err = 0;
1021         dlmgmt_link_t           link_in_file, *linkp = NULL, *dblinkp;
1022         boolean_t               persist = (req->ls_flags == DLMGMT_PERSIST);
1023         boolean_t               writeall, rename, attr_renamed;
1024         char                    buf[MAXLINELEN];
1025 
1026         writeall = (req->ls_linkid == DATALINK_ALL_LINKID);
1027 
1028         if (req->ls_op == DLMGMT_DB_OP_WRITE && !writeall) {
1029                 /*
1030                  * find the link in the avl tree with the given linkid.
1031                  */
1032                 linkp = link_by_id(req->ls_linkid, req->ls_zoneid);
1033                 if (linkp == NULL || (linkp->ll_flags & req->ls_flags) == 0) {
1034                         /*
1035                          * This link has already been changed. This could
1036                          * happen if the request is pending because of
1037                          * read-only file-system. If so, we are done.
1038                          */
1039                         return (0);
1040                 }
1041                 /*
1042                  * In the case of a rename, linkp's name has been updated to
1043                  * the new name, and req->ls_link is the old link name.
1044                  */
1045                 rename = (strcmp(req->ls_link, linkp->ll_link) != 0);
1046         }
1047 
1048         /*
1049          * fp can be NULL if the file didn't initially exist and we're
1050          * creating it as part of this write operation.
1051          */
1052         if (fp == NULL)
1053                 goto write;
1054 
1055         while (err == 0 && fgets(buf, sizeof (buf), fp) != NULL &&
1056             process_link_line(buf, &link_in_file)) {
1057                 /*
1058                  * Only the link name is needed. Free the memory allocated for
1059                  * the link attributes list of link_in_file.
1060                  */
1061                 linkattr_destroy(&link_in_file);
1062 
1063                 if (link_in_file.ll_link[0] == '\0' || done) {
1064                         /*
1065                          * this is a comment line or we are done updating the
1066                          * line for the specified link, write the rest of
1067                          * lines out.
1068                          */
1069                         if (fputs(buf, nfp) == EOF)
1070                                 err = errno;
1071                         continue;
1072                 }
1073 
1074                 switch (req->ls_op) {
1075                 case DLMGMT_DB_OP_WRITE:
1076                         /*
1077                          * For write operations, we generate a new output line
1078                          * if we're either writing all links (writeall) or if
1079                          * the name of the link in the file matches the one
1080                          * we're looking for.  Otherwise, we write out the
1081                          * buffer as-is.
1082                          *
1083                          * If we're doing a rename operation, ensure that any
1084                          * references to the link being renamed in link
1085                          * properties are also updated before we write
1086                          * anything.
1087                          */
1088                         if (writeall) {
1089                                 linkp = link_by_name(link_in_file.ll_link,
1090                                     req->ls_zoneid);
1091                         }
1092                         if (writeall || strcmp(req->ls_link,
1093                             link_in_file.ll_link) == 0) {
1094                                 generate_link_line(linkp, persist, buf);
1095                                 if (!writeall && !rename)
1096                                         done = B_TRUE;
1097                         } else if (rename && persist) {
1098                                 dblinkp = link_by_name(link_in_file.ll_link,
1099                                     req->ls_zoneid);
1100                                 err = dlmgmt_attr_rename(dblinkp, req->ls_link,
1101                                     linkp->ll_link, &attr_renamed);
1102                                 if (err != 0)
1103                                         break;
1104                                 if (attr_renamed) {
1105                                         generate_link_line(dblinkp, persist,
1106                                             buf);
1107                                 }
1108                         }
1109                         if (fputs(buf, nfp) == EOF)
1110                                 err = errno;
1111                         break;
1112                 case DLMGMT_DB_OP_DELETE:
1113                         /*
1114                          * Delete is simple.  If buf does not represent the
1115                          * link we're deleting, write it out.
1116                          */
1117                         if (strcmp(req->ls_link, link_in_file.ll_link) != 0) {
1118                                 if (fputs(buf, nfp) == EOF)
1119                                         err = errno;
1120                         } else {
1121                                 done = B_TRUE;
1122                         }
1123                         break;
1124                 case DLMGMT_DB_OP_READ:
1125                 default:
1126                         err = EINVAL;
1127                         break;
1128                 }
1129         }
1130 
1131 write:
1132         /*
1133          * If we get to the end of the file and have not seen what linkid
1134          * points to, write it out then.
1135          */
1136         if (req->ls_op == DLMGMT_DB_OP_WRITE && !writeall && !rename && !done) {
1137                 generate_link_line(linkp, persist, buf);
1138                 done = B_TRUE;
1139                 if (fputs(buf, nfp) == EOF)
1140                         err = errno;
1141         }
1142 
1143         return (err);
1144 }
1145 
1146 static int
1147 process_db_read(dlmgmt_db_req_t *req, FILE *fp)
1148 {
1149         avl_index_t     name_where, id_where;
1150         dlmgmt_link_t   link_in_file, *newlink, *link_in_db;
1151         char            buf[MAXLINELEN];
1152         int             err = 0;
1153 
1154         /*
1155          * This loop processes each line of the configuration file.
1156          */
1157         while (fgets(buf, MAXLINELEN, fp) != NULL) {
1158                 if (!process_link_line(buf, &link_in_file)) {
1159                         err = EINVAL;
1160                         break;
1161                 }
1162 
1163                 /*
1164                  * Skip the comment line.
1165                  */
1166                 if (link_in_file.ll_link[0] == '\0') {
1167                         linkattr_destroy(&link_in_file);
1168                         continue;
1169                 }
1170 
1171                 if ((req->ls_flags & DLMGMT_ACTIVE) &&
1172                     link_in_file.ll_linkid == DATALINK_INVALID_LINKID) {
1173                         linkattr_destroy(&link_in_file);
1174                         continue;
1175                 }
1176 
1177                 assert(req->ls_zoneid == 0 ||
1178                     link_in_file.ll_zoneid == req->ls_zoneid);
1179                 link_in_db = link_by_name(link_in_file.ll_link,
1180                     link_in_file.ll_zoneid);
1181                 if (link_in_db != NULL) {
1182                         /*
1183                          * If the link in the database already has the flag
1184                          * for this request set, then the entry is a
1185                          * duplicate.  If it's not a duplicate, then simply
1186                          * turn on the appropriate flag on the existing link.
1187                          */
1188                         if (link_in_db->ll_flags & req->ls_flags) {
1189                                 dlmgmt_log(LOG_WARNING, "Duplicate links "
1190                                     "in the repository: %s",
1191                                     link_in_file.ll_link);
1192                                 linkattr_destroy(&link_in_file);
1193                         } else {
1194                                 if (req->ls_flags & DLMGMT_PERSIST) {
1195                                         /*
1196                                          * Save the newly read properties into
1197                                          * the existing link.
1198                                          */
1199                                         assert(link_in_db->ll_head == NULL);
1200                                         link_in_db->ll_head =
1201                                             link_in_file.ll_head;
1202                                 } else {
1203                                         linkattr_destroy(&link_in_file);
1204                                 }
1205                                 link_in_db->ll_flags |= req->ls_flags;
1206                         }
1207                 } else {
1208                         /*
1209                          * This is a new link.  Allocate a new dlmgmt_link_t
1210                          * and add it to the trees.
1211                          */
1212                         newlink = calloc(1, sizeof (*newlink));
1213                         if (newlink == NULL) {
1214                                 dlmgmt_log(LOG_WARNING, "Unable to allocate "
1215                                     "memory to create new link %s",
1216                                     link_in_file.ll_link);
1217                                 linkattr_destroy(&link_in_file);
1218                                 continue;
1219                         }
1220                         bcopy(&link_in_file, newlink, sizeof (*newlink));
1221 
1222                         if (newlink->ll_linkid == DATALINK_INVALID_LINKID)
1223                                 newlink->ll_linkid = dlmgmt_nextlinkid;
1224                         if (avl_find(&dlmgmt_id_avl, newlink, &id_where) !=
1225                             NULL) {
1226                                 dlmgmt_log(LOG_WARNING, "Link ID %d is already"
1227                                     " in use, destroying link %s",
1228                                     newlink->ll_linkid, newlink->ll_link);
1229                                 link_destroy(newlink);
1230                                 continue;
1231                         }
1232 
1233                         if ((req->ls_flags & DLMGMT_ACTIVE) &&
1234                             link_activate(newlink) != 0) {
1235                                 dlmgmt_log(LOG_WARNING, "Unable to activate %s",
1236                                     newlink->ll_link);
1237                                 link_destroy(newlink);
1238                                 continue;
1239                         }
1240 
1241                         avl_insert(&dlmgmt_id_avl, newlink, id_where);
1242                         /*
1243                          * link_activate call above can insert newlink in
1244                          * dlmgmt_name_avl tree when activating a link that is
1245                          * assigned to a NGZ.
1246                          */
1247                         if (avl_find(&dlmgmt_name_avl, newlink,
1248                             &name_where) == NULL)
1249                                 avl_insert(&dlmgmt_name_avl, newlink,
1250                                     name_where);
1251 
1252                         dlmgmt_advance(newlink);
1253                         newlink->ll_flags |= req->ls_flags;
1254                 }
1255         }
1256 
1257         return (err);
1258 }
1259 
1260 /*
1261  * Generate an entry in the link database.
1262  * Each entry has this format:
1263  * <link name>    <prop0>=<type>,<val>;...;<propn>=<type>,<val>;
1264  */
1265 static void
1266 generate_link_line(dlmgmt_link_t *linkp, boolean_t persist, char *buf)
1267 {
1268         char                    tmpbuf[MAXLINELEN];
1269         char                    *ptr = tmpbuf;
1270         char                    *lim = tmpbuf + MAXLINELEN;
1271         dlmgmt_linkattr_t       *cur_p = NULL;
1272         uint64_t                u64;
1273 
1274         ptr += snprintf(ptr, BUFLEN(lim, ptr), "%s\t", linkp->ll_link);
1275         if (!persist) {
1276                 char zname[ZONENAME_MAX];
1277 
1278                 /*
1279                  * We store the linkid and the zone name in the active database
1280                  * so that dlmgmtd can recover in the event that it is
1281                  * restarted.
1282                  */
1283                 u64 = linkp->ll_linkid;
1284                 ptr += write_uint64(ptr, BUFLEN(lim, ptr), "linkid", &u64);
1285 
1286                 if (getzonenamebyid(linkp->ll_zoneid, zname,
1287                     sizeof (zname)) != -1) {
1288                         ptr += write_str(ptr, BUFLEN(lim, ptr), "zone", zname);
1289                 }
1290         }
1291         u64 = linkp->ll_class;
1292         ptr += write_uint64(ptr, BUFLEN(lim, ptr), "class", &u64);
1293         u64 = linkp->ll_media;
1294         ptr += write_uint64(ptr, BUFLEN(lim, ptr), "media", &u64);
1295 
1296         if (!persist && linkp->ll_transient) {
1297                 boolean_t b = B_TRUE;
1298                 ptr += write_boolean(ptr, BUFLEN(lim, ptr), "transient", &b);
1299         }
1300 
1301         /*
1302          * The daemon does not keep any active link attribute. Only store the
1303          * attributes if this request is for persistent configuration,
1304          */
1305         if (persist) {
1306                 for (cur_p = linkp->ll_head; cur_p != NULL;
1307                     cur_p = cur_p->lp_next) {
1308                         ptr += translators[cur_p->lp_type].write_func(ptr,
1309                             BUFLEN(lim, ptr), cur_p->lp_name, cur_p->lp_val);
1310                 }
1311         }
1312 
1313         if (ptr <= lim)
1314                 (void) snprintf(buf, MAXLINELEN, "%s\n", tmpbuf);
1315 }
1316 
1317 int
1318 dlmgmt_delete_db_entry(dlmgmt_link_t *linkp, uint32_t flags)
1319 {
1320         return (dlmgmt_db_update(DLMGMT_DB_OP_DELETE, linkp->ll_link, linkp,
1321             flags));
1322 }
1323 
1324 int
1325 dlmgmt_write_db_entry(const char *entryname, dlmgmt_link_t *linkp,
1326     uint32_t flags)
1327 {
1328         int err;
1329 
1330         if (flags & DLMGMT_PERSIST) {
1331                 if ((err = dlmgmt_db_update(DLMGMT_DB_OP_WRITE, entryname,
1332                     linkp, DLMGMT_PERSIST)) != 0) {
1333                         return (err);
1334                 }
1335         }
1336 
1337         if (flags & DLMGMT_ACTIVE) {
1338                 if (((err = dlmgmt_db_update(DLMGMT_DB_OP_WRITE, entryname,
1339                     linkp, DLMGMT_ACTIVE)) != 0) && (flags & DLMGMT_PERSIST)) {
1340                         (void) dlmgmt_db_update(DLMGMT_DB_OP_DELETE, entryname,
1341                             linkp, DLMGMT_PERSIST);
1342                         return (err);
1343                 }
1344         }
1345 
1346         return (0);
1347 }
1348 
1349 /*
1350  * Upgrade properties that have link IDs as values to link names.  Because '.'
1351  * is a valid linkname character, the port separater for link aggregations
1352  * must be changed to ':'.
1353  */
1354 static void
1355 linkattr_upgrade(dlmgmt_linkattr_t *attrp)
1356 {
1357         datalink_id_t   linkid;
1358         char            *portidstr;
1359         char            portname[MAXLINKNAMELEN + 1];
1360         dlmgmt_link_t   *linkp;
1361         char            *new_attr_val;
1362         size_t          new_attr_sz;
1363         boolean_t       upgraded = B_FALSE;
1364 
1365         if (strcmp(attrp->lp_name, "linkover") == 0 ||
1366             strcmp(attrp->lp_name, "simnetpeer") == 0) {
1367                 if (attrp->lp_type == DLADM_TYPE_UINT64) {
1368                         linkid = (datalink_id_t)*(uint64_t *)attrp->lp_val;
1369                         if ((linkp = link_by_id(linkid, GLOBAL_ZONEID)) == NULL)
1370                                 return;
1371                         new_attr_sz = strlen(linkp->ll_link) + 1;
1372                         if ((new_attr_val = malloc(new_attr_sz)) == NULL)
1373                                 return;
1374                         (void) strcpy(new_attr_val, linkp->ll_link);
1375                         upgraded = B_TRUE;
1376                 }
1377         } else if (strcmp(attrp->lp_name, "portnames") == 0) {
1378                 /*
1379                  * The old format for "portnames" was
1380                  * "<linkid>.[<linkid>.]...".  The new format is
1381                  * "<linkname>:[<linkname>:]...".
1382                  */
1383                 if (!isdigit(((char *)attrp->lp_val)[0]))
1384                         return;
1385                 new_attr_val = calloc(MAXLINKATTRVALLEN, sizeof (char));
1386                 if (new_attr_val == NULL)
1387                         return;
1388                 portidstr = (char *)attrp->lp_val;
1389                 while (*portidstr != '\0') {
1390                         errno = 0;
1391                         linkid = strtol(portidstr, &portidstr, 10);
1392                         if (linkid == 0 || *portidstr != '.' ||
1393                             (linkp = link_by_id(linkid, GLOBAL_ZONEID)) ==
1394                             NULL) {
1395                                 free(new_attr_val);
1396                                 return;
1397                         }
1398                         (void) snprintf(portname, sizeof (portname), "%s:",
1399                             linkp->ll_link);
1400                         if (strlcat(new_attr_val, portname,
1401                             MAXLINKATTRVALLEN) >= MAXLINKATTRVALLEN) {
1402                                 free(new_attr_val);
1403                                 return;
1404                         }
1405                         /* skip the '.' delimiter */
1406                         portidstr++;
1407                 }
1408                 new_attr_sz = strlen(new_attr_val) + 1;
1409                 upgraded = B_TRUE;
1410         }
1411 
1412         if (upgraded) {
1413                 attrp->lp_type = DLADM_TYPE_STR;
1414                 attrp->lp_sz = new_attr_sz;
1415                 free(attrp->lp_val);
1416                 attrp->lp_val = new_attr_val;
1417         }
1418 }
1419 
1420 static void
1421 dlmgmt_db_upgrade(dlmgmt_link_t *linkp)
1422 {
1423         dlmgmt_linkattr_t *attrp;
1424 
1425         for (attrp = linkp->ll_head; attrp != NULL; attrp = attrp->lp_next)
1426                 linkattr_upgrade(attrp);
1427 }
1428 
1429 static void
1430 dlmgmt_db_phys_activate(dlmgmt_link_t *linkp)
1431 {
1432         linkp->ll_flags |= DLMGMT_ACTIVE;
1433         (void) dlmgmt_write_db_entry(linkp->ll_link, linkp, DLMGMT_ACTIVE);
1434 }
1435 
1436 static void
1437 dlmgmt_db_walk(zoneid_t zoneid, datalink_class_t class, db_walk_func_t *func)
1438 {
1439         dlmgmt_link_t *linkp;
1440 
1441         for (linkp = avl_first(&dlmgmt_id_avl); linkp != NULL;
1442             linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
1443                 if (linkp->ll_zoneid == zoneid && (linkp->ll_class & class))
1444                         func(linkp);
1445         }
1446 }
1447 
1448 /*
1449  * Attempt to mitigate one of the deadlocks in the dlmgmtd architecture.
1450  *
1451  * dlmgmt_db_init() calls dlmgmt_process_db_req() which eventually gets to
1452  * dlmgmt_zfop() which tries to fork, enter the zone and read the file.
1453  * Because of the upcall architecture of dlmgmtd this can lead to deadlock
1454  * with the following scenario:
1455  *    a) the thread preparing to fork will have acquired the malloc locks
1456  *       then attempt to suspend every thread in preparation to fork.
1457  *    b) all of the upcalls will be blocked in door_ucred() trying to malloc()
1458  *       and get the credentials of their caller.
1459  *    c) we can't suspend the in-kernel thread making the upcall.
1460  *
1461  * Thus, we cannot serve door requests because we're blocked in malloc()
1462  * which fork() owns, but fork() is in turn blocked on the in-kernel thread
1463  * making the door upcall.  This is a fundamental architectural problem with
1464  * any server handling upcalls and also trying to fork().
1465  *
1466  * To minimize the chance of this deadlock occuring, we check ahead of time to
1467  * see if the file we want to read actually exists in the zone (which it almost
1468  * never does), so we don't need fork in that case (i.e. rarely to never).
1469  */
1470 static boolean_t
1471 zone_file_exists(char *zoneroot, char *filename)
1472 {
1473         struct stat     sb;
1474         char            fname[MAXPATHLEN];
1475 
1476         (void) snprintf(fname, sizeof (fname), "%s/%s", zoneroot, filename);
1477 
1478         if (stat(fname, &sb) == -1)
1479                 return (B_FALSE);
1480 
1481         return (B_TRUE);
1482 }
1483 
1484 /*
1485  * Initialize the datalink <link name, linkid> mapping and the link's
1486  * attributes list based on the configuration file /etc/dladm/datalink.conf
1487  * and the active configuration cache file
1488  * /etc/svc/volatile/dladm/datalink-management:default.cache.
1489  */
1490 int
1491 dlmgmt_db_init(zoneid_t zoneid, char *zoneroot)
1492 {
1493         dlmgmt_db_req_t *req;
1494         int             err;
1495         boolean_t       boot = B_FALSE;
1496         char            tdir[MAXPATHLEN];
1497         char            *path = cachefile;
1498 
1499         if ((req = dlmgmt_db_req_alloc(DLMGMT_DB_OP_READ, NULL,
1500             DATALINK_INVALID_LINKID, zoneid, DLMGMT_ACTIVE, &err)) == NULL)
1501                 return (err);
1502 
1503         /* Handle running in a non-native branded zone (i.e. has /native) */
1504         if (zone_file_exists(zoneroot, "/native" DLMGMT_TMPFS_DIR)) {
1505                 (void) snprintf(tdir, sizeof (tdir), "/native%s", cachefile);
1506                 path = tdir;
1507         }
1508 
1509         if (zone_file_exists(zoneroot, path)) {
1510                 if ((err = dlmgmt_process_db_req(req)) != 0) {
1511                         /*
1512                          * If we get back ENOENT, that means that the active
1513                          * configuration file doesn't exist yet, and is not an
1514                          * error.  We'll create it down below after we've
1515                          * loaded the persistent configuration.
1516                          */
1517                         if (err != ENOENT)
1518                                 goto done;
1519                         boot = B_TRUE;
1520                 }
1521         } else {
1522                 boot = B_TRUE;
1523         }
1524 
1525         if (zone_file_exists(zoneroot, DLMGMT_PERSISTENT_DB_PATH)) {
1526                 req->ls_flags = DLMGMT_PERSIST;
1527                 err = dlmgmt_process_db_req(req);
1528                 if (err != 0 && err != ENOENT)
1529                         goto done;
1530         }
1531         err = 0;
1532         if (rewrite_needed) {
1533                 /*
1534                  * First update links in memory, then dump the entire db to
1535                  * disk.
1536                  */
1537                 dlmgmt_db_walk(zoneid, DATALINK_CLASS_ALL, dlmgmt_db_upgrade);
1538                 req->ls_op = DLMGMT_DB_OP_WRITE;
1539                 req->ls_linkid = DATALINK_ALL_LINKID;
1540                 if ((err = dlmgmt_process_db_req(req)) != 0 &&
1541                     err != EINPROGRESS)
1542                         goto done;
1543         }
1544         if (boot) {
1545                 dlmgmt_db_walk(zoneid, DATALINK_CLASS_PHYS,
1546                     dlmgmt_db_phys_activate);
1547         }
1548 
1549 done:
1550         if (err == EINPROGRESS)
1551                 err = 0;
1552         else
1553                 free(req);
1554         return (err);
1555 }
1556 
1557 /*
1558  * Remove all links in the given zoneid.
1559  */
1560 void
1561 dlmgmt_db_fini(zoneid_t zoneid)
1562 {
1563         dlmgmt_link_t *linkp = avl_first(&dlmgmt_name_avl), *next_linkp;
1564 
1565         while (linkp != NULL) {
1566                 next_linkp = AVL_NEXT(&dlmgmt_name_avl, linkp);
1567                 if (linkp->ll_zoneid == zoneid) {
1568                         (void) dlmgmt_destroy_common(linkp,
1569                             DLMGMT_ACTIVE | DLMGMT_PERSIST);
1570                 }
1571                 linkp = next_linkp;
1572         }
1573 }