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