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.
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.
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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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).
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'.
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.
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.
56 #include <priv_utils.h>
61 #include <sys/param.h>
64 #include "ipmgmt_impl.h"
68 #include <libdllink.h>
69 #include <net/route.h>
70 #include <ipadm_ipmgmt.h>
71 #include <sys/brand.h>
75 /* readers-writers lock for reading/writing daemon data store */
76 pthread_rwlock_t ipmgmt_dbconf_lock
= PTHREAD_RWLOCK_INITIALIZER
;
78 /* tracks address object to {ifname|logical number|interface id} mapping */
79 ipmgmt_aobjmap_list_t aobjmap
;
81 /* used to communicate failure to parent process, which spawned the daemon */
84 /* file descriptor to IPMGMT_DOOR */
85 static int ipmgmt_door_fd
= -1;
87 static void ipmgmt_exit(int);
88 static int ipmgmt_init();
89 static int ipmgmt_init_privileges();
90 static void ipmgmt_ngz_persist_if();
92 static ipadm_handle_t iph
;
93 typedef struct ipmgmt_pif_s
{
94 struct ipmgmt_pif_s
*pif_next
;
95 char pif_ifname
[LIFNAMSIZ
];
100 static ipmgmt_pif_t
*ngz_pifs
;
107 boolean_t upgrade
= B_TRUE
;
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.
116 if ((scferr
= ipmgmt_create_scf_resources(IPMGMTD_FMRI
, &res
)) == 0)
117 upgrade
= ipmgmt_needs_upgrade(&res
);
119 err
= ipmgmt_db_walk(ipmgmt_db_upgrade
, NULL
, IPADM_DB_WRITE
);
121 ipmgmt_log(LOG_ERR
, "could not upgrade the "
122 "ipadm data-store: %s", strerror(err
));
126 * upgrade was success, let's update SCF with the
127 * current data-store version number.
130 ipmgmt_update_dbver(&res
);
134 ipmgmt_release_scf_resources(&res
);
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) {
140 ipmgmt_log(LOG_ERR
, "could not open %s: %s",
141 ADDROBJ_MAPPING_DB_FILE
, strerror(err
));
146 aobjmap
.aobjmap_head
= NULL
;
147 (void) pthread_rwlock_init(&aobjmap
.aobjmap_rwlock
, NULL
);
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.
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 */
163 ipmgmt_ngz_persist_if(); /* create persistent interface info for NGZ */
174 /* create the door file for ipmgmtd */
175 if ((fd
= open(IPMGMT_DOOR
, O_CREAT
|O_RDONLY
, IPADM_FILE_MODE
)) == -1) {
177 ipmgmt_log(LOG_ERR
, "could not open %s: %s",
178 IPMGMT_DOOR
, strerror(err
));
183 if ((ipmgmt_door_fd
= door_create(ipmgmt_handler
, NULL
,
184 DOOR_REFUSE_DESC
| DOOR_NO_CANCEL
)) == -1) {
186 ipmgmt_log(LOG_ERR
, "failed to create door: %s", strerror(err
));
190 * fdetach first in case a previous daemon instance exited
193 (void) fdetach(IPMGMT_DOOR
);
194 if (fattach(ipmgmt_door_fd
, IPMGMT_DOOR
) != 0) {
196 ipmgmt_log(LOG_ERR
, "failed to attach door to %s: %s",
197 IPMGMT_DOOR
, strerror(err
));
202 (void) door_revoke(ipmgmt_door_fd
);
210 if (ipmgmt_door_fd
== -1)
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
));
225 if (signal(SIGTERM
, ipmgmt_exit
) == SIG_ERR
||
226 signal(SIGINT
, ipmgmt_exit
) == SIG_ERR
) {
228 ipmgmt_log(LOG_ERR
, "signal() for SIGTERM/INT failed: %s",
232 if ((err
= ipmgmt_db_init()) != 0 || (err
= ipmgmt_door_init()) != 0)
238 * This is called by the child process to inform the parent process to
239 * exit with the given return value.
242 ipmgmt_inform_parent_exit(int rv
)
244 if (write(pfds
[1], &rv
, sizeof (int)) != sizeof (int)) {
245 ipmgmt_log(LOG_WARNING
,
246 "failed to inform parent process of status: %s",
248 (void) close(pfds
[1]);
251 (void) close(pfds
[1]);
256 ipmgmt_exit(int signo
)
258 (void) close(pfds
[1]);
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.
275 ipmgmt_persist_if_cb(char *ifname
, boolean_t v4
, boolean_t v6
)
279 pif
= calloc(1, sizeof (*pif
));
281 ipmgmt_log(LOG_WARNING
,
282 "Could not allocate memory to configure %s", ifname
);
285 (void) strlcpy(pif
->pif_ifname
, ifname
, sizeof (pif
->pif_ifname
));
288 pif
->pif_next
= ngz_pifs
;
293 * ipmgmt_ngz_init() initializes exclusive-IP stack non-global zones by
294 * extracting configuration that has been saved in the kernel and applying
301 boolean_t firstboot
= B_TRUE
, s10c
= B_FALSE
;
302 char brand
[MAXNAMELEN
];
303 ipadm_status_t ipstatus
;
305 zoneid
= getzoneid();
306 if (zoneid
!= GLOBAL_ZONEID
) {
308 if (zone_getattr(zoneid
, ZONE_ATTR_BRAND
, brand
,
309 sizeof (brand
)) < 0) {
310 ipmgmt_log(LOG_ERR
, "Could not get brand name");
314 * firstboot is always true for S10C zones, where ipadm is not
315 * available for restoring persistent configuration.
317 if (strcmp(brand
, NATIVE_BRAND_NAME
) == 0)
318 firstboot
= ipmgmt_ngz_firstboot_postinstall();
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
));
332 * Only pass down the callback to persist the interface
333 * for NATIVE (ipkg) zones.
335 (void) ipadm_init_net_from_gz(iph
, NULL
,
336 (s10c
? NULL
: ipmgmt_persist_if_cb
));
342 * Set the uid of this daemon to the "netadm" user. Finish the following
343 * operations before setuid() because they need root privileges:
345 * - create the /etc/svc/volatile/ipadm directory;
346 * - change its uid/gid to "netadm"/"netadm";
349 ipmgmt_init_privileges()
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) {
361 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
367 if ((chmod(IPADM_TMPFS_DIR
, 0755) < 0) ||
368 (chown(IPADM_TMPFS_DIR
, UID_NETADM
, GID_NETADM
) < 0)) {
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)
382 * Apply all protocol module properties. We need to apply all protocol
383 * properties before we drop root privileges.
388 * limit the privileges of this daemon and set the uid of this
389 * daemon to UID_NETADM
391 if (__init_daemon_priv(PU_RESETGROUPS
|PU_CLEARLIMITSET
, UID_NETADM
,
392 GID_NETADM
, NULL
) == -1) {
399 (void) ipmgmt_log(LOG_ERR
, "failed to initialize the daemon: %s",
405 * Keep the pfds fd open, close other fds.
409 closefunc(void *arg
, int fd
)
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.
425 ipmgmt_daemonize(void)
430 if (pipe(pfds
) < 0) {
431 (void) fprintf(stderr
, "%s: pipe() failed: %s\n",
432 progname
, strerror(errno
));
436 if ((pid
= fork()) == -1) {
437 (void) fprintf(stderr
, "%s: fork() failed: %s\n",
438 progname
, strerror(errno
));
440 } else if (pid
> 0) { /* Parent */
441 (void) close(pfds
[1]);
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
449 * If the child process exits unexpectedly, read() returns -1.
451 if (read(pfds
[0], &rv
, sizeof (int)) != sizeof (int)) {
452 (void) kill(pid
, SIGKILL
);
456 (void) close(pfds
[0]);
461 (void) close(pfds
[0]);
464 /* close all files except pfds[1] */
465 (void) fdwalk(closefunc
, NULL
);
467 openlog(progname
, LOG_PID
, LOG_DAEMON
);
472 main(int argc
, char *argv
[])
475 boolean_t fg
= B_FALSE
;
477 progname
= strrchr(argv
[0], '/');
478 if (progname
!= NULL
)
483 /* Process options */
484 while ((opt
= getopt(argc
, argv
, "f")) != EOF
) {
490 (void) fprintf(stderr
, "Usage: %s [-f]\n", progname
);
491 return (EXIT_FAILURE
);
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");
502 if (!fg
&& !ipmgmt_daemonize())
503 return (EXIT_FAILURE
);
505 if (ipmgmt_init_privileges() != 0)
508 if (ipmgmt_init() != 0)
511 /* Inform the parent process that it can successfully exit */
512 ipmgmt_inform_parent_exit(EXIT_SUCCESS
);
518 /* return from main() forcibly exits an MT process */
519 ipmgmt_inform_parent_exit(EXIT_FAILURE
);
520 return (EXIT_FAILURE
);
524 * Return TRUE if `ifname' has persistent configuration for the `af' address
525 * family in the datastore
528 ipmgmt_persist_if_exists(char *ifname
, sa_family_t af
)
530 ipmgmt_getif_cbarg_t cbarg
;
531 boolean_t exists
= B_FALSE
;
532 ipadm_if_info_t
*ifp
;
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
))) {
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.
555 ipmgmt_ngz_persist_if()
557 ipmgmt_pif_t
*pif
, *next
;
558 ipmgmt_if_arg_t ifarg
;
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
;
567 !ipmgmt_persist_if_exists(pif
->pif_ifname
, AF_INET
)) {
568 ifarg
.ia_family
= AF_INET
;
569 (void) ipmgmt_persist_if(&ifarg
);
572 !ipmgmt_persist_if_exists(pif
->pif_ifname
, AF_INET6
)) {
573 ifarg
.ia_family
= AF_INET6
;
574 (void) ipmgmt_persist_if(&ifarg
);
578 ngz_pifs
= NULL
; /* no red herrings */