arp.c revision 44627
1130803Smarcel/*
2130803Smarcel * Copyright (c) 1984, 1993
3130803Smarcel *	The Regents of the University of California.  All rights reserved.
4130803Smarcel *
5130803Smarcel * This code is derived from software contributed to Berkeley by
6130803Smarcel * Sun Microsystems, Inc.
7130803Smarcel *
8130803Smarcel * Redistribution and use in source and binary forms, with or without
9130803Smarcel * modification, are permitted provided that the following conditions
10130803Smarcel * are met:
11130803Smarcel * 1. Redistributions of source code must retain the above copyright
12130803Smarcel *    notice, this list of conditions and the following disclaimer.
13130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
14130803Smarcel *    notice, this list of conditions and the following disclaimer in the
15130803Smarcel *    documentation and/or other materials provided with the distribution.
16130803Smarcel * 3. All advertising materials mentioning features or use of this software
17130803Smarcel *    must display the following acknowledgement:
18130803Smarcel *	This product includes software developed by the University of
19130803Smarcel *	California, Berkeley and its contributors.
20130803Smarcel * 4. Neither the name of the University nor the names of its contributors
21130803Smarcel *    may be used to endorse or promote products derived from this software
22130803Smarcel *    without specific prior written permission.
23130803Smarcel *
24130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25130803Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26130803Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28130803Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30130803Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31130803Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32130803Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33130803Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34130803Smarcel * SUCH DAMAGE.
35130803Smarcel */
36130803Smarcel
37130803Smarcel#ifndef lint
38130803Smarcelstatic char const copyright[] =
39130803Smarcel"@(#) Copyright (c) 1984, 1993\n\
40130803Smarcel	The Regents of the University of California.  All rights reserved.\n";
41130803Smarcel#endif /* not lint */
42130803Smarcel
43130803Smarcel#ifndef lint
44130803Smarcel#if 0
45130803Smarcelstatic char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
46130803Smarcel#endif
47130803Smarcelstatic const char rcsid[] =
48130803Smarcel	"$Id: arp.c,v 1.14 1998/01/16 17:38:51 bde Exp $";
49130803Smarcel#endif /* not lint */
50130803Smarcel
51130803Smarcel/*
52130803Smarcel * arp - display, set, and delete arp table entries
53130803Smarcel */
54130803Smarcel
55130803Smarcel
56130803Smarcel#include <sys/param.h>
57130803Smarcel#include <sys/file.h>
58130803Smarcel#include <sys/socket.h>
59130803Smarcel#include <sys/sockio.h>
60130803Smarcel#include <sys/sysctl.h>
61130803Smarcel#include <sys/ioctl.h>
62130803Smarcel#include <sys/time.h>
63130803Smarcel
64130803Smarcel#include <net/if.h>
65130803Smarcel#include <net/if_dl.h>
66130803Smarcel#include <net/if_types.h>
67130803Smarcel#include <net/route.h>
68130803Smarcel
69130803Smarcel#include <netinet/in.h>
70130803Smarcel#include <netinet/if_ether.h>
71130803Smarcel
72130803Smarcel#include <arpa/inet.h>
73130803Smarcel
74130803Smarcel#include <err.h>
75130803Smarcel#include <errno.h>
76130803Smarcel#include <netdb.h>
77130803Smarcel#include <nlist.h>
78130803Smarcel#include <paths.h>
79130803Smarcel#include <stdio.h>
80130803Smarcel#include <stdlib.h>
81130803Smarcel#include <strings.h>
82130803Smarcel#include <unistd.h>
83130803Smarcel
84130803Smarcelvoid search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
85130803Smarcel	struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
86130803Smarcelvoid print_entry(struct sockaddr_dl *sdl,
87130803Smarcel	struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
88130803Smarcelvoid nuke_entry(struct sockaddr_dl *sdl,
89130803Smarcel	struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
90130803Smarcelint delete(char *host, char *info);
91130803Smarcelvoid ether_print(u_char *cp);
92130803Smarcelvoid usage(void);
93130803Smarcelint set(int argc, char **argv);
94130803Smarcelint get(char *host);
95130803Smarcelint file(char *name);
96130803Smarcelvoid getsocket(void);
97130803Smarcelint my_ether_aton(char *a, u_char *n);
98130803Smarcelint rtmsg(int cmd);
99130803Smarcelint get_ether_addr(u_long ipaddr, u_char *hwaddr);
100130803Smarcel
101130803Smarcelstatic int pid;
102130803Smarcelstatic int nflag;	/* no reverse dns lookups */
103130803Smarcelstatic int aflag;	/* do it for all entries */
104130803Smarcelstatic int s = -1;
105130803Smarcel
106130803Smarcel/* which function we're supposed to do */
107130803Smarcel#define F_GET		1
108130803Smarcel#define F_SET		2
109130803Smarcel#define F_FILESET	3
110130803Smarcel#define F_REPLACE	4
111130803Smarcel#define F_DELETE	5
112130803Smarcel
113130803Smarcel#define SETFUNC(f)	{ if (func) usage(); func = (f); }
114130803Smarcel
115130803Smarcelint
116130803Smarcelmain(argc, argv)
117130803Smarcel	int argc;
118130803Smarcel	char **argv;
119130803Smarcel{
120130803Smarcel	int ch, func = 0;
121130803Smarcel	int rtn = 0;
122130803Smarcel
123130803Smarcel	pid = getpid();
124130803Smarcel	while ((ch = getopt(argc, argv, "andfsS")) != -1)
125130803Smarcel		switch((char)ch) {
126130803Smarcel		case 'a':
127130803Smarcel			aflag = 1;
128130803Smarcel			break;
129130803Smarcel		case 'd':
130130803Smarcel			SETFUNC(F_DELETE);
131130803Smarcel			break;
132130803Smarcel		case 'n':
133130803Smarcel			nflag = 1;
134130803Smarcel			break;
135130803Smarcel		case 'S':
136130803Smarcel			SETFUNC(F_REPLACE);
137130803Smarcel			break;
138130803Smarcel		case 's':
139130803Smarcel			SETFUNC(F_SET);
140130803Smarcel			break;
141130803Smarcel		case 'f' :
142130803Smarcel			SETFUNC(F_FILESET);
143130803Smarcel			break;
144130803Smarcel		case '?':
145130803Smarcel		default:
146130803Smarcel			usage();
147130803Smarcel		}
148130803Smarcel	argc -= optind;
149130803Smarcel	argv += optind;
150130803Smarcel
151130803Smarcel	if (!func)
152130803Smarcel		func = F_GET;
153130803Smarcel	switch (func) {
154130803Smarcel	case F_GET:
155130803Smarcel		if (aflag) {
156130803Smarcel			if (argc != 0)
157130803Smarcel				usage();
158130803Smarcel			search(0, print_entry);
159130803Smarcel		} else {
160130803Smarcel			if (argc != 1)
161130803Smarcel				usage();
162130803Smarcel			get(argv[0]);
163130803Smarcel		}
164130803Smarcel		break;
165130803Smarcel	case F_SET:
166130803Smarcel	case F_REPLACE:
167130803Smarcel		if (argc < 2 || argc > 5)
168130803Smarcel			usage();
169130803Smarcel		if (func == F_REPLACE)
170130803Smarcel			(void) delete(argv[0], NULL);
171130803Smarcel		rtn = set(argc, argv) ? 1 : 0;
172130803Smarcel		break;
173130803Smarcel	case F_DELETE:
174130803Smarcel		if (aflag) {
175130803Smarcel			if (argc != 0)
176130803Smarcel				usage();
177130803Smarcel			search(0, nuke_entry);
178130803Smarcel		} else {
179130803Smarcel			if (argc < 1 || argc > 2)
180130803Smarcel				usage();
181130803Smarcel			rtn = delete(argv[0], argv[1]);
182130803Smarcel		}
183130803Smarcel		break;
184130803Smarcel	case F_FILESET:
185130803Smarcel		if (argc != 1)
186130803Smarcel			usage();
187130803Smarcel		rtn = file(argv[0]);
188130803Smarcel		break;
189130803Smarcel	}
190130803Smarcel
191130803Smarcel	return(rtn);
192130803Smarcel}
193130803Smarcel
194130803Smarcel/*
195130803Smarcel * Process a file to set standard arp entries
196130803Smarcel */
197130803Smarcelint
198130803Smarcelfile(char *name)
199130803Smarcel{
200130803Smarcel	FILE *fp;
201130803Smarcel	int i, retval;
202130803Smarcel	char line[100], arg[5][50], *args[5];
203130803Smarcel
204130803Smarcel	if ((fp = fopen(name, "r")) == NULL)
205130803Smarcel		errx(1, "cannot open %s", name);
206130803Smarcel	args[0] = &arg[0][0];
207130803Smarcel	args[1] = &arg[1][0];
208130803Smarcel	args[2] = &arg[2][0];
209130803Smarcel	args[3] = &arg[3][0];
210130803Smarcel	args[4] = &arg[4][0];
211130803Smarcel	retval = 0;
212130803Smarcel	while(fgets(line, 100, fp) != NULL) {
213130803Smarcel		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
214130803Smarcel		    arg[3], arg[4]);
215130803Smarcel		if (i < 2) {
216130803Smarcel			warnx("bad line: %s", line);
217130803Smarcel			retval = 1;
218130803Smarcel			continue;
219130803Smarcel		}
220130803Smarcel		if (set(i, args))
221130803Smarcel			retval = 1;
222130803Smarcel	}
223130803Smarcel	fclose(fp);
224130803Smarcel	return (retval);
225130803Smarcel}
226130803Smarcel
227130803Smarcelvoid
228130803Smarcelgetsocket(void)
229130803Smarcel{
230130803Smarcel	if (s < 0) {
231130803Smarcel		s = socket(PF_ROUTE, SOCK_RAW, 0);
232130803Smarcel		if (s < 0)
233130803Smarcel			err(1, "socket");
234130803Smarcel	}
235130803Smarcel}
236130803Smarcel
237130803Smarcelstruct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
238130803Smarcelstruct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
239130803Smarcelstruct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
240130803Smarcelint	expire_time, flags, export_only, doing_proxy, found_entry;
241130803Smarcelstruct	{
242130803Smarcel	struct	rt_msghdr m_rtm;
243130803Smarcel	char	m_space[512];
244130803Smarcel}	m_rtmsg;
245130803Smarcel
246130803Smarcel/*
247130803Smarcel * Set an individual arp entry
248130803Smarcel */
249130803Smarcelint
250130803Smarcelset(int argc, char **argv)
251130803Smarcel{
252130803Smarcel	struct hostent *hp;
253130803Smarcel	register struct sockaddr_inarp *sin = &sin_m;
254130803Smarcel	register struct sockaddr_dl *sdl;
255130803Smarcel	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
256130803Smarcel	u_char *ea;
257130803Smarcel	char *host = argv[0], *eaddr = argv[1];
258130803Smarcel
259130803Smarcel	getsocket();
260130803Smarcel	argc -= 2;
261130803Smarcel	argv += 2;
262130803Smarcel	sdl_m = blank_sdl;
263130803Smarcel	sin_m = blank_sin;
264130803Smarcel	sin->sin_addr.s_addr = inet_addr(host);
265130803Smarcel	if (sin->sin_addr.s_addr == -1) {
266130803Smarcel		if (!(hp = gethostbyname(host))) {
267130803Smarcel			warnx("%s: %s", host, hstrerror(h_errno));
268130803Smarcel			return (1);
269130803Smarcel		}
270130803Smarcel		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
271130803Smarcel		    sizeof sin->sin_addr);
272130803Smarcel	}
273130803Smarcel	doing_proxy = flags = export_only = expire_time = 0;
274130803Smarcel	while (argc-- > 0) {
275130803Smarcel		if (strncmp(argv[0], "temp", 4) == 0) {
276130803Smarcel			struct timeval time;
277130803Smarcel			gettimeofday(&time, 0);
278130803Smarcel			expire_time = time.tv_sec + 20 * 60;
279130803Smarcel		}
280130803Smarcel		else if (strncmp(argv[0], "pub", 3) == 0) {
281130803Smarcel			flags |= RTF_ANNOUNCE;
282130803Smarcel			doing_proxy = SIN_PROXY;
283130803Smarcel		} else if (strncmp(argv[0], "trail", 5) == 0) {
284130803Smarcel			printf("%s: Sending trailers is no longer supported\n",
285130803Smarcel				host);
286130803Smarcel		}
287130803Smarcel		argv++;
288130803Smarcel	}
289130803Smarcel	ea = (u_char *)LLADDR(&sdl_m);
290130803Smarcel	if (doing_proxy && !strcmp(eaddr, "auto")) {
291130803Smarcel		if (!get_ether_addr(sin->sin_addr.s_addr, ea)) {
292130803Smarcel			return (1);
293130803Smarcel		}
294130803Smarcel		sdl_m.sdl_alen = 6;
295130803Smarcel	} else {
296130803Smarcel		if (my_ether_aton(eaddr, ea) == 0)
297130803Smarcel			sdl_m.sdl_alen = 6;
298130803Smarcel	}
299130803Smarceltryagain:
300130803Smarcel	if (rtmsg(RTM_GET) < 0) {
301130803Smarcel		warn("%s", host);
302130803Smarcel		return (1);
303130803Smarcel	}
304130803Smarcel	sin = (struct sockaddr_inarp *)(rtm + 1);
305	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
306	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
307		if (sdl->sdl_family == AF_LINK &&
308		    (rtm->rtm_flags & RTF_LLINFO) &&
309		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
310		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
311		case IFT_ISO88024: case IFT_ISO88025:
312			goto overwrite;
313		}
314		if (doing_proxy == 0) {
315			printf("set: can only proxy for %s\n", host);
316			return (1);
317		}
318		if (sin_m.sin_other & SIN_PROXY) {
319			printf("set: proxy entry exists for non 802 device\n");
320			return(1);
321		}
322		sin_m.sin_other = SIN_PROXY;
323		export_only = 1;
324		goto tryagain;
325	}
326overwrite:
327	if (sdl->sdl_family != AF_LINK) {
328		printf("cannot intuit interface index and type for %s\n", host);
329		return (1);
330	}
331	sdl_m.sdl_type = sdl->sdl_type;
332	sdl_m.sdl_index = sdl->sdl_index;
333	return (rtmsg(RTM_ADD));
334}
335
336/*
337 * Display an individual arp entry
338 */
339int
340get(char *host)
341{
342	struct hostent *hp;
343	struct sockaddr_inarp *sin = &sin_m;
344
345	sin_m = blank_sin;
346	sin->sin_addr.s_addr = inet_addr(host);
347	if (sin->sin_addr.s_addr == -1) {
348		if (!(hp = gethostbyname(host)))
349			errx(1, "%s: %s", host, hstrerror(h_errno));
350		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
351		    sizeof sin->sin_addr);
352	}
353	search(sin->sin_addr.s_addr, print_entry);
354	if (found_entry == 0) {
355		printf("%s (%s) -- no entry\n",
356		    host, inet_ntoa(sin->sin_addr));
357		return(1);
358	}
359	return(0);
360}
361
362/*
363 * Delete an arp entry
364 */
365int
366delete(char *host, char *info)
367{
368	struct hostent *hp;
369	register struct sockaddr_inarp *sin = &sin_m;
370	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
371	struct sockaddr_dl *sdl;
372
373	if (info && strncmp(info, "pro", 3) )
374		export_only = 1;
375	getsocket();
376	sin_m = blank_sin;
377	sin->sin_addr.s_addr = inet_addr(host);
378	if (sin->sin_addr.s_addr == -1) {
379		if (!(hp = gethostbyname(host))) {
380			warnx("%s: %s", host, hstrerror(h_errno));
381			return (1);
382		}
383		bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
384		    sizeof sin->sin_addr);
385	}
386tryagain:
387	if (rtmsg(RTM_GET) < 0) {
388		warn("%s", host);
389		return (1);
390	}
391	sin = (struct sockaddr_inarp *)(rtm + 1);
392	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
393	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
394		if (sdl->sdl_family == AF_LINK &&
395		    (rtm->rtm_flags & RTF_LLINFO) &&
396		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
397		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
398		case IFT_ISO88024: case IFT_ISO88025:
399			goto delete;
400		}
401	}
402	if (sin_m.sin_other & SIN_PROXY) {
403		fprintf(stderr, "delete: can't locate %s\n",host);
404		return (1);
405	} else {
406		sin_m.sin_other = SIN_PROXY;
407		goto tryagain;
408	}
409delete:
410	if (sdl->sdl_family != AF_LINK) {
411		printf("cannot locate %s\n", host);
412		return (1);
413	}
414	if (rtmsg(RTM_DELETE) == 0) {
415		printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
416		return (0);
417	}
418	return (1);
419}
420
421/*
422 * Search the arp table and do some action on matching entries
423 */
424void
425search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
426	struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
427{
428	int mib[6];
429	size_t needed;
430	char *lim, *buf, *next;
431	struct rt_msghdr *rtm;
432	struct sockaddr_inarp *sin;
433	struct sockaddr_dl *sdl;
434	extern int h_errno;
435
436	mib[0] = CTL_NET;
437	mib[1] = PF_ROUTE;
438	mib[2] = 0;
439	mib[3] = AF_INET;
440	mib[4] = NET_RT_FLAGS;
441	mib[5] = RTF_LLINFO;
442	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
443		errx(1, "route-sysctl-estimate");
444	if ((buf = malloc(needed)) == NULL)
445		errx(1, "malloc");
446	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
447		errx(1, "actual retrieval of routing table");
448	lim = buf + needed;
449	for (next = buf; next < lim; next += rtm->rtm_msglen) {
450		rtm = (struct rt_msghdr *)next;
451		sin = (struct sockaddr_inarp *)(rtm + 1);
452		sdl = (struct sockaddr_dl *)(sin + 1);
453		if (addr) {
454			if (addr != sin->sin_addr.s_addr)
455				continue;
456			found_entry = 1;
457		}
458		(*action)(sdl, sin, rtm);
459	}
460}
461
462/*
463 * Display an arp entry
464 */
465void
466print_entry(struct sockaddr_dl *sdl,
467	struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
468{
469	char *host;
470	extern int h_errno;
471	struct hostent *hp;
472	int seg;
473
474	if (nflag == 0)
475		hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
476		    sizeof sin->sin_addr, AF_INET);
477	else
478		hp = 0;
479	if (hp)
480		host = hp->h_name;
481	else {
482		host = "?";
483		if (h_errno == TRY_AGAIN)
484			nflag = 1;
485	}
486	printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
487	if (sdl->sdl_alen)
488		ether_print(LLADDR(sdl));
489	else
490		printf("(incomplete)");
491	if (rtm->rtm_rmx.rmx_expire == 0)
492		printf(" permanent");
493	if (sin->sin_other & SIN_PROXY)
494		printf(" published (proxy only)");
495	if (rtm->rtm_addrs & RTA_NETMASK) {
496		sin = (struct sockaddr_inarp *)
497			(sdl->sdl_len + (char *)sdl);
498		if (sin->sin_addr.s_addr == 0xffffffff)
499			printf(" published");
500		if (sin->sin_len != 8)
501			printf("(wierd)");
502	}
503        switch(sdl->sdl_type) {
504            case IFT_ETHER:
505                printf(" [ethernet]");
506                break;
507            case IFT_ISO88025:
508                printf(" [token-ring]");
509                break;
510            default:
511        }
512	if (sdl->sdl_rcf != NULL) {
513		printf(" rt=%x", ntohs(sdl->sdl_rcf));
514		for (seg = 0; seg < ((((ntohs(sdl->sdl_rcf) & 0x1f00) >> 8) - 2 ) / 2); seg++)
515			printf(":%x", ntohs(sdl->sdl_route[seg]));
516	}
517
518	printf("\n");
519
520}
521
522/*
523 * Nuke an arp entry
524 */
525void
526nuke_entry(struct sockaddr_dl *sdl,
527	struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
528{
529	char ip[20];
530
531	snprintf(ip, sizeof(ip), "%s", inet_ntoa(sin->sin_addr));
532	delete(ip, NULL);
533}
534
535void
536ether_print(u_char *cp)
537{
538	printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
539}
540
541int
542my_ether_aton(char *a, u_char *n)
543{
544	int i, o[6];
545
546	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
547					   &o[3], &o[4], &o[5]);
548	if (i != 6) {
549		warnx("invalid Ethernet address '%s'", a);
550		return (1);
551	}
552	for (i=0; i<6; i++)
553		n[i] = o[i];
554	return (0);
555}
556
557void
558usage(void)
559{
560	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
561		"usage: arp [-n] hostname",
562		"       arp [-n] -a",
563		"       arp -d hostname [proxy]",
564		"       arp -d -a",
565		"       arp -s hostname ether_addr [temp] [pub]",
566		"       arp -S hostname ether_addr [temp] [pub]",
567		"       arp -f filename");
568	exit(1);
569}
570
571int
572rtmsg(int cmd)
573{
574	static int seq;
575	int rlen;
576	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
577	register char *cp = m_rtmsg.m_space;
578	register int l;
579
580	errno = 0;
581	if (cmd == RTM_DELETE)
582		goto doit;
583	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
584	rtm->rtm_flags = flags;
585	rtm->rtm_version = RTM_VERSION;
586
587	switch (cmd) {
588	default:
589		errx(1, "internal wrong cmd");
590	case RTM_ADD:
591		rtm->rtm_addrs |= RTA_GATEWAY;
592		rtm->rtm_rmx.rmx_expire = expire_time;
593		rtm->rtm_inits = RTV_EXPIRE;
594		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
595		sin_m.sin_other = 0;
596		if (doing_proxy) {
597			if (export_only)
598				sin_m.sin_other = SIN_PROXY;
599			else {
600				rtm->rtm_addrs |= RTA_NETMASK;
601				rtm->rtm_flags &= ~RTF_HOST;
602			}
603		}
604		/* FALLTHROUGH */
605	case RTM_GET:
606		rtm->rtm_addrs |= RTA_DST;
607	}
608#define NEXTADDR(w, s) \
609	if (rtm->rtm_addrs & (w)) { \
610		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
611
612	NEXTADDR(RTA_DST, sin_m);
613	NEXTADDR(RTA_GATEWAY, sdl_m);
614	NEXTADDR(RTA_NETMASK, so_mask);
615
616	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
617doit:
618	l = rtm->rtm_msglen;
619	rtm->rtm_seq = ++seq;
620	rtm->rtm_type = cmd;
621	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
622		if (errno != ESRCH || cmd != RTM_DELETE) {
623			warn("writing to routing socket");
624			return (-1);
625		}
626	}
627	do {
628		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
629	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
630	if (l < 0)
631		warn("read from routing socket");
632	return (0);
633}
634
635/*
636 * get_ether_addr - get the hardware address of an interface on the
637 * the same subnet as ipaddr.
638 */
639#define MAX_IFS		32
640
641int
642get_ether_addr(u_long ipaddr, u_char *hwaddr)
643{
644	struct ifreq *ifr, *ifend, *ifp;
645	u_long ina, mask;
646	struct sockaddr_dl *dla;
647	struct ifreq ifreq;
648	struct ifconf ifc;
649	struct ifreq ifs[MAX_IFS];
650	int s;
651
652	s = socket(AF_INET, SOCK_DGRAM, 0);
653	if (s < 0)
654		err(1, "socket");
655
656	ifc.ifc_len = sizeof(ifs);
657	ifc.ifc_req = ifs;
658	if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
659		warnx("ioctl(SIOCGIFCONF)");
660		close(s);
661		return 0;
662	}
663
664	/*
665	* Scan through looking for an interface with an Internet
666	* address on the same subnet as `ipaddr'.
667	*/
668	ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
669	for (ifr = ifc.ifc_req; ifr < ifend; ) {
670		if (ifr->ifr_addr.sa_family == AF_INET) {
671			ina = ((struct sockaddr_in *)
672				&ifr->ifr_addr)->sin_addr.s_addr;
673			strncpy(ifreq.ifr_name, ifr->ifr_name,
674				sizeof(ifreq.ifr_name));
675			/*
676			 * Check that the interface is up,
677			 * and not point-to-point or loopback.
678			 */
679			if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
680				continue;
681			if ((ifreq.ifr_flags &
682			     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
683					IFF_LOOPBACK|IFF_NOARP))
684			     != (IFF_UP|IFF_BROADCAST))
685				goto nextif;
686			/*
687			 * Get its netmask and check that it's on
688			 * the right subnet.
689			 */
690			if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
691				continue;
692			mask = ((struct sockaddr_in *)
693				&ifreq.ifr_addr)->sin_addr.s_addr;
694			if ((ipaddr & mask) != (ina & mask))
695				goto nextif;
696			break;
697		}
698nextif:
699		ifr = (struct ifreq *)
700		    ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
701	}
702
703	if (ifr >= ifend) {
704		close(s);
705		return 0;
706	}
707
708	/*
709	* Now scan through again looking for a link-level address
710	* for this interface.
711	*/
712	ifp = ifr;
713	for (ifr = ifc.ifc_req; ifr < ifend; ) {
714		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
715		    && ifr->ifr_addr.sa_family == AF_LINK) {
716			/*
717			 * Found the link-level address - copy it out
718			 */
719		 	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
720			memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
721			close (s);
722			printf("using interface %s for proxy with address ",
723				ifp->ifr_name);
724			ether_print(hwaddr);
725			printf("\n");
726			return dla->sdl_alen;
727		}
728		ifr = (struct ifreq *)
729			((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
730	}
731	return 0;
732}
733