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.2 2007-11-05 21:45:29 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 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
320 /* ERF encapsulation */
321 /* The Extensible Record Format is not dropped for this kind of encapsulation,
322 * and will be handled as a pseudo header by the decoding application.
323 * The information carried in the ERF header and in the optional subheader (if present)
324 * could be merged with the libpcap information, to offer a better decoding.
325 * The packet length is
326 * o the length of the packet on the link (header->wlen),
327 * o plus the length of the ERF header (dag_record_size), as the length of the
328 * pseudo header will be adjusted during the decoding,
329 * o plus the length of the optional subheader (if present).
331 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
332 * if the capture length is greater than the packet length.
334 if (p
->linktype
== DLT_ERF
) {
335 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
337 switch ((header
->type
& 0x7f)) {
341 packet_len
+= 4; /* MC header */
344 case TYPE_COLOR_HASH_ETH
:
345 case TYPE_DSM_COLOR_ETH
:
348 packet_len
+= 2; /* ETH header */
352 /* Include ERF extension headers */
353 packet_len
+= (8 * num_ext_hdr
);
355 if (caplen
> packet_len
) {
359 /* Other kind of encapsulation according to the header Type */
361 /* Skip over generic ERF header */
362 dp
+= dag_record_size
;
363 /* Skip over extension headers */
364 dp
+= 8 * num_ext_hdr
;
366 switch((header
->type
& 0x7f)) {
369 if (header
->type
== TYPE_AAL5
) {
370 packet_len
= ntohs(header
->wlen
);
371 caplen
= rlen
- dag_record_size
;
374 if (header
->type
== TYPE_MC_ATM
) {
375 caplen
= packet_len
= ATM_CELL_SIZE
;
379 if (header
->type
== TYPE_MC_AAL5
) {
380 packet_len
= ntohs(header
->wlen
);
381 caplen
= rlen
- dag_record_size
- 4;
384 if (header
->type
== TYPE_ATM
) {
385 caplen
= packet_len
= ATM_CELL_SIZE
;
387 if (p
->linktype
== DLT_SUNATM
) {
388 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
389 unsigned long rawatm
;
391 rawatm
= ntohl(*((unsigned long *)dp
));
392 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
393 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
394 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
395 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
396 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
397 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
398 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
399 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
402 packet_len
-= ATM_HDR_SIZE
;
403 caplen
-= ATM_HDR_SIZE
;
408 case TYPE_COLOR_HASH_ETH
:
409 case TYPE_DSM_COLOR_ETH
:
412 packet_len
= ntohs(header
->wlen
);
413 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
414 caplen
= rlen
- dag_record_size
- 2;
415 if (caplen
> packet_len
) {
421 case TYPE_COLOR_HASH_POS
:
422 case TYPE_DSM_COLOR_HDLC_POS
:
423 case TYPE_COLOR_HDLC_POS
:
425 packet_len
= ntohs(header
->wlen
);
426 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
427 caplen
= rlen
- dag_record_size
;
428 if (caplen
> packet_len
) {
433 case TYPE_COLOR_MC_HDLC_POS
:
435 packet_len
= ntohs(header
->wlen
);
436 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
437 caplen
= rlen
- dag_record_size
- 4;
438 if (caplen
> packet_len
) {
441 /* jump the MC_HDLC_HEADER */
443 #ifdef DLT_MTP2_WITH_PHDR
444 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
445 /* Add the MTP2 Pseudo Header */
446 caplen
+= MTP2_HDR_LEN
;
447 packet_len
+= MTP2_HDR_LEN
;
449 TempPkt
[MTP2_SENT_OFFSET
] = 0;
450 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
451 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
452 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
453 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
460 packet_len
= ntohs(header
->wlen
);
461 caplen
= rlen
- dag_record_size
;
462 if (caplen
> packet_len
) {
468 /* Unhandled ERF type.
469 * Ignore rather than generating error
474 /* Skip over extension headers */
475 caplen
-= (8 * num_ext_hdr
);
477 } /* ERF encapsulation */
479 if (caplen
> p
->snapshot
)
480 caplen
= p
->snapshot
;
482 /* Count lost packets. */
483 switch((header
->type
& 0x7f)) {
484 /* in these types the color value overwrites the lctr */
485 case TYPE_COLOR_HDLC_POS
:
487 case TYPE_DSM_COLOR_HDLC_POS
:
488 case TYPE_DSM_COLOR_ETH
:
489 case TYPE_COLOR_MC_HDLC_POS
:
490 case TYPE_COLOR_HASH_ETH
:
491 case TYPE_COLOR_HASH_POS
:
496 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
497 p
->md
.stat
.ps_drop
= UINT_MAX
;
499 p
->md
.stat
.ps_drop
+= ntohs(header
->lctr
);
504 /* Run the packet filter if there is one. */
505 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
507 /* convert between timestamp formats */
508 register unsigned long long ts
;
510 if (IS_BIGENDIAN()) {
511 ts
= SWAPLL(header
->ts
);
516 pcap_header
.ts
.tv_sec
= ts
>> 32;
517 ts
= (ts
& 0xffffffffULL
) * 1000000;
518 ts
+= 0x80000000; /* rounding */
519 pcap_header
.ts
.tv_usec
= ts
>> 32;
520 if (pcap_header
.ts
.tv_usec
>= 1000000) {
521 pcap_header
.ts
.tv_usec
-= 1000000;
522 pcap_header
.ts
.tv_sec
++;
525 /* Fill in our own header data */
526 pcap_header
.caplen
= caplen
;
527 pcap_header
.len
= packet_len
;
529 /* Count the packet. */
530 p
->md
.stat
.ps_recv
++;
532 /* Call the user supplied callback function */
533 callback(user
, &pcap_header
, dp
);
535 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
537 if (processed
== cnt
)
539 /* Reached the user-specified limit. */
549 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
551 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
557 * Get a handle for a live capture from the given DAG device. Passing a NULL
558 * device will result in a failure. The promisc flag is ignored because DAG
559 * cards are always promiscuous. The to_ms parameter is also ignored as it is
560 * not supported in hardware.
562 * snaplen is now also ignored, until we get per-stream slen support. Set
563 * slen with approprite DAG tool BEFORE pcap_open_live().
568 dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
571 char conf
[30]; /* dag configure string */
577 char * newDev
= NULL
;
578 #ifdef HAVE_DAG_STREAMS_API
580 struct timeval maxwait
;
584 if (device
== NULL
) {
585 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
588 /* Allocate a handle for this session. */
590 handle
= malloc(sizeof(*handle
));
591 if (handle
== NULL
) {
592 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
596 /* Initialize some components of the pcap structure. */
598 memset(handle
, 0, sizeof(*handle
));
600 #ifdef HAVE_DAG_STREAMS_API
601 newDev
= (char *)malloc(strlen(device
) + 16);
602 if (newDev
== NULL
) {
603 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
607 /* Parse input name to get dag device and stream number if provided */
608 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handle
->md
.dag_stream
) < 0) {
609 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
614 if (handle
->md
.dag_stream
%2) {
615 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
619 if (strncmp(device
, "/dev/", 5) != 0) {
620 newDev
= (char *)malloc(strlen(device
) + 5);
621 if (newDev
== NULL
) {
622 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
625 strcpy(newDev
, "/dev/");
626 strcat(newDev
, device
);
629 #endif /* HAVE_DAG_STREAMS_API */
631 /* setup device parameters */
632 if((handle
->fd
= dag_open((char *)device
)) < 0) {
633 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
637 #ifdef HAVE_DAG_STREAMS_API
638 /* Open requested stream. Can fail if already locked or on error */
639 if (dag_attach_stream(handle
->fd
, handle
->md
.dag_stream
, 0, 0) < 0) {
640 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
644 /* Set up default poll parameters for stream
645 * Can be overridden by pcap_set_nonblock()
647 if (dag_get_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
648 &mindata
, &maxwait
, &poll
) < 0) {
649 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
653 /* Amount of data to collect in Bytes before calling callbacks.
654 * Important for efficiency, but can introduce latency
655 * at low packet rates if to_ms not set!
659 /* Obey to_ms if supplied. This is a good idea!
660 * Recommend 10-100ms. Calls will time out even if no data arrived.
662 maxwait
.tv_sec
= to_ms
/1000;
663 maxwait
.tv_usec
= (to_ms
%1000) * 1000;
665 if (dag_set_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
666 mindata
, &maxwait
, &poll
) < 0) {
667 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
672 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
673 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
677 #endif /* HAVE_DAG_STREAMS_API */
679 /* XXX Not calling dag_configure() to set slen; this is unsafe in
680 * multi-stream environments as the gpp config is global.
681 * Once the firmware provides 'per-stream slen' this can be supported
682 * again via the Config API without side-effects */
684 /* set the card snap length to the specified snaplen parameter */
685 /* This is a really bad idea, as different cards have different
686 * valid slen ranges. Should fix in Config API. */
687 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
688 snaplen
= MAX_DAG_SNAPLEN
;
689 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
690 snaplen
= MIN_DAG_SNAPLEN
;
692 /* snap len has to be a multiple of 4 */
693 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
695 if(dag_configure(handle
->fd
, conf
) < 0) {
696 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
701 #ifdef HAVE_DAG_STREAMS_API
702 if(dag_start_stream(handle
->fd
, handle
->md
.dag_stream
) < 0) {
703 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
707 if(dag_start(handle
->fd
) < 0) {
708 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
711 #endif /* HAVE_DAG_STREAMS_API */
714 * Important! You have to ensure bottom is properly
715 * initialized to zero on startup, it won't give you
716 * a compiler warning if you make this mistake!
718 handle
->md
.dag_mem_bottom
= 0;
719 handle
->md
.dag_mem_top
= 0;
722 * Find out how many FCS bits we should strip.
723 * First, query the card to see if it strips the FCS.
725 daginf
= dag_info(handle
->fd
);
726 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
727 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
728 handle
->md
.dag_fcs_bits
= 0;
730 /* Note that no FCS will be supplied. */
731 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
734 * Start out assuming it's 32 bits.
736 handle
->md
.dag_fcs_bits
= 32;
738 /* Allow an environment variable to override. */
739 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
740 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
741 handle
->md
.dag_fcs_bits
= n
;
743 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
744 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
750 * Did the user request that they not be stripped?
752 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
753 /* Yes. Note the number of bytes that will be
755 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handle
->md
.dag_fcs_bits
/16);
757 /* And don't strip them. */
758 handle
->md
.dag_fcs_bits
= 0;
762 handle
->snapshot
= snaplen
;
763 handle
->md
.dag_timeout
= to_ms
;
765 handle
->linktype
= -1;
766 if (dag_get_datalink(handle
) < 0) {
767 strcpy(ebuf
, handle
->errbuf
);
773 if (new_pcap_dag(handle
) < 0) {
774 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
779 * "select()" and "poll()" don't work on DAG device descriptors.
781 handle
->selectable_fd
= -1;
783 if (newDev
!= NULL
) {
784 free((char *)newDev
);
787 handle
->read_op
= dag_read
;
788 handle
->inject_op
= dag_inject
;
789 handle
->setfilter_op
= dag_setfilter
;
790 handle
->setdirection_op
= NULL
; /* Not implemented.*/
791 handle
->set_datalink_op
= dag_set_datalink
;
792 handle
->getnonblock_op
= pcap_getnonblock_fd
;
793 handle
->setnonblock_op
= dag_setnonblock
;
794 handle
->stats_op
= dag_stats
;
795 handle
->close_op
= dag_platform_close
;
796 handle
->md
.stat
.ps_drop
= 0;
797 handle
->md
.stat
.ps_recv
= 0;
800 #ifdef HAVE_DAG_STREAMS_API
802 if (handle
!= NULL
) {
803 if (dag_stop_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
804 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
808 if (handle
!= NULL
) {
809 if (dag_detach_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
810 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
814 if (handle
!= NULL
) {
815 if (dag_stop(p
->fd
) < 0)
816 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
818 #endif /* HAVE_DAG_STREAMS_API */
821 if (handle
!= NULL
) {
822 if (dag_close(handle
->fd
) < 0)
823 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
826 delete_pcap_dag(handle
);
829 if (newDev
!= NULL
) {
830 free((char *)newDev
);
832 if (handle
!= NULL
) {
834 * Get rid of any link-layer type list we allocated.
836 if (handle
->dlt_list
!= NULL
) {
837 free(handle
->dlt_list
);
846 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
847 /* This needs to be filled out correctly. Hopefully a dagapi call will
848 provide all necessary information.
850 /*p->md.stat.ps_recv = 0;*/
851 /*p->md.stat.ps_drop = 0;*/
859 * Previously we just generated a list of all possible names and let
860 * pcap_add_if() attempt to open each one, but with streams this adds up
861 * to 81 possibilities which is inefficient.
863 * Since we know more about the devices we can prune the tree here.
864 * pcap_add_if() will still retest each device but the total number of
865 * open attempts will still be much less than the naive approach.
868 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
870 char name
[12]; /* XXX - pick a size */
873 char dagname
[DAGNAME_BUFSIZE
];
877 /* Try all the DAGs 0-9 */
878 for (c
= 0; c
< 9; c
++) {
879 snprintf(name
, 12, "dag%d", c
);
880 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
884 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
885 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
891 #ifdef HAVE_DAG_STREAMS_API
893 int stream
, rxstreams
;
894 rxstreams
= dag_rx_get_stream_count(dagfd
);
895 for(stream
=0;stream
<16;stream
+=2) {
896 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
897 dag_detach_stream(dagfd
, stream
);
899 snprintf(name
, 10, "dag%d:%d", c
, stream
);
900 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
909 #endif /* HAVE_DAG_STREAMS_API */
918 * Installs the given bpf filter program in the given pcap structure. There is
919 * no attempt to store the filter in kernel memory as that is not supported
923 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
928 strncpy(p
->errbuf
, "setfilter: No filter specified",
933 /* Make our private copy of the filter */
935 if (install_bpf_program(p
, fp
) < 0)
944 dag_set_datalink(pcap_t
*p
, int dlt
)
952 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
955 * Set non-blocking mode on the FD.
956 * XXX - is that necessary? If not, don't bother calling it,
957 * and have a "dag_getnonblock()" function that looks at
958 * "p->md.dag_offset_flags".
960 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
962 #ifdef HAVE_DAG_STREAMS_API
965 struct timeval maxwait
;
968 if (dag_get_stream_poll(p
->fd
, p
->md
.dag_stream
,
969 &mindata
, &maxwait
, &poll
) < 0) {
970 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
974 /* Amount of data to collect in Bytes before calling callbacks.
975 * Important for efficiency, but can introduce latency
976 * at low packet rates if to_ms not set!
983 if (dag_set_stream_poll(p
->fd
, p
->md
.dag_stream
,
984 mindata
, &maxwait
, &poll
) < 0) {
985 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
989 #endif /* HAVE_DAG_STREAMS_API */
991 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
993 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
999 dag_get_datalink(pcap_t
*p
)
1001 int index
=0, dlt_index
=0;
1004 memset(types
, 0, 255);
1006 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1007 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1013 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1014 /* Get list of possible ERF types for this card */
1015 if (dag_get_stream_erf_types(p
->fd
, p
->md
.dag_stream
, types
, 255) < 0) {
1016 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1020 while (types
[index
]) {
1022 #elif defined HAVE_DAG_GET_ERF_TYPES
1023 /* Get list of possible ERF types for this card */
1024 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1025 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1029 while (types
[index
]) {
1031 /* Check the type through a dagapi call. */
1032 types
[index
] = dag_linktype(p
->fd
);
1036 switch((types
[index
] & 0x7f)) {
1039 case TYPE_COLOR_HDLC_POS
:
1040 case TYPE_DSM_COLOR_HDLC_POS
:
1041 case TYPE_COLOR_HASH_POS
:
1043 if (p
->dlt_list
!= NULL
) {
1044 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1045 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1046 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1049 p
->linktype
= DLT_CHDLC
;
1053 case TYPE_COLOR_ETH
:
1054 case TYPE_DSM_COLOR_ETH
:
1055 case TYPE_COLOR_HASH_ETH
:
1057 * This is (presumably) a real Ethernet capture; give it a
1058 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1059 * that an application can let you choose it, in case you're
1060 * capturing DOCSIS traffic that a Cisco Cable Modem
1061 * Termination System is putting out onto an Ethernet (it
1062 * doesn't put an Ethernet header onto the wire, it puts raw
1063 * DOCSIS frames out on the wire inside the low-level
1064 * Ethernet framing).
1066 if (p
->dlt_list
!= NULL
) {
1067 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1068 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1071 p
->linktype
= DLT_EN10MB
;
1078 if (p
->dlt_list
!= NULL
) {
1079 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1080 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1083 p
->linktype
= DLT_ATM_RFC1483
;
1086 case TYPE_COLOR_MC_HDLC_POS
:
1088 if (p
->dlt_list
!= NULL
) {
1089 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1090 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1091 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1092 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1093 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1094 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1097 p
->linktype
= DLT_CHDLC
;
1102 p
->linktype
= DLT_RAW
;
1107 p
->linktype
= DLT_NULL
;
1111 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "unknown DAG linktype %d", types
[index
]);
1118 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1120 p
->dlt_count
= dlt_index
;