rarpd.c revision 17817
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
22char 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
28static char rcsid[] =
29    "@(#) $Header: /home/ncvs/src/usr.sbin/rarpd/rarpd.c,v 1.5 1995/07/18 21:35:32 wpaul Exp $ (LBL)";
30#endif
31
32
33/*
34 * rarpd - Reverse ARP Daemon
35 *
36 * Usage:	rarpd -a [ -f ] [ hostname ]
37 *		rarpd [ -f ] interface [ hostname ]
38 *
39 * 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
40 * Currently, the argument is ignored.
41 */
42
43#include <stdio.h>
44#include <syslog.h>
45#include <string.h>
46#include <strings.h>
47#include <sys/types.h>
48/* SunOS 4.x defines this while 3.x does not. */
49#ifdef __sys_types_h
50#define SUNOS4
51#endif
52#include <sys/time.h>
53#include <net/bpf.h>
54#include <sys/socket.h>
55#include <sys/ioctl.h>
56#include <net/if.h>
57#include <netinet/in.h>
58#include <netinet/if_ether.h>
59#include <sys/errno.h>
60#include <sys/file.h>
61#include <netdb.h>
62
63#ifdef SUNOS4
64#include <dirent.h>
65#else
66#include <sys/dir.h>
67#endif
68
69/*
70 * Map field names in ether_arp struct.  What a pain in the neck.
71 */
72#if !defined(SUNOS4) && !defined(__FreeBSD__)
73#undef arp_sha
74#undef arp_spa
75#undef arp_tha
76#undef arp_tpa
77#define arp_sha arp_xsha
78#define arp_spa arp_xspa
79#define arp_tha arp_xtha
80#define arp_tpa arp_xtpa
81#endif
82
83#ifndef __GNUC__
84#define inline
85#endif
86
87extern int errno;
88extern int ether_ntohost __P((char *, struct ether_addr *));
89
90/*
91 * The structure for each interface.
92 */
93struct if_info {
94	int 	ii_fd;		/* BPF file descriptor */
95	u_char	ii_eaddr[6];	/* Ethernet address of this interface */
96	u_long	ii_ipaddr;	/* IP address of this interface */
97	u_long	ii_netmask;	/* subnet or net mask */
98	struct if_info *ii_next;
99};
100
101/*
102 * The list of all interfaces that are being listened to.  rarp_loop()
103 * "selects" on the descriptors in this list.
104 */
105struct if_info *iflist;
106
107extern char *malloc();
108extern void exit();
109
110u_long ipaddrtonetmask();
111void init_one();
112void init_all();
113void rarp_loop();
114void lookup_eaddr();
115void lookup_ipaddr();
116
117void
118main(argc, argv)
119	int argc;
120	char **argv;
121{
122	int op, pid;
123	char *ifname, *hostname, *name;
124
125	int aflag = 0;		/* listen on "all" interfaces  */
126	int fflag = 0;		/* don't fork */
127
128	extern char *optarg;
129	extern int optind, opterr;
130
131	if (name = strrchr(argv[0], '/'))
132		++name;
133	else
134		name = argv[0];
135	if (*name == '-')
136		++name;
137
138	/*
139	 * All error reporting is done through syslogs.
140	 */
141	openlog(name, LOG_PID, LOG_DAEMON);
142
143	opterr = 0;
144	while ((op = getopt(argc, argv, "af")) != EOF) {
145		switch (op) {
146		case 'a':
147			++aflag;
148			break;
149
150		case 'f':
151			++fflag;
152			break;
153
154		default:
155			usage();
156			/* NOTREACHED */
157		}
158	}
159	ifname = argv[optind++];
160	hostname =  ifname ? argv[optind] : 0;
161	if ((aflag && ifname) || (!aflag && ifname == 0))
162		usage();
163
164	if (aflag)
165		init_all();
166	else
167		init_one(ifname);
168
169	if (!fflag)
170		if (daemon(0,0)) {
171			perror("fork");
172			exit(0);
173		}
174	rarp_loop();
175}
176
177/*
178 * Add 'ifname' to the interface list.  Lookup its IP address and network
179 * mask and Ethernet address, and open a BPF file for it.
180 */
181void
182init_one(ifname)
183	char *ifname;
184{
185	struct if_info *p;
186
187
188	p = (struct if_info *)malloc(sizeof(*p));
189	p->ii_next = iflist;
190	iflist = p;
191
192	p->ii_fd = rarp_open(ifname);
193	lookup_eaddr(p->ii_fd, p->ii_eaddr);
194	lookup_ipaddr(ifname, &p->ii_ipaddr, &p->ii_netmask);
195}
196
197/*
198 * Initialize all "candidate" interfaces that are in the system
199 * configuration list.  A "candidate" is up, not loopback and not
200 * point to point.
201 */
202void
203init_all()
204{
205	int fd;
206	int ifflags;
207	struct ifreq ibuf[8], tmp_ibuf, *ifptr, *n;
208	struct ifconf ifc;
209
210	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
211		syslog(LOG_ERR, "socket: %m");
212		exit(1);
213	}
214	ifc.ifc_len = sizeof ibuf;
215	ifc.ifc_buf = (caddr_t)ibuf;
216	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
217	    ifc.ifc_len < sizeof(struct ifreq)) {
218		syslog(LOG_ERR, "SIOCGIFCONF: %m");
219		exit(1);
220	}
221	ifptr = ifc.ifc_req;
222	ifflags = ifptr->ifr_flags;
223	n = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
224	while (ifptr < n) {
225		bcopy((char *)ifptr, (char *)&tmp_ibuf, sizeof(struct ifreq));
226		if (ioctl(fd, SIOCGIFFLAGS, (char *)&tmp_ibuf) < 0) {
227			syslog(LOG_ERR, "SIOCGIFFLAGS: %m");
228			exit(1);
229		}
230		if (ifptr->ifr_flags == ifflags && (tmp_ibuf.ifr_flags &
231			(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) == IFF_UP)
232			init_one(ifptr->ifr_name);
233		if(ifptr->ifr_addr.sa_len)	/* Dohw! */
234			ifptr = (struct ifreq *) ((caddr_t) ifptr +
235			ifptr->ifr_addr.sa_len -
236			sizeof(struct sockaddr));
237		ifptr++;
238	}
239	(void)close(fd);
240}
241
242usage()
243{
244	(void)fprintf(stderr, "usage: rarpd [ -af ] [ interface ]\n");
245	exit(1);
246}
247
248static int
249bpf_open()
250{
251	int fd;
252	int n = 0;
253	char device[sizeof "/dev/bpf000"];
254
255	/*
256	 * Go through all the minors and find one that isn't in use.
257	 */
258	do {
259		(void)sprintf(device, "/dev/bpf%d", n++);
260		fd = open(device, O_RDWR);
261	} while (fd < 0 && errno == EBUSY);
262
263	if (fd < 0) {
264		syslog(LOG_ERR, "%s: %m", device);
265		exit(-1);
266	}
267	return fd;
268}
269
270/*
271 * Open a BPF file and attach it to the interface named 'device'.
272 * Set immediate mode, and set a filter that accepts only RARP requests.
273 */
274int
275rarp_open(device)
276	char *device;
277{
278	int fd;
279	struct ifreq ifr;
280	int immediate, link_type;
281
282	static struct bpf_insn insns[] = {
283                BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
284                BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3),
285                BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
286                BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARPOP_REVREQUEST, 0, 1),
287                BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) +
288                                sizeof(struct ether_header)),
289                BPF_STMT(BPF_RET+BPF_K, 0),
290        };
291
292        static struct bpf_program filter = {
293                sizeof insns / sizeof(insns[0]),
294                (struct bpf_insn *)&insns
295        };
296
297	fd = bpf_open();
298	/*
299	 * Set immediate mode so packets are processed as they arrive.
300	 */
301	immediate = 1;
302	if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
303		syslog(LOG_ERR, "BIOCIMMEDIATE: %m");
304		exit(1);
305	}
306	(void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
307	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
308		syslog(LOG_ERR, "BIOCSETIF: %m");
309		exit(1);
310	}
311	/*
312	 * Check that the data link layer is an Ethernet; this code won't
313	 * work with anything else.
314	 */
315	if (ioctl(fd, BIOCGDLT, &link_type) < 0) {
316		syslog(LOG_ERR, "BIOCGDLP: %m");
317		exit(1);
318	}
319	if (link_type != DLT_EN10MB) {
320		syslog(LOG_ERR, "%s not on ethernet", device);
321		exit(1);
322	}
323	/*
324	 * Set filter program.
325	 */
326	if (ioctl(fd, BIOCSETF, (caddr_t)&filter) < 0) {
327		syslog(LOG_ERR, "BIOCSETF: %m");
328		exit(1);
329	}
330	return fd;
331}
332
333/*
334 * Perform various sanity checks on the RARP request packet.  Return
335 * false on failure and log the reason.
336 */
337static int
338rarp_check(p, len)
339	u_char *p;
340	int len;
341{
342	struct ether_header *ep = (struct ether_header *)p;
343	struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep));
344
345	if (len < sizeof(*ep) + sizeof(*ap)) {
346		syslog(LOG_ERR, "truncated request");
347		return 0;
348	}
349	/*
350	 * XXX This test might be better off broken out...
351	 */
352	if (ep->ether_type != htons(ETHERTYPE_REVARP) ||
353	    ap->arp_hrd != htons(ARPHRD_ETHER) ||
354	    ap->arp_op != htons(ARPOP_REVREQUEST) ||
355	    ap->arp_pro != htons(ETHERTYPE_IP) ||
356	    ap->arp_hln != 6 || ap->arp_pln != 4) {
357		syslog(LOG_DEBUG, "request fails sanity check");
358		return 0;
359	}
360	if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) {
361		syslog(LOG_DEBUG, "ether/arp sender address mismatch");
362		return 0;
363	}
364	if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) {
365		syslog(LOG_DEBUG, "ether/arp target address mismatch");
366		return 0;
367	}
368	return 1;
369}
370
371#ifndef FD_SETSIZE
372#define FD_SET(n, fdp) ((fdp)->fds_bits[0] |= (1 << (n)))
373#define FD_ISSET(n, fdp) ((fdp)->fds_bits[0] & (1 << (n)))
374#define FD_ZERO(fdp) ((fdp)->fds_bits[0] = 0)
375#endif
376
377/*
378 * Loop indefinitely listening for RARP requests on the
379 * interfaces in 'iflist'.
380 */
381void
382rarp_loop()
383{
384	struct bpf_hdr *bhp;
385	u_char *pkt;
386	int cc, fd;
387	fd_set fds, listeners;
388	int bufsize, maxfd = 0;
389	struct if_info *ii;
390
391	if (iflist == 0) {
392		syslog(LOG_ERR, "no interfaces");
393		exit(1);
394	}
395	if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) < 0) {
396		syslog(LOG_ERR, "BIOCGBLEN: %m");
397		exit(1);
398	}
399	bhp = (struct bpf_hdr *)malloc((unsigned)bufsize);
400
401	/*
402	 * Find the highest numbered file descriptor for select().
403	 * Initialize the set of descriptors to listen to.
404	 */
405	FD_ZERO(&fds);
406	for (ii = iflist; ii; ii = ii->ii_next) {
407		FD_SET(ii->ii_fd, &fds);
408		if (ii->ii_fd > maxfd)
409			maxfd = ii->ii_fd;
410	}
411	while (1) {
412		listeners = fds;
413		if (select(maxfd + 1, &listeners, (struct fd_set *)0,
414			   (struct fd_set *)0, (struct timeval *)0) < 0) {
415			syslog(LOG_ERR, "select: %m");
416			exit(1);
417		}
418		for (ii = iflist; ii; ii = ii->ii_next) {
419			fd = ii->ii_fd;
420			if (FD_ISSET(fd, &listeners)) {
421			again:
422				cc = read(fd, (char *)bhp, bufsize);
423				/*
424				 * Due to a SunOS bug, after 2^31 bytes, the
425				 * file offset overflows and read fails with
426				 * EINVAL.  The lseek() to 0 will fix things.
427				 */
428				if (cc < 0) {
429					if (errno == EINVAL &&
430					    (long)(lseek(fd, 0L, SEEK_CUR) + bufsize) < 0) {
431						(void)lseek(fd, 0, 0);
432						goto again;
433					}
434					syslog(LOG_ERR, "read: %m");
435					exit(1);
436				}
437				pkt = (u_char *)bhp + bhp->bh_hdrlen;
438
439				if (rarp_check(pkt, (int)bhp->bh_datalen))
440					rarp_process(ii, pkt);
441			}
442		}
443	}
444}
445
446#ifndef TFTP_DIR
447#define TFTP_DIR "/tftpboot"
448#endif
449
450/*
451 * True if this server can boot the host whose IP address is 'addr'.
452 * This check is made by looking in the tftp directory for the
453 * configuration file.
454 */
455rarp_bootable(addr)
456	u_long addr;
457{
458
459#ifdef SUNOS4
460	register struct dirent *dent;
461#else
462	register struct direct *dent;
463#endif
464	register DIR *d;
465	char ipname[9];
466	static DIR *dd = 0;
467
468	/*
469	 * XXX   Need to htonl() the IP address or it'll
470	 * come out backwards.
471	 */
472	(void)sprintf(ipname, "%08X", htonl(addr));
473	/*
474	 * If directory is already open, rewind it.  Otherwise, open it.
475	 */
476	if (d = dd)
477		rewinddir(d);
478	else {
479		if (chdir(TFTP_DIR) == -1) {
480			syslog(LOG_ERR, "chdir: %m");
481			exit(1);
482		}
483		d = opendir(".");
484		if (d == 0) {
485			syslog(LOG_ERR, "opendir: %m");
486			exit(1);
487		}
488		dd = d;
489	}
490	while (dent = readdir(d))
491		if (strncmp(dent->d_name, ipname, 8) == 0)
492			return 1;
493	return 0;
494
495}
496
497/*
498 * Given a list of IP addresses, 'alist', return the first address that
499 * is on network 'net'; 'netmask' is a mask indicating the network portion
500 * of the address.
501 */
502u_long
503choose_ipaddr(alist, net, netmask)
504	u_long **alist;
505	u_long net;
506	u_long netmask;
507{
508	for (; *alist; ++alist) {
509		if ((**alist & netmask) == net)
510			return **alist;
511	}
512	return 0;
513}
514
515/*
516 * A one entry ip/ethernet address cache.
517 */
518static u_long cache_ipaddr;
519static u_char cache_eaddr[6];
520
521/*
522 * Answer the RARP request in 'pkt', on the interface 'ii'.  'pkt' has
523 * already been checked for validity.  The reply is overlaid on the request.
524 */
525rarp_process(ii, pkt)
526	struct if_info *ii;
527	u_char *pkt;
528{
529	struct ether_header *ep;
530	struct hostent *hp;
531	u_long target_ipaddr;
532	char ename[256];
533
534	ep = (struct ether_header *)pkt;
535	/*
536	 * If the address in the one element cache, don't bother
537	 * looking up names.
538	 */
539	if (bcmp((char *)cache_eaddr, (char *)&ep->ether_shost, 6) == 0)
540		target_ipaddr = cache_ipaddr;
541	else {
542		if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0 ||
543		    (hp = gethostbyname(ename)) == 0)
544			return;
545		/*
546		 * Choose correct address from list.
547		 */
548		if (hp->h_addrtype != AF_INET) {
549			syslog(LOG_ERR, "cannot handle non IP addresses");
550			exit(1);
551		}
552		target_ipaddr = choose_ipaddr((u_long **)hp->h_addr_list,
553					      ii->ii_ipaddr & ii->ii_netmask,
554					      ii->ii_netmask);
555		if (target_ipaddr == 0) {
556			syslog(LOG_ERR, "cannot find %s on %08x",
557			       ename, ii->ii_ipaddr & ii->ii_netmask);
558			return;
559		}
560		bcopy((char *)&ep->ether_shost, (char *)cache_eaddr, 6);
561		cache_ipaddr = target_ipaddr;
562	}
563	if (rarp_bootable(target_ipaddr))
564		rarp_reply(ii, ep, target_ipaddr);
565}
566
567/*
568 * Lookup the ethernet address of the interface attached to the BPF
569 * file descriptor 'fd'; return it in 'eaddr'.
570 */
571void
572lookup_eaddr(fd, eaddr)
573	int fd;
574	u_char *eaddr;
575{
576	struct ifreq ifr;
577
578	/* Use BPF descriptor to get ethernet address. */
579	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
580		syslog(LOG_ERR, "SIOCGIFADDR: %m");
581		exit(1);
582	}
583	bcopy((char *)&ifr.ifr_addr.sa_data[0], (char *)eaddr, 6);
584}
585
586/*
587 * Lookup the IP address and network mask of the interface named 'ifname'.
588 */
589void
590lookup_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 data gram socket to get IP address. */
599	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
600		syslog(LOG_ERR, "socket: %m");
601		exit(1);
602	}
603	(void)strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
604	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
605		syslog(LOG_ERR, "SIOCGIFADDR: %m");
606		exit(1);
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	/*
615	 * If SIOCGIFNETMASK didn't work, figure out a mask from
616	 * the IP address class.
617	 */
618	if (*netmaskp == 0)
619		*netmaskp = ipaddrtonetmask(*addrp);
620
621	(void)close(fd);
622}
623
624/*
625 * Poke the kernel arp tables with the ethernet/ip address combinataion
626 * given.  When processing a reply, we must do this so that the booting
627 * host (i.e. the guy running rarpd), won't try to ARP for the hardware
628 * address of the guy being booted (he cannot answer the ARP).
629 */
630update_arptab(ep, ipaddr)
631	u_char *ep;
632	u_long ipaddr;
633{
634#ifdef SIOCSARP
635	int s;
636	struct arpreq request;
637	struct sockaddr_in *sin;
638
639	request.arp_flags = 0;
640	sin = (struct sockaddr_in *)&request.arp_pa;
641	sin->sin_family = AF_INET;
642	sin->sin_addr.s_addr = ipaddr;
643	request.arp_ha.sa_family = AF_UNSPEC;
644	bcopy((char *)ep, (char *)request.arp_ha.sa_data, 6);
645
646	s = socket(AF_INET, SOCK_DGRAM, 0);
647	if (ioctl(s, SIOCSARP, (caddr_t)&request) < 0)
648		syslog(LOG_ERR, "SIOCSARP: %m");
649	(void)close(s);
650#else
651	if (arptab_set(ep, ipaddr) > 0)
652		syslog(LOG_ERR, "couldn't update arp table");
653#endif
654}
655
656/*
657 * Build a reverse ARP packet and sent it out on the interface.
658 * 'ep' points to a valid ARPOP_REVREQUEST.  The ARPOP_REVREPLY is built
659 * on top of the request, then written to the network.
660 *
661 * RFC 903 defines the ether_arp fields as follows.  The following comments
662 * are taken (more or less) straight from this document.
663 *
664 * ARPOP_REVREQUEST
665 *
666 * arp_sha is the hardware address of the sender of the packet.
667 * arp_spa is undefined.
668 * arp_tha is the 'target' hardware address.
669 *   In the case where the sender wishes to determine his own
670 *   protocol address, this, like arp_sha, will be the hardware
671 *   address of the sender.
672 * arp_tpa is undefined.
673 *
674 * ARPOP_REVREPLY
675 *
676 * arp_sha is the hardware address of the responder (the sender of the
677 *   reply packet).
678 * arp_spa is the protocol address of the responder (see the note below).
679 * arp_tha is the hardware address of the target, and should be the same as
680 *   that which was given in the request.
681 * arp_tpa is the protocol address of the target, that is, the desired address.
682 *
683 * Note that the requirement that arp_spa be filled in with the responder's
684 * protocol is purely for convenience.  For instance, if a system were to use
685 * both ARP and RARP, then the inclusion of the valid protocol-hardware
686 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
687 * ARP request.
688 */
689rarp_reply(ii, ep, ipaddr)
690	struct if_info *ii;
691	struct ether_header *ep;
692	u_long ipaddr;
693{
694	int n;
695	struct ether_arp *ap = (struct ether_arp *)(ep + 1);
696	int len, raw_sock;
697
698	update_arptab((u_char *)&ap->arp_sha, ipaddr);
699
700	/*
701	 * Build the rarp reply by modifying the rarp request in place.
702	 */
703	ap->arp_op = htons(ARPOP_REVREPLY);
704
705	/*
706	 * XXX   Using htons(ETHERTYPE_REVARP) doesn't work: you wind
707	 * up transmitting 0x3580 instead of the correct value of
708	 * 0x8035. What makes no sense is that the NetBSD people
709	 * do in fact use htons(ETHERTYPE_REVARP) in their rarpd.
710	 * (Thank god for tcpdump or I would never have figured this
711	 * out.)
712	 */
713	ep->ether_type = htons(ETHERTYPE_REVARP);
714
715	bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6);
716	bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6);
717	bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6);
718
719	bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4);
720	/* Target hardware is unchanged. */
721	bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4);
722
723	len = sizeof(*ep) + sizeof(*ap);
724	n = write(ii->ii_fd, (char *)ep, len);
725	if (n != len) {
726		syslog(LOG_ERR, "write: only %d of %d bytes written", n, len);
727	}
728}
729
730/*
731 * Get the netmask of an IP address.  This routine is used if
732 * SIOCGIFNETMASK doesn't work.
733 */
734u_long
735ipaddrtonetmask(addr)
736	u_long addr;
737{
738	if (IN_CLASSA(addr))
739		return IN_CLASSA_NET;
740	if (IN_CLASSB(addr))
741		return IN_CLASSB_NET;
742	if (IN_CLASSC(addr))
743		return IN_CLASSC_NET;
744	syslog(LOG_DEBUG, "unknown IP address class: %08X", addr);
745	exit(1);
746	/* NOTREACHED */
747}
748