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