2 * pcap-dag.c: Packet capture interface for Endace DAG cards.
4 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
5 * Modifications: Jesper Peterson
7 * Stephen Donnelly <stephen.donnelly@endace.com>
14 #include <sys/param.h> /* optionally get BSD define */
22 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
28 struct mbuf
; /* Squelch compiler warnings on some platforms for */
29 struct rtentry
; /* declarations in <net/if.h> */
35 #include "dag_config_api.h"
40 * DAG devices have names beginning with "dag", followed by a number
41 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
42 * from 0 to DAG_STREAM_MAX.
44 #ifndef DAG_MAX_BOARDS
45 #define DAG_MAX_BOARDS 32
50 #define ERF_TYPE_AAL5 4
53 #ifndef ERF_TYPE_MC_HDLC
54 #define ERF_TYPE_MC_HDLC 5
57 #ifndef ERF_TYPE_MC_RAW
58 #define ERF_TYPE_MC_RAW 6
61 #ifndef ERF_TYPE_MC_ATM
62 #define ERF_TYPE_MC_ATM 7
65 #ifndef ERF_TYPE_MC_RAW_CHANNEL
66 #define ERF_TYPE_MC_RAW_CHANNEL 8
69 #ifndef ERF_TYPE_MC_AAL5
70 #define ERF_TYPE_MC_AAL5 9
73 #ifndef ERF_TYPE_COLOR_HDLC_POS
74 #define ERF_TYPE_COLOR_HDLC_POS 10
77 #ifndef ERF_TYPE_COLOR_ETH
78 #define ERF_TYPE_COLOR_ETH 11
81 #ifndef ERF_TYPE_MC_AAL2
82 #define ERF_TYPE_MC_AAL2 12
85 #ifndef ERF_TYPE_IP_COUNTER
86 #define ERF_TYPE_IP_COUNTER 13
89 #ifndef ERF_TYPE_TCP_FLOW_COUNTER
90 #define ERF_TYPE_TCP_FLOW_COUNTER 14
93 #ifndef ERF_TYPE_DSM_COLOR_HDLC_POS
94 #define ERF_TYPE_DSM_COLOR_HDLC_POS 15
97 #ifndef ERF_TYPE_DSM_COLOR_ETH
98 #define ERF_TYPE_DSM_COLOR_ETH 16
101 #ifndef ERF_TYPE_COLOR_MC_HDLC_POS
102 #define ERF_TYPE_COLOR_MC_HDLC_POS 17
105 #ifndef ERF_TYPE_AAL2
106 #define ERF_TYPE_AAL2 18
109 #ifndef ERF_TYPE_COLOR_HASH_POS
110 #define ERF_TYPE_COLOR_HASH_POS 19
113 #ifndef ERF_TYPE_COLOR_HASH_ETH
114 #define ERF_TYPE_COLOR_HASH_ETH 20
117 #ifndef ERF_TYPE_INFINIBAND
118 #define ERF_TYPE_INFINIBAND 21
121 #ifndef ERF_TYPE_IPV4
122 #define ERF_TYPE_IPV4 22
125 #ifndef ERF_TYPE_IPV6
126 #define ERF_TYPE_IPV6 23
129 #ifndef ERF_TYPE_RAW_LINK
130 #define ERF_TYPE_RAW_LINK 24
133 #ifndef ERF_TYPE_INFINIBAND_LINK
134 #define ERF_TYPE_INFINIBAND_LINK 25
137 #ifndef ERF_TYPE_META
138 #define ERF_TYPE_META 27
142 #define ERF_TYPE_PAD 48
145 #define ATM_CELL_SIZE 52
146 #define ATM_HDR_SIZE 4
149 * A header containing additional MTP information.
151 #define MTP2_SENT_OFFSET 0 /* 1 byte */
152 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
153 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
154 #define MTP2_HDR_LEN 4 /* length of the header */
156 #define MTP2_ANNEX_A_NOT_USED 0
157 #define MTP2_ANNEX_A_USED 1
158 #define MTP2_ANNEX_A_USED_UNKNOWN 2
160 /* SunATM pseudo header */
162 unsigned char flags
; /* destination and traffic type */
163 unsigned char vpi
; /* VPI */
164 unsigned short vci
; /* VCI */
168 * Private data for capturing on DAG devices.
171 struct pcap_stat stat
;
172 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
173 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
174 int dag_fcs_bits
; /* Number of checksum bits from link layer */
175 int dag_flags
; /* Flags */
176 int dag_stream
; /* DAG stream number */
177 int dag_timeout
; /* timeout specified to pcap_open_live.
178 * Same as in linux above, introduce
180 dag_card_ref_t dag_ref
; /* DAG Configuration/Status API card reference */
181 dag_component_t dag_root
; /* DAG CSAPI Root component */
182 attr_uuid_t drop_attr
; /* DAG Stream Drop Attribute handle, if available */
183 struct timeval required_select_timeout
;
184 /* Timeout caller must use in event loops */
187 typedef struct pcap_dag_node
{
188 struct pcap_dag_node
*next
;
193 static pcap_dag_node_t
*pcap_dags
= NULL
;
194 static int atexit_handler_installed
= 0;
195 static const unsigned short endian_test_word
= 0x0100;
197 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
199 #define MAX_DAG_PACKET 65536
201 static unsigned char TempPkt
[MAX_DAG_PACKET
];
203 #ifndef HAVE_DAG_LARGE_STREAMS_API
204 #define dag_attach_stream64(a, b, c, d) dag_attach_stream(a, b, c, d)
205 #define dag_get_stream_poll64(a, b, c, d, e) dag_get_stream_poll(a, b, c, d, e)
206 #define dag_set_stream_poll64(a, b, c, d, e) dag_set_stream_poll(a, b, c, d, e)
207 #define dag_size_t uint32_t
210 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
211 static int dag_set_datalink(pcap_t
*p
, int dlt
);
212 static int dag_get_datalink(pcap_t
*p
);
213 static int dag_setnonblock(pcap_t
*p
, int nonblock
);
216 delete_pcap_dag(pcap_t
*p
)
218 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
220 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
224 if (curr
!= NULL
&& curr
->p
== p
) {
226 prev
->next
= curr
->next
;
228 pcap_dags
= curr
->next
;
234 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
235 * in the pcap_t structure, and closes the file descriptor for the DAG card.
239 dag_platform_cleanup(pcap_t
*p
)
241 struct pcap_dag
*pd
= p
->priv
;
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(pd
->dag_ref
!= NULL
) {
250 dag_config_dispose(pd
->dag_ref
);
252 * Note: we don't need to call close(p->fd) or
253 * dag_close(p->fd), as dag_config_dispose(pd->dag_ref)
256 * Set p->fd to -1 to make sure that's not done.
262 pcap_cleanup_live_common(p
);
268 while (pcap_dags
!= NULL
) {
269 if (pcap_dags
->pid
== getpid()) {
270 if (pcap_dags
->p
!= NULL
)
271 dag_platform_cleanup(pcap_dags
->p
);
273 delete_pcap_dag(pcap_dags
->p
);
279 new_pcap_dag(pcap_t
*p
)
281 pcap_dag_node_t
*node
= NULL
;
283 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
287 if (!atexit_handler_installed
) {
288 atexit(atexit_handler
);
289 atexit_handler_installed
= 1;
292 node
->next
= pcap_dags
;
294 node
->pid
= getpid();
302 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
304 uint32_t hdr_num
= 0;
307 /* basic sanity checks */
313 /* check if we have any extension headers */
314 if ( (erf
[8] & 0x80) == 0x00 )
317 /* loop over the extension headers */
320 /* sanity check we have enough bytes */
321 if ( len
< (24 + (hdr_num
* 8)) )
324 /* get the header type */
325 hdr_type
= erf
[(16 + (hdr_num
* 8))];
328 } while ( hdr_type
& 0x80 );
334 * Read at most max_packets from the capture stream and call the callback
335 * for each of them. Returns the number of packets handled, -1 if an
336 * error occurred, or -2 if we were told to break out of the loop.
339 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
341 struct pcap_dag
*pd
= p
->priv
;
342 unsigned int processed
= 0;
343 unsigned int nonblocking
= pd
->dag_flags
& DAGF_NONBLOCK
;
344 unsigned int num_ext_hdr
= 0;
345 unsigned int ticks_per_second
;
347 /* Get the next bufferful of packets (if necessary). */
348 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
) {
351 * Has "pcap_breakloop()" been called?
355 * Yes - clear the flag that indicates that
356 * it has, and return -2 to indicate that
357 * we were told to break out of the loop.
363 /* dag_advance_stream() will block (unless nonblock is called)
364 * until 64kB of data has accumulated.
365 * If to_ms is set, it will timeout before 64kB has accumulated.
366 * We wait for 64kB because processing a few packets at a time
367 * can cause problems at high packet rates (>200kpps) due
369 * This does mean if to_ms is not specified the capture may 'hang'
370 * for long periods if the data rate is extremely slow (<64kB/sec)
371 * If non-block is specified it will return immediately. The user
372 * is then responsible for efficiency.
374 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
378 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
380 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
386 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
388 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
395 * Process the packets.
397 * This assumes that a single buffer of packets will have
398 * <= INT_MAX packets, so the packet count doesn't overflow.
400 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
402 unsigned short packet_len
= 0;
404 struct pcap_pkthdr pcap_header
;
406 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
408 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
412 * Has "pcap_breakloop()" been called?
416 * Yes - clear the flag that indicates that
417 * it has, and return -2 to indicate that
418 * we were told to break out of the loop.
424 rlen
= ntohs(header
->rlen
);
425 if (rlen
< dag_record_size
)
427 pcap_strlcpy(p
->errbuf
, "dag_read: record too small",
431 pd
->dag_mem_bottom
+= rlen
;
433 /* Count lost packets. */
434 switch((header
->type
& 0x7f)) {
435 /* in these types the color value overwrites the lctr */
436 case ERF_TYPE_COLOR_HDLC_POS
:
437 case ERF_TYPE_COLOR_ETH
:
438 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
439 case ERF_TYPE_DSM_COLOR_ETH
:
440 case ERF_TYPE_COLOR_MC_HDLC_POS
:
441 case ERF_TYPE_COLOR_HASH_ETH
:
442 case ERF_TYPE_COLOR_HASH_POS
:
446 if ( (pd
->drop_attr
== kNullAttributeUuid
) && (header
->lctr
) ) {
447 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
451 if ((header
->type
& 0x7f) == ERF_TYPE_PAD
) {
455 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
457 /* ERF encapsulation */
458 /* The Extensible Record Format is not dropped for this kind of encapsulation,
459 * and will be handled as a pseudo header by the decoding application.
460 * The information carried in the ERF header and in the optional subheader (if present)
461 * could be merged with the libpcap information, to offer a better decoding.
462 * The packet length is
463 * o the length of the packet on the link (header->wlen),
464 * o plus the length of the ERF header (dag_record_size), as the length of the
465 * pseudo header will be adjusted during the decoding,
466 * o plus the length of the optional subheader (if present).
468 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
469 * if the capture length is greater than the packet length.
471 if (p
->linktype
== DLT_ERF
) {
472 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
474 switch ((header
->type
& 0x7f)) {
475 case ERF_TYPE_MC_AAL5
:
476 case ERF_TYPE_MC_ATM
:
477 case ERF_TYPE_MC_HDLC
:
478 case ERF_TYPE_MC_RAW_CHANNEL
:
479 case ERF_TYPE_MC_RAW
:
480 case ERF_TYPE_MC_AAL2
:
481 case ERF_TYPE_COLOR_MC_HDLC_POS
:
482 packet_len
+= 4; /* MC header */
485 case ERF_TYPE_COLOR_HASH_ETH
:
486 case ERF_TYPE_DSM_COLOR_ETH
:
487 case ERF_TYPE_COLOR_ETH
:
489 packet_len
+= 2; /* ETH header */
493 /* Include ERF extension headers */
494 packet_len
+= (8 * num_ext_hdr
);
496 if (caplen
> packet_len
) {
500 /* Other kind of encapsulation according to the header Type */
502 /* Skip over generic ERF header */
503 dp
+= dag_record_size
;
504 /* Skip over extension headers */
505 dp
+= 8 * num_ext_hdr
;
507 switch((header
->type
& 0x7f)) {
510 if ((header
->type
& 0x7f) == ERF_TYPE_AAL5
) {
511 packet_len
= ntohs(header
->wlen
);
512 caplen
= rlen
- dag_record_size
;
514 case ERF_TYPE_MC_ATM
:
515 if ((header
->type
& 0x7f) == ERF_TYPE_MC_ATM
) {
516 caplen
= packet_len
= ATM_CELL_SIZE
;
519 case ERF_TYPE_MC_AAL5
:
520 if ((header
->type
& 0x7f) == ERF_TYPE_MC_AAL5
) {
521 packet_len
= ntohs(header
->wlen
);
522 caplen
= rlen
- dag_record_size
- 4;
525 /* Skip over extension headers */
526 caplen
-= (8 * num_ext_hdr
);
528 if ((header
->type
& 0x7f) == ERF_TYPE_ATM
) {
529 caplen
= packet_len
= ATM_CELL_SIZE
;
531 if (p
->linktype
== DLT_SUNATM
) {
532 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
533 unsigned long rawatm
;
535 rawatm
= ntohl(*((unsigned long *)dp
));
536 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
537 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
538 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
539 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
540 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
541 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
542 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
543 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
545 } else if (p
->linktype
== DLT_ATM_RFC1483
) {
546 packet_len
-= ATM_HDR_SIZE
;
547 caplen
-= ATM_HDR_SIZE
;
553 case ERF_TYPE_COLOR_HASH_ETH
:
554 case ERF_TYPE_DSM_COLOR_ETH
:
555 case ERF_TYPE_COLOR_ETH
:
557 if ((p
->linktype
!= DLT_EN10MB
) &&
558 (p
->linktype
!= DLT_DOCSIS
))
560 packet_len
= ntohs(header
->wlen
);
561 packet_len
-= (pd
->dag_fcs_bits
>> 3);
562 caplen
= rlen
- dag_record_size
- 2;
563 /* Skip over extension headers */
564 caplen
-= (8 * num_ext_hdr
);
565 if (caplen
> packet_len
) {
571 case ERF_TYPE_COLOR_HASH_POS
:
572 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
573 case ERF_TYPE_COLOR_HDLC_POS
:
574 case ERF_TYPE_HDLC_POS
:
575 if ((p
->linktype
!= DLT_CHDLC
) &&
576 (p
->linktype
!= DLT_PPP_SERIAL
) &&
577 (p
->linktype
!= DLT_FRELAY
))
579 packet_len
= ntohs(header
->wlen
);
580 packet_len
-= (pd
->dag_fcs_bits
>> 3);
581 caplen
= rlen
- dag_record_size
;
582 /* Skip over extension headers */
583 caplen
-= (8 * num_ext_hdr
);
584 if (caplen
> packet_len
) {
589 case ERF_TYPE_COLOR_MC_HDLC_POS
:
590 case ERF_TYPE_MC_HDLC
:
591 if ((p
->linktype
!= DLT_CHDLC
) &&
592 (p
->linktype
!= DLT_PPP_SERIAL
) &&
593 (p
->linktype
!= DLT_FRELAY
) &&
594 (p
->linktype
!= DLT_MTP2
) &&
595 (p
->linktype
!= DLT_MTP2_WITH_PHDR
) &&
596 (p
->linktype
!= DLT_LAPD
))
598 packet_len
= ntohs(header
->wlen
);
599 packet_len
-= (pd
->dag_fcs_bits
>> 3);
600 caplen
= rlen
- dag_record_size
- 4;
601 /* Skip over extension headers */
602 caplen
-= (8 * num_ext_hdr
);
603 if (caplen
> packet_len
) {
606 /* jump the MC_HDLC_HEADER */
608 #ifdef DLT_MTP2_WITH_PHDR
609 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
610 /* Add the MTP2 Pseudo Header */
611 caplen
+= MTP2_HDR_LEN
;
612 packet_len
+= MTP2_HDR_LEN
;
614 TempPkt
[MTP2_SENT_OFFSET
] = 0;
615 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
616 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
617 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
618 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
625 if ((p
->linktype
!= DLT_RAW
) &&
626 (p
->linktype
!= DLT_IPV4
))
628 packet_len
= ntohs(header
->wlen
);
629 caplen
= rlen
- dag_record_size
;
630 /* Skip over extension headers */
631 caplen
-= (8 * num_ext_hdr
);
632 if (caplen
> packet_len
) {
638 if ((p
->linktype
!= DLT_RAW
) &&
639 (p
->linktype
!= DLT_IPV6
))
641 packet_len
= ntohs(header
->wlen
);
642 caplen
= rlen
- dag_record_size
;
643 /* Skip over extension headers */
644 caplen
-= (8 * num_ext_hdr
);
645 if (caplen
> packet_len
) {
650 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
651 case ERF_TYPE_MC_RAW
:
652 case ERF_TYPE_MC_RAW_CHANNEL
:
653 case ERF_TYPE_IP_COUNTER
:
654 case ERF_TYPE_TCP_FLOW_COUNTER
:
655 case ERF_TYPE_INFINIBAND
:
656 case ERF_TYPE_RAW_LINK
:
657 case ERF_TYPE_INFINIBAND_LINK
:
659 /* Unhandled ERF type.
660 * Ignore rather than generating error
665 } /* ERF encapsulation */
667 if (caplen
> p
->snapshot
)
668 caplen
= p
->snapshot
;
670 /* Run the packet filter if there is one. */
671 if ((p
->fcode
.bf_insns
== NULL
) || pcap_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
673 /* convert between timestamp formats */
674 register unsigned long long ts
;
676 if (IS_BIGENDIAN()) {
677 ts
= SWAPLL(header
->ts
);
682 switch (p
->opt
.tstamp_precision
) {
683 case PCAP_TSTAMP_PRECISION_NANO
:
684 ticks_per_second
= 1000000000;
686 case PCAP_TSTAMP_PRECISION_MICRO
:
688 ticks_per_second
= 1000000;
692 pcap_header
.ts
.tv_sec
= ts
>> 32;
693 ts
= (ts
& 0xffffffffULL
) * ticks_per_second
;
694 ts
+= 0x80000000; /* rounding */
695 pcap_header
.ts
.tv_usec
= ts
>> 32;
696 if (pcap_header
.ts
.tv_usec
>= ticks_per_second
) {
697 pcap_header
.ts
.tv_usec
-= ticks_per_second
;
698 pcap_header
.ts
.tv_sec
++;
701 /* Fill in our own header data */
702 pcap_header
.caplen
= caplen
;
703 pcap_header
.len
= packet_len
;
705 /* Count the packet. */
708 /* Call the user supplied callback function */
709 callback(user
, &pcap_header
, dp
);
711 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
713 if (processed
== cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
715 /* Reached the user-specified limit. */
725 dag_inject(pcap_t
*p
, const void *buf _U_
, int size _U_
)
727 pcap_strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
733 * Get a handle for a live capture from the given DAG device. Passing a NULL
734 * device will result in a failure. The promisc flag is ignored because DAG
735 * cards are always promiscuous. The to_ms parameter is used in setting the
736 * API polling parameters.
738 * snaplen is now also ignored, until we get per-stream slen support. Set
739 * slen with approprite DAG tool BEFORE pcap_activate().
743 static int dag_activate(pcap_t
* p
)
745 struct pcap_dag
*pd
= p
->priv
;
749 char * newDev
= NULL
;
750 char * device
= p
->opt
.device
;
753 struct timeval maxwait
;
756 if (device
== NULL
) {
757 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL");
761 /* Initialize some components of the pcap structure. */
762 newDev
= (char *)malloc(strlen(device
) + 16);
763 if (newDev
== NULL
) {
765 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
766 errno
, "Can't allocate string for device name");
770 /* Parse input name to get dag device and stream number if provided */
771 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &pd
->dag_stream
) < 0) {
773 * XXX - it'd be nice if this indicated what was wrong
774 * with the name. Does this reliably set errno?
775 * Should this return PCAP_ERROR_NO_SUCH_DEVICE in some
779 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
780 errno
, "dag_parse_name");
785 if (pd
->dag_stream
%2) {
787 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture");
791 /* setup device parameters */
792 if((pd
->dag_ref
= dag_config_init((char *)device
)) == NULL
) {
794 * XXX - does this reliably set errno?
797 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
798 else if (errno
== EPERM
|| errno
== EACCES
)
799 ret
= PCAP_ERROR_PERM_DENIED
;
802 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
803 errno
, "dag_config_init %s", device
);
807 if((p
->fd
= dag_config_get_card_fd(pd
->dag_ref
)) < 0) {
809 * XXX - does this reliably set errno?
812 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
813 errno
, "dag_config_get_card_fd %s", device
);
817 /* Open requested stream. Can fail if already locked or on error */
818 if (dag_attach_stream64(p
->fd
, pd
->dag_stream
, 0, 0) < 0) {
820 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
821 errno
, "dag_attach_stream");
825 /* Try to find Stream Drop attribute */
826 pd
->drop_attr
= kNullAttributeUuid
;
827 pd
->dag_root
= dag_config_get_root_component(pd
->dag_ref
);
828 if ( dag_component_get_subcomponent(pd
->dag_root
, kComponentStreamFeatures
, 0) )
830 pd
->drop_attr
= dag_config_get_indexed_attribute_uuid(pd
->dag_ref
, kUint32AttributeStreamDropCount
, pd
->dag_stream
/2);
833 /* Set up default poll parameters for stream
834 * Can be overridden by pcap_set_nonblock()
836 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
837 &mindata
, &maxwait
, &poll
) < 0) {
839 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
840 errno
, "dag_get_stream_poll");
844 /* Use the poll time as the required select timeout for callers
845 * who are using select()/etc. in an event loop waiting for
848 pd
->required_select_timeout
= poll
;
849 p
->required_select_timeout
= &pd
->required_select_timeout
;
852 * Turn a negative snapshot value (invalid), a snapshot value of
853 * 0 (unspecified), or a value bigger than the normal maximum
854 * value, into the maximum allowed value.
856 * If some application really *needs* a bigger snapshot
857 * length, we should just increase MAXIMUM_SNAPLEN.
859 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
860 p
->snapshot
= MAXIMUM_SNAPLEN
;
862 if (p
->opt
.immediate
) {
863 /* Call callback immediately.
864 * XXX - is this the right way to p this?
868 /* Amount of data to collect in Bytes before calling callbacks.
869 * Important for efficiency, but can introduce latency
870 * at low packet rates if to_ms not set!
875 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
876 * Recommend 10-100ms. Calls will time out even if no data arrived.
878 maxwait
.tv_sec
= p
->opt
.timeout
/1000;
879 maxwait
.tv_usec
= (p
->opt
.timeout
%1000) * 1000;
881 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
882 mindata
, &maxwait
, &poll
) < 0) {
884 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
885 errno
, "dag_set_stream_poll");
889 /* XXX Not calling dag_configure() to set slen; this is unsafe in
890 * multi-stream environments as the gpp config is global.
891 * Once the firmware provides 'per-stream slen' this can be supported
892 * again via the Config API without side-effects */
894 /* set the card snap length to the specified snaplen parameter */
895 /* This is a really bad idea, as different cards have different
896 * valid slen ranges. Should fix in Config API. */
897 if (p
->snapshot
== 0 || p
->snapshot
> MAX_DAG_SNAPLEN
) {
898 p
->snapshot
= MAX_DAG_SNAPLEN
;
899 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
900 p
->snapshot
= MIN_DAG_SNAPLEN
;
902 /* snap len has to be a multiple of 4 */
905 if(dag_start_stream(p
->fd
, pd
->dag_stream
) < 0) {
907 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
908 errno
, "dag_start_stream %s", device
);
913 * Important! You have to ensure bottom is properly
914 * initialized to zero on startup, it won't give you
915 * a compiler warning if you make this mistake!
917 pd
->dag_mem_bottom
= 0;
921 * Find out how many FCS bits we should strip.
922 * First, query the card to see if it strips the FCS.
924 daginf
= dag_info(p
->fd
);
925 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
926 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
927 pd
->dag_fcs_bits
= 0;
929 /* Note that no FCS will be supplied. */
930 p
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
933 * Start out assuming it's 32 bits.
935 pd
->dag_fcs_bits
= 32;
937 /* Allow an environment variable to override. */
938 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
939 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
940 pd
->dag_fcs_bits
= n
;
943 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
944 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment", device
, n
);
950 * Did the user request that they not be stripped?
952 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
953 /* Yes. Note the number of bytes that will be
955 p
->linktype_ext
= LT_FCS_DATALINK_EXT(pd
->dag_fcs_bits
/16);
957 /* And don't strip them. */
958 pd
->dag_fcs_bits
= 0;
962 pd
->dag_timeout
= p
->opt
.timeout
;
965 if (dag_get_datalink(p
) < 0) {
972 if (new_pcap_dag(p
) < 0) {
974 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
975 errno
, "new_pcap_dag %s", device
);
980 * "select()" and "poll()" don't work on DAG device descriptors.
982 p
->selectable_fd
= -1;
984 if (newDev
!= NULL
) {
985 free((char *)newDev
);
988 p
->read_op
= dag_read
;
989 p
->inject_op
= dag_inject
;
990 p
->setfilter_op
= install_bpf_program
;
991 p
->setdirection_op
= NULL
; /* Not implemented.*/
992 p
->set_datalink_op
= dag_set_datalink
;
993 p
->getnonblock_op
= pcap_getnonblock_fd
;
994 p
->setnonblock_op
= dag_setnonblock
;
995 p
->stats_op
= dag_stats
;
996 p
->cleanup_op
= dag_platform_cleanup
;
997 pd
->stat
.ps_drop
= 0;
998 pd
->stat
.ps_recv
= 0;
999 pd
->stat
.ps_ifdrop
= 0;
1003 if (dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0) {
1004 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
1008 if (dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
1009 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
1012 dag_config_dispose(pd
->dag_ref
);
1014 * Note: we don't need to call close(p->fd) or dag_close(p->fd),
1015 * as dag_config_dispose(pd->dag_ref) does this.
1017 * Set p->fd to -1 to make sure that's not done.
1024 pcap_cleanup_live_common(p
);
1025 if (newDev
!= NULL
) {
1026 free((char *)newDev
);
1032 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
1040 /* Does this look like a DAG device? */
1041 cp
= strrchr(device
, '/');
1044 /* Does it begin with "dag"? */
1045 if (strncmp(cp
, "dag", 3) != 0) {
1046 /* Nope, doesn't begin with "dag" */
1050 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
1052 devnum
= strtol(cp
, &cpend
, 10);
1053 if (*cpend
== ':') {
1054 /* Followed by a stream number. */
1055 stream
= strtol(++cpend
, &cpend
, 10);
1058 if (cpend
== cp
|| *cpend
!= '\0') {
1059 /* Not followed by a number. */
1064 if (devnum
< 0 || devnum
>= DAG_MAX_BOARDS
) {
1065 /* Followed by a non-valid number. */
1070 if (stream
<0 || stream
>= DAG_STREAM_MAX
) {
1071 /* Followed by a non-valid stream number. */
1076 /* OK, it's probably ours. */
1079 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dag
);
1083 p
->activate_op
= dag_activate
;
1086 * We claim that we support microsecond and nanosecond time
1089 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1090 * power of two precisions yet. We can convert to either MICRO or NANO.
1092 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
1093 if (p
->tstamp_precision_list
== NULL
) {
1094 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1099 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
1100 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
1101 p
->tstamp_precision_count
= 2;
1106 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
1107 struct pcap_dag
*pd
= p
->priv
;
1108 uint32_t stream_drop
;
1109 dag_err_t dag_error
;
1112 * Packet records received (ps_recv) are counted in dag_read().
1113 * Packet records dropped (ps_drop) are read from Stream Drop attribute if present,
1114 * otherwise integrate the ERF Header lctr counts (if available) in dag_read().
1115 * We are reporting that no records are dropped by the card/driver (ps_ifdrop).
1118 if(pd
->drop_attr
!= kNullAttributeUuid
) {
1119 /* Note this counter is cleared at start of capture and will wrap at UINT_MAX.
1120 * The application is responsible for polling ps_drop frequently enough
1121 * to detect each wrap and integrate total drop with a wider counter */
1122 if ((dag_error
= dag_config_get_uint32_attribute_ex(pd
->dag_ref
, pd
->drop_attr
, &stream_drop
)) == kDagErrNone
) {
1123 pd
->stat
.ps_drop
= stream_drop
;
1125 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "reading stream drop attribute: %s",
1126 dag_config_strerror(dag_error
));
1137 * Add all DAG devices.
1140 dag_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1142 char name
[12]; /* XXX - pick a size */
1144 char dagname
[DAGNAME_BUFSIZE
];
1147 dag_card_inf_t
*inf
;
1149 int stream
, rxstreams
;
1151 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1152 for (c
= 0; c
< DAG_MAX_BOARDS
; c
++) {
1153 snprintf(name
, 12, "dag%d", c
);
1154 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
1156 (void) snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1157 "dag: device name %s can't be parsed", name
);
1160 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
1162 if ((inf
= dag_pciinfo(dagfd
)))
1163 description
= dag_device_name(inf
->device_code
, 1);
1165 * XXX - is there a way to determine whether
1166 * the card is plugged into a network or not?
1167 * If so, we should check that and set
1168 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
1169 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
1171 * Also, are there notions of "up" and "running"?
1173 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1179 rxstreams
= dag_rx_get_stream_count(dagfd
);
1180 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
1181 if (0 == dag_attach_stream64(dagfd
, stream
, 0, 0)) {
1182 dag_detach_stream(dagfd
, stream
);
1184 snprintf(name
, 10, "dag%d:%d", c
, stream
);
1185 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1193 if(rxstreams
<= 0) {
1206 dag_set_datalink(pcap_t
*p
, int dlt
)
1214 dag_setnonblock(pcap_t
*p
, int nonblock
)
1216 struct pcap_dag
*pd
= p
->priv
;
1218 struct timeval maxwait
;
1219 struct timeval poll
;
1222 * Set non-blocking mode on the FD.
1223 * XXX - is that necessary? If not, don't bother calling it,
1224 * and have a "dag_getnonblock()" function that looks at
1227 if (pcap_setnonblock_fd(p
, nonblock
) < 0)
1230 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
1231 &mindata
, &maxwait
, &poll
) < 0) {
1232 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1233 errno
, "dag_get_stream_poll");
1237 /* Amount of data to collect in Bytes before calling callbacks.
1238 * Important for efficiency, but can introduce latency
1239 * at low packet rates if to_ms not set!
1246 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
1247 mindata
, &maxwait
, &poll
) < 0) {
1248 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1249 errno
, "dag_set_stream_poll");
1254 pd
->dag_flags
|= DAGF_NONBLOCK
;
1256 pd
->dag_flags
&= ~DAGF_NONBLOCK
;
1262 dag_get_datalink(pcap_t
*p
)
1264 struct pcap_dag
*pd
= p
->priv
;
1265 int index
=0, dlt_index
=0;
1268 memset(types
, 0, 255);
1270 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1271 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1278 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1279 /* Get list of possible ERF types for this card */
1280 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1281 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1282 errno
, "dag_get_stream_erf_types");
1286 while (types
[index
]) {
1288 #elif defined HAVE_DAG_GET_ERF_TYPES
1289 /* Get list of possible ERF types for this card */
1290 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1291 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1292 errno
, "dag_get_erf_types");
1296 while (types
[index
]) {
1298 /* Check the type through a dagapi call. */
1299 types
[index
] = dag_linktype(p
->fd
);
1303 switch((types
[index
] & 0x7f)) {
1305 case ERF_TYPE_HDLC_POS
:
1306 case ERF_TYPE_COLOR_HDLC_POS
:
1307 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
1308 case ERF_TYPE_COLOR_HASH_POS
:
1310 if (p
->dlt_list
!= NULL
) {
1311 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1312 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1313 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1316 p
->linktype
= DLT_CHDLC
;
1320 case ERF_TYPE_COLOR_ETH
:
1321 case ERF_TYPE_DSM_COLOR_ETH
:
1322 case ERF_TYPE_COLOR_HASH_ETH
:
1324 * This is (presumably) a real Ethernet capture; give it a
1325 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1326 * that an application can let you choose it, in case you're
1327 * capturing DOCSIS traffic that a Cisco Cable Modem
1328 * Termination System is putting out onto an Ethernet (it
1329 * doesn't put an Ethernet header onto the wire, it puts raw
1330 * DOCSIS frames out on the wire inside the low-level
1331 * Ethernet framing).
1333 if (p
->dlt_list
!= NULL
) {
1334 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1335 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1338 p
->linktype
= DLT_EN10MB
;
1343 case ERF_TYPE_MC_ATM
:
1344 case ERF_TYPE_MC_AAL5
:
1345 if (p
->dlt_list
!= NULL
) {
1346 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1347 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1350 p
->linktype
= DLT_ATM_RFC1483
;
1353 case ERF_TYPE_COLOR_MC_HDLC_POS
:
1354 case ERF_TYPE_MC_HDLC
:
1355 if (p
->dlt_list
!= NULL
) {
1356 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1357 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1358 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1359 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1360 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1361 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1364 p
->linktype
= DLT_CHDLC
;
1368 if (p
->dlt_list
!= NULL
) {
1369 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1370 p
->dlt_list
[dlt_index
++] = DLT_IPV4
;
1373 p
->linktype
= DLT_RAW
;
1377 if (p
->dlt_list
!= NULL
) {
1378 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1379 p
->dlt_list
[dlt_index
++] = DLT_IPV6
;
1382 p
->linktype
= DLT_RAW
;
1385 case ERF_TYPE_LEGACY
:
1386 case ERF_TYPE_MC_RAW
:
1387 case ERF_TYPE_MC_RAW_CHANNEL
:
1388 case ERF_TYPE_IP_COUNTER
:
1389 case ERF_TYPE_TCP_FLOW_COUNTER
:
1390 case ERF_TYPE_INFINIBAND
:
1391 case ERF_TYPE_RAW_LINK
:
1392 case ERF_TYPE_INFINIBAND_LINK
:
1395 /* Libpcap cannot deal with these types yet */
1396 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1403 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1405 p
->dlt_count
= dlt_index
;
1408 p
->linktype
= DLT_ERF
;
1415 * This libpcap build supports only DAG cards, not regular network
1420 * There are no regular interfaces, just DAG interfaces.
1423 pcap_platform_finddevs(pcap_if_list_t
*devlistp _U_
, char *errbuf
)
1429 * Attempts to open a regular interface fail.
1432 pcap_create_interface(const char *device
, char *errbuf
)
1434 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1435 "This version of libpcap only supports DAG cards");
1440 * Libpcap version string.
1443 pcap_lib_version(void)
1445 return (PCAP_VERSION_STRING
" (DAG-only)");