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