]>
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.18 2004-03-23 19:18:04 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 dag_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
383 strlcpy(p
->errbuf
, "Sending packets isn't supported on DAG cards",
389 * Get a handle for a live capture from the given DAG device. Passing a NULL
390 * device will result in a failure. The promisc flag is ignored because DAG
391 * cards are always promiscuous. The to_ms parameter is also ignored as it is
392 * not supported in hardware.
397 dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
399 char conf
[30]; /* dag configure string */
404 if (device
== NULL
) {
405 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
408 /* Allocate a handle for this session. */
410 handle
= malloc(sizeof(*handle
));
411 if (handle
== NULL
) {
412 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
416 /* Initialize some components of the pcap structure. */
418 memset(handle
, 0, sizeof(*handle
));
420 if (strstr(device
, "/dev") == NULL
) {
421 char * newDev
= (char *)malloc(strlen(device
) + 6);
423 strcat(newDev
, "/dev/");
424 strcat(newDev
,device
);
427 device
= strdup(device
);
430 if (device
== NULL
) {
431 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "str_dup: %s\n", pcap_strerror(errno
));
435 /* setup device parameters */
436 if((handle
->fd
= dag_open((char *)device
)) < 0) {
437 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
441 /* set the card snap length to the specified snaplen parameter */
442 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
443 snaplen
= MAX_DAG_SNAPLEN
;
444 } else if (snaplen
< MIN_DAG_SNAPLEN
) {
445 snaplen
= MIN_DAG_SNAPLEN
;
447 /* snap len has to be a multiple of 4 */
448 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
450 if(dag_configure(handle
->fd
, conf
) < 0) {
451 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
455 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
456 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
460 if(dag_start(handle
->fd
) < 0) {
461 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
466 * Important! You have to ensure bottom is properly
467 * initialized to zero on startup, it won't give you
468 * a compiler warning if you make this mistake!
470 handle
->md
.dag_mem_bottom
= 0;
471 handle
->md
.dag_mem_top
= 0;
473 /* TODO: query the card */
474 handle
->md
.dag_fcs_bits
= 32;
475 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
476 if ((n
= atoi(s
)) == 0 || n
== 16|| n
== 32) {
477 handle
->md
.dag_fcs_bits
= n
;
479 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
480 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
485 handle
->snapshot
= snaplen
;
486 /*handle->md.timeout = to_ms; */
488 handle
->linktype
= -1;
489 if (dag_get_datalink(handle
) < 0) {
490 strcpy(ebuf
, handle
->errbuf
);
496 if (new_pcap_dag(handle
) < 0) {
497 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
502 * "select()" and "poll()" don't (yet) work on DAG device descriptors.
504 handle
->selectable_fd
= -1;
507 handle
->md
.device
= (char *)device
;
509 free((char *)device
);
513 handle
->read_op
= dag_read
;
514 handle
->inject_op
= dag_inject
;
515 handle
->setfilter_op
= dag_setfilter
;
516 handle
->set_datalink_op
= dag_set_datalink
;
517 handle
->getnonblock_op
= pcap_getnonblock_fd
;
518 handle
->setnonblock_op
= dag_setnonblock
;
519 handle
->stats_op
= dag_stats
;
520 handle
->close_op
= dag_platform_close
;
525 if (device
!= NULL
) {
526 free((char *)device
);
528 if (handle
!= NULL
) {
530 * Get rid of any link-layer type list we allocated.
532 if (handle
->dlt_list
!= NULL
) {
533 free(handle
->dlt_list
);
542 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
543 /* This needs to be filled out correctly. Hopefully a dagapi call will
544 provide all necessary information.
546 /*p->md.stat.ps_recv = 0;*/
547 /*p->md.stat.ps_drop = 0;*/
555 * Get from "/proc/dag" all interfaces listed there; if they're
556 * already in the list of interfaces we have, that won't add another
557 * instance, but if they're not, that'll add them.
559 * We don't bother getting any addresses for them.
561 * We also don't fail if we couldn't open "/proc/dag"; we just leave
562 * the list of interfaces as is.
565 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
571 char name
[512]; /* XXX - pick a size */
575 /* Quick exit if /proc/dag not readable */
576 proc_dag_f
= fopen("/proc/dag", "r");
577 if (proc_dag_f
== NULL
)
580 char dev
[16] = "dagx";
582 for (i
= '0'; ret
== 0 && i
<= '9'; i
++) {
584 if (pcap_add_if(devlistp
, dev
, 0, NULL
, errbuf
) == -1) {
595 for (linenum
= 1; fgets(linebuf
, sizeof linebuf
, proc_dag_f
) != NULL
; linenum
++) {
598 * Skip the first two lines - they're headers.
605 if (*p
== '\0' || *p
== '\n' || *p
!= 'D')
606 continue; /* not a Dag line */
609 * Get the interface name.
612 while (*p
!= '\0' && *p
!= ':') {
614 *q
++ = tolower(*p
++);
621 * Add an entry for this interface, with no addresses.
623 p
[strlen(p
) - 1] = '\0'; /* get rid of \n */
624 if (pcap_add_if(devlistp
, name
, 0, strdup(p
+ 2), errbuf
) == -1) {
634 * Well, we didn't fail for any other reason; did we
635 * fail due to an error reading the file?
637 if (ferror(proc_dag_f
)) {
638 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
639 "Error reading /proc/dag: %s",
640 pcap_strerror(errno
));
645 (void)fclose(proc_dag_f
);
650 * Installs the given bpf filter program in the given pcap structure. There is
651 * no attempt to store the filter in kernel memory as that is not supported
655 dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
660 strncpy(p
->errbuf
, "setfilter: No filter specified",
665 /* Make our private copy of the filter */
667 if (install_bpf_program(p
, fp
) < 0) {
668 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
669 "malloc: %s", pcap_strerror(errno
));
679 dag_set_datalink(pcap_t
*p
, int dlt
)
687 dag_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
690 * Set non-blocking mode on the FD.
691 * XXX - is that necessary? If not, don't bother calling it,
692 * and have a "dag_getnonblock()" function that looks at
693 * "p->md.dag_offset_flags".
695 if (pcap_setnonblock_fd(p
, nonblock
, errbuf
) < 0)
699 p
->md
.dag_offset_flags
|= DAGF_NONBLOCK
;
701 p
->md
.dag_offset_flags
&= ~DAGF_NONBLOCK
;
707 dag_get_datalink(pcap_t
*p
)
711 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(2*sizeof(*(p
->dlt_list
)))) == NULL
) {
712 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
), "malloc: %s", pcap_strerror(errno
));
716 /* Check the type through a dagapi call. */
717 daglinktype
= dag_linktype(p
->fd
);
719 switch(daglinktype
) {
722 if (p
->dlt_list
!= NULL
) {
724 p
->dlt_list
[0] = DLT_CHDLC
;
725 p
->dlt_list
[1] = DLT_PPP_SERIAL
;
727 p
->linktype
= DLT_CHDLC
;
732 * This is (presumably) a real Ethernet capture; give it a
733 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
734 * that an application can let you choose it, in case you're
735 * capturing DOCSIS traffic that a Cisco Cable Modem
736 * Termination System is putting out onto an Ethernet (it
737 * doesn't put an Ethernet header onto the wire, it puts raw
738 * DOCSIS frames out on the wire inside the low-level
741 if (p
->dlt_list
!= NULL
) {
743 p
->dlt_list
[0] = DLT_EN10MB
;
744 p
->dlt_list
[1] = DLT_DOCSIS
;
746 p
->linktype
= DLT_EN10MB
;
751 if (p
->dlt_list
!= NULL
) {
753 p
->dlt_list
[0] = DLT_ATM_RFC1483
;
754 p
->dlt_list
[1] = DLT_SUNATM
;
756 p
->linktype
= DLT_ATM_RFC1483
;
761 p
->linktype
= DLT_NULL
;
765 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "unknown DAG linktype %d\n", daglinktype
);