xref: /titanic_44/usr/src/cmd/cmd-inet/usr.lib/in.mpathd/mpd_main.c (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "mpd_defs.h"
29 #include "mpd_tables.h"
30 
31 int debug = 0;				/* Debug flag */
32 static int pollfd_num = 0;		/* Num. of poll descriptors */
33 static struct pollfd *pollfds = NULL;	/* Array of poll descriptors */
34 
35 					/* All times below in ms */
36 int	user_failure_detection_time;	/* user specified failure detection */
37 					/* time (fdt) */
38 int	user_probe_interval;		/* derived from user specified fdt */
39 
40 static int	rtsock_v4;		/* AF_INET routing socket */
41 static int	rtsock_v6;		/* AF_INET6 routing socket */
42 int	ifsock_v4 = -1;			/* IPv4 socket for ioctls  */
43 int	ifsock_v6 = -1;			/* IPv6 socket for ioctls  */
44 static int	lsock_v4;		/* Listen socket to detect mpathd */
45 static int	lsock_v6;		/* Listen socket to detect mpathd */
46 static int	mibfd = -1;		/* fd to get mib info */
47 static boolean_t force_mcast = _B_FALSE; /* Only for test purposes */
48 
49 boolean_t	full_scan_required = _B_FALSE;
50 static uint_t	last_initifs_time;	/* Time when initifs was last run */
51 static	char **argv0;			/* Saved for re-exec on SIGHUP */
52 boolean_t handle_link_notifications = _B_TRUE;
53 
54 static void	initlog(void);
55 static void	run_timeouts(void);
56 static void	initifs(void);
57 static void	check_if_removed(struct phyint_instance *pii);
58 static void	select_test_ifs(void);
59 static void	ire_process_v4(mib2_ipRouteEntry_t *buf, size_t len);
60 static void	ire_process_v6(mib2_ipv6RouteEntry_t *buf, size_t len);
61 static void	router_add_v4(mib2_ipRouteEntry_t *rp1,
62     struct in_addr nexthop_v4);
63 static void	router_add_v6(mib2_ipv6RouteEntry_t *rp1,
64     struct in6_addr nexthop_v6);
65 static void	router_add_common(int af, char *ifname,
66     struct in6_addr nexthop);
67 static void	init_router_targets();
68 static void	cleanup(void);
69 static int	setup_listener(int af);
70 static void	check_config(void);
71 static void	check_testconfig(void);
72 static void	check_addr_unique(struct phyint_instance *,
73     struct sockaddr_storage *);
74 static void	init_host_targets(void);
75 static void	dup_host_targets(struct phyint_instance *desired_pii);
76 static void	loopback_cmd(int sock, int family);
77 static int	poll_remove(int fd);
78 static boolean_t daemonize(void);
79 static int	closefunc(void *, int);
80 static unsigned int process_cmd(int newfd, union mi_commands *mpi);
81 static unsigned int process_query(int fd, mi_query_t *miq);
82 static unsigned int send_groupinfo(int fd, ipmp_groupinfo_t *grinfop);
83 static unsigned int send_grouplist(int fd, ipmp_grouplist_t *grlistp);
84 static unsigned int send_ifinfo(int fd, ipmp_ifinfo_t *ifinfop);
85 static unsigned int send_result(int fd, unsigned int error, int syserror);
86 
87 struct local_addr *laddr_list = NULL;
88 
89 /*
90  * Return the current time in milliseconds (from an arbitrary reference)
91  * truncated to fit into an int. Truncation is ok since we are interested
92  * only in differences and not the absolute values.
93  */
94 uint_t
95 getcurrenttime(void)
96 {
97 	uint_t	cur_time;	/* In ms */
98 
99 	/*
100 	 * Use of a non-user-adjustable source of time is
101 	 * required. However millisecond precision is sufficient.
102 	 * divide by 10^6
103 	 */
104 	cur_time = (uint_t)(gethrtime() / 1000000LL);
105 	return (cur_time);
106 }
107 
108 uint64_t
109 getcurrentsec(void)
110 {
111 	return (gethrtime() / NANOSEC);
112 }
113 
114 /*
115  * Add fd to the set being polled. Returns 0 if ok; -1 if failed.
116  */
117 int
118 poll_add(int fd)
119 {
120 	int i;
121 	int new_num;
122 	struct pollfd *newfds;
123 retry:
124 	/* Check if already present */
125 	for (i = 0; i < pollfd_num; i++) {
126 		if (pollfds[i].fd == fd)
127 			return (0);
128 	}
129 	/* Check for empty spot already present */
130 	for (i = 0; i < pollfd_num; i++) {
131 		if (pollfds[i].fd == -1) {
132 			pollfds[i].fd = fd;
133 			return (0);
134 		}
135 	}
136 
137 	/* Allocate space for 32 more fds and initialize to -1 */
138 	new_num = pollfd_num + 32;
139 	newfds = realloc(pollfds, new_num * sizeof (struct pollfd));
140 	if (newfds == NULL) {
141 		logperror("poll_add: realloc");
142 		return (-1);
143 	}
144 	for (i = pollfd_num; i < new_num; i++) {
145 		newfds[i].fd = -1;
146 		newfds[i].events = POLLIN;
147 	}
148 	pollfd_num = new_num;
149 	pollfds = newfds;
150 	goto retry;
151 }
152 
153 /*
154  * Remove fd from the set being polled. Returns 0 if ok; -1 if failed.
155  */
156 static int
157 poll_remove(int fd)
158 {
159 	int i;
160 
161 	/* Check if already present */
162 	for (i = 0; i < pollfd_num; i++) {
163 		if (pollfds[i].fd == fd) {
164 			pollfds[i].fd = -1;
165 			return (0);
166 		}
167 	}
168 	return (-1);
169 }
170 
171 /*
172  * Extract information about the phyint instance. If the phyint instance still
173  * exists in the kernel then set pii_in_use, else clear it. check_if_removed()
174  * will use it to detect phyint instances that don't exist any longer and
175  * remove them, from our database of phyint instances.
176  * Return value:
177  *	returns true if the phyint instance exists in the kernel,
178  *	returns false otherwise
179  */
180 static boolean_t
181 pii_process(int af, char *name, struct phyint_instance **pii_p)
182 {
183 	int err;
184 	struct phyint_instance *pii;
185 	struct phyint_instance *pii_other;
186 
187 	if (debug & D_PHYINT)
188 		logdebug("pii_process(%s %s)\n", AF_STR(af), name);
189 
190 	pii = phyint_inst_lookup(af, name);
191 	if (pii == NULL) {
192 		/*
193 		 * Phyint instance does not exist in our tables,
194 		 * create new phyint instance
195 		 */
196 		pii = phyint_inst_init_from_k(af, name);
197 	} else {
198 		/* Phyint exists in our tables */
199 		err = phyint_inst_update_from_k(pii);
200 
201 		switch (err) {
202 		case PI_IOCTL_ERROR:
203 			/* Some ioctl error. don't change anything */
204 			pii->pii_in_use = 1;
205 			break;
206 
207 		case PI_GROUP_CHANGED:
208 			/*
209 			 * The phyint has changed group.
210 			 */
211 			restore_phyint(pii->pii_phyint);
212 			/* FALLTHRU */
213 
214 		case PI_IFINDEX_CHANGED:
215 			/*
216 			 * Interface index has changed. Delete and
217 			 * recreate the phyint as it is quite likely
218 			 * the interface has been unplumbed and replumbed.
219 			 */
220 			pii_other = phyint_inst_other(pii);
221 			if (pii_other != NULL)
222 				phyint_inst_delete(pii_other);
223 			phyint_inst_delete(pii);
224 			pii = phyint_inst_init_from_k(af, name);
225 			break;
226 
227 		case PI_DELETED:
228 			/* Phyint instance has disappeared from kernel */
229 			pii->pii_in_use = 0;
230 			break;
231 
232 		case PI_OK:
233 			/* Phyint instance exists and is fine */
234 			pii->pii_in_use = 1;
235 			break;
236 
237 		default:
238 			/* Unknown status */
239 			logerr("pii_process: Unknown status %d\n", err);
240 			break;
241 		}
242 	}
243 
244 	*pii_p = pii;
245 	if (pii != NULL)
246 		return (pii->pii_in_use ? _B_TRUE : _B_FALSE);
247 	else
248 		return (_B_FALSE);
249 }
250 
251 /*
252  * This phyint is leaving the group. Try to restore the phyint to its
253  * initial state. Return the addresses that belong to other group members,
254  * to the group, and take back any addresses owned by this phyint
255  */
256 void
257 restore_phyint(struct phyint *pi)
258 {
259 	if (pi->pi_group == phyint_anongroup)
260 		return;
261 
262 	/*
263 	 * Move everthing to some other member in the group.
264 	 * The phyint has changed group in the kernel. But we
265 	 * have yet to do it in our tables.
266 	 */
267 	if (!pi->pi_empty)
268 		(void) try_failover(pi, FAILOVER_TO_ANY);
269 	/*
270 	 * Move all addresses owned by 'pi' back to pi, from each
271 	 * of the other members of the group
272 	 */
273 	(void) try_failback(pi);
274 }
275 
276 /*
277  * Scan all interfaces to detect changes as well as new and deleted interfaces
278  */
279 static void
280 initifs()
281 {
282 	int	n;
283 	int	af;
284 	char	*cp;
285 	char	*buf;
286 	int	numifs;
287 	struct lifnum	lifn;
288 	struct lifconf	lifc;
289 	struct lifreq	*lifr;
290 	struct logint	*li;
291 	struct phyint_instance *pii;
292 	struct phyint_instance *next_pii;
293 	char	pi_name[LIFNAMSIZ + 1];
294 	boolean_t exists;
295 	struct phyint	*pi;
296 	struct local_addr *next;
297 
298 	if (debug & D_PHYINT)
299 		logdebug("initifs: Scanning interfaces\n");
300 
301 	last_initifs_time = getcurrenttime();
302 
303 	/*
304 	 * Free the laddr_list before collecting the local addresses.
305 	 */
306 	while (laddr_list != NULL) {
307 		next = laddr_list->next;
308 		free(laddr_list);
309 		laddr_list = next;
310 	}
311 
312 	/*
313 	 * Mark the interfaces so that we can find phyints and logints
314 	 * which have disappeared from the kernel. pii_process() and
315 	 * logint_init_from_k() will set {pii,li}_in_use when they find
316 	 * the interface in the kernel. Also, clear dupaddr bit on probe
317 	 * logint. check_addr_unique() will set the dupaddr bit on the
318 	 * probe logint, if the testaddress is not unique.
319 	 */
320 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
321 		pii->pii_in_use = 0;
322 		for (li = pii->pii_logint; li != NULL; li = li->li_next) {
323 			li->li_in_use = 0;
324 			if (pii->pii_probe_logint == li)
325 				li->li_dupaddr = 0;
326 		}
327 	}
328 
329 	lifn.lifn_family = AF_UNSPEC;
330 	lifn.lifn_flags = LIFC_ALLZONES;
331 	if (ioctl(ifsock_v4, SIOCGLIFNUM, (char *)&lifn) < 0) {
332 		logperror("initifs: ioctl (get interface numbers)");
333 		return;
334 	}
335 	numifs = lifn.lifn_count;
336 
337 	buf = (char *)calloc(numifs, sizeof (struct lifreq));
338 	if (buf == NULL) {
339 		logperror("initifs: calloc");
340 		return;
341 	}
342 
343 	lifc.lifc_family = AF_UNSPEC;
344 	lifc.lifc_flags = LIFC_ALLZONES;
345 	lifc.lifc_len = numifs * sizeof (struct lifreq);
346 	lifc.lifc_buf = buf;
347 
348 	if (ioctl(ifsock_v4, SIOCGLIFCONF, (char *)&lifc) < 0) {
349 		/*
350 		 * EINVAL is commonly encountered, when things change
351 		 * underneath us rapidly, (eg. at boot, when new interfaces
352 		 * are plumbed successively) and the kernel finds the buffer
353 		 * size we passed as too small. We will retry again
354 		 * when we see the next routing socket msg, or at worst after
355 		 * IF_SCAN_INTERVAL ms.
356 		 */
357 		if (errno != EINVAL) {
358 			logperror("initifs: ioctl"
359 			    " (get interface configuration)");
360 		}
361 		free(buf);
362 		return;
363 	}
364 
365 	lifr = (struct lifreq *)lifc.lifc_req;
366 
367 	/*
368 	 * For each lifreq returned by SIOGGLIFCONF, call pii_process()
369 	 * and get the state of the corresponding phyint_instance. If it is
370 	 * successful, then call logint_init_from_k() to get the state of the
371 	 * logint.
372 	 */
373 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++) {
374 		int	sockfd;
375 		struct local_addr	*taddr;
376 		struct sockaddr_in	*sin;
377 		struct sockaddr_in6	*sin6;
378 		struct lifreq	lifreq;
379 
380 		af = lifr->lifr_addr.ss_family;
381 
382 		/*
383 		 * Collect all local addresses.
384 		 */
385 		sockfd = (af == AF_INET) ? ifsock_v4 : ifsock_v6;
386 		(void) memset(&lifreq, 0, sizeof (lifreq));
387 		(void) strlcpy(lifreq.lifr_name, lifr->lifr_name,
388 		    sizeof (lifreq.lifr_name));
389 
390 		if (ioctl(sockfd, SIOCGLIFFLAGS, &lifreq) == -1) {
391 			if (errno != ENXIO)
392 				logperror("initifs: ioctl (SIOCGLIFFLAGS)");
393 			continue;
394 		}
395 
396 		/*
397 		 * Add the interface address to laddr_list.
398 		 * Another node might have the same IP address which is up.
399 		 * In that case, it is appropriate  to use the address as a
400 		 * target, even though it is also configured (but not up) on
401 		 * the local system.
402 		 * Hence,the interface address is not added to laddr_list
403 		 * unless it is IFF_UP.
404 		 */
405 		if (lifreq.lifr_flags & IFF_UP) {
406 			taddr = malloc(sizeof (struct local_addr));
407 			if (taddr == NULL) {
408 				logperror("initifs: malloc");
409 				continue;
410 			}
411 			if (af == AF_INET) {
412 				sin = (struct sockaddr_in *)&lifr->lifr_addr;
413 				IN6_INADDR_TO_V4MAPPED(&sin->sin_addr,
414 				    &taddr->addr);
415 			} else {
416 				sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
417 				taddr->addr = sin6->sin6_addr;
418 			}
419 			taddr->next = laddr_list;
420 			laddr_list = taddr;
421 		}
422 
423 		/*
424 		 * Need to pass a phyint name to pii_process. Insert the
425 		 * null where the ':' IF_SEPARATOR is found in the logical
426 		 * name.
427 		 */
428 		(void) strlcpy(pi_name, lifr->lifr_name, sizeof (pi_name));
429 		if ((cp = strchr(pi_name, IF_SEPARATOR)) != NULL)
430 			*cp = '\0';
431 
432 		exists = pii_process(af, pi_name, &pii);
433 		if (exists) {
434 			/* The phyint is fine. So process the logint */
435 			logint_init_from_k(pii, lifr->lifr_name);
436 			check_addr_unique(pii, &lifr->lifr_addr);
437 		}
438 
439 	}
440 
441 	free(buf);
442 
443 	/*
444 	 * Scan for phyints and logints that have disappeared from the
445 	 * kernel, and delete them.
446 	 */
447 	for (pii = phyint_instances; pii != NULL; pii = next_pii) {
448 		next_pii = pii->pii_next;
449 		check_if_removed(pii);
450 	}
451 
452 	/*
453 	 * Select a test address for sending probes on each phyint instance
454 	 */
455 	select_test_ifs();
456 
457 	/*
458 	 * Handle link up/down notifications from the NICs.
459 	 */
460 	process_link_state_changes();
461 
462 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
463 		/*
464 		 * If this is a case of group failure, we don't have much
465 		 * to do until the group recovers again.
466 		 */
467 		if (GROUP_FAILED(pi->pi_group))
468 			continue;
469 
470 		/*
471 		 * Try/Retry any pending failovers / failbacks, that did not
472 		 * not complete, or that could not be initiated previously.
473 		 * This implements the 3 invariants described in the big block
474 		 * comment at the beginning of probe.c
475 		 */
476 		if (pi->pi_flags & IFF_INACTIVE) {
477 			if (!pi->pi_empty && (pi->pi_flags & IFF_STANDBY))
478 				(void) try_failover(pi, FAILOVER_TO_NONSTANDBY);
479 		} else {
480 			struct phyint_instance *pii;
481 
482 			/*
483 			 * Skip LINK UP interfaces which are not capable
484 			 * of probing.
485 			 */
486 			pii = pi->pi_v4;
487 			if (pii == NULL ||
488 			    (LINK_UP(pi) && !PROBE_CAPABLE(pii))) {
489 				pii = pi->pi_v6;
490 				if (pii == NULL ||
491 				    (LINK_UP(pi) && !PROBE_CAPABLE(pii)))
492 					continue;
493 			}
494 
495 			/*
496 			 * It is possible that the phyint has started
497 			 * receiving packets, after it has been marked
498 			 * PI_FAILED. Don't initiate failover, if the
499 			 * phyint has started recovering. failure_state()
500 			 * captures this check. A similar logic is used
501 			 * for failback/repair case.
502 			 */
503 			if (pi->pi_state == PI_FAILED && !pi->pi_empty &&
504 			    (failure_state(pii) == PHYINT_FAILURE)) {
505 				(void) try_failover(pi, FAILOVER_NORMAL);
506 			} else if (pi->pi_state == PI_RUNNING && !pi->pi_full) {
507 				if (try_failback(pi) != IPMP_FAILURE) {
508 					(void) change_lif_flags(pi, IFF_FAILED,
509 					    _B_FALSE);
510 					/* Per state diagram */
511 					pi->pi_empty = 0;
512 				}
513 			}
514 		}
515 	}
516 }
517 
518 /*
519  * Check that a given test address is unique across all of the interfaces in a
520  * group.  (e.g., IPv6 link-locals may not be inherently unique, and binding
521  * to such an (IFF_NOFAILOVER) address can produce unexpected results.)
522  * Any issues will be reported by check_testconfig().
523  */
524 static void
525 check_addr_unique(struct phyint_instance *ourpii, struct sockaddr_storage *ss)
526 {
527 	struct phyint		*pi;
528 	struct phyint_group	*pg;
529 	struct in6_addr		addr;
530 	struct phyint_instance	*pii;
531 	struct sockaddr_in	*sin;
532 
533 	if (ss->ss_family == AF_INET) {
534 		sin = (struct sockaddr_in *)ss;
535 		IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &addr);
536 	} else {
537 		assert(ss->ss_family == AF_INET6);
538 		addr = ((struct sockaddr_in6 *)ss)->sin6_addr;
539 	}
540 
541 	/*
542 	 * For anonymous groups, every interface is assumed to be on its own
543 	 * link, so there is no chance of overlapping addresses.
544 	 */
545 	pg = ourpii->pii_phyint->pi_group;
546 	if (pg == phyint_anongroup)
547 		return;
548 
549 	/*
550 	 * Walk the list of phyint instances in the group and check for test
551 	 * addresses matching ours.  Of course, we skip ourself.
552 	 */
553 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
554 		pii = PHYINT_INSTANCE(pi, ss->ss_family);
555 		if (pii == NULL || pii == ourpii ||
556 		    pii->pii_probe_logint == NULL)
557 			continue;
558 
559 		/*
560 		 * If this test address is not unique, set the dupaddr bit.
561 		 */
562 		if (IN6_ARE_ADDR_EQUAL(&addr, &pii->pii_probe_logint->li_addr))
563 			pii->pii_probe_logint->li_dupaddr = 1;
564 	}
565 }
566 
567 /*
568  * Stop probing an interface.  Called when an interface is offlined.
569  * The probe socket is closed on each interface instance, and the
570  * interface state set to PI_OFFLINE.
571  */
572 static void
573 stop_probing(struct phyint *pi)
574 {
575 	struct phyint_instance *pii;
576 
577 	pii = pi->pi_v4;
578 	if (pii != NULL) {
579 		if (pii->pii_probe_sock != -1)
580 			close_probe_socket(pii, _B_TRUE);
581 		pii->pii_probe_logint = NULL;
582 	}
583 
584 	pii = pi->pi_v6;
585 	if (pii != NULL) {
586 		if (pii->pii_probe_sock != -1)
587 			close_probe_socket(pii, _B_TRUE);
588 		pii->pii_probe_logint = NULL;
589 	}
590 
591 	phyint_chstate(pi, PI_OFFLINE);
592 }
593 
594 enum { BAD_TESTFLAGS, OK_TESTFLAGS, BEST_TESTFLAGS };
595 
596 /*
597  * Rate the provided test flags.  By definition, IFF_NOFAILOVER must be set.
598  * IFF_UP must also be set so that the associated address can be used as a
599  * source address.  Further, we must be able to exchange packets with local
600  * destinations, so IFF_NOXMIT and IFF_NOLOCAL must be clear.  For historical
601  * reasons, we have a proclivity for IFF_DEPRECATED IPv4 test addresses.
602  */
603 static int
604 rate_testflags(uint64_t flags)
605 {
606 	if ((flags & (IFF_NOFAILOVER | IFF_UP)) != (IFF_NOFAILOVER | IFF_UP))
607 		return (BAD_TESTFLAGS);
608 
609 	if ((flags & (IFF_NOXMIT | IFF_NOLOCAL)) != 0)
610 		return (BAD_TESTFLAGS);
611 
612 	if ((flags & (IFF_IPV6 | IFF_DEPRECATED)) == IFF_DEPRECATED)
613 		return (BEST_TESTFLAGS);
614 
615 	if ((flags & (IFF_IPV6 | IFF_DEPRECATED)) == IFF_IPV6)
616 		return (BEST_TESTFLAGS);
617 
618 	return (OK_TESTFLAGS);
619 }
620 
621 /*
622  * Attempt to select a test address for each phyint instance.
623  * Call phyint_inst_sockinit() to complete the initializations.
624  */
625 static void
626 select_test_ifs(void)
627 {
628 	struct phyint		*pi;
629 	struct phyint_instance	*pii;
630 	struct phyint_instance	*next_pii;
631 	struct logint		*li;
632 	struct logint  		*probe_logint;
633 	boolean_t		target_scan_reqd = _B_FALSE;
634 	struct target		*tg;
635 	int			rating;
636 
637 	if (debug & D_PHYINT)
638 		logdebug("select_test_ifs\n");
639 
640 	/*
641 	 * For each phyint instance, do the test address selection
642 	 */
643 	for (pii = phyint_instances; pii != NULL; pii = next_pii) {
644 		next_pii = pii->pii_next;
645 		probe_logint = NULL;
646 
647 		/*
648 		 * An interface that is offline, should not be probed.
649 		 * Offline interfaces should always in PI_OFFLINE state,
650 		 * unless some other entity has set the offline flag.
651 		 */
652 		if (pii->pii_phyint->pi_flags & IFF_OFFLINE) {
653 			if (pii->pii_phyint->pi_state != PI_OFFLINE) {
654 				logerr("shouldn't be probing offline"
655 				    " interface %s (state is: %u)."
656 				    " Stopping probes.\n",
657 				    pii->pii_phyint->pi_name,
658 				    pii->pii_phyint->pi_state);
659 				stop_probing(pii->pii_phyint);
660 			}
661 			continue;
662 		}
663 
664 		li = pii->pii_probe_logint;
665 		if (li != NULL) {
666 			/*
667 			 * We've already got a test address; only proceed
668 			 * if it's suboptimal.
669 			 */
670 			if (rate_testflags(li->li_flags) == BEST_TESTFLAGS)
671 				continue;
672 		}
673 
674 		/*
675 		 * Walk the logints of this phyint instance, and select
676 		 * the best available test address
677 		 */
678 		for (li = pii->pii_logint; li != NULL; li = li->li_next) {
679 			/*
680 			 * Skip 0.0.0.0 addresses, as those are never
681 			 * actually usable.
682 			 */
683 			if (pii->pii_af == AF_INET &&
684 			    IN6_IS_ADDR_V4MAPPED_ANY(&li->li_addr))
685 				continue;
686 
687 			/*
688 			 * Skip any IPv6 logints that are not link-local,
689 			 * since we should always have a link-local address
690 			 * anyway and in6_data() expects link-local replies.
691 			 */
692 			if (pii->pii_af == AF_INET6 &&
693 			    !IN6_IS_ADDR_LINKLOCAL(&li->li_addr))
694 				continue;
695 
696 			/*
697 			 * Rate the testflags. If we've found an optimal
698 			 * match, then break out; otherwise, record the most
699 			 * recent OK one.
700 			 */
701 			rating = rate_testflags(li->li_flags);
702 			if (rating == BAD_TESTFLAGS)
703 				continue;
704 
705 			probe_logint = li;
706 			if (rating == BEST_TESTFLAGS)
707 				break;
708 		}
709 
710 		/*
711 		 * If the probe logint has changed, ditch the old one.
712 		 */
713 		if (pii->pii_probe_logint != NULL &&
714 		    pii->pii_probe_logint != probe_logint) {
715 			if (pii->pii_probe_sock != -1)
716 				close_probe_socket(pii, _B_TRUE);
717 			pii->pii_probe_logint = NULL;
718 		}
719 
720 		if (probe_logint == NULL) {
721 			/*
722 			 * We don't have a test address; zero out the probe
723 			 * stats array since it is no longer relevant.
724 			 * Optimize by checking if it is already zeroed out.
725 			 */
726 			int pr_ndx;
727 
728 			pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
729 			if (pii->pii_probes[pr_ndx].pr_status != PR_UNUSED) {
730 				clear_pii_probe_stats(pii);
731 				reset_crtt_all(pii->pii_phyint);
732 			}
733 			continue;
734 		} else if (probe_logint == pii->pii_probe_logint) {
735 			/*
736 			 * If we didn't find any new test addr, go to the
737 			 * next phyint.
738 			 */
739 			continue;
740 		}
741 
742 		/*
743 		 * The phyint is either being assigned a new testaddr
744 		 * or is being assigned a testaddr for the 1st time.
745 		 * Need to initialize the phyint socket
746 		 */
747 		pii->pii_probe_logint = probe_logint;
748 		if (!phyint_inst_sockinit(pii)) {
749 			if (debug & D_PHYINT) {
750 				logdebug("select_test_ifs: "
751 				    "phyint_sockinit failed\n");
752 			}
753 			phyint_inst_delete(pii);
754 			continue;
755 		}
756 
757 		/*
758 		 * This phyint instance is now enabled for probes; this
759 		 * impacts our state machine in two ways:
760 		 *
761 		 * 1. If we're probe *capable* as well (i.e., we have
762 		 *    probe targets) and the interface is in PI_NOTARGETS,
763 		 *    then transition to PI_RUNNING.
764 		 *
765 		 * 2. If we're not probe capable, and the other phyint
766 		 *    instance is also not probe capable, and we were in
767 		 *    PI_RUNNING, then transition to PI_NOTARGETS.
768 		 *
769 		 * Also see the state diagram in mpd_probe.c.
770 		 */
771 		if (PROBE_CAPABLE(pii)) {
772 			if (pii->pii_phyint->pi_state == PI_NOTARGETS)
773 				phyint_chstate(pii->pii_phyint, PI_RUNNING);
774 		} else if (!PROBE_CAPABLE(phyint_inst_other(pii))) {
775 			if (pii->pii_phyint->pi_state == PI_RUNNING)
776 				phyint_chstate(pii->pii_phyint, PI_NOTARGETS);
777 		}
778 
779 		if (pii->pii_phyint->pi_flags & IFF_POINTOPOINT) {
780 			tg = pii->pii_targets;
781 			if (tg != NULL)
782 				target_delete(tg);
783 			assert(pii->pii_targets == NULL);
784 			assert(pii->pii_target_next == NULL);
785 			assert(pii->pii_ntargets == 0);
786 			target_create(pii, probe_logint->li_dstaddr,
787 			    _B_TRUE);
788 		}
789 
790 		/*
791 		 * If no targets are currently known for this phyint
792 		 * we need to call init_router_targets. Since
793 		 * init_router_targets() initializes the list of targets
794 		 * for all phyints it is done below the loop.
795 		 */
796 		if (pii->pii_targets == NULL)
797 			target_scan_reqd = _B_TRUE;
798 
799 		/*
800 		 * Start the probe timer for this instance.
801 		 */
802 		if (!pii->pii_basetime_inited && PROBE_ENABLED(pii)) {
803 			start_timer(pii);
804 			pii->pii_basetime_inited = 1;
805 		}
806 	}
807 
808 	/*
809 	 * Check the interface list for any interfaces that are marked
810 	 * PI_FAILED but no longer enabled to send probes, and call
811 	 * phyint_check_for_repair() to see if the link now indicates that the
812 	 * interface should be repaired.  Also see the state diagram in
813 	 * mpd_probe.c.
814 	 */
815 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
816 		if (pi->pi_state == PI_FAILED &&
817 		    !PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6)) {
818 			phyint_check_for_repair(pi);
819 		}
820 	}
821 
822 	check_testconfig();
823 
824 	/*
825 	 * Try to populate the target list. init_router_targets populates
826 	 * the target list from the routing table. If our target list is
827 	 * still empty, init_host_targets adds host targets based on the
828 	 * host target list of other phyints in the group.
829 	 */
830 	if (target_scan_reqd) {
831 		init_router_targets();
832 		init_host_targets();
833 	}
834 }
835 
836 /*
837  * Check test address configuration, and log notices/errors if appropriate.
838  * Note that this function only logs pre-existing conditions (e.g., that
839  * probe-based failure detection is disabled).
840  */
841 static void
842 check_testconfig(void)
843 {
844 	struct phyint	*pi;
845 	struct logint  	*li;
846 	char		abuf[INET6_ADDRSTRLEN];
847 	int		pri;
848 
849 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
850 		if (pi->pi_flags & IFF_OFFLINE)
851 			continue;
852 
853 		if (PROBE_ENABLED(pi->pi_v4) || PROBE_ENABLED(pi->pi_v6)) {
854 			if (pi->pi_taddrmsg_printed ||
855 			    pi->pi_duptaddrmsg_printed) {
856 				if (pi->pi_duptaddrmsg_printed)
857 					pri = LOG_ERR;
858 				else
859 					pri = LOG_INFO;
860 				logmsg(pri, "Test address now configured on "
861 				    "interface %s; enabling probe-based "
862 				    "failure detection on it\n", pi->pi_name);
863 				pi->pi_taddrmsg_printed = 0;
864 				pi->pi_duptaddrmsg_printed = 0;
865 			}
866 			continue;
867 		}
868 
869 		li = NULL;
870 		if (pi->pi_v4 != NULL && pi->pi_v4->pii_probe_logint != NULL &&
871 		    pi->pi_v4->pii_probe_logint->li_dupaddr)
872 			li = pi->pi_v4->pii_probe_logint;
873 
874 		if (pi->pi_v6 != NULL && pi->pi_v6->pii_probe_logint != NULL &&
875 		    pi->pi_v6->pii_probe_logint->li_dupaddr)
876 			li = pi->pi_v6->pii_probe_logint;
877 
878 		if (li != NULL) {
879 			if (!pi->pi_duptaddrmsg_printed) {
880 				(void) pr_addr(li->li_phyint_inst->pii_af,
881 				    li->li_addr, abuf, sizeof (abuf));
882 				logerr("Test address %s is not unique in "
883 				    "group; disabling probe-based failure "
884 				    "detection on %s\n", abuf, pi->pi_name);
885 				pi->pi_duptaddrmsg_printed = 1;
886 			}
887 			continue;
888 		}
889 
890 		if (getcurrentsec() < pi->pi_taddrthresh)
891 			continue;
892 
893 		if (!pi->pi_taddrmsg_printed) {
894 			logtrace("No test address configured on interface %s; "
895 			    "disabling probe-based failure detection on it\n",
896 			    pi->pi_name);
897 			pi->pi_taddrmsg_printed = 1;
898 		}
899 	}
900 }
901 
902 /*
903  * Check phyint group configuration, to detect any inconsistencies,
904  * and log an error message. This is called from runtimeouts every
905  * 20 secs. But the error message is displayed once. If the
906  * consistency is resolved by the admin, a recovery message is displayed
907  * once.
908  */
909 static void
910 check_config(void)
911 {
912 	struct phyint_group *pg;
913 	struct phyint *pi;
914 	boolean_t v4_in_group;
915 	boolean_t v6_in_group;
916 
917 	/*
918 	 * All phyints of a group must be homogenous to ensure that
919 	 * failover or failback can be done. If any phyint in a group
920 	 * has IPv4 plumbed, check that all phyints have IPv4 plumbed.
921 	 * Do a similar check for IPv6.
922 	 */
923 	for (pg = phyint_groups; pg != NULL; pg = pg->pg_next) {
924 		if (pg == phyint_anongroup)
925 			continue;
926 
927 		v4_in_group = _B_FALSE;
928 		v6_in_group = _B_FALSE;
929 		/*
930 		 * 1st pass. Determine if at least 1 phyint in the group
931 		 * has IPv4 plumbed and if so set v4_in_group to true.
932 		 * Repeat similarly for IPv6.
933 		 */
934 		for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
935 			if (pi->pi_v4 != NULL)
936 				v4_in_group = _B_TRUE;
937 			if (pi->pi_v6 != NULL)
938 				v6_in_group = _B_TRUE;
939 		}
940 
941 		/*
942 		 * 2nd pass. If v4_in_group is true, check that phyint
943 		 * has IPv4 plumbed. Repeat similarly for IPv6. Print
944 		 * out a message the 1st time only.
945 		 */
946 		for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
947 			if (pi->pi_flags & IFF_OFFLINE)
948 				continue;
949 
950 			if (v4_in_group == _B_TRUE && pi->pi_v4 == NULL) {
951 				if (!pi->pi_cfgmsg_printed) {
952 					logerr("NIC %s of group %s is"
953 					    " not plumbed for IPv4 and may"
954 					    " affect failover capability\n",
955 					    pi->pi_name,
956 					    pi->pi_group->pg_name);
957 					pi->pi_cfgmsg_printed = 1;
958 				}
959 			} else if (v6_in_group == _B_TRUE &&
960 			    pi->pi_v6 == NULL) {
961 				if (!pi->pi_cfgmsg_printed) {
962 					logerr("NIC %s of group %s is"
963 					    " not plumbed for IPv6 and may"
964 					    " affect failover capability\n",
965 					    pi->pi_name,
966 					    pi->pi_group->pg_name);
967 					pi->pi_cfgmsg_printed = 1;
968 				}
969 			} else {
970 				/*
971 				 * The phyint matches the group configuration,
972 				 * if we have reached this point. If it was
973 				 * improperly configured earlier, log an
974 				 * error recovery message
975 				 */
976 				if (pi->pi_cfgmsg_printed) {
977 					logerr("NIC %s is now consistent with "
978 					    "group %s and failover capability "
979 					    "is restored\n", pi->pi_name,
980 					    pi->pi_group->pg_name);
981 					pi->pi_cfgmsg_printed = 0;
982 				}
983 			}
984 
985 		}
986 	}
987 }
988 
989 /*
990  * Timer mechanism using relative time (in milliseconds) from the
991  * previous timer event. Timers exceeding TIMER_INFINITY milliseconds
992  * will fire after TIMER_INFINITY milliseconds.
993  * Unsigned arithmetic note: We assume a 32-bit circular sequence space for
994  * time values. Hence 2 consecutive timer events cannot be spaced farther
995  * than 0x7fffffff. We call this TIMER_INFINITY, and it is the maximum value
996  * that can be passed for the delay parameter of timer_schedule()
997  */
998 static uint_t timer_next;	/* Currently scheduled timeout */
999 static boolean_t timer_active = _B_FALSE; /* SIGALRM has not yet occurred */
1000 
1001 static void
1002 timer_init(void)
1003 {
1004 	timer_next = getcurrenttime() + TIMER_INFINITY;
1005 	/*
1006 	 * The call to run_timeouts() will get the timer started
1007 	 * Since there are no phyints at this point, the timer will
1008 	 * be set for IF_SCAN_INTERVAL ms.
1009 	 */
1010 	run_timeouts();
1011 }
1012 
1013 /*
1014  * Make sure the next SIGALRM occurs delay milliseconds from the current
1015  * time if not earlier. We are interested only in time differences.
1016  */
1017 void
1018 timer_schedule(uint_t delay)
1019 {
1020 	uint_t now;
1021 	struct itimerval itimerval;
1022 
1023 	if (debug & D_TIMER)
1024 		logdebug("timer_schedule(%u)\n", delay);
1025 
1026 	assert(delay <= TIMER_INFINITY);
1027 
1028 	now = getcurrenttime();
1029 	if (delay == 0) {
1030 		/* Minimum allowed delay */
1031 		delay = 1;
1032 	}
1033 	/* Will this timer occur before the currently scheduled SIGALRM? */
1034 	if (timer_active && TIME_GE(now + delay, timer_next)) {
1035 		if (debug & D_TIMER) {
1036 			logdebug("timer_schedule(%u) - no action: "
1037 			    "now %u next %u\n", delay, now, timer_next);
1038 		}
1039 		return;
1040 	}
1041 	timer_next = now + delay;
1042 
1043 	itimerval.it_value.tv_sec = delay / 1000;
1044 	itimerval.it_value.tv_usec = (delay % 1000) * 1000;
1045 	itimerval.it_interval.tv_sec = 0;
1046 	itimerval.it_interval.tv_usec = 0;
1047 	if (debug & D_TIMER) {
1048 		logdebug("timer_schedule(%u): sec %ld usec %ld\n",
1049 		    delay, itimerval.it_value.tv_sec,
1050 		    itimerval.it_value.tv_usec);
1051 	}
1052 	timer_active = _B_TRUE;
1053 	if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) {
1054 		logperror("timer_schedule: setitimer");
1055 		exit(2);
1056 	}
1057 }
1058 
1059 /*
1060  * Timer has fired. Determine when the next timer event will occur by asking
1061  * all the timer routines. Should not be called from a timer routine.
1062  */
1063 static void
1064 run_timeouts(void)
1065 {
1066 	uint_t next;
1067 	uint_t next_event_time;
1068 	struct phyint_instance *pii;
1069 	struct phyint_instance *next_pii;
1070 	static boolean_t timeout_running;
1071 
1072 	/* assert that recursive timeouts don't happen. */
1073 	assert(!timeout_running);
1074 
1075 	timeout_running = _B_TRUE;
1076 
1077 	if (debug & D_TIMER)
1078 		logdebug("run_timeouts()\n");
1079 
1080 	if ((getcurrenttime() - last_initifs_time) > IF_SCAN_INTERVAL) {
1081 		initifs();
1082 		check_config();
1083 	}
1084 
1085 	next = TIMER_INFINITY;
1086 
1087 	for (pii = phyint_instances; pii != NULL; pii = next_pii) {
1088 		next_pii = pii->pii_next;
1089 		next_event_time = phyint_inst_timer(pii);
1090 		if (next_event_time != TIMER_INFINITY && next_event_time < next)
1091 			next = next_event_time;
1092 
1093 		if (debug & D_TIMER) {
1094 			logdebug("run_timeouts(%s %s): next scheduled for"
1095 			    " this phyint inst %u, next scheduled global"
1096 			    " %u ms\n",
1097 			    AF_STR(pii->pii_af), pii->pii_phyint->pi_name,
1098 			    next_event_time, next);
1099 		}
1100 	}
1101 
1102 	/*
1103 	 * Make sure initifs() is called at least once every
1104 	 * IF_SCAN_INTERVAL, to make sure that we are in sync
1105 	 * with the kernel, in case we have missed any routing
1106 	 * socket messages.
1107 	 */
1108 	if (next > IF_SCAN_INTERVAL)
1109 		next = IF_SCAN_INTERVAL;
1110 
1111 	if (debug & D_TIMER)
1112 		logdebug("run_timeouts: %u ms\n", next);
1113 
1114 	timer_schedule(next);
1115 	timeout_running = _B_FALSE;
1116 }
1117 
1118 static int eventpipe_read = -1;	/* Used for synchronous signal delivery */
1119 static int eventpipe_write = -1;
1120 static boolean_t cleanup_started = _B_FALSE;
1121 				/* Don't write to eventpipe if in cleanup */
1122 /*
1123  * Ensure that signals are processed synchronously with the rest of
1124  * the code by just writing a one character signal number on the pipe.
1125  * The poll loop will pick this up and process the signal event.
1126  */
1127 static void
1128 sig_handler(int signo)
1129 {
1130 	uchar_t buf = (uchar_t)signo;
1131 
1132 	/*
1133 	 * Don't write to pipe if cleanup has already begun. cleanup()
1134 	 * might have closed the pipe already
1135 	 */
1136 	if (cleanup_started)
1137 		return;
1138 
1139 	if (eventpipe_write == -1) {
1140 		logerr("sig_handler: no pipe found\n");
1141 		return;
1142 	}
1143 	if (write(eventpipe_write, &buf, sizeof (buf)) < 0)
1144 		logperror("sig_handler: write");
1145 }
1146 
1147 extern struct probes_missed probes_missed;
1148 
1149 /*
1150  * Pick up a signal "byte" from the pipe and process it.
1151  */
1152 static void
1153 in_signal(int fd)
1154 {
1155 	uchar_t buf;
1156 	uint64_t  sent, acked, lost, unacked, unknown;
1157 	struct phyint_instance *pii;
1158 	int pr_ndx;
1159 
1160 	switch (read(fd, &buf, sizeof (buf))) {
1161 	case -1:
1162 		logperror("in_signal: read");
1163 		exit(1);
1164 		/* NOTREACHED */
1165 	case 1:
1166 		break;
1167 	case 0:
1168 		logerr("in_signal: read end of file\n");
1169 		exit(1);
1170 		/* NOTREACHED */
1171 	default:
1172 		logerr("in_signal: read > 1\n");
1173 		exit(1);
1174 	}
1175 
1176 	if (debug & D_TIMER)
1177 		logdebug("in_signal() got %d\n", buf);
1178 
1179 	switch (buf) {
1180 	case SIGALRM:
1181 		if (debug & D_TIMER) {
1182 			uint_t now = getcurrenttime();
1183 
1184 			logdebug("in_signal(SIGALRM) delta %u\n",
1185 			    now - timer_next);
1186 		}
1187 		timer_active = _B_FALSE;
1188 		run_timeouts();
1189 		break;
1190 	case SIGUSR1:
1191 		logdebug("Printing configuration:\n");
1192 		/* Print out the internal tables */
1193 		phyint_inst_print_all();
1194 
1195 		/*
1196 		 * Print out the accumulated statistics about missed
1197 		 * probes (happens due to scheduling delay).
1198 		 */
1199 		logerr("Missed sending total of %d probes spread over"
1200 		    " %d occurrences\n", probes_missed.pm_nprobes,
1201 		    probes_missed.pm_ntimes);
1202 
1203 		/*
1204 		 * Print out the accumulated statistics about probes
1205 		 * that were sent.
1206 		 */
1207 		for (pii = phyint_instances; pii != NULL;
1208 		    pii = pii->pii_next) {
1209 			unacked = 0;
1210 			acked = pii->pii_cum_stats.acked;
1211 			lost = pii->pii_cum_stats.lost;
1212 			sent = pii->pii_cum_stats.sent;
1213 			unknown = pii->pii_cum_stats.unknown;
1214 			for (pr_ndx = 0; pr_ndx < PROBE_STATS_COUNT; pr_ndx++) {
1215 				switch (pii->pii_probes[pr_ndx].pr_status) {
1216 				case PR_ACKED:
1217 					acked++;
1218 					break;
1219 				case PR_LOST:
1220 					lost++;
1221 					break;
1222 				case PR_UNACKED:
1223 					unacked++;
1224 					break;
1225 				}
1226 			}
1227 			logerr("\nProbe stats on (%s %s)\n"
1228 			    "Number of probes sent %lld\n"
1229 			    "Number of probe acks received %lld\n"
1230 			    "Number of probes/acks lost %lld\n"
1231 			    "Number of valid unacknowled probes %lld\n"
1232 			    "Number of ambiguous probe acks received %lld\n",
1233 			    AF_STR(pii->pii_af), pii->pii_name,
1234 			    sent, acked, lost, unacked, unknown);
1235 		}
1236 		break;
1237 	case SIGHUP:
1238 		logerr("SIGHUP: restart and reread config file\n");
1239 		cleanup();
1240 		(void) execv(argv0[0], argv0);
1241 		_exit(0177);
1242 		/* NOTREACHED */
1243 	case SIGINT:
1244 	case SIGTERM:
1245 	case SIGQUIT:
1246 		cleanup();
1247 		exit(0);
1248 		/* NOTREACHED */
1249 	default:
1250 		logerr("in_signal: unknown signal: %d\n", buf);
1251 	}
1252 }
1253 
1254 static void
1255 cleanup(void)
1256 {
1257 	struct phyint_instance *pii;
1258 	struct phyint_instance *next_pii;
1259 
1260 	/*
1261 	 * Make sure that we don't write to eventpipe in
1262 	 * sig_handler() if any signal notably SIGALRM,
1263 	 * occurs after we close the eventpipe descriptor below
1264 	 */
1265 	cleanup_started = _B_TRUE;
1266 
1267 	for (pii = phyint_instances; pii != NULL; pii = next_pii) {
1268 		next_pii = pii->pii_next;
1269 		phyint_inst_delete(pii);
1270 	}
1271 
1272 	(void) close(ifsock_v4);
1273 	(void) close(ifsock_v6);
1274 	(void) close(rtsock_v4);
1275 	(void) close(rtsock_v6);
1276 	(void) close(lsock_v4);
1277 	(void) close(lsock_v6);
1278 	(void) close(0);
1279 	(void) close(1);
1280 	(void) close(2);
1281 	(void) close(mibfd);
1282 	(void) close(eventpipe_read);
1283 	(void) close(eventpipe_write);
1284 }
1285 
1286 /*
1287  * Create pipe for signal delivery and set up signal handlers.
1288  */
1289 static void
1290 setup_eventpipe(void)
1291 {
1292 	int fds[2];
1293 	struct sigaction act;
1294 
1295 	if ((pipe(fds)) < 0) {
1296 		logperror("setup_eventpipe: pipe");
1297 		exit(1);
1298 	}
1299 	eventpipe_read = fds[0];
1300 	eventpipe_write = fds[1];
1301 	if (poll_add(eventpipe_read) == -1) {
1302 		exit(1);
1303 	}
1304 
1305 	act.sa_handler = sig_handler;
1306 	act.sa_flags = SA_RESTART;
1307 	(void) sigaction(SIGALRM, &act, NULL);
1308 
1309 	(void) sigset(SIGHUP, sig_handler);
1310 	(void) sigset(SIGUSR1, sig_handler);
1311 	(void) sigset(SIGTERM, sig_handler);
1312 	(void) sigset(SIGINT, sig_handler);
1313 	(void) sigset(SIGQUIT, sig_handler);
1314 }
1315 
1316 /*
1317  * Create a routing socket for receiving RTM_IFINFO messages.
1318  */
1319 static int
1320 setup_rtsock(int af)
1321 {
1322 	int	s;
1323 	int	flags;
1324 
1325 	s = socket(PF_ROUTE, SOCK_RAW, af);
1326 	if (s == -1) {
1327 		logperror("setup_rtsock: socket PF_ROUTE");
1328 		exit(1);
1329 	}
1330 	if ((flags = fcntl(s, F_GETFL, 0)) < 0) {
1331 		logperror("setup_rtsock: fcntl F_GETFL");
1332 		(void) close(s);
1333 		exit(1);
1334 	}
1335 	if ((fcntl(s, F_SETFL, flags | O_NONBLOCK)) < 0) {
1336 		logperror("setup_rtsock: fcntl F_SETFL");
1337 		(void) close(s);
1338 		exit(1);
1339 	}
1340 	if (poll_add(s) == -1) {
1341 		(void) close(s);
1342 		exit(1);
1343 	}
1344 	return (s);
1345 }
1346 
1347 /*
1348  * Process an RTM_IFINFO message received on a routing socket.
1349  * The return value indicates whether a full interface scan is required.
1350  * Link up/down notifications from the NICs are reflected in the
1351  * IFF_RUNNING flag.
1352  * If just the state of the IFF_RUNNING interface flag has changed, a
1353  * a full interface scan isn't required.
1354  */
1355 static boolean_t
1356 process_rtm_ifinfo(if_msghdr_t *ifm, int type)
1357 {
1358 	struct sockaddr_dl *sdl;
1359 	struct phyint *pi;
1360 	uint64_t old_flags;
1361 	struct phyint_instance *pii;
1362 
1363 	assert(ifm->ifm_type == RTM_IFINFO && ifm->ifm_addrs == RTA_IFP);
1364 
1365 	/*
1366 	 * Although the sockaddr_dl structure is directly after the
1367 	 * if_msghdr_t structure. At the time of writing, the size of the
1368 	 * if_msghdr_t structure is different on 32 and 64 bit kernels, due
1369 	 * to the presence of a timeval structure, which contains longs,
1370 	 * in the if_data structure.  Anyway, we know where the message ends,
1371 	 * so we work backwards to get the start of the sockaddr_dl structure.
1372 	 */
1373 	/*LINTED*/
1374 	sdl = (struct sockaddr_dl *)((char *)ifm + ifm->ifm_msglen -
1375 	    sizeof (struct sockaddr_dl));
1376 
1377 	assert(sdl->sdl_family == AF_LINK);
1378 
1379 	/*
1380 	 * The interface name is in sdl_data.
1381 	 * RTM_IFINFO messages are only generated for logical interface
1382 	 * zero, so there is no colon and logical interface number to
1383 	 * strip from the name.	 The name is not null terminated, but
1384 	 * there should be enough space in sdl_data to add the null.
1385 	 */
1386 	if (sdl->sdl_nlen >= sizeof (sdl->sdl_data)) {
1387 		if (debug & D_LINKNOTE)
1388 			logdebug("process_rtm_ifinfo: phyint name too long\n");
1389 		return (_B_TRUE);
1390 	}
1391 	sdl->sdl_data[sdl->sdl_nlen] = 0;
1392 
1393 	pi = phyint_lookup(sdl->sdl_data);
1394 	if (pi == NULL) {
1395 		if (debug & D_LINKNOTE)
1396 			logdebug("process_rtm_ifinfo: phyint lookup failed"
1397 			    " for %s\n", sdl->sdl_data);
1398 		return (_B_TRUE);
1399 	}
1400 
1401 	/*
1402 	 * We want to try and avoid doing a full interface scan for
1403 	 * link state notifications from the NICs, as indicated
1404 	 * by the state of the IFF_RUNNING flag.  If just the
1405 	 * IFF_RUNNING flag has changed state, the link state changes
1406 	 * are processed without a full scan.
1407 	 * If there is both an IPv4 and IPv6 instance associated with
1408 	 * the physical interface, we will get an RTM_IFINFO message
1409 	 * for each instance.  If we just maintained a single copy of
1410 	 * the physical interface flags, it would appear that no flags
1411 	 * had changed when the second message is processed, leading us
1412 	 * to believe that the message wasn't generated by a flags change,
1413 	 * and that a full interface scan is required.
1414 	 * To get around this problem, two additional copies of the flags
1415 	 * are kept, one copy for each instance.  These are only used in
1416 	 * this routine.  At any one time, all three copies of the flags
1417 	 * should be identical except for the IFF_RUNNING flag.	 The
1418 	 * copy of the flags in the "phyint" structure is always up to
1419 	 * date.
1420 	 */
1421 	pii = (type == AF_INET) ? pi->pi_v4 : pi->pi_v6;
1422 	if (pii == NULL) {
1423 		if (debug & D_LINKNOTE)
1424 			logdebug("process_rtm_ifinfo: no instance of address "
1425 			    "family %s for %s\n", AF_STR(type), pi->pi_name);
1426 		return (_B_TRUE);
1427 	}
1428 
1429 	old_flags = pii->pii_flags;
1430 	pii->pii_flags = PHYINT_FLAGS(ifm->ifm_flags);
1431 	pi->pi_flags = pii->pii_flags;
1432 
1433 	if (debug & D_LINKNOTE) {
1434 		logdebug("process_rtm_ifinfo: %s address family: %s, "
1435 		    "old flags: %llx, new flags: %llx\n", pi->pi_name,
1436 		    AF_STR(type), old_flags, pi->pi_flags);
1437 	}
1438 
1439 	/*
1440 	 * If IFF_STANDBY has changed, indicate that the interface has changed
1441 	 * types.
1442 	 */
1443 	if ((old_flags ^ pii->pii_flags) & IFF_STANDBY)
1444 		phyint_newtype(pi);
1445 
1446 	/*
1447 	 * If IFF_INACTIVE has been set, then no data addresses should be
1448 	 * hosted on the interface.  If IFF_INACTIVE has been cleared, then
1449 	 * move previously failed-over addresses back to it, provided it is
1450 	 * not failed.	For details, see the state diagram in mpd_probe.c.
1451 	 */
1452 	if ((old_flags ^ pii->pii_flags) & IFF_INACTIVE) {
1453 		if (pii->pii_flags & IFF_INACTIVE) {
1454 			if (!pi->pi_empty && (pi->pi_flags & IFF_STANDBY))
1455 				(void) try_failover(pi, FAILOVER_TO_NONSTANDBY);
1456 		} else {
1457 			if (pi->pi_state == PI_RUNNING && !pi->pi_full) {
1458 				pi->pi_empty = 0;
1459 				(void) try_failback(pi);
1460 			}
1461 		}
1462 	}
1463 
1464 	/* Has just the IFF_RUNNING flag changed state ? */
1465 	if ((old_flags ^ pii->pii_flags) != IFF_RUNNING) {
1466 		struct phyint_instance *pii_other;
1467 		/*
1468 		 * It wasn't just a link state change.	Update
1469 		 * the other instance's copy of the flags.
1470 		 */
1471 		pii_other = phyint_inst_other(pii);
1472 		if (pii_other != NULL)
1473 			pii_other->pii_flags = pii->pii_flags;
1474 		return (_B_TRUE);
1475 	}
1476 
1477 	return (_B_FALSE);
1478 }
1479 
1480 /*
1481  * Retrieve as many routing socket messages as possible, and try to
1482  * empty the routing sockets. Initiate full scan of targets or interfaces
1483  * as needed.
1484  * We listen on separate IPv4 an IPv6 sockets so that we can accurately
1485  * detect changes in certain flags (see "process_rtm_ifinfo()" above).
1486  */
1487 static void
1488 process_rtsock(int rtsock_v4, int rtsock_v6)
1489 {
1490 	int	nbytes;
1491 	int64_t msg[2048 / 8];
1492 	struct rt_msghdr *rtm;
1493 	boolean_t need_if_scan = _B_FALSE;
1494 	boolean_t need_rt_scan = _B_FALSE;
1495 	boolean_t rtm_ifinfo_seen = _B_FALSE;
1496 	int type;
1497 
1498 	/* Read as many messages as possible and try to empty the sockets */
1499 	for (type = AF_INET; ; type = AF_INET6) {
1500 		for (;;) {
1501 			nbytes = read((type == AF_INET) ? rtsock_v4 :
1502 			    rtsock_v6, msg, sizeof (msg));
1503 			if (nbytes <= 0) {
1504 				/* No more messages */
1505 				break;
1506 			}
1507 			rtm = (struct rt_msghdr *)msg;
1508 			if (rtm->rtm_version != RTM_VERSION) {
1509 				logerr("process_rtsock: version %d "
1510 				    "not understood\n", rtm->rtm_version);
1511 				break;
1512 			}
1513 
1514 			if (debug & D_PHYINT) {
1515 				logdebug("process_rtsock: message %d\n",
1516 				    rtm->rtm_type);
1517 			}
1518 
1519 			switch (rtm->rtm_type) {
1520 			case RTM_NEWADDR:
1521 			case RTM_DELADDR:
1522 				/*
1523 				 * Some logical interface has changed,
1524 				 * have to scan everything to determine
1525 				 * what actually changed.
1526 				 */
1527 				need_if_scan = _B_TRUE;
1528 				break;
1529 
1530 			case RTM_IFINFO:
1531 				rtm_ifinfo_seen = _B_TRUE;
1532 				need_if_scan |= process_rtm_ifinfo(
1533 				    (if_msghdr_t *)rtm, type);
1534 				break;
1535 
1536 			case RTM_ADD:
1537 			case RTM_DELETE:
1538 			case RTM_CHANGE:
1539 			case RTM_OLDADD:
1540 			case RTM_OLDDEL:
1541 				need_rt_scan = _B_TRUE;
1542 				break;
1543 
1544 			default:
1545 				/* Not interesting */
1546 				break;
1547 			}
1548 		}
1549 		if (type == AF_INET6)
1550 			break;
1551 	}
1552 
1553 	if (need_if_scan) {
1554 		if (debug & D_LINKNOTE && rtm_ifinfo_seen)
1555 			logdebug("process_rtsock: synchronizing with kernel\n");
1556 		initifs();
1557 	} else if (rtm_ifinfo_seen) {
1558 		if (debug & D_LINKNOTE)
1559 			logdebug("process_rtsock: "
1560 			    "link up/down notification(s) seen\n");
1561 		process_link_state_changes();
1562 	}
1563 
1564 	if (need_rt_scan)
1565 		init_router_targets();
1566 }
1567 
1568 /*
1569  * Look if the phyint instance or one of its logints have been removed from
1570  * the kernel and take appropriate action.
1571  * Uses {pii,li}_in_use.
1572  */
1573 static void
1574 check_if_removed(struct phyint_instance *pii)
1575 {
1576 	struct logint *li;
1577 	struct logint *next_li;
1578 
1579 	/* Detect phyints that have been removed from the kernel. */
1580 	if (!pii->pii_in_use) {
1581 		logtrace("%s %s has been removed from kernel\n",
1582 		    AF_STR(pii->pii_af), pii->pii_phyint->pi_name);
1583 		phyint_inst_delete(pii);
1584 	} else {
1585 		/* Detect logints that have been removed. */
1586 		for (li = pii->pii_logint; li != NULL; li = next_li) {
1587 			next_li = li->li_next;
1588 			if (!li->li_in_use) {
1589 				logint_delete(li);
1590 			}
1591 		}
1592 	}
1593 }
1594 
1595 /*
1596  * Send down a T_OPTMGMT_REQ to ip asking for all data in the various
1597  * tables defined by mib2.h. Parse the returned data and extract
1598  * the 'routing' information table. Process the 'routing' table
1599  * to get the list of known onlink routers, and update our database.
1600  * These onlink routers will serve as our probe targets.
1601  * Returns false, if any system calls resulted in errors, true otherwise.
1602  */
1603 static boolean_t
1604 update_router_list(int fd)
1605 {
1606 	union {
1607 		char	ubuf[1024];
1608 		union T_primitives uprim;
1609 	} buf;
1610 
1611 	int			flags;
1612 	struct strbuf		ctlbuf;
1613 	struct strbuf		databuf;
1614 	struct T_optmgmt_req	*tor;
1615 	struct T_optmgmt_ack	*toa;
1616 	struct T_error_ack	*tea;
1617 	struct opthdr		*optp;
1618 	struct opthdr		*req;
1619 	int			status;
1620 	t_scalar_t		prim;
1621 
1622 	tor = (struct T_optmgmt_req *)&buf;
1623 
1624 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
1625 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
1626 	tor->OPT_length = sizeof (struct opthdr);
1627 	tor->MGMT_flags = T_CURRENT;
1628 
1629 	req = (struct opthdr *)&tor[1];
1630 	req->level = MIB2_IP;	/* any MIB2_xxx value ok here */
1631 	req->name  = 0;
1632 	req->len   = 0;
1633 
1634 	ctlbuf.buf = (char *)&buf;
1635 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
1636 	ctlbuf.maxlen = sizeof (buf);
1637 	flags = 0;
1638 	if (putmsg(fd, &ctlbuf, NULL, flags) == -1) {
1639 		logperror("update_router_list: putmsg(ctl)");
1640 		return (_B_FALSE);
1641 	}
1642 
1643 	/*
1644 	 * The response consists of multiple T_OPTMGMT_ACK msgs, 1 msg for
1645 	 * each table defined in mib2.h.  Each T_OPTMGMT_ACK msg contains
1646 	 * a control and data part. The control part contains a struct
1647 	 * T_optmgmt_ack followed by a struct opthdr. The 'opthdr' identifies
1648 	 * the level, name and length of the data in the data part. The
1649 	 * data part contains the actual table data. The last message
1650 	 * is an end-of-data (EOD), consisting of a T_OPTMGMT_ACK and a
1651 	 * single option with zero optlen.
1652 	 */
1653 
1654 	for (;;) {
1655 		/*
1656 		 * Go around this loop once for each table. Ignore
1657 		 * all tables except the routing information table.
1658 		 */
1659 		flags = 0;
1660 		status = getmsg(fd, &ctlbuf, NULL, &flags);
1661 		if (status < 0) {
1662 			if (errno == EINTR)
1663 				continue;
1664 			logperror("update_router_list: getmsg(ctl)");
1665 			return (_B_FALSE);
1666 		}
1667 		if (ctlbuf.len < sizeof (t_scalar_t)) {
1668 			logerr("update_router_list: ctlbuf.len %d\n",
1669 			    ctlbuf.len);
1670 			return (_B_FALSE);
1671 		}
1672 
1673 		prim = buf.uprim.type;
1674 
1675 		switch (prim) {
1676 
1677 		case T_ERROR_ACK:
1678 			tea = &buf.uprim.error_ack;
1679 			if (ctlbuf.len < sizeof (struct T_error_ack)) {
1680 				logerr("update_router_list: T_ERROR_ACK"
1681 				    " ctlbuf.len %d\n", ctlbuf.len);
1682 				return (_B_FALSE);
1683 			}
1684 			logerr("update_router_list: T_ERROR_ACK:"
1685 			    " TLI_error = 0x%lx, UNIX_error = 0x%lx\n",
1686 			    tea->TLI_error, tea->UNIX_error);
1687 			return (_B_FALSE);
1688 
1689 		case T_OPTMGMT_ACK:
1690 			toa = &buf.uprim.optmgmt_ack;
1691 			optp = (struct opthdr *)&toa[1];
1692 			if (ctlbuf.len < sizeof (struct T_optmgmt_ack)) {
1693 				logerr("update_router_list: ctlbuf.len %d\n",
1694 				    ctlbuf.len);
1695 				return (_B_FALSE);
1696 			}
1697 			if (toa->MGMT_flags != T_SUCCESS) {
1698 				logerr("update_router_list: MGMT_flags 0x%lx\n",
1699 				    toa->MGMT_flags);
1700 				return (_B_FALSE);
1701 			}
1702 			break;
1703 
1704 		default:
1705 			logerr("update_router_list: unknown primitive %ld\n",
1706 			    prim);
1707 			return (_B_FALSE);
1708 		}
1709 
1710 		/* Process the T_OPGMGMT_ACK below */
1711 		assert(prim == T_OPTMGMT_ACK);
1712 
1713 		switch (status) {
1714 		case 0:
1715 			/*
1716 			 * We have reached the end of this T_OPTMGMT_ACK
1717 			 * message. If this is the last message i.e EOD,
1718 			 * return, else process the next T_OPTMGMT_ACK msg.
1719 			 */
1720 			if ((ctlbuf.len == sizeof (struct T_optmgmt_ack) +
1721 			    sizeof (struct opthdr)) && optp->len == 0 &&
1722 			    optp->name == 0 && optp->level == 0) {
1723 				/*
1724 				 * This is the EOD message. Return
1725 				 */
1726 				return (_B_TRUE);
1727 			}
1728 			continue;
1729 
1730 		case MORECTL:
1731 		case MORECTL | MOREDATA:
1732 			/*
1733 			 * This should not happen. We should be able to read
1734 			 * the control portion in a single getmsg.
1735 			 */
1736 			logerr("update_router_list: MORECTL\n");
1737 			return (_B_FALSE);
1738 
1739 		case MOREDATA:
1740 			databuf.maxlen = optp->len;
1741 			/* malloc of 0 bytes is ok */
1742 			databuf.buf = malloc((size_t)optp->len);
1743 			if (databuf.maxlen != 0 && databuf.buf == NULL) {
1744 				logperror("update_router_list: malloc");
1745 				return (_B_FALSE);
1746 			}
1747 			databuf.len = 0;
1748 			flags = 0;
1749 			for (;;) {
1750 				status = getmsg(fd, NULL, &databuf, &flags);
1751 				if (status >= 0) {
1752 					break;
1753 				} else if (errno == EINTR) {
1754 					continue;
1755 				} else {
1756 					logperror("update_router_list:"
1757 					    " getmsg(data)");
1758 					free(databuf.buf);
1759 					return (_B_FALSE);
1760 				}
1761 			}
1762 
1763 			if (optp->level == MIB2_IP &&
1764 			    optp->name == MIB2_IP_ROUTE) {
1765 				/* LINTED */
1766 				ire_process_v4((mib2_ipRouteEntry_t *)
1767 				    databuf.buf, databuf.len);
1768 			} else if (optp->level == MIB2_IP6 &&
1769 			    optp->name == MIB2_IP6_ROUTE) {
1770 				/* LINTED */
1771 				ire_process_v6((mib2_ipv6RouteEntry_t *)
1772 				    databuf.buf, databuf.len);
1773 			}
1774 			free(databuf.buf);
1775 		}
1776 	}
1777 	/* NOTREACHED */
1778 }
1779 
1780 /*
1781  * Examine the IPv4 routing table, for default routers. For each default
1782  * router, populate the list of targets of each phyint that is on the same
1783  * link as the default router
1784  */
1785 static void
1786 ire_process_v4(mib2_ipRouteEntry_t *buf, size_t len)
1787 {
1788 	mib2_ipRouteEntry_t	*rp;
1789 	mib2_ipRouteEntry_t	*rp1;
1790 	struct	in_addr		nexthop_v4;
1791 	mib2_ipRouteEntry_t	*endp;
1792 
1793 	if (len == 0)
1794 		return;
1795 	assert((len % sizeof (mib2_ipRouteEntry_t)) == 0);
1796 
1797 	endp = buf + (len / sizeof (mib2_ipRouteEntry_t));
1798 
1799 	/*
1800 	 * Loop thru the routing table entries. Process any IRE_DEFAULT,
1801 	 * IRE_PREFIX, IRE_HOST, IRE_HOST_REDIRECT ire. Ignore the others.
1802 	 * For each such IRE_OFFSUBNET ire, get the nexthop gateway address.
1803 	 * This is a potential target for probing, which we try to add
1804 	 * to the list of probe targets.
1805 	 */
1806 	for (rp = buf; rp < endp; rp++) {
1807 		if (!(rp->ipRouteInfo.re_ire_type & IRE_OFFSUBNET))
1808 			continue;
1809 
1810 		/*  Get the nexthop address. */
1811 		nexthop_v4.s_addr = rp->ipRouteNextHop;
1812 
1813 		/*
1814 		 * Get the nexthop address. Then determine the outgoing
1815 		 * interface, by examining all interface IREs, and picking the
1816 		 * match. We don't look at the interface specified in the route
1817 		 * because we need to add the router target on all matching
1818 		 * interfaces anyway; the goal is to avoid falling back to
1819 		 * multicast when some interfaces are in the same subnet but
1820 		 * not in the same group.
1821 		 */
1822 		for (rp1 = buf; rp1 < endp; rp1++) {
1823 			if (!(rp1->ipRouteInfo.re_ire_type & IRE_INTERFACE)) {
1824 				continue;
1825 			}
1826 
1827 			/*
1828 			 * Determine the interface IRE that matches the nexthop.
1829 			 * i.e.	 (IRE addr & IRE mask) == (nexthop & IRE mask)
1830 			 */
1831 			if ((rp1->ipRouteDest & rp1->ipRouteMask) ==
1832 			    (nexthop_v4.s_addr & rp1->ipRouteMask)) {
1833 				/*
1834 				 * We found the interface ire
1835 				 */
1836 				router_add_v4(rp1, nexthop_v4);
1837 			}
1838 		}
1839 	}
1840 }
1841 
1842 void
1843 router_add_v4(mib2_ipRouteEntry_t *rp1, struct in_addr nexthop_v4)
1844 {
1845 	char *cp;
1846 	char ifname[LIFNAMSIZ + 1];
1847 	struct in6_addr	nexthop;
1848 	int len;
1849 
1850 	if (debug & D_TARGET)
1851 		logdebug("router_add_v4()\n");
1852 
1853 	len = MIN(rp1->ipRouteIfIndex.o_length, sizeof (ifname) - 1);
1854 	(void) memcpy(ifname, rp1->ipRouteIfIndex.o_bytes, len);
1855 	ifname[len] = '\0';
1856 
1857 	if (ifname[0] == '\0')
1858 		return;
1859 
1860 	cp = strchr(ifname, IF_SEPARATOR);
1861 	if (cp != NULL)
1862 		*cp = '\0';
1863 
1864 	IN6_INADDR_TO_V4MAPPED(&nexthop_v4, &nexthop);
1865 	router_add_common(AF_INET, ifname, nexthop);
1866 }
1867 
1868 void
1869 router_add_common(int af, char *ifname, struct in6_addr nexthop)
1870 {
1871 	struct phyint_instance *pii;
1872 	struct phyint *pi;
1873 
1874 	if (debug & D_TARGET)
1875 		logdebug("router_add_common(%s %s)\n", AF_STR(af), ifname);
1876 
1877 	/*
1878 	 * Retrieve the phyint instance; bail if it's not known to us yet.
1879 	 */
1880 	pii = phyint_inst_lookup(af, ifname);
1881 	if (pii == NULL)
1882 		return;
1883 
1884 	/*
1885 	 * Don't use our own addresses as targets.
1886 	 */
1887 	if (own_address(nexthop))
1888 		return;
1889 
1890 	/*
1891 	 * If the phyint is part a named group, then add the address to all
1892 	 * members of the group; note that this is suboptimal in the IPv4 case
1893 	 * as it has already been added to all matching interfaces in
1894 	 * ire_process_v4(). Otherwise, add the address only to the phyint
1895 	 * itself, since other phyints in the anongroup may not be on the same
1896 	 * subnet.
1897 	 */
1898 	pi = pii->pii_phyint;
1899 	if (pi->pi_group == phyint_anongroup) {
1900 		target_add(pii, nexthop, _B_TRUE);
1901 	} else {
1902 		pi = pi->pi_group->pg_phyint;
1903 		for (; pi != NULL; pi = pi->pi_pgnext)
1904 			target_add(PHYINT_INSTANCE(pi, af), nexthop, _B_TRUE);
1905 	}
1906 }
1907 
1908 /*
1909  * Examine the IPv6 routing table, for default routers. For each default
1910  * router, populate the list of targets of each phyint that is on the same
1911  * link as the default router
1912  */
1913 static void
1914 ire_process_v6(mib2_ipv6RouteEntry_t *buf, size_t len)
1915 {
1916 	mib2_ipv6RouteEntry_t	*rp;
1917 	mib2_ipv6RouteEntry_t	*endp;
1918 	struct	in6_addr nexthop_v6;
1919 
1920 	if (debug & D_TARGET)
1921 		logdebug("ire_process_v6(len %d)\n", len);
1922 
1923 	if (len == 0)
1924 		return;
1925 
1926 	assert((len % sizeof (mib2_ipv6RouteEntry_t)) == 0);
1927 	endp = buf + (len / sizeof (mib2_ipv6RouteEntry_t));
1928 
1929 	/*
1930 	 * Loop thru the routing table entries. Process any IRE_DEFAULT,
1931 	 * IRE_PREFIX, IRE_HOST, IRE_HOST_REDIRECT ire. Ignore the others.
1932 	 * For each such IRE_OFFSUBNET ire, get the nexthop gateway address.
1933 	 * This is a potential target for probing, which we try to add
1934 	 * to the list of probe targets.
1935 	 */
1936 	for (rp = buf; rp < endp; rp++) {
1937 		if (!(rp->ipv6RouteInfo.re_ire_type & IRE_OFFSUBNET))
1938 			continue;
1939 
1940 		/*
1941 		 * We have the outgoing interface in ipv6RouteIfIndex
1942 		 * if ipv6RouteIfindex.o_length is non-zero. The outgoing
1943 		 * interface must be present for link-local addresses. Since
1944 		 * we use only link-local addreses for probing, we don't
1945 		 * consider the case when the outgoing interface is not
1946 		 * known and we need to scan interface ires
1947 		 */
1948 		nexthop_v6 = rp->ipv6RouteNextHop;
1949 		if (rp->ipv6RouteIfIndex.o_length != 0) {
1950 			/*
1951 			 * We already have the outgoing interface
1952 			 * in ipv6RouteIfIndex.
1953 			 */
1954 			router_add_v6(rp, nexthop_v6);
1955 		}
1956 	}
1957 }
1958 
1959 
1960 void
1961 router_add_v6(mib2_ipv6RouteEntry_t *rp1, struct in6_addr nexthop_v6)
1962 {
1963 	char ifname[LIFNAMSIZ + 1];
1964 	char *cp;
1965 	int  len;
1966 
1967 	if (debug & D_TARGET)
1968 		logdebug("router_add_v6()\n");
1969 
1970 	len = MIN(rp1->ipv6RouteIfIndex.o_length, sizeof (ifname) - 1);
1971 	(void) memcpy(ifname, rp1->ipv6RouteIfIndex.o_bytes, len);
1972 	ifname[len] = '\0';
1973 
1974 	if (ifname[0] == '\0')
1975 		return;
1976 
1977 	cp = strchr(ifname, IF_SEPARATOR);
1978 	if (cp != NULL)
1979 		*cp = '\0';
1980 
1981 	router_add_common(AF_INET6, ifname, nexthop_v6);
1982 }
1983 
1984 
1985 
1986 /*
1987  * Build a list of target routers, by scanning the routing tables.
1988  * It is assumed that interface routes exist, to reach the routers.
1989  */
1990 static void
1991 init_router_targets(void)
1992 {
1993 	struct	target *tg;
1994 	struct	target *next_tg;
1995 	struct	phyint_instance *pii;
1996 	struct	phyint *pi;
1997 
1998 	if (force_mcast)
1999 		return;
2000 
2001 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
2002 		pi = pii->pii_phyint;
2003 		/*
2004 		 * Exclude ptp and host targets. Set tg_in_use to false,
2005 		 * only for router targets.
2006 		 */
2007 		if (!pii->pii_targets_are_routers ||
2008 		    (pi->pi_flags & IFF_POINTOPOINT))
2009 			continue;
2010 
2011 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next)
2012 			tg->tg_in_use = 0;
2013 	}
2014 
2015 	if (mibfd < 0) {
2016 		mibfd = open("/dev/ip", O_RDWR);
2017 		if (mibfd < 0) {
2018 			logperror("mibopen: ip open");
2019 			exit(1);
2020 		}
2021 	}
2022 
2023 	if (!update_router_list(mibfd)) {
2024 		(void) close(mibfd);
2025 		mibfd = -1;
2026 	}
2027 
2028 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
2029 		if (!pii->pii_targets_are_routers ||
2030 		    (pi->pi_flags & IFF_POINTOPOINT))
2031 			continue;
2032 
2033 		for (tg = pii->pii_targets; tg != NULL; tg = next_tg) {
2034 			next_tg = tg->tg_next;
2035 			if (!tg->tg_in_use) {
2036 				target_delete(tg);
2037 			}
2038 		}
2039 	}
2040 }
2041 
2042 /*
2043  * Attempt to assign host targets to any interfaces that do not currently
2044  * have probe targets by sharing targets with other interfaces in the group.
2045  */
2046 static void
2047 init_host_targets(void)
2048 {
2049 	struct phyint_instance *pii;
2050 	struct phyint_group *pg;
2051 
2052 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
2053 		pg = pii->pii_phyint->pi_group;
2054 		if (pg != phyint_anongroup && pii->pii_targets == NULL)
2055 			dup_host_targets(pii);
2056 	}
2057 }
2058 
2059 /*
2060  * Duplicate host targets from other phyints of the group to
2061  * the phyint instance 'desired_pii'.
2062  */
2063 static void
2064 dup_host_targets(struct phyint_instance	 *desired_pii)
2065 {
2066 	int af;
2067 	struct phyint *pi;
2068 	struct phyint_instance *pii;
2069 	struct target *tg;
2070 
2071 	assert(desired_pii->pii_phyint->pi_group != phyint_anongroup);
2072 
2073 	af = desired_pii->pii_af;
2074 
2075 	/*
2076 	 * For every phyint in the same group as desired_pii, check if
2077 	 * it has any host targets. If so add them to desired_pii.
2078 	 */
2079 	for (pi = desired_pii->pii_phyint; pi != NULL; pi = pi->pi_pgnext) {
2080 		pii = PHYINT_INSTANCE(pi, af);
2081 		/*
2082 		 * We know that we don't have targets on this phyint instance
2083 		 * since we have been called. But we still check for
2084 		 * pii_targets_are_routers because another phyint instance
2085 		 * could have router targets, since IFF_NOFAILOVER addresses
2086 		 * on different phyint instances may belong to different
2087 		 * subnets.
2088 		 */
2089 		if ((pii == NULL) || (pii == desired_pii) ||
2090 		    pii->pii_targets_are_routers)
2091 			continue;
2092 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
2093 			target_create(desired_pii, tg->tg_address, _B_FALSE);
2094 		}
2095 	}
2096 }
2097 
2098 static void
2099 usage(char *cmd)
2100 {
2101 	(void) fprintf(stderr, "usage: %s\n", cmd);
2102 }
2103 
2104 
2105 #define	MPATHD_DEFAULT_FILE	"/etc/default/mpathd"
2106 
2107 /* Get an option from the /etc/default/mpathd file */
2108 static char *
2109 getdefault(char *name)
2110 {
2111 	char namebuf[BUFSIZ];
2112 	char *value = NULL;
2113 
2114 	if (defopen(MPATHD_DEFAULT_FILE) == 0) {
2115 		char	*cp;
2116 		int	flags;
2117 
2118 		/*
2119 		 * ignore case
2120 		 */
2121 		flags = defcntl(DC_GETFLAGS, 0);
2122 		TURNOFF(flags, DC_CASE);
2123 		(void) defcntl(DC_SETFLAGS, flags);
2124 
2125 		/* Add "=" to the name */
2126 		(void) strncpy(namebuf, name, sizeof (namebuf) - 2);
2127 		(void) strncat(namebuf, "=", 2);
2128 
2129 		if ((cp = defread(namebuf)) != NULL)
2130 			value = strdup(cp);
2131 
2132 		/* close */
2133 		(void) defopen((char *)NULL);
2134 	}
2135 	return (value);
2136 }
2137 
2138 
2139 /*
2140  * Command line options below
2141  */
2142 boolean_t	failback_enabled = _B_TRUE;	/* failback enabled/disabled */
2143 boolean_t	track_all_phyints = _B_FALSE;	/* option to track all NICs */
2144 static boolean_t adopt = _B_FALSE;
2145 static boolean_t foreground = _B_FALSE;
2146 
2147 int
2148 main(int argc, char *argv[])
2149 {
2150 	int i;
2151 	int c;
2152 	struct phyint_instance *pii;
2153 	char *value;
2154 
2155 	argv0 = argv;		/* Saved for re-exec on SIGHUP */
2156 	srandom(gethostid());	/* Initialize the random number generator */
2157 
2158 	/*
2159 	 * NOTE: The messages output by in.mpathd are not suitable for
2160 	 * translation, so we do not call textdomain().
2161 	 */
2162 	(void) setlocale(LC_ALL, "");
2163 
2164 	/*
2165 	 * Get the user specified value of 'failure detection time'
2166 	 * from /etc/default/mpathd
2167 	 */
2168 	value = getdefault("FAILURE_DETECTION_TIME");
2169 	if (value != NULL) {
2170 		user_failure_detection_time =
2171 		    (int)strtol((char *)value, NULL, 0);
2172 
2173 		if (user_failure_detection_time <= 0) {
2174 			user_failure_detection_time = FAILURE_DETECTION_TIME;
2175 			logerr("Invalid failure detection time %s, assuming "
2176 			    "default %d\n", value, user_failure_detection_time);
2177 
2178 		} else if (user_failure_detection_time <
2179 		    MIN_FAILURE_DETECTION_TIME) {
2180 			user_failure_detection_time =
2181 			    MIN_FAILURE_DETECTION_TIME;
2182 			logerr("Too small failure detection time of %s, "
2183 			    "assuming minimum %d\n", value,
2184 			    user_failure_detection_time);
2185 		}
2186 		free(value);
2187 	} else {
2188 		/* User has not specified the parameter, Use default value */
2189 		user_failure_detection_time = FAILURE_DETECTION_TIME;
2190 	}
2191 
2192 	/*
2193 	 * This gives the frequency at which probes will be sent.
2194 	 * When fdt ms elapses, we should be able to determine
2195 	 * whether 5 consecutive probes have failed or not.
2196 	 * 1 probe will be sent in every user_probe_interval ms,
2197 	 * randomly anytime in the (0.5  - 1.0) 2nd half of every
2198 	 * user_probe_interval. Thus when we send out probe 'n' we
2199 	 * can be sure that probe 'n - 2' is lost, if we have not
2200 	 * got the ack. (since the probe interval is > crtt). But
2201 	 * probe 'n - 1' may be a valid unacked probe, since the
2202 	 * time between 2 successive probes could be as small as
2203 	 * 0.5 * user_probe_interval.  Hence the NUM_PROBE_FAILS + 2
2204 	 */
2205 	user_probe_interval = user_failure_detection_time /
2206 	    (NUM_PROBE_FAILS + 2);
2207 
2208 	/*
2209 	 * Get the user specified value of failback_enabled from
2210 	 * /etc/default/mpathd
2211 	 */
2212 	value = getdefault("FAILBACK");
2213 	if (value != NULL) {
2214 		if (strncasecmp(value, "yes", 3) == 0)
2215 			failback_enabled = _B_TRUE;
2216 		else if (strncasecmp(value, "no", 2) == 0)
2217 			failback_enabled = _B_FALSE;
2218 		else
2219 			logerr("Invalid value for FAILBACK %s\n", value);
2220 		free(value);
2221 	} else {
2222 		failback_enabled = _B_TRUE;
2223 	}
2224 
2225 	/*
2226 	 * Get the user specified value of track_all_phyints from
2227 	 * /etc/default/mpathd. The sense is reversed in
2228 	 * TRACK_INTERFACES_ONLY_WITH_GROUPS.
2229 	 */
2230 	value = getdefault("TRACK_INTERFACES_ONLY_WITH_GROUPS");
2231 	if (value != NULL) {
2232 		if (strncasecmp(value, "yes", 3) == 0)
2233 			track_all_phyints = _B_FALSE;
2234 		else if (strncasecmp(value, "no", 2) == 0)
2235 			track_all_phyints = _B_TRUE;
2236 		else
2237 			logerr("Invalid value for "
2238 			    "TRACK_INTERFACES_ONLY_WITH_GROUPS %s\n", value);
2239 		free(value);
2240 	} else {
2241 		track_all_phyints = _B_FALSE;
2242 	}
2243 
2244 	while ((c = getopt(argc, argv, "adD:ml")) != EOF) {
2245 		switch (c) {
2246 		case 'a':
2247 			adopt = _B_TRUE;
2248 			break;
2249 		case 'm':
2250 			force_mcast = _B_TRUE;
2251 			break;
2252 		case 'd':
2253 			debug = D_ALL;
2254 			foreground = _B_TRUE;
2255 			break;
2256 		case 'D':
2257 			i = (int)strtol(optarg, NULL, 0);
2258 			if (i == 0) {
2259 				(void) fprintf(stderr, "Bad debug flags: %s\n",
2260 				    optarg);
2261 				exit(1);
2262 			}
2263 			debug |= i;
2264 			foreground = _B_TRUE;
2265 			break;
2266 		case 'l':
2267 			/*
2268 			 * Turn off link state notification handling.
2269 			 * Undocumented command line flag, for debugging
2270 			 * purposes.
2271 			 */
2272 			handle_link_notifications = _B_FALSE;
2273 			break;
2274 		default:
2275 			usage(argv[0]);
2276 			exit(1);
2277 		}
2278 	}
2279 
2280 	/*
2281 	 * The sockets for the loopback command interface should be listening
2282 	 * before we fork and exit in daemonize(). This way, whoever started us
2283 	 * can use the loopback interface as soon as they get a zero exit
2284 	 * status.
2285 	 */
2286 	lsock_v4 = setup_listener(AF_INET);
2287 	lsock_v6 = setup_listener(AF_INET6);
2288 
2289 	if (lsock_v4 < 0 && lsock_v6 < 0) {
2290 		logerr("main: setup_listener failed for both IPv4 and IPv6\n");
2291 		exit(1);
2292 	}
2293 
2294 	if (!foreground) {
2295 		if (!daemonize()) {
2296 			logerr("cannot daemonize\n");
2297 			exit(EXIT_FAILURE);
2298 		}
2299 		initlog();
2300 	}
2301 
2302 	/*
2303 	 * Initializations:
2304 	 * 1. Create ifsock* sockets. These are used for performing SIOC*
2305 	 *    ioctls. We have 2 sockets 1 each for IPv4 and IPv6.
2306 	 * 2. Initialize a pipe for handling/recording signal events.
2307 	 * 3. Create the routing sockets,  used for listening
2308 	 *    to routing / interface changes.
2309 	 * 4. phyint_init() - Initialize physical interface state
2310 	 *    (in mpd_tables.c).  Must be done before creating interfaces,
2311 	 *    which timer_init() does indirectly.
2312 	 * 5. timer_init()  - Initialize timer related stuff
2313 	 * 6. initifs() - Initialize our database of all known interfaces
2314 	 * 7. init_router_targets() - Initialize our database of all known
2315 	 *    router targets.
2316 	 */
2317 	ifsock_v4 = socket(AF_INET, SOCK_DGRAM, 0);
2318 	if (ifsock_v4 < 0) {
2319 		logperror("main: IPv4 socket open");
2320 		exit(1);
2321 	}
2322 
2323 	ifsock_v6 = socket(AF_INET6, SOCK_DGRAM, 0);
2324 	if (ifsock_v6 < 0) {
2325 		logperror("main: IPv6 socket open");
2326 		exit(1);
2327 	}
2328 
2329 	setup_eventpipe();
2330 
2331 	rtsock_v4 = setup_rtsock(AF_INET);
2332 	rtsock_v6 = setup_rtsock(AF_INET6);
2333 
2334 	if (phyint_init() == -1) {
2335 		logerr("cannot initialize physical interface structures");
2336 		exit(1);
2337 	}
2338 
2339 	timer_init();
2340 
2341 	initifs();
2342 
2343 	/* Inform kernel whether failback is enabled or disabled */
2344 	if (ioctl(ifsock_v4, SIOCSIPMPFAILBACK, (int *)&failback_enabled) < 0) {
2345 		logperror("main: ioctl (SIOCSIPMPFAILBACK)");
2346 		exit(1);
2347 	}
2348 
2349 	/*
2350 	 * If we're operating in "adopt" mode and no interfaces need to be
2351 	 * tracked, shut down (ifconfig(1M) will restart us on demand if
2352 	 * interfaces are subsequently put into multipathing groups).
2353 	 */
2354 	if (adopt && phyint_instances == NULL)
2355 		exit(0);
2356 
2357 	/*
2358 	 * Main body. Keep listening for activity on any of the sockets
2359 	 * that we are monitoring and take appropriate action as necessary.
2360 	 * signals are also handled synchronously.
2361 	 */
2362 	for (;;) {
2363 		if (poll(pollfds, pollfd_num, -1) < 0) {
2364 			if (errno == EINTR)
2365 				continue;
2366 			logperror("main: poll");
2367 			exit(1);
2368 		}
2369 		for (i = 0; i < pollfd_num; i++) {
2370 			if ((pollfds[i].fd == -1) ||
2371 			    !(pollfds[i].revents & POLLIN))
2372 				continue;
2373 			if (pollfds[i].fd == eventpipe_read) {
2374 				in_signal(eventpipe_read);
2375 				break;
2376 			}
2377 			if (pollfds[i].fd == rtsock_v4 ||
2378 			    pollfds[i].fd == rtsock_v6) {
2379 				process_rtsock(rtsock_v4, rtsock_v6);
2380 				break;
2381 			}
2382 			for (pii = phyint_instances; pii != NULL;
2383 			    pii = pii->pii_next) {
2384 				if (pollfds[i].fd == pii->pii_probe_sock) {
2385 					if (pii->pii_af == AF_INET)
2386 						in_data(pii);
2387 					else
2388 						in6_data(pii);
2389 					break;
2390 				}
2391 			}
2392 			if (pollfds[i].fd == lsock_v4)
2393 				loopback_cmd(lsock_v4, AF_INET);
2394 			else if (pollfds[i].fd == lsock_v6)
2395 				loopback_cmd(lsock_v6, AF_INET6);
2396 		}
2397 		if (full_scan_required) {
2398 			initifs();
2399 			full_scan_required = _B_FALSE;
2400 		}
2401 	}
2402 	/* NOTREACHED */
2403 	return (EXIT_SUCCESS);
2404 }
2405 
2406 static int
2407 setup_listener(int af)
2408 {
2409 	int sock;
2410 	int on;
2411 	int len;
2412 	int ret;
2413 	struct sockaddr_storage laddr;
2414 	struct sockaddr_in  *sin;
2415 	struct sockaddr_in6 *sin6;
2416 	struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
2417 
2418 	assert(af == AF_INET || af == AF_INET6);
2419 
2420 	sock = socket(af, SOCK_STREAM, 0);
2421 	if (sock < 0) {
2422 		logperror("setup_listener: socket");
2423 		exit(1);
2424 	}
2425 
2426 	on = 1;
2427 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
2428 	    sizeof (on)) < 0) {
2429 		logperror("setup_listener: setsockopt (SO_REUSEADDR)");
2430 		exit(1);
2431 	}
2432 
2433 	bzero(&laddr, sizeof (laddr));
2434 	laddr.ss_family = af;
2435 
2436 	if (af == AF_INET) {
2437 		sin = (struct sockaddr_in *)&laddr;
2438 		sin->sin_port = htons(MPATHD_PORT);
2439 		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2440 		len = sizeof (struct sockaddr_in);
2441 	} else {
2442 		sin6 = (struct sockaddr_in6 *)&laddr;
2443 		sin6->sin6_port = htons(MPATHD_PORT);
2444 		sin6->sin6_addr = loopback_addr;
2445 		len = sizeof (struct sockaddr_in6);
2446 	}
2447 
2448 	ret = bind(sock, (struct sockaddr *)&laddr, len);
2449 	if (ret < 0) {
2450 		if (errno == EADDRINUSE) {
2451 			/*
2452 			 * Another instance of mpathd may be already active.
2453 			 */
2454 			logerr("main: is another instance of in.mpathd "
2455 			    "already active?\n");
2456 			exit(1);
2457 		} else {
2458 			(void) close(sock);
2459 			return (-1);
2460 		}
2461 	}
2462 	if (listen(sock, 30) < 0) {
2463 		logperror("main: listen");
2464 		exit(1);
2465 	}
2466 	if (poll_add(sock) == -1) {
2467 		(void) close(sock);
2468 		exit(1);
2469 	}
2470 
2471 	return (sock);
2472 }
2473 
2474 /*
2475  * Table of commands and their expected size; used by loopback_cmd().
2476  */
2477 static struct {
2478 	const char	*name;
2479 	unsigned int	size;
2480 } commands[] = {
2481 	{ "MI_PING",		sizeof (uint32_t)	},
2482 	{ "MI_OFFLINE",		sizeof (mi_offline_t)	},
2483 	{ "MI_UNDO_OFFLINE",	sizeof (mi_undo_offline_t) },
2484 	{ "MI_SETOINDEX",	sizeof (mi_setoindex_t) },
2485 	{ "MI_QUERY",		sizeof (mi_query_t)	}
2486 };
2487 
2488 /*
2489  * Commands received over the loopback interface come here. Currently
2490  * the agents that send commands are ifconfig, if_mpadm and the RCM IPMP
2491  * module. ifconfig only makes a connection, and closes it to check if
2492  * in.mpathd is running.
2493  * if_mpadm sends commands in the format specified by the mpathd_interface
2494  * structure.
2495  */
2496 static void
2497 loopback_cmd(int sock, int family)
2498 {
2499 	int newfd;
2500 	ssize_t len;
2501 	struct sockaddr_storage	peer;
2502 	struct sockaddr_in	*peer_sin;
2503 	struct sockaddr_in6	*peer_sin6;
2504 	socklen_t peerlen;
2505 	union mi_commands mpi;
2506 	struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
2507 	char abuf[INET6_ADDRSTRLEN];
2508 	uint_t cmd;
2509 	int retval;
2510 
2511 	peerlen = sizeof (peer);
2512 	newfd = accept(sock, (struct sockaddr *)&peer, &peerlen);
2513 	if (newfd < 0) {
2514 		logperror("loopback_cmd: accept");
2515 		return;
2516 	}
2517 
2518 	switch (family) {
2519 	case AF_INET:
2520 		/*
2521 		 * Validate the address and port to make sure that
2522 		 * non privileged processes don't connect and start
2523 		 * talking to us.
2524 		 */
2525 		if (peerlen != sizeof (struct sockaddr_in)) {
2526 			logerr("loopback_cmd: AF_INET peerlen %d\n", peerlen);
2527 			(void) close(newfd);
2528 			return;
2529 		}
2530 		peer_sin = (struct sockaddr_in *)&peer;
2531 		if ((ntohs(peer_sin->sin_port) >= IPPORT_RESERVED) ||
2532 		    (ntohl(peer_sin->sin_addr.s_addr) != INADDR_LOOPBACK)) {
2533 			(void) inet_ntop(AF_INET, &peer_sin->sin_addr.s_addr,
2534 			    abuf, sizeof (abuf));
2535 			logerr("Attempt to connect from addr %s port %d\n",
2536 			    abuf, ntohs(peer_sin->sin_port));
2537 			(void) close(newfd);
2538 			return;
2539 		}
2540 		break;
2541 
2542 	case AF_INET6:
2543 		if (peerlen != sizeof (struct sockaddr_in6)) {
2544 			logerr("loopback_cmd: AF_INET6 peerlen %d\n", peerlen);
2545 			(void) close(newfd);
2546 			return;
2547 		}
2548 		/*
2549 		 * Validate the address and port to make sure that
2550 		 * non privileged processes don't connect and start
2551 		 * talking to us.
2552 		 */
2553 		peer_sin6 = (struct sockaddr_in6 *)&peer;
2554 		if ((ntohs(peer_sin6->sin6_port) >= IPPORT_RESERVED) ||
2555 		    (!IN6_ARE_ADDR_EQUAL(&peer_sin6->sin6_addr,
2556 		    &loopback_addr))) {
2557 			(void) inet_ntop(AF_INET6, &peer_sin6->sin6_addr, abuf,
2558 			    sizeof (abuf));
2559 			logerr("Attempt to connect from addr %s port %d\n",
2560 			    abuf, ntohs(peer_sin6->sin6_port));
2561 			(void) close(newfd);
2562 			return;
2563 		}
2564 
2565 	default:
2566 		logdebug("loopback_cmd: family %d\n", family);
2567 		(void) close(newfd);
2568 		return;
2569 	}
2570 
2571 	/*
2572 	 * The sizeof the 'mpi' buffer corresponds to the maximum size of
2573 	 * all supported commands
2574 	 */
2575 	len = read(newfd, &mpi, sizeof (mpi));
2576 
2577 	/*
2578 	 * ifconfig does not send any data. Just tests to see if mpathd
2579 	 * is already running.
2580 	 */
2581 	if (len <= 0) {
2582 		(void) close(newfd);
2583 		return;
2584 	}
2585 
2586 	/*
2587 	 * In theory, we can receive any sized message for a stream socket,
2588 	 * but we don't expect that to happen for a small message over a
2589 	 * loopback connection.
2590 	 */
2591 	if (len < sizeof (uint32_t)) {
2592 		logerr("loopback_cmd: bad command format or read returns "
2593 		    "partial data %d\n", len);
2594 	}
2595 
2596 	cmd = mpi.mi_command;
2597 	if (cmd >= MI_NCMD) {
2598 		logerr("loopback_cmd: unknown command id `%d'\n", cmd);
2599 		(void) close(newfd);
2600 		return;
2601 	}
2602 
2603 	if (len < commands[cmd].size) {
2604 		logerr("loopback_cmd: short %s command (expected %d, got %d)\n",
2605 		    commands[cmd].name, commands[cmd].size, len);
2606 		(void) close(newfd);
2607 		return;
2608 	}
2609 
2610 	retval = process_cmd(newfd, &mpi);
2611 	if (retval != IPMP_SUCCESS) {
2612 		logerr("failed processing %s: %s\n", commands[cmd].name,
2613 		    ipmp_errmsg(retval));
2614 	}
2615 	(void) close(newfd);
2616 }
2617 
2618 extern int global_errno;	/* set by failover() or failback() */
2619 
2620 /*
2621  * Process the offline, undo offline and set original index commands,
2622  * received from if_mpadm(1M)
2623  */
2624 static unsigned int
2625 process_cmd(int newfd, union mi_commands *mpi)
2626 {
2627 	uint_t	nif = 0;
2628 	uint32_t cmd;
2629 	struct phyint *pi;
2630 	struct phyint *pi2;
2631 	struct phyint_group *pg;
2632 	boolean_t success;
2633 	int error;
2634 	struct mi_offline *mio;
2635 	struct mi_undo_offline *miu;
2636 	struct lifreq lifr;
2637 	int ifsock;
2638 	struct mi_setoindex *mis;
2639 
2640 	cmd = mpi->mi_command;
2641 
2642 	switch (cmd) {
2643 	case MI_OFFLINE:
2644 		mio = &mpi->mi_ocmd;
2645 		/*
2646 		 * Lookup the interface that needs to be offlined.
2647 		 * If it does not exist, return a suitable error.
2648 		 */
2649 		pi = phyint_lookup(mio->mio_ifname);
2650 		if (pi == NULL)
2651 			return (send_result(newfd, IPMP_FAILURE, EINVAL));
2652 
2653 		/*
2654 		 * Verify that the minimum redundancy requirements are met.
2655 		 * The multipathing group must have at least the specified
2656 		 * number of functional interfaces after offlining the
2657 		 * requested interface. Otherwise return a suitable error.
2658 		 */
2659 		pg = pi->pi_group;
2660 		nif = 0;
2661 		if (pg != phyint_anongroup) {
2662 			for (nif = 0, pi2 = pg->pg_phyint; pi2 != NULL;
2663 			    pi2 = pi2->pi_pgnext) {
2664 				if ((pi2->pi_state == PI_RUNNING) ||
2665 				    (pg->pg_groupfailed &&
2666 				    !(pi2->pi_flags & IFF_OFFLINE)))
2667 					nif++;
2668 			}
2669 		}
2670 		if (nif < mio->mio_min_redundancy)
2671 			return (send_result(newfd, IPMP_EMINRED, 0));
2672 
2673 		/*
2674 		 * The order of operation is to set IFF_OFFLINE, followed by
2675 		 * failover. Setting IFF_OFFLINE ensures that no new ipif's
2676 		 * can be created. Subsequent failover moves everything on
2677 		 * the OFFLINE interface to some other functional interface.
2678 		 */
2679 		success = change_lif_flags(pi, IFF_OFFLINE, _B_TRUE);
2680 		if (success) {
2681 			if (!pi->pi_empty) {
2682 				error = try_failover(pi, FAILOVER_NORMAL);
2683 				if (error != 0) {
2684 					if (!change_lif_flags(pi, IFF_OFFLINE,
2685 					    _B_FALSE)) {
2686 						logerr("process_cmd: couldn't"
2687 						    " clear OFFLINE flag on"
2688 						    " %s\n", pi->pi_name);
2689 						/*
2690 						 * Offline interfaces should
2691 						 * not be probed.
2692 						 */
2693 						stop_probing(pi);
2694 					}
2695 					return (send_result(newfd, error,
2696 					    global_errno));
2697 				}
2698 			}
2699 		} else {
2700 			return (send_result(newfd, IPMP_FAILURE, errno));
2701 		}
2702 
2703 		/*
2704 		 * The interface is now Offline, so stop probing it.
2705 		 * Note that if_mpadm(1M) will down the test addresses,
2706 		 * after receiving a success reply from us. The routing
2707 		 * socket message will then make us close the socket used
2708 		 * for sending probes. But it is more logical that an
2709 		 * offlined interface must not be probed, even if it has
2710 		 * test addresses.
2711 		 */
2712 		stop_probing(pi);
2713 		return (send_result(newfd, IPMP_SUCCESS, 0));
2714 
2715 	case MI_UNDO_OFFLINE:
2716 		miu = &mpi->mi_ucmd;
2717 		/*
2718 		 * Undo the offline command. As usual lookup the interface.
2719 		 * Send an error if it does not exist or is not offline.
2720 		 */
2721 		pi = phyint_lookup(miu->miu_ifname);
2722 		if (pi == NULL || pi->pi_state != PI_OFFLINE)
2723 			return (send_result(newfd, IPMP_FAILURE, EINVAL));
2724 
2725 		/*
2726 		 * Reset the state of the interface based on the current link
2727 		 * state; if this phyint subsequently acquires a test address,
2728 		 * the state will be updated later as a result of the probes.
2729 		 */
2730 		if (LINK_UP(pi))
2731 			phyint_chstate(pi, PI_RUNNING);
2732 		else
2733 			phyint_chstate(pi, PI_FAILED);
2734 
2735 		if (pi->pi_state == PI_RUNNING) {
2736 			/*
2737 			 * Note that the success of MI_UNDO_OFFLINE is not
2738 			 * contingent on actually failing back; in the odd
2739 			 * case where we cannot do it here, we will try again
2740 			 * in initifs() since pi->pi_full will still be zero.
2741 			 */
2742 			if (do_failback(pi) != IPMP_SUCCESS) {
2743 				logdebug("process_cmd: cannot failback from "
2744 				    "%s during MI_UNDO_OFFLINE\n", pi->pi_name);
2745 			}
2746 		}
2747 
2748 		/*
2749 		 * Clear the IFF_OFFLINE flag.  We have to do this last
2750 		 * because do_failback() relies on it being set to decide
2751 		 * when to display messages.
2752 		 */
2753 		(void) change_lif_flags(pi, IFF_OFFLINE, _B_FALSE);
2754 
2755 		/*
2756 		 * Give the requestor time to configure test addresses
2757 		 * before complaining that they're missing.
2758 		 */
2759 		pi->pi_taddrthresh = getcurrentsec() + TESTADDR_CONF_TIME;
2760 
2761 		return (send_result(newfd, IPMP_SUCCESS, 0));
2762 
2763 	case MI_SETOINDEX:
2764 		mis = &mpi->mi_scmd;
2765 
2766 		/* Get the socket for doing ioctls */
2767 		ifsock = (mis->mis_iftype == AF_INET) ? ifsock_v4 : ifsock_v6;
2768 
2769 		/*
2770 		 * Get index of new original interface.
2771 		 * The index is returned in lifr.lifr_index.
2772 		 */
2773 		(void) strlcpy(lifr.lifr_name, mis->mis_new_pifname,
2774 		    sizeof (lifr.lifr_name));
2775 
2776 		if (ioctl(ifsock, SIOCGLIFINDEX, (char *)&lifr) < 0)
2777 			return (send_result(newfd, IPMP_FAILURE, errno));
2778 
2779 		/*
2780 		 * Set new original interface index.
2781 		 * The new index was put into lifr.lifr_index by the
2782 		 * SIOCGLIFINDEX ioctl.
2783 		 */
2784 		(void) strlcpy(lifr.lifr_name, mis->mis_lifname,
2785 		    sizeof (lifr.lifr_name));
2786 
2787 		if (ioctl(ifsock, SIOCSLIFOINDEX, (char *)&lifr) < 0)
2788 			return (send_result(newfd, IPMP_FAILURE, errno));
2789 
2790 		return (send_result(newfd, IPMP_SUCCESS, 0));
2791 
2792 	case MI_QUERY:
2793 		return (process_query(newfd, &mpi->mi_qcmd));
2794 
2795 	default:
2796 		break;
2797 	}
2798 
2799 	return (send_result(newfd, IPMP_EPROTO, 0));
2800 }
2801 
2802 /*
2803  * Process the query request pointed to by `miq' and send a reply on file
2804  * descriptor `fd'.  Returns an IPMP error code.
2805  */
2806 static unsigned int
2807 process_query(int fd, mi_query_t *miq)
2808 {
2809 	ipmp_groupinfo_t	*grinfop;
2810 	ipmp_groupinfolist_t	*grlp;
2811 	ipmp_grouplist_t	*grlistp;
2812 	ipmp_ifinfo_t		*ifinfop;
2813 	ipmp_ifinfolist_t	*iflp;
2814 	ipmp_snap_t		*snap;
2815 	unsigned int		retval;
2816 
2817 	switch (miq->miq_inforeq) {
2818 	case IPMP_GROUPLIST:
2819 		retval = getgrouplist(&grlistp);
2820 		if (retval != IPMP_SUCCESS)
2821 			return (send_result(fd, retval, errno));
2822 
2823 		retval = send_result(fd, IPMP_SUCCESS, 0);
2824 		if (retval == IPMP_SUCCESS)
2825 			retval = send_grouplist(fd, grlistp);
2826 
2827 		ipmp_freegrouplist(grlistp);
2828 		return (retval);
2829 
2830 	case IPMP_GROUPINFO:
2831 		miq->miq_grname[LIFGRNAMSIZ - 1] = '\0';
2832 		retval = getgroupinfo(miq->miq_ifname, &grinfop);
2833 		if (retval != IPMP_SUCCESS)
2834 			return (send_result(fd, retval, errno));
2835 
2836 		retval = send_result(fd, IPMP_SUCCESS, 0);
2837 		if (retval == IPMP_SUCCESS)
2838 			retval = send_groupinfo(fd, grinfop);
2839 
2840 		ipmp_freegroupinfo(grinfop);
2841 		return (retval);
2842 
2843 	case IPMP_IFINFO:
2844 		miq->miq_ifname[LIFNAMSIZ - 1] = '\0';
2845 		retval = getifinfo(miq->miq_ifname, &ifinfop);
2846 		if (retval != IPMP_SUCCESS)
2847 			return (send_result(fd, retval, errno));
2848 
2849 		retval = send_result(fd, IPMP_SUCCESS, 0);
2850 		if (retval == IPMP_SUCCESS)
2851 			retval = send_ifinfo(fd, ifinfop);
2852 
2853 		ipmp_freeifinfo(ifinfop);
2854 		return (retval);
2855 
2856 	case IPMP_SNAP:
2857 		retval = getsnap(&snap);
2858 		if (retval != IPMP_SUCCESS)
2859 			return (send_result(fd, retval, errno));
2860 
2861 		retval = send_result(fd, IPMP_SUCCESS, 0);
2862 		if (retval != IPMP_SUCCESS)
2863 			goto out;
2864 
2865 		retval = ipmp_writetlv(fd, IPMP_SNAP, sizeof (*snap), snap);
2866 		if (retval != IPMP_SUCCESS)
2867 			goto out;
2868 
2869 		retval = send_grouplist(fd, snap->sn_grlistp);
2870 		if (retval != IPMP_SUCCESS)
2871 			goto out;
2872 
2873 		iflp = snap->sn_ifinfolistp;
2874 		for (; iflp != NULL; iflp = iflp->ifl_next) {
2875 			retval = send_ifinfo(fd, iflp->ifl_ifinfop);
2876 			if (retval != IPMP_SUCCESS)
2877 				goto out;
2878 		}
2879 
2880 		grlp = snap->sn_grinfolistp;
2881 		for (; grlp != NULL; grlp = grlp->grl_next) {
2882 			retval = send_groupinfo(fd, grlp->grl_grinfop);
2883 			if (retval != IPMP_SUCCESS)
2884 				goto out;
2885 		}
2886 	out:
2887 		ipmp_snap_free(snap);
2888 		return (retval);
2889 
2890 	default:
2891 		break;
2892 
2893 	}
2894 	return (send_result(fd, IPMP_EPROTO, 0));
2895 }
2896 
2897 /*
2898  * Send the group information pointed to by `grinfop' on file descriptor `fd'.
2899  * Returns an IPMP error code.
2900  */
2901 static unsigned int
2902 send_groupinfo(int fd, ipmp_groupinfo_t *grinfop)
2903 {
2904 	ipmp_iflist_t	*iflistp = grinfop->gr_iflistp;
2905 	unsigned int	retval;
2906 
2907 	retval = ipmp_writetlv(fd, IPMP_GROUPINFO, sizeof (*grinfop), grinfop);
2908 	if (retval != IPMP_SUCCESS)
2909 		return (retval);
2910 
2911 	return (ipmp_writetlv(fd, IPMP_IFLIST,
2912 	    IPMP_IFLIST_SIZE(iflistp->il_nif), iflistp));
2913 }
2914 
2915 /*
2916  * Send the interface information pointed to by `ifinfop' on file descriptor
2917  * `fd'.  Returns an IPMP error code.
2918  */
2919 static unsigned int
2920 send_ifinfo(int fd, ipmp_ifinfo_t *ifinfop)
2921 {
2922 	return (ipmp_writetlv(fd, IPMP_IFINFO, sizeof (*ifinfop), ifinfop));
2923 }
2924 
2925 /*
2926  * Send the group list pointed to by `grlistp' on file descriptor `fd'.
2927  * Returns an IPMP error code.
2928  */
2929 static unsigned int
2930 send_grouplist(int fd, ipmp_grouplist_t *grlistp)
2931 {
2932 	return (ipmp_writetlv(fd, IPMP_GROUPLIST,
2933 	    IPMP_GROUPLIST_SIZE(grlistp->gl_ngroup), grlistp));
2934 }
2935 
2936 /*
2937  * Initialize an mi_result_t structure using `error' and `syserror' and
2938  * send it on file descriptor `fd'.  Returns an IPMP error code.
2939  */
2940 static unsigned int
2941 send_result(int fd, unsigned int error, int syserror)
2942 {
2943 	mi_result_t me;
2944 
2945 	me.me_mpathd_error = error;
2946 	if (error == IPMP_FAILURE)
2947 		me.me_sys_error = syserror;
2948 	else
2949 		me.me_sys_error = 0;
2950 
2951 	return (ipmp_write(fd, &me, sizeof (me)));
2952 }
2953 
2954 /*
2955  * Daemonize the process.
2956  */
2957 static boolean_t
2958 daemonize(void)
2959 {
2960 	switch (fork()) {
2961 	case -1:
2962 		return (_B_FALSE);
2963 
2964 	case  0:
2965 		/*
2966 		 * Lose our controlling terminal, and become both a session
2967 		 * leader and a process group leader.
2968 		 */
2969 		if (setsid() == -1)
2970 			return (_B_FALSE);
2971 
2972 		/*
2973 		 * Under POSIX, a session leader can accidentally (through
2974 		 * open(2)) acquire a controlling terminal if it does not
2975 		 * have one.  Just to be safe, fork() again so we are not a
2976 		 * session leader.
2977 		 */
2978 		switch (fork()) {
2979 		case -1:
2980 			return (_B_FALSE);
2981 
2982 		case 0:
2983 			(void) chdir("/");
2984 			(void) umask(022);
2985 			(void) fdwalk(closefunc, NULL);
2986 			break;
2987 
2988 		default:
2989 			_exit(EXIT_SUCCESS);
2990 		}
2991 		break;
2992 
2993 	default:
2994 		_exit(EXIT_SUCCESS);
2995 	}
2996 
2997 	return (_B_TRUE);
2998 }
2999 
3000 /*
3001  * The parent has created some fds before forking on purpose, keep them open.
3002  */
3003 static int
3004 closefunc(void *not_used, int fd)
3005 /* ARGSUSED */
3006 {
3007 	if (fd != lsock_v4 && fd != lsock_v6)
3008 		(void) close(fd);
3009 	return (0);
3010 }
3011 
3012 /* LOGGER */
3013 
3014 #include <syslog.h>
3015 
3016 /*
3017  * Logging routines.  All routines log to syslog, unless the daemon is
3018  * running in the foreground, in which case the logging goes to stderr.
3019  *
3020  * The following routines are available:
3021  *
3022  *	logdebug(): A printf-like function for outputting debug messages
3023  *	(messages at LOG_DEBUG) that are only of use to developers.
3024  *
3025  *	logtrace(): A printf-like function for outputting tracing messages
3026  *	(messages at LOG_INFO) from the daemon.	 This is typically used
3027  *	to log the receipt of interesting network-related conditions.
3028  *
3029  *	logerr(): A printf-like function for outputting error messages
3030  *	(messages at LOG_ERR) from the daemon.
3031  *
3032  *	logperror*(): A set of functions used to output error messages
3033  *	(messages at LOG_ERR); these automatically append strerror(errno)
3034  *	and a newline to the message passed to them.
3035  *
3036  * NOTE: since the logging functions write to syslog, the messages passed
3037  *	 to them are not eligible for localization.  Thus, gettext() must
3038  *	 *not* be used.
3039  */
3040 
3041 static int logging = 0;
3042 
3043 static void
3044 initlog(void)
3045 {
3046 	logging++;
3047 	openlog("in.mpathd", LOG_PID, LOG_DAEMON);
3048 }
3049 
3050 /* PRINTFLIKE2 */
3051 void
3052 logmsg(int pri, const char *fmt, ...)
3053 {
3054 	va_list ap;
3055 
3056 	va_start(ap, fmt);
3057 
3058 	if (logging)
3059 		vsyslog(pri, fmt, ap);
3060 	else
3061 		(void) vfprintf(stderr, fmt, ap);
3062 	va_end(ap);
3063 }
3064 
3065 /* PRINTFLIKE1 */
3066 void
3067 logperror(const char *str)
3068 {
3069 	if (logging)
3070 		syslog(LOG_ERR, "%s: %m\n", str);
3071 	else
3072 		(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
3073 }
3074 
3075 void
3076 logperror_pii(struct phyint_instance *pii, const char *str)
3077 {
3078 	if (logging) {
3079 		syslog(LOG_ERR, "%s (%s %s): %m\n",
3080 		    str, AF_STR(pii->pii_af), pii->pii_phyint->pi_name);
3081 	} else {
3082 		(void) fprintf(stderr, "%s (%s %s): %s\n",
3083 		    str, AF_STR(pii->pii_af), pii->pii_phyint->pi_name,
3084 		    strerror(errno));
3085 	}
3086 }
3087 
3088 void
3089 logperror_li(struct logint *li, const char *str)
3090 {
3091 	struct	phyint_instance	*pii = li->li_phyint_inst;
3092 
3093 	if (logging) {
3094 		syslog(LOG_ERR, "%s (%s %s): %m\n",
3095 		    str, AF_STR(pii->pii_af), li->li_name);
3096 	} else {
3097 		(void) fprintf(stderr, "%s (%s %s): %s\n",
3098 		    str, AF_STR(pii->pii_af), li->li_name,
3099 		    strerror(errno));
3100 	}
3101 }
3102 
3103 void
3104 close_probe_socket(struct phyint_instance *pii, boolean_t polled)
3105 {
3106 	if (polled)
3107 		(void) poll_remove(pii->pii_probe_sock);
3108 	(void) close(pii->pii_probe_sock);
3109 	pii->pii_probe_sock = -1;
3110 	pii->pii_basetime_inited = 0;
3111 }
3112