xref: /titanic_52/usr/src/cmd/cmd-inet/usr.lib/ncaconfd/ncaconfd.c (revision ff550d0e264b51131fb34e9e83163b348d916640)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*ff550d0eSmasputra  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
327c478bd9Sstevel@tonic-gate #include <stropts.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <libintl.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate #include <sys/ethernet.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <net/if.h>
517c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
527c478bd9Sstevel@tonic-gate #include <inet/ip.h>
537c478bd9Sstevel@tonic-gate #include <net/route.h>
547c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
557c478bd9Sstevel@tonic-gate #include "ncaconf.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* NCA does not support IPv6... */
587c478bd9Sstevel@tonic-gate #ifndef	NCA_MOD_NAME
597c478bd9Sstevel@tonic-gate #define	NCA_MOD_NAME	"nca"
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #ifndef	ARP_MOD_NAME
637c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	IF_SEPARATOR	':'
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	ping_prog	"/usr/sbin/ping"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* Structure to hold info about each network interface. */
717c478bd9Sstevel@tonic-gate typedef struct nif_s {
727c478bd9Sstevel@tonic-gate 	char		name[LIFNAMSIZ+1];
737c478bd9Sstevel@tonic-gate 	struct in_addr	local_addr;
747c478bd9Sstevel@tonic-gate 	struct in_addr	router_addr;
757c478bd9Sstevel@tonic-gate 	uchar_t		router_ether_addr[ETHERADDRL];
767c478bd9Sstevel@tonic-gate } nif_t;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate typedef struct mib_item_s {
797c478bd9Sstevel@tonic-gate 	struct mib_item_s	*next_item;
807c478bd9Sstevel@tonic-gate 	int			group;
817c478bd9Sstevel@tonic-gate 	int			mib_id;
827c478bd9Sstevel@tonic-gate 	int			length;
837c478bd9Sstevel@tonic-gate 	char			*valp;
847c478bd9Sstevel@tonic-gate } mib_item_t;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* The network interface array. */
877c478bd9Sstevel@tonic-gate static nif_t *nif_list;
887c478bd9Sstevel@tonic-gate /* Number of network interface to process. */
897c478bd9Sstevel@tonic-gate static int num_nif;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* Interface request to IP. */
927c478bd9Sstevel@tonic-gate static struct lifreq lifr;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* True if syslog is to be used. */
957c478bd9Sstevel@tonic-gate static boolean_t logging;
967c478bd9Sstevel@tonic-gate /* True if additional debugging messages are printed. */
977c478bd9Sstevel@tonic-gate static boolean_t debug;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* File descriptor to the routing socket. */
1007c478bd9Sstevel@tonic-gate static int rt_fd;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static void logperror(char *);
1037c478bd9Sstevel@tonic-gate static void logwarn(char *, ...);
1047c478bd9Sstevel@tonic-gate static void logdebug(char *, ...);
1057c478bd9Sstevel@tonic-gate static int ip_domux2fd(int *, int *);
1067c478bd9Sstevel@tonic-gate static void ip_plink(int, int);
1077c478bd9Sstevel@tonic-gate static int find_nca_pos(int);
1087c478bd9Sstevel@tonic-gate static int nca_set_nif(int, struct in_addr, uchar_t *);
1097c478bd9Sstevel@tonic-gate static void nca_setup(boolean_t *);
1107c478bd9Sstevel@tonic-gate static int get_if_ip_addr(void);
1117c478bd9Sstevel@tonic-gate static mib_item_t *mibget(int);
1127c478bd9Sstevel@tonic-gate static int ire_process(mib2_ipRouteEntry_t *, size_t, boolean_t *);
1137c478bd9Sstevel@tonic-gate static int arp_process(mib2_ipNetToMediaEntry_t *, size_t, boolean_t *);
1147c478bd9Sstevel@tonic-gate static int get_router_ip_addr(mib_item_t *, boolean_t *);
1157c478bd9Sstevel@tonic-gate static int get_router_ether_addr(mib_item_t *, boolean_t *);
1167c478bd9Sstevel@tonic-gate static int get_if_info(boolean_t *);
1177c478bd9Sstevel@tonic-gate static void daemon_init(void);
1187c478bd9Sstevel@tonic-gate static void daemon_work(void);
1197c478bd9Sstevel@tonic-gate static void ping_them(void);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Print out system error messages, either to syslog or stderr.  Note that
1237c478bd9Sstevel@tonic-gate  * syslog() should print out system error messages in the correct language
1247c478bd9Sstevel@tonic-gate  * used.  There is no need to use gettext().
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate static void
1277c478bd9Sstevel@tonic-gate logperror(char *str)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	if (logging) {
1307c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %m\n", str);
1317c478bd9Sstevel@tonic-gate 	} else {
1327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncaconfd: %s: %s\n", str,
1337c478bd9Sstevel@tonic-gate 		    strerror(errno));
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Print out warning messages.  The caller should use gettext() to have
1397c478bd9Sstevel@tonic-gate  * the message printed out in the correct language.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1427c478bd9Sstevel@tonic-gate static void
1437c478bd9Sstevel@tonic-gate logwarn(char *fmt, ...)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	va_list ap;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1487c478bd9Sstevel@tonic-gate 	if (logging) {
1497c478bd9Sstevel@tonic-gate 		vsyslog(LOG_WARNING, fmt, ap);
1507c478bd9Sstevel@tonic-gate 	} else {
1517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncaconfd: ");
1527c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	va_end(ap);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Print out debugging info.  Note that syslogd(1M) should be configured to
1597c478bd9Sstevel@tonic-gate  * take ordinary debug info for it to get this kind of info.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1627c478bd9Sstevel@tonic-gate static void
1637c478bd9Sstevel@tonic-gate logdebug(char *fmt, ...)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	va_list ap;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1687c478bd9Sstevel@tonic-gate 	if (logging) {
1697c478bd9Sstevel@tonic-gate 		vsyslog(LOG_WARNING, fmt, ap);
1707c478bd9Sstevel@tonic-gate 	} else {
1717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ncaconfd: ");
1727c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	va_end(ap);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Helper function for nca_setup().  It gets a fd to the lower IP
1797c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
1807c478bd9Sstevel@tonic-gate  * global variable lifr.
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * Param:
1837c478bd9Sstevel@tonic-gate  *	int *udp_fd: (referenced) fd to /dev/udp (upper IP stream).
1847c478bd9Sstevel@tonic-gate  *	int *fd: (referenced) fd to the lower IP stream.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * Return:
1877c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static int
1907c478bd9Sstevel@tonic-gate ip_domux2fd(int *udp_fd, int *fd)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	int ip_fd;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if ((ip_fd = open(IP_DEV_NAME, O_RDWR)) < 0) {
1957c478bd9Sstevel@tonic-gate 		logperror("Cannot open IP");
1967c478bd9Sstevel@tonic-gate 		return (-1);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	if ((*udp_fd = open(UDP_DEV_NAME, O_RDWR)) < 0) {
1997c478bd9Sstevel@tonic-gate 		logperror("Cannot open UDP");
2007c478bd9Sstevel@tonic-gate 		(void) close(ip_fd);
2017c478bd9Sstevel@tonic-gate 		return (-1);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2047c478bd9Sstevel@tonic-gate 		logperror("ioctl(SIOCGLIFMUXID) failed");
2057c478bd9Sstevel@tonic-gate 		(void) close(ip_fd);
2067c478bd9Sstevel@tonic-gate 		return (-1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	if (debug) {
2097c478bd9Sstevel@tonic-gate 		logdebug("ARP_muxid %d IP_muxid %d\n", lifr.lifr_arp_muxid,
2107c478bd9Sstevel@tonic-gate 		    lifr.lifr_ip_muxid);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	if ((*fd = ioctl(*udp_fd, _I_MUXID2FD, lifr.lifr_ip_muxid)) < 0) {
2137c478bd9Sstevel@tonic-gate 		logperror("ioctl(_I_MUXID2FD) failed");
2147c478bd9Sstevel@tonic-gate 		(void) close(ip_fd);
2157c478bd9Sstevel@tonic-gate 		(void) close(*udp_fd);
2167c478bd9Sstevel@tonic-gate 		return (-1);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	(void) close(ip_fd);
2197c478bd9Sstevel@tonic-gate 	return (0);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Helper function for nca_setup().  It I_PLINK's back the upper and
2247c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
2257c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
2267c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
2277c478bd9Sstevel@tonic-gate  * must be called in pairs.
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  * Param:
2307c478bd9Sstevel@tonic-gate  *	int udp_fd: fd to /dev/udp (upper IP stream).
2317c478bd9Sstevel@tonic-gate  *	int fd: fd to the lower IP stream.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate ip_plink(int udp_fd, int fd)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	int mux_id;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if ((mux_id = ioctl(udp_fd, I_PLINK, fd)) < 0) {
2397c478bd9Sstevel@tonic-gate 		logperror("ioctl(I_PLINK) failed");
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if (debug > 0) {
2437c478bd9Sstevel@tonic-gate 		logdebug("New IP_muxid %d\n", mux_id);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	lifr.lifr_ip_muxid = mux_id;
2467c478bd9Sstevel@tonic-gate 	if (ioctl(udp_fd, SIOCSLIFMUXID, (caddr_t)&lifr) < 0) {
2477c478bd9Sstevel@tonic-gate 		logperror("ioctl(SIOCSLIFMUXID) failed");
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #define	FOUND_NCA	-1
2527c478bd9Sstevel@tonic-gate #define	FOUND_NONE	-2
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Find the proper position to insert NCA, which is just below IP.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * Param:
2577c478bd9Sstevel@tonic-gate  *	int fd: fd to the lower IP stream.
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  * Return:
2607c478bd9Sstevel@tonic-gate  *	If positive, it is the position to insert NCA.
2617c478bd9Sstevel@tonic-gate  *	FOUND_NCA: found NCA!  So skip this one for plumbing.  But we
2627c478bd9Sstevel@tonic-gate  *		still keep it in the interface list.
2637c478bd9Sstevel@tonic-gate  *	FOUND_NONE: could not find IP or encounter other errors.  Remove
2647c478bd9Sstevel@tonic-gate  *		this interface from the	list.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static int
2677c478bd9Sstevel@tonic-gate find_nca_pos(int fd)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	int num_mods;
2707c478bd9Sstevel@tonic-gate 	int i, pos;
2717c478bd9Sstevel@tonic-gate 	struct str_list strlist;
2727c478bd9Sstevel@tonic-gate 	boolean_t found_ip = B_FALSE;
2737c478bd9Sstevel@tonic-gate 	boolean_t found_nca = B_FALSE;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(fd, I_LIST, NULL)) < 0) {
2767c478bd9Sstevel@tonic-gate 		logperror("ioctl(I_LIST) failed");
2777c478bd9Sstevel@tonic-gate 		return (FOUND_NONE);
2787c478bd9Sstevel@tonic-gate 	} else {
2797c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
2807c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = calloc(num_mods,
2817c478bd9Sstevel@tonic-gate 		    sizeof (struct str_mlist));
2827c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
2837c478bd9Sstevel@tonic-gate 			logperror("cannot malloc");
2847c478bd9Sstevel@tonic-gate 			return (FOUND_NONE);
2857c478bd9Sstevel@tonic-gate 		} else {
2867c478bd9Sstevel@tonic-gate 			if (ioctl(fd, I_LIST, (caddr_t)&strlist) < 0) {
2877c478bd9Sstevel@tonic-gate 				logperror("ioctl(I_LIST) failed");
2887c478bd9Sstevel@tonic-gate 			} else {
2897c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
2907c478bd9Sstevel@tonic-gate 					if (strcmp(IP_MOD_NAME,
2917c478bd9Sstevel@tonic-gate 					    strlist.sl_modlist[i].l_name)
2927c478bd9Sstevel@tonic-gate 					    == 0) {
2937c478bd9Sstevel@tonic-gate 						found_ip = B_TRUE;
2947c478bd9Sstevel@tonic-gate 						/*
2957c478bd9Sstevel@tonic-gate 						 * NCA should be just below
2967c478bd9Sstevel@tonic-gate 						 * IP.
2977c478bd9Sstevel@tonic-gate 						 */
2987c478bd9Sstevel@tonic-gate 						pos = i + 1;
2997c478bd9Sstevel@tonic-gate 					} else if (strncmp(NCA_MOD_NAME,
3007c478bd9Sstevel@tonic-gate 					    strlist.sl_modlist[i].l_name,
3017c478bd9Sstevel@tonic-gate 					    strlen(NCA_MOD_NAME)) == 0) {
3027c478bd9Sstevel@tonic-gate 						found_nca = B_TRUE;
3037c478bd9Sstevel@tonic-gate 					}
3047c478bd9Sstevel@tonic-gate 				}
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	if (found_nca) {
3107c478bd9Sstevel@tonic-gate 		return (FOUND_NCA);
3117c478bd9Sstevel@tonic-gate 	} else if (found_ip) {
3127c478bd9Sstevel@tonic-gate 		if (debug) {
3137c478bd9Sstevel@tonic-gate 			logdebug("NCA is at position %d in the stream.\n", pos);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		return (pos);
3167c478bd9Sstevel@tonic-gate 	} else {
3177c478bd9Sstevel@tonic-gate 		if (debug) {
3187c478bd9Sstevel@tonic-gate 			logdebug("Cannot find IP??\n");
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		return (FOUND_NONE);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * To set the local IP address and default router ethernet address.
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * Param:
3287c478bd9Sstevel@tonic-gate  *	int fd: the fd to the lower IP stream.
3297c478bd9Sstevel@tonic-gate  *	struct in_addr local_addr: the IP address for this interface.
3307c478bd9Sstevel@tonic-gate  *	uchar_t *ether_addr: the ethernet address of the default router for
3317c478bd9Sstevel@tonic-gate  *		for this interface.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * Return:
3347c478bd9Sstevel@tonic-gate  *	-1 if the system does not support this NCA ioctl(), 0 otherwise.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate static int
3377c478bd9Sstevel@tonic-gate nca_set_nif(int fd, struct in_addr local_addr, uchar_t *ether_addr)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	struct nca_set_ioctl nca_ioctl;
3407c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
3417c478bd9Sstevel@tonic-gate 	int len;
3427c478bd9Sstevel@tonic-gate 	uchar_t *dst;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	strioc.ic_cmd = NCA_SET_IF;
3457c478bd9Sstevel@tonic-gate 	strioc.ic_timout = INFTIM;
3467c478bd9Sstevel@tonic-gate 	strioc.ic_len = sizeof (nca_ioctl);
3477c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&nca_ioctl;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	nca_ioctl.local_addr = local_addr.s_addr;
3507c478bd9Sstevel@tonic-gate 	dst = nca_ioctl.router_ether_addr;
3517c478bd9Sstevel@tonic-gate 	for (len = ETHERADDRL; len > 0; len--)
3527c478bd9Sstevel@tonic-gate 		*dst++ = *ether_addr++;
3537c478bd9Sstevel@tonic-gate 	nca_ioctl.action = ADD_DEF_ROUTE;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &strioc) < 0) {
3567c478bd9Sstevel@tonic-gate 		logperror("ioctl(NCA_SET_IF) failed");
3577c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
3587c478bd9Sstevel@tonic-gate 			return (-1);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * To setup the NCA stream.  First insert NCA into the proper position.
3657c478bd9Sstevel@tonic-gate  * Then tell NCA the local IP address and default router by using the
3667c478bd9Sstevel@tonic-gate  * NCA_SET_IF ioctl.
3677c478bd9Sstevel@tonic-gate  *
3687c478bd9Sstevel@tonic-gate  * Param:
3697c478bd9Sstevel@tonic-gate  *	boolean_t *active: (referenced) B_TRUE if NCA is setup to do active
3707c478bd9Sstevel@tonic-gate  *		connection.  If NCA does not support active connection,
3717c478bd9Sstevel@tonic-gate  *		in return, active will be set to B_FALSE.
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate static void
3747c478bd9Sstevel@tonic-gate nca_setup(boolean_t *active)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	int i;
3777c478bd9Sstevel@tonic-gate 	int udp_fd;
3787c478bd9Sstevel@tonic-gate 	int fd;
3797c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
3807c478bd9Sstevel@tonic-gate 	/* 128 is enough because interface name can only be LIFNAMSIZ long. */
3817c478bd9Sstevel@tonic-gate 	char err_buf[128];
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	mod.mod_name = NCA_MOD_NAME;
3847c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = AF_INET;
3857c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
3867c478bd9Sstevel@tonic-gate 		if (debug) {
3877c478bd9Sstevel@tonic-gate 			logdebug("Plumbing NCA for %s\n", nif_list[i].name);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		/* This interface does not exist according to IP. */
3907c478bd9Sstevel@tonic-gate 		if (nif_list[i].local_addr.s_addr == 0) {
3917c478bd9Sstevel@tonic-gate 			continue;
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nif_list[i].name,
3947c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		if (ip_domux2fd(&udp_fd, &fd) < 0) {
3977c478bd9Sstevel@tonic-gate 			continue;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 		if (ioctl(udp_fd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
4007c478bd9Sstevel@tonic-gate 			(void) snprintf(err_buf, sizeof (err_buf),
4017c478bd9Sstevel@tonic-gate 			    "ioctl(I_PUNLINK) for %s failed", nif_list[i].name);
4027c478bd9Sstevel@tonic-gate 			logperror(err_buf);
4037c478bd9Sstevel@tonic-gate 			(void) close(udp_fd);
4047c478bd9Sstevel@tonic-gate 			(void) close(fd);
4057c478bd9Sstevel@tonic-gate 			continue;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 		if ((mod.pos = find_nca_pos(fd)) < 0) {
4087c478bd9Sstevel@tonic-gate 			if (mod.pos == FOUND_NCA) {
4097c478bd9Sstevel@tonic-gate 				if (debug) {
4107c478bd9Sstevel@tonic-gate 					logdebug("Find NCA in the %s"
4117c478bd9Sstevel@tonic-gate 					    " stream\n", nif_list[i].name);
4127c478bd9Sstevel@tonic-gate 				}
4137c478bd9Sstevel@tonic-gate 				/* Just skip plumbing NCA. */
4147c478bd9Sstevel@tonic-gate 				goto set_nif;
4157c478bd9Sstevel@tonic-gate 			}
4167c478bd9Sstevel@tonic-gate 			if (debug) {
4177c478bd9Sstevel@tonic-gate 				logdebug("Cannot find pos for %s\n",
4187c478bd9Sstevel@tonic-gate 				    nif_list[i].name);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			goto clean_up;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		if (ioctl(fd, _I_INSERT, (caddr_t)&mod) < 0) {
4237c478bd9Sstevel@tonic-gate 			(void) snprintf(err_buf, sizeof (err_buf),
4247c478bd9Sstevel@tonic-gate 			    "ioctl(_I_INSERT) for %s failed", nif_list[i].name);
4257c478bd9Sstevel@tonic-gate 			logperror(err_buf);
4267c478bd9Sstevel@tonic-gate 			goto clean_up;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		/*
4307c478bd9Sstevel@tonic-gate 		 * Only do the following if NCA is also used to make
4317c478bd9Sstevel@tonic-gate 		 * outgoing connections, and all necessary info is
4327c478bd9Sstevel@tonic-gate 		 * there.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate set_nif:
4357c478bd9Sstevel@tonic-gate 		if (*active && nif_list[i].router_addr.s_addr != 0) {
4367c478bd9Sstevel@tonic-gate 			if (nca_set_nif(fd, nif_list[i].local_addr,
4377c478bd9Sstevel@tonic-gate 			    nif_list[i].router_ether_addr) < 0) {
4387c478bd9Sstevel@tonic-gate 				/*
4397c478bd9Sstevel@tonic-gate 				 * The system does not support this ioctl()!
4407c478bd9Sstevel@tonic-gate 				 * Skip all active stack processing but
4417c478bd9Sstevel@tonic-gate 				 * continue to plumb NCA.
4427c478bd9Sstevel@tonic-gate 				 */
4437c478bd9Sstevel@tonic-gate 				logwarn("NCA does not support active stack!");
4447c478bd9Sstevel@tonic-gate 				*active = B_FALSE;
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate clean_up:
4487c478bd9Sstevel@tonic-gate 		ip_plink(udp_fd, fd);
4497c478bd9Sstevel@tonic-gate 		(void) close(udp_fd);
4507c478bd9Sstevel@tonic-gate 		(void) close(fd);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * To get IP address of network interface from IP.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static int
4587c478bd9Sstevel@tonic-gate get_if_ip_addr(void)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	int sock;
4617c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
4627c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
4637c478bd9Sstevel@tonic-gate 	struct lifreq *lifr;
4647c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
4657c478bd9Sstevel@tonic-gate 	char *buf;
4667c478bd9Sstevel@tonic-gate 	int num_lifr;
4677c478bd9Sstevel@tonic-gate 	int i, j;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/* NCA only supports IPv4... */
4707c478bd9Sstevel@tonic-gate 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
4717c478bd9Sstevel@tonic-gate 		logperror(gettext("Cannot open socket"));
4727c478bd9Sstevel@tonic-gate 		return (-1);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
4757c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = 0;
4767c478bd9Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
4777c478bd9Sstevel@tonic-gate 		logperror(gettext("ioctl(SIOCGLIFNUM) failed"));
4787c478bd9Sstevel@tonic-gate 		(void) close(sock);
4797c478bd9Sstevel@tonic-gate 		return (-1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	buf = (char *)calloc(lifn.lifn_count, sizeof (struct lifreq));
4827c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
4837c478bd9Sstevel@tonic-gate 		logperror(gettext("calloc() failed"));
4847c478bd9Sstevel@tonic-gate 		(void) close(sock);
4857c478bd9Sstevel@tonic-gate 		return (-1);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
4897c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = 0;
4907c478bd9Sstevel@tonic-gate 	lifc.lifc_len = lifn.lifn_count * sizeof (struct lifreq);
4917c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFCONF, (char *)&lifc) < 0) {
4947c478bd9Sstevel@tonic-gate 		/*
4957c478bd9Sstevel@tonic-gate 		 * NCA is set up after all the interfaces have been
4967c478bd9Sstevel@tonic-gate 		 * plumbed.  So normally we should not get any error.
4977c478bd9Sstevel@tonic-gate 		 * Just abort if we encounter an error.
4987c478bd9Sstevel@tonic-gate 		 */
4997c478bd9Sstevel@tonic-gate 		logperror(gettext("ioctl(SIOCGLIFCONF) failed"));
5007c478bd9Sstevel@tonic-gate 		free(buf);
5017c478bd9Sstevel@tonic-gate 		(void) close(sock);
5027c478bd9Sstevel@tonic-gate 		return (-1);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	num_lifr = lifc.lifc_len / sizeof (struct lifreq);
5057c478bd9Sstevel@tonic-gate 	/* Find the interface and copy the local IP address. */
5067c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
5077c478bd9Sstevel@tonic-gate 		lifr = (struct lifreq *)lifc.lifc_req;
5087c478bd9Sstevel@tonic-gate 		for (j = num_lifr; j > 0; j--, lifr++) {
5097c478bd9Sstevel@tonic-gate 			/* Again, NCA only supports IPv4. */
5107c478bd9Sstevel@tonic-gate 			if (lifr->lifr_addr.ss_family != AF_INET)
5117c478bd9Sstevel@tonic-gate 				continue;
5127c478bd9Sstevel@tonic-gate 			if (strncmp(nif_list[i].name, lifr->lifr_name,
5137c478bd9Sstevel@tonic-gate 			    strlen(nif_list[i].name)) == 0) {
5147c478bd9Sstevel@tonic-gate 				sin = (struct sockaddr_in *)&lifr->lifr_addr;
5157c478bd9Sstevel@tonic-gate 				nif_list[i].local_addr = sin->sin_addr;
5167c478bd9Sstevel@tonic-gate 				if (debug) {
5177c478bd9Sstevel@tonic-gate 					logdebug("IP address of %s: %s\n",
5187c478bd9Sstevel@tonic-gate 					    nif_list[i].name,
5197c478bd9Sstevel@tonic-gate 					    inet_ntoa(sin->sin_addr));
5207c478bd9Sstevel@tonic-gate 				}
5217c478bd9Sstevel@tonic-gate 				break;
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		if (j == 0) {
5257c478bd9Sstevel@tonic-gate 			/*
5267c478bd9Sstevel@tonic-gate 			 * The interface does not exist according to IP!
5277c478bd9Sstevel@tonic-gate 			 * Log a warning and go on.
5287c478bd9Sstevel@tonic-gate 			 */
5297c478bd9Sstevel@tonic-gate 			logwarn(gettext("Network interface %s"
5307c478bd9Sstevel@tonic-gate 			    " does not exist!\n"), nif_list[i].name);
5317c478bd9Sstevel@tonic-gate 			/*
5327c478bd9Sstevel@tonic-gate 			 * Set local_addr to 0 so that nca_setup() will
5337c478bd9Sstevel@tonic-gate 			 * not do anything for this interface.
5347c478bd9Sstevel@tonic-gate 			 */
5357c478bd9Sstevel@tonic-gate 			nif_list[i].local_addr.s_addr = 0;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	free(buf);
5397c478bd9Sstevel@tonic-gate 	(void) close(sock);
5407c478bd9Sstevel@tonic-gate 	return (0);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Get MIB2 info from IP.
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * Param:
5477c478bd9Sstevel@tonic-gate  *	int sd: descriptor to IP to send down mib request.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate static mib_item_t *
5507c478bd9Sstevel@tonic-gate mibget(int sd)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	char			buf[1024];
5537c478bd9Sstevel@tonic-gate 	int			flags;
5547c478bd9Sstevel@tonic-gate 	int			i, j, getcode;
5557c478bd9Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
5567c478bd9Sstevel@tonic-gate 	/* LINTED */
5577c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
5587c478bd9Sstevel@tonic-gate 	/* LINTED */
5597c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
5607c478bd9Sstevel@tonic-gate 	/* LINTED */
5617c478bd9Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
5627c478bd9Sstevel@tonic-gate 	struct opthdr		*req;
5637c478bd9Sstevel@tonic-gate 	mib_item_t		*first_item = (mib_item_t *)0;
5647c478bd9Sstevel@tonic-gate 	mib_item_t		*last_item  = (mib_item_t *)0;
5657c478bd9Sstevel@tonic-gate 	mib_item_t		*temp;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
5687c478bd9Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
5697c478bd9Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
5707c478bd9Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
5717c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
5727c478bd9Sstevel@tonic-gate 	req->level = MIB2_IP;		/* any MIB2_xxx value ok here */
5737c478bd9Sstevel@tonic-gate 	req->name  = 0;
5747c478bd9Sstevel@tonic-gate 	req->len   = 0;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	ctlbuf.buf = buf;
5777c478bd9Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
5787c478bd9Sstevel@tonic-gate 	flags = 0;
5797c478bd9Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
5807c478bd9Sstevel@tonic-gate 		logperror("mibget: putmsg(ctl) failed");
5817c478bd9Sstevel@tonic-gate 		goto error_exit;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
5867c478bd9Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
5877c478bd9Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
5887c478bd9Sstevel@tonic-gate 	 * len is the size of the data part of the message.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
5917c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
5927c478bd9Sstevel@tonic-gate 	j = 1;
5937c478bd9Sstevel@tonic-gate 	for (;;) {
5947c478bd9Sstevel@tonic-gate 		flags = 0;
5957c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
5967c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
5977c478bd9Sstevel@tonic-gate 			logperror("mibget getmsg(ctl) failed");
5987c478bd9Sstevel@tonic-gate 			if (debug) {
5997c478bd9Sstevel@tonic-gate 				logdebug("#   level   name    len\n");
6007c478bd9Sstevel@tonic-gate 				i = 0;
6017c478bd9Sstevel@tonic-gate 				for (last_item = first_item; last_item;
6027c478bd9Sstevel@tonic-gate 					last_item = last_item->next_item)
6037c478bd9Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
6047c478bd9Sstevel@tonic-gate 					    ++i,
6057c478bd9Sstevel@tonic-gate 					    last_item->group,
6067c478bd9Sstevel@tonic-gate 					    last_item->mib_id,
6077c478bd9Sstevel@tonic-gate 					    last_item->length);
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 			goto error_exit;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		if (getcode == 0 &&
6127c478bd9Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
6137c478bd9Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
6147c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
6157c478bd9Sstevel@tonic-gate 		    req->len == 0) {
6167c478bd9Sstevel@tonic-gate 			if (debug) {
6177c478bd9Sstevel@tonic-gate 				logdebug("mibget getmsg() %d returned "
6187c478bd9Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
6197c478bd9Sstevel@tonic-gate 				    j, req->level, req->name);
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
6257c478bd9Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
6267c478bd9Sstevel@tonic-gate 			logwarn("mibget %d gives T_ERROR_ACK: TLI_error ="
6277c478bd9Sstevel@tonic-gate 			    " 0x%lx, UNIX_error = 0x%lx\n",
6287c478bd9Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
6297c478bd9Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
6307c478bd9Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
6317c478bd9Sstevel@tonic-gate 			goto error_exit;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		if (getcode != MOREDATA ||
6357c478bd9Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
6367c478bd9Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
6377c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
6387c478bd9Sstevel@tonic-gate 			logwarn("mibget getmsg(ctl) %d returned %d, "
6397c478bd9Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
6407c478bd9Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
6417c478bd9Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK) {
6427c478bd9Sstevel@tonic-gate 				logwarn("T_OPTMGMT_ACK: "
6437c478bd9Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
6447c478bd9Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			errno = ENOMSG;
6477c478bd9Sstevel@tonic-gate 			goto error_exit;
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
6517c478bd9Sstevel@tonic-gate 		if (!temp) {
6527c478bd9Sstevel@tonic-gate 			logperror("mibget malloc failed");
6537c478bd9Sstevel@tonic-gate 			goto error_exit;
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		if (last_item)
6567c478bd9Sstevel@tonic-gate 			last_item->next_item = temp;
6577c478bd9Sstevel@tonic-gate 		else
6587c478bd9Sstevel@tonic-gate 			first_item = temp;
6597c478bd9Sstevel@tonic-gate 		last_item = temp;
6607c478bd9Sstevel@tonic-gate 		last_item->next_item = (mib_item_t *)0;
6617c478bd9Sstevel@tonic-gate 		last_item->group = req->level;
6627c478bd9Sstevel@tonic-gate 		last_item->mib_id = req->name;
6637c478bd9Sstevel@tonic-gate 		last_item->length = req->len;
6647c478bd9Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
6677c478bd9Sstevel@tonic-gate 		databuf.buf    = last_item->valp;
6687c478bd9Sstevel@tonic-gate 		databuf.len    = 0;
6697c478bd9Sstevel@tonic-gate 		flags = 0;
6707c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
6717c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
6727c478bd9Sstevel@tonic-gate 			logperror("mibget getmsg(data) failed");
6737c478bd9Sstevel@tonic-gate 			goto error_exit;
6747c478bd9Sstevel@tonic-gate 		} else if (getcode != 0) {
6757c478bd9Sstevel@tonic-gate 			logwarn("mibget getmsg(data) returned %d, "
6767c478bd9Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
6777c478bd9Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
6787c478bd9Sstevel@tonic-gate 			goto error_exit;
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 		j++;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate error_exit:;
6847c478bd9Sstevel@tonic-gate 	while (first_item) {
6857c478bd9Sstevel@tonic-gate 		last_item = first_item;
6867c478bd9Sstevel@tonic-gate 		first_item = first_item->next_item;
6877c478bd9Sstevel@tonic-gate 		free(last_item);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	return (first_item);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Examine the IPv4 routing table for default routers.  For each interface,
6947c478bd9Sstevel@tonic-gate  * find its default router.
6957c478bd9Sstevel@tonic-gate  *
6967c478bd9Sstevel@tonic-gate  * Param:
6977c478bd9Sstevel@tonic-gate  *	mib2_ipRouteEntry_t *buf: the mib info buffer.
6987c478bd9Sstevel@tonic-gate  *	size_t len: length of buffer.
6997c478bd9Sstevel@tonic-gate  *	boolean_t *changed (referenced): set to B_TRUE if there is a change
7007c478bd9Sstevel@tonic-gate  *		in router info.
7017c478bd9Sstevel@tonic-gate  *
7027c478bd9Sstevel@tonic-gate  * Return:
7037c478bd9Sstevel@tonic-gate  *	number of default router found.
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate static int
7067c478bd9Sstevel@tonic-gate ire_process(mib2_ipRouteEntry_t *buf, size_t len, boolean_t *changed)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t 	*rp;
7097c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t 	*rp1;
7107c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t 	*rp2;
7117c478bd9Sstevel@tonic-gate 	struct	in_addr		nexthop_v4;
7127c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*endp;
7137c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
7147c478bd9Sstevel@tonic-gate 	char			*cp;
7157c478bd9Sstevel@tonic-gate 	int			i;
7167c478bd9Sstevel@tonic-gate 	int			ifname_len;
7177c478bd9Sstevel@tonic-gate 	boolean_t		found;
7187c478bd9Sstevel@tonic-gate 	int			num_found = 0;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (len == 0)
7217c478bd9Sstevel@tonic-gate 		return (0);
7227c478bd9Sstevel@tonic-gate 	endp = buf + (len / sizeof (mib2_ipRouteEntry_t));
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
7257c478bd9Sstevel@tonic-gate 		/*
7267c478bd9Sstevel@tonic-gate 		 * Loop thru the routing table entries. Process any
7277c478bd9Sstevel@tonic-gate 		 * IRE_DEFAULT ire.  Ignore the others.  For each such
7287c478bd9Sstevel@tonic-gate 		 * ire, get the nexthop gateway address.
7297c478bd9Sstevel@tonic-gate 		 */
7307c478bd9Sstevel@tonic-gate 		found = B_FALSE;
7317c478bd9Sstevel@tonic-gate 		for (rp = buf; rp < endp; rp++) {
7327c478bd9Sstevel@tonic-gate 			/*
7337c478bd9Sstevel@tonic-gate 			 * NCA is only interested in default routes associated
7347c478bd9Sstevel@tonic-gate 			 * with an interface.
7357c478bd9Sstevel@tonic-gate 			 */
7367c478bd9Sstevel@tonic-gate 			if (!(rp->ipRouteInfo.re_ire_type & IRE_DEFAULT)) {
7377c478bd9Sstevel@tonic-gate 				continue;
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate 			/*  Get the nexthop address. */
7407c478bd9Sstevel@tonic-gate 			nexthop_v4.s_addr = rp->ipRouteNextHop;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 			/*
7437c478bd9Sstevel@tonic-gate 			 * Right now, not all IREs have the interface name
7447c478bd9Sstevel@tonic-gate 			 * it is associated with.
7457c478bd9Sstevel@tonic-gate 			 */
7467c478bd9Sstevel@tonic-gate 			if (rp->ipRouteIfIndex.o_length == 0) {
7477c478bd9Sstevel@tonic-gate 				/*
7487c478bd9Sstevel@tonic-gate 				 * We don't have the outgoing interface in
7497c478bd9Sstevel@tonic-gate 				 * this case.  Get the nexthop address. Then
7507c478bd9Sstevel@tonic-gate 				 * determine the outgoing interface, by
7517c478bd9Sstevel@tonic-gate 				 * examining all interface IREs, and
7527c478bd9Sstevel@tonic-gate 				 * picking the match.
7537c478bd9Sstevel@tonic-gate 				 */
7547c478bd9Sstevel@tonic-gate 				for (rp1 = buf; rp1 < endp; rp1++) {
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 				if (!(rp1->ipRouteInfo.re_ire_type &
7577c478bd9Sstevel@tonic-gate 				    IRE_INTERFACE)) {
7587c478bd9Sstevel@tonic-gate 					continue;
7597c478bd9Sstevel@tonic-gate 				}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 				/*
7627c478bd9Sstevel@tonic-gate 				 * Determine the interface IRE that
7637c478bd9Sstevel@tonic-gate 				 * matches the nexthop. i.e.
7647c478bd9Sstevel@tonic-gate 				 * (IRE addr & IRE mask) ==
7657c478bd9Sstevel@tonic-gate 				 * (nexthop & IRE mask)
7667c478bd9Sstevel@tonic-gate 				 */
7677c478bd9Sstevel@tonic-gate 				if ((rp1->ipRouteDest & rp1->ipRouteMask) ==
7687c478bd9Sstevel@tonic-gate 				    (nexthop_v4.s_addr & rp1->ipRouteMask)) {
7697c478bd9Sstevel@tonic-gate 					/*
7707c478bd9Sstevel@tonic-gate 					 * We found the interface to go to
7717c478bd9Sstevel@tonic-gate 					 * the default router.  Check the
7727c478bd9Sstevel@tonic-gate 					 * interface name.
7737c478bd9Sstevel@tonic-gate 					 */
7747c478bd9Sstevel@tonic-gate 					/* Can this be possible?? */
7757c478bd9Sstevel@tonic-gate 					if (rp1->ipRouteIfIndex.o_length == 0)
7767c478bd9Sstevel@tonic-gate 						continue;
7777c478bd9Sstevel@tonic-gate 					rp2 = rp1;
7787c478bd9Sstevel@tonic-gate 					break;
7797c478bd9Sstevel@tonic-gate 				}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 				} /* End inner for loop. */
7827c478bd9Sstevel@tonic-gate 			} else {
7837c478bd9Sstevel@tonic-gate 				rp2 = rp;
7847c478bd9Sstevel@tonic-gate 			}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 			ifname_len = MIN(rp2->ipRouteIfIndex.o_length,
7877c478bd9Sstevel@tonic-gate 			    sizeof (ifname) - 1);
7887c478bd9Sstevel@tonic-gate 			(void) memcpy(ifname, rp2->ipRouteIfIndex.o_bytes,
7897c478bd9Sstevel@tonic-gate 			    ifname_len);
7907c478bd9Sstevel@tonic-gate 			ifname[ifname_len] = '\0';
7917c478bd9Sstevel@tonic-gate 			if (ifname[0] == '\0')
7927c478bd9Sstevel@tonic-gate 				continue;
7937c478bd9Sstevel@tonic-gate 			cp = strchr(ifname, IF_SEPARATOR);
7947c478bd9Sstevel@tonic-gate 			if (cp != NULL)
7957c478bd9Sstevel@tonic-gate 				*cp = '\0';
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 			/* We are sure both are NULL terminated. */
7987c478bd9Sstevel@tonic-gate 			if (strcmp(nif_list[i].name, ifname) == 0) {
7997c478bd9Sstevel@tonic-gate 				/* No change, do not do anything. */
8007c478bd9Sstevel@tonic-gate 				if (nexthop_v4.s_addr ==
8017c478bd9Sstevel@tonic-gate 				    nif_list[i].router_addr.s_addr) {
8027c478bd9Sstevel@tonic-gate 					found = B_TRUE;
8037c478bd9Sstevel@tonic-gate 					break;
8047c478bd9Sstevel@tonic-gate 				}
8057c478bd9Sstevel@tonic-gate 				nif_list[i].router_addr.s_addr =
8067c478bd9Sstevel@tonic-gate 				    nexthop_v4.s_addr;
8077c478bd9Sstevel@tonic-gate 				if (debug) {
8087c478bd9Sstevel@tonic-gate 					logdebug("Get default"
8097c478bd9Sstevel@tonic-gate 					    " router for %s: %s\n", ifname,
8107c478bd9Sstevel@tonic-gate 					    inet_ntoa(nexthop_v4));
8117c478bd9Sstevel@tonic-gate 				}
8127c478bd9Sstevel@tonic-gate 				found = B_TRUE;
8137c478bd9Sstevel@tonic-gate 				*changed = B_TRUE;
8147c478bd9Sstevel@tonic-gate 				break;
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		if (!found) {
8197c478bd9Sstevel@tonic-gate 			/*
8207c478bd9Sstevel@tonic-gate 			 * The interface does not have a default router.
8217c478bd9Sstevel@tonic-gate 			 * Log a warning and go on.
8227c478bd9Sstevel@tonic-gate 			 */
8237c478bd9Sstevel@tonic-gate 			logwarn(gettext("Network interface %s"
8247c478bd9Sstevel@tonic-gate 			    " does not have a default router.\n"),
8257c478bd9Sstevel@tonic-gate 			    nif_list[i].name);
8267c478bd9Sstevel@tonic-gate 			/*
8277c478bd9Sstevel@tonic-gate 			 * Set router_addr to 0 so that we will
8287c478bd9Sstevel@tonic-gate 			 * not do anything for this interface.
8297c478bd9Sstevel@tonic-gate 			 */
8307c478bd9Sstevel@tonic-gate 			nif_list[i].router_addr.s_addr = 0;
8317c478bd9Sstevel@tonic-gate 		} else {
8327c478bd9Sstevel@tonic-gate 			num_found++;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	return (num_found);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * Examine the ARP table to find ethernet address for default routers.
8407c478bd9Sstevel@tonic-gate  *
8417c478bd9Sstevel@tonic-gate  * Param:
8427c478bd9Sstevel@tonic-gate  *	mib2_ipNetToMdeiaEntry_t *buf: the mib info buffer.
8437c478bd9Sstevel@tonic-gate  *	size_t len: length of buffer.
8447c478bd9Sstevel@tonic-gate  *	boolean_t *changed (referenced): set to B_TRUE if there is any change
8457c478bd9Sstevel@tonic-gate  *		in ethernet address for any default router.
8467c478bd9Sstevel@tonic-gate  *
8477c478bd9Sstevel@tonic-gate  * Return:
8487c478bd9Sstevel@tonic-gate  *	number of ethernet address found.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate static int
8517c478bd9Sstevel@tonic-gate arp_process(mib2_ipNetToMediaEntry_t *buf, size_t len, boolean_t *changed)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t 	*rp;
8547c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*endp;
8557c478bd9Sstevel@tonic-gate 	int				i;
8567c478bd9Sstevel@tonic-gate 	boolean_t			found;
8577c478bd9Sstevel@tonic-gate 	int				num_found = 0;
8587c478bd9Sstevel@tonic-gate 	uchar_t				*src, *dst;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (len == 0)
8617c478bd9Sstevel@tonic-gate 		return (0);
8627c478bd9Sstevel@tonic-gate 	endp = buf + (len / sizeof (mib2_ipNetToMediaEntry_t));
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
8657c478bd9Sstevel@tonic-gate 		/*
8667c478bd9Sstevel@tonic-gate 		 * Loop thru the arp table entries and find the ethernet
8677c478bd9Sstevel@tonic-gate 		 * address of those default routers.
8687c478bd9Sstevel@tonic-gate 		 */
8697c478bd9Sstevel@tonic-gate 		if (nif_list[i].router_addr.s_addr == 0)
8707c478bd9Sstevel@tonic-gate 			continue;
8717c478bd9Sstevel@tonic-gate 		found = B_FALSE;
8727c478bd9Sstevel@tonic-gate 		for (rp = buf; rp < endp; rp++) {
8737c478bd9Sstevel@tonic-gate 			if (rp->ipNetToMediaNetAddress ==
8747c478bd9Sstevel@tonic-gate 			    nif_list[i].router_addr.s_addr) {
8757c478bd9Sstevel@tonic-gate 				/*
8767c478bd9Sstevel@tonic-gate 				 * Sanity check.  Make sure that this
8777c478bd9Sstevel@tonic-gate 				 * default router is only reachable thru this
8787c478bd9Sstevel@tonic-gate 				 * interface.
8797c478bd9Sstevel@tonic-gate 				 */
8807c478bd9Sstevel@tonic-gate 				if (rp->ipNetToMediaIfIndex.o_length !=
8817c478bd9Sstevel@tonic-gate 				    strlen(nif_list[i].name) ||
8827c478bd9Sstevel@tonic-gate 				    strncmp(rp->ipNetToMediaIfIndex.o_bytes,
8837c478bd9Sstevel@tonic-gate 					nif_list[i].name,
8847c478bd9Sstevel@tonic-gate 					rp->ipNetToMediaIfIndex.o_length) !=
8857c478bd9Sstevel@tonic-gate 				    0) {
8867c478bd9Sstevel@tonic-gate 					break;
8877c478bd9Sstevel@tonic-gate 				}
8887c478bd9Sstevel@tonic-gate 				/* No change, do not do anything. */
8897c478bd9Sstevel@tonic-gate 				if (bcmp(nif_list[i].router_ether_addr,
8907c478bd9Sstevel@tonic-gate 				    rp->ipNetToMediaPhysAddress.o_bytes,
8917c478bd9Sstevel@tonic-gate 				    ETHERADDRL) == 0) {
8927c478bd9Sstevel@tonic-gate 					found = B_TRUE;
8937c478bd9Sstevel@tonic-gate 					continue;
8947c478bd9Sstevel@tonic-gate 				}
8957c478bd9Sstevel@tonic-gate 				dst = nif_list[i].router_ether_addr;
8967c478bd9Sstevel@tonic-gate 				src = (uchar_t *)
8977c478bd9Sstevel@tonic-gate 				    rp->ipNetToMediaPhysAddress.o_bytes;
8987c478bd9Sstevel@tonic-gate 				for (len = ETHERADDRL; len > 0; len--)
8997c478bd9Sstevel@tonic-gate 					*dst++ = *src++;
9007c478bd9Sstevel@tonic-gate 				if (debug) {
9017c478bd9Sstevel@tonic-gate 					int j;
9027c478bd9Sstevel@tonic-gate 					uchar_t *cp;
9037c478bd9Sstevel@tonic-gate 					char err_buf[128];
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 					(void) snprintf(err_buf,
9067c478bd9Sstevel@tonic-gate 					    sizeof (err_buf),
9077c478bd9Sstevel@tonic-gate 					    "Get address for %s: ",
9087c478bd9Sstevel@tonic-gate 					    inet_ntoa(nif_list[i].router_addr));
9097c478bd9Sstevel@tonic-gate 					cp = (uchar_t *)
9107c478bd9Sstevel@tonic-gate 					    nif_list[i].router_ether_addr;
9117c478bd9Sstevel@tonic-gate 					for (j = 0; j < ETHERADDRL; j++) {
9127c478bd9Sstevel@tonic-gate 						(void) sprintf(err_buf +
9137c478bd9Sstevel@tonic-gate 						    strlen(err_buf),
9147c478bd9Sstevel@tonic-gate 						    "%02x:", 0xff & cp[j]);
9157c478bd9Sstevel@tonic-gate 					}
9167c478bd9Sstevel@tonic-gate 					(void) sprintf(err_buf +
9177c478bd9Sstevel@tonic-gate 					    strlen(err_buf) - 1, "\n");
9187c478bd9Sstevel@tonic-gate 					logdebug(err_buf);
9197c478bd9Sstevel@tonic-gate 				}
9207c478bd9Sstevel@tonic-gate 				found = B_TRUE;
9217c478bd9Sstevel@tonic-gate 				*changed = B_TRUE;
9227c478bd9Sstevel@tonic-gate 			}
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 		if (!found) {
9257c478bd9Sstevel@tonic-gate 			logwarn("Cannot reach %s using %s\n",
9267c478bd9Sstevel@tonic-gate 			    inet_ntoa(nif_list[i].router_addr),
9277c478bd9Sstevel@tonic-gate 			    nif_list[i].name);
9287c478bd9Sstevel@tonic-gate 			/* Clear this default router. */
9297c478bd9Sstevel@tonic-gate 			nif_list[i].router_addr.s_addr = 0;
9307c478bd9Sstevel@tonic-gate 		} else {
9317c478bd9Sstevel@tonic-gate 			num_found++;
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 	return (num_found);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Get IP address of default routers for each interface.
9397c478bd9Sstevel@tonic-gate  *
9407c478bd9Sstevel@tonic-gate  * Param:
9417c478bd9Sstevel@tonic-gate  *	mib_item_t *item: the mib info buffer.
9427c478bd9Sstevel@tonic-gate  *	boolean_t *changed (referenced): set to B_TRUE if there is any change
9437c478bd9Sstevel@tonic-gate  *		in router info.
9447c478bd9Sstevel@tonic-gate  *
9457c478bd9Sstevel@tonic-gate  * Return:
9467c478bd9Sstevel@tonic-gate  *	-1 if there is no router found, 0 otherwise.
9477c478bd9Sstevel@tonic-gate  */
9487c478bd9Sstevel@tonic-gate static int
9497c478bd9Sstevel@tonic-gate get_router_ip_addr(mib_item_t *item, boolean_t *changed)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	int found = 0;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
9547c478bd9Sstevel@tonic-gate 		/* NCA does not support IPv6... */
9557c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_ROUTE))
9567c478bd9Sstevel@tonic-gate 			continue;
9577c478bd9Sstevel@tonic-gate 		/* LINTED */
9587c478bd9Sstevel@tonic-gate 		found += ire_process((mib2_ipRouteEntry_t *)item->valp,
9597c478bd9Sstevel@tonic-gate 		    item->length, changed);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	if (found == 0)
9627c478bd9Sstevel@tonic-gate 		return (-1);
9637c478bd9Sstevel@tonic-gate 	else
9647c478bd9Sstevel@tonic-gate 		return (0);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate  * Get Ethernet address for each default router from ARP.
9697c478bd9Sstevel@tonic-gate  *
9707c478bd9Sstevel@tonic-gate  * Param:
9717c478bd9Sstevel@tonic-gate  *	mib_item_t *item: the mib info buffer.
9727c478bd9Sstevel@tonic-gate  *	boolean_t *changed (referenced): set to B_TRUE if there is any change
9737c478bd9Sstevel@tonic-gate  *		in ethernet address of router.
9747c478bd9Sstevel@tonic-gate  *
9757c478bd9Sstevel@tonic-gate  * Return:
9767c478bd9Sstevel@tonic-gate  *	-1 if there is no ethernet address found, 0 otherwise.
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate static int
9797c478bd9Sstevel@tonic-gate get_router_ether_addr(mib_item_t *item, boolean_t *changed)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	int found = 0;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
9847c478bd9Sstevel@tonic-gate 		/* NCA does not support IPv6... */
9857c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
9867c478bd9Sstevel@tonic-gate 			continue;
9877c478bd9Sstevel@tonic-gate 		/* LINTED */
9887c478bd9Sstevel@tonic-gate 		found += arp_process((mib2_ipNetToMediaEntry_t *)item->valp,
9897c478bd9Sstevel@tonic-gate 		    item->length, changed);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 	if (found == 0)
9927c478bd9Sstevel@tonic-gate 		return (-1);
9937c478bd9Sstevel@tonic-gate 	else
9947c478bd9Sstevel@tonic-gate 		return (0);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * Ping all default routers.  It just uses system(3F) to call
9997c478bd9Sstevel@tonic-gate  * ping(1M) to do the job...
10007c478bd9Sstevel@tonic-gate  */
10017c478bd9Sstevel@tonic-gate static void
10027c478bd9Sstevel@tonic-gate ping_them(void)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	int i;
10057c478bd9Sstevel@tonic-gate 	char ping_cmd[128];
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
10087c478bd9Sstevel@tonic-gate 		if (nif_list[i].router_addr.s_addr != 0) {
10097c478bd9Sstevel@tonic-gate 			(void) snprintf(ping_cmd, sizeof (ping_cmd),
10107c478bd9Sstevel@tonic-gate 			    "%s %s > /dev/null 2>&1",
10117c478bd9Sstevel@tonic-gate 			    ping_prog,
10127c478bd9Sstevel@tonic-gate 			    inet_ntoa(nif_list[i].router_addr));
10137c478bd9Sstevel@tonic-gate 			(void) system(ping_cmd);
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate  * To get default router info (both IP address and ethernet address) for
10207c478bd9Sstevel@tonic-gate  * each configured interface from IP.
10217c478bd9Sstevel@tonic-gate  *
10227c478bd9Sstevel@tonic-gate  * Param:
10237c478bd9Sstevel@tonic-gate  *	boolean_t *changed (referenced): set to B_TRUE if there is any change
10247c478bd9Sstevel@tonic-gate  *		of info.
10257c478bd9Sstevel@tonic-gate  *
10267c478bd9Sstevel@tonic-gate  * Return:
10277c478bd9Sstevel@tonic-gate  *	-1 if there is any error, 0 if everything is fine.
10287c478bd9Sstevel@tonic-gate  */
10297c478bd9Sstevel@tonic-gate static int
10307c478bd9Sstevel@tonic-gate get_if_info(boolean_t *changed)
10317c478bd9Sstevel@tonic-gate {
10327c478bd9Sstevel@tonic-gate 	int mib_fd;
10337c478bd9Sstevel@tonic-gate 	mib_item_t *item;
10347c478bd9Sstevel@tonic-gate 	boolean_t ip_changed = B_FALSE;
10357c478bd9Sstevel@tonic-gate 	boolean_t ether_changed = B_FALSE;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if ((mib_fd = open(IP_DEV_NAME, O_RDWR)) < 0) {
10387c478bd9Sstevel@tonic-gate 		logperror("cannot open ip to get router info");
10397c478bd9Sstevel@tonic-gate 		return (-1);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	if (ioctl(mib_fd, I_PUSH, ARP_MOD_NAME) == -1) {
10427c478bd9Sstevel@tonic-gate 		logperror("cannot push arp");
10437c478bd9Sstevel@tonic-gate 		goto err;
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	if ((item = mibget(mib_fd)) == NULL) {
10477c478bd9Sstevel@tonic-gate 		goto err;
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (get_router_ip_addr(item, &ip_changed) < 0) {
10517c478bd9Sstevel@tonic-gate 		goto err;
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 	/*
10547c478bd9Sstevel@tonic-gate 	 * Ping every routers to make sure that ARP has all their ethernet
10557c478bd9Sstevel@tonic-gate 	 * addresses.
10567c478bd9Sstevel@tonic-gate 	 */
10577c478bd9Sstevel@tonic-gate 	ping_them();
10587c478bd9Sstevel@tonic-gate 	/*
10597c478bd9Sstevel@tonic-gate 	 * If the router IP address is not changed, its ethernet address
10607c478bd9Sstevel@tonic-gate 	 * should not be changed.  But just in case there is some IP
10617c478bd9Sstevel@tonic-gate 	 * failover going on...
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	if (get_router_ether_addr(item, &ether_changed) < 0) {
10647c478bd9Sstevel@tonic-gate 		goto err;
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 	(void) close(mib_fd);
10677c478bd9Sstevel@tonic-gate 	*changed = ip_changed || ether_changed;
10687c478bd9Sstevel@tonic-gate 	return (0);
10697c478bd9Sstevel@tonic-gate err:
10707c478bd9Sstevel@tonic-gate 	(void) close(mib_fd);
10717c478bd9Sstevel@tonic-gate 	return (-1);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * To remove the default router from an interface.
10767c478bd9Sstevel@tonic-gate  *
10777c478bd9Sstevel@tonic-gate  * Param:
10787c478bd9Sstevel@tonic-gate  *	struct in_addr gw_addr: the IP address of the default router to be
10797c478bd9Sstevel@tonic-gate  *	removed.
10807c478bd9Sstevel@tonic-gate  */
10817c478bd9Sstevel@tonic-gate static void
10827c478bd9Sstevel@tonic-gate nca_del_nif(struct in_addr gw_addr)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	struct nca_set_ioctl nca_ioctl;
10857c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
10867c478bd9Sstevel@tonic-gate 	int i;
10877c478bd9Sstevel@tonic-gate 	int udp_fd, fd;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* Search for the interface for this router. */
10907c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_nif; i++) {
10917c478bd9Sstevel@tonic-gate 		if (nif_list[i].router_addr.s_addr == gw_addr.s_addr)
10927c478bd9Sstevel@tonic-gate 			break;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 	if (i == num_nif)
10957c478bd9Sstevel@tonic-gate 		return;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (ip_domux2fd(&udp_fd, &fd) < 0) {
10987c478bd9Sstevel@tonic-gate 		logwarn(gettext("Removing interface %s from the"
10997c478bd9Sstevel@tonic-gate 		    " configuration list.\n"), nif_list[i].name);
11007c478bd9Sstevel@tonic-gate 		nif_list[i].name[0] = 0;
11017c478bd9Sstevel@tonic-gate 		return;
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	if (ioctl(udp_fd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
11047c478bd9Sstevel@tonic-gate 		logwarn(gettext("Removing interface %s from the"
11057c478bd9Sstevel@tonic-gate 		    " configuration list.\n"), nif_list[i].name);
11067c478bd9Sstevel@tonic-gate 		nif_list[i].name[0] = 0;
11077c478bd9Sstevel@tonic-gate 		(void) close(udp_fd);
11087c478bd9Sstevel@tonic-gate 		(void) close(fd);
11097c478bd9Sstevel@tonic-gate 		return;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	strioc.ic_cmd = NCA_SET_IF;
11137c478bd9Sstevel@tonic-gate 	strioc.ic_timout = INFTIM;
11147c478bd9Sstevel@tonic-gate 	strioc.ic_len = sizeof (nca_ioctl);
11157c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&nca_ioctl;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	nca_ioctl.local_addr = 0;
11187c478bd9Sstevel@tonic-gate 	(void) memset(nca_ioctl.router_ether_addr, 0, ETHERADDRL);
11197c478bd9Sstevel@tonic-gate 	nca_ioctl.action = DEL_DEF_ROUTE;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &strioc) < 0) {
11227c478bd9Sstevel@tonic-gate 		logperror("ioctl(NCA_SET_IF) failed");
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 	ip_plink(udp_fd, fd);
11257c478bd9Sstevel@tonic-gate 	(void) close(udp_fd);
11267c478bd9Sstevel@tonic-gate 	(void) close(fd);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/* Clear the fields for this interface. */
11297c478bd9Sstevel@tonic-gate 	nif_list[i].router_addr.s_addr = 0;
11307c478bd9Sstevel@tonic-gate 	(void) memset(nif_list[i].router_ether_addr, 0, ETHERADDRL);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * Wait for any changes in the routing table.  If there are changes to
11357c478bd9Sstevel@tonic-gate  * IP address or router ethernet address, send down the info to NCA.
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate static void
11387c478bd9Sstevel@tonic-gate daemon_work(void)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	int n;
11417c478bd9Sstevel@tonic-gate 	int i;
11427c478bd9Sstevel@tonic-gate 	int udp_fd;
11437c478bd9Sstevel@tonic-gate 	int fd;
11447c478bd9Sstevel@tonic-gate 	int64_t msg[2048/8];
11457c478bd9Sstevel@tonic-gate 	struct rt_msghdr *rtm;
11467c478bd9Sstevel@tonic-gate 	boolean_t changed;
11477c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
11487c478bd9Sstevel@tonic-gate 	struct in_addr gw_addr;
11497c478bd9Sstevel@tonic-gate 	uchar_t *cp;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/* Loop forever waiting for any routing changes. */
11527c478bd9Sstevel@tonic-gate 	for (;;) {
11537c478bd9Sstevel@tonic-gate 		if (debug) {
11547c478bd9Sstevel@tonic-gate 			logdebug("Waiting to read routing info...\n");
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 		n = read(rt_fd, msg, sizeof (msg));
11577c478bd9Sstevel@tonic-gate 		/* Don't die...  Reinitialize socket and listen again. */
11587c478bd9Sstevel@tonic-gate 		if (n <= 0) {
11597c478bd9Sstevel@tonic-gate 			if (debug) {
11607c478bd9Sstevel@tonic-gate 				logdebug("Routing socket read error.\n");
11617c478bd9Sstevel@tonic-gate 			}
11627c478bd9Sstevel@tonic-gate 			(void) close(rt_fd);
11637c478bd9Sstevel@tonic-gate 			rt_fd = socket(PF_ROUTE, SOCK_RAW, AF_INET);
11647c478bd9Sstevel@tonic-gate 			i = 0;
11657c478bd9Sstevel@tonic-gate 			while (rt_fd < 0) {
11667c478bd9Sstevel@tonic-gate 				if (i++ == 0) {
11677c478bd9Sstevel@tonic-gate 					logperror(gettext("cannot reinitialize"
11687c478bd9Sstevel@tonic-gate 					    " routing socket"));
11697c478bd9Sstevel@tonic-gate 				} else if (i > 5) {
11707c478bd9Sstevel@tonic-gate 					logwarn(gettext("Give up on trying to"
11717c478bd9Sstevel@tonic-gate 					    " reinitializing routing"
11727c478bd9Sstevel@tonic-gate 					    " socket\n"));
11737c478bd9Sstevel@tonic-gate 					exit(1);
11747c478bd9Sstevel@tonic-gate 				}
11757c478bd9Sstevel@tonic-gate 				/* May be a transient error... */
11767c478bd9Sstevel@tonic-gate 				(void) sleep(10);
11777c478bd9Sstevel@tonic-gate 				rt_fd = socket(PF_ROUTE, SOCK_RAW, AF_INET);
11787c478bd9Sstevel@tonic-gate 			}
11797c478bd9Sstevel@tonic-gate 		} else {
11807c478bd9Sstevel@tonic-gate 			rtm = (struct rt_msghdr *)msg;
11817c478bd9Sstevel@tonic-gate 			if (rtm->rtm_version != RTM_VERSION) {
11827c478bd9Sstevel@tonic-gate 				logwarn(gettext("Do non understand routing"
11837c478bd9Sstevel@tonic-gate 				    " socket info.\n"));
11847c478bd9Sstevel@tonic-gate 				continue;
11857c478bd9Sstevel@tonic-gate 			}
11867c478bd9Sstevel@tonic-gate 			if (debug) {
11877c478bd9Sstevel@tonic-gate 				logdebug("Get routing info.\n");
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 			switch (rtm->rtm_type) {
11907c478bd9Sstevel@tonic-gate 			case RTM_DELETE:
11917c478bd9Sstevel@tonic-gate 			case RTM_OLDDEL:
11927c478bd9Sstevel@tonic-gate 				sin = (struct sockaddr_in *)(rtm + 1);
11937c478bd9Sstevel@tonic-gate 				cp = (uchar_t *)sin;
11947c478bd9Sstevel@tonic-gate 				/* Only handle default route deletion. */
11957c478bd9Sstevel@tonic-gate 				if ((rtm->rtm_addrs & RTA_DST) &&
11967c478bd9Sstevel@tonic-gate 				    (sin->sin_addr.s_addr == 0)) {
11977c478bd9Sstevel@tonic-gate 					if (!(rtm->rtm_addrs & RTA_GATEWAY)) {
11987c478bd9Sstevel@tonic-gate 						break;
11997c478bd9Sstevel@tonic-gate 					}
12007c478bd9Sstevel@tonic-gate 					cp += sizeof (struct sockaddr_in);
12017c478bd9Sstevel@tonic-gate 					/* LINTED */
12027c478bd9Sstevel@tonic-gate 					sin = (struct sockaddr_in *)cp;
12037c478bd9Sstevel@tonic-gate 					gw_addr = sin->sin_addr;
12047c478bd9Sstevel@tonic-gate 					if (debug) {
12057c478bd9Sstevel@tonic-gate 						logdebug("Get default route "
12067c478bd9Sstevel@tonic-gate 						    "removal notice: gw %s\n",
12077c478bd9Sstevel@tonic-gate 						    inet_ntoa(gw_addr));
12087c478bd9Sstevel@tonic-gate 					}
12097c478bd9Sstevel@tonic-gate 					nca_del_nif(gw_addr);
12107c478bd9Sstevel@tonic-gate 				}
12117c478bd9Sstevel@tonic-gate 				break;
12127c478bd9Sstevel@tonic-gate 			case RTM_ADD:
12137c478bd9Sstevel@tonic-gate 			case RTM_OLDADD:
12147c478bd9Sstevel@tonic-gate 			case RTM_CHANGE:
12157c478bd9Sstevel@tonic-gate 				changed = B_FALSE;
12167c478bd9Sstevel@tonic-gate 				if (get_if_info(&changed) < 0) {
12177c478bd9Sstevel@tonic-gate 					/* May be a transient error... */
12187c478bd9Sstevel@tonic-gate 					(void) sleep(10);
12197c478bd9Sstevel@tonic-gate 					break;
12207c478bd9Sstevel@tonic-gate 				}
12217c478bd9Sstevel@tonic-gate 				/* Nothing is changed, do nothing. */
12227c478bd9Sstevel@tonic-gate 				if (!changed) {
12237c478bd9Sstevel@tonic-gate 					if (debug) {
12247c478bd9Sstevel@tonic-gate 						logdebug("Get route change "
12257c478bd9Sstevel@tonic-gate 						    "notice, but nothing is "
12267c478bd9Sstevel@tonic-gate 						    "changed for us!");
12277c478bd9Sstevel@tonic-gate 					}
12287c478bd9Sstevel@tonic-gate 					break;
12297c478bd9Sstevel@tonic-gate 				}
12307c478bd9Sstevel@tonic-gate 				lifr.lifr_addr.ss_family = AF_INET;
12317c478bd9Sstevel@tonic-gate 				for (i = 0; i < num_nif; i++) {
12327c478bd9Sstevel@tonic-gate 					int ret;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 					/*
12357c478bd9Sstevel@tonic-gate 					 * If name is NULL, it means that
12367c478bd9Sstevel@tonic-gate 					 * we have encontered some problems
12377c478bd9Sstevel@tonic-gate 					 * when configurating the interface.
12387c478bd9Sstevel@tonic-gate 					 * So we remove it from the list.
12397c478bd9Sstevel@tonic-gate 					 */
12407c478bd9Sstevel@tonic-gate 					if (nif_list[i].name[0] == 0 ||
12417c478bd9Sstevel@tonic-gate 					    nif_list[i].local_addr.s_addr == 0)
12427c478bd9Sstevel@tonic-gate 						continue;
12437c478bd9Sstevel@tonic-gate 					(void) strlcpy(lifr.lifr_name,
12447c478bd9Sstevel@tonic-gate 					    nif_list[i].name,
12457c478bd9Sstevel@tonic-gate 					    sizeof (lifr.lifr_name));
12467c478bd9Sstevel@tonic-gate 					if (ip_domux2fd(&udp_fd, &fd) < 0) {
12477c478bd9Sstevel@tonic-gate 						logwarn(gettext("Removing"
12487c478bd9Sstevel@tonic-gate 						    " interface %s from the"
12497c478bd9Sstevel@tonic-gate 						    " configuration list.\n"),
12507c478bd9Sstevel@tonic-gate 						    nif_list[i].name);
12517c478bd9Sstevel@tonic-gate 						nif_list[i].name[0] = 0;
12527c478bd9Sstevel@tonic-gate 						continue;
12537c478bd9Sstevel@tonic-gate 					}
12547c478bd9Sstevel@tonic-gate 					if (ioctl(udp_fd, I_PUNLINK,
12557c478bd9Sstevel@tonic-gate 					    lifr.lifr_ip_muxid) < 0) {
12567c478bd9Sstevel@tonic-gate 						logwarn(gettext("Removing"
12577c478bd9Sstevel@tonic-gate 						    " interface %s from the"
12587c478bd9Sstevel@tonic-gate 						    " configuration list.\n"),
12597c478bd9Sstevel@tonic-gate 						    nif_list[i].name);
12607c478bd9Sstevel@tonic-gate 						nif_list[i].name[0] = 0;
12617c478bd9Sstevel@tonic-gate 						(void) close(udp_fd);
12627c478bd9Sstevel@tonic-gate 						(void) close(fd);
12637c478bd9Sstevel@tonic-gate 						continue;
12647c478bd9Sstevel@tonic-gate 					}
12657c478bd9Sstevel@tonic-gate 					if (debug) {
12667c478bd9Sstevel@tonic-gate 						logdebug("Configuring"
12677c478bd9Sstevel@tonic-gate 						    " %s\n", nif_list[i].name);
12687c478bd9Sstevel@tonic-gate 					}
12697c478bd9Sstevel@tonic-gate 					ret = nca_set_nif(fd,
12707c478bd9Sstevel@tonic-gate 					    nif_list[i].local_addr,
12717c478bd9Sstevel@tonic-gate 					    nif_list[i].router_ether_addr);
12727c478bd9Sstevel@tonic-gate 					ip_plink(udp_fd, fd);
12737c478bd9Sstevel@tonic-gate 					if (ret < 0) {
12747c478bd9Sstevel@tonic-gate 						/*
12757c478bd9Sstevel@tonic-gate 						 * This should not be possible
12767c478bd9Sstevel@tonic-gate 						 * since if NCA does not
12777c478bd9Sstevel@tonic-gate 						 * support the ioctl, the
12787c478bd9Sstevel@tonic-gate 						 * active flag should be
12797c478bd9Sstevel@tonic-gate 						 * cleared already and this
12807c478bd9Sstevel@tonic-gate 						 * function should not have
12817c478bd9Sstevel@tonic-gate 						 * been called at all!
12827c478bd9Sstevel@tonic-gate 						 */
12837c478bd9Sstevel@tonic-gate 						logwarn("Daemon dies\n");
12847c478bd9Sstevel@tonic-gate 						exit(1);
12857c478bd9Sstevel@tonic-gate 					}
12867c478bd9Sstevel@tonic-gate 					(void) close(udp_fd);
12877c478bd9Sstevel@tonic-gate 					(void) close(fd);
12887c478bd9Sstevel@tonic-gate 				}
12897c478bd9Sstevel@tonic-gate 				break;
12907c478bd9Sstevel@tonic-gate 			default:
12917c478bd9Sstevel@tonic-gate 				continue;
12927c478bd9Sstevel@tonic-gate 			}
12937c478bd9Sstevel@tonic-gate 		}
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate /*
12987c478bd9Sstevel@tonic-gate  * Make us a daemon.
12997c478bd9Sstevel@tonic-gate  */
13007c478bd9Sstevel@tonic-gate static void
13017c478bd9Sstevel@tonic-gate daemon_init(void)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	pid_t pid;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
13067c478bd9Sstevel@tonic-gate 		/* Write directly to terminal, instead of syslog. */
13077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ncaconfd: cannot fork: %s\n"),
13087c478bd9Sstevel@tonic-gate 		    strerror(errno));
13097c478bd9Sstevel@tonic-gate 		exit(1);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 	if (pid != 0)
13127c478bd9Sstevel@tonic-gate 		exit(0);
13137c478bd9Sstevel@tonic-gate 	(void) setsid();
13147c478bd9Sstevel@tonic-gate 	/* Fork again so that we will never get a controlling terminal. */
13157c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
13167c478bd9Sstevel@tonic-gate 		/* Write directly to terminal, instead of syslog. */
13177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ncaconfd: cannot fork: %s\n"),
13187c478bd9Sstevel@tonic-gate 		    strerror(errno));
13197c478bd9Sstevel@tonic-gate 		exit(1);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 	if (pid != 0)
13227c478bd9Sstevel@tonic-gate 		exit(0);
13237c478bd9Sstevel@tonic-gate 	(void) chdir("/");
13247c478bd9Sstevel@tonic-gate 	(void) umask(0);
13257c478bd9Sstevel@tonic-gate 	(void) fclose(stdin);
13267c478bd9Sstevel@tonic-gate 	(void) fclose(stdout);
13277c478bd9Sstevel@tonic-gate 	(void) fclose(stderr);
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate int
13317c478bd9Sstevel@tonic-gate main(int argc, char **argv)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	int i, j;
13347c478bd9Sstevel@tonic-gate 	int c;
13357c478bd9Sstevel@tonic-gate 	boolean_t active = B_FALSE;
13367c478bd9Sstevel@tonic-gate 	boolean_t as_daemon = B_TRUE;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	if (argc == 1) {
13397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: %s [-al]"
13407c478bd9Sstevel@tonic-gate 		    " [interface1 interface2 ...]\n"), argv[0]);
13417c478bd9Sstevel@tonic-gate 		return (1);
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
13457c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
13467c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
13477c478bd9Sstevel@tonic-gate #endif
13487c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "adcl")) != EOF) {
13517c478bd9Sstevel@tonic-gate 		switch (c) {
13527c478bd9Sstevel@tonic-gate 		case 'a':
13537c478bd9Sstevel@tonic-gate 			active = B_TRUE;
13547c478bd9Sstevel@tonic-gate 			break;
13557c478bd9Sstevel@tonic-gate 		case 'd':
13567c478bd9Sstevel@tonic-gate 			debug = B_TRUE;
13577c478bd9Sstevel@tonic-gate 			break;
13587c478bd9Sstevel@tonic-gate 		case 'c':
13597c478bd9Sstevel@tonic-gate 			/* Don't run as daemon. */
13607c478bd9Sstevel@tonic-gate 			as_daemon = B_FALSE;
13617c478bd9Sstevel@tonic-gate 			break;
13627c478bd9Sstevel@tonic-gate 		case 'l':
13637c478bd9Sstevel@tonic-gate 			logging = B_TRUE;
13647c478bd9Sstevel@tonic-gate 			break;
13657c478bd9Sstevel@tonic-gate 		default:
13667c478bd9Sstevel@tonic-gate 			/* -d and -c are "undocumented" options. */
13677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Usage: %s [-al]"
13687c478bd9Sstevel@tonic-gate 			    " [interface1 interface2 ...]\n"), argv[0]);
13697c478bd9Sstevel@tonic-gate 			return (1);
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 	num_nif = argc - optind;
13737c478bd9Sstevel@tonic-gate 	if (num_nif == 0) {
13747c478bd9Sstevel@tonic-gate 		/* No network interface to proces... */
13757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage: %s [-al]"
13767c478bd9Sstevel@tonic-gate 		    " [interface1 interface2 ...]\n"), argv[0]);
13777c478bd9Sstevel@tonic-gate 		return (0);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	nif_list = calloc(num_nif, sizeof (nif_t));
13807c478bd9Sstevel@tonic-gate 	if (nif_list == NULL) {
13817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ncaconfd: Cannot malloc: %s\n"),
13827c478bd9Sstevel@tonic-gate 		    strerror(errno));
13837c478bd9Sstevel@tonic-gate 		return (1);
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 	for (i = 0, j = optind; i < num_nif; i++, j++) {
13867c478bd9Sstevel@tonic-gate 		(void) strlcpy(nif_list[i].name, argv[j], LIFNAMSIZ+1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/* Get IP address info for all the intefaces. */
13907c478bd9Sstevel@tonic-gate 	if (get_if_ip_addr() < 0) {
13917c478bd9Sstevel@tonic-gate 		if (debug) {
13927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ncaconfd: Cannot get IP"
13937c478bd9Sstevel@tonic-gate 			    " addresses for interfaces.\n");
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 		return (1);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if (logging)
13987c478bd9Sstevel@tonic-gate 		openlog("ncaconfd", LOG_PID, LOG_DAEMON);
13997c478bd9Sstevel@tonic-gate 	/* No need to run as daemon if NCA is not making active connections. */
14007c478bd9Sstevel@tonic-gate 	if (active && as_daemon)
14017c478bd9Sstevel@tonic-gate 		daemon_init();
14027c478bd9Sstevel@tonic-gate 	if (active) {
14037c478bd9Sstevel@tonic-gate 		boolean_t changed;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		/* NCA does not support IPv6... */
14067c478bd9Sstevel@tonic-gate 		if ((rt_fd = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
14077c478bd9Sstevel@tonic-gate 			logperror("Cannot open routing socket");
14087c478bd9Sstevel@tonic-gate 			return (1);
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 		/*
14117c478bd9Sstevel@tonic-gate 		 * At boot up time, the default router may not have been
14127c478bd9Sstevel@tonic-gate 		 * found.  So ignore the error and check later.
14137c478bd9Sstevel@tonic-gate 		 */
14147c478bd9Sstevel@tonic-gate 		if (get_if_info(&changed) < 0) {
14157c478bd9Sstevel@tonic-gate 			if (debug) {
14167c478bd9Sstevel@tonic-gate 				(void) logwarn("Cannot get"
14177c478bd9Sstevel@tonic-gate 				    " information from network interface.\n");
14187c478bd9Sstevel@tonic-gate 			}
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 	/* Do the set up as daemon (if we are) to save time at boot up... */
14227c478bd9Sstevel@tonic-gate 	nca_setup(&active);
14237c478bd9Sstevel@tonic-gate 	if (active)
14247c478bd9Sstevel@tonic-gate 		daemon_work();
14257c478bd9Sstevel@tonic-gate 	return (0);
14267c478bd9Sstevel@tonic-gate }
1427