11553Srgrimes/*
21553Srgrimes * Copyright (c) 1984, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * This code is derived from software contributed to Berkeley by
61553Srgrimes * Sun Microsystems, Inc.
71553Srgrimes *
81553Srgrimes * Redistribution and use in source and binary forms, with or without
91553Srgrimes * modification, are permitted provided that the following conditions
101553Srgrimes * are met:
111553Srgrimes * 1. Redistributions of source code must retain the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer.
131553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer in the
151553Srgrimes *    documentation and/or other materials provided with the distribution.
161553Srgrimes * 4. Neither the name of the University nor the names of its contributors
171553Srgrimes *    may be used to endorse or promote products derived from this software
181553Srgrimes *    without specific prior written permission.
191553Srgrimes *
201553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301553Srgrimes * SUCH DAMAGE.
311553Srgrimes */
321553Srgrimes
33114601Sobrien#if 0
341553Srgrimes#ifndef lint
3513977Sphkstatic char const copyright[] =
361553Srgrimes"@(#) Copyright (c) 1984, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
4113977Sphkstatic char const sccsid[] = "@(#)from: arp.c	8.2 (Berkeley) 1/2/94";
42114601Sobrien#endif /* not lint */
4329060Scharnier#endif
44114601Sobrien#include <sys/cdefs.h>
45114601Sobrien__FBSDID("$FreeBSD: releng/10.3/usr.sbin/arp/arp.c 294203 2016-01-17 06:02:59Z melifaro $");
461553Srgrimes
471553Srgrimes/*
481553Srgrimes * arp - display, set, and delete arp table entries
491553Srgrimes */
501553Srgrimes
511553Srgrimes
521553Srgrimes#include <sys/param.h>
531553Srgrimes#include <sys/file.h>
541553Srgrimes#include <sys/socket.h>
5513977Sphk#include <sys/sockio.h>
561553Srgrimes#include <sys/sysctl.h>
5713977Sphk#include <sys/ioctl.h>
5820287Swollman#include <sys/time.h>
591553Srgrimes
601553Srgrimes#include <net/if.h>
611553Srgrimes#include <net/if_dl.h>
621553Srgrimes#include <net/if_types.h>
6396235Swpaul#include <net/route.h>
6496202Skbyanc#include <net/iso88025.h>
651553Srgrimes
661553Srgrimes#include <netinet/in.h>
671553Srgrimes#include <netinet/if_ether.h>
681553Srgrimes
691553Srgrimes#include <arpa/inet.h>
701553Srgrimes
71108314Sru#include <ctype.h>
7229060Scharnier#include <err.h>
7329060Scharnier#include <errno.h>
741553Srgrimes#include <netdb.h>
751553Srgrimes#include <nlist.h>
7629060Scharnier#include <paths.h>
771553Srgrimes#include <stdio.h>
7813977Sphk#include <stdlib.h>
7993590Smike#include <string.h>
8029060Scharnier#include <strings.h>
8113977Sphk#include <unistd.h>
821553Srgrimes
83128181Sluigitypedef void (action_fn)(struct sockaddr_dl *sdl,
84246143Sglebius	struct sockaddr_in *s_in, struct rt_msghdr *rtm);
8513977Sphk
86128181Sluigistatic int search(u_long addr, action_fn *action);
87128181Sluigistatic action_fn print_entry;
88128181Sluigistatic action_fn nuke_entry;
89128181Sluigi
90246143Sglebiusstatic int delete(char *host);
91128181Sluigistatic void usage(void);
92128181Sluigistatic int set(int argc, char **argv);
93147172Srustatic int get(char *host);
94128181Sluigistatic int file(char *name);
95128192Sluigistatic struct rt_msghdr *rtmsg(int cmd,
96246143Sglebius    struct sockaddr_in *dst, struct sockaddr_dl *sdl);
97147172Srustatic int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr);
98246143Sglebiusstatic struct sockaddr_in *getaddr(char *host);
99147172Srustatic int valid_type(int type);
100128181Sluigi
10131145Sjulianstatic int nflag;	/* no reverse dns lookups */
102117729Syarstatic char *rifname;
1031553Srgrimes
104203919Srustatic time_t	expire_time;
105246143Sglebiusstatic int	flags, doing_proxy;
106128192Sluigi
107264688Sgnnstruct if_nameindex *ifnameindex;
108264688Sgnn
10931145Sjulian/* which function we're supposed to do */
11031145Sjulian#define F_GET		1
11131145Sjulian#define F_SET		2
11231145Sjulian#define F_FILESET	3
11331145Sjulian#define F_REPLACE	4
11431145Sjulian#define F_DELETE	5
11531145Sjulian
11631145Sjulian#define SETFUNC(f)	{ if (func) usage(); func = (f); }
11731145Sjulian
11813977Sphkint
11977870Srumain(int argc, char *argv[])
1201553Srgrimes{
12131145Sjulian	int ch, func = 0;
12231145Sjulian	int rtn = 0;
123128181Sluigi	int aflag = 0;	/* do it for all entries */
1241553Srgrimes
125117729Syar	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
126196216Sremko		switch(ch) {
1271553Srgrimes		case 'a':
12831145Sjulian			aflag = 1;
12931145Sjulian			break;
1301553Srgrimes		case 'd':
13131145Sjulian			SETFUNC(F_DELETE);
13231145Sjulian			break;
1331553Srgrimes		case 'n':
1341553Srgrimes			nflag = 1;
13531145Sjulian			break;
13613977Sphk		case 'S':
13731145Sjulian			SETFUNC(F_REPLACE);
13831145Sjulian			break;
1391553Srgrimes		case 's':
14031145Sjulian			SETFUNC(F_SET);
14131145Sjulian			break;
1429874Sjkh		case 'f' :
14331145Sjulian			SETFUNC(F_FILESET);
14431145Sjulian			break;
145117729Syar		case 'i':
146117729Syar			rifname = optarg;
147117729Syar			break;
1481553Srgrimes		case '?':
1491553Srgrimes		default:
1501553Srgrimes			usage();
1511553Srgrimes		}
15231145Sjulian	argc -= optind;
15331145Sjulian	argv += optind;
15431145Sjulian
15531145Sjulian	if (!func)
15631145Sjulian		func = F_GET;
157117729Syar	if (rifname) {
158154162Sbrooks		if (func != F_GET && !(func == F_DELETE && aflag))
159117729Syar			errx(1, "-i not applicable to this operation");
160117729Syar		if (if_nametoindex(rifname) == 0) {
161117729Syar			if (errno == ENXIO)
162117729Syar				errx(1, "interface %s does not exist", rifname);
163117729Syar			else
164117729Syar				err(1, "if_nametoindex(%s)", rifname);
165117729Syar		}
166117729Syar	}
16731145Sjulian	switch (func) {
16831145Sjulian	case F_GET:
16931145Sjulian		if (aflag) {
17031145Sjulian			if (argc != 0)
17131145Sjulian				usage();
17231145Sjulian			search(0, print_entry);
17331145Sjulian		} else {
17431145Sjulian			if (argc != 1)
17531145Sjulian				usage();
176147172Sru			rtn = get(argv[0]);
17731145Sjulian		}
17831145Sjulian		break;
17931145Sjulian	case F_SET:
18031145Sjulian	case F_REPLACE:
18177870Sru		if (argc < 2 || argc > 6)
18231145Sjulian			usage();
18331145Sjulian		if (func == F_REPLACE)
184246143Sglebius			(void)delete(argv[0]);
18531145Sjulian		rtn = set(argc, argv) ? 1 : 0;
18631145Sjulian		break;
18731145Sjulian	case F_DELETE:
18831145Sjulian		if (aflag) {
18931145Sjulian			if (argc != 0)
19031145Sjulian				usage();
19131145Sjulian			search(0, nuke_entry);
192255821Sglebius		} else {
193255821Sglebius			if (argc != 1)
194255821Sglebius				usage();
195246143Sglebius			rtn = delete(argv[0]);
196255821Sglebius		}
19731145Sjulian		break;
19831145Sjulian	case F_FILESET:
19931145Sjulian		if (argc != 1)
20031145Sjulian			usage();
20131145Sjulian		rtn = file(argv[0]);
20231145Sjulian		break;
20331145Sjulian	}
20431145Sjulian
205264688Sgnn	if (ifnameindex != NULL)
206264688Sgnn		if_freenameindex(ifnameindex);
207264688Sgnn
208147172Sru	return (rtn);
2091553Srgrimes}
2101553Srgrimes
2111553Srgrimes/*
2121553Srgrimes * Process a file to set standard arp entries
2131553Srgrimes */
214128181Sluigistatic int
21513977Sphkfile(char *name)
2161553Srgrimes{
2171553Srgrimes	FILE *fp;
2181553Srgrimes	int i, retval;
219109413Sru	char line[100], arg[5][50], *args[5], *p;
2201553Srgrimes
22129060Scharnier	if ((fp = fopen(name, "r")) == NULL)
222128054Smux		err(1, "cannot open %s", name);
2231553Srgrimes	args[0] = &arg[0][0];
2241553Srgrimes	args[1] = &arg[1][0];
2251553Srgrimes	args[2] = &arg[2][0];
2261553Srgrimes	args[3] = &arg[3][0];
2271553Srgrimes	args[4] = &arg[4][0];
2281553Srgrimes	retval = 0;
229167260Skevlo	while(fgets(line, sizeof(line), fp) != NULL) {
230109413Sru		if ((p = strchr(line, '#')) != NULL)
231109413Sru			*p = '\0';
232109413Sru		for (p = line; isblank(*p); p++);
233111910Sru		if (*p == '\n' || *p == '\0')
234108314Sru			continue;
235109413Sru		i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
23656134Sjkh		    arg[2], arg[3], arg[4]);
2371553Srgrimes		if (i < 2) {
23829060Scharnier			warnx("bad line: %s", line);
2391553Srgrimes			retval = 1;
2401553Srgrimes			continue;
2411553Srgrimes		}
2421553Srgrimes		if (set(i, args))
2431553Srgrimes			retval = 1;
2441553Srgrimes	}
2451553Srgrimes	fclose(fp);
2461553Srgrimes	return (retval);
2471553Srgrimes}
2481553Srgrimes
2491553Srgrimes/*
250246143Sglebius * Given a hostname, fills up a (static) struct sockaddr_in with
251128192Sluigi * the address of the host and returns a pointer to the
252128192Sluigi * structure.
253128192Sluigi */
254246143Sglebiusstatic struct sockaddr_in *
255128192Sluigigetaddr(char *host)
256128192Sluigi{
257128192Sluigi	struct hostent *hp;
258246143Sglebius	static struct sockaddr_in reply;
259128192Sluigi
260128192Sluigi	bzero(&reply, sizeof(reply));
261128192Sluigi	reply.sin_len = sizeof(reply);
262128192Sluigi	reply.sin_family = AF_INET;
263128192Sluigi	reply.sin_addr.s_addr = inet_addr(host);
264128192Sluigi	if (reply.sin_addr.s_addr == INADDR_NONE) {
265128192Sluigi		if (!(hp = gethostbyname(host))) {
266128192Sluigi			warnx("%s: %s", host, hstrerror(h_errno));
267147172Sru			return (NULL);
268128192Sluigi		}
269128192Sluigi		bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
270128192Sluigi			sizeof reply.sin_addr);
271128192Sluigi	}
272147172Sru	return (&reply);
273128192Sluigi}
274128192Sluigi
275128192Sluigi/*
276147172Sru * Returns true if the type is a valid one for ARP.
277128192Sluigi */
278128192Sluigistatic int
279128192Sluigivalid_type(int type)
280128192Sluigi{
281147172Sru
282128192Sluigi	switch (type) {
283128192Sluigi	case IFT_ETHER:
284128192Sluigi	case IFT_FDDI:
285281471Smarkj	case IFT_INFINIBAND:
286128192Sluigi	case IFT_ISO88023:
287128192Sluigi	case IFT_ISO88024:
288128192Sluigi	case IFT_ISO88025:
289128192Sluigi	case IFT_L2VLAN:
290151989Sthompsa	case IFT_BRIDGE:
291147172Sru		return (1);
292147172Sru	default:
293147172Sru		return (0);
294128192Sluigi	}
295128192Sluigi}
296128192Sluigi
297128192Sluigi/*
2988857Srgrimes * Set an individual arp entry
2991553Srgrimes */
300128192Sluigistatic int
30113977Sphkset(int argc, char **argv)
3021553Srgrimes{
303246143Sglebius	struct sockaddr_in *addr;
304246143Sglebius	struct sockaddr_in *dst;	/* what are we looking for */
305128054Smux	struct sockaddr_dl *sdl;
306128181Sluigi	struct rt_msghdr *rtm;
30793952Sru	struct ether_addr *ea;
3081553Srgrimes	char *host = argv[0], *eaddr = argv[1];
309147172Sru	struct sockaddr_dl sdl_m;
3101553Srgrimes
3111553Srgrimes	argc -= 2;
3121553Srgrimes	argv += 2;
313128181Sluigi
314128181Sluigi	bzero(&sdl_m, sizeof(sdl_m));
315128181Sluigi	sdl_m.sdl_len = sizeof(sdl_m);
316128181Sluigi	sdl_m.sdl_family = AF_LINK;
317128181Sluigi
318128192Sluigi	dst = getaddr(host);
319128192Sluigi	if (dst == NULL)
320128192Sluigi		return (1);
321246143Sglebius	doing_proxy = flags = expire_time = 0;
3221553Srgrimes	while (argc-- > 0) {
3231553Srgrimes		if (strncmp(argv[0], "temp", 4) == 0) {
324216076Sglebius			struct timespec tp;
325216078Sglebius			int max_age;
326216078Sglebius			size_t len = sizeof(max_age);
327216078Sglebius
328216076Sglebius			clock_gettime(CLOCK_MONOTONIC, &tp);
329216078Sglebius			if (sysctlbyname("net.link.ether.inet.max_age",
330216078Sglebius			    &max_age, &len, NULL, 0) != 0)
331216078Sglebius				err(1, "sysctlbyname");
332216078Sglebius			expire_time = tp.tv_sec + max_age;
333177362Ssam		} else if (strncmp(argv[0], "pub", 3) == 0) {
3341553Srgrimes			flags |= RTF_ANNOUNCE;
33577870Sru			doing_proxy = 1;
33677870Sru			if (argc && strncmp(argv[1], "only", 3) == 0) {
337246143Sglebius				/*
338246143Sglebius				 * Compatibility: in pre FreeBSD 8 times
339246143Sglebius				 * the "only" keyword used to mean that
340246143Sglebius				 * an ARP entry should be announced, but
341246143Sglebius				 * not installed into routing table.
342246143Sglebius				 */
34377870Sru				argc--; argv++;
34477870Sru			}
345177362Ssam		} else if (strncmp(argv[0], "blackhole", 9) == 0) {
346186485Strhodes			if (flags & RTF_REJECT) {
347186485Strhodes				printf("Choose one of blackhole or reject, not both.\n");
348186485Strhodes			}
349177362Ssam			flags |= RTF_BLACKHOLE;
350177362Ssam		} else if (strncmp(argv[0], "reject", 6) == 0) {
351186485Strhodes			if (flags & RTF_BLACKHOLE) {
352186485Strhodes				printf("Choose one of blackhole or reject, not both.\n");
353186485Strhodes			}
354177362Ssam			flags |= RTF_REJECT;
3551553Srgrimes		} else if (strncmp(argv[0], "trail", 5) == 0) {
356128192Sluigi			/* XXX deprecated and undocumented feature */
3571553Srgrimes			printf("%s: Sending trailers is no longer supported\n",
3581553Srgrimes				host);
3591553Srgrimes		}
3601553Srgrimes		argv++;
3611553Srgrimes	}
36293952Sru	ea = (struct ether_addr *)LLADDR(&sdl_m);
36313977Sphk	if (doing_proxy && !strcmp(eaddr, "auto")) {
364128192Sluigi		if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
36573135Sru			printf("no interface found for %s\n",
366128192Sluigi			       inet_ntoa(dst->sin_addr));
36713977Sphk			return (1);
36813977Sphk		}
36993952Sru		sdl_m.sdl_alen = ETHER_ADDR_LEN;
37013977Sphk	} else {
371128192Sluigi		struct ether_addr *ea1 = ether_aton(eaddr);
372128192Sluigi
373155471Sglebius		if (ea1 == NULL) {
374128192Sluigi			warnx("invalid Ethernet address '%s'", eaddr);
375155471Sglebius			return (1);
376155471Sglebius		} else {
377128192Sluigi			*ea = *ea1;
37893952Sru			sdl_m.sdl_alen = ETHER_ADDR_LEN;
379128192Sluigi		}
38013977Sphk	}
381201282Sqingli
382201282Sqingli	/*
383201282Sqingli	 * In the case a proxy-arp entry is being added for
384201282Sqingli	 * a remote end point, the RTF_ANNOUNCE flag in the
385201282Sqingli	 * RTM_GET command is an indication to the kernel
386201282Sqingli	 * routing code that the interface associated with
387201282Sqingli	 * the prefix route covering the local end of the
388201282Sqingli	 * PPP link should be returned, on which ARP applies.
389201282Sqingli	 */
390201282Sqingli	rtm = rtmsg(RTM_GET, dst, &sdl_m);
391201282Sqingli	if (rtm == NULL) {
392201282Sqingli		warn("%s", host);
393201282Sqingli		return (1);
3941553Srgrimes	}
395246143Sglebius	addr = (struct sockaddr_in *)(rtm + 1);
396201282Sqingli	sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
397128192Sluigi
398201282Sqingli	if ((sdl->sdl_family != AF_LINK) ||
399201282Sqingli	    (rtm->rtm_flags & RTF_GATEWAY) ||
400201282Sqingli	    !valid_type(sdl->sdl_type)) {
4011553Srgrimes		printf("cannot intuit interface index and type for %s\n", host);
4021553Srgrimes		return (1);
4031553Srgrimes	}
4041553Srgrimes	sdl_m.sdl_type = sdl->sdl_type;
4051553Srgrimes	sdl_m.sdl_index = sdl->sdl_index;
406147170Sglebius	return (rtmsg(RTM_ADD, dst, &sdl_m) == NULL);
4071553Srgrimes}
4081553Srgrimes
4091553Srgrimes/*
4101553Srgrimes * Display an individual arp entry
4111553Srgrimes */
412147172Srustatic int
41313977Sphkget(char *host)
4141553Srgrimes{
415246143Sglebius	struct sockaddr_in *addr;
4161553Srgrimes
417128192Sluigi	addr = getaddr(host);
418128192Sluigi	if (addr == NULL)
419147172Sru		return (1);
420128181Sluigi	if (0 == search(addr->sin_addr.s_addr, print_entry)) {
421117729Syar		printf("%s (%s) -- no entry",
42287598Smikeh		    host, inet_ntoa(addr->sin_addr));
423117729Syar		if (rifname)
424117729Syar			printf(" on %s", rifname);
425117729Syar		printf("\n");
426147172Sru		return (1);
4271553Srgrimes	}
428147172Sru	return (0);
4291553Srgrimes}
4301553Srgrimes
4311553Srgrimes/*
4328857Srgrimes * Delete an arp entry
4331553Srgrimes */
434128181Sluigistatic int
435246143Sglebiusdelete(char *host)
4361553Srgrimes{
437246143Sglebius	struct sockaddr_in *addr, *dst;
438128181Sluigi	struct rt_msghdr *rtm;
4391553Srgrimes	struct sockaddr_dl *sdl;
440186119Sqingli	struct sockaddr_dl sdl_m;
4411553Srgrimes
442128192Sluigi	dst = getaddr(host);
443128192Sluigi	if (dst == NULL)
444147172Sru		return (1);
445186119Sqingli
446186119Sqingli	/*
447201282Sqingli	 * Perform a regular entry delete first.
448201282Sqingli	 */
449201282Sqingli	flags &= ~RTF_ANNOUNCE;
450201282Sqingli
451201282Sqingli	/*
452186119Sqingli	 * setup the data structure to notify the kernel
453186119Sqingli	 * it is the ARP entry the RTM_GET is interested
454186119Sqingli	 * in
455186119Sqingli	 */
456186119Sqingli	bzero(&sdl_m, sizeof(sdl_m));
457186119Sqingli	sdl_m.sdl_len = sizeof(sdl_m);
458186119Sqingli	sdl_m.sdl_family = AF_LINK;
459186119Sqingli
460128192Sluigi	for (;;) {	/* try twice */
461186119Sqingli		rtm = rtmsg(RTM_GET, dst, &sdl_m);
462128192Sluigi		if (rtm == NULL) {
463128192Sluigi			warn("%s", host);
4641553Srgrimes			return (1);
4651553Srgrimes		}
466246143Sglebius		addr = (struct sockaddr_in *)(rtm + 1);
467128192Sluigi		sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
468186119Sqingli
469186119Sqingli		/*
470186119Sqingli		 * With the new L2/L3 restructure, the route
471186119Sqingli		 * returned is a prefix route. The important
472186119Sqingli		 * piece of information from the previous
473186119Sqingli		 * RTM_GET is the interface index. In the
474186119Sqingli		 * case of ECMP, the kernel will traverse
475186119Sqingli		 * the route group for the given entry.
476186119Sqingli		 */
477186119Sqingli		if (sdl->sdl_family == AF_LINK &&
478128192Sluigi		    !(rtm->rtm_flags & RTF_GATEWAY) &&
479186119Sqingli		    valid_type(sdl->sdl_type) ) {
480186119Sqingli			addr->sin_addr.s_addr = dst->sin_addr.s_addr;
481186119Sqingli			break;
482186119Sqingli		}
483186119Sqingli
484201282Sqingli		/*
485201282Sqingli		 * Regualar entry delete failed, now check if there
486201282Sqingli		 * is a proxy-arp entry to remove.
487201282Sqingli		 */
488201282Sqingli		if (flags & RTF_ANNOUNCE) {
489128192Sluigi			fprintf(stderr, "delete: cannot locate %s\n",host);
490128192Sluigi			return (1);
491128192Sluigi		}
492201282Sqingli
493201282Sqingli		flags |= RTF_ANNOUNCE;
4941553Srgrimes	}
495186500Sqingli	rtm->rtm_flags |= RTF_LLDATA;
496128192Sluigi	if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
49787598Smikeh		printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
49813977Sphk		return (0);
49913977Sphk	}
50013977Sphk	return (1);
5011553Srgrimes}
5021553Srgrimes
503201282Sqingli
5041553Srgrimes/*
50531145Sjulian * Search the arp table and do some action on matching entries
5061553Srgrimes */
507128181Sluigistatic int
508128181Sluigisearch(u_long addr, action_fn *action)
5091553Srgrimes{
5101553Srgrimes	int mib[6];
5111553Srgrimes	size_t needed;
512201202Sjhb	char *lim, *buf, *next;
5131553Srgrimes	struct rt_msghdr *rtm;
514246143Sglebius	struct sockaddr_in *sin2;
5151553Srgrimes	struct sockaddr_dl *sdl;
516117729Syar	char ifname[IF_NAMESIZE];
517140735Smaxim	int st, found_entry = 0;
5181553Srgrimes
5191553Srgrimes	mib[0] = CTL_NET;
5201553Srgrimes	mib[1] = PF_ROUTE;
5211553Srgrimes	mib[2] = 0;
5221553Srgrimes	mib[3] = AF_INET;
5231553Srgrimes	mib[4] = NET_RT_FLAGS;
524186119Sqingli#ifdef RTF_LLINFO
5251553Srgrimes	mib[5] = RTF_LLINFO;
526186119Sqingli#else
527186119Sqingli	mib[5] = 0;
528186119Sqingli#endif
5291553Srgrimes	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
530128054Smux		err(1, "route-sysctl-estimate");
531128192Sluigi	if (needed == 0)	/* empty table */
532128181Sluigi		return 0;
533140735Smaxim	buf = NULL;
534140748Smaxim	for (;;) {
535201202Sjhb		buf = reallocf(buf, needed);
536201202Sjhb		if (buf == NULL)
537140735Smaxim			errx(1, "could not reallocate memory");
538140735Smaxim		st = sysctl(mib, 6, buf, &needed, NULL, 0);
539140748Smaxim		if (st == 0 || errno != ENOMEM)
540140748Smaxim			break;
541140748Smaxim		needed += needed / 8;
542140748Smaxim	}
543140735Smaxim	if (st == -1)
544128054Smux		err(1, "actual retrieval of routing table");
5451553Srgrimes	lim = buf + needed;
5461553Srgrimes	for (next = buf; next < lim; next += rtm->rtm_msglen) {
5471553Srgrimes		rtm = (struct rt_msghdr *)next;
548246143Sglebius		sin2 = (struct sockaddr_in *)(rtm + 1);
549130246Sstefanf		sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
550117729Syar		if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
551117729Syar		    strcmp(ifname, rifname))
552117729Syar			continue;
5531553Srgrimes		if (addr) {
55487598Smikeh			if (addr != sin2->sin_addr.s_addr)
5551553Srgrimes				continue;
5561553Srgrimes			found_entry = 1;
5571553Srgrimes		}
55887598Smikeh		(*action)(sdl, sin2, rtm);
5591553Srgrimes	}
56076216Syar	free(buf);
561147172Sru	return (found_entry);
5621553Srgrimes}
5631553Srgrimes
56431145Sjulian/*
56531145Sjulian * Display an arp entry
56631145Sjulian */
567209063Smlaier
568128181Sluigistatic void
56931145Sjulianprint_entry(struct sockaddr_dl *sdl,
570246143Sglebius	struct sockaddr_in *addr, struct rt_msghdr *rtm)
57131145Sjulian{
57277870Sru	const char *host;
57331145Sjulian	struct hostent *hp;
57496202Skbyanc	struct iso88025_sockaddr_dl_data *trld;
575264688Sgnn	struct if_nameindex *p;
57644627Sjulian	int seg;
57731145Sjulian
578264688Sgnn	if (ifnameindex == NULL)
579264688Sgnn		if ((ifnameindex = if_nameindex()) == NULL)
580264688Sgnn			err(1, "cannot retrieve interface names");
581264688Sgnn
58231145Sjulian	if (nflag == 0)
58387598Smikeh		hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
58487598Smikeh		    sizeof addr->sin_addr, AF_INET);
58531145Sjulian	else
58631145Sjulian		hp = 0;
58731145Sjulian	if (hp)
58831145Sjulian		host = hp->h_name;
58931145Sjulian	else {
59031145Sjulian		host = "?";
59131145Sjulian		if (h_errno == TRY_AGAIN)
59231145Sjulian			nflag = 1;
59331145Sjulian	}
59487598Smikeh	printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
595130408Sdfr	if (sdl->sdl_alen) {
596139270Sru		if ((sdl->sdl_type == IFT_ETHER ||
597151989Sthompsa		    sdl->sdl_type == IFT_L2VLAN ||
598151989Sthompsa		    sdl->sdl_type == IFT_BRIDGE) &&
599130408Sdfr		    sdl->sdl_alen == ETHER_ADDR_LEN)
600130408Sdfr			printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
601130408Sdfr		else {
602130408Sdfr			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
603130408Sdfr
604130408Sdfr			printf("%s", link_ntoa(sdl) + n);
605130408Sdfr		}
606130408Sdfr	} else
60731145Sjulian		printf("(incomplete)");
608264688Sgnn
609264688Sgnn	for (p = ifnameindex; p && ifnameindex->if_index &&
610264688Sgnn		 ifnameindex->if_name; p++) {
611264688Sgnn		if (p->if_index == sdl->sdl_index) {
612264688Sgnn			printf(" on %s", p->if_name);
613264688Sgnn			break;
614264688Sgnn		}
615264688Sgnn	}
616264688Sgnn
61731145Sjulian	if (rtm->rtm_rmx.rmx_expire == 0)
61831145Sjulian		printf(" permanent");
619203919Sru	else {
620216075Sglebius		static struct timespec tp;
621216075Sglebius		if (tp.tv_sec == 0)
622216075Sglebius			clock_gettime(CLOCK_MONOTONIC, &tp);
623216075Sglebius		if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0)
624203919Sru			printf(" expires in %d seconds", (int)expire_time);
625203919Sru		else
626203919Sru			printf(" expired");
627203919Sru	}
628186119Sqingli	if (rtm->rtm_flags & RTF_ANNOUNCE)
629186119Sqingli		printf(" published");
630186119Sqingli	switch(sdl->sdl_type) {
631128181Sluigi	case IFT_ETHER:
63244627Sjulian                printf(" [ethernet]");
63344627Sjulian                break;
634128181Sluigi	case IFT_ISO88025:
63544627Sjulian                printf(" [token-ring]");
63696202Skbyanc		trld = SDL_ISO88025(sdl);
63796202Skbyanc		if (trld->trld_rcf != 0) {
63896202Skbyanc			printf(" rt=%x", ntohs(trld->trld_rcf));
63996202Skbyanc			for (seg = 0;
64096202Skbyanc			     seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
64196202Skbyanc			     seg++)
64296235Swpaul				printf(":%x", ntohs(*(trld->trld_route[seg])));
64396202Skbyanc		}
64444627Sjulian                break;
645128181Sluigi	case IFT_FDDI:
64685123Smdodd                printf(" [fddi]");
64785123Smdodd                break;
648128181Sluigi	case IFT_ATM:
64985123Smdodd                printf(" [atm]");
65085123Smdodd                break;
651128181Sluigi	case IFT_L2VLAN:
65275180Syar		printf(" [vlan]");
65375180Syar		break;
654130408Sdfr	case IFT_IEEE1394:
655130408Sdfr                printf(" [firewire]");
656130408Sdfr                break;
657151989Sthompsa	case IFT_BRIDGE:
658151989Sthompsa		printf(" [bridge]");
659151989Sthompsa		break;
660281471Smarkj	case IFT_INFINIBAND:
661281471Smarkj		printf(" [infiniband]");
662281471Smarkj		break;
663128181Sluigi	default:
66494075Smurray		break;
66544627Sjulian        }
66644627Sjulian
66731145Sjulian	printf("\n");
66844627Sjulian
66931145Sjulian}
67031145Sjulian
67131145Sjulian/*
67231145Sjulian * Nuke an arp entry
67331145Sjulian */
674128181Sluigistatic void
67587598Smikehnuke_entry(struct sockaddr_dl *sdl __unused,
676294203Smelifaro	struct sockaddr_in *addr, struct rt_msghdr *rtm)
67731145Sjulian{
67831145Sjulian	char ip[20];
67931145Sjulian
680294203Smelifaro	if (rtm->rtm_flags & RTF_PINNED)
681294203Smelifaro		return;
682294203Smelifaro
68387598Smikeh	snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
684246143Sglebius	delete(ip);
68531145Sjulian}
68631145Sjulian
687128181Sluigistatic void
68813977Sphkusage(void)
6891553Srgrimes{
69031145Sjulian	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
691117729Syar		"usage: arp [-n] [-i interface] hostname",
692117729Syar		"       arp [-n] [-i interface] -a",
69377870Sru		"       arp -d hostname [pub]",
694154191Sbrooks		"       arp -d [-i interface] -a",
695186485Strhodes		"       arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
696186485Strhodes		"       arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
69729060Scharnier		"       arp -f filename");
6981553Srgrimes	exit(1);
6991553Srgrimes}
7001553Srgrimes
701128181Sluigistatic struct rt_msghdr *
702246143Sglebiusrtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl)
7031553Srgrimes{
7041553Srgrimes	static int seq;
7051553Srgrimes	int rlen;
706128181Sluigi	int l;
707175206Ssam	struct sockaddr_in so_mask, *som = &so_mask;
708128181Sluigi	static int s = -1;
709128181Sluigi	static pid_t pid;
710128181Sluigi
711128181Sluigi	static struct	{
712128181Sluigi		struct	rt_msghdr m_rtm;
713128181Sluigi		char	m_space[512];
714128181Sluigi	}	m_rtmsg;
715128181Sluigi
716128054Smux	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
717128054Smux	char *cp = m_rtmsg.m_space;
7181553Srgrimes
719128181Sluigi	if (s < 0) {	/* first time: open socket, get pid */
720128181Sluigi		s = socket(PF_ROUTE, SOCK_RAW, 0);
721128181Sluigi		if (s < 0)
722128181Sluigi			err(1, "socket");
723128181Sluigi		pid = getpid();
724128181Sluigi	}
725128181Sluigi	bzero(&so_mask, sizeof(so_mask));
726128181Sluigi	so_mask.sin_len = 8;
727128181Sluigi	so_mask.sin_addr.s_addr = 0xffffffff;
728128181Sluigi
7291553Srgrimes	errno = 0;
730128192Sluigi	/*
731128192Sluigi	 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
732128192Sluigi	 * appropriately.
733128192Sluigi	 */
7341553Srgrimes	if (cmd == RTM_DELETE)
7351553Srgrimes		goto doit;
7361553Srgrimes	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
7371553Srgrimes	rtm->rtm_flags = flags;
7381553Srgrimes	rtm->rtm_version = RTM_VERSION;
7391553Srgrimes
7401553Srgrimes	switch (cmd) {
7411553Srgrimes	default:
74229060Scharnier		errx(1, "internal wrong cmd");
7431553Srgrimes	case RTM_ADD:
7441553Srgrimes		rtm->rtm_addrs |= RTA_GATEWAY;
7451553Srgrimes		rtm->rtm_rmx.rmx_expire = expire_time;
7461553Srgrimes		rtm->rtm_inits = RTV_EXPIRE;
747186500Sqingli		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
7481553Srgrimes		if (doing_proxy) {
749246143Sglebius			rtm->rtm_addrs |= RTA_NETMASK;
750246143Sglebius			rtm->rtm_flags &= ~RTF_HOST;
7511553Srgrimes		}
7521553Srgrimes		/* FALLTHROUGH */
7531553Srgrimes	case RTM_GET:
7541553Srgrimes		rtm->rtm_addrs |= RTA_DST;
7551553Srgrimes	}
756193976Sdes#define NEXTADDR(w, s)					   \
757193976Sdes	do {						   \
758193976Sdes		if ((s) != NULL && rtm->rtm_addrs & (w)) { \
759193976Sdes			bcopy((s), cp, sizeof(*(s)));	   \
760193976Sdes			cp += SA_SIZE(s);		   \
761193976Sdes		}					   \
762193976Sdes	} while (0)
7631553Srgrimes
764128192Sluigi	NEXTADDR(RTA_DST, dst);
765128192Sluigi	NEXTADDR(RTA_GATEWAY, sdl);
766175206Ssam	NEXTADDR(RTA_NETMASK, som);
7671553Srgrimes
7681553Srgrimes	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
7691553Srgrimesdoit:
7701553Srgrimes	l = rtm->rtm_msglen;
7711553Srgrimes	rtm->rtm_seq = ++seq;
7721553Srgrimes	rtm->rtm_type = cmd;
7731553Srgrimes	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
7741553Srgrimes		if (errno != ESRCH || cmd != RTM_DELETE) {
77529060Scharnier			warn("writing to routing socket");
776147172Sru			return (NULL);
7771553Srgrimes		}
7781553Srgrimes	}
7791553Srgrimes	do {
7801553Srgrimes		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
7811553Srgrimes	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
7821553Srgrimes	if (l < 0)
78329060Scharnier		warn("read from routing socket");
784147172Sru	return (rtm);
7851553Srgrimes}
7861553Srgrimes
78713977Sphk/*
78813977Sphk * get_ether_addr - get the hardware address of an interface on the
78913977Sphk * the same subnet as ipaddr.
79013977Sphk */
79113977Sphk#define MAX_IFS		32
79213977Sphk
793128181Sluigistatic int
794147172Sruget_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr)
79513977Sphk{
79613977Sphk	struct ifreq *ifr, *ifend, *ifp;
797147172Sru	in_addr_t ina, mask;
79813977Sphk	struct sockaddr_dl *dla;
79913977Sphk	struct ifreq ifreq;
80013977Sphk	struct ifconf ifc;
80113977Sphk	struct ifreq ifs[MAX_IFS];
80287598Smikeh	int sock;
803128192Sluigi	int retval = 0;
80413977Sphk
80587598Smikeh	sock = socket(AF_INET, SOCK_DGRAM, 0);
80687598Smikeh	if (sock < 0)
80729060Scharnier		err(1, "socket");
80813977Sphk
80913977Sphk	ifc.ifc_len = sizeof(ifs);
81013977Sphk	ifc.ifc_req = ifs;
81192735Smikeh	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
81229060Scharnier		warnx("ioctl(SIOCGIFCONF)");
813128192Sluigi		goto done;
81413977Sphk	}
81513977Sphk
816128192Sluigi#define NEXTIFR(i)						\
817128192Sluigi    ((struct ifreq *)((char *)&(i)->ifr_addr			\
818128192Sluigi	+ MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) )
819128192Sluigi
82013977Sphk	/*
821128192Sluigi	 * Scan through looking for an interface with an Internet
822128192Sluigi	 * address on the same subnet as `ipaddr'.
823128192Sluigi	 */
824128192Sluigi	ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
825128192Sluigi	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) {
826128192Sluigi		if (ifr->ifr_addr.sa_family != AF_INET)
827128192Sluigi			continue;
828128192Sluigi		strncpy(ifreq.ifr_name, ifr->ifr_name,
829128192Sluigi			sizeof(ifreq.ifr_name));
830163305Sglebius		ifreq.ifr_addr = ifr->ifr_addr;
831128192Sluigi		/*
832128192Sluigi		 * Check that the interface is up,
833128192Sluigi		 * and not point-to-point or loopback.
834128192Sluigi		 */
835128192Sluigi		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
836128192Sluigi			continue;
837128192Sluigi		if ((ifreq.ifr_flags &
838128192Sluigi		     (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
839128192Sluigi				IFF_LOOPBACK|IFF_NOARP))
840128192Sluigi		     != (IFF_UP|IFF_BROADCAST))
841128192Sluigi			continue;
842128192Sluigi		/*
843128192Sluigi		 * Get its netmask and check that it's on
844128192Sluigi		 * the right subnet.
845128192Sluigi		 */
846128192Sluigi		if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
847128192Sluigi			continue;
848128192Sluigi		mask = ((struct sockaddr_in *)
849128192Sluigi			&ifreq.ifr_addr)->sin_addr.s_addr;
850128192Sluigi		ina = ((struct sockaddr_in *)
851128192Sluigi			&ifr->ifr_addr)->sin_addr.s_addr;
852128192Sluigi		if ((ipaddr & mask) == (ina & mask))
853128192Sluigi			break; /* ok, we got it! */
85413977Sphk	}
85513977Sphk
856128192Sluigi	if (ifr >= ifend)
857128192Sluigi		goto done;
85813977Sphk
85913977Sphk	/*
860128192Sluigi	 * Now scan through again looking for a link-level address
861128192Sluigi	 * for this interface.
862128192Sluigi	 */
86313977Sphk	ifp = ifr;
864128192Sluigi	for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr))
865128192Sluigi		if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 &&
866128192Sluigi		    ifr->ifr_addr.sa_family == AF_LINK)
867128192Sluigi			break;
868128192Sluigi	if (ifr >= ifend)
869128192Sluigi		goto done;
870128192Sluigi	/*
871128192Sluigi	 * Found the link-level address - copy it out
872128192Sluigi	 */
873128192Sluigi	dla = (struct sockaddr_dl *) &ifr->ifr_addr;
874128192Sluigi	memcpy(hwaddr,  LLADDR(dla), dla->sdl_alen);
875128192Sluigi	printf("using interface %s for proxy with address ",
876128192Sluigi		ifp->ifr_name);
877128192Sluigi	printf("%s\n", ether_ntoa(hwaddr));
878128192Sluigi	retval = dla->sdl_alen;
879128192Sluigidone:
880128192Sluigi	close(sock);
881147172Sru	return (retval);
88213977Sphk}
883