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