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