2 * pcap-dag.c: Packet capture interface for Endace DAG card.
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
7 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
8 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
9 * is not defined then nothing is altered - the dag_ functions will be
10 * called as required from their pcap-linux/bpf equivalents.
12 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
19 static const char rcsid
[] _U_
=
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.39 2008-04-14 20:40:58 guy Exp $ (LBL)";
27 #include <sys/param.h> /* optionally get BSD define */
36 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
42 struct mbuf
; /* Squelch compiler warnings on some platforms for */
43 struct rtentry
; /* declarations in <net/if.h> */
52 * DAG devices have names beginning with "dag", followed by a number
57 #define ATM_CELL_SIZE 52
58 #define ATM_HDR_SIZE 4
61 * A header containing additional MTP information.
63 #define MTP2_SENT_OFFSET 0 /* 1 byte */
64 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
65 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
66 #define MTP2_HDR_LEN 4 /* length of the header */
68 #define MTP2_ANNEX_A_NOT_USED 0
69 #define MTP2_ANNEX_A_USED 1
70 #define MTP2_ANNEX_A_USED_UNKNOWN 2
72 /* SunATM pseudo header */
74 unsigned char flags
; /* destination and traffic type */
75 unsigned char vpi
; /* VPI */
76 unsigned short vci
; /* VCI */
79 typedef struct pcap_dag_node
{
80 struct pcap_dag_node
*next
;
85 static pcap_dag_node_t
*pcap_dags
= NULL
;
86 static int atexit_handler_installed
= 0;
87 static const unsigned short endian_test_word
= 0x0100;
89 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
91 #define MAX_DAG_PACKET 65536
93 static unsigned char TempPkt
[MAX_DAG_PACKET
];
95 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
96 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
97 static int dag_set_datalink(pcap_t
*p
, int dlt
);
98 static int dag_get_datalink(pcap_t
*p
);
99 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
102 delete_pcap_dag(pcap_t
*p
)
104 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
106 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
110 if (curr
!= NULL
&& curr
->p
== p
) {
112 prev
->next
= curr
->next
;
114 pcap_dags
= curr
->next
;
120 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
121 * in the pcap_t structure, and closes the file descriptor for the DAG card.
125 dag_platform_cleanup(pcap_t
*p
)
129 #ifdef HAVE_DAG_STREAMS_API
130 if(dag_stop_stream(p
->fd
, p
->md
.dag_stream
) < 0)
131 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
133 if(dag_detach_stream(p
->fd
, p
->md
.dag_stream
) < 0)
134 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
136 if(dag_stop(p
->fd
) < 0)
137 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
138 #endif /* HAVE_DAG_STREAMS_API */
140 if(dag_close(p
->fd
) < 0)
141 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
145 pcap_cleanup_live_common(p
);
147 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
153 while (pcap_dags
!= NULL
) {
154 if (pcap_dags
->pid
== getpid()) {
155 dag_platform_cleanup(pcap_dags
->p
);
157 delete_pcap_dag(pcap_dags
->p
);
163 new_pcap_dag(pcap_t
*p
)
165 pcap_dag_node_t
*node
= NULL
;
167 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
171 if (!atexit_handler_installed
) {
172 atexit(atexit_handler
);
173 atexit_handler_installed
= 1;
176 node
->next
= pcap_dags
;
178 node
->pid
= getpid();
186 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
188 uint32_t hdr_num
= 0;
191 /* basic sanity checks */
197 /* check if we have any extension headers */
198 if ( (erf
[8] & 0x80) == 0x00 )
201 /* loop over the extension headers */
204 /* sanity check we have enough bytes */
205 if ( len
<= (24 + (hdr_num
* 8)) )
208 /* get the header type */
209 hdr_type
= erf
[(16 + (hdr_num
* 8))];
212 } while ( hdr_type
& 0x80 );
218 * Read at most max_packets from the capture stream and call the callback
219 * for each of them. Returns the number of packets handled, -1 if an
220 * error occured, or -2 if we were told to break out of the loop.
223 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
225 unsigned int processed
= 0;
226 int flags
= p
->md
.dag_offset_flags
;
227 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
228 unsigned int num_ext_hdr
= 0;
230 /* Get the next bufferful of packets (if necessary). */
231 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) {
234 * Has "pcap_breakloop()" been called?
238 * Yes - clear the flag that indicates that
239 * it has, and return -2 to indicate that
240 * we were told to break out of the loop.
246 #ifdef HAVE_DAG_STREAMS_API
247 /* dag_advance_stream() will block (unless nonblock is called)
248 * until 64kB of data has accumulated.
249 * If to_ms is set, it will timeout before 64kB has accumulated.
250 * We wait for 64kB because processing a few packets at a time
251 * can cause problems at high packet rates (>200kpps) due
253 * This does mean if to_ms is not specified the capture may 'hang'
254 * for long periods if the data rate is extremely slow (<64kB/sec)
255 * If non-block is specified it will return immediately. The user
256 * is then responsible for efficiency.
258 if ( NULL
== (p
->md
.dag_mem_top
= dag_advance_stream(p
->fd
, p
->md
.dag_stream
, &(p
->md
.dag_mem_bottom
))) ) {
262 /* dag_offset does not support timeouts */
263 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), flags
);
264 #endif /* HAVE_DAG_STREAMS_API */
266 if (nonblocking
&& (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
268 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
274 (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
276 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
282 /* Process the packets. */
283 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
>= dag_record_size
) {
285 unsigned short packet_len
= 0;
287 struct pcap_pkthdr pcap_header
;
289 #ifdef HAVE_DAG_STREAMS_API
290 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_bottom
);
292 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
293 #endif /* HAVE_DAG_STREAMS_API */
295 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
299 * Has "pcap_breakloop()" been called?
303 * Yes - clear the flag that indicates that
304 * it has, and return -2 to indicate that
305 * we were told to break out of the loop.
311 rlen
= ntohs(header
->rlen
);
312 if (rlen
< dag_record_size
)
314 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
317 p
->md
.dag_mem_bottom
+= rlen
;
319 /* Count lost packets. */
320 switch((header
->type
& 0x7f)) {
321 /* in these types the color value overwrites the lctr */
322 case TYPE_COLOR_HDLC_POS
:
324 case TYPE_DSM_COLOR_HDLC_POS
:
325 case TYPE_DSM_COLOR_ETH
:
326 case TYPE_COLOR_MC_HDLC_POS
:
327 case TYPE_COLOR_HASH_ETH
:
328 case TYPE_COLOR_HASH_POS
:
333 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
334 p
->md
.stat
.ps_drop
= UINT_MAX
;
336 p
->md
.stat
.ps_drop
+= ntohs(header
->lctr
);
341 if ((header
->type
& 0x7f) == TYPE_PAD
) {
345 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
347 /* ERF encapsulation */
348 /* The Extensible Record Format is not dropped for this kind of encapsulation,
349 * and will be handled as a pseudo header by the decoding application.
350 * The information carried in the ERF header and in the optional subheader (if present)
351 * could be merged with the libpcap information, to offer a better decoding.
352 * The packet length is
353 * o the length of the packet on the link (header->wlen),
354 * o plus the length of the ERF header (dag_record_size), as the length of the
355 * pseudo header will be adjusted during the decoding,
356 * o plus the length of the optional subheader (if present).
358 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
359 * if the capture length is greater than the packet length.
361 if (p
->linktype
== DLT_ERF
) {
362 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
364 switch ((header
->type
& 0x7f)) {
368 case TYPE_MC_RAW_CHANNEL
:
371 case TYPE_COLOR_MC_HDLC_POS
:
372 packet_len
+= 4; /* MC header */
375 case TYPE_COLOR_HASH_ETH
:
376 case TYPE_DSM_COLOR_ETH
:
379 packet_len
+= 2; /* ETH header */
383 /* Include ERF extension headers */
384 packet_len
+= (8 * num_ext_hdr
);
386 if (caplen
> packet_len
) {
390 /* Other kind of encapsulation according to the header Type */
392 /* Skip over generic ERF header */
393 dp
+= dag_record_size
;
394 /* Skip over extension headers */
395 dp
+= 8 * num_ext_hdr
;
397 switch((header
->type
& 0x7f)) {
400 if (header
->type
== TYPE_AAL5
) {
401 packet_len
= ntohs(header
->wlen
);
402 caplen
= rlen
- dag_record_size
;
405 if (header
->type
== TYPE_MC_ATM
) {
406 caplen
= packet_len
= ATM_CELL_SIZE
;
410 if (header
->type
== TYPE_MC_AAL5
) {
411 packet_len
= ntohs(header
->wlen
);
412 caplen
= rlen
- dag_record_size
- 4;
415 if (header
->type
== TYPE_ATM
) {
416 caplen
= packet_len
= ATM_CELL_SIZE
;
418 if (p
->linktype
== DLT_SUNATM
) {
419 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
420 unsigned long rawatm
;
422 rawatm
= ntohl(*((unsigned long *)dp
));
423 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
424 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
425 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
426 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
427 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
428 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
429 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
430 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
433 packet_len
-= ATM_HDR_SIZE
;
434 caplen
-= ATM_HDR_SIZE
;
439 case TYPE_COLOR_HASH_ETH
:
440 case TYPE_DSM_COLOR_ETH
:
443 packet_len
= ntohs(header
->wlen
);
444 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
445 caplen
= rlen
- dag_record_size
- 2;
446 if (caplen
> packet_len
) {
452 case TYPE_COLOR_HASH_POS
:
453 case TYPE_DSM_COLOR_HDLC_POS
:
454 case TYPE_COLOR_HDLC_POS
:
456 packet_len
= ntohs(header
->wlen
);
457 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
458 caplen
= rlen
- dag_record_size
;
459 if (caplen
> packet_len
) {
464 case TYPE_COLOR_MC_HDLC_POS
:
466 packet_len
= ntohs(header
->wlen
);
467 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
468 caplen
= rlen
- dag_record_size
- 4;
469 if (caplen
> packet_len
) {
472 /* jump the MC_HDLC_HEADER */
474 #ifdef DLT_MTP2_WITH_PHDR
475 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
476 /* Add the MTP2 Pseudo Header */
477 caplen
+= MTP2_HDR_LEN
;
478 packet_len
+= MTP2_HDR_LEN
;
480 TempPkt
[MTP2_SENT_OFFSET
] = 0;
481 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
482 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
483 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
484 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
492 packet_len
= ntohs(header
->wlen
);
493 caplen
= rlen
- dag_record_size
;
494 if (caplen
> packet_len
) {
499 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
501 case TYPE_MC_RAW_CHANNEL
:
502 case TYPE_IP_COUNTER
:
503 case TYPE_TCP_FLOW_COUNTER
:
504 case TYPE_INFINIBAND
:
506 case TYPE_INFINIBAND_LINK
:
508 /* Unhandled ERF type.
509 * Ignore rather than generating error
514 /* Skip over extension headers */
515 caplen
-= (8 * num_ext_hdr
);
517 } /* ERF encapsulation */
519 if (caplen
> p
->snapshot
)
520 caplen
= p
->snapshot
;
522 /* Run the packet filter if there is one. */
523 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
525 /* convert between timestamp formats */
526 register unsigned long long ts
;
528 if (IS_BIGENDIAN()) {
529 ts
= SWAPLL(header
->ts
);
534 pcap_header
.ts
.tv_sec
= ts
>> 32;
535 ts
= (ts
& 0xffffffffULL
) * 1000000;
536 ts
+= 0x80000000; /* rounding */
537 pcap_header
.ts
.tv_usec
= ts
>> 32;
538 if (pcap_header
.ts
.tv_usec
>= 1000000) {
539 pcap_header
.ts
.tv_usec
-= 1000000;
540 pcap_header
.ts
.tv_sec
++;
543 /* Fill in our own header data */
544 pcap_header
.caplen
= caplen
;
545 pcap_header
.len
= packet_len
;
547 /* Count the packet. */
548 p
->md
.stat
.ps_recv
++;
550 /* Call the user supplied callback function */
551 callback(user
, &pcap_header
, dp
);
553 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
555 if (processed
== cnt
&& cnt
> 0)
557 /* Reached the user-specified limit. */
567 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
569 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
575 * Get a handle for a live capture from the given DAG device. Passing a NULL
576 * device will result in a failure. The promisc flag is ignored because DAG
577 * cards are always promiscuous. The to_ms parameter is used in setting the
578 * API polling parameters.
580 * snaplen is now also ignored, until we get per-stream slen support. Set
581 * slen with approprite DAG tool BEFORE pcap_activate().
585 static int dag_activate(pcap_t
* handle
)
588 char conf
[30]; /* dag configure string */
593 char * newDev
= NULL
;
594 char * device
= handle
->opt
.source
;
595 #ifdef HAVE_DAG_STREAMS_API
597 struct timeval maxwait
;
601 if (device
== NULL
) {
602 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
606 /* Initialize some components of the pcap structure. */
608 #ifdef HAVE_DAG_STREAMS_API
609 newDev
= (char *)malloc(strlen(device
) + 16);
610 if (newDev
== NULL
) {
611 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
615 /* Parse input name to get dag device and stream number if provided */
616 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handle
->md
.dag_stream
) < 0) {
617 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
622 if (handle
->md
.dag_stream
%2) {
623 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
627 if (strncmp(device
, "/dev/", 5) != 0) {
628 newDev
= (char *)malloc(strlen(device
) + 5);
629 if (newDev
== NULL
) {
630 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
633 strcpy(newDev
, "/dev/");
634 strcat(newDev
, device
);
637 #endif /* HAVE_DAG_STREAMS_API */
639 /* setup device parameters */
640 if((handle
->fd
= dag_open((char *)device
)) < 0) {
641 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
645 #ifdef HAVE_DAG_STREAMS_API
646 /* Open requested stream. Can fail if already locked or on error */
647 if (dag_attach_stream(handle
->fd
, handle
->md
.dag_stream
, 0, 0) < 0) {
648 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
652 /* Set up default poll parameters for stream
653 * Can be overridden by pcap_set_nonblock()
655 if (dag_get_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
656 &mindata
, &maxwait
, &poll
) < 0) {
657 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
661 /* Amount of data to collect in Bytes before calling callbacks.
662 * Important for efficiency, but can introduce latency
663 * at low packet rates if to_ms not set!
667 /* Obey md.timeout (was to_ms) if supplied. This is a good idea!
668 * Recommend 10-100ms. Calls will time out even if no data arrived.
670 maxwait
.tv_sec
= handle
->md
.timeout
/1000;
671 maxwait
.tv_usec
= (handle
->md
.timeout
%1000) * 1000;
673 if (dag_set_stream_poll(handle
->fd
, handle
->md
.dag_stream
,
674 mindata
, &maxwait
, &poll
) < 0) {
675 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
680 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
681 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
685 #endif /* HAVE_DAG_STREAMS_API */
687 /* XXX Not calling dag_configure() to set slen; this is unsafe in
688 * multi-stream environments as the gpp config is global.
689 * Once the firmware provides 'per-stream slen' this can be supported
690 * again via the Config API without side-effects */
692 /* set the card snap length to the specified snaplen parameter */
693 /* This is a really bad idea, as different cards have different
694 * valid slen ranges. Should fix in Config API. */
695 if (handle
->snapshot
== 0 || handle
->snapshot
> MAX_DAG_SNAPLEN
) {
696 handle
->snapshot
= MAX_DAG_SNAPLEN
;
697 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
698 handle
->snapshot
= MIN_DAG_SNAPLEN
;
700 /* snap len has to be a multiple of 4 */
701 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
703 if(dag_configure(handle
->fd
, conf
) < 0) {
704 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
709 #ifdef HAVE_DAG_STREAMS_API
710 if(dag_start_stream(handle
->fd
, handle
->md
.dag_stream
) < 0) {
711 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
715 if(dag_start(handle
->fd
) < 0) {
716 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
719 #endif /* HAVE_DAG_STREAMS_API */
722 * Important! You have to ensure bottom is properly
723 * initialized to zero on startup, it won't give you
724 * a compiler warning if you make this mistake!
726 handle
->md
.dag_mem_bottom
= 0;
727 handle
->md
.dag_mem_top
= 0;
730 * Find out how many FCS bits we should strip.
731 * First, query the card to see if it strips the FCS.
733 daginf
= dag_info(handle
->fd
);
734 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
735 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
736 handle
->md
.dag_fcs_bits
= 0;
738 /* Note that no FCS will be supplied. */
739 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
742 * Start out assuming it's 32 bits.
744 handle
->md
.dag_fcs_bits
= 32;
746 /* Allow an environment variable to override. */
747 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
748 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
749 handle
->md
.dag_fcs_bits
= n
;
751 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
752 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
758 * Did the user request that they not be stripped?
760 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
761 /* Yes. Note the number of bytes that will be
763 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handle
->md
.dag_fcs_bits
/16);
765 /* And don't strip them. */
766 handle
->md
.dag_fcs_bits
= 0;
770 handle
->md
.dag_timeout
= handle
->md
.timeout
;
772 handle
->linktype
= -1;
773 if (dag_get_datalink(handle
) < 0)
778 if (new_pcap_dag(handle
) < 0) {
779 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
784 * "select()" and "poll()" don't work on DAG device descriptors.
786 handle
->selectable_fd
= -1;
788 if (newDev
!= NULL
) {
789 free((char *)newDev
);
792 handle
->read_op
= dag_read
;
793 handle
->inject_op
= dag_inject
;
794 handle
->setfilter_op
= dag_setfilter
;
795 handle
->setdirection_op
= NULL
; /* Not implemented.*/
796 handle
->set_datalink_op
= dag_set_datalink
;
797 handle
->getnonblock_op
= pcap_getnonblock_fd
;
798 handle
->setnonblock_op
= dag_setnonblock
;
799 handle
->stats_op
= dag_stats
;
800 handle
->cleanup_op
= dag_platform_cleanup
;
801 handle
->md
.stat
.ps_drop
= 0;
802 handle
->md
.stat
.ps_recv
= 0;
803 handle
->md
.stat
.ps_ifdrop
= 0;
806 #ifdef HAVE_DAG_STREAMS_API
808 if (dag_stop_stream(handle
->fd
, handle
->md
.dag_stream
) < 0) {
809 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
813 if (dag_detach_stream(handle
->fd
, handle
->md
.dag_stream
) < 0)
814 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
817 if (dag_stop(handle
->fd
) < 0)
818 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
819 #endif /* HAVE_DAG_STREAMS_API */
822 if (dag_close(handle
->fd
) < 0)
823 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
824 delete_pcap_dag(handle
);
827 pcap_cleanup_live_common(handle
);
828 if (newDev
!= NULL
) {
829 free((char *)newDev
);
835 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
841 /* Does this look like a DAG device? */
842 cp
= strrchr(device
, '/');
845 /* Does it begin with "dag"? */
846 if (strncmp(cp
, "dag", 3) != 0) {
847 /* Nope, doesn't begin with "dag" */
851 /* Yes - is "dag" followed by a number from 0 to MAXDAG? */
853 devnum
= strtol(cp
, &cpend
, 10);
854 if (cpend
== cp
|| *cpend
!= '\0') {
855 /* Not followed by a number. */
859 if (devnum
< 0 || devnum
> MAXDAG
) {
860 /* Followed by a non-valid number. */
865 /* OK, it's probably ours. */
868 p
= pcap_create_common(device
, ebuf
);
872 p
->activate_op
= dag_activate
;
877 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
878 /* This needs to be filled out correctly. Hopefully a dagapi call will
879 provide all necessary information.
881 /*p->md.stat.ps_recv = 0;*/
882 /*p->md.stat.ps_drop = 0;*/
890 * Previously we just generated a list of all possible names and let
891 * pcap_add_if() attempt to open each one, but with streams this adds up
892 * to 81 possibilities which is inefficient.
894 * Since we know more about the devices we can prune the tree here.
895 * pcap_add_if() will still retest each device but the total number of
896 * open attempts will still be much less than the naive approach.
899 dag_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
901 char name
[12]; /* XXX - pick a size */
904 char dagname
[DAGNAME_BUFSIZE
];
908 /* Try all the DAGs 0-MAXDAG */
909 for (c
= 0; c
<= MAXDAG
; c
++) {
910 snprintf(name
, 12, "dag%d", c
);
911 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
915 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
916 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
922 #ifdef HAVE_DAG_STREAMS_API
924 int stream
, rxstreams
;
925 rxstreams
= dag_rx_get_stream_count(dagfd
);
926 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
927 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
928 dag_detach_stream(dagfd
, stream
);
930 snprintf(name
, 10, "dag%d:%d", c
, stream
);
931 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
945 #endif /* HAVE_DAG_STREAMS_API */
954 * Installs the given bpf filter program in the given pcap structure. There is
955 * no attempt to store the filter in kernel memory as that is not supported
959 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
964 strncpy(p
->errbuf
, "setfilter: No filter specified",
969 /* Make our private copy of the filter */
971 if (install_bpf_program(p
, fp
) < 0)
980 dag_set_datalink(pcap_t
*p
, int dlt
)
988 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
991 * Set non-blocking mode on the FD.
992 * XXX - is that necessary? If not, don't bother calling it,
993 * and have a "dag_getnonblock()" function that looks at
994 * "p->md.dag_offset_flags".
996 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
998 #ifdef HAVE_DAG_STREAMS_API
1001 struct timeval maxwait
;
1002 struct timeval poll
;
1004 if (dag_get_stream_poll(p
->fd
, p
->md
.dag_stream
,
1005 &mindata
, &maxwait
, &poll
) < 0) {
1006 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
1010 /* Amount of data to collect in Bytes before calling callbacks.
1011 * Important for efficiency, but can introduce latency
1012 * at low packet rates if to_ms not set!
1019 if (dag_set_stream_poll(p
->fd
, p
->md
.dag_stream
,
1020 mindata
, &maxwait
, &poll
) < 0) {
1021 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
1025 #endif /* HAVE_DAG_STREAMS_API */
1027 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
1029 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
1035 dag_get_datalink(pcap_t
*p
)
1037 int index
=0, dlt_index
=0;
1040 memset(types
, 0, 255);
1042 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1043 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1049 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1050 /* Get list of possible ERF types for this card */
1051 if (dag_get_stream_erf_types(p
->fd
, p
->md
.dag_stream
, types
, 255) < 0) {
1052 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1056 while (types
[index
]) {
1058 #elif defined HAVE_DAG_GET_ERF_TYPES
1059 /* Get list of possible ERF types for this card */
1060 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1061 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1065 while (types
[index
]) {
1067 /* Check the type through a dagapi call. */
1068 types
[index
] = dag_linktype(p
->fd
);
1072 switch((types
[index
] & 0x7f)) {
1075 case TYPE_COLOR_HDLC_POS
:
1076 case TYPE_DSM_COLOR_HDLC_POS
:
1077 case TYPE_COLOR_HASH_POS
:
1079 if (p
->dlt_list
!= NULL
) {
1080 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1081 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1082 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1085 p
->linktype
= DLT_CHDLC
;
1089 case TYPE_COLOR_ETH
:
1090 case TYPE_DSM_COLOR_ETH
:
1091 case TYPE_COLOR_HASH_ETH
:
1093 * This is (presumably) a real Ethernet capture; give it a
1094 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1095 * that an application can let you choose it, in case you're
1096 * capturing DOCSIS traffic that a Cisco Cable Modem
1097 * Termination System is putting out onto an Ethernet (it
1098 * doesn't put an Ethernet header onto the wire, it puts raw
1099 * DOCSIS frames out on the wire inside the low-level
1100 * Ethernet framing).
1102 if (p
->dlt_list
!= NULL
) {
1103 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1104 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1107 p
->linktype
= DLT_EN10MB
;
1114 if (p
->dlt_list
!= NULL
) {
1115 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1116 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1119 p
->linktype
= DLT_ATM_RFC1483
;
1122 case TYPE_COLOR_MC_HDLC_POS
:
1124 if (p
->dlt_list
!= NULL
) {
1125 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1126 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1127 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1128 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1129 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1130 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1133 p
->linktype
= DLT_CHDLC
;
1139 p
->linktype
= DLT_RAW
;
1144 case TYPE_MC_RAW_CHANNEL
:
1145 case TYPE_IP_COUNTER
:
1146 case TYPE_TCP_FLOW_COUNTER
:
1147 case TYPE_INFINIBAND
:
1149 case TYPE_INFINIBAND_LINK
:
1151 /* Libpcap cannot deal with these types yet */
1152 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1159 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1161 p
->dlt_count
= dlt_index
;
1164 p
->linktype
= DLT_ERF
;