xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/hostconfig.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <sys/utsname.h>
350Sstevel@tonic-gate #include <sys/systeminfo.h>
360Sstevel@tonic-gate #include <netdb.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/ioctl.h>
420Sstevel@tonic-gate #include <sys/signal.h>
430Sstevel@tonic-gate #include <sys/wait.h>
440Sstevel@tonic-gate #include <sys/time.h>
450Sstevel@tonic-gate #include <sys/socket.h>
460Sstevel@tonic-gate #include <sys/stropts.h>
470Sstevel@tonic-gate #include <sys/resource.h>
480Sstevel@tonic-gate #include <net/if.h>
490Sstevel@tonic-gate #include <net/if_arp.h>
500Sstevel@tonic-gate #include <sys/stream.h>
510Sstevel@tonic-gate #include <net/route.h>
520Sstevel@tonic-gate #include <netinet/in.h>
530Sstevel@tonic-gate #include <arpa/inet.h>
540Sstevel@tonic-gate #include <netinet/if_ether.h>
550Sstevel@tonic-gate #include <netinet/ip_var.h>
560Sstevel@tonic-gate #include <netinet/udp.h>
570Sstevel@tonic-gate #include <netinet/udp_var.h>
580Sstevel@tonic-gate #include <rpc/rpc.h>
590Sstevel@tonic-gate #include <rpcsvc/bootparam_prot.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	MAXIFS	256
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* command line flags */
640Sstevel@tonic-gate int		debug = 0;		/* do debug printfs */
650Sstevel@tonic-gate int		echo_host = 0;		/* just echo hostname, don't set it */
660Sstevel@tonic-gate int		verbose = 0;		/* do verbose printfs */
670Sstevel@tonic-gate int		safe = 0;		/* don't change anything */
680Sstevel@tonic-gate int		multiple = 0;		/* take multiple replies */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static ulong_t	if_netmask;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate void		notsupported(), usage(), bp_whoami();
730Sstevel@tonic-gate int		get_ifdata();		/* get IP addr, subnet mask from IF */
740Sstevel@tonic-gate extern char	*inet_ntoa();
750Sstevel@tonic-gate extern int	getopt(), setdomainname();
760Sstevel@tonic-gate 
770Sstevel@tonic-gate struct prototab {
780Sstevel@tonic-gate 	char *name;
790Sstevel@tonic-gate 	void (*func)();
800Sstevel@tonic-gate } prototab[] = {
810Sstevel@tonic-gate 	{ "bootparams", bp_whoami },
820Sstevel@tonic-gate 	{ "bootp", notsupported },
830Sstevel@tonic-gate 	{ 0, 0 }
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * usage: hostconfig [-p <protocol>] [-v] [-n] [-h] [<ifname>] [-f <hostname>]
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  * options:
920Sstevel@tonic-gate  *	-d		Debug mode.
930Sstevel@tonic-gate  * 	-v		Verbose mode.
940Sstevel@tonic-gate  *	-n		Don't change anything.
950Sstevel@tonic-gate  *	-h		Don't set hostname, just echo to standard out.
960Sstevel@tonic-gate  *	-m		Wait for multiple answers (best used with the "-n"
970Sstevel@tonic-gate  *			and "-v" flags).
980Sstevel@tonic-gate  *	-f <hostname>	Fake mode - get bootparams for <hostname> (also
990Sstevel@tonic-gate  *			best used with the "-n" and "-v" flags).
1000Sstevel@tonic-gate  *	<ifname>	Use IP address of <interface> in whoami request.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * If no interface name is specified, bp_whoami will cycle through the
1030Sstevel@tonic-gate  * interfaces, using the IP address of each in turn until an answer is
1040Sstevel@tonic-gate  * received.  Note that rpc_broadcast() broadcasts the RPC call on all
1050Sstevel@tonic-gate  * interfaces, so the <ifname> argument doesn't restrict the request
1060Sstevel@tonic-gate  * to that interface, it just uses that interface to determine the IP
1070Sstevel@tonic-gate  * address to put into the request.  If "-f <hostname>" is specified,
1080Sstevel@tonic-gate  * we put the IP address of <hostname> in the whoami request.  Otherwise,
1090Sstevel@tonic-gate  * we put the IP address of the interface in the whoami request.
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
114*473Sbw int
main(argc,argv)1150Sstevel@tonic-gate main(argc, argv)
1160Sstevel@tonic-gate 	int argc;
1170Sstevel@tonic-gate 	char **argv;
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	struct ifreq *reqbuf;
1200Sstevel@tonic-gate 	struct ifreq *ifr;
1210Sstevel@tonic-gate 	struct ifconf ifc;
1220Sstevel@tonic-gate 	struct in_addr targetaddr;
1230Sstevel@tonic-gate 	struct hostent *hp;
1240Sstevel@tonic-gate 	char *targethost = NULL;
1250Sstevel@tonic-gate 	char *cmdname;
1260Sstevel@tonic-gate 	int c;
1270Sstevel@tonic-gate 	int n;
1280Sstevel@tonic-gate 	struct prototab *ptp;
1290Sstevel@tonic-gate 	void (*protofunc)() = NULL;
1300Sstevel@tonic-gate 	int numifs;
1310Sstevel@tonic-gate 	unsigned bufsize;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	extern char *optarg;
1340Sstevel@tonic-gate 	extern int optind;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	cmdname = argv[0];
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dhvnmf:p:")) != -1) {
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		switch ((char)c) {
1410Sstevel@tonic-gate 		case 'd':
1420Sstevel@tonic-gate 			debug++;
1430Sstevel@tonic-gate 			break;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		case 'h':
1460Sstevel@tonic-gate 			echo_host++;
1470Sstevel@tonic-gate 			break;
1480Sstevel@tonic-gate 		case 'v':
1490Sstevel@tonic-gate 			verbose++;
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		case 'm':
1530Sstevel@tonic-gate 			multiple++;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		case 'n':
1570Sstevel@tonic-gate 			safe++;
1580Sstevel@tonic-gate 			break;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		case 'f':
1610Sstevel@tonic-gate 			targethost = optarg;
1620Sstevel@tonic-gate 			break;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		case 'p':
1650Sstevel@tonic-gate 			protofunc = NULL;
1660Sstevel@tonic-gate 			for (ptp = &prototab[0]; ptp->func; ptp++)
1670Sstevel@tonic-gate 				if (strcmp(optarg, ptp->name) == 0) {
1680Sstevel@tonic-gate 					protofunc = ptp->func;
1690Sstevel@tonic-gate 					break;
1700Sstevel@tonic-gate 				}
1710Sstevel@tonic-gate 			if (protofunc == NULL)
1720Sstevel@tonic-gate 				usage(cmdname);
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		case '?':
1760Sstevel@tonic-gate 			usage(cmdname);
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (protofunc == NULL)
1810Sstevel@tonic-gate 		usage(cmdname);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (targethost) {
1840Sstevel@tonic-gate 		/* we are faking it */
1850Sstevel@tonic-gate 		if (debug)
1860Sstevel@tonic-gate 			fprintf(stdout, "targethost = %s\n", targethost);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		if ((hp = gethostbyname(targethost)) == NULL) {
1890Sstevel@tonic-gate 			if ((targetaddr.s_addr = inet_addr(targethost)) ==
1900Sstevel@tonic-gate 			    (ulong_t)(-1)) {
1910Sstevel@tonic-gate 				(void) fprintf(stderr,
1920Sstevel@tonic-gate 					"%s: cannot get IP address for %s\n",
1930Sstevel@tonic-gate 					cmdname, targethost);
1940Sstevel@tonic-gate 				return (1);
1950Sstevel@tonic-gate 			}
1960Sstevel@tonic-gate 		} else {
1970Sstevel@tonic-gate 			if (hp->h_length != sizeof (targetaddr)) {
1980Sstevel@tonic-gate 				(void) fprintf(stderr,
1990Sstevel@tonic-gate 					"%s: cannot find host entry for %s\n",
2000Sstevel@tonic-gate 					cmdname, targethost);
2010Sstevel@tonic-gate 				return (1);
2020Sstevel@tonic-gate 			} else
2030Sstevel@tonic-gate 				(void) memcpy(&targetaddr.s_addr, hp->h_addr,
2040Sstevel@tonic-gate 				    sizeof (targetaddr));
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	} else
2070Sstevel@tonic-gate 		targetaddr.s_addr = 0;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (optind < argc) {
2100Sstevel@tonic-gate 		/* interface names were specified */
2110Sstevel@tonic-gate 		for (; optind < argc; optind++) {
2120Sstevel@tonic-gate 			if (debug)
2130Sstevel@tonic-gate 				fprintf(stdout, "Trying arg %s\n",
2140Sstevel@tonic-gate 					argv[optind]);
2150Sstevel@tonic-gate 			(*protofunc)(argv[optind], targetaddr);
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	} else {
2180Sstevel@tonic-gate 		/* no interface names specified - try them all */
2190Sstevel@tonic-gate 		int ifcount = 0;	/* count of useable interfaces */
2200Sstevel@tonic-gate 		int s;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2230Sstevel@tonic-gate 			perror("socket");
2240Sstevel@tonic-gate 			return (1);
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate #ifdef SIOCGIFNUM
2270Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
2280Sstevel@tonic-gate 			numifs = MAXIFS;
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate #else
2310Sstevel@tonic-gate 		numifs = MAXIFS;
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate 		bufsize = numifs * sizeof (struct ifreq);
2340Sstevel@tonic-gate 		reqbuf = (struct ifreq *)malloc(bufsize);
2350Sstevel@tonic-gate 		if (reqbuf == NULL) {
2360Sstevel@tonic-gate 			fprintf(stderr, "out of memory\n");
2370Sstevel@tonic-gate 			return (1);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 		ifc.ifc_buf = (caddr_t)&reqbuf[0];
2400Sstevel@tonic-gate 		ifc.ifc_len = bufsize;
2410Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
2420Sstevel@tonic-gate 			perror("ioctl(SIOCGIFCONF)");
2430Sstevel@tonic-gate 			return (1);
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 		ifr = ifc.ifc_req;
2460Sstevel@tonic-gate 		n = ifc.ifc_len/sizeof (struct ifreq);
2470Sstevel@tonic-gate 		for (; n > 0; n--, ifr++) {
2480Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0) {
2490Sstevel@tonic-gate 				perror("ioctl(SIOCGIFFLAGS)");
2500Sstevel@tonic-gate 				return (1);
2510Sstevel@tonic-gate 			}
2520Sstevel@tonic-gate 			if ((ifr->ifr_flags & IFF_LOOPBACK) ||
2530Sstevel@tonic-gate 			    !(ifr->ifr_flags & IFF_BROADCAST) ||
2540Sstevel@tonic-gate 			    !(ifr->ifr_flags & IFF_UP) ||
2550Sstevel@tonic-gate 			    (ifr->ifr_flags & IFF_NOARP) ||
2560Sstevel@tonic-gate 			    (ifr->ifr_flags & IFF_POINTOPOINT)) {
2570Sstevel@tonic-gate 				if (debug)
2580Sstevel@tonic-gate 					fprintf(stdout, "If %s not suitable\n",
2590Sstevel@tonic-gate 						ifr->ifr_name);
2600Sstevel@tonic-gate 				continue;
2610Sstevel@tonic-gate 			} else {
2620Sstevel@tonic-gate 				if (debug)
2630Sstevel@tonic-gate 					fprintf(stdout, "Trying device %s\n",
2640Sstevel@tonic-gate 						ifr->ifr_name);
2650Sstevel@tonic-gate 				(*protofunc)(ifr->ifr_name,  targetaddr);
2660Sstevel@tonic-gate 				ifcount++;
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		if (verbose && ifcount == 0) {
2700Sstevel@tonic-gate 			fprintf(stderr, "No useable interfaces found.\n");
2710Sstevel@tonic-gate 			return (1);
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		(void) close(s);
2740Sstevel@tonic-gate 		(void) free((char *)reqbuf);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	return (0);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate void
add_default_route(router_addr)2810Sstevel@tonic-gate add_default_route(router_addr)
2820Sstevel@tonic-gate 	struct in_addr router_addr;
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	struct rtentry route;
2850Sstevel@tonic-gate 	struct sockaddr_in *sin;
2860Sstevel@tonic-gate 	int s;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) memset(&route, 0, sizeof (route));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* route destination is "default" - zero */
2910Sstevel@tonic-gate 	/* LINTED - alignment OK (32bit) */
2920Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&route.rt_dst;
2930Sstevel@tonic-gate 	sin->sin_family = AF_INET;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* LINTED - alignment OK (32bit) */
2960Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&route.rt_gateway;
2970Sstevel@tonic-gate 	sin->sin_family = AF_INET;
2980Sstevel@tonic-gate 	sin->sin_addr.s_addr = router_addr.s_addr;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	route.rt_flags = RTF_GATEWAY | RTF_UP;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
3030Sstevel@tonic-gate 		perror("socket");
3040Sstevel@tonic-gate 		return;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	if (ioctl(s, SIOCADDRT, (char *)&route) == -1) {
3070Sstevel@tonic-gate 		perror("add default route");
3080Sstevel@tonic-gate 		return;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	(void) close(s);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate int
bpanswer(res,nb)3150Sstevel@tonic-gate bpanswer(res, nb)
3160Sstevel@tonic-gate 	struct bp_whoami_res *res;
3170Sstevel@tonic-gate 	struct netbuf *nb;
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	struct in_addr router_addr;
3200Sstevel@tonic-gate 	static int set;
3210Sstevel@tonic-gate 	int len;
3220Sstevel@tonic-gate 	char errbuf[MAX_MACHINE_NAME + 28];
3230Sstevel@tonic-gate 	/* MAX_MACHINE_NAME + strlen ("sysinfo(SI_SET_HOSTNAME)()") + null */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	(void) memcpy(&router_addr, &res->router_address.bp_address_u.ip_addr,
3260Sstevel@tonic-gate 	    sizeof (router_addr));
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (verbose) {
3290Sstevel@tonic-gate 		struct sockaddr_in *addr;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		if (nb) {
3320Sstevel@tonic-gate 			/* LINTED - alignment (32bit) OK */
3330Sstevel@tonic-gate 			addr = (struct sockaddr_in *)nb->buf;
3340Sstevel@tonic-gate 			fprintf(stdout, "From [%s]: ",
3350Sstevel@tonic-gate 			    inet_ntoa(addr->sin_addr));
3360Sstevel@tonic-gate 		} else {
3370Sstevel@tonic-gate 			fprintf(stdout, "Reply:\\t\\t");
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 		fprintf(stdout, "hostname = %s\n", res->client_name);
3400Sstevel@tonic-gate 		fprintf(stdout, "\t\typdomain = %s\n", res->domain_name);
3410Sstevel@tonic-gate 		fprintf(stdout, "\t\trouter = %s\n", inet_ntoa(router_addr));
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (!safe && !set) {
3450Sstevel@tonic-gate 		/*
3460Sstevel@tonic-gate 		 * Stuff the values from the RPC reply into the kernel.
3470Sstevel@tonic-gate 		 * Only allow one pass through this code; There's no reason
3480Sstevel@tonic-gate 		 * why all replies should tweak the kernel.
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 		set++;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		len = strlen(res->client_name);
3530Sstevel@tonic-gate 		if (len != 0) {
3540Sstevel@tonic-gate 			if (!echo_host) {
3550Sstevel@tonic-gate 				if (sysinfo(SI_SET_HOSTNAME, res->client_name,
3560Sstevel@tonic-gate 				    len) < 0) {
3570Sstevel@tonic-gate 					(void) snprintf(errbuf, sizeof (errbuf),
3580Sstevel@tonic-gate 					    "sysinfo(SI_SET_HOSTNAME)(%s)",
3590Sstevel@tonic-gate 					    res->client_name);
3600Sstevel@tonic-gate 					perror(errbuf);
3610Sstevel@tonic-gate 				}
3620Sstevel@tonic-gate 			} else
3630Sstevel@tonic-gate 				(void) fprintf(stdout, "%s\n",
3640Sstevel@tonic-gate 				    res->client_name);
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		len = strlen(res->domain_name);
3680Sstevel@tonic-gate 		if (len != 0) {
3690Sstevel@tonic-gate 			if (setdomainname(res->domain_name, len) == -1) {
3700Sstevel@tonic-gate 				(void) snprintf(errbuf, sizeof (errbuf),
3710Sstevel@tonic-gate 				    "setdomainname(%s)", res->domain_name);
3720Sstevel@tonic-gate 				perror(errbuf);
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		/* we really should validate this router value */
3770Sstevel@tonic-gate 		if (router_addr.s_addr != 0)
3780Sstevel@tonic-gate 			add_default_route(router_addr);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (multiple)
3820Sstevel@tonic-gate 		return (NULL);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* our job is done */
3850Sstevel@tonic-gate 	exit(0);
3860Sstevel@tonic-gate 	/* NOTREACHED */
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate void
bp_whoami(device,addr)3900Sstevel@tonic-gate bp_whoami(device, addr)
3910Sstevel@tonic-gate 	char *device;
3920Sstevel@tonic-gate 	struct in_addr addr;
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	struct bp_whoami_arg req;
3950Sstevel@tonic-gate 	struct bp_whoami_res res;
3960Sstevel@tonic-gate 	struct in_addr lookupaddr;
3970Sstevel@tonic-gate 	enum clnt_stat stat;
3980Sstevel@tonic-gate 	int val = 1;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (debug)
4010Sstevel@tonic-gate 		fprintf(stdout, "bp_whoami on interface %s addr %s\n", device,
4020Sstevel@tonic-gate 		    inet_ntoa(addr));
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (addr.s_addr ==  0) {
4050Sstevel@tonic-gate 		if (get_ifdata(device, &lookupaddr, &if_netmask) == -1)
4060Sstevel@tonic-gate 			exit(1);
4070Sstevel@tonic-gate 	} else
4080Sstevel@tonic-gate 		(void) memcpy(&lookupaddr, &addr, sizeof (addr));
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	lookupaddr.s_addr = ntohl(lookupaddr.s_addr);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if (debug)
4130Sstevel@tonic-gate 		fprintf(stdout, "lookup address is %s\n",
4140Sstevel@tonic-gate 			inet_ntoa(lookupaddr));
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	(void) memset(&req, 0, sizeof (req));
4170Sstevel@tonic-gate 	(void) memset(&res, 0, sizeof (res));
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	req.client_address.address_type = IP_ADDR_TYPE;
4200Sstevel@tonic-gate 	(void) memcpy(&req.client_address.bp_address_u.ip_addr, &lookupaddr,
4210Sstevel@tonic-gate 	    sizeof (lookupaddr));
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * Broadcast using portmap version number 2  ONLY to
4250Sstevel@tonic-gate 	 * prevent broadcast storm
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	(void) __rpc_control(CLCR_SET_LOWVERS, &val);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	stat = rpc_broadcast(BOOTPARAMPROG, BOOTPARAMVERS, BOOTPARAMPROC_WHOAMI,
4310Sstevel@tonic-gate 	    xdr_bp_whoami_arg, (caddr_t)&req, xdr_bp_whoami_res, (caddr_t)&res,
4320Sstevel@tonic-gate 	    (resultproc_t)bpanswer, "udp");
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/* Now try version 3 as well */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	val = 0;
4370Sstevel@tonic-gate 	(void) __rpc_control(CLCR_SET_LOWVERS, &val);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	stat = rpc_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
4400Sstevel@tonic-gate 	    BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg, (caddr_t)&req,
4410Sstevel@tonic-gate 	    xdr_bp_whoami_res, (caddr_t)&res, (resultproc_t)bpanswer, "udp");
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (stat != RPC_SUCCESS) {
4440Sstevel@tonic-gate 		clnt_perrno(stat);
4450Sstevel@tonic-gate 		exit(1);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * Get IP address of an interface.  As long as we are looking, get the
4520Sstevel@tonic-gate  * netmask as well.
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate int
get_ifdata(dev,ipp,maskp)4550Sstevel@tonic-gate get_ifdata(dev, ipp, maskp)
4560Sstevel@tonic-gate 	char *dev;
4570Sstevel@tonic-gate 	ulong_t *ipp, *maskp;
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	struct ifreq ifr;
4600Sstevel@tonic-gate 	/* LINTED - alignment OK (32bit) */
4610Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
4620Sstevel@tonic-gate 	int s;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
4650Sstevel@tonic-gate 		perror("socket");
4660Sstevel@tonic-gate 		return (-1);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (strlcpy(ifr.ifr_name, dev, sizeof (ifr.ifr_name)) >=
4700Sstevel@tonic-gate 		sizeof (ifr.ifr_name)) {
4710Sstevel@tonic-gate 			(void) fprintf(stderr, "Device name too long %s\n",
4720Sstevel@tonic-gate 			    dev);
4730Sstevel@tonic-gate 			return (-1);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (ipp) {
4770Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
4780Sstevel@tonic-gate 			perror("ioctl(SIOCGIFADDR)");
4790Sstevel@tonic-gate 			return (-1);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		*ipp = ntohl(sin->sin_addr.s_addr);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 		if (debug)
4840Sstevel@tonic-gate 			(void) fprintf(stderr, "Interface '%s' address %s\n",
4850Sstevel@tonic-gate 			    dev, inet_ntoa(sin->sin_addr));
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (maskp) {
4890Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
4900Sstevel@tonic-gate 			perror("SIOCGIFNETMASK");
4910Sstevel@tonic-gate 			return (-1);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		*maskp = ntohl(sin->sin_addr.s_addr);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		if (debug)
4960Sstevel@tonic-gate 			(void) fprintf(stderr,
4970Sstevel@tonic-gate 				"Interface '%s' subnet mask %s\n", dev,
4980Sstevel@tonic-gate 				inet_ntoa(sin->sin_addr));
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	(void) close(s);
5020Sstevel@tonic-gate 	return (0);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate void
notsupported()5060Sstevel@tonic-gate notsupported()
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	fprintf(stderr, "requested protocol is not supported\n");
5090Sstevel@tonic-gate 	exit(1);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate void
usage(cmdname)5130Sstevel@tonic-gate usage(cmdname)
5140Sstevel@tonic-gate 	char *cmdname;
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s [-v] [-n] [-m] [-h] [<ifname>] "
5170Sstevel@tonic-gate 	    "[-f <hostname>] -p bootparams|bootp\n", cmdname);
5180Sstevel@tonic-gate 	(void) fflush(stderr);
5190Sstevel@tonic-gate 	exit(1);
5200Sstevel@tonic-gate }
521