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
58 #define ERF_TYPE_AAL5 4
61 #ifndef ERF_TYPE_MC_HDLC
62 #define ERF_TYPE_MC_HDLC 5
65 #ifndef ERF_TYPE_MC_RAW
66 #define ERF_TYPE_MC_RAW 6
69 #ifndef ERF_TYPE_MC_ATM
70 #define ERF_TYPE_MC_ATM 7
73 #ifndef ERF_TYPE_MC_RAW_CHANNEL
74 #define ERF_TYPE_MC_RAW_CHANNEL 8
77 #ifndef ERF_TYPE_MC_AAL5
78 #define ERF_TYPE_MC_AAL5 9
81 #ifndef ERF_TYPE_COLOR_HDLC_POS
82 #define ERF_TYPE_COLOR_HDLC_POS 10
85 #ifndef ERF_TYPE_COLOR_ETH
86 #define ERF_TYPE_COLOR_ETH 11
89 #ifndef ERF_TYPE_MC_AAL2
90 #define ERF_TYPE_MC_AAL2 12
93 #ifndef ERF_TYPE_IP_COUNTER
94 #define ERF_TYPE_IP_COUNTER 13
97 #ifndef ERF_TYPE_TCP_FLOW_COUNTER
98 #define ERF_TYPE_TCP_FLOW_COUNTER 14
101 #ifndef ERF_TYPE_DSM_COLOR_HDLC_POS
102 #define ERF_TYPE_DSM_COLOR_HDLC_POS 15
105 #ifndef ERF_TYPE_DSM_COLOR_ETH
106 #define ERF_TYPE_DSM_COLOR_ETH 16
109 #ifndef ERF_TYPE_COLOR_MC_HDLC_POS
110 #define ERF_TYPE_COLOR_MC_HDLC_POS 17
113 #ifndef ERF_TYPE_AAL2
114 #define ERF_TYPE_AAL2 18
117 #ifndef ERF_TYPE_COLOR_HASH_POS
118 #define ERF_TYPE_COLOR_HASH_POS 19
121 #ifndef ERF_TYPE_COLOR_HASH_ETH
122 #define ERF_TYPE_COLOR_HASH_ETH 20
125 #ifndef ERF_TYPE_INFINIBAND
126 #define ERF_TYPE_INFINIBAND 21
129 #ifndef ERF_TYPE_IPV4
130 #define ERF_TYPE_IPV4 22
133 #ifndef ERF_TYPE_IPV6
134 #define ERF_TYPE_IPV6 23
137 #ifndef ERF_TYPE_RAW_LINK
138 #define ERF_TYPE_RAW_LINK 24
141 #ifndef ERF_TYPE_INFINIBAND_LINK
142 #define ERF_TYPE_INFINIBAND_LINK 25
145 #ifndef ERF_TYPE_META
146 #define ERF_TYPE_META 27
150 #define ERF_TYPE_PAD 48
153 #define ATM_CELL_SIZE 52
154 #define ATM_HDR_SIZE 4
157 * A header containing additional MTP information.
159 #define MTP2_SENT_OFFSET 0 /* 1 byte */
160 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
161 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
162 #define MTP2_HDR_LEN 4 /* length of the header */
164 #define MTP2_ANNEX_A_NOT_USED 0
165 #define MTP2_ANNEX_A_USED 1
166 #define MTP2_ANNEX_A_USED_UNKNOWN 2
168 /* SunATM pseudo header */
170 unsigned char flags
; /* destination and traffic type */
171 unsigned char vpi
; /* VPI */
172 unsigned short vci
; /* VCI */
176 * Private data for capturing on DAG devices.
179 struct pcap_stat stat
;
180 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
181 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
182 int dag_fcs_bits
; /* Number of checksum bits from link layer */
183 int dag_flags
; /* Flags */
184 int dag_stream
; /* DAG stream number */
185 int dag_timeout
; /* timeout specified to pcap_open_live.
186 * Same as in linux above, introduce
190 typedef struct pcap_dag_node
{
191 struct pcap_dag_node
*next
;
196 static pcap_dag_node_t
*pcap_dags
= NULL
;
197 static int atexit_handler_installed
= 0;
198 static const unsigned short endian_test_word
= 0x0100;
200 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
202 #define MAX_DAG_PACKET 65536
204 static unsigned char TempPkt
[MAX_DAG_PACKET
];
206 #ifndef HAVE_DAG_LARGE_STREAMS_API
207 #define dag_attach_stream64(a, b, c, d) dag_attach_stream(a, b, c, d)
208 #define dag_get_stream_poll64(a, b, c, d, e) dag_get_stream_poll(a, b, c, d, e)
209 #define dag_set_stream_poll64(a, b, c, d, e) dag_set_stream_poll(a, b, c, d, e)
210 #define dag_size_t uint32_t
213 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
214 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
215 static int dag_set_datalink(pcap_t
*p
, int dlt
);
216 static int dag_get_datalink(pcap_t
*p
);
217 static int dag_setnonblock(pcap_t
*p
, int nonblock
);
220 delete_pcap_dag(pcap_t
*p
)
222 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
224 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
228 if (curr
!= NULL
&& curr
->p
== p
) {
230 prev
->next
= curr
->next
;
232 pcap_dags
= curr
->next
;
238 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
239 * in the pcap_t structure, and closes the file descriptor for the DAG card.
243 dag_platform_cleanup(pcap_t
*p
)
245 struct pcap_dag
*pd
= p
->priv
;
247 if(dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0)
248 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
250 if(dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
251 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
254 if(dag_close(p
->fd
) < 0)
255 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
259 pcap_cleanup_live_common(p
);
260 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
266 while (pcap_dags
!= NULL
) {
267 if (pcap_dags
->pid
== getpid()) {
268 if (pcap_dags
->p
!= NULL
)
269 dag_platform_cleanup(pcap_dags
->p
);
271 delete_pcap_dag(pcap_dags
->p
);
277 new_pcap_dag(pcap_t
*p
)
279 pcap_dag_node_t
*node
= NULL
;
281 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
285 if (!atexit_handler_installed
) {
286 atexit(atexit_handler
);
287 atexit_handler_installed
= 1;
290 node
->next
= pcap_dags
;
292 node
->pid
= getpid();
300 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
302 uint32_t hdr_num
= 0;
305 /* basic sanity checks */
311 /* check if we have any extension headers */
312 if ( (erf
[8] & 0x80) == 0x00 )
315 /* loop over the extension headers */
318 /* sanity check we have enough bytes */
319 if ( len
< (24 + (hdr_num
* 8)) )
322 /* get the header type */
323 hdr_type
= erf
[(16 + (hdr_num
* 8))];
326 } while ( hdr_type
& 0x80 );
332 * Read at most max_packets from the capture stream and call the callback
333 * for each of them. Returns the number of packets handled, -1 if an
334 * error occured, or -2 if we were told to break out of the loop.
337 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
339 struct pcap_dag
*pd
= p
->priv
;
340 unsigned int processed
= 0;
341 unsigned int nonblocking
= pd
->dag_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 /* dag_advance_stream() will block (unless nonblock is called)
362 * until 64kB of data has accumulated.
363 * If to_ms is set, it will timeout before 64kB has accumulated.
364 * We wait for 64kB because processing a few packets at a time
365 * can cause problems at high packet rates (>200kpps) due
367 * This does mean if to_ms is not specified the capture may 'hang'
368 * for long periods if the data rate is extremely slow (<64kB/sec)
369 * If non-block is specified it will return immediately. The user
370 * is then responsible for efficiency.
372 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
376 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
378 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
384 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
386 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
392 /* Process the packets. */
393 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
395 unsigned short packet_len
= 0;
397 struct pcap_pkthdr pcap_header
;
399 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
401 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
405 * Has "pcap_breakloop()" been called?
409 * Yes - clear the flag that indicates that
410 * it has, and return -2 to indicate that
411 * we were told to break out of the loop.
417 rlen
= ntohs(header
->rlen
);
418 if (rlen
< dag_record_size
)
420 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
423 pd
->dag_mem_bottom
+= rlen
;
425 /* Count lost packets. */
426 switch((header
->type
& 0x7f)) {
427 /* in these types the color value overwrites the lctr */
428 case ERF_TYPE_COLOR_HDLC_POS
:
429 case ERF_TYPE_COLOR_ETH
:
430 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
431 case ERF_TYPE_DSM_COLOR_ETH
:
432 case ERF_TYPE_COLOR_MC_HDLC_POS
:
433 case ERF_TYPE_COLOR_HASH_ETH
:
434 case ERF_TYPE_COLOR_HASH_POS
:
439 if (pd
->stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
440 pd
->stat
.ps_drop
= UINT_MAX
;
442 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
447 if ((header
->type
& 0x7f) == ERF_TYPE_PAD
) {
451 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
453 /* ERF encapsulation */
454 /* The Extensible Record Format is not dropped for this kind of encapsulation,
455 * and will be handled as a pseudo header by the decoding application.
456 * The information carried in the ERF header and in the optional subheader (if present)
457 * could be merged with the libpcap information, to offer a better decoding.
458 * The packet length is
459 * o the length of the packet on the link (header->wlen),
460 * o plus the length of the ERF header (dag_record_size), as the length of the
461 * pseudo header will be adjusted during the decoding,
462 * o plus the length of the optional subheader (if present).
464 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
465 * if the capture length is greater than the packet length.
467 if (p
->linktype
== DLT_ERF
) {
468 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
470 switch ((header
->type
& 0x7f)) {
471 case ERF_TYPE_MC_AAL5
:
472 case ERF_TYPE_MC_ATM
:
473 case ERF_TYPE_MC_HDLC
:
474 case ERF_TYPE_MC_RAW_CHANNEL
:
475 case ERF_TYPE_MC_RAW
:
476 case ERF_TYPE_MC_AAL2
:
477 case ERF_TYPE_COLOR_MC_HDLC_POS
:
478 packet_len
+= 4; /* MC header */
481 case ERF_TYPE_COLOR_HASH_ETH
:
482 case ERF_TYPE_DSM_COLOR_ETH
:
483 case ERF_TYPE_COLOR_ETH
:
485 packet_len
+= 2; /* ETH header */
489 /* Include ERF extension headers */
490 packet_len
+= (8 * num_ext_hdr
);
492 if (caplen
> packet_len
) {
496 /* Other kind of encapsulation according to the header Type */
498 /* Skip over generic ERF header */
499 dp
+= dag_record_size
;
500 /* Skip over extension headers */
501 dp
+= 8 * num_ext_hdr
;
503 switch((header
->type
& 0x7f)) {
506 if ((header
->type
& 0x7f) == ERF_TYPE_AAL5
) {
507 packet_len
= ntohs(header
->wlen
);
508 caplen
= rlen
- dag_record_size
;
510 case ERF_TYPE_MC_ATM
:
511 if ((header
->type
& 0x7f) == ERF_TYPE_MC_ATM
) {
512 caplen
= packet_len
= ATM_CELL_SIZE
;
515 case ERF_TYPE_MC_AAL5
:
516 if ((header
->type
& 0x7f) == ERF_TYPE_MC_AAL5
) {
517 packet_len
= ntohs(header
->wlen
);
518 caplen
= rlen
- dag_record_size
- 4;
521 /* Skip over extension headers */
522 caplen
-= (8 * num_ext_hdr
);
524 if ((header
->type
& 0x7f) == ERF_TYPE_ATM
) {
525 caplen
= packet_len
= ATM_CELL_SIZE
;
527 if (p
->linktype
== DLT_SUNATM
) {
528 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
529 unsigned long rawatm
;
531 rawatm
= ntohl(*((unsigned long *)dp
));
532 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
533 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
534 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
535 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
536 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
537 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
538 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
539 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
541 } else if (p
->linktype
== DLT_ATM_RFC1483
) {
542 packet_len
-= ATM_HDR_SIZE
;
543 caplen
-= ATM_HDR_SIZE
;
549 case ERF_TYPE_COLOR_HASH_ETH
:
550 case ERF_TYPE_DSM_COLOR_ETH
:
551 case ERF_TYPE_COLOR_ETH
:
553 if ((p
->linktype
!= DLT_EN10MB
) &&
554 (p
->linktype
!= DLT_DOCSIS
))
556 packet_len
= ntohs(header
->wlen
);
557 packet_len
-= (pd
->dag_fcs_bits
>> 3);
558 caplen
= rlen
- dag_record_size
- 2;
559 /* Skip over extension headers */
560 caplen
-= (8 * num_ext_hdr
);
561 if (caplen
> packet_len
) {
567 case ERF_TYPE_COLOR_HASH_POS
:
568 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
569 case ERF_TYPE_COLOR_HDLC_POS
:
570 case ERF_TYPE_HDLC_POS
:
571 if ((p
->linktype
!= DLT_CHDLC
) &&
572 (p
->linktype
!= DLT_PPP_SERIAL
) &&
573 (p
->linktype
!= DLT_FRELAY
))
575 packet_len
= ntohs(header
->wlen
);
576 packet_len
-= (pd
->dag_fcs_bits
>> 3);
577 caplen
= rlen
- dag_record_size
;
578 /* Skip over extension headers */
579 caplen
-= (8 * num_ext_hdr
);
580 if (caplen
> packet_len
) {
585 case ERF_TYPE_COLOR_MC_HDLC_POS
:
586 case ERF_TYPE_MC_HDLC
:
587 if ((p
->linktype
!= DLT_CHDLC
) &&
588 (p
->linktype
!= DLT_PPP_SERIAL
) &&
589 (p
->linktype
!= DLT_FRELAY
) &&
590 (p
->linktype
!= DLT_MTP2
) &&
591 (p
->linktype
!= DLT_MTP2_WITH_PHDR
) &&
592 (p
->linktype
!= DLT_LAPD
))
594 packet_len
= ntohs(header
->wlen
);
595 packet_len
-= (pd
->dag_fcs_bits
>> 3);
596 caplen
= rlen
- dag_record_size
- 4;
597 /* Skip over extension headers */
598 caplen
-= (8 * num_ext_hdr
);
599 if (caplen
> packet_len
) {
602 /* jump the MC_HDLC_HEADER */
604 #ifdef DLT_MTP2_WITH_PHDR
605 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
606 /* Add the MTP2 Pseudo Header */
607 caplen
+= MTP2_HDR_LEN
;
608 packet_len
+= MTP2_HDR_LEN
;
610 TempPkt
[MTP2_SENT_OFFSET
] = 0;
611 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
612 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
613 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
614 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
621 if ((p
->linktype
!= DLT_RAW
) &&
622 (p
->linktype
!= DLT_IPV4
))
624 packet_len
= ntohs(header
->wlen
);
625 caplen
= rlen
- dag_record_size
;
626 /* Skip over extension headers */
627 caplen
-= (8 * num_ext_hdr
);
628 if (caplen
> packet_len
) {
634 if ((p
->linktype
!= DLT_RAW
) &&
635 (p
->linktype
!= DLT_IPV6
))
637 packet_len
= ntohs(header
->wlen
);
638 caplen
= rlen
- dag_record_size
;
639 /* Skip over extension headers */
640 caplen
-= (8 * num_ext_hdr
);
641 if (caplen
> packet_len
) {
646 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
647 case ERF_TYPE_MC_RAW
:
648 case ERF_TYPE_MC_RAW_CHANNEL
:
649 case ERF_TYPE_IP_COUNTER
:
650 case ERF_TYPE_TCP_FLOW_COUNTER
:
651 case ERF_TYPE_INFINIBAND
:
652 case ERF_TYPE_RAW_LINK
:
653 case ERF_TYPE_INFINIBAND_LINK
:
655 /* Unhandled ERF type.
656 * Ignore rather than generating error
661 } /* ERF encapsulation */
663 if (caplen
> p
->snapshot
)
664 caplen
= p
->snapshot
;
666 /* Run the packet filter if there is one. */
667 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
669 /* convert between timestamp formats */
670 register unsigned long long ts
;
672 if (IS_BIGENDIAN()) {
673 ts
= SWAPLL(header
->ts
);
678 switch (p
->opt
.tstamp_precision
) {
679 case PCAP_TSTAMP_PRECISION_NANO
:
680 ticks_per_second
= 1000000000;
682 case PCAP_TSTAMP_PRECISION_MICRO
:
684 ticks_per_second
= 1000000;
688 pcap_header
.ts
.tv_sec
= ts
>> 32;
689 ts
= (ts
& 0xffffffffULL
) * ticks_per_second
;
690 ts
+= 0x80000000; /* rounding */
691 pcap_header
.ts
.tv_usec
= ts
>> 32;
692 if (pcap_header
.ts
.tv_usec
>= ticks_per_second
) {
693 pcap_header
.ts
.tv_usec
-= ticks_per_second
;
694 pcap_header
.ts
.tv_sec
++;
697 /* Fill in our own header data */
698 pcap_header
.caplen
= caplen
;
699 pcap_header
.len
= packet_len
;
701 /* Count the packet. */
704 /* Call the user supplied callback function */
705 callback(user
, &pcap_header
, dp
);
707 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
709 if (processed
== cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
711 /* Reached the user-specified limit. */
721 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
723 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
729 * Get a handle for a live capture from the given DAG device. Passing a NULL
730 * device will result in a failure. The promisc flag is ignored because DAG
731 * cards are always promiscuous. The to_ms parameter is used in setting the
732 * API polling parameters.
734 * snaplen is now also ignored, until we get per-stream slen support. Set
735 * slen with approprite DAG tool BEFORE pcap_activate().
739 static int dag_activate(pcap_t
* p
)
741 struct pcap_dag
*pd
= p
->priv
;
745 char * newDev
= NULL
;
746 char * device
= p
->opt
.device
;
748 struct timeval maxwait
;
751 if (device
== NULL
) {
752 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
756 /* Initialize some components of the pcap structure. */
757 newDev
= (char *)malloc(strlen(device
) + 16);
758 if (newDev
== NULL
) {
759 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s", pcap_strerror(errno
));
763 /* Parse input name to get dag device and stream number if provided */
764 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &pd
->dag_stream
) < 0) {
765 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s", pcap_strerror(errno
));
770 if (pd
->dag_stream
%2) {
771 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture");
775 /* setup device parameters */
776 if((p
->fd
= dag_open((char *)device
)) < 0) {
777 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
781 /* Open requested stream. Can fail if already locked or on error */
782 if (dag_attach_stream64(p
->fd
, pd
->dag_stream
, 0, 0) < 0) {
783 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s", pcap_strerror(errno
));
787 /* Set up default poll parameters for stream
788 * Can be overridden by pcap_set_nonblock()
790 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
791 &mindata
, &maxwait
, &poll
) < 0) {
792 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s", pcap_strerror(errno
));
797 * Turn a negative snapshot value (invalid), a snapshot value of
798 * 0 (unspecified), or a value bigger than the normal maximum
799 * value, into the maximum allowed value.
801 * If some application really *needs* a bigger snapshot
802 * length, we should just increase MAXIMUM_SNAPLEN.
804 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
805 p
->snapshot
= MAXIMUM_SNAPLEN
;
807 if (p
->opt
.immediate
) {
808 /* Call callback immediately.
809 * XXX - is this the right way to p this?
813 /* Amount of data to collect in Bytes before calling callbacks.
814 * Important for efficiency, but can introduce latency
815 * at low packet rates if to_ms not set!
820 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
821 * Recommend 10-100ms. Calls will time out even if no data arrived.
823 maxwait
.tv_sec
= p
->opt
.timeout
/1000;
824 maxwait
.tv_usec
= (p
->opt
.timeout
%1000) * 1000;
826 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
827 mindata
, &maxwait
, &poll
) < 0) {
828 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s", pcap_strerror(errno
));
832 /* XXX Not calling dag_configure() to set slen; this is unsafe in
833 * multi-stream environments as the gpp config is global.
834 * Once the firmware provides 'per-stream slen' this can be supported
835 * again via the Config API without side-effects */
837 /* set the card snap length to the specified snaplen parameter */
838 /* This is a really bad idea, as different cards have different
839 * valid slen ranges. Should fix in Config API. */
840 if (p
->snapshot
== 0 || p
->snapshot
> MAX_DAG_SNAPLEN
) {
841 p
->snapshot
= MAX_DAG_SNAPLEN
;
842 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
843 p
->snapshot
= MIN_DAG_SNAPLEN
;
845 /* snap len has to be a multiple of 4 */
848 if(dag_start_stream(p
->fd
, pd
->dag_stream
) < 0) {
849 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s", device
, pcap_strerror(errno
));
854 * Important! You have to ensure bottom is properly
855 * initialized to zero on startup, it won't give you
856 * a compiler warning if you make this mistake!
858 pd
->dag_mem_bottom
= 0;
862 * Find out how many FCS bits we should strip.
863 * First, query the card to see if it strips the FCS.
865 daginf
= dag_info(p
->fd
);
866 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
867 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
868 pd
->dag_fcs_bits
= 0;
870 /* Note that no FCS will be supplied. */
871 p
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
874 * Start out assuming it's 32 bits.
876 pd
->dag_fcs_bits
= 32;
878 /* Allow an environment variable to override. */
879 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
880 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
881 pd
->dag_fcs_bits
= n
;
883 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
884 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment", device
, n
);
890 * Did the user request that they not be stripped?
892 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
893 /* Yes. Note the number of bytes that will be
895 p
->linktype_ext
= LT_FCS_DATALINK_EXT(pd
->dag_fcs_bits
/16);
897 /* And don't strip them. */
898 pd
->dag_fcs_bits
= 0;
902 pd
->dag_timeout
= p
->opt
.timeout
;
905 if (dag_get_datalink(p
) < 0)
910 if (new_pcap_dag(p
) < 0) {
911 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s", device
, pcap_strerror(errno
));
916 * "select()" and "poll()" don't work on DAG device descriptors.
918 p
->selectable_fd
= -1;
920 if (newDev
!= NULL
) {
921 free((char *)newDev
);
924 p
->read_op
= dag_read
;
925 p
->inject_op
= dag_inject
;
926 p
->setfilter_op
= dag_setfilter
;
927 p
->setdirection_op
= NULL
; /* Not implemented.*/
928 p
->set_datalink_op
= dag_set_datalink
;
929 p
->getnonblock_op
= pcap_getnonblock_fd
;
930 p
->setnonblock_op
= dag_setnonblock
;
931 p
->stats_op
= dag_stats
;
932 p
->cleanup_op
= dag_platform_cleanup
;
933 pd
->stat
.ps_drop
= 0;
934 pd
->stat
.ps_recv
= 0;
935 pd
->stat
.ps_ifdrop
= 0;
939 if (dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0) {
940 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
944 if (dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
945 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
948 if (dag_close(p
->fd
) < 0)
949 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
953 pcap_cleanup_live_common(p
);
954 if (newDev
!= NULL
) {
955 free((char *)newDev
);
961 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
969 /* Does this look like a DAG device? */
970 cp
= strrchr(device
, '/');
973 /* Does it begin with "dag"? */
974 if (strncmp(cp
, "dag", 3) != 0) {
975 /* Nope, doesn't begin with "dag" */
979 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
981 devnum
= strtol(cp
, &cpend
, 10);
983 /* Followed by a stream number. */
984 stream
= strtol(++cpend
, &cpend
, 10);
987 if (cpend
== cp
|| *cpend
!= '\0') {
988 /* Not followed by a number. */
993 if (devnum
< 0 || devnum
>= DAG_MAX_BOARDS
) {
994 /* Followed by a non-valid number. */
999 if (stream
<0 || stream
>= DAG_STREAM_MAX
) {
1000 /* Followed by a non-valid stream number. */
1005 /* OK, it's probably ours. */
1008 p
= pcap_create_common(ebuf
, sizeof (struct pcap_dag
));
1012 p
->activate_op
= dag_activate
;
1015 * We claim that we support microsecond and nanosecond time
1018 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1019 * power of two precisions yet. We can convert to either MICRO or NANO.
1021 p
->tstamp_precision_count
= 2;
1022 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
1023 if (p
->tstamp_precision_list
== NULL
) {
1024 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1025 pcap_strerror(errno
));
1029 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
1030 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
1035 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
1036 struct pcap_dag
*pd
= p
->priv
;
1038 /* This needs to be filled out correctly. Hopefully a dagapi call will
1039 provide all necessary information.
1041 /*pd->stat.ps_recv = 0;*/
1042 /*pd->stat.ps_drop = 0;*/
1050 * Add all DAG devices.
1053 dag_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1055 char name
[12]; /* XXX - pick a size */
1058 char dagname
[DAGNAME_BUFSIZE
];
1061 dag_card_inf_t
*inf
;
1063 int stream
, rxstreams
;
1065 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1066 for (c
= 0; c
< DAG_MAX_BOARDS
; c
++) {
1067 pcap_snprintf(name
, 12, "dag%d", c
);
1068 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
1070 (void) pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1071 "dag: device name %s can't be parsed", name
);
1074 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
1076 if ((inf
= dag_pciinfo(dagfd
)))
1077 description
= dag_device_name(inf
->device_code
, 1);
1078 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1084 rxstreams
= dag_rx_get_stream_count(dagfd
);
1085 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
1086 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
1087 dag_detach_stream(dagfd
, stream
);
1089 pcap_snprintf(name
, 10, "dag%d:%d", c
, stream
);
1090 if (add_dev(devlistp
, name
, 0, description
, errbuf
) == NULL
) {
1098 if(rxstreams
<= 0) {
1111 * Installs the given bpf filter program in the given pcap structure. There is
1112 * no attempt to store the filter in kernel memory as that is not supported
1116 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
1121 strncpy(p
->errbuf
, "setfilter: No filter specified",
1126 /* Make our private copy of the filter */
1128 if (install_bpf_program(p
, fp
) < 0)
1135 dag_set_datalink(pcap_t
*p
, int dlt
)
1143 dag_setnonblock(pcap_t
*p
, int nonblock
)
1145 struct pcap_dag
*pd
= p
->priv
;
1147 struct timeval maxwait
;
1148 struct timeval poll
;
1151 * Set non-blocking mode on the FD.
1152 * XXX - is that necessary? If not, don't bother calling it,
1153 * and have a "dag_getnonblock()" function that looks at
1156 if (pcap_setnonblock_fd(p
, nonblock
) < 0)
1159 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
1160 &mindata
, &maxwait
, &poll
) < 0) {
1161 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s", pcap_strerror(errno
));
1165 /* Amount of data to collect in Bytes before calling callbacks.
1166 * Important for efficiency, but can introduce latency
1167 * at low packet rates if to_ms not set!
1174 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
1175 mindata
, &maxwait
, &poll
) < 0) {
1176 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s", pcap_strerror(errno
));
1181 pd
->dag_flags
|= DAGF_NONBLOCK
;
1183 pd
->dag_flags
&= ~DAGF_NONBLOCK
;
1189 dag_get_datalink(pcap_t
*p
)
1191 struct pcap_dag
*pd
= p
->priv
;
1192 int index
=0, dlt_index
=0;
1195 memset(types
, 0, 255);
1197 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1198 (void)pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1204 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1205 /* Get list of possible ERF types for this card */
1206 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1207 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1211 while (types
[index
]) {
1213 #elif defined HAVE_DAG_GET_ERF_TYPES
1214 /* Get list of possible ERF types for this card */
1215 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1216 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1220 while (types
[index
]) {
1222 /* Check the type through a dagapi call. */
1223 types
[index
] = dag_linktype(p
->fd
);
1227 switch((types
[index
] & 0x7f)) {
1229 case ERF_TYPE_HDLC_POS
:
1230 case ERF_TYPE_COLOR_HDLC_POS
:
1231 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
1232 case ERF_TYPE_COLOR_HASH_POS
:
1234 if (p
->dlt_list
!= NULL
) {
1235 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1236 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1237 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1240 p
->linktype
= DLT_CHDLC
;
1244 case ERF_TYPE_COLOR_ETH
:
1245 case ERF_TYPE_DSM_COLOR_ETH
:
1246 case ERF_TYPE_COLOR_HASH_ETH
:
1248 * This is (presumably) a real Ethernet capture; give it a
1249 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1250 * that an application can let you choose it, in case you're
1251 * capturing DOCSIS traffic that a Cisco Cable Modem
1252 * Termination System is putting out onto an Ethernet (it
1253 * doesn't put an Ethernet header onto the wire, it puts raw
1254 * DOCSIS frames out on the wire inside the low-level
1255 * Ethernet framing).
1257 if (p
->dlt_list
!= NULL
) {
1258 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1259 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1262 p
->linktype
= DLT_EN10MB
;
1267 case ERF_TYPE_MC_ATM
:
1268 case ERF_TYPE_MC_AAL5
:
1269 if (p
->dlt_list
!= NULL
) {
1270 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1271 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1274 p
->linktype
= DLT_ATM_RFC1483
;
1277 case ERF_TYPE_COLOR_MC_HDLC_POS
:
1278 case ERF_TYPE_MC_HDLC
:
1279 if (p
->dlt_list
!= NULL
) {
1280 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1281 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1282 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1283 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1284 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1285 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1288 p
->linktype
= DLT_CHDLC
;
1292 if (p
->dlt_list
!= NULL
) {
1293 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1294 p
->dlt_list
[dlt_index
++] = DLT_IPV4
;
1297 p
->linktype
= DLT_RAW
;
1301 if (p
->dlt_list
!= NULL
) {
1302 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1303 p
->dlt_list
[dlt_index
++] = DLT_IPV6
;
1306 p
->linktype
= DLT_RAW
;
1309 case ERF_TYPE_LEGACY
:
1310 case ERF_TYPE_MC_RAW
:
1311 case ERF_TYPE_MC_RAW_CHANNEL
:
1312 case ERF_TYPE_IP_COUNTER
:
1313 case ERF_TYPE_TCP_FLOW_COUNTER
:
1314 case ERF_TYPE_INFINIBAND
:
1315 case ERF_TYPE_RAW_LINK
:
1316 case ERF_TYPE_INFINIBAND_LINK
:
1319 /* Libpcap cannot deal with these types yet */
1320 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1327 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1329 p
->dlt_count
= dlt_index
;
1332 p
->linktype
= DLT_ERF
;
1339 * This libpcap build supports only DAG cards, not regular network
1344 * There are no regular interfaces, just DAG interfaces.
1347 pcap_platform_finddevs(pcap_if_list_t
*devlistp _U_
, char *errbuf
)
1353 * Attempts to open a regular interface fail.
1356 pcap_create_interface(const char *device
, char *errbuf
)
1358 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1359 "This version of libpcap only supports DAG cards");
1363 #include "pcap_version.h"
1366 * Libpcap version string.
1369 pcap_lib_version(void)
1371 return (PCAP_VERSION_STRING
" (DAG-only)");