1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include "common.h" |
---|
27 | #include "config.h" |
---|
28 | #include "libtrace.h" |
---|
29 | #include "libtrace_int.h" |
---|
30 | #include "format_helper.h" |
---|
31 | |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <assert.h> |
---|
34 | #include <stdio.h> |
---|
35 | #include <stdlib.h> |
---|
36 | #include <string.h> |
---|
37 | #include <errno.h> |
---|
38 | |
---|
39 | #ifdef HAVE_PCAP_H |
---|
40 | # include <pcap.h> |
---|
41 | # ifdef HAVE_PCAP_INT_H |
---|
42 | # include <pcap-int.h> |
---|
43 | # endif |
---|
44 | #endif |
---|
45 | |
---|
46 | /* This format module deals with traces captured using the PCAP library. This |
---|
47 | * format module handles both captures from a live interface and from PCAP |
---|
48 | * files. |
---|
49 | * |
---|
50 | * However, the PCAP file support featured in this code is superceded by our |
---|
51 | * own implementation of the PCAP file format, called pcapfile. See |
---|
52 | * format_pcapfile.c for more information. |
---|
53 | * |
---|
54 | * Both the live and trace formats support writing, provided your PCAP library |
---|
55 | * also supports it. |
---|
56 | * |
---|
57 | */ |
---|
58 | |
---|
59 | /* Formats implemented in this module: |
---|
60 | * pcap - deals with PCAP trace files |
---|
61 | * pcapint - deals with live PCAP interfaces |
---|
62 | */ |
---|
63 | |
---|
64 | #ifdef HAVE_LIBPCAP |
---|
65 | static struct libtrace_format_t pcap; |
---|
66 | static struct libtrace_format_t pcapint; |
---|
67 | |
---|
68 | #define DATA(x) ((struct pcap_format_data_t*)((x)->format_data)) |
---|
69 | #define DATAOUT(x) ((struct pcap_format_data_out_t*)((x)->format_data)) |
---|
70 | |
---|
71 | #define INPUT DATA(libtrace)->input |
---|
72 | #define OUTPUT DATAOUT(libtrace)->output |
---|
73 | struct pcap_format_data_t { |
---|
74 | /** Information about the current state of the input trace */ |
---|
75 | union { |
---|
76 | /* The PCAP input source */ |
---|
77 | pcap_t *pcap; |
---|
78 | } input; |
---|
79 | /* A filter to be applied to all packets read from the source */ |
---|
80 | libtrace_filter_t *filter; |
---|
81 | /* The snap length to be applied to all captured packets (live only) */ |
---|
82 | int snaplen; |
---|
83 | /* Whether the capture interface should be set to promiscuous mode |
---|
84 | * (live only) */ |
---|
85 | int promisc; |
---|
86 | }; |
---|
87 | |
---|
88 | struct pcap_format_data_out_t { |
---|
89 | /* Information about the current state of the output trace */ |
---|
90 | union { |
---|
91 | struct { |
---|
92 | /* The PCAP output device or trace */ |
---|
93 | pcap_t *pcap; |
---|
94 | /* The PCAP dumper */ |
---|
95 | pcap_dumper_t *dump; |
---|
96 | } trace; |
---|
97 | |
---|
98 | } output; |
---|
99 | }; |
---|
100 | |
---|
101 | static int pcap_init_input(libtrace_t *libtrace) { |
---|
102 | libtrace->format_data = malloc(sizeof(struct pcap_format_data_t)); |
---|
103 | |
---|
104 | INPUT.pcap = NULL; |
---|
105 | DATA(libtrace)->filter = NULL; |
---|
106 | DATA(libtrace)->snaplen = LIBTRACE_PACKET_BUFSIZE; |
---|
107 | DATA(libtrace)->promisc = 0; |
---|
108 | |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | static int pcap_start_input(libtrace_t *libtrace) { |
---|
113 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
114 | |
---|
115 | |
---|
116 | /* Check if the file is already open */ |
---|
117 | if (INPUT.pcap) |
---|
118 | return 0; /* success */ |
---|
119 | |
---|
120 | /* Open the trace file for reading */ |
---|
121 | if ((INPUT.pcap = |
---|
122 | pcap_open_offline(libtrace->uridata, |
---|
123 | errbuf)) == NULL) { |
---|
124 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s", |
---|
125 | errbuf); |
---|
126 | return -1; |
---|
127 | } |
---|
128 | |
---|
129 | /* If a filter has been configured, compile and apply it */ |
---|
130 | #ifdef HAVE_BPF |
---|
131 | if (DATA(libtrace)->filter) { |
---|
132 | if (DATA(libtrace)->filter->flag == 0) { |
---|
133 | pcap_compile(INPUT.pcap, |
---|
134 | &DATA(libtrace)->filter->filter, |
---|
135 | DATA(libtrace)->filter->filterstring, |
---|
136 | 1, 0); |
---|
137 | DATA(libtrace)->filter->flag = 1; |
---|
138 | } |
---|
139 | if (pcap_setfilter(INPUT.pcap,&DATA(libtrace)->filter->filter) |
---|
140 | == -1) { |
---|
141 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s", |
---|
142 | pcap_geterr(INPUT.pcap)); |
---|
143 | return -1; |
---|
144 | } |
---|
145 | } |
---|
146 | #endif |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|
150 | static int pcap_config_input(libtrace_t *libtrace, |
---|
151 | trace_option_t option, |
---|
152 | void *data) |
---|
153 | { |
---|
154 | switch(option) { |
---|
155 | case TRACE_OPTION_FILTER: |
---|
156 | #ifdef HAVE_BPF |
---|
157 | DATA(libtrace)->filter=data; |
---|
158 | return 0; |
---|
159 | #else |
---|
160 | return -1; |
---|
161 | #endif |
---|
162 | case TRACE_OPTION_SNAPLEN: |
---|
163 | /* Snapping isn't supported directly, so fall thru |
---|
164 | * and let libtrace deal with it |
---|
165 | */ |
---|
166 | case TRACE_OPTION_PROMISC: |
---|
167 | /* Can't do promisc on a trace! */ |
---|
168 | case TRACE_OPTION_META_FREQ: |
---|
169 | /* No meta data for this format */ |
---|
170 | case TRACE_OPTION_EVENT_REALTIME: |
---|
171 | /* We do not support this option for PCAP traces */ |
---|
172 | default: |
---|
173 | return -1; |
---|
174 | } |
---|
175 | assert(0); |
---|
176 | } |
---|
177 | |
---|
178 | static int pcap_init_output(libtrace_out_t *libtrace) { |
---|
179 | libtrace->format_data = malloc(sizeof(struct pcap_format_data_out_t)); |
---|
180 | OUTPUT.trace.pcap = NULL; |
---|
181 | OUTPUT.trace.dump = NULL; |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int pcapint_init_output(libtrace_out_t *libtrace) { |
---|
186 | #ifdef HAVE_PCAP_INJECT |
---|
187 | libtrace->format_data = malloc(sizeof(struct pcap_format_data_out_t)); |
---|
188 | OUTPUT.trace.pcap = NULL; |
---|
189 | OUTPUT.trace.dump = NULL; |
---|
190 | return 0; |
---|
191 | #else |
---|
192 | #ifdef HAVE_PCAP_SENDPACKET |
---|
193 | libtrace->format_data = malloc(sizeof(struct pcap_format_data_out_t)); |
---|
194 | OUTPUT.trace.pcap = NULL; |
---|
195 | OUTPUT.trace.dump = NULL; |
---|
196 | return 0; |
---|
197 | #else |
---|
198 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
199 | "writing not supported by this version of pcap"); |
---|
200 | return -1; |
---|
201 | #endif |
---|
202 | #endif |
---|
203 | } |
---|
204 | |
---|
205 | static int pcapint_init_input(libtrace_t *libtrace) { |
---|
206 | libtrace->format_data = malloc(sizeof(struct pcap_format_data_t)); |
---|
207 | DATA(libtrace)->filter = NULL; |
---|
208 | DATA(libtrace)->snaplen = LIBTRACE_PACKET_BUFSIZE; |
---|
209 | DATA(libtrace)->promisc = 0; |
---|
210 | return 0; /* success */ |
---|
211 | } |
---|
212 | |
---|
213 | static int pcapint_config_input(libtrace_t *libtrace, |
---|
214 | trace_option_t option, |
---|
215 | void *data) |
---|
216 | { |
---|
217 | switch(option) { |
---|
218 | case TRACE_OPTION_FILTER: |
---|
219 | #ifdef HAVE_BPF |
---|
220 | DATA(libtrace)->filter=(libtrace_filter_t*)data; |
---|
221 | return 0; |
---|
222 | #else |
---|
223 | return -1; |
---|
224 | #endif |
---|
225 | case TRACE_OPTION_SNAPLEN: |
---|
226 | DATA(libtrace)->snaplen=*(int*)data; |
---|
227 | return 0; |
---|
228 | case TRACE_OPTION_PROMISC: |
---|
229 | DATA(libtrace)->promisc=*(int*)data; |
---|
230 | return 0; |
---|
231 | case TRACE_OPTION_META_FREQ: |
---|
232 | /* No meta-data for this format */ |
---|
233 | case TRACE_OPTION_EVENT_REALTIME: |
---|
234 | /* live interface is always real-time! */ |
---|
235 | default: |
---|
236 | /* Don't set an error here - trace_config will try |
---|
237 | * to handle the option when we return. If it can't |
---|
238 | * deal with it, then it will do the necessary |
---|
239 | * error-setting. */ |
---|
240 | return -1; |
---|
241 | } |
---|
242 | assert(0); |
---|
243 | } |
---|
244 | |
---|
245 | static int pcapint_start_input(libtrace_t *libtrace) { |
---|
246 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
247 | |
---|
248 | #ifdef HAVE_PCAP_CREATE |
---|
249 | int ret = 0; |
---|
250 | |
---|
251 | if ((INPUT.pcap = pcap_create(libtrace->uridata, errbuf)) == NULL) { |
---|
252 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
253 | return -1; /* failure */ |
---|
254 | } |
---|
255 | if ((pcap_set_snaplen(INPUT.pcap, DATA(libtrace)->snaplen) == |
---|
256 | PCAP_ERROR_ACTIVATED)) { |
---|
257 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
258 | return -1; /* failure */ |
---|
259 | } |
---|
260 | |
---|
261 | if ((pcap_set_promisc(INPUT.pcap, DATA(libtrace)->promisc) == |
---|
262 | PCAP_ERROR_ACTIVATED)) { |
---|
263 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
264 | return -1; /* failure */ |
---|
265 | } |
---|
266 | |
---|
267 | if ((pcap_set_timeout(INPUT.pcap, 1) == PCAP_ERROR_ACTIVATED)) { |
---|
268 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
269 | return -1; /* failure */ |
---|
270 | } |
---|
271 | |
---|
272 | #ifdef HAVE_PCAP_IMMEDIATE |
---|
273 | if ((pcap_set_immediate_mode(INPUT.pcap, 1) == PCAP_ERROR_ACTIVATED)) { |
---|
274 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
275 | return -1; /* failure */ |
---|
276 | } |
---|
277 | #endif |
---|
278 | |
---|
279 | if ((ret = pcap_activate(INPUT.pcap)) != 0) { |
---|
280 | if (ret == PCAP_WARNING_PROMISC_NOTSUP) { |
---|
281 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,"Promiscuous mode unsupported"); |
---|
282 | return -1; |
---|
283 | } |
---|
284 | if (ret == PCAP_WARNING) { |
---|
285 | pcap_perror(INPUT.pcap, "Pcap Warning:"); |
---|
286 | } else { |
---|
287 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s", |
---|
288 | pcap_geterr(INPUT.pcap)); |
---|
289 | return -1; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | #else |
---|
294 | |
---|
295 | /* Open the live device */ |
---|
296 | if ((INPUT.pcap = |
---|
297 | pcap_open_live(libtrace->uridata, |
---|
298 | DATA(libtrace)->snaplen, |
---|
299 | DATA(libtrace)->promisc, |
---|
300 | 1, |
---|
301 | errbuf)) == NULL) { |
---|
302 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s",errbuf); |
---|
303 | return -1; /* failure */ |
---|
304 | } |
---|
305 | #endif |
---|
306 | #ifdef HAVE_PCAP_SETNONBLOCK |
---|
307 | pcap_setnonblock(INPUT.pcap,0,errbuf); |
---|
308 | #endif |
---|
309 | /* Set a filter if one is defined */ |
---|
310 | #ifdef HAVE_BPF |
---|
311 | if (DATA(libtrace)->filter) { |
---|
312 | struct pcap_pkthdr *pcap_hdr = NULL; |
---|
313 | u_char *pcap_payload = NULL; |
---|
314 | int pcapret; |
---|
315 | |
---|
316 | if (DATA(libtrace)->filter->flag == 0) { |
---|
317 | pcap_compile(INPUT.pcap, |
---|
318 | &DATA(libtrace)->filter->filter, |
---|
319 | DATA(libtrace)->filter->filterstring, |
---|
320 | 1, 0); |
---|
321 | DATA(libtrace)->filter->flag = 1; |
---|
322 | } |
---|
323 | if (pcap_setfilter(INPUT.pcap,&DATA(libtrace)->filter->filter) |
---|
324 | == -1) { |
---|
325 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"%s", |
---|
326 | pcap_geterr(INPUT.pcap)); |
---|
327 | return -1; /* failure */ |
---|
328 | } |
---|
329 | |
---|
330 | /* Consume the first packet in the queue, as this may not |
---|
331 | * have had the filter applied to it. |
---|
332 | * |
---|
333 | * Otherwise we can get problems with the event API, where |
---|
334 | * select tells us that there is a packet available but |
---|
335 | * calling trace_read_packet will block forever because the |
---|
336 | * packet in the queue didn't match the filter so |
---|
337 | * pcap_next_ex returns "timed out". |
---|
338 | * |
---|
339 | * This does mean we may consume a legitimate packet, but |
---|
340 | * that's a pretty small downside compared with trace_event |
---|
341 | * getting stuck in an infinite loop because of pcap |
---|
342 | * wackiness. |
---|
343 | * |
---|
344 | * For some reason, we only need to consume one packet for |
---|
345 | * this to work, so let's hope that holds in the future. |
---|
346 | */ |
---|
347 | do { |
---|
348 | pcapret = pcap_next_ex(INPUT.pcap, &pcap_hdr, |
---|
349 | (const u_char **)&pcap_payload); |
---|
350 | } while (0); |
---|
351 | |
---|
352 | if (pcapret < 0) |
---|
353 | return -1; |
---|
354 | } |
---|
355 | #endif |
---|
356 | return 0; /* success */ |
---|
357 | } |
---|
358 | |
---|
359 | static int pcap_pause_input(libtrace_t *libtrace UNUSED) |
---|
360 | { |
---|
361 | return 0; /* success */ |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | static int pcap_fin_input(libtrace_t *libtrace) |
---|
366 | { |
---|
367 | pcap_close(INPUT.pcap); |
---|
368 | INPUT.pcap=NULL; |
---|
369 | free(libtrace->format_data); |
---|
370 | return 0; /* success */ |
---|
371 | } |
---|
372 | |
---|
373 | static int pcap_fin_output(libtrace_out_t *libtrace) |
---|
374 | { |
---|
375 | if (OUTPUT.trace.dump) { |
---|
376 | pcap_dump_flush(OUTPUT.trace.dump); |
---|
377 | pcap_dump_close(OUTPUT.trace.dump); |
---|
378 | } |
---|
379 | pcap_close(OUTPUT.trace.pcap); |
---|
380 | free(libtrace->format_data); |
---|
381 | return 0; |
---|
382 | } |
---|
383 | |
---|
384 | static int pcapint_fin_output(libtrace_out_t *libtrace) |
---|
385 | { |
---|
386 | pcap_close(OUTPUT.trace.pcap); |
---|
387 | free(libtrace->format_data); |
---|
388 | return 0; |
---|
389 | } |
---|
390 | |
---|
391 | static int pcap_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, |
---|
392 | void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
393 | |
---|
394 | if (packet->buffer != buffer && |
---|
395 | packet->buf_control == TRACE_CTRL_PACKET) { |
---|
396 | free(packet->buffer); |
---|
397 | } |
---|
398 | |
---|
399 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
---|
400 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
401 | } else |
---|
402 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
403 | |
---|
404 | |
---|
405 | packet->buffer = buffer; |
---|
406 | packet->header = buffer; |
---|
407 | packet->type = rt_type; |
---|
408 | |
---|
409 | /* Assuming header and payload are sequential in the buffer - |
---|
410 | * regular pcap often doesn't work like this though, so hopefully |
---|
411 | * we're not called by something that is reading genuine pcap! */ |
---|
412 | packet->payload = (char *)packet->header + sizeof(struct pcap_pkthdr); |
---|
413 | |
---|
414 | if (libtrace->format_data == NULL) { |
---|
415 | if (pcap_init_input(libtrace)) |
---|
416 | return -1; |
---|
417 | } |
---|
418 | return 0; |
---|
419 | } |
---|
420 | |
---|
421 | static int pcap_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
422 | int ret = 0; |
---|
423 | int linktype; |
---|
424 | uint32_t flags = 0; |
---|
425 | |
---|
426 | assert(libtrace->format_data); |
---|
427 | linktype = pcap_datalink(DATA(libtrace)->input.pcap); |
---|
428 | packet->type = pcap_linktype_to_rt(linktype); |
---|
429 | |
---|
430 | /* If we're using the replacement pcap_next_ex() we need to |
---|
431 | * make sure we have a buffer to *shudder* memcpy into |
---|
432 | */ |
---|
433 | if (!packet->buffer) { |
---|
434 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
435 | if (!packet->buffer) { |
---|
436 | trace_set_err(libtrace, errno, |
---|
437 | "Cannot allocate memory"); |
---|
438 | return -1; |
---|
439 | } |
---|
440 | packet->header = packet->buffer; |
---|
441 | packet->payload = (char *)packet->buffer+sizeof(struct pcap_pkthdr); |
---|
442 | |
---|
443 | } |
---|
444 | |
---|
445 | flags |= TRACE_PREP_OWN_BUFFER; |
---|
446 | |
---|
447 | for(;;) { |
---|
448 | |
---|
449 | struct pcap_pkthdr *pcap_hdr = NULL; |
---|
450 | u_char *pcap_payload = NULL; |
---|
451 | |
---|
452 | ret = pcap_next_ex(INPUT.pcap, &pcap_hdr, |
---|
453 | (const u_char **)&pcap_payload); |
---|
454 | |
---|
455 | packet->header = pcap_hdr; |
---|
456 | packet->payload = pcap_payload; |
---|
457 | |
---|
458 | switch(ret) { |
---|
459 | case 1: break; /* no error */ |
---|
460 | case 0: |
---|
461 | if ((ret=is_halted(libtrace)) != -1) |
---|
462 | return ret; |
---|
463 | continue; /* timeout expired */ |
---|
464 | case -1: |
---|
465 | trace_set_err(libtrace,TRACE_ERR_BAD_PACKET, |
---|
466 | "%s",pcap_geterr(INPUT.pcap)); |
---|
467 | return -1; /* Error */ |
---|
468 | case -2: |
---|
469 | return 0; /* EOF */ |
---|
470 | } |
---|
471 | |
---|
472 | /* |
---|
473 | * pcap is nasty in that the header and payload aren't |
---|
474 | * necessarily located sequentially in memory, but most |
---|
475 | * sensible uses of pcap_prepare_packet will involve a |
---|
476 | * buffer where header and payload are sequential. |
---|
477 | * |
---|
478 | * Basically, don't call pcap_prepare_packet here! |
---|
479 | * |
---|
480 | if (pcap_prepare_packet(libtrace, packet, packet->buffer, |
---|
481 | packet->type, flags)) { |
---|
482 | return -1; |
---|
483 | } |
---|
484 | */ |
---|
485 | return ((struct pcap_pkthdr*)packet->header)->len |
---|
486 | +sizeof(struct pcap_pkthdr); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | static int pcap_write_packet(libtrace_out_t *libtrace, |
---|
491 | libtrace_packet_t *packet) |
---|
492 | { |
---|
493 | struct pcap_pkthdr pcap_pkt_hdr; |
---|
494 | void *link; |
---|
495 | libtrace_linktype_t linktype; |
---|
496 | uint32_t remaining; |
---|
497 | |
---|
498 | link = trace_get_packet_buffer(packet,&linktype,&remaining); |
---|
499 | |
---|
500 | /* We may have to convert this packet into a suitable PCAP packet */ |
---|
501 | |
---|
502 | /* If this packet cannot be converted to a pcap linktype then |
---|
503 | * pop off the top header until it can be converted |
---|
504 | */ |
---|
505 | while (libtrace_to_pcap_linktype(linktype)==TRACE_DLT_ERROR) { |
---|
506 | if (!demote_packet(packet)) { |
---|
507 | trace_set_err_out(libtrace, |
---|
508 | TRACE_ERR_NO_CONVERSION, |
---|
509 | "pcap does not support this format"); |
---|
510 | return -1; |
---|
511 | } |
---|
512 | |
---|
513 | link = trace_get_packet_buffer(packet,&linktype,&remaining); |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | if (!OUTPUT.trace.pcap) { |
---|
518 | int linktype=libtrace_to_pcap_dlt(trace_get_link_type(packet)); |
---|
519 | OUTPUT.trace.pcap = pcap_open_dead(linktype,65536); |
---|
520 | if (!OUTPUT.trace.pcap) { |
---|
521 | trace_set_err_out(libtrace,TRACE_ERR_INIT_FAILED, |
---|
522 | "Failed to open dead trace: %s\n", |
---|
523 | pcap_geterr(OUTPUT.trace.pcap)); |
---|
524 | } |
---|
525 | OUTPUT.trace.dump = pcap_dump_open(OUTPUT.trace.pcap, |
---|
526 | libtrace->uridata); |
---|
527 | if (!OUTPUT.trace.dump) { |
---|
528 | char *errmsg = pcap_geterr(OUTPUT.trace.pcap); |
---|
529 | trace_set_err_out(libtrace,TRACE_ERR_INIT_FAILED,"Failed to open output file: %s\n", |
---|
530 | errmsg ? errmsg : "Unknown error"); |
---|
531 | return -1; |
---|
532 | } |
---|
533 | } |
---|
534 | |
---|
535 | /* Corrupt packet, or other "non data" packet, so skip it */ |
---|
536 | if (link == NULL) { |
---|
537 | /* Return "success", but nothing written */ |
---|
538 | return 0; |
---|
539 | } |
---|
540 | |
---|
541 | /* Check if the packet was captured using one of the PCAP formats */ |
---|
542 | if (packet->trace->format == &pcap || |
---|
543 | packet->trace->format == &pcapint) { |
---|
544 | /* Yes - this means we can write it straight out */ |
---|
545 | pcap_dump((u_char*)OUTPUT.trace.dump, |
---|
546 | (struct pcap_pkthdr *)packet->header, |
---|
547 | packet->payload); |
---|
548 | } else { |
---|
549 | /* No - need to fill in a PCAP header with the appropriate |
---|
550 | * values */ |
---|
551 | |
---|
552 | /* Leave the manual copy as it is, as it gets around |
---|
553 | * some OS's having different structures in pcap_pkt_hdr |
---|
554 | */ |
---|
555 | struct timeval ts = trace_get_timeval(packet); |
---|
556 | pcap_pkt_hdr.ts.tv_sec = ts.tv_sec; |
---|
557 | pcap_pkt_hdr.ts.tv_usec = ts.tv_usec; |
---|
558 | pcap_pkt_hdr.caplen = remaining; |
---|
559 | /* trace_get_wire_length includes FCS, while pcap doesn't */ |
---|
560 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
561 | if (trace_get_wire_length(packet) >= 4) { |
---|
562 | pcap_pkt_hdr.len = |
---|
563 | trace_get_wire_length(packet)-4; |
---|
564 | } |
---|
565 | else { |
---|
566 | pcap_pkt_hdr.len = 0; |
---|
567 | } |
---|
568 | else |
---|
569 | pcap_pkt_hdr.len = trace_get_wire_length(packet); |
---|
570 | |
---|
571 | assert(pcap_pkt_hdr.caplen<65536); |
---|
572 | assert(pcap_pkt_hdr.len<65536); |
---|
573 | |
---|
574 | pcap_dump((u_char*)OUTPUT.trace.dump, &pcap_pkt_hdr, packet->payload); |
---|
575 | } |
---|
576 | return remaining; |
---|
577 | } |
---|
578 | |
---|
579 | static int pcapint_write_packet(libtrace_out_t *libtrace, |
---|
580 | libtrace_packet_t *packet) |
---|
581 | { |
---|
582 | int err; |
---|
583 | |
---|
584 | if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA) |
---|
585 | return 0; |
---|
586 | |
---|
587 | if (!OUTPUT.trace.pcap) { |
---|
588 | OUTPUT.trace.pcap = (pcap_t *)pcap_open_live( |
---|
589 | libtrace->uridata,65536,0,0,NULL); |
---|
590 | } |
---|
591 | #ifdef HAVE_PCAP_INJECT |
---|
592 | err=pcap_inject(OUTPUT.trace.pcap, |
---|
593 | packet->payload, |
---|
594 | trace_get_capture_length(packet)); |
---|
595 | if (err!=(int)trace_get_capture_length(packet)) |
---|
596 | err=-1; |
---|
597 | #else |
---|
598 | #ifdef HAVE_PCAP_SENDPACKET |
---|
599 | err=pcap_sendpacket(OUTPUT.trace.pcap, |
---|
600 | packet->payload, |
---|
601 | trace_get_capture_length(packet)); |
---|
602 | #else |
---|
603 | trace_set_err(packet->trace,TRACE_ERR_UNSUPPORTED,"writing is not supported on this platform"); |
---|
604 | return -1; |
---|
605 | #endif |
---|
606 | #endif |
---|
607 | return err; |
---|
608 | } |
---|
609 | |
---|
610 | static libtrace_linktype_t pcap_get_link_type(const libtrace_packet_t *packet) { |
---|
611 | /* PCAP doesn't store linktype in the framing header so we need |
---|
612 | * RT to do it for us |
---|
613 | */ |
---|
614 | int linktype = rt_to_pcap_linktype(packet->type); |
---|
615 | return pcap_linktype_to_libtrace(linktype); |
---|
616 | } |
---|
617 | |
---|
618 | static libtrace_direction_t pcap_set_direction(libtrace_packet_t *packet, |
---|
619 | libtrace_direction_t dir) { |
---|
620 | |
---|
621 | /* We only support tagging with IN or OUT return error for any others */ |
---|
622 | if(!(dir == TRACE_DIR_OUTGOING || dir == TRACE_DIR_INCOMING)) |
---|
623 | return -1; |
---|
624 | |
---|
625 | /* PCAP doesn't have a direction field in the header, so we need to |
---|
626 | * promote to Linux SLL to tag it properly */ |
---|
627 | libtrace_sll_header_t *sll; |
---|
628 | promote_packet(packet); |
---|
629 | sll=packet->payload; |
---|
630 | |
---|
631 | /* sll->pkttype should be in the endianness of the host that the |
---|
632 | * trace was taken on. This is impossible to achieve so we assume |
---|
633 | * host endianness |
---|
634 | */ |
---|
635 | if(dir==TRACE_DIR_OUTGOING) |
---|
636 | sll->pkttype=TRACE_SLL_OUTGOING; |
---|
637 | else |
---|
638 | sll->pkttype=TRACE_SLL_HOST; |
---|
639 | return dir; |
---|
640 | } |
---|
641 | |
---|
642 | static libtrace_direction_t pcapint_get_direction(const libtrace_packet_t *packet) { |
---|
643 | /* This function is defined in format_helper.c */ |
---|
644 | return pcap_get_direction(packet); |
---|
645 | } |
---|
646 | |
---|
647 | |
---|
648 | static struct timeval pcap_get_timeval(const libtrace_packet_t *packet) { |
---|
649 | struct pcap_pkthdr *pcapptr = (struct pcap_pkthdr *)packet->header; |
---|
650 | struct timeval ts; |
---|
651 | ts.tv_sec = pcapptr->ts.tv_sec; |
---|
652 | ts.tv_usec = pcapptr->ts.tv_usec; |
---|
653 | return ts; |
---|
654 | } |
---|
655 | |
---|
656 | |
---|
657 | static int pcap_get_capture_length(const libtrace_packet_t *packet) { |
---|
658 | struct pcap_pkthdr *pcapptr = 0; |
---|
659 | pcapptr = (struct pcap_pkthdr *)packet->header; |
---|
660 | assert(pcapptr->caplen<=65536); |
---|
661 | |
---|
662 | return pcapptr->caplen; |
---|
663 | } |
---|
664 | |
---|
665 | static int pcap_get_wire_length(const libtrace_packet_t *packet) { |
---|
666 | struct pcap_pkthdr *pcapptr = 0; |
---|
667 | pcapptr = (struct pcap_pkthdr *)packet->header; |
---|
668 | if (packet->type==pcap_linktype_to_rt(TRACE_DLT_EN10MB)) |
---|
669 | return pcapptr->len+4; /* Include the missing FCS */ |
---|
670 | else if (packet->type==pcap_linktype_to_rt(TRACE_DLT_IEEE802_11_RADIO)) { |
---|
671 | libtrace_linktype_t linktype; |
---|
672 | void *link = trace_get_packet_buffer(packet,&linktype,NULL); |
---|
673 | /* If the packet is Radiotap and the flags field indicates |
---|
674 | * that the FCS is not included in the 802.11 frame, then |
---|
675 | * we need to add 4 to the wire-length to account for it. |
---|
676 | */ |
---|
677 | uint8_t flags; |
---|
678 | trace_get_wireless_flags(link, |
---|
679 | linktype, &flags); |
---|
680 | if ((flags & TRACE_RADIOTAP_F_FCS) == 0) |
---|
681 | return pcapptr->len + 4; |
---|
682 | } |
---|
683 | return pcapptr->len; |
---|
684 | } |
---|
685 | |
---|
686 | static int pcap_get_framing_length(UNUSED const libtrace_packet_t *packet) { |
---|
687 | return sizeof(struct pcap_pkthdr); |
---|
688 | } |
---|
689 | |
---|
690 | static size_t pcap_set_capture_length(libtrace_packet_t *packet,size_t size) { |
---|
691 | struct pcap_pkthdr *pcapptr = 0; |
---|
692 | assert(packet); |
---|
693 | if (size > trace_get_capture_length(packet)) { |
---|
694 | /* Can't make a packet larger */ |
---|
695 | return trace_get_capture_length(packet); |
---|
696 | } |
---|
697 | /* Reset the cached capture length */ |
---|
698 | packet->capture_length = -1; |
---|
699 | pcapptr = (struct pcap_pkthdr *)packet->header; |
---|
700 | pcapptr->caplen = size; |
---|
701 | return trace_get_capture_length(packet); |
---|
702 | } |
---|
703 | |
---|
704 | static int pcap_get_fd(const libtrace_t *trace) { |
---|
705 | |
---|
706 | assert(trace->format_data); |
---|
707 | return pcap_fileno(DATA(trace)->input.pcap); |
---|
708 | } |
---|
709 | |
---|
710 | static void pcap_get_statistics(libtrace_t *trace, libtrace_stat_t *stat) { |
---|
711 | |
---|
712 | struct pcap_stat pcapstats; |
---|
713 | if (pcap_stats(DATA(trace)->input.pcap,&pcapstats)==-1) { |
---|
714 | char *errmsg = pcap_geterr(DATA(trace)->input.pcap); |
---|
715 | trace_set_err(trace,TRACE_ERR_UNSUPPORTED, |
---|
716 | "Failed to retrieve stats: %s\n", |
---|
717 | errmsg ? errmsg : "Unknown pcap error"); |
---|
718 | return; |
---|
719 | } |
---|
720 | |
---|
721 | stat->received_valid = 1; |
---|
722 | stat->received = pcapstats.ps_recv; |
---|
723 | stat->dropped_valid = 1; |
---|
724 | stat->dropped = pcapstats.ps_drop; |
---|
725 | } |
---|
726 | |
---|
727 | static void pcap_help(void) { |
---|
728 | printf("pcap format module: $Revision: 1729 $\n"); |
---|
729 | printf("Supported input URIs:\n"); |
---|
730 | printf("\tpcap:/path/to/file\n"); |
---|
731 | printf("\n"); |
---|
732 | printf("\te.g.: pcap:/tmp/trace.pcap\n"); |
---|
733 | printf("\n"); |
---|
734 | printf("Supported output URIs:\n"); |
---|
735 | printf("\tnone\n"); |
---|
736 | printf("\n"); |
---|
737 | } |
---|
738 | |
---|
739 | static void pcapint_help(void) { |
---|
740 | printf("pcapint format module: $Revision: 1729 $\n"); |
---|
741 | printf("Supported input URIs:\n"); |
---|
742 | printf("\tpcapint:interface\n"); |
---|
743 | printf("\n"); |
---|
744 | printf("\te.g.: pcapint:eth0\n"); |
---|
745 | printf("\n"); |
---|
746 | printf("Supported output URIs:\n"); |
---|
747 | printf("\tnone\n"); |
---|
748 | printf("\n"); |
---|
749 | } |
---|
750 | |
---|
751 | |
---|
752 | static struct libtrace_format_t pcap = { |
---|
753 | "pcap", |
---|
754 | "$Id$", |
---|
755 | TRACE_FORMAT_PCAP, |
---|
756 | NULL, /* probe filename */ |
---|
757 | NULL, /* probe magic */ |
---|
758 | pcap_init_input, /* init_input */ |
---|
759 | pcap_config_input, /* config_input */ |
---|
760 | pcap_start_input, /* start_input */ |
---|
761 | pcap_pause_input, /* pause_input */ |
---|
762 | pcap_init_output, /* init_output */ |
---|
763 | NULL, /* config_output */ |
---|
764 | NULL, /* start_output */ |
---|
765 | pcap_fin_input, /* fin_input */ |
---|
766 | pcap_fin_output, /* fin_output */ |
---|
767 | pcap_read_packet, /* read_packet */ |
---|
768 | pcap_prepare_packet, /* prepare_packet */ |
---|
769 | NULL, /* fin_packet */ |
---|
770 | pcap_write_packet, /* write_packet */ |
---|
771 | pcap_get_link_type, /* get_link_type */ |
---|
772 | pcapint_get_direction, /* get_direction */ |
---|
773 | pcap_set_direction, /* set_direction */ |
---|
774 | NULL, /* get_erf_timestamp */ |
---|
775 | pcap_get_timeval, /* get_timeval */ |
---|
776 | NULL, /* get_seconds */ |
---|
777 | NULL, /* get_timespec */ |
---|
778 | NULL, /* seek_erf */ |
---|
779 | NULL, /* seek_timeval */ |
---|
780 | NULL, /* seek_seconds */ |
---|
781 | pcap_get_capture_length, /* get_capture_length */ |
---|
782 | pcap_get_wire_length, /* get_wire_length */ |
---|
783 | pcap_get_framing_length, /* get_framing_length */ |
---|
784 | pcap_set_capture_length, /* set_capture_length */ |
---|
785 | NULL, /* get_received_packets */ |
---|
786 | NULL, /* get_filtered_packets */ |
---|
787 | NULL, /* get_dropped_packets */ |
---|
788 | NULL, /* get_statistics */ |
---|
789 | NULL, /* get_fd */ |
---|
790 | trace_event_trace, /* trace_event */ |
---|
791 | pcap_help, /* help */ |
---|
792 | NULL, /* next pointer */ |
---|
793 | NON_PARALLEL(false) |
---|
794 | }; |
---|
795 | |
---|
796 | static struct libtrace_format_t pcapint = { |
---|
797 | "pcapint", |
---|
798 | "$Id$", |
---|
799 | TRACE_FORMAT_PCAP, |
---|
800 | NULL, /* probe filename */ |
---|
801 | NULL, /* probe magic */ |
---|
802 | pcapint_init_input, /* init_input */ |
---|
803 | pcapint_config_input, /* config_input */ |
---|
804 | pcapint_start_input, /* start_input */ |
---|
805 | pcap_pause_input, /* pause_input */ |
---|
806 | pcapint_init_output, /* init_output */ |
---|
807 | NULL, /* config_output */ |
---|
808 | NULL, /* start_output */ |
---|
809 | pcap_fin_input, /* fin_input */ |
---|
810 | pcapint_fin_output, /* fin_output */ |
---|
811 | pcap_read_packet, /* read_packet */ |
---|
812 | pcap_prepare_packet, /* prepare_packet */ |
---|
813 | NULL, /* fin_packet */ |
---|
814 | pcapint_write_packet, /* write_packet */ |
---|
815 | pcap_get_link_type, /* get_link_type */ |
---|
816 | pcapint_get_direction, /* get_direction */ |
---|
817 | pcap_set_direction, /* set_direction */ |
---|
818 | NULL, /* get_erf_timestamp */ |
---|
819 | pcap_get_timeval, /* get_timeval */ |
---|
820 | NULL, /* get_seconds */ |
---|
821 | NULL, /* get_timespec */ |
---|
822 | NULL, /* seek_erf */ |
---|
823 | NULL, /* seek_timeval */ |
---|
824 | NULL, /* seek_seconds */ |
---|
825 | pcap_get_capture_length, /* get_capture_length */ |
---|
826 | pcap_get_wire_length, /* get_wire_length */ |
---|
827 | pcap_get_framing_length, /* get_framing_length */ |
---|
828 | pcap_set_capture_length, /* set_capture_length */ |
---|
829 | NULL, /* get_received_packets */ |
---|
830 | NULL, /* get_filtered_packets */ |
---|
831 | NULL, /* get_dropped_packets */ |
---|
832 | pcap_get_statistics, /* get_statistics */ |
---|
833 | pcap_get_fd, /* get_fd */ |
---|
834 | trace_event_device, /* trace_event */ |
---|
835 | pcapint_help, /* help */ |
---|
836 | NULL, /* next pointer */ |
---|
837 | NON_PARALLEL(true) |
---|
838 | }; |
---|
839 | |
---|
840 | void pcap_constructor(void) { |
---|
841 | register_format(&pcap); |
---|
842 | register_format(&pcapint); |
---|
843 | } |
---|
844 | |
---|
845 | |
---|
846 | #endif |
---|