pflogd.c revision 1.10 1 1.10 joerg /* $NetBSD: pflogd.c,v 1.10 2015/08/28 12:17:41 joerg Exp $ */
2 1.5 yamt /* $OpenBSD: pflogd.c,v 1.45 2007/06/06 14:11:26 henning Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2001 Theo de Raadt
6 1.1 itojun * Copyright (c) 2001 Can Erkin Acar
7 1.1 itojun * All rights reserved.
8 1.1 itojun *
9 1.1 itojun * Redistribution and use in source and binary forms, with or without
10 1.1 itojun * modification, are permitted provided that the following conditions
11 1.1 itojun * are met:
12 1.1 itojun *
13 1.1 itojun * - Redistributions of source code must retain the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer.
15 1.1 itojun * - Redistributions in binary form must reproduce the above
16 1.1 itojun * copyright notice, this list of conditions and the following
17 1.1 itojun * disclaimer in the documentation and/or other materials provided
18 1.1 itojun * with the distribution.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
32 1.1 itojun */
33 1.1 itojun
34 1.1 itojun #include <sys/types.h>
35 1.1 itojun #include <sys/ioctl.h>
36 1.1 itojun #include <sys/file.h>
37 1.1 itojun #include <sys/stat.h>
38 1.5 yamt #include <sys/socket.h>
39 1.5 yamt #include <net/if.h>
40 1.1 itojun #include <stdio.h>
41 1.1 itojun #include <stdlib.h>
42 1.1 itojun #include <string.h>
43 1.1 itojun #include <unistd.h>
44 1.4 tls /*
45 1.4 tls * If we're going to include parts of the libpcap internals we MUST
46 1.4 tls * set the feature-test macros they expect, or they may misbehave.
47 1.4 tls */
48 1.4 tls #define HAVE_STRLCPY
49 1.4 tls #define HAVE_SNPRINTF
50 1.4 tls #define HAVE_VSNPRINTF
51 1.1 itojun #include <pcap-int.h>
52 1.1 itojun #include <pcap.h>
53 1.1 itojun #include <syslog.h>
54 1.1 itojun #include <signal.h>
55 1.5 yamt #include <err.h>
56 1.1 itojun #include <errno.h>
57 1.1 itojun #include <stdarg.h>
58 1.1 itojun #include <fcntl.h>
59 1.1 itojun #include <util.h>
60 1.1 itojun #include "pflogd.h"
61 1.1 itojun
62 1.1 itojun pcap_t *hpcap;
63 1.1 itojun static FILE *dpcap;
64 1.1 itojun
65 1.1 itojun int Debug = 0;
66 1.8 christos static uint32_t snaplen = DEF_SNAPLEN;
67 1.8 christos static uint32_t cur_snaplen = DEF_SNAPLEN;
68 1.1 itojun
69 1.1 itojun volatile sig_atomic_t gotsig_close, gotsig_alrm, gotsig_hup;
70 1.1 itojun
71 1.8 christos const char *filename = PFLOGD_LOG_FILE;
72 1.8 christos const char *interface = PFLOGD_DEFAULT_IF;
73 1.8 christos const char *filter = NULL;
74 1.1 itojun
75 1.1 itojun char errbuf[PCAP_ERRBUF_SIZE];
76 1.1 itojun
77 1.1 itojun int log_debug = 0;
78 1.1 itojun unsigned int delay = FLUSH_DELAY;
79 1.1 itojun
80 1.1 itojun char *copy_argv(char * const *);
81 1.1 itojun void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
82 1.1 itojun void dump_packet_nobuf(u_char *, const struct pcap_pkthdr *, const u_char *);
83 1.1 itojun int flush_buffer(FILE *);
84 1.8 christos int if_exists(const char *);
85 1.1 itojun int init_pcap(void);
86 1.1 itojun void logmsg(int, const char *, ...);
87 1.1 itojun void purge_buffer(void);
88 1.5 yamt int reset_dump(int);
89 1.1 itojun int scan_dump(FILE *, off_t);
90 1.8 christos int set_snaplen(uint32_t);
91 1.1 itojun void set_suspended(int);
92 1.1 itojun void sig_alrm(int);
93 1.1 itojun void sig_close(int);
94 1.1 itojun void sig_hup(int);
95 1.1 itojun void usage(void);
96 1.1 itojun
97 1.5 yamt static int try_reset_dump(int);
98 1.5 yamt
99 1.1 itojun /* buffer must always be greater than snaplen */
100 1.8 christos static size_t bufpkt = 0; /* number of packets in buffer */
101 1.8 christos static size_t buflen = 0; /* allocated size of buffer */
102 1.1 itojun static char *buffer = NULL; /* packet buffer */
103 1.1 itojun static char *bufpos = NULL; /* position in buffer */
104 1.8 christos static size_t bufleft = 0; /* bytes left in buffer */
105 1.1 itojun
106 1.1 itojun /* if error, stop logging but count dropped packets */
107 1.1 itojun static int suspended = -1;
108 1.1 itojun static long packets_dropped = 0;
109 1.1 itojun
110 1.1 itojun void
111 1.1 itojun set_suspended(int s)
112 1.1 itojun {
113 1.1 itojun if (suspended == s)
114 1.1 itojun return;
115 1.1 itojun
116 1.1 itojun suspended = s;
117 1.5 yamt setproctitle("[%s] -s %d -i %s -f %s",
118 1.5 yamt suspended ? "suspended" : "running",
119 1.5 yamt cur_snaplen, interface, filename);
120 1.1 itojun }
121 1.1 itojun
122 1.1 itojun char *
123 1.1 itojun copy_argv(char * const *argv)
124 1.1 itojun {
125 1.1 itojun size_t len = 0, n;
126 1.1 itojun char *buf;
127 1.1 itojun
128 1.1 itojun if (argv == NULL)
129 1.1 itojun return (NULL);
130 1.1 itojun
131 1.1 itojun for (n = 0; argv[n]; n++)
132 1.1 itojun len += strlen(argv[n])+1;
133 1.1 itojun if (len == 0)
134 1.1 itojun return (NULL);
135 1.1 itojun
136 1.1 itojun buf = malloc(len);
137 1.1 itojun if (buf == NULL)
138 1.1 itojun return (NULL);
139 1.1 itojun
140 1.1 itojun strlcpy(buf, argv[0], len);
141 1.1 itojun for (n = 1; argv[n]; n++) {
142 1.1 itojun strlcat(buf, " ", len);
143 1.1 itojun strlcat(buf, argv[n], len);
144 1.1 itojun }
145 1.1 itojun return (buf);
146 1.1 itojun }
147 1.1 itojun
148 1.1 itojun void
149 1.1 itojun logmsg(int pri, const char *message, ...)
150 1.1 itojun {
151 1.1 itojun va_list ap;
152 1.1 itojun va_start(ap, message);
153 1.1 itojun
154 1.1 itojun if (log_debug) {
155 1.1 itojun vfprintf(stderr, message, ap);
156 1.1 itojun fprintf(stderr, "\n");
157 1.1 itojun } else
158 1.1 itojun vsyslog(pri, message, ap);
159 1.1 itojun va_end(ap);
160 1.1 itojun }
161 1.1 itojun
162 1.1 itojun __dead void
163 1.1 itojun usage(void)
164 1.1 itojun {
165 1.5 yamt fprintf(stderr, "usage: pflogd [-Dx] [-d delay] [-f filename]");
166 1.5 yamt fprintf(stderr, " [-i interface] [-p pidfile]\n");
167 1.5 yamt fprintf(stderr, " [-s snaplen] [expression]\n");
168 1.1 itojun exit(1);
169 1.1 itojun }
170 1.1 itojun
171 1.1 itojun void
172 1.1 itojun sig_close(int sig)
173 1.1 itojun {
174 1.1 itojun gotsig_close = 1;
175 1.1 itojun }
176 1.1 itojun
177 1.1 itojun void
178 1.1 itojun sig_hup(int sig)
179 1.1 itojun {
180 1.1 itojun gotsig_hup = 1;
181 1.1 itojun }
182 1.1 itojun
183 1.1 itojun void
184 1.1 itojun sig_alrm(int sig)
185 1.1 itojun {
186 1.1 itojun gotsig_alrm = 1;
187 1.1 itojun }
188 1.1 itojun
189 1.1 itojun void
190 1.1 itojun set_pcap_filter(void)
191 1.1 itojun {
192 1.1 itojun struct bpf_program bprog;
193 1.1 itojun
194 1.1 itojun if (pcap_compile(hpcap, &bprog, filter, PCAP_OPT_FIL, 0) < 0)
195 1.1 itojun logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
196 1.1 itojun else {
197 1.1 itojun if (pcap_setfilter(hpcap, &bprog) < 0)
198 1.1 itojun logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
199 1.1 itojun pcap_freecode(&bprog);
200 1.1 itojun }
201 1.1 itojun }
202 1.1 itojun
203 1.1 itojun int
204 1.8 christos if_exists(const char *ifname)
205 1.5 yamt {
206 1.5 yamt int s;
207 1.7 minskim #ifdef SIOCGIFDATA
208 1.6 christos struct ifdatareq ifr;
209 1.6 christos #define ifr_name ifdr_name
210 1.6 christos #else
211 1.5 yamt struct ifreq ifr;
212 1.5 yamt struct if_data ifrdat;
213 1.6 christos #endif
214 1.5 yamt
215 1.5 yamt if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
216 1.5 yamt err(1, "socket");
217 1.5 yamt bzero(&ifr, sizeof(ifr));
218 1.5 yamt if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
219 1.5 yamt sizeof(ifr.ifr_name))
220 1.5 yamt errx(1, "main ifr_name: strlcpy");
221 1.6 christos #ifndef ifr_name
222 1.5 yamt ifr.ifr_data = (caddr_t)&ifrdat;
223 1.6 christos #endif
224 1.5 yamt if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
225 1.5 yamt return (0);
226 1.5 yamt if (close(s))
227 1.5 yamt err(1, "close");
228 1.5 yamt
229 1.5 yamt return (1);
230 1.5 yamt }
231 1.5 yamt
232 1.5 yamt int
233 1.1 itojun init_pcap(void)
234 1.1 itojun {
235 1.1 itojun hpcap = pcap_open_live(interface, snaplen, 1, PCAP_TO_MS, errbuf);
236 1.1 itojun if (hpcap == NULL) {
237 1.1 itojun logmsg(LOG_ERR, "Failed to initialize: %s", errbuf);
238 1.1 itojun return (-1);
239 1.1 itojun }
240 1.1 itojun
241 1.1 itojun if (pcap_datalink(hpcap) != DLT_PFLOG) {
242 1.1 itojun logmsg(LOG_ERR, "Invalid datalink type");
243 1.1 itojun pcap_close(hpcap);
244 1.1 itojun hpcap = NULL;
245 1.1 itojun return (-1);
246 1.1 itojun }
247 1.1 itojun
248 1.1 itojun set_pcap_filter();
249 1.1 itojun
250 1.1 itojun cur_snaplen = snaplen = pcap_snapshot(hpcap);
251 1.1 itojun
252 1.1 itojun /* lock */
253 1.2 peter #ifdef __OpenBSD__
254 1.1 itojun if (ioctl(pcap_fileno(hpcap), BIOCLOCK) < 0) {
255 1.1 itojun logmsg(LOG_ERR, "BIOCLOCK: %s", strerror(errno));
256 1.1 itojun return (-1);
257 1.1 itojun }
258 1.2 peter #endif
259 1.1 itojun
260 1.1 itojun return (0);
261 1.1 itojun }
262 1.1 itojun
263 1.1 itojun int
264 1.8 christos set_snaplen(uint32_t snap)
265 1.1 itojun {
266 1.1 itojun if (priv_set_snaplen(snap))
267 1.1 itojun return (1);
268 1.1 itojun
269 1.1 itojun if (cur_snaplen > snap)
270 1.1 itojun purge_buffer();
271 1.1 itojun
272 1.1 itojun cur_snaplen = snap;
273 1.1 itojun
274 1.1 itojun return (0);
275 1.1 itojun }
276 1.1 itojun
277 1.1 itojun int
278 1.5 yamt reset_dump(int nomove)
279 1.5 yamt {
280 1.5 yamt int ret;
281 1.5 yamt
282 1.5 yamt for (;;) {
283 1.5 yamt ret = try_reset_dump(nomove);
284 1.5 yamt if (ret <= 0)
285 1.5 yamt break;
286 1.5 yamt }
287 1.5 yamt
288 1.5 yamt return (ret);
289 1.5 yamt }
290 1.5 yamt
291 1.5 yamt /*
292 1.5 yamt * tries to (re)open log file, nomove flag is used with -x switch
293 1.5 yamt * returns 0: success, 1: retry (log moved), -1: error
294 1.5 yamt */
295 1.5 yamt int
296 1.5 yamt try_reset_dump(int nomove)
297 1.1 itojun {
298 1.1 itojun struct pcap_file_header hdr;
299 1.1 itojun struct stat st;
300 1.1 itojun int fd;
301 1.1 itojun FILE *fp;
302 1.1 itojun
303 1.1 itojun if (hpcap == NULL)
304 1.1 itojun return (-1);
305 1.1 itojun
306 1.1 itojun if (dpcap) {
307 1.1 itojun flush_buffer(dpcap);
308 1.1 itojun fclose(dpcap);
309 1.1 itojun dpcap = NULL;
310 1.1 itojun }
311 1.1 itojun
312 1.1 itojun /*
313 1.1 itojun * Basically reimplement pcap_dump_open() because it truncates
314 1.1 itojun * files and duplicates headers and such.
315 1.1 itojun */
316 1.1 itojun fd = priv_open_log();
317 1.1 itojun if (fd < 0)
318 1.5 yamt return (-1);
319 1.1 itojun
320 1.1 itojun fp = fdopen(fd, "a+");
321 1.1 itojun
322 1.1 itojun if (fp == NULL) {
323 1.5 yamt logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
324 1.2 peter close(fd);
325 1.5 yamt return (-1);
326 1.1 itojun }
327 1.1 itojun if (fstat(fileno(fp), &st) == -1) {
328 1.5 yamt logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
329 1.2 peter fclose(fp);
330 1.5 yamt return (-1);
331 1.1 itojun }
332 1.1 itojun
333 1.1 itojun /* set FILE unbuffered, we do our own buffering */
334 1.1 itojun if (setvbuf(fp, NULL, _IONBF, 0)) {
335 1.5 yamt logmsg(LOG_ERR, "Failed to set output buffers");
336 1.2 peter fclose(fp);
337 1.5 yamt return (-1);
338 1.1 itojun }
339 1.1 itojun
340 1.1 itojun #define TCPDUMP_MAGIC 0xa1b2c3d4
341 1.1 itojun
342 1.1 itojun if (st.st_size == 0) {
343 1.1 itojun if (snaplen != cur_snaplen) {
344 1.1 itojun logmsg(LOG_NOTICE, "Using snaplen %d", snaplen);
345 1.5 yamt if (set_snaplen(snaplen))
346 1.1 itojun logmsg(LOG_WARNING,
347 1.1 itojun "Failed, using old settings");
348 1.1 itojun }
349 1.1 itojun hdr.magic = TCPDUMP_MAGIC;
350 1.1 itojun hdr.version_major = PCAP_VERSION_MAJOR;
351 1.1 itojun hdr.version_minor = PCAP_VERSION_MINOR;
352 1.1 itojun hdr.thiszone = hpcap->tzoff;
353 1.1 itojun hdr.snaplen = hpcap->snapshot;
354 1.1 itojun hdr.sigfigs = 0;
355 1.1 itojun hdr.linktype = hpcap->linktype;
356 1.1 itojun
357 1.1 itojun if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
358 1.1 itojun fclose(fp);
359 1.5 yamt return (-1);
360 1.1 itojun }
361 1.1 itojun } else if (scan_dump(fp, st.st_size)) {
362 1.1 itojun fclose(fp);
363 1.5 yamt if (nomove || priv_move_log()) {
364 1.5 yamt logmsg(LOG_ERR,
365 1.5 yamt "Invalid/incompatible log file, move it away");
366 1.5 yamt return (-1);
367 1.5 yamt }
368 1.1 itojun return (1);
369 1.1 itojun }
370 1.1 itojun
371 1.1 itojun dpcap = fp;
372 1.1 itojun
373 1.1 itojun set_suspended(0);
374 1.1 itojun flush_buffer(fp);
375 1.1 itojun
376 1.1 itojun return (0);
377 1.1 itojun }
378 1.1 itojun
379 1.1 itojun int
380 1.1 itojun scan_dump(FILE *fp, off_t size)
381 1.1 itojun {
382 1.1 itojun struct pcap_file_header hdr;
383 1.2 peter #ifdef __OpenBSD__
384 1.1 itojun struct pcap_pkthdr ph;
385 1.2 peter #else
386 1.2 peter struct pcap_sf_pkthdr ph;
387 1.2 peter #endif
388 1.1 itojun off_t pos;
389 1.1 itojun
390 1.1 itojun /*
391 1.1 itojun * Must read the file, compare the header against our new
392 1.1 itojun * options (in particular, snaplen) and adjust our options so
393 1.1 itojun * that we generate a correct file. Furthermore, check the file
394 1.1 itojun * for consistency so that we can append safely.
395 1.1 itojun *
396 1.1 itojun * XXX this may take a long time for large logs.
397 1.1 itojun */
398 1.1 itojun (void) fseek(fp, 0L, SEEK_SET);
399 1.1 itojun
400 1.1 itojun if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
401 1.1 itojun logmsg(LOG_ERR, "Short file header");
402 1.1 itojun return (1);
403 1.1 itojun }
404 1.1 itojun
405 1.1 itojun if (hdr.magic != TCPDUMP_MAGIC ||
406 1.1 itojun hdr.version_major != PCAP_VERSION_MAJOR ||
407 1.1 itojun hdr.version_minor != PCAP_VERSION_MINOR ||
408 1.8 christos hdr.linktype != (uint32_t)hpcap->linktype ||
409 1.1 itojun hdr.snaplen > PFLOGD_MAXSNAPLEN) {
410 1.1 itojun return (1);
411 1.1 itojun }
412 1.1 itojun
413 1.1 itojun pos = sizeof(hdr);
414 1.1 itojun
415 1.1 itojun while (!feof(fp)) {
416 1.1 itojun off_t len = fread((char *)&ph, 1, sizeof(ph), fp);
417 1.1 itojun if (len == 0)
418 1.1 itojun break;
419 1.1 itojun
420 1.1 itojun if (len != sizeof(ph))
421 1.1 itojun goto error;
422 1.1 itojun if (ph.caplen > hdr.snaplen || ph.caplen > PFLOGD_MAXSNAPLEN)
423 1.1 itojun goto error;
424 1.1 itojun pos += sizeof(ph) + ph.caplen;
425 1.1 itojun if (pos > size)
426 1.1 itojun goto error;
427 1.1 itojun fseek(fp, ph.caplen, SEEK_CUR);
428 1.1 itojun }
429 1.1 itojun
430 1.1 itojun if (pos != size)
431 1.1 itojun goto error;
432 1.1 itojun
433 1.1 itojun if (hdr.snaplen != cur_snaplen) {
434 1.1 itojun logmsg(LOG_WARNING,
435 1.1 itojun "Existing file has different snaplen %u, using it",
436 1.1 itojun hdr.snaplen);
437 1.1 itojun if (set_snaplen(hdr.snaplen)) {
438 1.1 itojun logmsg(LOG_WARNING,
439 1.1 itojun "Failed, using old settings, offset %llu",
440 1.1 itojun (unsigned long long) size);
441 1.1 itojun }
442 1.1 itojun }
443 1.1 itojun
444 1.1 itojun return (0);
445 1.1 itojun
446 1.1 itojun error:
447 1.1 itojun logmsg(LOG_ERR, "Corrupted log file.");
448 1.1 itojun return (1);
449 1.1 itojun }
450 1.1 itojun
451 1.1 itojun /* dump a packet directly to the stream, which is unbuffered */
452 1.1 itojun void
453 1.1 itojun dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
454 1.1 itojun {
455 1.2 peter #ifndef __OpenBSD__
456 1.2 peter struct pcap_sf_pkthdr sf_hdr;
457 1.2 peter #endif
458 1.1 itojun FILE *f = (FILE *)user;
459 1.1 itojun
460 1.1 itojun if (suspended) {
461 1.1 itojun packets_dropped++;
462 1.1 itojun return;
463 1.1 itojun }
464 1.1 itojun
465 1.2 peter #ifndef __OpenBSD__
466 1.2 peter sf_hdr.ts.tv_sec = h->ts.tv_sec;
467 1.2 peter sf_hdr.ts.tv_usec = h->ts.tv_usec;
468 1.2 peter sf_hdr.caplen = h->caplen;
469 1.2 peter sf_hdr.len = h->len;
470 1.2 peter #endif
471 1.2 peter
472 1.2 peter #ifdef __OpenBSD__
473 1.1 itojun if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
474 1.2 peter #else
475 1.2 peter if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) != 1) {
476 1.2 peter #endif
477 1.1 itojun /* try to undo header to prevent corruption */
478 1.8 christos size_t pos = (size_t)ftello(f);
479 1.2 peter #ifdef __OpenBSD__
480 1.1 itojun if (pos < sizeof(*h) ||
481 1.1 itojun ftruncate(fileno(f), pos - sizeof(*h))) {
482 1.2 peter #else
483 1.2 peter if (pos < sizeof(sf_hdr) ||
484 1.2 peter ftruncate(fileno(f), pos - sizeof(sf_hdr))) {
485 1.2 peter #endif
486 1.1 itojun logmsg(LOG_ERR, "Write failed, corrupted logfile!");
487 1.1 itojun set_suspended(1);
488 1.1 itojun gotsig_close = 1;
489 1.1 itojun return;
490 1.1 itojun }
491 1.1 itojun goto error;
492 1.1 itojun }
493 1.1 itojun
494 1.8 christos if (fwrite(sp, h->caplen, 1, f) != 1)
495 1.1 itojun goto error;
496 1.1 itojun
497 1.1 itojun return;
498 1.1 itojun
499 1.1 itojun error:
500 1.1 itojun set_suspended(1);
501 1.1 itojun packets_dropped ++;
502 1.1 itojun logmsg(LOG_ERR, "Logging suspended: fwrite: %s", strerror(errno));
503 1.1 itojun }
504 1.1 itojun
505 1.1 itojun int
506 1.1 itojun flush_buffer(FILE *f)
507 1.1 itojun {
508 1.1 itojun off_t offset;
509 1.1 itojun int len = bufpos - buffer;
510 1.1 itojun
511 1.1 itojun if (len <= 0)
512 1.1 itojun return (0);
513 1.1 itojun
514 1.1 itojun offset = ftello(f);
515 1.1 itojun if (offset == (off_t)-1) {
516 1.1 itojun set_suspended(1);
517 1.1 itojun logmsg(LOG_ERR, "Logging suspended: ftello: %s",
518 1.1 itojun strerror(errno));
519 1.1 itojun return (1);
520 1.1 itojun }
521 1.1 itojun
522 1.1 itojun if (fwrite(buffer, len, 1, f) != 1) {
523 1.1 itojun set_suspended(1);
524 1.1 itojun logmsg(LOG_ERR, "Logging suspended: fwrite: %s",
525 1.1 itojun strerror(errno));
526 1.1 itojun ftruncate(fileno(f), offset);
527 1.1 itojun return (1);
528 1.1 itojun }
529 1.1 itojun
530 1.1 itojun set_suspended(0);
531 1.1 itojun bufpos = buffer;
532 1.1 itojun bufleft = buflen;
533 1.1 itojun bufpkt = 0;
534 1.1 itojun
535 1.1 itojun return (0);
536 1.1 itojun }
537 1.1 itojun
538 1.1 itojun void
539 1.1 itojun purge_buffer(void)
540 1.1 itojun {
541 1.1 itojun packets_dropped += bufpkt;
542 1.1 itojun
543 1.1 itojun set_suspended(0);
544 1.1 itojun bufpos = buffer;
545 1.1 itojun bufleft = buflen;
546 1.1 itojun bufpkt = 0;
547 1.1 itojun }
548 1.1 itojun
549 1.1 itojun /* append packet to the buffer, flushing if necessary */
550 1.1 itojun void
551 1.1 itojun dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
552 1.1 itojun {
553 1.1 itojun FILE *f = (FILE *)user;
554 1.2 peter #ifdef __OpenBSD__
555 1.1 itojun size_t len = sizeof(*h) + h->caplen;
556 1.2 peter #else
557 1.2 peter struct pcap_sf_pkthdr sf_hdr;
558 1.2 peter size_t len = sizeof(sf_hdr) + h->caplen;
559 1.2 peter #endif
560 1.1 itojun
561 1.1 itojun if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
562 1.9 joerg logmsg(LOG_NOTICE, "invalid size %zu (%u/%u), packet dropped",
563 1.1 itojun len, cur_snaplen, snaplen);
564 1.1 itojun packets_dropped++;
565 1.1 itojun return;
566 1.1 itojun }
567 1.1 itojun
568 1.1 itojun if (len <= bufleft)
569 1.1 itojun goto append;
570 1.1 itojun
571 1.1 itojun if (suspended) {
572 1.1 itojun packets_dropped++;
573 1.1 itojun return;
574 1.1 itojun }
575 1.1 itojun
576 1.1 itojun if (flush_buffer(f)) {
577 1.1 itojun packets_dropped++;
578 1.1 itojun return;
579 1.1 itojun }
580 1.1 itojun
581 1.1 itojun if (len > bufleft) {
582 1.1 itojun dump_packet_nobuf(user, h, sp);
583 1.1 itojun return;
584 1.1 itojun }
585 1.1 itojun
586 1.2 peter append:
587 1.2 peter #ifdef __OpenBSD__
588 1.1 itojun memcpy(bufpos, h, sizeof(*h));
589 1.1 itojun memcpy(bufpos + sizeof(*h), sp, h->caplen);
590 1.2 peter #else
591 1.2 peter sf_hdr.ts.tv_sec = h->ts.tv_sec;
592 1.2 peter sf_hdr.ts.tv_usec = h->ts.tv_usec;
593 1.2 peter sf_hdr.caplen = h->caplen;
594 1.2 peter sf_hdr.len = h->len;
595 1.2 peter
596 1.2 peter memcpy(bufpos, &sf_hdr, sizeof(sf_hdr));
597 1.2 peter memcpy(bufpos + sizeof(sf_hdr), sp, h->caplen);
598 1.2 peter #endif
599 1.1 itojun
600 1.1 itojun bufpos += len;
601 1.1 itojun bufleft -= len;
602 1.1 itojun bufpkt++;
603 1.1 itojun
604 1.1 itojun return;
605 1.1 itojun }
606 1.1 itojun
607 1.1 itojun int
608 1.1 itojun main(int argc, char **argv)
609 1.1 itojun {
610 1.1 itojun struct pcap_stat pstat;
611 1.5 yamt int ch, np, ret, Xflag = 0;
612 1.1 itojun pcap_handler phandler = dump_packet;
613 1.2 peter const char *errstr = NULL;
614 1.5 yamt char *pidf = NULL;
615 1.5 yamt
616 1.5 yamt ret = 0;
617 1.1 itojun
618 1.1 itojun closefrom(STDERR_FILENO + 1);
619 1.1 itojun
620 1.5 yamt while ((ch = getopt(argc, argv, "Dxd:f:i:p:s:")) != -1) {
621 1.1 itojun switch (ch) {
622 1.1 itojun case 'D':
623 1.1 itojun Debug = 1;
624 1.1 itojun break;
625 1.1 itojun case 'd':
626 1.2 peter delay = strtonum(optarg, 5, 60*60, &errstr);
627 1.2 peter if (errstr)
628 1.1 itojun usage();
629 1.1 itojun break;
630 1.1 itojun case 'f':
631 1.1 itojun filename = optarg;
632 1.1 itojun break;
633 1.5 yamt case 'i':
634 1.5 yamt interface = optarg;
635 1.5 yamt break;
636 1.5 yamt case 'p':
637 1.5 yamt pidf = optarg;
638 1.5 yamt break;
639 1.1 itojun case 's':
640 1.2 peter snaplen = strtonum(optarg, 0, PFLOGD_MAXSNAPLEN,
641 1.2 peter &errstr);
642 1.1 itojun if (snaplen <= 0)
643 1.1 itojun snaplen = DEF_SNAPLEN;
644 1.2 peter if (errstr)
645 1.1 itojun snaplen = PFLOGD_MAXSNAPLEN;
646 1.1 itojun break;
647 1.1 itojun case 'x':
648 1.1 itojun Xflag++;
649 1.1 itojun break;
650 1.1 itojun default:
651 1.1 itojun usage();
652 1.1 itojun }
653 1.1 itojun
654 1.1 itojun }
655 1.1 itojun
656 1.1 itojun log_debug = Debug;
657 1.1 itojun argc -= optind;
658 1.1 itojun argv += optind;
659 1.1 itojun
660 1.5 yamt /* does interface exist */
661 1.5 yamt if (!if_exists(interface)) {
662 1.5 yamt warn("Failed to initialize: %s", interface);
663 1.5 yamt logmsg(LOG_ERR, "Failed to initialize: %s", interface);
664 1.5 yamt logmsg(LOG_ERR, "Exiting, init failure");
665 1.5 yamt exit(1);
666 1.5 yamt }
667 1.5 yamt
668 1.1 itojun if (!Debug) {
669 1.1 itojun openlog("pflogd", LOG_PID | LOG_CONS, LOG_DAEMON);
670 1.1 itojun if (daemon(0, 0)) {
671 1.1 itojun logmsg(LOG_WARNING, "Failed to become daemon: %s",
672 1.1 itojun strerror(errno));
673 1.1 itojun }
674 1.5 yamt pidfile(pidf);
675 1.1 itojun }
676 1.1 itojun
677 1.3 peter tzset();
678 1.1 itojun (void)umask(S_IRWXG | S_IRWXO);
679 1.1 itojun
680 1.1 itojun /* filter will be used by the privileged process */
681 1.1 itojun if (argc) {
682 1.1 itojun filter = copy_argv(argv);
683 1.1 itojun if (filter == NULL)
684 1.1 itojun logmsg(LOG_NOTICE, "Failed to form filter expression");
685 1.1 itojun }
686 1.1 itojun
687 1.1 itojun /* initialize pcap before dropping privileges */
688 1.1 itojun if (init_pcap()) {
689 1.1 itojun logmsg(LOG_ERR, "Exiting, init failure");
690 1.1 itojun exit(1);
691 1.1 itojun }
692 1.1 itojun
693 1.1 itojun /* Privilege separation begins here */
694 1.1 itojun if (priv_init()) {
695 1.1 itojun logmsg(LOG_ERR, "unable to privsep");
696 1.1 itojun exit(1);
697 1.1 itojun }
698 1.1 itojun
699 1.1 itojun setproctitle("[initializing]");
700 1.1 itojun /* Process is now unprivileged and inside a chroot */
701 1.1 itojun signal(SIGTERM, sig_close);
702 1.1 itojun signal(SIGINT, sig_close);
703 1.1 itojun signal(SIGQUIT, sig_close);
704 1.1 itojun signal(SIGALRM, sig_alrm);
705 1.1 itojun signal(SIGHUP, sig_hup);
706 1.1 itojun alarm(delay);
707 1.1 itojun
708 1.1 itojun buffer = malloc(PFLOGD_BUFSIZE);
709 1.1 itojun
710 1.1 itojun if (buffer == NULL) {
711 1.1 itojun logmsg(LOG_WARNING, "Failed to allocate output buffer");
712 1.1 itojun phandler = dump_packet_nobuf;
713 1.1 itojun } else {
714 1.1 itojun bufleft = buflen = PFLOGD_BUFSIZE;
715 1.1 itojun bufpos = buffer;
716 1.1 itojun bufpkt = 0;
717 1.1 itojun }
718 1.1 itojun
719 1.5 yamt if (reset_dump(Xflag) < 0) {
720 1.1 itojun if (Xflag)
721 1.1 itojun return (1);
722 1.1 itojun
723 1.1 itojun logmsg(LOG_ERR, "Logging suspended: open error");
724 1.1 itojun set_suspended(1);
725 1.1 itojun } else if (Xflag)
726 1.1 itojun return (0);
727 1.1 itojun
728 1.1 itojun while (1) {
729 1.1 itojun np = pcap_dispatch(hpcap, PCAP_NUM_PKTS,
730 1.3 peter phandler, (u_char *)dpcap);
731 1.5 yamt if (np < 0) {
732 1.10 joerg if (!if_exists(interface)) {
733 1.5 yamt logmsg(LOG_NOTICE, "interface %s went away",
734 1.5 yamt interface);
735 1.5 yamt ret = -1;
736 1.5 yamt break;
737 1.5 yamt }
738 1.1 itojun logmsg(LOG_NOTICE, "%s", pcap_geterr(hpcap));
739 1.5 yamt }
740 1.1 itojun
741 1.1 itojun if (gotsig_close)
742 1.1 itojun break;
743 1.1 itojun if (gotsig_hup) {
744 1.5 yamt if (reset_dump(0)) {
745 1.1 itojun logmsg(LOG_ERR,
746 1.1 itojun "Logging suspended: open error");
747 1.1 itojun set_suspended(1);
748 1.1 itojun }
749 1.1 itojun gotsig_hup = 0;
750 1.1 itojun }
751 1.1 itojun
752 1.1 itojun if (gotsig_alrm) {
753 1.1 itojun if (dpcap)
754 1.1 itojun flush_buffer(dpcap);
755 1.5 yamt else
756 1.5 yamt gotsig_hup = 1;
757 1.1 itojun gotsig_alrm = 0;
758 1.1 itojun alarm(delay);
759 1.1 itojun }
760 1.1 itojun }
761 1.1 itojun
762 1.1 itojun logmsg(LOG_NOTICE, "Exiting");
763 1.1 itojun if (dpcap) {
764 1.1 itojun flush_buffer(dpcap);
765 1.1 itojun fclose(dpcap);
766 1.1 itojun }
767 1.1 itojun purge_buffer();
768 1.1 itojun
769 1.1 itojun if (pcap_stats(hpcap, &pstat) < 0)
770 1.1 itojun logmsg(LOG_WARNING, "Reading stats: %s", pcap_geterr(hpcap));
771 1.1 itojun else
772 1.1 itojun logmsg(LOG_NOTICE,
773 1.9 joerg "%u packets received, %u/%ld dropped (kernel/pflogd)",
774 1.1 itojun pstat.ps_recv, pstat.ps_drop, packets_dropped);
775 1.1 itojun
776 1.1 itojun pcap_close(hpcap);
777 1.1 itojun if (!Debug)
778 1.1 itojun closelog();
779 1.5 yamt return (ret);
780 1.1 itojun }
781