Home | History | Annotate | Line # | Download | only in npfd
npfd_log.c revision 1.8
      1  1.8  christos /*	$NetBSD: npfd_log.c,v 1.8 2017/01/07 16:48:03 christos Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.1     rmind  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1     rmind  * All rights reserved.
      6  1.1     rmind  *
      7  1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     rmind  * by Mindaugas Rasiukevicius.
      9  1.1     rmind  *
     10  1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1     rmind  * modification, are permitted provided that the following conditions
     12  1.1     rmind  * are met:
     13  1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1     rmind  *
     19  1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     rmind  */
     31  1.1     rmind 
     32  1.1     rmind #include <sys/cdefs.h>
     33  1.8  christos __RCSID("$NetBSD: npfd_log.c,v 1.8 2017/01/07 16:48:03 christos Exp $");
     34  1.2  christos 
     35  1.2  christos #include <sys/types.h>
     36  1.2  christos #include <sys/param.h>
     37  1.6  christos #include <sys/stat.h>
     38  1.6  christos 
     39  1.2  christos #include <net/if.h>
     40  1.1     rmind 
     41  1.1     rmind #include <stdio.h>
     42  1.4  christos #include <err.h>
     43  1.1     rmind #include <inttypes.h>
     44  1.1     rmind #include <limits.h>
     45  1.2  christos #include <stdlib.h>
     46  1.3  christos #include <unistd.h>
     47  1.2  christos #include <syslog.h>
     48  1.2  christos #include <stdbool.h>
     49  1.1     rmind 
     50  1.1     rmind #include <pcap/pcap.h>
     51  1.2  christos #include "npfd.h"
     52  1.1     rmind 
     53  1.1     rmind struct npfd_log {
     54  1.2  christos 	char ifname[IFNAMSIZ];
     55  1.2  christos 	char path[MAXPATHLEN];
     56  1.2  christos 	pcap_t *pcap;
     57  1.2  christos 	pcap_dumper_t *dumper;
     58  1.1     rmind };
     59  1.1     rmind 
     60  1.4  christos static void
     61  1.4  christos npfd_log_setfilter(npfd_log_t *ctx, const char *filter)
     62  1.4  christos {
     63  1.4  christos 	struct bpf_program bprog;
     64  1.4  christos 
     65  1.4  christos 	if (pcap_compile(ctx->pcap, &bprog, filter, 1, 0) == -1)
     66  1.4  christos 		errx(EXIT_FAILURE, "pcap_compile failed for `%s': %s", filter,
     67  1.4  christos 		    pcap_geterr(ctx->pcap));
     68  1.4  christos 	if (pcap_setfilter(ctx->pcap, &bprog) == -1)
     69  1.4  christos 		errx(EXIT_FAILURE, "pcap_setfilter failed: %s",
     70  1.4  christos 		    pcap_geterr(ctx->pcap));
     71  1.4  christos 	pcap_freecode(&bprog);
     72  1.4  christos }
     73  1.4  christos 
     74  1.6  christos static FILE *
     75  1.6  christos npfd_log_gethdr(npfd_log_t *ctx, struct pcap_file_header*hdr)
     76  1.6  christos {
     77  1.6  christos 	FILE *fp = fopen(ctx->path, "r");
     78  1.6  christos 
     79  1.6  christos 	hdr->magic = 0;
     80  1.6  christos 	if (fp == NULL)
     81  1.6  christos 		return NULL;
     82  1.6  christos 
     83  1.6  christos #define TCPDUMP_MAGIC 0xa1b2c3d4
     84  1.6  christos 
     85  1.6  christos 	switch (fread(hdr, sizeof(*hdr), 1, fp)) {
     86  1.6  christos 	case 0:
     87  1.6  christos 		hdr->magic = 0;
     88  1.6  christos 		fclose(fp);
     89  1.6  christos 		return NULL;
     90  1.6  christos 	case 1:
     91  1.6  christos 		if (hdr->magic != TCPDUMP_MAGIC ||
     92  1.6  christos 		    hdr->version_major != PCAP_VERSION_MAJOR ||
     93  1.6  christos 		    hdr->version_minor != PCAP_VERSION_MINOR)
     94  1.6  christos 			goto out;
     95  1.6  christos 		break;
     96  1.6  christos 	default:
     97  1.6  christos 		goto out;
     98  1.6  christos 	}
     99  1.6  christos 
    100  1.6  christos 	return fp;
    101  1.6  christos out:
    102  1.6  christos 	fclose(fp);
    103  1.8  christos 	hdr->magic = (uint32_t)-1;
    104  1.6  christos 	return NULL;
    105  1.6  christos }
    106  1.6  christos 
    107  1.6  christos static int
    108  1.6  christos npfd_log_getsnaplen(npfd_log_t *ctx)
    109  1.6  christos {
    110  1.6  christos 	struct pcap_file_header hdr;
    111  1.6  christos 	FILE *fp = npfd_log_gethdr(ctx, &hdr);
    112  1.6  christos 	if (fp == NULL)
    113  1.6  christos 		return hdr.magic == (uint32_t)-1 ? -1 : 0;
    114  1.6  christos 	fclose(fp);
    115  1.6  christos 	return hdr.snaplen;
    116  1.6  christos }
    117  1.6  christos 
    118  1.6  christos static int
    119  1.6  christos npfd_log_validate(npfd_log_t *ctx)
    120  1.6  christos {
    121  1.6  christos 	struct pcap_file_header hdr;
    122  1.6  christos 	FILE *fp = npfd_log_gethdr(ctx, &hdr);
    123  1.6  christos 	size_t o, no;
    124  1.6  christos 
    125  1.6  christos 	if (fp == NULL) {
    126  1.6  christos 		if (hdr.magic == 0)
    127  1.6  christos 			return 0;
    128  1.6  christos 		goto rename;
    129  1.6  christos 	}
    130  1.6  christos 
    131  1.6  christos 	struct stat st;
    132  1.6  christos 	if (fstat(fileno(fp), &st) == -1)
    133  1.6  christos 		goto rename;
    134  1.6  christos 
    135  1.6  christos 	size_t count = 0;
    136  1.6  christos 	for (o = sizeof(hdr);; count++) {
    137  1.6  christos 		struct {
    138  1.6  christos 			uint32_t sec;
    139  1.6  christos 			uint32_t usec;
    140  1.6  christos 			uint32_t caplen;
    141  1.6  christos 			uint32_t len;
    142  1.6  christos 		} pkt;
    143  1.6  christos 		switch (fread(&pkt, sizeof(pkt), 1, fp)) {
    144  1.6  christos 		case 0:
    145  1.6  christos 			syslog(LOG_INFO, "%zu packets read from `%s'", count,
    146  1.6  christos 			    ctx->path);
    147  1.6  christos 			fclose(fp);
    148  1.6  christos 			return hdr.snaplen;
    149  1.6  christos 		case 1:
    150  1.6  christos 			no = o + sizeof(pkt) + pkt.caplen;
    151  1.6  christos 			if (pkt.caplen > hdr.snaplen)
    152  1.6  christos 				goto fix;
    153  1.6  christos 			if (no > (size_t)st.st_size)
    154  1.6  christos 				goto fix;
    155  1.6  christos 			if (fseeko(fp, pkt.caplen, SEEK_CUR) != 0)
    156  1.6  christos 				goto fix;
    157  1.6  christos 			o = no;
    158  1.6  christos 			break;
    159  1.6  christos 		default:
    160  1.6  christos 			goto fix;
    161  1.6  christos 		}
    162  1.6  christos 	}
    163  1.6  christos 
    164  1.6  christos fix:
    165  1.6  christos 	fclose(fp);
    166  1.6  christos 	no = st.st_size - o;
    167  1.6  christos 	syslog(LOG_INFO, "%zu packets read from `%s', %zu extra bytes",
    168  1.6  christos 	    count, ctx->path, no);
    169  1.6  christos 	if (no < 10240) {
    170  1.6  christos 		syslog(LOG_WARNING,
    171  1.6  christos 		    "Incomplete last packet in `%s', truncating",
    172  1.6  christos 		    ctx->path);
    173  1.8  christos 		if (truncate(ctx->path, (off_t)o) == -1) {
    174  1.6  christos 			syslog(LOG_ERR, "Cannot truncate `%s': %m", ctx->path);
    175  1.6  christos 			goto rename;
    176  1.6  christos 		}
    177  1.6  christos 	} else {
    178  1.6  christos 		syslog(LOG_ERR, "Corrupt file `%s'", ctx->path);
    179  1.6  christos 		goto rename;
    180  1.6  christos 	}
    181  1.6  christos 	fclose(fp);
    182  1.6  christos 	return hdr.snaplen;
    183  1.6  christos rename:
    184  1.6  christos 	fclose(fp);
    185  1.6  christos 	char tmp[MAXPATHLEN];
    186  1.6  christos 	snprintf(tmp, sizeof(tmp), "%s.XXXXXX", ctx->path);
    187  1.6  christos 	int fd;
    188  1.6  christos 	if ((fd = mkstemp(tmp)) == -1) {
    189  1.6  christos 		syslog(LOG_ERR, "Can't make temp file `%s': %m", tmp);
    190  1.6  christos 		return -1;
    191  1.6  christos 	}
    192  1.6  christos 	close(fd);
    193  1.6  christos 	if (rename(ctx->path, tmp) == -1) {
    194  1.6  christos 		syslog(LOG_ERR, "Can't rename `%s' to `%s': %m",
    195  1.6  christos 		    ctx->path, tmp);
    196  1.6  christos 		return -1;
    197  1.6  christos 	}
    198  1.6  christos 	syslog(LOG_ERR, "Renamed to `%s'", tmp);
    199  1.6  christos 	return 0;
    200  1.6  christos }
    201  1.6  christos 
    202  1.6  christos 
    203  1.1     rmind npfd_log_t *
    204  1.6  christos npfd_log_create(const char *filename, const char *ifname, const char *filter,
    205  1.6  christos     int snaplen)
    206  1.1     rmind {
    207  1.1     rmind 	npfd_log_t *ctx;
    208  1.1     rmind 	char errbuf[PCAP_ERRBUF_SIZE];
    209  1.1     rmind 
    210  1.4  christos 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
    211  1.4  christos 		err(EXIT_FAILURE, "malloc failed");
    212  1.1     rmind 
    213  1.1     rmind 	/*
    214  1.1     rmind 	 * Open a live capture handle in non-blocking mode.
    215  1.1     rmind 	 */
    216  1.4  christos 	snprintf(ctx->ifname, sizeof(ctx->ifname), "%s", ifname);
    217  1.2  christos 	ctx->pcap = pcap_create(ctx->ifname, errbuf);
    218  1.4  christos 	if (ctx->pcap == NULL)
    219  1.4  christos 		errx(EXIT_FAILURE, "pcap_create failed: %s", errbuf);
    220  1.1     rmind 
    221  1.4  christos 	if (pcap_setnonblock(ctx->pcap, 1, errbuf) == -1)
    222  1.4  christos 		errx(EXIT_FAILURE, "pcap_setnonblock failed: %s", errbuf);
    223  1.2  christos 
    224  1.6  christos 	if (filename == NULL)
    225  1.6  christos 		snprintf(ctx->path, sizeof(ctx->path), NPFD_LOG_PATH "/%s.pcap",
    226  1.6  christos 		    ctx->ifname);
    227  1.6  christos 	else
    228  1.6  christos 		snprintf(ctx->path, sizeof(ctx->path), "%s", filename);
    229  1.6  christos 
    230  1.6  christos 	int sl = npfd_log_getsnaplen(ctx);
    231  1.6  christos 	if (sl == -1)
    232  1.6  christos 		errx(EXIT_FAILURE, "corrupt log file `%s'", ctx->path);
    233  1.6  christos 
    234  1.6  christos 	if (sl != 0 && sl != snaplen) {
    235  1.6  christos 		warnx("Overriding snaplen from %d to %d from `%s'", snaplen,
    236  1.6  christos 		    sl, filename);
    237  1.6  christos 		snaplen = sl;
    238  1.6  christos 	}
    239  1.6  christos 
    240  1.4  christos 	if (pcap_set_snaplen(ctx->pcap, snaplen) == -1)
    241  1.4  christos 		errx(EXIT_FAILURE, "pcap_set_snaplen failed: %s",
    242  1.3  christos 		    pcap_geterr(ctx->pcap));
    243  1.2  christos 
    244  1.5  christos 	if (pcap_set_timeout(ctx->pcap, 1000) == -1)
    245  1.5  christos 		errx(EXIT_FAILURE, "pcap_set_timeout failed: %s",
    246  1.5  christos 		    pcap_geterr(ctx->pcap));
    247  1.5  christos 
    248  1.4  christos 	if (pcap_activate(ctx->pcap) == -1)
    249  1.4  christos 		errx(EXIT_FAILURE, "pcap_activate failed: %s",
    250  1.4  christos 		    pcap_geterr(ctx->pcap));
    251  1.4  christos 
    252  1.4  christos 	if (filter)
    253  1.4  christos 		npfd_log_setfilter(ctx, filter);
    254  1.4  christos 
    255  1.2  christos 
    256  1.6  christos 	npfd_log_reopen(ctx, false);
    257  1.1     rmind 	return ctx;
    258  1.1     rmind }
    259  1.1     rmind 
    260  1.2  christos bool
    261  1.4  christos npfd_log_reopen(npfd_log_t *ctx, bool die)
    262  1.2  christos {
    263  1.7  christos 	mode_t omask = umask(077);
    264  1.7  christos 
    265  1.2  christos 	if (ctx->dumper)
    266  1.2  christos 		pcap_dump_close(ctx->dumper);
    267  1.2  christos 	/*
    268  1.2  christos 	 * Open a log file to write for a given interface and dump there.
    269  1.2  christos 	 */
    270  1.6  christos 	switch (npfd_log_validate(ctx)) {
    271  1.6  christos 	case -1:
    272  1.6  christos 		syslog(LOG_ERR, "Giving up");
    273  1.6  christos 		exit(EXIT_FAILURE);
    274  1.6  christos 		/*NOTREACHED*/
    275  1.6  christos 	case 0:
    276  1.6  christos 		ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
    277  1.6  christos 		break;
    278  1.6  christos 	default:
    279  1.3  christos 		ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
    280  1.6  christos 		break;
    281  1.6  christos 	}
    282  1.7  christos 	(void)umask(omask);
    283  1.6  christos 
    284  1.2  christos 	if (ctx->dumper == NULL) {
    285  1.4  christos 		if (die)
    286  1.4  christos 			errx(EXIT_FAILURE, "pcap_dump_open failed for `%s': %s",
    287  1.4  christos 			    ctx->path, pcap_geterr(ctx->pcap));
    288  1.3  christos 		syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
    289  1.2  christos 		    ctx->path, pcap_geterr(ctx->pcap));
    290  1.2  christos 		return false;
    291  1.2  christos 	}
    292  1.2  christos 	return true;
    293  1.2  christos }
    294  1.2  christos 
    295  1.1     rmind void
    296  1.1     rmind npfd_log_destroy(npfd_log_t *ctx)
    297  1.1     rmind {
    298  1.1     rmind 	if (ctx->dumper)
    299  1.1     rmind 		pcap_dump_close(ctx->dumper);
    300  1.1     rmind 	if (ctx->pcap)
    301  1.1     rmind 		pcap_close(ctx->pcap);
    302  1.1     rmind 	free(ctx);
    303  1.1     rmind }
    304  1.1     rmind 
    305  1.1     rmind int
    306  1.1     rmind npfd_log_getsock(npfd_log_t *ctx)
    307  1.1     rmind {
    308  1.1     rmind 	return pcap_get_selectable_fd(ctx->pcap);
    309  1.1     rmind }
    310  1.1     rmind 
    311  1.1     rmind void
    312  1.4  christos npfd_log_flush(npfd_log_t *ctx)
    313  1.4  christos {
    314  1.4  christos 	if (!ctx->dumper)
    315  1.4  christos 		return;
    316  1.4  christos 	if (pcap_dump_flush(ctx->dumper) == -1)
    317  1.4  christos 		syslog(LOG_ERR, "pcap_dump_flush failed for `%s': %m",
    318  1.4  christos 		    ctx->path);
    319  1.4  christos }
    320  1.4  christos 
    321  1.4  christos 
    322  1.4  christos void
    323  1.1     rmind npfd_log(npfd_log_t *ctx)
    324  1.1     rmind {
    325  1.1     rmind 	pcap_dumper_t *dumper = ctx->dumper;
    326  1.1     rmind 
    327  1.8  christos 	pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (void *)dumper);
    328  1.1     rmind }
    329  1.1     rmind 
    330  1.1     rmind void
    331  1.1     rmind npfd_log_stats(npfd_log_t *ctx)
    332  1.1     rmind {
    333  1.1     rmind 	pcap_t *pcap = ctx->pcap;
    334  1.1     rmind 	struct pcap_stat ps;
    335  1.1     rmind 
    336  1.2  christos 	if (pcap_stats(pcap, &ps) == -1) {
    337  1.1     rmind 		syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
    338  1.1     rmind 		return;
    339  1.1     rmind 	}
    340  1.4  christos 	syslog(LOG_INFO, "packet statistics: %s: %u received, %u dropped",
    341  1.4  christos 	    ctx->ifname, ps.ps_recv, ps.ps_drop);
    342  1.1     rmind }
    343