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> */
44 #include "dag_config_api.h"
49 * DAG devices have names beginning with "dag", followed by a number
50 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
51 * from 0 to DAG_STREAM_MAX.
53 #ifndef DAG_MAX_BOARDS
54 #define DAG_MAX_BOARDS 32
59 #define ERF_TYPE_AAL5 4
62 #ifndef ERF_TYPE_MC_HDLC
63 #define ERF_TYPE_MC_HDLC 5
66 #ifndef ERF_TYPE_MC_RAW
67 #define ERF_TYPE_MC_RAW 6
70 #ifndef ERF_TYPE_MC_ATM
71 #define ERF_TYPE_MC_ATM 7
74 #ifndef ERF_TYPE_MC_RAW_CHANNEL
75 #define ERF_TYPE_MC_RAW_CHANNEL 8
78 #ifndef ERF_TYPE_MC_AAL5
79 #define ERF_TYPE_MC_AAL5 9
82 #ifndef ERF_TYPE_COLOR_HDLC_POS
83 #define ERF_TYPE_COLOR_HDLC_POS 10
86 #ifndef ERF_TYPE_COLOR_ETH
87 #define ERF_TYPE_COLOR_ETH 11
90 #ifndef ERF_TYPE_MC_AAL2
91 #define ERF_TYPE_MC_AAL2 12
94 #ifndef ERF_TYPE_IP_COUNTER
95 #define ERF_TYPE_IP_COUNTER 13
98 #ifndef ERF_TYPE_TCP_FLOW_COUNTER
99 #define ERF_TYPE_TCP_FLOW_COUNTER 14
102 #ifndef ERF_TYPE_DSM_COLOR_HDLC_POS
103 #define ERF_TYPE_DSM_COLOR_HDLC_POS 15
106 #ifndef ERF_TYPE_DSM_COLOR_ETH
107 #define ERF_TYPE_DSM_COLOR_ETH 16
110 #ifndef ERF_TYPE_COLOR_MC_HDLC_POS
111 #define ERF_TYPE_COLOR_MC_HDLC_POS 17
114 #ifndef ERF_TYPE_AAL2
115 #define ERF_TYPE_AAL2 18
118 #ifndef ERF_TYPE_COLOR_HASH_POS
119 #define ERF_TYPE_COLOR_HASH_POS 19
122 #ifndef ERF_TYPE_COLOR_HASH_ETH
123 #define ERF_TYPE_COLOR_HASH_ETH 20
126 #ifndef ERF_TYPE_INFINIBAND
127 #define ERF_TYPE_INFINIBAND 21
130 #ifndef ERF_TYPE_IPV4
131 #define ERF_TYPE_IPV4 22
134 #ifndef ERF_TYPE_IPV6
135 #define ERF_TYPE_IPV6 23
138 #ifndef ERF_TYPE_RAW_LINK
139 #define ERF_TYPE_RAW_LINK 24
142 #ifndef ERF_TYPE_INFINIBAND_LINK
143 #define ERF_TYPE_INFINIBAND_LINK 25
146 #ifndef ERF_TYPE_META
147 #define ERF_TYPE_META 27
151 #define ERF_TYPE_PAD 48
154 #define ATM_CELL_SIZE 52
155 #define ATM_HDR_SIZE 4
158 * A header containing additional MTP information.
160 #define MTP2_SENT_OFFSET 0 /* 1 byte */
161 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
162 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
163 #define MTP2_HDR_LEN 4 /* length of the header */
165 #define MTP2_ANNEX_A_NOT_USED 0
166 #define MTP2_ANNEX_A_USED 1
167 #define MTP2_ANNEX_A_USED_UNKNOWN 2
169 /* SunATM pseudo header */
171 unsigned char flags
; /* destination and traffic type */
172 unsigned char vpi
; /* VPI */
173 unsigned short vci
; /* VCI */
177 * Private data for capturing on DAG devices.
180 struct pcap_stat stat
;
181 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
182 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
183 int dag_fcs_bits
; /* Number of checksum bits from link layer */
184 int dag_flags
; /* Flags */
185 int dag_stream
; /* DAG stream number */
186 int dag_timeout
; /* timeout specified to pcap_open_live.
187 * Same as in linux above, introduce
189 dag_card_ref_t dag_ref
; /* DAG Configuration/Status API card reference */
190 dag_component_t dag_root
; /* DAG CSAPI Root component */
191 attr_uuid_t drop_attr
; /* DAG Stream Drop Attribute handle, if available */
192 struct timeval required_select_timeout
;
193 /* Timeout caller must use in event loops */
196 typedef struct pcap_dag_node
{
197 struct pcap_dag_node
*next
;
202 static pcap_dag_node_t
*pcap_dags
= NULL
;
203 static int atexit_handler_installed
= 0;
204 static const unsigned short endian_test_word
= 0x0100;
206 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
208 #define MAX_DAG_PACKET 65536
210 static unsigned char TempPkt
[MAX_DAG_PACKET
];
212 #ifndef HAVE_DAG_LARGE_STREAMS_API
213 #define dag_attach_stream64(a, b, c, d) dag_attach_stream(a, b, c, d)
214 #define dag_get_stream_poll64(a, b, c, d, e) dag_get_stream_poll(a, b, c, d, e)
215 #define dag_set_stream_poll64(a, b, c, d, e) dag_set_stream_poll(a, b, c, d, e)
216 #define dag_size_t uint32_t
219 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
220 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
221 static int dag_set_datalink(pcap_t
*p
, int dlt
);
222 static int dag_get_datalink(pcap_t
*p
);
223 static int dag_setnonblock(pcap_t
*p
, int nonblock
);
226 delete_pcap_dag(pcap_t
*p
)
228 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
230 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
234 if (curr
!= NULL
&& curr
->p
== p
) {
236 prev
->next
= curr
->next
;
238 pcap_dags
= curr
->next
;
244 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
245 * in the pcap_t structure, and closes the file descriptor for the DAG card.
249 dag_platform_cleanup(pcap_t
*p
)
251 struct pcap_dag
*pd
= p
->priv
;
253 if(dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0)
254 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
256 if(dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
257 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
259 if(pd
->dag_ref
!= NULL
) {
260 dag_config_dispose(pd
->dag_ref
);
265 pcap_cleanup_live_common(p
);
266 /* Note: don't need to call close(p->fd) or dag_close(p->fd) as dag_config_dispose(pd->dag_ref) does this. */
272 while (pcap_dags
!= NULL
) {
273 if (pcap_dags
->pid
== getpid()) {
274 if (pcap_dags
->p
!= NULL
)
275 dag_platform_cleanup(pcap_dags
->p
);
277 delete_pcap_dag(pcap_dags
->p
);
283 new_pcap_dag(pcap_t
*p
)
285 pcap_dag_node_t
*node
= NULL
;
287 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
291 if (!atexit_handler_installed
) {
292 atexit(atexit_handler
);
293 atexit_handler_installed
= 1;
296 node
->next
= pcap_dags
;
298 node
->pid
= getpid();
306 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
308 uint32_t hdr_num
= 0;
311 /* basic sanity checks */
317 /* check if we have any extension headers */
318 if ( (erf
[8] & 0x80) == 0x00 )
321 /* loop over the extension headers */
324 /* sanity check we have enough bytes */
325 if ( len
< (24 + (hdr_num
* 8)) )
328 /* get the header type */
329 hdr_type
= erf
[(16 + (hdr_num
* 8))];
332 } while ( hdr_type
& 0x80 );
338 * Read at most max_packets from the capture stream and call the callback
339 * for each of them. Returns the number of packets handled, -1 if an
340 * error occured, or -2 if we were told to break out of the loop.
343 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
345 struct pcap_dag
*pd
= p
->priv
;
346 unsigned int processed
= 0;
347 unsigned int nonblocking
= pd
->dag_flags
& DAGF_NONBLOCK
;
348 unsigned int num_ext_hdr
= 0;
349 unsigned int ticks_per_second
;
351 /* Get the next bufferful of packets (if necessary). */
352 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
) {
355 * Has "pcap_breakloop()" been called?
359 * Yes - clear the flag that indicates that
360 * it has, and return -2 to indicate that
361 * we were told to break out of the loop.
367 /* dag_advance_stream() will block (unless nonblock is called)
368 * until 64kB of data has accumulated.
369 * If to_ms is set, it will timeout before 64kB has accumulated.
370 * We wait for 64kB because processing a few packets at a time
371 * can cause problems at high packet rates (>200kpps) due
373 * This does mean if to_ms is not specified the capture may 'hang'
374 * for long periods if the data rate is extremely slow (<64kB/sec)
375 * If non-block is specified it will return immediately. The user
376 * is then responsible for efficiency.
378 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
382 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
384 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
390 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
392 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
398 /* Process the packets. */
399 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
401 unsigned short packet_len
= 0;
403 struct pcap_pkthdr pcap_header
;
405 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
407 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
411 * Has "pcap_breakloop()" been called?
415 * Yes - clear the flag that indicates that
416 * it has, and return -2 to indicate that
417 * we were told to break out of the loop.
423 rlen
= ntohs(header
->rlen
);
424 if (rlen
< dag_record_size
)
426 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
429 pd
->dag_mem_bottom
+= rlen
;
431 /* Count lost packets. */
432 switch((header
->type
& 0x7f)) {
433 /* in these types the color value overwrites the lctr */
434 case ERF_TYPE_COLOR_HDLC_POS
:
435 case ERF_TYPE_COLOR_ETH
:
436 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
437 case ERF_TYPE_DSM_COLOR_ETH
:
438 case ERF_TYPE_COLOR_MC_HDLC_POS
:
439 case ERF_TYPE_COLOR_HASH_ETH
:
440 case ERF_TYPE_COLOR_HASH_POS
:
444 if ( (pd
->drop_attr
== kNullAttributeUuid
) && (header
->lctr
) ) {
445 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
449 if ((header
->type
& 0x7f) == ERF_TYPE_PAD
) {
453 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
455 /* ERF encapsulation */
456 /* The Extensible Record Format is not dropped for this kind of encapsulation,
457 * and will be handled as a pseudo header by the decoding application.
458 * The information carried in the ERF header and in the optional subheader (if present)
459 * could be merged with the libpcap information, to offer a better decoding.
460 * The packet length is
461 * o the length of the packet on the link (header->wlen),
462 * o plus the length of the ERF header (dag_record_size), as the length of the
463 * pseudo header will be adjusted during the decoding,
464 * o plus the length of the optional subheader (if present).
466 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
467 * if the capture length is greater than the packet length.
469 if (p
->linktype
== DLT_ERF
) {
470 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
472 switch ((header
->type
& 0x7f)) {
473 case ERF_TYPE_MC_AAL5
:
474 case ERF_TYPE_MC_ATM
:
475 case ERF_TYPE_MC_HDLC
:
476 case ERF_TYPE_MC_RAW_CHANNEL
:
477 case ERF_TYPE_MC_RAW
:
478 case ERF_TYPE_MC_AAL2
:
479 case ERF_TYPE_COLOR_MC_HDLC_POS
:
480 packet_len
+= 4; /* MC header */
483 case ERF_TYPE_COLOR_HASH_ETH
:
484 case ERF_TYPE_DSM_COLOR_ETH
:
485 case ERF_TYPE_COLOR_ETH
:
487 packet_len
+= 2; /* ETH header */
491 /* Include ERF extension headers */
492 packet_len
+= (8 * num_ext_hdr
);
494 if (caplen
> packet_len
) {
498 /* Other kind of encapsulation according to the header Type */
500 /* Skip over generic ERF header */
501 dp
+= dag_record_size
;
502 /* Skip over extension headers */
503 dp
+= 8 * num_ext_hdr
;
505 switch((header
->type
& 0x7f)) {
508 if ((header
->type
& 0x7f) == ERF_TYPE_AAL5
) {
509 packet_len
= ntohs(header
->wlen
);
510 caplen
= rlen
- dag_record_size
;
512 case ERF_TYPE_MC_ATM
:
513 if ((header
->type
& 0x7f) == ERF_TYPE_MC_ATM
) {
514 caplen
= packet_len
= ATM_CELL_SIZE
;
517 case ERF_TYPE_MC_AAL5
:
518 if ((header
->type
& 0x7f) == ERF_TYPE_MC_AAL5
) {
519 packet_len
= ntohs(header
->wlen
);
520 caplen
= rlen
- dag_record_size
- 4;
523 /* Skip over extension headers */
524 caplen
-= (8 * num_ext_hdr
);
526 if ((header
->type
& 0x7f) == ERF_TYPE_ATM
) {
527 caplen
= packet_len
= ATM_CELL_SIZE
;
529 if (p
->linktype
== DLT_SUNATM
) {
530 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
531 unsigned long rawatm
;
533 rawatm
= ntohl(*((unsigned long *)dp
));
534 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
535 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
536 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
537 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
538 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
539 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
540 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
541 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
543 } else if (p
->linktype
== DLT_ATM_RFC1483
) {
544 packet_len
-= ATM_HDR_SIZE
;
545 caplen
-= ATM_HDR_SIZE
;
551 case ERF_TYPE_COLOR_HASH_ETH
:
552 case ERF_TYPE_DSM_COLOR_ETH
:
553 case ERF_TYPE_COLOR_ETH
:
555 if ((p
->linktype
!= DLT_EN10MB
) &&
556 (p
->linktype
!= DLT_DOCSIS
))
558 packet_len
= ntohs(header
->wlen
);
559 packet_len
-= (pd
->dag_fcs_bits
>> 3);
560 caplen
= rlen
- dag_record_size
- 2;
561 /* Skip over extension headers */
562 caplen
-= (8 * num_ext_hdr
);
563 if (caplen
> packet_len
) {
569 case ERF_TYPE_COLOR_HASH_POS
:
570 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
571 case ERF_TYPE_COLOR_HDLC_POS
:
572 case ERF_TYPE_HDLC_POS
:
573 if ((p
->linktype
!= DLT_CHDLC
) &&
574 (p
->linktype
!= DLT_PPP_SERIAL
) &&
575 (p
->linktype
!= DLT_FRELAY
))
577 packet_len
= ntohs(header
->wlen
);
578 packet_len
-= (pd
->dag_fcs_bits
>> 3);
579 caplen
= rlen
- dag_record_size
;
580 /* Skip over extension headers */
581 caplen
-= (8 * num_ext_hdr
);
582 if (caplen
> packet_len
) {
587 case ERF_TYPE_COLOR_MC_HDLC_POS
:
588 case ERF_TYPE_MC_HDLC
:
589 if ((p
->linktype
!= DLT_CHDLC
) &&
590 (p
->linktype
!= DLT_PPP_SERIAL
) &&
591 (p
->linktype
!= DLT_FRELAY
) &&
592 (p
->linktype
!= DLT_MTP2
) &&
593 (p
->linktype
!= DLT_MTP2_WITH_PHDR
) &&
594 (p
->linktype
!= DLT_LAPD
))
596 packet_len
= ntohs(header
->wlen
);
597 packet_len
-= (pd
->dag_fcs_bits
>> 3);
598 caplen
= rlen
- dag_record_size
- 4;
599 /* Skip over extension headers */
600 caplen
-= (8 * num_ext_hdr
);
601 if (caplen
> packet_len
) {
604 /* jump the MC_HDLC_HEADER */
606 #ifdef DLT_MTP2_WITH_PHDR
607 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
608 /* Add the MTP2 Pseudo Header */
609 caplen
+= MTP2_HDR_LEN
;
610 packet_len
+= MTP2_HDR_LEN
;
612 TempPkt
[MTP2_SENT_OFFSET
] = 0;
613 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
614 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
615 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
616 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
623 if ((p
->linktype
!= DLT_RAW
) &&
624 (p
->linktype
!= DLT_IPV4
))
626 packet_len
= ntohs(header
->wlen
);
627 caplen
= rlen
- dag_record_size
;
628 /* Skip over extension headers */
629 caplen
-= (8 * num_ext_hdr
);
630 if (caplen
> packet_len
) {
636 if ((p
->linktype
!= DLT_RAW
) &&
637 (p
->linktype
!= DLT_IPV6
))
639 packet_len
= ntohs(header
->wlen
);
640 caplen
= rlen
- dag_record_size
;
641 /* Skip over extension headers */
642 caplen
-= (8 * num_ext_hdr
);
643 if (caplen
> packet_len
) {
648 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
649 case ERF_TYPE_MC_RAW
:
650 case ERF_TYPE_MC_RAW_CHANNEL
:
651 case ERF_TYPE_IP_COUNTER
:
652 case ERF_TYPE_TCP_FLOW_COUNTER
:
653 case ERF_TYPE_INFINIBAND
:
654 case ERF_TYPE_RAW_LINK
:
655 case ERF_TYPE_INFINIBAND_LINK
:
657 /* Unhandled ERF type.
658 * Ignore rather than generating error
663 } /* ERF encapsulation */
665 if (caplen
> p
->snapshot
)
666 caplen
= p
->snapshot
;
668 /* Run the packet filter if there is one. */
669 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
671 /* convert between timestamp formats */
672 register unsigned long long ts
;
674 if (IS_BIGENDIAN()) {
675 ts
= SWAPLL(header
->ts
);
680 switch (p
->opt
.tstamp_precision
) {
681 case PCAP_TSTAMP_PRECISION_NANO
:
682 ticks_per_second
= 1000000000;
684 case PCAP_TSTAMP_PRECISION_MICRO
:
686 ticks_per_second
= 1000000;
690 pcap_header
.ts
.tv_sec
= ts
>> 32;
691 ts
= (ts
& 0xffffffffULL
) * ticks_per_second
;
692 ts
+= 0x80000000; /* rounding */
693 pcap_header
.ts
.tv_usec
= ts
>> 32;
694 if (pcap_header
.ts
.tv_usec
>= ticks_per_second
) {
695 pcap_header
.ts
.tv_usec
-= ticks_per_second
;
696 pcap_header
.ts
.tv_sec
++;
699 /* Fill in our own header data */
700 pcap_header
.caplen
= caplen
;
701 pcap_header
.len
= packet_len
;
703 /* Count the packet. */
706 /* Call the user supplied callback function */
707 callback(user
, &pcap_header
, dp
);
709 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
711 if (processed
== cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
713 /* Reached the user-specified limit. */
723 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
725 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
731 * Get a handle for a live capture from the given DAG device. Passing a NULL
732 * device will result in a failure. The promisc flag is ignored because DAG
733 * cards are always promiscuous. The to_ms parameter is used in setting the
734 * API polling parameters.
736 * snaplen is now also ignored, until we get per-stream slen support. Set
737 * slen with approprite DAG tool BEFORE pcap_activate().
741 static int dag_activate(pcap_t
* p
)
743 struct pcap_dag
*pd
= p
->priv
;
747 char * newDev
= NULL
;
748 char * device
= p
->opt
.device
;
750 struct timeval maxwait
;
753 if (device
== NULL
) {
754 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL");
758 /* Initialize some components of the pcap structure. */
759 newDev
= (char *)malloc(strlen(device
) + 16);
760 if (newDev
== NULL
) {
761 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
762 errno
, "Can't allocate string for device name");
766 /* Parse input name to get dag device and stream number if provided */
767 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &pd
->dag_stream
) < 0) {
768 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
769 errno
, "dag_parse_name");
774 if (pd
->dag_stream
%2) {
775 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture");
779 /* setup device parameters */
780 if((pd
->dag_ref
= dag_config_init((char *)device
)) == NULL
) {
781 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
782 errno
, "dag_config_init %s", device
);
786 if((p
->fd
= dag_config_get_card_fd(pd
->dag_ref
)) < 0) {
787 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
788 errno
, "dag_config_get_card_fd %s", device
);
792 /* Open requested stream. Can fail if already locked or on error */
793 if (dag_attach_stream64(p
->fd
, pd
->dag_stream
, 0, 0) < 0) {
794 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
795 errno
, "dag_attach_stream");
799 /* Try to find Stream Drop attribute */
800 pd
->drop_attr
= kNullAttributeUuid
;
801 pd
->dag_root
= dag_config_get_root_component(pd
->dag_ref
);
802 if ( dag_component_get_subcomponent(pd
->dag_root
, kComponentStreamFeatures
, 0) )
804 pd
->drop_attr
= dag_config_get_indexed_attribute_uuid(pd
->dag_ref
, kUint32AttributeStreamDropCount
, pd
->dag_stream
/2);
807 /* Set up default poll parameters for stream
808 * Can be overridden by pcap_set_nonblock()
810 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
811 &mindata
, &maxwait
, &poll
) < 0) {
812 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
813 errno
, "dag_get_stream_poll");
817 /* Use the poll time as the required select timeout for callers
818 * who are using select()/etc. in an event loop waiting for
821 pd
->required_select_timeout
= poll
;
822 p
->required_select_timeout
= &pd
->required_select_timeout
;
825 * Turn a negative snapshot value (invalid), a snapshot value of
826 * 0 (unspecified), or a value bigger than the normal maximum
827 * value, into the maximum allowed value.
829 * If some application really *needs* a bigger snapshot
830 * length, we should just increase MAXIMUM_SNAPLEN.
832 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
833 p
->snapshot
= MAXIMUM_SNAPLEN
;
835 if (p
->opt
.immediate
) {
836 /* Call callback immediately.
837 * XXX - is this the right way to p this?
841 /* Amount of data to collect in Bytes before calling callbacks.
842 * Important for efficiency, but can introduce latency
843 * at low packet rates if to_ms not set!
848 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
849 * Recommend 10-100ms. Calls will time out even if no data arrived.
851 maxwait
.tv_sec
= p
->opt
.timeout
/1000;
852 maxwait
.tv_usec
= (p
->opt
.timeout
%1000) * 1000;
854 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
855 mindata
, &maxwait
, &poll
) < 0) {
856 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
857 errno
, "dag_set_stream_poll");
861 /* XXX Not calling dag_configure() to set slen; this is unsafe in
862 * multi-stream environments as the gpp config is global.
863 * Once the firmware provides 'per-stream slen' this can be supported
864 * again via the Config API without side-effects */
866 /* set the card snap length to the specified snaplen parameter */
867 /* This is a really bad idea, as different cards have different
868 * valid slen ranges. Should fix in Config API. */
869 if (p
->snapshot
== 0 || p
->snapshot
> MAX_DAG_SNAPLEN
) {
870 p
->snapshot
= MAX_DAG_SNAPLEN
;
871 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
872 p
->snapshot
= MIN_DAG_SNAPLEN
;
874 /* snap len has to be a multiple of 4 */
877 if(dag_start_stream(p
->fd
, pd
->dag_stream
) < 0) {
878 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
879 errno
, "dag_start_stream %s", device
);
884 * Important! You have to ensure bottom is properly
885 * initialized to zero on startup, it won't give you
886 * a compiler warning if you make this mistake!
888 pd
->dag_mem_bottom
= 0;
892 * Find out how many FCS bits we should strip.
893 * First, query the card to see if it strips the FCS.
895 daginf
= dag_info(p
->fd
);
896 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
897 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
898 pd
->dag_fcs_bits
= 0;
900 /* Note that no FCS will be supplied. */
901 p
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
904 * Start out assuming it's 32 bits.
906 pd
->dag_fcs_bits
= 32;
908 /* Allow an environment variable to override. */
909 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
910 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
911 pd
->dag_fcs_bits
= n
;
913 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
914 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment", device
, n
);
920 * Did the user request that they not be stripped?
922 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
923 /* Yes. Note the number of bytes that will be
925 p
->linktype_ext
= LT_FCS_DATALINK_EXT(pd
->dag_fcs_bits
/16);
927 /* And don't strip them. */
928 pd
->dag_fcs_bits
= 0;
932 pd
->dag_timeout
= p
->opt
.timeout
;
935 if (dag_get_datalink(p
) < 0)
940 if (new_pcap_dag(p
) < 0) {
941 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
942 errno
, "new_pcap_dag %s", device
);
947 * "select()" and "poll()" don't work on DAG device descriptors.
949 p
->selectable_fd
= -1;
951 if (newDev
!= NULL
) {
952 free((char *)newDev
);
955 p
->read_op
= dag_read
;
956 p
->inject_op
= dag_inject
;
957 p
->setfilter_op
= dag_setfilter
;
958 p
->setdirection_op
= NULL
; /* Not implemented.*/
959 p
->set_datalink_op
= dag_set_datalink
;
960 p
->getnonblock_op
= pcap_getnonblock_fd
;
961 p
->setnonblock_op
= dag_setnonblock
;
962 p
->stats_op
= dag_stats
;
963 p
->cleanup_op
= dag_platform_cleanup
;
964 pd
->stat
.ps_drop
= 0;
965 pd
->stat
.ps_recv
= 0;
966 pd
->stat
.ps_ifdrop
= 0;
970 if (dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0) {
971 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
975 if (dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
976 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
979 dag_config_dispose(pd
->dag_ref
);
983 pcap_cleanup_live_common(p
);
984 if (newDev
!= NULL
) {
985 free((char *)newDev
);
991 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
999 /* Does this look like a DAG device? */
1000 cp
= strrchr(device
, '/');
1003 /* Does it begin with "dag"? */
1004 if (strncmp(cp
, "dag", 3) != 0) {
1005 /* Nope, doesn't begin with "dag" */
1009 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
1011 devnum
= strtol(cp
, &cpend
, 10);
1012 if (*cpend
== ':') {
1013 /* Followed by a stream number. */
1014 stream
= strtol(++cpend
, &cpend
, 10);
1017 if (cpend
== cp
|| *cpend
!= '\0') {
1018 /* Not followed by a number. */
1023 if (devnum
< 0 || devnum
>= DAG_MAX_BOARDS
) {
1024 /* Followed by a non-valid number. */
1029 if (stream
<0 || stream
>= DAG_STREAM_MAX
) {
1030 /* Followed by a non-valid stream number. */
1035 /* OK, it's probably ours. */
1038 p
= pcap_create_common(ebuf
, sizeof (struct pcap_dag
));
1042 p
->activate_op
= dag_activate
;
1045 * We claim that we support microsecond and nanosecond time
1048 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1049 * power of two precisions yet. We can convert to either MICRO or NANO.
1051 p
->tstamp_precision_count
= 2;
1052 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
1053 if (p
->tstamp_precision_list
== NULL
) {
1054 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1059 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
1060 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
1065 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
1066 struct pcap_dag
*pd
= p
->priv
;
1067 uint32_t stream_drop
;
1070 * Packet records received (ps_recv) are counted in dag_read().
1071 * Packet records dropped (ps_drop) are read from Stream Drop attribute if present,
1072 * otherwise integrate the ERF Header lctr counts (if available) in dag_read().
1073 * We are reporting that no records are dropped by the card/driver (ps_ifdrop).
1076 if(pd
->drop_attr
!= kNullAttributeUuid
) {
1077 /* Note this counter is cleared at start of capture and will wrap at UINT_MAX.
1078 * The application is responsible for polling ps_drop frequently enough
1079 * to detect each wrap and integrate total drop with a wider counter */
1080 if (dag_config_get_uint32_attribute_ex(pd
->dag_ref
, pd
->drop_attr
, &stream_drop
) == kDagErrNone
) {
1081 pd
->stat
.ps_drop
= stream_drop
;
1083 /* Currently not reporting errors reading stats */
1093 * Add all DAG devices.
1096 dag_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1098 char name
[12]; /* XXX - pick a size */
1100 char dagname
[DAGNAME_BUFSIZE
];
1103 dag_card_inf_t
*inf
;
1105 int stream
, rxstreams
;
1107 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1108 for (c
= 0; c
< DAG_MAX_BOARDS
; c
++) {
1109 pcap_snprintf(name
, 12, "dag%d", c
);
1110 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
1112 (void) pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1113 "dag: device name %s can't be parsed", name
);
1116 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
1118 if ((inf
= dag_pciinfo(dagfd
)))
1119 description
= dag_device_name(inf
->device_code
, 1);
1121 * XXX - is there a way to determine whether
1122 * the card is plugged into a network or not?
1123 * If so, we should check that and set
1124 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
1125 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
1127 * Also, are there notions of "up" and "running"?
1129 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1135 rxstreams
= dag_rx_get_stream_count(dagfd
);
1136 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
1137 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
1138 dag_detach_stream(dagfd
, stream
);
1140 pcap_snprintf(name
, 10, "dag%d:%d", c
, stream
);
1141 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1149 if(rxstreams
<= 0) {
1162 * Installs the given bpf filter program in the given pcap structure. There is
1163 * no attempt to store the filter in kernel memory as that is not supported
1167 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
1172 strncpy(p
->errbuf
, "setfilter: No filter specified",
1177 /* Make our private copy of the filter */
1179 if (install_bpf_program(p
, fp
) < 0)
1186 dag_set_datalink(pcap_t
*p
, int dlt
)
1194 dag_setnonblock(pcap_t
*p
, int nonblock
)
1196 struct pcap_dag
*pd
= p
->priv
;
1198 struct timeval maxwait
;
1199 struct timeval poll
;
1202 * Set non-blocking mode on the FD.
1203 * XXX - is that necessary? If not, don't bother calling it,
1204 * and have a "dag_getnonblock()" function that looks at
1207 if (pcap_setnonblock_fd(p
, nonblock
) < 0)
1210 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
1211 &mindata
, &maxwait
, &poll
) < 0) {
1212 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1213 errno
, "dag_get_stream_poll");
1217 /* Amount of data to collect in Bytes before calling callbacks.
1218 * Important for efficiency, but can introduce latency
1219 * at low packet rates if to_ms not set!
1226 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
1227 mindata
, &maxwait
, &poll
) < 0) {
1228 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1229 errno
, "dag_set_stream_poll");
1234 pd
->dag_flags
|= DAGF_NONBLOCK
;
1236 pd
->dag_flags
&= ~DAGF_NONBLOCK
;
1242 dag_get_datalink(pcap_t
*p
)
1244 struct pcap_dag
*pd
= p
->priv
;
1245 int index
=0, dlt_index
=0;
1248 memset(types
, 0, 255);
1250 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1251 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1258 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1259 /* Get list of possible ERF types for this card */
1260 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1261 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1262 errno
, "dag_get_stream_erf_types");
1266 while (types
[index
]) {
1268 #elif defined HAVE_DAG_GET_ERF_TYPES
1269 /* Get list of possible ERF types for this card */
1270 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1271 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1272 errno
, "dag_get_erf_types");
1276 while (types
[index
]) {
1278 /* Check the type through a dagapi call. */
1279 types
[index
] = dag_linktype(p
->fd
);
1283 switch((types
[index
] & 0x7f)) {
1285 case ERF_TYPE_HDLC_POS
:
1286 case ERF_TYPE_COLOR_HDLC_POS
:
1287 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
1288 case ERF_TYPE_COLOR_HASH_POS
:
1290 if (p
->dlt_list
!= NULL
) {
1291 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1292 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1293 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1296 p
->linktype
= DLT_CHDLC
;
1300 case ERF_TYPE_COLOR_ETH
:
1301 case ERF_TYPE_DSM_COLOR_ETH
:
1302 case ERF_TYPE_COLOR_HASH_ETH
:
1304 * This is (presumably) a real Ethernet capture; give it a
1305 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1306 * that an application can let you choose it, in case you're
1307 * capturing DOCSIS traffic that a Cisco Cable Modem
1308 * Termination System is putting out onto an Ethernet (it
1309 * doesn't put an Ethernet header onto the wire, it puts raw
1310 * DOCSIS frames out on the wire inside the low-level
1311 * Ethernet framing).
1313 if (p
->dlt_list
!= NULL
) {
1314 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1315 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1318 p
->linktype
= DLT_EN10MB
;
1323 case ERF_TYPE_MC_ATM
:
1324 case ERF_TYPE_MC_AAL5
:
1325 if (p
->dlt_list
!= NULL
) {
1326 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1327 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1330 p
->linktype
= DLT_ATM_RFC1483
;
1333 case ERF_TYPE_COLOR_MC_HDLC_POS
:
1334 case ERF_TYPE_MC_HDLC
:
1335 if (p
->dlt_list
!= NULL
) {
1336 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1337 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1338 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1339 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1340 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1341 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1344 p
->linktype
= DLT_CHDLC
;
1348 if (p
->dlt_list
!= NULL
) {
1349 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1350 p
->dlt_list
[dlt_index
++] = DLT_IPV4
;
1353 p
->linktype
= DLT_RAW
;
1357 if (p
->dlt_list
!= NULL
) {
1358 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1359 p
->dlt_list
[dlt_index
++] = DLT_IPV6
;
1362 p
->linktype
= DLT_RAW
;
1365 case ERF_TYPE_LEGACY
:
1366 case ERF_TYPE_MC_RAW
:
1367 case ERF_TYPE_MC_RAW_CHANNEL
:
1368 case ERF_TYPE_IP_COUNTER
:
1369 case ERF_TYPE_TCP_FLOW_COUNTER
:
1370 case ERF_TYPE_INFINIBAND
:
1371 case ERF_TYPE_RAW_LINK
:
1372 case ERF_TYPE_INFINIBAND_LINK
:
1375 /* Libpcap cannot deal with these types yet */
1376 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1383 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1385 p
->dlt_count
= dlt_index
;
1388 p
->linktype
= DLT_ERF
;
1395 * This libpcap build supports only DAG cards, not regular network
1400 * There are no regular interfaces, just DAG interfaces.
1403 pcap_platform_finddevs(pcap_if_list_t
*devlistp _U_
, char *errbuf
)
1409 * Attempts to open a regular interface fail.
1412 pcap_create_interface(const char *device
, char *errbuf
)
1414 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1415 "This version of libpcap only supports DAG cards");
1420 * Libpcap version string.
1423 pcap_lib_version(void)
1425 return (PCAP_VERSION_STRING
" (DAG-only)");