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