]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
From Jesper Peterson:
[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.9 2003-10-02 07:07:49 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 or -1 if an
214 * error occured. A blocking
215 */
216 static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
217 u_char *dp = NULL;
218 int packet_len = 0, caplen = 0;
219 struct pcap_pkthdr pcap_header;
220
221 dag_record_t *header;
222 register unsigned long long ts;
223
224 /* Receive a single packet from the kernel */
225 header = get_next_dag_header(p);
226 dp = ((u_char *)header) + dag_record_size;
227
228 switch(header->type) {
229 case TYPE_ATM:
230 packet_len = ATM_WLEN(header);
231 caplen = ATM_SLEN(header);
232 dp += 4;
233 break;
234 case TYPE_ETH:
235 packet_len = ETHERNET_WLEN(header, p->md.dag_fcs_bits);
236 caplen = ETHERNET_SLEN(header, p->md.dag_fcs_bits);
237 dp += 2;
238 break;
239 case TYPE_HDLC_POS:
240 packet_len = HDLC_WLEN(header, p->md.dag_fcs_bits);
241 caplen = HDLC_SLEN(header, p->md.dag_fcs_bits);
242 break;
243 }
244
245 if (caplen > p->snapshot)
246 caplen = p->snapshot;
247
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;
253 } else {
254 p->md.stat.ps_drop += header->lctr;
255 }
256 }
257
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 */
262 return 0;
263 }
264 }
265
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;
275 }
276
277 /* Fill in our own header data */
278 pcap_header.caplen = caplen;
279 pcap_header.len = packet_len;
280
281 /*
282 * Count the packet.
283 */
284 p->md.stat.ps_recv++;
285
286 /* Call the user supplied callback function */
287 callback(user, &pcap_header, dp);
288
289 return 1;
290 }
291
292 /*
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.
297 *
298 * See also pcap(3).
299 */
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 */
302 pcap_t *handle;
303 char *s;
304 int n;
305
306 if (device == NULL) {
307 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
308 return NULL;
309 }
310 /* Allocate a handle for this session. */
311
312 handle = malloc(sizeof(*handle));
313 if (handle == NULL) {
314 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
315 return NULL;
316 }
317
318 /* Initialize some components of the pcap structure. */
319
320 memset(handle, 0, sizeof(*handle));
321
322 if (strstr(device, "/dev") == NULL) {
323 char * newDev = (char *)malloc(strlen(device) + 6);
324 newDev[0] = '\0';
325 strcat(newDev, "/dev/");
326 strcat(newDev,device);
327 device = newDev;
328 }
329
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));
333 return NULL;
334 }
335
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;
339 } else
340 if (snaplen < MIN_DAG_SNAPLEN) {
341 snaplen = MIN_DAG_SNAPLEN;
342 }
343 /* snap len has to be a multiple of 4 */
344 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
345
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));
349 return NULL;
350 }
351
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));
354 return NULL;
355 }
356
357 if(dag_start(handle->fd) < 0) {
358 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
359 return NULL;
360 }
361
362 /*
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!
366 */
367 handle->md.dag_mem_bottom = 0;
368 handle->md.dag_mem_top = 0;
369
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;
375 } else {
376 snprintf(ebuf, PCAP_ERRBUF_SIZE,
377 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
378 return NULL;
379 }
380 }
381
382 handle->snapshot = snaplen;
383 /*handle->md.timeout = to_ms; */
384
385 #ifdef linux
386 if (device) {
387 handle->md.device = strdup(device);
388 }
389
390 if (handle->md.device == NULL) {
391 snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup %s: %s\n", device, pcap_strerror(errno));
392 free(handle);
393 return NULL;
394 }
395 #endif
396
397 if ((handle->linktype = dag_get_datalink(handle)) < 0) {
398 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_linktype %s: unknown linktype\n", device);
399 return NULL;
400 }
401
402 handle->bufsize = 0;
403
404 if (new_pcap_dag(handle) < 0) {
405 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
406 return NULL;
407 }
408
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;
414
415 return handle;
416 }
417
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.
421 */
422 /*p->md.stat.ps_recv = 0;*/
423 /*p->md.stat.ps_drop = 0;*/
424
425 *ps = p->md.stat;
426
427 return 0;
428 }
429
430 /*
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.
434 *
435 * We don't bother getting any addresses for them.
436 *
437 * We also don't fail if we couldn't open "/proc/dag"; we just leave
438 * the list of interfaces as is.
439 */
440 int
441 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
442 {
443 FILE *proc_dag_f;
444 char linebuf[512];
445 int linenum;
446 unsigned char *p;
447 char name[512]; /* XXX - pick a size */
448 char *q, *saveq;
449 struct ifreq ifrflags;
450 int ret = 0;
451
452 /* Quick exit if /proc/dag not readable */
453 proc_dag_f = fopen("/proc/dag", "r");
454 if (proc_dag_f == NULL)
455 {
456 int i, fd;
457 char dev[16] = "dagx";
458
459 for (i = '0'; ret == 0 && i <= '9'; i++) {
460 dev[3] = i;
461 if (pcap_add_if(devlistp, dev, 0, NULL, errbuf) == -1) {
462 /*
463 * Failure.
464 */
465 ret = -1;
466 }
467 }
468
469 return (ret);
470 }
471
472 for (linenum = 1;
473 fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) {
474
475 /*
476 * Skip the first two lines - they're headers.
477 */
478 if (linenum <= 2)
479 continue;
480
481 p = &linebuf[0];
482
483 if (*p == '\0' || *p == '\n' || *p != 'D')
484 continue; /* not a Dag line */
485
486 /*
487 * Get the interface name.
488 */
489 q = &name[0];
490 while (*p != '\0' && *p != ':') {
491 if (*p != ' ')
492 *q++ = tolower(*p++);
493 else
494 p++;
495 }
496 *q = '\0';
497
498 /*
499 * Add an entry for this interface, with no addresses.
500 */
501 p[strlen(p) - 1] = '\0'; /* get rid of \n */
502 if (pcap_add_if(devlistp, name, 0, strdup(p + 2), errbuf) == -1) {
503 /*
504 * Failure.
505 */
506 ret = -1;
507 break;
508 }
509 }
510 if (ret != -1) {
511 /*
512 * Well, we didn't fail for any other reason; did we
513 * fail due to an error reading the file?
514 */
515 if (ferror(proc_dag_f)) {
516 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
517 "Error reading /proc/dag: %s",
518 pcap_strerror(errno));
519 ret = -1;
520 }
521 }
522
523 (void)fclose(proc_dag_f);
524 return (ret);
525 }
526
527 /*
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
530 * with DAG cards.
531 */
532 static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
533 if (!p)
534 return -1;
535 if (!fp) {
536 strncpy(p->errbuf, "setfilter: No filter specified",
537 sizeof(p->errbuf));
538 return -1;
539 }
540
541 /* Make our private copy of the filter */
542
543 if (install_bpf_program(p, fp) < 0) {
544 snprintf(p->errbuf, sizeof(p->errbuf),
545 "malloc: %s", pcap_strerror(errno));
546 return -1;
547 }
548
549 p->md.use_bpf = 0;
550
551 return (0);
552 }
553
554 static int
555 dag_set_datalink(pcap_t *p, int dlt)
556 {
557 return (0);
558 }
559
560 static int
561 dag_get_datalink(pcap_t *p)
562 {
563 int linktype = -1;
564
565 /* Check the type through a dagapi call.
566 */
567 switch(dag_linktype(p->fd)) {
568 case TYPE_HDLC_POS: {
569 dag_record_t *record;
570
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);
574 }
575 record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
576
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);
580 } else {
581 linktype = DLT_CHDLC;
582 fprintf(stderr, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype);
583 }
584 break;
585 }
586 case TYPE_ETH:
587 linktype = DLT_EN10MB;
588 fprintf(stderr, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype);
589 break;
590 case TYPE_ATM:
591 linktype = DLT_ATM_RFC1483;
592 fprintf(stderr, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype);
593 break;
594 case TYPE_LEGACY:
595 linktype = DLT_NULL;
596 fprintf(stderr, "Set DAG linktype to %d (DLT_NULL)\n", linktype);
597 break;
598 default:
599 fprintf(stderr, "Unknown DAG linktype %d\n", dag_linktype(p->fd));
600 break;
601 }
602
603 return linktype;
604 }