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 | * Richard Sanger |
---|
11 | * |
---|
12 | * All rights reserved. |
---|
13 | * |
---|
14 | * This code has been developed by the University of Waikato WAND |
---|
15 | * research group. For further information please see http://www.wand.net.nz/ |
---|
16 | * |
---|
17 | * libtrace is free software; you can redistribute it and/or modify |
---|
18 | * it under the terms of the GNU General Public License as published by |
---|
19 | * the Free Software Foundation; either version 2 of the License, or |
---|
20 | * (at your option) any later version. |
---|
21 | * |
---|
22 | * libtrace is distributed in the hope that it will be useful, |
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
25 | * GNU General Public License for more details. |
---|
26 | * |
---|
27 | * You should have received a copy of the GNU General Public License |
---|
28 | * along with libtrace; if not, write to the Free Software |
---|
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
30 | * |
---|
31 | * $Id$ |
---|
32 | * |
---|
33 | */ |
---|
34 | |
---|
35 | /* This format module deals with using the Linux Native capture format. |
---|
36 | * |
---|
37 | * Linux Native is a LIVE capture format. |
---|
38 | * |
---|
39 | * This format also supports writing which will write packets out to the |
---|
40 | * network as a form of packet replay. This should not be confused with the |
---|
41 | * RT protocol which is intended to transfer captured packet records between |
---|
42 | * RT-speaking programs. |
---|
43 | */ |
---|
44 | |
---|
45 | #include "config.h" |
---|
46 | #include "libtrace.h" |
---|
47 | #include "libtrace_int.h" |
---|
48 | #include "format_helper.h" |
---|
49 | #include "libtrace_arphrd.h" |
---|
50 | #include <stdlib.h> |
---|
51 | #include <errno.h> |
---|
52 | #include <unistd.h> |
---|
53 | #include <string.h> |
---|
54 | #include <assert.h> |
---|
55 | |
---|
56 | #ifdef HAVE_INTTYPES_H |
---|
57 | # include <inttypes.h> |
---|
58 | #else |
---|
59 | # error "Can't find inttypes.h" |
---|
60 | #endif |
---|
61 | |
---|
62 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
63 | |
---|
64 | #include <sys/socket.h> |
---|
65 | #include <netpacket/packet.h> |
---|
66 | #include <net/ethernet.h> |
---|
67 | #include <net/if_arp.h> |
---|
68 | |
---|
69 | #include <net/if.h> |
---|
70 | #include <sys/ioctl.h> |
---|
71 | #include <poll.h> |
---|
72 | #include <sys/mman.h> |
---|
73 | |
---|
74 | /* MAX_ORDER is defined in linux/mmzone.h. 10 is default for 2.4 kernel. |
---|
75 | * max_order will be decreased by one if the ring buffer fails to allocate. |
---|
76 | * Used to get correct sized buffers from the kernel. |
---|
77 | */ |
---|
78 | #define MAX_ORDER 10 |
---|
79 | |
---|
80 | /* Cached page size, the page size shouldn't be changing */ |
---|
81 | static int pagesize = 0; |
---|
82 | |
---|
83 | /* Number of frames in the ring used by both TX and TR rings. More frames |
---|
84 | * hopefully means less packet loss, especially if traffic comes in bursts. |
---|
85 | */ |
---|
86 | #define CONF_RING_FRAMES 0x100 |
---|
87 | |
---|
88 | /* The maximum frames allowed to be waiting in the TX_RING before the kernel is |
---|
89 | * notified to write them out. Make sure this is less than CONF_RING_FRAMES. |
---|
90 | * Performance doesn't seem to increase any more when setting this above 10. |
---|
91 | */ |
---|
92 | #define TX_MAX_QUEUE 10 |
---|
93 | |
---|
94 | /* Get current frame in the ring buffer*/ |
---|
95 | #define GET_CURRENT_BUFFER(libtrace) ((void *) FORMAT(libtrace->format_data)->rx_ring + \ |
---|
96 | (FORMAT(libtrace->format_data)->rxring_offset * FORMAT(libtrace->format_data)->req.tp_frame_size)) |
---|
97 | |
---|
98 | |
---|
99 | #else /* HAVE_NETPACKET_PACKET_H */ |
---|
100 | /* Need to know what a sockaddr_ll looks like */ |
---|
101 | struct sockaddr_ll { |
---|
102 | uint16_t sll_family; |
---|
103 | uint16_t sll_protocol; |
---|
104 | int32_t sll_ifindex; |
---|
105 | uint16_t sll_hatype; |
---|
106 | uint8_t sll_pkttype; |
---|
107 | uint8_t sll_halen; |
---|
108 | uint8_t sll_addr[8]; |
---|
109 | }; |
---|
110 | |
---|
111 | /* Packet types. */ |
---|
112 | |
---|
113 | #define PACKET_HOST 0 /* To us. */ |
---|
114 | #define PACKET_BROADCAST 1 /* To all. */ |
---|
115 | #define PACKET_MULTICAST 2 /* To group. */ |
---|
116 | #define PACKET_OTHERHOST 3 /* To someone else. */ |
---|
117 | #define PACKET_OUTGOING 4 /* Originated by us . */ |
---|
118 | #define PACKET_LOOPBACK 5 |
---|
119 | #define PACKET_FASTROUTE 6 |
---|
120 | |
---|
121 | /* Packet socket options. */ |
---|
122 | |
---|
123 | #define PACKET_ADD_MEMBERSHIP 1 |
---|
124 | #define PACKET_DROP_MEMBERSHIP 2 |
---|
125 | #define PACKET_RECV_OUTPUT 3 |
---|
126 | #define PACKET_RX_RING 5 |
---|
127 | #define PACKET_STATISTICS 6 |
---|
128 | |
---|
129 | |
---|
130 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
131 | |
---|
132 | struct tpacket_stats { |
---|
133 | unsigned int tp_packets; |
---|
134 | unsigned int tp_drops; |
---|
135 | }; |
---|
136 | |
---|
137 | typedef enum { TS_NONE, TS_TIMEVAL, TS_TIMESPEC } timestamptype_t; |
---|
138 | |
---|
139 | /* linux/if_packet.h defines. They are here rather than including the header |
---|
140 | * this means that we can interpret a ring frame on a kernel that doesn't |
---|
141 | * support the format directly. |
---|
142 | */ |
---|
143 | |
---|
144 | |
---|
145 | #define PACKET_RX_RING 5 |
---|
146 | #define PACKET_VERSION 10 |
---|
147 | #define PACKET_HDRLEN 11 |
---|
148 | #define PACKET_TX_RING 13 |
---|
149 | #define TP_STATUS_USER 0x1 |
---|
150 | #define TP_STATUS_SEND_REQUEST 0x1 |
---|
151 | #define TP_STATUS_AVAILABLE 0x0 |
---|
152 | #define TO_TP_HDR(x) ((struct tpacket2_hdr *) (x)) |
---|
153 | #define TPACKET_ALIGNMENT 16 |
---|
154 | #define TPACKET_ALIGN(x) (((x)+TPACKET_ALIGNMENT-1)&~(TPACKET_ALIGNMENT-1)) |
---|
155 | #define TPACKET_HDRLEN (TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + sizeof(struct sockaddr_ll)) |
---|
156 | |
---|
157 | enum tpacket_versions { |
---|
158 | TPACKET_V1, |
---|
159 | TPACKET_V2 |
---|
160 | }; |
---|
161 | |
---|
162 | struct tpacket2_hdr { |
---|
163 | /* Frame status - in use by kernel or libtrace etc. */ |
---|
164 | uint32_t tp_status; |
---|
165 | /* Wire length */ |
---|
166 | uint32_t tp_len; |
---|
167 | /* Captured length */ |
---|
168 | uint32_t tp_snaplen; |
---|
169 | /* Offset in bytes from frame start to the mac (link layer) header */ |
---|
170 | uint16_t tp_mac; |
---|
171 | /* Offset in bytes from frame start to the net (network layer) header */ |
---|
172 | uint16_t tp_net; |
---|
173 | /* Timestamp */ |
---|
174 | uint32_t tp_sec; |
---|
175 | uint32_t tp_nsec; |
---|
176 | /* Not used VLAN tag control information */ |
---|
177 | uint16_t tp_vlan_tci; |
---|
178 | uint16_t tp_padding; |
---|
179 | }; |
---|
180 | |
---|
181 | struct tpacket_req { |
---|
182 | unsigned int tp_block_size; /* Minimal size of contiguous block */ |
---|
183 | unsigned int tp_block_nr; /* Number of blocks */ |
---|
184 | unsigned int tp_frame_size; /* Size of frame */ |
---|
185 | unsigned int tp_frame_nr; /* Total number of frames */ |
---|
186 | }; |
---|
187 | |
---|
188 | struct linux_format_data_t { |
---|
189 | /* The file descriptor being used for the capture */ |
---|
190 | int fd; |
---|
191 | /* The snap length for the capture */ |
---|
192 | int snaplen; |
---|
193 | /* Flag indicating whether the interface should be placed in |
---|
194 | * promiscuous mode */ |
---|
195 | int promisc; |
---|
196 | /* The timestamp format used by the capture */ |
---|
197 | timestamptype_t timestamptype; |
---|
198 | /* A BPF filter that is applied to every captured packet */ |
---|
199 | libtrace_filter_t *filter; |
---|
200 | /* Statistics for the capture process, e.g. dropped packet counts */ |
---|
201 | struct tpacket_stats stats; |
---|
202 | /* Flag indicating whether the statistics are current or not */ |
---|
203 | int stats_valid; |
---|
204 | /* The rx ring mmap location*/ |
---|
205 | char * rx_ring; |
---|
206 | /* The current frame number within the rx ring */ |
---|
207 | int rxring_offset; |
---|
208 | /* The actual format being used - ring vs int */ |
---|
209 | libtrace_rt_types_t format; |
---|
210 | /* The current ring buffer layout */ |
---|
211 | struct tpacket_req req; |
---|
212 | /* Used to determine buffer size for the ring buffer */ |
---|
213 | uint32_t max_order; |
---|
214 | }; |
---|
215 | |
---|
216 | |
---|
217 | /* Note that this structure is passed over the wire in rt encapsulation, and |
---|
218 | * thus we need to be careful with data sizes. timeval's and timespec's |
---|
219 | * can also change their size on 32/64 machines. |
---|
220 | */ |
---|
221 | |
---|
222 | /* Format header for encapsulating packets captured using linux native */ |
---|
223 | struct libtrace_linuxnative_header { |
---|
224 | /* Timestamp of the packet, as a timeval */ |
---|
225 | struct { |
---|
226 | uint32_t tv_sec; |
---|
227 | uint32_t tv_usec; |
---|
228 | } tv; |
---|
229 | /* Timestamp of the packet, as a timespec */ |
---|
230 | struct { |
---|
231 | uint32_t tv_sec; |
---|
232 | uint32_t tv_nsec; |
---|
233 | } ts; |
---|
234 | /* The timestamp format used by the process that captured this packet */ |
---|
235 | uint8_t timestamptype; |
---|
236 | /* Wire length */ |
---|
237 | uint32_t wirelen; |
---|
238 | /* Capture length */ |
---|
239 | uint32_t caplen; |
---|
240 | /* The linux native header itself */ |
---|
241 | struct sockaddr_ll hdr; |
---|
242 | }; |
---|
243 | |
---|
244 | struct linux_output_format_data_t { |
---|
245 | /* The file descriptor used to write the packets */ |
---|
246 | int fd; |
---|
247 | /* The tx ring mmap location */ |
---|
248 | char * tx_ring; |
---|
249 | /* The current frame number within the tx ring */ |
---|
250 | int txring_offset; |
---|
251 | /* The current ring buffer layout */ |
---|
252 | struct tpacket_req req; |
---|
253 | /* Our sockaddr structure, here so we can cache the interface number */ |
---|
254 | struct sockaddr_ll sock_hdr; |
---|
255 | /* The (maximum) number of packets that haven't been written */ |
---|
256 | int queue; |
---|
257 | /* The format this trace is using linuxring or linuxnative */ |
---|
258 | libtrace_rt_types_t format; |
---|
259 | /* Used to determine buffer size for the ring buffer */ |
---|
260 | uint32_t max_order; |
---|
261 | }; |
---|
262 | |
---|
263 | /* Get the sockaddr_ll structure from a frame */ |
---|
264 | #define GET_SOCKADDR_HDR(x) ((struct sockaddr_ll *) (((char *) (x))\ |
---|
265 | + TPACKET_ALIGN(sizeof(struct tpacket2_hdr)))) |
---|
266 | |
---|
267 | #define FORMAT(x) ((struct linux_format_data_t*)(x)) |
---|
268 | #define DATAOUT(x) ((struct linux_output_format_data_t*)((x)->format_data)) |
---|
269 | |
---|
270 | /* Get the start of the captured data. I'm not sure if tp_mac (link layer) is |
---|
271 | * always guaranteed. If it's not there then just use tp_net. |
---|
272 | */ |
---|
273 | #define TP_TRACE_START(mac, net, hdrend) \ |
---|
274 | ((mac) > (hdrend) && (mac) < (net) ? (mac) : (net)) |
---|
275 | |
---|
276 | |
---|
277 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
278 | /* |
---|
279 | * Try figure out the best sizes for the ring buffer. Ensure that: |
---|
280 | * - max(Block_size) == page_size << max_order |
---|
281 | * - Frame_size == page_size << x (so that block_size%frame_size == 0) |
---|
282 | * This means that there will be no wasted space between blocks |
---|
283 | * - Frame_size < block_size |
---|
284 | * - Frame_size is as close as possible to LIBTRACE_PACKET_BUFSIZE, but not |
---|
285 | * bigger |
---|
286 | * - Frame_nr = Block_nr * (frames per block) |
---|
287 | * - CONF_RING_FRAMES is used a minimum number of frames to hold |
---|
288 | * - Calculates based on max_order and buf_min |
---|
289 | */ |
---|
290 | static void calculate_buffers(struct tpacket_req * req, int fd, char * uri, |
---|
291 | uint32_t max_order){ |
---|
292 | |
---|
293 | struct ifreq ifr; |
---|
294 | unsigned max_frame = LIBTRACE_PACKET_BUFSIZE; |
---|
295 | pagesize = getpagesize(); |
---|
296 | |
---|
297 | strcpy(ifr.ifr_name, uri); |
---|
298 | /* Don't bother trying to set frame size above mtu linux will drop |
---|
299 | * these anyway. |
---|
300 | * |
---|
301 | * Remember, that our frame also has to include a TPACKET header! |
---|
302 | */ |
---|
303 | if (ioctl(fd, SIOCGIFMTU, (caddr_t) &ifr) >= 0) |
---|
304 | max_frame = ifr.ifr_mtu + TPACKET_ALIGN(TPACKET_HDRLEN); |
---|
305 | if (max_frame > LIBTRACE_PACKET_BUFSIZE) |
---|
306 | max_frame = LIBTRACE_PACKET_BUFSIZE; |
---|
307 | |
---|
308 | /* Calculate frame size */ |
---|
309 | req->tp_frame_size = pagesize; |
---|
310 | while(req->tp_frame_size < max_frame && |
---|
311 | req->tp_frame_size < LIBTRACE_PACKET_BUFSIZE){ |
---|
312 | req->tp_frame_size <<= 1; |
---|
313 | } |
---|
314 | if(req->tp_frame_size > LIBTRACE_PACKET_BUFSIZE) |
---|
315 | req->tp_frame_size >>= 1; |
---|
316 | |
---|
317 | /* Calculate block size */ |
---|
318 | req->tp_block_size = pagesize << max_order; |
---|
319 | do{ |
---|
320 | req->tp_block_size >>= 1; |
---|
321 | } while((CONF_RING_FRAMES * req->tp_frame_size) <= req->tp_block_size); |
---|
322 | req->tp_block_size <<= 1; |
---|
323 | |
---|
324 | /* Calculate number of blocks */ |
---|
325 | req->tp_block_nr = (CONF_RING_FRAMES * req->tp_frame_size) |
---|
326 | / req->tp_block_size; |
---|
327 | if((CONF_RING_FRAMES * req->tp_frame_size) % req->tp_block_size != 0) |
---|
328 | req->tp_block_nr++; |
---|
329 | |
---|
330 | /* Calculate packets such that we use all the space we have to allocated */ |
---|
331 | req->tp_frame_nr = req->tp_block_nr * |
---|
332 | (req->tp_block_size / req->tp_frame_size); |
---|
333 | |
---|
334 | /* |
---|
335 | printf("MaxO 0x%x BS 0x%x BN 0x%x FS 0x%x FN 0x%x\n", |
---|
336 | max_order, |
---|
337 | req->tp_block_size, |
---|
338 | req->tp_block_nr, |
---|
339 | req->tp_frame_size, |
---|
340 | req->tp_frame_nr); |
---|
341 | */ |
---|
342 | |
---|
343 | /* In case we have some silly values*/ |
---|
344 | assert(req->tp_block_size); |
---|
345 | assert(req->tp_block_nr); |
---|
346 | assert(req->tp_frame_size); |
---|
347 | assert(req->tp_frame_nr); |
---|
348 | assert(req->tp_block_size % req->tp_frame_size == 0); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | static int linuxnative_probe_filename(const char *filename) |
---|
353 | { |
---|
354 | /* Is this an interface? */ |
---|
355 | return (if_nametoindex(filename) != 0); |
---|
356 | } |
---|
357 | |
---|
358 | static inline void init_input(libtrace_t *libtrace){ |
---|
359 | libtrace->format_data = (struct linux_format_data_t *) |
---|
360 | malloc(sizeof(struct linux_format_data_t)); |
---|
361 | FORMAT(libtrace->format_data)->fd = -1; |
---|
362 | FORMAT(libtrace->format_data)->promisc = -1; |
---|
363 | FORMAT(libtrace->format_data)->snaplen = LIBTRACE_PACKET_BUFSIZE; |
---|
364 | FORMAT(libtrace->format_data)->filter = NULL; |
---|
365 | FORMAT(libtrace->format_data)->stats_valid = 0; |
---|
366 | FORMAT(libtrace->format_data)->rx_ring = NULL; |
---|
367 | FORMAT(libtrace->format_data)->rxring_offset = 0; |
---|
368 | FORMAT(libtrace->format_data)->max_order = MAX_ORDER; |
---|
369 | } |
---|
370 | static int linuxring_init_input(libtrace_t *libtrace) |
---|
371 | { |
---|
372 | init_input(libtrace); |
---|
373 | FORMAT(libtrace->format_data)->format = TRACE_FORMAT_LINUX_RING; |
---|
374 | return 0; |
---|
375 | } |
---|
376 | static int linuxnative_init_input(libtrace_t *libtrace) |
---|
377 | { |
---|
378 | init_input(libtrace); |
---|
379 | FORMAT(libtrace->format_data)->format = TRACE_FORMAT_LINUX_NATIVE; |
---|
380 | return 0; |
---|
381 | } |
---|
382 | |
---|
383 | static inline void init_output(libtrace_out_t *libtrace) |
---|
384 | { |
---|
385 | libtrace->format_data = (struct linux_output_format_data_t*) |
---|
386 | malloc(sizeof(struct linux_output_format_data_t)); |
---|
387 | DATAOUT(libtrace)->fd = -1; |
---|
388 | DATAOUT(libtrace)->tx_ring = NULL; |
---|
389 | DATAOUT(libtrace)->txring_offset = 0; |
---|
390 | DATAOUT(libtrace)->queue = 0; |
---|
391 | DATAOUT(libtrace)->max_order = MAX_ORDER; |
---|
392 | } |
---|
393 | static int linuxnative_init_output(libtrace_out_t *libtrace) |
---|
394 | { |
---|
395 | init_output(libtrace); |
---|
396 | DATAOUT(libtrace)->format = TRACE_FORMAT_LINUX_NATIVE; |
---|
397 | return 0; |
---|
398 | } |
---|
399 | static int linuxring_init_output(libtrace_out_t *libtrace) |
---|
400 | { |
---|
401 | init_output(libtrace); |
---|
402 | DATAOUT(libtrace)->format = TRACE_FORMAT_LINUX_RING; |
---|
403 | return 0; |
---|
404 | } |
---|
405 | |
---|
406 | static int linuxnative_start_input(libtrace_t *libtrace) |
---|
407 | { |
---|
408 | struct sockaddr_ll addr; |
---|
409 | int one = 1; |
---|
410 | memset(&addr,0,sizeof(addr)); |
---|
411 | libtrace_filter_t *filter = FORMAT(libtrace->format_data)->filter; |
---|
412 | |
---|
413 | /* Create a raw socket for reading packets on */ |
---|
414 | FORMAT(libtrace->format_data)->fd = |
---|
415 | socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
---|
416 | if (FORMAT(libtrace->format_data)->fd==-1) { |
---|
417 | trace_set_err(libtrace, errno, "Could not create raw socket"); |
---|
418 | free(libtrace->format_data); |
---|
419 | libtrace->format_data = NULL; |
---|
420 | return -1; |
---|
421 | } |
---|
422 | |
---|
423 | /* Bind to the capture interface */ |
---|
424 | addr.sll_family = AF_PACKET; |
---|
425 | addr.sll_protocol = htons(ETH_P_ALL); |
---|
426 | if (strlen(libtrace->uridata)) { |
---|
427 | addr.sll_ifindex = if_nametoindex(libtrace->uridata); |
---|
428 | if (addr.sll_ifindex == 0) { |
---|
429 | close(FORMAT(libtrace->format_data)->fd); |
---|
430 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Failed to find interface %s", libtrace->uridata); |
---|
431 | free(libtrace->format_data); |
---|
432 | libtrace->format_data = NULL; |
---|
433 | return -1; |
---|
434 | } |
---|
435 | } |
---|
436 | else { |
---|
437 | addr.sll_ifindex = 0; |
---|
438 | } |
---|
439 | if (bind(FORMAT(libtrace->format_data)->fd, |
---|
440 | (struct sockaddr*)&addr, |
---|
441 | (socklen_t)sizeof(addr))==-1) { |
---|
442 | free(libtrace->format_data); |
---|
443 | libtrace->format_data = NULL; |
---|
444 | trace_set_err(libtrace, errno, "Failed to bind to interface %s", libtrace->uridata); |
---|
445 | return -1; |
---|
446 | } |
---|
447 | |
---|
448 | /* If promisc hasn't been specified, set it to "true" if we're |
---|
449 | * capturing on one interface, or "false" if we're capturing on |
---|
450 | * all interfaces. |
---|
451 | */ |
---|
452 | if (FORMAT(libtrace->format_data)->promisc==-1) { |
---|
453 | if (addr.sll_ifindex!=0) |
---|
454 | FORMAT(libtrace->format_data)->promisc=1; |
---|
455 | else |
---|
456 | FORMAT(libtrace->format_data)->promisc=0; |
---|
457 | } |
---|
458 | |
---|
459 | /* Enable promiscuous mode, if requested */ |
---|
460 | if (FORMAT(libtrace->format_data)->promisc) { |
---|
461 | struct packet_mreq mreq; |
---|
462 | socklen_t socklen = sizeof(mreq); |
---|
463 | memset(&mreq,0,sizeof(mreq)); |
---|
464 | mreq.mr_ifindex = addr.sll_ifindex; |
---|
465 | mreq.mr_type = PACKET_MR_PROMISC; |
---|
466 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
467 | SOL_PACKET, |
---|
468 | PACKET_ADD_MEMBERSHIP, |
---|
469 | &mreq, |
---|
470 | socklen)==-1) { |
---|
471 | perror("setsockopt(PROMISC)"); |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | /* Set the timestamp option on the socket - aim for the most detailed |
---|
476 | * clock resolution possible */ |
---|
477 | #ifdef SO_TIMESTAMPNS |
---|
478 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
479 | SOL_SOCKET, |
---|
480 | SO_TIMESTAMPNS, |
---|
481 | &one, |
---|
482 | (socklen_t)sizeof(one))!=-1) { |
---|
483 | FORMAT(libtrace->format_data)->timestamptype = TS_TIMESPEC; |
---|
484 | } |
---|
485 | else |
---|
486 | /* DANGER: This is a dangling else to only do the next setsockopt() if we fail the first! */ |
---|
487 | #endif |
---|
488 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
489 | SOL_SOCKET, |
---|
490 | SO_TIMESTAMP, |
---|
491 | &one, |
---|
492 | (socklen_t)sizeof(one))!=-1) { |
---|
493 | FORMAT(libtrace->format_data)->timestamptype = TS_TIMEVAL; |
---|
494 | } |
---|
495 | else |
---|
496 | FORMAT(libtrace->format_data)->timestamptype = TS_NONE; |
---|
497 | |
---|
498 | /* Push BPF filter into the kernel. At this stage we can safely assume |
---|
499 | * that the filterstring has been compiled, or the filter was supplied |
---|
500 | * pre-compiled. |
---|
501 | */ |
---|
502 | if (filter != NULL) { |
---|
503 | /* Check if the filter was successfully compiled. If not, |
---|
504 | * it is probably a bad filter and we should return an error |
---|
505 | * before the caller tries to read any packets */ |
---|
506 | if (filter->flag == 0) { |
---|
507 | return -1; |
---|
508 | } |
---|
509 | |
---|
510 | if (setsockopt(FORMAT(libtrace->format_data)->fd, |
---|
511 | SOL_SOCKET, |
---|
512 | SO_ATTACH_FILTER, |
---|
513 | &filter->filter, |
---|
514 | sizeof(filter->filter)) == -1) { |
---|
515 | perror("setsockopt(SO_ATTACH_FILTER)"); |
---|
516 | } else { |
---|
517 | /* The socket accepted the filter, so we need to |
---|
518 | * consume any buffered packets that were received |
---|
519 | * between opening the socket and applying the filter. |
---|
520 | */ |
---|
521 | void *buf = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
522 | while(recv(FORMAT(libtrace->format_data)->fd, |
---|
523 | buf, |
---|
524 | (size_t) LIBTRACE_PACKET_BUFSIZE, |
---|
525 | MSG_DONTWAIT) != -1) { } |
---|
526 | free(buf); |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | FORMAT(libtrace->format_data)->stats_valid=0; |
---|
531 | |
---|
532 | return 0; |
---|
533 | } |
---|
534 | static inline int socket_to_packetmmap( char * uridata, int ring_type, |
---|
535 | int fd, |
---|
536 | struct tpacket_req * req, |
---|
537 | char ** ring_location, |
---|
538 | uint32_t *max_order, |
---|
539 | char *error){ |
---|
540 | int val; |
---|
541 | |
---|
542 | /* Switch to TPACKET header version 2, we only try support v2 because v1 had problems */ |
---|
543 | |
---|
544 | val = TPACKET_V2; |
---|
545 | if (setsockopt(fd, |
---|
546 | SOL_PACKET, |
---|
547 | PACKET_VERSION, |
---|
548 | &val, |
---|
549 | sizeof(val)) == -1){ |
---|
550 | strncpy(error, "TPACKET2 not supported", 2048); |
---|
551 | return -1; |
---|
552 | } |
---|
553 | |
---|
554 | /* Try switch to a ring buffer. If it fails we assume the the kernel |
---|
555 | * cannot allocate a block of that size, so decrease max_block and retry. |
---|
556 | */ |
---|
557 | while(1) { |
---|
558 | if (*max_order <= 0) { |
---|
559 | strncpy(error,"Cannot allocate enough memory for ring buffer", 2048); |
---|
560 | return -1; |
---|
561 | } |
---|
562 | calculate_buffers(req, fd, uridata, *max_order); |
---|
563 | if (setsockopt(fd, |
---|
564 | SOL_PACKET, |
---|
565 | ring_type, |
---|
566 | req, |
---|
567 | sizeof(struct tpacket_req)) == -1) { |
---|
568 | if(errno == ENOMEM){ |
---|
569 | (*max_order)--; |
---|
570 | } else { |
---|
571 | strncpy(error, "Error setting the ring buffer size", 2048); |
---|
572 | return -1; |
---|
573 | } |
---|
574 | |
---|
575 | } else break; |
---|
576 | } |
---|
577 | |
---|
578 | /* Map the ring buffer into userspace */ |
---|
579 | *ring_location = mmap(NULL, |
---|
580 | req->tp_block_size * req->tp_block_nr, |
---|
581 | PROT_READ | PROT_WRITE, |
---|
582 | MAP_SHARED, |
---|
583 | fd, 0); |
---|
584 | if(*ring_location == MAP_FAILED){ |
---|
585 | strncpy(error, "Failed to map memory for ring buffer", 2048); |
---|
586 | return -1; |
---|
587 | } |
---|
588 | return 0; |
---|
589 | } |
---|
590 | static int linuxring_start_input(libtrace_t *libtrace){ |
---|
591 | |
---|
592 | char error[2048]; |
---|
593 | |
---|
594 | /* We set the socket up the same and then convert it to PACKET_MMAP */ |
---|
595 | if(linuxnative_start_input(libtrace) != 0) |
---|
596 | return -1; |
---|
597 | |
---|
598 | strncpy(error, "No known error", 2048); |
---|
599 | |
---|
600 | /* Make it a packetmmap */ |
---|
601 | if(socket_to_packetmmap(libtrace->uridata, PACKET_RX_RING, |
---|
602 | FORMAT(libtrace->format_data)->fd, |
---|
603 | &FORMAT(libtrace->format_data)->req, |
---|
604 | &FORMAT(libtrace->format_data)->rx_ring, |
---|
605 | &FORMAT(libtrace->format_data)->max_order, |
---|
606 | error) != 0){ |
---|
607 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Initialisation of packet MMAP failed: %s", error); |
---|
608 | close(DATAOUT(libtrace)->fd); |
---|
609 | free(libtrace->format_data); |
---|
610 | libtrace->format_data = NULL; |
---|
611 | return -1; |
---|
612 | } |
---|
613 | |
---|
614 | return 0; |
---|
615 | } |
---|
616 | |
---|
617 | static int linuxnative_start_output(libtrace_out_t *libtrace) |
---|
618 | { |
---|
619 | DATAOUT(libtrace)->fd = socket(PF_PACKET, SOCK_RAW, 0); |
---|
620 | if (DATAOUT(libtrace)->fd==-1) { |
---|
621 | free(DATAOUT(libtrace)); |
---|
622 | return -1; |
---|
623 | } |
---|
624 | |
---|
625 | return 0; |
---|
626 | } |
---|
627 | |
---|
628 | static int linuxring_start_output(libtrace_out_t *libtrace) |
---|
629 | { |
---|
630 | char error[2048]; |
---|
631 | /* We set the socket up the same and then convert it to PACKET_MMAP */ |
---|
632 | if(linuxnative_start_output(libtrace) != 0) |
---|
633 | return -1; |
---|
634 | |
---|
635 | /* Make it a packetmmap */ |
---|
636 | if(socket_to_packetmmap(libtrace->uridata, PACKET_TX_RING, |
---|
637 | DATAOUT(libtrace)->fd, |
---|
638 | &DATAOUT(libtrace)->req, |
---|
639 | &DATAOUT(libtrace)->tx_ring, |
---|
640 | &DATAOUT(libtrace)->max_order, |
---|
641 | error) != 0){ |
---|
642 | trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Initialisation of packet MMAP failed: %s", error); |
---|
643 | close(DATAOUT(libtrace)->fd); |
---|
644 | free(libtrace->format_data); |
---|
645 | libtrace->format_data = NULL; |
---|
646 | return -1; |
---|
647 | } |
---|
648 | |
---|
649 | DATAOUT(libtrace)->sock_hdr.sll_family = AF_PACKET; |
---|
650 | DATAOUT(libtrace)->sock_hdr.sll_protocol = 0; |
---|
651 | DATAOUT(libtrace)->sock_hdr.sll_ifindex = |
---|
652 | if_nametoindex(libtrace->uridata); |
---|
653 | DATAOUT(libtrace)->sock_hdr.sll_hatype = 0; |
---|
654 | DATAOUT(libtrace)->sock_hdr.sll_pkttype = 0; |
---|
655 | DATAOUT(libtrace)->sock_hdr.sll_halen = 0; |
---|
656 | DATAOUT(libtrace)->queue = 0; |
---|
657 | |
---|
658 | return 0; |
---|
659 | } |
---|
660 | |
---|
661 | static int linuxnative_pause_input(libtrace_t *libtrace) |
---|
662 | { |
---|
663 | close(FORMAT(libtrace->format_data)->fd); |
---|
664 | FORMAT(libtrace->format_data)->fd=-1; |
---|
665 | |
---|
666 | return 0; |
---|
667 | } |
---|
668 | static int linuxring_pause_input(libtrace_t *libtrace) |
---|
669 | { |
---|
670 | munmap(FORMAT(libtrace->format_data)->rx_ring, |
---|
671 | FORMAT(libtrace->format_data)->req.tp_block_size * |
---|
672 | FORMAT(libtrace->format_data)->req.tp_block_nr); |
---|
673 | FORMAT(libtrace->format_data)->rx_ring = NULL; |
---|
674 | return linuxnative_pause_input(libtrace); |
---|
675 | } |
---|
676 | |
---|
677 | static int linuxnative_fin_input(libtrace_t *libtrace) |
---|
678 | { |
---|
679 | if (libtrace->format_data) { |
---|
680 | if (FORMAT(libtrace->format_data)->filter != NULL) |
---|
681 | free(FORMAT(libtrace->format_data)->filter); |
---|
682 | free(libtrace->format_data); |
---|
683 | } |
---|
684 | |
---|
685 | return 0; |
---|
686 | } |
---|
687 | |
---|
688 | static int linuxnative_fin_output(libtrace_out_t *libtrace) |
---|
689 | { |
---|
690 | close(DATAOUT(libtrace)->fd); |
---|
691 | DATAOUT(libtrace)->fd=-1; |
---|
692 | free(libtrace->format_data); |
---|
693 | return 0; |
---|
694 | } |
---|
695 | static int linuxring_fin_output(libtrace_out_t *libtrace) |
---|
696 | { |
---|
697 | /* Make sure any remaining frames get sent */ |
---|
698 | sendto(DATAOUT(libtrace)->fd, |
---|
699 | NULL, |
---|
700 | 0, |
---|
701 | 0, |
---|
702 | (void *) &DATAOUT(libtrace)->sock_hdr, |
---|
703 | sizeof(DATAOUT(libtrace)->sock_hdr)); |
---|
704 | |
---|
705 | /* Unmap our data area */ |
---|
706 | munmap(DATAOUT(libtrace)->tx_ring, |
---|
707 | DATAOUT(libtrace)->req.tp_block_size * |
---|
708 | DATAOUT(libtrace)->req.tp_block_nr); |
---|
709 | |
---|
710 | return linuxnative_fin_output(libtrace); |
---|
711 | } |
---|
712 | |
---|
713 | /* Compiles a libtrace BPF filter for use with a linux native socket */ |
---|
714 | static int linuxnative_configure_bpf(libtrace_t *libtrace, |
---|
715 | libtrace_filter_t *filter) { |
---|
716 | #ifdef HAVE_LIBPCAP |
---|
717 | struct ifreq ifr; |
---|
718 | unsigned int arphrd; |
---|
719 | libtrace_dlt_t dlt; |
---|
720 | libtrace_filter_t *f; |
---|
721 | int sock; |
---|
722 | pcap_t *pcap; |
---|
723 | |
---|
724 | /* Take a copy of the filter object as it was passed in */ |
---|
725 | f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t)); |
---|
726 | memcpy(f, filter, sizeof(libtrace_filter_t)); |
---|
727 | |
---|
728 | /* If we are passed a filter with "flag" set to zero, then we must |
---|
729 | * compile the filterstring before continuing. This involves |
---|
730 | * determining the linktype, passing the filterstring to libpcap to |
---|
731 | * compile, and saving the result for trace_start() to push into the |
---|
732 | * kernel. |
---|
733 | * If flag is set to one, then the filter was probably generated using |
---|
734 | * trace_create_filter_from_bytecode() and so we don't need to do |
---|
735 | * anything (we've just copied it above). |
---|
736 | */ |
---|
737 | if (f->flag == 0) { |
---|
738 | sock = socket(PF_INET, SOCK_STREAM, 0); |
---|
739 | memset(&ifr, 0, sizeof(struct ifreq)); |
---|
740 | strncpy(ifr.ifr_name, libtrace->uridata, IF_NAMESIZE); |
---|
741 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) { |
---|
742 | perror("Can't get HWADDR for interface"); |
---|
743 | return -1; |
---|
744 | } |
---|
745 | close(sock); |
---|
746 | |
---|
747 | arphrd = ifr.ifr_hwaddr.sa_family; |
---|
748 | dlt = libtrace_to_pcap_dlt(arphrd_type_to_libtrace(arphrd)); |
---|
749 | |
---|
750 | pcap = pcap_open_dead(dlt, |
---|
751 | FORMAT(libtrace->format_data)->snaplen); |
---|
752 | |
---|
753 | if (pcap_compile(pcap, &f->filter, f->filterstring, 0, 0) == -1) { |
---|
754 | /* Filter didn't compile, set flag to 0 so we can |
---|
755 | * detect this when trace_start() is called and |
---|
756 | * produce a useful error |
---|
757 | */ |
---|
758 | f->flag = 0; |
---|
759 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
760 | "Failed to compile BPF filter (%s): %s", |
---|
761 | f->filterstring, pcap_geterr(pcap)); |
---|
762 | } else { |
---|
763 | /* Set the "flag" to indicate that the filterstring |
---|
764 | * has been compiled |
---|
765 | */ |
---|
766 | f->flag = 1; |
---|
767 | } |
---|
768 | |
---|
769 | pcap_close(pcap); |
---|
770 | |
---|
771 | } |
---|
772 | |
---|
773 | if (FORMAT(libtrace->format_data)->filter != NULL) |
---|
774 | free(FORMAT(libtrace->format_data)->filter); |
---|
775 | |
---|
776 | FORMAT(libtrace->format_data)->filter = f; |
---|
777 | |
---|
778 | return 0; |
---|
779 | #else |
---|
780 | return -1 |
---|
781 | #endif |
---|
782 | } |
---|
783 | static int linuxnative_config_input(libtrace_t *libtrace, |
---|
784 | trace_option_t option, |
---|
785 | void *data) |
---|
786 | { |
---|
787 | switch(option) { |
---|
788 | case TRACE_OPTION_SNAPLEN: |
---|
789 | FORMAT(libtrace->format_data)->snaplen=*(int*)data; |
---|
790 | return 0; |
---|
791 | case TRACE_OPTION_PROMISC: |
---|
792 | FORMAT(libtrace->format_data)->promisc=*(int*)data; |
---|
793 | return 0; |
---|
794 | case TRACE_OPTION_FILTER: |
---|
795 | return linuxnative_configure_bpf(libtrace, |
---|
796 | (libtrace_filter_t *) data); |
---|
797 | case TRACE_OPTION_META_FREQ: |
---|
798 | /* No meta-data for this format */ |
---|
799 | break; |
---|
800 | case TRACE_OPTION_EVENT_REALTIME: |
---|
801 | /* Live captures are always going to be in trace time */ |
---|
802 | break; |
---|
803 | /* Avoid default: so that future options will cause a warning |
---|
804 | * here to remind us to implement it, or flag it as |
---|
805 | * unimplementable |
---|
806 | */ |
---|
807 | } |
---|
808 | |
---|
809 | /* Don't set an error - trace_config will try to deal with the |
---|
810 | * option and will set an error if it fails */ |
---|
811 | return -1; |
---|
812 | } |
---|
813 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
814 | |
---|
815 | static int linuxnative_prepare_packet(libtrace_t *libtrace UNUSED, |
---|
816 | libtrace_packet_t *packet, void *buffer, |
---|
817 | libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
818 | |
---|
819 | if (packet->buffer != buffer && |
---|
820 | packet->buf_control == TRACE_CTRL_PACKET) { |
---|
821 | free(packet->buffer); |
---|
822 | } |
---|
823 | |
---|
824 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
---|
825 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
826 | } else |
---|
827 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
828 | |
---|
829 | |
---|
830 | packet->buffer = buffer; |
---|
831 | packet->header = buffer; |
---|
832 | packet->payload = (char *)buffer + |
---|
833 | sizeof(struct libtrace_linuxnative_header); |
---|
834 | packet->type = rt_type; |
---|
835 | |
---|
836 | /* |
---|
837 | if (libtrace->format_data == NULL) { |
---|
838 | if (linuxnative_init_input(libtrace)) |
---|
839 | return -1; |
---|
840 | } |
---|
841 | */ |
---|
842 | return 0; |
---|
843 | |
---|
844 | } |
---|
845 | |
---|
846 | static int linuxring_prepare_packet(libtrace_t *libtrace UNUSED, |
---|
847 | libtrace_packet_t *packet, void *buffer, |
---|
848 | libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
849 | |
---|
850 | if (packet->buffer != buffer && |
---|
851 | packet->buf_control == TRACE_CTRL_PACKET) { |
---|
852 | free(packet->buffer); |
---|
853 | } |
---|
854 | |
---|
855 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
---|
856 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
857 | } else |
---|
858 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
859 | |
---|
860 | |
---|
861 | packet->buffer = buffer; |
---|
862 | packet->header = buffer; |
---|
863 | packet->payload = (char *)buffer + |
---|
864 | TP_TRACE_START( |
---|
865 | TO_TP_HDR(packet->header)->tp_mac, |
---|
866 | TO_TP_HDR(packet->header)->tp_net, |
---|
867 | TPACKET_HDRLEN); |
---|
868 | packet->type = rt_type; |
---|
869 | |
---|
870 | /* |
---|
871 | if (libtrace->format_data == NULL) { |
---|
872 | if (linuxnative_init_input(libtrace)) |
---|
873 | return -1; |
---|
874 | } |
---|
875 | */ |
---|
876 | return 0; |
---|
877 | |
---|
878 | } |
---|
879 | |
---|
880 | #define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) |
---|
881 | |
---|
882 | /* 20 isn't enough on x86_64 */ |
---|
883 | #define CMSG_BUF_SIZE 128 |
---|
884 | |
---|
885 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
886 | static int linuxnative_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) |
---|
887 | { |
---|
888 | struct libtrace_linuxnative_header *hdr; |
---|
889 | struct msghdr msghdr; |
---|
890 | struct iovec iovec; |
---|
891 | unsigned char controlbuf[CMSG_BUF_SIZE]; |
---|
892 | struct cmsghdr *cmsg; |
---|
893 | int snaplen; |
---|
894 | uint32_t flags = 0; |
---|
895 | |
---|
896 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
897 | packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
898 | if (!packet->buffer) { |
---|
899 | perror("Cannot allocate buffer"); |
---|
900 | } |
---|
901 | } |
---|
902 | |
---|
903 | flags |= TRACE_PREP_OWN_BUFFER; |
---|
904 | |
---|
905 | packet->type = TRACE_RT_DATA_LINUX_NATIVE; |
---|
906 | |
---|
907 | hdr=(struct libtrace_linuxnative_header*)packet->buffer; |
---|
908 | snaplen=LIBTRACE_MIN( |
---|
909 | (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*hdr), |
---|
910 | (int)FORMAT(libtrace->format_data)->snaplen); |
---|
911 | |
---|
912 | /* Prepare the msghdr and iovec for the kernel to write the |
---|
913 | * captured packet into. The msghdr will point to the part of our |
---|
914 | * buffer reserved for sll header, while the iovec will point at |
---|
915 | * the buffer following the sll header. */ |
---|
916 | |
---|
917 | msghdr.msg_name = &hdr->hdr; |
---|
918 | msghdr.msg_namelen = sizeof(struct sockaddr_ll); |
---|
919 | |
---|
920 | msghdr.msg_iov = &iovec; |
---|
921 | msghdr.msg_iovlen = 1; |
---|
922 | |
---|
923 | msghdr.msg_control = &controlbuf; |
---|
924 | msghdr.msg_controllen = CMSG_BUF_SIZE; |
---|
925 | msghdr.msg_flags = 0; |
---|
926 | |
---|
927 | iovec.iov_base = (void*)(packet->buffer+sizeof(*hdr)); |
---|
928 | iovec.iov_len = snaplen; |
---|
929 | |
---|
930 | hdr->wirelen = recvmsg(FORMAT(libtrace->format_data)->fd, &msghdr, MSG_TRUNC); |
---|
931 | |
---|
932 | if (hdr->wirelen==~0U) { |
---|
933 | trace_set_err(libtrace,errno,"recvmsg"); |
---|
934 | return -1; |
---|
935 | } |
---|
936 | |
---|
937 | hdr->caplen=LIBTRACE_MIN((unsigned int)snaplen,(unsigned int)hdr->wirelen); |
---|
938 | |
---|
939 | /* Extract the timestamps from the msghdr and store them in our |
---|
940 | * linux native encapsulation, so that we can preserve the formatting |
---|
941 | * across multiple architectures */ |
---|
942 | |
---|
943 | for (cmsg = CMSG_FIRSTHDR(&msghdr); |
---|
944 | cmsg != NULL; |
---|
945 | cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
---|
946 | if (cmsg->cmsg_level == SOL_SOCKET |
---|
947 | && cmsg->cmsg_type == SO_TIMESTAMP |
---|
948 | && cmsg->cmsg_len <= CMSG_LEN(sizeof(struct timeval))) { |
---|
949 | |
---|
950 | struct timeval *tv; |
---|
951 | tv = (struct timeval *)CMSG_DATA(cmsg); |
---|
952 | |
---|
953 | |
---|
954 | hdr->tv.tv_sec = tv->tv_sec; |
---|
955 | hdr->tv.tv_usec = tv->tv_usec; |
---|
956 | hdr->timestamptype = TS_TIMEVAL; |
---|
957 | break; |
---|
958 | } |
---|
959 | #ifdef SO_TIMESTAMPNS |
---|
960 | else if (cmsg->cmsg_level == SOL_SOCKET |
---|
961 | && cmsg->cmsg_type == SO_TIMESTAMPNS |
---|
962 | && cmsg->cmsg_len <= CMSG_LEN(sizeof(struct timespec))) { |
---|
963 | |
---|
964 | struct timespec *tv; |
---|
965 | tv = (struct timespec *)CMSG_DATA(cmsg); |
---|
966 | |
---|
967 | hdr->ts.tv_sec = tv->tv_sec; |
---|
968 | hdr->ts.tv_nsec = tv->tv_nsec; |
---|
969 | hdr->timestamptype = TS_TIMESPEC; |
---|
970 | break; |
---|
971 | } |
---|
972 | #endif |
---|
973 | } |
---|
974 | |
---|
975 | /* Did we not get given a timestamp? Try to get one from the |
---|
976 | * file descriptor directly */ |
---|
977 | if (cmsg == NULL) { |
---|
978 | struct timeval tv; |
---|
979 | if (ioctl(FORMAT(libtrace->format_data)->fd, |
---|
980 | SIOCGSTAMP,&tv)==0) { |
---|
981 | hdr->tv.tv_sec = tv.tv_sec; |
---|
982 | hdr->tv.tv_usec = tv.tv_usec; |
---|
983 | hdr->timestamptype = TS_TIMEVAL; |
---|
984 | } |
---|
985 | else { |
---|
986 | hdr->timestamptype = TS_NONE; |
---|
987 | } |
---|
988 | } |
---|
989 | |
---|
990 | /* Buffer contains all of our packet (including our custom header) so |
---|
991 | * we just need to get prepare_packet to set all our packet pointers |
---|
992 | * appropriately */ |
---|
993 | |
---|
994 | if (linuxnative_prepare_packet(libtrace, packet, packet->buffer, |
---|
995 | packet->type, flags)) |
---|
996 | return -1; |
---|
997 | |
---|
998 | return hdr->wirelen+sizeof(*hdr); |
---|
999 | } |
---|
1000 | |
---|
1001 | #define LIBTRACE_BETWEEN(test,a,b) ((test) >= (a) && (test) < (b)) |
---|
1002 | static int linuxring_get_capture_length(const libtrace_packet_t *packet); |
---|
1003 | static int linuxring_get_framing_length(const libtrace_packet_t *packet); |
---|
1004 | |
---|
1005 | /* Release a frame back to the kernel or free() if it's a malloc'd buffer |
---|
1006 | */ |
---|
1007 | inline static void ring_release_frame(libtrace_t *libtrace, libtrace_packet_t *packet ){ |
---|
1008 | /* Free the old packet */ |
---|
1009 | if(packet->buffer == NULL) |
---|
1010 | return; |
---|
1011 | |
---|
1012 | if(packet->buf_control == TRACE_CTRL_PACKET){ |
---|
1013 | free(packet->buffer); |
---|
1014 | packet->buffer = NULL; |
---|
1015 | } |
---|
1016 | if(packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
1017 | struct linux_format_data_t *ftd = FORMAT(libtrace->format_data); |
---|
1018 | |
---|
1019 | /* Check it's within our buffer first */ |
---|
1020 | if(LIBTRACE_BETWEEN((char *) packet->buffer, |
---|
1021 | (char *) ftd->rx_ring, |
---|
1022 | ftd->rx_ring |
---|
1023 | + ftd->req.tp_block_size * ftd->req.tp_block_nr)){ |
---|
1024 | TO_TP_HDR(packet->buffer)->tp_status = 0; |
---|
1025 | packet->buffer = NULL; |
---|
1026 | } |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | static int linuxring_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
1031 | |
---|
1032 | struct tpacket2_hdr *header; |
---|
1033 | struct pollfd pollset; |
---|
1034 | int ret; |
---|
1035 | unsigned int snaplen; |
---|
1036 | |
---|
1037 | ring_release_frame(libtrace, packet); |
---|
1038 | |
---|
1039 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
1040 | packet->type = TRACE_RT_DATA_LINUX_RING; |
---|
1041 | |
---|
1042 | /* Fetch the current frame */ |
---|
1043 | header = GET_CURRENT_BUFFER(libtrace); |
---|
1044 | assert((((unsigned long) header) & (pagesize - 1)) == 0); |
---|
1045 | |
---|
1046 | /* TP_STATUS_USER means that we can use the frame. |
---|
1047 | * When a slot does not have this flag set, the frame is not |
---|
1048 | * ready for consumption. |
---|
1049 | */ |
---|
1050 | while (!(header->tp_status & TP_STATUS_USER)) { |
---|
1051 | pollset.fd = FORMAT(libtrace->format_data)->fd; |
---|
1052 | pollset.events = POLLIN; |
---|
1053 | pollset.revents = 0; |
---|
1054 | /* Wait for more data */ |
---|
1055 | ret = poll(&pollset, 1, -1); |
---|
1056 | if (ret < 0) { |
---|
1057 | if (errno != EINTR) |
---|
1058 | trace_set_err(libtrace,errno,"poll()"); |
---|
1059 | return -1; |
---|
1060 | } |
---|
1061 | } |
---|
1062 | |
---|
1063 | packet->buffer = header; |
---|
1064 | |
---|
1065 | /* If a snaplen was configured, automatically truncate the packet to |
---|
1066 | * the desired length. |
---|
1067 | */ |
---|
1068 | snaplen=LIBTRACE_MIN( |
---|
1069 | (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*header), |
---|
1070 | (int)FORMAT(libtrace->format_data)->snaplen); |
---|
1071 | |
---|
1072 | TO_TP_HDR(packet->buffer)->tp_snaplen = LIBTRACE_MIN((unsigned int)snaplen, TO_TP_HDR(packet->buffer)->tp_len); |
---|
1073 | |
---|
1074 | /* Move to next buffer */ |
---|
1075 | FORMAT(libtrace->format_data)->rxring_offset++; |
---|
1076 | FORMAT(libtrace->format_data)->rxring_offset %= FORMAT(libtrace->format_data)->req.tp_frame_nr; |
---|
1077 | |
---|
1078 | /* We just need to get prepare_packet to set all our packet pointers |
---|
1079 | * appropriately */ |
---|
1080 | if (linuxring_prepare_packet(libtrace, packet, packet->buffer, |
---|
1081 | packet->type, 0)) |
---|
1082 | return -1; |
---|
1083 | return linuxring_get_framing_length(packet) + |
---|
1084 | linuxring_get_capture_length(packet); |
---|
1085 | |
---|
1086 | } |
---|
1087 | |
---|
1088 | /* Non-blocking read */ |
---|
1089 | static libtrace_eventobj_t linuxring_event(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
1090 | struct tpacket2_hdr *header; |
---|
1091 | libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
1092 | |
---|
1093 | /* We must free the old packet, otherwise select() will instantly return |
---|
1094 | */ |
---|
1095 | ring_release_frame(libtrace, packet); |
---|
1096 | |
---|
1097 | /* Fetch the current frame */ |
---|
1098 | header = GET_CURRENT_BUFFER(libtrace); |
---|
1099 | if(header->tp_status & TP_STATUS_USER){ |
---|
1100 | /* We have a frame waiting */ |
---|
1101 | event.size = trace_read_packet(libtrace, packet); |
---|
1102 | event.type = TRACE_EVENT_PACKET; |
---|
1103 | } else { |
---|
1104 | /* Ok we don't have a packet waiting */ |
---|
1105 | event.type = TRACE_EVENT_IOWAIT; |
---|
1106 | event.fd = FORMAT(libtrace->format_data)->fd; |
---|
1107 | } |
---|
1108 | |
---|
1109 | return event; |
---|
1110 | } |
---|
1111 | |
---|
1112 | |
---|
1113 | static int linuxnative_write_packet(libtrace_out_t *trace, |
---|
1114 | libtrace_packet_t *packet) |
---|
1115 | { |
---|
1116 | struct sockaddr_ll hdr; |
---|
1117 | int ret = 0; |
---|
1118 | |
---|
1119 | if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA) |
---|
1120 | return 0; |
---|
1121 | |
---|
1122 | hdr.sll_family = AF_PACKET; |
---|
1123 | hdr.sll_protocol = 0; |
---|
1124 | hdr.sll_ifindex = if_nametoindex(trace->uridata); |
---|
1125 | hdr.sll_hatype = 0; |
---|
1126 | hdr.sll_pkttype = 0; |
---|
1127 | hdr.sll_halen = htons(6); /* FIXME */ |
---|
1128 | memcpy(hdr.sll_addr,packet->payload,(size_t)ntohs(hdr.sll_halen)); |
---|
1129 | |
---|
1130 | /* This is pretty easy, just send the payload using sendto() (after |
---|
1131 | * setting up the sll header properly, of course) */ |
---|
1132 | ret = sendto(DATAOUT(trace)->fd, |
---|
1133 | packet->payload, |
---|
1134 | trace_get_capture_length(packet), |
---|
1135 | 0, |
---|
1136 | (struct sockaddr*)&hdr, (socklen_t)sizeof(hdr)); |
---|
1137 | |
---|
1138 | if (ret < 0) { |
---|
1139 | trace_set_err_out(trace, errno, "sendto failed"); |
---|
1140 | } |
---|
1141 | |
---|
1142 | return ret; |
---|
1143 | |
---|
1144 | } |
---|
1145 | static int linuxring_write_packet(libtrace_out_t *trace, |
---|
1146 | libtrace_packet_t *packet) |
---|
1147 | { |
---|
1148 | struct tpacket2_hdr *header; |
---|
1149 | struct pollfd pollset; |
---|
1150 | struct socket_addr; |
---|
1151 | int ret; |
---|
1152 | unsigned max_size; |
---|
1153 | void * off; |
---|
1154 | |
---|
1155 | if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA) |
---|
1156 | return 0; |
---|
1157 | |
---|
1158 | max_size = DATAOUT(trace)->req.tp_frame_size - |
---|
1159 | - TPACKET_HDRLEN + sizeof(struct sockaddr_ll); |
---|
1160 | |
---|
1161 | header = (void *) DATAOUT(trace)->tx_ring + |
---|
1162 | (DATAOUT(trace)->txring_offset * DATAOUT(trace)->req.tp_frame_size); |
---|
1163 | |
---|
1164 | while(header->tp_status != TP_STATUS_AVAILABLE){ |
---|
1165 | /* if none available: wait on more data */ |
---|
1166 | pollset.fd = DATAOUT(trace)->fd; |
---|
1167 | pollset.events = POLLOUT; |
---|
1168 | pollset.revents = 0; |
---|
1169 | ret = poll(&pollset, 1, 1000); |
---|
1170 | if (ret < 0 && errno != EINTR) { |
---|
1171 | perror("poll"); |
---|
1172 | return -1; |
---|
1173 | } |
---|
1174 | if(ret == 0) |
---|
1175 | /* Timeout something has gone wrong - maybe the queue is |
---|
1176 | * to large so try issue another send command |
---|
1177 | */ |
---|
1178 | ret = sendto(DATAOUT(trace)->fd, |
---|
1179 | NULL, |
---|
1180 | 0, |
---|
1181 | 0, |
---|
1182 | (void *) &DATAOUT(trace)->sock_hdr, |
---|
1183 | sizeof(DATAOUT(trace)->sock_hdr)); |
---|
1184 | if (ret < 0) { |
---|
1185 | trace_set_err_out(trace, errno, |
---|
1186 | "sendto after timeout failed"); |
---|
1187 | return -1; |
---|
1188 | } |
---|
1189 | } |
---|
1190 | |
---|
1191 | header->tp_len = trace_get_capture_length(packet); |
---|
1192 | |
---|
1193 | /* We cannot write the whole packet so just write part of it */ |
---|
1194 | if (header->tp_len > max_size) |
---|
1195 | header->tp_len = max_size; |
---|
1196 | |
---|
1197 | /* Fill packet - no sockaddr_ll in header when writing to the TX_RING */ |
---|
1198 | off = ((void *) header) + (TPACKET_HDRLEN - sizeof(struct sockaddr_ll)); |
---|
1199 | memcpy(off, |
---|
1200 | (char *) packet->payload, |
---|
1201 | header->tp_len); |
---|
1202 | |
---|
1203 | /* 'Send it' and increase ring pointer to the next frame */ |
---|
1204 | header->tp_status = TP_STATUS_SEND_REQUEST; |
---|
1205 | DATAOUT(trace)->txring_offset = (DATAOUT(trace)->txring_offset + 1) % |
---|
1206 | DATAOUT(trace)->req.tp_frame_nr; |
---|
1207 | |
---|
1208 | /* Notify kernel there are frames to send */ |
---|
1209 | DATAOUT(trace)->queue ++; |
---|
1210 | DATAOUT(trace)->queue %= TX_MAX_QUEUE; |
---|
1211 | if(DATAOUT(trace)->queue == 0){ |
---|
1212 | ret = sendto(DATAOUT(trace)->fd, |
---|
1213 | NULL, |
---|
1214 | 0, |
---|
1215 | MSG_DONTWAIT, |
---|
1216 | (void *) &DATAOUT(trace)->sock_hdr, |
---|
1217 | sizeof(DATAOUT(trace)->sock_hdr)); |
---|
1218 | if (ret < 0) { |
---|
1219 | trace_set_err_out(trace, errno, "sendto failed"); |
---|
1220 | return -1; |
---|
1221 | } |
---|
1222 | } |
---|
1223 | return header->tp_len; |
---|
1224 | |
---|
1225 | } |
---|
1226 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
1227 | |
---|
1228 | static inline libtrace_linktype_t get_libtrace_link_type(uint16_t linktype){ |
---|
1229 | /* Convert the ARPHRD type into an appropriate libtrace link type */ |
---|
1230 | switch (linktype) { |
---|
1231 | case LIBTRACE_ARPHRD_ETHER: |
---|
1232 | case LIBTRACE_ARPHRD_LOOPBACK: |
---|
1233 | return TRACE_TYPE_ETH; |
---|
1234 | case LIBTRACE_ARPHRD_PPP: |
---|
1235 | return TRACE_TYPE_NONE; |
---|
1236 | case LIBTRACE_ARPHRD_IEEE80211_RADIOTAP: |
---|
1237 | return TRACE_TYPE_80211_RADIO; |
---|
1238 | case LIBTRACE_ARPHRD_IEEE80211: |
---|
1239 | return TRACE_TYPE_80211; |
---|
1240 | case LIBTRACE_ARPHRD_SIT: |
---|
1241 | case LIBTRACE_ARPHRD_NONE: |
---|
1242 | return TRACE_TYPE_NONE; |
---|
1243 | default: /* shrug, beyond me! */ |
---|
1244 | printf("unknown Linux ARPHRD type 0x%04x\n",linktype); |
---|
1245 | return (libtrace_linktype_t)~0U; |
---|
1246 | } |
---|
1247 | } |
---|
1248 | static libtrace_linktype_t linuxnative_get_link_type(const struct libtrace_packet_t *packet) { |
---|
1249 | uint16_t linktype=(((struct libtrace_linuxnative_header*)(packet->buffer)) |
---|
1250 | ->hdr.sll_hatype); |
---|
1251 | return get_libtrace_link_type(linktype); |
---|
1252 | } |
---|
1253 | static libtrace_linktype_t linuxring_get_link_type(const struct libtrace_packet_t *packet) { |
---|
1254 | uint16_t linktype= GET_SOCKADDR_HDR(packet->buffer)->sll_hatype; |
---|
1255 | return get_libtrace_link_type(linktype); |
---|
1256 | } |
---|
1257 | |
---|
1258 | static inline libtrace_direction_t get_libtrace_direction(uint8_t pkttype){ |
---|
1259 | switch (pkttype) { |
---|
1260 | case PACKET_OUTGOING: |
---|
1261 | case PACKET_LOOPBACK: |
---|
1262 | return TRACE_DIR_OUTGOING; |
---|
1263 | case PACKET_OTHERHOST: |
---|
1264 | return TRACE_DIR_OTHER; |
---|
1265 | default: |
---|
1266 | return TRACE_DIR_INCOMING; |
---|
1267 | } |
---|
1268 | } |
---|
1269 | static libtrace_direction_t linuxnative_get_direction(const struct libtrace_packet_t *packet) { |
---|
1270 | return get_libtrace_direction(((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_pkttype); |
---|
1271 | } |
---|
1272 | static libtrace_direction_t linuxring_get_direction(const struct libtrace_packet_t *packet) { |
---|
1273 | return get_libtrace_direction(GET_SOCKADDR_HDR(packet->buffer)->sll_pkttype); |
---|
1274 | } |
---|
1275 | |
---|
1276 | static libtrace_direction_t set_direction(struct sockaddr_ll * skadr, libtrace_direction_t direction){ |
---|
1277 | switch (direction) { |
---|
1278 | case TRACE_DIR_OUTGOING: |
---|
1279 | skadr->sll_pkttype = PACKET_OUTGOING; |
---|
1280 | return TRACE_DIR_OUTGOING; |
---|
1281 | case TRACE_DIR_INCOMING: |
---|
1282 | skadr->sll_pkttype = PACKET_HOST; |
---|
1283 | return TRACE_DIR_INCOMING; |
---|
1284 | case TRACE_DIR_OTHER: |
---|
1285 | skadr->sll_pkttype = PACKET_OTHERHOST; |
---|
1286 | return TRACE_DIR_OTHER; |
---|
1287 | default: |
---|
1288 | return -1; |
---|
1289 | } |
---|
1290 | } |
---|
1291 | static libtrace_direction_t linuxnative_set_direction( |
---|
1292 | libtrace_packet_t *packet, |
---|
1293 | libtrace_direction_t direction) { |
---|
1294 | return set_direction(&((struct libtrace_linuxnative_header*)(packet->buffer))->hdr, direction); |
---|
1295 | } |
---|
1296 | static libtrace_direction_t linuxring_set_direction( |
---|
1297 | libtrace_packet_t *packet, |
---|
1298 | libtrace_direction_t direction) { |
---|
1299 | return set_direction(GET_SOCKADDR_HDR(packet->buffer), direction); |
---|
1300 | } |
---|
1301 | |
---|
1302 | static struct timespec linuxnative_get_timespec(const libtrace_packet_t *packet) |
---|
1303 | { |
---|
1304 | struct libtrace_linuxnative_header *hdr = |
---|
1305 | (struct libtrace_linuxnative_header*) packet->buffer; |
---|
1306 | /* We have to upconvert from timeval to timespec */ |
---|
1307 | if (hdr->timestamptype == TS_TIMEVAL) { |
---|
1308 | struct timespec ts; |
---|
1309 | ts.tv_sec = hdr->tv.tv_sec; |
---|
1310 | ts.tv_nsec = hdr->tv.tv_usec*1000; |
---|
1311 | return ts; |
---|
1312 | } |
---|
1313 | else { |
---|
1314 | struct timespec ts; |
---|
1315 | ts.tv_sec = hdr->ts.tv_sec; |
---|
1316 | ts.tv_nsec = hdr->ts.tv_nsec; |
---|
1317 | return ts; |
---|
1318 | } |
---|
1319 | } |
---|
1320 | static struct timespec linuxring_get_timespec(const libtrace_packet_t *packet) |
---|
1321 | { |
---|
1322 | struct timespec ts; |
---|
1323 | ts.tv_sec = TO_TP_HDR(packet->buffer)->tp_sec; |
---|
1324 | ts.tv_nsec = TO_TP_HDR(packet->buffer)->tp_nsec; |
---|
1325 | return ts; |
---|
1326 | } |
---|
1327 | |
---|
1328 | |
---|
1329 | static struct timeval linuxnative_get_timeval(const libtrace_packet_t *packet) |
---|
1330 | { |
---|
1331 | struct libtrace_linuxnative_header *hdr = |
---|
1332 | (struct libtrace_linuxnative_header*) packet->buffer; |
---|
1333 | /* We have to downconvert from timespec to timeval */ |
---|
1334 | if (hdr->timestamptype == TS_TIMESPEC) { |
---|
1335 | struct timeval tv; |
---|
1336 | tv.tv_sec = hdr->ts.tv_sec; |
---|
1337 | tv.tv_usec = hdr->ts.tv_nsec/1000; |
---|
1338 | return tv; |
---|
1339 | } |
---|
1340 | else { |
---|
1341 | struct timeval tv; |
---|
1342 | tv.tv_sec = hdr->tv.tv_sec; |
---|
1343 | tv.tv_usec = hdr->tv.tv_usec; |
---|
1344 | return tv; |
---|
1345 | } |
---|
1346 | } |
---|
1347 | static struct timeval linuxring_get_timeval(const libtrace_packet_t *packet) |
---|
1348 | { |
---|
1349 | struct timeval tv; |
---|
1350 | tv.tv_sec = TO_TP_HDR(packet->buffer)->tp_sec; |
---|
1351 | tv.tv_usec = TO_TP_HDR(packet->buffer)->tp_nsec / 1000; |
---|
1352 | return tv; |
---|
1353 | } |
---|
1354 | |
---|
1355 | static int linuxnative_get_capture_length(const libtrace_packet_t *packet) |
---|
1356 | { |
---|
1357 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->caplen; |
---|
1358 | } |
---|
1359 | |
---|
1360 | static int linuxring_get_capture_length(const libtrace_packet_t *packet) |
---|
1361 | { |
---|
1362 | return TO_TP_HDR(packet->buffer)->tp_snaplen; |
---|
1363 | } |
---|
1364 | |
---|
1365 | static int linuxnative_get_wire_length(const libtrace_packet_t *packet) |
---|
1366 | { |
---|
1367 | |
---|
1368 | int wirelen = ((struct libtrace_linuxnative_header*)(packet->buffer))->wirelen; |
---|
1369 | |
---|
1370 | /* Include the missing FCS */ |
---|
1371 | if (trace_get_link_type(packet) == TRACE_TYPE_ETH) |
---|
1372 | wirelen += 4; |
---|
1373 | |
---|
1374 | return wirelen; |
---|
1375 | } |
---|
1376 | |
---|
1377 | static int linuxring_get_wire_length(const libtrace_packet_t *packet) |
---|
1378 | { |
---|
1379 | int wirelen = TO_TP_HDR(packet->buffer)->tp_len; |
---|
1380 | |
---|
1381 | /* Include the missing FCS */ |
---|
1382 | if (trace_get_link_type(packet) == TRACE_TYPE_ETH) |
---|
1383 | wirelen += 4; |
---|
1384 | |
---|
1385 | return wirelen; |
---|
1386 | } |
---|
1387 | |
---|
1388 | static int linuxnative_get_framing_length(UNUSED |
---|
1389 | const libtrace_packet_t *packet) |
---|
1390 | { |
---|
1391 | return sizeof(struct libtrace_linuxnative_header); |
---|
1392 | } |
---|
1393 | |
---|
1394 | static int linuxring_get_framing_length(const libtrace_packet_t *packet) |
---|
1395 | { |
---|
1396 | /* |
---|
1397 | * Need to make frame_length + capture_length = complete capture length |
---|
1398 | * so include alligment whitespace. So reverse calculate from packet. |
---|
1399 | */ |
---|
1400 | return (char *) packet->payload - (char *) packet->buffer; |
---|
1401 | } |
---|
1402 | |
---|
1403 | static size_t linuxnative_set_capture_length(libtrace_packet_t *packet, |
---|
1404 | size_t size) { |
---|
1405 | |
---|
1406 | struct libtrace_linuxnative_header *linux_hdr = NULL; |
---|
1407 | assert(packet); |
---|
1408 | if (size > trace_get_capture_length(packet)) { |
---|
1409 | /* We should avoid making a packet larger */ |
---|
1410 | return trace_get_capture_length(packet); |
---|
1411 | } |
---|
1412 | |
---|
1413 | /* Reset the cached capture length */ |
---|
1414 | packet->capture_length = -1; |
---|
1415 | |
---|
1416 | linux_hdr = (struct libtrace_linuxnative_header *)packet->header; |
---|
1417 | linux_hdr->caplen = size; |
---|
1418 | return trace_get_capture_length(packet); |
---|
1419 | } |
---|
1420 | |
---|
1421 | static size_t linuxring_set_capture_length(libtrace_packet_t *packet, |
---|
1422 | size_t size) { |
---|
1423 | assert(packet); |
---|
1424 | if (size > trace_get_capture_length(packet)) { |
---|
1425 | /* We should avoid making a packet larger */ |
---|
1426 | return trace_get_capture_length(packet); |
---|
1427 | } |
---|
1428 | |
---|
1429 | /* Reset the cached capture length */ |
---|
1430 | packet->capture_length = -1; |
---|
1431 | |
---|
1432 | TO_TP_HDR(packet->buffer)->tp_snaplen = size; |
---|
1433 | |
---|
1434 | return trace_get_capture_length(packet); |
---|
1435 | } |
---|
1436 | |
---|
1437 | static int linuxnative_get_fd(const libtrace_t *trace) { |
---|
1438 | if (trace->format_data == NULL) |
---|
1439 | return -1; |
---|
1440 | return FORMAT(trace->format_data)->fd; |
---|
1441 | } |
---|
1442 | |
---|
1443 | /* Linux doesn't keep track how many packets were seen before filtering |
---|
1444 | * so we can't tell how many packets were filtered. Bugger. So annoying. |
---|
1445 | * |
---|
1446 | * Since we tell libtrace that we do support filtering, if we don't declare |
---|
1447 | * this here as failing, libtrace will happily report for us that it didn't |
---|
1448 | * filter any packets, so don't lie -- return that we don't know. |
---|
1449 | */ |
---|
1450 | static uint64_t linuxnative_get_filtered_packets(libtrace_t *trace UNUSED) { |
---|
1451 | return UINT64_MAX; |
---|
1452 | } |
---|
1453 | |
---|
1454 | /* Number of packets that passed filtering */ |
---|
1455 | static uint64_t linuxnative_get_captured_packets(libtrace_t *trace) { |
---|
1456 | if (trace->format_data == NULL) |
---|
1457 | return UINT64_MAX; |
---|
1458 | if (FORMAT(trace->format_data)->fd == -1) { |
---|
1459 | /* This is probably a 'dead' trace so obviously we can't query |
---|
1460 | * the socket for capture counts, can we? */ |
---|
1461 | return UINT64_MAX; |
---|
1462 | } |
---|
1463 | |
---|
1464 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
1465 | if ((FORMAT(trace->format_data)->stats_valid & 1) |
---|
1466 | || FORMAT(trace->format_data)->stats_valid == 0) { |
---|
1467 | socklen_t len = sizeof(FORMAT(trace->format_data)->stats); |
---|
1468 | getsockopt(FORMAT(trace->format_data)->fd, |
---|
1469 | SOL_PACKET, |
---|
1470 | PACKET_STATISTICS, |
---|
1471 | &FORMAT(trace->format_data)->stats, |
---|
1472 | &len); |
---|
1473 | FORMAT(trace->format_data)->stats_valid |= 1; |
---|
1474 | } |
---|
1475 | |
---|
1476 | return FORMAT(trace->format_data)->stats.tp_packets; |
---|
1477 | #else |
---|
1478 | return UINT64_MAX; |
---|
1479 | #endif |
---|
1480 | } |
---|
1481 | |
---|
1482 | /* Number of packets that got past filtering and were then dropped because |
---|
1483 | * of lack of space |
---|
1484 | */ |
---|
1485 | static uint64_t linuxnative_get_dropped_packets(libtrace_t *trace) { |
---|
1486 | if (trace->format_data == NULL) |
---|
1487 | return UINT64_MAX; |
---|
1488 | if (FORMAT(trace->format_data)->fd == -1) { |
---|
1489 | /* This is probably a 'dead' trace so obviously we can't query |
---|
1490 | * the socket for drop counts, can we? */ |
---|
1491 | return UINT64_MAX; |
---|
1492 | } |
---|
1493 | |
---|
1494 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
1495 | if ((FORMAT(trace->format_data)->stats_valid & 2) |
---|
1496 | || (FORMAT(trace->format_data)->stats_valid==0)) { |
---|
1497 | socklen_t len = sizeof(FORMAT(trace->format_data)->stats); |
---|
1498 | getsockopt(FORMAT(trace->format_data)->fd, |
---|
1499 | SOL_PACKET, |
---|
1500 | PACKET_STATISTICS, |
---|
1501 | &FORMAT(trace->format_data)->stats, |
---|
1502 | &len); |
---|
1503 | FORMAT(trace->format_data)->stats_valid |= 2; |
---|
1504 | } |
---|
1505 | |
---|
1506 | return FORMAT(trace->format_data)->stats.tp_drops; |
---|
1507 | #else |
---|
1508 | return UINT64_MAX; |
---|
1509 | #endif |
---|
1510 | } |
---|
1511 | |
---|
1512 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
1513 | static void linuxnative_help(void) { |
---|
1514 | printf("linuxnative format module: $Revision: 1793 $\n"); |
---|
1515 | printf("Supported input URIs:\n"); |
---|
1516 | printf("\tint:eth0\n"); |
---|
1517 | printf("\n"); |
---|
1518 | printf("Supported output URIs:\n"); |
---|
1519 | printf("\tint:eth0\n"); |
---|
1520 | printf("\n"); |
---|
1521 | return; |
---|
1522 | } |
---|
1523 | |
---|
1524 | static void linuxring_help(void) { |
---|
1525 | printf("linuxring format module: $Revision: 1793 $\n"); |
---|
1526 | printf("Supported input URIs:\n"); |
---|
1527 | printf("\tring:eth0\n"); |
---|
1528 | printf("\n"); |
---|
1529 | printf("Supported output URIs:\n"); |
---|
1530 | printf("\tring:eth0\n"); |
---|
1531 | printf("\n"); |
---|
1532 | return; |
---|
1533 | } |
---|
1534 | |
---|
1535 | static struct libtrace_format_t linuxnative = { |
---|
1536 | "int", |
---|
1537 | "$Id$", |
---|
1538 | TRACE_FORMAT_LINUX_NATIVE, |
---|
1539 | linuxnative_probe_filename, /* probe filename */ |
---|
1540 | NULL, /* probe magic */ |
---|
1541 | linuxnative_init_input, /* init_input */ |
---|
1542 | linuxnative_config_input, /* config_input */ |
---|
1543 | linuxnative_start_input, /* start_input */ |
---|
1544 | linuxnative_pause_input, /* pause_input */ |
---|
1545 | linuxnative_init_output, /* init_output */ |
---|
1546 | NULL, /* config_output */ |
---|
1547 | linuxnative_start_output, /* start_ouput */ |
---|
1548 | linuxnative_fin_input, /* fin_input */ |
---|
1549 | linuxnative_fin_output, /* fin_output */ |
---|
1550 | linuxnative_read_packet, /* read_packet */ |
---|
1551 | linuxnative_prepare_packet, /* prepare_packet */ |
---|
1552 | NULL, /* fin_packet */ |
---|
1553 | linuxnative_write_packet, /* write_packet */ |
---|
1554 | linuxnative_get_link_type, /* get_link_type */ |
---|
1555 | linuxnative_get_direction, /* get_direction */ |
---|
1556 | linuxnative_set_direction, /* set_direction */ |
---|
1557 | NULL, /* get_erf_timestamp */ |
---|
1558 | linuxnative_get_timeval, /* get_timeval */ |
---|
1559 | linuxnative_get_timespec, /* get_timespec */ |
---|
1560 | NULL, /* get_seconds */ |
---|
1561 | NULL, /* seek_erf */ |
---|
1562 | NULL, /* seek_timeval */ |
---|
1563 | NULL, /* seek_seconds */ |
---|
1564 | linuxnative_get_capture_length, /* get_capture_length */ |
---|
1565 | linuxnative_get_wire_length, /* get_wire_length */ |
---|
1566 | linuxnative_get_framing_length, /* get_framing_length */ |
---|
1567 | linuxnative_set_capture_length, /* set_capture_length */ |
---|
1568 | NULL, /* get_received_packets */ |
---|
1569 | linuxnative_get_filtered_packets,/* get_filtered_packets */ |
---|
1570 | linuxnative_get_dropped_packets,/* get_dropped_packets */ |
---|
1571 | linuxnative_get_captured_packets,/* get_captured_packets */ |
---|
1572 | linuxnative_get_fd, /* get_fd */ |
---|
1573 | trace_event_device, /* trace_event */ |
---|
1574 | linuxnative_help, /* help */ |
---|
1575 | NULL |
---|
1576 | }; |
---|
1577 | |
---|
1578 | static struct libtrace_format_t linuxring = { |
---|
1579 | "ring", |
---|
1580 | "$Id$", |
---|
1581 | TRACE_FORMAT_LINUX_RING, |
---|
1582 | linuxnative_probe_filename, /* probe filename */ |
---|
1583 | NULL, /* probe magic */ |
---|
1584 | linuxring_init_input, /* init_input */ |
---|
1585 | linuxnative_config_input, /* config_input */ |
---|
1586 | linuxring_start_input, /* start_input */ |
---|
1587 | linuxring_pause_input, /* pause_input */ |
---|
1588 | linuxring_init_output, /* init_output */ |
---|
1589 | NULL, /* config_output */ |
---|
1590 | linuxring_start_output, /* start_ouput */ |
---|
1591 | linuxnative_fin_input, /* fin_input */ |
---|
1592 | linuxring_fin_output, /* fin_output */ |
---|
1593 | linuxring_read_packet, /* read_packet */ |
---|
1594 | linuxring_prepare_packet, /* prepare_packet */ |
---|
1595 | NULL, /* fin_packet */ |
---|
1596 | linuxring_write_packet, /* write_packet */ |
---|
1597 | linuxring_get_link_type, /* get_link_type */ |
---|
1598 | linuxring_get_direction, /* get_direction */ |
---|
1599 | linuxring_set_direction, /* set_direction */ |
---|
1600 | NULL, /* get_erf_timestamp */ |
---|
1601 | linuxring_get_timeval, /* get_timeval */ |
---|
1602 | linuxring_get_timespec, /* get_timespec */ |
---|
1603 | NULL, /* get_seconds */ |
---|
1604 | NULL, /* seek_erf */ |
---|
1605 | NULL, /* seek_timeval */ |
---|
1606 | NULL, /* seek_seconds */ |
---|
1607 | linuxring_get_capture_length, /* get_capture_length */ |
---|
1608 | linuxring_get_wire_length, /* get_wire_length */ |
---|
1609 | linuxring_get_framing_length, /* get_framing_length */ |
---|
1610 | linuxring_set_capture_length, /* set_capture_length */ |
---|
1611 | NULL, /* get_received_packets */ |
---|
1612 | linuxnative_get_filtered_packets,/* get_filtered_packets */ |
---|
1613 | linuxnative_get_dropped_packets,/* get_dropped_packets */ |
---|
1614 | linuxnative_get_captured_packets,/* get_captured_packets */ |
---|
1615 | linuxnative_get_fd, /* get_fd */ |
---|
1616 | linuxring_event, /* trace_event */ |
---|
1617 | linuxring_help, /* help */ |
---|
1618 | NULL |
---|
1619 | }; |
---|
1620 | #else |
---|
1621 | static void linuxnative_help(void) { |
---|
1622 | printf("linuxnative format module: $Revision: 1793 $\n"); |
---|
1623 | printf("Not supported on this host\n"); |
---|
1624 | } |
---|
1625 | static void linuxring_help(void) { |
---|
1626 | printf("linuxring format module: $Revision: 1793 $\n"); |
---|
1627 | printf("Not supported on this host\n"); |
---|
1628 | } |
---|
1629 | |
---|
1630 | static struct libtrace_format_t linuxnative = { |
---|
1631 | "int", |
---|
1632 | "$Id$", |
---|
1633 | TRACE_FORMAT_LINUX_NATIVE, |
---|
1634 | NULL, /* probe filename */ |
---|
1635 | NULL, /* probe magic */ |
---|
1636 | NULL, /* init_input */ |
---|
1637 | NULL, /* config_input */ |
---|
1638 | NULL, /* start_input */ |
---|
1639 | NULL, /* pause_input */ |
---|
1640 | NULL, /* init_output */ |
---|
1641 | NULL, /* config_output */ |
---|
1642 | NULL, /* start_ouput */ |
---|
1643 | NULL, /* fin_input */ |
---|
1644 | NULL, /* fin_output */ |
---|
1645 | NULL, /* read_packet */ |
---|
1646 | linuxnative_prepare_packet, /* prepare_packet */ |
---|
1647 | NULL, /* fin_packet */ |
---|
1648 | NULL, /* write_packet */ |
---|
1649 | linuxnative_get_link_type, /* get_link_type */ |
---|
1650 | linuxnative_get_direction, /* get_direction */ |
---|
1651 | linuxnative_set_direction, /* set_direction */ |
---|
1652 | NULL, /* get_erf_timestamp */ |
---|
1653 | linuxnative_get_timeval, /* get_timeval */ |
---|
1654 | linuxnative_get_timespec, /* get_timespec */ |
---|
1655 | NULL, /* get_seconds */ |
---|
1656 | NULL, /* seek_erf */ |
---|
1657 | NULL, /* seek_timeval */ |
---|
1658 | NULL, /* seek_seconds */ |
---|
1659 | linuxnative_get_capture_length, /* get_capture_length */ |
---|
1660 | linuxnative_get_wire_length, /* get_wire_length */ |
---|
1661 | linuxnative_get_framing_length, /* get_framing_length */ |
---|
1662 | linuxnative_set_capture_length, /* set_capture_length */ |
---|
1663 | NULL, /* get_received_packets */ |
---|
1664 | linuxnative_get_filtered_packets,/* get_filtered_packets */ |
---|
1665 | linuxnative_get_dropped_packets,/* get_dropped_packets */ |
---|
1666 | linuxnative_get_captured_packets,/* get_captured_packets */ |
---|
1667 | linuxnative_get_fd, /* get_fd */ |
---|
1668 | trace_event_device, /* trace_event */ |
---|
1669 | linuxnative_help, /* help */ |
---|
1670 | NULL |
---|
1671 | }; |
---|
1672 | |
---|
1673 | static struct libtrace_format_t linuxring = { |
---|
1674 | "ring", |
---|
1675 | "$Id$", |
---|
1676 | TRACE_FORMAT_LINUX_RING, |
---|
1677 | NULL, /* probe filename */ |
---|
1678 | NULL, /* probe magic */ |
---|
1679 | NULL, /* init_input */ |
---|
1680 | NULL, /* config_input */ |
---|
1681 | NULL, /* start_input */ |
---|
1682 | NULL, /* pause_input */ |
---|
1683 | NULL, /* init_output */ |
---|
1684 | NULL, /* config_output */ |
---|
1685 | NULL, /* start_ouput */ |
---|
1686 | NULL, /* fin_input */ |
---|
1687 | NULL, /* fin_output */ |
---|
1688 | NULL, /* read_packet */ |
---|
1689 | linuxring_prepare_packet, /* prepare_packet */ |
---|
1690 | NULL, /* fin_packet */ |
---|
1691 | NULL, /* write_packet */ |
---|
1692 | linuxring_get_link_type, /* get_link_type */ |
---|
1693 | linuxring_get_direction, /* get_direction */ |
---|
1694 | linuxring_set_direction, /* set_direction */ |
---|
1695 | NULL, /* get_erf_timestamp */ |
---|
1696 | linuxring_get_timeval, /* get_timeval */ |
---|
1697 | linuxring_get_timespec, /* get_timespec */ |
---|
1698 | NULL, /* get_seconds */ |
---|
1699 | NULL, /* seek_erf */ |
---|
1700 | NULL, /* seek_timeval */ |
---|
1701 | NULL, /* seek_seconds */ |
---|
1702 | linuxring_get_capture_length, /* get_capture_length */ |
---|
1703 | linuxring_get_wire_length, /* get_wire_length */ |
---|
1704 | linuxring_get_framing_length, /* get_framing_length */ |
---|
1705 | linuxring_set_capture_length, /* set_capture_length */ |
---|
1706 | NULL, /* get_received_packets */ |
---|
1707 | linuxnative_get_filtered_packets,/* get_filtered_packets */ |
---|
1708 | linuxnative_get_dropped_packets,/* get_dropped_packets */ |
---|
1709 | linuxnative_get_captured_packets,/* get_captured_packets */ |
---|
1710 | linuxnative_get_fd, /* get_fd */ |
---|
1711 | NULL, /* trace_event */ |
---|
1712 | linuxring_help, /* help */ |
---|
1713 | NULL |
---|
1714 | }; |
---|
1715 | |
---|
1716 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
1717 | |
---|
1718 | |
---|
1719 | void linuxnative_constructor(void) { |
---|
1720 | /* TODO: once we're happy with ring:, it would be a good idea to |
---|
1721 | * swap the order of these calls so that ring: is preferred over |
---|
1722 | * int: if the user just gives an interface name as an input without |
---|
1723 | * explicitly choosing a format. |
---|
1724 | */ |
---|
1725 | register_format(&linuxnative); |
---|
1726 | register_format(&linuxring); |
---|
1727 | } |
---|