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