2 * pcap-dag.c: Packet capture interface for Emulex EndaceDAG cards.
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
15 * Stephen Donnelly <stephen.donnelly@emulex.com>
22 #include <sys/param.h> /* optionally get BSD define */
31 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
37 struct mbuf; /* Squelch compiler warnings on some platforms for */
38 struct rtentry; /* declarations in <net/if.h> */
48 * DAG devices have names beginning with "dag", followed by a number
49 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
50 * from 0 to DAG_STREAM_MAX.
52 #ifndef DAG_MAX_BOARDS
53 #define DAG_MAX_BOARDS 32
62 #define TYPE_MC_HDLC 5
73 #ifndef TYPE_MC_RAW_CHANNEL
74 #define TYPE_MC_RAW_CHANNEL 8
78 #define TYPE_MC_AAL5 9
81 #ifndef TYPE_COLOR_HDLC_POS
82 #define TYPE_COLOR_HDLC_POS 10
85 #ifndef TYPE_COLOR_ETH
86 #define TYPE_COLOR_ETH 11
90 #define TYPE_MC_AAL2 12
93 #ifndef TYPE_IP_COUNTER
94 #define TYPE_IP_COUNTER 13
97 #ifndef TYPE_TCP_FLOW_COUNTER
98 #define TYPE_TCP_FLOW_COUNTER 14
101 #ifndef TYPE_DSM_COLOR_HDLC_POS
102 #define TYPE_DSM_COLOR_HDLC_POS 15
105 #ifndef TYPE_DSM_COLOR_ETH
106 #define TYPE_DSM_COLOR_ETH 16
109 #ifndef TYPE_COLOR_MC_HDLC_POS
110 #define TYPE_COLOR_MC_HDLC_POS 17
117 #ifndef TYPE_COLOR_HASH_POS
118 #define TYPE_COLOR_HASH_POS 19
121 #ifndef TYPE_COLOR_HASH_ETH
122 #define TYPE_COLOR_HASH_ETH 20
125 #ifndef TYPE_INFINIBAND
126 #define TYPE_INFINIBAND 21
137 #ifndef TYPE_RAW_LINK
138 #define TYPE_RAW_LINK 24
141 #ifndef TYPE_INFINIBAND_LINK
142 #define TYPE_INFINIBAND_LINK 25
149 #define ATM_CELL_SIZE 52
150 #define ATM_HDR_SIZE 4
153 * A header containing additional MTP information.
155 #define MTP2_SENT_OFFSET 0 /* 1 byte */
156 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
157 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
158 #define MTP2_HDR_LEN 4 /* length of the header */
160 #define MTP2_ANNEX_A_NOT_USED 0
161 #define MTP2_ANNEX_A_USED 1
162 #define MTP2_ANNEX_A_USED_UNKNOWN 2
164 /* SunATM pseudo header */
166 unsigned char flags; /* destination and traffic type */
167 unsigned char vpi; /* VPI */
168 unsigned short vci; /* VCI */
172 * Private data for capturing on DAG devices.
175 struct pcap_stat stat;
176 #ifdef HAVE_DAG_STREAMS_API
177 u_char *dag_mem_bottom; /* DAG card current memory bottom pointer */
178 u_char *dag_mem_top; /* DAG card current memory top pointer */
179 #else /* HAVE_DAG_STREAMS_API */
180 void *dag_mem_base; /* DAG card memory base address */
181 u_int dag_mem_bottom; /* DAG card current memory bottom offset */
182 u_int dag_mem_top; /* DAG card current memory top offset */
183 #endif /* HAVE_DAG_STREAMS_API */
184 int dag_fcs_bits; /* Number of checksum bits from link layer */
185 int dag_offset_flags; /* Flags to pass to dag_offset(). */
186 int dag_stream; /* DAG stream number */
187 int dag_timeout; /* timeout specified to pcap_open_live.
188 * Same as in linux above, introduce
192 typedef struct pcap_dag_node {
193 struct pcap_dag_node *next;
198 static pcap_dag_node_t *pcap_dags = NULL;
199 static int atexit_handler_installed = 0;
200 static const unsigned short endian_test_word = 0x0100;
202 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
204 #define MAX_DAG_PACKET 65536
206 static unsigned char TempPkt[MAX_DAG_PACKET];
208 static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
209 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
210 static int dag_set_datalink(pcap_t *p, int dlt);
211 static int dag_get_datalink(pcap_t *p);
212 static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
215 delete_pcap_dag(pcap_t *p)
217 pcap_dag_node_t *curr = NULL, *prev = NULL;
219 for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
223 if (curr != NULL && curr->p == p) {
225 prev->next = curr->next;
227 pcap_dags = curr->next;
233 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
234 * in the pcap_t structure, and closes the file descriptor for the DAG card.
238 dag_platform_cleanup(pcap_t *p)
240 struct pcap_dag *pd = p->pr;
242 #ifdef HAVE_DAG_STREAMS_API
243 if(dag_stop_stream(p->fd, pd->dag_stream) < 0)
244 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
246 if(dag_detach_stream(p->fd, pd->dag_stream) < 0)
247 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
249 if(dag_stop(p->fd) < 0)
250 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
251 #endif /* HAVE_DAG_STREAMS_API */
253 if(dag_close(p->fd) < 0)
254 fprintf(stderr,"dag_close: %s\n", strerror(errno));
258 pcap_cleanup_live_common(p);
259 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
265 while (pcap_dags != NULL) {
266 if (pcap_dags->pid == getpid()) {
267 if (pcap_dags->p != NULL)
268 dag_platform_cleanup(pcap_dags->p);
270 delete_pcap_dag(pcap_dags->p);
276 new_pcap_dag(pcap_t *p)
278 pcap_dag_node_t *node = NULL;
280 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
284 if (!atexit_handler_installed) {
285 atexit(atexit_handler);
286 atexit_handler_installed = 1;
289 node->next = pcap_dags;
291 node->pid = getpid();
299 dag_erf_ext_header_count(uint8_t * erf, size_t len)
301 uint32_t hdr_num = 0;
304 /* basic sanity checks */
310 /* check if we have any extension headers */
311 if ( (erf[8] & 0x80) == 0x00 )
314 /* loop over the extension headers */
317 /* sanity check we have enough bytes */
318 if ( len < (24 + (hdr_num * 8)) )
321 /* get the header type */
322 hdr_type = erf[(16 + (hdr_num * 8))];
325 } while ( hdr_type & 0x80 );
331 * Read at most max_packets from the capture stream and call the callback
332 * for each of them. Returns the number of packets handled, -1 if an
333 * error occured, or -2 if we were told to break out of the loop.
336 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
338 struct pcap_dag *pd = p->priv;
339 unsigned int processed = 0;
340 int flags = pd->dag_offset_flags;
341 unsigned int nonblocking = flags & DAGF_NONBLOCK;
342 unsigned int num_ext_hdr = 0;
343 unsigned int ticks_per_second;
345 /* Get the next bufferful of packets (if necessary). */
346 while (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size) {
349 * Has "pcap_breakloop()" been called?
353 * Yes - clear the flag that indicates that
354 * it has, and return -2 to indicate that
355 * we were told to break out of the loop.
361 #ifdef HAVE_DAG_STREAMS_API
362 /* dag_advance_stream() will block (unless nonblock is called)
363 * until 64kB of data has accumulated.
364 * If to_ms is set, it will timeout before 64kB has accumulated.
365 * We wait for 64kB because processing a few packets at a time
366 * can cause problems at high packet rates (>200kpps) due
368 * This does mean if to_ms is not specified the capture may 'hang'
369 * for long periods if the data rate is extremely slow (<64kB/sec)
370 * If non-block is specified it will return immediately. The user
371 * is then responsible for efficiency.
373 if ( NULL == (pd->dag_mem_top = dag_advance_stream(p->fd, pd->dag_stream, &(pd->dag_mem_bottom))) ) {
377 /* dag_offset does not support timeouts */
378 pd->dag_mem_top = dag_offset(p->fd, &(pd->dag_mem_bottom), flags);
379 #endif /* HAVE_DAG_STREAMS_API */
381 if (nonblocking && (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
383 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
389 (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
391 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
397 /* Process the packets. */
398 while (pd->dag_mem_top - pd->dag_mem_bottom >= dag_record_size) {
400 unsigned short packet_len = 0;
402 struct pcap_pkthdr pcap_header;
404 #ifdef HAVE_DAG_STREAMS_API
405 dag_record_t *header = (dag_record_t *)(pd->dag_mem_bottom);
407 dag_record_t *header = (dag_record_t *)(pd->dag_mem_base + pd->dag_mem_bottom);
408 #endif /* HAVE_DAG_STREAMS_API */
410 u_char *dp = ((u_char *)header); /* + dag_record_size; */
414 * Has "pcap_breakloop()" been called?
418 * Yes - clear the flag that indicates that
419 * it has, and return -2 to indicate that
420 * we were told to break out of the loop.
426 rlen = ntohs(header->rlen);
427 if (rlen < dag_record_size)
429 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
432 pd->dag_mem_bottom += rlen;
434 /* Count lost packets. */
435 switch((header->type & 0x7f)) {
436 /* in these types the color value overwrites the lctr */
437 case TYPE_COLOR_HDLC_POS:
439 case TYPE_DSM_COLOR_HDLC_POS:
440 case TYPE_DSM_COLOR_ETH:
441 case TYPE_COLOR_MC_HDLC_POS:
442 case TYPE_COLOR_HASH_ETH:
443 case TYPE_COLOR_HASH_POS:
448 if (pd->stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
449 pd->stat.ps_drop = UINT_MAX;
451 pd->stat.ps_drop += ntohs(header->lctr);
456 if ((header->type & 0x7f) == TYPE_PAD) {
460 num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
462 /* ERF encapsulation */
463 /* The Extensible Record Format is not dropped for this kind of encapsulation,
464 * and will be handled as a pseudo header by the decoding application.
465 * The information carried in the ERF header and in the optional subheader (if present)
466 * could be merged with the libpcap information, to offer a better decoding.
467 * The packet length is
468 * o the length of the packet on the link (header->wlen),
469 * o plus the length of the ERF header (dag_record_size), as the length of the
470 * pseudo header will be adjusted during the decoding,
471 * o plus the length of the optional subheader (if present).
473 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
474 * if the capture length is greater than the packet length.
476 if (p->linktype == DLT_ERF) {
477 packet_len = ntohs(header->wlen) + dag_record_size;
479 switch ((header->type & 0x7f)) {
483 case TYPE_MC_RAW_CHANNEL:
486 case TYPE_COLOR_MC_HDLC_POS:
487 packet_len += 4; /* MC header */
490 case TYPE_COLOR_HASH_ETH:
491 case TYPE_DSM_COLOR_ETH:
494 packet_len += 2; /* ETH header */
498 /* Include ERF extension headers */
499 packet_len += (8 * num_ext_hdr);
501 if (caplen > packet_len) {
505 /* Other kind of encapsulation according to the header Type */
507 /* Skip over generic ERF header */
508 dp += dag_record_size;
509 /* Skip over extension headers */
510 dp += 8 * num_ext_hdr;
512 switch((header->type & 0x7f)) {
515 if (header->type == TYPE_AAL5) {
516 packet_len = ntohs(header->wlen);
517 caplen = rlen - dag_record_size;
520 if (header->type == TYPE_MC_ATM) {
521 caplen = packet_len = ATM_CELL_SIZE;
525 if (header->type == TYPE_MC_AAL5) {
526 packet_len = ntohs(header->wlen);
527 caplen = rlen - dag_record_size - 4;
530 /* Skip over extension headers */
531 caplen -= (8 * num_ext_hdr);
533 if (header->type == TYPE_ATM) {
534 caplen = packet_len = ATM_CELL_SIZE;
536 if (p->linktype == DLT_SUNATM) {
537 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
538 unsigned long rawatm;
540 rawatm = ntohl(*((unsigned long *)dp));
541 sunatm->vci = htons((rawatm >> 4) & 0xffff);
542 sunatm->vpi = (rawatm >> 20) & 0x00ff;
543 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
544 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
545 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
546 ((dp[ATM_HDR_SIZE] == 0xaa &&
547 dp[ATM_HDR_SIZE+1] == 0xaa &&
548 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
551 packet_len -= ATM_HDR_SIZE;
552 caplen -= ATM_HDR_SIZE;
557 case TYPE_COLOR_HASH_ETH:
558 case TYPE_DSM_COLOR_ETH:
561 packet_len = ntohs(header->wlen);
562 packet_len -= (pd->dag_fcs_bits >> 3);
563 caplen = rlen - dag_record_size - 2;
564 /* Skip over extension headers */
565 caplen -= (8 * num_ext_hdr);
566 if (caplen > packet_len) {
572 case TYPE_COLOR_HASH_POS:
573 case TYPE_DSM_COLOR_HDLC_POS:
574 case TYPE_COLOR_HDLC_POS:
576 packet_len = ntohs(header->wlen);
577 packet_len -= (pd->dag_fcs_bits >> 3);
578 caplen = rlen - dag_record_size;
579 /* Skip over extension headers */
580 caplen -= (8 * num_ext_hdr);
581 if (caplen > packet_len) {
586 case TYPE_COLOR_MC_HDLC_POS:
588 packet_len = ntohs(header->wlen);
589 packet_len -= (pd->dag_fcs_bits >> 3);
590 caplen = rlen - dag_record_size - 4;
591 /* Skip over extension headers */
592 caplen -= (8 * num_ext_hdr);
593 if (caplen > packet_len) {
596 /* jump the MC_HDLC_HEADER */
598 #ifdef DLT_MTP2_WITH_PHDR
599 if (p->linktype == DLT_MTP2_WITH_PHDR) {
600 /* Add the MTP2 Pseudo Header */
601 caplen += MTP2_HDR_LEN;
602 packet_len += MTP2_HDR_LEN;
604 TempPkt[MTP2_SENT_OFFSET] = 0;
605 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
606 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
607 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
608 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
616 packet_len = ntohs(header->wlen);
617 caplen = rlen - dag_record_size;
618 /* Skip over extension headers */
619 caplen -= (8 * num_ext_hdr);
620 if (caplen > packet_len) {
625 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
627 case TYPE_MC_RAW_CHANNEL:
628 case TYPE_IP_COUNTER:
629 case TYPE_TCP_FLOW_COUNTER:
630 case TYPE_INFINIBAND:
632 case TYPE_INFINIBAND_LINK:
634 /* Unhandled ERF type.
635 * Ignore rather than generating error
640 } /* ERF encapsulation */
642 if (caplen > p->snapshot)
643 caplen = p->snapshot;
645 /* Run the packet filter if there is one. */
646 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
648 /* convert between timestamp formats */
649 register unsigned long long ts;
651 if (IS_BIGENDIAN()) {
652 ts = SWAPLL(header->ts);
657 switch (p->opt.tstamp_precision) {
658 case PCAP_TSTAMP_PRECISION_NANO:
659 ticks_per_second = 1000000000;
661 case PCAP_TSTAMP_PRECISION_MICRO:
663 ticks_per_second = 1000000;
667 pcap_header.ts.tv_sec = ts >> 32;
668 ts = (ts & 0xffffffffULL) * ticks_per_second;
669 ts += 0x80000000; /* rounding */
670 pcap_header.ts.tv_usec = ts >> 32;
671 if (pcap_header.ts.tv_usec >= ticks_per_second) {
672 pcap_header.ts.tv_usec -= ticks_per_second;
673 pcap_header.ts.tv_sec++;
676 /* Fill in our own header data */
677 pcap_header.caplen = caplen;
678 pcap_header.len = packet_len;
680 /* Count the packet. */
683 /* Call the user supplied callback function */
684 callback(user, &pcap_header, dp);
686 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
688 if (processed == cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
690 /* Reached the user-specified limit. */
700 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
702 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
708 * Get a handle for a live capture from the given DAG device. Passing a NULL
709 * device will result in a failure. The promisc flag is ignored because DAG
710 * cards are always promiscuous. The to_ms parameter is used in setting the
711 * API polling parameters.
713 * snaplen is now also ignored, until we get per-stream slen support. Set
714 * slen with approprite DAG tool BEFORE pcap_activate().
718 static int dag_activate(pcap_t* handle)
720 struct pcap_dag *handlep = handle->priv;
722 char conf[30]; /* dag configure string */
727 char * newDev = NULL;
728 char * device = handle->opt.device;
729 #ifdef HAVE_DAG_STREAMS_API
731 struct timeval maxwait;
735 if (device == NULL) {
736 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
740 /* Initialize some components of the pcap structure. */
742 #ifdef HAVE_DAG_STREAMS_API
743 newDev = (char *)malloc(strlen(device) + 16);
744 if (newDev == NULL) {
745 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s", pcap_strerror(errno));
749 /* Parse input name to get dag device and stream number if provided */
750 if (dag_parse_name(device, newDev, strlen(device) + 16, &handlep->dag_stream) < 0) {
751 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s", pcap_strerror(errno));
756 if (handlep->dag_stream%2) {
757 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture");
761 if (strncmp(device, "/dev/", 5) != 0) {
762 newDev = (char *)malloc(strlen(device) + 5);
763 if (newDev == NULL) {
764 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s", pcap_strerror(errno));
767 strcpy(newDev, "/dev/");
768 strcat(newDev, device);
771 #endif /* HAVE_DAG_STREAMS_API */
773 /* setup device parameters */
774 if((handle->fd = dag_open((char *)device)) < 0) {
775 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
779 #ifdef HAVE_DAG_STREAMS_API
780 /* Open requested stream. Can fail if already locked or on error */
781 if (dag_attach_stream(handle->fd, handlep->dag_stream, 0, 0) < 0) {
782 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s", pcap_strerror(errno));
786 /* Set up default poll parameters for stream
787 * Can be overridden by pcap_set_nonblock()
789 if (dag_get_stream_poll(handle->fd, handlep->dag_stream,
790 &mindata, &maxwait, &poll) < 0) {
791 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno));
795 if (handle->opt.immediate) {
796 /* Call callback immediately.
797 * XXX - is this the right way to handle this?
801 /* Amount of data to collect in Bytes before calling callbacks.
802 * Important for efficiency, but can introduce latency
803 * at low packet rates if to_ms not set!
808 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
809 * Recommend 10-100ms. Calls will time out even if no data arrived.
811 maxwait.tv_sec = handle->opt.timeout/1000;
812 maxwait.tv_usec = (handle->opt.timeout%1000) * 1000;
814 if (dag_set_stream_poll(handle->fd, handlep->dag_stream,
815 mindata, &maxwait, &poll) < 0) {
816 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno));
821 if((handlep->dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
822 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s", device, pcap_strerror(errno));
826 #endif /* HAVE_DAG_STREAMS_API */
828 /* XXX Not calling dag_configure() to set slen; this is unsafe in
829 * multi-stream environments as the gpp config is global.
830 * Once the firmware provides 'per-stream slen' this can be supported
831 * again via the Config API without side-effects */
833 /* set the card snap length to the specified snaplen parameter */
834 /* This is a really bad idea, as different cards have different
835 * valid slen ranges. Should fix in Config API. */
836 if (handle->snapshot == 0 || handle->snapshot > MAX_DAG_SNAPLEN) {
837 handle->snapshot = MAX_DAG_SNAPLEN;
838 } else if (snaplen < MIN_DAG_SNAPLEN) {
839 handle->snapshot = MIN_DAG_SNAPLEN;
841 /* snap len has to be a multiple of 4 */
842 pcap_snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
844 if(dag_configure(handle->fd, conf) < 0) {
845 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s", device, pcap_strerror(errno));
850 #ifdef HAVE_DAG_STREAMS_API
851 if(dag_start_stream(handle->fd, handlep->dag_stream) < 0) {
852 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s", device, pcap_strerror(errno));
856 if(dag_start(handle->fd) < 0) {
857 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s", device, pcap_strerror(errno));
860 #endif /* HAVE_DAG_STREAMS_API */
863 * Important! You have to ensure bottom is properly
864 * initialized to zero on startup, it won't give you
865 * a compiler warning if you make this mistake!
867 handlep->dag_mem_bottom = 0;
868 handlep->dag_mem_top = 0;
871 * Find out how many FCS bits we should strip.
872 * First, query the card to see if it strips the FCS.
874 daginf = dag_info(handle->fd);
875 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) {
876 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
877 handlep->dag_fcs_bits = 0;
879 /* Note that no FCS will be supplied. */
880 handle->linktype_ext = LT_FCS_DATALINK_EXT(0);
883 * Start out assuming it's 32 bits.
885 handlep->dag_fcs_bits = 32;
887 /* Allow an environment variable to override. */
888 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
889 if ((n = atoi(s)) == 0 || n == 16 || n == 32) {
890 handlep->dag_fcs_bits = n;
892 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
893 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment", device, n);
899 * Did the user request that they not be stripped?
901 if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) {
902 /* Yes. Note the number of bytes that will be
904 handle->linktype_ext = LT_FCS_DATALINK_EXT(handlep->dag_fcs_bits/16);
906 /* And don't strip them. */
907 handlep->dag_fcs_bits = 0;
911 handlep->dag_timeout = handle->opt.timeout;
913 handle->linktype = -1;
914 if (dag_get_datalink(handle) < 0)
919 if (new_pcap_dag(handle) < 0) {
920 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s", device, pcap_strerror(errno));
925 * "select()" and "poll()" don't work on DAG device descriptors.
927 handle->selectable_fd = -1;
929 if (newDev != NULL) {
930 free((char *)newDev);
933 handle->read_op = dag_read;
934 handle->inject_op = dag_inject;
935 handle->setfilter_op = dag_setfilter;
936 handle->setdirection_op = NULL; /* Not implemented.*/
937 handle->set_datalink_op = dag_set_datalink;
938 handle->getnonblock_op = pcap_getnonblock_fd;
939 handle->setnonblock_op = dag_setnonblock;
940 handle->stats_op = dag_stats;
941 handle->cleanup_op = dag_platform_cleanup;
942 handlep->stat.ps_drop = 0;
943 handlep->stat.ps_recv = 0;
944 handlep->stat.ps_ifdrop = 0;
947 #ifdef HAVE_DAG_STREAMS_API
949 if (dag_stop_stream(handle->fd, handlep->dag_stream) < 0) {
950 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
954 if (dag_detach_stream(handle->fd, handlep->dag_stream) < 0)
955 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
958 if (dag_stop(handle->fd) < 0)
959 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
960 #endif /* HAVE_DAG_STREAMS_API */
963 if (dag_close(handle->fd) < 0)
964 fprintf(stderr,"dag_close: %s\n", strerror(errno));
965 delete_pcap_dag(handle);
968 pcap_cleanup_live_common(handle);
969 if (newDev != NULL) {
970 free((char *)newDev);
976 pcap_t *dag_create(const char *device, char *ebuf, int *is_ours)
982 #ifdef HAVE_DAG_STREAMS_API
986 /* Does this look like a DAG device? */
987 cp = strrchr(device, '/');
990 /* Does it begin with "dag"? */
991 if (strncmp(cp, "dag", 3) != 0) {
992 /* Nope, doesn't begin with "dag" */
996 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
998 devnum = strtol(cp, &cpend, 10);
999 #ifdef HAVE_DAG_STREAMS_API
1000 if (*cpend == ':') {
1001 /* Followed by a stream number. */
1002 stream = strtol(++cpend, &cpend, 10);
1005 if (cpend == cp || *cpend != '\0') {
1006 /* Not followed by a number. */
1010 if (devnum < 0 || devnum >= DAG_MAX_BOARDS) {
1011 /* Followed by a non-valid number. */
1015 #ifdef HAVE_DAG_STREAMS_API
1016 if (stream <0 || stream >= DAG_STREAM_MAX) {
1017 /* Followed by a non-valid stream number. */
1023 /* OK, it's probably ours. */
1026 p = pcap_create_common(ebuf, sizeof (struct pcap_dag));
1030 p->activate_op = dag_activate;
1033 * We claim that we support microsecond and nanosecond time
1036 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1037 * power of two precisions yet. We can convert to either MICRO or NANO.
1039 p->tstamp_precision_count = 2;
1040 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
1041 if (p->tstamp_precision_list == NULL) {
1042 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
1043 pcap_strerror(errno));
1047 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
1048 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
1053 dag_stats(pcap_t *p, struct pcap_stat *ps) {
1054 struct pcap_dag *pd = p->priv;
1056 /* This needs to be filled out correctly. Hopefully a dagapi call will
1057 provide all necessary information.
1059 /*pd->stat.ps_recv = 0;*/
1060 /*pd->stat.ps_drop = 0;*/
1068 * Previously we just generated a list of all possible names and let
1069 * pcap_add_if() attempt to open each one, but with streams this adds up
1070 * to 81 possibilities which is inefficient.
1072 * Since we know more about the devices we can prune the tree here.
1073 * pcap_add_if() will still retest each device but the total number of
1074 * open attempts will still be much less than the naive approach.
1077 dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
1079 char name[12]; /* XXX - pick a size */
1082 char dagname[DAGNAME_BUFSIZE];
1085 dag_card_inf_t *inf;
1088 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1089 for (c = 0; c < DAG_MAX_BOARDS; c++) {
1090 pcap_snprintf(name, 12, "dag%d", c);
1091 if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
1096 if ( (dagfd = dag_open(dagname)) >= 0 ) {
1097 if ((inf = dag_pciinfo(dagfd)))
1098 description = dag_device_name(inf->device_code, 1);
1099 if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) {
1105 #ifdef HAVE_DAG_STREAMS_API
1107 int stream, rxstreams;
1108 rxstreams = dag_rx_get_stream_count(dagfd);
1109 for(stream=0;stream<DAG_STREAM_MAX;stream+=2) {
1110 if (0 == dag_attach_stream(dagfd, stream, 0, 0)) {
1111 dag_detach_stream(dagfd, stream);
1113 pcap_snprintf(name, 10, "dag%d:%d", c, stream);
1114 if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) {
1122 if(rxstreams <= 0) {
1128 #endif /* HAVE_DAG_STREAMS_API */
1137 * Installs the given bpf filter program in the given pcap structure. There is
1138 * no attempt to store the filter in kernel memory as that is not supported
1142 dag_setfilter(pcap_t *p, struct bpf_program *fp)
1147 strncpy(p->errbuf, "setfilter: No filter specified",
1152 /* Make our private copy of the filter */
1154 if (install_bpf_program(p, fp) < 0)
1161 dag_set_datalink(pcap_t *p, int dlt)
1169 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1171 struct pcap_dag *pd = p->priv;
1174 * Set non-blocking mode on the FD.
1175 * XXX - is that necessary? If not, don't bother calling it,
1176 * and have a "dag_getnonblock()" function that looks at
1177 * "pd->dag_offset_flags".
1179 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
1181 #ifdef HAVE_DAG_STREAMS_API
1184 struct timeval maxwait;
1185 struct timeval poll;
1187 if (dag_get_stream_poll(p->fd, pd->dag_stream,
1188 &mindata, &maxwait, &poll) < 0) {
1189 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno));
1193 /* Amount of data to collect in Bytes before calling callbacks.
1194 * Important for efficiency, but can introduce latency
1195 * at low packet rates if to_ms not set!
1202 if (dag_set_stream_poll(p->fd, pd->dag_stream,
1203 mindata, &maxwait, &poll) < 0) {
1204 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno));
1208 #endif /* HAVE_DAG_STREAMS_API */
1210 pd->dag_offset_flags |= DAGF_NONBLOCK;
1212 pd->dag_offset_flags &= ~DAGF_NONBLOCK;
1218 dag_get_datalink(pcap_t *p)
1220 struct pcap_dag *pd = p->priv;
1221 int index=0, dlt_index=0;
1224 memset(types, 0, 255);
1226 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
1227 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
1233 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1234 /* Get list of possible ERF types for this card */
1235 if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) {
1236 pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno));
1240 while (types[index]) {
1242 #elif defined HAVE_DAG_GET_ERF_TYPES
1243 /* Get list of possible ERF types for this card */
1244 if (dag_get_erf_types(p->fd, types, 255) < 0) {
1245 pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
1249 while (types[index]) {
1251 /* Check the type through a dagapi call. */
1252 types[index] = dag_linktype(p->fd);
1256 switch((types[index] & 0x7f)) {
1259 case TYPE_COLOR_HDLC_POS:
1260 case TYPE_DSM_COLOR_HDLC_POS:
1261 case TYPE_COLOR_HASH_POS:
1263 if (p->dlt_list != NULL) {
1264 p->dlt_list[dlt_index++] = DLT_CHDLC;
1265 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1266 p->dlt_list[dlt_index++] = DLT_FRELAY;
1269 p->linktype = DLT_CHDLC;
1273 case TYPE_COLOR_ETH:
1274 case TYPE_DSM_COLOR_ETH:
1275 case TYPE_COLOR_HASH_ETH:
1277 * This is (presumably) a real Ethernet capture; give it a
1278 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1279 * that an application can let you choose it, in case you're
1280 * capturing DOCSIS traffic that a Cisco Cable Modem
1281 * Termination System is putting out onto an Ethernet (it
1282 * doesn't put an Ethernet header onto the wire, it puts raw
1283 * DOCSIS frames out on the wire inside the low-level
1284 * Ethernet framing).
1286 if (p->dlt_list != NULL) {
1287 p->dlt_list[dlt_index++] = DLT_EN10MB;
1288 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1291 p->linktype = DLT_EN10MB;
1298 if (p->dlt_list != NULL) {
1299 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1300 p->dlt_list[dlt_index++] = DLT_SUNATM;
1303 p->linktype = DLT_ATM_RFC1483;
1306 case TYPE_COLOR_MC_HDLC_POS:
1308 if (p->dlt_list != NULL) {
1309 p->dlt_list[dlt_index++] = DLT_CHDLC;
1310 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1311 p->dlt_list[dlt_index++] = DLT_FRELAY;
1312 p->dlt_list[dlt_index++] = DLT_MTP2;
1313 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1314 p->dlt_list[dlt_index++] = DLT_LAPD;
1317 p->linktype = DLT_CHDLC;
1323 p->linktype = DLT_RAW;
1328 case TYPE_MC_RAW_CHANNEL:
1329 case TYPE_IP_COUNTER:
1330 case TYPE_TCP_FLOW_COUNTER:
1331 case TYPE_INFINIBAND:
1333 case TYPE_INFINIBAND_LINK:
1335 /* Libpcap cannot deal with these types yet */
1336 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1343 p->dlt_list[dlt_index++] = DLT_ERF;
1345 p->dlt_count = dlt_index;
1348 p->linktype = DLT_ERF;
1355 * This libpcap build supports only DAG cards, not regular network
1360 * There are no regular interfaces, just DAG interfaces.
1363 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
1370 * Attempts to open a regular interface fail.
1373 pcap_create_interface(const char *device, char *errbuf)
1375 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1376 "This version of libpcap only supports DAG cards");