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 /* Amount of data to collect in Bytes before calling callbacks.
687 * Important for efficiency, but can introduce latency
688 * at low packet rates if to_ms not set!
692 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
693 * Recommend 10-100ms. Calls will time out even if no data arrived.
695 maxwait
.tv_sec
= handle
->opt
.timeout
/1000;
696 maxwait
.tv_usec
= (handle
->opt
.timeout
%1000) * 1000;
698 if (dag_set_stream_poll(handle
->fd
, handlep
->dag_stream
,
699 mindata
, &maxwait
, &poll
) < 0) {
700 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
705 if((handlep
->dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
706 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
710 #endif /* HAVE_DAG_STREAMS_API */
712 /* XXX Not calling dag_configure() to set slen; this is unsafe in
713 * multi-stream environments as the gpp config is global.
714 * Once the firmware provides 'per-stream slen' this can be supported
715 * again via the Config API without side-effects */
717 /* set the card snap length to the specified snaplen parameter */
718 /* This is a really bad idea, as different cards have different
719 * valid slen ranges. Should fix in Config API. */
720 if (handle
->snapshot
== 0 || handle
->snapshot
> MAX_DAG_SNAPLEN
) {
721 handle
->snapshot
= MAX_DAG_SNAPLEN
;
722 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
723 handle
->snapshot
= MIN_DAG_SNAPLEN
;
725 /* snap len has to be a multiple of 4 */
726 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
728 if(dag_configure(handle
->fd
, conf
) < 0) {
729 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
734 #ifdef HAVE_DAG_STREAMS_API
735 if(dag_start_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
736 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
740 if(dag_start(handle
->fd
) < 0) {
741 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
744 #endif /* HAVE_DAG_STREAMS_API */
747 * Important! You have to ensure bottom is properly
748 * initialized to zero on startup, it won't give you
749 * a compiler warning if you make this mistake!
751 handlep
->dag_mem_bottom
= 0;
752 handlep
->dag_mem_top
= 0;
755 * Find out how many FCS bits we should strip.
756 * First, query the card to see if it strips the FCS.
758 daginf
= dag_info(handle
->fd
);
759 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
760 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
761 handlep
->dag_fcs_bits
= 0;
763 /* Note that no FCS will be supplied. */
764 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
767 * Start out assuming it's 32 bits.
769 handlep
->dag_fcs_bits
= 32;
771 /* Allow an environment variable to override. */
772 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
773 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
774 handlep
->dag_fcs_bits
= n
;
776 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
777 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
783 * Did the user request that they not be stripped?
785 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
786 /* Yes. Note the number of bytes that will be
788 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handlep
->dag_fcs_bits
/16);
790 /* And don't strip them. */
791 handlep
->dag_fcs_bits
= 0;
795 handlep
->dag_timeout
= handle
->opt
.timeout
;
797 handle
->linktype
= -1;
798 if (dag_get_datalink(handle
) < 0)
803 if (new_pcap_dag(handle
) < 0) {
804 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
809 * "select()" and "poll()" don't work on DAG device descriptors.
811 handle
->selectable_fd
= -1;
813 if (newDev
!= NULL
) {
814 free((char *)newDev
);
817 handle
->read_op
= dag_read
;
818 handle
->inject_op
= dag_inject
;
819 handle
->setfilter_op
= dag_setfilter
;
820 handle
->setdirection_op
= NULL
; /* Not implemented.*/
821 handle
->set_datalink_op
= dag_set_datalink
;
822 handle
->getnonblock_op
= pcap_getnonblock_fd
;
823 handle
->setnonblock_op
= dag_setnonblock
;
824 handle
->stats_op
= dag_stats
;
825 handle
->cleanup_op
= dag_platform_cleanup
;
826 handlep
->stat
.ps_drop
= 0;
827 handlep
->stat
.ps_recv
= 0;
828 handlep
->stat
.ps_ifdrop
= 0;
831 #ifdef HAVE_DAG_STREAMS_API
833 if (dag_stop_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
834 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
838 if (dag_detach_stream(handle
->fd
, handlep
->dag_stream
) < 0)
839 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
842 if (dag_stop(handle
->fd
) < 0)
843 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
844 #endif /* HAVE_DAG_STREAMS_API */
847 if (dag_close(handle
->fd
) < 0)
848 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
849 delete_pcap_dag(handle
);
852 pcap_cleanup_live_common(handle
);
853 if (newDev
!= NULL
) {
854 free((char *)newDev
);
860 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
867 /* Does this look like a DAG device? */
868 cp
= strrchr(device
, '/');
871 /* Does it begin with "dag"? */
872 if (strncmp(cp
, "dag", 3) != 0) {
873 /* Nope, doesn't begin with "dag" */
877 /* Yes - is "dag" followed by a number from 0 to MAXDAG? */
879 devnum
= strtol(cp
, &cpend
, 10);
880 if (cpend
== cp
|| *cpend
!= '\0') {
881 /* Not followed by a number. */
885 if (devnum
< 0 || devnum
> MAXDAG
) {
886 /* Followed by a non-valid number. */
891 /* OK, it's probably ours. */
894 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dag
));
898 p
->activate_op
= dag_activate
;
903 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
904 struct pcap_dag
*pd
= p
->private;
906 /* This needs to be filled out correctly. Hopefully a dagapi call will
907 provide all necessary information.
909 /*pd->stat.ps_recv = 0;*/
910 /*pd->stat.ps_drop = 0;*/
918 * Previously we just generated a list of all possible names and let
919 * pcap_add_if() attempt to open each one, but with streams this adds up
920 * to 81 possibilities which is inefficient.
922 * Since we know more about the devices we can prune the tree here.
923 * pcap_add_if() will still retest each device but the total number of
924 * open attempts will still be much less than the naive approach.
927 dag_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
929 char name
[12]; /* XXX - pick a size */
932 char dagname
[DAGNAME_BUFSIZE
];
936 /* Try all the DAGs 0-MAXDAG */
937 for (c
= 0; c
<= MAXDAG
; c
++) {
938 snprintf(name
, 12, "dag%d", c
);
939 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
943 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
944 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
950 #ifdef HAVE_DAG_STREAMS_API
952 int stream
, rxstreams
;
953 rxstreams
= dag_rx_get_stream_count(dagfd
);
954 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
955 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
956 dag_detach_stream(dagfd
, stream
);
958 snprintf(name
, 10, "dag%d:%d", c
, stream
);
959 if (pcap_add_if(devlistp
, name
, 0, NULL
, errbuf
) == -1) {
973 #endif /* HAVE_DAG_STREAMS_API */
982 * Installs the given bpf filter program in the given pcap structure. There is
983 * no attempt to store the filter in kernel memory as that is not supported
987 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
992 strncpy(p
->errbuf
, "setfilter: No filter specified",
997 /* Make our private copy of the filter */
999 if (install_bpf_program(p
, fp
) < 0)
1006 dag_set_datalink(pcap_t
*p
, int dlt
)
1014 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
1016 struct pcap_dag
*pd
= p
->private;
1019 * Set non-blocking mode on the FD.
1020 * XXX - is that necessary? If not, don't bother calling it,
1021 * and have a "dag_getnonblock()" function that looks at
1022 * "pd->dag_offset_flags".
1024 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
1026 #ifdef HAVE_DAG_STREAMS_API
1029 struct timeval maxwait
;
1030 struct timeval poll
;
1032 if (dag_get_stream_poll(p
->fd
, pd
->dag_stream
,
1033 &mindata
, &maxwait
, &poll
) < 0) {
1034 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
1038 /* Amount of data to collect in Bytes before calling callbacks.
1039 * Important for efficiency, but can introduce latency
1040 * at low packet rates if to_ms not set!
1047 if (dag_set_stream_poll(p
->fd
, pd
->dag_stream
,
1048 mindata
, &maxwait
, &poll
) < 0) {
1049 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
1053 #endif /* HAVE_DAG_STREAMS_API */
1055 pd
->dag_offset_flags
|= DAGF_NONBLOCK
;
1057 pd
->dag_offset_flags
&= ~DAGF_NONBLOCK
;
1063 dag_get_datalink(pcap_t
*p
)
1065 struct pcap_dag
*pd
= p
->private;
1066 int index
=0, dlt_index
=0;
1069 memset(types
, 0, 255);
1071 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1072 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1078 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1079 /* Get list of possible ERF types for this card */
1080 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1081 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1085 while (types
[index
]) {
1087 #elif defined HAVE_DAG_GET_ERF_TYPES
1088 /* Get list of possible ERF types for this card */
1089 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1090 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1094 while (types
[index
]) {
1096 /* Check the type through a dagapi call. */
1097 types
[index
] = dag_linktype(p
->fd
);
1101 switch((types
[index
] & 0x7f)) {
1104 case TYPE_COLOR_HDLC_POS
:
1105 case TYPE_DSM_COLOR_HDLC_POS
:
1106 case TYPE_COLOR_HASH_POS
:
1108 if (p
->dlt_list
!= NULL
) {
1109 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1110 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1111 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1114 p
->linktype
= DLT_CHDLC
;
1118 case TYPE_COLOR_ETH
:
1119 case TYPE_DSM_COLOR_ETH
:
1120 case TYPE_COLOR_HASH_ETH
:
1122 * This is (presumably) a real Ethernet capture; give it a
1123 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1124 * that an application can let you choose it, in case you're
1125 * capturing DOCSIS traffic that a Cisco Cable Modem
1126 * Termination System is putting out onto an Ethernet (it
1127 * doesn't put an Ethernet header onto the wire, it puts raw
1128 * DOCSIS frames out on the wire inside the low-level
1129 * Ethernet framing).
1131 if (p
->dlt_list
!= NULL
) {
1132 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1133 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1136 p
->linktype
= DLT_EN10MB
;
1143 if (p
->dlt_list
!= NULL
) {
1144 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1145 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1148 p
->linktype
= DLT_ATM_RFC1483
;
1151 case TYPE_COLOR_MC_HDLC_POS
:
1153 if (p
->dlt_list
!= NULL
) {
1154 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1155 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1156 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1157 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1158 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1159 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1162 p
->linktype
= DLT_CHDLC
;
1168 p
->linktype
= DLT_RAW
;
1173 case TYPE_MC_RAW_CHANNEL
:
1174 case TYPE_IP_COUNTER
:
1175 case TYPE_TCP_FLOW_COUNTER
:
1176 case TYPE_INFINIBAND
:
1178 case TYPE_INFINIBAND_LINK
:
1180 /* Libpcap cannot deal with these types yet */
1181 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1188 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1190 p
->dlt_count
= dlt_index
;
1193 p
->linktype
= DLT_ERF
;