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