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