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.8 2008-04-14 20:41:51 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_create pcap_create
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_cleanup(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 */
143 if(dag_close(p->fd) < 0)
144 fprintf(stderr,"dag_close: %s\n", strerror(errno));
148 pcap_cleanup_live_common(p);
150 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
156 while (pcap_dags != NULL) {
157 if (pcap_dags->pid == getpid()) {
158 dag_platform_cleanup(pcap_dags->p);
160 delete_pcap_dag(pcap_dags->p);
166 new_pcap_dag(pcap_t *p)
168 pcap_dag_node_t *node = NULL;
170 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
174 if (!atexit_handler_installed) {
175 atexit(atexit_handler);
176 atexit_handler_installed = 1;
179 node->next = pcap_dags;
181 node->pid = getpid();
189 dag_erf_ext_header_count(uint8_t * erf, size_t len)
191 uint32_t hdr_num = 0;
194 /* basic sanity checks */
200 /* check if we have any extension headers */
201 if ( (erf[8] & 0x80) == 0x00 )
204 /* loop over the extension headers */
207 /* sanity check we have enough bytes */
208 if ( len <= (24 + (hdr_num * 8)) )
211 /* get the header type */
212 hdr_type = erf[(16 + (hdr_num * 8))];
215 } while ( hdr_type & 0x80 );
221 * Read at most max_packets from the capture stream and call the callback
222 * for each of them. Returns the number of packets handled, -1 if an
223 * error occured, or -2 if we were told to break out of the loop.
226 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
228 unsigned int processed = 0;
229 int flags = p->md.dag_offset_flags;
230 unsigned int nonblocking = flags & DAGF_NONBLOCK;
231 unsigned int num_ext_hdr = 0;
233 /* Get the next bufferful of packets (if necessary). */
234 while (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) {
237 * Has "pcap_breakloop()" been called?
241 * Yes - clear the flag that indicates that
242 * it has, and return -2 to indicate that
243 * we were told to break out of the loop.
249 #ifdef HAVE_DAG_STREAMS_API
250 /* dag_advance_stream() will block (unless nonblock is called)
251 * until 64kB of data has accumulated.
252 * If to_ms is set, it will timeout before 64kB has accumulated.
253 * We wait for 64kB because processing a few packets at a time
254 * can cause problems at high packet rates (>200kpps) due
256 * This does mean if to_ms is not specified the capture may 'hang'
257 * for long periods if the data rate is extremely slow (<64kB/sec)
258 * If non-block is specified it will return immediately. The user
259 * is then responsible for efficiency.
261 if ( NULL == (p->md.dag_mem_top = dag_advance_stream(p->fd, p->md.dag_stream, &(p->md.dag_mem_bottom))) ) {
265 /* dag_offset does not support timeouts */
266 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
267 #endif /* HAVE_DAG_STREAMS_API */
269 if (nonblocking && (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
271 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
277 (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
279 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
285 /* Process the packets. */
286 while (p->md.dag_mem_top - p->md.dag_mem_bottom >= dag_record_size) {
288 unsigned short packet_len = 0;
290 struct pcap_pkthdr pcap_header;
292 #ifdef HAVE_DAG_STREAMS_API
293 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_bottom);
295 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
296 #endif /* HAVE_DAG_STREAMS_API */
298 u_char *dp = ((u_char *)header); /* + dag_record_size; */
302 * Has "pcap_breakloop()" been called?
306 * Yes - clear the flag that indicates that
307 * it has, and return -2 to indicate that
308 * we were told to break out of the loop.
314 rlen = ntohs(header->rlen);
315 if (rlen < dag_record_size)
317 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
320 p->md.dag_mem_bottom += rlen;
322 /* Count lost packets. */
323 switch((header->type & 0x7f)) {
324 /* in these types the color value overwrites the lctr */
325 case TYPE_COLOR_HDLC_POS:
327 case TYPE_DSM_COLOR_HDLC_POS:
328 case TYPE_DSM_COLOR_ETH:
329 case TYPE_COLOR_MC_HDLC_POS:
330 case TYPE_COLOR_HASH_ETH:
331 case TYPE_COLOR_HASH_POS:
336 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
337 p->md.stat.ps_drop = UINT_MAX;
339 p->md.stat.ps_drop += ntohs(header->lctr);
344 if ((header->type & 0x7f) == TYPE_PAD) {
348 num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
350 /* ERF encapsulation */
351 /* The Extensible Record Format is not dropped for this kind of encapsulation,
352 * and will be handled as a pseudo header by the decoding application.
353 * The information carried in the ERF header and in the optional subheader (if present)
354 * could be merged with the libpcap information, to offer a better decoding.
355 * The packet length is
356 * o the length of the packet on the link (header->wlen),
357 * o plus the length of the ERF header (dag_record_size), as the length of the
358 * pseudo header will be adjusted during the decoding,
359 * o plus the length of the optional subheader (if present).
361 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
362 * if the capture length is greater than the packet length.
364 if (p->linktype == DLT_ERF) {
365 packet_len = ntohs(header->wlen) + dag_record_size;
367 switch ((header->type & 0x7f)) {
371 packet_len += 4; /* MC header */
374 case TYPE_COLOR_HASH_ETH:
375 case TYPE_DSM_COLOR_ETH:
378 packet_len += 2; /* ETH header */
382 /* Include ERF extension headers */
383 packet_len += (8 * num_ext_hdr);
385 if (caplen > packet_len) {
389 /* Other kind of encapsulation according to the header Type */
391 /* Skip over generic ERF header */
392 dp += dag_record_size;
393 /* Skip over extension headers */
394 dp += 8 * num_ext_hdr;
396 switch((header->type & 0x7f)) {
399 if (header->type == TYPE_AAL5) {
400 packet_len = ntohs(header->wlen);
401 caplen = rlen - dag_record_size;
404 if (header->type == TYPE_MC_ATM) {
405 caplen = packet_len = ATM_CELL_SIZE;
409 if (header->type == TYPE_MC_AAL5) {
410 packet_len = ntohs(header->wlen);
411 caplen = rlen - dag_record_size - 4;
414 if (header->type == TYPE_ATM) {
415 caplen = packet_len = ATM_CELL_SIZE;
417 if (p->linktype == DLT_SUNATM) {
418 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
419 unsigned long rawatm;
421 rawatm = ntohl(*((unsigned long *)dp));
422 sunatm->vci = htons((rawatm >> 4) & 0xffff);
423 sunatm->vpi = (rawatm >> 20) & 0x00ff;
424 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
425 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
426 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
427 ((dp[ATM_HDR_SIZE] == 0xaa &&
428 dp[ATM_HDR_SIZE+1] == 0xaa &&
429 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
432 packet_len -= ATM_HDR_SIZE;
433 caplen -= ATM_HDR_SIZE;
438 case TYPE_COLOR_HASH_ETH:
439 case TYPE_DSM_COLOR_ETH:
442 packet_len = ntohs(header->wlen);
443 packet_len -= (p->md.dag_fcs_bits >> 3);
444 caplen = rlen - dag_record_size - 2;
445 if (caplen > packet_len) {
451 case TYPE_COLOR_HASH_POS:
452 case TYPE_DSM_COLOR_HDLC_POS:
453 case TYPE_COLOR_HDLC_POS:
455 packet_len = ntohs(header->wlen);
456 packet_len -= (p->md.dag_fcs_bits >> 3);
457 caplen = rlen - dag_record_size;
458 if (caplen > packet_len) {
463 case TYPE_COLOR_MC_HDLC_POS:
465 packet_len = ntohs(header->wlen);
466 packet_len -= (p->md.dag_fcs_bits >> 3);
467 caplen = rlen - dag_record_size - 4;
468 if (caplen > packet_len) {
471 /* jump the MC_HDLC_HEADER */
473 #ifdef DLT_MTP2_WITH_PHDR
474 if (p->linktype == DLT_MTP2_WITH_PHDR) {
475 /* Add the MTP2 Pseudo Header */
476 caplen += MTP2_HDR_LEN;
477 packet_len += MTP2_HDR_LEN;
479 TempPkt[MTP2_SENT_OFFSET] = 0;
480 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
481 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
482 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
483 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
490 packet_len = ntohs(header->wlen);
491 caplen = rlen - dag_record_size;
492 if (caplen > packet_len) {
498 /* Unhandled ERF type.
499 * Ignore rather than generating error
504 /* Skip over extension headers */
505 caplen -= (8 * num_ext_hdr);
507 } /* ERF encapsulation */
509 if (caplen > p->snapshot)
510 caplen = p->snapshot;
512 /* Run the packet filter if there is one. */
513 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
515 /* convert between timestamp formats */
516 register unsigned long long ts;
518 if (IS_BIGENDIAN()) {
519 ts = SWAPLL(header->ts);
524 pcap_header.ts.tv_sec = ts >> 32;
525 ts = (ts & 0xffffffffULL) * 1000000;
526 ts += 0x80000000; /* rounding */
527 pcap_header.ts.tv_usec = ts >> 32;
528 if (pcap_header.ts.tv_usec >= 1000000) {
529 pcap_header.ts.tv_usec -= 1000000;
530 pcap_header.ts.tv_sec++;
533 /* Fill in our own header data */
534 pcap_header.caplen = caplen;
535 pcap_header.len = packet_len;
537 /* Count the packet. */
538 p->md.stat.ps_recv++;
540 /* Call the user supplied callback function */
541 callback(user, &pcap_header, dp);
543 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
545 if (processed == cnt && cnt > 0)
547 /* Reached the user-specified limit. */
557 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
559 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
565 * Get a handle for a live capture from the given DAG device. Passing a NULL
566 * device will result in a failure. The promisc flag is ignored because DAG
567 * cards are always promiscuous. The to_ms parameter is used in setting the
568 * API polling parameters.
570 * snaplen is now also ignored, until we get per-stream slen support. Set
571 * slen with approprite DAG tool BEFORE pcap_activate().
575 static int dag_activate(pcap_t* handle)
578 char conf[30]; /* dag configure string */
583 char * newDev = NULL;
584 char * device = handle->opt.source;
585 #ifdef HAVE_DAG_STREAMS_API
587 struct timeval maxwait;
591 if (device == NULL) {
592 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
596 /* Initialize some components of the pcap structure. */
598 #ifdef HAVE_DAG_STREAMS_API
599 newDev = (char *)malloc(strlen(device) + 16);
600 if (newDev == NULL) {
601 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
605 /* Parse input name to get dag device and stream number if provided */
606 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
607 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
612 if (handle->md.dag_stream%2) {
613 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
617 if (strncmp(device, "/dev/", 5) != 0) {
618 newDev = (char *)malloc(strlen(device) + 5);
619 if (newDev == NULL) {
620 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
623 strcpy(newDev, "/dev/");
624 strcat(newDev, device);
627 #endif /* HAVE_DAG_STREAMS_API */
629 /* setup device parameters */
630 if((handle->fd = dag_open((char *)device)) < 0) {
631 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
635 #ifdef HAVE_DAG_STREAMS_API
636 /* Open requested stream. Can fail if already locked or on error */
637 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
638 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
642 /* Set up default poll parameters for stream
643 * Can be overridden by pcap_set_nonblock()
645 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
646 &mindata, &maxwait, &poll) < 0) {
647 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
651 /* Amount of data to collect in Bytes before calling callbacks.
652 * Important for efficiency, but can introduce latency
653 * at low packet rates if to_ms not set!
657 /* Obey md.timeout (was to_ms) if supplied. This is a good idea!
658 * Recommend 10-100ms. Calls will time out even if no data arrived.
660 maxwait.tv_sec = handle->md.timeout/1000;
661 maxwait.tv_usec = (handle->md.timeout%1000) * 1000;
663 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
664 mindata, &maxwait, &poll) < 0) {
665 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
670 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
671 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
675 #endif /* HAVE_DAG_STREAMS_API */
677 /* XXX Not calling dag_configure() to set slen; this is unsafe in
678 * multi-stream environments as the gpp config is global.
679 * Once the firmware provides 'per-stream slen' this can be supported
680 * again via the Config API without side-effects */
682 /* set the card snap length to the specified snaplen parameter */
683 /* This is a really bad idea, as different cards have different
684 * valid slen ranges. Should fix in Config API. */
685 if (handle->snapshot == 0 || handle->snapshot > MAX_DAG_SNAPLEN) {
686 handle->snapshot = MAX_DAG_SNAPLEN;
687 } else if (snaplen < MIN_DAG_SNAPLEN) {
688 handle->snapshot = MIN_DAG_SNAPLEN;
690 /* snap len has to be a multiple of 4 */
691 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
693 if(dag_configure(handle->fd, conf) < 0) {
694 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
699 #ifdef HAVE_DAG_STREAMS_API
700 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
701 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
705 if(dag_start(handle->fd) < 0) {
706 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
709 #endif /* HAVE_DAG_STREAMS_API */
712 * Important! You have to ensure bottom is properly
713 * initialized to zero on startup, it won't give you
714 * a compiler warning if you make this mistake!
716 handle->md.dag_mem_bottom = 0;
717 handle->md.dag_mem_top = 0;
720 * Find out how many FCS bits we should strip.
721 * First, query the card to see if it strips the FCS.
723 daginf = dag_info(handle->fd);
724 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) {
725 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
726 handle->md.dag_fcs_bits = 0;
728 /* Note that no FCS will be supplied. */
729 handle->linktype_ext = LT_FCS_DATALINK_EXT(0);
732 * Start out assuming it's 32 bits.
734 handle->md.dag_fcs_bits = 32;
736 /* Allow an environment variable to override. */
737 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
738 if ((n = atoi(s)) == 0 || n == 16 || n == 32) {
739 handle->md.dag_fcs_bits = n;
741 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
742 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
748 * Did the user request that they not be stripped?
750 if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) {
751 /* Yes. Note the number of bytes that will be
753 handle->linktype_ext = LT_FCS_DATALINK_EXT(handle->md.dag_fcs_bits/16);
755 /* And don't strip them. */
756 handle->md.dag_fcs_bits = 0;
760 handle->md.dag_timeout = handle->md.timeout;
762 handle->linktype = -1;
763 if (dag_get_datalink(handle) < 0)
768 if (new_pcap_dag(handle) < 0) {
769 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
774 * "select()" and "poll()" don't work on DAG device descriptors.
776 handle->selectable_fd = -1;
778 if (newDev != NULL) {
779 free((char *)newDev);
782 handle->read_op = dag_read;
783 handle->inject_op = dag_inject;
784 handle->setfilter_op = dag_setfilter;
785 handle->setdirection_op = NULL; /* Not implemented.*/
786 handle->set_datalink_op = dag_set_datalink;
787 handle->getnonblock_op = pcap_getnonblock_fd;
788 handle->setnonblock_op = dag_setnonblock;
789 handle->stats_op = dag_stats;
790 handle->cleanup_op = dag_platform_cleanup;
791 handle->md.stat.ps_drop = 0;
792 handle->md.stat.ps_recv = 0;
795 #ifdef HAVE_DAG_STREAMS_API
797 if (dag_stop_stream(handle->fd, handle->md.dag_stream) < 0) {
798 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
802 if (dag_detach_stream(handle->fd, handle->md.dag_stream) < 0)
803 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
806 if (dag_stop(handle->fd) < 0)
807 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
808 #endif /* HAVE_DAG_STREAMS_API */
811 if (dag_close(handle->fd) < 0)
812 fprintf(stderr,"dag_close: %s\n", strerror(errno));
813 delete_pcap_dag(handle);
816 pcap_cleanup_live_common(handle);
817 if (newDev != NULL) {
818 free((char *)newDev);
824 pcap_t *dag_create(const char *device, char *ebuf)
828 p = pcap_create_common(device, ebuf);
832 p->activate_op = dag_activate;
837 dag_stats(pcap_t *p, struct pcap_stat *ps) {
838 /* This needs to be filled out correctly. Hopefully a dagapi call will
839 provide all necessary information.
841 /*p->md.stat.ps_recv = 0;*/
842 /*p->md.stat.ps_drop = 0;*/
850 * Previously we just generated a list of all possible names and let
851 * pcap_add_if() attempt to open each one, but with streams this adds up
852 * to 81 possibilities which is inefficient.
854 * Since we know more about the devices we can prune the tree here.
855 * pcap_add_if() will still retest each device but the total number of
856 * open attempts will still be much less than the naive approach.
859 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
861 char name[12]; /* XXX - pick a size */
864 char dagname[DAGNAME_BUFSIZE];
868 /* Try all the DAGs 0-9 */
869 for (c = 0; c < 9; c++) {
870 snprintf(name, 12, "dag%d", c);
871 if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
875 if ( (dagfd = dag_open(dagname)) >= 0 ) {
876 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
882 #ifdef HAVE_DAG_STREAMS_API
884 int stream, rxstreams;
885 rxstreams = dag_rx_get_stream_count(dagfd);
886 for(stream=0;stream<16;stream+=2) {
887 if (0 == dag_attach_stream(dagfd, stream, 0, 0)) {
888 dag_detach_stream(dagfd, stream);
890 snprintf(name, 10, "dag%d:%d", c, stream);
891 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
900 #endif /* HAVE_DAG_STREAMS_API */
909 * Installs the given bpf filter program in the given pcap structure. There is
910 * no attempt to store the filter in kernel memory as that is not supported
914 dag_setfilter(pcap_t *p, struct bpf_program *fp)
919 strncpy(p->errbuf, "setfilter: No filter specified",
924 /* Make our private copy of the filter */
926 if (install_bpf_program(p, fp) < 0)
935 dag_set_datalink(pcap_t *p, int dlt)
943 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
946 * Set non-blocking mode on the FD.
947 * XXX - is that necessary? If not, don't bother calling it,
948 * and have a "dag_getnonblock()" function that looks at
949 * "p->md.dag_offset_flags".
951 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
953 #ifdef HAVE_DAG_STREAMS_API
956 struct timeval maxwait;
959 if (dag_get_stream_poll(p->fd, p->md.dag_stream,
960 &mindata, &maxwait, &poll) < 0) {
961 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
965 /* Amount of data to collect in Bytes before calling callbacks.
966 * Important for efficiency, but can introduce latency
967 * at low packet rates if to_ms not set!
974 if (dag_set_stream_poll(p->fd, p->md.dag_stream,
975 mindata, &maxwait, &poll) < 0) {
976 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
980 #endif /* HAVE_DAG_STREAMS_API */
982 p->md.dag_offset_flags |= DAGF_NONBLOCK;
984 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
990 dag_get_datalink(pcap_t *p)
992 int index=0, dlt_index=0;
995 memset(types, 0, 255);
997 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
998 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
1004 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1005 /* Get list of possible ERF types for this card */
1006 if (dag_get_stream_erf_types(p->fd, p->md.dag_stream, types, 255) < 0) {
1007 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno));
1011 while (types[index]) {
1013 #elif defined HAVE_DAG_GET_ERF_TYPES
1014 /* Get list of possible ERF types for this card */
1015 if (dag_get_erf_types(p->fd, types, 255) < 0) {
1016 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
1020 while (types[index]) {
1022 /* Check the type through a dagapi call. */
1023 types[index] = dag_linktype(p->fd);
1027 switch((types[index] & 0x7f)) {
1030 case TYPE_COLOR_HDLC_POS:
1031 case TYPE_DSM_COLOR_HDLC_POS:
1032 case TYPE_COLOR_HASH_POS:
1034 if (p->dlt_list != NULL) {
1035 p->dlt_list[dlt_index++] = DLT_CHDLC;
1036 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1037 p->dlt_list[dlt_index++] = DLT_FRELAY;
1040 p->linktype = DLT_CHDLC;
1044 case TYPE_COLOR_ETH:
1045 case TYPE_DSM_COLOR_ETH:
1046 case TYPE_COLOR_HASH_ETH:
1048 * This is (presumably) a real Ethernet capture; give it a
1049 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1050 * that an application can let you choose it, in case you're
1051 * capturing DOCSIS traffic that a Cisco Cable Modem
1052 * Termination System is putting out onto an Ethernet (it
1053 * doesn't put an Ethernet header onto the wire, it puts raw
1054 * DOCSIS frames out on the wire inside the low-level
1055 * Ethernet framing).
1057 if (p->dlt_list != NULL) {
1058 p->dlt_list[dlt_index++] = DLT_EN10MB;
1059 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1062 p->linktype = DLT_EN10MB;
1069 if (p->dlt_list != NULL) {
1070 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1071 p->dlt_list[dlt_index++] = DLT_SUNATM;
1074 p->linktype = DLT_ATM_RFC1483;
1077 case TYPE_COLOR_MC_HDLC_POS:
1079 if (p->dlt_list != NULL) {
1080 p->dlt_list[dlt_index++] = DLT_CHDLC;
1081 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1082 p->dlt_list[dlt_index++] = DLT_FRELAY;
1083 p->dlt_list[dlt_index++] = DLT_MTP2;
1084 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1085 p->dlt_list[dlt_index++] = DLT_LAPD;
1088 p->linktype = DLT_CHDLC;
1093 p->linktype = DLT_RAW;
1098 case TYPE_MC_RAW_CHANNEL:
1099 case TYPE_IP_COUNTER:
1100 case TYPE_TCP_FLOW_COUNTER:
1101 case TYPE_INFINIBAND:
1104 /* Libpcap cannot deal with these types yet */
1105 /* Add no DLTs, but still covered by DLT_ERF */
1112 p->dlt_list[dlt_index++] = DLT_ERF;
1114 p->dlt_count = dlt_index;
1117 p->linktype = DLT_ERF;