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