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