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) 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2015 Joyent, Inc.
  25  */
  26 
  27 /*
  28  * The ipmgmtd daemon is started by ip-interface-management SMF service. This
  29  * daemon is used to manage, mapping of 'address object' to 'interface name' and
  30  * 'logical interface number', on which the address is created. It also provides
  31  * a means to update the ipadm persistent data-store.
  32  *
  33  * The daemon tracks the <addrobj, lifname> mapping in-memory using a linked
  34  * list `aobjmap'. Access to this list is synchronized using a readers-writers
  35  * lock. The active <addrobj, lifname> mapping is kept in
  36  * /etc/svc/volatile/ipadm/aobjmap.conf cache file, so that the mapping can be
  37  * recovered when ipmgmtd exits for some reason (e.g., when ipmgmtd is restarted
  38  * using svcadm or accidentally killed).
  39  *
  40  * Today, the persistent configuration of interfaces, addresses and protocol
  41  * properties is kept in /etc/ipadm/ipadm.conf. The access to the persistent
  42  * data store is synchronized using reader-writers lock `ipmgmt_dbconf_lock'.
  43  *
  44  * The communication between the library, libipadm.so and the daemon, is through
  45  * doors RPC. The library interacts with the daemon using the commands defined
  46  * by `ipmgmt_door_cmd_type_t'. Further any 'write' operation would require
  47  * the `NETWORK_INTERFACE_CONFIG_AUTH' authorization.
  48  *
  49  * On reboot, the aforementioned SMF service starts the daemon before any other
  50  * networking service that configures network IP interfaces is started.
  51  * Afterwards, the network/physical SMF script instantiates the persisted
  52  * network interfaces, interface properties and addresses.
  53  */
  54 
  55 #include <errno.h>
  56 #include <fcntl.h>
  57 #include <priv_utils.h>
  58 #include <signal.h>
  59 #include <stdlib.h>
  60 #include <stdio.h>
  61 #include <strings.h>
  62 #include <sys/param.h>
  63 #include <sys/stat.h>
  64 #include <unistd.h>
  65 #include "ipmgmt_impl.h"
  66 #include <zone.h>
  67 #include <libipadm.h>
  68 #include <libdladm.h>
  69 #include <libdllink.h>
  70 #include <net/route.h>
  71 #include <ipadm_ipmgmt.h>
  72 #include <sys/brand.h>
  73 
  74 const char              *progname;
  75 
  76 /* readers-writers lock for reading/writing daemon data store */
  77 pthread_rwlock_t        ipmgmt_dbconf_lock = PTHREAD_RWLOCK_INITIALIZER;
  78 
  79 /* tracks address object to {ifname|logical number|interface id} mapping */
  80 ipmgmt_aobjmap_list_t   aobjmap;
  81 
  82 /* used to communicate failure to parent process, which spawned the daemon */
  83 static int              pfds[2];
  84 
  85 /* file descriptor to IPMGMT_DOOR */
  86 static int              ipmgmt_door_fd = -1;
  87 
  88 static void             ipmgmt_exit(int);
  89 static int              ipmgmt_init();
  90 static int              ipmgmt_init_privileges();
  91 static void             ipmgmt_ngz_persist_if();
  92 
  93 static ipadm_handle_t iph;
  94 typedef struct ipmgmt_pif_s {
  95         struct ipmgmt_pif_s     *pif_next;
  96         char                    pif_ifname[LIFNAMSIZ];
  97         boolean_t               pif_v4;
  98         boolean_t               pif_v6;
  99 } ipmgmt_pif_t;
 100 
 101 static ipmgmt_pif_t *ngz_pifs;
 102 
 103 static int
 104 ipmgmt_db_init()
 105 {
 106         int             fd, err, scferr;
 107         scf_resources_t res;
 108         boolean_t       upgrade = B_TRUE;
 109         char            aobjpath[MAXPATHLEN];
 110 
 111         /*
 112          * Check to see if we need to upgrade the data-store. We need to
 113          * upgrade, if the version of the data-store does not match with
 114          * IPADM_DB_VERSION. Further, if we cannot determine the current
 115          * version of the data-store, we always err on the side of caution
 116          * and upgrade the data-store to current version.
 117          */
 118         if ((scferr = ipmgmt_create_scf_resources(IPMGMTD_FMRI, &res)) == 0)
 119                 upgrade = ipmgmt_needs_upgrade(&res);
 120         if (upgrade) {
 121                 err = ipmgmt_db_walk(ipmgmt_db_upgrade, NULL, IPADM_DB_WRITE);
 122                 if (err != 0) {
 123                         ipmgmt_log(LOG_ERR, "could not upgrade the "
 124                             "ipadm data-store: %s", strerror(err));
 125                         err = 0;
 126                 } else {
 127                         /*
 128                          * upgrade was success, let's update SCF with the
 129                          * current data-store version number.
 130                          */
 131                         if (scferr == 0)
 132                                 ipmgmt_update_dbver(&res);
 133                 }
 134         }
 135         if (scferr == 0)
 136                 ipmgmt_release_scf_resources(&res);
 137 
 138         /* creates the address object data store, if it doesn't exist */
 139         ipmgmt_path(IPADM_PATH_ADDROBJ_MAP_DB, aobjpath, sizeof (aobjpath));
 140         if ((fd = open(aobjpath, O_CREAT|O_RDONLY, IPADM_FILE_MODE)) == -1) {
 141                 err = errno;
 142                 ipmgmt_log(LOG_ERR, "could not open %s: %s", aobjpath,
 143                     strerror(err));
 144                 return (err);
 145         }
 146         (void) close(fd);
 147 
 148         aobjmap.aobjmap_head = NULL;
 149         (void) pthread_rwlock_init(&aobjmap.aobjmap_rwlock, NULL);
 150 
 151         /*
 152          * If the daemon is recovering from a crash or restart, read the
 153          * address object to logical interface mapping and build an in-memory
 154          * representation of the mapping. That is, build `aobjmap' structure
 155          * from address object data store.
 156          */
 157         if ((err = ipadm_rw_db(ipmgmt_aobjmap_init, NULL, aobjpath, 0,
 158             IPADM_DB_READ)) != 0) {
 159                 /* if there was nothing to initialize, it's fine */
 160                 if (err != ENOENT)
 161                         return (err);
 162                 err = 0;
 163         }
 164 
 165         ipmgmt_ngz_persist_if(); /* create persistent interface info for NGZ */
 166 
 167         return (err);
 168 }
 169 
 170 static const char *
 171 ipmgmt_door_path()
 172 {
 173         static char door[MAXPATHLEN];
 174         static boolean_t init_done = B_FALSE;
 175 
 176         if (!init_done) {
 177                 const char *zroot = zone_get_nroot();
 178 
 179                 /*
 180                  * If this is a branded zone, make sure we use the "/native"
 181                  * prefix for the door path:
 182                  */
 183                 (void) snprintf(door, sizeof (door), "%s%s", zroot != NULL ?
 184                     zroot : "", IPMGMT_DOOR);
 185 
 186                 init_done = B_TRUE;
 187         }
 188 
 189         return (door);
 190 }
 191 
 192 static int
 193 ipmgmt_door_init()
 194 {
 195         int fd;
 196         int err;
 197         const char *door = ipmgmt_door_path();
 198 
 199         /*
 200          * Create the door file for ipmgmtd.
 201          */
 202         if ((fd = open(door, O_CREAT | O_RDONLY, IPADM_FILE_MODE)) == -1) {
 203                 err = errno;
 204                 ipmgmt_log(LOG_ERR, "could not open %s: %s", door,
 205                     strerror(err));
 206                 return (err);
 207         }
 208         (void) close(fd);
 209 
 210         if ((ipmgmt_door_fd = door_create(ipmgmt_handler, NULL,
 211             DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
 212                 err = errno;
 213                 ipmgmt_log(LOG_ERR, "failed to create door: %s", strerror(err));
 214                 return (err);
 215         }
 216 
 217         /*
 218          * fdetach first in case a previous daemon instance exited
 219          * ungracefully.
 220          */
 221         (void) fdetach(door);
 222         if (fattach(ipmgmt_door_fd, door) != 0) {
 223                 err = errno;
 224                 ipmgmt_log(LOG_ERR, "failed to attach door to %s: %s", door,
 225                     strerror(err));
 226                 goto fail;
 227         }
 228         return (0);
 229 fail:
 230         (void) door_revoke(ipmgmt_door_fd);
 231         ipmgmt_door_fd = -1;
 232         return (err);
 233 }
 234 
 235 static void
 236 ipmgmt_door_fini()
 237 {
 238         const char *door = ipmgmt_door_path();
 239 
 240         if (ipmgmt_door_fd == -1)
 241                 return;
 242 
 243         (void) fdetach(door);
 244         if (door_revoke(ipmgmt_door_fd) == -1) {
 245                 ipmgmt_log(LOG_ERR, "failed to revoke access to door %s: %s",
 246                     door, strerror(errno));
 247         }
 248 }
 249 
 250 static int
 251 ipmgmt_init()
 252 {
 253         int err;
 254 
 255         if (signal(SIGTERM, ipmgmt_exit) == SIG_ERR ||
 256             signal(SIGINT, ipmgmt_exit) == SIG_ERR) {
 257                 err = errno;
 258                 ipmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
 259                     strerror(err));
 260                 return (err);
 261         }
 262         if ((err = ipmgmt_db_init()) != 0 || (err = ipmgmt_door_init()) != 0)
 263                 return (err);
 264         return (0);
 265 }
 266 
 267 /*
 268  * This is called by the child process to inform the parent process to
 269  * exit with the given return value.
 270  */
 271 static void
 272 ipmgmt_inform_parent_exit(int rv)
 273 {
 274         if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
 275                 ipmgmt_log(LOG_WARNING,
 276                     "failed to inform parent process of status: %s",
 277                     strerror(errno));
 278                 (void) close(pfds[1]);
 279                 exit(EXIT_FAILURE);
 280         }
 281         (void) close(pfds[1]);
 282 }
 283 
 284 /*ARGSUSED*/
 285 static void
 286 ipmgmt_exit(int signo)
 287 {
 288         (void) close(pfds[1]);
 289         ipmgmt_door_fini();
 290         exit(EXIT_FAILURE);
 291 }
 292 
 293 /*
 294  * On the first reboot after installation of an ipkg zone,
 295  * ipmgmt_persist_if_cb() is used in non-global zones to track the interfaces
 296  * that have IP address configuration assignments from the global zone.
 297  * Persistent configuration for the interfaces is created on the first boot
 298  * by ipmgmtd, and the addresses assigned to the interfaces by the GZ
 299  * will be subsequently configured when the interface is enabled.
 300  * Note that ipmgmt_persist_if_cb() only sets up a list of interfaces
 301  * that need to be persisted- the actual update of the ipadm data-store happens
 302  * in ipmgmt_persist_if() after the appropriate privs/uid state has been set up.
 303  */
 304 static void
 305 ipmgmt_persist_if_cb(char *ifname, boolean_t v4, boolean_t v6)
 306 {
 307         ipmgmt_pif_t *pif;
 308 
 309         pif = calloc(1, sizeof (*pif));
 310         if (pif == NULL) {
 311                 ipmgmt_log(LOG_WARNING,
 312                     "Could not allocate memory to configure %s", ifname);
 313                 return;
 314         }
 315         (void) strlcpy(pif->pif_ifname, ifname, sizeof (pif->pif_ifname));
 316         pif->pif_v4 = v4;
 317         pif->pif_v6 = v6;
 318         pif->pif_next = ngz_pifs;
 319         ngz_pifs = pif;
 320 }
 321 
 322 /*
 323  * ipmgmt_ngz_init() initializes exclusive-IP stack non-global zones by
 324  * extracting configuration that has been saved in the kernel and applying
 325  * it at zone boot.
 326  */
 327 static void
 328 ipmgmt_ngz_init()
 329 {
 330         zoneid_t zoneid;
 331         boolean_t firstboot = B_TRUE, s10c = B_FALSE;
 332         char brand[MAXNAMELEN];
 333         ipadm_status_t ipstatus;
 334 
 335         zoneid = getzoneid();
 336         if (zoneid != GLOBAL_ZONEID) {
 337 
 338                 if (zone_getattr(zoneid, ZONE_ATTR_BRAND, brand,
 339                     sizeof (brand)) < 0) {
 340                         ipmgmt_log(LOG_ERR, "Could not get brand name");
 341                         return;
 342                 }
 343                 /*
 344                  * firstboot is always true for S10C zones, where ipadm is not
 345                  * available for restoring persistent configuration.
 346                  */
 347                 if (strcmp(brand, NATIVE_BRAND_NAME) == 0)
 348                         firstboot = ipmgmt_ngz_firstboot_postinstall();
 349                 else
 350                         s10c = B_TRUE;
 351 
 352                 if (!firstboot)
 353                         return;
 354 
 355                 ipstatus = ipadm_open(&iph, IPH_IPMGMTD);
 356                 if (ipstatus != IPADM_SUCCESS) {
 357                         ipmgmt_log(LOG_ERR, "could not open ipadm handle",
 358                             ipadm_status2str(ipstatus));
 359                         return;
 360                 }
 361                 /*
 362                  * Only pass down the callback to persist the interface
 363                  * for NATIVE (ipkg) zones.
 364                  */
 365                 (void) ipadm_init_net_from_gz(iph, NULL,
 366                     (s10c ? NULL : ipmgmt_persist_if_cb));
 367                 ipadm_close(iph);
 368         }
 369 }
 370 
 371 /*
 372  * Set the uid of this daemon to the "netadm" user. Finish the following
 373  * operations before setuid() because they need root privileges:
 374  *
 375  *    - create the /etc/svc/volatile/ipadm directory;
 376  *    - change its uid/gid to "netadm"/"netadm";
 377  */
 378 static int
 379 ipmgmt_init_privileges()
 380 {
 381         struct stat     statbuf;
 382         int             err;
 383         char            tmpfsdir[MAXPATHLEN];
 384 
 385         /*
 386          * Create the volatile storage directory:
 387          */
 388         ipmgmt_path(IPADM_PATH_TMPFS_DIR, tmpfsdir, sizeof (tmpfsdir));
 389         if (stat(tmpfsdir, &statbuf) < 0) {
 390                 if (mkdir(tmpfsdir, (mode_t)0755) < 0) {
 391                         err = errno;
 392                         goto fail;
 393                 }
 394         } else {
 395                 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
 396                         err = ENOTDIR;
 397                         goto fail;
 398                 }
 399         }
 400 
 401         if ((chmod(tmpfsdir, 0755) < 0) ||
 402             (chown(tmpfsdir, UID_NETADM, GID_NETADM) < 0)) {
 403                 err = errno;
 404                 goto fail;
 405         }
 406 
 407         /*
 408          * initialize any NGZ specific network information before dropping
 409          * privileges. We need these privileges to plumb IP interfaces handed
 410          * down from the GZ (for dlpi_open() etc.) and also to configure the
 411          * address itself (for any IPI_PRIV ioctls like SLIFADDR)
 412          */
 413         ipmgmt_ngz_init();
 414 
 415         /*
 416          * Apply all protocol module properties. We need to apply all protocol
 417          * properties before we drop root privileges.
 418          */
 419         ipmgmt_init_prop();
 420 
 421         /*
 422          * limit the privileges of this daemon and set the uid of this
 423          * daemon to UID_NETADM
 424          */
 425         if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_NETADM,
 426             GID_NETADM, NULL) == -1) {
 427                 err = EPERM;
 428                 goto fail;
 429         }
 430 
 431         return (0);
 432 fail:
 433         (void) ipmgmt_log(LOG_ERR, "failed to initialize the daemon: %s",
 434             strerror(err));
 435         return (err);
 436 }
 437 
 438 /*
 439  * Keep the pfds fd open, close other fds.
 440  */
 441 /*ARGSUSED*/
 442 static int
 443 closefunc(void *arg, int fd)
 444 {
 445         if (fd != pfds[1])
 446                 (void) close(fd);
 447         return (0);
 448 }
 449 
 450 /*
 451  * We cannot use libc's daemon() because the door we create is associated with
 452  * the process ID. If we create the door before the call to daemon(), it will
 453  * be associated with the parent and it's incorrect. On the other hand if we
 454  * create the door later, after the call to daemon(), parent process exits
 455  * early and gives a false notion to SMF that 'ipmgmtd' is up and running,
 456  * which is incorrect. So, we have our own daemon() equivalent.
 457  */
 458 static boolean_t
 459 ipmgmt_daemonize(void)
 460 {
 461         pid_t pid;
 462         int rv;
 463 
 464         if (pipe(pfds) < 0) {
 465                 (void) fprintf(stderr, "%s: pipe() failed: %s\n",
 466                     progname, strerror(errno));
 467                 exit(EXIT_FAILURE);
 468         }
 469 
 470         if ((pid = fork()) == -1) {
 471                 (void) fprintf(stderr, "%s: fork() failed: %s\n",
 472                     progname, strerror(errno));
 473                 exit(EXIT_FAILURE);
 474         } else if (pid > 0) { /* Parent */
 475                 (void) close(pfds[1]);
 476 
 477                 /*
 478                  * Parent should not exit early, it should wait for the child
 479                  * to return Success/Failure. If the parent exits early, then
 480                  * SMF will think 'ipmgmtd' is up and would start all the
 481                  * depended services.
 482                  *
 483                  * If the child process exits unexpectedly, read() returns -1.
 484                  */
 485                 if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
 486                         (void) kill(pid, SIGKILL);
 487                         rv = EXIT_FAILURE;
 488                 }
 489 
 490                 (void) close(pfds[0]);
 491                 exit(rv);
 492         }
 493 
 494         /* Child */
 495         (void) close(pfds[0]);
 496         (void) setsid();
 497 
 498         /* close all files except pfds[1] */
 499         (void) fdwalk(closefunc, NULL);
 500         (void) chdir("/");
 501         openlog(progname, LOG_PID, LOG_DAEMON);
 502         return (B_TRUE);
 503 }
 504 
 505 int
 506 main(int argc, char *argv[])
 507 {
 508         int opt;
 509         boolean_t fg = B_FALSE;
 510 
 511         progname = strrchr(argv[0], '/');
 512         if (progname != NULL)
 513                 progname++;
 514         else
 515                 progname = argv[0];
 516 
 517         /* Process options */
 518         while ((opt = getopt(argc, argv, "f")) != EOF) {
 519                 switch (opt) {
 520                 case 'f':
 521                         fg = B_TRUE;
 522                         break;
 523                 default:
 524                         (void) fprintf(stderr, "Usage: %s [-f]\n", progname);
 525                         return (EXIT_FAILURE);
 526                 }
 527         }
 528 
 529         if (!fg && getenv("SMF_FMRI") == NULL) {
 530                 (void) fprintf(stderr,
 531                     "ipmgmtd is a smf(5) managed service and cannot be run "
 532                     "from the command line.\n");
 533                 return (EINVAL);
 534         }
 535 
 536         if (!fg && !ipmgmt_daemonize())
 537                 return (EXIT_FAILURE);
 538 
 539         if (ipmgmt_init_privileges() != 0)
 540                 goto child_out;
 541 
 542         if (ipmgmt_init() != 0)
 543                 goto child_out;
 544 
 545         /* Inform the parent process that it can successfully exit */
 546         ipmgmt_inform_parent_exit(EXIT_SUCCESS);
 547 
 548         for (;;)
 549                 (void) pause();
 550 
 551 child_out:
 552         /* return from main() forcibly exits an MT process */
 553         ipmgmt_inform_parent_exit(EXIT_FAILURE);
 554         return (EXIT_FAILURE);
 555 }
 556 
 557 /*
 558  * Return TRUE if `ifname' has persistent configuration for the `af' address
 559  * family in the datastore
 560  */
 561 static boolean_t
 562 ipmgmt_persist_if_exists(char *ifname, sa_family_t af)
 563 {
 564         ipmgmt_getif_cbarg_t cbarg;
 565         boolean_t exists = B_FALSE;
 566         ipadm_if_info_t *ifp;
 567 
 568         bzero(&cbarg, sizeof (cbarg));
 569         cbarg.cb_ifname = ifname;
 570         (void) ipmgmt_db_walk(ipmgmt_db_getif, &cbarg, IPADM_DB_READ);
 571         if ((ifp = cbarg.cb_ifinfo) != NULL) {
 572                 if ((af == AF_INET && (ifp->ifi_pflags & IFIF_IPV4)) ||
 573                     (af == AF_INET6 && (ifp->ifi_pflags & IFIF_IPV6))) {
 574                         exists = B_TRUE;
 575                 }
 576         }
 577         free(ifp);
 578         return (exists);
 579 }
 580 
 581 /*
 582  * Persist any NGZ interfaces assigned to us from the global zone if they do
 583  * not already exist in the persistent db. We need to
 584  * do this before any calls to ipadm_enable_if() can succeed (i.e.,
 585  * before opening up for door_calls), and after setuid to 'netadm' so that
 586  * the persistent db is created with the right permissions.
 587  */
 588 static void
 589 ipmgmt_ngz_persist_if()
 590 {
 591         ipmgmt_pif_t *pif, *next;
 592         ipmgmt_if_arg_t ifarg;
 593 
 594         for (pif = ngz_pifs; pif != NULL; pif = next) {
 595                 next = pif->pif_next;
 596                 bzero(&ifarg, sizeof (ifarg));
 597                 (void) strlcpy(ifarg.ia_ifname, pif->pif_ifname,
 598                     sizeof (ifarg.ia_ifname));
 599                 ifarg.ia_flags = IPMGMT_PERSIST;
 600                 if (pif->pif_v4 &&
 601                     !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET)) {
 602                         ifarg.ia_family = AF_INET;
 603                         (void) ipmgmt_persist_if(&ifarg);
 604                 }
 605                 if (pif->pif_v6 &&
 606                     !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET6)) {
 607                         ifarg.ia_family = AF_INET6;
 608                         (void) ipmgmt_persist_if(&ifarg);
 609                 }
 610                 free(pif);
 611         }
 612         ngz_pifs = NULL; /* no red herrings */
 613 }