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