arp.c revision 128186
1338530Sdelphij/*
2280849Scy * Copyright (c) 1984, 1993
3280849Scy *	The Regents of the University of California.  All rights reserved.
4280849Scy *
5280849Scy * This code is derived from software contributed to Berkeley by
6280849Scy * Sun Microsystems, Inc.
7280849Scy *
8338530Sdelphij * Redistribution and use in source and binary forms, with or without
9280849Scy * modification, are permitted provided that the following conditions
10280849Scy * are met:
11280849Scy * 1. Redistributions of source code must retain the above copyright
12280849Scy *    notice, this list of conditions and the following disclaimer.
13280849Scy * 2. Redistributions in binary form must reproduce the above copyright
14280849Scy *    notice, this list of conditions and the following disclaimer in the
15280849Scy *    documentation and/or other materials provided with the distribution.
16280849Scy * 3. All advertising materials mentioning features or use of this software
17280849Scy *    must display the following acknowledgement:
18280849Scy *	This product includes software developed by the University of
19280849Scy *	California, Berkeley and its contributors.
20280849Scy * 4. Neither the name of the University nor the names of its contributors
21280849Scy *    may be used to endorse or promote products derived from this software
22280849Scy *    without specific prior written permission.
23280849Scy *
24280849Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25280849Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26280849Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27280849Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28280849Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29280849Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30280849Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31280849Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32280849Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33280849Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34280849Scy * SUCH DAMAGE.
35280849Scy */
36280849Scy
37280849Scy#if 0
38280849Scy#ifndef lint
39280849Scystatic char const copyright[] =
40280849Scy"@(#) Copyright (c) 1984, 1993\n\
41280849Scy	The Regents of the University of California.  All rights reserved.\n";
42280849Scy#endif /* not lint */
43280849Scy
44280849Scy#ifndef lint
45280849Scystatic char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46280849Scy#endif /* not lint */
47280849Scy#endif
48280849Scy#include <sys/cdefs.h>
49280849Scy__FBSDID("$FreeBSD: head/usr.sbin/arp/arp.c 128186 2004-04-13 11:24:43Z luigi $");
50280849Scy
51280849Scy/*
52280849Scy * arp - display, set, and delete arp table entries
53280849Scy */
54280849Scy
55280849Scy
56280849Scy#include <sys/param.h>
57280849Scy#include <sys/file.h>
58280849Scy#include <sys/socket.h>
59280849Scy#include <sys/sockio.h>
60280849Scy#include <sys/sysctl.h>
61280849Scy#include <sys/ioctl.h>
62289764Sglebius#include <sys/time.h>
63289764Sglebius
64280849Scy#include <net/if.h>
65280849Scy#include <net/if_dl.h>
66280849Scy#include <net/if_types.h>
67280849Scy#include <net/route.h>
68280849Scy#include <net/iso88025.h>
69280849Scy
70280849Scy#include <netinet/in.h>
71280849Scy#include <netinet/if_ether.h>
72280849Scy
73280849Scy#include <arpa/inet.h>
74280849Scy
75280849Scy#include <ctype.h>
76280849Scy#include <err.h>
77280849Scy#include <errno.h>
78280849Scy#include <netdb.h>
79280849Scy#include <nlist.h>
80280849Scy#include <paths.h>
81280849Scy#include <stdio.h>
82280849Scy#include <stdlib.h>
83280849Scy#include <string.h>
84280849Scy#include <strings.h>
85280849Scy#include <unistd.h>
86280849Scy
87280849Scytypedef void (action_fn)(struct sockaddr_dl *sdl,
88280849Scy        struct sockaddr_inarp *s_in, struct rt_msghdr *rtm);
89280849Scy
90280849Scystatic int search(u_long addr, action_fn *action);
91280849Scystatic action_fn print_entry;
92280849Scystatic action_fn nuke_entry;
93280849Scy
94280849Scystatic int delete(char *host, char *info);
95280849Scystatic void usage(void);
96280849Scystatic int set(int argc, char **argv);
97280849Scystatic int get(char *host);
98280849Scystatic int file(char *name);
99280849Scystatic int my_ether_aton(char *a, struct ether_addr *n);
100280849Scystatic struct rt_msghdr *rtmsg(int cmd);
101280849Scystatic int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
102280849Scy
103280849Scystatic int nflag;	/* no reverse dns lookups */
104280849Scystatic char *rifname;
105280849Scy
106280849Scystatic struct	sockaddr_inarp blank_sin, sin_m;
107280849Scystatic struct	sockaddr_dl sdl_m;
108280849Scystatic int	expire_time, flags, doing_proxy, proxy_only;
109280849Scy/* which function we're supposed to do */
110280849Scy#define F_GET		1
111280849Scy#define F_SET		2
112280849Scy#define F_FILESET	3
113280849Scy#define F_REPLACE	4
114280849Scy#define F_DELETE	5
115280849Scy
116280849Scy#define SETFUNC(f)	{ if (func) usage(); func = (f); }
117280849Scy
118280849Scyint
119280849Scymain(int argc, char *argv[])
120280849Scy{
121280849Scy	int ch, func = 0;
122280849Scy	int rtn = 0;
123280849Scy	int aflag = 0;	/* do it for all entries */
124280849Scy
125280849Scy	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
126280849Scy		switch((char)ch) {
127280849Scy		case 'a':
128280849Scy			aflag = 1;
129280849Scy			break;
130280849Scy		case 'd':
131280849Scy			SETFUNC(F_DELETE);
132280849Scy			break;
133280849Scy		case 'n':
134280849Scy			nflag = 1;
135280849Scy			break;
136280849Scy		case 'S':
137280849Scy			SETFUNC(F_REPLACE);
138280849Scy			break;
139280849Scy		case 's':
140280849Scy			SETFUNC(F_SET);
141280849Scy			break;
142280849Scy		case 'f' :
143330106Sdelphij			SETFUNC(F_FILESET);
144330106Sdelphij			break;
145330106Sdelphij		case 'i':
146330106Sdelphij			rifname = optarg;
147330106Sdelphij			break;
148280849Scy		case '?':
149280849Scy		default:
150280849Scy			usage();
151280849Scy		}
152280849Scy	argc -= optind;
153280849Scy	argv += optind;
154280849Scy
155280849Scy	bzero(&blank_sin, sizeof(blank_sin));
156280849Scy	blank_sin.sin_len = sizeof(blank_sin);
157280849Scy	blank_sin.sin_family = AF_INET;
158280849Scy
159280849Scy	if (!func)
160280849Scy		func = F_GET;
161280849Scy	if (rifname) {
162280849Scy		if (func != F_GET)
163280849Scy			errx(1, "-i not applicable to this operation");
164280849Scy		if (if_nametoindex(rifname) == 0) {
165280849Scy			if (errno == ENXIO)
166280849Scy				errx(1, "interface %s does not exist", rifname);
167280849Scy			else
168280849Scy				err(1, "if_nametoindex(%s)", rifname);
169280849Scy		}
170280849Scy	}
171280849Scy	switch (func) {
172280849Scy	case F_GET:
173280849Scy		if (aflag) {
174280849Scy			if (argc != 0)
175280849Scy				usage();
176280849Scy			search(0, print_entry);
177280849Scy		} else {
178280849Scy			if (argc != 1)
179280849Scy				usage();
180280849Scy			get(argv[0]);
181280849Scy		}
182280849Scy		break;
183280849Scy	case F_SET:
184280849Scy	case F_REPLACE:
185280849Scy		if (argc < 2 || argc > 6)
186280849Scy			usage();
187280849Scy		if (func == F_REPLACE)
188280849Scy			(void) delete(argv[0], NULL);
189280849Scy		rtn = set(argc, argv) ? 1 : 0;
190280849Scy		break;
191280849Scy	case F_DELETE:
192280849Scy		if (aflag) {
193280849Scy			if (argc != 0)
194280849Scy				usage();
195280849Scy			search(0, nuke_entry);
196280849Scy		} else {
197280849Scy			if (argc < 1 || argc > 2)
198280849Scy				usage();
199280849Scy			rtn = delete(argv[0], argv[1]);
200280849Scy		}
201280849Scy		break;
202280849Scy	case F_FILESET:
203280849Scy		if (argc != 1)
204280849Scy			usage();
205280849Scy		rtn = file(argv[0]);
206280849Scy		break;
207280849Scy	}
208280849Scy
209280849Scy	return(rtn);
210280849Scy}
211280849Scy
212280849Scy/*
213280849Scy * Process a file to set standard arp entries
214280849Scy */
215280849Scystatic int
216280849Scyfile(char *name)
217280849Scy{
218280849Scy	FILE *fp;
219280849Scy	int i, retval;
220280849Scy	char line[100], arg[5][50], *args[5], *p;
221304878Scy
222280849Scy	if ((fp = fopen(name, "r")) == NULL)
223280849Scy		err(1, "cannot open %s", name);
224280849Scy	args[0] = &arg[0][0];
225280849Scy	args[1] = &arg[1][0];
226280849Scy	args[2] = &arg[2][0];
227280849Scy	args[3] = &arg[3][0];
228280849Scy	args[4] = &arg[4][0];
229280849Scy	retval = 0;
230280849Scy	while(fgets(line, 100, fp) != NULL) {
231280849Scy		if ((p = strchr(line, '#')) != NULL)
232280849Scy			*p = '\0';
233280849Scy		for (p = line; isblank(*p); p++);
234280849Scy		if (*p == '\n' || *p == '\0')
235280849Scy			continue;
236280849Scy		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
237280849Scy		    arg[2], arg[3], arg[4]);
238280849Scy		if (i < 2) {
239280849Scy			warnx("bad line: %s", line);
240280849Scy			retval = 1;
241280849Scy			continue;
242280849Scy		}
243280849Scy		if (set(i, args))
244280849Scy			retval = 1;
245280849Scy	}
246280849Scy	fclose(fp);
247280849Scy	return (retval);
248280849Scy}
249280849Scy
250280849Scy/*
251280849Scy * Set an individual arp entry
252280849Scy */
253280849Scyint
254280849Scyset(int argc, char **argv)
255280849Scy{
256280849Scy	struct hostent *hp;
257280849Scy	struct sockaddr_inarp *addr = &sin_m;
258280849Scy	struct sockaddr_dl *sdl;
259280849Scy	struct rt_msghdr *rtm;
260280849Scy	struct ether_addr *ea;
261280849Scy	char *host = argv[0], *eaddr = argv[1];
262280849Scy
263280849Scy	argc -= 2;
264280849Scy	argv += 2;
265280849Scy
266280849Scy	bzero(&sdl_m, sizeof(sdl_m));
267280849Scy	sdl_m.sdl_len = sizeof(sdl_m);
268280849Scy	sdl_m.sdl_family = AF_LINK;
269280849Scy
270280849Scy	sin_m = blank_sin;
271280849Scy	addr->sin_addr.s_addr = inet_addr(host);
272280849Scy	if (addr->sin_addr.s_addr == INADDR_NONE) {
273280849Scy		if (!(hp = gethostbyname(host))) {
274280849Scy			warnx("%s: %s", host, hstrerror(h_errno));
275280849Scy			return (1);
276280849Scy		}
277280849Scy		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
278280849Scy		    sizeof addr->sin_addr);
279280849Scy	}
280280849Scy	doing_proxy = flags = proxy_only = expire_time = 0;
281280849Scy	while (argc-- > 0) {
282280849Scy		if (strncmp(argv[0], "temp", 4) == 0) {
283280849Scy			struct timeval tv;
284280849Scy			gettimeofday(&tv, 0);
285280849Scy			expire_time = tv.tv_sec + 20 * 60;
286280849Scy		}
287280849Scy		else if (strncmp(argv[0], "pub", 3) == 0) {
288280849Scy			flags |= RTF_ANNOUNCE;
289280849Scy			doing_proxy = 1;
290280849Scy			if (argc && strncmp(argv[1], "only", 3) == 0) {
291280849Scy				proxy_only = 1;
292280849Scy				sin_m.sin_other = SIN_PROXY;
293280849Scy				argc--; argv++;
294280849Scy			}
295280849Scy		} else if (strncmp(argv[0], "trail", 5) == 0) {
296280849Scy			printf("%s: Sending trailers is no longer supported\n",
297280849Scy				host);
298280849Scy		}
299280849Scy		argv++;
300280849Scy	}
301280849Scy	ea = (struct ether_addr *)LLADDR(&sdl_m);
302280849Scy	if (doing_proxy && !strcmp(eaddr, "auto")) {
303280849Scy		if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
304280849Scy			printf("no interface found for %s\n",
305280849Scy			       inet_ntoa(addr->sin_addr));
306280849Scy			return (1);
307280849Scy		}
308280849Scy		sdl_m.sdl_alen = ETHER_ADDR_LEN;
309280849Scy	} else {
310280849Scy		if (my_ether_aton(eaddr, ea) == 0)
311280849Scy			sdl_m.sdl_alen = ETHER_ADDR_LEN;
312280849Scy	}
313316068Sdelphijtryagain:
314280849Scy	rtm = rtmsg(RTM_GET);
315280849Scy	if (rtm == NULL) {
316280849Scy		warn("%s", host);
317280849Scy		return (1);
318280849Scy	}
319280849Scy	addr = (struct sockaddr_inarp *)(rtm + 1);
320	sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
321	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
322		if (sdl->sdl_family == AF_LINK &&
323		    (rtm->rtm_flags & RTF_LLINFO) &&
324		    !(rtm->rtm_flags & RTF_GATEWAY))
325			switch (sdl->sdl_type) {
326			case IFT_ETHER:
327			case IFT_FDDI:
328			case IFT_ISO88023:
329			case IFT_ISO88024:
330			case IFT_ISO88025:
331			case IFT_L2VLAN:
332				goto overwrite;
333			}
334		if (doing_proxy == 0) {
335			printf("set: can only proxy for %s\n", host);
336			return (1);
337		}
338		if (sin_m.sin_other & SIN_PROXY) {
339			printf("set: proxy entry exists for non 802 device\n");
340			return(1);
341		}
342		sin_m.sin_other = SIN_PROXY;
343		proxy_only = 1;
344		goto tryagain;
345	}
346overwrite:
347	if (sdl->sdl_family != AF_LINK) {
348		printf("cannot intuit interface index and type for %s\n", host);
349		return (1);
350	}
351	sdl_m.sdl_type = sdl->sdl_type;
352	sdl_m.sdl_index = sdl->sdl_index;
353	return (rtmsg(RTM_ADD) != NULL);
354}
355
356/*
357 * Display an individual arp entry
358 */
359static int
360get(char *host)
361{
362	struct hostent *hp;
363	struct sockaddr_inarp *addr = &sin_m;
364
365	sin_m = blank_sin;
366	addr->sin_addr.s_addr = inet_addr(host);
367	if (addr->sin_addr.s_addr == INADDR_NONE) {
368		if (!(hp = gethostbyname(host)))
369			errx(1, "%s: %s", host, hstrerror(h_errno));
370		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
371		    sizeof addr->sin_addr);
372	}
373	if (0 == search(addr->sin_addr.s_addr, print_entry)) {
374		printf("%s (%s) -- no entry",
375		    host, inet_ntoa(addr->sin_addr));
376		if (rifname)
377			printf(" on %s", rifname);
378		printf("\n");
379		return(1);
380	}
381	return(0);
382}
383
384/*
385 * Delete an arp entry
386 */
387static int
388delete(char *host, char *info)
389{
390	struct hostent *hp;
391	struct sockaddr_inarp *addr = &sin_m;
392	struct rt_msghdr *rtm;
393	struct sockaddr_dl *sdl;
394
395	sin_m = blank_sin;
396	if (info) {
397		if (strncmp(info, "pub", 3) == 0)
398			sin_m.sin_other = SIN_PROXY;
399		else
400			usage();
401	}
402	addr->sin_addr.s_addr = inet_addr(host);
403	if (addr->sin_addr.s_addr == INADDR_NONE) {
404		if (!(hp = gethostbyname(host))) {
405			warnx("%s: %s", host, hstrerror(h_errno));
406			return (1);
407		}
408		bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
409		    sizeof addr->sin_addr);
410	}
411tryagain:
412	rtm = rtmsg(RTM_GET);
413	if (rtm == NULL) {
414		warn("%s", host);
415		return (1);
416	}
417	addr = (struct sockaddr_inarp *)(rtm + 1);
418	sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
419	if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
420		if (sdl->sdl_family == AF_LINK &&
421		    (rtm->rtm_flags & RTF_LLINFO) &&
422		    !(rtm->rtm_flags & RTF_GATEWAY))
423			switch (sdl->sdl_type) {
424			case IFT_ETHER:
425			case IFT_FDDI:
426			case IFT_ISO88023:
427			case IFT_ISO88024:
428			case IFT_ISO88025:
429			case IFT_L2VLAN:
430				goto delete;
431			}
432	}
433	if (sin_m.sin_other & SIN_PROXY) {
434		fprintf(stderr, "delete: can't locate %s\n",host);
435		return (1);
436	} else {
437		sin_m.sin_other = SIN_PROXY;
438		goto tryagain;
439	}
440delete:
441	if (sdl->sdl_family != AF_LINK) {
442		printf("cannot locate %s\n", host);
443		return (1);
444	}
445	if (rtmsg(RTM_DELETE) != NULL) {
446		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
447		return (0);
448	}
449	return (1);
450}
451
452/*
453 * Search the arp table and do some action on matching entries
454 */
455static int
456search(u_long addr, action_fn *action)
457{
458	int mib[6];
459	size_t needed;
460	char *lim, *buf, *next;
461	struct rt_msghdr *rtm;
462	struct sockaddr_inarp *sin2;
463	struct sockaddr_dl *sdl;
464	char ifname[IF_NAMESIZE];
465	int found_entry = 0;
466
467	mib[0] = CTL_NET;
468	mib[1] = PF_ROUTE;
469	mib[2] = 0;
470	mib[3] = AF_INET;
471	mib[4] = NET_RT_FLAGS;
472	mib[5] = RTF_LLINFO;
473	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
474		err(1, "route-sysctl-estimate");
475	if (needed == 0)
476		return 0;
477	if ((buf = malloc(needed)) == NULL)
478		err(1, "malloc");
479	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
480		err(1, "actual retrieval of routing table");
481	lim = buf + needed;
482	for (next = buf; next < lim; next += rtm->rtm_msglen) {
483		rtm = (struct rt_msghdr *)next;
484		sin2 = (struct sockaddr_inarp *)(rtm + 1);
485		(char *)sdl = (char *)sin2 + SA_SIZE(sin2);
486		if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
487		    strcmp(ifname, rifname))
488			continue;
489		if (addr) {
490			if (addr != sin2->sin_addr.s_addr)
491				continue;
492			found_entry = 1;
493		}
494		(*action)(sdl, sin2, rtm);
495	}
496	free(buf);
497	return found_entry;
498}
499
500/*
501 * Display an arp entry
502 */
503static void
504print_entry(struct sockaddr_dl *sdl,
505	struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
506{
507	const char *host;
508	struct hostent *hp;
509	struct iso88025_sockaddr_dl_data *trld;
510	char ifname[IF_NAMESIZE];
511	int seg;
512
513	if (nflag == 0)
514		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
515		    sizeof addr->sin_addr, AF_INET);
516	else
517		hp = 0;
518	if (hp)
519		host = hp->h_name;
520	else {
521		host = "?";
522		if (h_errno == TRY_AGAIN)
523			nflag = 1;
524	}
525	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
526	if (sdl->sdl_alen)
527		printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
528	else
529		printf("(incomplete)");
530	if (if_indextoname(sdl->sdl_index, ifname) != NULL)
531		printf(" on %s", ifname);
532	if (rtm->rtm_rmx.rmx_expire == 0)
533		printf(" permanent");
534	if (addr->sin_other & SIN_PROXY)
535		printf(" published (proxy only)");
536	if (rtm->rtm_addrs & RTA_NETMASK) {
537		addr = (struct sockaddr_inarp *)
538			(SA_SIZE(sdl) + (char *)sdl);
539		if (addr->sin_addr.s_addr == 0xffffffff)
540			printf(" published");
541		if (addr->sin_len != 8)
542			printf("(weird)");
543	}
544        switch(sdl->sdl_type) {
545	case IFT_ETHER:
546                printf(" [ethernet]");
547                break;
548	case IFT_ISO88025:
549                printf(" [token-ring]");
550		trld = SDL_ISO88025(sdl);
551		if (trld->trld_rcf != 0) {
552			printf(" rt=%x", ntohs(trld->trld_rcf));
553			for (seg = 0;
554			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
555			     seg++)
556				printf(":%x", ntohs(*(trld->trld_route[seg])));
557		}
558                break;
559	case IFT_FDDI:
560                printf(" [fddi]");
561                break;
562	case IFT_ATM:
563                printf(" [atm]");
564                break;
565	case IFT_L2VLAN:
566		printf(" [vlan]");
567		break;
568	default:
569		break;
570        }
571
572	printf("\n");
573
574}
575
576/*
577 * Nuke an arp entry
578 */
579static void
580nuke_entry(struct sockaddr_dl *sdl __unused,
581	struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
582{
583	char ip[20];
584
585	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
586	delete(ip, NULL);
587}
588
589static int
590my_ether_aton(char *a, struct ether_addr *n)
591{
592	struct ether_addr *ea;
593
594	if ((ea = ether_aton(a)) == NULL) {
595		warnx("invalid Ethernet address '%s'", a);
596		return (1);
597	}
598	*n = *ea;
599	return (0);
600}
601
602static void
603usage(void)
604{
605	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
606		"usage: arp [-n] [-i interface] hostname",
607		"       arp [-n] [-i interface] -a",
608		"       arp -d hostname [pub]",
609		"       arp -d -a",
610		"       arp -s hostname ether_addr [temp] [pub]",
611		"       arp -S hostname ether_addr [temp] [pub]",
612		"       arp -f filename");
613	exit(1);
614}
615
616static struct rt_msghdr *
617rtmsg(int cmd)
618{
619	static int seq;
620	int rlen;
621	int l;
622	struct sockaddr_in so_mask;
623	static int s = -1;
624	static pid_t pid;
625
626	static struct	{
627		struct	rt_msghdr m_rtm;
628		char	m_space[512];
629	}	m_rtmsg;
630
631	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
632	char *cp = m_rtmsg.m_space;
633
634	if (s < 0) {	/* first time: open socket, get pid */
635		s = socket(PF_ROUTE, SOCK_RAW, 0);
636		if (s < 0)
637			err(1, "socket");
638		pid = getpid();
639	}
640	bzero(&so_mask, sizeof(so_mask));
641	so_mask.sin_len = 8;
642	so_mask.sin_addr.s_addr = 0xffffffff;
643
644	errno = 0;
645	if (cmd == RTM_DELETE)
646		goto doit;
647	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
648	rtm->rtm_flags = flags;
649	rtm->rtm_version = RTM_VERSION;
650
651	switch (cmd) {
652	default:
653		errx(1, "internal wrong cmd");
654	case RTM_ADD:
655		rtm->rtm_addrs |= RTA_GATEWAY;
656		rtm->rtm_rmx.rmx_expire = expire_time;
657		rtm->rtm_inits = RTV_EXPIRE;
658		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
659		sin_m.sin_other = 0;
660		if (doing_proxy) {
661			if (proxy_only)
662				sin_m.sin_other = SIN_PROXY;
663			else {
664				rtm->rtm_addrs |= RTA_NETMASK;
665				rtm->rtm_flags &= ~RTF_HOST;
666			}
667		}
668		/* FALLTHROUGH */
669	case RTM_GET:
670		rtm->rtm_addrs |= RTA_DST;
671	}
672#define NEXTADDR(w, s) \
673	if (rtm->rtm_addrs & (w)) { \
674		bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
675
676	NEXTADDR(RTA_DST, sin_m);
677	NEXTADDR(RTA_GATEWAY, sdl_m);
678	NEXTADDR(RTA_NETMASK, so_mask);
679
680	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
681doit:
682	l = rtm->rtm_msglen;
683	rtm->rtm_seq = ++seq;
684	rtm->rtm_type = cmd;
685	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
686		if (errno != ESRCH || cmd != RTM_DELETE) {
687			warn("writing to routing socket");
688			return NULL;
689		}
690	}
691	do {
692		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
693	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
694	if (l < 0)
695		warn("read from routing socket");
696	return rtm;
697}
698
699/*
700 * get_ether_addr - get the hardware address of an interface on the
701 * the same subnet as ipaddr.
702 */
703#define MAX_IFS		32
704
705static int
706get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
707{
708	struct ifreq *ifr, *ifend, *ifp;
709	u_int32_t ina, mask;
710	struct sockaddr_dl *dla;
711	struct ifreq ifreq;
712	struct ifconf ifc;
713	struct ifreq ifs[MAX_IFS];
714	int sock;
715
716	sock = socket(AF_INET, SOCK_DGRAM, 0);
717	if (sock < 0)
718		err(1, "socket");
719
720	ifc.ifc_len = sizeof(ifs);
721	ifc.ifc_req = ifs;
722	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
723		warnx("ioctl(SIOCGIFCONF)");
724		close(sock);
725		return 0;
726	}
727
728	/*
729	* Scan through looking for an interface with an Internet
730	* address on the same subnet as `ipaddr'.
731	*/
732	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
733	for (ifr = ifc.ifc_req; ifr < ifend; ) {
734		if (ifr->ifr_addr.sa_family == AF_INET) {
735			ina = ((struct sockaddr_in *)
736				&ifr->ifr_addr)->sin_addr.s_addr;
737			strncpy(ifreq.ifr_name, ifr->ifr_name,
738				sizeof(ifreq.ifr_name));
739			/*
740			 * Check that the interface is up,
741			 * and not point-to-point or loopback.
742			 */
743			if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
744				continue;
745			if ((ifreq.ifr_flags &
746			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
747					IFF_LOOPBACK|IFF_NOARP))
748			     != (IFF_UP|IFF_BROADCAST))
749				goto nextif;
750			/*
751			 * Get its netmask and check that it's on
752			 * the right subnet.
753			 */
754			if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
755				continue;
756			mask = ((struct sockaddr_in *)
757				&ifreq.ifr_addr)->sin_addr.s_addr;
758			if ((ipaddr & mask) != (ina & mask))
759				goto nextif;
760			break;
761		}
762nextif:
763		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
764		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
765	}
766
767	if (ifr >= ifend) {
768		close(sock);
769		return 0;
770	}
771
772	/*
773	* Now scan through again looking for a link-level address
774	* for this interface.
775	*/
776	ifp = ifr;
777	for (ifr = ifc.ifc_req; ifr < ifend; ) {
778		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
779		    && ifr->ifr_addr.sa_family == AF_LINK) {
780			/*
781			 * Found the link-level address - copy it out
782			 */
783		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
784			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
785			close (sock);
786			printf("using interface %s for proxy with address ",
787				ifp->ifr_name);
788			printf("%s\n", ether_ntoa(hwaddr));
789			return dla->sdl_alen;
790		}
791		ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
792		    + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
793	}
794	return 0;
795}
796