]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
ae9c7abd7027acf6dc28b2c618b97d614f84bbe3
[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 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
16 */
17
18 #ifndef lint
19 static const char rcsid[] _U_ =
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.27 2007-01-29 20:08:06 guy Exp $ (LBL)";
21 #endif
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <sys/param.h> /* optionally get BSD define */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "pcap-int.h"
34
35 #include <ctype.h>
36 #include <netinet/in.h>
37 #include <sys/mman.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41
42 struct mbuf; /* Squelch compiler warnings on some platforms for */
43 struct rtentry; /* declarations in <net/if.h> */
44 #include <net/if.h>
45
46 #include "dagnew.h"
47 #include "dagapi.h"
48
49 #define ATM_CELL_SIZE 52
50 #define ATM_HDR_SIZE 4
51
52 /* SunATM pseudo header */
53 struct sunatm_hdr {
54 unsigned char flags; /* destination and traffic type */
55 unsigned char vpi; /* VPI */
56 unsigned short vci; /* VCI */
57 };
58
59 typedef struct pcap_dag_node {
60 struct pcap_dag_node *next;
61 pcap_t *p;
62 pid_t pid;
63 } pcap_dag_node_t;
64
65 static pcap_dag_node_t *pcap_dags = NULL;
66 static int atexit_handler_installed = 0;
67 static const unsigned short endian_test_word = 0x0100;
68
69 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
70
71
72 #ifdef DAG_ONLY
73 /* This code is required when compiling for a DAG device only. */
74 #include "pcap-dag.h"
75
76 /* Replace dag function names with pcap equivalent. */
77 #define dag_open_live pcap_open_live
78 #define dag_platform_finddevs pcap_platform_finddevs
79 #endif /* DAG_ONLY */
80
81 static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
82 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
83 static int dag_set_datalink(pcap_t *p, int dlt);
84 static int dag_get_datalink(pcap_t *p);
85 static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
86
87 static void
88 delete_pcap_dag(pcap_t *p)
89 {
90 pcap_dag_node_t *curr = NULL, *prev = NULL;
91
92 for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
93 /* empty */
94 }
95
96 if (curr != NULL && curr->p == p) {
97 if (prev != NULL) {
98 prev->next = curr->next;
99 } else {
100 pcap_dags = curr->next;
101 }
102 }
103 }
104
105 /*
106 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
107 * in the pcap_t structure, and closes the file descriptor for the DAG card.
108 */
109
110 static void
111 dag_platform_close(pcap_t *p)
112 {
113
114 if (p != NULL) {
115 #ifdef HAVE_DAG_STREAMS_API
116 if(dag_stop_stream(p->fd, p->md.dag_stream) < 0)
117 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
118
119 if(dag_detach_stream(p->fd, p->md.dag_stream) < 0)
120 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
121 #else
122 if(dag_stop(p->fd) < 0)
123 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
124 #endif /* HAVE_DAG_STREAMS_API */
125 if(dag_close(p->fd) < 0)
126 fprintf(stderr,"dag_close: %s\n", strerror(errno));
127 #ifdef linux
128 free(p->md.device);
129 #endif
130 }
131 delete_pcap_dag(p);
132 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
133 }
134
135 static void
136 atexit_handler(void)
137 {
138 while (pcap_dags != NULL) {
139 if (pcap_dags->pid == getpid()) {
140 dag_platform_close(pcap_dags->p);
141 } else {
142 delete_pcap_dag(pcap_dags->p);
143 }
144 }
145 }
146
147 static int
148 new_pcap_dag(pcap_t *p)
149 {
150 pcap_dag_node_t *node = NULL;
151
152 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
153 return -1;
154 }
155
156 if (!atexit_handler_installed) {
157 atexit(atexit_handler);
158 atexit_handler_installed = 1;
159 }
160
161 node->next = pcap_dags;
162 node->p = p;
163 node->pid = getpid();
164
165 pcap_dags = node;
166
167 return 0;
168 }
169
170 /*
171 * Read at most max_packets from the capture stream and call the callback
172 * for each of them. Returns the number of packets handled, -1 if an
173 * error occured, or -2 if we were told to break out of the loop.
174 */
175 static int
176 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
177 {
178 unsigned int processed = 0;
179 int flags = p->md.dag_offset_flags;
180 unsigned int nonblocking = flags & DAGF_NONBLOCK;
181
182 /* Get the next bufferful of packets (if necessary). */
183 while (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) {
184
185 /*
186 * Has "pcap_breakloop()" been called?
187 */
188 if (p->break_loop) {
189 /*
190 * Yes - clear the flag that indicates that
191 * it has, and return -2 to indicate that
192 * we were told to break out of the loop.
193 */
194 p->break_loop = 0;
195 return -2;
196 }
197
198 #ifdef HAVE_DAG_STREAMS_API
199 /* dag_advance_stream() will block (unless nonblock is called)
200 * until 64kB of data has accumulated.
201 * If to_ms is set, it will timeout before 64kB has accumulated.
202 * We wait for 64kB because processing a few packets at a time
203 * can cause problems at high packet rates (>200kpps) due
204 * to inefficiencies.
205 * This does mean if to_ms is not specified the capture may 'hang'
206 * for long periods if the data rate is extremely slow (<64kB/sec)
207 * If non-block is specified it will return immediately. The user
208 * is then responsible for efficiency.
209 */
210 p->md.dag_mem_top = dag_advance_stream(p->fd, p->md.dag_stream, &(p->md.dag_mem_bottom));
211 #else
212 /* dag_offset does not support timeouts */
213 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
214 #endif /* HAVE_DAG_STREAMS_API */
215
216 if (nonblocking && (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
217 {
218 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
219 return 0;
220 }
221
222 if(!nonblocking &&
223 p->md.dag_timeout &&
224 (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
225 {
226 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
227 return 0;
228 }
229
230 }
231
232 /* Process the packets. */
233 while (p->md.dag_mem_top - p->md.dag_mem_bottom >= dag_record_size) {
234
235 unsigned short packet_len = 0;
236 int caplen = 0;
237 struct pcap_pkthdr pcap_header;
238
239 #ifdef HAVE_DAG_STREAMS_API
240 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_bottom);
241 #else
242 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
243 #endif /* HAVE_DAG_STREAMS_API */
244
245 u_char *dp = ((u_char *)header) + dag_record_size;
246 unsigned short rlen;
247
248 /*
249 * Has "pcap_breakloop()" been called?
250 */
251 if (p->break_loop) {
252 /*
253 * Yes - clear the flag that indicates that
254 * it has, and return -2 to indicate that
255 * we were told to break out of the loop.
256 */
257 p->break_loop = 0;
258 return -2;
259 }
260
261 rlen = ntohs(header->rlen);
262 if (rlen < dag_record_size)
263 {
264 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
265 return -1;
266 }
267 p->md.dag_mem_bottom += rlen;
268
269 switch(header->type) {
270 case TYPE_ATM:
271 #ifdef TYPE_AAL5
272 case TYPE_AAL5:
273 if (header->type == TYPE_AAL5) {
274 packet_len = ntohs(header->wlen);
275 caplen = rlen - dag_record_size;
276 }
277 #endif
278 #ifdef TYPE_MC_ATM
279 case TYPE_MC_ATM:
280 if (header->type == TYPE_MC_ATM) {
281 caplen = packet_len = ATM_CELL_SIZE;
282 dp+=4;
283 }
284 #endif
285 #ifdef TYPE_MC_AAL5
286 case TYPE_MC_AAL5:
287 if (header->type == TYPE_MC_AAL5) {
288 packet_len = ntohs(header->wlen);
289 caplen = rlen - dag_record_size - 4;
290 dp+=4;
291 }
292 #endif
293 if (header->type == TYPE_ATM) {
294 caplen = packet_len = ATM_CELL_SIZE;
295 }
296 if (p->linktype == DLT_SUNATM) {
297 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
298 unsigned long rawatm;
299
300 rawatm = ntohl(*((unsigned long *)dp));
301 sunatm->vci = htons((rawatm >> 4) & 0xffff);
302 sunatm->vpi = (rawatm >> 20) & 0x00ff;
303 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
304 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
305 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
306 ((dp[ATM_HDR_SIZE] == 0xaa &&
307 dp[ATM_HDR_SIZE+1] == 0xaa &&
308 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
309
310 } else {
311 packet_len -= ATM_HDR_SIZE;
312 caplen -= ATM_HDR_SIZE;
313 dp += ATM_HDR_SIZE;
314 }
315 break;
316
317 #ifdef TYPE_DSM_COLOR_ETH
318 case TYPE_DSM_COLOR_ETH:
319 #endif
320 #ifdef TYPE_COLOR_ETH
321 case TYPE_COLOR_ETH:
322 #endif
323 case TYPE_ETH:
324 packet_len = ntohs(header->wlen);
325 packet_len -= (p->md.dag_fcs_bits >> 3);
326 caplen = rlen - dag_record_size - 2;
327 if (caplen > packet_len) {
328 caplen = packet_len;
329 }
330 dp += 2;
331 break;
332 #ifdef TYPE_DSM_COLOR_HDLC_POS
333 case TYPE_DSM_COLOR_HDLC_POS:
334 #endif
335 #ifdef TYPE_COLOR_HDLC_POS
336 case TYPE_COLOR_HDLC_POS:
337 #endif
338 case TYPE_HDLC_POS:
339 packet_len = ntohs(header->wlen);
340 packet_len -= (p->md.dag_fcs_bits >> 3);
341 caplen = rlen - dag_record_size;
342 if (caplen > packet_len) {
343 caplen = packet_len;
344 }
345 break;
346 #ifdef TYPE_MC_HDLC
347 case TYPE_MC_HDLC:
348 packet_len = ntohs(header->wlen);
349 packet_len -= (p->md.dag_fcs_bits >> 3);
350 caplen = rlen - dag_record_size - 4;
351 if (caplen > packet_len) {
352 caplen = packet_len;
353 }
354 dp += 4;
355 break;
356 #endif
357 default:
358 /* Unhandled ERF type.
359 * Ignore rather than generating error
360 */
361 continue;
362 }
363
364 if (caplen > p->snapshot)
365 caplen = p->snapshot;
366
367 /* Count lost packets. */
368 switch(header->type) {
369 #ifdef TYPE_COLOR_HDLC_POS
370 /* in this type the color value overwrites the lctr */
371 case TYPE_COLOR_HDLC_POS:
372 break;
373 #endif
374 #ifdef TYPE_COLOR_ETH
375 /* in this type the color value overwrites the lctr */
376 case TYPE_COLOR_ETH:
377 break;
378 #endif
379 #ifdef TYPE_DSM_COLOR_HDLC_POS
380 /* in this type the color value overwrites the lctr */
381 case TYPE_DSM_COLOR_HDLC_POS:
382 break;
383 #endif
384 #ifdef TYPE_DSM_COLOR_ETH
385 /* in this type the color value overwrites the lctr */
386 case TYPE_DSM_COLOR_ETH:
387 break;
388 #endif
389
390 default:
391 if (header->lctr) {
392 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
393 p->md.stat.ps_drop = UINT_MAX;
394 } else {
395 p->md.stat.ps_drop += ntohs(header->lctr);
396 }
397 }
398 }
399
400 /* Run the packet filter if there is one. */
401 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
402
403 /* convert between timestamp formats */
404 register unsigned long long ts;
405
406 if (IS_BIGENDIAN()) {
407 ts = SWAPLL(header->ts);
408 } else {
409 ts = header->ts;
410 }
411
412 pcap_header.ts.tv_sec = ts >> 32;
413 ts = (ts & 0xffffffffULL) * 1000000;
414 ts += 0x80000000; /* rounding */
415 pcap_header.ts.tv_usec = ts >> 32;
416 if (pcap_header.ts.tv_usec >= 1000000) {
417 pcap_header.ts.tv_usec -= 1000000;
418 pcap_header.ts.tv_sec++;
419 }
420
421 /* Fill in our own header data */
422 pcap_header.caplen = caplen;
423 pcap_header.len = packet_len;
424
425 /* Count the packet. */
426 p->md.stat.ps_recv++;
427
428 /* Call the user supplied callback function */
429 callback(user, &pcap_header, dp);
430
431 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
432 processed++;
433 if (processed == cnt)
434 {
435 /* Reached the user-specified limit. */
436 return cnt;
437 }
438 }
439 }
440
441 return processed;
442 }
443
444 static int
445 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
446 {
447 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
448 PCAP_ERRBUF_SIZE);
449 return (-1);
450 }
451
452 /*
453 * Get a handle for a live capture from the given DAG device. Passing a NULL
454 * device will result in a failure. The promisc flag is ignored because DAG
455 * cards are always promiscuous. The to_ms parameter is also ignored as it is
456 * not supported in hardware.
457 *
458 * snaplen is now also ignored, until we get per-stream slen support. Set
459 * slen with approprite DAG tool BEFORE pcap_open_live().
460 *
461 * See also pcap(3).
462 */
463 pcap_t *
464 dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
465 {
466 char conf[30]; /* dag configure string */
467 pcap_t *handle;
468 char *s;
469 int n;
470 daginf_t* daginf;
471 char * newDev;
472 #ifdef HAVE_DAG_STREAMS_API
473 uint32_t mindata;
474 struct timeval maxwait;
475 struct timeval poll;
476 #endif
477
478 if (device == NULL) {
479 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
480 return NULL;
481 }
482 /* Allocate a handle for this session. */
483
484 handle = malloc(sizeof(*handle));
485 if (handle == NULL) {
486 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
487 return NULL;
488 }
489
490 /* Initialize some components of the pcap structure. */
491
492 memset(handle, 0, sizeof(*handle));
493
494 newDev = (char *)malloc(strlen(device) + 16);
495
496 #ifdef HAVE_DAG_STREAMS_API
497
498 /* Parse input name to get dag device and stream number if provided */
499 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
500 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
501 goto fail;
502 }
503 device = newDev;
504
505 if (handle->md.dag_stream%2) {
506 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
507 goto fail;
508 }
509 #else
510 if (strstr(device, "/dev") == NULL) {
511 newDev[0] = '\0';
512 strcat(newDev, "/dev/");
513 strcat(newDev,device);
514 device = newDev;
515 } else {
516 device = strdup(device);
517 }
518
519 if (device == NULL) {
520 snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup: %s\n", pcap_strerror(errno));
521 goto fail;
522 }
523 #endif /* HAVE_DAG_STREAMS_API */
524
525 /* setup device parameters */
526 if((handle->fd = dag_open((char *)device)) < 0) {
527 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
528 goto fail;
529 }
530
531 #ifdef HAVE_DAG_STREAMS_API
532 /* Open requested stream. Can fail if already locked or on error */
533 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
534 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
535 goto fail;
536 }
537
538 /* Set up default poll parameters for stream
539 * Can be overridden by pcap_set_nonblock()
540 */
541 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
542 &mindata, &maxwait, &poll) < 0) {
543 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
544 goto fail;
545 }
546
547 /* Amount of data to collect in Bytes before calling callbacks.
548 * Important for efficiency, but can introduce latency
549 * at low packet rates if to_ms not set!
550 */
551 mindata = 65536;
552
553 /* Obey to_ms if supplied. This is a good idea!
554 * Recommend 10-100ms. Calls will time out even if no data arrived.
555 */
556 maxwait.tv_sec = to_ms/1000;
557 maxwait.tv_usec = (to_ms%1000) * 1000;
558
559 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
560 mindata, &maxwait, &poll) < 0) {
561 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
562 goto fail;
563 }
564
565 #else
566 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
567 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
568 goto fail;
569 }
570
571 #endif /* HAVE_DAG_STREAMS_API */
572
573 /* XXX Not calling dag_configure() to set slen; this is unsafe in
574 * multi-stream environments as the gpp config is global.
575 * Once the firmware provides 'per-stream slen' this can be supported
576 * again via the Config API without side-effects */
577 #if 0
578 /* set the card snap length to the specified snaplen parameter */
579 /* This is a really bad idea, as different cards have different
580 * valid slen ranges. Should fix in Config API. */
581 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
582 snaplen = MAX_DAG_SNAPLEN;
583 } else if (snaplen < MIN_DAG_SNAPLEN) {
584 snaplen = MIN_DAG_SNAPLEN;
585 }
586 /* snap len has to be a multiple of 4 */
587 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
588
589 if(dag_configure(handle->fd, conf) < 0) {
590 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
591 goto fail;
592 }
593 #endif
594
595 #ifdef HAVE_DAG_STREAMS_API
596 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
597 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
598 goto fail;
599 }
600 #else
601 if(dag_start(handle->fd) < 0) {
602 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
603 goto fail;
604 }
605 #endif /* HAVE_DAG_STREAMS_API */
606
607 /*
608 * Important! You have to ensure bottom is properly
609 * initialized to zero on startup, it won't give you
610 * a compiler warning if you make this mistake!
611 */
612 handle->md.dag_mem_bottom = 0;
613 handle->md.dag_mem_top = 0;
614 handle->md.dag_fcs_bits = 32;
615
616 /* Query the card first for special cases. */
617 daginf = dag_info(handle->fd);
618 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code))
619 {
620 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
621 handle->md.dag_fcs_bits = 0;
622 }
623
624 /* Then allow an environment variable to override. */
625 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
626 if ((n = atoi(s)) == 0 || n == 16|| n == 32) {
627 handle->md.dag_fcs_bits = n;
628 } else {
629 snprintf(ebuf, PCAP_ERRBUF_SIZE,
630 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
631 goto fail;
632 }
633 }
634
635 handle->snapshot = snaplen;
636 handle->md.dag_timeout = to_ms;
637
638 handle->linktype = -1;
639 if (dag_get_datalink(handle) < 0) {
640 strcpy(ebuf, handle->errbuf);
641 goto fail;
642 }
643
644 handle->bufsize = 0;
645
646 if (new_pcap_dag(handle) < 0) {
647 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
648 goto fail;
649 }
650
651 /*
652 * "select()" and "poll()" don't work on DAG device descriptors.
653 */
654 handle->selectable_fd = -1;
655
656 #ifdef linux
657 handle->md.device = (char *)device;
658 handle->md.timeout = to_ms;
659 #else
660 free((char *)device);
661 device = NULL;
662 #endif
663
664 handle->read_op = dag_read;
665 handle->inject_op = dag_inject;
666 handle->setfilter_op = dag_setfilter;
667 handle->setdirection_op = NULL; /* Not implemented.*/
668 handle->set_datalink_op = dag_set_datalink;
669 handle->getnonblock_op = pcap_getnonblock_fd;
670 handle->setnonblock_op = dag_setnonblock;
671 handle->stats_op = dag_stats;
672 handle->close_op = dag_platform_close;
673 handle->md.stat.ps_drop = 0;
674 handle->md.stat.ps_recv = 0;
675 return handle;
676
677 fail:
678 if (newDev != NULL) {
679 free((char *)newDev);
680 }
681 if (handle != NULL) {
682 /*
683 * Get rid of any link-layer type list we allocated.
684 */
685 if (handle->dlt_list != NULL) {
686 free(handle->dlt_list);
687 }
688 free(handle);
689 }
690
691 return NULL;
692 }
693
694 static int
695 dag_stats(pcap_t *p, struct pcap_stat *ps) {
696 /* This needs to be filled out correctly. Hopefully a dagapi call will
697 provide all necessary information.
698 */
699 /*p->md.stat.ps_recv = 0;*/
700 /*p->md.stat.ps_drop = 0;*/
701
702 *ps = p->md.stat;
703
704 return 0;
705 }
706
707 /*
708 * Simply submit all possible dag names as candidates.
709 * pcap_add_if() internally tests each candidate with pcap_open_live(),
710 * so any non-existent devices are dropped.
711 * For 2.5 try all rx stream names as well.
712 */
713 int
714 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
715 {
716 char name[12]; /* XXX - pick a size */
717 int ret = 0;
718 int c;
719
720 /* Try all the DAGs 0-9 */
721 for (c = 0; c < 9; c++) {
722 snprintf(name, 12, "dag%d", c);
723 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
724 /*
725 * Failure.
726 */
727 ret = -1;
728 }
729 #ifdef HAVE_DAG_STREAMS_API
730 {
731 int stream;
732 for(stream=0;stream<16;stream+=2) {
733 snprintf(name, 10, "dag%d:%d", c, stream);
734 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
735 /*
736 * Failure.
737 */
738 ret = -1;
739 }
740 }
741 }
742 #endif /* HAVE_DAG_STREAMS_API */
743 }
744 return (ret);
745 }
746
747 /*
748 * Installs the given bpf filter program in the given pcap structure. There is
749 * no attempt to store the filter in kernel memory as that is not supported
750 * with DAG cards.
751 */
752 static int
753 dag_setfilter(pcap_t *p, struct bpf_program *fp)
754 {
755 if (!p)
756 return -1;
757 if (!fp) {
758 strncpy(p->errbuf, "setfilter: No filter specified",
759 sizeof(p->errbuf));
760 return -1;
761 }
762
763 /* Make our private copy of the filter */
764
765 if (install_bpf_program(p, fp) < 0)
766 return -1;
767
768 p->md.use_bpf = 0;
769
770 return (0);
771 }
772
773 static int
774 dag_set_datalink(pcap_t *p, int dlt)
775 {
776 p->linktype = dlt;
777
778 return (0);
779 }
780
781 static int
782 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
783 {
784 /*
785 * Set non-blocking mode on the FD.
786 * XXX - is that necessary? If not, don't bother calling it,
787 * and have a "dag_getnonblock()" function that looks at
788 * "p->md.dag_offset_flags".
789 */
790 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
791 return (-1);
792 #ifdef HAVE_DAG_STREAMS_API
793 {
794 uint32_t mindata;
795 struct timeval maxwait;
796 struct timeval poll;
797
798 if (dag_get_stream_poll(p->fd, p->md.dag_stream,
799 &mindata, &maxwait, &poll) < 0) {
800 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
801 return -1;
802 }
803
804 /* Amount of data to collect in Bytes before calling callbacks.
805 * Important for efficiency, but can introduce latency
806 * at low packet rates if to_ms not set!
807 */
808 if(nonblock)
809 mindata = 0;
810 else
811 mindata = 65536;
812
813 if (dag_set_stream_poll(p->fd, p->md.dag_stream,
814 mindata, &maxwait, &poll) < 0) {
815 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
816 return -1;
817 }
818 }
819 #endif /* HAVE_DAG_STREAMS_API */
820 if (nonblock) {
821 p->md.dag_offset_flags |= DAGF_NONBLOCK;
822 } else {
823 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
824 }
825 return (0);
826 }
827
828 static int
829 dag_get_datalink(pcap_t *p)
830 {
831 int index=0;
832 uint8_t types[255];
833
834 memset(types, 0, 255);
835
836 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
837 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
838 return (-1);
839 }
840
841 p->linktype = 0;
842
843 #ifdef HAVE_DAG_GET_ERF_TYPES
844 /* Get list of possible ERF types for this card */
845 if (dag_get_erf_types(p->fd, types, 255) < 0) {
846 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
847 return (-1);
848 }
849
850 while (types[index]) {
851 #else
852 /* Check the type through a dagapi call. */
853 types[index] = dag_linktype(p->fd);
854
855 {
856 #endif
857 switch(types[index]) {
858
859 case TYPE_HDLC_POS:
860 #ifdef TYPE_COLOR_HDLC_POS
861 case TYPE_COLOR_HDLC_POS:
862 #endif
863 #ifdef TYPE_DSM_COLOR_HDLC_POS
864 case TYPE_DSM_COLOR_HDLC_POS:
865 #endif
866 if (p->dlt_list != NULL) {
867 p->dlt_list[index++] = DLT_CHDLC;
868 p->dlt_list[index++] = DLT_PPP_SERIAL;
869 p->dlt_list[index++] = DLT_FRELAY;
870 }
871 if(!p->linktype)
872 p->linktype = DLT_CHDLC;
873 break;
874
875 case TYPE_ETH:
876 #ifdef TYPE_COLOR_ETH
877 case TYPE_COLOR_ETH:
878 #endif
879 #ifdef TYPE_DSM_COLOR_ETH
880 case TYPE_DSM_COLOR_ETH:
881 #endif
882 /*
883 * This is (presumably) a real Ethernet capture; give it a
884 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
885 * that an application can let you choose it, in case you're
886 * capturing DOCSIS traffic that a Cisco Cable Modem
887 * Termination System is putting out onto an Ethernet (it
888 * doesn't put an Ethernet header onto the wire, it puts raw
889 * DOCSIS frames out on the wire inside the low-level
890 * Ethernet framing).
891 */
892 if (p->dlt_list != NULL) {
893 p->dlt_list[index++] = DLT_EN10MB;
894 p->dlt_list[index++] = DLT_DOCSIS;
895 }
896 if(!p->linktype)
897 p->linktype = DLT_EN10MB;
898 break;
899
900 case TYPE_ATM:
901 #ifdef TYPE_AAL5
902 case TYPE_AAL5:
903 #endif
904 #ifdef TYPE_MC_ATM
905 case TYPE_MC_ATM:
906 #endif
907 #ifdef TYPE_MC_AAL5
908 case TYPE_MC_AAL5:
909 #endif
910 if (p->dlt_list != NULL) {
911 p->dlt_list[index++] = DLT_ATM_RFC1483;
912 p->dlt_list[index++] = DLT_SUNATM;
913 }
914 if(!p->linktype)
915 p->linktype = DLT_ATM_RFC1483;
916 break;
917
918 #ifdef TYPE_MC_HDLC
919 case TYPE_MC_HDLC:
920 if (p->dlt_list != NULL) {
921 p->dlt_list[index++] = DLT_CHDLC;
922 p->dlt_list[index++] = DLT_PPP_SERIAL;
923 p->dlt_list[index++] = DLT_FRELAY;
924 p->dlt_list[index++] = DLT_MTP2;
925 }
926 if(!p->linktype)
927 p->linktype = DLT_CHDLC;
928 break;
929 #endif
930
931 case TYPE_LEGACY:
932 if(!p->linktype)
933 p->linktype = DLT_NULL;
934 break;
935
936 default:
937 snprintf(p->errbuf, sizeof(p->errbuf), "unknown DAG linktype %d", types[index]);
938 return (-1);
939
940 } /* switch */
941 }
942
943 p->dlt_count = index;
944
945 return p->linktype;
946 }