]>
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.9 2003-10-02 07:07:49 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 or -1 if an
214 * error occured. A blocking
216 static int dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
) {
218 int packet_len
= 0, caplen
= 0;
219 struct pcap_pkthdr pcap_header
;
221 dag_record_t
*header
;
222 register unsigned long long ts
;
224 /* Receive a single packet from the kernel */
225 header
= get_next_dag_header(p
);
226 dp
= ((u_char
*)header
) + dag_record_size
;
228 switch(header
->type
) {
230 packet_len
= ATM_WLEN(header
);
231 caplen
= ATM_SLEN(header
);
235 packet_len
= ETHERNET_WLEN(header
, p
->md
.dag_fcs_bits
);
236 caplen
= ETHERNET_SLEN(header
, p
->md
.dag_fcs_bits
);
240 packet_len
= HDLC_WLEN(header
, p
->md
.dag_fcs_bits
);
241 caplen
= HDLC_SLEN(header
, p
->md
.dag_fcs_bits
);
245 if (caplen
> p
->snapshot
)
246 caplen
= p
->snapshot
;
248 /* Count lost packets */
249 if (header
->lctr
> 0 && (p
->md
.stat
.ps_drop
+1) != 0) {
250 if (header
->lctr
== 0xffff ||
251 (p
->md
.stat
.ps_drop
+ header
->lctr
) < p
->md
.stat
.ps_drop
) {
252 p
->md
.stat
.ps_drop
== ~0;
254 p
->md
.stat
.ps_drop
+= header
->lctr
;
258 /* Run the packet filter if not using kernel filter */
259 if (p
->fcode
.bf_insns
) {
260 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0) {
261 /* rejected by filter */
266 /* convert between timestamp formats */
267 ts
= SWAP_TS(header
->ts
);
268 pcap_header
.ts
.tv_sec
= ts
>> 32;
269 ts
= ((ts
& 0xffffffffULL
) * 1000 * 1000);
270 ts
+= (ts
& 0x80000000ULL
) << 1; /* rounding */
271 pcap_header
.ts
.tv_usec
= ts
>> 32;
272 if (pcap_header
.ts
.tv_usec
>= 1000000) {
273 pcap_header
.ts
.tv_usec
-= 1000000;
274 pcap_header
.ts
.tv_sec
+= 1;
277 /* Fill in our own header data */
278 pcap_header
.caplen
= caplen
;
279 pcap_header
.len
= packet_len
;
284 p
->md
.stat
.ps_recv
++;
286 /* Call the user supplied callback function */
287 callback(user
, &pcap_header
, dp
);
293 * Get a handle for a live capture from the given DAG device. Passing a NULL
294 * device will result in a failure. The promisc flag is ignored because DAG
295 * cards are always promiscuous. The to_ms parameter is also ignored as it is
296 * not supported in hardware.
300 pcap_t
*dag_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
) {
301 char conf
[30]; /* dag configure string */
306 if (device
== NULL
) {
307 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "device is NULL: %s", pcap_strerror(errno
));
310 /* Allocate a handle for this session. */
312 handle
= malloc(sizeof(*handle
));
313 if (handle
== NULL
) {
314 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc %s: %s", device
, pcap_strerror(errno
));
318 /* Initialize some components of the pcap structure. */
320 memset(handle
, 0, sizeof(*handle
));
322 if (strstr(device
, "/dev") == NULL
) {
323 char * newDev
= (char *)malloc(strlen(device
) + 6);
325 strcat(newDev
, "/dev/");
326 strcat(newDev
,device
);
330 /* setup device parameters */
331 if((handle
->fd
= dag_open((char *)device
)) < 0) {
332 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_open %s: %s", device
, pcap_strerror(errno
));
336 /* set the card snap length as specified by the specified snaplen parameter */
337 if (snaplen
== 0 || snaplen
> MAX_DAG_SNAPLEN
) {
338 snaplen
= MAX_DAG_SNAPLEN
;
340 if (snaplen
< MIN_DAG_SNAPLEN
) {
341 snaplen
= MIN_DAG_SNAPLEN
;
343 /* snap len has to be a multiple of 4 */
344 snprintf(conf
, 30, "varlen slen=%d", (snaplen
+ 3) & ~3);
346 fprintf(stderr
, "Configuring DAG with '%s'.\n", conf
);
347 if(dag_configure(handle
->fd
, conf
) < 0) {
348 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_configure %s: %s\n", device
, pcap_strerror(errno
));
352 if((handle
->md
.dag_mem_base
= dag_mmap(handle
->fd
)) == MAP_FAILED
) {
353 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"dag_mmap %s: %s\n", device
, pcap_strerror(errno
));
357 if(dag_start(handle
->fd
) < 0) {
358 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_start %s: %s\n", device
, pcap_strerror(errno
));
363 * Important! You have to ensure bottom is properly
364 * initialized to zero on startup, it won't give you
365 * a compiler warning if you make this mistake!
367 handle
->md
.dag_mem_bottom
= 0;
368 handle
->md
.dag_mem_top
= 0;
370 /* TODO: query the card */
371 handle
->md
.dag_fcs_bits
= 32;
372 if ((s
= getenv("ERF_FCS_BITS")) != NULL
) {
373 if ((n
= atoi(s
)) == 0 || n
== 16|| n
== 32) {
374 handle
->md
.dag_fcs_bits
= n
;
376 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
377 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device
, n
);
382 handle
->snapshot
= snaplen
;
383 /*handle->md.timeout = to_ms; */
387 handle
->md
.device
= strdup(device
);
390 if (handle
->md
.device
== NULL
) {
391 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "str_dup %s: %s\n", device
, pcap_strerror(errno
));
397 if ((handle
->linktype
= dag_get_datalink(handle
)) < 0) {
398 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "dag_get_linktype %s: unknown linktype\n", device
);
404 if (new_pcap_dag(handle
) < 0) {
405 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "new_pcap_dag %s: %s\n", device
, pcap_strerror(errno
));
409 handle
->read_op
= dag_read
;
410 handle
->setfilter_op
= dag_setfilter
;
411 handle
->set_datalink_op
= dag_set_datalink
;
412 handle
->stats_op
= dag_stats
;
413 handle
->close_op
= dag_platform_close
;
418 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
419 /* This needs to be filled out correctly. Hopefully a dagapi call will
420 provide all necessary information.
422 /*p->md.stat.ps_recv = 0;*/
423 /*p->md.stat.ps_drop = 0;*/
431 * Get from "/proc/dag" all interfaces listed there; if they're
432 * already in the list of interfaces we have, that won't add another
433 * instance, but if they're not, that'll add them.
435 * We don't bother getting any addresses for them.
437 * We also don't fail if we couldn't open "/proc/dag"; we just leave
438 * the list of interfaces as is.
441 dag_platform_finddevs(pcap_if_t
**devlistp
, char *errbuf
)
447 char name
[512]; /* XXX - pick a size */
449 struct ifreq ifrflags
;
452 /* Quick exit if /proc/dag not readable */
453 proc_dag_f
= fopen("/proc/dag", "r");
454 if (proc_dag_f
== NULL
)
457 char dev
[16] = "dagx";
459 for (i
= '0'; ret
== 0 && i
<= '9'; i
++) {
461 if (pcap_add_if(devlistp
, dev
, 0, NULL
, errbuf
) == -1) {
473 fgets(linebuf
, sizeof linebuf
, proc_dag_f
) != NULL
; linenum
++) {
476 * Skip the first two lines - they're headers.
483 if (*p
== '\0' || *p
== '\n' || *p
!= 'D')
484 continue; /* not a Dag line */
487 * Get the interface name.
490 while (*p
!= '\0' && *p
!= ':') {
492 *q
++ = tolower(*p
++);
499 * Add an entry for this interface, with no addresses.
501 p
[strlen(p
) - 1] = '\0'; /* get rid of \n */
502 if (pcap_add_if(devlistp
, name
, 0, strdup(p
+ 2), errbuf
) == -1) {
512 * Well, we didn't fail for any other reason; did we
513 * fail due to an error reading the file?
515 if (ferror(proc_dag_f
)) {
516 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
517 "Error reading /proc/dag: %s",
518 pcap_strerror(errno
));
523 (void)fclose(proc_dag_f
);
528 * Installs the gven bpf filter program in the given pcap structure. There is
529 * no attempt to store the filter in kernel memory as that is not supported
532 static int dag_setfilter(pcap_t
*p
, struct bpf_program
*fp
) {
536 strncpy(p
->errbuf
, "setfilter: No filter specified",
541 /* Make our private copy of the filter */
543 if (install_bpf_program(p
, fp
) < 0) {
544 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
545 "malloc: %s", pcap_strerror(errno
));
555 dag_set_datalink(pcap_t
*p
, int dlt
)
561 dag_get_datalink(pcap_t
*p
)
565 /* Check the type through a dagapi call.
567 switch(dag_linktype(p
->fd
)) {
568 case TYPE_HDLC_POS
: {
569 dag_record_t
*record
;
571 /* peek at the first available record to see if it is PPP */
572 while ((p
->md
.dag_mem_top
- p
->md
.dag_mem_bottom
) < (dag_record_size
+ 4)) {
573 p
->md
.dag_mem_top
= dag_offset(p
->fd
, &(p
->md
.dag_mem_bottom
), 0);
575 record
= (dag_record_t
*)(p
->md
.dag_mem_base
+ p
->md
.dag_mem_bottom
);
577 if ((ntohl(record
->rec
.pos
.hdlc
) & 0xffff0000) == 0xff030000) {
578 linktype
= DLT_PPP_SERIAL
;
579 fprintf(stderr
, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype
);
581 linktype
= DLT_CHDLC
;
582 fprintf(stderr
, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype
);
587 linktype
= DLT_EN10MB
;
588 fprintf(stderr
, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype
);
591 linktype
= DLT_ATM_RFC1483
;
592 fprintf(stderr
, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype
);
596 fprintf(stderr
, "Set DAG linktype to %d (DLT_NULL)\n", linktype
);
599 fprintf(stderr
, "Unknown DAG linktype %d\n", dag_linktype(p
->fd
));