arp.c revision 111910
1193323Sed/*
2193323Sed * Copyright (c) 1984, 1993
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed *
5193323Sed * This code is derived from software contributed to Berkeley by
6193323Sed * Sun Microsystems, Inc.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed * 1. Redistributions of source code must retain the above copyright
12193323Sed *    notice, this list of conditions and the following disclaimer.
13193323Sed * 2. Redistributions in binary form must reproduce the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer in the
15249423Sdim *    documentation and/or other materials provided with the distribution.
16249423Sdim * 3. All advertising materials mentioning features or use of this software
17193323Sed *    must display the following acknowledgement:
18193323Sed *	This product includes software developed by the University of
19193323Sed *	California, Berkeley and its contributors.
20193323Sed * 4. Neither the name of the University nor the names of its contributors
21193323Sed *    may be used to endorse or promote products derived from this software
22193323Sed *    without specific prior written permission.
23193323Sed *
24193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34193323Sed * SUCH DAMAGE.
35193323Sed */
36193323Sed
37193323Sed#ifndef lint
38193323Sedstatic char const copyright[] =
39193323Sed"@(#) Copyright (c) 1984, 1993\n\
40193323Sed	The Regents of the University of California.  All rights reserved.\n";
41193323Sed#endif /* not lint */
42193323Sed
43193323Sed#ifndef lint
44193323Sed#if 0
45193323Sedstatic char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46193323Sed#endif
47193323Sedstatic const char rcsid[] =
48193323Sed  "$FreeBSD: head/usr.sbin/arp/arp.c 111910 2003-03-05 15:53:18Z ru $";
49193323Sed#endif /* not lint */
50193323Sed
51193323Sed/*
52193323Sed * arp - display, set, and delete arp table entries
53193323Sed */
54193323Sed
55193323Sed
56193323Sed#include <sys/param.h>
57193323Sed#include <sys/file.h>
58193323Sed#include <sys/socket.h>
59193323Sed#include <sys/sockio.h>
60193323Sed#include <sys/sysctl.h>
61193323Sed#include <sys/ioctl.h>
62193323Sed#include <sys/time.h>
63193323Sed
64193323Sed#include <net/if.h>
65193323Sed#include <net/if_dl.h>
66193323Sed#include <net/if_types.h>
67193323Sed#include <net/route.h>
68193323Sed#include <net/iso88025.h>
69193323Sed
70193323Sed#include <netinet/in.h>
71218893Sdim#include <netinet/if_ether.h>
72218893Sdim
73218893Sdim#include <arpa/inet.h>
74218893Sdim
75218893Sdim#include <ctype.h>
76193323Sed#include <err.h>
77218893Sdim#include <errno.h>
78193323Sed#include <netdb.h>
79218893Sdim#include <nlist.h>
80193323Sed#include <paths.h>
81193323Sed#include <stdio.h>
82218893Sdim#include <stdlib.h>
83193323Sed#include <string.h>
84218893Sdim#include <strings.h>
85193323Sed#include <unistd.h>
86218893Sdim
87193323Sedvoid search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
88193323Sed	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
89193323Sedvoid print_entry(struct sockaddr_dl *sdl,
90193323Sed	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
91193323Sedvoid nuke_entry(struct sockaddr_dl *sdl,
92193323Sed	struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
93193323Sedint delete(char *host, char *info);
94193323Sedvoid usage(void);
95193323Sedint set(int argc, char **argv);
96193323Sedint get(char *host);
97193323Sedint file(char *name);
98193323Sedvoid getsocket(void);
99193323Sedint my_ether_aton(char *a, struct ether_addr *n);
100193323Sedint rtmsg(int cmd);
101193323Sedint get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
102193323Sed
103193323Sedstatic pid_t pid;
104193323Sedstatic int nflag;	/* no reverse dns lookups */
105193323Sedstatic int aflag;	/* do it for all entries */
106193323Sedstatic int s = -1;
107193323Sed
108193323Sedstruct	sockaddr_in so_mask;
109193323Sedstruct	sockaddr_inarp blank_sin, sin_m;
110193323Sedstruct	sockaddr_dl blank_sdl, sdl_m;
111193323Sedint	expire_time, flags, doing_proxy, proxy_only, found_entry;
112193323Sedstruct	{
113193323Sed	struct	rt_msghdr m_rtm;
114193323Sed	char	m_space[512];
115193323Sed}	m_rtmsg;
116193323Sed
117193323Sed/* which function we're supposed to do */
118193323Sed#define F_GET		1
119193323Sed#define F_SET		2
120193323Sed#define F_FILESET	3
121193323Sed#define F_REPLACE	4
122193323Sed#define F_DELETE	5
123193323Sed
124193323Sed#define ROUNDUP(a) \
125193323Sed	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
126193323Sed#define SETFUNC(f)	{ if (func) usage(); func = (f); }
127193323Sed
128193323Sedint
129193323Sedmain(int argc, char *argv[])
130193323Sed{
131193323Sed	int ch, func = 0;
132193323Sed	int rtn = 0;
133193323Sed
134193323Sed	pid = getpid();
135193323Sed	while ((ch = getopt(argc, argv, "andfsS")) != -1)
136193323Sed		switch((char)ch) {
137193323Sed		case 'a':
138239462Sdim			aflag = 1;
139239462Sdim			break;
140239462Sdim		case 'd':
141239462Sdim			SETFUNC(F_DELETE);
142239462Sdim			break;
143239462Sdim		case 'n':
144239462Sdim			nflag = 1;
145239462Sdim			break;
146239462Sdim		case 'S':
147239462Sdim			SETFUNC(F_REPLACE);
148239462Sdim			break;
149239462Sdim		case 's':
150239462Sdim			SETFUNC(F_SET);
151193323Sed			break;
152193323Sed		case 'f' :
153193323Sed			SETFUNC(F_FILESET);
154193323Sed			break;
155193323Sed		case '?':
156193323Sed		default:
157193323Sed			usage();
158193323Sed		}
159193323Sed	argc -= optind;
160193323Sed	argv += optind;
161193323Sed
162193323Sed	bzero(&so_mask, sizeof(so_mask));
163193323Sed	so_mask.sin_len = 8;
164249423Sdim	so_mask.sin_addr.s_addr = 0xffffffff;
165193323Sed	bzero(&blank_sin, sizeof(blank_sin));
166193323Sed	blank_sin.sin_len = sizeof(blank_sin);
167193323Sed	blank_sin.sin_family = AF_INET;
168249423Sdim	bzero(&blank_sdl, sizeof(blank_sdl));
169193323Sed	blank_sdl.sdl_len = sizeof(blank_sdl);
170193323Sed	blank_sdl.sdl_family = AF_LINK;
171193323Sed
172193323Sed	if (!func)
173193323Sed		func = F_GET;
174193323Sed	switch (func) {
175193323Sed	case F_GET:
176193323Sed		if (aflag) {
177193323Sed			if (argc != 0)
178193323Sed				usage();
179193323Sed			search(0, print_entry);
180193323Sed		} else {
181193323Sed			if (argc != 1)
182193323Sed				usage();
183193323Sed			get(argv[0]);
184193323Sed		}
185193323Sed		break;
186193323Sed	case F_SET:
187193323Sed	case F_REPLACE:
188193323Sed		if (argc < 2 || argc > 6)
189193323Sed			usage();
190193323Sed		if (func == F_REPLACE)
191193323Sed			(void) delete(argv[0], NULL);
192193323Sed		rtn = set(argc, argv) ? 1 : 0;
193193323Sed		break;
194193323Sed	case F_DELETE:
195193323Sed		if (aflag) {
196193323Sed			if (argc != 0)
197193323Sed				usage();
198193323Sed			search(0, nuke_entry);
199193323Sed		} else {
200193323Sed			if (argc < 1 || argc > 2)
201193323Sed				usage();
202193323Sed			rtn = delete(argv[0], argv[1]);
203193323Sed		}
204193323Sed		break;
205193323Sed	case F_FILESET:
206193323Sed		if (argc != 1)
207193323Sed			usage();
208193323Sed		rtn = file(argv[0]);
209193323Sed		break;
210193323Sed	}
211193323Sed
212193323Sed	return(rtn);
213193323Sed}
214193323Sed
215193323Sed/*
216193323Sed * Process a file to set standard arp entries
217193323Sed */
218193323Sedint
219193323Sedfile(char *name)
220193323Sed{
221193323Sed	FILE *fp;
222193323Sed	int i, retval;
223193323Sed	char line[100], arg[5][50], *args[5], *p;
224193323Sed
225193323Sed	if ((fp = fopen(name, "r")) == NULL)
226193323Sed		errx(1, "cannot open %s", name);
227193323Sed	args[0] = &arg[0][0];
228193323Sed	args[1] = &arg[1][0];
229193323Sed	args[2] = &arg[2][0];
230193323Sed	args[3] = &arg[3][0];
231193323Sed	args[4] = &arg[4][0];
232193323Sed	retval = 0;
233193323Sed	while(fgets(line, 100, fp) != NULL) {
234193323Sed		if ((p = strchr(line, '#')) != NULL)
235193323Sed			*p = '\0';
236193323Sed		for (p = line; isblank(*p); p++);
237193323Sed		if (*p == '\n' || *p == '\0')
238193323Sed			continue;
239193323Sed		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
240193323Sed		    arg[2], arg[3], arg[4]);
241193323Sed		if (i < 2) {
242193323Sed			warnx("bad line: %s", line);
243193323Sed			retval = 1;
244193323Sed			continue;
245193323Sed		}
246193323Sed		if (set(i, args))
247193323Sed			retval = 1;
248193323Sed	}
249193323Sed	fclose(fp);
250193323Sed	return (retval);
251193323Sed}
252193323Sed
253193323Sedvoid
254193323Sedgetsocket(void)
255193323Sed{
256193323Sed	if (s < 0) {
257193323Sed		s = socket(PF_ROUTE, SOCK_RAW, 0);
258193323Sed		if (s < 0)
259193323Sed			err(1, "socket");
260193323Sed	}
261193323Sed}
262193323Sed
263193323Sed/*
264193323Sed * Set an individual arp entry
265193323Sed */
266239462Sdimint
267239462Sdimset(int argc, char **argv)
268239462Sdim{
269239462Sdim	struct hostent *hp;
270239462Sdim	register struct sockaddr_inarp *addr = &sin_m;
271239462Sdim	register struct sockaddr_dl *sdl;
272239462Sdim	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
273239462Sdim	struct ether_addr *ea;
274239462Sdim	char *host = argv[0], *eaddr = argv[1];
275239462Sdim
276239462Sdim	getsocket();
277239462Sdim	argc -= 2;
278239462Sdim	argv += 2;
279239462Sdim	sdl_m = blank_sdl;
280239462Sdim	sin_m = blank_sin;
281239462Sdim	addr->sin_addr.s_addr = inet_addr(host);
282239462Sdim	if (addr->sin_addr.s_addr == INADDR_NONE) {
283239462Sdim		if (!(hp = gethostbyname(host))) {
284239462Sdim			warnx("%s: %s", host, hstrerror(h_errno));
285239462Sdim			return (1);
286239462Sdim		}
287239462Sdim		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
288239462Sdim		    sizeof addr->sin_addr);
289239462Sdim	}
290239462Sdim	doing_proxy = flags = proxy_only = expire_time = 0;
291239462Sdim	while (argc-- > 0) {
292239462Sdim		if (strncmp(argv[0], "temp", 4) == 0) {
293193323Sed			struct timeval tv;
294193323Sed			gettimeofday(&tv, 0);
295193323Sed			expire_time = tv.tv_sec + 20 * 60;
296193323Sed		}
297193323Sed		else if (strncmp(argv[0], "pub", 3) == 0) {
298239462Sdim			flags |= RTF_ANNOUNCE;
299239462Sdim			doing_proxy = 1;
300239462Sdim			if (argc && strncmp(argv[1], "only", 3) == 0) {
301239462Sdim				proxy_only = 1;
302239462Sdim				sin_m.sin_other = SIN_PROXY;
303239462Sdim				argc--; argv++;
304239462Sdim			}
305193323Sed		} else if (strncmp(argv[0], "trail", 5) == 0) {
306193323Sed			printf("%s: Sending trailers is no longer supported\n",
307193323Sed				host);
308193323Sed		}
309193323Sed		argv++;
310193323Sed	}
311193323Sed	ea = (struct ether_addr *)LLADDR(&sdl_m);
312193323Sed	if (doing_proxy && !strcmp(eaddr, "auto")) {
313		if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
314			printf("no interface found for %s\n",
315			       inet_ntoa(addr->sin_addr));
316			return (1);
317		}
318		sdl_m.sdl_alen = ETHER_ADDR_LEN;
319	} else {
320		if (my_ether_aton(eaddr, ea) == 0)
321			sdl_m.sdl_alen = ETHER_ADDR_LEN;
322	}
323tryagain:
324	if (rtmsg(RTM_GET) < 0) {
325		warn("%s", host);
326		return (1);
327	}
328	addr = (struct sockaddr_inarp *)(rtm + 1);
329	sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
330	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
331		if (sdl->sdl_family == AF_LINK &&
332		    (rtm->rtm_flags & RTF_LLINFO) &&
333		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
334		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
335		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
336			goto overwrite;
337		}
338		if (doing_proxy == 0) {
339			printf("set: can only proxy for %s\n", host);
340			return (1);
341		}
342		if (sin_m.sin_other & SIN_PROXY) {
343			printf("set: proxy entry exists for non 802 device\n");
344			return(1);
345		}
346		sin_m.sin_other = SIN_PROXY;
347		proxy_only = 1;
348		goto tryagain;
349	}
350overwrite:
351	if (sdl->sdl_family != AF_LINK) {
352		printf("cannot intuit interface index and type for %s\n", host);
353		return (1);
354	}
355	sdl_m.sdl_type = sdl->sdl_type;
356	sdl_m.sdl_index = sdl->sdl_index;
357	return (rtmsg(RTM_ADD));
358}
359
360/*
361 * Display an individual arp entry
362 */
363int
364get(char *host)
365{
366	struct hostent *hp;
367	struct sockaddr_inarp *addr = &sin_m;
368
369	sin_m = blank_sin;
370	addr->sin_addr.s_addr = inet_addr(host);
371	if (addr->sin_addr.s_addr == INADDR_NONE) {
372		if (!(hp = gethostbyname(host)))
373			errx(1, "%s: %s", host, hstrerror(h_errno));
374		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
375		    sizeof addr->sin_addr);
376	}
377	search(addr->sin_addr.s_addr, print_entry);
378	if (found_entry == 0) {
379		printf("%s (%s) -- no entry\n",
380		    host, inet_ntoa(addr->sin_addr));
381		return(1);
382	}
383	return(0);
384}
385
386/*
387 * Delete an arp entry
388 */
389int
390delete(char *host, char *info)
391{
392	struct hostent *hp;
393	register struct sockaddr_inarp *addr = &sin_m;
394	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
395	struct sockaddr_dl *sdl;
396
397	getsocket();
398	sin_m = blank_sin;
399	if (info) {
400		if (strncmp(info, "pub", 3) == 0)
401			sin_m.sin_other = SIN_PROXY;
402		else
403			usage();
404	}
405	addr->sin_addr.s_addr = inet_addr(host);
406	if (addr->sin_addr.s_addr == INADDR_NONE) {
407		if (!(hp = gethostbyname(host))) {
408			warnx("%s: %s", host, hstrerror(h_errno));
409			return (1);
410		}
411		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
412		    sizeof addr->sin_addr);
413	}
414tryagain:
415	if (rtmsg(RTM_GET) < 0) {
416		warn("%s", host);
417		return (1);
418	}
419	addr = (struct sockaddr_inarp *)(rtm + 1);
420	sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
421	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
422		if (sdl->sdl_family == AF_LINK &&
423		    (rtm->rtm_flags & RTF_LLINFO) &&
424		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
425		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
426		case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
427			goto delete;
428		}
429	}
430	if (sin_m.sin_other & SIN_PROXY) {
431		fprintf(stderr, "delete: can't locate %s\n",host);
432		return (1);
433	} else {
434		sin_m.sin_other = SIN_PROXY;
435		goto tryagain;
436	}
437delete:
438	if (sdl->sdl_family != AF_LINK) {
439		printf("cannot locate %s\n", host);
440		return (1);
441	}
442	if (rtmsg(RTM_DELETE) == 0) {
443		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
444		return (0);
445	}
446	return (1);
447}
448
449/*
450 * Search the arp table and do some action on matching entries
451 */
452void
453search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
454	struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
455{
456	int mib[6];
457	size_t needed;
458	char *lim, *buf, *next;
459	struct rt_msghdr *rtm;
460	struct sockaddr_inarp *sin2;
461	struct sockaddr_dl *sdl;
462
463	mib[0] = CTL_NET;
464	mib[1] = PF_ROUTE;
465	mib[2] = 0;
466	mib[3] = AF_INET;
467	mib[4] = NET_RT_FLAGS;
468	mib[5] = RTF_LLINFO;
469	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
470		errx(1, "route-sysctl-estimate");
471	if ((buf = malloc(needed)) == NULL)
472		errx(1, "malloc");
473	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
474		errx(1, "actual retrieval of routing table");
475	lim = buf + needed;
476	for (next = buf; next < lim; next += rtm->rtm_msglen) {
477		rtm = (struct rt_msghdr *)next;
478		sin2 = (struct sockaddr_inarp *)(rtm + 1);
479		(char *)sdl = (char *)sin2 + ROUNDUP(sin2->sin_len);
480		if (addr) {
481			if (addr != sin2->sin_addr.s_addr)
482				continue;
483			found_entry = 1;
484		}
485		(*action)(sdl, sin2, rtm);
486	}
487	free(buf);
488}
489
490/*
491 * Display an arp entry
492 */
493void
494print_entry(struct sockaddr_dl *sdl,
495	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
496{
497	const char *host;
498	struct hostent *hp;
499	struct iso88025_sockaddr_dl_data *trld;
500	char ifname[IF_NAMESIZE];
501	int seg;
502
503	if (nflag == 0)
504		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
505		    sizeof addr->sin_addr, AF_INET);
506	else
507		hp = 0;
508	if (hp)
509		host = hp->h_name;
510	else {
511		host = "?";
512		if (h_errno == TRY_AGAIN)
513			nflag = 1;
514	}
515	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
516	if (sdl->sdl_alen)
517		printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
518	else
519		printf("(incomplete)");
520	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
521		printf(" on %s", ifname);
522	if (rtm->rtm_rmx.rmx_expire == 0)
523		printf(" permanent");
524	if (addr->sin_other & SIN_PROXY)
525		printf(" published (proxy only)");
526	if (rtm->rtm_addrs & RTA_NETMASK) {
527		addr = (struct sockaddr_inarp *)
528			(ROUNDUP(sdl->sdl_len) + (char *)sdl);
529		if (addr->sin_addr.s_addr == 0xffffffff)
530			printf(" published");
531		if (addr->sin_len != 8)
532			printf("(weird)");
533	}
534        switch(sdl->sdl_type) {
535            case IFT_ETHER:
536                printf(" [ethernet]");
537                break;
538            case IFT_ISO88025:
539                printf(" [token-ring]");
540		trld = SDL_ISO88025(sdl);
541		if (trld->trld_rcf != 0) {
542			printf(" rt=%x", ntohs(trld->trld_rcf));
543			for (seg = 0;
544			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
545			     seg++)
546				printf(":%x", ntohs(*(trld->trld_route[seg])));
547		}
548                break;
549            case IFT_FDDI:
550                printf(" [fddi]");
551                break;
552            case IFT_ATM:
553                printf(" [atm]");
554                break;
555	    case IFT_L2VLAN:
556		printf(" [vlan]");
557		break;
558            default:
559		break;
560        }
561
562	printf("\n");
563
564}
565
566/*
567 * Nuke an arp entry
568 */
569void
570nuke_entry(struct sockaddr_dl *sdl __unused,
571	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
572{
573	char ip[20];
574
575	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
576	delete(ip, NULL);
577}
578
579int
580my_ether_aton(char *a, struct ether_addr *n)
581{
582	struct ether_addr *ea;
583
584	if ((ea = ether_aton(a)) == NULL) {
585		warnx("invalid Ethernet address '%s'", a);
586		return (1);
587	}
588	*n = *ea;
589	return (0);
590}
591
592void
593usage(void)
594{
595	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
596		"usage: arp [-n] hostname",
597		"       arp [-n] -a",
598		"       arp -d hostname [pub]",
599		"       arp -d -a",
600		"       arp -s hostname ether_addr [temp] [pub]",
601		"       arp -S hostname ether_addr [temp] [pub]",
602		"       arp -f filename");
603	exit(1);
604}
605
606int
607rtmsg(int cmd)
608{
609	static int seq;
610	int rlen;
611	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
612	register char *cp = m_rtmsg.m_space;
613	register int l;
614
615	errno = 0;
616	if (cmd == RTM_DELETE)
617		goto doit;
618	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
619	rtm->rtm_flags = flags;
620	rtm->rtm_version = RTM_VERSION;
621
622	switch (cmd) {
623	default:
624		errx(1, "internal wrong cmd");
625	case RTM_ADD:
626		rtm->rtm_addrs |= RTA_GATEWAY;
627		rtm->rtm_rmx.rmx_expire = expire_time;
628		rtm->rtm_inits = RTV_EXPIRE;
629		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
630		sin_m.sin_other = 0;
631		if (doing_proxy) {
632			if (proxy_only)
633				sin_m.sin_other = SIN_PROXY;
634			else {
635				rtm->rtm_addrs |= RTA_NETMASK;
636				rtm->rtm_flags &= ~RTF_HOST;
637			}
638		}
639		/* FALLTHROUGH */
640	case RTM_GET:
641		rtm->rtm_addrs |= RTA_DST;
642	}
643#define NEXTADDR(w, s) \
644	if (rtm->rtm_addrs & (w)) { \
645		bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
646
647	NEXTADDR(RTA_DST, sin_m);
648	NEXTADDR(RTA_GATEWAY, sdl_m);
649	NEXTADDR(RTA_NETMASK, so_mask);
650
651	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
652doit:
653	l = rtm->rtm_msglen;
654	rtm->rtm_seq = ++seq;
655	rtm->rtm_type = cmd;
656	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
657		if (errno != ESRCH || cmd != RTM_DELETE) {
658			warn("writing to routing socket");
659			return (-1);
660		}
661	}
662	do {
663		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
664	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
665	if (l < 0)
666		warn("read from routing socket");
667	return (0);
668}
669
670/*
671 * get_ether_addr - get the hardware address of an interface on the
672 * the same subnet as ipaddr.
673 */
674#define MAX_IFS		32
675
676int
677get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
678{
679	struct ifreq *ifr, *ifend, *ifp;
680	u_int32_t ina, mask;
681	struct sockaddr_dl *dla;
682	struct ifreq ifreq;
683	struct ifconf ifc;
684	struct ifreq ifs[MAX_IFS];
685	int sock;
686
687	sock = socket(AF_INET, SOCK_DGRAM, 0);
688	if (sock < 0)
689		err(1, "socket");
690
691	ifc.ifc_len = sizeof(ifs);
692	ifc.ifc_req = ifs;
693	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
694		warnx("ioctl(SIOCGIFCONF)");
695		close(sock);
696		return 0;
697	}
698
699	/*
700	* Scan through looking for an interface with an Internet
701	* address on the same subnet as `ipaddr'.
702	*/
703	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
704	for (ifr = ifc.ifc_req; ifr < ifend; ) {
705		if (ifr->ifr_addr.sa_family == AF_INET) {
706			ina = ((struct sockaddr_in *)
707				&ifr->ifr_addr)->sin_addr.s_addr;
708			strncpy(ifreq.ifr_name, ifr->ifr_name,
709				sizeof(ifreq.ifr_name));
710			/*
711			 * Check that the interface is up,
712			 * and not point-to-point or loopback.
713			 */
714			if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
715				continue;
716			if ((ifreq.ifr_flags &
717			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
718					IFF_LOOPBACK|IFF_NOARP))
719			     != (IFF_UP|IFF_BROADCAST))
720				goto nextif;
721			/*
722			 * Get its netmask and check that it's on
723			 * the right subnet.
724			 */
725			if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
726				continue;
727			mask = ((struct sockaddr_in *)
728				&ifreq.ifr_addr)->sin_addr.s_addr;
729			if ((ipaddr & mask) != (ina & mask))
730				goto nextif;
731			break;
732		}
733nextif:
734		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
735		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
736	}
737
738	if (ifr >= ifend) {
739		close(sock);
740		return 0;
741	}
742
743	/*
744	* Now scan through again looking for a link-level address
745	* for this interface.
746	*/
747	ifp = ifr;
748	for (ifr = ifc.ifc_req; ifr < ifend; ) {
749		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
750		    && ifr->ifr_addr.sa_family == AF_LINK) {
751			/*
752			 * Found the link-level address - copy it out
753			 */
754		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
755			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
756			close (sock);
757			printf("using interface %s for proxy with address ",
758				ifp->ifr_name);
759			printf("%s\n", ether_ntoa(hwaddr));
760			return dla->sdl_alen;
761		}
762		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
763		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
764	}
765	return 0;
766}
767