]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
2 * pcap-dag.c: Packet capture interface for Endace DAG card.
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
7 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
8 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
9 * is not defined then nothing is altered - the dag_ functions will be
10 * called as required from their pcap-linux/bpf equivalents.
12 * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
15 * 2003 May - Jesper Peterson <support@endace.com>
16 * Code shuffled around to suit fad-xxx.c structure
17 * Added atexit() handler to stop DAG if application is too lazy
18 * 2003 September - Koryn Grant <koryn@endace.com>
19 * Added support for nonblocking operation.
20 * Added support for processing more than a single packet in pcap_dispatch().
21 * Fixed bug in loss counter code.
22 * Improved portability of loss counter code (e.g. use UINT_MAX instead of 0xffff).
23 * Removed unused local variables.
24 * Added required headers (ctype.h, limits.h, unistd.h, netinet/in.h).
25 * 2003 October - Koryn Grant <koryn@endace.com.>
26 * Changed semantics to match those of standard pcap on linux.
27 * - packets rejected by the filter are not counted.
31 static const char rcsid
[] _U_
=
32 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.12 2003-11-20 01:21:26 guy Exp $ (LBL)";
39 #include <sys/param.h> /* optionally get BSD define */
48 #include <netinet/in.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
54 struct mbuf
; /* Squelch compiler warnings on some platforms for */
55 struct rtentry
; /* declarations in <net/if.h> */
61 #define MIN_DAG_SNAPLEN 12
62 #define MAX_DAG_SNAPLEN 2040
63 #define ATM_SNAPLEN 48
65 typedef struct pcap_dag_node
{
66 struct pcap_dag_node
*next
;
71 static pcap_dag_node_t
*pcap_dags
= NULL
;
72 static int atexit_handler_installed
= 0;
73 static const unsigned short endian_test_word
= 0x0100;
75 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
78 * Swap byte ordering of unsigned long long timestamp on a big endian
81 #define SWAP_TS(ull) ((ull & 0xff00000000000000LL) >> 56) | \
82 ((ull & 0x00ff000000000000LL) >> 40) | \
83 ((ull & 0x0000ff0000000000LL) >> 24) | \
84 ((ull & 0x000000ff00000000LL) >> 8) | \
85 ((ull & 0x00000000ff000000LL) << 8) | \
86 ((ull & 0x0000000000ff0000LL) << 24) | \
87 ((ull & 0x000000000000ff00LL) << 40) | \
88 ((ull & 0x00000000000000ffLL) << 56)
92 /* This code is required when compiling for a DAG device only. */
95 /* Replace dag function names with pcap equivalent. */
96 #define dag_open_live pcap_open_live
97 #define dag_platform_finddevs pcap_platform_finddevs
100 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
101 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
102 static int dag_set_datalink(pcap_t
*p
, int dlt
);
103 static int dag_get_datalink(pcap_t
*p
);
105 static void delete_pcap_dag(pcap_t
*p
) {
106 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
108 for (prev
= NULL
, curr
= pcap_dags
;
109 curr
!= NULL
&& curr
->p
!= p
;
110 prev
= curr
, curr
= curr
->next
) {
114 if (curr
!= NULL
&& curr
->p
== p
) {
116 prev
->next
= curr
->next
;
118 pcap_dags
= curr
->next
;
124 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
125 * in the pcap_t structure, and closes the file descriptor for the DAG card.
128 static void dag_platform_close(pcap_t
*p
) {
131 if (p
!= NULL
&& p
->md
.device
!= NULL
) {
132 if(dag_stop(p
->fd
) < 0)
133 fprintf(stderr
,"dag_stop %s: %s\n", p
->md
.device
, strerror(errno
));
134 if(dag_close(p
->fd
) < 0)
135 fprintf(stderr
,"dag_close %s: %s\n", p
->md
.device
, strerror(errno
));
141 if(dag_stop(p
->fd
) < 0)
142 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
143 if(dag_close(p
->fd
) < 0)
144 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
148 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
151 static void atexit_handler(void) {
152 while (pcap_dags
!= NULL
) {
153 if (pcap_dags
->pid
== getpid()) {
154 dag_platform_close(pcap_dags
->p
);
156 delete_pcap_dag(pcap_dags
->p
);
161 static int new_pcap_dag(pcap_t
*p
) {
162 pcap_dag_node_t
*node
= NULL
;
164 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
168 if (!atexit_handler_installed
) {
169 atexit(atexit_handler
);
170 atexit_handler_installed
= 1;
173 node
->next
= pcap_dags
;
175 node
->pid
= getpid();
183 * Read at most max_packets from the capture stream and call the callback
184 * for each of them. Returns the number of packets handled, -1 if an
185 * error occured, or -2 if we were told to break out of the loop.
187 static int dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
) {
188 unsigned int processed
= 0;
189 int flags
= p
->md
.dag_offset_flags
;
190 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
194 /* Get the next bufferful of packets (if necessary). */
195 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) {
198 * Has "pcap_breakloop()" been called?
202 * Yes - clear the flag that indicates that
203 * it has, and return -2 to indicate that
204 * we were told to break out of the loop.
210 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), flags
);
211 if ((p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) && nonblocking
)
213 /* Pcap is configured to process only available packets, and there aren't any. */
218 /* Process the packets. */
219 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
>= dag_record_size
) {
221 unsigned short packet_len
= 0;
223 struct pcap_pkthdr pcap_header
;
225 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
226 u_char
*dp
= ((u_char
*)header
) + dag_record_size
;
230 * Has "pcap_breakloop()" been called?
234 * Yes - clear the flag that indicates that
235 * it has, and return -2 to indicate that
236 * we were told to break out of the loop.
248 rlen
= ntohs(header
->rlen
);
250 p
->md
.dag_mem_bottom
+= rlen
;
252 switch(header
->type
) {
254 packet_len
= ATM_SNAPLEN
;
255 caplen
= ATM_SNAPLEN
;
262 packet_len
= header
->wlen
;
266 packet_len
= ntohs(header
->wlen
);
268 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
269 caplen
= rlen
- dag_record_size
- 2;
270 if (caplen
> packet_len
)
280 packet_len
= header
->wlen
;
284 packet_len
= ntohs(header
->wlen
);
286 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
287 caplen
= rlen
- dag_record_size
;
288 if (caplen
> packet_len
)
295 if (caplen
> p
->snapshot
)
296 caplen
= p
->snapshot
;
298 /* Count lost packets. */
300 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- header
->lctr
)) {
301 p
->md
.stat
.ps_drop
= UINT_MAX
;
303 p
->md
.stat
.ps_drop
+= header
->lctr
;
307 /* Run the packet filter if there is one. */
308 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
310 /* convert between timestamp formats */
311 register unsigned long long ts
;
315 ts
= SWAP_TS(header
->ts
);
322 pcap_header
.ts
.tv_sec
= ts
>> 32;
323 ts
= (ts
& 0xffffffffULL
) * 1000000;
324 ts
+= 0x80000000; /* rounding */
325 pcap_header
.ts
.tv_usec
= ts
>> 32;
326 if (pcap_header
.ts
.tv_usec
>= 1000000) {
327 pcap_header
.ts
.tv_usec
-= 1000000;
328 pcap_header
.ts
.tv_sec
++;
331 /* Fill in our own header data */
332 pcap_header
.caplen
= caplen
;
333 pcap_header
.len
= packet_len
;
335 /* Count the packet. */
336 p
->md
.stat
.ps_recv
++;
338 /* Call the user supplied callback function */
339 callback(user
, &pcap_header
, dp
);
341 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
343 if (processed
== cnt
)
345 /* Reached the user-specified limit. */
351 if (nonblocking
|| processed
)
361 * Get a handle for a live capture from the given DAG device. Passing a NULL
362 * device will result in a failure. The promisc flag is ignored because DAG
363 * cards are always promiscuous. The to_ms parameter is also ignored as it is
364 * not supported in hardware.
368 pcap_t
*dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
) {
369 char conf
[30]; /* dag configure string */
374 if (device
== NULL
) {
375 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
378 /* Allocate a handle for this session. */
380 handle
= malloc(sizeof(*handle
));
381 if (handle
== NULL
) {
382 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
386 /* Initialize some components of the pcap structure. */
388 memset(handle
, 0, sizeof(*handle
));
390 if (strstr(device
, "/dev") == NULL
) {
391 char * newDev
= (char *)malloc(strlen(device
) + 6);
393 strcat(newDev
, "/dev/");
394 strcat(newDev
,device
);
397 device
= strdup(device
);
400 if (device
== NULL
) {
401 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "str_dup: %s\n", pcap_strerror(errno
));
405 /* setup device parameters */
406 if((handle
->fd
= dag_open((char *)device
)) < 0) {
407 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
411 /* set the card snap length to the specified snaplen parameter */
412 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
413 snaplen
= MAX_DAG_SNAPLEN
;
414 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
415 snaplen
= MIN_DAG_SNAPLEN
;
417 /* snap len has to be a multiple of 4 */
418 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
420 fprintf(stderr
, "Configuring DAG with '%s'.\n", conf
);
421 if(dag_configure(handle
->fd
, conf
) < 0) {
422 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
426 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
427 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
431 if(dag_start(handle
->fd
) < 0) {
432 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
437 * Important! You have to ensure bottom is properly
438 * initialized to zero on startup, it won't give you
439 * a compiler warning if you make this mistake!
441 handle
->md
.dag_mem_bottom
= 0;
442 handle
->md
.dag_mem_top
= 0;
444 /* TODO: query the card */
445 handle
->md
.dag_fcs_bits
= 32;
446 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
447 if ((n
= atoi(s
)) == 0 || n
== 16|| n
== 32) {
448 handle
->md
.dag_fcs_bits
= n
;
450 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
451 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
456 handle
->snapshot
= snaplen
;
457 /*handle->md.timeout = to_ms; */
459 if ((handle
->linktype
= dag_get_datalink(handle
)) < 0) {
460 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_linktype %s: unknown linktype\n", device
);
466 if (new_pcap_dag(handle
) < 0) {
467 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
472 handle
->md
.device
= (char *)device
;
474 free((char *)device
);
478 handle
->read_op
= dag_read
;
479 handle
->setfilter_op
= dag_setfilter
;
480 handle
->set_datalink_op
= dag_set_datalink
;
481 handle
->stats_op
= dag_stats
;
482 handle
->close_op
= dag_platform_close
;
487 if (device
!= NULL
) {
488 free((char *)device
);
490 if (handle
!= NULL
) {
497 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
498 /* This needs to be filled out correctly. Hopefully a dagapi call will
499 provide all necessary information.
501 /*p->md.stat.ps_recv = 0;*/
502 /*p->md.stat.ps_drop = 0;*/
510 * Get from "/proc/dag" all interfaces listed there; if they're
511 * already in the list of interfaces we have, that won't add another
512 * instance, but if they're not, that'll add them.
514 * We don't bother getting any addresses for them.
516 * We also don't fail if we couldn't open "/proc/dag"; we just leave
517 * the list of interfaces as is.
520 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
526 char name
[512]; /* XXX - pick a size */
530 /* Quick exit if /proc/dag not readable */
531 proc_dag_f
= fopen("/proc/dag", "r");
532 if (proc_dag_f
== NULL
)
535 char dev
[16] = "dagx";
537 for (i
= '0'; ret
== 0 && i
<= '9'; i
++) {
539 if (pcap_add_if(devlistp
, dev
, 0, NULL
, errbuf
) == -1) {
551 fgets(linebuf
, sizeof linebuf
, proc_dag_f
) != NULL
; linenum
++) {
554 * Skip the first two lines - they're headers.
561 if (*p
== '\0' || *p
== '\n' || *p
!= 'D')
562 continue; /* not a Dag line */
565 * Get the interface name.
568 while (*p
!= '\0' && *p
!= ':') {
570 *q
++ = tolower(*p
++);
577 * Add an entry for this interface, with no addresses.
579 p
[strlen(p
) - 1] = '\0'; /* get rid of \n */
580 if (pcap_add_if(devlistp
, name
, 0, strdup(p
+ 2), errbuf
) == -1) {
590 * Well, we didn't fail for any other reason; did we
591 * fail due to an error reading the file?
593 if (ferror(proc_dag_f
)) {
594 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
595 "Error reading /proc/dag: %s",
596 pcap_strerror(errno
));
601 (void)fclose(proc_dag_f
);
606 * Installs the given bpf filter program in the given pcap structure. There is
607 * no attempt to store the filter in kernel memory as that is not supported
610 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
) {
614 strncpy(p
->errbuf
, "setfilter: No filter specified",
619 /* Make our private copy of the filter */
621 if (install_bpf_program(p
, fp
) < 0) {
622 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
623 "malloc: %s", pcap_strerror(errno
));
633 dag_set_datalink(pcap_t
*p
, int dlt
)
639 dag_get_datalink(pcap_t
*p
)
643 /* Check the type through a dagapi call.
645 switch(dag_linktype(p
->fd
)) {
646 case TYPE_HDLC_POS
: {
647 dag_record_t
*record
;
649 /* peek at the first available record to see if it is PPP */
650 while ((p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
) < (dag_record_size
+ 4)) {
651 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), 0);
653 record
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
655 if ((ntohl(record
->rec
.pos
.hdlc
) & 0xffff0000) == 0xff030000) {
656 linktype
= DLT_PPP_SERIAL
;
657 fprintf(stderr
, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype
);
659 linktype
= DLT_CHDLC
;
660 fprintf(stderr
, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype
);
665 linktype
= DLT_EN10MB
;
666 fprintf(stderr
, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype
);
669 linktype
= DLT_ATM_RFC1483
;
670 fprintf(stderr
, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype
);
674 fprintf(stderr
, "Set DAG linktype to %d (DLT_NULL)\n", linktype
);
677 fprintf(stderr
, "Unknown DAG linktype %d\n", dag_linktype(p
->fd
));