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