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