npfd_log.c revision 1.3 1 1.3 christos /* $NetBSD: npfd_log.c,v 1.3 2016/12/28 03:02:54 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.3 christos __RCSID("$NetBSD: npfd_log.c,v 1.3 2016/12/28 03:02:54 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.1 rmind #include <inttypes.h>
41 1.1 rmind #include <limits.h>
42 1.2 christos #include <stdlib.h>
43 1.3 christos #include <unistd.h>
44 1.2 christos #include <syslog.h>
45 1.2 christos #include <stdbool.h>
46 1.1 rmind
47 1.1 rmind #include <pcap/pcap.h>
48 1.2 christos #include "npfd.h"
49 1.1 rmind
50 1.1 rmind struct npfd_log {
51 1.2 christos char ifname[IFNAMSIZ];
52 1.2 christos char path[MAXPATHLEN];
53 1.2 christos pcap_t *pcap;
54 1.2 christos pcap_dumper_t *dumper;
55 1.1 rmind };
56 1.1 rmind
57 1.1 rmind npfd_log_t *
58 1.1 rmind npfd_log_create(unsigned if_idx)
59 1.1 rmind {
60 1.1 rmind npfd_log_t *ctx;
61 1.1 rmind char errbuf[PCAP_ERRBUF_SIZE];
62 1.1 rmind
63 1.2 christos if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
64 1.1 rmind syslog(LOG_ERR, "malloc failed: %m");
65 1.1 rmind return NULL;
66 1.1 rmind }
67 1.1 rmind
68 1.1 rmind /*
69 1.1 rmind * Open a live capture handle in non-blocking mode.
70 1.1 rmind */
71 1.2 christos snprintf(ctx->ifname, sizeof(ctx->ifname), NPFD_NPFLOG "%u", if_idx);
72 1.2 christos ctx->pcap = pcap_create(ctx->ifname, errbuf);
73 1.2 christos if (ctx->pcap == NULL) {
74 1.1 rmind syslog(LOG_ERR, "pcap_create failed: %s", errbuf);
75 1.1 rmind goto err;
76 1.1 rmind }
77 1.2 christos if (pcap_setnonblock(ctx->pcap, 1, errbuf) == -1) {
78 1.1 rmind syslog(LOG_ERR, "pcap_setnonblock failed: %s", errbuf);
79 1.1 rmind goto err;
80 1.1 rmind }
81 1.1 rmind
82 1.2 christos pcap_set_snaplen(ctx->pcap, 10240);
83 1.2 christos
84 1.3 christos if (pcap_activate(ctx->pcap) == -1) {
85 1.3 christos syslog(LOG_ERR, "pcap_activate failed: %s",
86 1.3 christos pcap_geterr(ctx->pcap));
87 1.3 christos goto err;
88 1.3 christos }
89 1.2 christos snprintf(ctx->path, sizeof(ctx->path), "%s/%s%s",
90 1.2 christos NPFD_LOG_PATH, ctx->ifname, ".pcap");
91 1.2 christos
92 1.2 christos if (!npfd_log_reopen(ctx))
93 1.1 rmind goto err;
94 1.2 christos
95 1.1 rmind return ctx;
96 1.1 rmind err:
97 1.1 rmind npfd_log_destroy(ctx);
98 1.1 rmind return NULL;
99 1.1 rmind }
100 1.1 rmind
101 1.2 christos bool
102 1.2 christos npfd_log_reopen(npfd_log_t *ctx)
103 1.2 christos {
104 1.2 christos if (ctx->dumper)
105 1.2 christos pcap_dump_close(ctx->dumper);
106 1.2 christos /*
107 1.2 christos * Open a log file to write for a given interface and dump there.
108 1.2 christos */
109 1.3 christos if (access(ctx->path, F_OK) == 0)
110 1.3 christos ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
111 1.3 christos else
112 1.3 christos ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
113 1.2 christos if (ctx->dumper == NULL) {
114 1.3 christos syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
115 1.2 christos ctx->path, pcap_geterr(ctx->pcap));
116 1.2 christos return false;
117 1.2 christos }
118 1.2 christos return true;
119 1.2 christos }
120 1.2 christos
121 1.1 rmind void
122 1.1 rmind npfd_log_destroy(npfd_log_t *ctx)
123 1.1 rmind {
124 1.1 rmind if (ctx->dumper)
125 1.1 rmind pcap_dump_close(ctx->dumper);
126 1.1 rmind if (ctx->pcap)
127 1.1 rmind pcap_close(ctx->pcap);
128 1.1 rmind free(ctx);
129 1.1 rmind }
130 1.1 rmind
131 1.1 rmind int
132 1.1 rmind npfd_log_getsock(npfd_log_t *ctx)
133 1.1 rmind {
134 1.1 rmind return pcap_get_selectable_fd(ctx->pcap);
135 1.1 rmind }
136 1.1 rmind
137 1.1 rmind void
138 1.1 rmind npfd_log(npfd_log_t *ctx)
139 1.1 rmind {
140 1.1 rmind pcap_dumper_t *dumper = ctx->dumper;
141 1.1 rmind
142 1.1 rmind pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (uint8_t *)dumper);
143 1.1 rmind }
144 1.1 rmind
145 1.1 rmind void
146 1.1 rmind npfd_log_stats(npfd_log_t *ctx)
147 1.1 rmind {
148 1.1 rmind pcap_t *pcap = ctx->pcap;
149 1.1 rmind struct pcap_stat ps;
150 1.1 rmind
151 1.2 christos if (pcap_stats(pcap, &ps) == -1) {
152 1.1 rmind syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
153 1.1 rmind return;
154 1.1 rmind }
155 1.1 rmind syslog(LOG_NOTICE, "packet statistics: %u received, %u dropped",
156 1.1 rmind ps.ps_recv, ps.ps_drop);
157 1.1 rmind }
158