]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
Add a "pcap_breakloop()" API to break out of the loop in
[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 */
19
20 #ifndef lint
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)";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <sys/param.h> /* optionally get BSD define */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "pcap-int.h"
36
37 #include <sys/mman.h>
38 #include <sys/socket.h>
39
40 struct mbuf; /* Squelch compiler warnings on some platforms for */
41 struct rtentry; /* declarations in <net/if.h> */
42 #include <net/if.h>
43
44 #include <dagnew.h>
45 #include <dagapi.h>
46
47 #ifndef min
48 #define min(a, b) ((a) > (b) ? (b) : (a))
49 #endif
50
51 #define MIN_DAG_SNAPLEN 12
52 #define MAX_DAG_SNAPLEN 2040
53 #define ATM_SNAPLEN 48
54
55 /* Size of ATM payload */
56 #define ATM_WLEN(h) ATM_SNAPLEN
57 #define ATM_SLEN(h) ATM_SNAPLEN
58
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)
63
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)
68
69 typedef struct pcap_dag_node {
70 struct pcap_dag_node *next;
71 pcap_t *p;
72 pid_t pid;
73 } pcap_dag_node_t;
74
75 static pcap_dag_node_t *pcap_dags = NULL;
76 static int atexit_handler_installed = 0;
77 static unsigned short endian_test_word = 0x0100;
78
79 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
80
81 /*
82 * Swap byte ordering of unsigned long long timestamp on a big endian
83 * machine.
84 */
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) \
94 : ull)
95
96 #ifdef DAG_ONLY
97 /* This code is reguired when compiling for a DAG device only. */
98 #include "pcap-dag.h"
99
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 */
104
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);
109
110 static void delete_pcap_dag(pcap_t *p) {
111 pcap_dag_node_t *curr = NULL, *prev = NULL;
112
113 for (prev = NULL, curr = pcap_dags;
114 curr != NULL && curr->p != p;
115 prev = curr, curr = curr->next) {
116 /* empty */
117 }
118
119 if (curr != NULL && curr->p == p) {
120 if (prev != NULL) {
121 prev->next = curr->next;
122 } else {
123 pcap_dags = curr->next;
124 }
125 }
126 }
127
128 /*
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.
131 */
132
133 static void dag_platform_close(pcap_t *p) {
134
135 #ifdef linux
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));
141
142 free(p->md.device);
143 }
144 #else
145 if (p != NULL) {
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));
150 }
151 #endif
152 delete_pcap_dag(p);
153 }
154
155 static void atexit_handler(void) {
156 while (pcap_dags != NULL) {
157 if (pcap_dags->pid == getpid()) {
158 dag_platform_close(pcap_dags->p);
159 } else {
160 delete_pcap_dag(pcap_dags->p);
161 }
162 }
163 }
164
165 static int new_pcap_dag(pcap_t *p) {
166 pcap_dag_node_t *node = NULL;
167
168 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
169 return -1;
170 }
171
172 if (!atexit_handler_installed) {
173 atexit(atexit_handler);
174 atexit_handler_installed = 1;
175 }
176
177 node->next = pcap_dags;
178 node->p = p;
179 node->pid = getpid();
180
181 pcap_dags = node;
182
183 return 0;
184 }
185
186 /*
187 * Get pointer to the ERF header for the next packet in the input
188 * stream. This function blocks until a packet becomes available.
189 */
190 static dag_record_t *get_next_dag_header(pcap_t *p) {
191 register dag_record_t *record;
192 int rlen;
193
194 /*
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'.
199 */
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);
202 }
203
204 record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
205
206 p->md.dag_mem_bottom += ntohs(record->rlen);
207
208 return record;
209 }
210
211 /*
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.
215 * A blocking
216 */
217 static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
218 u_char *dp = NULL;
219 int packet_len = 0, caplen = 0;
220 struct pcap_pkthdr pcap_header;
221
222 dag_record_t *header;
223 register unsigned long long ts;
224
225 /*
226 * Has "pcap_breakloop()" been called?
227 */
228 if (p->break_loop) {
229 /*
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.
232 */
233 p->break_loop = 0;
234 return -2;
235 }
236
237 /* Receive a single packet from the kernel */
238 header = get_next_dag_header(p);
239 dp = ((u_char *)header) + dag_record_size;
240
241 switch(header->type) {
242 case TYPE_ATM:
243 packet_len = ATM_WLEN(header);
244 caplen = ATM_SLEN(header);
245 dp += 4;
246 break;
247 case TYPE_ETH:
248 packet_len = ETHERNET_WLEN(header, p->md.dag_fcs_bits);
249 caplen = ETHERNET_SLEN(header, p->md.dag_fcs_bits);
250 dp += 2;
251 break;
252 case TYPE_HDLC_POS:
253 packet_len = HDLC_WLEN(header, p->md.dag_fcs_bits);
254 caplen = HDLC_SLEN(header, p->md.dag_fcs_bits);
255 break;
256 }
257
258 if (caplen > p->snapshot)
259 caplen = p->snapshot;
260
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;
266 } else {
267 p->md.stat.ps_drop += header->lctr;
268 }
269 }
270
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 */
275 return 0;
276 }
277 }
278
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;
288 }
289
290 /* Fill in our own header data */
291 pcap_header.caplen = caplen;
292 pcap_header.len = packet_len;
293
294 /*
295 * Count the packet.
296 */
297 p->md.stat.ps_recv++;
298
299 /* Call the user supplied callback function */
300 callback(user, &pcap_header, dp);
301
302 return 1;
303 }
304
305 /*
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.
310 *
311 * See also pcap(3).
312 */
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 */
315 pcap_t *handle;
316 char *s;
317 int n;
318
319 if (device == NULL) {
320 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
321 return NULL;
322 }
323 /* Allocate a handle for this session. */
324
325 handle = malloc(sizeof(*handle));
326 if (handle == NULL) {
327 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
328 return NULL;
329 }
330
331 /* Initialize some components of the pcap structure. */
332
333 memset(handle, 0, sizeof(*handle));
334
335 if (strstr(device, "/dev") == NULL) {
336 char * newDev = (char *)malloc(strlen(device) + 6);
337 newDev[0] = '\0';
338 strcat(newDev, "/dev/");
339 strcat(newDev,device);
340 device = newDev;
341 }
342
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));
346 return NULL;
347 }
348
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;
352 } else
353 if (snaplen < MIN_DAG_SNAPLEN) {
354 snaplen = MIN_DAG_SNAPLEN;
355 }
356 /* snap len has to be a multiple of 4 */
357 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
358
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));
362 return NULL;
363 }
364
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));
367 return NULL;
368 }
369
370 if(dag_start(handle->fd) < 0) {
371 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
372 return NULL;
373 }
374
375 /*
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!
379 */
380 handle->md.dag_mem_bottom = 0;
381 handle->md.dag_mem_top = 0;
382
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;
388 } else {
389 snprintf(ebuf, PCAP_ERRBUF_SIZE,
390 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
391 return NULL;
392 }
393 }
394
395 handle->snapshot = snaplen;
396 /*handle->md.timeout = to_ms; */
397
398 #ifdef linux
399 if (device) {
400 handle->md.device = strdup(device);
401 }
402
403 if (handle->md.device == NULL) {
404 snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup %s: %s\n", device, pcap_strerror(errno));
405 free(handle);
406 return NULL;
407 }
408 #endif
409
410 if ((handle->linktype = dag_get_datalink(handle)) < 0) {
411 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_linktype %s: unknown linktype\n", device);
412 return NULL;
413 }
414
415 handle->bufsize = 0;
416
417 if (new_pcap_dag(handle) < 0) {
418 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
419 return NULL;
420 }
421
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;
427
428 return handle;
429 }
430
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.
434 */
435 /*p->md.stat.ps_recv = 0;*/
436 /*p->md.stat.ps_drop = 0;*/
437
438 *ps = p->md.stat;
439
440 return 0;
441 }
442
443 /*
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.
447 *
448 * We don't bother getting any addresses for them.
449 *
450 * We also don't fail if we couldn't open "/proc/dag"; we just leave
451 * the list of interfaces as is.
452 */
453 int
454 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
455 {
456 FILE *proc_dag_f;
457 char linebuf[512];
458 int linenum;
459 unsigned char *p;
460 char name[512]; /* XXX - pick a size */
461 char *q, *saveq;
462 struct ifreq ifrflags;
463 int ret = 0;
464
465 /* Quick exit if /proc/dag not readable */
466 proc_dag_f = fopen("/proc/dag", "r");
467 if (proc_dag_f == NULL)
468 {
469 int i, fd;
470 char dev[16] = "dagx";
471
472 for (i = '0'; ret == 0 && i <= '9'; i++) {
473 dev[3] = i;
474 if (pcap_add_if(devlistp, dev, 0, NULL, errbuf) == -1) {
475 /*
476 * Failure.
477 */
478 ret = -1;
479 }
480 }
481
482 return (ret);
483 }
484
485 for (linenum = 1;
486 fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) {
487
488 /*
489 * Skip the first two lines - they're headers.
490 */
491 if (linenum <= 2)
492 continue;
493
494 p = &linebuf[0];
495
496 if (*p == '\0' || *p == '\n' || *p != 'D')
497 continue; /* not a Dag line */
498
499 /*
500 * Get the interface name.
501 */
502 q = &name[0];
503 while (*p != '\0' && *p != ':') {
504 if (*p != ' ')
505 *q++ = tolower(*p++);
506 else
507 p++;
508 }
509 *q = '\0';
510
511 /*
512 * Add an entry for this interface, with no addresses.
513 */
514 p[strlen(p) - 1] = '\0'; /* get rid of \n */
515 if (pcap_add_if(devlistp, name, 0, strdup(p + 2), errbuf) == -1) {
516 /*
517 * Failure.
518 */
519 ret = -1;
520 break;
521 }
522 }
523 if (ret != -1) {
524 /*
525 * Well, we didn't fail for any other reason; did we
526 * fail due to an error reading the file?
527 */
528 if (ferror(proc_dag_f)) {
529 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
530 "Error reading /proc/dag: %s",
531 pcap_strerror(errno));
532 ret = -1;
533 }
534 }
535
536 (void)fclose(proc_dag_f);
537 return (ret);
538 }
539
540 /*
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
543 * with DAG cards.
544 */
545 static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
546 if (!p)
547 return -1;
548 if (!fp) {
549 strncpy(p->errbuf, "setfilter: No filter specified",
550 sizeof(p->errbuf));
551 return -1;
552 }
553
554 /* Make our private copy of the filter */
555
556 if (install_bpf_program(p, fp) < 0) {
557 snprintf(p->errbuf, sizeof(p->errbuf),
558 "malloc: %s", pcap_strerror(errno));
559 return -1;
560 }
561
562 p->md.use_bpf = 0;
563
564 return (0);
565 }
566
567 static int
568 dag_set_datalink(pcap_t *p, int dlt)
569 {
570 return (0);
571 }
572
573 static int
574 dag_get_datalink(pcap_t *p)
575 {
576 int linktype = -1;
577
578 /* Check the type through a dagapi call.
579 */
580 switch(dag_linktype(p->fd)) {
581 case TYPE_HDLC_POS: {
582 dag_record_t *record;
583
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);
587 }
588 record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
589
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);
593 } else {
594 linktype = DLT_CHDLC;
595 fprintf(stderr, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype);
596 }
597 break;
598 }
599 case TYPE_ETH:
600 linktype = DLT_EN10MB;
601 fprintf(stderr, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype);
602 break;
603 case TYPE_ATM:
604 linktype = DLT_ATM_RFC1483;
605 fprintf(stderr, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype);
606 break;
607 case TYPE_LEGACY:
608 linktype = DLT_NULL;
609 fprintf(stderr, "Set DAG linktype to %d (DLT_NULL)\n", linktype);
610 break;
611 default:
612 fprintf(stderr, "Unknown DAG linktype %d\n", dag_linktype(p->fd));
613 break;
614 }
615
616 return linktype;
617 }