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