Home | History | Annotate | Line # | Download | only in rarpd
rarpd.c revision 1.35
      1 /*	$NetBSD: rarpd.c,v 1.35 2000/02/07 01:15:52 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that: (1) source code distributions
      9  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  * distributions including binary code include the above copyright notice and
     11  * this paragraph in its entirety in the documentation or other materials
     12  * provided with the distribution, and (3) all advertising materials mentioning
     13  * features or use of this software display the following acknowledgement:
     14  * ``This product includes software developed by the University of California,
     15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  * the University nor the names of its contributors may be used to endorse
     17  * or promote products derived from this software without specific prior
     18  * written permission.
     19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  */
     23 #include <sys/cdefs.h>
     24 #ifndef lint
     25 __COPYRIGHT(
     26     "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
     27  All rights reserved.\n");
     28 #endif /* not lint */
     29 
     30 #ifndef lint
     31 __RCSID("$NetBSD: rarpd.c,v 1.35 2000/02/07 01:15:52 nathanw Exp $");
     32 #endif
     33 
     34 
     35 /*
     36  * rarpd - Reverse ARP Daemon
     37  *
     38  * Usage:	rarpd -a [-d|-f] [-l]
     39  *		rarpd [-d|-f] [-l] interface
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/file.h>
     44 #include <sys/time.h>
     45 #include <sys/socket.h>
     46 #include <sys/ioctl.h>
     47 
     48 #include <net/bpf.h>
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #ifdef __NetBSD__
     52 #include <net/if_ether.h>
     53 #endif
     54 #include <net/if_types.h>
     55 #include <netinet/in.h>
     56 #ifdef __NetBSD__
     57 #include <netinet/if_inarp.h>
     58 #else
     59 #include <netinet/if_ether.h>
     60 #endif
     61 
     62 #include <arpa/inet.h>
     63 
     64 #include <errno.h>
     65 #include <dirent.h>
     66 #include <netdb.h>
     67 #include <stdio.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <syslog.h>
     71 #include <unistd.h>
     72 #include <util.h>
     73 
     74 #define FATAL		1	/* fatal error occurred */
     75 #define NONFATAL	0	/* non fatal error occurred */
     76 
     77 /*
     78  * The structure for each interface.
     79  */
     80 struct if_info {
     81 	int     ii_fd;		/* BPF file descriptor */
     82 	u_char  ii_eaddr[6];	/* Ethernet address of this interface */
     83 	u_int32_t  ii_ipaddr;	/* IP address of this interface */
     84 	u_int32_t  ii_netmask;	/* subnet or net mask */
     85 	char	*ii_name;	/* interface name */
     86 	struct if_info *ii_alias;
     87 	struct if_info *ii_next;
     88 };
     89 /*
     90  * The list of all interfaces that are being listened to.  rarp_loop()
     91  * "selects" on the descriptors in this list.
     92  */
     93 struct if_info *iflist;
     94 
     95 u_int32_t choose_ipaddr __P((u_int32_t **, u_int32_t, u_int32_t));
     96 void	debug __P((const char *,...));
     97 void	init_all __P((void));
     98 void	init_one __P((char *, u_int32_t));
     99 u_int32_t	ipaddrtonetmask __P((u_int32_t));
    100 void	lookup_eaddr __P((char *, u_char *));
    101 void	lookup_ipaddr __P((char *, u_int32_t *, u_int32_t *));
    102 int	main __P((int, char **));
    103 void	rarp_loop __P((void));
    104 int	rarp_open __P((char *));
    105 void	rarp_process __P((struct if_info *, u_char *));
    106 void	rarp_reply __P((struct if_info *, struct ether_header *, u_int32_t,
    107 			struct hostent *));
    108 void	rarperr __P((int, const char *,...));
    109 
    110 #if defined(__NetBSD__)
    111 #include "mkarp.h"
    112 #else
    113 void	update_arptab __P((u_char *, u_int32_t));
    114 #endif
    115 
    116 void	usage __P((void));
    117 
    118 static int	bpf_open __P((void));
    119 static int	rarp_check __P((u_char *, int));
    120 
    121 #ifdef REQUIRE_TFTPBOOT
    122 int	rarp_bootable __P((u_int32_t));
    123 #endif
    124 
    125 int     aflag = 0;		/* listen on "all" interfaces  */
    126 int     dflag = 0;		/* print debugging messages */
    127 int     fflag = 0;		/* don't fork */
    128 int	lflag = 0;		/* log all replies */
    129 
    130 int
    131 main(argc, argv)
    132 	int     argc;
    133 	char  **argv;
    134 {
    135 	extern char *__progname;
    136 
    137 	int     op;
    138 	char   *ifname, *hostname;
    139 
    140 	/* All error reporting is done through syslogs. */
    141 	openlog(__progname, LOG_PID, LOG_DAEMON);
    142 
    143 	opterr = 0;
    144 	while ((op = getopt(argc, argv, "adfl")) != -1) {
    145 		switch (op) {
    146 		case 'a':
    147 			++aflag;
    148 			break;
    149 
    150 		case 'd':
    151 			++dflag;
    152 			break;
    153 
    154 		case 'f':
    155 			++fflag;
    156 			break;
    157 
    158 		case 'l':
    159 			++lflag;
    160 			break;
    161 
    162 		default:
    163 			usage();
    164 			/* NOTREACHED */
    165 		}
    166 	}
    167 	ifname = argv[optind++];
    168 	hostname = ifname ? argv[optind] : 0;
    169 	if ((aflag && ifname) || (!aflag && ifname == 0))
    170 		usage();
    171 
    172 	if (aflag)
    173 		init_all();
    174 	else
    175 		init_one(ifname, INADDR_ANY);
    176 
    177 	if ((!fflag) && (!dflag)) {
    178 		if (daemon(0, 0))
    179 			rarperr(FATAL, "daemon");
    180 		pidfile(NULL);
    181 	}
    182 	rarp_loop();
    183 	/* NOTREACHED */
    184 	return (0);
    185 }
    186 
    187 /*
    188  * Add 'ifname' to the interface list.  Lookup its IP address and network
    189  * mask and Ethernet address, and open a BPF file for it.
    190  */
    191 void
    192 init_one(ifname, ipaddr)
    193 	char   *ifname;
    194 	u_int32_t ipaddr;
    195 {
    196 	struct if_info *h;
    197 	struct if_info *p;
    198 	int fd;
    199 
    200 	for (h = iflist; h != NULL; h = h->ii_next) {
    201 		if (!strcmp(h->ii_name, ifname))
    202 			break;
    203 	}
    204 	if (h == NULL) {
    205 		fd = rarp_open(ifname);
    206 		if (fd < 0)
    207 			return;
    208 	} else {
    209 		fd = h->ii_fd;
    210 	}
    211 
    212 	p = (struct if_info *)malloc(sizeof(*p));
    213 	if (p == 0) {
    214 		rarperr(FATAL, "malloc: %s", strerror(errno));
    215 		/* NOTREACHED */
    216 	}
    217 	p->ii_name = strdup(ifname);
    218 	if (p->ii_name == 0) {
    219 		rarperr(FATAL, "malloc: %s", strerror(errno));
    220 		/* NOTREACHED */
    221 	}
    222 	if (h != NULL) {
    223 		p->ii_alias = h->ii_alias;
    224 		h->ii_alias = p;
    225 	} else {
    226 		p->ii_next = iflist;
    227 		iflist = p;
    228 	}
    229 
    230 	p->ii_fd = fd;
    231 	p->ii_ipaddr = ipaddr;
    232 	lookup_eaddr(ifname, p->ii_eaddr);
    233 	lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask);
    234 }
    235 
    236 /*
    237  * Initialize all "candidate" interfaces that are in the system
    238  * configuration list.  A "candidate" is up, not loopback and not
    239  * point to point.
    240  */
    241 void
    242 init_all()
    243 {
    244 	char inbuf[8192*2];
    245 	struct ifconf ifc;
    246 	struct ifreq ifreq, ifrd, *ifr, *ifrp;
    247 	int fd;
    248 	int i, len;
    249 
    250 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    251 		rarperr(FATAL, "socket: %s", strerror(errno));
    252 		/* NOTREACHED */
    253 	}
    254 
    255 	ifc.ifc_len = sizeof(inbuf);
    256 	ifc.ifc_buf = inbuf;
    257 	if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
    258 	    ifc.ifc_len < sizeof(struct ifreq)) {
    259 		rarperr(FATAL, "init_all: SIOCGIFCONF: %s", strerror(errno));
    260 		/* NOTREACHED */
    261 	}
    262 	ifr = &ifrd;
    263 	ifrp = ifc.ifc_req;
    264 	ifreq.ifr_name[0] = '\0';
    265 	for (i = 0; i < ifc.ifc_len;
    266 	     i += len, ifrp = (struct ifreq *)((caddr_t)ifrp + len)) {
    267 #define SIN(s)	((struct sockaddr_in *) (s))
    268 		memcpy(&ifrd, ifrp, sizeof (ifrd));
    269 		len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
    270 		if (ifr->ifr_addr.sa_family != AF_INET)
    271 			continue;
    272 		if (!strncmp(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name))
    273 		    && SIN(&ifreq.ifr_addr)->sin_addr.s_addr == SIN(&ifr->ifr_addr)->sin_addr.s_addr)
    274 			continue;
    275 		ifreq = *ifr;
    276 		if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
    277 			rarperr(FATAL, "init_all: SIOCGIFFLAGS: %s",
    278 			    strerror(errno));
    279 			/* NOTREACHED */
    280 		}
    281 		if ((ifr->ifr_flags &
    282 		    (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
    283 			continue;
    284 		init_one(ifr->ifr_name, SIN(&ifreq.ifr_addr)->sin_addr.s_addr);
    285 #undef	SIN
    286 	}
    287 	(void)close(fd);
    288 }
    289 
    290 void
    291 usage()
    292 {
    293 	(void) fprintf(stderr, "usage: rarpd -a [-d|-f] [-l]\n");
    294 	(void) fprintf(stderr, "       rarpd [-d|-f] [-l] interface\n");
    295 	exit(1);
    296 }
    297 
    298 static int
    299 bpf_open()
    300 {
    301 	int     fd;
    302 	int     n = 0;
    303 	char    device[sizeof "/dev/bpf000"];
    304 
    305 	/* Go through all the minors and find one that isn't in use. */
    306 	do {
    307 		(void)sprintf(device, "/dev/bpf%d", n++);
    308 		fd = open(device, O_RDWR);
    309 	} while (fd < 0 && errno == EBUSY);
    310 
    311 	if (fd < 0) {
    312 		rarperr(FATAL, "%s: %s", device, strerror(errno));
    313 		/* NOTREACHED */
    314 	}
    315 	return fd;
    316 }
    317 /*
    318  * Open a BPF file and attach it to the interface named 'device'.
    319  * Set immediate mode, and set a filter that accepts only RARP requests.
    320  */
    321 int
    322 rarp_open(device)
    323 	char   *device;
    324 {
    325 	int     fd;
    326 	struct ifreq ifr;
    327 	u_int   dlt;
    328 	int     immediate;
    329 
    330 	static struct bpf_insn insns[] = {
    331 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
    332 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_REVARP, 0, 3),
    333 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
    334 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ARPOP_REVREQUEST, 0, 1),
    335 		BPF_STMT(BPF_RET | BPF_K,
    336 		    sizeof(struct arphdr) +
    337 		    2 * ETHER_ADDR_LEN + 2 * sizeof(struct in_addr) +
    338 		    sizeof(struct ether_header)),
    339 		BPF_STMT(BPF_RET | BPF_K, 0),
    340 	};
    341 	static struct bpf_program filter = {
    342 		sizeof insns / sizeof(insns[0]),
    343 		insns
    344 	};
    345 
    346 	fd = bpf_open();
    347 
    348 	/* Set immediate mode so packets are processed as they arrive. */
    349 	immediate = 1;
    350 	if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
    351 		rarperr(FATAL, "BIOCIMMEDIATE: %s", strerror(errno));
    352 		/* NOTREACHED */
    353 	}
    354 	(void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name - 1);
    355 	ifr.ifr_name[sizeof ifr.ifr_name - 1] = '\0';
    356 	if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
    357 		if (aflag) {	/* for -a skip non-ethernet interfaces */
    358 			close(fd);
    359 			return(-1);
    360 		}
    361 		rarperr(FATAL, "BIOCSETIF: %s", strerror(errno));
    362 		/* NOTREACHED */
    363 	}
    364 	/* Check that the data link layer is an Ethernet; this code won't work
    365 	 * with anything else. */
    366 	if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
    367 		rarperr(FATAL, "BIOCGDLT: %s", strerror(errno));
    368 		/* NOTREACHED */
    369 	}
    370 	if (dlt != DLT_EN10MB) {
    371 		if (aflag) {	/* for -a skip non-ethernet interfaces */
    372 			close(fd);
    373 			return(-1);
    374 		}
    375 		rarperr(FATAL, "%s is not an ethernet", device);
    376 		/* NOTREACHED */
    377 	}
    378 	/* Set filter program. */
    379 	if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
    380 		rarperr(FATAL, "BIOCSETF: %s", strerror(errno));
    381 		/* NOTREACHED */
    382 	}
    383 	return fd;
    384 }
    385 /*
    386  * Perform various sanity checks on the RARP request packet.  Return
    387  * false on failure and log the reason.
    388  */
    389 static int
    390 rarp_check(p, len)
    391 	u_char *p;
    392 	int     len;
    393 {
    394 	struct ether_header *ep = (struct ether_header *) p;
    395 #ifdef __NetBSD__
    396 	struct arphdr *ap = (struct arphdr *) (p + sizeof(*ep));
    397 #else
    398 	struct ether_arp *ap = (struct ether_arp *) (p + sizeof(*ep));
    399 #endif
    400 
    401 	if (dflag)
    402 		fprintf(stderr, "got a packet\n");
    403 
    404 	if (len < sizeof(*ep) + sizeof(*ap)) {
    405 		rarperr(NONFATAL, "truncated request");
    406 		return 0;
    407 	}
    408 #ifdef __NetBSD__
    409 	/* now that we know the fixed part of the ARP hdr is there: */
    410 	if (len < sizeof(*ap) + 2 * ap->ar_hln + 2 * ap->ar_pln) {
    411 		rarperr(NONFATAL, "truncated request");
    412 		return 0;
    413 	}
    414 #endif
    415 	/* XXX This test might be better off broken out... */
    416 #ifdef __FreeBSD__
    417 	/* BPF (incorrectly) returns this in host order. */
    418 	if (ep->ether_type != ETHERTYPE_REVARP ||
    419 #else
    420 	if (ntohs (ep->ether_type) != ETHERTYPE_REVARP ||
    421 #endif
    422 #ifdef __NetBSD__
    423 	    ntohs (ap->ar_hrd) != ARPHRD_ETHER ||
    424 	    ntohs (ap->ar_op) != ARPOP_REVREQUEST ||
    425 	    ntohs (ap->ar_pro) != ETHERTYPE_IP ||
    426 	    ap->ar_hln != 6 || ap->ar_pln != 4) {
    427 #else
    428 	    ntohs (ap->arp_hrd) != ARPHRD_ETHER ||
    429 	    ntohs (ap->arp_op) != ARPOP_REVREQUEST ||
    430 	    ntohs (ap->arp_pro) != ETHERTYPE_IP ||
    431 	    ap->arp_hln != 6 || ap->arp_pln != 4) {
    432 #endif
    433 		rarperr(NONFATAL, "request fails sanity check");
    434 		return 0;
    435 	}
    436 #ifdef __NetBSD__
    437 	if (memcmp((char *) &ep->ether_shost, ar_sha(ap), 6) != 0) {
    438 #else
    439 	if (memcmp((char *) &ep->ether_shost, ap->arp_sha, 6) != 0) {
    440 #endif
    441 		rarperr(NONFATAL, "ether/arp sender address mismatch");
    442 		return 0;
    443 	}
    444 #ifdef __NetBSD__
    445 	if (memcmp(ar_sha(ap), ar_tha(ap), 6) != 0) {
    446 #else
    447 	if (memcmp((char *) &ap->arp_sha, (char *) &ap->arp_tha, 6) != 0) {
    448 #endif
    449 		rarperr(NONFATAL, "ether/arp target address mismatch");
    450 		return 0;
    451 	}
    452 	return 1;
    453 }
    454 
    455 /*
    456  * Loop indefinitely listening for RARP requests on the
    457  * interfaces in 'iflist'.
    458  */
    459 void
    460 rarp_loop()
    461 {
    462 	u_char *buf, *bp, *ep;
    463 	int     cc, fd;
    464 	fd_set  fds, listeners;
    465 	int     bufsize, maxfd = 0;
    466 	struct if_info *ii;
    467 
    468 	if (iflist == 0) {
    469 		rarperr(FATAL, "no interfaces");
    470 		/* NOTREACHED */
    471 	}
    472 	if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
    473 		rarperr(FATAL, "BIOCGBLEN: %s", strerror(errno));
    474 		/* NOTREACHED */
    475 	}
    476 	buf = (u_char *) malloc((unsigned) bufsize);
    477 	if (buf == 0) {
    478 		rarperr(FATAL, "malloc: %s", strerror(errno));
    479 		/* NOTREACHED */
    480 	}
    481 	/*
    482          * Find the highest numbered file descriptor for select().
    483          * Initialize the set of descriptors to listen to.
    484          */
    485 	FD_ZERO(&fds);
    486 	for (ii = iflist; ii; ii = ii->ii_next) {
    487 		FD_SET(ii->ii_fd, &fds);
    488 		if (ii->ii_fd > maxfd)
    489 			maxfd = ii->ii_fd;
    490 	}
    491 	while (1) {
    492 		listeners = fds;
    493 		if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
    494 			(struct fd_set *) 0, (struct timeval *) 0) < 0) {
    495 			rarperr(FATAL, "select: %s", strerror(errno));
    496 			/* NOTREACHED */
    497 		}
    498 		for (ii = iflist; ii; ii = ii->ii_next) {
    499 			fd = ii->ii_fd;
    500 			if (!FD_ISSET(fd, &listeners))
    501 				continue;
    502 		again:
    503 			cc = read(fd, (char *) buf, bufsize);
    504 			/* Don't choke when we get ptraced */
    505 			if (cc < 0 && errno == EINTR)
    506 				goto again;
    507 			/* Due to a SunOS bug, after 2^31 bytes, the file
    508 			 * offset overflows and read fails with EINVAL.  The
    509 			 * lseek() to 0 will fix things. */
    510 			if (cc < 0) {
    511 				if (errno == EINVAL &&
    512 				    (lseek(fd, 0, SEEK_CUR) + bufsize) < 0) {
    513 					(void)lseek(fd, 0, 0);
    514 					goto again;
    515 				}
    516 				rarperr(FATAL, "read: %s", strerror(errno));
    517 				/* NOTREACHED */
    518 			}
    519 			/* Loop through the packet(s) */
    520 #define bhp ((struct bpf_hdr *)bp)
    521 			bp = buf;
    522 			ep = bp + cc;
    523 			while (bp < ep) {
    524 				register int caplen, hdrlen;
    525 
    526 				caplen = bhp->bh_caplen;
    527 				hdrlen = bhp->bh_hdrlen;
    528 				if (rarp_check(bp + hdrlen, caplen))
    529 					rarp_process(ii, bp + hdrlen);
    530 				bp += BPF_WORDALIGN(hdrlen + caplen);
    531 			}
    532 		}
    533 	}
    534 }
    535 
    536 #ifdef REQUIRE_TFTPBOOT
    537 
    538 #ifndef TFTP_DIR
    539 #define TFTP_DIR "/tftpboot"
    540 #endif
    541 
    542 /*
    543  * True if this server can boot the host whose IP address is 'addr'.
    544  * This check is made by looking in the tftp directory for the
    545  * configuration file.
    546  */
    547 int
    548 rarp_bootable(addr)
    549 	u_int32_t  addr;
    550 {
    551 	register struct dirent *dent;
    552 	register DIR *d;
    553 	char    ipname[9];
    554 	static DIR *dd = 0;
    555 
    556 	(void)sprintf(ipname, "%08X", addr);
    557 	/* If directory is already open, rewind it.  Otherwise, open it. */
    558 	if (d = dd)
    559 		rewinddir(d);
    560 	else {
    561 		if (chdir(TFTP_DIR) == -1) {
    562 			rarperr(FATAL, "chdir: %s", strerror(errno));
    563 			/* NOTREACHED */
    564 		}
    565 		d = opendir(".");
    566 		if (d == 0) {
    567 			rarperr(FATAL, "opendir: %s", strerror(errno));
    568 			/* NOTREACHED */
    569 		}
    570 		dd = d;
    571 	}
    572 	while (dent = readdir(d))
    573 		if (strncmp(dent->d_name, ipname, 8) == 0)
    574 			return 1;
    575 	return 0;
    576 }
    577 #endif /* REQUIRE_TFTPBOOT */
    578 
    579 /*
    580  * Given a list of IP addresses, 'alist', return the first address that
    581  * is on network 'net'; 'netmask' is a mask indicating the network portion
    582  * of the address.
    583  */
    584 u_int32_t
    585 choose_ipaddr(alist, net, netmask)
    586 	u_int32_t **alist;
    587 	u_int32_t net;
    588 	u_int32_t netmask;
    589 {
    590 
    591 	for (; *alist; ++alist) {
    592 		if ((**alist & netmask) == net)
    593 			return **alist;
    594 	}
    595 	return 0;
    596 }
    597 /*
    598  * Answer the RARP request in 'pkt', on the interface 'ii'.  'pkt' has
    599  * already been checked for validity.  The reply is overlaid on the request.
    600  */
    601 void
    602 rarp_process(ii, pkt)
    603 	struct if_info *ii;
    604 	u_char *pkt;
    605 {
    606 	struct ether_header *ep;
    607 	struct hostent *hp;
    608 	u_int32_t  target_ipaddr = 0;
    609 	char    ename[MAXHOSTNAMELEN + 1];
    610 	struct	in_addr in;
    611 
    612 	ep = (struct ether_header *) pkt;
    613 
    614 	if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
    615 		debug("no IP address for %s",
    616 		    ether_ntoa((struct ether_addr *)&ep->ether_shost));
    617 		return;
    618 	}
    619 	ename[sizeof(ename)-1] = '\0';
    620 
    621 	if ((hp = gethostbyname(ename)) == 0) {
    622 		debug("gethostbyname(%s) failed: %s", ename,
    623 		    hstrerror(h_errno));
    624 		return;
    625 	}
    626 
    627 	/* Choose correct address from list. */
    628 	if (hp->h_addrtype != AF_INET) {
    629 		rarperr(FATAL, "cannot handle non IP addresses");
    630 		/* NOTREACHED */
    631 	}
    632 	for (;; ii = ii->ii_alias) {
    633 		target_ipaddr = choose_ipaddr((u_int32_t **) hp->h_addr_list,
    634 		    ii->ii_ipaddr & ii->ii_netmask, ii->ii_netmask);
    635 		if (target_ipaddr != 0)
    636 			break;
    637 		if (ii->ii_alias == NULL)
    638 			break;
    639 	}
    640 
    641 	if (target_ipaddr == 0) {
    642 		in.s_addr = ii->ii_ipaddr & ii->ii_netmask;
    643 		rarperr(NONFATAL, "cannot find %s on net %s",
    644 		    ename, inet_ntoa(in));
    645 		return;
    646 	}
    647 #ifdef REQUIRE_TFTPBOOT
    648 	if (rarp_bootable(htonl(target_ipaddr)))
    649 #endif
    650 		rarp_reply(ii, ep, target_ipaddr, hp);
    651 #ifdef REQUIRE_TFTPBOOT
    652 	else
    653 		debug("%08X not bootable", htonl(target_ipaddr));
    654 #endif
    655 }
    656 /*
    657  * Lookup the ethernet address of the interface attached to the BPF
    658  * file descriptor 'fd'; return it in 'eaddr'.
    659  */
    660 void
    661 lookup_eaddr(ifname, eaddr)
    662 	char *ifname;
    663 	u_char *eaddr;
    664 {
    665 	char inbuf[8192*2];
    666 	struct ifconf ifc;
    667 	struct ifreq *ifr;
    668 	struct sockaddr_dl *sdl;
    669 	int fd;
    670 	int i, len;
    671 
    672 	/* We cannot use SIOCGIFADDR on the BPF descriptor.
    673 	   We must instead get all the interfaces with SIOCGIFCONF
    674 	   and find the right one.  */
    675 
    676 	/* Use datagram socket to get Ethernet address. */
    677 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    678 		rarperr(FATAL, "socket: %s", strerror(errno));
    679 		/* NOTREACHED */
    680 	}
    681 
    682 	ifc.ifc_len = sizeof(inbuf);
    683 	ifc.ifc_buf = inbuf;
    684 	if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
    685 	    ifc.ifc_len < sizeof(struct ifreq)) {
    686 		rarperr(FATAL, "lookup_eaddr: SIOGIFCONF: %s", strerror(errno));
    687 		/* NOTREACHED */
    688 	}
    689 	ifr = ifc.ifc_req;
    690 	for (i = 0; i < ifc.ifc_len;
    691 	     i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
    692 		len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
    693 		sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
    694 		if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
    695 		    sdl->sdl_alen != 6)
    696 			continue;
    697 		if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
    698 			memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
    699 			debug("%s: %x:%x:%x:%x:%x:%x",
    700 			    ifr->ifr_name, eaddr[0], eaddr[1],
    701 			    eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
    702 			return;
    703 		}
    704 	}
    705 
    706 	rarperr(FATAL, "lookup_eaddr: Never saw interface `%s'!", ifname);
    707 }
    708 /*
    709  * Lookup the IP address and network mask of the interface named 'ifname'.
    710  */
    711 void
    712 lookup_ipaddr(ifname, addrp, netmaskp)
    713 	char   *ifname;
    714 	u_int32_t *addrp;
    715 	u_int32_t *netmaskp;
    716 {
    717 	int     fd;
    718 
    719 	/* Use datagram socket to get IP address. */
    720 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    721 		rarperr(FATAL, "socket: %s", strerror(errno));
    722 		/* NOTREACHED */
    723 	}
    724 	if (*addrp == INADDR_ANY) {
    725 		struct ifreq ifr;
    726 		memset(&ifr, 0, sizeof(ifr));
    727 		(void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
    728 		if (ioctl(fd, SIOCGIFADDR, (char *) &ifr) < 0) {
    729 			rarperr(FATAL, "SIOCGIFADDR: %s", strerror(errno));
    730 			/* NOTREACHED */
    731 		}
    732 		*addrp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
    733 		if (ioctl(fd, SIOCGIFNETMASK, (char *) &ifr) < 0) {
    734 			perror("SIOCGIFNETMASK");
    735 			exit(1);
    736 		}
    737 		*netmaskp = ((struct sockaddr_in *) & ifr.ifr_addr)->sin_addr.s_addr;
    738 	} else {
    739 		struct ifaliasreq ifra;
    740 		memset(&ifra, 0, sizeof(ifra));
    741 		(void)strncpy(ifra.ifra_name, ifname, sizeof ifra.ifra_name);
    742 		((struct sockaddr_in *) & ifra.ifra_addr)->sin_family = AF_INET;
    743 		((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr = *addrp;
    744 		if (ioctl(fd, SIOCGIFALIAS, (char *) &ifra) < 0) {
    745 			rarperr(FATAL, "SIOCGIFALIAS: %s", strerror(errno));
    746 			/* NOTREACHED */
    747 		}
    748 		*addrp = ((struct sockaddr_in *) & ifra.ifra_addr)->sin_addr.s_addr;
    749 		*netmaskp = ((struct sockaddr_in *) & ifra.ifra_mask)->sin_addr.s_addr;
    750 	}
    751 	/* If SIOCGIFNETMASK didn't work, figure out a mask from the IP
    752 	 * address class. */
    753 	if (*netmaskp == 0)
    754 		*netmaskp = ipaddrtonetmask(*addrp);
    755 
    756 	(void)close(fd);
    757 }
    758 /*
    759  * Poke the kernel arp tables with the ethernet/ip address combinataion
    760  * given.  When processing a reply, we must do this so that the booting
    761  * host (i.e. the guy running rarpd), won't try to ARP for the hardware
    762  * address of the guy being booted (he cannot answer the ARP).
    763  */
    764 #ifndef __NetBSD__
    765 void
    766 update_arptab(ep, ipaddr)
    767 	u_char *ep;
    768 	u_int32_t  ipaddr;
    769 {
    770 	struct arpreq request;
    771 	struct sockaddr_in *sin;
    772 
    773 	request.arp_flags = 0;
    774 	sin = (struct sockaddr_in *) & request.arp_pa;
    775 	sin->sin_family = AF_INET;
    776 	sin->sin_addr.s_addr = ipaddr;
    777 	request.arp_ha.sa_family = AF_UNSPEC;
    778 	/* This is needed #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN,
    779 	   because AF_UNSPEC is zero and the kernel assumes that a zero
    780 	   sa_family means that the real sa_family value is in sa_len.  */
    781 	request.arp_ha.sa_len = 16; /* XXX */
    782 	memmove((char *) request.arp_ha.sa_data, (char *)ep, 6);
    783 
    784 #if 0
    785 	s = socket(AF_INET, SOCK_DGRAM, 0);
    786 	if (ioctl(s, SIOCSARP, (caddr_t) & request) < 0) {
    787 		rarperr(NONFATAL, "SIOCSARP: %s", strerror(errno));
    788 	}
    789 	(void)close(s);
    790 #endif
    791 }
    792 #endif
    793 
    794 /*
    795  * Build a reverse ARP packet and sent it out on the interface.
    796  * 'ep' points to a valid ARPOP_REVREQUEST.  The ARPOP_REVREPLY is built
    797  * on top of the request, then written to the network.
    798  *
    799  * RFC 903 defines the ether_arp fields as follows.  The following comments
    800  * are taken (more or less) straight from this document.
    801  *
    802  * ARPOP_REVREQUEST
    803  *
    804  * arp_sha is the hardware address of the sender of the packet.
    805  * arp_spa is undefined.
    806  * arp_tha is the 'target' hardware address.
    807  *   In the case where the sender wishes to determine his own
    808  *   protocol address, this, like arp_sha, will be the hardware
    809  *   address of the sender.
    810  * arp_tpa is undefined.
    811  *
    812  * ARPOP_REVREPLY
    813  *
    814  * arp_sha is the hardware address of the responder (the sender of the
    815  *   reply packet).
    816  * arp_spa is the protocol address of the responder (see the note below).
    817  * arp_tha is the hardware address of the target, and should be the same as
    818  *   that which was given in the request.
    819  * arp_tpa is the protocol address of the target, that is, the desired address.
    820  *
    821  * Note that the requirement that arp_spa be filled in with the responder's
    822  * protocol is purely for convenience.  For instance, if a system were to use
    823  * both ARP and RARP, then the inclusion of the valid protocol-hardware
    824  * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
    825  * ARP request.
    826  */
    827 void
    828 rarp_reply(ii, ep, ipaddr, hp)
    829 	struct if_info *ii;
    830 	struct ether_header *ep;
    831 	u_int32_t  ipaddr;
    832 	struct hostent *hp;
    833 {
    834 	int     n;
    835 #ifdef __NetBSD__
    836 	struct arphdr *ap = (struct arphdr *) (ep + 1);
    837 #else
    838 	struct ether_arp *ap = (struct ether_arp *) (ep + 1);
    839 #endif
    840 
    841 	int     len;
    842 
    843 #ifdef __NetBSD__
    844 	(void)mkarp(ar_sha(ap), ipaddr);
    845 #else
    846 	update_arptab((u_char *) & ap->arp_sha, ipaddr);
    847 #endif
    848 
    849 	/* Build the rarp reply by modifying the rarp request in place. */
    850 #ifdef __FreeBSD__
    851 	/* BPF (incorrectly) wants this in host order. */
    852 	ep->ether_type = ETHERTYPE_REVARP;
    853 #else
    854 	ep->ether_type = htons(ETHERTYPE_REVARP);
    855 #endif
    856 #ifdef __NetBSD__
    857 	ap->ar_hrd = htons(ARPHRD_ETHER);
    858 	ap->ar_pro = htons(ETHERTYPE_IP);
    859 	ap->ar_op = htons(ARPOP_REVREPLY);
    860 
    861 	memmove((char *) &ep->ether_dhost, ar_sha(ap), 6);
    862 	memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
    863 	memmove(ar_sha(ap), (char *) ii->ii_eaddr, 6);
    864 
    865 	memmove(ar_tpa(ap), (char *) &ipaddr, 4);
    866 	/* Target hardware is unchanged. */
    867 	memmove(ar_spa(ap), (char *) &ii->ii_ipaddr, 4);
    868 
    869 	len = sizeof(*ep) + sizeof(*ap) +
    870 	    2 * ap->ar_pln + 2 * ap->ar_hln;
    871 #else
    872 	ap->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
    873 	ap->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
    874 	ap->arp_op = htons(ARPOP_REVREPLY);
    875 
    876 	memmove((char *) &ep->ether_dhost, (char *) &ap->arp_sha, 6);
    877 	memmove((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
    878 	memmove((char *) &ap->arp_sha, (char *) ii->ii_eaddr, 6);
    879 
    880 	memmove((char *) ap->arp_tpa, (char *) &ipaddr, 4);
    881 	/* Target hardware is unchanged. */
    882 	memmove((char *) ap->arp_spa, (char *) &ii->ii_ipaddr, 4);
    883 
    884 	len = sizeof(*ep) + sizeof(*ap);
    885 #endif
    886 
    887 	if (lflag)
    888 		syslog(LOG_INFO, "%s asked; %s replied", hp->h_name,
    889 		    ether_ntoa((struct ether_addr *)ar_tha(ap)));
    890 	n = write(ii->ii_fd, (char *) ep, len);
    891 	if (n != len) {
    892 		rarperr(NONFATAL, "write: only %d of %d bytes written", n, len);
    893 	}
    894 }
    895 /*
    896  * Get the netmask of an IP address.  This routine is used if
    897  * SIOCGIFNETMASK doesn't work.
    898  */
    899 u_int32_t
    900 ipaddrtonetmask(addr)
    901 	u_int32_t  addr;
    902 {
    903 
    904 	if (IN_CLASSA(addr))
    905 		return IN_CLASSA_NET;
    906 	if (IN_CLASSB(addr))
    907 		return IN_CLASSB_NET;
    908 	if (IN_CLASSC(addr))
    909 		return IN_CLASSC_NET;
    910 	rarperr(FATAL, "unknown IP address class: %08X", addr);
    911 	/* NOTREACHED */
    912 	return(-1);
    913 }
    914 
    915 #if __STDC__
    916 #include <stdarg.h>
    917 #else
    918 #include <varargs.h>
    919 #endif
    920 
    921 void
    922 #if __STDC__
    923 rarperr(int fatal, const char *fmt,...)
    924 #else
    925 rarperr(fmt, va_alist)
    926 	int     fatal;
    927 	char   *fmt;
    928 va_dcl
    929 #endif
    930 {
    931 	va_list ap;
    932 
    933 #if __STDC__
    934 	va_start(ap, fmt);
    935 #else
    936 	va_start(ap);
    937 #endif
    938 	if (dflag) {
    939 		if (fatal)
    940 			(void)fprintf(stderr, "rarpd: error: ");
    941 		else
    942 			(void)fprintf(stderr, "rarpd: warning: ");
    943 		(void)vfprintf(stderr, fmt, ap);
    944 		(void)fprintf(stderr, "\n");
    945 	}
    946 	vsyslog(LOG_ERR, fmt, ap);
    947 	va_end(ap);
    948 	if (fatal)
    949 		exit(1);
    950 	/* NOTREACHED */
    951 }
    952 
    953 void
    954 #if __STDC__
    955 debug(const char *fmt,...)
    956 #else
    957 debug(fmt, va_alist)
    958 	char   *fmt;
    959 va_dcl
    960 #endif
    961 {
    962 	va_list ap;
    963 
    964 #if __STDC__
    965 	va_start(ap, fmt);
    966 #else
    967 	va_start(ap);
    968 #endif
    969 	if (dflag) {
    970 		(void)fprintf(stderr, "rarpd: ");
    971 		(void)vfprintf(stderr, fmt, ap);
    972 		(void)fprintf(stderr, "\n");
    973 	}
    974 	vsyslog(LOG_WARNING, fmt, ap);
    975 	va_end(ap);
    976 }
    977