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