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