]>
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 * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
15 * 2003 May - Jesper Peterson <support@endace.com>
16 * Code shuffled around to suit fad-xxx.c structure
17 * Added atexit() handler to stop DAG if application is too lazy
21 static const char rcsid
[] =
22 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.10 2003-11-04 07:05:33 guy Exp $ (LBL)";
29 #include <sys/param.h> /* optionally get BSD define */
38 #include <sys/socket.h>
40 struct mbuf
; /* Squelch compiler warnings on some platforms for */
41 struct rtentry
; /* declarations in <net/if.h> */
48 #define min(a, b) ((a) > (b) ? (b) : (a))
51 #define MIN_DAG_SNAPLEN 12
52 #define MAX_DAG_SNAPLEN 2040
53 #define ATM_SNAPLEN 48
55 /* Size of ATM payload */
56 #define ATM_WLEN(h) ATM_SNAPLEN
57 #define ATM_SLEN(h) ATM_SNAPLEN
59 /* Size Ethernet payload */
60 #define ETHERNET_WLEN(h, b) (ntohs((h)->wlen) - ((b) >> 3))
61 #define ETHERNET_SLEN(h, b) min(ETHERNET_WLEN(h, b), \
62 ntohs((h)->rlen) - dag_record_size - 2)
64 /* Size of HDLC payload */
65 #define HDLC_WLEN(h, b) (ntohs((h)->wlen) - ((b) >> 3))
66 #define HDLC_SLEN(h, b) min(HDLC_WLEN(h, b), \
67 ntohs((h)->rlen) - dag_record_size)
69 typedef struct pcap_dag_node
{
70 struct pcap_dag_node
*next
;
75 static pcap_dag_node_t
*pcap_dags
= NULL
;
76 static int atexit_handler_installed
= 0;
77 static unsigned short endian_test_word
= 0x0100;
79 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
82 * Swap byte ordering of unsigned long long timestamp on a big endian
85 #define SWAP_TS(ull) \
86 (IS_BIGENDIAN() ? ((ull & 0xff00000000000000LL) >> 56) | \
87 ((ull & 0x00ff000000000000LL) >> 40) | \
88 ((ull & 0x0000ff0000000000LL) >> 24) | \
89 ((ull & 0x000000ff00000000LL) >> 8) | \
90 ((ull & 0x00000000ff000000LL) << 8) | \
91 ((ull & 0x0000000000ff0000LL) << 24) | \
92 ((ull & 0x000000000000ff00LL) << 40) | \
93 ((ull & 0x00000000000000ffLL) << 56) \
97 /* This code is reguired when compiling for a DAG device only. */
100 /* Replace dag function names with pcap equivalent. */
101 #define dag_open_live pcap_open_live
102 #define dag_platform_finddevs pcap_platform_finddevs
103 #endif /* DAG_ONLY */
105 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
);
106 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
107 static int dag_set_datalink(pcap_t
*p
, int dlt
);
108 static int dag_get_datalink(pcap_t
*p
);
110 static void delete_pcap_dag(pcap_t
*p
) {
111 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
113 for (prev
= NULL
, curr
= pcap_dags
;
114 curr
!= NULL
&& curr
->p
!= p
;
115 prev
= curr
, curr
= curr
->next
) {
119 if (curr
!= NULL
&& curr
->p
== p
) {
121 prev
->next
= curr
->next
;
123 pcap_dags
= curr
->next
;
129 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
130 * in the pcap_t structure, and closes the file descriptor for the DAG card.
133 static void dag_platform_close(pcap_t
*p
) {
136 if (p
!= NULL
&& p
->md
.device
!= NULL
) {
137 if(dag_stop(p
->fd
) < 0)
138 fprintf(stderr
,"dag_stop %s: %s\n", p
->md
.device
, strerror(errno
));
139 if(dag_close(p
->fd
) < 0)
140 fprintf(stderr
,"dag_close %s: %s\n", p
->md
.device
, strerror(errno
));
146 if(dag_stop(p
->fd
) < 0)
147 fprintf(stderr
,"dag_stop: %s\n", strerror(errno
));
148 if(dag_close(p
->fd
) < 0)
149 fprintf(stderr
,"dag_close: %s\n", strerror(errno
));
155 static void atexit_handler(void) {
156 while (pcap_dags
!= NULL
) {
157 if (pcap_dags
->pid
== getpid()) {
158 dag_platform_close(pcap_dags
->p
);
160 delete_pcap_dag(pcap_dags
->p
);
165 static int new_pcap_dag(pcap_t
*p
) {
166 pcap_dag_node_t
*node
= NULL
;
168 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
172 if (!atexit_handler_installed
) {
173 atexit(atexit_handler
);
174 atexit_handler_installed
= 1;
177 node
->next
= pcap_dags
;
179 node
->pid
= getpid();
187 * Get pointer to the ERF header for the next packet in the input
188 * stream. This function blocks until a packet becomes available.
190 static dag_record_t
*get_next_dag_header(pcap_t
*p
) {
191 register dag_record_t
*record
;
195 * The buffer is guaranteed to only contain complete records so any
196 * time top and bottom differ there will be at least one record available.
197 * Here we test the difference is at least the size of a record header
198 * using the poorly named constant 'dag_record_size'.
200 while ((p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
) < dag_record_size
) {
201 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), 0);
204 record
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
206 p
->md
.dag_mem_bottom
+= ntohs(record
->rlen
);
212 * Read at most max_packets from the capture stream and call the callback
213 * for each of them. Returns the number of packets handled, -1 if an
214 * error occured, or -2 if we were told to break out of the loop.
217 static int dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
) {
219 int packet_len
= 0, caplen
= 0;
220 struct pcap_pkthdr pcap_header
;
222 dag_record_t
*header
;
223 register unsigned long long ts
;
226 * Has "pcap_breakloop()" been called?
230 * Yes - clear the flag that indicates that it has, and return -2
231 * to indicate that we were told to break out of the loop.
237 /* Receive a single packet from the kernel */
238 header
= get_next_dag_header(p
);
239 dp
= ((u_char
*)header
) + dag_record_size
;
241 switch(header
->type
) {
243 packet_len
= ATM_WLEN(header
);
244 caplen
= ATM_SLEN(header
);
248 packet_len
= ETHERNET_WLEN(header
, p
->md
.dag_fcs_bits
);
249 caplen
= ETHERNET_SLEN(header
, p
->md
.dag_fcs_bits
);
253 packet_len
= HDLC_WLEN(header
, p
->md
.dag_fcs_bits
);
254 caplen
= HDLC_SLEN(header
, p
->md
.dag_fcs_bits
);
258 if (caplen
> p
->snapshot
)
259 caplen
= p
->snapshot
;
261 /* Count lost packets */
262 if (header
->lctr
> 0 && (p
->md
.stat
.ps_drop
+1) != 0) {
263 if (header
->lctr
== 0xffff ||
264 (p
->md
.stat
.ps_drop
+ header
->lctr
) < p
->md
.stat
.ps_drop
) {
265 p
->md
.stat
.ps_drop
== ~0;
267 p
->md
.stat
.ps_drop
+= header
->lctr
;
271 /* Run the packet filter if not using kernel filter */
272 if (p
->fcode
.bf_insns
) {
273 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0) {
274 /* rejected by filter */
279 /* convert between timestamp formats */
280 ts
= SWAP_TS(header
->ts
);
281 pcap_header
.ts
.tv_sec
= ts
>> 32;
282 ts
= ((ts
& 0xffffffffULL
) * 1000 * 1000);
283 ts
+= (ts
& 0x80000000ULL
) << 1; /* rounding */
284 pcap_header
.ts
.tv_usec
= ts
>> 32;
285 if (pcap_header
.ts
.tv_usec
>= 1000000) {
286 pcap_header
.ts
.tv_usec
-= 1000000;
287 pcap_header
.ts
.tv_sec
+= 1;
290 /* Fill in our own header data */
291 pcap_header
.caplen
= caplen
;
292 pcap_header
.len
= packet_len
;
297 p
->md
.stat
.ps_recv
++;
299 /* Call the user supplied callback function */
300 callback(user
, &pcap_header
, dp
);
306 * Get a handle for a live capture from the given DAG device. Passing a NULL
307 * device will result in a failure. The promisc flag is ignored because DAG
308 * cards are always promiscuous. The to_ms parameter is also ignored as it is
309 * not supported in hardware.
313 pcap_t
*dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
) {
314 char conf
[30]; /* dag configure string */
319 if (device
== NULL
) {
320 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
323 /* Allocate a handle for this session. */
325 handle
= malloc(sizeof(*handle
));
326 if (handle
== NULL
) {
327 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
331 /* Initialize some components of the pcap structure. */
333 memset(handle
, 0, sizeof(*handle
));
335 if (strstr(device
, "/dev") == NULL
) {
336 char * newDev
= (char *)malloc(strlen(device
) + 6);
338 strcat(newDev
, "/dev/");
339 strcat(newDev
,device
);
343 /* setup device parameters */
344 if((handle
->fd
= dag_open((char *)device
)) < 0) {
345 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
349 /* set the card snap length as specified by the specified snaplen parameter */
350 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
351 snaplen
= MAX_DAG_SNAPLEN
;
353 if (snaplen
< MIN_DAG_SNAPLEN
) {
354 snaplen
= MIN_DAG_SNAPLEN
;
356 /* snap len has to be a multiple of 4 */
357 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
359 fprintf(stderr
, "Configuring DAG with '%s'.\n", conf
);
360 if(dag_configure(handle
->fd
, conf
) < 0) {
361 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
365 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
366 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
370 if(dag_start(handle
->fd
) < 0) {
371 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
376 * Important! You have to ensure bottom is properly
377 * initialized to zero on startup, it won't give you
378 * a compiler warning if you make this mistake!
380 handle
->md
.dag_mem_bottom
= 0;
381 handle
->md
.dag_mem_top
= 0;
383 /* TODO: query the card */
384 handle
->md
.dag_fcs_bits
= 32;
385 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
386 if ((n
= atoi(s
)) == 0 || n
== 16|| n
== 32) {
387 handle
->md
.dag_fcs_bits
= n
;
389 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
390 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
395 handle
->snapshot
= snaplen
;
396 /*handle->md.timeout = to_ms; */
400 handle
->md
.device
= strdup(device
);
403 if (handle
->md
.device
== NULL
) {
404 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "str_dup %s: %s\n", device
, pcap_strerror(errno
));
410 if ((handle
->linktype
= dag_get_datalink(handle
)) < 0) {
411 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_linktype %s: unknown linktype\n", device
);
417 if (new_pcap_dag(handle
) < 0) {
418 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
422 handle
->read_op
= dag_read
;
423 handle
->setfilter_op
= dag_setfilter
;
424 handle
->set_datalink_op
= dag_set_datalink
;
425 handle
->stats_op
= dag_stats
;
426 handle
->close_op
= dag_platform_close
;
431 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
432 /* This needs to be filled out correctly. Hopefully a dagapi call will
433 provide all necessary information.
435 /*p->md.stat.ps_recv = 0;*/
436 /*p->md.stat.ps_drop = 0;*/
444 * Get from "/proc/dag" all interfaces listed there; if they're
445 * already in the list of interfaces we have, that won't add another
446 * instance, but if they're not, that'll add them.
448 * We don't bother getting any addresses for them.
450 * We also don't fail if we couldn't open "/proc/dag"; we just leave
451 * the list of interfaces as is.
454 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
460 char name
[512]; /* XXX - pick a size */
462 struct ifreq ifrflags
;
465 /* Quick exit if /proc/dag not readable */
466 proc_dag_f
= fopen("/proc/dag", "r");
467 if (proc_dag_f
== NULL
)
470 char dev
[16] = "dagx";
472 for (i
= '0'; ret
== 0 && i
<= '9'; i
++) {
474 if (pcap_add_if(devlistp
, dev
, 0, NULL
, errbuf
) == -1) {
486 fgets(linebuf
, sizeof linebuf
, proc_dag_f
) != NULL
; linenum
++) {
489 * Skip the first two lines - they're headers.
496 if (*p
== '\0' || *p
== '\n' || *p
!= 'D')
497 continue; /* not a Dag line */
500 * Get the interface name.
503 while (*p
!= '\0' && *p
!= ':') {
505 *q
++ = tolower(*p
++);
512 * Add an entry for this interface, with no addresses.
514 p
[strlen(p
) - 1] = '\0'; /* get rid of \n */
515 if (pcap_add_if(devlistp
, name
, 0, strdup(p
+ 2), errbuf
) == -1) {
525 * Well, we didn't fail for any other reason; did we
526 * fail due to an error reading the file?
528 if (ferror(proc_dag_f
)) {
529 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
530 "Error reading /proc/dag: %s",
531 pcap_strerror(errno
));
536 (void)fclose(proc_dag_f
);
541 * Installs the gven bpf filter program in the given pcap structure. There is
542 * no attempt to store the filter in kernel memory as that is not supported
545 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
) {
549 strncpy(p
->errbuf
, "setfilter: No filter specified",
554 /* Make our private copy of the filter */
556 if (install_bpf_program(p
, fp
) < 0) {
557 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
558 "malloc: %s", pcap_strerror(errno
));
568 dag_set_datalink(pcap_t
*p
, int dlt
)
574 dag_get_datalink(pcap_t
*p
)
578 /* Check the type through a dagapi call.
580 switch(dag_linktype(p
->fd
)) {
581 case TYPE_HDLC_POS
: {
582 dag_record_t
*record
;
584 /* peek at the first available record to see if it is PPP */
585 while ((p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
) < (dag_record_size
+ 4)) {
586 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), 0);
588 record
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
590 if ((ntohl(record
->rec
.pos
.hdlc
) & 0xffff0000) == 0xff030000) {
591 linktype
= DLT_PPP_SERIAL
;
592 fprintf(stderr
, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype
);
594 linktype
= DLT_CHDLC
;
595 fprintf(stderr
, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype
);
600 linktype
= DLT_EN10MB
;
601 fprintf(stderr
, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype
);
604 linktype
= DLT_ATM_RFC1483
;
605 fprintf(stderr
, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype
);
609 fprintf(stderr
, "Set DAG linktype to %d (DLT_NULL)\n", linktype
);
612 fprintf(stderr
, "Unknown DAG linktype %d\n", dag_linktype(p
->fd
));