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