npfd_log.c revision 1.5 1 1.5 christos /* $NetBSD: npfd_log.c,v 1.5 2017/01/05 16:23:31 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.5 christos __RCSID("$NetBSD: npfd_log.c,v 1.5 2017/01/05 16:23:31 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.5 christos if (pcap_set_timeout(ctx->pcap, 1000) == -1)
97 1.5 christos errx(EXIT_FAILURE, "pcap_set_timeout failed: %s",
98 1.5 christos pcap_geterr(ctx->pcap));
99 1.5 christos
100 1.4 christos if (pcap_activate(ctx->pcap) == -1)
101 1.4 christos errx(EXIT_FAILURE, "pcap_activate failed: %s",
102 1.4 christos pcap_geterr(ctx->pcap));
103 1.4 christos
104 1.4 christos if (filter)
105 1.4 christos npfd_log_setfilter(ctx, filter);
106 1.4 christos
107 1.4 christos snprintf(ctx->path, sizeof(ctx->path), NPFD_LOG_PATH "/%s.pcap",
108 1.4 christos ctx->ifname);
109 1.2 christos
110 1.4 christos npfd_log_reopen(ctx, true);
111 1.1 rmind return ctx;
112 1.1 rmind }
113 1.1 rmind
114 1.2 christos bool
115 1.4 christos npfd_log_reopen(npfd_log_t *ctx, bool die)
116 1.2 christos {
117 1.2 christos if (ctx->dumper)
118 1.2 christos pcap_dump_close(ctx->dumper);
119 1.2 christos /*
120 1.2 christos * Open a log file to write for a given interface and dump there.
121 1.2 christos */
122 1.3 christos if (access(ctx->path, F_OK) == 0)
123 1.3 christos ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
124 1.3 christos else
125 1.3 christos ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
126 1.2 christos if (ctx->dumper == NULL) {
127 1.4 christos if (die)
128 1.4 christos errx(EXIT_FAILURE, "pcap_dump_open failed for `%s': %s",
129 1.4 christos ctx->path, pcap_geterr(ctx->pcap));
130 1.3 christos syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
131 1.2 christos ctx->path, pcap_geterr(ctx->pcap));
132 1.2 christos return false;
133 1.2 christos }
134 1.2 christos return true;
135 1.2 christos }
136 1.2 christos
137 1.1 rmind void
138 1.1 rmind npfd_log_destroy(npfd_log_t *ctx)
139 1.1 rmind {
140 1.1 rmind if (ctx->dumper)
141 1.1 rmind pcap_dump_close(ctx->dumper);
142 1.1 rmind if (ctx->pcap)
143 1.1 rmind pcap_close(ctx->pcap);
144 1.1 rmind free(ctx);
145 1.1 rmind }
146 1.1 rmind
147 1.1 rmind int
148 1.1 rmind npfd_log_getsock(npfd_log_t *ctx)
149 1.1 rmind {
150 1.1 rmind return pcap_get_selectable_fd(ctx->pcap);
151 1.1 rmind }
152 1.1 rmind
153 1.1 rmind void
154 1.4 christos npfd_log_flush(npfd_log_t *ctx)
155 1.4 christos {
156 1.4 christos if (!ctx->dumper)
157 1.4 christos return;
158 1.4 christos if (pcap_dump_flush(ctx->dumper) == -1)
159 1.4 christos syslog(LOG_ERR, "pcap_dump_flush failed for `%s': %m",
160 1.4 christos ctx->path);
161 1.4 christos }
162 1.4 christos
163 1.4 christos
164 1.4 christos void
165 1.1 rmind npfd_log(npfd_log_t *ctx)
166 1.1 rmind {
167 1.1 rmind pcap_dumper_t *dumper = ctx->dumper;
168 1.1 rmind
169 1.1 rmind pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (uint8_t *)dumper);
170 1.1 rmind }
171 1.1 rmind
172 1.1 rmind void
173 1.1 rmind npfd_log_stats(npfd_log_t *ctx)
174 1.1 rmind {
175 1.1 rmind pcap_t *pcap = ctx->pcap;
176 1.1 rmind struct pcap_stat ps;
177 1.1 rmind
178 1.2 christos if (pcap_stats(pcap, &ps) == -1) {
179 1.1 rmind syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
180 1.1 rmind return;
181 1.1 rmind }
182 1.4 christos syslog(LOG_INFO, "packet statistics: %s: %u received, %u dropped",
183 1.4 christos ctx->ifname, ps.ps_recv, ps.ps_drop);
184 1.1 rmind }
185