Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.58.4.3
      1  1.58.4.3  pgoyette /*	$NetBSD: arp.c,v 1.58.4.3 2018/09/06 06:56:50 pgoyette Exp $ */
      2      1.10    chopps 
      3       1.1       cgd /*
      4       1.5   mycroft  * Copyright (c) 1984, 1993
      5       1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Sun Microsystems, Inc.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.38       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.19     lukem #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37      1.47     lukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
     38      1.47     lukem  The Regents of the University of California.  All rights reserved.");
     39       1.1       cgd #endif /* not lint */
     40       1.1       cgd 
     41       1.1       cgd #ifndef lint
     42      1.19     lukem #if 0
     43      1.19     lukem static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     44      1.19     lukem #else
     45  1.58.4.3  pgoyette __RCSID("$NetBSD: arp.c,v 1.58.4.3 2018/09/06 06:56:50 pgoyette Exp $");
     46      1.19     lukem #endif
     47       1.1       cgd #endif /* not lint */
     48       1.1       cgd 
     49       1.1       cgd /*
     50       1.1       cgd  * arp - display, set, and delete arp table entries
     51       1.1       cgd  */
     52       1.1       cgd 
     53       1.1       cgd #include <sys/param.h>
     54       1.1       cgd #include <sys/file.h>
     55       1.1       cgd #include <sys/socket.h>
     56       1.5   mycroft #include <sys/sysctl.h>
     57      1.29      fair #include <sys/ioctl.h>
     58       1.5   mycroft 
     59       1.5   mycroft #include <net/if.h>
     60       1.5   mycroft #include <net/if_dl.h>
     61      1.17        is #include <net/if_ether.h>
     62       1.5   mycroft #include <net/if_types.h>
     63       1.5   mycroft #include <net/route.h>
     64       1.1       cgd #include <netinet/in.h>
     65      1.17        is #include <netinet/if_inarp.h>
     66       1.2   mycroft #include <arpa/inet.h>
     67       1.1       cgd 
     68      1.15     mikel #include <err.h>
     69      1.15     mikel #include <errno.h>
     70       1.5   mycroft #include <netdb.h>
     71       1.1       cgd #include <nlist.h>
     72      1.15     mikel #include <paths.h>
     73       1.1       cgd #include <stdio.h>
     74       1.8    chopps #include <stdlib.h>
     75      1.12       cgd #include <string.h>
     76      1.15     mikel #include <unistd.h>
     77      1.35     rafal #include <ifaddrs.h>
     78       1.1       cgd 
     79      1.52     ozaki #include "prog_ops.h"
     80      1.52     ozaki 
     81      1.43    dyoung static int is_llinfo(const struct sockaddr_dl *, int);
     82      1.58     ozaki static int delete_one(struct rt_msghdr *);
     83      1.44  christos static void dump(uint32_t);
     84      1.58     ozaki static void delete(const char *, const char *);
     85      1.44  christos static void sdl_print(const struct sockaddr_dl *);
     86      1.44  christos static int getifname(u_int16_t, char *, size_t);
     87  1.58.4.2  pgoyette static int atosdl(const char *, struct sockaddr_dl *);
     88      1.44  christos static int file(const char *);
     89      1.44  christos static void get(const char *);
     90      1.44  christos static int getinetaddr(const char *, struct in_addr *);
     91      1.53     ozaki static int getsocket(void);
     92  1.58.4.2  pgoyette static int getetheraddr(struct in_addr, struct sockaddr_dl *);
     93  1.58.4.2  pgoyette static struct rt_msghdr * rtmsg(const int, const int,  struct rt_msghdr *,
     94  1.58.4.2  pgoyette 	const struct sockaddr_inarp *, const struct sockaddr_dl *);
     95      1.44  christos static int set(int, char **);
     96      1.45     perry static void usage(void) __dead;
     97      1.17        is 
     98      1.30    atatat static int aflag, nflag, vflag;
     99      1.56       ryo static struct sockaddr_in so_mask = {
    100      1.44  christos 	.sin_len = 8,
    101      1.44  christos 	.sin_addr = {
    102      1.44  christos 		.s_addr = 0xffffffff
    103      1.44  christos 	}
    104      1.44  christos };
    105      1.44  christos static struct sockaddr_inarp blank_sin = {
    106      1.44  christos 	.sin_len = sizeof(blank_sin),
    107      1.44  christos 	.sin_family = AF_INET
    108      1.44  christos };
    109      1.44  christos static struct sockaddr_dl blank_sdl = {
    110      1.44  christos 	.sdl_len = sizeof(blank_sdl),
    111      1.44  christos 	.sdl_family = AF_LINK
    112      1.44  christos };
    113      1.44  christos 
    114      1.44  christos static int expire_time, flags, export_only, doing_proxy, found_entry;
    115      1.16     mikel 
    116       1.8    chopps int
    117      1.41   xtraeme main(int argc, char **argv)
    118       1.1       cgd {
    119       1.1       cgd 	int ch;
    120      1.15     mikel 	int op = 0;
    121      1.34        tv 
    122      1.34        tv 	setprogname(argv[0]);
    123       1.1       cgd 
    124      1.23       mrg 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    125       1.1       cgd 		switch((char)ch) {
    126       1.5   mycroft 		case 'a':
    127      1.30    atatat 			aflag = 1;
    128      1.30    atatat 			break;
    129       1.1       cgd 		case 'd':
    130      1.15     mikel 		case 's':
    131      1.15     mikel 		case 'f':
    132      1.15     mikel 			if (op)
    133       1.1       cgd 				usage();
    134      1.15     mikel 			op = ch;
    135      1.15     mikel 			break;
    136       1.5   mycroft 		case 'n':
    137       1.5   mycroft 			nflag = 1;
    138       1.8    chopps 			break;
    139      1.23       mrg 		case 'v':
    140      1.23       mrg 			vflag = 1;
    141      1.23       mrg 			break;
    142       1.1       cgd 		default:
    143       1.1       cgd 			usage();
    144       1.1       cgd 		}
    145      1.15     mikel 	argc -= optind;
    146      1.15     mikel 	argv += optind;
    147      1.15     mikel 
    148      1.30    atatat 	if (!op && aflag)
    149      1.30    atatat 		op = 'a';
    150      1.30    atatat 
    151      1.52     ozaki 	if (prog_init && prog_init() == -1)
    152      1.52     ozaki 		err(1, "init failed");
    153      1.52     ozaki 
    154      1.15     mikel 	switch((char)op) {
    155      1.15     mikel 	case 'a':
    156      1.15     mikel 		dump(0);
    157      1.15     mikel 		break;
    158      1.15     mikel 	case 'd':
    159      1.30    atatat 		if (aflag && argc == 0)
    160      1.58     ozaki 			delete(NULL, NULL);
    161      1.30    atatat 		else {
    162      1.30    atatat 			if (aflag || argc < 1 || argc > 2)
    163      1.30    atatat 				usage();
    164      1.58     ozaki 			delete(argv[0], argv[1]);
    165      1.30    atatat 		}
    166      1.15     mikel 		break;
    167      1.15     mikel 	case 's':
    168      1.51  christos 		if (argc < 2 || argc > 7)
    169      1.15     mikel 			usage();
    170      1.15     mikel 		return (set(argc, argv) ? 1 : 0);
    171      1.15     mikel 	case 'f':
    172      1.15     mikel 		if (argc != 1)
    173      1.15     mikel 			usage();
    174      1.15     mikel 		return (file(argv[0]));
    175      1.15     mikel 	default:
    176      1.15     mikel 		if (argc != 1)
    177      1.15     mikel 			usage();
    178      1.15     mikel 		get(argv[0]);
    179      1.15     mikel 		break;
    180      1.15     mikel 	}
    181       1.8    chopps 	return (0);
    182       1.1       cgd }
    183       1.1       cgd 
    184       1.1       cgd /*
    185       1.1       cgd  * Process a file to set standard arp entries
    186       1.1       cgd  */
    187      1.44  christos static int
    188      1.44  christos file(const char *name)
    189       1.1       cgd {
    190      1.44  christos 	char *line, *argv[5];
    191       1.8    chopps 	int i, retval;
    192       1.1       cgd 	FILE *fp;
    193       1.1       cgd 
    194      1.49  dholland 	if (!strcmp(name, "-")) {
    195      1.49  dholland 		fp = stdin;
    196      1.49  dholland 	} else {
    197      1.49  dholland 		fp = fopen(name, "r");
    198      1.49  dholland 		if (fp == NULL) {
    199      1.49  dholland 			err(1, "Cannot open %s", name);
    200      1.49  dholland 		}
    201      1.49  dholland 	}
    202       1.1       cgd 	retval = 0;
    203      1.44  christos 	for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
    204      1.44  christos 		char **ap, *inputstring;
    205      1.44  christos 
    206      1.44  christos 		inputstring = line;
    207      1.44  christos 		for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
    208      1.44  christos 		    (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
    209      1.56       ryo 			if (**ap != '\0')
    210      1.44  christos 				ap++;
    211      1.44  christos 		}
    212      1.44  christos 		i = ap - argv;
    213       1.1       cgd 		if (i < 2) {
    214       1.8    chopps 			warnx("bad line: %s", line);
    215       1.1       cgd 			retval = 1;
    216       1.1       cgd 			continue;
    217       1.1       cgd 		}
    218      1.44  christos 		if (set(i, argv))
    219       1.1       cgd 			retval = 1;
    220       1.1       cgd 	}
    221      1.49  dholland 	if (fp != stdin)
    222      1.49  dholland 		(void)fclose(fp);
    223      1.44  christos 	return retval;
    224       1.1       cgd }
    225       1.1       cgd 
    226      1.53     ozaki static int
    227      1.41   xtraeme getsocket(void)
    228       1.8    chopps {
    229      1.53     ozaki 	int s;
    230      1.52     ozaki 	s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
    231       1.8    chopps 	if (s < 0)
    232       1.8    chopps 		err(1, "socket");
    233      1.53     ozaki 	return s;
    234       1.5   mycroft }
    235       1.5   mycroft 
    236      1.50  christos static int
    237      1.50  christos getlink(const char *name, struct sockaddr_dl *sdl)
    238      1.50  christos {
    239      1.50  christos 	struct ifaddrs *ifap, *ifa;
    240      1.50  christos 
    241      1.50  christos 	if (getifaddrs(&ifap) != 0) {
    242      1.50  christos 		warn("getifaddrs");
    243      1.50  christos 		return 0;
    244      1.50  christos 	}
    245      1.50  christos 
    246      1.50  christos 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    247      1.50  christos 		if (ifa->ifa_addr->sa_family != AF_LINK)
    248      1.50  christos 			continue;
    249      1.50  christos 		if (strcmp(ifa->ifa_name, name) != 0)
    250      1.50  christos 			continue;
    251      1.50  christos 		memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
    252      1.50  christos 		freeifaddrs(ifap);
    253      1.50  christos 		return 1;
    254      1.50  christos 	}
    255      1.50  christos 	freeifaddrs(ifap);
    256      1.50  christos 	return 0;
    257      1.50  christos }
    258      1.50  christos 
    259       1.1       cgd /*
    260      1.56       ryo  * Set an individual arp entry
    261       1.1       cgd  */
    262      1.44  christos static int
    263      1.41   xtraeme set(int argc, char **argv)
    264       1.1       cgd {
    265      1.41   xtraeme 	struct sockaddr_inarp *sina;
    266      1.55     ozaki 	struct sockaddr_dl *sdl = NULL;
    267      1.19     lukem 	struct rt_msghdr *rtm;
    268       1.8    chopps 	char *host = argv[0], *eaddr;
    269      1.53     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    270      1.53     ozaki 	struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */
    271      1.53     ozaki 	int s;
    272       1.8    chopps 
    273       1.8    chopps 	eaddr = argv[1];
    274       1.1       cgd 
    275      1.53     ozaki 	s = getsocket();
    276       1.1       cgd 	argc -= 2;
    277       1.1       cgd 	argv += 2;
    278      1.53     ozaki 
    279      1.53     ozaki 	if (getinetaddr(host, &sin_m.sin_addr) == -1)
    280       1.8    chopps 		return (1);
    281  1.58.4.2  pgoyette 	if (strcmp(eaddr, "auto") != 0 && atosdl(eaddr, &sdl_m))
    282      1.17        is 		warnx("invalid link-level address '%s'", eaddr);
    283       1.5   mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    284      1.50  christos 	for (; argc-- > 0; argv++) {
    285       1.5   mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    286      1.41   xtraeme 			struct timeval timev;
    287      1.41   xtraeme 			(void)gettimeofday(&timev, 0);
    288      1.41   xtraeme 			expire_time = timev.tv_sec + 20 * 60;
    289       1.5   mycroft 		}
    290       1.5   mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    291       1.5   mycroft 			flags |= RTF_ANNOUNCE;
    292      1.54     ozaki 			doing_proxy = 1;
    293      1.46     seanb 			if (argc && strncmp(argv[1], "pro", 3) == 0) {
    294      1.56       ryo 				export_only = 1;
    295      1.56       ryo 				argc--; argv++;
    296      1.46     seanb 			}
    297       1.5   mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    298      1.44  christos 			warnx("%s: Sending trailers is no longer supported",
    299      1.44  christos 			    host);
    300      1.50  christos 		} else if (strcmp(argv[0], "ifscope") == 0) {
    301      1.50  christos 			if (argc == 0) {
    302      1.50  christos 				warnx("missing interface for ifscope");
    303      1.50  christos 				continue;
    304      1.50  christos 			}
    305      1.50  christos 			argc--;
    306      1.50  christos 			argv++;
    307      1.50  christos 			if (!getlink(argv[0], &sdl_m))
    308      1.50  christos 				warnx("cannot get link address for %s", argv[0]);
    309       1.5   mycroft 		}
    310      1.50  christos 
    311       1.1       cgd 	}
    312  1.58.4.2  pgoyette 	if (doing_proxy && strcmp(eaddr, "auto") == 0) {
    313  1.58.4.2  pgoyette 		if (getetheraddr(sin_m.sin_addr, &sdl_m) == -1)
    314  1.58.4.2  pgoyette 			return 1;
    315  1.58.4.2  pgoyette 	}
    316       1.5   mycroft tryagain:
    317      1.58     ozaki 	rtm = rtmsg(s, RTM_GET, NULL, &sin_m, &sdl_m);
    318      1.53     ozaki 	if (rtm == NULL) {
    319       1.8    chopps 		warn("%s", host);
    320       1.5   mycroft 		return (1);
    321       1.1       cgd 	}
    322      1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    323      1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    324      1.44  christos 	    (char *)(void *)sina);
    325      1.41   xtraeme 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    326      1.43    dyoung 		if (is_llinfo(sdl, rtm->rtm_flags))
    327       1.5   mycroft 			goto overwrite;
    328       1.5   mycroft 		if (doing_proxy == 0) {
    329      1.44  christos 			warnx("set: can only proxy for %s", host);
    330       1.5   mycroft 			return (1);
    331       1.5   mycroft 		}
    332       1.5   mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    333      1.44  christos 			warnx("set: proxy entry exists for non 802 device");
    334       1.8    chopps 			return (1);
    335       1.5   mycroft 		}
    336       1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    337       1.5   mycroft 		export_only = 1;
    338       1.5   mycroft 		goto tryagain;
    339       1.5   mycroft 	}
    340       1.5   mycroft overwrite:
    341       1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    342      1.44  christos 		warnx("cannot intuit interface index and type for %s",
    343       1.8    chopps 		    host);
    344       1.5   mycroft 		return (1);
    345       1.1       cgd 	}
    346       1.5   mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    347       1.5   mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    348      1.53     ozaki 	sin_m.sin_other = 0;
    349      1.53     ozaki 	if (doing_proxy && export_only)
    350      1.53     ozaki 		sin_m.sin_other = SIN_PROXY;
    351      1.58     ozaki 	rtm = rtmsg(s, RTM_ADD, NULL, &sin_m, &sdl_m);
    352      1.23       mrg 	if (vflag)
    353      1.23       mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    354      1.53     ozaki 	return (rtm == NULL) ? 1 : 0;
    355       1.1       cgd }
    356       1.1       cgd 
    357       1.1       cgd /*
    358       1.1       cgd  * Display an individual arp entry
    359       1.1       cgd  */
    360      1.44  christos static void
    361      1.41   xtraeme get(const char *host)
    362       1.1       cgd {
    363      1.53     ozaki 	struct sockaddr_inarp sin = blank_sin; /* struct copy */
    364       1.1       cgd 
    365      1.53     ozaki 	if (getinetaddr(host, &sin.sin_addr) == -1)
    366       1.8    chopps 		exit(1);
    367      1.53     ozaki 	dump(sin.sin_addr.s_addr);
    368      1.44  christos 	if (found_entry == 0)
    369      1.53     ozaki 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr));
    370       1.1       cgd }
    371       1.1       cgd 
    372      1.43    dyoung 
    373      1.43    dyoung static int
    374      1.43    dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
    375      1.43    dyoung {
    376      1.43    dyoung 	if (sdl->sdl_family != AF_LINK ||
    377      1.55     ozaki 	    (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
    378      1.43    dyoung 		return 0;
    379      1.43    dyoung 
    380      1.43    dyoung 	switch (sdl->sdl_type) {
    381      1.43    dyoung 	case IFT_ETHER:
    382      1.43    dyoung 	case IFT_FDDI:
    383      1.43    dyoung 	case IFT_ISO88023:
    384      1.43    dyoung 	case IFT_ISO88024:
    385      1.43    dyoung 	case IFT_ISO88025:
    386      1.43    dyoung 	case IFT_ARCNET:
    387      1.43    dyoung 		return 1;
    388      1.43    dyoung 	default:
    389      1.43    dyoung 		return 0;
    390      1.43    dyoung 	}
    391      1.43    dyoung }
    392      1.43    dyoung 
    393       1.1       cgd /*
    394      1.56       ryo  * Delete an arp entry
    395       1.1       cgd  */
    396       1.8    chopps int
    397      1.58     ozaki delete_one(struct rt_msghdr *rtm)
    398       1.1       cgd {
    399      1.41   xtraeme 	struct sockaddr_inarp *sina;
    400      1.53     ozaki 	struct sockaddr_dl *sdl;
    401      1.53     ozaki 	int s;
    402       1.8    chopps 
    403      1.53     ozaki 	s = getsocket();
    404      1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    405      1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    406      1.44  christos 	    (char *)(void *)sina);
    407      1.58     ozaki 	if (sdl->sdl_family != AF_LINK)
    408       1.5   mycroft 		return (1);
    409      1.58     ozaki 	rtm = rtmsg(s, RTM_DELETE, rtm, sina, sdl);
    410      1.53     ozaki 	if (rtm == NULL)
    411       1.8    chopps 		return (1);
    412       1.8    chopps 	return (0);
    413       1.1       cgd }
    414       1.1       cgd 
    415       1.1       cgd /*
    416       1.1       cgd  * Dump the entire arp table
    417       1.1       cgd  */
    418       1.8    chopps void
    419      1.44  christos dump(uint32_t addr)
    420       1.1       cgd {
    421       1.5   mycroft 	int mib[6];
    422       1.5   mycroft 	size_t needed;
    423      1.29      fair 	char ifname[IFNAMSIZ];
    424      1.41   xtraeme 	char *lim, *buf, *next;
    425      1.41   xtraeme         const char *host;
    426       1.5   mycroft 	struct rt_msghdr *rtm;
    427      1.41   xtraeme 	struct sockaddr_inarp *sina;
    428       1.5   mycroft 	struct sockaddr_dl *sdl;
    429       1.1       cgd 	struct hostent *hp;
    430       1.1       cgd 
    431       1.5   mycroft 	mib[0] = CTL_NET;
    432       1.5   mycroft 	mib[1] = PF_ROUTE;
    433       1.5   mycroft 	mib[2] = 0;
    434       1.5   mycroft 	mib[3] = AF_INET;
    435       1.5   mycroft 	mib[4] = NET_RT_FLAGS;
    436      1.55     ozaki 	mib[5] = 0;
    437      1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    438       1.8    chopps 		err(1, "route-sysctl-estimate");
    439      1.22      fvdl 	if (needed == 0)
    440      1.22      fvdl 		return;
    441       1.5   mycroft 	if ((buf = malloc(needed)) == NULL)
    442       1.8    chopps 		err(1, "malloc");
    443      1.52     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    444       1.8    chopps 		err(1, "actual retrieval of routing table");
    445       1.5   mycroft 	lim = buf + needed;
    446       1.5   mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    447      1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    448      1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    449      1.44  christos 		sdl = (struct sockaddr_dl *)(void *)
    450      1.48  christos 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
    451       1.5   mycroft 		if (addr) {
    452      1.41   xtraeme 			if (addr != sina->sin_addr.s_addr)
    453       1.5   mycroft 				continue;
    454       1.5   mycroft 			found_entry = 1;
    455       1.5   mycroft 		}
    456       1.5   mycroft 		if (nflag == 0)
    457      1.44  christos 			hp = gethostbyaddr((const char *)(void *)
    458      1.44  christos 			    &(sina->sin_addr),
    459      1.41   xtraeme 			    sizeof sina->sin_addr, AF_INET);
    460       1.1       cgd 		else
    461      1.27  christos 			hp = NULL;
    462      1.27  christos 
    463      1.27  christos 		host = hp ? hp->h_name : "?";
    464      1.27  christos 
    465      1.41   xtraeme 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
    466       1.5   mycroft 		if (sdl->sdl_alen)
    467      1.17        is 			sdl_print(sdl);
    468       1.1       cgd 		else
    469       1.8    chopps 			(void)printf("(incomplete)");
    470      1.29      fair 
    471      1.29      fair 		if (sdl->sdl_index) {
    472      1.37    itojun 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
    473      1.44  christos 				(void)printf(" on %s", ifname);
    474      1.29      fair 		}
    475      1.29      fair 
    476       1.5   mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    477       1.8    chopps 			(void)printf(" permanent");
    478      1.41   xtraeme 		if (sina->sin_other & SIN_PROXY)
    479       1.8    chopps 			(void)printf(" published (proxy only)");
    480       1.5   mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    481      1.44  christos 			sina = (struct sockaddr_inarp *)(void *)
    482      1.48  christos 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
    483      1.41   xtraeme 			if (sina->sin_addr.s_addr == 0xffffffff)
    484       1.8    chopps 				(void)printf(" published");
    485      1.41   xtraeme 			if (sina->sin_len != 8)
    486      1.25       erh 				(void)printf("(weird)");
    487       1.5   mycroft 		}
    488       1.8    chopps 		(void)printf("\n");
    489       1.1       cgd 	}
    490      1.39    itojun 	free(buf);
    491       1.1       cgd }
    492       1.1       cgd 
    493      1.30    atatat /*
    494      1.30    atatat  * Delete the entire arp table
    495      1.30    atatat  */
    496      1.30    atatat void
    497      1.58     ozaki delete(const char *host, const char *info)
    498      1.30    atatat {
    499      1.30    atatat 	int mib[6];
    500  1.58.4.3  pgoyette 	char addr[sizeof("000.000.000.000\0")];
    501      1.30    atatat 	size_t needed;
    502      1.30    atatat 	char *lim, *buf, *next;
    503      1.30    atatat 	struct rt_msghdr *rtm;
    504      1.41   xtraeme 	struct sockaddr_inarp *sina;
    505      1.58     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    506  1.58.4.2  pgoyette 	bool found = false;
    507      1.58     ozaki 
    508      1.58     ozaki 	if (host != NULL) {
    509      1.58     ozaki 		int ret = getinetaddr(host, &sin_m.sin_addr);
    510      1.58     ozaki 		if (ret == -1)
    511      1.58     ozaki 			return;
    512      1.58     ozaki 	}
    513      1.58     ozaki 	if (info && strncmp(info, "pro", 3) == 0)
    514      1.58     ozaki 		sin_m.sin_other = SIN_PROXY;
    515      1.30    atatat 
    516      1.58     ozaki retry:
    517      1.30    atatat 	mib[0] = CTL_NET;
    518      1.30    atatat 	mib[1] = PF_ROUTE;
    519      1.30    atatat 	mib[2] = 0;
    520      1.30    atatat 	mib[3] = AF_INET;
    521      1.30    atatat 	mib[4] = NET_RT_FLAGS;
    522      1.58     ozaki 	mib[5] = RTF_LLDATA;
    523      1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    524      1.30    atatat 		err(1, "route-sysctl-estimate");
    525      1.30    atatat 	if (needed == 0)
    526      1.30    atatat 		return;
    527      1.30    atatat 	if ((buf = malloc(needed)) == NULL)
    528      1.30    atatat 		err(1, "malloc");
    529      1.58     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    530      1.58     ozaki 		free(buf);
    531      1.58     ozaki 		if (errno == ENOBUFS)
    532      1.58     ozaki 			goto retry;
    533      1.30    atatat 		err(1, "actual retrieval of routing table");
    534      1.58     ozaki 	}
    535      1.30    atatat 	lim = buf + needed;
    536      1.58     ozaki 
    537      1.30    atatat 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    538      1.58     ozaki 		int ret;
    539      1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    540      1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    541      1.58     ozaki 		if (host != NULL &&
    542      1.58     ozaki 		    sina->sin_addr.s_addr != sin_m.sin_addr.s_addr)
    543      1.58     ozaki 			continue;
    544  1.58.4.2  pgoyette 		found = true;
    545      1.58     ozaki 		ret = delete_one(rtm);
    546      1.58     ozaki 		if (vflag && ret == 0) {
    547  1.58.4.3  pgoyette 			snprintf(addr, sizeof(addr), "%s",
    548      1.58     ozaki 			    inet_ntoa(sina->sin_addr));
    549  1.58.4.3  pgoyette 			(void)printf("%s (%s) deleted\n",
    550  1.58.4.3  pgoyette 			    host != NULL ? host : addr, addr);
    551      1.58     ozaki 		}
    552      1.30    atatat 	}
    553  1.58.4.2  pgoyette 	if (host != NULL && !found)
    554  1.58.4.2  pgoyette 		warnx("delete: can't locate %s", host);
    555      1.39    itojun 	free(buf);
    556      1.30    atatat }
    557      1.30    atatat 
    558       1.8    chopps void
    559      1.41   xtraeme sdl_print(const struct sockaddr_dl *sdl)
    560      1.17        is {
    561      1.32     bjh21 	char hbuf[NI_MAXHOST];
    562      1.17        is 
    563      1.44  christos 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
    564      1.44  christos 	    (socklen_t)sdl->sdl_len,
    565      1.32     bjh21 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    566      1.44  christos 		(void)printf("<invalid>");
    567      1.32     bjh21 	else
    568      1.44  christos 		(void)printf("%s", hbuf);
    569      1.17        is }
    570      1.17        is 
    571      1.44  christos static int
    572      1.41   xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
    573       1.1       cgd {
    574      1.17        is 	int i;
    575      1.44  christos 	unsigned long b;
    576      1.44  christos 	char *endp;
    577      1.56       ryo 	char *p;
    578      1.17        is 	char *t, *r;
    579      1.17        is 
    580      1.17        is 	p = LLADDR(sdl);
    581      1.44  christos 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
    582      1.17        is 	i = 0;
    583      1.56       ryo 
    584      1.44  christos 	b = strtoul(ss, &t, 16);
    585      1.44  christos 	if (b > 255 || t == ss)
    586      1.17        is 		return 1;
    587      1.17        is 
    588      1.44  christos 	*p++ = (char)b;
    589      1.17        is 	++i;
    590      1.17        is 	while ((p < endp) && (*t++ == ':')) {
    591      1.44  christos 		b = strtoul(t, &r, 16);
    592      1.44  christos 		if (b > 255 || r == t)
    593      1.17        is 			break;
    594      1.44  christos 		*p++ = (char)b;
    595      1.17        is 		++i;
    596      1.17        is 		t = r;
    597      1.17        is 	}
    598      1.17        is 	sdl->sdl_alen = i;
    599      1.17        is 
    600      1.17        is 	return 0;
    601       1.1       cgd }
    602       1.1       cgd 
    603      1.44  christos static void
    604      1.41   xtraeme usage(void)
    605       1.1       cgd {
    606      1.33     pooka 	const char *progname;
    607      1.20     lukem 
    608      1.33     pooka 	progname = getprogname();
    609      1.44  christos 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
    610      1.56       ryo 	(void)fprintf(stderr, "       %s [-nv] -a\n", progname);
    611  1.58.4.1  pgoyette 	(void)fprintf(stderr, "       %s [-v] -d [-a|hostname [proxy]]\n",
    612      1.44  christos 	    progname);
    613      1.46     seanb 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
    614      1.44  christos 	    progname);
    615      1.44  christos 	(void)fprintf(stderr, "       %s -f filename\n", progname);
    616       1.5   mycroft 	exit(1);
    617       1.5   mycroft }
    618       1.5   mycroft 
    619      1.53     ozaki static struct rt_msghdr *
    620      1.58     ozaki rtmsg(const int s, const int cmd, struct rt_msghdr *_rtm,
    621      1.58     ozaki     const struct sockaddr_inarp *sin, const struct sockaddr_dl *sdl)
    622       1.5   mycroft {
    623       1.5   mycroft 	static int seq;
    624      1.58     ozaki 	struct rt_msghdr *rtm = _rtm;
    625      1.19     lukem 	char *cp;
    626      1.19     lukem 	int l;
    627      1.53     ozaki 	static struct {
    628      1.53     ozaki 		struct	rt_msghdr m_rtm;
    629      1.53     ozaki 		char	m_space[512];
    630      1.53     ozaki 	} m_rtmsg;
    631      1.53     ozaki 	pid_t pid;
    632       1.5   mycroft 
    633       1.5   mycroft 	errno = 0;
    634      1.58     ozaki 	if (rtm != NULL) {
    635      1.58     ozaki 		memcpy(&m_rtmsg, rtm, rtm->rtm_msglen);
    636      1.58     ozaki 		rtm = &m_rtmsg.m_rtm;
    637       1.5   mycroft 		goto doit;
    638      1.55     ozaki 	}
    639       1.8    chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    640      1.58     ozaki 	rtm = &m_rtmsg.m_rtm;
    641      1.58     ozaki 	cp = m_rtmsg.m_space;
    642      1.58     ozaki 
    643       1.5   mycroft 	rtm->rtm_flags = flags;
    644       1.5   mycroft 	rtm->rtm_version = RTM_VERSION;
    645       1.5   mycroft 
    646       1.5   mycroft 	switch (cmd) {
    647       1.5   mycroft 	default:
    648       1.8    chopps 		errx(1, "internal wrong cmd");
    649       1.8    chopps 		/*NOTREACHED*/
    650       1.5   mycroft 	case RTM_ADD:
    651       1.5   mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    652       1.5   mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    653       1.5   mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    654      1.55     ozaki 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
    655       1.5   mycroft 		if (doing_proxy) {
    656      1.53     ozaki 			if (!export_only) {
    657       1.5   mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    658       1.5   mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    659       1.5   mycroft 			}
    660       1.5   mycroft 		}
    661      1.55     ozaki 		rtm->rtm_addrs |= RTA_DST;
    662      1.55     ozaki 		break;
    663       1.5   mycroft 	case RTM_GET:
    664      1.55     ozaki 		rtm->rtm_flags |= RTF_LLDATA;
    665      1.57     ozaki 		rtm->rtm_addrs |= RTA_DST | RTA_GATEWAY;
    666       1.5   mycroft 	}
    667      1.25       erh 
    668       1.5   mycroft #define NEXTADDR(w, s) \
    669       1.5   mycroft 	if (rtm->rtm_addrs & (w)) { \
    670      1.44  christos 		(void)memcpy(cp, &s, \
    671      1.53     ozaki 		    (size_t)((const struct sockaddr *)&s)->sa_len); \
    672      1.53     ozaki 		RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \
    673      1.25       erh 	}
    674       1.5   mycroft 
    675      1.53     ozaki 	NEXTADDR(RTA_DST, *sin);
    676      1.53     ozaki 	NEXTADDR(RTA_GATEWAY, *sdl);
    677       1.5   mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    678       1.5   mycroft 
    679      1.44  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    680       1.5   mycroft doit:
    681       1.5   mycroft 	l = rtm->rtm_msglen;
    682       1.5   mycroft 	rtm->rtm_seq = ++seq;
    683       1.5   mycroft 	rtm->rtm_type = cmd;
    684      1.52     ozaki 	if (prog_write(s, &m_rtmsg, (size_t)l) < 0) {
    685       1.5   mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    686       1.8    chopps 			warn("writing to routing socket");
    687      1.53     ozaki 			return NULL;
    688       1.5   mycroft 		}
    689       1.5   mycroft 	}
    690      1.53     ozaki 
    691      1.53     ozaki 	pid = prog_getpid();
    692       1.5   mycroft 	do {
    693      1.52     ozaki 		l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg));
    694       1.5   mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    695       1.5   mycroft 	if (l < 0)
    696       1.8    chopps 		warn("read from routing socket");
    697      1.53     ozaki 	return rtm;
    698       1.5   mycroft }
    699       1.5   mycroft 
    700      1.44  christos static int
    701      1.41   xtraeme getinetaddr(const char *host, struct in_addr *inap)
    702       1.5   mycroft {
    703       1.8    chopps 	struct hostent *hp;
    704       1.8    chopps 
    705       1.9    chopps 	if (inet_aton(host, inap) == 1)
    706       1.8    chopps 		return (0);
    707       1.8    chopps 	if ((hp = gethostbyname(host)) == NULL) {
    708      1.31       chs 		warnx("%s: %s", host, hstrerror(h_errno));
    709       1.8    chopps 		return (-1);
    710       1.8    chopps 	}
    711       1.8    chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    712       1.8    chopps 	return (0);
    713      1.29      fair }
    714      1.29      fair 
    715      1.44  christos static int
    716      1.41   xtraeme getifname(u_int16_t ifindex, char *ifname, size_t l)
    717      1.29      fair {
    718      1.35     rafal 	int i;
    719      1.35     rafal 	struct ifaddrs *addr;
    720  1.58.4.2  pgoyette 	const struct sockaddr_dl *sdl;
    721      1.53     ozaki 	static struct ifaddrs* ifaddrs = NULL;
    722      1.29      fair 
    723      1.35     rafal 	if (ifaddrs == NULL) {
    724      1.35     rafal 		i = getifaddrs(&ifaddrs);
    725      1.35     rafal 		if (i != 0)
    726      1.35     rafal 			err(1, "getifaddrs");
    727      1.29      fair 	}
    728      1.29      fair 
    729      1.35     rafal 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    730      1.56       ryo 		if (addr->ifa_addr == NULL ||
    731      1.35     rafal 		    addr->ifa_addr->sa_family != AF_LINK)
    732      1.29      fair 			continue;
    733      1.29      fair 
    734      1.44  christos 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
    735      1.29      fair 		if (sdl && sdl->sdl_index == ifindex) {
    736      1.37    itojun 			(void) strlcpy(ifname, addr->ifa_name, l);
    737      1.29      fair 			return 0;
    738      1.29      fair 		}
    739      1.29      fair 	}
    740      1.29      fair 
    741      1.29      fair 	return -1;
    742       1.1       cgd }
    743  1.58.4.2  pgoyette 
    744  1.58.4.2  pgoyette static int
    745  1.58.4.2  pgoyette getetheraddr(struct in_addr ipaddr, struct sockaddr_dl *sdl)
    746  1.58.4.2  pgoyette {
    747  1.58.4.2  pgoyette 	struct ifaddrs *ifaddrs, *addr;
    748  1.58.4.2  pgoyette 	in_addr_t ina, mask;
    749  1.58.4.2  pgoyette 	char ifname[IFNAMSIZ];
    750  1.58.4.2  pgoyette 
    751  1.58.4.2  pgoyette 	if (getifaddrs(&ifaddrs) != 0) {
    752  1.58.4.2  pgoyette 		warn("getifaddrs");
    753  1.58.4.2  pgoyette 		return -1;
    754  1.58.4.2  pgoyette 	}
    755  1.58.4.2  pgoyette 
    756  1.58.4.2  pgoyette 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    757  1.58.4.2  pgoyette 		if (addr->ifa_addr == NULL ||
    758  1.58.4.2  pgoyette 		    addr->ifa_addr->sa_family != AF_INET)
    759  1.58.4.2  pgoyette 			continue;
    760  1.58.4.2  pgoyette 		if ((addr->ifa_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
    761  1.58.4.2  pgoyette 		    IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST))
    762  1.58.4.2  pgoyette 			continue;
    763  1.58.4.2  pgoyette 
    764  1.58.4.2  pgoyette 		mask = ((struct sockaddr_in *)(void *)addr->ifa_netmask)->sin_addr.s_addr;
    765  1.58.4.2  pgoyette 		ina = ((struct sockaddr_in *)(void *)addr->ifa_addr)->sin_addr.s_addr;
    766  1.58.4.2  pgoyette 		if ((ipaddr.s_addr & mask) != (ina & mask))
    767  1.58.4.2  pgoyette 			continue;
    768  1.58.4.2  pgoyette 		strlcpy(ifname, addr->ifa_name, sizeof(ifname));
    769  1.58.4.2  pgoyette 		break;
    770  1.58.4.2  pgoyette 	}
    771  1.58.4.2  pgoyette 	if (addr == NULL) {
    772  1.58.4.2  pgoyette 		warnx("No interface matched %s", inet_ntoa(ipaddr));
    773  1.58.4.2  pgoyette 		freeifaddrs(ifaddrs);
    774  1.58.4.2  pgoyette 		return -1;
    775  1.58.4.2  pgoyette 	}
    776  1.58.4.2  pgoyette 
    777  1.58.4.2  pgoyette 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    778  1.58.4.2  pgoyette 		if (addr->ifa_addr == NULL ||
    779  1.58.4.2  pgoyette 		    addr->ifa_addr->sa_family != AF_LINK)
    780  1.58.4.2  pgoyette 			continue;
    781  1.58.4.2  pgoyette 		if (strcmp(ifname, addr->ifa_name) != 0)
    782  1.58.4.2  pgoyette 			continue;
    783  1.58.4.2  pgoyette 		memcpy(sdl, addr->ifa_addr, sizeof(*sdl));
    784  1.58.4.2  pgoyette 		freeifaddrs(ifaddrs);
    785  1.58.4.2  pgoyette 		return 0;
    786  1.58.4.2  pgoyette 	}
    787  1.58.4.2  pgoyette 	warnx("No link address for interface %s", ifname);
    788  1.58.4.2  pgoyette 	freeifaddrs(ifaddrs);
    789  1.58.4.2  pgoyette 	return -1;
    790  1.58.4.2  pgoyette }
    791