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