]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
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, Koryn Grant <support@endace.com>
17 static const char rcsid
[] _U_
=
18 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.17 2004-01-30 02:23:53 guy Exp $ (LBL)";
25 #include <sys/param.h> /* optionally get BSD define */
34 #include <netinet/in.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
40 struct mbuf
; /* Squelch compiler warnings on some platforms for */
41 struct rtentry
; /* declarations in <net/if.h> */
47 #define MIN_DAG_SNAPLEN 12
48 #define MAX_DAG_SNAPLEN 2040
49 #define ATM_CELL_SIZE 52
50 #define ATM_HDR_SIZE 4
52 /* SunATM pseudo header */
54 unsigned char flags
; /* destination and traffic type */
55 unsigned char vpi
; /* VPI */
56 unsigned short vci
; /* VCI */
59 typedef struct pcap_dag_node
{
60 struct pcap_dag_node
*next
;
65 static pcap_dag_node_t
*pcap_dags
= NULL
;
66 static int atexit_handler_installed
= 0;
67 static const unsigned short endian_test_word
= 0x0100;
69 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
72 * Swap byte ordering of unsigned long long timestamp on a big endian
75 #define SWAP_TS(ull) ((ull & 0xff00000000000000LL) >> 56) | \
76 ((ull & 0x00ff000000000000LL) >> 40) | \
77 ((ull & 0x0000ff0000000000LL) >> 24) | \
78 ((ull & 0x000000ff00000000LL) >> 8) | \
79 ((ull & 0x00000000ff000000LL) << 8) | \
80 ((ull & 0x0000000000ff0000LL) << 24) | \
81 ((ull & 0x000000000000ff00LL) << 40) | \
82 ((ull & 0x00000000000000ffLL) << 56)
86 /* This code is required when compiling for a DAG device only. */
89 /* Replace dag function names with pcap equivalent. */
90 #define dag_open_live pcap_open_live
91 #define dag_platform_finddevs pcap_platform_finddevs
94 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
95 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
96 static int dag_set_datalink(pcap_t
*p
, int dlt
);
97 static int dag_get_datalink(pcap_t
*p
);
98 static int dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
);
101 delete_pcap_dag(pcap_t
*p
)
103 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
105 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
109 if (curr
!= NULL
&& curr
->p
== p
) {
111 prev
->next
= curr
->next
;
113 pcap_dags
= curr
->next
;
119 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
120 * in the pcap_t structure, and closes the file descriptor for the DAG card.
124 dag_platform_close(pcap_t
*p
)
128 if (p
!= NULL
&& p
->md
.device
!= NULL
) {
129 if(dag_stop(p
->fd
) < 0)
130 fprintf(stderr
,"dag_stop %s: %s\n", p
->md
.device
, strerror(errno
));
131 if(dag_close(p
->fd
) < 0)
132 fprintf(stderr
,"dag_close %s: %s\n", p
->md
.device
, strerror(errno
));
138 if(dag_stop(p
->fd
) < 0)
139 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
140 if(dag_close(p
->fd
) < 0)
141 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
145 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
151 while (pcap_dags
!= NULL
) {
152 if (pcap_dags
->pid
== getpid()) {
153 dag_platform_close(pcap_dags
->p
);
155 delete_pcap_dag(pcap_dags
->p
);
161 new_pcap_dag(pcap_t
*p
)
163 pcap_dag_node_t
*node
= NULL
;
165 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
169 if (!atexit_handler_installed
) {
170 atexit(atexit_handler
);
171 atexit_handler_installed
= 1;
174 node
->next
= pcap_dags
;
176 node
->pid
= getpid();
184 * Read at most max_packets from the capture stream and call the callback
185 * for each of them. Returns the number of packets handled, -1 if an
186 * error occured, or -2 if we were told to break out of the loop.
189 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
191 unsigned int processed
= 0;
192 int flags
= p
->md
.dag_offset_flags
;
193 unsigned int nonblocking
= flags
& DAGF_NONBLOCK
;
197 /* Get the next bufferful of packets (if necessary). */
198 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
) {
201 * Has "pcap_breakloop()" been called?
205 * Yes - clear the flag that indicates that
206 * it has, and return -2 to indicate that
207 * we were told to break out of the loop.
213 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), flags
);
214 if (nonblocking
&& (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
< dag_record_size
))
216 /* Pcap is configured to process only available packets, and there aren't any. */
221 /* Process the packets. */
222 while (p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
>= dag_record_size
) {
224 unsigned short packet_len
= 0;
226 struct pcap_pkthdr pcap_header
;
228 dag_record_t
*header
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
229 u_char
*dp
= ((u_char
*)header
) + dag_record_size
;
233 * Has "pcap_breakloop()" been called?
237 * Yes - clear the flag that indicates that
238 * it has, and return -2 to indicate that
239 * we were told to break out of the loop.
245 if (IS_BIGENDIAN()) {
248 rlen
= ntohs(header
->rlen
);
250 p
->md
.dag_mem_bottom
+= rlen
;
252 switch(header
->type
) {
255 if (header
->type
== TYPE_AAL5
) {
256 if (IS_BIGENDIAN()) {
257 packet_len
= header
->wlen
;
259 packet_len
= ntohs(header
->wlen
);
261 caplen
= rlen
- dag_record_size
;
263 caplen
= packet_len
= ATM_CELL_SIZE
;
265 if (p
->linktype
== DLT_SUNATM
) {
266 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
267 unsigned long rawatm
;
268 if (IS_BIGENDIAN()) {
269 rawatm
= *((unsigned long *)dp
);
270 sunatm
->vci
= (rawatm
>> 4) & 0xffff;
272 rawatm
= ntohl(*((unsigned long *)dp
));
273 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
275 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
276 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
277 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
278 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
279 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
280 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
281 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
284 packet_len
-= ATM_HDR_SIZE
;
285 caplen
-= ATM_HDR_SIZE
;
291 if (IS_BIGENDIAN()) {
292 packet_len
= header
->wlen
;
294 packet_len
= ntohs(header
->wlen
);
296 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
297 caplen
= rlen
- dag_record_size
- 2;
298 if (caplen
> packet_len
) {
305 if (IS_BIGENDIAN()) {
306 packet_len
= header
->wlen
;
308 packet_len
= ntohs(header
->wlen
);
310 packet_len
-= (p
->md
.dag_fcs_bits
>> 3);
311 caplen
= rlen
- dag_record_size
;
312 if (caplen
> packet_len
) {
318 if (caplen
> p
->snapshot
)
319 caplen
= p
->snapshot
;
321 /* Count lost packets. */
323 if (p
->md
.stat
.ps_drop
> (UINT_MAX
- ntohs(header
->lctr
))) {
324 p
->md
.stat
.ps_drop
= UINT_MAX
;
326 p
->md
.stat
.ps_drop
+= ntohs(header
->lctr
);
330 /* Run the packet filter if there is one. */
331 if ((p
->fcode
.bf_insns
== NULL
) || bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
333 /* convert between timestamp formats */
334 register unsigned long long ts
;
336 if (IS_BIGENDIAN()) {
337 ts
= SWAP_TS(header
->ts
);
342 pcap_header
.ts
.tv_sec
= ts
>> 32;
343 ts
= (ts
& 0xffffffffULL
) * 1000000;
344 ts
+= 0x80000000; /* rounding */
345 pcap_header
.ts
.tv_usec
= ts
>> 32;
346 if (pcap_header
.ts
.tv_usec
>= 1000000) {
347 pcap_header
.ts
.tv_usec
-= 1000000;
348 pcap_header
.ts
.tv_sec
++;
351 /* Fill in our own header data */
352 pcap_header
.caplen
= caplen
;
353 pcap_header
.len
= packet_len
;
355 /* Count the packet. */
356 p
->md
.stat
.ps_recv
++;
358 /* Call the user supplied callback function */
359 callback(user
, &pcap_header
, dp
);
361 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
363 if (processed
== cnt
)
365 /* Reached the user-specified limit. */
371 if (nonblocking
|| processed
)
381 * Get a handle for a live capture from the given DAG device. Passing a NULL
382 * device will result in a failure. The promisc flag is ignored because DAG
383 * cards are always promiscuous. The to_ms parameter is also ignored as it is
384 * not supported in hardware.
389 dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
391 char conf
[30]; /* dag configure string */
396 if (device
== NULL
) {
397 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
400 /* Allocate a handle for this session. */
402 handle
= malloc(sizeof(*handle
));
403 if (handle
== NULL
) {
404 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
408 /* Initialize some components of the pcap structure. */
410 memset(handle
, 0, sizeof(*handle
));
412 if (strstr(device
, "/dev") == NULL
) {
413 char * newDev
= (char *)malloc(strlen(device
) + 6);
415 strcat(newDev
, "/dev/");
416 strcat(newDev
,device
);
419 device
= strdup(device
);
422 if (device
== NULL
) {
423 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "str_dup: %s\n", pcap_strerror(errno
));
427 /* setup device parameters */
428 if((handle
->fd
= dag_open((char *)device
)) < 0) {
429 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
433 /* set the card snap length to the specified snaplen parameter */
434 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
435 snaplen
= MAX_DAG_SNAPLEN
;
436 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
437 snaplen
= MIN_DAG_SNAPLEN
;
439 /* snap len has to be a multiple of 4 */
440 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
442 if(dag_configure(handle
->fd
, conf
) < 0) {
443 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
447 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
448 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
452 if(dag_start(handle
->fd
) < 0) {
453 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
458 * Important! You have to ensure bottom is properly
459 * initialized to zero on startup, it won't give you
460 * a compiler warning if you make this mistake!
462 handle
->md
.dag_mem_bottom
= 0;
463 handle
->md
.dag_mem_top
= 0;
465 /* TODO: query the card */
466 handle
->md
.dag_fcs_bits
= 32;
467 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
468 if ((n
= atoi(s
)) == 0 || n
== 16|| n
== 32) {
469 handle
->md
.dag_fcs_bits
= n
;
471 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
472 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
477 handle
->snapshot
= snaplen
;
478 /*handle->md.timeout = to_ms; */
480 handle
->linktype
= -1;
481 if (dag_get_datalink(handle
) < 0) {
482 strcpy(ebuf
, handle
->errbuf
);
488 if (new_pcap_dag(handle
) < 0) {
489 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
494 * "select()" and "poll()" don't (yet) work on DAG device descriptors.
496 handle
->selectable_fd
= -1;
499 handle
->md
.device
= (char *)device
;
501 free((char *)device
);
505 handle
->read_op
= dag_read
;
506 handle
->setfilter_op
= dag_setfilter
;
507 handle
->set_datalink_op
= dag_set_datalink
;
508 handle
->getnonblock_op
= pcap_getnonblock_fd
;
509 handle
->setnonblock_op
= dag_setnonblock
;
510 handle
->stats_op
= dag_stats
;
511 handle
->close_op
= dag_platform_close
;
516 if (device
!= NULL
) {
517 free((char *)device
);
519 if (handle
!= NULL
) {
521 * Get rid of any link-layer type list we allocated.
523 if (handle
->dlt_list
!= NULL
) {
524 free(handle
->dlt_list
);
533 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
534 /* This needs to be filled out correctly. Hopefully a dagapi call will
535 provide all necessary information.
537 /*p->md.stat.ps_recv = 0;*/
538 /*p->md.stat.ps_drop = 0;*/
546 * Get from "/proc/dag" all interfaces listed there; if they're
547 * already in the list of interfaces we have, that won't add another
548 * instance, but if they're not, that'll add them.
550 * We don't bother getting any addresses for them.
552 * We also don't fail if we couldn't open "/proc/dag"; we just leave
553 * the list of interfaces as is.
556 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
562 char name
[512]; /* XXX - pick a size */
566 /* Quick exit if /proc/dag not readable */
567 proc_dag_f
= fopen("/proc/dag", "r");
568 if (proc_dag_f
== NULL
)
571 char dev
[16] = "dagx";
573 for (i
= '0'; ret
== 0 && i
<= '9'; i
++) {
575 if (pcap_add_if(devlistp
, dev
, 0, NULL
, errbuf
) == -1) {
586 for (linenum
= 1; fgets(linebuf
, sizeof linebuf
, proc_dag_f
) != NULL
; linenum
++) {
589 * Skip the first two lines - they're headers.
596 if (*p
== '\0' || *p
== '\n' || *p
!= 'D')
597 continue; /* not a Dag line */
600 * Get the interface name.
603 while (*p
!= '\0' && *p
!= ':') {
605 *q
++ = tolower(*p
++);
612 * Add an entry for this interface, with no addresses.
614 p
[strlen(p
) - 1] = '\0'; /* get rid of \n */
615 if (pcap_add_if(devlistp
, name
, 0, strdup(p
+ 2), errbuf
) == -1) {
625 * Well, we didn't fail for any other reason; did we
626 * fail due to an error reading the file?
628 if (ferror(proc_dag_f
)) {
629 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
630 "Error reading /proc/dag: %s",
631 pcap_strerror(errno
));
636 (void)fclose(proc_dag_f
);
641 * Installs the given bpf filter program in the given pcap structure. There is
642 * no attempt to store the filter in kernel memory as that is not supported
646 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
651 strncpy(p
->errbuf
, "setfilter: No filter specified",
656 /* Make our private copy of the filter */
658 if (install_bpf_program(p
, fp
) < 0) {
659 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
660 "malloc: %s", pcap_strerror(errno
));
670 dag_set_datalink(pcap_t
*p
, int dlt
)
678 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
681 * Set non-blocking mode on the FD.
682 * XXX - is that necessary? If not, don't bother calling it,
683 * and have a "dag_getnonblock()" function that looks at
684 * "p->md.dag_offset_flags".
686 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
690 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
692 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
698 dag_get_datalink(pcap_t
*p
)
702 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(2*sizeof(*(p
->dlt_list
)))) == NULL
) {
703 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
707 /* Check the type through a dagapi call. */
708 daglinktype
= dag_linktype(p
->fd
);
710 switch(daglinktype
) {
713 if (p
->dlt_list
!= NULL
) {
715 p
->dlt_list
[0] = DLT_CHDLC
;
716 p
->dlt_list
[1] = DLT_PPP_SERIAL
;
718 p
->linktype
= DLT_CHDLC
;
723 * This is (presumably) a real Ethernet capture; give it a
724 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
725 * that an application can let you choose it, in case you're
726 * capturing DOCSIS traffic that a Cisco Cable Modem
727 * Termination System is putting out onto an Ethernet (it
728 * doesn't put an Ethernet header onto the wire, it puts raw
729 * DOCSIS frames out on the wire inside the low-level
732 if (p
->dlt_list
!= NULL
) {
734 p
->dlt_list
[0] = DLT_EN10MB
;
735 p
->dlt_list
[1] = DLT_DOCSIS
;
737 p
->linktype
= DLT_EN10MB
;
742 if (p
->dlt_list
!= NULL
) {
744 p
->dlt_list
[0] = DLT_ATM_RFC1483
;
745 p
->dlt_list
[1] = DLT_SUNATM
;
747 p
->linktype
= DLT_ATM_RFC1483
;
752 p
->linktype
= DLT_NULL
;
756 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "unknown DAG linktype %d\n", daglinktype
);