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