Home | History | Annotate | Line # | Download | only in npfd
npfd_log.c revision 1.4
      1  1.4  christos /*	$NetBSD: npfd_log.c,v 1.4 2016/12/30 19:55:46 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.4  christos __RCSID("$NetBSD: npfd_log.c,v 1.4 2016/12/30 19:55:46 christos Exp $");
     34  1.2  christos 
     35  1.2  christos #include <sys/types.h>
     36  1.2  christos #include <sys/param.h>
     37  1.2  christos #include <net/if.h>
     38  1.1     rmind 
     39  1.1     rmind #include <stdio.h>
     40  1.4  christos #include <err.h>
     41  1.1     rmind #include <inttypes.h>
     42  1.1     rmind #include <limits.h>
     43  1.2  christos #include <stdlib.h>
     44  1.3  christos #include <unistd.h>
     45  1.2  christos #include <syslog.h>
     46  1.2  christos #include <stdbool.h>
     47  1.1     rmind 
     48  1.1     rmind #include <pcap/pcap.h>
     49  1.2  christos #include "npfd.h"
     50  1.1     rmind 
     51  1.1     rmind struct npfd_log {
     52  1.2  christos 	char ifname[IFNAMSIZ];
     53  1.2  christos 	char path[MAXPATHLEN];
     54  1.2  christos 	pcap_t *pcap;
     55  1.2  christos 	pcap_dumper_t *dumper;
     56  1.1     rmind };
     57  1.1     rmind 
     58  1.4  christos static void
     59  1.4  christos npfd_log_setfilter(npfd_log_t *ctx, const char *filter)
     60  1.4  christos {
     61  1.4  christos 	struct bpf_program bprog;
     62  1.4  christos 
     63  1.4  christos 	if (pcap_compile(ctx->pcap, &bprog, filter, 1, 0) == -1)
     64  1.4  christos 		errx(EXIT_FAILURE, "pcap_compile failed for `%s': %s", filter,
     65  1.4  christos 		    pcap_geterr(ctx->pcap));
     66  1.4  christos 	if (pcap_setfilter(ctx->pcap, &bprog) == -1)
     67  1.4  christos 		errx(EXIT_FAILURE, "pcap_setfilter failed: %s",
     68  1.4  christos 		    pcap_geterr(ctx->pcap));
     69  1.4  christos 	pcap_freecode(&bprog);
     70  1.4  christos }
     71  1.4  christos 
     72  1.1     rmind npfd_log_t *
     73  1.4  christos npfd_log_create(const char *ifname, const char *filter, int snaplen)
     74  1.1     rmind {
     75  1.1     rmind 	npfd_log_t *ctx;
     76  1.1     rmind 	char errbuf[PCAP_ERRBUF_SIZE];
     77  1.1     rmind 
     78  1.4  christos 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
     79  1.4  christos 		err(EXIT_FAILURE, "malloc failed");
     80  1.1     rmind 
     81  1.1     rmind 	/*
     82  1.1     rmind 	 * Open a live capture handle in non-blocking mode.
     83  1.1     rmind 	 */
     84  1.4  christos 	snprintf(ctx->ifname, sizeof(ctx->ifname), "%s", ifname);
     85  1.2  christos 	ctx->pcap = pcap_create(ctx->ifname, errbuf);
     86  1.4  christos 	if (ctx->pcap == NULL)
     87  1.4  christos 		errx(EXIT_FAILURE, "pcap_create failed: %s", errbuf);
     88  1.1     rmind 
     89  1.4  christos 	if (pcap_setnonblock(ctx->pcap, 1, errbuf) == -1)
     90  1.4  christos 		errx(EXIT_FAILURE, "pcap_setnonblock failed: %s", errbuf);
     91  1.2  christos 
     92  1.4  christos 	if (pcap_set_snaplen(ctx->pcap, snaplen) == -1)
     93  1.4  christos 		errx(EXIT_FAILURE, "pcap_set_snaplen failed: %s",
     94  1.3  christos 		    pcap_geterr(ctx->pcap));
     95  1.2  christos 
     96  1.4  christos 	if (pcap_activate(ctx->pcap) == -1)
     97  1.4  christos 		errx(EXIT_FAILURE, "pcap_activate failed: %s",
     98  1.4  christos 		    pcap_geterr(ctx->pcap));
     99  1.4  christos 
    100  1.4  christos 	if (filter)
    101  1.4  christos 		npfd_log_setfilter(ctx, filter);
    102  1.4  christos 
    103  1.4  christos 	snprintf(ctx->path, sizeof(ctx->path), NPFD_LOG_PATH "/%s.pcap",
    104  1.4  christos 	    ctx->ifname);
    105  1.2  christos 
    106  1.4  christos 	npfd_log_reopen(ctx, true);
    107  1.1     rmind 	return ctx;
    108  1.1     rmind }
    109  1.1     rmind 
    110  1.2  christos bool
    111  1.4  christos npfd_log_reopen(npfd_log_t *ctx, bool die)
    112  1.2  christos {
    113  1.2  christos 	if (ctx->dumper)
    114  1.2  christos 		pcap_dump_close(ctx->dumper);
    115  1.2  christos 	/*
    116  1.2  christos 	 * Open a log file to write for a given interface and dump there.
    117  1.2  christos 	 */
    118  1.3  christos 	if (access(ctx->path, F_OK) == 0)
    119  1.3  christos 		ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
    120  1.3  christos 	else
    121  1.3  christos 		ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
    122  1.2  christos 	if (ctx->dumper == NULL) {
    123  1.4  christos 		if (die)
    124  1.4  christos 			errx(EXIT_FAILURE, "pcap_dump_open failed for `%s': %s",
    125  1.4  christos 			    ctx->path, pcap_geterr(ctx->pcap));
    126  1.3  christos 		syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
    127  1.2  christos 		    ctx->path, pcap_geterr(ctx->pcap));
    128  1.2  christos 		return false;
    129  1.2  christos 	}
    130  1.2  christos 	return true;
    131  1.2  christos }
    132  1.2  christos 
    133  1.1     rmind void
    134  1.1     rmind npfd_log_destroy(npfd_log_t *ctx)
    135  1.1     rmind {
    136  1.1     rmind 	if (ctx->dumper)
    137  1.1     rmind 		pcap_dump_close(ctx->dumper);
    138  1.1     rmind 	if (ctx->pcap)
    139  1.1     rmind 		pcap_close(ctx->pcap);
    140  1.1     rmind 	free(ctx);
    141  1.1     rmind }
    142  1.1     rmind 
    143  1.1     rmind int
    144  1.1     rmind npfd_log_getsock(npfd_log_t *ctx)
    145  1.1     rmind {
    146  1.1     rmind 	return pcap_get_selectable_fd(ctx->pcap);
    147  1.1     rmind }
    148  1.1     rmind 
    149  1.1     rmind void
    150  1.4  christos npfd_log_flush(npfd_log_t *ctx)
    151  1.4  christos {
    152  1.4  christos 	if (!ctx->dumper)
    153  1.4  christos 		return;
    154  1.4  christos 	if (pcap_dump_flush(ctx->dumper) == -1)
    155  1.4  christos 		syslog(LOG_ERR, "pcap_dump_flush failed for `%s': %m",
    156  1.4  christos 		    ctx->path);
    157  1.4  christos }
    158  1.4  christos 
    159  1.4  christos 
    160  1.4  christos void
    161  1.1     rmind npfd_log(npfd_log_t *ctx)
    162  1.1     rmind {
    163  1.1     rmind 	pcap_dumper_t *dumper = ctx->dumper;
    164  1.1     rmind 
    165  1.1     rmind 	pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (uint8_t *)dumper);
    166  1.1     rmind }
    167  1.1     rmind 
    168  1.1     rmind void
    169  1.1     rmind npfd_log_stats(npfd_log_t *ctx)
    170  1.1     rmind {
    171  1.1     rmind 	pcap_t *pcap = ctx->pcap;
    172  1.1     rmind 	struct pcap_stat ps;
    173  1.1     rmind 
    174  1.2  christos 	if (pcap_stats(pcap, &ps) == -1) {
    175  1.1     rmind 		syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
    176  1.1     rmind 		return;
    177  1.1     rmind 	}
    178  1.4  christos 	syslog(LOG_INFO, "packet statistics: %s: %u received, %u dropped",
    179  1.4  christos 	    ctx->ifname, ps.ps_recv, ps.ps_drop);
    180  1.1     rmind }
    181