npfd_log.c revision 1.10 1 1.10 christos /* $NetBSD: npfd_log.c,v 1.10 2017/03/25 11:00:27 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.10 christos __RCSID("$NetBSD: npfd_log.c,v 1.10 2017/03/25 11:00:27 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.9 christos hdr->version_minor != PCAP_VERSION_MINOR ||
94 1.9 christos hdr->sigfigs != (u_int)pcap_get_tstamp_precision(ctx->pcap))
95 1.6 christos goto out;
96 1.6 christos break;
97 1.6 christos default:
98 1.6 christos goto out;
99 1.6 christos }
100 1.6 christos
101 1.6 christos return fp;
102 1.6 christos out:
103 1.6 christos fclose(fp);
104 1.8 christos hdr->magic = (uint32_t)-1;
105 1.6 christos return NULL;
106 1.6 christos }
107 1.6 christos
108 1.6 christos static int
109 1.6 christos npfd_log_getsnaplen(npfd_log_t *ctx)
110 1.6 christos {
111 1.6 christos struct pcap_file_header hdr;
112 1.6 christos FILE *fp = npfd_log_gethdr(ctx, &hdr);
113 1.6 christos if (fp == NULL)
114 1.6 christos return hdr.magic == (uint32_t)-1 ? -1 : 0;
115 1.6 christos fclose(fp);
116 1.6 christos return hdr.snaplen;
117 1.6 christos }
118 1.6 christos
119 1.6 christos static int
120 1.6 christos npfd_log_validate(npfd_log_t *ctx)
121 1.6 christos {
122 1.6 christos struct pcap_file_header hdr;
123 1.6 christos FILE *fp = npfd_log_gethdr(ctx, &hdr);
124 1.6 christos size_t o, no;
125 1.6 christos
126 1.6 christos if (fp == NULL) {
127 1.6 christos if (hdr.magic == 0)
128 1.6 christos return 0;
129 1.6 christos goto rename;
130 1.6 christos }
131 1.6 christos
132 1.6 christos struct stat st;
133 1.6 christos if (fstat(fileno(fp), &st) == -1)
134 1.6 christos goto rename;
135 1.6 christos
136 1.6 christos size_t count = 0;
137 1.6 christos for (o = sizeof(hdr);; count++) {
138 1.6 christos struct {
139 1.6 christos uint32_t sec;
140 1.6 christos uint32_t usec;
141 1.6 christos uint32_t caplen;
142 1.6 christos uint32_t len;
143 1.6 christos } pkt;
144 1.6 christos switch (fread(&pkt, sizeof(pkt), 1, fp)) {
145 1.6 christos case 0:
146 1.6 christos syslog(LOG_INFO, "%zu packets read from `%s'", count,
147 1.6 christos ctx->path);
148 1.6 christos fclose(fp);
149 1.6 christos return hdr.snaplen;
150 1.6 christos case 1:
151 1.6 christos no = o + sizeof(pkt) + pkt.caplen;
152 1.6 christos if (pkt.caplen > hdr.snaplen)
153 1.6 christos goto fix;
154 1.6 christos if (no > (size_t)st.st_size)
155 1.6 christos goto fix;
156 1.6 christos if (fseeko(fp, pkt.caplen, SEEK_CUR) != 0)
157 1.6 christos goto fix;
158 1.6 christos o = no;
159 1.6 christos break;
160 1.6 christos default:
161 1.6 christos goto fix;
162 1.6 christos }
163 1.6 christos }
164 1.6 christos
165 1.6 christos fix:
166 1.6 christos fclose(fp);
167 1.6 christos no = st.st_size - o;
168 1.6 christos syslog(LOG_INFO, "%zu packets read from `%s', %zu extra bytes",
169 1.6 christos count, ctx->path, no);
170 1.6 christos if (no < 10240) {
171 1.6 christos syslog(LOG_WARNING,
172 1.6 christos "Incomplete last packet in `%s', truncating",
173 1.6 christos ctx->path);
174 1.8 christos if (truncate(ctx->path, (off_t)o) == -1) {
175 1.6 christos syslog(LOG_ERR, "Cannot truncate `%s': %m", ctx->path);
176 1.6 christos goto rename;
177 1.6 christos }
178 1.6 christos } else {
179 1.6 christos syslog(LOG_ERR, "Corrupt file `%s'", ctx->path);
180 1.6 christos goto rename;
181 1.6 christos }
182 1.6 christos fclose(fp);
183 1.6 christos return hdr.snaplen;
184 1.6 christos rename:
185 1.6 christos fclose(fp);
186 1.6 christos char tmp[MAXPATHLEN];
187 1.6 christos snprintf(tmp, sizeof(tmp), "%s.XXXXXX", ctx->path);
188 1.6 christos int fd;
189 1.6 christos if ((fd = mkstemp(tmp)) == -1) {
190 1.6 christos syslog(LOG_ERR, "Can't make temp file `%s': %m", tmp);
191 1.6 christos return -1;
192 1.6 christos }
193 1.6 christos close(fd);
194 1.6 christos if (rename(ctx->path, tmp) == -1) {
195 1.6 christos syslog(LOG_ERR, "Can't rename `%s' to `%s': %m",
196 1.6 christos ctx->path, tmp);
197 1.6 christos return -1;
198 1.6 christos }
199 1.6 christos syslog(LOG_ERR, "Renamed to `%s'", tmp);
200 1.6 christos return 0;
201 1.6 christos }
202 1.6 christos
203 1.6 christos
204 1.1 rmind npfd_log_t *
205 1.6 christos npfd_log_create(const char *filename, const char *ifname, const char *filter,
206 1.6 christos int snaplen)
207 1.1 rmind {
208 1.1 rmind npfd_log_t *ctx;
209 1.1 rmind char errbuf[PCAP_ERRBUF_SIZE];
210 1.1 rmind
211 1.4 christos if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
212 1.4 christos err(EXIT_FAILURE, "malloc failed");
213 1.1 rmind
214 1.1 rmind /*
215 1.1 rmind * Open a live capture handle in non-blocking mode.
216 1.1 rmind */
217 1.4 christos snprintf(ctx->ifname, sizeof(ctx->ifname), "%s", ifname);
218 1.2 christos ctx->pcap = pcap_create(ctx->ifname, errbuf);
219 1.4 christos if (ctx->pcap == NULL)
220 1.4 christos errx(EXIT_FAILURE, "pcap_create failed: %s", errbuf);
221 1.1 rmind
222 1.4 christos if (pcap_setnonblock(ctx->pcap, 1, errbuf) == -1)
223 1.4 christos errx(EXIT_FAILURE, "pcap_setnonblock failed: %s", errbuf);
224 1.2 christos
225 1.6 christos if (filename == NULL)
226 1.6 christos snprintf(ctx->path, sizeof(ctx->path), NPFD_LOG_PATH "/%s.pcap",
227 1.6 christos ctx->ifname);
228 1.6 christos else
229 1.6 christos snprintf(ctx->path, sizeof(ctx->path), "%s", filename);
230 1.6 christos
231 1.6 christos int sl = npfd_log_getsnaplen(ctx);
232 1.6 christos if (sl == -1)
233 1.6 christos errx(EXIT_FAILURE, "corrupt log file `%s'", ctx->path);
234 1.6 christos
235 1.6 christos if (sl != 0 && sl != snaplen) {
236 1.6 christos warnx("Overriding snaplen from %d to %d from `%s'", snaplen,
237 1.6 christos sl, filename);
238 1.6 christos snaplen = sl;
239 1.6 christos }
240 1.6 christos
241 1.4 christos if (pcap_set_snaplen(ctx->pcap, snaplen) == -1)
242 1.4 christos errx(EXIT_FAILURE, "pcap_set_snaplen failed: %s",
243 1.3 christos pcap_geterr(ctx->pcap));
244 1.2 christos
245 1.5 christos if (pcap_set_timeout(ctx->pcap, 1000) == -1)
246 1.5 christos errx(EXIT_FAILURE, "pcap_set_timeout failed: %s",
247 1.5 christos pcap_geterr(ctx->pcap));
248 1.5 christos
249 1.4 christos if (pcap_activate(ctx->pcap) == -1)
250 1.4 christos errx(EXIT_FAILURE, "pcap_activate failed: %s",
251 1.4 christos pcap_geterr(ctx->pcap));
252 1.4 christos
253 1.4 christos if (filter)
254 1.4 christos npfd_log_setfilter(ctx, filter);
255 1.4 christos
256 1.2 christos
257 1.6 christos npfd_log_reopen(ctx, false);
258 1.1 rmind return ctx;
259 1.1 rmind }
260 1.1 rmind
261 1.2 christos bool
262 1.4 christos npfd_log_reopen(npfd_log_t *ctx, bool die)
263 1.2 christos {
264 1.7 christos mode_t omask = umask(077);
265 1.7 christos
266 1.2 christos if (ctx->dumper)
267 1.2 christos pcap_dump_close(ctx->dumper);
268 1.2 christos /*
269 1.2 christos * Open a log file to write for a given interface and dump there.
270 1.2 christos */
271 1.6 christos switch (npfd_log_validate(ctx)) {
272 1.6 christos case -1:
273 1.6 christos syslog(LOG_ERR, "Giving up");
274 1.6 christos exit(EXIT_FAILURE);
275 1.6 christos /*NOTREACHED*/
276 1.6 christos case 0:
277 1.6 christos ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
278 1.6 christos break;
279 1.6 christos default:
280 1.3 christos ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
281 1.6 christos break;
282 1.6 christos }
283 1.7 christos (void)umask(omask);
284 1.6 christos
285 1.2 christos if (ctx->dumper == NULL) {
286 1.4 christos if (die)
287 1.4 christos errx(EXIT_FAILURE, "pcap_dump_open failed for `%s': %s",
288 1.4 christos ctx->path, pcap_geterr(ctx->pcap));
289 1.3 christos syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
290 1.2 christos ctx->path, pcap_geterr(ctx->pcap));
291 1.2 christos return false;
292 1.2 christos }
293 1.2 christos return true;
294 1.2 christos }
295 1.2 christos
296 1.1 rmind void
297 1.1 rmind npfd_log_destroy(npfd_log_t *ctx)
298 1.1 rmind {
299 1.1 rmind if (ctx->dumper)
300 1.1 rmind pcap_dump_close(ctx->dumper);
301 1.1 rmind if (ctx->pcap)
302 1.1 rmind pcap_close(ctx->pcap);
303 1.1 rmind free(ctx);
304 1.1 rmind }
305 1.1 rmind
306 1.1 rmind int
307 1.1 rmind npfd_log_getsock(npfd_log_t *ctx)
308 1.1 rmind {
309 1.1 rmind return pcap_get_selectable_fd(ctx->pcap);
310 1.1 rmind }
311 1.1 rmind
312 1.1 rmind void
313 1.4 christos npfd_log_flush(npfd_log_t *ctx)
314 1.4 christos {
315 1.4 christos if (!ctx->dumper)
316 1.4 christos return;
317 1.4 christos if (pcap_dump_flush(ctx->dumper) == -1)
318 1.4 christos syslog(LOG_ERR, "pcap_dump_flush failed for `%s': %m",
319 1.4 christos ctx->path);
320 1.4 christos }
321 1.4 christos
322 1.4 christos
323 1.4 christos void
324 1.1 rmind npfd_log(npfd_log_t *ctx)
325 1.1 rmind {
326 1.1 rmind pcap_dumper_t *dumper = ctx->dumper;
327 1.1 rmind
328 1.8 christos pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (void *)dumper);
329 1.1 rmind }
330 1.1 rmind
331 1.1 rmind void
332 1.1 rmind npfd_log_stats(npfd_log_t *ctx)
333 1.1 rmind {
334 1.1 rmind pcap_t *pcap = ctx->pcap;
335 1.1 rmind struct pcap_stat ps;
336 1.1 rmind
337 1.2 christos if (pcap_stats(pcap, &ps) == -1) {
338 1.1 rmind syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
339 1.1 rmind return;
340 1.1 rmind }
341 1.4 christos syslog(LOG_INFO, "packet statistics: %s: %u received, %u dropped",
342 1.4 christos ctx->ifname, ps.ps_recv, ps.ps_drop);
343 1.1 rmind }
344