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