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