xref: /titanic_41/usr/src/cmd/cmd-inet/lib/ipmgmtd/ipmgmt_main.c (revision 7484d118eeb19c6c9d97e3efb0d63d59a9c4153b)
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;
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_init();
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;
107 
108 	/* creates the address object data store, if it doesn't exist */
109 	if ((fd = open(ADDROBJ_MAPPING_DB_FILE, O_CREAT|O_RDONLY,
110 	    IPADM_FILE_MODE)) == -1) {
111 		err = errno;
112 		ipmgmt_log(LOG_ERR, "could not open %s: %s",
113 		    ADDROBJ_MAPPING_DB_FILE, strerror(err));
114 		return (err);
115 	}
116 	(void) close(fd);
117 
118 	aobjmap.aobjmap_head = NULL;
119 	(void) pthread_rwlock_init(&aobjmap.aobjmap_rwlock, NULL);
120 
121 	/*
122 	 * If the daemon is recovering from a crash or restart, read the
123 	 * address object to logical interface mapping and build an in-memory
124 	 * representation of the mapping. That is, build `aobjmap' structure
125 	 * from address object data store.
126 	 */
127 	if ((err = ipadm_rw_db(ipmgmt_aobjmap_init, NULL,
128 	    ADDROBJ_MAPPING_DB_FILE, 0, IPADM_DB_READ)) != 0) {
129 		/* if there was nothing to initialize, it's fine */
130 		if (err != ENOENT)
131 			return (err);
132 		err = 0;
133 	}
134 
135 	(void) pthread_rwlock_init(&ipmgmt_dbconf_lock, NULL);
136 
137 	ipmgmt_ngz_persist_if(); /* create persistent interface info for NGZ */
138 
139 	return (err);
140 }
141 
142 static int
143 ipmgmt_door_init()
144 {
145 	int fd;
146 	int err;
147 
148 	/* create the door file for ipmgmtd */
149 	if ((fd = open(IPMGMT_DOOR, O_CREAT|O_RDONLY, IPADM_FILE_MODE)) == -1) {
150 		err = errno;
151 		ipmgmt_log(LOG_ERR, "could not open %s: %s",
152 		    IPMGMT_DOOR, strerror(err));
153 		return (err);
154 	}
155 	(void) close(fd);
156 
157 	if ((ipmgmt_door_fd = door_create(ipmgmt_handler, NULL,
158 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
159 		err = errno;
160 		ipmgmt_log(LOG_ERR, "failed to create door: %s", strerror(err));
161 		return (err);
162 	}
163 	/*
164 	 * fdetach first in case a previous daemon instance exited
165 	 * ungracefully.
166 	 */
167 	(void) fdetach(IPMGMT_DOOR);
168 	if (fattach(ipmgmt_door_fd, IPMGMT_DOOR) != 0) {
169 		err = errno;
170 		ipmgmt_log(LOG_ERR, "failed to attach door to %s: %s",
171 		    IPMGMT_DOOR, strerror(err));
172 		goto fail;
173 	}
174 	return (0);
175 fail:
176 	(void) door_revoke(ipmgmt_door_fd);
177 	ipmgmt_door_fd = -1;
178 	return (err);
179 }
180 
181 static void
182 ipmgmt_door_fini()
183 {
184 	if (ipmgmt_door_fd == -1)
185 		return;
186 
187 	(void) fdetach(IPMGMT_DOOR);
188 	if (door_revoke(ipmgmt_door_fd) == -1) {
189 		ipmgmt_log(LOG_ERR, "failed to revoke access to door %s: %s",
190 		    IPMGMT_DOOR, strerror(errno));
191 	}
192 }
193 
194 static int
195 ipmgmt_init()
196 {
197 	int err;
198 
199 	if (signal(SIGTERM, ipmgmt_exit) == SIG_ERR ||
200 	    signal(SIGINT, ipmgmt_exit) == SIG_ERR) {
201 		err = errno;
202 		ipmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
203 		    strerror(err));
204 		return (err);
205 	}
206 	if ((err = ipmgmt_db_init()) != 0 || (err = ipmgmt_door_init()) != 0)
207 		return (err);
208 	return (0);
209 }
210 
211 /*
212  * This is called by the child process to inform the parent process to
213  * exit with the given return value.
214  */
215 static void
216 ipmgmt_inform_parent_exit(int rv)
217 {
218 	if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
219 		ipmgmt_log(LOG_WARNING,
220 		    "failed to inform parent process of status: %s",
221 		    strerror(errno));
222 		(void) close(pfds[1]);
223 		exit(EXIT_FAILURE);
224 	}
225 	(void) close(pfds[1]);
226 }
227 
228 /*ARGSUSED*/
229 static void
230 ipmgmt_exit(int signo)
231 {
232 	(void) close(pfds[1]);
233 	ipmgmt_door_fini();
234 	exit(EXIT_FAILURE);
235 }
236 
237 /*
238  * On the first reboot after installation of an ipkg zone,
239  * ipmgmt_persist_if_cb() is used in non-global zones to track the interfaces
240  * that have IP address configuration assignments from the global zone.
241  * Persistent configuration for the interfaces is created on the first boot
242  * by ipmgmtd, and the addresses assigned to the interfaces by the GZ
243  * will be subsequently configured when the interface is enabled.
244  * Note that ipmgmt_persist_if_cb() only sets up a list of interfaces
245  * that need to be persisted- the actual update of the ipadm data-store happens
246  * in ipmgmt_persist_if() after the appropriate privs/uid state has been set up.
247  */
248 static void
249 ipmgmt_persist_if_cb(char *ifname, boolean_t v4, boolean_t v6)
250 {
251 	ipmgmt_pif_t *pif;
252 
253 	pif = calloc(1, sizeof (*pif));
254 	if (pif == NULL) {
255 		ipmgmt_log(LOG_WARNING,
256 		    "Could not allocate memory to configure %s", ifname);
257 		return;
258 	}
259 	(void) strlcpy(pif->pif_ifname, ifname, sizeof (pif->pif_ifname));
260 	pif->pif_v4 = v4;
261 	pif->pif_v6 = v6;
262 	pif->pif_next = ngz_pifs;
263 	ngz_pifs = pif;
264 }
265 
266 /*
267  * ipmgmt_ngz_init() initializes exclusive-IP stack non-global zones by
268  * extracting configuration that has been saved in the kernel and applying
269  * it at zone boot.
270  */
271 static void
272 ipmgmt_ngz_init()
273 {
274 	zoneid_t zoneid;
275 	boolean_t firstboot = B_TRUE, s10c = B_FALSE;
276 	char brand[MAXNAMELEN];
277 	ipadm_status_t ipstatus;
278 
279 	zoneid = getzoneid();
280 	if (zoneid != GLOBAL_ZONEID) {
281 
282 		if (zone_getattr(zoneid, ZONE_ATTR_BRAND, brand,
283 		    sizeof (brand)) < 0) {
284 			ipmgmt_log(LOG_ERR, "Could not get brand name");
285 			return;
286 		}
287 		/*
288 		 * firstboot is always true for S10C zones, where ipadm is not
289 		 * available for restoring persistent configuration.
290 		 */
291 		if (strcmp(brand, NATIVE_BRAND_NAME) == 0)
292 			firstboot = ipmgmt_first_boot();
293 		else
294 			s10c = B_TRUE;
295 
296 		if (!firstboot)
297 			return;
298 
299 		ipstatus = ipadm_open(&iph, IPH_IPMGMTD);
300 		if (ipstatus != IPADM_SUCCESS) {
301 			ipmgmt_log(LOG_ERR, "could not open ipadm handle",
302 			    ipadm_status2str(ipstatus));
303 			return;
304 		}
305 		/*
306 		 * Only pass down the callback to persist the interface
307 		 * for NATIVE (ipkg) zones.
308 		 */
309 		(void) ipadm_init_net_from_gz(iph, NULL,
310 		    (s10c ? NULL : ipmgmt_persist_if_cb));
311 		ipadm_close(iph);
312 	}
313 }
314 
315 /*
316  * Set the uid of this daemon to the "netadm" user. Finish the following
317  * operations before setuid() because they need root privileges:
318  *
319  *    - create the /etc/svc/volatile/ipadm directory;
320  *    - change its uid/gid to "netadm"/"netadm";
321  */
322 static int
323 ipmgmt_init_privileges()
324 {
325 	struct stat	statbuf;
326 	int err;
327 
328 	/* create the IPADM_TMPFS_DIR directory */
329 	if (stat(IPADM_TMPFS_DIR, &statbuf) < 0) {
330 		if (mkdir(IPADM_TMPFS_DIR, (mode_t)0755) < 0) {
331 			err = errno;
332 			goto fail;
333 		}
334 	} else {
335 		if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
336 			err = ENOTDIR;
337 			goto fail;
338 		}
339 	}
340 
341 	if ((chmod(IPADM_TMPFS_DIR, 0755) < 0) ||
342 	    (chown(IPADM_TMPFS_DIR, UID_NETADM, GID_NETADM) < 0)) {
343 		err = errno;
344 		goto fail;
345 	}
346 
347 	/*
348 	 * initialize any NGZ specific network information before dropping
349 	 * privileges. We need these privileges to plumb IP interfaces handed
350 	 * down from the GZ (for dlpi_open() etc.) and also to configure the
351 	 * address itself (for any IPI_PRIV ioctls like SLIFADDR)
352 	 */
353 	ipmgmt_ngz_init();
354 
355 	/*
356 	 * limit the privileges of this daemon and set the uid of this
357 	 * daemon to UID_NETADM
358 	 */
359 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_NETADM,
360 	    GID_NETADM, NULL) == -1) {
361 		err = EPERM;
362 		goto fail;
363 	}
364 
365 	return (0);
366 fail:
367 	(void) ipmgmt_log(LOG_ERR, "failed to initialize the daemon: %s",
368 	    strerror(err));
369 	return (err);
370 }
371 
372 /*
373  * Keep the pfds fd open, close other fds.
374  */
375 /*ARGSUSED*/
376 static int
377 closefunc(void *arg, int fd)
378 {
379 	if (fd != pfds[1])
380 		(void) close(fd);
381 	return (0);
382 }
383 
384 /*
385  * We cannot use libc's daemon() because the door we create is associated with
386  * the process ID. If we create the door before the call to daemon(), it will
387  * be associated with the parent and it's incorrect. On the other hand if we
388  * create the door later, after the call to daemon(), parent process exits
389  * early and gives a false notion to SMF that 'ipmgmtd' is up and running,
390  * which is incorrect. So, we have our own daemon() equivalent.
391  */
392 static boolean_t
393 ipmgmt_daemonize(void)
394 {
395 	pid_t pid;
396 	int rv;
397 
398 	if (pipe(pfds) < 0) {
399 		(void) fprintf(stderr, "%s: pipe() failed: %s\n",
400 		    progname, strerror(errno));
401 		exit(EXIT_FAILURE);
402 	}
403 
404 	if ((pid = fork()) == -1) {
405 		(void) fprintf(stderr, "%s: fork() failed: %s\n",
406 		    progname, strerror(errno));
407 		exit(EXIT_FAILURE);
408 	} else if (pid > 0) { /* Parent */
409 		(void) close(pfds[1]);
410 
411 		/*
412 		 * Parent should not exit early, it should wait for the child
413 		 * to return Success/Failure. If the parent exits early, then
414 		 * SMF will think 'ipmgmtd' is up and would start all the
415 		 * depended services.
416 		 *
417 		 * If the child process exits unexpectedly, read() returns -1.
418 		 */
419 		if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
420 			(void) kill(pid, SIGKILL);
421 			rv = EXIT_FAILURE;
422 		}
423 
424 		(void) close(pfds[0]);
425 		exit(rv);
426 	}
427 
428 	/* Child */
429 	(void) close(pfds[0]);
430 	(void) setsid();
431 
432 	/* close all files except pfds[1] */
433 	(void) fdwalk(closefunc, NULL);
434 	(void) chdir("/");
435 	openlog(progname, LOG_PID, LOG_DAEMON);
436 	return (B_TRUE);
437 }
438 
439 int
440 main(int argc, char *argv[])
441 {
442 	int opt;
443 	boolean_t fg = B_FALSE;
444 
445 	progname = strrchr(argv[0], '/');
446 	if (progname != NULL)
447 		progname++;
448 	else
449 		progname = argv[0];
450 
451 	/* Process options */
452 	while ((opt = getopt(argc, argv, "f")) != EOF) {
453 		switch (opt) {
454 		case 'f':
455 			fg = B_TRUE;
456 			break;
457 		default:
458 			(void) fprintf(stderr, "Usage: %s [-f]\n", progname);
459 			return (EXIT_FAILURE);
460 		}
461 	}
462 
463 	if (!fg && getenv("SMF_FMRI") == NULL) {
464 		(void) fprintf(stderr,
465 		    "ipmgmtd is a smf(5) managed service and cannot be run "
466 		    "from the command line.\n");
467 		return (EINVAL);
468 	}
469 
470 	if (!fg && !ipmgmt_daemonize())
471 		return (EXIT_FAILURE);
472 
473 	if (ipmgmt_init_privileges() != 0)
474 		goto child_out;
475 
476 	if (ipmgmt_init() != 0)
477 		goto child_out;
478 
479 	/* Inform the parent process that it can successfully exit */
480 	ipmgmt_inform_parent_exit(EXIT_SUCCESS);
481 
482 	for (;;)
483 		(void) pause();
484 
485 child_out:
486 	/* return from main() forcibly exits an MT process */
487 	ipmgmt_inform_parent_exit(EXIT_FAILURE);
488 	return (EXIT_FAILURE);
489 }
490 
491 /*
492  * Return TRUE if `ifname' has persistent configuration for the `af' address
493  * family in the datastore
494  */
495 static boolean_t
496 ipmgmt_persist_if_exists(char *ifname, sa_family_t af)
497 {
498 	ipmgmt_getif_cbarg_t cbarg;
499 	boolean_t exists = B_FALSE;
500 	ipadm_if_info_t *ifp;
501 
502 	bzero(&cbarg, sizeof (cbarg));
503 	cbarg.cb_ifname = ifname;
504 	(void) ipmgmt_db_walk(ipmgmt_db_getif, &cbarg, IPADM_DB_READ);
505 	if ((ifp = cbarg.cb_ifinfo) != NULL) {
506 		if ((af == AF_INET && (ifp->ifi_pflags & IFIF_IPV4)) ||
507 		    (af == AF_INET6 && (ifp->ifi_pflags & IFIF_IPV6))) {
508 			exists = B_TRUE;
509 		}
510 	}
511 	free(ifp);
512 	return (exists);
513 }
514 
515 /*
516  * Persist any NGZ interfaces assigned to us from the global zone if they do
517  * not already exist in the persistent db. We need to
518  * do this before any calls to ipadm_enable_if() can succeed (i.e.,
519  * before opening up for door_calls), and after setuid to 'netadm' so that
520  * the persistent db is created with the right permissions.
521  */
522 static void
523 ipmgmt_ngz_persist_if()
524 {
525 	ipmgmt_pif_t *pif, *next;
526 	ipmgmt_if_arg_t ifarg;
527 
528 	for (pif = ngz_pifs; pif != NULL; pif = next) {
529 		next = pif->pif_next;
530 		bzero(&ifarg, sizeof (ifarg));
531 		(void) strlcpy(ifarg.ia_ifname, pif->pif_ifname,
532 		    sizeof (ifarg.ia_ifname));
533 		ifarg.ia_flags = IPMGMT_PERSIST;
534 		if (pif->pif_v4 &&
535 		    !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET)) {
536 			ifarg.ia_family = AF_INET;
537 			(void) ipmgmt_persist_if(&ifarg);
538 		}
539 		if (pif->pif_v6 &&
540 		    !ipmgmt_persist_if_exists(pif->pif_ifname, AF_INET6)) {
541 			ifarg.ia_family = AF_INET6;
542 			(void) ipmgmt_persist_if(&ifarg);
543 		}
544 		free(pif);
545 	}
546 	ngz_pifs = NULL; /* no red herrings */
547 }
548