1 /* $NetBSD: pcap-dag.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
4 * pcap-dag.c: Packet capture interface for Emulex EndaceDAG cards.
6 * The functionality of this code attempts to mimic that of pcap-linux as much
7 * as possible. This code is compiled in several different ways depending on
8 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
9 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
10 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
11 * is not defined then nothing is altered - the dag_ functions will be
12 * called as required from their pcap-linux/bpf equivalents.
14 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
15 * Modifications: Jesper Peterson
17 * Stephen Donnelly <stephen.donnelly@emulex.com>
20 #include <sys/cdefs.h>
21 __RCSID("$NetBSD: pcap-dag.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
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> */
53 * DAG devices have names beginning with "dag", followed by a number
54 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
55 * from 0 to DAG_STREAM_MAX.
57 #ifndef DAG_MAX_BOARDS
58 #define DAG_MAX_BOARDS 32
61 #define ATM_CELL_SIZE 52
62 #define ATM_HDR_SIZE 4
65 * A header containing additional MTP information.
67 #define MTP2_SENT_OFFSET 0 /* 1 byte */
68 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
69 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
70 #define MTP2_HDR_LEN 4 /* length of the header */
72 #define MTP2_ANNEX_A_NOT_USED 0
73 #define MTP2_ANNEX_A_USED 1
74 #define MTP2_ANNEX_A_USED_UNKNOWN 2
76 /* SunATM pseudo header */
78 unsigned char flags
; /* destination and traffic type */
79 unsigned char vpi
; /* VPI */
80 unsigned short vci
; /* VCI */
84 * Private data for capturing on DAG devices.
87 struct pcap_stat stat
;
88 #ifdef HAVE_DAG_STREAMS_API
89 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
90 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
91 #else /* HAVE_DAG_STREAMS_API */
92 void *dag_mem_base
; /* DAG card memory base address */
93 u_int dag_mem_bottom
; /* DAG card current memory bottom offset */
94 u_int dag_mem_top
; /* DAG card current memory top offset */
95 #endif /* HAVE_DAG_STREAMS_API */
96 int dag_fcs_bits
; /* Number of checksum bits from link layer */
97 int dag_offset_flags
; /* Flags to pass to dag_offset(). */
98 int dag_stream
; /* DAG stream number */
99 int dag_timeout
; /* timeout specified to pcap_open_live.
100 * Same as in linux above, introduce
104 typedef struct pcap_dag_node
{
105 struct pcap_dag_node
*next
;
110 static pcap_dag_node_t
*pcap_dags
= NULL
;
111 static int atexit_handler_installed
= 0;
112 static const unsigned short endian_test_word
= 0x0100;
114 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
116 #define MAX_DAG_PACKET 65536
118 static unsigned char TempPkt
[MAX_DAG_PACKET
];
120 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
121 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
122 static int dag_set_datalink(pcap_t
*p
, int dlt
);
123 static int dag_get_datalink(pcap_t
*p
);
124 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
127 delete_pcap_dag(pcap_t
*p
)
129 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
131 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
135 if (curr
!= NULL
&& curr
->p
== p
) {
137 prev
->next
= curr
->next
;
139 pcap_dags
= curr
->next
;
145 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
146 * in the pcap_t structure, and closes the file descriptor for the DAG card.
150 dag_platform_cleanup(pcap_t
*p
)
156 #ifdef HAVE_DAG_STREAMS_API
157 if(dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0)
158 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
160 if(dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
161 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
163 if(dag_stop(p
->fd
) < 0)
164 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
165 #endif /* HAVE_DAG_STREAMS_API */
167 if(dag_close(p
->fd
) < 0)
168 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
172 pcap_cleanup_live_common(p
);
174 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
180 while (pcap_dags
!= NULL
) {
181 if (pcap_dags
->pid
== getpid()) {
182 dag_platform_cleanup(pcap_dags
->p
);
184 delete_pcap_dag(pcap_dags
->p
);
190 new_pcap_dag(pcap_t
*p
)
192 pcap_dag_node_t
*node
= NULL
;
194 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
198 if (!atexit_handler_installed
) {
199 atexit(atexit_handler
);
200 atexit_handler_installed
= 1;
203 node
->next
= pcap_dags
;
205 node
->pid
= getpid();
213 dag_erf_ext_header_count(uint8_t * erf
, size_t len
)
215 uint32_t hdr_num
= 0;
218 /* basic sanity checks */
224 /* check if we have any extension headers */
225 if ( (erf
[8] & 0x80) == 0x00 )
228 /* loop over the extension headers */
231 /* sanity check we have enough bytes */
232 if ( len
< (24 + (hdr_num
* 8)) )
235 /* get the header type */
236 hdr_type
= erf
[(16 + (hdr_num
* 8))];
239 } while ( hdr_type
& 0x80 );
245 * Read at most max_packets from the capture stream and call the callback
246 * for each of them. Returns the number of packets handled, -1 if an
247 * error occured, or -2 if we were told to break out of the loop.
250 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
252 struct pcap_dag
*pd
= p
->priv
;
253 unsigned int processed
= 0;
254 int flags
= pd
->dag_offset_flags
;
255 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
256 unsigned int num_ext_hdr
= 0;
257 unsigned int ticks_per_second
;
259 /* Get the next bufferful of packets (if necessary). */
260 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
) {
263 * Has "pcap_breakloop()" been called?
267 * Yes - clear the flag that indicates that
268 * it has, and return -2 to indicate that
269 * we were told to break out of the loop.
275 #ifdef HAVE_DAG_STREAMS_API
276 /* dag_advance_stream() will block (unless nonblock is called)
277 * until 64kB of data has accumulated.
278 * If to_ms is set, it will timeout before 64kB has accumulated.
279 * We wait for 64kB because processing a few packets at a time
280 * can cause problems at high packet rates (>200kpps) due
282 * This does mean if to_ms is not specified the capture may 'hang'
283 * for long periods if the data rate is extremely slow (<64kB/sec)
284 * If non-block is specified it will return immediately. The user
285 * is then responsible for efficiency.
287 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
291 /* dag_offset does not support timeouts */
292 pd
->dag_mem_top
= dag_offset(p
->fd
, &(pd
->dag_mem_bottom
), flags
);
293 #endif /* HAVE_DAG_STREAMS_API */
295 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
297 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
303 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
305 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
311 /* Process the packets. */
312 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
314 unsigned short packet_len
= 0;
316 struct pcap_pkthdr pcap_header
;
318 #ifdef HAVE_DAG_STREAMS_API
319 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
321 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_base
+ pd
->dag_mem_bottom
);
322 #endif /* HAVE_DAG_STREAMS_API */
324 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
328 * Has "pcap_breakloop()" been called?
332 * Yes - clear the flag that indicates that
333 * it has, and return -2 to indicate that
334 * we were told to break out of the loop.
340 rlen
= ntohs(header
->rlen
);
341 if (rlen
< dag_record_size
)
343 strncpy(p
->errbuf
, "dag_read: record too small", PCAP_ERRBUF_SIZE
);
346 pd
->dag_mem_bottom
+= rlen
;
348 /* Count lost packets. */
349 switch((header
->type
& 0x7f)) {
350 /* in these types the color value overwrites the lctr */
351 case TYPE_COLOR_HDLC_POS
:
353 case TYPE_DSM_COLOR_HDLC_POS
:
354 case TYPE_DSM_COLOR_ETH
:
355 case TYPE_COLOR_MC_HDLC_POS
:
356 case TYPE_COLOR_HASH_ETH
:
357 case TYPE_COLOR_HASH_POS
:
362 if (pd
->stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
363 pd
->stat
.ps_drop
= UINT_MAX
;
365 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
370 if ((header
->type
& 0x7f) == TYPE_PAD
) {
374 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
376 /* ERF encapsulation */
377 /* The Extensible Record Format is not dropped for this kind of encapsulation,
378 * and will be handled as a pseudo header by the decoding application.
379 * The information carried in the ERF header and in the optional subheader (if present)
380 * could be merged with the libpcap information, to offer a better decoding.
381 * The packet length is
382 * o the length of the packet on the link (header->wlen),
383 * o plus the length of the ERF header (dag_record_size), as the length of the
384 * pseudo header will be adjusted during the decoding,
385 * o plus the length of the optional subheader (if present).
387 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
388 * if the capture length is greater than the packet length.
390 if (p
->linktype
== DLT_ERF
) {
391 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
393 switch ((header
->type
& 0x7f)) {
397 case TYPE_MC_RAW_CHANNEL
:
400 case TYPE_COLOR_MC_HDLC_POS
:
401 packet_len
+= 4; /* MC header */
404 case TYPE_COLOR_HASH_ETH
:
405 case TYPE_DSM_COLOR_ETH
:
408 packet_len
+= 2; /* ETH header */
412 /* Include ERF extension headers */
413 packet_len
+= (8 * num_ext_hdr
);
415 if (caplen
> packet_len
) {
419 /* Other kind of encapsulation according to the header Type */
421 /* Skip over generic ERF header */
422 dp
+= dag_record_size
;
423 /* Skip over extension headers */
424 dp
+= 8 * num_ext_hdr
;
426 switch((header
->type
& 0x7f)) {
429 if (header
->type
== TYPE_AAL5
) {
430 packet_len
= ntohs(header
->wlen
);
431 caplen
= rlen
- dag_record_size
;
434 if (header
->type
== TYPE_MC_ATM
) {
435 caplen
= packet_len
= ATM_CELL_SIZE
;
439 if (header
->type
== TYPE_MC_AAL5
) {
440 packet_len
= ntohs(header
->wlen
);
441 caplen
= rlen
- dag_record_size
- 4;
444 /* Skip over extension headers */
445 caplen
-= (8 * num_ext_hdr
);
447 if (header
->type
== TYPE_ATM
) {
448 caplen
= packet_len
= ATM_CELL_SIZE
;
450 if (p
->linktype
== DLT_SUNATM
) {
451 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
452 unsigned long rawatm
;
454 rawatm
= ntohl(*((unsigned long *)dp
));
455 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
456 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
457 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
458 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
459 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
460 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
461 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
462 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
465 packet_len
-= ATM_HDR_SIZE
;
466 caplen
-= ATM_HDR_SIZE
;
471 case TYPE_COLOR_HASH_ETH
:
472 case TYPE_DSM_COLOR_ETH
:
475 packet_len
= ntohs(header
->wlen
);
476 packet_len
-= (pd
->dag_fcs_bits
>> 3);
477 caplen
= rlen
- dag_record_size
- 2;
478 /* Skip over extension headers */
479 caplen
-= (8 * num_ext_hdr
);
480 if (caplen
> packet_len
) {
486 case TYPE_COLOR_HASH_POS
:
487 case TYPE_DSM_COLOR_HDLC_POS
:
488 case TYPE_COLOR_HDLC_POS
:
490 packet_len
= ntohs(header
->wlen
);
491 packet_len
-= (pd
->dag_fcs_bits
>> 3);
492 caplen
= rlen
- dag_record_size
;
493 /* Skip over extension headers */
494 caplen
-= (8 * num_ext_hdr
);
495 if (caplen
> packet_len
) {
500 case TYPE_COLOR_MC_HDLC_POS
:
502 packet_len
= ntohs(header
->wlen
);
503 packet_len
-= (pd
->dag_fcs_bits
>> 3);
504 caplen
= rlen
- dag_record_size
- 4;
505 /* Skip over extension headers */
506 caplen
-= (8 * num_ext_hdr
);
507 if (caplen
> packet_len
) {
510 /* jump the MC_HDLC_HEADER */
512 #ifdef DLT_MTP2_WITH_PHDR
513 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
514 /* Add the MTP2 Pseudo Header */
515 caplen
+= MTP2_HDR_LEN
;
516 packet_len
+= MTP2_HDR_LEN
;
518 TempPkt
[MTP2_SENT_OFFSET
] = 0;
519 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
520 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
521 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
522 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
530 packet_len
= ntohs(header
->wlen
);
531 caplen
= rlen
- dag_record_size
;
532 /* Skip over extension headers */
533 caplen
-= (8 * num_ext_hdr
);
534 if (caplen
> packet_len
) {
539 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
541 case TYPE_MC_RAW_CHANNEL
:
542 case TYPE_IP_COUNTER
:
543 case TYPE_TCP_FLOW_COUNTER
:
544 case TYPE_INFINIBAND
:
546 case TYPE_INFINIBAND_LINK
:
548 /* Unhandled ERF type.
549 * Ignore rather than generating error
554 } /* ERF encapsulation */
556 if (caplen
> p
->snapshot
)
557 caplen
= p
->snapshot
;
559 /* Run the packet filter if there is one. */
560 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
562 /* convert between timestamp formats */
563 register unsigned long long ts
;
565 if (IS_BIGENDIAN()) {
566 ts
= SWAPLL(header
->ts
);
571 switch (p
->opt
.tstamp_precision
) {
572 case PCAP_TSTAMP_PRECISION_NANO
:
573 ticks_per_second
= 1000000000;
575 case PCAP_TSTAMP_PRECISION_MICRO
:
577 ticks_per_second
= 1000000;
581 pcap_header
.ts
.tv_sec
= ts
>> 32;
582 ts
= (ts
& 0xffffffffULL
) * ticks_per_second
;
583 ts
+= 0x80000000; /* rounding */
584 pcap_header
.ts
.tv_usec
= ts
>> 32;
585 if (pcap_header
.ts
.tv_usec
>= ticks_per_second
) {
586 pcap_header
.ts
.tv_usec
-= ticks_per_second
;
587 pcap_header
.ts
.tv_sec
++;
590 /* Fill in our own header data */
591 pcap_header
.caplen
= caplen
;
592 pcap_header
.len
= packet_len
;
594 /* Count the packet. */
597 /* Call the user supplied callback function */
598 callback(user
, &pcap_header
, dp
);
600 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
602 if (processed
== cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
604 /* Reached the user-specified limit. */
614 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
616 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
622 * Get a handle for a live capture from the given DAG device. Passing a NULL
623 * device will result in a failure. The promisc flag is ignored because DAG
624 * cards are always promiscuous. The to_ms parameter is used in setting the
625 * API polling parameters.
627 * snaplen is now also ignored, until we get per-stream slen support. Set
628 * slen with approprite DAG tool BEFORE pcap_activate().
632 static int dag_activate(pcap_t
* handle
)
634 struct pcap_dag
*handlep
= handle
->priv
;
636 char conf
[30]; /* dag configure string */
641 char * newDev
= NULL
;
642 char * device
= handle
->opt
.source
;
643 #ifdef HAVE_DAG_STREAMS_API
645 struct timeval maxwait
;
649 if (device
== NULL
) {
650 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
654 /* Initialize some components of the pcap structure. */
656 #ifdef HAVE_DAG_STREAMS_API
657 newDev
= (char *)malloc(strlen(device
) + 16);
658 if (newDev
== NULL
) {
659 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
663 /* Parse input name to get dag device and stream number if provided */
664 if (dag_parse_name(device
, newDev
, strlen(device
) + 16, &handlep
->dag_stream
) < 0) {
665 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: %s\n", pcap_strerror(errno
));
670 if (handlep
->dag_stream
%2) {
671 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
675 if (strncmp(device
, "/dev/", 5) != 0) {
676 newDev
= (char *)malloc(strlen(device
) + 5);
677 if (newDev
== NULL
) {
678 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate string for device name: %s\n", pcap_strerror(errno
));
681 strcpy(newDev
, "/dev/");
682 strcat(newDev
, device
);
685 #endif /* HAVE_DAG_STREAMS_API */
687 /* setup device parameters */
688 if((handle
->fd
= dag_open((char *)device
)) < 0) {
689 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
693 #ifdef HAVE_DAG_STREAMS_API
694 /* Open requested stream. Can fail if already locked or on error */
695 if (dag_attach_stream(handle
->fd
, handlep
->dag_stream
, 0, 0) < 0) {
696 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_attach_stream: %s\n", pcap_strerror(errno
));
700 /* Set up default poll parameters for stream
701 * Can be overridden by pcap_set_nonblock()
703 if (dag_get_stream_poll(handle
->fd
, handlep
->dag_stream
,
704 &mindata
, &maxwait
, &poll
) < 0) {
705 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
709 if (handle
->opt
.immediate
) {
710 /* Call callback immediately.
711 * XXX - is this the right way to handle this?
715 /* Amount of data to collect in Bytes before calling callbacks.
716 * Important for efficiency, but can introduce latency
717 * at low packet rates if to_ms not set!
722 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
723 * Recommend 10-100ms. Calls will time out even if no data arrived.
725 maxwait
.tv_sec
= handle
->opt
.timeout
/1000;
726 maxwait
.tv_usec
= (handle
->opt
.timeout
%1000) * 1000;
728 if (dag_set_stream_poll(handle
->fd
, handlep
->dag_stream
,
729 mindata
, &maxwait
, &poll
) < 0) {
730 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
735 if((handlep
->dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
736 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
740 #endif /* HAVE_DAG_STREAMS_API */
742 /* XXX Not calling dag_configure() to set slen; this is unsafe in
743 * multi-stream environments as the gpp config is global.
744 * Once the firmware provides 'per-stream slen' this can be supported
745 * again via the Config API without side-effects */
747 /* set the card snap length to the specified snaplen parameter */
748 /* This is a really bad idea, as different cards have different
749 * valid slen ranges. Should fix in Config API. */
750 if (handle
->snapshot
== 0 || handle
->snapshot
> MAX_DAG_SNAPLEN
) {
751 handle
->snapshot
= MAX_DAG_SNAPLEN
;
752 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
753 handle
->snapshot
= MIN_DAG_SNAPLEN
;
755 /* snap len has to be a multiple of 4 */
756 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
758 if(dag_configure(handle
->fd
, conf
) < 0) {
759 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
764 #ifdef HAVE_DAG_STREAMS_API
765 if(dag_start_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
766 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start_stream %s: %s\n", device
, pcap_strerror(errno
));
770 if(dag_start(handle
->fd
) < 0) {
771 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
774 #endif /* HAVE_DAG_STREAMS_API */
777 * Important! You have to ensure bottom is properly
778 * initialized to zero on startup, it won't give you
779 * a compiler warning if you make this mistake!
781 handlep
->dag_mem_bottom
= 0;
782 handlep
->dag_mem_top
= 0;
785 * Find out how many FCS bits we should strip.
786 * First, query the card to see if it strips the FCS.
788 daginf
= dag_info(handle
->fd
);
789 if ((0x4200 == daginf
->device_code
) || (0x4230 == daginf
->device_code
)) {
790 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
791 handlep
->dag_fcs_bits
= 0;
793 /* Note that no FCS will be supplied. */
794 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(0);
797 * Start out assuming it's 32 bits.
799 handlep
->dag_fcs_bits
= 32;
801 /* Allow an environment variable to override. */
802 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
803 if ((n
= atoi(s
)) == 0 || n
== 16 || n
== 32) {
804 handlep
->dag_fcs_bits
= n
;
806 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
807 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
813 * Did the user request that they not be stripped?
815 if ((s
= getenv("ERF_DONT_STRIP_FCS")) != NULL
) {
816 /* Yes. Note the number of bytes that will be
818 handle
->linktype_ext
= LT_FCS_DATALINK_EXT(handlep
->dag_fcs_bits
/16);
820 /* And don't strip them. */
821 handlep
->dag_fcs_bits
= 0;
825 handlep
->dag_timeout
= handle
->opt
.timeout
;
827 handle
->linktype
= -1;
828 if (dag_get_datalink(handle
) < 0)
833 if (new_pcap_dag(handle
) < 0) {
834 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
839 * "select()" and "poll()" don't work on DAG device descriptors.
841 handle
->selectable_fd
= -1;
843 if (newDev
!= NULL
) {
844 free((char *)newDev
);
847 handle
->read_op
= dag_read
;
848 handle
->inject_op
= dag_inject
;
849 handle
->setfilter_op
= dag_setfilter
;
850 handle
->setdirection_op
= NULL
; /* Not implemented.*/
851 handle
->set_datalink_op
= dag_set_datalink
;
852 handle
->getnonblock_op
= pcap_getnonblock_fd
;
853 handle
->setnonblock_op
= dag_setnonblock
;
854 handle
->stats_op
= dag_stats
;
855 handle
->cleanup_op
= dag_platform_cleanup
;
856 handlep
->stat
.ps_drop
= 0;
857 handlep
->stat
.ps_recv
= 0;
858 handlep
->stat
.ps_ifdrop
= 0;
861 #ifdef HAVE_DAG_STREAMS_API
863 if (dag_stop_stream(handle
->fd
, handlep
->dag_stream
) < 0) {
864 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
868 if (dag_detach_stream(handle
->fd
, handlep
->dag_stream
) < 0)
869 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
872 if (dag_stop(handle
->fd
) < 0)
873 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
874 #endif /* HAVE_DAG_STREAMS_API */
877 if (dag_close(handle
->fd
) < 0)
878 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
879 delete_pcap_dag(handle
);
882 pcap_cleanup_live_common(handle
);
883 if (newDev
!= NULL
) {
884 free((char *)newDev
);
890 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
896 #ifdef HAVE_DAG_STREAMS_API
900 /* Does this look like a DAG device? */
901 cp
= strrchr(device
, '/');
904 /* Does it begin with "dag"? */
905 if (strncmp(cp
, "dag", 3) != 0) {
906 /* Nope, doesn't begin with "dag" */
910 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
912 devnum
= strtol(cp
, &cpend
, 10);
913 #ifdef HAVE_DAG_STREAMS_API
915 /* Followed by a stream number. */
916 stream
= strtol(++cpend
, &cpend
, 10);
919 if (cpend
== cp
|| *cpend
!= '\0') {
920 /* Not followed by a number. */
924 if (devnum
< 0 || devnum
>= DAG_MAX_BOARDS
) {
925 /* Followed by a non-valid number. */
929 #ifdef HAVE_DAG_STREAMS_API
930 if (stream
<0 || stream
>= DAG_STREAM_MAX
) {
931 /* Followed by a non-valid stream number. */
937 /* OK, it's probably ours. */
940 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dag
));
944 p
->activate_op
= dag_activate
;
947 * We claim that we support microsecond and nanosecond time
950 * XXX Our native precision is 2^-32s, but libpcap doesn't support
951 * power of two precisions yet. We can convert to either MICRO or NANO.
953 p
->tstamp_precision_count
= 2;
954 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
955 if (p
->tstamp_precision_list
== NULL
) {
956 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
957 pcap_strerror(errno
));
958 if (p
->tstamp_type_list
!= NULL
)
959 free(p
->tstamp_type_list
);
963 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
964 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
969 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
970 struct pcap_dag
*pd
= p
->priv
;
972 /* This needs to be filled out correctly. Hopefully a dagapi call will
973 provide all necessary information.
975 /*pd->stat.ps_recv = 0;*/
976 /*pd->stat.ps_drop = 0;*/
984 * Previously we just generated a list of all possible names and let
985 * pcap_add_if() attempt to open each one, but with streams this adds up
986 * to 81 possibilities which is inefficient.
988 * Since we know more about the devices we can prune the tree here.
989 * pcap_add_if() will still retest each device but the total number of
990 * open attempts will still be much less than the naive approach.
993 dag_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
995 char name
[12]; /* XXX - pick a size */
998 char dagname
[DAGNAME_BUFSIZE
];
1001 dag_card_inf_t
*inf
;
1004 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1005 for (c
= 0; c
< DAG_MAX_BOARDS
; c
++) {
1006 snprintf(name
, 12, "dag%d", c
);
1007 if (-1 == dag_parse_name(name
, dagname
, DAGNAME_BUFSIZE
, &dagstream
))
1012 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
1013 if ((inf
= dag_pciinfo(dagfd
)))
1014 description
= dag_device_name(inf
->device_code
, 1);
1015 if (pcap_add_if(devlistp
, name
, 0, description
, errbuf
) == -1) {
1021 #ifdef HAVE_DAG_STREAMS_API
1023 int stream
, rxstreams
;
1024 rxstreams
= dag_rx_get_stream_count(dagfd
);
1025 for(stream
=0;stream
<DAG_STREAM_MAX
;stream
+=2) {
1026 if (0 == dag_attach_stream(dagfd
, stream
, 0, 0)) {
1027 dag_detach_stream(dagfd
, stream
);
1029 snprintf(name
, 10, "dag%d:%d", c
, stream
);
1030 if (pcap_add_if(devlistp
, name
, 0, description
, errbuf
) == -1) {
1038 if(rxstreams
<= 0) {
1044 #endif /* HAVE_DAG_STREAMS_API */
1053 * Installs the given bpf filter program in the given pcap structure. There is
1054 * no attempt to store the filter in kernel memory as that is not supported
1058 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
1063 strncpy(p
->errbuf
, "setfilter: No filter specified",
1068 /* Make our private copy of the filter */
1070 if (install_bpf_program(p
, fp
) < 0)
1077 dag_set_datalink(pcap_t
*p
, int dlt
)
1085 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
1087 struct pcap_dag
*pd
= p
->priv
;
1090 * Set non-blocking mode on the FD.
1091 * XXX - is that necessary? If not, don't bother calling it,
1092 * and have a "dag_getnonblock()" function that looks at
1093 * "pd->dag_offset_flags".
1095 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
1097 #ifdef HAVE_DAG_STREAMS_API
1100 struct timeval maxwait
;
1101 struct timeval poll
;
1103 if (dag_get_stream_poll(p
->fd
, pd
->dag_stream
,
1104 &mindata
, &maxwait
, &poll
) < 0) {
1105 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_get_stream_poll: %s\n", pcap_strerror(errno
));
1109 /* Amount of data to collect in Bytes before calling callbacks.
1110 * Important for efficiency, but can introduce latency
1111 * at low packet rates if to_ms not set!
1118 if (dag_set_stream_poll(p
->fd
, pd
->dag_stream
,
1119 mindata
, &maxwait
, &poll
) < 0) {
1120 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "dag_set_stream_poll: %s\n", pcap_strerror(errno
));
1124 #endif /* HAVE_DAG_STREAMS_API */
1126 pd
->dag_offset_flags
|= DAGF_NONBLOCK
;
1128 pd
->dag_offset_flags
&= ~DAGF_NONBLOCK
;
1134 dag_get_datalink(pcap_t
*p
)
1136 struct pcap_dag
*pd
= p
->priv
;
1137 int index
=0, dlt_index
=0;
1140 memset(types
, 0, 255);
1142 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1143 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
1149 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1150 /* Get list of possible ERF types for this card */
1151 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1152 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_stream_erf_types: %s", pcap_strerror(errno
));
1156 while (types
[index
]) {
1158 #elif defined HAVE_DAG_GET_ERF_TYPES
1159 /* Get list of possible ERF types for this card */
1160 if (dag_get_erf_types(p
->fd
, types
, 255) < 0) {
1161 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "dag_get_erf_types: %s", pcap_strerror(errno
));
1165 while (types
[index
]) {
1167 /* Check the type through a dagapi call. */
1168 types
[index
] = dag_linktype(p
->fd
);
1172 switch((types
[index
] & 0x7f)) {
1175 case TYPE_COLOR_HDLC_POS
:
1176 case TYPE_DSM_COLOR_HDLC_POS
:
1177 case TYPE_COLOR_HASH_POS
:
1179 if (p
->dlt_list
!= NULL
) {
1180 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1181 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1182 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1185 p
->linktype
= DLT_CHDLC
;
1189 case TYPE_COLOR_ETH
:
1190 case TYPE_DSM_COLOR_ETH
:
1191 case TYPE_COLOR_HASH_ETH
:
1193 * This is (presumably) a real Ethernet capture; give it a
1194 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1195 * that an application can let you choose it, in case you're
1196 * capturing DOCSIS traffic that a Cisco Cable Modem
1197 * Termination System is putting out onto an Ethernet (it
1198 * doesn't put an Ethernet header onto the wire, it puts raw
1199 * DOCSIS frames out on the wire inside the low-level
1200 * Ethernet framing).
1202 if (p
->dlt_list
!= NULL
) {
1203 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1204 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1207 p
->linktype
= DLT_EN10MB
;
1214 if (p
->dlt_list
!= NULL
) {
1215 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1216 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1219 p
->linktype
= DLT_ATM_RFC1483
;
1222 case TYPE_COLOR_MC_HDLC_POS
:
1224 if (p
->dlt_list
!= NULL
) {
1225 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1226 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1227 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1228 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1229 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1230 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1233 p
->linktype
= DLT_CHDLC
;
1239 p
->linktype
= DLT_RAW
;
1244 case TYPE_MC_RAW_CHANNEL
:
1245 case TYPE_IP_COUNTER
:
1246 case TYPE_TCP_FLOW_COUNTER
:
1247 case TYPE_INFINIBAND
:
1249 case TYPE_INFINIBAND_LINK
:
1251 /* Libpcap cannot deal with these types yet */
1252 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1259 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1261 p
->dlt_count
= dlt_index
;
1264 p
->linktype
= DLT_ERF
;