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.32 2007-10-30 10:16:45 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> */
49 #define ATM_CELL_SIZE 52
50 #define ATM_HDR_SIZE 4
53 * A header containing additional MTP information.
55 #define MTP2_SENT_OFFSET 0 /* 1 byte */
56 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
57 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
58 #define MTP2_HDR_LEN 4 /* length of the header */
60 #define MTP2_ANNEX_A_NOT_USED 0
61 #define MTP2_ANNEX_A_USED 1
62 #define MTP2_ANNEX_A_USED_UNKNOWN 2
64 /* SunATM pseudo header */
66 unsigned char flags
; /* destination and traffic type */
67 unsigned char vpi
; /* VPI */
68 unsigned short vci
; /* VCI */
71 typedef struct pcap_dag_node
{
72 struct pcap_dag_node
*next
;
77 static pcap_dag_node_t
*pcap_dags
= NULL
;
78 static int atexit_handler_installed
= 0;
79 static const unsigned short endian_test_word
= 0x0100;
81 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
85 /* This code is required when compiling for a DAG device only. */
88 /* Replace dag function names with pcap equivalent. */
89 #define dag_open_live pcap_open_live
90 #define dag_platform_finddevs pcap_platform_finddevs
93 #define MAX_DAG_PACKET 65536
95 static unsigned char TempPkt
[MAX_DAG_PACKET
];
97 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
98 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
99 static int dag_set_datalink(pcap_t
*p
, int dlt
);
100 static int dag_get_datalink(pcap_t
*p
);
101 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
104 delete_pcap_dag(pcap_t
*p
)
106 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
108 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
112 if (curr
!= NULL
&& curr
->p
== p
) {
114 prev
->next
= curr
->next
;
116 pcap_dags
= curr
->next
;
122 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
123 * in the pcap_t structure, and closes the file descriptor for the DAG card.
127 dag_platform_close(pcap_t
*p
)
131 #ifdef HAVE_DAG_STREAMS_API
132 if(dag_stop_stream(p
->fd
, p
->md
.dag_stream
) < 0)
133 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
135 if(dag_detach_stream(p
->fd
, p
->md
.dag_stream
) < 0)
136 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
138 if(dag_stop(p
->fd
) < 0)
139 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
140 #endif /* HAVE_DAG_STREAMS_API */
141 if(dag_close(p
->fd
) < 0)
142 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
145 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
151 while (pcap_dags
!= NULL
) {
152 if (pcap_dags
->pid
== getpid()) {
153 dag_platform_close(pcap_dags
->p
);
155 delete_pcap_dag(pcap_dags
->p
);
161 new_pcap_dag(pcap_t
*p
)
163 pcap_dag_node_t
*node
= NULL
;
165 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
169 if (!atexit_handler_installed
) {
170 atexit(atexit_handler
);
171 atexit_handler_installed
= 1;
174 node
->next
= pcap_dags
;
176 node
->pid
= getpid();
184 * Read at most max_packets from the capture stream and call the callback
185 * for each of them. Returns the number of packets handled, -1 if an
186 * error occured, or -2 if we were told to break out of the loop.
189 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
191 unsigned int processed
= 0;
192 int flags
= p
->md
.dag_offset_flags
;
193 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
195 /* Get the next bufferful of packets (if necessary). */
196 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) {
199 * Has "pcap_breakloop()" been called?
203 * Yes - clear the flag that indicates that
204 * it has, and return -2 to indicate that
205 * we were told to break out of the loop.
211 #ifdef HAVE_DAG_STREAMS_API
212 /* dag_advance_stream() will block (unless nonblock is called)
213 * until 64kB of data has accumulated.
214 * If to_ms is set, it will timeout before 64kB has accumulated.
215 * We wait for 64kB because processing a few packets at a time
216 * can cause problems at high packet rates (>200kpps) due
218 * This does mean if to_ms is not specified the capture may 'hang'
219 * for long periods if the data rate is extremely slow (<64kB/sec)
220 * If non-block is specified it will return immediately. The user
221 * is then responsible for efficiency.
223 if ( NULL
== (p
->md
.dag_mem_top
= dag_advance_stream(p
->fd
, p
->md
.dag_stream
, &(p
->md
.dag_mem_bottom
))) ) {
227 /* dag_offset does not support timeouts */
228 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), flags
);
229 #endif /* HAVE_DAG_STREAMS_API */
231 if (nonblocking
&& (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
233 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
239 (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
241 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
247 /* Process the packets. */
248 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
>= dag_record_size
) {
250 unsigned short packet_len
= 0;
252 struct pcap_pkthdr pcap_header
;
254 #ifdef HAVE_DAG_STREAMS_API
255 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_bottom
);
257 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
258 #endif /* HAVE_DAG_STREAMS_API */
260 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
264 * Has "pcap_breakloop()" been called?
268 * Yes - clear the flag that indicates that
269 * it has, and return -2 to indicate that
270 * we were told to break out of the loop.
276 rlen
= ntohs(header
->rlen
);
277 if (rlen
< dag_record_size
)
279 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
282 p
->md
.dag_mem_bottom
+= rlen
;
284 /* ERF encapsulation */
285 /* The Extensible Record Format is not dropped for this kind of encapsulation,
286 * and will be handled as a pseudo header by the decoding application.
287 * The information carried in the ERF header and in the optional subheader (if present)
288 * could be merged with the libpcap information, to offer a better decoding.
289 * The packet length is
290 * o the length of the packet on the link (header->wlen),
291 * o plus the length of the ERF header (dag_record_size), as the length of the
292 * pseudo header will be adjusted during the decoding,
293 * o plus the length of the optional subheader (if present).
295 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
296 * if the capture length is greater than the packet length.
298 if (p
->linktype
== DLT_ERF
) {
299 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
301 switch (header
->type
) {
305 packet_len
+= 4; /* MC header */
308 case TYPE_DSM_COLOR_ETH
:
311 packet_len
+= 2; /* ETH header */
315 if (caplen
> packet_len
) {
319 /* Other kind of encapsulation according to the header Type */
320 dp
+= dag_record_size
;
322 switch(header
->type
) {
326 if (header
->type
== TYPE_AAL5
) {
327 packet_len
= ntohs(header
->wlen
);
328 caplen
= rlen
- dag_record_size
;
333 if (header
->type
== TYPE_MC_ATM
) {
334 caplen
= packet_len
= ATM_CELL_SIZE
;
340 if (header
->type
== TYPE_MC_AAL5
) {
341 packet_len
= ntohs(header
->wlen
);
342 caplen
= rlen
- dag_record_size
- 4;
346 if (header
->type
== TYPE_ATM
) {
347 caplen
= packet_len
= ATM_CELL_SIZE
;
349 if (p
->linktype
== DLT_SUNATM
) {
350 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
351 unsigned long rawatm
;
353 rawatm
= ntohl(*((unsigned long *)dp
));
354 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
355 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
356 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
357 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
358 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
359 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
360 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
361 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
364 packet_len
-= ATM_HDR_SIZE
;
365 caplen
-= ATM_HDR_SIZE
;
370 #ifdef TYPE_DSM_COLOR_ETH
371 case TYPE_DSM_COLOR_ETH
:
373 #ifdef TYPE_COLOR_ETH
377 packet_len
= ntohs(header
->wlen
);
378 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
379 caplen
= rlen
- dag_record_size
- 2;
380 if (caplen
> packet_len
) {
385 #ifdef TYPE_DSM_COLOR_HDLC_POS
386 case TYPE_DSM_COLOR_HDLC_POS
:
388 #ifdef TYPE_COLOR_HDLC_POS
389 case TYPE_COLOR_HDLC_POS
:
392 packet_len
= ntohs(header
->wlen
);
393 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
394 caplen
= rlen
- dag_record_size
;
395 if (caplen
> packet_len
) {
399 #ifdef TYPE_COLOR_MC_HDLC_POS
400 case TYPE_COLOR_MC_HDLC_POS
:
404 packet_len
= ntohs(header
->wlen
);
405 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
406 caplen
= rlen
- dag_record_size
- 4;
407 if (caplen
> packet_len
) {
410 /* jump the MC_HDLC_HEADER */
412 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
413 /* Add the MTP2 Pseudo Header */
414 caplen
+= MTP2_HDR_LEN
;
415 packet_len
+= MTP2_HDR_LEN
;
417 TempPkt
[MTP2_SENT_OFFSET
] = 0;
418 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
419 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
420 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
421 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
427 /* Unhandled ERF type.
428 * Ignore rather than generating error
432 } /* ERF encapsulation */
434 if (caplen
> p
->snapshot
)
435 caplen
= p
->snapshot
;
437 /* Count lost packets. */
438 switch(header
->type
) {
439 #ifdef TYPE_COLOR_HDLC_POS
440 /* in this type the color value overwrites the lctr */
441 case TYPE_COLOR_HDLC_POS
:
444 #ifdef TYPE_COLOR_ETH
445 /* in this type the color value overwrites the lctr */
449 #ifdef TYPE_DSM_COLOR_HDLC_POS
450 /* in this type the color value overwrites the lctr */
451 case TYPE_DSM_COLOR_HDLC_POS
:
454 #ifdef TYPE_DSM_COLOR_ETH
455 /* in this type the color value overwrites the lctr */
456 case TYPE_DSM_COLOR_ETH
:
459 #ifdef TYPE_COLOR_MC_HDLC_POS
460 case TYPE_COLOR_MC_HDLC_POS
:
466 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
467 p
->md
.stat
.ps_drop
= UINT_MAX
;
469 p
->md
.stat
.ps_drop
+= ntohs(header
->lctr
);
474 /* Run the packet filter if there is one. */
475 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
477 /* convert between timestamp formats */
478 register unsigned long long ts
;
480 if (IS_BIGENDIAN()) {
481 ts
= SWAPLL(header
->ts
);
486 pcap_header
.ts
.tv_sec
= ts
>> 32;
487 ts
= (ts
& 0xffffffffULL
) * 1000000;
488 ts
+= 0x80000000; /* rounding */
489 pcap_header
.ts
.tv_usec
= ts
>> 32;
490 if (pcap_header
.ts
.tv_usec
>= 1000000) {
491 pcap_header
.ts
.tv_usec
-= 1000000;
492 pcap_header
.ts
.tv_sec
++;
495 /* Fill in our own header data */
496 pcap_header
.caplen
= caplen
;
497 pcap_header
.len
= packet_len
;
499 /* Count the packet. */
500 p
->md
.stat
.ps_recv
++;
502 /* Call the user supplied callback function */
503 callback(user
, &pcap_header
, dp
);
505 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
507 if (processed
== cnt
)
509 /* Reached the user-specified limit. */
519 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
521 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
527 * Get a handle for a live capture from the given DAG device. Passing a NULL
528 * device will result in a failure. The promisc flag is ignored because DAG
529 * cards are always promiscuous. The to_ms parameter is also ignored as it is
530 * not supported in hardware.
532 * snaplen is now also ignored, until we get per-stream slen support. Set
533 * slen with approprite DAG tool BEFORE pcap_open_live().
538 dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
540 char conf
[30]; /* dag configure string */
545 char * newDev
= NULL
;
546 #ifdef HAVE_DAG_STREAMS_API
548 struct timeval maxwait
;
552 if (device
== NULL
) {
553 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
556 /* Allocate a handle for this session. */
558 handle
= malloc(sizeof(*handle
));
559 if (handle
== NULL
) {
560 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
564 /* Initialize some components of the pcap structure. */
566 memset(handle
, 0, sizeof(*handle
));
568 #ifdef HAVE_DAG_STREAMS_API
569 newDev
= (char *)malloc(strlen(device
) + 16);
570 if (newDev
== NULL
) {
571 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
575 /* Parse input name to get dag device and stream number if provided */
576 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handle
->md
.dag_stream
) < 0) {
577 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
582 if (handle
->md
.dag_stream
%2) {
583 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
587 if (strncmp(device
, "/dev/", 5) != 0) {
588 newDev
= (char *)malloc(strlen(device
) + 5);
589 if (newDev
== NULL
) {
590 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
593 strcpy(newDev
, "/dev/");
594 strcat(newDev
, device
);
597 #endif /* HAVE_DAG_STREAMS_API */
599 /* setup device parameters */
600 if((handle
->fd
= dag_open((char *)device
)) < 0) {
601 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
605 #ifdef HAVE_DAG_STREAMS_API
606 /* Open requested stream. Can fail if already locked or on error */
607 if (dag_attach_stream(handle
->fd
, handle
->md
.dag_stream
, 0, 0) < 0) {
608 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
612 /* Set up default poll parameters for stream
613 * Can be overridden by pcap_set_nonblock()
615 if (dag_get_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
616 &mindata
, &maxwait
, &poll
) < 0) {
617 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
621 /* Amount of data to collect in Bytes before calling callbacks.
622 * Important for efficiency, but can introduce latency
623 * at low packet rates if to_ms not set!
627 /* Obey to_ms if supplied. This is a good idea!
628 * Recommend 10-100ms. Calls will time out even if no data arrived.
630 maxwait
.tv_sec
= to_ms
/1000;
631 maxwait
.tv_usec
= (to_ms
%1000) * 1000;
633 if (dag_set_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
634 mindata
, &maxwait
, &poll
) < 0) {
635 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
640 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
641 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
645 #endif /* HAVE_DAG_STREAMS_API */
647 /* XXX Not calling dag_configure() to set slen; this is unsafe in
648 * multi-stream environments as the gpp config is global.
649 * Once the firmware provides 'per-stream slen' this can be supported
650 * again via the Config API without side-effects */
652 /* set the card snap length to the specified snaplen parameter */
653 /* This is a really bad idea, as different cards have different
654 * valid slen ranges. Should fix in Config API. */
655 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
656 snaplen
= MAX_DAG_SNAPLEN
;
657 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
658 snaplen
= MIN_DAG_SNAPLEN
;
660 /* snap len has to be a multiple of 4 */
661 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
663 if(dag_configure(handle
->fd
, conf
) < 0) {
664 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
669 #ifdef HAVE_DAG_STREAMS_API
670 if(dag_start_stream(handle
->fd
, handle
->md
.dag_stream
) < 0) {
671 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
675 if(dag_start(handle
->fd
) < 0) {
676 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
679 #endif /* HAVE_DAG_STREAMS_API */
682 * Important! You have to ensure bottom is properly
683 * initialized to zero on startup, it won't give you
684 * a compiler warning if you make this mistake!
686 handle
->md
.dag_mem_bottom
= 0;
687 handle
->md
.dag_mem_top
= 0;
690 * Find out how many FCS bits we should strip.
691 * First, query the card to see if it strips the FCS.
693 daginf
= dag_info(handle
->fd
);
694 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
695 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
696 handle
->md
.dag_fcs_bits
= 0;
698 /* Note that no FCS will be supplied. */
699 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
702 * Start out assuming it's 32 bits.
704 handle
->md
.dag_fcs_bits
= 32;
706 /* Allow an environment variable to override. */
707 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
708 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
709 handle
->md
.dag_fcs_bits
= n
;
711 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
712 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
718 * Did the user request that they not be stripped?
720 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
721 /* Yes. Note the number of bytes that will be
723 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handle
->md
.dag_fcs_bits
/16);
725 /* And don't strip them. */
726 handle
->md
.dag_fcs_bits
= 0;
730 handle
->snapshot
= snaplen
;
731 handle
->md
.dag_timeout
= to_ms
;
733 handle
->linktype
= -1;
734 if (dag_get_datalink(handle
) < 0) {
735 strcpy(ebuf
, handle
->errbuf
);
741 if (new_pcap_dag(handle
) < 0) {
742 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
747 * "select()" and "poll()" don't work on DAG device descriptors.
749 handle
->selectable_fd
= -1;
751 if (newDev
!= NULL
) {
752 free((char *)newDev
);
755 handle
->read_op
= dag_read
;
756 handle
->inject_op
= dag_inject
;
757 handle
->setfilter_op
= dag_setfilter
;
758 handle
->setdirection_op
= NULL
; /* Not implemented.*/
759 handle
->set_datalink_op
= dag_set_datalink
;
760 handle
->getnonblock_op
= pcap_getnonblock_fd
;
761 handle
->setnonblock_op
= dag_setnonblock
;
762 handle
->stats_op
= dag_stats
;
763 handle
->close_op
= dag_platform_close
;
764 handle
->md
.stat
.ps_drop
= 0;
765 handle
->md
.stat
.ps_recv
= 0;
768 #ifdef HAVE_DAG_STREAMS_API
770 if (handle
!= NULL
) {
771 if (dag_stop_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
772 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
776 if (handle
!= NULL
) {
777 if (dag_detach_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
778 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
782 if (handle
!= NULL
) {
783 if (dag_stop(p
->fd
) < 0)
784 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
786 #endif /* HAVE_DAG_STREAMS_API */
789 if (handle
!= NULL
) {
790 if (dag_close(handle
->fd
) < 0)
791 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
794 delete_pcap_dag(handle
);
797 if (newDev
!= NULL
) {
798 free((char *)newDev
);
800 if (handle
!= NULL
) {
802 * Get rid of any link-layer type list we allocated.
804 if (handle
->dlt_list
!= NULL
) {
805 free(handle
->dlt_list
);
814 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
815 /* This needs to be filled out correctly. Hopefully a dagapi call will
816 provide all necessary information.
818 /*p->md.stat.ps_recv = 0;*/
819 /*p->md.stat.ps_drop = 0;*/
827 * Previously we just generated a list of all possible names and let
828 * pcap_add_if() attempt to open each one, but with streams this adds up
829 * to 81 possibilities which is inefficient.
831 * Since we know more about the devices we can prune the tree here.
832 * pcap_add_if() will still retest each device but the total number of
833 * open attempts will still be much less than the naive approach.
836 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
838 char name
[12]; /* XXX - pick a size */
841 char dagname
[DAGNAME_BUFSIZE
];
845 /* Try all the DAGs 0-9 */
846 for (c
= 0; c
< 9; c
++) {
847 snprintf(name
, 12, "dag%d", c
);
848 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
852 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
853 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
859 #ifdef HAVE_DAG_STREAMS_API
861 int stream
, rxstreams
, found
=0;
862 rxstreams
= dag_rx_get_stream_count(dagfd
);
863 for(stream
=0;stream
<16;stream
+=2) {
864 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
865 dag_detach_stream(dagfd
, stream
);
867 snprintf(name
, 10, "dag%d:%d", c
, stream
);
868 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
877 #endif /* HAVE_DAG_STREAMS_API */
886 * Installs the given bpf filter program in the given pcap structure. There is
887 * no attempt to store the filter in kernel memory as that is not supported
891 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
896 strncpy(p
->errbuf
, "setfilter: No filter specified",
901 /* Make our private copy of the filter */
903 if (install_bpf_program(p
, fp
) < 0)
912 dag_set_datalink(pcap_t
*p
, int dlt
)
920 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
923 * Set non-blocking mode on the FD.
924 * XXX - is that necessary? If not, don't bother calling it,
925 * and have a "dag_getnonblock()" function that looks at
926 * "p->md.dag_offset_flags".
928 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
930 #ifdef HAVE_DAG_STREAMS_API
933 struct timeval maxwait
;
936 if (dag_get_stream_poll(p
->fd
, p
->md
.dag_stream
,
937 &mindata
, &maxwait
, &poll
) < 0) {
938 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
942 /* Amount of data to collect in Bytes before calling callbacks.
943 * Important for efficiency, but can introduce latency
944 * at low packet rates if to_ms not set!
951 if (dag_set_stream_poll(p
->fd
, p
->md
.dag_stream
,
952 mindata
, &maxwait
, &poll
) < 0) {
953 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
957 #endif /* HAVE_DAG_STREAMS_API */
959 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
961 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
967 dag_get_datalink(pcap_t
*p
)
969 int index
=0, dlt_index
=0;
972 memset(types
, 0, 255);
974 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
975 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
981 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
982 /* Get list of possible ERF types for this card */
983 if (dag_get_stream_erf_types(p
->fd
, p
->md
.dag_stream
, types
, 255) < 0) {
984 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
988 while (types
[index
]) {
990 #elif defined HAVE_DAG_GET_ERF_TYPES
991 /* Get list of possible ERF types for this card */
992 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
993 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
997 while (types
[index
]) {
999 /* Check the type through a dagapi call. */
1000 types
[index
] = dag_linktype(p
->fd
);
1004 switch(types
[index
]) {
1007 #ifdef TYPE_COLOR_HDLC_POS
1008 case TYPE_COLOR_HDLC_POS
:
1010 #ifdef TYPE_DSM_COLOR_HDLC_POS
1011 case TYPE_DSM_COLOR_HDLC_POS
:
1013 if (p
->dlt_list
!= NULL
) {
1014 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1015 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1016 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1019 p
->linktype
= DLT_CHDLC
;
1023 #ifdef TYPE_COLOR_ETH
1024 case TYPE_COLOR_ETH
:
1026 #ifdef TYPE_DSM_COLOR_ETH
1027 case TYPE_DSM_COLOR_ETH
:
1030 * This is (presumably) a real Ethernet capture; give it a
1031 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1032 * that an application can let you choose it, in case you're
1033 * capturing DOCSIS traffic that a Cisco Cable Modem
1034 * Termination System is putting out onto an Ethernet (it
1035 * doesn't put an Ethernet header onto the wire, it puts raw
1036 * DOCSIS frames out on the wire inside the low-level
1037 * Ethernet framing).
1039 if (p
->dlt_list
!= NULL
) {
1040 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1041 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1044 p
->linktype
= DLT_EN10MB
;
1057 if (p
->dlt_list
!= NULL
) {
1058 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1059 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1062 p
->linktype
= DLT_ATM_RFC1483
;
1065 #ifdef TYPE_COLOR_MC_HDLC_POS
1066 case TYPE_COLOR_MC_HDLC_POS
:
1070 if (p
->dlt_list
!= NULL
) {
1071 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1072 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1073 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1074 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1075 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1076 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1079 p
->linktype
= DLT_CHDLC
;
1085 p
->linktype
= DLT_NULL
;
1089 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "unknown DAG linktype %d", types
[index
]);
1096 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1098 p
->dlt_count
= dlt_index
;