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 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
19 static const char rcsid
[] _U_
=
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.31.2.3 2007-11-09 00:57:01 guy Exp $ (LBL)";
27 #include <sys/param.h> /* optionally get BSD define */
36 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
42 struct mbuf
; /* Squelch compiler warnings on some platforms for */
43 struct rtentry
; /* declarations in <net/if.h> */
51 #define ATM_CELL_SIZE 52
52 #define ATM_HDR_SIZE 4
55 * A header containing additional MTP information.
57 #define MTP2_SENT_OFFSET 0 /* 1 byte */
58 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
59 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
60 #define MTP2_HDR_LEN 4 /* length of the header */
62 #define MTP2_ANNEX_A_NOT_USED 0
63 #define MTP2_ANNEX_A_USED 1
64 #define MTP2_ANNEX_A_USED_UNKNOWN 2
66 /* SunATM pseudo header */
68 unsigned char flags
; /* destination and traffic type */
69 unsigned char vpi
; /* VPI */
70 unsigned short vci
; /* VCI */
73 typedef struct pcap_dag_node
{
74 struct pcap_dag_node
*next
;
79 static pcap_dag_node_t
*pcap_dags
= NULL
;
80 static int atexit_handler_installed
= 0;
81 static const unsigned short endian_test_word
= 0x0100;
83 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
87 /* This code is required when compiling for a DAG device only. */
89 /* Replace dag function names with pcap equivalent. */
90 #define dag_open_live pcap_open_live
91 #define dag_platform_finddevs pcap_platform_finddevs
94 #define MAX_DAG_PACKET 65536
96 static unsigned char TempPkt
[MAX_DAG_PACKET
];
98 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
99 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
100 static int dag_set_datalink(pcap_t
*p
, int dlt
);
101 static int dag_get_datalink(pcap_t
*p
);
102 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
105 delete_pcap_dag(pcap_t
*p
)
107 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
109 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
113 if (curr
!= NULL
&& curr
->p
== p
) {
115 prev
->next
= curr
->next
;
117 pcap_dags
= curr
->next
;
123 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
124 * in the pcap_t structure, and closes the file descriptor for the DAG card.
128 dag_platform_close(pcap_t
*p
)
132 #ifdef HAVE_DAG_STREAMS_API
133 if(dag_stop_stream(p
->fd
, p
->md
.dag_stream
) < 0)
134 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
136 if(dag_detach_stream(p
->fd
, p
->md
.dag_stream
) < 0)
137 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
139 if(dag_stop(p
->fd
) < 0)
140 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
141 #endif /* HAVE_DAG_STREAMS_API */
142 if(dag_close(p
->fd
) < 0)
143 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
146 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
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
);
162 new_pcap_dag(pcap_t
*p
)
164 pcap_dag_node_t
*node
= NULL
;
166 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
170 if (!atexit_handler_installed
) {
171 atexit(atexit_handler
);
172 atexit_handler_installed
= 1;
175 node
->next
= pcap_dags
;
177 node
->pid
= getpid();
185 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
187 uint32_t hdr_num
= 0;
190 /* basic sanity checks */
196 /* check if we have any extension headers */
197 if ( (erf
[8] & 0x80) == 0x00 )
200 /* loop over the extension headers */
203 /* sanity check we have enough bytes */
204 if ( len
<= (24 + (hdr_num
* 8)) )
207 /* get the header type */
208 hdr_type
= erf
[(16 + (hdr_num
* 8))];
211 } while ( hdr_type
& 0x80 );
217 * Read at most max_packets from the capture stream and call the callback
218 * for each of them. Returns the number of packets handled, -1 if an
219 * error occured, or -2 if we were told to break out of the loop.
222 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
224 unsigned int processed
= 0;
225 int flags
= p
->md
.dag_offset_flags
;
226 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
227 unsigned int num_ext_hdr
= 0;
229 /* Get the next bufferful of packets (if necessary). */
230 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) {
233 * Has "pcap_breakloop()" been called?
237 * Yes - clear the flag that indicates that
238 * it has, and return -2 to indicate that
239 * we were told to break out of the loop.
245 #ifdef HAVE_DAG_STREAMS_API
246 /* dag_advance_stream() will block (unless nonblock is called)
247 * until 64kB of data has accumulated.
248 * If to_ms is set, it will timeout before 64kB has accumulated.
249 * We wait for 64kB because processing a few packets at a time
250 * can cause problems at high packet rates (>200kpps) due
252 * This does mean if to_ms is not specified the capture may 'hang'
253 * for long periods if the data rate is extremely slow (<64kB/sec)
254 * If non-block is specified it will return immediately. The user
255 * is then responsible for efficiency.
257 if ( NULL
== (p
->md
.dag_mem_top
= dag_advance_stream(p
->fd
, p
->md
.dag_stream
, &(p
->md
.dag_mem_bottom
))) ) {
261 /* dag_offset does not support timeouts */
262 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), flags
);
263 #endif /* HAVE_DAG_STREAMS_API */
265 if (nonblocking
&& (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
267 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
273 (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
275 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
281 /* Process the packets. */
282 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
>= dag_record_size
) {
284 unsigned short packet_len
= 0;
286 struct pcap_pkthdr pcap_header
;
288 #ifdef HAVE_DAG_STREAMS_API
289 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_bottom
);
291 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
292 #endif /* HAVE_DAG_STREAMS_API */
294 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
298 * Has "pcap_breakloop()" been called?
302 * Yes - clear the flag that indicates that
303 * it has, and return -2 to indicate that
304 * we were told to break out of the loop.
310 rlen
= ntohs(header
->rlen
);
311 if (rlen
< dag_record_size
)
313 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
316 p
->md
.dag_mem_bottom
+= rlen
;
318 /* Count lost packets. */
319 switch((header
->type
& 0x7f)) {
320 /* in these types the color value overwrites the lctr */
321 case TYPE_COLOR_HDLC_POS
:
323 case TYPE_DSM_COLOR_HDLC_POS
:
324 case TYPE_DSM_COLOR_ETH
:
325 case TYPE_COLOR_MC_HDLC_POS
:
326 case TYPE_COLOR_HASH_ETH
:
327 case TYPE_COLOR_HASH_POS
:
332 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
333 p
->md
.stat
.ps_drop
= UINT_MAX
;
335 p
->md
.stat
.ps_drop
+= ntohs(header
->lctr
);
340 if ((header
->type
& 0x7f) == TYPE_PAD
) {
344 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
346 /* ERF encapsulation */
347 /* The Extensible Record Format is not dropped for this kind of encapsulation,
348 * and will be handled as a pseudo header by the decoding application.
349 * The information carried in the ERF header and in the optional subheader (if present)
350 * could be merged with the libpcap information, to offer a better decoding.
351 * The packet length is
352 * o the length of the packet on the link (header->wlen),
353 * o plus the length of the ERF header (dag_record_size), as the length of the
354 * pseudo header will be adjusted during the decoding,
355 * o plus the length of the optional subheader (if present).
357 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
358 * if the capture length is greater than the packet length.
360 if (p
->linktype
== DLT_ERF
) {
361 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
363 switch ((header
->type
& 0x7f)) {
367 packet_len
+= 4; /* MC header */
370 case TYPE_COLOR_HASH_ETH
:
371 case TYPE_DSM_COLOR_ETH
:
374 packet_len
+= 2; /* ETH header */
378 /* Include ERF extension headers */
379 packet_len
+= (8 * num_ext_hdr
);
381 if (caplen
> packet_len
) {
385 /* Other kind of encapsulation according to the header Type */
387 /* Skip over generic ERF header */
388 dp
+= dag_record_size
;
389 /* Skip over extension headers */
390 dp
+= 8 * num_ext_hdr
;
392 switch((header
->type
& 0x7f)) {
395 if (header
->type
== TYPE_AAL5
) {
396 packet_len
= ntohs(header
->wlen
);
397 caplen
= rlen
- dag_record_size
;
400 if (header
->type
== TYPE_MC_ATM
) {
401 caplen
= packet_len
= ATM_CELL_SIZE
;
405 if (header
->type
== TYPE_MC_AAL5
) {
406 packet_len
= ntohs(header
->wlen
);
407 caplen
= rlen
- dag_record_size
- 4;
410 if (header
->type
== TYPE_ATM
) {
411 caplen
= packet_len
= ATM_CELL_SIZE
;
413 if (p
->linktype
== DLT_SUNATM
) {
414 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
415 unsigned long rawatm
;
417 rawatm
= ntohl(*((unsigned long *)dp
));
418 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
419 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
420 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
421 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
422 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
423 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
424 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
425 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
428 packet_len
-= ATM_HDR_SIZE
;
429 caplen
-= ATM_HDR_SIZE
;
434 case TYPE_COLOR_HASH_ETH
:
435 case TYPE_DSM_COLOR_ETH
:
438 packet_len
= ntohs(header
->wlen
);
439 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
440 caplen
= rlen
- dag_record_size
- 2;
441 if (caplen
> packet_len
) {
447 case TYPE_COLOR_HASH_POS
:
448 case TYPE_DSM_COLOR_HDLC_POS
:
449 case TYPE_COLOR_HDLC_POS
:
451 packet_len
= ntohs(header
->wlen
);
452 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
453 caplen
= rlen
- dag_record_size
;
454 if (caplen
> packet_len
) {
459 case TYPE_COLOR_MC_HDLC_POS
:
461 packet_len
= ntohs(header
->wlen
);
462 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
463 caplen
= rlen
- dag_record_size
- 4;
464 if (caplen
> packet_len
) {
467 /* jump the MC_HDLC_HEADER */
469 #ifdef DLT_MTP2_WITH_PHDR
470 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
471 /* Add the MTP2 Pseudo Header */
472 caplen
+= MTP2_HDR_LEN
;
473 packet_len
+= MTP2_HDR_LEN
;
475 TempPkt
[MTP2_SENT_OFFSET
] = 0;
476 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
477 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
478 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
479 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
486 packet_len
= ntohs(header
->wlen
);
487 caplen
= rlen
- dag_record_size
;
488 if (caplen
> packet_len
) {
494 /* Unhandled ERF type.
495 * Ignore rather than generating error
500 /* Skip over extension headers */
501 caplen
-= (8 * num_ext_hdr
);
503 } /* ERF encapsulation */
505 if (caplen
> p
->snapshot
)
506 caplen
= p
->snapshot
;
508 /* Run the packet filter if there is one. */
509 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
511 /* convert between timestamp formats */
512 register unsigned long long ts
;
514 if (IS_BIGENDIAN()) {
515 ts
= SWAPLL(header
->ts
);
520 pcap_header
.ts
.tv_sec
= ts
>> 32;
521 ts
= (ts
& 0xffffffffULL
) * 1000000;
522 ts
+= 0x80000000; /* rounding */
523 pcap_header
.ts
.tv_usec
= ts
>> 32;
524 if (pcap_header
.ts
.tv_usec
>= 1000000) {
525 pcap_header
.ts
.tv_usec
-= 1000000;
526 pcap_header
.ts
.tv_sec
++;
529 /* Fill in our own header data */
530 pcap_header
.caplen
= caplen
;
531 pcap_header
.len
= packet_len
;
533 /* Count the packet. */
534 p
->md
.stat
.ps_recv
++;
536 /* Call the user supplied callback function */
537 callback(user
, &pcap_header
, dp
);
539 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
541 if (processed
== cnt
)
543 /* Reached the user-specified limit. */
553 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
555 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
561 * Get a handle for a live capture from the given DAG device. Passing a NULL
562 * device will result in a failure. The promisc flag is ignored because DAG
563 * cards are always promiscuous. The to_ms parameter is also ignored as it is
564 * not supported in hardware.
566 * snaplen is now also ignored, until we get per-stream slen support. Set
567 * slen with approprite DAG tool BEFORE pcap_open_live().
572 dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
575 char conf
[30]; /* dag configure string */
581 char * newDev
= NULL
;
582 #ifdef HAVE_DAG_STREAMS_API
584 struct timeval maxwait
;
588 if (device
== NULL
) {
589 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
592 /* Allocate a handle for this session. */
594 handle
= malloc(sizeof(*handle
));
595 if (handle
== NULL
) {
596 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
600 /* Initialize some components of the pcap structure. */
602 memset(handle
, 0, sizeof(*handle
));
604 #ifdef HAVE_DAG_STREAMS_API
605 newDev
= (char *)malloc(strlen(device
) + 16);
606 if (newDev
== NULL
) {
607 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
611 /* Parse input name to get dag device and stream number if provided */
612 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handle
->md
.dag_stream
) < 0) {
613 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
618 if (handle
->md
.dag_stream
%2) {
619 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
623 if (strncmp(device
, "/dev/", 5) != 0) {
624 newDev
= (char *)malloc(strlen(device
) + 5);
625 if (newDev
== NULL
) {
626 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
629 strcpy(newDev
, "/dev/");
630 strcat(newDev
, device
);
633 #endif /* HAVE_DAG_STREAMS_API */
635 /* setup device parameters */
636 if((handle
->fd
= dag_open((char *)device
)) < 0) {
637 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
641 #ifdef HAVE_DAG_STREAMS_API
642 /* Open requested stream. Can fail if already locked or on error */
643 if (dag_attach_stream(handle
->fd
, handle
->md
.dag_stream
, 0, 0) < 0) {
644 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
648 /* Set up default poll parameters for stream
649 * Can be overridden by pcap_set_nonblock()
651 if (dag_get_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
652 &mindata
, &maxwait
, &poll
) < 0) {
653 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
657 /* Amount of data to collect in Bytes before calling callbacks.
658 * Important for efficiency, but can introduce latency
659 * at low packet rates if to_ms not set!
663 /* Obey to_ms if supplied. This is a good idea!
664 * Recommend 10-100ms. Calls will time out even if no data arrived.
666 maxwait
.tv_sec
= to_ms
/1000;
667 maxwait
.tv_usec
= (to_ms
%1000) * 1000;
669 if (dag_set_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
670 mindata
, &maxwait
, &poll
) < 0) {
671 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
676 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
677 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
681 #endif /* HAVE_DAG_STREAMS_API */
683 /* XXX Not calling dag_configure() to set slen; this is unsafe in
684 * multi-stream environments as the gpp config is global.
685 * Once the firmware provides 'per-stream slen' this can be supported
686 * again via the Config API without side-effects */
688 /* set the card snap length to the specified snaplen parameter */
689 /* This is a really bad idea, as different cards have different
690 * valid slen ranges. Should fix in Config API. */
691 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
692 snaplen
= MAX_DAG_SNAPLEN
;
693 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
694 snaplen
= MIN_DAG_SNAPLEN
;
696 /* snap len has to be a multiple of 4 */
697 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
699 if(dag_configure(handle
->fd
, conf
) < 0) {
700 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
705 #ifdef HAVE_DAG_STREAMS_API
706 if(dag_start_stream(handle
->fd
, handle
->md
.dag_stream
) < 0) {
707 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
711 if(dag_start(handle
->fd
) < 0) {
712 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
715 #endif /* HAVE_DAG_STREAMS_API */
718 * Important! You have to ensure bottom is properly
719 * initialized to zero on startup, it won't give you
720 * a compiler warning if you make this mistake!
722 handle
->md
.dag_mem_bottom
= 0;
723 handle
->md
.dag_mem_top
= 0;
726 * Find out how many FCS bits we should strip.
727 * First, query the card to see if it strips the FCS.
729 daginf
= dag_info(handle
->fd
);
730 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
731 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
732 handle
->md
.dag_fcs_bits
= 0;
734 /* Note that no FCS will be supplied. */
735 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
738 * Start out assuming it's 32 bits.
740 handle
->md
.dag_fcs_bits
= 32;
742 /* Allow an environment variable to override. */
743 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
744 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
745 handle
->md
.dag_fcs_bits
= n
;
747 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
748 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
754 * Did the user request that they not be stripped?
756 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
757 /* Yes. Note the number of bytes that will be
759 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handle
->md
.dag_fcs_bits
/16);
761 /* And don't strip them. */
762 handle
->md
.dag_fcs_bits
= 0;
766 handle
->snapshot
= snaplen
;
767 handle
->md
.dag_timeout
= to_ms
;
769 handle
->linktype
= -1;
770 if (dag_get_datalink(handle
) < 0) {
771 strcpy(ebuf
, handle
->errbuf
);
777 if (new_pcap_dag(handle
) < 0) {
778 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
783 * "select()" and "poll()" don't work on DAG device descriptors.
785 handle
->selectable_fd
= -1;
787 if (newDev
!= NULL
) {
788 free((char *)newDev
);
791 handle
->read_op
= dag_read
;
792 handle
->inject_op
= dag_inject
;
793 handle
->setfilter_op
= dag_setfilter
;
794 handle
->setdirection_op
= NULL
; /* Not implemented.*/
795 handle
->set_datalink_op
= dag_set_datalink
;
796 handle
->getnonblock_op
= pcap_getnonblock_fd
;
797 handle
->setnonblock_op
= dag_setnonblock
;
798 handle
->stats_op
= dag_stats
;
799 handle
->close_op
= dag_platform_close
;
800 handle
->md
.stat
.ps_drop
= 0;
801 handle
->md
.stat
.ps_recv
= 0;
804 #ifdef HAVE_DAG_STREAMS_API
806 if (handle
!= NULL
) {
807 if (dag_stop_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
808 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
812 if (handle
!= NULL
) {
813 if (dag_detach_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
814 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
818 if (handle
!= NULL
) {
819 if (dag_stop(p
->fd
) < 0)
820 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
822 #endif /* HAVE_DAG_STREAMS_API */
825 if (handle
!= NULL
) {
826 if (dag_close(handle
->fd
) < 0)
827 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
830 delete_pcap_dag(handle
);
833 if (newDev
!= NULL
) {
834 free((char *)newDev
);
836 if (handle
!= NULL
) {
838 * Get rid of any link-layer type list we allocated.
840 if (handle
->dlt_list
!= NULL
) {
841 free(handle
->dlt_list
);
850 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
851 /* This needs to be filled out correctly. Hopefully a dagapi call will
852 provide all necessary information.
854 /*p->md.stat.ps_recv = 0;*/
855 /*p->md.stat.ps_drop = 0;*/
863 * Previously we just generated a list of all possible names and let
864 * pcap_add_if() attempt to open each one, but with streams this adds up
865 * to 81 possibilities which is inefficient.
867 * Since we know more about the devices we can prune the tree here.
868 * pcap_add_if() will still retest each device but the total number of
869 * open attempts will still be much less than the naive approach.
872 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
874 char name
[12]; /* XXX - pick a size */
877 char dagname
[DAGNAME_BUFSIZE
];
881 /* Try all the DAGs 0-9 */
882 for (c
= 0; c
< 9; c
++) {
883 snprintf(name
, 12, "dag%d", c
);
884 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
888 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
889 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
895 #ifdef HAVE_DAG_STREAMS_API
897 int stream
, rxstreams
;
898 rxstreams
= dag_rx_get_stream_count(dagfd
);
899 for(stream
=0;stream
<16;stream
+=2) {
900 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
901 dag_detach_stream(dagfd
, stream
);
903 snprintf(name
, 10, "dag%d:%d", c
, stream
);
904 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
913 #endif /* HAVE_DAG_STREAMS_API */
922 * Installs the given bpf filter program in the given pcap structure. There is
923 * no attempt to store the filter in kernel memory as that is not supported
927 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
932 strncpy(p
->errbuf
, "setfilter: No filter specified",
937 /* Make our private copy of the filter */
939 if (install_bpf_program(p
, fp
) < 0)
948 dag_set_datalink(pcap_t
*p
, int dlt
)
956 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
959 * Set non-blocking mode on the FD.
960 * XXX - is that necessary? If not, don't bother calling it,
961 * and have a "dag_getnonblock()" function that looks at
962 * "p->md.dag_offset_flags".
964 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
966 #ifdef HAVE_DAG_STREAMS_API
969 struct timeval maxwait
;
972 if (dag_get_stream_poll(p
->fd
, p
->md
.dag_stream
,
973 &mindata
, &maxwait
, &poll
) < 0) {
974 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
978 /* Amount of data to collect in Bytes before calling callbacks.
979 * Important for efficiency, but can introduce latency
980 * at low packet rates if to_ms not set!
987 if (dag_set_stream_poll(p
->fd
, p
->md
.dag_stream
,
988 mindata
, &maxwait
, &poll
) < 0) {
989 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
993 #endif /* HAVE_DAG_STREAMS_API */
995 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
997 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
1003 dag_get_datalink(pcap_t
*p
)
1005 int index
=0, dlt_index
=0;
1008 memset(types
, 0, 255);
1010 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1011 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1017 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1018 /* Get list of possible ERF types for this card */
1019 if (dag_get_stream_erf_types(p
->fd
, p
->md
.dag_stream
, types
, 255) < 0) {
1020 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1024 while (types
[index
]) {
1026 #elif defined HAVE_DAG_GET_ERF_TYPES
1027 /* Get list of possible ERF types for this card */
1028 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1029 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1033 while (types
[index
]) {
1035 /* Check the type through a dagapi call. */
1036 types
[index
] = dag_linktype(p
->fd
);
1040 switch((types
[index
] & 0x7f)) {
1043 case TYPE_COLOR_HDLC_POS
:
1044 case TYPE_DSM_COLOR_HDLC_POS
:
1045 case TYPE_COLOR_HASH_POS
:
1047 if (p
->dlt_list
!= NULL
) {
1048 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1049 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1050 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1053 p
->linktype
= DLT_CHDLC
;
1057 case TYPE_COLOR_ETH
:
1058 case TYPE_DSM_COLOR_ETH
:
1059 case TYPE_COLOR_HASH_ETH
:
1061 * This is (presumably) a real Ethernet capture; give it a
1062 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1063 * that an application can let you choose it, in case you're
1064 * capturing DOCSIS traffic that a Cisco Cable Modem
1065 * Termination System is putting out onto an Ethernet (it
1066 * doesn't put an Ethernet header onto the wire, it puts raw
1067 * DOCSIS frames out on the wire inside the low-level
1068 * Ethernet framing).
1070 if (p
->dlt_list
!= NULL
) {
1071 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1072 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1075 p
->linktype
= DLT_EN10MB
;
1082 if (p
->dlt_list
!= NULL
) {
1083 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1084 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1087 p
->linktype
= DLT_ATM_RFC1483
;
1090 case TYPE_COLOR_MC_HDLC_POS
:
1092 if (p
->dlt_list
!= NULL
) {
1093 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1094 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1095 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1096 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1097 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1098 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1101 p
->linktype
= DLT_CHDLC
;
1106 p
->linktype
= DLT_RAW
;
1111 case TYPE_MC_RAW_CHANNEL
:
1112 case TYPE_IP_COUNTER
:
1113 case TYPE_TCP_FLOW_COUNTER
:
1114 case TYPE_INFINIBAND
:
1117 /* Libpcap cannot deal with these types yet */
1118 /* Add no DLTs, but still covered by DLT_ERF */
1125 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1127 p
->dlt_count
= dlt_index
;
1130 p
->linktype
= DLT_ERF
;