]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
From Koryn Grant <[email protected]> - DAG support enhancements and fixes:
[libpcap] / pcap-dag.c
1 /*
2 * pcap-dag.c: Packet capture interface for Endace DAG card.
3 *
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.
11 *
12 * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 *
14 * Modifications:
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
18 * 2003 September - Koryn Grant <koryn@endace.com>
19 * Added support for nonblocking operation.
20 * Added support for processing more than a single packet in pcap_dispatch().
21 * Fixed bug in loss counter code.
22 * Improved portability of loss counter code (e.g. use UINT_MAX instead of 0xffff).
23 * Removed unused local variables.
24 * Added required headers (ctype.h, limits.h, unistd.h, netinet/in.h).
25 * 2003 October - Koryn Grant <koryn@endace.com.>
26 * Changed semantics to match those of standard pcap on linux.
27 * - packets rejected by the filter are not counted.
28 */
29
30 #ifndef lint
31 static const char rcsid[] _U_ =
32 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.12 2003-11-20 01:21:26 guy Exp $ (LBL)";
33 #endif
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <sys/param.h> /* optionally get BSD define */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #include "pcap-int.h"
46
47 #include <ctype.h>
48 #include <netinet/in.h>
49 #include <sys/mman.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 #include <unistd.h>
53
54 struct mbuf; /* Squelch compiler warnings on some platforms for */
55 struct rtentry; /* declarations in <net/if.h> */
56 #include <net/if.h>
57
58 #include <dagnew.h>
59 #include <dagapi.h>
60
61 #define MIN_DAG_SNAPLEN 12
62 #define MAX_DAG_SNAPLEN 2040
63 #define ATM_SNAPLEN 48
64
65 typedef struct pcap_dag_node {
66 struct pcap_dag_node *next;
67 pcap_t *p;
68 pid_t pid;
69 } pcap_dag_node_t;
70
71 static pcap_dag_node_t *pcap_dags = NULL;
72 static int atexit_handler_installed = 0;
73 static const unsigned short endian_test_word = 0x0100;
74
75 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
76
77 /*
78 * Swap byte ordering of unsigned long long timestamp on a big endian
79 * machine.
80 */
81 #define SWAP_TS(ull) ((ull & 0xff00000000000000LL) >> 56) | \
82 ((ull & 0x00ff000000000000LL) >> 40) | \
83 ((ull & 0x0000ff0000000000LL) >> 24) | \
84 ((ull & 0x000000ff00000000LL) >> 8) | \
85 ((ull & 0x00000000ff000000LL) << 8) | \
86 ((ull & 0x0000000000ff0000LL) << 24) | \
87 ((ull & 0x000000000000ff00LL) << 40) | \
88 ((ull & 0x00000000000000ffLL) << 56)
89
90
91 #ifdef DAG_ONLY
92 /* This code is required when compiling for a DAG device only. */
93 #include "pcap-dag.h"
94
95 /* Replace dag function names with pcap equivalent. */
96 #define dag_open_live pcap_open_live
97 #define dag_platform_finddevs pcap_platform_finddevs
98 #endif /* DAG_ONLY */
99
100 static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
101 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
102 static int dag_set_datalink(pcap_t *p, int dlt);
103 static int dag_get_datalink(pcap_t *p);
104
105 static void delete_pcap_dag(pcap_t *p) {
106 pcap_dag_node_t *curr = NULL, *prev = NULL;
107
108 for (prev = NULL, curr = pcap_dags;
109 curr != NULL && curr->p != p;
110 prev = curr, curr = curr->next) {
111 /* empty */
112 }
113
114 if (curr != NULL && curr->p == p) {
115 if (prev != NULL) {
116 prev->next = curr->next;
117 } else {
118 pcap_dags = curr->next;
119 }
120 }
121 }
122
123 /*
124 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
125 * in the pcap_t structure, and closes the file descriptor for the DAG card.
126 */
127
128 static void dag_platform_close(pcap_t *p) {
129
130 #ifdef linux
131 if (p != NULL && p->md.device != NULL) {
132 if(dag_stop(p->fd) < 0)
133 fprintf(stderr,"dag_stop %s: %s\n", p->md.device, strerror(errno));
134 if(dag_close(p->fd) < 0)
135 fprintf(stderr,"dag_close %s: %s\n", p->md.device, strerror(errno));
136
137 free(p->md.device);
138 }
139 #else
140 if (p != NULL) {
141 if(dag_stop(p->fd) < 0)
142 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
143 if(dag_close(p->fd) < 0)
144 fprintf(stderr,"dag_close: %s\n", strerror(errno));
145 }
146 #endif
147 delete_pcap_dag(p);
148 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
149 }
150
151 static void atexit_handler(void) {
152 while (pcap_dags != NULL) {
153 if (pcap_dags->pid == getpid()) {
154 dag_platform_close(pcap_dags->p);
155 } else {
156 delete_pcap_dag(pcap_dags->p);
157 }
158 }
159 }
160
161 static int new_pcap_dag(pcap_t *p) {
162 pcap_dag_node_t *node = NULL;
163
164 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
165 return -1;
166 }
167
168 if (!atexit_handler_installed) {
169 atexit(atexit_handler);
170 atexit_handler_installed = 1;
171 }
172
173 node->next = pcap_dags;
174 node->p = p;
175 node->pid = getpid();
176
177 pcap_dags = node;
178
179 return 0;
180 }
181
182 /*
183 * Read at most max_packets from the capture stream and call the callback
184 * for each of them. Returns the number of packets handled, -1 if an
185 * error occured, or -2 if we were told to break out of the loop.
186 */
187 static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
188 unsigned int processed = 0;
189 int flags = p->md.dag_offset_flags;
190 unsigned int nonblocking = flags & DAGF_NONBLOCK;
191
192 for (;;)
193 {
194 /* Get the next bufferful of packets (if necessary). */
195 while (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) {
196
197 /*
198 * Has "pcap_breakloop()" been called?
199 */
200 if (p->break_loop) {
201 /*
202 * Yes - clear the flag that indicates that
203 * it has, and return -2 to indicate that
204 * we were told to break out of the loop.
205 */
206 p->break_loop = 0;
207 return -2;
208 }
209
210 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
211 if ((p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) && nonblocking)
212 {
213 /* Pcap is configured to process only available packets, and there aren't any. */
214 return 0;
215 }
216 }
217
218 /* Process the packets. */
219 while (p->md.dag_mem_top - p->md.dag_mem_bottom >= dag_record_size) {
220
221 unsigned short packet_len = 0;
222 int caplen = 0;
223 struct pcap_pkthdr pcap_header;
224
225 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
226 u_char *dp = ((u_char *)header) + dag_record_size;
227 unsigned short rlen;
228
229 /*
230 * Has "pcap_breakloop()" been called?
231 */
232 if (p->break_loop) {
233 /*
234 * Yes - clear the flag that indicates that
235 * it has, and return -2 to indicate that
236 * we were told to break out of the loop.
237 */
238 p->break_loop = 0;
239 return -2;
240 }
241
242 if (IS_BIGENDIAN())
243 {
244 rlen = header->rlen;
245 }
246 else
247 {
248 rlen = ntohs(header->rlen);
249 }
250 p->md.dag_mem_bottom += rlen;
251
252 switch(header->type) {
253 case TYPE_ATM:
254 packet_len = ATM_SNAPLEN;
255 caplen = ATM_SNAPLEN;
256 dp += 4;
257 break;
258
259 case TYPE_ETH:
260 if (IS_BIGENDIAN())
261 {
262 packet_len = header->wlen;
263 }
264 else
265 {
266 packet_len = ntohs(header->wlen);
267 }
268 packet_len -= (p->md.dag_fcs_bits >> 3);
269 caplen = rlen - dag_record_size - 2;
270 if (caplen > packet_len)
271 {
272 caplen = packet_len;
273 }
274 dp += 2;
275 break;
276
277 case TYPE_HDLC_POS:
278 if (IS_BIGENDIAN())
279 {
280 packet_len = header->wlen;
281 }
282 else
283 {
284 packet_len = ntohs(header->wlen);
285 }
286 packet_len -= (p->md.dag_fcs_bits >> 3);
287 caplen = rlen - dag_record_size;
288 if (caplen > packet_len)
289 {
290 caplen = packet_len;
291 }
292 break;
293 }
294
295 if (caplen > p->snapshot)
296 caplen = p->snapshot;
297
298 /* Count lost packets. */
299 if (header->lctr) {
300 if (p->md.stat.ps_drop > (UINT_MAX - header->lctr)) {
301 p->md.stat.ps_drop = UINT_MAX;
302 } else {
303 p->md.stat.ps_drop += header->lctr;
304 }
305 }
306
307 /* Run the packet filter if there is one. */
308 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
309
310 /* convert between timestamp formats */
311 register unsigned long long ts;
312
313 if (IS_BIGENDIAN())
314 {
315 ts = SWAP_TS(header->ts);
316 }
317 else
318 {
319 ts = header->ts;
320 }
321
322 pcap_header.ts.tv_sec = ts >> 32;
323 ts = (ts & 0xffffffffULL) * 1000000;
324 ts += 0x80000000; /* rounding */
325 pcap_header.ts.tv_usec = ts >> 32;
326 if (pcap_header.ts.tv_usec >= 1000000) {
327 pcap_header.ts.tv_usec -= 1000000;
328 pcap_header.ts.tv_sec++;
329 }
330
331 /* Fill in our own header data */
332 pcap_header.caplen = caplen;
333 pcap_header.len = packet_len;
334
335 /* Count the packet. */
336 p->md.stat.ps_recv++;
337
338 /* Call the user supplied callback function */
339 callback(user, &pcap_header, dp);
340
341 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
342 processed++;
343 if (processed == cnt)
344 {
345 /* Reached the user-specified limit. */
346 return cnt;
347 }
348 }
349 }
350
351 if (nonblocking || processed)
352 {
353 return processed;
354 }
355 }
356
357 return processed;
358 }
359
360 /*
361 * Get a handle for a live capture from the given DAG device. Passing a NULL
362 * device will result in a failure. The promisc flag is ignored because DAG
363 * cards are always promiscuous. The to_ms parameter is also ignored as it is
364 * not supported in hardware.
365 *
366 * See also pcap(3).
367 */
368 pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) {
369 char conf[30]; /* dag configure string */
370 pcap_t *handle;
371 char *s;
372 int n;
373
374 if (device == NULL) {
375 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
376 return NULL;
377 }
378 /* Allocate a handle for this session. */
379
380 handle = malloc(sizeof(*handle));
381 if (handle == NULL) {
382 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
383 return NULL;
384 }
385
386 /* Initialize some components of the pcap structure. */
387
388 memset(handle, 0, sizeof(*handle));
389
390 if (strstr(device, "/dev") == NULL) {
391 char * newDev = (char *)malloc(strlen(device) + 6);
392 newDev[0] = '\0';
393 strcat(newDev, "/dev/");
394 strcat(newDev,device);
395 device = newDev;
396 } else {
397 device = strdup(device);
398 }
399
400 if (device == NULL) {
401 snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup: %s\n", pcap_strerror(errno));
402 goto fail;
403 }
404
405 /* setup device parameters */
406 if((handle->fd = dag_open((char *)device)) < 0) {
407 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
408 goto fail;
409 }
410
411 /* set the card snap length to the specified snaplen parameter */
412 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
413 snaplen = MAX_DAG_SNAPLEN;
414 } else if (snaplen < MIN_DAG_SNAPLEN) {
415 snaplen = MIN_DAG_SNAPLEN;
416 }
417 /* snap len has to be a multiple of 4 */
418 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
419
420 fprintf(stderr, "Configuring DAG with '%s'.\n", conf);
421 if(dag_configure(handle->fd, conf) < 0) {
422 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
423 goto fail;
424 }
425
426 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
427 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
428 goto fail;
429 }
430
431 if(dag_start(handle->fd) < 0) {
432 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
433 goto fail;
434 }
435
436 /*
437 * Important! You have to ensure bottom is properly
438 * initialized to zero on startup, it won't give you
439 * a compiler warning if you make this mistake!
440 */
441 handle->md.dag_mem_bottom = 0;
442 handle->md.dag_mem_top = 0;
443
444 /* TODO: query the card */
445 handle->md.dag_fcs_bits = 32;
446 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
447 if ((n = atoi(s)) == 0 || n == 16|| n == 32) {
448 handle->md.dag_fcs_bits = n;
449 } else {
450 snprintf(ebuf, PCAP_ERRBUF_SIZE,
451 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
452 goto fail;
453 }
454 }
455
456 handle->snapshot = snaplen;
457 /*handle->md.timeout = to_ms; */
458
459 if ((handle->linktype = dag_get_datalink(handle)) < 0) {
460 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_linktype %s: unknown linktype\n", device);
461 goto fail;
462 }
463
464 handle->bufsize = 0;
465
466 if (new_pcap_dag(handle) < 0) {
467 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
468 goto fail;
469 }
470
471 #ifdef linux
472 handle->md.device = (char *)device;
473 #else
474 free((char *)device);
475 device = NULL;
476 #endif
477
478 handle->read_op = dag_read;
479 handle->setfilter_op = dag_setfilter;
480 handle->set_datalink_op = dag_set_datalink;
481 handle->stats_op = dag_stats;
482 handle->close_op = dag_platform_close;
483
484 return handle;
485
486 fail:
487 if (device != NULL) {
488 free((char *)device);
489 }
490 if (handle != NULL) {
491 free(handle);
492 }
493
494 return NULL;
495 }
496
497 static int dag_stats(pcap_t *p, struct pcap_stat *ps) {
498 /* This needs to be filled out correctly. Hopefully a dagapi call will
499 provide all necessary information.
500 */
501 /*p->md.stat.ps_recv = 0;*/
502 /*p->md.stat.ps_drop = 0;*/
503
504 *ps = p->md.stat;
505
506 return 0;
507 }
508
509 /*
510 * Get from "/proc/dag" all interfaces listed there; if they're
511 * already in the list of interfaces we have, that won't add another
512 * instance, but if they're not, that'll add them.
513 *
514 * We don't bother getting any addresses for them.
515 *
516 * We also don't fail if we couldn't open "/proc/dag"; we just leave
517 * the list of interfaces as is.
518 */
519 int
520 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
521 {
522 FILE *proc_dag_f;
523 char linebuf[512];
524 int linenum;
525 unsigned char *p;
526 char name[512]; /* XXX - pick a size */
527 char *q;
528 int ret = 0;
529
530 /* Quick exit if /proc/dag not readable */
531 proc_dag_f = fopen("/proc/dag", "r");
532 if (proc_dag_f == NULL)
533 {
534 int i;
535 char dev[16] = "dagx";
536
537 for (i = '0'; ret == 0 && i <= '9'; i++) {
538 dev[3] = i;
539 if (pcap_add_if(devlistp, dev, 0, NULL, errbuf) == -1) {
540 /*
541 * Failure.
542 */
543 ret = -1;
544 }
545 }
546
547 return (ret);
548 }
549
550 for (linenum = 1;
551 fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) {
552
553 /*
554 * Skip the first two lines - they're headers.
555 */
556 if (linenum <= 2)
557 continue;
558
559 p = &linebuf[0];
560
561 if (*p == '\0' || *p == '\n' || *p != 'D')
562 continue; /* not a Dag line */
563
564 /*
565 * Get the interface name.
566 */
567 q = &name[0];
568 while (*p != '\0' && *p != ':') {
569 if (*p != ' ')
570 *q++ = tolower(*p++);
571 else
572 p++;
573 }
574 *q = '\0';
575
576 /*
577 * Add an entry for this interface, with no addresses.
578 */
579 p[strlen(p) - 1] = '\0'; /* get rid of \n */
580 if (pcap_add_if(devlistp, name, 0, strdup(p + 2), errbuf) == -1) {
581 /*
582 * Failure.
583 */
584 ret = -1;
585 break;
586 }
587 }
588 if (ret != -1) {
589 /*
590 * Well, we didn't fail for any other reason; did we
591 * fail due to an error reading the file?
592 */
593 if (ferror(proc_dag_f)) {
594 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
595 "Error reading /proc/dag: %s",
596 pcap_strerror(errno));
597 ret = -1;
598 }
599 }
600
601 (void)fclose(proc_dag_f);
602 return (ret);
603 }
604
605 /*
606 * Installs the given bpf filter program in the given pcap structure. There is
607 * no attempt to store the filter in kernel memory as that is not supported
608 * with DAG cards.
609 */
610 static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
611 if (!p)
612 return -1;
613 if (!fp) {
614 strncpy(p->errbuf, "setfilter: No filter specified",
615 sizeof(p->errbuf));
616 return -1;
617 }
618
619 /* Make our private copy of the filter */
620
621 if (install_bpf_program(p, fp) < 0) {
622 snprintf(p->errbuf, sizeof(p->errbuf),
623 "malloc: %s", pcap_strerror(errno));
624 return -1;
625 }
626
627 p->md.use_bpf = 0;
628
629 return (0);
630 }
631
632 static int
633 dag_set_datalink(pcap_t *p, int dlt)
634 {
635 return (0);
636 }
637
638 static int
639 dag_get_datalink(pcap_t *p)
640 {
641 int linktype = -1;
642
643 /* Check the type through a dagapi call.
644 */
645 switch(dag_linktype(p->fd)) {
646 case TYPE_HDLC_POS: {
647 dag_record_t *record;
648
649 /* peek at the first available record to see if it is PPP */
650 while ((p->md.dag_mem_top - p->md.dag_mem_bottom) < (dag_record_size + 4)) {
651 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), 0);
652 }
653 record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
654
655 if ((ntohl(record->rec.pos.hdlc) & 0xffff0000) == 0xff030000) {
656 linktype = DLT_PPP_SERIAL;
657 fprintf(stderr, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype);
658 } else {
659 linktype = DLT_CHDLC;
660 fprintf(stderr, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype);
661 }
662 break;
663 }
664 case TYPE_ETH:
665 linktype = DLT_EN10MB;
666 fprintf(stderr, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype);
667 break;
668 case TYPE_ATM:
669 linktype = DLT_ATM_RFC1483;
670 fprintf(stderr, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype);
671 break;
672 case TYPE_LEGACY:
673 linktype = DLT_NULL;
674 fprintf(stderr, "Set DAG linktype to %d (DLT_NULL)\n", linktype);
675 break;
676 default:
677 fprintf(stderr, "Unknown DAG linktype %d\n", dag_linktype(p->fd));
678 break;
679 }
680
681 return linktype;
682 }