1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Daniel Lawson |
---|
8 | * Perry Lorier |
---|
9 | * Shane Alcock |
---|
10 | * |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This code has been developed by the University of Waikato WAND |
---|
14 | * research group. For further information please see http://www.wand.net.nz/ |
---|
15 | * |
---|
16 | * libtrace is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * libtrace is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with libtrace; if not, write to the Free Software |
---|
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
29 | * |
---|
30 | * $Id$ |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #include "libtrace.h" |
---|
36 | #include "libtrace_int.h" |
---|
37 | #include "format_helper.h" |
---|
38 | #include "config.h" |
---|
39 | #include <stdlib.h> |
---|
40 | |
---|
41 | #ifdef HAVE_INTTYPES_H |
---|
42 | # include <inttypes.h> |
---|
43 | #else |
---|
44 | # error "Can't find inttypes.h" |
---|
45 | #endif |
---|
46 | |
---|
47 | #include <sys/socket.h> |
---|
48 | #include <netpacket/packet.h> |
---|
49 | #include <net/ethernet.h> |
---|
50 | #include <net/if_arp.h> |
---|
51 | |
---|
52 | #include <string.h> |
---|
53 | #include <net/if.h> |
---|
54 | #include <sys/ioctl.h> |
---|
55 | #include <errno.h> |
---|
56 | #include <unistd.h> |
---|
57 | |
---|
58 | #include <assert.h> |
---|
59 | |
---|
60 | /* This format module deals with using the Linux Native capture format. |
---|
61 | * |
---|
62 | * Linux Native is a LIVE capture format. |
---|
63 | * |
---|
64 | * This format also supports writing which will write packets out to the |
---|
65 | * network as a form of packet replay. This should not be confused with the |
---|
66 | * RT protocol which is intended to transfer captured packet records between |
---|
67 | * RT-speaking programs. |
---|
68 | */ |
---|
69 | |
---|
70 | /* Declared in linux/if_arp.h but not in net/if_arp.h sigh */ |
---|
71 | #ifndef ARPHRD_NONE |
---|
72 | #define ARPHRD_NONE 0xfffe |
---|
73 | #endif |
---|
74 | |
---|
75 | struct tpacket_stats { |
---|
76 | unsigned int tp_packets; |
---|
77 | unsigned int tp_drops; |
---|
78 | }; |
---|
79 | |
---|
80 | typedef enum { TS_NONE, TS_TIMEVAL, TS_TIMESPEC } timestamptype_t; |
---|
81 | |
---|
82 | struct linux_format_data_t { |
---|
83 | /* The file descriptor being used for the capture */ |
---|
84 | int fd; |
---|
85 | /* The snap length for the capture */ |
---|
86 | int snaplen; |
---|
87 | /* Flag indicating whether the interface should be placed in |
---|
88 | * promiscuous mode */ |
---|
89 | int promisc; |
---|
90 | /* The timestamp format used by the capture */ |
---|
91 | timestamptype_t timestamptype; |
---|
92 | /* A BPF filter that is applied to every captured packet */ |
---|
93 | libtrace_filter_t *filter; |
---|
94 | /* Statistics for the capture process, e.g. dropped packet counts */ |
---|
95 | struct tpacket_stats stats; |
---|
96 | /* Flag indicating whether the statistics are current or not */ |
---|
97 | int stats_valid; |
---|
98 | }; |
---|
99 | |
---|
100 | |
---|
101 | /* Note that this structure is passed over the wire in rt encapsulation, and |
---|
102 | * thus we need to be careful with data sizes. timeval's and timespec's |
---|
103 | * can also change their size on 32/64 machines. |
---|
104 | */ |
---|
105 | |
---|
106 | /* Format header for encapsulating packets captured using linux native */ |
---|
107 | struct libtrace_linuxnative_header { |
---|
108 | /* Timestamp of the packet, as a timeval */ |
---|
109 | struct { |
---|
110 | uint32_t tv_sec; |
---|
111 | uint32_t tv_usec; |
---|
112 | } tv; |
---|
113 | /* Timestamp of the packet, as a timespec */ |
---|
114 | struct { |
---|
115 | uint32_t tv_sec; |
---|
116 | uint32_t tv_nsec; |
---|
117 | } ts; |
---|
118 | /* The timestamp format used by the process that captured this packet */ |
---|
119 | uint8_t timestamptype; |
---|
120 | /* Wire length */ |
---|
121 | uint32_t wirelen; |
---|
122 | /* Capture length */ |
---|
123 | uint32_t caplen; |
---|
124 | /* The linux native header itself */ |
---|
125 | struct sockaddr_ll hdr; |
---|
126 | }; |
---|
127 | |
---|
128 | struct linux_output_format_data_t { |
---|
129 | /* The file descriptor used to write the packets */ |
---|
130 | int fd; |
---|
131 | }; |
---|
132 | |
---|
133 | #define FORMAT(x) ((struct linux_format_data_t*)(x)) |
---|
134 | #define DATAOUT(x) ((struct linux_output_format_data_t*)((x)->format_data)) |
---|
135 | |
---|
136 | static int linuxnative_probe_filename(const char *filename) |
---|
137 | { |
---|
138 | /* Is this an interface? */ |
---|
139 | return (if_nametoindex(filename) != 0); |
---|
140 | } |
---|
141 | |
---|
142 | static int linuxnative_init_input(libtrace_t *libtrace) |
---|
143 | { |
---|
144 | libtrace->format_data = (struct linux_format_data_t *) |
---|
145 | malloc(sizeof(struct linux_format_data_t)); |
---|
146 | FORMAT(libtrace->format_data)->fd = -1; |
---|
147 | FORMAT(libtrace->format_data)->promisc = -1; |
---|
148 | FORMAT(libtrace->format_data)->snaplen = LIBTRACE_PACKET_BUFSIZE; |
---|
149 | FORMAT(libtrace->format_data)->filter = NULL; |
---|
150 | FORMAT(libtrace->format_data)->stats_valid = 0; |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | static int linuxnative_init_output(libtrace_out_t *libtrace) |
---|
156 | { |
---|
157 | libtrace->format_data = (struct linux_output_format_data_t*) |
---|
158 | malloc(sizeof(struct linux_output_format_data_t)); |
---|
159 | DATAOUT(libtrace)->fd = -1; |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | static int linuxnative_start_input(libtrace_t *libtrace) |
---|
165 | { |
---|
166 | struct sockaddr_ll addr; |
---|
167 | int one = 1; |
---|
168 | memset(&addr,0,sizeof(addr)); |
---|
169 | libtrace_filter_t *filter = FORMAT(libtrace->format_data)->filter; |
---|
170 | |
---|
171 | /* Create a raw socket for reading packets on */ |
---|
172 | FORMAT(libtrace->format_data)->fd = |
---|
173 | socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
---|
174 | if (FORMAT(libtrace->format_data)->fd==-1) { |
---|
175 | trace_set_err(libtrace, errno, "Could not create raw socket"); |
---|
176 | free(libtrace->format_data); |
---|
177 | return -1; |
---|
178 | } |
---|
179 | |
---|
180 | /* Bind to the capture interface */ |
---|
181 | addr.sll_family = AF_PACKET; |
---|
182 | addr.sll_protocol = htons(ETH_P_ALL); |
---|
183 | if (strlen(libtrace->uridata)) { |
---|
184 | addr.sll_ifindex = if_nametoindex(libtrace->uridata); |
---|
185 | if (addr.sll_ifindex == 0) { |
---|
186 | close(FORMAT(libtrace->format_data)->fd); |
---|
187 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Failed to find interface %s", libtrace->uridata); |
---|
188 | free(libtrace->format_data); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | } |
---|
192 | else { |
---|
193 | addr.sll_ifindex = 0; |
---|
194 | } |
---|
195 | if (bind(FORMAT(libtrace->format_data)->fd, |
---|
196 | (struct sockaddr*)&addr, |
---|
197 | (socklen_t)sizeof(addr))==-1) { |
---|
198 | free(libtrace->format_data); |
---|
199 | trace_set_err(libtrace, errno, "Failed to bind to interface %s", libtrace->uridata); |
---|
200 | return -1; |
---|
201 | } |
---|
202 | |
---|
203 | /* If promisc hasn't been specified, set it to "true" if we're |
---|
204 | * capturing on one interface, or "false" if we're capturing on |
---|
205 | * all interfaces. |
---|
206 | */ |
---|
207 | if (FORMAT(libtrace->format_data)->promisc==-1) { |
---|
208 | if (addr.sll_ifindex!=0) |
---|
209 | FORMAT(libtrace->format_data)->promisc=1; |
---|
210 | else |
---|
211 | FORMAT(libtrace->format_data)->promisc=0; |
---|
212 | } |
---|
213 | |
---|
214 | /* Enable promiscuous mode, if requested */ |
---|
215 | if (FORMAT(libtrace->format_data)->promisc) { |
---|
216 | struct packet_mreq mreq; |
---|
217 | socklen_t socklen = sizeof(mreq); |
---|
218 | memset(&mreq,0,sizeof(mreq)); |
---|
219 | mreq.mr_ifindex = addr.sll_ifindex; |
---|
220 | mreq.mr_type = PACKET_MR_PROMISC; |
---|
221 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
222 | SOL_PACKET, |
---|
223 | PACKET_ADD_MEMBERSHIP, |
---|
224 | &mreq, |
---|
225 | socklen)==-1) { |
---|
226 | perror("setsockopt(PROMISC)"); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | /* Set the timestamp option on the socket - aim for the most detailed |
---|
231 | * clock resolution possible */ |
---|
232 | #ifdef SO_TIMESTAMPNS |
---|
233 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
234 | SOL_SOCKET, |
---|
235 | SO_TIMESTAMPNS, |
---|
236 | &one, |
---|
237 | (socklen_t)sizeof(one))!=-1) { |
---|
238 | FORMAT(libtrace->format_data)->timestamptype = TS_TIMESPEC; |
---|
239 | } |
---|
240 | else |
---|
241 | /* DANGER: This is a dangling else to only do the next setsockopt() if we fail the first! */ |
---|
242 | #endif |
---|
243 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
244 | SOL_SOCKET, |
---|
245 | SO_TIMESTAMP, |
---|
246 | &one, |
---|
247 | (socklen_t)sizeof(one))!=-1) { |
---|
248 | FORMAT(libtrace->format_data)->timestamptype = TS_TIMEVAL; |
---|
249 | } |
---|
250 | else |
---|
251 | FORMAT(libtrace->format_data)->timestamptype = TS_NONE; |
---|
252 | |
---|
253 | /* Push BPF filter into the kernel. At this stage we can safely assume |
---|
254 | * that the filterstring has been compiled, or the filter was supplied |
---|
255 | * pre-compiled. |
---|
256 | */ |
---|
257 | if (filter != NULL) { |
---|
258 | assert(filter->flag == 1); |
---|
259 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
260 | SOL_SOCKET, |
---|
261 | SO_ATTACH_FILTER, |
---|
262 | &filter->filter, |
---|
263 | sizeof(filter->filter)) == -1) { |
---|
264 | perror("setsockopt(SO_ATTACH_FILTER)"); |
---|
265 | } else { |
---|
266 | /* The socket accepted the filter, so we need to |
---|
267 | * consume any buffered packets that were received |
---|
268 | * between opening the socket and applying the filter. |
---|
269 | */ |
---|
270 | void *buf = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
271 | while(recv(FORMAT(libtrace->format_data)->fd, |
---|
272 | buf, |
---|
273 | (size_t) LIBTRACE_PACKET_BUFSIZE, |
---|
274 | MSG_DONTWAIT) != -1) { } |
---|
275 | free(buf); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | FORMAT(libtrace->format_data)->stats_valid=0; |
---|
280 | |
---|
281 | return 0; |
---|
282 | } |
---|
283 | |
---|
284 | static int linuxnative_start_output(libtrace_out_t *libtrace) |
---|
285 | { |
---|
286 | FORMAT(libtrace->format_data)->fd = |
---|
287 | socket(PF_PACKET, SOCK_RAW, 0); |
---|
288 | if (FORMAT(libtrace->format_data)->fd==-1) { |
---|
289 | free(libtrace->format_data); |
---|
290 | return -1; |
---|
291 | } |
---|
292 | FORMAT(libtrace->format_data)->stats_valid=0; |
---|
293 | |
---|
294 | return 0; |
---|
295 | } |
---|
296 | |
---|
297 | static int linuxnative_pause_input(libtrace_t *libtrace) |
---|
298 | { |
---|
299 | close(FORMAT(libtrace->format_data)->fd); |
---|
300 | FORMAT(libtrace->format_data)->fd=-1; |
---|
301 | |
---|
302 | return 0; |
---|
303 | } |
---|
304 | |
---|
305 | static int linuxnative_fin_input(libtrace_t *libtrace) |
---|
306 | { |
---|
307 | if (FORMAT(libtrace->format_data)->filter != NULL) |
---|
308 | free(FORMAT(libtrace->format_data)->filter); |
---|
309 | if (libtrace->format_data) |
---|
310 | free(libtrace->format_data); |
---|
311 | |
---|
312 | return 0; |
---|
313 | } |
---|
314 | |
---|
315 | static int linuxnative_fin_output(libtrace_out_t *libtrace) |
---|
316 | { |
---|
317 | close(DATAOUT(libtrace)->fd); |
---|
318 | DATAOUT(libtrace)->fd=-1; |
---|
319 | free(libtrace->format_data); |
---|
320 | return 0; |
---|
321 | } |
---|
322 | |
---|
323 | /* Compiles a libtrace BPF filter for use with a linux native socket */ |
---|
324 | static int linuxnative_configure_bpf(libtrace_t *libtrace, |
---|
325 | libtrace_filter_t *filter) { |
---|
326 | #ifdef HAVE_LIBPCAP |
---|
327 | struct ifreq ifr; |
---|
328 | unsigned int arphrd; |
---|
329 | libtrace_dlt_t dlt; |
---|
330 | libtrace_filter_t *f; |
---|
331 | int sock; |
---|
332 | pcap_t *pcap; |
---|
333 | |
---|
334 | /* Take a copy of the filter object as it was passed in */ |
---|
335 | f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t)); |
---|
336 | memcpy(f, filter, sizeof(libtrace_filter_t)); |
---|
337 | |
---|
338 | /* If we are passed a filter with "flag" set to zero, then we must |
---|
339 | * compile the filterstring before continuing. This involves |
---|
340 | * determining the linktype, passing the filterstring to libpcap to |
---|
341 | * compile, and saving the result for trace_start() to push into the |
---|
342 | * kernel. |
---|
343 | * If flag is set to one, then the filter was probably generated using |
---|
344 | * trace_create_filter_from_bytecode() and so we don't need to do |
---|
345 | * anything (we've just copied it above). |
---|
346 | */ |
---|
347 | if (f->flag == 0) { |
---|
348 | sock = socket(PF_INET, SOCK_STREAM, 0); |
---|
349 | memset(&ifr, 0, sizeof(struct ifreq)); |
---|
350 | strncpy(ifr.ifr_name, libtrace->uridata, IF_NAMESIZE); |
---|
351 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) { |
---|
352 | perror("Can't get HWADDR for interface"); |
---|
353 | return -1; |
---|
354 | } |
---|
355 | close(sock); |
---|
356 | |
---|
357 | arphrd = ifr.ifr_hwaddr.sa_family; |
---|
358 | dlt = libtrace_to_pcap_dlt(arphrd_type_to_libtrace(arphrd)); |
---|
359 | |
---|
360 | pcap = pcap_open_dead(dlt, |
---|
361 | FORMAT(libtrace->format_data)->snaplen); |
---|
362 | |
---|
363 | if (pcap_compile(pcap, &f->filter, f->filterstring, 0, 0) == -1) { |
---|
364 | perror("PCAP failed to compile the filterstring"); |
---|
365 | return -1; |
---|
366 | } |
---|
367 | |
---|
368 | pcap_close(pcap); |
---|
369 | |
---|
370 | /* Set the "flag" to indicate that the filterstring has been |
---|
371 | * compiled |
---|
372 | */ |
---|
373 | f->flag = 1; |
---|
374 | } |
---|
375 | |
---|
376 | if (FORMAT(libtrace->format_data)->filter != NULL) |
---|
377 | free(FORMAT(libtrace->format_data)->filter); |
---|
378 | |
---|
379 | FORMAT(libtrace->format_data)->filter = f; |
---|
380 | |
---|
381 | return 0; |
---|
382 | #else |
---|
383 | return -1 |
---|
384 | #endif |
---|
385 | } |
---|
386 | static int linuxnative_config_input(libtrace_t *libtrace, |
---|
387 | trace_option_t option, |
---|
388 | void *data) |
---|
389 | { |
---|
390 | switch(option) { |
---|
391 | case TRACE_OPTION_SNAPLEN: |
---|
392 | FORMAT(libtrace->format_data)->snaplen=*(int*)data; |
---|
393 | return 0; |
---|
394 | case TRACE_OPTION_PROMISC: |
---|
395 | FORMAT(libtrace->format_data)->promisc=*(int*)data; |
---|
396 | return 0; |
---|
397 | case TRACE_OPTION_FILTER: |
---|
398 | return linuxnative_configure_bpf(libtrace, |
---|
399 | (libtrace_filter_t *) data); |
---|
400 | case TRACE_OPTION_META_FREQ: |
---|
401 | /* No meta-data for this format */ |
---|
402 | break; |
---|
403 | case TRACE_OPTION_EVENT_REALTIME: |
---|
404 | /* Live captures are always going to be in trace time */ |
---|
405 | break; |
---|
406 | /* Avoid default: so that future options will cause a warning |
---|
407 | * here to remind us to implement it, or flag it as |
---|
408 | * unimplementable |
---|
409 | */ |
---|
410 | } |
---|
411 | |
---|
412 | /* Don't set an error - trace_config will try to deal with the |
---|
413 | * option and will set an error if it fails */ |
---|
414 | return -1; |
---|
415 | } |
---|
416 | |
---|
417 | static int linuxnative_prepare_packet(libtrace_t *libtrace, |
---|
418 | libtrace_packet_t *packet, void *buffer, |
---|
419 | libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
420 | |
---|
421 | if (packet->buffer != buffer && |
---|
422 | packet->buf_control == TRACE_CTRL_PACKET) { |
---|
423 | free(packet->buffer); |
---|
424 | } |
---|
425 | |
---|
426 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
---|
427 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
428 | } else |
---|
429 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
430 | |
---|
431 | |
---|
432 | packet->buffer = buffer; |
---|
433 | packet->header = buffer; |
---|
434 | packet->payload = (char *)buffer + |
---|
435 | sizeof(struct libtrace_linuxnative_header); |
---|
436 | packet->type = rt_type; |
---|
437 | |
---|
438 | if (libtrace->format_data == NULL) { |
---|
439 | if (linuxnative_init_input(libtrace)) |
---|
440 | return -1; |
---|
441 | } |
---|
442 | return 0; |
---|
443 | |
---|
444 | } |
---|
445 | |
---|
446 | #define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) |
---|
447 | |
---|
448 | /* 20 isn't enough on x86_64 */ |
---|
449 | #define CMSG_BUF_SIZE 128 |
---|
450 | static int linuxnative_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) |
---|
451 | { |
---|
452 | struct libtrace_linuxnative_header *hdr; |
---|
453 | struct msghdr msghdr; |
---|
454 | struct iovec iovec; |
---|
455 | unsigned char controlbuf[CMSG_BUF_SIZE]; |
---|
456 | struct cmsghdr *cmsg; |
---|
457 | socklen_t socklen; |
---|
458 | int snaplen; |
---|
459 | uint32_t flags = 0; |
---|
460 | |
---|
461 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
462 | packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
463 | if (!packet->buffer) { |
---|
464 | perror("Cannot allocate buffer"); |
---|
465 | } |
---|
466 | } |
---|
467 | |
---|
468 | flags |= TRACE_PREP_OWN_BUFFER; |
---|
469 | |
---|
470 | packet->type = TRACE_RT_DATA_LINUX_NATIVE; |
---|
471 | |
---|
472 | hdr=(struct libtrace_linuxnative_header*)packet->buffer; |
---|
473 | socklen=sizeof(hdr->hdr); |
---|
474 | snaplen=LIBTRACE_MIN( |
---|
475 | (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*hdr), |
---|
476 | (int)FORMAT(libtrace->format_data)->snaplen); |
---|
477 | |
---|
478 | /* Prepare the msghdr and iovec for the kernel to write the |
---|
479 | * captured packet into. The msghdr will point to the part of our |
---|
480 | * buffer reserved for sll header, while the iovec will point at |
---|
481 | * the buffer following the sll header. */ |
---|
482 | |
---|
483 | msghdr.msg_name = &hdr->hdr; |
---|
484 | msghdr.msg_namelen = sizeof(struct sockaddr_ll); |
---|
485 | |
---|
486 | msghdr.msg_iov = &iovec; |
---|
487 | msghdr.msg_iovlen = 1; |
---|
488 | |
---|
489 | msghdr.msg_control = &controlbuf; |
---|
490 | msghdr.msg_controllen = CMSG_BUF_SIZE; |
---|
491 | msghdr.msg_flags = 0; |
---|
492 | |
---|
493 | iovec.iov_base = (void*)(packet->buffer+sizeof(*hdr)); |
---|
494 | iovec.iov_len = snaplen; |
---|
495 | |
---|
496 | hdr->wirelen = recvmsg(FORMAT(libtrace->format_data)->fd, &msghdr, 0); |
---|
497 | |
---|
498 | if (hdr->wirelen==~0U) { |
---|
499 | trace_set_err(libtrace,errno,"recvmsg"); |
---|
500 | return -1; |
---|
501 | } |
---|
502 | |
---|
503 | hdr->caplen=LIBTRACE_MIN((unsigned int)snaplen,(unsigned int)hdr->wirelen); |
---|
504 | |
---|
505 | /* Extract the timestamps from the msghdr and store them in our |
---|
506 | * linux native encapsulation, so that we can preserve the formatting |
---|
507 | * across multiple architectures */ |
---|
508 | |
---|
509 | for (cmsg = CMSG_FIRSTHDR(&msghdr); |
---|
510 | cmsg != NULL; |
---|
511 | cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
---|
512 | if (cmsg->cmsg_level == SOL_SOCKET |
---|
513 | && cmsg->cmsg_type == SO_TIMESTAMP |
---|
514 | && cmsg->cmsg_len <= CMSG_LEN(sizeof(struct timeval))) { |
---|
515 | hdr->tv.tv_sec = ((struct timeval*)CMSG_DATA(cmsg))->tv_sec; |
---|
516 | hdr->tv.tv_usec = ((struct timeval*)CMSG_DATA(cmsg))->tv_usec; |
---|
517 | hdr->timestamptype = TS_TIMEVAL; |
---|
518 | break; |
---|
519 | } |
---|
520 | #ifdef SO_TIMESTAMPNS |
---|
521 | else if (cmsg->cmsg_level == SOL_SOCKET |
---|
522 | && cmsg->cmsg_type == SO_TIMESTAMPNS |
---|
523 | && cmsg->cmsg_len <= CMSG_LEN(sizeof(struct timespec))) { |
---|
524 | hdr->ts.tv_sec = ((struct timespec*)CMSG_DATA(cmsg))->tv_sec; |
---|
525 | hdr->ts.tv_nsec = ((struct timespec*)CMSG_DATA(cmsg))->tv_nsec; |
---|
526 | hdr->timestamptype = TS_TIMESPEC; |
---|
527 | break; |
---|
528 | } |
---|
529 | #endif |
---|
530 | } |
---|
531 | |
---|
532 | /* Did we not get given a timestamp? Try to get one from the |
---|
533 | * file descriptor directly */ |
---|
534 | if (cmsg == NULL) { |
---|
535 | struct timeval tv; |
---|
536 | if (ioctl(FORMAT(libtrace->format_data)->fd, |
---|
537 | SIOCGSTAMP,&tv)==0) { |
---|
538 | hdr->tv.tv_sec = tv.tv_sec; |
---|
539 | hdr->tv.tv_usec = tv.tv_usec; |
---|
540 | hdr->timestamptype = TS_TIMEVAL; |
---|
541 | } |
---|
542 | else { |
---|
543 | hdr->timestamptype = TS_NONE; |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | /* Buffer contains all of our packet (including our custom header) so |
---|
548 | * we just need to get prepare_packet to set all our packet pointers |
---|
549 | * appropriately */ |
---|
550 | |
---|
551 | if (linuxnative_prepare_packet(libtrace, packet, packet->buffer, |
---|
552 | packet->type, flags)) |
---|
553 | return -1; |
---|
554 | |
---|
555 | return hdr->wirelen+sizeof(*hdr); |
---|
556 | } |
---|
557 | |
---|
558 | static int linuxnative_write_packet(libtrace_out_t *trace, |
---|
559 | libtrace_packet_t *packet) |
---|
560 | { |
---|
561 | struct sockaddr_ll hdr; |
---|
562 | |
---|
563 | hdr.sll_family = AF_PACKET; |
---|
564 | hdr.sll_protocol = 0; |
---|
565 | hdr.sll_ifindex = if_nametoindex(trace->uridata); |
---|
566 | hdr.sll_hatype = 0; |
---|
567 | hdr.sll_pkttype = 0; |
---|
568 | hdr.sll_halen = htons(6); /* FIXME */ |
---|
569 | memcpy(hdr.sll_addr,packet->payload,(size_t)ntohs(hdr.sll_halen)); |
---|
570 | |
---|
571 | /* This is pretty easy, just send the payload using sendto() (after |
---|
572 | * setting up the sll header properly, of course) */ |
---|
573 | return sendto(DATAOUT(trace)->fd, |
---|
574 | packet->payload, |
---|
575 | trace_get_capture_length(packet), |
---|
576 | 0, |
---|
577 | (struct sockaddr*)&hdr, (socklen_t)sizeof(hdr)); |
---|
578 | |
---|
579 | } |
---|
580 | |
---|
581 | static libtrace_linktype_t linuxnative_get_link_type(const struct libtrace_packet_t *packet) { |
---|
582 | int linktype=(((struct libtrace_linuxnative_header*)(packet->buffer)) |
---|
583 | ->hdr.sll_hatype); |
---|
584 | /* Convert the ARPHRD type into an appropriate libtrace link type */ |
---|
585 | |
---|
586 | switch (linktype) { |
---|
587 | case ARPHRD_ETHER: |
---|
588 | return TRACE_TYPE_ETH; |
---|
589 | case ARPHRD_PPP: |
---|
590 | return TRACE_TYPE_NONE; |
---|
591 | case ARPHRD_80211_RADIOTAP: |
---|
592 | return TRACE_TYPE_80211_RADIO; |
---|
593 | case ARPHRD_IEEE80211: |
---|
594 | return TRACE_TYPE_80211; |
---|
595 | case ARPHRD_SIT: |
---|
596 | case ARPHRD_NONE: |
---|
597 | return TRACE_TYPE_NONE; |
---|
598 | default: /* shrug, beyond me! */ |
---|
599 | printf("unknown Linux ARPHRD type 0x%04x\n",linktype); |
---|
600 | return (libtrace_linktype_t)~0U; |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | static libtrace_direction_t linuxnative_get_direction(const struct libtrace_packet_t *packet) { |
---|
605 | switch (((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_pkttype) { |
---|
606 | case PACKET_OUTGOING: |
---|
607 | case PACKET_LOOPBACK: |
---|
608 | return TRACE_DIR_OUTGOING; |
---|
609 | default: |
---|
610 | return TRACE_DIR_INCOMING; |
---|
611 | } |
---|
612 | } |
---|
613 | |
---|
614 | static libtrace_direction_t linuxnative_set_direction( |
---|
615 | libtrace_packet_t *packet, |
---|
616 | libtrace_direction_t direction) { |
---|
617 | |
---|
618 | switch (direction) { |
---|
619 | case TRACE_DIR_OUTGOING: |
---|
620 | ((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_pkttype = PACKET_OUTGOING; |
---|
621 | return TRACE_DIR_OUTGOING; |
---|
622 | case TRACE_DIR_INCOMING: |
---|
623 | ((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_pkttype = PACKET_HOST; |
---|
624 | return TRACE_DIR_INCOMING; |
---|
625 | default: |
---|
626 | return -1; |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | static struct timespec linuxnative_get_timespec(const libtrace_packet_t *packet) |
---|
631 | { |
---|
632 | struct libtrace_linuxnative_header *hdr = |
---|
633 | (struct libtrace_linuxnative_header*) packet->buffer; |
---|
634 | /* We have to upconvert from timeval to timespec */ |
---|
635 | if (hdr->timestamptype == TS_TIMEVAL) { |
---|
636 | struct timespec ts; |
---|
637 | ts.tv_sec = hdr->tv.tv_sec; |
---|
638 | ts.tv_nsec = hdr->tv.tv_usec*1000; |
---|
639 | return ts; |
---|
640 | } |
---|
641 | else { |
---|
642 | struct timespec ts; |
---|
643 | ts.tv_sec = hdr->ts.tv_sec; |
---|
644 | ts.tv_nsec = hdr->ts.tv_nsec; |
---|
645 | return ts; |
---|
646 | } |
---|
647 | } |
---|
648 | |
---|
649 | static struct timeval linuxnative_get_timeval(const libtrace_packet_t *packet) |
---|
650 | { |
---|
651 | struct libtrace_linuxnative_header *hdr = |
---|
652 | (struct libtrace_linuxnative_header*) packet->buffer; |
---|
653 | /* We have to downconvert from timespec to timeval */ |
---|
654 | if (hdr->timestamptype == TS_TIMESPEC) { |
---|
655 | struct timeval tv; |
---|
656 | tv.tv_sec = hdr->ts.tv_sec; |
---|
657 | tv.tv_usec = hdr->ts.tv_nsec/1000; |
---|
658 | return tv; |
---|
659 | } |
---|
660 | else { |
---|
661 | struct timeval tv; |
---|
662 | tv.tv_sec = hdr->tv.tv_sec; |
---|
663 | tv.tv_usec = hdr->tv.tv_usec; |
---|
664 | return tv; |
---|
665 | } |
---|
666 | } |
---|
667 | |
---|
668 | static int linuxnative_get_capture_length(const libtrace_packet_t *packet) |
---|
669 | { |
---|
670 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->caplen; |
---|
671 | } |
---|
672 | |
---|
673 | static int linuxnative_get_wire_length(const libtrace_packet_t *packet) |
---|
674 | { |
---|
675 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->wirelen; |
---|
676 | } |
---|
677 | |
---|
678 | static int linuxnative_get_framing_length(UNUSED |
---|
679 | const libtrace_packet_t *packet) |
---|
680 | { |
---|
681 | return sizeof(struct libtrace_linuxnative_header); |
---|
682 | } |
---|
683 | |
---|
684 | static size_t linuxnative_set_capture_length(libtrace_packet_t *packet, |
---|
685 | size_t size) { |
---|
686 | |
---|
687 | struct libtrace_linuxnative_header *linux_hdr = NULL; |
---|
688 | assert(packet); |
---|
689 | if (size > trace_get_capture_length(packet)) { |
---|
690 | /* We should avoid making a packet larger */ |
---|
691 | return trace_get_capture_length(packet); |
---|
692 | } |
---|
693 | |
---|
694 | /* Reset the cached capture length */ |
---|
695 | packet->capture_length = -1; |
---|
696 | |
---|
697 | linux_hdr = (struct libtrace_linuxnative_header *)packet->header; |
---|
698 | linux_hdr->caplen = size; |
---|
699 | return trace_get_capture_length(packet); |
---|
700 | } |
---|
701 | |
---|
702 | static int linuxnative_get_fd(const libtrace_t *trace) { |
---|
703 | if (trace->format_data == NULL) |
---|
704 | return -1; |
---|
705 | return FORMAT(trace->format_data)->fd; |
---|
706 | } |
---|
707 | |
---|
708 | /* Linux doesn't keep track how many packets were seen before filtering |
---|
709 | * so we can't tell how many packets were filtered. Bugger. So annoying. |
---|
710 | * |
---|
711 | * Since we tell libtrace that we do support filtering, if we don't declare |
---|
712 | * this here as failing, libtrace will happily report for us that it didn't |
---|
713 | * filter any packets, so don't lie -- return that we don't know. |
---|
714 | */ |
---|
715 | static uint64_t linuxnative_get_filtered_packets(libtrace_t *trace UNUSED) { |
---|
716 | return UINT64_MAX; |
---|
717 | } |
---|
718 | |
---|
719 | /* Number of packets that passed filtering */ |
---|
720 | static uint64_t linuxnative_get_captured_packets(libtrace_t *trace) { |
---|
721 | if (trace->format_data == NULL) |
---|
722 | return UINT64_MAX; |
---|
723 | if (FORMAT(trace->format_data)->fd == -1) { |
---|
724 | /* This is probably a 'dead' trace so obviously we can't query |
---|
725 | * the socket for capture counts, can we? */ |
---|
726 | return UINT64_MAX; |
---|
727 | } |
---|
728 | |
---|
729 | if ((FORMAT(trace->format_data)->stats_valid & 1) |
---|
730 | || FORMAT(trace->format_data)->stats_valid == 0) { |
---|
731 | socklen_t len = sizeof(FORMAT(trace->format_data)->stats); |
---|
732 | getsockopt(FORMAT(trace->format_data)->fd, |
---|
733 | SOL_PACKET, |
---|
734 | PACKET_STATISTICS, |
---|
735 | &FORMAT(trace->format_data)->stats, |
---|
736 | &len); |
---|
737 | FORMAT(trace->format_data)->stats_valid |= 1; |
---|
738 | } |
---|
739 | |
---|
740 | return FORMAT(trace->format_data)->stats.tp_packets; |
---|
741 | } |
---|
742 | |
---|
743 | /* Number of packets that got past filtering and were then dropped because |
---|
744 | * of lack of space |
---|
745 | */ |
---|
746 | static uint64_t linuxnative_get_dropped_packets(libtrace_t *trace) { |
---|
747 | if (trace->format_data == NULL) |
---|
748 | return UINT64_MAX; |
---|
749 | if (FORMAT(trace->format_data)->fd == -1) { |
---|
750 | /* This is probably a 'dead' trace so obviously we can't query |
---|
751 | * the socket for drop counts, can we? */ |
---|
752 | return UINT64_MAX; |
---|
753 | } |
---|
754 | |
---|
755 | if ((FORMAT(trace->format_data)->stats_valid & 2) |
---|
756 | || (FORMAT(trace->format_data)->stats_valid==0)) { |
---|
757 | socklen_t len = sizeof(FORMAT(trace->format_data)->stats); |
---|
758 | getsockopt(FORMAT(trace->format_data)->fd, |
---|
759 | SOL_PACKET, |
---|
760 | PACKET_STATISTICS, |
---|
761 | &FORMAT(trace->format_data)->stats, |
---|
762 | &len); |
---|
763 | FORMAT(trace->format_data)->stats_valid |= 2; |
---|
764 | } |
---|
765 | |
---|
766 | return FORMAT(trace->format_data)->stats.tp_drops; |
---|
767 | } |
---|
768 | |
---|
769 | static void linuxnative_help(void) { |
---|
770 | printf("linuxnative format module: $Revision$\n"); |
---|
771 | printf("Supported input URIs:\n"); |
---|
772 | printf("\tint:eth0\n"); |
---|
773 | printf("\n"); |
---|
774 | printf("Supported output URIs:\n"); |
---|
775 | printf("\tint:eth0\n"); |
---|
776 | printf("\n"); |
---|
777 | return; |
---|
778 | } |
---|
779 | static struct libtrace_format_t linuxnative = { |
---|
780 | "int", |
---|
781 | "$Id$", |
---|
782 | TRACE_FORMAT_LINUX_NATIVE, |
---|
783 | linuxnative_probe_filename, /* probe filename */ |
---|
784 | NULL, /* probe magic */ |
---|
785 | linuxnative_init_input, /* init_input */ |
---|
786 | linuxnative_config_input, /* config_input */ |
---|
787 | linuxnative_start_input, /* start_input */ |
---|
788 | linuxnative_pause_input, /* pause_input */ |
---|
789 | linuxnative_init_output, /* init_output */ |
---|
790 | NULL, /* config_output */ |
---|
791 | linuxnative_start_output, /* start_ouput */ |
---|
792 | linuxnative_fin_input, /* fin_input */ |
---|
793 | linuxnative_fin_output, /* fin_output */ |
---|
794 | linuxnative_read_packet, /* read_packet */ |
---|
795 | linuxnative_prepare_packet, /* prepare_packet */ |
---|
796 | NULL, /* fin_packet */ |
---|
797 | linuxnative_write_packet, /* write_packet */ |
---|
798 | linuxnative_get_link_type, /* get_link_type */ |
---|
799 | linuxnative_get_direction, /* get_direction */ |
---|
800 | linuxnative_set_direction, /* set_direction */ |
---|
801 | NULL, /* get_erf_timestamp */ |
---|
802 | linuxnative_get_timeval, /* get_timeval */ |
---|
803 | linuxnative_get_timespec, /* get_timespec */ |
---|
804 | NULL, /* get_seconds */ |
---|
805 | NULL, /* seek_erf */ |
---|
806 | NULL, /* seek_timeval */ |
---|
807 | NULL, /* seek_seconds */ |
---|
808 | linuxnative_get_capture_length, /* get_capture_length */ |
---|
809 | linuxnative_get_wire_length, /* get_wire_length */ |
---|
810 | linuxnative_get_framing_length, /* get_framing_length */ |
---|
811 | linuxnative_set_capture_length, /* set_capture_length */ |
---|
812 | NULL, /* get_received_packets */ |
---|
813 | linuxnative_get_filtered_packets,/* get_filtered_packets */ |
---|
814 | linuxnative_get_dropped_packets,/* get_dropped_packets */ |
---|
815 | linuxnative_get_captured_packets,/* get_captured_packets */ |
---|
816 | linuxnative_get_fd, /* get_fd */ |
---|
817 | trace_event_device, /* trace_event */ |
---|
818 | linuxnative_help, /* help */ |
---|
819 | NULL |
---|
820 | }; |
---|
821 | |
---|
822 | void linuxnative_constructor(void) { |
---|
823 | register_format(&linuxnative); |
---|
824 | } |
---|