xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 #include "defs.h"
26 #include "tables.h"
27 #include <fcntl.h>
28 #include <sys/un.h>
29 
30 static void	initlog(void);
31 static void	run_timeouts(void);
32 
33 static void	advertise(struct sockaddr_in6 *sin6, struct phyint *pi,
34 		    boolean_t no_prefixes);
35 static void	solicit(struct sockaddr_in6 *sin6, struct phyint *pi);
36 static void	initifs(boolean_t first);
37 static void	check_if_removed(struct phyint *pi);
38 static void	loopback_ra_enqueue(struct phyint *pi,
39 		    struct nd_router_advert *ra, int len);
40 static void	loopback_ra_dequeue(void);
41 static void	check_daemonize(void);
42 
43 struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0,
44 				    0x0, 0x0, 0x0, 0x0,
45 				    0x0, 0x0, 0x0, 0x0,
46 				    0x0, 0x0, 0x0, 0x1 } };
47 
48 struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0,
49 				    0x0, 0x0, 0x0, 0x0,
50 				    0x0, 0x0, 0x0, 0x0,
51 				    0x0, 0x0, 0x0, 0x2 } };
52 
53 static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0,
54 				    { 0xff, 0x2, 0x0, 0x0,
55 				    0x0, 0x0, 0x0, 0x0,
56 				    0x0, 0x0, 0x0, 0x0,
57 				    0x0, 0x0, 0x0, 0x1 } };
58 
59 static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0,
60 				    { 0xff, 0x2, 0x0, 0x0,
61 				    0x0, 0x0, 0x0, 0x0,
62 				    0x0, 0x0, 0x0, 0x0,
63 				    0x0, 0x0, 0x0, 0x2 } };
64 
65 static char **argv0;		/* Saved for re-exec on SIGHUP */
66 
67 static uint64_t packet[(IP_MAXPACKET + 1)/8];
68 
69 static int	show_ifs = 0;
70 static boolean_t	already_daemonized = _B_FALSE;
71 int		debug = 0;
72 int		no_loopback = 0; /* Do not send RA packets to ourselves */
73 
74 /*
75  * Size of routing socket message used by in.ndpd which includes the header,
76  * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6)
77  * plus space for the RTA_IFP (a sockaddr_dl).
78  */
79 #define	NDP_RTM_MSGLEN	sizeof (struct rt_msghdr) +	\
80 			sizeof (struct sockaddr_in6) +	\
81 			sizeof (struct sockaddr_in6) +	\
82 			sizeof (struct sockaddr_in6) +	\
83 			sizeof (struct sockaddr_dl)
84 
85 /*
86  * These are referenced externally in tables.c in order to fill in the
87  * dynamic portions of the routing socket message and then to send the message
88  * itself.
89  */
90 int	rtsock = -1;			/* Routing socket */
91 struct	rt_msghdr	*rt_msg;	/* Routing socket message */
92 struct	sockaddr_in6	*rta_gateway;	/* RTA_GATEWAY sockaddr */
93 struct	sockaddr_dl	*rta_ifp;	/* RTA_IFP sockaddr */
94 int	mibsock = -1;			/* mib request socket */
95 
96 /*
97  * Return the current time in milliseconds truncated to
98  * fit in an integer.
99  */
100 uint_t
101 getcurrenttime(void)
102 {
103 	struct timeval tp;
104 
105 	if (gettimeofday(&tp, NULL) < 0) {
106 		logperror("getcurrenttime: gettimeofday failed");
107 		exit(1);
108 	}
109 	return (tp.tv_sec * 1000 + tp.tv_usec / 1000);
110 }
111 
112 /*
113  * Output a preformated packet from the packet[] buffer.
114  */
115 static void
116 sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags)
117 {
118 	int cc;
119 	char abuf[INET6_ADDRSTRLEN];
120 
121 	cc = sendto(sock, (char *)packet, size, flags,
122 	    (struct sockaddr *)sin6, sizeof (*sin6));
123 	if (cc < 0 || cc != size) {
124 		if (cc < 0) {
125 			logperror("sendpacket: sendto");
126 		}
127 		logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n",
128 		    inet_ntop(sin6->sin6_family,
129 		    (void *)&sin6->sin6_addr,
130 		    abuf, sizeof (abuf)),
131 		    size, cc);
132 	}
133 }
134 
135 /*
136  * If possible, place an ND_OPT_SOURCE_LINKADDR option at `optp'.
137  * Return the number of bytes placed in the option.
138  */
139 static uint_t
140 add_opt_lla(struct phyint *pi, struct nd_opt_lla *optp)
141 {
142 	uint_t optlen;
143 	uint_t hwaddrlen;
144 	struct lifreq lifr;
145 
146 	/* If this phyint doesn't have a link-layer address, bail */
147 	if (phyint_get_lla(pi, &lifr) == -1)
148 		return (0);
149 
150 	hwaddrlen = lifr.lifr_nd.lnr_hdw_len;
151 	/* roundup to multiple of 8 and make padding zero */
152 	optlen = ((sizeof (struct nd_opt_hdr) + hwaddrlen + 7) / 8) * 8;
153 	bzero(optp, optlen);
154 	optp->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR;
155 	optp->nd_opt_lla_len = optlen / 8;
156 	bcopy(lifr.lifr_nd.lnr_hdw_addr, optp->nd_opt_lla_hdw_addr, hwaddrlen);
157 
158 	return (optlen);
159 }
160 
161 /* Send a Router Solicitation */
162 static void
163 solicit(struct sockaddr_in6 *sin6, struct phyint *pi)
164 {
165 	int packetlen = 0;
166 	struct	nd_router_solicit *rs = (struct nd_router_solicit *)packet;
167 	char *pptr = (char *)packet;
168 
169 	rs->nd_rs_type = ND_ROUTER_SOLICIT;
170 	rs->nd_rs_code = 0;
171 	rs->nd_rs_cksum = htons(0);
172 	rs->nd_rs_reserved = htonl(0);
173 
174 	packetlen += sizeof (*rs);
175 	pptr += sizeof (*rs);
176 
177 	/* add options */
178 	packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
179 
180 	if (debug & D_PKTOUT) {
181 		print_route_sol("Sending solicitation to ", pi, rs, packetlen,
182 		    sin6);
183 	}
184 	sendpacket(sin6, pi->pi_sock, packetlen, 0);
185 }
186 
187 /*
188  * Send a (set of) Router Advertisements and feed them back to ourselves
189  * for processing. Unless no_prefixes is set all prefixes are included.
190  * If there are too many prefix options to fit in one packet multiple
191  * packets will be sent - each containing a subset of the prefix options.
192  */
193 static void
194 advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes)
195 {
196 	struct	nd_opt_prefix_info *po;
197 	char *pptr = (char *)packet;
198 	struct nd_router_advert *ra;
199 	struct adv_prefix *adv_pr;
200 	int packetlen = 0;
201 
202 	ra = (struct nd_router_advert *)pptr;
203 	ra->nd_ra_type = ND_ROUTER_ADVERT;
204 	ra->nd_ra_code = 0;
205 	ra->nd_ra_cksum = htons(0);
206 	ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit;
207 	ra->nd_ra_flags_reserved = 0;
208 	if (pi->pi_AdvManagedFlag)
209 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
210 	if (pi->pi_AdvOtherConfigFlag)
211 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
212 
213 	if (pi->pi_adv_state == FINAL_ADV)
214 		ra->nd_ra_router_lifetime = htons(0);
215 	else
216 		ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime);
217 	ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime);
218 	ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer);
219 
220 	packetlen = sizeof (*ra);
221 	pptr += sizeof (*ra);
222 
223 	if (pi->pi_adv_state == FINAL_ADV) {
224 		if (debug & D_PKTOUT) {
225 			print_route_adv("Sending advert (FINAL) to ", pi,
226 			    ra, packetlen, sin6);
227 		}
228 		sendpacket(sin6, pi->pi_sock, packetlen, 0);
229 		/* Feed packet back in for router operation */
230 		loopback_ra_enqueue(pi, ra, packetlen);
231 		return;
232 	}
233 
234 	/* add options */
235 	packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
236 	pptr = (char *)packet + packetlen;
237 
238 	if (pi->pi_AdvLinkMTU != 0) {
239 		struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr;
240 
241 		mo->nd_opt_mtu_type = ND_OPT_MTU;
242 		mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8;
243 		mo->nd_opt_mtu_reserved = 0;
244 		mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU);
245 
246 		packetlen += sizeof (struct nd_opt_mtu);
247 		pptr += sizeof (struct nd_opt_mtu);
248 	}
249 
250 	if (no_prefixes) {
251 		if (debug & D_PKTOUT) {
252 			print_route_adv("Sending advert to ", pi,
253 			    ra, packetlen, sin6);
254 		}
255 		sendpacket(sin6, pi->pi_sock, packetlen, 0);
256 		/* Feed packet back in for router operation */
257 		loopback_ra_enqueue(pi, ra, packetlen);
258 		return;
259 	}
260 
261 	po = (struct nd_opt_prefix_info *)pptr;
262 	for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
263 	    adv_pr = adv_pr->adv_pr_next) {
264 		if (!adv_pr->adv_pr_AdvOnLinkFlag &&
265 		    !adv_pr->adv_pr_AdvAutonomousFlag) {
266 			continue;
267 		}
268 
269 		/*
270 		 * If the prefix doesn't fit in packet send
271 		 * what we have so far and start with new packet.
272 		 */
273 		if (packetlen + sizeof (*po) >
274 		    pi->pi_LinkMTU - sizeof (struct ip6_hdr)) {
275 			if (debug & D_PKTOUT) {
276 				print_route_adv("Sending advert "
277 				    "(FRAG) to ",
278 				    pi, ra, packetlen, sin6);
279 			}
280 			sendpacket(sin6, pi->pi_sock, packetlen, 0);
281 			/* Feed packet back in for router operation */
282 			loopback_ra_enqueue(pi, ra, packetlen);
283 			packetlen = sizeof (*ra);
284 			pptr = (char *)packet + sizeof (*ra);
285 			po = (struct nd_opt_prefix_info *)pptr;
286 		}
287 		po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
288 		po->nd_opt_pi_len = sizeof (*po)/8;
289 		po->nd_opt_pi_flags_reserved = 0;
290 		if (adv_pr->adv_pr_AdvOnLinkFlag) {
291 			po->nd_opt_pi_flags_reserved |=
292 			    ND_OPT_PI_FLAG_ONLINK;
293 		}
294 		if (adv_pr->adv_pr_AdvAutonomousFlag) {
295 			po->nd_opt_pi_flags_reserved |=
296 			    ND_OPT_PI_FLAG_AUTO;
297 		}
298 		po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len;
299 		/*
300 		 * If both Adv*Expiration and Adv*Lifetime are
301 		 * set we prefer the former and make the lifetime
302 		 * decrement in real time.
303 		 */
304 		if (adv_pr->adv_pr_AdvValidRealTime) {
305 			po->nd_opt_pi_valid_time =
306 			    htonl(adv_pr->adv_pr_AdvValidExpiration);
307 		} else {
308 			po->nd_opt_pi_valid_time =
309 			    htonl(adv_pr->adv_pr_AdvValidLifetime);
310 		}
311 		if (adv_pr->adv_pr_AdvPreferredRealTime) {
312 			po->nd_opt_pi_preferred_time =
313 			    htonl(adv_pr->adv_pr_AdvPreferredExpiration);
314 		} else {
315 			po->nd_opt_pi_preferred_time =
316 			    htonl(adv_pr->adv_pr_AdvPreferredLifetime);
317 		}
318 		po->nd_opt_pi_reserved2 = htonl(0);
319 		po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix;
320 
321 		po++;
322 		packetlen += sizeof (*po);
323 	}
324 	if (debug & D_PKTOUT) {
325 		print_route_adv("Sending advert to ", pi,
326 		    ra, packetlen, sin6);
327 	}
328 	sendpacket(sin6, pi->pi_sock, packetlen, 0);
329 	/* Feed packet back in for router operation */
330 	loopback_ra_enqueue(pi, ra, packetlen);
331 }
332 
333 /* Poll support */
334 static int		pollfd_num = 0;	/* Allocated and initialized */
335 static struct pollfd	*pollfds = NULL;
336 
337 /*
338  * Add fd to the set being polled. Returns 0 if ok; -1 if failed.
339  */
340 int
341 poll_add(int fd)
342 {
343 	int i;
344 	int new_num;
345 	struct pollfd *newfds;
346 
347 	/* Check if already present */
348 	for (i = 0; i < pollfd_num; i++) {
349 		if (pollfds[i].fd == fd)
350 			return (0);
351 	}
352 	/* Check for empty spot already present */
353 	for (i = 0; i < pollfd_num; i++) {
354 		if (pollfds[i].fd == -1) {
355 			pollfds[i].fd = fd;
356 			return (0);
357 		}
358 	}
359 
360 	/* Allocate space for 32 more fds and initialize to -1 */
361 	new_num = pollfd_num + 32;
362 	newfds = realloc(pollfds, new_num * sizeof (struct pollfd));
363 	if (newfds == NULL) {
364 		logperror("poll_add: realloc");
365 		return (-1);
366 	}
367 
368 	newfds[pollfd_num].fd = fd;
369 	newfds[pollfd_num++].events = POLLIN;
370 
371 	for (i = pollfd_num; i < new_num; i++) {
372 		newfds[i].fd = -1;
373 		newfds[i].events = POLLIN;
374 	}
375 	pollfd_num = new_num;
376 	pollfds = newfds;
377 	return (0);
378 }
379 
380 /*
381  * Remove fd from the set being polled. Returns 0 if ok; -1 if failed.
382  */
383 int
384 poll_remove(int fd)
385 {
386 	int i;
387 
388 	/* Check if already present */
389 	for (i = 0; i < pollfd_num; i++) {
390 		if (pollfds[i].fd == fd) {
391 			pollfds[i].fd = -1;
392 			return (0);
393 		}
394 	}
395 	return (-1);
396 }
397 
398 /*
399  * Extract information about the ifname (either a physical interface and
400  * the ":0" logical interface or just a logical interface).
401  * If the interface (still) exists in kernel set pr_in_use
402  * for caller to be able to detect interfaces that are removed.
403  * Starts sending advertisements/solicitations when new physical interfaces
404  * are detected.
405  */
406 static void
407 if_process(int s, char *ifname, boolean_t first)
408 {
409 	struct lifreq lifr;
410 	struct phyint *pi;
411 	struct prefix *pr;
412 	char *cp;
413 	char phyintname[LIFNAMSIZ + 1];
414 
415 	if (debug & D_IFSCAN)
416 		logmsg(LOG_DEBUG, "if_process(%s)\n", ifname);
417 
418 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
419 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
420 	if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
421 		if (errno == ENXIO) {
422 			/*
423 			 * Interface has disappeared
424 			 */
425 			return;
426 		}
427 		logperror("if_process: ioctl (get interface flags)");
428 		return;
429 	}
430 
431 	/*
432 	 * Ignore loopback and point-to-multipoint interfaces.
433 	 * Point-to-point interfaces always have IFF_MULTICAST set.
434 	 */
435 	if (!(lifr.lifr_flags & IFF_MULTICAST) ||
436 	    (lifr.lifr_flags & IFF_LOOPBACK)) {
437 		return;
438 	}
439 
440 	if (!(lifr.lifr_flags & IFF_IPV6))
441 		return;
442 
443 	(void) strncpy(phyintname, ifname, sizeof (phyintname));
444 	phyintname[sizeof (phyintname) - 1] = '\0';
445 	if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) {
446 		*cp = '\0';
447 	}
448 
449 	pi = phyint_lookup(phyintname);
450 	if (pi == NULL) {
451 		/*
452 		 * Do not add anything for new interfaces until they are UP.
453 		 * For existing interfaces we track the up flag.
454 		 */
455 		if (!(lifr.lifr_flags & IFF_UP))
456 			return;
457 
458 		pi = phyint_create(phyintname);
459 		if (pi == NULL) {
460 			logmsg(LOG_ERR, "if_process: out of memory\n");
461 			return;
462 		}
463 	}
464 	(void) phyint_init_from_k(pi);
465 	if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) {
466 		/* Interface is not yet present */
467 		if (debug & D_PHYINT) {
468 			logmsg(LOG_DEBUG, "if_process: interface not yet "
469 			    "present %s\n", pi->pi_name);
470 		}
471 		return;
472 	}
473 
474 	if (pi->pi_sock != -1) {
475 		if (poll_add(pi->pi_sock) == -1) {
476 			/*
477 			 * reset state.
478 			 */
479 			phyint_cleanup(pi);
480 		}
481 	}
482 
483 	/*
484 	 * Check if IFF_ROUTER has been turned off in kernel in which
485 	 * case we have to turn off AdvSendAdvertisements.
486 	 * The kernel will automatically turn off IFF_ROUTER if
487 	 * ip6_forwarding is turned off.
488 	 * Note that we do not switch back should IFF_ROUTER be turned on.
489 	 */
490 	if (!first &&
491 	    pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) {
492 		logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name);
493 		check_to_advertise(pi, START_FINAL_ADV);
494 
495 		pi->pi_AdvSendAdvertisements = 0;
496 		pi->pi_sol_state = NO_SOLICIT;
497 	}
498 
499 	/*
500 	 * Send advertisments and solicitation only if the interface is
501 	 * present in the kernel.
502 	 */
503 	if (pi->pi_kernel_state & PI_PRESENT) {
504 
505 		if (pi->pi_AdvSendAdvertisements) {
506 			if (pi->pi_adv_state == NO_ADV)
507 				check_to_advertise(pi, START_INIT_ADV);
508 		} else {
509 			if (pi->pi_sol_state == NO_SOLICIT)
510 				check_to_solicit(pi, START_INIT_SOLICIT);
511 		}
512 	}
513 
514 	/*
515 	 * Track static kernel prefixes to prevent in.ndpd from clobbering
516 	 * them by creating a struct prefix for each prefix detected in the
517 	 * kernel.
518 	 */
519 	pr = prefix_lookup_name(pi, ifname);
520 	if (pr == NULL) {
521 		pr = prefix_create_name(pi, ifname);
522 		if (pr == NULL) {
523 			logmsg(LOG_ERR, "if_process: out of memory\n");
524 			return;
525 		}
526 		if (prefix_init_from_k(pr) == -1) {
527 			prefix_delete(pr);
528 			return;
529 		}
530 	}
531 	/* Detect prefixes which are removed */
532 	if (pr->pr_kernel_state != 0)
533 		pr->pr_in_use = _B_TRUE;
534 
535 	if ((lifr.lifr_flags & IFF_DUPLICATE) &&
536 	    !(lifr.lifr_flags & IFF_DHCPRUNNING) &&
537 	    (pr->pr_flags & IFF_TEMPORARY)) {
538 		in6_addr_t *token;
539 		int i;
540 		char abuf[INET6_ADDRSTRLEN];
541 
542 		if (++pr->pr_attempts >= MAX_DAD_FAILURES) {
543 			logmsg(LOG_ERR, "%s: token %s is duplicate after %d "
544 			    "attempts; disabling temporary addresses on %s",
545 			    pr->pr_name, inet_ntop(AF_INET6,
546 			    (void *)&pi->pi_tmp_token, abuf, sizeof (abuf)),
547 			    pr->pr_attempts, pi->pi_name);
548 			pi->pi_TmpAddrsEnabled = 0;
549 			tmptoken_delete(pi);
550 			prefix_delete(pr);
551 			return;
552 		}
553 		logmsg(LOG_WARNING, "%s: token %s is duplicate; trying again",
554 		    pr->pr_name, inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token,
555 		    abuf, sizeof (abuf)));
556 		if (!tmptoken_create(pi)) {
557 			prefix_delete(pr);
558 			return;
559 		}
560 		token = &pi->pi_tmp_token;
561 		for (i = 0; i < 16; i++) {
562 			/*
563 			 * prefix_create ensures that pr_prefix has all-zero
564 			 * bits after prefixlen.
565 			 */
566 			pr->pr_address.s6_addr[i] = pr->pr_prefix.s6_addr[i] |
567 			    token->s6_addr[i];
568 		}
569 		if (prefix_lookup_addr_match(pr) != NULL) {
570 			prefix_delete(pr);
571 			return;
572 		}
573 		pr->pr_CreateTime = getcurrenttime() / MILLISEC;
574 		/*
575 		 * We've got a new token.  Clearing PR_AUTO causes
576 		 * prefix_update_k to bring the interface up and set the
577 		 * address.
578 		 */
579 		pr->pr_kernel_state &= ~PR_AUTO;
580 		prefix_update_k(pr);
581 	}
582 }
583 
584 static int ifsock = -1;
585 
586 /*
587  * Scan all interfaces to detect changes as well as new and deleted intefaces
588  * 'first' is set for the initial call only. Do not effect anything.
589  */
590 static void
591 initifs(boolean_t first)
592 {
593 	char *buf;
594 	int bufsize;
595 	int numifs;
596 	int n;
597 	struct lifnum lifn;
598 	struct lifconf lifc;
599 	struct lifreq *lifr;
600 	struct phyint *pi;
601 	struct phyint *next_pi;
602 	struct prefix *pr;
603 
604 	if (debug & D_IFSCAN)
605 		logmsg(LOG_DEBUG, "Reading interface configuration\n");
606 	if (ifsock < 0) {
607 		ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
608 		if (ifsock < 0) {
609 			logperror("initifs: socket");
610 			return;
611 		}
612 	}
613 	lifn.lifn_family = AF_INET6;
614 	lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
615 	if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) {
616 		logperror("initifs: ioctl (get interface numbers)");
617 		return;
618 	}
619 	numifs = lifn.lifn_count;
620 	bufsize = numifs * sizeof (struct lifreq);
621 
622 	buf = (char *)malloc(bufsize);
623 	if (buf == NULL) {
624 		logmsg(LOG_ERR, "initifs: out of memory\n");
625 		return;
626 	}
627 
628 	/*
629 	 * Mark the interfaces so that we can find phyints and prefixes
630 	 * which have disappeared from the kernel.
631 	 * if_process will set pr_in_use when it finds the interface
632 	 * in the kernel.
633 	 */
634 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
635 		/*
636 		 * Before re-examining the state of the interfaces,
637 		 * PI_PRESENT should be cleared from pi_kernel_state.
638 		 */
639 		pi->pi_kernel_state &= ~PI_PRESENT;
640 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
641 			pr->pr_in_use = _B_FALSE;
642 		}
643 	}
644 
645 	lifc.lifc_family = AF_INET6;
646 	lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
647 	lifc.lifc_len = bufsize;
648 	lifc.lifc_buf = buf;
649 
650 	if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) {
651 		logperror("initifs: ioctl (get interface configuration)");
652 		free(buf);
653 		return;
654 	}
655 
656 	lifr = (struct lifreq *)lifc.lifc_req;
657 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++)
658 		if_process(ifsock, lifr->lifr_name, first);
659 	free(buf);
660 
661 	/*
662 	 * Detect phyints that have been removed from the kernel.
663 	 * Since we can't recreate it here (would require ifconfig plumb
664 	 * logic) we just terminate use of that phyint.
665 	 */
666 	for (pi = phyints; pi != NULL; pi = next_pi) {
667 		next_pi = pi->pi_next;
668 		/*
669 		 * If interface (still) exists in kernel, set
670 		 * pi_state to indicate that.
671 		 */
672 		if (pi->pi_kernel_state & PI_PRESENT) {
673 			pi->pi_state |= PI_PRESENT;
674 		}
675 
676 		check_if_removed(pi);
677 	}
678 	if (show_ifs)
679 		phyint_print_all();
680 }
681 
682 
683 /*
684  * Router advertisement state machine. Used for everything but timer
685  * events which use advertise_event directly.
686  */
687 void
688 check_to_advertise(struct phyint *pi, enum adv_events event)
689 {
690 	uint_t delay;
691 	enum adv_states old_state = pi->pi_adv_state;
692 
693 	if (debug & D_STATE) {
694 		logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n",
695 		    pi->pi_name, (int)event, (int)old_state);
696 	}
697 	delay = advertise_event(pi, event, 0);
698 	if (delay != TIMER_INFINITY) {
699 		/* Make sure the global next event is updated */
700 		timer_schedule(delay);
701 	}
702 
703 	if (debug & D_STATE) {
704 		logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n",
705 		    pi->pi_name, (int)event, (int)old_state,
706 		    (int)pi->pi_adv_state);
707 	}
708 }
709 
710 /*
711  * Router advertisement state machine.
712  * Return the number of milliseconds until next timeout (TIMER_INFINITY
713  * if never).
714  * For the ADV_TIMER event the caller passes in the number of milliseconds
715  * since the last timer event in the 'elapsed' parameter.
716  */
717 uint_t
718 advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed)
719 {
720 	uint_t delay;
721 
722 	if (debug & D_STATE) {
723 		logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n",
724 		    pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state);
725 	}
726 	check_daemonize();
727 	if (!pi->pi_AdvSendAdvertisements)
728 		return (TIMER_INFINITY);
729 	if (pi->pi_flags & IFF_NORTEXCH) {
730 		if (debug & D_PKTOUT) {
731 			logmsg(LOG_DEBUG, "Suppress sending RA packet on %s "
732 			    "(no route exchange on interface)\n",
733 			    pi->pi_name);
734 		}
735 		return (TIMER_INFINITY);
736 	}
737 
738 	switch (event) {
739 	case ADV_OFF:
740 		pi->pi_adv_state = NO_ADV;
741 		return (TIMER_INFINITY);
742 
743 	case START_INIT_ADV:
744 		if (pi->pi_adv_state == INIT_ADV)
745 			return (pi->pi_adv_time_left);
746 		pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS;
747 		pi->pi_adv_time_left = 0;
748 		pi->pi_adv_state = INIT_ADV;
749 		break;	/* send advertisement */
750 
751 	case START_FINAL_ADV:
752 		if (pi->pi_adv_state == NO_ADV)
753 			return (TIMER_INFINITY);
754 		if (pi->pi_adv_state == FINAL_ADV)
755 			return (pi->pi_adv_time_left);
756 		pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS;
757 		pi->pi_adv_time_left = 0;
758 		pi->pi_adv_state = FINAL_ADV;
759 		break;	/* send advertisement */
760 
761 	case RECEIVED_SOLICIT:
762 		if (pi->pi_adv_state == NO_ADV)
763 			return (TIMER_INFINITY);
764 		if (pi->pi_adv_state == SOLICIT_ADV) {
765 			if (pi->pi_adv_time_left != 0)
766 				return (pi->pi_adv_time_left);
767 			break;
768 		}
769 		delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME);
770 		if (delay < pi->pi_adv_time_left)
771 			pi->pi_adv_time_left = delay;
772 		if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) {
773 			/*
774 			 * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS
775 			 * plus random delay) after the previous
776 			 * advertisement was sent.
777 			 */
778 			pi->pi_adv_time_left = delay +
779 			    ND_MIN_DELAY_BETWEEN_RAS -
780 			    pi->pi_adv_time_since_sent;
781 		}
782 		pi->pi_adv_state = SOLICIT_ADV;
783 		break;
784 
785 	case ADV_TIMER:
786 		if (pi->pi_adv_state == NO_ADV)
787 			return (TIMER_INFINITY);
788 		/* Decrease time left */
789 		if (pi->pi_adv_time_left >= elapsed)
790 			pi->pi_adv_time_left -= elapsed;
791 		else
792 			pi->pi_adv_time_left = 0;
793 
794 		/* Increase time since last advertisement was sent */
795 		pi->pi_adv_time_since_sent += elapsed;
796 		break;
797 	default:
798 		logmsg(LOG_ERR, "advertise_event: Unknown event %d\n",
799 		    (int)event);
800 		return (TIMER_INFINITY);
801 	}
802 
803 	if (pi->pi_adv_time_left != 0)
804 		return (pi->pi_adv_time_left);
805 
806 	/* Send advertisement and calculate next time to send */
807 	if (pi->pi_adv_state == FINAL_ADV) {
808 		/* Omit the prefixes */
809 		advertise(&v6allnodes, pi, _B_TRUE);
810 	} else {
811 		advertise(&v6allnodes, pi, _B_FALSE);
812 	}
813 	pi->pi_adv_time_since_sent = 0;
814 
815 	switch (pi->pi_adv_state) {
816 	case SOLICIT_ADV:
817 		/*
818 		 * The solicited advertisement has been sent.
819 		 * Revert to periodic advertisements.
820 		 */
821 		pi->pi_adv_state = REG_ADV;
822 		/* FALLTHRU */
823 	case REG_ADV:
824 		pi->pi_adv_time_left =
825 		    GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
826 		    1000 * pi->pi_MaxRtrAdvInterval);
827 		break;
828 
829 	case INIT_ADV:
830 		if (--pi->pi_adv_count > 0) {
831 			delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
832 			    1000 * pi->pi_MaxRtrAdvInterval);
833 			if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL)
834 				delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
835 			pi->pi_adv_time_left = delay;
836 		} else {
837 			pi->pi_adv_time_left =
838 			    GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
839 			    1000 * pi->pi_MaxRtrAdvInterval);
840 			pi->pi_adv_state = REG_ADV;
841 		}
842 		break;
843 
844 	case FINAL_ADV:
845 		if (--pi->pi_adv_count > 0) {
846 			pi->pi_adv_time_left =
847 			    ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
848 		} else {
849 			pi->pi_adv_state = NO_ADV;
850 		}
851 		break;
852 	}
853 	if (pi->pi_adv_state != NO_ADV)
854 		return (pi->pi_adv_time_left);
855 	else
856 		return (TIMER_INFINITY);
857 }
858 
859 /*
860  * Router solicitation state machine. Used for everything but timer
861  * events which use solicit_event directly.
862  */
863 void
864 check_to_solicit(struct phyint *pi, enum solicit_events event)
865 {
866 	uint_t delay;
867 	enum solicit_states old_state = pi->pi_sol_state;
868 
869 	if (debug & D_STATE) {
870 		logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
871 		    pi->pi_name, (int)event, (int)old_state);
872 	}
873 	delay = solicit_event(pi, event, 0);
874 	if (delay != TIMER_INFINITY) {
875 		/* Make sure the global next event is updated */
876 		timer_schedule(delay);
877 	}
878 
879 	if (debug & D_STATE) {
880 		logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
881 		    pi->pi_name, (int)event, (int)old_state,
882 		    (int)pi->pi_sol_state);
883 	}
884 }
885 
886 static void
887 daemonize_ndpd(void)
888 {
889 	FILE *pidfp;
890 	mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
891 	struct itimerval it;
892 	boolean_t timerval = _B_TRUE;
893 
894 	/*
895 	 * Need to get current timer settings so they can be restored
896 	 * after the fork(), as the it_value and it_interval values for
897 	 * the ITIMER_REAL timer are reset to 0 in the child process.
898 	 */
899 	if (getitimer(ITIMER_REAL, &it) < 0) {
900 		if (debug & D_TIMER)
901 			logmsg(LOG_DEBUG,
902 			    "daemonize_ndpd: failed to get itimerval\n");
903 		timerval = _B_FALSE;
904 	}
905 
906 	/* Daemonize. */
907 	switch (fork()) {
908 	case 0:
909 		/* Child */
910 		break;
911 	case -1:
912 		logperror("fork");
913 		exit(1);
914 	default:
915 		/* Parent */
916 		_exit(0);
917 	}
918 
919 	/* Store our process id, blow away any existing file if it exists. */
920 	if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
921 		(void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
922 		    argv0[0], strerror(errno));
923 	} else {
924 		(void) fprintf(pidfp, "%ld\n", getpid());
925 		(void) fclose(pidfp);
926 		(void) chmod(PATH_PID, pidmode);
927 	}
928 
929 	(void) close(0);
930 	(void) close(1);
931 	(void) close(2);
932 
933 	(void) chdir("/");
934 	(void) open("/dev/null", O_RDWR);
935 	(void) dup2(0, 1);
936 	(void) dup2(0, 2);
937 	(void) setsid();
938 
939 	already_daemonized = _B_TRUE;
940 
941 	/*
942 	 * Restore timer values, if we were able to save them; if not,
943 	 * check and set the right value by calling run_timeouts().
944 	 */
945 	if (timerval) {
946 		if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
947 			logperror("daemonize_ndpd: setitimer");
948 			exit(2);
949 		}
950 	} else {
951 		run_timeouts();
952 	}
953 }
954 
955 /*
956  * Check to see if the time is right to daemonize.  The right time is when:
957  *
958  * 1.  We haven't already daemonized.
959  * 2.  We are not in debug mode.
960  * 3.  All interfaces are marked IFF_NOXMIT.
961  * 4.  All non-router interfaces have their prefixes set up and we're
962  *     done sending router solicitations on those interfaces without
963  *     prefixes.
964  */
965 static void
966 check_daemonize(void)
967 {
968 	struct phyint		*pi;
969 
970 	if (already_daemonized || debug != 0)
971 		return;
972 
973 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
974 		if (!(pi->pi_flags & IFF_NOXMIT))
975 			break;
976 	}
977 
978 	/*
979 	 * If we can't transmit on any of the interfaces there is no reason
980 	 * to hold up progress.
981 	 */
982 	if (pi == NULL) {
983 		daemonize_ndpd();
984 		return;
985 	}
986 
987 	/* Check all interfaces.  If any are still soliciting, just return. */
988 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
989 		if (pi->pi_AdvSendAdvertisements ||
990 		    !(pi->pi_kernel_state & PI_PRESENT))
991 			continue;
992 
993 		if (pi->pi_sol_state == INIT_SOLICIT)
994 			return;
995 	}
996 
997 	daemonize_ndpd();
998 }
999 
1000 /*
1001  * Router solicitation state machine.
1002  * Return the number of milliseconds until next timeout (TIMER_INFINITY
1003  * if never).
1004  * For the SOL_TIMER event the caller passes in the number of milliseconds
1005  * since the last timer event in the 'elapsed' parameter.
1006  */
1007 uint_t
1008 solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed)
1009 {
1010 	if (debug & D_STATE) {
1011 		logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n",
1012 		    pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state);
1013 	}
1014 
1015 	if (pi->pi_AdvSendAdvertisements)
1016 		return (TIMER_INFINITY);
1017 	if (pi->pi_flags & IFF_NORTEXCH) {
1018 		if (debug & D_PKTOUT) {
1019 			logmsg(LOG_DEBUG, "Suppress sending RS packet on %s "
1020 			    "(no route exchange on interface)\n",
1021 			    pi->pi_name);
1022 		}
1023 		return (TIMER_INFINITY);
1024 	}
1025 
1026 	switch (event) {
1027 	case SOLICIT_OFF:
1028 		pi->pi_sol_state = NO_SOLICIT;
1029 		check_daemonize();
1030 		return (TIMER_INFINITY);
1031 
1032 	case SOLICIT_DONE:
1033 		pi->pi_sol_state = DONE_SOLICIT;
1034 		check_daemonize();
1035 		return (TIMER_INFINITY);
1036 
1037 	case RESTART_INIT_SOLICIT:
1038 		/*
1039 		 * This event allows us to start solicitation over again
1040 		 * without losing the RA flags.  We start solicitation over
1041 		 * when we are missing an interface prefix for a newly-
1042 		 * encountered DHCP interface.
1043 		 */
1044 		if (pi->pi_sol_state == INIT_SOLICIT)
1045 			return (pi->pi_sol_time_left);
1046 		pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
1047 		pi->pi_sol_time_left =
1048 		    GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
1049 		pi->pi_sol_state = INIT_SOLICIT;
1050 		break;
1051 
1052 	case START_INIT_SOLICIT:
1053 		if (pi->pi_sol_state == INIT_SOLICIT)
1054 			return (pi->pi_sol_time_left);
1055 		pi->pi_ra_flags = 0;
1056 		pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
1057 		pi->pi_sol_time_left =
1058 		    GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
1059 		pi->pi_sol_state = INIT_SOLICIT;
1060 		break;
1061 
1062 	case SOL_TIMER:
1063 		if (pi->pi_sol_state == NO_SOLICIT)
1064 			return (TIMER_INFINITY);
1065 		/* Decrease time left */
1066 		if (pi->pi_sol_time_left >= elapsed)
1067 			pi->pi_sol_time_left -= elapsed;
1068 		else
1069 			pi->pi_sol_time_left = 0;
1070 		break;
1071 	default:
1072 		logmsg(LOG_ERR, "solicit_event: Unknown event %d\n",
1073 		    (int)event);
1074 		return (TIMER_INFINITY);
1075 	}
1076 
1077 	if (pi->pi_sol_time_left != 0)
1078 		return (pi->pi_sol_time_left);
1079 
1080 	/* Send solicitation and calculate next time */
1081 	switch (pi->pi_sol_state) {
1082 	case INIT_SOLICIT:
1083 		solicit(&v6allrouters, pi);
1084 		if (--pi->pi_sol_count == 0) {
1085 			if (debug & D_STATE) {
1086 				logmsg(LOG_DEBUG, "solicit_event: no routers "
1087 				    "found on %s; assuming default flags\n",
1088 				    pi->pi_name);
1089 			}
1090 			if (pi->pi_StatefulAddrConf) {
1091 				pi->pi_ra_flags |= ND_RA_FLAG_MANAGED |
1092 				    ND_RA_FLAG_OTHER;
1093 				start_dhcp(pi);
1094 			}
1095 			pi->pi_sol_state = DONE_SOLICIT;
1096 			check_daemonize();
1097 			return (TIMER_INFINITY);
1098 		}
1099 		pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL;
1100 		return (pi->pi_sol_time_left);
1101 	case NO_SOLICIT:
1102 	case DONE_SOLICIT:
1103 		return (TIMER_INFINITY);
1104 	default:
1105 		return (pi->pi_sol_time_left);
1106 	}
1107 }
1108 
1109 /*
1110  * Timer mechanism using relative time (in milliseconds) from the
1111  * previous timer event. Timers exceeding TIMER_INFINITY milliseconds
1112  * will fire after TIMER_INFINITY milliseconds.
1113  */
1114 static uint_t timer_previous;	/* When last SIGALRM occurred */
1115 static uint_t timer_next;	/* Currently scheduled timeout */
1116 
1117 static void
1118 timer_init(void)
1119 {
1120 	timer_previous = getcurrenttime();
1121 	timer_next = TIMER_INFINITY;
1122 	run_timeouts();
1123 }
1124 
1125 /*
1126  * Make sure the next SIGALRM occurs delay milliseconds from the current
1127  * time if not earlier.
1128  * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound
1129  * by treating differences greater than 0x80000000 as negative.
1130  */
1131 void
1132 timer_schedule(uint_t delay)
1133 {
1134 	uint_t now;
1135 	struct itimerval itimerval;
1136 
1137 	now = getcurrenttime();
1138 	if (debug & D_TIMER) {
1139 		logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n",
1140 		    delay, now, timer_next);
1141 	}
1142 	/* Will this timer occur before the currently scheduled SIGALRM? */
1143 	if (delay >= timer_next - now) {
1144 		if (debug & D_TIMER) {
1145 			logmsg(LOG_DEBUG, "timer_schedule(%u): no action - "
1146 			    "next in %u ms\n",
1147 			    delay, timer_next - now);
1148 		}
1149 		return;
1150 	}
1151 	if (delay == 0) {
1152 		/* Minimum allowed delay */
1153 		delay = 1;
1154 	}
1155 	timer_next = now + delay;
1156 
1157 	itimerval.it_value.tv_sec = delay / 1000;
1158 	itimerval.it_value.tv_usec = (delay % 1000) * 1000;
1159 	itimerval.it_interval.tv_sec = 0;
1160 	itimerval.it_interval.tv_usec = 0;
1161 	if (debug & D_TIMER) {
1162 		logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n",
1163 		    delay,
1164 		    itimerval.it_value.tv_sec, itimerval.it_value.tv_usec);
1165 	}
1166 	if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) {
1167 		logperror("timer_schedule: setitimer");
1168 		exit(2);
1169 	}
1170 }
1171 
1172 /*
1173  * Conditional running of timer. If more than 'minimal_time' millseconds
1174  * since the timer routines were last run we run them.
1175  * Used when packets arrive.
1176  */
1177 static void
1178 conditional_run_timeouts(uint_t minimal_time)
1179 {
1180 	uint_t now;
1181 	uint_t elapsed;
1182 
1183 	now = getcurrenttime();
1184 	elapsed = now - timer_previous;
1185 	if (elapsed > minimal_time) {
1186 		if (debug & D_TIMER) {
1187 			logmsg(LOG_DEBUG, "conditional_run_timeouts: "
1188 			    "elapsed %d\n", elapsed);
1189 		}
1190 		run_timeouts();
1191 	}
1192 }
1193 
1194 /*
1195  * Timer has fired.
1196  * Determine when the next timer event will occur by asking all
1197  * the timer routines.
1198  * Should not be called from a timer routine but in some cases this is
1199  * done because the code doesn't know that e.g. it was called from
1200  * ifconfig_timer(). In this case the nested run_timeouts will just return but
1201  * the running run_timeouts will ensure to call all the timer functions by
1202  * looping once more.
1203  */
1204 static void
1205 run_timeouts(void)
1206 {
1207 	uint_t now;
1208 	uint_t elapsed;
1209 	uint_t next;
1210 	uint_t nexti;
1211 	struct phyint *pi;
1212 	struct phyint *next_pi;
1213 	struct prefix *pr;
1214 	struct prefix *next_pr;
1215 	struct adv_prefix *adv_pr;
1216 	struct adv_prefix *next_adv_pr;
1217 	struct router *dr;
1218 	struct router *next_dr;
1219 	static boolean_t timeout_running;
1220 	static boolean_t do_retry;
1221 
1222 	if (timeout_running) {
1223 		if (debug & D_TIMER)
1224 			logmsg(LOG_DEBUG, "run_timeouts: nested call\n");
1225 		do_retry = _B_TRUE;
1226 		return;
1227 	}
1228 	timeout_running = _B_TRUE;
1229 retry:
1230 	/* How much time since the last time we were called? */
1231 	now = getcurrenttime();
1232 	elapsed = now - timer_previous;
1233 	timer_previous = now;
1234 
1235 	if (debug & D_TIMER)
1236 		logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed);
1237 
1238 	next = TIMER_INFINITY;
1239 	for (pi = phyints; pi != NULL; pi = next_pi) {
1240 		next_pi = pi->pi_next;
1241 		nexti = phyint_timer(pi, elapsed);
1242 		if (nexti != TIMER_INFINITY && nexti < next)
1243 			next = nexti;
1244 		if (debug & D_TIMER) {
1245 			logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n",
1246 			    pi->pi_name, nexti, next);
1247 		}
1248 		for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1249 			next_pr = pr->pr_next;
1250 			nexti = prefix_timer(pr, elapsed);
1251 			if (nexti != TIMER_INFINITY && nexti < next)
1252 				next = nexti;
1253 			if (debug & D_TIMER) {
1254 				logmsg(LOG_DEBUG, "run_timeouts (pr %s): "
1255 				    "%d -> %u ms\n", pr->pr_name, nexti, next);
1256 			}
1257 		}
1258 		for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
1259 		    adv_pr = next_adv_pr) {
1260 			next_adv_pr = adv_pr->adv_pr_next;
1261 			nexti = adv_prefix_timer(adv_pr, elapsed);
1262 			if (nexti != TIMER_INFINITY && nexti < next)
1263 				next = nexti;
1264 			if (debug & D_TIMER) {
1265 				logmsg(LOG_DEBUG, "run_timeouts "
1266 				    "(adv pr on %s): %d -> %u ms\n",
1267 				    adv_pr->adv_pr_physical->pi_name,
1268 				    nexti, next);
1269 			}
1270 		}
1271 		for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) {
1272 			next_dr = dr->dr_next;
1273 			nexti = router_timer(dr, elapsed);
1274 			if (nexti != TIMER_INFINITY && nexti < next)
1275 				next = nexti;
1276 			if (debug & D_TIMER) {
1277 				logmsg(LOG_DEBUG, "run_timeouts (dr): "
1278 				    "%d -> %u ms\n", nexti, next);
1279 			}
1280 		}
1281 		if (pi->pi_TmpAddrsEnabled) {
1282 			nexti = tmptoken_timer(pi, elapsed);
1283 			if (nexti != TIMER_INFINITY && nexti < next)
1284 				next = nexti;
1285 			if (debug & D_TIMER) {
1286 				logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): "
1287 				    "%d -> %u ms\n", pi->pi_name, nexti, next);
1288 			}
1289 		}
1290 	}
1291 	/*
1292 	 * Make sure the timer functions are run at least once
1293 	 * an hour.
1294 	 */
1295 	if (next == TIMER_INFINITY)
1296 		next = 3600 * 1000;	/* 1 hour */
1297 
1298 	if (debug & D_TIMER)
1299 		logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next);
1300 	timer_schedule(next);
1301 	if (do_retry) {
1302 		if (debug & D_TIMER)
1303 			logmsg(LOG_DEBUG, "run_timeouts: retry\n");
1304 		do_retry = _B_FALSE;
1305 		goto retry;
1306 	}
1307 	timeout_running = _B_FALSE;
1308 }
1309 
1310 static int eventpipe_read = -1;	/* Used for synchronous signal delivery */
1311 static int eventpipe_write = -1;
1312 
1313 /*
1314  * Ensure that signals are processed synchronously with the rest of
1315  * the code by just writing a one character signal number on the pipe.
1316  * The poll loop will pick this up and process the signal event.
1317  */
1318 static void
1319 sig_handler(int signo)
1320 {
1321 	uchar_t buf = (uchar_t)signo;
1322 
1323 	if (eventpipe_write == -1) {
1324 		logmsg(LOG_ERR, "sig_handler: no pipe\n");
1325 		return;
1326 	}
1327 	if (write(eventpipe_write, &buf, sizeof (buf)) < 0)
1328 		logperror("sig_handler: write");
1329 }
1330 
1331 /*
1332  * Pick up a signal "byte" from the pipe and process it.
1333  */
1334 static void
1335 in_signal(int fd)
1336 {
1337 	uchar_t buf;
1338 	struct phyint *pi;
1339 	struct phyint *next_pi;
1340 
1341 	switch (read(fd, &buf, sizeof (buf))) {
1342 	case -1:
1343 		logperror("in_signal: read");
1344 		exit(1);
1345 		/* NOTREACHED */
1346 	case 1:
1347 		break;
1348 	case 0:
1349 		logmsg(LOG_ERR, "in_signal: read eof\n");
1350 		exit(1);
1351 		/* NOTREACHED */
1352 	default:
1353 		logmsg(LOG_ERR, "in_signal: read > 1\n");
1354 		exit(1);
1355 	}
1356 
1357 	if (debug & D_TIMER)
1358 		logmsg(LOG_DEBUG, "in_signal() got %d\n", buf);
1359 
1360 	switch (buf) {
1361 	case SIGALRM:
1362 		if (debug & D_TIMER) {
1363 			uint_t now = getcurrenttime();
1364 
1365 			logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n",
1366 			    now - timer_next);
1367 		}
1368 		timer_next = TIMER_INFINITY;
1369 		run_timeouts();
1370 		break;
1371 	case SIGHUP:
1372 		/* Re-read config file by exec'ing ourselves */
1373 		for (pi = phyints; pi != NULL; pi = next_pi) {
1374 			next_pi = pi->pi_next;
1375 			if (pi->pi_AdvSendAdvertisements)
1376 				check_to_advertise(pi, START_FINAL_ADV);
1377 
1378 			phyint_delete(pi);
1379 		}
1380 
1381 		/*
1382 		 * Prevent fd leaks.  Everything gets re-opened at start-up
1383 		 * time.  0, 1, and 2 are closed and re-opened as
1384 		 * /dev/null, so we'll leave those open.
1385 		 */
1386 		closefrom(3);
1387 
1388 		logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
1389 		(void) execv(argv0[0], argv0);
1390 		(void) unlink(PATH_PID);
1391 		_exit(0177);
1392 		/* NOTREACHED */
1393 	case SIGUSR1:
1394 		logmsg(LOG_DEBUG, "Printing configuration:\n");
1395 		phyint_print_all();
1396 		break;
1397 	case SIGINT:
1398 	case SIGTERM:
1399 	case SIGQUIT:
1400 		for (pi = phyints; pi != NULL; pi = next_pi) {
1401 			next_pi = pi->pi_next;
1402 			if (pi->pi_AdvSendAdvertisements)
1403 				check_to_advertise(pi, START_FINAL_ADV);
1404 
1405 			phyint_delete(pi);
1406 		}
1407 		(void) unlink(NDPD_SNMP_SOCKET);
1408 		(void) unlink(PATH_PID);
1409 		exit(0);
1410 		/* NOTREACHED */
1411 	case 255:
1412 		/*
1413 		 * Special "signal" from looback_ra_enqueue.
1414 		 * Handle any queued loopback router advertisements.
1415 		 */
1416 		loopback_ra_dequeue();
1417 		break;
1418 	default:
1419 		logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
1420 	}
1421 }
1422 
1423 /*
1424  * Create pipe for signal delivery and set up signal handlers.
1425  */
1426 static void
1427 setup_eventpipe(void)
1428 {
1429 	int fds[2];
1430 	struct sigaction act;
1431 
1432 	if ((pipe(fds)) < 0) {
1433 		logperror("setup_eventpipe: pipe");
1434 		exit(1);
1435 	}
1436 	eventpipe_read = fds[0];
1437 	eventpipe_write = fds[1];
1438 	if (poll_add(eventpipe_read) == -1) {
1439 		exit(1);
1440 	}
1441 	act.sa_handler = sig_handler;
1442 	act.sa_flags = SA_RESTART;
1443 	(void) sigaction(SIGALRM, &act, NULL);
1444 
1445 	(void) sigset(SIGHUP, sig_handler);
1446 	(void) sigset(SIGUSR1, sig_handler);
1447 	(void) sigset(SIGTERM, sig_handler);
1448 	(void) sigset(SIGINT, sig_handler);
1449 	(void) sigset(SIGQUIT, sig_handler);
1450 }
1451 
1452 /*
1453  * Create a routing socket for receiving RTM_IFINFO messages and initialize
1454  * the routing socket message header and as much of the sockaddrs as possible.
1455  */
1456 static int
1457 setup_rtsock(void)
1458 {
1459 	int s;
1460 	int ret;
1461 	char *cp;
1462 	struct sockaddr_in6 *sin6;
1463 
1464 	s = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
1465 	if (s == -1) {
1466 		logperror("socket(PF_ROUTE)");
1467 		exit(1);
1468 	}
1469 	ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK);
1470 	if (ret < 0) {
1471 		logperror("fcntl(O_NDELAY)");
1472 		exit(1);
1473 	}
1474 	if (poll_add(s) == -1) {
1475 		exit(1);
1476 	}
1477 
1478 	/*
1479 	 * Allocate storage for the routing socket message.
1480 	 */
1481 	rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN);
1482 	if (rt_msg == NULL) {
1483 		logperror("malloc");
1484 		exit(1);
1485 	}
1486 
1487 	/*
1488 	 * Initialize the routing socket message by zero-filling it and then
1489 	 * setting the fields where are constant through the lifetime of the
1490 	 * process.
1491 	 */
1492 	bzero(rt_msg, NDP_RTM_MSGLEN);
1493 	rt_msg->rtm_msglen = NDP_RTM_MSGLEN;
1494 	rt_msg->rtm_version = RTM_VERSION;
1495 	rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP;
1496 	rt_msg->rtm_pid = getpid();
1497 	if (rt_msg->rtm_pid < 0) {
1498 		logperror("getpid");
1499 		exit(1);
1500 	}
1501 
1502 	/*
1503 	 * The RTA_DST sockaddr does not change during the lifetime of the
1504 	 * process so it can be completely initialized at this time.
1505 	 */
1506 	cp = (char *)rt_msg + sizeof (struct rt_msghdr);
1507 	sin6 = (struct sockaddr_in6 *)cp;
1508 	sin6->sin6_family = AF_INET6;
1509 	sin6->sin6_addr = in6addr_any;
1510 
1511 	/*
1512 	 * Initialize the constant portion of the RTA_GATEWAY sockaddr.
1513 	 */
1514 	cp += sizeof (struct sockaddr_in6);
1515 	rta_gateway = (struct sockaddr_in6 *)cp;
1516 	rta_gateway->sin6_family = AF_INET6;
1517 
1518 	/*
1519 	 * The RTA_NETMASK sockaddr does not change during the lifetime of the
1520 	 * process so it can be completely initialized at this time.
1521 	 */
1522 	cp += sizeof (struct sockaddr_in6);
1523 	sin6 = (struct sockaddr_in6 *)cp;
1524 	sin6->sin6_family = AF_INET6;
1525 	sin6->sin6_addr = in6addr_any;
1526 
1527 	/*
1528 	 * Initialize the constant portion of the RTA_IFP sockaddr.
1529 	 */
1530 	cp += sizeof (struct sockaddr_in6);
1531 	rta_ifp = (struct sockaddr_dl *)cp;
1532 	rta_ifp->sdl_family = AF_LINK;
1533 
1534 	return (s);
1535 }
1536 
1537 static int
1538 setup_mibsock(void)
1539 {
1540 	int sock;
1541 	int ret;
1542 	int len;
1543 	struct sockaddr_un laddr;
1544 
1545 	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
1546 	if (sock == -1) {
1547 		logperror("setup_mibsock: socket(AF_UNIX)");
1548 		exit(1);
1549 	}
1550 
1551 	bzero(&laddr, sizeof (laddr));
1552 	laddr.sun_family = AF_UNIX;
1553 
1554 	(void) strncpy(laddr.sun_path, NDPD_SNMP_SOCKET,
1555 	    sizeof (laddr.sun_path));
1556 	len = sizeof (struct sockaddr_un);
1557 
1558 	(void) unlink(NDPD_SNMP_SOCKET);
1559 	ret = bind(sock, (struct sockaddr *)&laddr, len);
1560 	if (ret < 0) {
1561 		logperror("setup_mibsock: bind\n");
1562 		exit(1);
1563 	}
1564 
1565 	ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1566 	if (ret < 0) {
1567 		logperror("fcntl(O_NONBLOCK)");
1568 		exit(1);
1569 	}
1570 	if (poll_add(sock) == -1) {
1571 		exit(1);
1572 	}
1573 	return (sock);
1574 }
1575 
1576 /*
1577  * Retrieve one routing socket message. If RTM_IFINFO indicates
1578  * new phyint do a full scan of the interfaces. If RTM_IFINFO
1579  * indicates an existing phyint, only scan that phyint and associated
1580  * prefixes.
1581  */
1582 static void
1583 process_rtsock(int rtsock)
1584 {
1585 	int n;
1586 #define	MSG_SIZE	2048/8
1587 	int64_t msg[MSG_SIZE];
1588 	struct rt_msghdr *rtm;
1589 	struct if_msghdr *ifm;
1590 	struct phyint *pi;
1591 	struct prefix *pr;
1592 	boolean_t need_initifs = _B_FALSE;
1593 	boolean_t need_ifscan = _B_FALSE;
1594 	int64_t	ifscan_msg[10][MSG_SIZE];
1595 	int ifscan_index = 0;
1596 	int i;
1597 
1598 	/* Empty the rtsock and coealesce all the work that we have */
1599 	while (ifscan_index < 10) {
1600 		n = read(rtsock, msg, sizeof (msg));
1601 		if (n <= 0) {
1602 			/* No more messages */
1603 			break;
1604 		}
1605 		rtm = (struct rt_msghdr *)msg;
1606 		if (rtm->rtm_version != RTM_VERSION) {
1607 			logmsg(LOG_ERR,
1608 			    "process_rtsock: version %d not understood\n",
1609 			    rtm->rtm_version);
1610 			return;
1611 		}
1612 		switch (rtm->rtm_type) {
1613 		case RTM_NEWADDR:
1614 		case RTM_DELADDR:
1615 			/*
1616 			 * Some logical interface has changed - have to scan
1617 			 * everything to determine what actually changed.
1618 			 */
1619 			if (debug & D_IFSCAN) {
1620 				logmsg(LOG_DEBUG, "process_rtsock: "
1621 				    "message %d\n", rtm->rtm_type);
1622 			}
1623 			need_initifs = _B_TRUE;
1624 			break;
1625 		case RTM_IFINFO:
1626 			need_ifscan = _B_TRUE;
1627 			(void) memcpy(ifscan_msg[ifscan_index], rtm,
1628 			    sizeof (msg));
1629 			ifscan_index++;
1630 			/* Handled below */
1631 			break;
1632 		default:
1633 			/* Not interesting */
1634 			break;
1635 		}
1636 	}
1637 	/*
1638 	 * If we do full scan i.e initifs, we don't need to
1639 	 * scan a particular interface as we should have
1640 	 * done that as part of initifs.
1641 	 */
1642 	if (need_initifs) {
1643 		initifs(_B_FALSE);
1644 		return;
1645 	}
1646 
1647 	if (!need_ifscan)
1648 		return;
1649 
1650 	for (i = 0; i < ifscan_index; i++) {
1651 		ifm = (struct if_msghdr *)ifscan_msg[i];
1652 		if (debug & D_IFSCAN)
1653 			logmsg(LOG_DEBUG, "process_rtsock: index %d\n",
1654 			    ifm->ifm_index);
1655 
1656 		pi = phyint_lookup_on_index(ifm->ifm_index);
1657 		if (pi == NULL) {
1658 			/*
1659 			 * A new physical interface. Do a full scan of the
1660 			 * to catch any new logical interfaces.
1661 			 */
1662 			initifs(_B_FALSE);
1663 			return;
1664 		}
1665 
1666 		if (ifm->ifm_flags != (uint_t)pi->pi_flags) {
1667 			if (debug & D_IFSCAN) {
1668 				logmsg(LOG_DEBUG, "process_rtsock: clr for "
1669 				    "%s old flags 0x%llx new flags 0x%x\n",
1670 				    pi->pi_name, pi->pi_flags, ifm->ifm_flags);
1671 			}
1672 		}
1673 
1674 
1675 		/*
1676 		 * Mark the interfaces so that we can find phyints and prefixes
1677 		 * which have disappeared from the kernel.
1678 		 * if_process will set pr_in_use when it finds the
1679 		 * interface in the kernel.
1680 		 * Before re-examining the state of the interfaces,
1681 		 * PI_PRESENT should be cleared from pi_kernel_state.
1682 		 */
1683 		pi->pi_kernel_state &= ~PI_PRESENT;
1684 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
1685 			pr->pr_in_use = _B_FALSE;
1686 		}
1687 
1688 		if (ifsock < 0) {
1689 			ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
1690 			if (ifsock < 0) {
1691 				logperror("process_rtsock: socket");
1692 				return;
1693 			}
1694 		}
1695 		if_process(ifsock, pi->pi_name, _B_FALSE);
1696 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
1697 			if_process(ifsock, pr->pr_name, _B_FALSE);
1698 		}
1699 		/*
1700 		 * If interface (still) exists in kernel, set
1701 		 * pi_state to indicate that.
1702 		 */
1703 		if (pi->pi_kernel_state & PI_PRESENT) {
1704 			pi->pi_state |= PI_PRESENT;
1705 		}
1706 		check_if_removed(pi);
1707 		if (show_ifs)
1708 			phyint_print_all();
1709 	}
1710 }
1711 
1712 static void
1713 process_mibsock(int mibsock)
1714 {
1715 	struct phyint *pi;
1716 	socklen_t fromlen;
1717 	struct sockaddr_un from;
1718 	ndpd_info_t ndpd_info;
1719 	ssize_t len;
1720 	int command;
1721 
1722 	fromlen = (socklen_t)sizeof (from);
1723 	len = recvfrom(mibsock, &command, sizeof (int), 0,
1724 	    (struct sockaddr *)&from, &fromlen);
1725 
1726 	if (len < sizeof (int) || command != NDPD_SNMP_INFO_REQ) {
1727 		logperror("process_mibsock: bad command \n");
1728 		return;
1729 	}
1730 
1731 	ndpd_info.info_type = NDPD_SNMP_INFO_RESPONSE;
1732 	ndpd_info.info_version = NDPD_SNMP_INFO_VER;
1733 	ndpd_info.info_num_of_phyints = num_of_phyints;
1734 
1735 	(void) sendto(mibsock, &ndpd_info, sizeof (ndpd_info_t), 0,
1736 	    (struct sockaddr *)&from, fromlen);
1737 
1738 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
1739 		int prefixes;
1740 		int routers;
1741 		struct prefix   *prefix_list;
1742 		struct router   *router_list;
1743 		ndpd_phyint_info_t phyint;
1744 		ndpd_prefix_info_t prefix;
1745 		ndpd_router_info_t router;
1746 		/*
1747 		 * get number of prefixes
1748 		 */
1749 		routers = 0;
1750 		prefixes = 0;
1751 		prefix_list = pi->pi_prefix_list;
1752 		while (prefix_list != NULL) {
1753 			prefixes++;
1754 			prefix_list = prefix_list->pr_next;
1755 		}
1756 
1757 		/*
1758 		 * get number of routers
1759 		 */
1760 		router_list = pi->pi_router_list;
1761 		while (router_list != NULL) {
1762 			routers++;
1763 			router_list = router_list->dr_next;
1764 		}
1765 
1766 		phyint.phyint_info_type = NDPD_PHYINT_INFO;
1767 		phyint.phyint_info_version = NDPD_PHYINT_INFO_VER;
1768 		phyint.phyint_index = pi->pi_index;
1769 		bcopy(pi->pi_config,
1770 		    phyint.phyint_config, I_IFSIZE);
1771 		phyint.phyint_num_of_prefixes = prefixes;
1772 		phyint.phyint_num_of_routers = routers;
1773 		(void) sendto(mibsock, &phyint, sizeof (phyint), 0,
1774 		    (struct sockaddr *)&from, fromlen);
1775 
1776 		/*
1777 		 * Copy prefix information
1778 		 */
1779 
1780 		prefix_list = pi->pi_prefix_list;
1781 		while (prefix_list != NULL) {
1782 			prefix.prefix_info_type = NDPD_PREFIX_INFO;
1783 			prefix.prefix_info_version = NDPD_PREFIX_INFO_VER;
1784 			prefix.prefix_prefix = prefix_list->pr_prefix;
1785 			prefix.prefix_len = prefix_list->pr_prefix_len;
1786 			prefix.prefix_flags = prefix_list->pr_flags;
1787 			prefix.prefix_phyint_index = pi->pi_index;
1788 			prefix.prefix_ValidLifetime =
1789 			    prefix_list->pr_ValidLifetime;
1790 			prefix.prefix_PreferredLifetime =
1791 			    prefix_list->pr_PreferredLifetime;
1792 			prefix.prefix_OnLinkLifetime =
1793 			    prefix_list->pr_OnLinkLifetime;
1794 			prefix.prefix_OnLinkFlag =
1795 			    prefix_list->pr_OnLinkFlag;
1796 			prefix.prefix_AutonomousFlag =
1797 			    prefix_list->pr_AutonomousFlag;
1798 			(void) sendto(mibsock, &prefix, sizeof (prefix), 0,
1799 			    (struct sockaddr *)&from, fromlen);
1800 			prefix_list = prefix_list->pr_next;
1801 		}
1802 		/*
1803 		 * Copy router information
1804 		 */
1805 		router_list = pi->pi_router_list;
1806 		while (router_list != NULL) {
1807 			router.router_info_type = NDPD_ROUTER_INFO;
1808 			router.router_info_version = NDPD_ROUTER_INFO_VER;
1809 			router.router_address = router_list->dr_address;
1810 			router.router_lifetime = router_list->dr_lifetime;
1811 			router.router_phyint_index = pi->pi_index;
1812 			(void) sendto(mibsock, &router, sizeof (router), 0,
1813 			    (struct sockaddr *)&from, fromlen);
1814 			router_list = router_list->dr_next;
1815 		}
1816 	}
1817 }
1818 
1819 /*
1820  * Look if the phyint or one of its prefixes have been removed from
1821  * the kernel and take appropriate action.
1822  * Uses pr_in_use and pi{,_kernel}_state.
1823  */
1824 static void
1825 check_if_removed(struct phyint *pi)
1826 {
1827 	struct prefix *pr, *next_pr;
1828 
1829 	/*
1830 	 * Detect prefixes which are removed.
1831 	 * Static prefixes are just removed from our tables.
1832 	 * Non-static prefixes are recreated i.e. in.ndpd takes precedence
1833 	 * over manually removing prefixes via ifconfig.
1834 	 */
1835 	for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1836 		next_pr = pr->pr_next;
1837 		if (!pr->pr_in_use) {
1838 			/* Clear everything except PR_STATIC */
1839 			pr->pr_kernel_state &= PR_STATIC;
1840 			pr->pr_name[0] = '\0';
1841 			if (pr->pr_state & PR_STATIC) {
1842 				prefix_delete(pr);
1843 			} else if (!(pi->pi_kernel_state & PI_PRESENT)) {
1844 				/*
1845 				 * Ensure that there are no future attempts to
1846 				 * run prefix_update_k since the phyint is gone.
1847 				 */
1848 				pr->pr_state = pr->pr_kernel_state;
1849 			} else if (pr->pr_state != pr->pr_kernel_state) {
1850 				logmsg(LOG_INFO, "Prefix manually removed "
1851 				    "on %s; recreating\n", pi->pi_name);
1852 				prefix_update_k(pr);
1853 			}
1854 		}
1855 	}
1856 
1857 	/*
1858 	 * Detect phyints that have been removed from the kernel, and tear
1859 	 * down any prefixes we created that are associated with that phyint.
1860 	 * (NOTE: IPMP depends on in.ndpd tearing down these prefixes so an
1861 	 * administrator can easily place an IP interface with ADDRCONF'd
1862 	 * addresses into an IPMP group.)
1863 	 */
1864 	if (!(pi->pi_kernel_state & PI_PRESENT) &&
1865 	    (pi->pi_state & PI_PRESENT)) {
1866 		logmsg(LOG_ERR, "Interface %s has been removed from kernel. "
1867 		    "in.ndpd will no longer use it\n", pi->pi_name);
1868 
1869 		for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1870 			next_pr = pr->pr_next;
1871 			if (pr->pr_state & PR_AUTO)
1872 				prefix_delete(pr);
1873 		}
1874 
1875 		/*
1876 		 * Clear state so that should the phyint reappear we will
1877 		 * start with initial advertisements or solicitations.
1878 		 */
1879 		phyint_cleanup(pi);
1880 	}
1881 }
1882 
1883 
1884 /*
1885  * Queuing mechanism for router advertisements that are sent by in.ndpd
1886  * and that also need to be processed by in.ndpd.
1887  * Uses "signal number" 255 to indicate to the main poll loop
1888  * that there is something to dequeue and send to incomining_ra().
1889  */
1890 struct raq {
1891 	struct raq	*raq_next;
1892 	struct phyint	*raq_pi;
1893 	int		raq_packetlen;
1894 	uchar_t		*raq_packet;
1895 };
1896 static struct raq *raq_head = NULL;
1897 
1898 /*
1899  * Allocate a struct raq and memory for the packet.
1900  * Send signal 255 to have poll dequeue.
1901  */
1902 static void
1903 loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len)
1904 {
1905 	struct raq *raq;
1906 	struct raq **raqp;
1907 
1908 	if (no_loopback)
1909 		return;
1910 
1911 	if (debug & D_PKTOUT)
1912 		logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name);
1913 
1914 	raq = calloc(sizeof (struct raq), 1);
1915 	if (raq == NULL) {
1916 		logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
1917 		return;
1918 	}
1919 	raq->raq_packet = malloc(len);
1920 	if (raq->raq_packet == NULL) {
1921 		free(raq);
1922 		logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
1923 		return;
1924 	}
1925 	bcopy(ra, raq->raq_packet, len);
1926 	raq->raq_packetlen = len;
1927 	raq->raq_pi = pi;
1928 
1929 	/* Tail insert */
1930 	raqp = &raq_head;
1931 	while (*raqp != NULL)
1932 		raqp = &((*raqp)->raq_next);
1933 	*raqp = raq;
1934 
1935 	/* Signal for poll loop */
1936 	sig_handler(255);
1937 }
1938 
1939 /*
1940  * Dequeue and process all queued advertisements.
1941  */
1942 static void
1943 loopback_ra_dequeue(void)
1944 {
1945 	struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT;
1946 	struct raq *raq;
1947 
1948 	if (debug & D_PKTIN)
1949 		logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n");
1950 
1951 	while ((raq = raq_head) != NULL) {
1952 		raq_head = raq->raq_next;
1953 		raq->raq_next = NULL;
1954 
1955 		if (debug & D_PKTIN) {
1956 			logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n",
1957 			    raq->raq_pi->pi_name);
1958 		}
1959 
1960 		incoming_ra(raq->raq_pi,
1961 		    (struct nd_router_advert *)raq->raq_packet,
1962 		    raq->raq_packetlen, &from, _B_TRUE);
1963 		free(raq->raq_packet);
1964 		free(raq);
1965 	}
1966 }
1967 
1968 
1969 static void
1970 usage(char *cmd)
1971 {
1972 	(void) fprintf(stderr,
1973 	    "usage: %s [ -adt ] [-f <config file>]\n", cmd);
1974 }
1975 
1976 int
1977 main(int argc, char *argv[])
1978 {
1979 	int i;
1980 	struct phyint *pi;
1981 	int c;
1982 	char *config_file = PATH_NDPD_CONF;
1983 	boolean_t file_required = _B_FALSE;
1984 
1985 	argv0 = argv;
1986 	srandom(gethostid());
1987 	(void) umask(0022);
1988 
1989 	while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) {
1990 		switch (c) {
1991 		case 'a':
1992 			/*
1993 			 * The StatelessAddrConf variable in ndpd.conf, if
1994 			 * present, will override this setting.
1995 			 */
1996 			ifdefaults[I_StatelessAddrConf].cf_value = 0;
1997 			break;
1998 		case 'd':
1999 			debug = D_ALL;
2000 			break;
2001 		case 'D':
2002 			i = strtol((char *)optarg, NULL, 0);
2003 			if (i == 0) {
2004 				(void) fprintf(stderr, "Bad debug flags: %s\n",
2005 				    (char *)optarg);
2006 				exit(1);
2007 			}
2008 			debug |= i;
2009 			break;
2010 		case 'n':
2011 			no_loopback = 1;
2012 			break;
2013 		case 'I':
2014 			show_ifs = 1;
2015 			break;
2016 		case 't':
2017 			debug |= D_PKTIN | D_PKTOUT | D_PKTBAD;
2018 			break;
2019 		case 'f':
2020 			config_file = (char *)optarg;
2021 			file_required = _B_TRUE;
2022 			break;
2023 		case '?':
2024 			usage(argv[0]);
2025 			exit(1);
2026 		}
2027 	}
2028 
2029 	if (parse_config(config_file, file_required) == -1)
2030 		exit(2);
2031 
2032 	if (show_ifs)
2033 		phyint_print_all();
2034 
2035 	if (debug == 0) {
2036 		initlog();
2037 	}
2038 
2039 	setup_eventpipe();
2040 	rtsock = setup_rtsock();
2041 	mibsock = setup_mibsock();
2042 	timer_init();
2043 	initifs(_B_TRUE);
2044 
2045 	check_daemonize();
2046 
2047 	for (;;) {
2048 		if (poll(pollfds, pollfd_num, -1) < 0) {
2049 			if (errno == EINTR)
2050 				continue;
2051 			logperror("main: poll");
2052 			exit(1);
2053 		}
2054 		for (i = 0; i < pollfd_num; i++) {
2055 			if (!(pollfds[i].revents & POLLIN))
2056 				continue;
2057 			if (pollfds[i].fd == eventpipe_read) {
2058 				in_signal(eventpipe_read);
2059 				break;
2060 			}
2061 			if (pollfds[i].fd == rtsock) {
2062 				process_rtsock(rtsock);
2063 				break;
2064 			}
2065 			if (pollfds[i].fd == mibsock) {
2066 				process_mibsock(mibsock);
2067 				break;
2068 			}
2069 			/*
2070 			 * Run timer routine to advance clock if more than
2071 			 * half a second since the clock was advanced.
2072 			 * This limits CPU usage under severe packet
2073 			 * arrival rates but it creates a slight inaccuracy
2074 			 * in the timer mechanism.
2075 			 */
2076 			conditional_run_timeouts(500U);
2077 			for (pi = phyints; pi != NULL; pi = pi->pi_next) {
2078 				if (pollfds[i].fd == pi->pi_sock) {
2079 					in_data(pi);
2080 					break;
2081 				}
2082 			}
2083 		}
2084 	}
2085 	/* NOTREACHED */
2086 	return (0);
2087 }
2088 
2089 /*
2090  * LOGGER
2091  */
2092 
2093 static boolean_t logging = _B_FALSE;
2094 
2095 static void
2096 initlog(void)
2097 {
2098 	logging = _B_TRUE;
2099 	openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON);
2100 }
2101 
2102 /* Print the date/time without a trailing carridge return */
2103 static void
2104 fprintdate(FILE *file)
2105 {
2106 	char buf[BUFSIZ];
2107 	struct tm tms;
2108 	time_t now;
2109 
2110 	now = time(NULL);
2111 	(void) localtime_r(&now, &tms);
2112 	(void) strftime(buf, sizeof (buf), "%h %d %X", &tms);
2113 	(void) fprintf(file, "%s ", buf);
2114 }
2115 
2116 /* PRINTFLIKE2 */
2117 void
2118 logmsg(int level, const char *fmt, ...)
2119 {
2120 	va_list ap;
2121 	va_start(ap, fmt);
2122 
2123 	if (logging) {
2124 		vsyslog(level, fmt, ap);
2125 	} else {
2126 		fprintdate(stderr);
2127 		(void) vfprintf(stderr, fmt, ap);
2128 	}
2129 	va_end(ap);
2130 }
2131 
2132 void
2133 logperror(const char *str)
2134 {
2135 	if (logging) {
2136 		syslog(LOG_ERR, "%s: %m\n", str);
2137 	} else {
2138 		fprintdate(stderr);
2139 		(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
2140 	}
2141 }
2142 
2143 void
2144 logperror_pi(const struct phyint *pi, const char *str)
2145 {
2146 	if (logging) {
2147 		syslog(LOG_ERR, "%s (interface %s): %m\n",
2148 		    str, pi->pi_name);
2149 	} else {
2150 		fprintdate(stderr);
2151 		(void) fprintf(stderr, "%s (interface %s): %s\n",
2152 		    str, pi->pi_name, strerror(errno));
2153 	}
2154 }
2155 
2156 void
2157 logperror_pr(const struct prefix *pr, const char *str)
2158 {
2159 	if (logging) {
2160 		syslog(LOG_ERR, "%s (prefix %s if %s): %m\n",
2161 		    str, pr->pr_name, pr->pr_physical->pi_name);
2162 	} else {
2163 		fprintdate(stderr);
2164 		(void) fprintf(stderr, "%s (prefix %s if %s): %s\n",
2165 		    str, pr->pr_name, pr->pr_physical->pi_name,
2166 		    strerror(errno));
2167 	}
2168 }
2169