112016SGirish.Moodalbail@Sun.COM /*
212016SGirish.Moodalbail@Sun.COM * CDDL HEADER START
312016SGirish.Moodalbail@Sun.COM *
412016SGirish.Moodalbail@Sun.COM * The contents of this file are subject to the terms of the
512016SGirish.Moodalbail@Sun.COM * Common Development and Distribution License (the "License").
612016SGirish.Moodalbail@Sun.COM * You may not use this file except in compliance with the License.
712016SGirish.Moodalbail@Sun.COM *
812016SGirish.Moodalbail@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
912016SGirish.Moodalbail@Sun.COM * or http://www.opensolaris.org/os/licensing.
1012016SGirish.Moodalbail@Sun.COM * See the License for the specific language governing permissions
1112016SGirish.Moodalbail@Sun.COM * and limitations under the License.
1212016SGirish.Moodalbail@Sun.COM *
1312016SGirish.Moodalbail@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
1412016SGirish.Moodalbail@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1512016SGirish.Moodalbail@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
1612016SGirish.Moodalbail@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
1712016SGirish.Moodalbail@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
1812016SGirish.Moodalbail@Sun.COM *
1912016SGirish.Moodalbail@Sun.COM * CDDL HEADER END
2012016SGirish.Moodalbail@Sun.COM */
2112016SGirish.Moodalbail@Sun.COM
2212016SGirish.Moodalbail@Sun.COM /*
2312748SSowmini.Varadhan@oracle.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2412016SGirish.Moodalbail@Sun.COM */
2512016SGirish.Moodalbail@Sun.COM
2612016SGirish.Moodalbail@Sun.COM /*
2712016SGirish.Moodalbail@Sun.COM * The ipmgmtd daemon is started by ip-interface-management SMF service. This
2812016SGirish.Moodalbail@Sun.COM * daemon is used to manage, mapping of 'address object' to 'interface name' and
2912016SGirish.Moodalbail@Sun.COM * 'logical interface number', on which the address is created. It also provides
3012016SGirish.Moodalbail@Sun.COM * a means to update the ipadm persistent data-store.
3112016SGirish.Moodalbail@Sun.COM *
3212016SGirish.Moodalbail@Sun.COM * The daemon tracks the <addrobj, lifname> mapping in-memory using a linked
3312016SGirish.Moodalbail@Sun.COM * list `aobjmap'. Access to this list is synchronized using a readers-writers
3412016SGirish.Moodalbail@Sun.COM * lock. The active <addrobj, lifname> mapping is kept in
3512016SGirish.Moodalbail@Sun.COM * /etc/svc/volatile/ipadm/aobjmap.conf cache file, so that the mapping can be
3612016SGirish.Moodalbail@Sun.COM * recovered when ipmgmtd exits for some reason (e.g., when ipmgmtd is restarted
3712016SGirish.Moodalbail@Sun.COM * using svcadm or accidentally killed).
3812016SGirish.Moodalbail@Sun.COM *
3912016SGirish.Moodalbail@Sun.COM * Today, the persistent configuration of interfaces, addresses and protocol
4012016SGirish.Moodalbail@Sun.COM * properties is kept in /etc/ipadm/ipadm.conf. The access to the persistent
4112016SGirish.Moodalbail@Sun.COM * data store is synchronized using reader-writers lock `ipmgmt_dbconf_lock'.
4212016SGirish.Moodalbail@Sun.COM *
4312016SGirish.Moodalbail@Sun.COM * The communication between the library, libipadm.so and the daemon, is through
4412016SGirish.Moodalbail@Sun.COM * doors RPC. The library interacts with the daemon using the commands defined
4512016SGirish.Moodalbail@Sun.COM * by `ipmgmt_door_cmd_type_t'. Further any 'write' operation would require
4612016SGirish.Moodalbail@Sun.COM * the `NETWORK_INTERFACE_CONFIG_AUTH' authorization.
4712016SGirish.Moodalbail@Sun.COM *
4812016SGirish.Moodalbail@Sun.COM * On reboot, the aforementioned SMF service starts the daemon before any other
4912016SGirish.Moodalbail@Sun.COM * networking service that configures network IP interfaces is started.
5012016SGirish.Moodalbail@Sun.COM * Afterwards, the network/physical SMF script instantiates the persisted
5112016SGirish.Moodalbail@Sun.COM * network interfaces, interface properties and addresses.
5212016SGirish.Moodalbail@Sun.COM */
5312016SGirish.Moodalbail@Sun.COM
5412016SGirish.Moodalbail@Sun.COM #include <errno.h>
5512016SGirish.Moodalbail@Sun.COM #include <fcntl.h>
5612016SGirish.Moodalbail@Sun.COM #include <priv_utils.h>
5712016SGirish.Moodalbail@Sun.COM #include <signal.h>
5812016SGirish.Moodalbail@Sun.COM #include <stdlib.h>
5912016SGirish.Moodalbail@Sun.COM #include <stdio.h>
6012016SGirish.Moodalbail@Sun.COM #include <strings.h>
6112016SGirish.Moodalbail@Sun.COM #include <sys/param.h>
6212016SGirish.Moodalbail@Sun.COM #include <sys/stat.h>
6312016SGirish.Moodalbail@Sun.COM #include <unistd.h>
6412016SGirish.Moodalbail@Sun.COM #include "ipmgmt_impl.h"
6512748SSowmini.Varadhan@oracle.COM #include <zone.h>
6612748SSowmini.Varadhan@oracle.COM #include <libipadm.h>
6712748SSowmini.Varadhan@oracle.COM #include <libdladm.h>
6812748SSowmini.Varadhan@oracle.COM #include <libdllink.h>
6912748SSowmini.Varadhan@oracle.COM #include <net/route.h>
7012748SSowmini.Varadhan@oracle.COM #include <ipadm_ipmgmt.h>
7112748SSowmini.Varadhan@oracle.COM #include <sys/brand.h>
7212016SGirish.Moodalbail@Sun.COM
7312016SGirish.Moodalbail@Sun.COM const char *progname;
7412016SGirish.Moodalbail@Sun.COM
7512016SGirish.Moodalbail@Sun.COM /* readers-writers lock for reading/writing daemon data store */
76*13125SGirish.Moodalbail@oracle.COM pthread_rwlock_t ipmgmt_dbconf_lock = PTHREAD_RWLOCK_INITIALIZER;
7712016SGirish.Moodalbail@Sun.COM
7812016SGirish.Moodalbail@Sun.COM /* tracks address object to {ifname|logical number|interface id} mapping */
7912016SGirish.Moodalbail@Sun.COM ipmgmt_aobjmap_list_t aobjmap;
8012016SGirish.Moodalbail@Sun.COM
8112016SGirish.Moodalbail@Sun.COM /* used to communicate failure to parent process, which spawned the daemon */
8212016SGirish.Moodalbail@Sun.COM static int pfds[2];
8312016SGirish.Moodalbail@Sun.COM
8412016SGirish.Moodalbail@Sun.COM /* file descriptor to IPMGMT_DOOR */
8512016SGirish.Moodalbail@Sun.COM static int ipmgmt_door_fd = -1;
8612016SGirish.Moodalbail@Sun.COM
8712016SGirish.Moodalbail@Sun.COM static void ipmgmt_exit(int);
8812016SGirish.Moodalbail@Sun.COM static int ipmgmt_init();
8912016SGirish.Moodalbail@Sun.COM static int ipmgmt_init_privileges();
9012748SSowmini.Varadhan@oracle.COM static void ipmgmt_ngz_persist_if();
9112748SSowmini.Varadhan@oracle.COM
9212748SSowmini.Varadhan@oracle.COM static ipadm_handle_t iph;
9312748SSowmini.Varadhan@oracle.COM typedef struct ipmgmt_pif_s {
9412748SSowmini.Varadhan@oracle.COM struct ipmgmt_pif_s *pif_next;
9512748SSowmini.Varadhan@oracle.COM char pif_ifname[LIFNAMSIZ];
9612748SSowmini.Varadhan@oracle.COM boolean_t pif_v4;
9712748SSowmini.Varadhan@oracle.COM boolean_t pif_v6;
9812748SSowmini.Varadhan@oracle.COM } ipmgmt_pif_t;
9912748SSowmini.Varadhan@oracle.COM
10012748SSowmini.Varadhan@oracle.COM static ipmgmt_pif_t *ngz_pifs;
10112016SGirish.Moodalbail@Sun.COM
10212016SGirish.Moodalbail@Sun.COM static int
ipmgmt_db_init()10312016SGirish.Moodalbail@Sun.COM ipmgmt_db_init()
10412016SGirish.Moodalbail@Sun.COM {
105*13125SGirish.Moodalbail@oracle.COM int fd, err, scferr;
106*13125SGirish.Moodalbail@oracle.COM scf_resources_t res;
107*13125SGirish.Moodalbail@oracle.COM boolean_t upgrade = B_TRUE;
108*13125SGirish.Moodalbail@oracle.COM
109*13125SGirish.Moodalbail@oracle.COM /*
110*13125SGirish.Moodalbail@oracle.COM * Check to see if we need to upgrade the data-store. We need to
111*13125SGirish.Moodalbail@oracle.COM * upgrade, if the version of the data-store does not match with
112*13125SGirish.Moodalbail@oracle.COM * IPADM_DB_VERSION. Further, if we cannot determine the current
113*13125SGirish.Moodalbail@oracle.COM * version of the data-store, we always err on the side of caution
114*13125SGirish.Moodalbail@oracle.COM * and upgrade the data-store to current version.
115*13125SGirish.Moodalbail@oracle.COM */
116*13125SGirish.Moodalbail@oracle.COM if ((scferr = ipmgmt_create_scf_resources(IPMGMTD_FMRI, &res)) == 0)
117*13125SGirish.Moodalbail@oracle.COM upgrade = ipmgmt_needs_upgrade(&res);
118*13125SGirish.Moodalbail@oracle.COM if (upgrade) {
119*13125SGirish.Moodalbail@oracle.COM err = ipmgmt_db_walk(ipmgmt_db_upgrade, NULL, IPADM_DB_WRITE);
120*13125SGirish.Moodalbail@oracle.COM if (err != 0) {
121*13125SGirish.Moodalbail@oracle.COM ipmgmt_log(LOG_ERR, "could not upgrade the "
122*13125SGirish.Moodalbail@oracle.COM "ipadm data-store: %s", strerror(err));
123*13125SGirish.Moodalbail@oracle.COM err = 0;
124*13125SGirish.Moodalbail@oracle.COM } else {
125*13125SGirish.Moodalbail@oracle.COM /*
126*13125SGirish.Moodalbail@oracle.COM * upgrade was success, let's update SCF with the
127*13125SGirish.Moodalbail@oracle.COM * current data-store version number.
128*13125SGirish.Moodalbail@oracle.COM */
129*13125SGirish.Moodalbail@oracle.COM if (scferr == 0)
130*13125SGirish.Moodalbail@oracle.COM ipmgmt_update_dbver(&res);
131*13125SGirish.Moodalbail@oracle.COM }
132*13125SGirish.Moodalbail@oracle.COM }
133*13125SGirish.Moodalbail@oracle.COM if (scferr == 0)
134*13125SGirish.Moodalbail@oracle.COM ipmgmt_release_scf_resources(&res);
13512016SGirish.Moodalbail@Sun.COM
13612016SGirish.Moodalbail@Sun.COM /* creates the address object data store, if it doesn't exist */
13712016SGirish.Moodalbail@Sun.COM if ((fd = open(ADDROBJ_MAPPING_DB_FILE, O_CREAT|O_RDONLY,
13812016SGirish.Moodalbail@Sun.COM IPADM_FILE_MODE)) == -1) {
13912016SGirish.Moodalbail@Sun.COM err = errno;
14012016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "could not open %s: %s",
14112016SGirish.Moodalbail@Sun.COM ADDROBJ_MAPPING_DB_FILE, strerror(err));
14212016SGirish.Moodalbail@Sun.COM return (err);
14312016SGirish.Moodalbail@Sun.COM }
14412016SGirish.Moodalbail@Sun.COM (void) close(fd);
14512016SGirish.Moodalbail@Sun.COM
14612016SGirish.Moodalbail@Sun.COM aobjmap.aobjmap_head = NULL;
14712016SGirish.Moodalbail@Sun.COM (void) pthread_rwlock_init(&aobjmap.aobjmap_rwlock, NULL);
14812016SGirish.Moodalbail@Sun.COM
14912016SGirish.Moodalbail@Sun.COM /*
15012016SGirish.Moodalbail@Sun.COM * If the daemon is recovering from a crash or restart, read the
15112016SGirish.Moodalbail@Sun.COM * address object to logical interface mapping and build an in-memory
15212016SGirish.Moodalbail@Sun.COM * representation of the mapping. That is, build `aobjmap' structure
15312016SGirish.Moodalbail@Sun.COM * from address object data store.
15412016SGirish.Moodalbail@Sun.COM */
15512016SGirish.Moodalbail@Sun.COM if ((err = ipadm_rw_db(ipmgmt_aobjmap_init, NULL,
15612016SGirish.Moodalbail@Sun.COM ADDROBJ_MAPPING_DB_FILE, 0, IPADM_DB_READ)) != 0) {
15712016SGirish.Moodalbail@Sun.COM /* if there was nothing to initialize, it's fine */
15812016SGirish.Moodalbail@Sun.COM if (err != ENOENT)
15912016SGirish.Moodalbail@Sun.COM return (err);
16012016SGirish.Moodalbail@Sun.COM err = 0;
16112016SGirish.Moodalbail@Sun.COM }
16212016SGirish.Moodalbail@Sun.COM
16312748SSowmini.Varadhan@oracle.COM ipmgmt_ngz_persist_if(); /* create persistent interface info for NGZ */
16412748SSowmini.Varadhan@oracle.COM
16512016SGirish.Moodalbail@Sun.COM return (err);
16612016SGirish.Moodalbail@Sun.COM }
16712016SGirish.Moodalbail@Sun.COM
16812016SGirish.Moodalbail@Sun.COM static int
ipmgmt_door_init()16912016SGirish.Moodalbail@Sun.COM ipmgmt_door_init()
17012016SGirish.Moodalbail@Sun.COM {
17112016SGirish.Moodalbail@Sun.COM int fd;
17212016SGirish.Moodalbail@Sun.COM int err;
17312016SGirish.Moodalbail@Sun.COM
17412016SGirish.Moodalbail@Sun.COM /* create the door file for ipmgmtd */
17512016SGirish.Moodalbail@Sun.COM if ((fd = open(IPMGMT_DOOR, O_CREAT|O_RDONLY, IPADM_FILE_MODE)) == -1) {
17612016SGirish.Moodalbail@Sun.COM err = errno;
17712016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "could not open %s: %s",
17812016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(err));
17912016SGirish.Moodalbail@Sun.COM return (err);
18012016SGirish.Moodalbail@Sun.COM }
18112016SGirish.Moodalbail@Sun.COM (void) close(fd);
18212016SGirish.Moodalbail@Sun.COM
18312016SGirish.Moodalbail@Sun.COM if ((ipmgmt_door_fd = door_create(ipmgmt_handler, NULL,
18412016SGirish.Moodalbail@Sun.COM DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
18512016SGirish.Moodalbail@Sun.COM err = errno;
18612016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to create door: %s", strerror(err));
18712016SGirish.Moodalbail@Sun.COM return (err);
18812016SGirish.Moodalbail@Sun.COM }
18912016SGirish.Moodalbail@Sun.COM /*
19012016SGirish.Moodalbail@Sun.COM * fdetach first in case a previous daemon instance exited
19112016SGirish.Moodalbail@Sun.COM * ungracefully.
19212016SGirish.Moodalbail@Sun.COM */
19312016SGirish.Moodalbail@Sun.COM (void) fdetach(IPMGMT_DOOR);
19412016SGirish.Moodalbail@Sun.COM if (fattach(ipmgmt_door_fd, IPMGMT_DOOR) != 0) {
19512016SGirish.Moodalbail@Sun.COM err = errno;
19612016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to attach door to %s: %s",
19712016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(err));
19812016SGirish.Moodalbail@Sun.COM goto fail;
19912016SGirish.Moodalbail@Sun.COM }
20012016SGirish.Moodalbail@Sun.COM return (0);
20112016SGirish.Moodalbail@Sun.COM fail:
20212016SGirish.Moodalbail@Sun.COM (void) door_revoke(ipmgmt_door_fd);
20312016SGirish.Moodalbail@Sun.COM ipmgmt_door_fd = -1;
20412016SGirish.Moodalbail@Sun.COM return (err);
20512016SGirish.Moodalbail@Sun.COM }
20612016SGirish.Moodalbail@Sun.COM
20712016SGirish.Moodalbail@Sun.COM static void
ipmgmt_door_fini()20812016SGirish.Moodalbail@Sun.COM ipmgmt_door_fini()
20912016SGirish.Moodalbail@Sun.COM {
21012016SGirish.Moodalbail@Sun.COM if (ipmgmt_door_fd == -1)
21112016SGirish.Moodalbail@Sun.COM return;
21212016SGirish.Moodalbail@Sun.COM
21312016SGirish.Moodalbail@Sun.COM (void) fdetach(IPMGMT_DOOR);
21412016SGirish.Moodalbail@Sun.COM if (door_revoke(ipmgmt_door_fd) == -1) {
21512016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to revoke access to door %s: %s",
21612016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(errno));
21712016SGirish.Moodalbail@Sun.COM }
21812016SGirish.Moodalbail@Sun.COM }
21912016SGirish.Moodalbail@Sun.COM
22012016SGirish.Moodalbail@Sun.COM static int
ipmgmt_init()22112016SGirish.Moodalbail@Sun.COM ipmgmt_init()
22212016SGirish.Moodalbail@Sun.COM {
22312016SGirish.Moodalbail@Sun.COM int err;
22412016SGirish.Moodalbail@Sun.COM
22512016SGirish.Moodalbail@Sun.COM if (signal(SIGTERM, ipmgmt_exit) == SIG_ERR ||
22612016SGirish.Moodalbail@Sun.COM signal(SIGINT, ipmgmt_exit) == SIG_ERR) {
22712016SGirish.Moodalbail@Sun.COM err = errno;
22812016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
22912016SGirish.Moodalbail@Sun.COM strerror(err));
23012016SGirish.Moodalbail@Sun.COM return (err);
23112016SGirish.Moodalbail@Sun.COM }
23212016SGirish.Moodalbail@Sun.COM if ((err = ipmgmt_db_init()) != 0 || (err = ipmgmt_door_init()) != 0)
23312016SGirish.Moodalbail@Sun.COM return (err);
23412016SGirish.Moodalbail@Sun.COM return (0);
23512016SGirish.Moodalbail@Sun.COM }
23612016SGirish.Moodalbail@Sun.COM
23712016SGirish.Moodalbail@Sun.COM /*
23812016SGirish.Moodalbail@Sun.COM * This is called by the child process to inform the parent process to
23912016SGirish.Moodalbail@Sun.COM * exit with the given return value.
24012016SGirish.Moodalbail@Sun.COM */
24112016SGirish.Moodalbail@Sun.COM static void
ipmgmt_inform_parent_exit(int rv)24212016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(int rv)
24312016SGirish.Moodalbail@Sun.COM {
24412016SGirish.Moodalbail@Sun.COM if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
24512016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_WARNING,
24612016SGirish.Moodalbail@Sun.COM "failed to inform parent process of status: %s",
24712016SGirish.Moodalbail@Sun.COM strerror(errno));
24812016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]);
24912016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE);
25012016SGirish.Moodalbail@Sun.COM }
25112016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]);
25212016SGirish.Moodalbail@Sun.COM }
25312016SGirish.Moodalbail@Sun.COM
25412016SGirish.Moodalbail@Sun.COM /*ARGSUSED*/
25512016SGirish.Moodalbail@Sun.COM static void
ipmgmt_exit(int signo)25612016SGirish.Moodalbail@Sun.COM ipmgmt_exit(int signo)
25712016SGirish.Moodalbail@Sun.COM {
25812016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]);
25912016SGirish.Moodalbail@Sun.COM ipmgmt_door_fini();
26012016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE);
26112016SGirish.Moodalbail@Sun.COM }
26212016SGirish.Moodalbail@Sun.COM
26312016SGirish.Moodalbail@Sun.COM /*
26412748SSowmini.Varadhan@oracle.COM * On the first reboot after installation of an ipkg zone,
26512748SSowmini.Varadhan@oracle.COM * ipmgmt_persist_if_cb() is used in non-global zones to track the interfaces
26612748SSowmini.Varadhan@oracle.COM * that have IP address configuration assignments from the global zone.
26712748SSowmini.Varadhan@oracle.COM * Persistent configuration for the interfaces is created on the first boot
26812748SSowmini.Varadhan@oracle.COM * by ipmgmtd, and the addresses assigned to the interfaces by the GZ
26912748SSowmini.Varadhan@oracle.COM * will be subsequently configured when the interface is enabled.
27012748SSowmini.Varadhan@oracle.COM * Note that ipmgmt_persist_if_cb() only sets up a list of interfaces
27112748SSowmini.Varadhan@oracle.COM * that need to be persisted- the actual update of the ipadm data-store happens
27212748SSowmini.Varadhan@oracle.COM * in ipmgmt_persist_if() after the appropriate privs/uid state has been set up.
27312748SSowmini.Varadhan@oracle.COM */
27412748SSowmini.Varadhan@oracle.COM static void
ipmgmt_persist_if_cb(char * ifname,boolean_t v4,boolean_t v6)27512748SSowmini.Varadhan@oracle.COM ipmgmt_persist_if_cb(char *ifname, boolean_t v4, boolean_t v6)
27612748SSowmini.Varadhan@oracle.COM {
27712748SSowmini.Varadhan@oracle.COM ipmgmt_pif_t *pif;
27812748SSowmini.Varadhan@oracle.COM
27912748SSowmini.Varadhan@oracle.COM pif = calloc(1, sizeof (*pif));
28012748SSowmini.Varadhan@oracle.COM if (pif == NULL) {
28112748SSowmini.Varadhan@oracle.COM ipmgmt_log(LOG_WARNING,
28212748SSowmini.Varadhan@oracle.COM "Could not allocate memory to configure %s", ifname);
28312748SSowmini.Varadhan@oracle.COM return;
28412748SSowmini.Varadhan@oracle.COM }
28512748SSowmini.Varadhan@oracle.COM (void) strlcpy(pif->pif_ifname, ifname, sizeof (pif->pif_ifname));
28612748SSowmini.Varadhan@oracle.COM pif->pif_v4 = v4;
28712748SSowmini.Varadhan@oracle.COM pif->pif_v6 = v6;
28812748SSowmini.Varadhan@oracle.COM pif->pif_next = ngz_pifs;
28912748SSowmini.Varadhan@oracle.COM ngz_pifs = pif;
29012748SSowmini.Varadhan@oracle.COM }
29112748SSowmini.Varadhan@oracle.COM
29212748SSowmini.Varadhan@oracle.COM /*
29312748SSowmini.Varadhan@oracle.COM * ipmgmt_ngz_init() initializes exclusive-IP stack non-global zones by
29412748SSowmini.Varadhan@oracle.COM * extracting configuration that has been saved in the kernel and applying
29512748SSowmini.Varadhan@oracle.COM * it at zone boot.
29612748SSowmini.Varadhan@oracle.COM */
29712748SSowmini.Varadhan@oracle.COM static void
ipmgmt_ngz_init()29812748SSowmini.Varadhan@oracle.COM ipmgmt_ngz_init()
29912748SSowmini.Varadhan@oracle.COM {
30012748SSowmini.Varadhan@oracle.COM zoneid_t zoneid;
30112748SSowmini.Varadhan@oracle.COM boolean_t firstboot = B_TRUE, s10c = B_FALSE;
30212748SSowmini.Varadhan@oracle.COM char brand[MAXNAMELEN];
30312748SSowmini.Varadhan@oracle.COM ipadm_status_t ipstatus;
30412748SSowmini.Varadhan@oracle.COM
30512748SSowmini.Varadhan@oracle.COM zoneid = getzoneid();
30612748SSowmini.Varadhan@oracle.COM if (zoneid != GLOBAL_ZONEID) {
30712748SSowmini.Varadhan@oracle.COM
30812748SSowmini.Varadhan@oracle.COM if (zone_getattr(zoneid, ZONE_ATTR_BRAND, brand,
30912748SSowmini.Varadhan@oracle.COM sizeof (brand)) < 0) {
31012748SSowmini.Varadhan@oracle.COM ipmgmt_log(LOG_ERR, "Could not get brand name");
31112748SSowmini.Varadhan@oracle.COM return;
31212748SSowmini.Varadhan@oracle.COM }
31312748SSowmini.Varadhan@oracle.COM /*
31412748SSowmini.Varadhan@oracle.COM * firstboot is always true for S10C zones, where ipadm is not
31512748SSowmini.Varadhan@oracle.COM * available for restoring persistent configuration.
31612748SSowmini.Varadhan@oracle.COM */
31712748SSowmini.Varadhan@oracle.COM if (strcmp(brand, NATIVE_BRAND_NAME) == 0)
318*13125SGirish.Moodalbail@oracle.COM firstboot = ipmgmt_ngz_firstboot_postinstall();
31912748SSowmini.Varadhan@oracle.COM else
32012748SSowmini.Varadhan@oracle.COM s10c = B_TRUE;
32112748SSowmini.Varadhan@oracle.COM
32212748SSowmini.Varadhan@oracle.COM if (!firstboot)
32312748SSowmini.Varadhan@oracle.COM return;
32412748SSowmini.Varadhan@oracle.COM
32512748SSowmini.Varadhan@oracle.COM ipstatus = ipadm_open(&iph, IPH_IPMGMTD);
32612748SSowmini.Varadhan@oracle.COM if (ipstatus != IPADM_SUCCESS) {
32712748SSowmini.Varadhan@oracle.COM ipmgmt_log(LOG_ERR, "could not open ipadm handle",
32812748SSowmini.Varadhan@oracle.COM ipadm_status2str(ipstatus));
32912748SSowmini.Varadhan@oracle.COM return;
33012748SSowmini.Varadhan@oracle.COM }
33112748SSowmini.Varadhan@oracle.COM /*
33212748SSowmini.Varadhan@oracle.COM * Only pass down the callback to persist the interface
33312748SSowmini.Varadhan@oracle.COM * for NATIVE (ipkg) zones.
33412748SSowmini.Varadhan@oracle.COM */
33512748SSowmini.Varadhan@oracle.COM (void) ipadm_init_net_from_gz(iph, NULL,
33612748SSowmini.Varadhan@oracle.COM (s10c ? NULL : ipmgmt_persist_if_cb));
33712748SSowmini.Varadhan@oracle.COM ipadm_close(iph);
33812748SSowmini.Varadhan@oracle.COM }
33912748SSowmini.Varadhan@oracle.COM }
34012748SSowmini.Varadhan@oracle.COM
34112748SSowmini.Varadhan@oracle.COM /*
34212748SSowmini.Varadhan@oracle.COM * Set the uid of this daemon to the "netadm" user. Finish the following
34312016SGirish.Moodalbail@Sun.COM * operations before setuid() because they need root privileges:
34412016SGirish.Moodalbail@Sun.COM *
34512016SGirish.Moodalbail@Sun.COM * - create the /etc/svc/volatile/ipadm directory;
34612748SSowmini.Varadhan@oracle.COM * - change its uid/gid to "netadm"/"netadm";
34712016SGirish.Moodalbail@Sun.COM */
34812016SGirish.Moodalbail@Sun.COM static int
ipmgmt_init_privileges()34912016SGirish.Moodalbail@Sun.COM ipmgmt_init_privileges()
35012016SGirish.Moodalbail@Sun.COM {
35112016SGirish.Moodalbail@Sun.COM struct stat statbuf;
352*13125SGirish.Moodalbail@oracle.COM int err;
35312016SGirish.Moodalbail@Sun.COM
35412016SGirish.Moodalbail@Sun.COM /* create the IPADM_TMPFS_DIR directory */
35512016SGirish.Moodalbail@Sun.COM if (stat(IPADM_TMPFS_DIR, &statbuf) < 0) {
35612016SGirish.Moodalbail@Sun.COM if (mkdir(IPADM_TMPFS_DIR, (mode_t)0755) < 0) {
35712016SGirish.Moodalbail@Sun.COM err = errno;
35812016SGirish.Moodalbail@Sun.COM goto fail;
35912016SGirish.Moodalbail@Sun.COM }
36012016SGirish.Moodalbail@Sun.COM } else {
36112016SGirish.Moodalbail@Sun.COM if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
36212016SGirish.Moodalbail@Sun.COM err = ENOTDIR;
36312016SGirish.Moodalbail@Sun.COM goto fail;
36412016SGirish.Moodalbail@Sun.COM }
36512016SGirish.Moodalbail@Sun.COM }
36612016SGirish.Moodalbail@Sun.COM
36712016SGirish.Moodalbail@Sun.COM if ((chmod(IPADM_TMPFS_DIR, 0755) < 0) ||
36812016SGirish.Moodalbail@Sun.COM (chown(IPADM_TMPFS_DIR, UID_NETADM, GID_NETADM) < 0)) {
36912016SGirish.Moodalbail@Sun.COM err = errno;
37012016SGirish.Moodalbail@Sun.COM goto fail;
37112016SGirish.Moodalbail@Sun.COM }
37212016SGirish.Moodalbail@Sun.COM
37312016SGirish.Moodalbail@Sun.COM /*
37412748SSowmini.Varadhan@oracle.COM * initialize any NGZ specific network information before dropping
37512748SSowmini.Varadhan@oracle.COM * privileges. We need these privileges to plumb IP interfaces handed
37612748SSowmini.Varadhan@oracle.COM * down from the GZ (for dlpi_open() etc.) and also to configure the
37712748SSowmini.Varadhan@oracle.COM * address itself (for any IPI_PRIV ioctls like SLIFADDR)
37812748SSowmini.Varadhan@oracle.COM */
37912748SSowmini.Varadhan@oracle.COM ipmgmt_ngz_init();
38012748SSowmini.Varadhan@oracle.COM
38112748SSowmini.Varadhan@oracle.COM /*
382*13125SGirish.Moodalbail@oracle.COM * Apply all protocol module properties. We need to apply all protocol
383*13125SGirish.Moodalbail@oracle.COM * properties before we drop root privileges.
384*13125SGirish.Moodalbail@oracle.COM */
385*13125SGirish.Moodalbail@oracle.COM ipmgmt_init_prop();
386*13125SGirish.Moodalbail@oracle.COM
387*13125SGirish.Moodalbail@oracle.COM /*
38812016SGirish.Moodalbail@Sun.COM * limit the privileges of this daemon and set the uid of this
38912016SGirish.Moodalbail@Sun.COM * daemon to UID_NETADM
39012016SGirish.Moodalbail@Sun.COM */
39112016SGirish.Moodalbail@Sun.COM if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_NETADM,
39212016SGirish.Moodalbail@Sun.COM GID_NETADM, NULL) == -1) {
39312016SGirish.Moodalbail@Sun.COM err = EPERM;
39412016SGirish.Moodalbail@Sun.COM goto fail;
39512016SGirish.Moodalbail@Sun.COM }
39612016SGirish.Moodalbail@Sun.COM
39712016SGirish.Moodalbail@Sun.COM return (0);
39812016SGirish.Moodalbail@Sun.COM fail:
39912016SGirish.Moodalbail@Sun.COM (void) ipmgmt_log(LOG_ERR, "failed to initialize the daemon: %s",
40012016SGirish.Moodalbail@Sun.COM strerror(err));
40112016SGirish.Moodalbail@Sun.COM return (err);
40212016SGirish.Moodalbail@Sun.COM }
40312016SGirish.Moodalbail@Sun.COM
40412016SGirish.Moodalbail@Sun.COM /*
40512016SGirish.Moodalbail@Sun.COM * Keep the pfds fd open, close other fds.
40612016SGirish.Moodalbail@Sun.COM */
40712016SGirish.Moodalbail@Sun.COM /*ARGSUSED*/
40812016SGirish.Moodalbail@Sun.COM static int
closefunc(void * arg,int fd)40912016SGirish.Moodalbail@Sun.COM closefunc(void *arg, int fd)
41012016SGirish.Moodalbail@Sun.COM {
41112016SGirish.Moodalbail@Sun.COM if (fd != pfds[1])
41212016SGirish.Moodalbail@Sun.COM (void) close(fd);
41312016SGirish.Moodalbail@Sun.COM return (0);
41412016SGirish.Moodalbail@Sun.COM }
41512016SGirish.Moodalbail@Sun.COM
41612016SGirish.Moodalbail@Sun.COM /*
41712016SGirish.Moodalbail@Sun.COM * We cannot use libc's daemon() because the door we create is associated with
41812016SGirish.Moodalbail@Sun.COM * the process ID. If we create the door before the call to daemon(), it will
41912016SGirish.Moodalbail@Sun.COM * be associated with the parent and it's incorrect. On the other hand if we
42012016SGirish.Moodalbail@Sun.COM * create the door later, after the call to daemon(), parent process exits
42112016SGirish.Moodalbail@Sun.COM * early and gives a false notion to SMF that 'ipmgmtd' is up and running,
42212016SGirish.Moodalbail@Sun.COM * which is incorrect. So, we have our own daemon() equivalent.
42312016SGirish.Moodalbail@Sun.COM */
42412016SGirish.Moodalbail@Sun.COM static boolean_t
ipmgmt_daemonize(void)42512016SGirish.Moodalbail@Sun.COM ipmgmt_daemonize(void)
42612016SGirish.Moodalbail@Sun.COM {
42712016SGirish.Moodalbail@Sun.COM pid_t pid;
42812016SGirish.Moodalbail@Sun.COM int rv;
42912016SGirish.Moodalbail@Sun.COM
43012016SGirish.Moodalbail@Sun.COM if (pipe(pfds) < 0) {
43112016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "%s: pipe() failed: %s\n",
43212016SGirish.Moodalbail@Sun.COM progname, strerror(errno));
43312016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE);
43412016SGirish.Moodalbail@Sun.COM }
43512016SGirish.Moodalbail@Sun.COM
43612016SGirish.Moodalbail@Sun.COM if ((pid = fork()) == -1) {
43712016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "%s: fork() failed: %s\n",
43812016SGirish.Moodalbail@Sun.COM progname, strerror(errno));
43912016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE);
44012016SGirish.Moodalbail@Sun.COM } else if (pid > 0) { /* Parent */
44112016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]);
44212016SGirish.Moodalbail@Sun.COM
44312016SGirish.Moodalbail@Sun.COM /*
44412016SGirish.Moodalbail@Sun.COM * Parent should not exit early, it should wait for the child
44512016SGirish.Moodalbail@Sun.COM * to return Success/Failure. If the parent exits early, then
44612016SGirish.Moodalbail@Sun.COM * SMF will think 'ipmgmtd' is up and would start all the
44712016SGirish.Moodalbail@Sun.COM * depended services.
44812016SGirish.Moodalbail@Sun.COM *
44912016SGirish.Moodalbail@Sun.COM * If the child process exits unexpectedly, read() returns -1.
45012016SGirish.Moodalbail@Sun.COM */
45112016SGirish.Moodalbail@Sun.COM if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
45212016SGirish.Moodalbail@Sun.COM (void) kill(pid, SIGKILL);
45312016SGirish.Moodalbail@Sun.COM rv = EXIT_FAILURE;
45412016SGirish.Moodalbail@Sun.COM }
45512016SGirish.Moodalbail@Sun.COM
45612016SGirish.Moodalbail@Sun.COM (void) close(pfds[0]);
45712016SGirish.Moodalbail@Sun.COM exit(rv);
45812016SGirish.Moodalbail@Sun.COM }
45912016SGirish.Moodalbail@Sun.COM
46012016SGirish.Moodalbail@Sun.COM /* Child */
46112016SGirish.Moodalbail@Sun.COM (void) close(pfds[0]);
46212016SGirish.Moodalbail@Sun.COM (void) setsid();
46312016SGirish.Moodalbail@Sun.COM
46412016SGirish.Moodalbail@Sun.COM /* close all files except pfds[1] */
46512016SGirish.Moodalbail@Sun.COM (void) fdwalk(closefunc, NULL);
46612016SGirish.Moodalbail@Sun.COM (void) chdir("/");
46712016SGirish.Moodalbail@Sun.COM openlog(progname, LOG_PID, LOG_DAEMON);
46812016SGirish.Moodalbail@Sun.COM return (B_TRUE);
46912016SGirish.Moodalbail@Sun.COM }
47012016SGirish.Moodalbail@Sun.COM
47112016SGirish.Moodalbail@Sun.COM int
main(int argc,char * argv[])47212016SGirish.Moodalbail@Sun.COM main(int argc, char *argv[])
47312016SGirish.Moodalbail@Sun.COM {
47412016SGirish.Moodalbail@Sun.COM int opt;
47512016SGirish.Moodalbail@Sun.COM boolean_t fg = B_FALSE;
47612016SGirish.Moodalbail@Sun.COM
47712016SGirish.Moodalbail@Sun.COM progname = strrchr(argv[0], '/');
47812016SGirish.Moodalbail@Sun.COM if (progname != NULL)
47912016SGirish.Moodalbail@Sun.COM progname++;
48012016SGirish.Moodalbail@Sun.COM else
48112016SGirish.Moodalbail@Sun.COM progname = argv[0];
48212016SGirish.Moodalbail@Sun.COM
48312016SGirish.Moodalbail@Sun.COM /* Process options */
48412016SGirish.Moodalbail@Sun.COM while ((opt = getopt(argc, argv, "f")) != EOF) {
48512016SGirish.Moodalbail@Sun.COM switch (opt) {
48612016SGirish.Moodalbail@Sun.COM case 'f':
48712016SGirish.Moodalbail@Sun.COM fg = B_TRUE;
48812016SGirish.Moodalbail@Sun.COM break;
48912016SGirish.Moodalbail@Sun.COM default:
49012016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "Usage: %s [-f]\n", progname);
49112016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE);
49212016SGirish.Moodalbail@Sun.COM }
49312016SGirish.Moodalbail@Sun.COM }
49412016SGirish.Moodalbail@Sun.COM
49512016SGirish.Moodalbail@Sun.COM if (!fg && getenv("SMF_FMRI") == NULL) {
49612016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr,
49712016SGirish.Moodalbail@Sun.COM "ipmgmtd is a smf(5) managed service and cannot be run "
49812016SGirish.Moodalbail@Sun.COM "from the command line.\n");
49912016SGirish.Moodalbail@Sun.COM return (EINVAL);
50012016SGirish.Moodalbail@Sun.COM }
50112016SGirish.Moodalbail@Sun.COM
50212016SGirish.Moodalbail@Sun.COM if (!fg && !ipmgmt_daemonize())
50312016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE);
50412016SGirish.Moodalbail@Sun.COM
50512016SGirish.Moodalbail@Sun.COM if (ipmgmt_init_privileges() != 0)
50612016SGirish.Moodalbail@Sun.COM goto child_out;
50712016SGirish.Moodalbail@Sun.COM
50812016SGirish.Moodalbail@Sun.COM if (ipmgmt_init() != 0)
50912016SGirish.Moodalbail@Sun.COM goto child_out;
51012016SGirish.Moodalbail@Sun.COM
51112016SGirish.Moodalbail@Sun.COM /* Inform the parent process that it can successfully exit */
51212016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(EXIT_SUCCESS);
51312016SGirish.Moodalbail@Sun.COM
51412016SGirish.Moodalbail@Sun.COM for (;;)
51512016SGirish.Moodalbail@Sun.COM (void) pause();
51612016SGirish.Moodalbail@Sun.COM
51712016SGirish.Moodalbail@Sun.COM child_out:
51812016SGirish.Moodalbail@Sun.COM /* return from main() forcibly exits an MT process */
51912016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(EXIT_FAILURE);
52012016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE);
52112016SGirish.Moodalbail@Sun.COM }
52212748SSowmini.Varadhan@oracle.COM
52312748SSowmini.Varadhan@oracle.COM /*
52412748SSowmini.Varadhan@oracle.COM * Return TRUE if `ifname' has persistent configuration for the `af' address
52512748SSowmini.Varadhan@oracle.COM * family in the datastore
52612748SSowmini.Varadhan@oracle.COM */
52712748SSowmini.Varadhan@oracle.COM static boolean_t
ipmgmt_persist_if_exists(char * ifname,sa_family_t af)52812748SSowmini.Varadhan@oracle.COM ipmgmt_persist_if_exists(char *ifname, sa_family_t af)
52912748SSowmini.Varadhan@oracle.COM {
53012748SSowmini.Varadhan@oracle.COM ipmgmt_getif_cbarg_t cbarg;
53112748SSowmini.Varadhan@oracle.COM boolean_t exists = B_FALSE;
53212748SSowmini.Varadhan@oracle.COM ipadm_if_info_t *ifp;
53312748SSowmini.Varadhan@oracle.COM
53412748SSowmini.Varadhan@oracle.COM bzero(&cbarg, sizeof (cbarg));
53512748SSowmini.Varadhan@oracle.COM cbarg.cb_ifname = ifname;
53612748SSowmini.Varadhan@oracle.COM (void) ipmgmt_db_walk(ipmgmt_db_getif, &cbarg, IPADM_DB_READ);
53712748SSowmini.Varadhan@oracle.COM if ((ifp = cbarg.cb_ifinfo) != NULL) {
53812748SSowmini.Varadhan@oracle.COM if ((af == AF_INET && (ifp->ifi_pflags & IFIF_IPV4)) ||
53912748SSowmini.Varadhan@oracle.COM (af == AF_INET6 && (ifp->ifi_pflags & IFIF_IPV6))) {
54012748SSowmini.Varadhan@oracle.COM exists = B_TRUE;
54112748SSowmini.Varadhan@oracle.COM }
54212748SSowmini.Varadhan@oracle.COM }
54312748SSowmini.Varadhan@oracle.COM free(ifp);
54412748SSowmini.Varadhan@oracle.COM return (exists);
54512748SSowmini.Varadhan@oracle.COM }
54612748SSowmini.Varadhan@oracle.COM
54712748SSowmini.Varadhan@oracle.COM /*
54812748SSowmini.Varadhan@oracle.COM * Persist any NGZ interfaces assigned to us from the global zone if they do
54912748SSowmini.Varadhan@oracle.COM * not already exist in the persistent db. We need to
55012748SSowmini.Varadhan@oracle.COM * do this before any calls to ipadm_enable_if() can succeed (i.e.,
55112748SSowmini.Varadhan@oracle.COM * before opening up for door_calls), and after setuid to 'netadm' so that
55212748SSowmini.Varadhan@oracle.COM * the persistent db is created with the right permissions.
55312748SSowmini.Varadhan@oracle.COM */
55412748SSowmini.Varadhan@oracle.COM static void
ipmgmt_ngz_persist_if()55512748SSowmini.Varadhan@oracle.COM ipmgmt_ngz_persist_if()
55612748SSowmini.Varadhan@oracle.COM {
55712748SSowmini.Varadhan@oracle.COM ipmgmt_pif_t *pif, *next;
55812748SSowmini.Varadhan@oracle.COM ipmgmt_if_arg_t ifarg;
55912748SSowmini.Varadhan@oracle.COM
56012748SSowmini.Varadhan@oracle.COM for (pif = ngz_pifs; pif != NULL; pif = next) {
56112748SSowmini.Varadhan@oracle.COM next = pif->pif_next;
56212748SSowmini.Varadhan@oracle.COM bzero(&ifarg, sizeof (ifarg));
56312748SSowmini.Varadhan@oracle.COM (void) strlcpy(ifarg.ia_ifname, pif->pif_ifname,
56412748SSowmini.Varadhan@oracle.COM sizeof (ifarg.ia_ifname));
56512748SSowmini.Varadhan@oracle.COM ifarg.ia_flags = IPMGMT_PERSIST;
56612748SSowmini.Varadhan@oracle.COM if (pif->pif_v4 &&
56712748SSowmini.Varadhan@oracle.COM !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET)) {
56812748SSowmini.Varadhan@oracle.COM ifarg.ia_family = AF_INET;
56912748SSowmini.Varadhan@oracle.COM (void) ipmgmt_persist_if(&ifarg);
57012748SSowmini.Varadhan@oracle.COM }
57112748SSowmini.Varadhan@oracle.COM if (pif->pif_v6 &&
57212748SSowmini.Varadhan@oracle.COM !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET6)) {
57312748SSowmini.Varadhan@oracle.COM ifarg.ia_family = AF_INET6;
57412748SSowmini.Varadhan@oracle.COM (void) ipmgmt_persist_if(&ifarg);
57512748SSowmini.Varadhan@oracle.COM }
57612748SSowmini.Varadhan@oracle.COM free(pif);
57712748SSowmini.Varadhan@oracle.COM }
57812748SSowmini.Varadhan@oracle.COM ngz_pifs = NULL; /* no red herrings */
57912748SSowmini.Varadhan@oracle.COM }
580