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 */
80 * Private data for capturing on DAG devices.
83 struct pcap_stat stat
;
84 #ifdef HAVE_DAG_STREAMS_API
85 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
86 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
87 #else /* HAVE_DAG_STREAMS_API */
88 void *dag_mem_base
; /* DAG card memory base address */
89 u_int dag_mem_bottom
; /* DAG card current memory bottom offset */
90 u_int dag_mem_top
; /* DAG card current memory top offset */
91 #endif /* HAVE_DAG_STREAMS_API */
92 int dag_fcs_bits
; /* Number of checksum bits from link layer */
93 int dag_offset_flags
; /* Flags to pass to dag_offset(). */
94 int dag_stream
; /* DAG stream number */
95 int dag_timeout
; /* timeout specified to pcap_open_live.
96 * Same as in linux above, introduce
100 typedef struct pcap_dag_node
{
101 struct pcap_dag_node
*next
;
106 static pcap_dag_node_t
*pcap_dags
= NULL
;
107 static int atexit_handler_installed
= 0;
108 static const unsigned short endian_test_word
= 0x0100;
110 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
112 #define MAX_DAG_PACKET 65536
114 static unsigned char TempPkt
[MAX_DAG_PACKET
];
116 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
117 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
118 static int dag_set_datalink(pcap_t
*p
, int dlt
);
119 static int dag_get_datalink(pcap_t
*p
);
120 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
123 delete_pcap_dag(pcap_t
*p
)
125 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
127 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
131 if (curr
!= NULL
&& curr
->p
== p
) {
133 prev
->next
= curr
->next
;
135 pcap_dags
= curr
->next
;
141 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
142 * in the pcap_t structure, and closes the file descriptor for the DAG card.
146 dag_platform_cleanup(pcap_t
*p
)
152 #ifdef HAVE_DAG_STREAMS_API
153 if(dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0)
154 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
156 if(dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
157 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
159 if(dag_stop(p
->fd
) < 0)
160 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
161 #endif /* HAVE_DAG_STREAMS_API */
163 if(dag_close(p
->fd
) < 0)
164 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
168 pcap_cleanup_live_common(p
);
170 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
176 while (pcap_dags
!= NULL
) {
177 if (pcap_dags
->pid
== getpid()) {
178 dag_platform_cleanup(pcap_dags
->p
);
180 delete_pcap_dag(pcap_dags
->p
);
186 new_pcap_dag(pcap_t
*p
)
188 pcap_dag_node_t
*node
= NULL
;
190 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
194 if (!atexit_handler_installed
) {
195 atexit(atexit_handler
);
196 atexit_handler_installed
= 1;
199 node
->next
= pcap_dags
;
201 node
->pid
= getpid();
209 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
211 uint32_t hdr_num
= 0;
214 /* basic sanity checks */
220 /* check if we have any extension headers */
221 if ( (erf
[8] & 0x80) == 0x00 )
224 /* loop over the extension headers */
227 /* sanity check we have enough bytes */
228 if ( len
< (24 + (hdr_num
* 8)) )
231 /* get the header type */
232 hdr_type
= erf
[(16 + (hdr_num
* 8))];
235 } while ( hdr_type
& 0x80 );
241 * Read at most max_packets from the capture stream and call the callback
242 * for each of them. Returns the number of packets handled, -1 if an
243 * error occured, or -2 if we were told to break out of the loop.
246 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
248 struct pcap_dag
*pd
= p
->private;
249 unsigned int processed
= 0;
250 int flags
= pd
->dag_offset_flags
;
251 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
252 unsigned int num_ext_hdr
= 0;
254 /* Get the next bufferful of packets (if necessary). */
255 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
) {
258 * Has "pcap_breakloop()" been called?
262 * Yes - clear the flag that indicates that
263 * it has, and return -2 to indicate that
264 * we were told to break out of the loop.
270 #ifdef HAVE_DAG_STREAMS_API
271 /* dag_advance_stream() will block (unless nonblock is called)
272 * until 64kB of data has accumulated.
273 * If to_ms is set, it will timeout before 64kB has accumulated.
274 * We wait for 64kB because processing a few packets at a time
275 * can cause problems at high packet rates (>200kpps) due
277 * This does mean if to_ms is not specified the capture may 'hang'
278 * for long periods if the data rate is extremely slow (<64kB/sec)
279 * If non-block is specified it will return immediately. The user
280 * is then responsible for efficiency.
282 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
286 /* dag_offset does not support timeouts */
287 pd
->dag_mem_top
= dag_offset(p
->fd
, &(pd
->dag_mem_bottom
), flags
);
288 #endif /* HAVE_DAG_STREAMS_API */
290 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
292 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
298 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
300 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
306 /* Process the packets. */
307 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
309 unsigned short packet_len
= 0;
311 struct pcap_pkthdr pcap_header
;
313 #ifdef HAVE_DAG_STREAMS_API
314 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
316 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_base
+ pd
->dag_mem_bottom
);
317 #endif /* HAVE_DAG_STREAMS_API */
319 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
323 * Has "pcap_breakloop()" been called?
327 * Yes - clear the flag that indicates that
328 * it has, and return -2 to indicate that
329 * we were told to break out of the loop.
335 rlen
= ntohs(header
->rlen
);
336 if (rlen
< dag_record_size
)
338 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
341 pd
->dag_mem_bottom
+= rlen
;
343 /* Count lost packets. */
344 switch((header
->type
& 0x7f)) {
345 /* in these types the color value overwrites the lctr */
346 case TYPE_COLOR_HDLC_POS
:
348 case TYPE_DSM_COLOR_HDLC_POS
:
349 case TYPE_DSM_COLOR_ETH
:
350 case TYPE_COLOR_MC_HDLC_POS
:
351 case TYPE_COLOR_HASH_ETH
:
352 case TYPE_COLOR_HASH_POS
:
357 if (pd
->stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
358 pd
->stat
.ps_drop
= UINT_MAX
;
360 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
365 if ((header
->type
& 0x7f) == TYPE_PAD
) {
369 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
371 /* ERF encapsulation */
372 /* The Extensible Record Format is not dropped for this kind of encapsulation,
373 * and will be handled as a pseudo header by the decoding application.
374 * The information carried in the ERF header and in the optional subheader (if present)
375 * could be merged with the libpcap information, to offer a better decoding.
376 * The packet length is
377 * o the length of the packet on the link (header->wlen),
378 * o plus the length of the ERF header (dag_record_size), as the length of the
379 * pseudo header will be adjusted during the decoding,
380 * o plus the length of the optional subheader (if present).
382 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
383 * if the capture length is greater than the packet length.
385 if (p
->linktype
== DLT_ERF
) {
386 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
388 switch ((header
->type
& 0x7f)) {
392 case TYPE_MC_RAW_CHANNEL
:
395 case TYPE_COLOR_MC_HDLC_POS
:
396 packet_len
+= 4; /* MC header */
399 case TYPE_COLOR_HASH_ETH
:
400 case TYPE_DSM_COLOR_ETH
:
403 packet_len
+= 2; /* ETH header */
407 /* Include ERF extension headers */
408 packet_len
+= (8 * num_ext_hdr
);
410 if (caplen
> packet_len
) {
414 /* Other kind of encapsulation according to the header Type */
416 /* Skip over generic ERF header */
417 dp
+= dag_record_size
;
418 /* Skip over extension headers */
419 dp
+= 8 * num_ext_hdr
;
421 switch((header
->type
& 0x7f)) {
424 if (header
->type
== TYPE_AAL5
) {
425 packet_len
= ntohs(header
->wlen
);
426 caplen
= rlen
- dag_record_size
;
429 if (header
->type
== TYPE_MC_ATM
) {
430 caplen
= packet_len
= ATM_CELL_SIZE
;
434 if (header
->type
== TYPE_MC_AAL5
) {
435 packet_len
= ntohs(header
->wlen
);
436 caplen
= rlen
- dag_record_size
- 4;
439 if (header
->type
== TYPE_ATM
) {
440 caplen
= packet_len
= ATM_CELL_SIZE
;
442 if (p
->linktype
== DLT_SUNATM
) {
443 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
444 unsigned long rawatm
;
446 rawatm
= ntohl(*((unsigned long *)dp
));
447 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
448 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
449 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
450 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
451 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
452 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
453 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
454 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
457 packet_len
-= ATM_HDR_SIZE
;
458 caplen
-= ATM_HDR_SIZE
;
463 case TYPE_COLOR_HASH_ETH
:
464 case TYPE_DSM_COLOR_ETH
:
467 packet_len
= ntohs(header
->wlen
);
468 packet_len
-= (pd
->dag_fcs_bits
>> 3);
469 caplen
= rlen
- dag_record_size
- 2;
470 if (caplen
> packet_len
) {
476 case TYPE_COLOR_HASH_POS
:
477 case TYPE_DSM_COLOR_HDLC_POS
:
478 case TYPE_COLOR_HDLC_POS
:
480 packet_len
= ntohs(header
->wlen
);
481 packet_len
-= (pd
->dag_fcs_bits
>> 3);
482 caplen
= rlen
- dag_record_size
;
483 if (caplen
> packet_len
) {
488 case TYPE_COLOR_MC_HDLC_POS
:
490 packet_len
= ntohs(header
->wlen
);
491 packet_len
-= (pd
->dag_fcs_bits
>> 3);
492 caplen
= rlen
- dag_record_size
- 4;
493 if (caplen
> packet_len
) {
496 /* jump the MC_HDLC_HEADER */
498 #ifdef DLT_MTP2_WITH_PHDR
499 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
500 /* Add the MTP2 Pseudo Header */
501 caplen
+= MTP2_HDR_LEN
;
502 packet_len
+= MTP2_HDR_LEN
;
504 TempPkt
[MTP2_SENT_OFFSET
] = 0;
505 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
506 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
507 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
508 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
516 packet_len
= ntohs(header
->wlen
);
517 caplen
= rlen
- dag_record_size
;
518 if (caplen
> packet_len
) {
523 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
525 case TYPE_MC_RAW_CHANNEL
:
526 case TYPE_IP_COUNTER
:
527 case TYPE_TCP_FLOW_COUNTER
:
528 case TYPE_INFINIBAND
:
530 case TYPE_INFINIBAND_LINK
:
532 /* Unhandled ERF type.
533 * Ignore rather than generating error
538 /* Skip over extension headers */
539 caplen
-= (8 * num_ext_hdr
);
541 } /* ERF encapsulation */
543 if (caplen
> p
->snapshot
)
544 caplen
= p
->snapshot
;
546 /* Run the packet filter if there is one. */
547 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
549 /* convert between timestamp formats */
550 register unsigned long long ts
;
552 if (IS_BIGENDIAN()) {
553 ts
= SWAPLL(header
->ts
);
558 pcap_header
.ts
.tv_sec
= ts
>> 32;
559 ts
= (ts
& 0xffffffffULL
) * 1000000;
560 ts
+= 0x80000000; /* rounding */
561 pcap_header
.ts
.tv_usec
= ts
>> 32;
562 if (pcap_header
.ts
.tv_usec
>= 1000000) {
563 pcap_header
.ts
.tv_usec
-= 1000000;
564 pcap_header
.ts
.tv_sec
++;
567 /* Fill in our own header data */
568 pcap_header
.caplen
= caplen
;
569 pcap_header
.len
= packet_len
;
571 /* Count the packet. */
574 /* Call the user supplied callback function */
575 callback(user
, &pcap_header
, dp
);
577 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
579 if (processed
== cnt
&& cnt
> 0)
581 /* Reached the user-specified limit. */
591 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
593 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
599 * Get a handle for a live capture from the given DAG device. Passing a NULL
600 * device will result in a failure. The promisc flag is ignored because DAG
601 * cards are always promiscuous. The to_ms parameter is used in setting the
602 * API polling parameters.
604 * snaplen is now also ignored, until we get per-stream slen support. Set
605 * slen with approprite DAG tool BEFORE pcap_activate().
609 static int dag_activate(pcap_t
* handle
)
611 struct pcap_dag
*handlep
= handle
->private;
613 char conf
[30]; /* dag configure string */
618 char * newDev
= NULL
;
619 char * device
= handle
->opt
.source
;
620 #ifdef HAVE_DAG_STREAMS_API
622 struct timeval maxwait
;
626 if (device
== NULL
) {
627 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
631 /* Initialize some components of the pcap structure. */
633 #ifdef HAVE_DAG_STREAMS_API
634 newDev
= (char *)malloc(strlen(device
) + 16);
635 if (newDev
== NULL
) {
636 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
640 /* Parse input name to get dag device and stream number if provided */
641 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handlep
->dag_stream
) < 0) {
642 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
647 if (handlep
->dag_stream
%2) {
648 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
652 if (strncmp(device
, "/dev/", 5) != 0) {
653 newDev
= (char *)malloc(strlen(device
) + 5);
654 if (newDev
== NULL
) {
655 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
658 strcpy(newDev
, "/dev/");
659 strcat(newDev
, device
);
662 #endif /* HAVE_DAG_STREAMS_API */
664 /* setup device parameters */
665 if((handle
->fd
= dag_open((char *)device
)) < 0) {
666 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
670 #ifdef HAVE_DAG_STREAMS_API
671 /* Open requested stream. Can fail if already locked or on error */
672 if (dag_attach_stream(handle
->fd
, handlep
->dag_stream
, 0, 0) < 0) {
673 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
677 /* Set up default poll parameters for stream
678 * Can be overridden by pcap_set_nonblock()
680 if (dag_get_stream_poll(handle
->fd
, handlep
->dag_stream
,
681 &mindata
, &maxwait
, &poll
) < 0) {
682 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
686 if (handle
->opt
.immediate
) {
687 /* Call callback immediately.
688 * XXX - is this the right way to handle this?
692 /* Amount of data to collect in Bytes before calling callbacks.
693 * Important for efficiency, but can introduce latency
694 * at low packet rates if to_ms not set!
699 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
700 * Recommend 10-100ms. Calls will time out even if no data arrived.
702 maxwait
.tv_sec
= handle
->opt
.timeout
/1000;
703 maxwait
.tv_usec
= (handle
->opt
.timeout
%1000) * 1000;
705 if (dag_set_stream_poll(handle
->fd
, handlep
->dag_stream
,
706 mindata
, &maxwait
, &poll
) < 0) {
707 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
712 if((handlep
->dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
713 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
717 #endif /* HAVE_DAG_STREAMS_API */
719 /* XXX Not calling dag_configure() to set slen; this is unsafe in
720 * multi-stream environments as the gpp config is global.
721 * Once the firmware provides 'per-stream slen' this can be supported
722 * again via the Config API without side-effects */
724 /* set the card snap length to the specified snaplen parameter */
725 /* This is a really bad idea, as different cards have different
726 * valid slen ranges. Should fix in Config API. */
727 if (handle
->snapshot
== 0 || handle
->snapshot
> MAX_DAG_SNAPLEN
) {
728 handle
->snapshot
= MAX_DAG_SNAPLEN
;
729 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
730 handle
->snapshot
= MIN_DAG_SNAPLEN
;
732 /* snap len has to be a multiple of 4 */
733 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
735 if(dag_configure(handle
->fd
, conf
) < 0) {
736 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
741 #ifdef HAVE_DAG_STREAMS_API
742 if(dag_start_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
743 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
747 if(dag_start(handle
->fd
) < 0) {
748 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
751 #endif /* HAVE_DAG_STREAMS_API */
754 * Important! You have to ensure bottom is properly
755 * initialized to zero on startup, it won't give you
756 * a compiler warning if you make this mistake!
758 handlep
->dag_mem_bottom
= 0;
759 handlep
->dag_mem_top
= 0;
762 * Find out how many FCS bits we should strip.
763 * First, query the card to see if it strips the FCS.
765 daginf
= dag_info(handle
->fd
);
766 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
767 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
768 handlep
->dag_fcs_bits
= 0;
770 /* Note that no FCS will be supplied. */
771 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
774 * Start out assuming it's 32 bits.
776 handlep
->dag_fcs_bits
= 32;
778 /* Allow an environment variable to override. */
779 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
780 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
781 handlep
->dag_fcs_bits
= n
;
783 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
784 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
790 * Did the user request that they not be stripped?
792 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
793 /* Yes. Note the number of bytes that will be
795 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handlep
->dag_fcs_bits
/16);
797 /* And don't strip them. */
798 handlep
->dag_fcs_bits
= 0;
802 handlep
->dag_timeout
= handle
->opt
.timeout
;
804 handle
->linktype
= -1;
805 if (dag_get_datalink(handle
) < 0)
810 if (new_pcap_dag(handle
) < 0) {
811 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
816 * "select()" and "poll()" don't work on DAG device descriptors.
818 handle
->selectable_fd
= -1;
820 if (newDev
!= NULL
) {
821 free((char *)newDev
);
824 handle
->read_op
= dag_read
;
825 handle
->inject_op
= dag_inject
;
826 handle
->setfilter_op
= dag_setfilter
;
827 handle
->setdirection_op
= NULL
; /* Not implemented.*/
828 handle
->set_datalink_op
= dag_set_datalink
;
829 handle
->getnonblock_op
= pcap_getnonblock_fd
;
830 handle
->setnonblock_op
= dag_setnonblock
;
831 handle
->stats_op
= dag_stats
;
832 handle
->cleanup_op
= dag_platform_cleanup
;
833 handlep
->stat
.ps_drop
= 0;
834 handlep
->stat
.ps_recv
= 0;
835 handlep
->stat
.ps_ifdrop
= 0;
838 #ifdef HAVE_DAG_STREAMS_API
840 if (dag_stop_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
841 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
845 if (dag_detach_stream(handle
->fd
, handlep
->dag_stream
) < 0)
846 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
849 if (dag_stop(handle
->fd
) < 0)
850 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
851 #endif /* HAVE_DAG_STREAMS_API */
854 if (dag_close(handle
->fd
) < 0)
855 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
856 delete_pcap_dag(handle
);
859 pcap_cleanup_live_common(handle
);
860 if (newDev
!= NULL
) {
861 free((char *)newDev
);
867 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
874 /* Does this look like a DAG device? */
875 cp
= strrchr(device
, '/');
878 /* Does it begin with "dag"? */
879 if (strncmp(cp
, "dag", 3) != 0) {
880 /* Nope, doesn't begin with "dag" */
884 /* Yes - is "dag" followed by a number from 0 to MAXDAG? */
886 devnum
= strtol(cp
, &cpend
, 10);
887 if (cpend
== cp
|| *cpend
!= '\0') {
888 /* Not followed by a number. */
892 if (devnum
< 0 || devnum
> MAXDAG
) {
893 /* Followed by a non-valid number. */
898 /* OK, it's probably ours. */
901 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dag
));
905 p
->activate_op
= dag_activate
;
910 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
911 struct pcap_dag
*pd
= p
->private;
913 /* This needs to be filled out correctly. Hopefully a dagapi call will
914 provide all necessary information.
916 /*pd->stat.ps_recv = 0;*/
917 /*pd->stat.ps_drop = 0;*/
925 * Previously we just generated a list of all possible names and let
926 * pcap_add_if() attempt to open each one, but with streams this adds up
927 * to 81 possibilities which is inefficient.
929 * Since we know more about the devices we can prune the tree here.
930 * pcap_add_if() will still retest each device but the total number of
931 * open attempts will still be much less than the naive approach.
934 dag_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
936 char name
[12]; /* XXX - pick a size */
939 char dagname
[DAGNAME_BUFSIZE
];
943 /* Try all the DAGs 0-MAXDAG */
944 for (c
= 0; c
<= MAXDAG
; c
++) {
945 snprintf(name
, 12, "dag%d", c
);
946 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
950 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
951 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
957 #ifdef HAVE_DAG_STREAMS_API
959 int stream
, rxstreams
;
960 rxstreams
= dag_rx_get_stream_count(dagfd
);
961 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
962 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
963 dag_detach_stream(dagfd
, stream
);
965 snprintf(name
, 10, "dag%d:%d", c
, stream
);
966 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
980 #endif /* HAVE_DAG_STREAMS_API */
989 * Installs the given bpf filter program in the given pcap structure. There is
990 * no attempt to store the filter in kernel memory as that is not supported
994 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
999 strncpy(p
->errbuf
, "setfilter: No filter specified",
1004 /* Make our private copy of the filter */
1006 if (install_bpf_program(p
, fp
) < 0)
1013 dag_set_datalink(pcap_t
*p
, int dlt
)
1021 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
1023 struct pcap_dag
*pd
= p
->private;
1026 * Set non-blocking mode on the FD.
1027 * XXX - is that necessary? If not, don't bother calling it,
1028 * and have a "dag_getnonblock()" function that looks at
1029 * "pd->dag_offset_flags".
1031 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
1033 #ifdef HAVE_DAG_STREAMS_API
1036 struct timeval maxwait
;
1037 struct timeval poll
;
1039 if (dag_get_stream_poll(p
->fd
, pd
->dag_stream
,
1040 &mindata
, &maxwait
, &poll
) < 0) {
1041 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
1045 /* Amount of data to collect in Bytes before calling callbacks.
1046 * Important for efficiency, but can introduce latency
1047 * at low packet rates if to_ms not set!
1054 if (dag_set_stream_poll(p
->fd
, pd
->dag_stream
,
1055 mindata
, &maxwait
, &poll
) < 0) {
1056 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
1060 #endif /* HAVE_DAG_STREAMS_API */
1062 pd
->dag_offset_flags
|= DAGF_NONBLOCK
;
1064 pd
->dag_offset_flags
&= ~DAGF_NONBLOCK
;
1070 dag_get_datalink(pcap_t
*p
)
1072 struct pcap_dag
*pd
= p
->private;
1073 int index
=0, dlt_index
=0;
1076 memset(types
, 0, 255);
1078 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1079 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1085 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1086 /* Get list of possible ERF types for this card */
1087 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1088 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1092 while (types
[index
]) {
1094 #elif defined HAVE_DAG_GET_ERF_TYPES
1095 /* Get list of possible ERF types for this card */
1096 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1097 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1101 while (types
[index
]) {
1103 /* Check the type through a dagapi call. */
1104 types
[index
] = dag_linktype(p
->fd
);
1108 switch((types
[index
] & 0x7f)) {
1111 case TYPE_COLOR_HDLC_POS
:
1112 case TYPE_DSM_COLOR_HDLC_POS
:
1113 case TYPE_COLOR_HASH_POS
:
1115 if (p
->dlt_list
!= NULL
) {
1116 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1117 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1118 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1121 p
->linktype
= DLT_CHDLC
;
1125 case TYPE_COLOR_ETH
:
1126 case TYPE_DSM_COLOR_ETH
:
1127 case TYPE_COLOR_HASH_ETH
:
1129 * This is (presumably) a real Ethernet capture; give it a
1130 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1131 * that an application can let you choose it, in case you're
1132 * capturing DOCSIS traffic that a Cisco Cable Modem
1133 * Termination System is putting out onto an Ethernet (it
1134 * doesn't put an Ethernet header onto the wire, it puts raw
1135 * DOCSIS frames out on the wire inside the low-level
1136 * Ethernet framing).
1138 if (p
->dlt_list
!= NULL
) {
1139 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1140 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1143 p
->linktype
= DLT_EN10MB
;
1150 if (p
->dlt_list
!= NULL
) {
1151 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1152 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1155 p
->linktype
= DLT_ATM_RFC1483
;
1158 case TYPE_COLOR_MC_HDLC_POS
:
1160 if (p
->dlt_list
!= NULL
) {
1161 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1162 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1163 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1164 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1165 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1166 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1169 p
->linktype
= DLT_CHDLC
;
1175 p
->linktype
= DLT_RAW
;
1180 case TYPE_MC_RAW_CHANNEL
:
1181 case TYPE_IP_COUNTER
:
1182 case TYPE_TCP_FLOW_COUNTER
:
1183 case TYPE_INFINIBAND
:
1185 case TYPE_INFINIBAND_LINK
:
1187 /* Libpcap cannot deal with these types yet */
1188 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1195 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1197 p
->dlt_count
= dlt_index
;
1200 p
->linktype
= DLT_ERF
;