Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.23.10.2
      1  1.23.10.2       he /*	$NetBSD: arp.c,v 1.23.10.2 2001/05/01 12:43:30 he Exp $ */
      2       1.10   chopps 
      3        1.1      cgd /*
      4        1.5  mycroft  * Copyright (c) 1984, 1993
      5        1.5  mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1      cgd  *
      7        1.1      cgd  * This code is derived from software contributed to Berkeley by
      8        1.1      cgd  * Sun Microsystems, Inc.
      9        1.1      cgd  *
     10        1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11        1.1      cgd  * modification, are permitted provided that the following conditions
     12        1.1      cgd  * are met:
     13        1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14        1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15        1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17        1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18        1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19        1.1      cgd  *    must display the following acknowledgement:
     20        1.1      cgd  *	This product includes software developed by the University of
     21        1.1      cgd  *	California, Berkeley and its contributors.
     22        1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23        1.1      cgd  *    may be used to endorse or promote products derived from this software
     24        1.1      cgd  *    without specific prior written permission.
     25        1.1      cgd  *
     26        1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27        1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28        1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29        1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30        1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31        1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32        1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33        1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34        1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35        1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36        1.1      cgd  * SUCH DAMAGE.
     37        1.1      cgd  */
     38        1.1      cgd 
     39       1.19    lukem #include <sys/cdefs.h>
     40        1.1      cgd #ifndef lint
     41       1.19    lukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\n\
     42       1.19    lukem 	The Regents of the University of California.  All rights reserved.\n");
     43        1.1      cgd #endif /* not lint */
     44        1.1      cgd 
     45        1.1      cgd #ifndef lint
     46       1.19    lukem #if 0
     47       1.19    lukem static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     48       1.19    lukem #else
     49  1.23.10.2       he __RCSID("$NetBSD: arp.c,v 1.23.10.2 2001/05/01 12:43:30 he Exp $");
     50       1.19    lukem #endif
     51        1.1      cgd #endif /* not lint */
     52        1.1      cgd 
     53        1.1      cgd /*
     54        1.1      cgd  * arp - display, set, and delete arp table entries
     55        1.1      cgd  */
     56        1.1      cgd 
     57        1.1      cgd #include <sys/param.h>
     58        1.1      cgd #include <sys/file.h>
     59        1.1      cgd #include <sys/socket.h>
     60        1.5  mycroft #include <sys/sysctl.h>
     61        1.5  mycroft 
     62        1.5  mycroft #include <net/if.h>
     63        1.5  mycroft #include <net/if_dl.h>
     64       1.17       is #include <net/if_ether.h>
     65        1.5  mycroft #include <net/if_types.h>
     66        1.5  mycroft #include <net/route.h>
     67        1.1      cgd #include <netinet/in.h>
     68       1.17       is #include <netinet/if_inarp.h>
     69        1.2  mycroft #include <arpa/inet.h>
     70        1.1      cgd 
     71       1.15    mikel #include <err.h>
     72       1.15    mikel #include <errno.h>
     73        1.5  mycroft #include <netdb.h>
     74        1.1      cgd #include <nlist.h>
     75       1.15    mikel #include <paths.h>
     76        1.1      cgd #include <stdio.h>
     77        1.8   chopps #include <stdlib.h>
     78       1.12      cgd #include <string.h>
     79       1.15    mikel #include <unistd.h>
     80        1.1      cgd 
     81       1.19    lukem int	delete __P((const char *, const char *));
     82       1.19    lukem void	dump __P((u_long));
     83  1.23.10.2       he void	delete_all __P((void));
     84       1.19    lukem void	sdl_print __P((const struct sockaddr_dl *));
     85       1.19    lukem int	atosdl __P((const char *s, struct sockaddr_dl *sdl));
     86       1.19    lukem int	file __P((char *));
     87       1.19    lukem void	get __P((const char *));
     88       1.19    lukem int	getinetaddr __P((const char *, struct in_addr *));
     89       1.19    lukem void	getsocket __P((void));
     90       1.19    lukem int	main __P((int, char **));
     91       1.19    lukem int	rtmsg __P((int));
     92       1.19    lukem int	set __P((int, char **));
     93       1.19    lukem void	usage __P((void));
     94       1.17       is 
     95        1.5  mycroft static int pid;
     96  1.23.10.2       he static int aflag, nflag, vflag;
     97        1.5  mycroft static int s = -1;
     98        1.1      cgd 
     99       1.16    mikel int	delete __P((const char *, const char *));
    100       1.16    mikel void	dump __P((u_long));
    101       1.16    mikel void	ether_print __P((const u_char *));
    102       1.16    mikel int	file __P((char *));
    103       1.16    mikel void	get __P((const char *));
    104       1.16    mikel int	getinetaddr __P((const char *, struct in_addr *));
    105       1.16    mikel void	getsocket __P((void));
    106       1.16    mikel int	rtmsg __P((int));
    107       1.16    mikel int	set __P((int, char **));
    108       1.16    mikel void	usage __P((void));
    109       1.16    mikel 
    110        1.8   chopps int
    111        1.1      cgd main(argc, argv)
    112        1.1      cgd 	int argc;
    113        1.1      cgd 	char **argv;
    114        1.1      cgd {
    115        1.1      cgd 	int ch;
    116       1.15    mikel 	int op = 0;
    117        1.1      cgd 
    118        1.5  mycroft 	pid = getpid();
    119       1.15    mikel 
    120       1.23      mrg 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    121        1.1      cgd 		switch((char)ch) {
    122        1.5  mycroft 		case 'a':
    123  1.23.10.2       he 			aflag = 1;
    124  1.23.10.2       he 			break;
    125        1.1      cgd 		case 'd':
    126       1.15    mikel 		case 's':
    127       1.15    mikel 		case 'f':
    128       1.15    mikel 			if (op)
    129        1.1      cgd 				usage();
    130       1.15    mikel 			op = ch;
    131       1.15    mikel 			break;
    132        1.5  mycroft 		case 'n':
    133        1.5  mycroft 			nflag = 1;
    134        1.8   chopps 			break;
    135       1.23      mrg 		case 'v':
    136       1.23      mrg 			vflag = 1;
    137       1.23      mrg 			break;
    138        1.1      cgd 		default:
    139        1.1      cgd 			usage();
    140        1.1      cgd 		}
    141       1.15    mikel 	argc -= optind;
    142       1.15    mikel 	argv += optind;
    143       1.15    mikel 
    144  1.23.10.2       he 	if (!op && aflag)
    145  1.23.10.2       he 		op = 'a';
    146  1.23.10.2       he 
    147       1.15    mikel 	switch((char)op) {
    148       1.15    mikel 	case 'a':
    149       1.15    mikel 		dump(0);
    150       1.15    mikel 		break;
    151       1.15    mikel 	case 'd':
    152  1.23.10.2       he 		if (aflag && argc == 0)
    153  1.23.10.2       he 			delete_all();
    154  1.23.10.2       he 		else {
    155  1.23.10.2       he 			if (aflag || argc < 1 || argc > 2)
    156  1.23.10.2       he 				usage();
    157  1.23.10.2       he 			(void)delete(argv[0], argv[1]);
    158  1.23.10.2       he 		}
    159       1.15    mikel 		break;
    160       1.15    mikel 	case 's':
    161       1.15    mikel 		if (argc < 2 || argc > 5)
    162       1.15    mikel 			usage();
    163       1.15    mikel 		return (set(argc, argv) ? 1 : 0);
    164       1.15    mikel 	case 'f':
    165       1.15    mikel 		if (argc != 1)
    166       1.15    mikel 			usage();
    167       1.15    mikel 		return (file(argv[0]));
    168       1.15    mikel 	default:
    169       1.15    mikel 		if (argc != 1)
    170       1.15    mikel 			usage();
    171       1.15    mikel 		get(argv[0]);
    172       1.15    mikel 		break;
    173       1.15    mikel 	}
    174        1.8   chopps 	return (0);
    175        1.1      cgd }
    176        1.1      cgd 
    177        1.1      cgd /*
    178        1.1      cgd  * Process a file to set standard arp entries
    179        1.1      cgd  */
    180        1.8   chopps int
    181        1.1      cgd file(name)
    182        1.1      cgd 	char *name;
    183        1.1      cgd {
    184        1.8   chopps 	char line[100], arg[5][50], *args[5];
    185        1.8   chopps 	int i, retval;
    186        1.1      cgd 	FILE *fp;
    187        1.1      cgd 
    188        1.8   chopps 	if ((fp = fopen(name, "r")) == NULL)
    189        1.8   chopps 		err(1, "cannot open %s", name);
    190        1.1      cgd 	args[0] = &arg[0][0];
    191        1.1      cgd 	args[1] = &arg[1][0];
    192        1.1      cgd 	args[2] = &arg[2][0];
    193        1.1      cgd 	args[3] = &arg[3][0];
    194        1.1      cgd 	args[4] = &arg[4][0];
    195        1.1      cgd 	retval = 0;
    196        1.8   chopps 	while (fgets(line, 100, fp) != NULL) {
    197        1.1      cgd 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    198        1.1      cgd 		    arg[3], arg[4]);
    199        1.1      cgd 		if (i < 2) {
    200        1.8   chopps 			warnx("bad line: %s", line);
    201        1.1      cgd 			retval = 1;
    202        1.1      cgd 			continue;
    203        1.1      cgd 		}
    204        1.1      cgd 		if (set(i, args))
    205        1.1      cgd 			retval = 1;
    206        1.1      cgd 	}
    207        1.1      cgd 	fclose(fp);
    208        1.1      cgd 	return (retval);
    209        1.1      cgd }
    210        1.1      cgd 
    211        1.8   chopps void
    212        1.8   chopps getsocket()
    213        1.8   chopps {
    214        1.8   chopps 	if (s >= 0)
    215        1.8   chopps 		return;
    216        1.8   chopps 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    217        1.8   chopps 	if (s < 0)
    218        1.8   chopps 		err(1, "socket");
    219        1.5  mycroft }
    220        1.5  mycroft 
    221        1.5  mycroft struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    222        1.5  mycroft struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    223        1.5  mycroft struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    224        1.5  mycroft int	expire_time, flags, export_only, doing_proxy, found_entry;
    225        1.5  mycroft struct	{
    226        1.5  mycroft 	struct	rt_msghdr m_rtm;
    227        1.5  mycroft 	char	m_space[512];
    228        1.5  mycroft }	m_rtmsg;
    229        1.5  mycroft 
    230        1.1      cgd /*
    231        1.1      cgd  * Set an individual arp entry
    232        1.1      cgd  */
    233        1.8   chopps int
    234        1.1      cgd set(argc, argv)
    235        1.1      cgd 	int argc;
    236        1.1      cgd 	char **argv;
    237        1.1      cgd {
    238       1.19    lukem 	struct sockaddr_inarp *sin;
    239       1.19    lukem 	struct sockaddr_dl *sdl;
    240       1.19    lukem 	struct rt_msghdr *rtm;
    241        1.8   chopps 	char *host = argv[0], *eaddr;
    242       1.23      mrg 	int rval;
    243        1.8   chopps 
    244        1.8   chopps 	sin = &sin_m;
    245        1.8   chopps 	rtm = &(m_rtmsg.m_rtm);
    246        1.8   chopps 	eaddr = argv[1];
    247        1.1      cgd 
    248        1.5  mycroft 	getsocket();
    249        1.1      cgd 	argc -= 2;
    250        1.1      cgd 	argv += 2;
    251        1.8   chopps 	sdl_m = blank_sdl;		/* struct copy */
    252        1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    253        1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    254        1.8   chopps 		return (1);
    255       1.17       is 	if (atosdl(eaddr, &sdl_m))
    256       1.17       is 		warnx("invalid link-level address '%s'", eaddr);
    257        1.5  mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    258        1.1      cgd 	while (argc-- > 0) {
    259        1.5  mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    260        1.5  mycroft 			struct timeval time;
    261        1.8   chopps 			(void)gettimeofday(&time, 0);
    262        1.5  mycroft 			expire_time = time.tv_sec + 20 * 60;
    263        1.5  mycroft 		}
    264        1.5  mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    265        1.5  mycroft 			flags |= RTF_ANNOUNCE;
    266        1.5  mycroft 			doing_proxy = SIN_PROXY;
    267        1.5  mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    268        1.8   chopps 			(void)printf(
    269        1.8   chopps 			    "%s: Sending trailers is no longer supported\n",
    270        1.8   chopps 			     host);
    271        1.5  mycroft 		}
    272        1.1      cgd 		argv++;
    273        1.1      cgd 	}
    274        1.5  mycroft tryagain:
    275        1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    276        1.8   chopps 		warn("%s", host);
    277        1.5  mycroft 		return (1);
    278        1.1      cgd 	}
    279        1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    280        1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    281        1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    282        1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    283        1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    284        1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    285        1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    286       1.21       is 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    287        1.5  mycroft 			goto overwrite;
    288        1.5  mycroft 		}
    289        1.5  mycroft 		if (doing_proxy == 0) {
    290        1.8   chopps 			(void)printf("set: can only proxy for %s\n", host);
    291        1.5  mycroft 			return (1);
    292        1.5  mycroft 		}
    293        1.5  mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    294        1.8   chopps 			(void)printf(
    295        1.8   chopps 			    "set: proxy entry exists for non 802 device\n");
    296        1.8   chopps 			return (1);
    297        1.5  mycroft 		}
    298        1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    299        1.5  mycroft 		export_only = 1;
    300        1.5  mycroft 		goto tryagain;
    301        1.5  mycroft 	}
    302        1.5  mycroft overwrite:
    303        1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    304        1.8   chopps 		(void)printf("cannot intuit interface index and type for %s\n",
    305        1.8   chopps 		    host);
    306        1.5  mycroft 		return (1);
    307        1.1      cgd 	}
    308        1.5  mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    309        1.5  mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    310       1.23      mrg 	rval = rtmsg(RTM_ADD);
    311       1.23      mrg 	if (vflag)
    312       1.23      mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    313       1.23      mrg 	return (rval);
    314        1.1      cgd }
    315        1.1      cgd 
    316        1.1      cgd /*
    317        1.1      cgd  * Display an individual arp entry
    318        1.1      cgd  */
    319        1.8   chopps void
    320        1.1      cgd get(host)
    321        1.8   chopps 	const char *host;
    322        1.1      cgd {
    323        1.8   chopps 	struct sockaddr_inarp *sin;
    324        1.1      cgd 
    325        1.8   chopps 	sin = &sin_m;
    326        1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    327        1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    328        1.8   chopps 		exit(1);
    329        1.5  mycroft 	dump(sin->sin_addr.s_addr);
    330        1.5  mycroft 	if (found_entry == 0) {
    331        1.8   chopps 		(void)printf("%s (%s) -- no entry\n", host,
    332        1.8   chopps 		    inet_ntoa(sin->sin_addr));
    333        1.1      cgd 		exit(1);
    334        1.1      cgd 	}
    335        1.1      cgd }
    336        1.1      cgd 
    337        1.1      cgd /*
    338        1.1      cgd  * Delete an arp entry
    339        1.1      cgd  */
    340        1.8   chopps int
    341        1.5  mycroft delete(host, info)
    342        1.8   chopps 	const char *host;
    343        1.8   chopps 	const char *info;
    344        1.1      cgd {
    345       1.19    lukem 	struct sockaddr_inarp *sin;
    346       1.19    lukem 	struct rt_msghdr *rtm;
    347        1.5  mycroft 	struct sockaddr_dl *sdl;
    348        1.1      cgd 
    349        1.8   chopps 	sin = &sin_m;
    350        1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    351        1.8   chopps 
    352        1.5  mycroft 	if (info && strncmp(info, "pro", 3) )
    353        1.5  mycroft 		export_only = 1;
    354        1.5  mycroft 	getsocket();
    355        1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    356        1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    357        1.8   chopps 		return (1);
    358        1.5  mycroft tryagain:
    359        1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    360        1.8   chopps 		warn("%s", host);
    361        1.5  mycroft 		return (1);
    362        1.5  mycroft 	}
    363        1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    364        1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    365        1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    366        1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    367        1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    368        1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    369        1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    370       1.21       is 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    371        1.5  mycroft 			goto delete;
    372        1.5  mycroft 		}
    373        1.1      cgd 	}
    374        1.5  mycroft 	if (sin_m.sin_other & SIN_PROXY) {
    375        1.8   chopps 		warnx("delete: can't locate %s", host);
    376        1.5  mycroft 		return (1);
    377        1.5  mycroft 	} else {
    378        1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    379        1.5  mycroft 		goto tryagain;
    380        1.5  mycroft 	}
    381        1.5  mycroft delete:
    382        1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    383        1.8   chopps 		(void)printf("cannot locate %s\n", host);
    384        1.5  mycroft 		return (1);
    385        1.1      cgd 	}
    386        1.8   chopps 	if (rtmsg(RTM_DELETE))
    387        1.8   chopps 		return (1);
    388       1.23      mrg 	if (vflag)
    389       1.23      mrg 		(void)printf("%s (%s) deleted\n", host,
    390       1.23      mrg 		    inet_ntoa(sin->sin_addr));
    391        1.8   chopps 	return (0);
    392        1.1      cgd }
    393        1.1      cgd 
    394        1.1      cgd /*
    395        1.1      cgd  * Dump the entire arp table
    396        1.1      cgd  */
    397        1.8   chopps void
    398        1.5  mycroft dump(addr)
    399        1.8   chopps 	u_long addr;
    400        1.1      cgd {
    401        1.5  mycroft 	int mib[6];
    402        1.5  mycroft 	size_t needed;
    403        1.8   chopps 	char *host, *lim, *buf, *next;
    404        1.5  mycroft 	struct rt_msghdr *rtm;
    405        1.5  mycroft 	struct sockaddr_inarp *sin;
    406        1.5  mycroft 	struct sockaddr_dl *sdl;
    407        1.1      cgd 	extern int h_errno;
    408        1.1      cgd 	struct hostent *hp;
    409        1.1      cgd 
    410        1.5  mycroft 	mib[0] = CTL_NET;
    411        1.5  mycroft 	mib[1] = PF_ROUTE;
    412        1.5  mycroft 	mib[2] = 0;
    413        1.5  mycroft 	mib[3] = AF_INET;
    414        1.5  mycroft 	mib[4] = NET_RT_FLAGS;
    415        1.5  mycroft 	mib[5] = RTF_LLINFO;
    416        1.5  mycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    417        1.8   chopps 		err(1, "route-sysctl-estimate");
    418       1.22     fvdl 	if (needed == 0)
    419       1.22     fvdl 		return;
    420        1.5  mycroft 	if ((buf = malloc(needed)) == NULL)
    421        1.8   chopps 		err(1, "malloc");
    422        1.5  mycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    423        1.8   chopps 		err(1, "actual retrieval of routing table");
    424        1.5  mycroft 	lim = buf + needed;
    425        1.5  mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    426        1.5  mycroft 		rtm = (struct rt_msghdr *)next;
    427        1.5  mycroft 		sin = (struct sockaddr_inarp *)(rtm + 1);
    428        1.5  mycroft 		sdl = (struct sockaddr_dl *)(sin + 1);
    429        1.5  mycroft 		if (addr) {
    430        1.5  mycroft 			if (addr != sin->sin_addr.s_addr)
    431        1.5  mycroft 				continue;
    432        1.5  mycroft 			found_entry = 1;
    433        1.5  mycroft 		}
    434        1.5  mycroft 		if (nflag == 0)
    435        1.5  mycroft 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    436        1.5  mycroft 			    sizeof sin->sin_addr, AF_INET);
    437        1.1      cgd 		else
    438        1.1      cgd 			hp = 0;
    439        1.1      cgd 		if (hp)
    440        1.1      cgd 			host = hp->h_name;
    441        1.1      cgd 		else {
    442        1.1      cgd 			host = "?";
    443        1.1      cgd 			if (h_errno == TRY_AGAIN)
    444        1.5  mycroft 				nflag = 1;
    445        1.1      cgd 		}
    446        1.8   chopps 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    447        1.5  mycroft 		if (sdl->sdl_alen)
    448       1.17       is 			sdl_print(sdl);
    449        1.1      cgd 		else
    450        1.8   chopps 			(void)printf("(incomplete)");
    451        1.5  mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    452        1.8   chopps 			(void)printf(" permanent");
    453        1.5  mycroft 		if (sin->sin_other & SIN_PROXY)
    454        1.8   chopps 			(void)printf(" published (proxy only)");
    455        1.5  mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    456        1.5  mycroft 			sin = (struct sockaddr_inarp *)
    457        1.5  mycroft 				(sdl->sdl_len + (char *)sdl);
    458        1.5  mycroft 			if (sin->sin_addr.s_addr == 0xffffffff)
    459        1.8   chopps 				(void)printf(" published");
    460        1.5  mycroft 			if (sin->sin_len != 8)
    461        1.8   chopps 				(void)printf("(wierd)");
    462        1.5  mycroft 		}
    463        1.8   chopps 		(void)printf("\n");
    464        1.1      cgd 	}
    465        1.1      cgd }
    466        1.1      cgd 
    467  1.23.10.2       he /*
    468  1.23.10.2       he  * Delete the entire arp table
    469  1.23.10.2       he  */
    470  1.23.10.2       he void
    471  1.23.10.2       he delete_all(void)
    472  1.23.10.2       he {
    473  1.23.10.2       he 	int mib[6];
    474  1.23.10.2       he 	size_t needed;
    475  1.23.10.2       he 	char addr[sizeof("000.000.000.000\0")];
    476  1.23.10.2       he 	char *lim, *buf, *next;
    477  1.23.10.2       he 	struct rt_msghdr *rtm;
    478  1.23.10.2       he 	struct sockaddr_inarp *sin;
    479  1.23.10.2       he 	struct sockaddr_dl *sdl;
    480  1.23.10.2       he 
    481  1.23.10.2       he 	mib[0] = CTL_NET;
    482  1.23.10.2       he 	mib[1] = PF_ROUTE;
    483  1.23.10.2       he 	mib[2] = 0;
    484  1.23.10.2       he 	mib[3] = AF_INET;
    485  1.23.10.2       he 	mib[4] = NET_RT_FLAGS;
    486  1.23.10.2       he 	mib[5] = RTF_LLINFO;
    487  1.23.10.2       he 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    488  1.23.10.2       he 		err(1, "route-sysctl-estimate");
    489  1.23.10.2       he 	if (needed == 0)
    490  1.23.10.2       he 		return;
    491  1.23.10.2       he 	if ((buf = malloc(needed)) == NULL)
    492  1.23.10.2       he 		err(1, "malloc");
    493  1.23.10.2       he 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    494  1.23.10.2       he 		err(1, "actual retrieval of routing table");
    495  1.23.10.2       he 	lim = buf + needed;
    496  1.23.10.2       he 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    497  1.23.10.2       he 		rtm = (struct rt_msghdr *)next;
    498  1.23.10.2       he 		sin = (struct sockaddr_inarp *)(rtm + 1);
    499  1.23.10.2       he 		sdl = (struct sockaddr_dl *)(sin + 1);
    500  1.23.10.2       he 		snprintf(addr, sizeof(addr), "%s", inet_ntoa(sin->sin_addr));
    501  1.23.10.2       he 		delete(addr, NULL);
    502  1.23.10.2       he 	}
    503  1.23.10.2       he }
    504  1.23.10.2       he 
    505        1.8   chopps void
    506       1.17       is sdl_print(sdl)
    507       1.17       is 	const struct sockaddr_dl *sdl;
    508       1.17       is {
    509       1.17       is 	int i;
    510       1.17       is 	u_int8_t *p;
    511       1.17       is 
    512       1.17       is 	i = sdl->sdl_alen;
    513       1.17       is 	p = LLADDR(sdl);
    514       1.17       is 
    515  1.23.10.1       tv 	(void)printf("%02x", *p);
    516       1.18  thorpej 	while (--i > 0) {
    517       1.18  thorpej 		putchar(':');
    518  1.23.10.1       tv 		(void)printf("%02x", *++p);
    519       1.18  thorpej 	}
    520       1.17       is }
    521       1.17       is 
    522       1.17       is int
    523       1.17       is atosdl(s, sdl)
    524       1.17       is 	const char *s;
    525       1.17       is 	struct sockaddr_dl *sdl;
    526        1.1      cgd {
    527       1.17       is 	int i;
    528       1.17       is 	long b;
    529       1.17       is 	caddr_t endp;
    530       1.17       is 	caddr_t p;
    531       1.17       is 	char *t, *r;
    532       1.17       is 
    533       1.17       is 	p = LLADDR(sdl);
    534       1.17       is 	endp = ((caddr_t)sdl) + sdl->sdl_len;
    535       1.17       is 	i = 0;
    536       1.17       is 
    537       1.17       is 	b = strtol(s, &t, 16);
    538       1.17       is 	if (t == s)
    539       1.17       is 		return 1;
    540       1.17       is 
    541       1.17       is 	*p++ = b;
    542       1.17       is 	++i;
    543       1.17       is 	while ((p < endp) && (*t++ == ':')) {
    544       1.17       is 		b = strtol(t, &r, 16);
    545       1.17       is 		if (r == t)
    546       1.17       is 			break;
    547       1.17       is 		*p++ = b;
    548       1.17       is 		++i;
    549       1.17       is 		t = r;
    550       1.17       is 	}
    551       1.17       is 	sdl->sdl_alen = i;
    552       1.17       is 
    553       1.17       is 	return 0;
    554        1.1      cgd }
    555        1.1      cgd 
    556        1.8   chopps void
    557        1.1      cgd usage()
    558        1.1      cgd {
    559       1.20    lukem 	extern char *__progname;
    560       1.20    lukem 
    561       1.20    lukem 	(void)fprintf(stderr, "usage: %s [-n] hostname\n", __progname);
    562       1.20    lukem 	(void)fprintf(stderr, "usage: %s [-n] -a\n", __progname);
    563  1.23.10.2       he 	(void)fprintf(stderr, "usage: %s -d [-a|hostname]\n", __progname);
    564        1.8   chopps 	(void)fprintf(stderr,
    565       1.20    lukem 	    "usage: %s -s hostname ether_addr [temp] [pub]\n", __progname);
    566       1.20    lukem 	(void)fprintf(stderr, "usage: %s -f filename\n", __progname);
    567        1.5  mycroft 	exit(1);
    568        1.5  mycroft }
    569        1.5  mycroft 
    570        1.8   chopps int
    571        1.5  mycroft rtmsg(cmd)
    572        1.8   chopps 	int cmd;
    573        1.5  mycroft {
    574        1.5  mycroft 	static int seq;
    575        1.5  mycroft 	int rlen;
    576       1.19    lukem 	struct rt_msghdr *rtm;
    577       1.19    lukem 	char *cp;
    578       1.19    lukem 	int l;
    579        1.5  mycroft 
    580        1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    581        1.8   chopps 	cp = m_rtmsg.m_space;
    582        1.5  mycroft 	errno = 0;
    583        1.8   chopps 
    584        1.5  mycroft 	if (cmd == RTM_DELETE)
    585        1.5  mycroft 		goto doit;
    586        1.8   chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    587        1.5  mycroft 	rtm->rtm_flags = flags;
    588        1.5  mycroft 	rtm->rtm_version = RTM_VERSION;
    589        1.5  mycroft 
    590        1.5  mycroft 	switch (cmd) {
    591        1.5  mycroft 	default:
    592        1.8   chopps 		errx(1, "internal wrong cmd");
    593        1.8   chopps 		/*NOTREACHED*/
    594        1.5  mycroft 	case RTM_ADD:
    595        1.5  mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    596        1.5  mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    597        1.5  mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    598        1.5  mycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    599        1.5  mycroft 		sin_m.sin_other = 0;
    600        1.5  mycroft 		if (doing_proxy) {
    601        1.5  mycroft 			if (export_only)
    602        1.5  mycroft 				sin_m.sin_other = SIN_PROXY;
    603        1.5  mycroft 			else {
    604        1.5  mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    605        1.5  mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    606        1.5  mycroft 			}
    607        1.5  mycroft 		}
    608        1.5  mycroft 		/* FALLTHROUGH */
    609        1.5  mycroft 	case RTM_GET:
    610        1.5  mycroft 		rtm->rtm_addrs |= RTA_DST;
    611        1.5  mycroft 	}
    612        1.5  mycroft #define NEXTADDR(w, s) \
    613        1.5  mycroft 	if (rtm->rtm_addrs & (w)) { \
    614        1.8   chopps 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    615        1.5  mycroft 
    616        1.5  mycroft 	NEXTADDR(RTA_DST, sin_m);
    617        1.5  mycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
    618        1.5  mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    619        1.5  mycroft 
    620        1.5  mycroft 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    621        1.5  mycroft doit:
    622        1.5  mycroft 	l = rtm->rtm_msglen;
    623        1.5  mycroft 	rtm->rtm_seq = ++seq;
    624        1.5  mycroft 	rtm->rtm_type = cmd;
    625        1.5  mycroft 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    626        1.5  mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    627        1.8   chopps 			warn("writing to routing socket");
    628        1.5  mycroft 			return (-1);
    629        1.5  mycroft 		}
    630        1.5  mycroft 	}
    631        1.5  mycroft 	do {
    632        1.5  mycroft 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    633        1.5  mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    634        1.5  mycroft 	if (l < 0)
    635        1.8   chopps 		warn("read from routing socket");
    636        1.5  mycroft 	return (0);
    637        1.5  mycroft }
    638        1.5  mycroft 
    639        1.8   chopps int
    640        1.8   chopps getinetaddr(host, inap)
    641        1.8   chopps 	const char *host;
    642        1.8   chopps 	struct in_addr *inap;
    643        1.5  mycroft {
    644        1.8   chopps 	struct hostent *hp;
    645        1.8   chopps 
    646        1.9   chopps 	if (inet_aton(host, inap) == 1)
    647        1.8   chopps 		return (0);
    648        1.8   chopps 	if ((hp = gethostbyname(host)) == NULL) {
    649       1.13  mycroft 		warnx("%s: %s\n", host, hstrerror(h_errno));
    650        1.8   chopps 		return (-1);
    651        1.8   chopps 	}
    652        1.8   chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    653        1.8   chopps 	return (0);
    654        1.1      cgd }
    655