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