]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
Add an API to set "immediate mode".
[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.39 2008-04-14 20:40:58 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 #include "pcap-dag.h"
50
51 /*
52 * DAG devices have names beginning with "dag", followed by a number
53 * from 0 to MAXDAG.
54 */
55 #define MAXDAG 31
56
57 #define ATM_CELL_SIZE 52
58 #define ATM_HDR_SIZE 4
59
60 /*
61 * A header containing additional MTP information.
62 */
63 #define MTP2_SENT_OFFSET 0 /* 1 byte */
64 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
65 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
66 #define MTP2_HDR_LEN 4 /* length of the header */
67
68 #define MTP2_ANNEX_A_NOT_USED 0
69 #define MTP2_ANNEX_A_USED 1
70 #define MTP2_ANNEX_A_USED_UNKNOWN 2
71
72 /* SunATM pseudo header */
73 struct sunatm_hdr {
74 unsigned char flags; /* destination and traffic type */
75 unsigned char vpi; /* VPI */
76 unsigned short vci; /* VCI */
77 };
78
79 /*
80 * Private data for capturing on DAG devices.
81 */
82 struct pcap_dag {
83 struct pcap_stat stat;
84 #ifdef HAVE_DAG_STREAMS_API
85 u_char *dag_mem_bottom; /* DAG card current memory bottom pointer */
86 u_char *dag_mem_top; /* DAG card current memory top pointer */
87 #else /* HAVE_DAG_STREAMS_API */
88 void *dag_mem_base; /* DAG card memory base address */
89 u_int dag_mem_bottom; /* DAG card current memory bottom offset */
90 u_int dag_mem_top; /* DAG card current memory top offset */
91 #endif /* HAVE_DAG_STREAMS_API */
92 int dag_fcs_bits; /* Number of checksum bits from link layer */
93 int dag_offset_flags; /* Flags to pass to dag_offset(). */
94 int dag_stream; /* DAG stream number */
95 int dag_timeout; /* timeout specified to pcap_open_live.
96 * Same as in linux above, introduce
97 * generally? */
98 };
99
100 typedef struct pcap_dag_node {
101 struct pcap_dag_node *next;
102 pcap_t *p;
103 pid_t pid;
104 } pcap_dag_node_t;
105
106 static pcap_dag_node_t *pcap_dags = NULL;
107 static int atexit_handler_installed = 0;
108 static const unsigned short endian_test_word = 0x0100;
109
110 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
111
112 #define MAX_DAG_PACKET 65536
113
114 static unsigned char TempPkt[MAX_DAG_PACKET];
115
116 static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
117 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
118 static int dag_set_datalink(pcap_t *p, int dlt);
119 static int dag_get_datalink(pcap_t *p);
120 static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
121
122 static void
123 delete_pcap_dag(pcap_t *p)
124 {
125 pcap_dag_node_t *curr = NULL, *prev = NULL;
126
127 for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
128 /* empty */
129 }
130
131 if (curr != NULL && curr->p == p) {
132 if (prev != NULL) {
133 prev->next = curr->next;
134 } else {
135 pcap_dags = curr->next;
136 }
137 }
138 }
139
140 /*
141 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
142 * in the pcap_t structure, and closes the file descriptor for the DAG card.
143 */
144
145 static void
146 dag_platform_cleanup(pcap_t *p)
147 {
148 struct pcap_dag *pd;
149
150 if (p != NULL) {
151 pd = p->private;
152 #ifdef HAVE_DAG_STREAMS_API
153 if(dag_stop_stream(p->fd, pd->dag_stream) < 0)
154 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
155
156 if(dag_detach_stream(p->fd, pd->dag_stream) < 0)
157 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
158 #else
159 if(dag_stop(p->fd) < 0)
160 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
161 #endif /* HAVE_DAG_STREAMS_API */
162 if(p->fd != -1) {
163 if(dag_close(p->fd) < 0)
164 fprintf(stderr,"dag_close: %s\n", strerror(errno));
165 p->fd = -1;
166 }
167 delete_pcap_dag(p);
168 pcap_cleanup_live_common(p);
169 }
170 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
171 }
172
173 static void
174 atexit_handler(void)
175 {
176 while (pcap_dags != NULL) {
177 if (pcap_dags->pid == getpid()) {
178 dag_platform_cleanup(pcap_dags->p);
179 } else {
180 delete_pcap_dag(pcap_dags->p);
181 }
182 }
183 }
184
185 static int
186 new_pcap_dag(pcap_t *p)
187 {
188 pcap_dag_node_t *node = NULL;
189
190 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
191 return -1;
192 }
193
194 if (!atexit_handler_installed) {
195 atexit(atexit_handler);
196 atexit_handler_installed = 1;
197 }
198
199 node->next = pcap_dags;
200 node->p = p;
201 node->pid = getpid();
202
203 pcap_dags = node;
204
205 return 0;
206 }
207
208 static unsigned int
209 dag_erf_ext_header_count(uint8_t * erf, size_t len)
210 {
211 uint32_t hdr_num = 0;
212 uint8_t hdr_type;
213
214 /* basic sanity checks */
215 if ( erf == NULL )
216 return 0;
217 if ( len < 16 )
218 return 0;
219
220 /* check if we have any extension headers */
221 if ( (erf[8] & 0x80) == 0x00 )
222 return 0;
223
224 /* loop over the extension headers */
225 do {
226
227 /* sanity check we have enough bytes */
228 if ( len < (24 + (hdr_num * 8)) )
229 return hdr_num;
230
231 /* get the header type */
232 hdr_type = erf[(16 + (hdr_num * 8))];
233 hdr_num++;
234
235 } while ( hdr_type & 0x80 );
236
237 return hdr_num;
238 }
239
240 /*
241 * Read at most max_packets from the capture stream and call the callback
242 * for each of them. Returns the number of packets handled, -1 if an
243 * error occured, or -2 if we were told to break out of the loop.
244 */
245 static int
246 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
247 {
248 struct pcap_dag *pd = p->private;
249 unsigned int processed = 0;
250 int flags = pd->dag_offset_flags;
251 unsigned int nonblocking = flags & DAGF_NONBLOCK;
252 unsigned int num_ext_hdr = 0;
253
254 /* Get the next bufferful of packets (if necessary). */
255 while (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size) {
256
257 /*
258 * Has "pcap_breakloop()" been called?
259 */
260 if (p->break_loop) {
261 /*
262 * Yes - clear the flag that indicates that
263 * it has, and return -2 to indicate that
264 * we were told to break out of the loop.
265 */
266 p->break_loop = 0;
267 return -2;
268 }
269
270 #ifdef HAVE_DAG_STREAMS_API
271 /* dag_advance_stream() will block (unless nonblock is called)
272 * until 64kB of data has accumulated.
273 * If to_ms is set, it will timeout before 64kB has accumulated.
274 * We wait for 64kB because processing a few packets at a time
275 * can cause problems at high packet rates (>200kpps) due
276 * to inefficiencies.
277 * This does mean if to_ms is not specified the capture may 'hang'
278 * for long periods if the data rate is extremely slow (<64kB/sec)
279 * If non-block is specified it will return immediately. The user
280 * is then responsible for efficiency.
281 */
282 if ( NULL == (pd->dag_mem_top = dag_advance_stream(p->fd, pd->dag_stream, &(pd->dag_mem_bottom))) ) {
283 return -1;
284 }
285 #else
286 /* dag_offset does not support timeouts */
287 pd->dag_mem_top = dag_offset(p->fd, &(pd->dag_mem_bottom), flags);
288 #endif /* HAVE_DAG_STREAMS_API */
289
290 if (nonblocking && (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
291 {
292 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
293 return 0;
294 }
295
296 if(!nonblocking &&
297 pd->dag_timeout &&
298 (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
299 {
300 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
301 return 0;
302 }
303
304 }
305
306 /* Process the packets. */
307 while (pd->dag_mem_top - pd->dag_mem_bottom >= dag_record_size) {
308
309 unsigned short packet_len = 0;
310 int caplen = 0;
311 struct pcap_pkthdr pcap_header;
312
313 #ifdef HAVE_DAG_STREAMS_API
314 dag_record_t *header = (dag_record_t *)(pd->dag_mem_bottom);
315 #else
316 dag_record_t *header = (dag_record_t *)(pd->dag_mem_base + pd->dag_mem_bottom);
317 #endif /* HAVE_DAG_STREAMS_API */
318
319 u_char *dp = ((u_char *)header); /* + dag_record_size; */
320 unsigned short rlen;
321
322 /*
323 * Has "pcap_breakloop()" been called?
324 */
325 if (p->break_loop) {
326 /*
327 * Yes - clear the flag that indicates that
328 * it has, and return -2 to indicate that
329 * we were told to break out of the loop.
330 */
331 p->break_loop = 0;
332 return -2;
333 }
334
335 rlen = ntohs(header->rlen);
336 if (rlen < dag_record_size)
337 {
338 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
339 return -1;
340 }
341 pd->dag_mem_bottom += rlen;
342
343 /* Count lost packets. */
344 switch((header->type & 0x7f)) {
345 /* in these types the color value overwrites the lctr */
346 case TYPE_COLOR_HDLC_POS:
347 case TYPE_COLOR_ETH:
348 case TYPE_DSM_COLOR_HDLC_POS:
349 case TYPE_DSM_COLOR_ETH:
350 case TYPE_COLOR_MC_HDLC_POS:
351 case TYPE_COLOR_HASH_ETH:
352 case TYPE_COLOR_HASH_POS:
353 break;
354
355 default:
356 if (header->lctr) {
357 if (pd->stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
358 pd->stat.ps_drop = UINT_MAX;
359 } else {
360 pd->stat.ps_drop += ntohs(header->lctr);
361 }
362 }
363 }
364
365 if ((header->type & 0x7f) == TYPE_PAD) {
366 continue;
367 }
368
369 num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
370
371 /* ERF encapsulation */
372 /* The Extensible Record Format is not dropped for this kind of encapsulation,
373 * and will be handled as a pseudo header by the decoding application.
374 * The information carried in the ERF header and in the optional subheader (if present)
375 * could be merged with the libpcap information, to offer a better decoding.
376 * The packet length is
377 * o the length of the packet on the link (header->wlen),
378 * o plus the length of the ERF header (dag_record_size), as the length of the
379 * pseudo header will be adjusted during the decoding,
380 * o plus the length of the optional subheader (if present).
381 *
382 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
383 * if the capture length is greater than the packet length.
384 */
385 if (p->linktype == DLT_ERF) {
386 packet_len = ntohs(header->wlen) + dag_record_size;
387 caplen = rlen;
388 switch ((header->type & 0x7f)) {
389 case TYPE_MC_AAL5:
390 case TYPE_MC_ATM:
391 case TYPE_MC_HDLC:
392 case TYPE_MC_RAW_CHANNEL:
393 case TYPE_MC_RAW:
394 case TYPE_MC_AAL2:
395 case TYPE_COLOR_MC_HDLC_POS:
396 packet_len += 4; /* MC header */
397 break;
398
399 case TYPE_COLOR_HASH_ETH:
400 case TYPE_DSM_COLOR_ETH:
401 case TYPE_COLOR_ETH:
402 case TYPE_ETH:
403 packet_len += 2; /* ETH header */
404 break;
405 } /* switch type */
406
407 /* Include ERF extension headers */
408 packet_len += (8 * num_ext_hdr);
409
410 if (caplen > packet_len) {
411 caplen = packet_len;
412 }
413 } else {
414 /* Other kind of encapsulation according to the header Type */
415
416 /* Skip over generic ERF header */
417 dp += dag_record_size;
418 /* Skip over extension headers */
419 dp += 8 * num_ext_hdr;
420
421 switch((header->type & 0x7f)) {
422 case TYPE_ATM:
423 case TYPE_AAL5:
424 if (header->type == TYPE_AAL5) {
425 packet_len = ntohs(header->wlen);
426 caplen = rlen - dag_record_size;
427 }
428 case TYPE_MC_ATM:
429 if (header->type == TYPE_MC_ATM) {
430 caplen = packet_len = ATM_CELL_SIZE;
431 dp+=4;
432 }
433 case TYPE_MC_AAL5:
434 if (header->type == TYPE_MC_AAL5) {
435 packet_len = ntohs(header->wlen);
436 caplen = rlen - dag_record_size - 4;
437 dp+=4;
438 }
439 if (header->type == TYPE_ATM) {
440 caplen = packet_len = ATM_CELL_SIZE;
441 }
442 if (p->linktype == DLT_SUNATM) {
443 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
444 unsigned long rawatm;
445
446 rawatm = ntohl(*((unsigned long *)dp));
447 sunatm->vci = htons((rawatm >> 4) & 0xffff);
448 sunatm->vpi = (rawatm >> 20) & 0x00ff;
449 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
450 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
451 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
452 ((dp[ATM_HDR_SIZE] == 0xaa &&
453 dp[ATM_HDR_SIZE+1] == 0xaa &&
454 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
455
456 } else {
457 packet_len -= ATM_HDR_SIZE;
458 caplen -= ATM_HDR_SIZE;
459 dp += ATM_HDR_SIZE;
460 }
461 break;
462
463 case TYPE_COLOR_HASH_ETH:
464 case TYPE_DSM_COLOR_ETH:
465 case TYPE_COLOR_ETH:
466 case TYPE_ETH:
467 packet_len = ntohs(header->wlen);
468 packet_len -= (pd->dag_fcs_bits >> 3);
469 caplen = rlen - dag_record_size - 2;
470 if (caplen > packet_len) {
471 caplen = packet_len;
472 }
473 dp += 2;
474 break;
475
476 case TYPE_COLOR_HASH_POS:
477 case TYPE_DSM_COLOR_HDLC_POS:
478 case TYPE_COLOR_HDLC_POS:
479 case TYPE_HDLC_POS:
480 packet_len = ntohs(header->wlen);
481 packet_len -= (pd->dag_fcs_bits >> 3);
482 caplen = rlen - dag_record_size;
483 if (caplen > packet_len) {
484 caplen = packet_len;
485 }
486 break;
487
488 case TYPE_COLOR_MC_HDLC_POS:
489 case TYPE_MC_HDLC:
490 packet_len = ntohs(header->wlen);
491 packet_len -= (pd->dag_fcs_bits >> 3);
492 caplen = rlen - dag_record_size - 4;
493 if (caplen > packet_len) {
494 caplen = packet_len;
495 }
496 /* jump the MC_HDLC_HEADER */
497 dp += 4;
498 #ifdef DLT_MTP2_WITH_PHDR
499 if (p->linktype == DLT_MTP2_WITH_PHDR) {
500 /* Add the MTP2 Pseudo Header */
501 caplen += MTP2_HDR_LEN;
502 packet_len += MTP2_HDR_LEN;
503
504 TempPkt[MTP2_SENT_OFFSET] = 0;
505 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
506 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
507 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
508 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
509 dp = TempPkt;
510 }
511 #endif
512 break;
513
514 case TYPE_IPV4:
515 case TYPE_IPV6:
516 packet_len = ntohs(header->wlen);
517 caplen = rlen - dag_record_size;
518 if (caplen > packet_len) {
519 caplen = packet_len;
520 }
521 break;
522
523 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
524 case TYPE_MC_RAW:
525 case TYPE_MC_RAW_CHANNEL:
526 case TYPE_IP_COUNTER:
527 case TYPE_TCP_FLOW_COUNTER:
528 case TYPE_INFINIBAND:
529 case TYPE_RAW_LINK:
530 case TYPE_INFINIBAND_LINK:
531 default:
532 /* Unhandled ERF type.
533 * Ignore rather than generating error
534 */
535 continue;
536 } /* switch type */
537
538 /* Skip over extension headers */
539 caplen -= (8 * num_ext_hdr);
540
541 } /* ERF encapsulation */
542
543 if (caplen > p->snapshot)
544 caplen = p->snapshot;
545
546 /* Run the packet filter if there is one. */
547 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
548
549 /* convert between timestamp formats */
550 register unsigned long long ts;
551
552 if (IS_BIGENDIAN()) {
553 ts = SWAPLL(header->ts);
554 } else {
555 ts = header->ts;
556 }
557
558 pcap_header.ts.tv_sec = ts >> 32;
559 ts = (ts & 0xffffffffULL) * 1000000;
560 ts += 0x80000000; /* rounding */
561 pcap_header.ts.tv_usec = ts >> 32;
562 if (pcap_header.ts.tv_usec >= 1000000) {
563 pcap_header.ts.tv_usec -= 1000000;
564 pcap_header.ts.tv_sec++;
565 }
566
567 /* Fill in our own header data */
568 pcap_header.caplen = caplen;
569 pcap_header.len = packet_len;
570
571 /* Count the packet. */
572 pd->stat.ps_recv++;
573
574 /* Call the user supplied callback function */
575 callback(user, &pcap_header, dp);
576
577 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
578 processed++;
579 if (processed == cnt && cnt > 0)
580 {
581 /* Reached the user-specified limit. */
582 return cnt;
583 }
584 }
585 }
586
587 return processed;
588 }
589
590 static int
591 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
592 {
593 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
594 PCAP_ERRBUF_SIZE);
595 return (-1);
596 }
597
598 /*
599 * Get a handle for a live capture from the given DAG device. Passing a NULL
600 * device will result in a failure. The promisc flag is ignored because DAG
601 * cards are always promiscuous. The to_ms parameter is used in setting the
602 * API polling parameters.
603 *
604 * snaplen is now also ignored, until we get per-stream slen support. Set
605 * slen with approprite DAG tool BEFORE pcap_activate().
606 *
607 * See also pcap(3).
608 */
609 static int dag_activate(pcap_t* handle)
610 {
611 struct pcap_dag *handlep = handle->private;
612 #if 0
613 char conf[30]; /* dag configure string */
614 #endif
615 char *s;
616 int n;
617 daginf_t* daginf;
618 char * newDev = NULL;
619 char * device = handle->opt.source;
620 #ifdef HAVE_DAG_STREAMS_API
621 uint32_t mindata;
622 struct timeval maxwait;
623 struct timeval poll;
624 #endif
625
626 if (device == NULL) {
627 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
628 return -1;
629 }
630
631 /* Initialize some components of the pcap structure. */
632
633 #ifdef HAVE_DAG_STREAMS_API
634 newDev = (char *)malloc(strlen(device) + 16);
635 if (newDev == NULL) {
636 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
637 goto fail;
638 }
639
640 /* Parse input name to get dag device and stream number if provided */
641 if (dag_parse_name(device, newDev, strlen(device) + 16, &handlep->dag_stream) < 0) {
642 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
643 goto fail;
644 }
645 device = newDev;
646
647 if (handlep->dag_stream%2) {
648 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
649 goto fail;
650 }
651 #else
652 if (strncmp(device, "/dev/", 5) != 0) {
653 newDev = (char *)malloc(strlen(device) + 5);
654 if (newDev == NULL) {
655 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
656 goto fail;
657 }
658 strcpy(newDev, "/dev/");
659 strcat(newDev, device);
660 device = newDev;
661 }
662 #endif /* HAVE_DAG_STREAMS_API */
663
664 /* setup device parameters */
665 if((handle->fd = dag_open((char *)device)) < 0) {
666 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
667 goto fail;
668 }
669
670 #ifdef HAVE_DAG_STREAMS_API
671 /* Open requested stream. Can fail if already locked or on error */
672 if (dag_attach_stream(handle->fd, handlep->dag_stream, 0, 0) < 0) {
673 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
674 goto failclose;
675 }
676
677 /* Set up default poll parameters for stream
678 * Can be overridden by pcap_set_nonblock()
679 */
680 if (dag_get_stream_poll(handle->fd, handlep->dag_stream,
681 &mindata, &maxwait, &poll) < 0) {
682 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
683 goto faildetach;
684 }
685
686 if (handle->opt.immediate) {
687 /* Call callback immediately.
688 * XXX - is this the right way to handle this?
689 */
690 mindata = 0;
691 } else {
692 /* Amount of data to collect in Bytes before calling callbacks.
693 * Important for efficiency, but can introduce latency
694 * at low packet rates if to_ms not set!
695 */
696 mindata = 65536;
697 }
698
699 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
700 * Recommend 10-100ms. Calls will time out even if no data arrived.
701 */
702 maxwait.tv_sec = handle->opt.timeout/1000;
703 maxwait.tv_usec = (handle->opt.timeout%1000) * 1000;
704
705 if (dag_set_stream_poll(handle->fd, handlep->dag_stream,
706 mindata, &maxwait, &poll) < 0) {
707 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
708 goto faildetach;
709 }
710
711 #else
712 if((handlep->dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
713 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
714 goto failclose;
715 }
716
717 #endif /* HAVE_DAG_STREAMS_API */
718
719 /* XXX Not calling dag_configure() to set slen; this is unsafe in
720 * multi-stream environments as the gpp config is global.
721 * Once the firmware provides 'per-stream slen' this can be supported
722 * again via the Config API without side-effects */
723 #if 0
724 /* set the card snap length to the specified snaplen parameter */
725 /* This is a really bad idea, as different cards have different
726 * valid slen ranges. Should fix in Config API. */
727 if (handle->snapshot == 0 || handle->snapshot > MAX_DAG_SNAPLEN) {
728 handle->snapshot = MAX_DAG_SNAPLEN;
729 } else if (snaplen < MIN_DAG_SNAPLEN) {
730 handle->snapshot = MIN_DAG_SNAPLEN;
731 }
732 /* snap len has to be a multiple of 4 */
733 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
734
735 if(dag_configure(handle->fd, conf) < 0) {
736 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
737 goto faildetach;
738 }
739 #endif
740
741 #ifdef HAVE_DAG_STREAMS_API
742 if(dag_start_stream(handle->fd, handlep->dag_stream) < 0) {
743 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
744 goto faildetach;
745 }
746 #else
747 if(dag_start(handle->fd) < 0) {
748 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
749 goto failclose;
750 }
751 #endif /* HAVE_DAG_STREAMS_API */
752
753 /*
754 * Important! You have to ensure bottom is properly
755 * initialized to zero on startup, it won't give you
756 * a compiler warning if you make this mistake!
757 */
758 handlep->dag_mem_bottom = 0;
759 handlep->dag_mem_top = 0;
760
761 /*
762 * Find out how many FCS bits we should strip.
763 * First, query the card to see if it strips the FCS.
764 */
765 daginf = dag_info(handle->fd);
766 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) {
767 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
768 handlep->dag_fcs_bits = 0;
769
770 /* Note that no FCS will be supplied. */
771 handle->linktype_ext = LT_FCS_DATALINK_EXT(0);
772 } else {
773 /*
774 * Start out assuming it's 32 bits.
775 */
776 handlep->dag_fcs_bits = 32;
777
778 /* Allow an environment variable to override. */
779 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
780 if ((n = atoi(s)) == 0 || n == 16 || n == 32) {
781 handlep->dag_fcs_bits = n;
782 } else {
783 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
784 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
785 goto failstop;
786 }
787 }
788
789 /*
790 * Did the user request that they not be stripped?
791 */
792 if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) {
793 /* Yes. Note the number of bytes that will be
794 supplied. */
795 handle->linktype_ext = LT_FCS_DATALINK_EXT(handlep->dag_fcs_bits/16);
796
797 /* And don't strip them. */
798 handlep->dag_fcs_bits = 0;
799 }
800 }
801
802 handlep->dag_timeout = handle->opt.timeout;
803
804 handle->linktype = -1;
805 if (dag_get_datalink(handle) < 0)
806 goto failstop;
807
808 handle->bufsize = 0;
809
810 if (new_pcap_dag(handle) < 0) {
811 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
812 goto failstop;
813 }
814
815 /*
816 * "select()" and "poll()" don't work on DAG device descriptors.
817 */
818 handle->selectable_fd = -1;
819
820 if (newDev != NULL) {
821 free((char *)newDev);
822 }
823
824 handle->read_op = dag_read;
825 handle->inject_op = dag_inject;
826 handle->setfilter_op = dag_setfilter;
827 handle->setdirection_op = NULL; /* Not implemented.*/
828 handle->set_datalink_op = dag_set_datalink;
829 handle->getnonblock_op = pcap_getnonblock_fd;
830 handle->setnonblock_op = dag_setnonblock;
831 handle->stats_op = dag_stats;
832 handle->cleanup_op = dag_platform_cleanup;
833 handlep->stat.ps_drop = 0;
834 handlep->stat.ps_recv = 0;
835 handlep->stat.ps_ifdrop = 0;
836 return 0;
837
838 #ifdef HAVE_DAG_STREAMS_API
839 failstop:
840 if (dag_stop_stream(handle->fd, handlep->dag_stream) < 0) {
841 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
842 }
843
844 faildetach:
845 if (dag_detach_stream(handle->fd, handlep->dag_stream) < 0)
846 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
847 #else
848 failstop:
849 if (dag_stop(handle->fd) < 0)
850 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
851 #endif /* HAVE_DAG_STREAMS_API */
852
853 failclose:
854 if (dag_close(handle->fd) < 0)
855 fprintf(stderr,"dag_close: %s\n", strerror(errno));
856 delete_pcap_dag(handle);
857
858 fail:
859 pcap_cleanup_live_common(handle);
860 if (newDev != NULL) {
861 free((char *)newDev);
862 }
863
864 return PCAP_ERROR;
865 }
866
867 pcap_t *dag_create(const char *device, char *ebuf, int *is_ours)
868 {
869 const char *cp;
870 char *cpend;
871 long devnum;
872 pcap_t *p;
873
874 /* Does this look like a DAG device? */
875 cp = strrchr(device, '/');
876 if (cp == NULL)
877 cp = device;
878 /* Does it begin with "dag"? */
879 if (strncmp(cp, "dag", 3) != 0) {
880 /* Nope, doesn't begin with "dag" */
881 *is_ours = 0;
882 return NULL;
883 }
884 /* Yes - is "dag" followed by a number from 0 to MAXDAG? */
885 cp += 3;
886 devnum = strtol(cp, &cpend, 10);
887 if (cpend == cp || *cpend != '\0') {
888 /* Not followed by a number. */
889 *is_ours = 0;
890 return NULL;
891 }
892 if (devnum < 0 || devnum > MAXDAG) {
893 /* Followed by a non-valid number. */
894 *is_ours = 0;
895 return NULL;
896 }
897
898 /* OK, it's probably ours. */
899 *is_ours = 1;
900
901 p = pcap_create_common(device, ebuf, sizeof (struct pcap_dag));
902 if (p == NULL)
903 return NULL;
904
905 p->activate_op = dag_activate;
906 return p;
907 }
908
909 static int
910 dag_stats(pcap_t *p, struct pcap_stat *ps) {
911 struct pcap_dag *pd = p->private;
912
913 /* This needs to be filled out correctly. Hopefully a dagapi call will
914 provide all necessary information.
915 */
916 /*pd->stat.ps_recv = 0;*/
917 /*pd->stat.ps_drop = 0;*/
918
919 *ps = pd->stat;
920
921 return 0;
922 }
923
924 /*
925 * Previously we just generated a list of all possible names and let
926 * pcap_add_if() attempt to open each one, but with streams this adds up
927 * to 81 possibilities which is inefficient.
928 *
929 * Since we know more about the devices we can prune the tree here.
930 * pcap_add_if() will still retest each device but the total number of
931 * open attempts will still be much less than the naive approach.
932 */
933 int
934 dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
935 {
936 char name[12]; /* XXX - pick a size */
937 int ret = 0;
938 int c;
939 char dagname[DAGNAME_BUFSIZE];
940 int dagstream;
941 int dagfd;
942
943 /* Try all the DAGs 0-MAXDAG */
944 for (c = 0; c <= MAXDAG; c++) {
945 snprintf(name, 12, "dag%d", c);
946 if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
947 {
948 return -1;
949 }
950 if ( (dagfd = dag_open(dagname)) >= 0 ) {
951 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
952 /*
953 * Failure.
954 */
955 ret = -1;
956 }
957 #ifdef HAVE_DAG_STREAMS_API
958 {
959 int stream, rxstreams;
960 rxstreams = dag_rx_get_stream_count(dagfd);
961 for(stream=0;stream<DAG_STREAM_MAX;stream+=2) {
962 if (0 == dag_attach_stream(dagfd, stream, 0, 0)) {
963 dag_detach_stream(dagfd, stream);
964
965 snprintf(name, 10, "dag%d:%d", c, stream);
966 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
967 /*
968 * Failure.
969 */
970 ret = -1;
971 }
972
973 rxstreams--;
974 if(rxstreams <= 0) {
975 break;
976 }
977 }
978 }
979 }
980 #endif /* HAVE_DAG_STREAMS_API */
981 dag_close(dagfd);
982 }
983
984 }
985 return (ret);
986 }
987
988 /*
989 * Installs the given bpf filter program in the given pcap structure. There is
990 * no attempt to store the filter in kernel memory as that is not supported
991 * with DAG cards.
992 */
993 static int
994 dag_setfilter(pcap_t *p, struct bpf_program *fp)
995 {
996 if (!p)
997 return -1;
998 if (!fp) {
999 strncpy(p->errbuf, "setfilter: No filter specified",
1000 sizeof(p->errbuf));
1001 return -1;
1002 }
1003
1004 /* Make our private copy of the filter */
1005
1006 if (install_bpf_program(p, fp) < 0)
1007 return -1;
1008
1009 return (0);
1010 }
1011
1012 static int
1013 dag_set_datalink(pcap_t *p, int dlt)
1014 {
1015 p->linktype = dlt;
1016
1017 return (0);
1018 }
1019
1020 static int
1021 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1022 {
1023 struct pcap_dag *pd = p->private;
1024
1025 /*
1026 * Set non-blocking mode on the FD.
1027 * XXX - is that necessary? If not, don't bother calling it,
1028 * and have a "dag_getnonblock()" function that looks at
1029 * "pd->dag_offset_flags".
1030 */
1031 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
1032 return (-1);
1033 #ifdef HAVE_DAG_STREAMS_API
1034 {
1035 uint32_t mindata;
1036 struct timeval maxwait;
1037 struct timeval poll;
1038
1039 if (dag_get_stream_poll(p->fd, pd->dag_stream,
1040 &mindata, &maxwait, &poll) < 0) {
1041 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
1042 return -1;
1043 }
1044
1045 /* Amount of data to collect in Bytes before calling callbacks.
1046 * Important for efficiency, but can introduce latency
1047 * at low packet rates if to_ms not set!
1048 */
1049 if(nonblock)
1050 mindata = 0;
1051 else
1052 mindata = 65536;
1053
1054 if (dag_set_stream_poll(p->fd, pd->dag_stream,
1055 mindata, &maxwait, &poll) < 0) {
1056 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
1057 return -1;
1058 }
1059 }
1060 #endif /* HAVE_DAG_STREAMS_API */
1061 if (nonblock) {
1062 pd->dag_offset_flags |= DAGF_NONBLOCK;
1063 } else {
1064 pd->dag_offset_flags &= ~DAGF_NONBLOCK;
1065 }
1066 return (0);
1067 }
1068
1069 static int
1070 dag_get_datalink(pcap_t *p)
1071 {
1072 struct pcap_dag *pd = p->private;
1073 int index=0, dlt_index=0;
1074 uint8_t types[255];
1075
1076 memset(types, 0, 255);
1077
1078 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
1079 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
1080 return (-1);
1081 }
1082
1083 p->linktype = 0;
1084
1085 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1086 /* Get list of possible ERF types for this card */
1087 if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) {
1088 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno));
1089 return (-1);
1090 }
1091
1092 while (types[index]) {
1093
1094 #elif defined HAVE_DAG_GET_ERF_TYPES
1095 /* Get list of possible ERF types for this card */
1096 if (dag_get_erf_types(p->fd, types, 255) < 0) {
1097 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
1098 return (-1);
1099 }
1100
1101 while (types[index]) {
1102 #else
1103 /* Check the type through a dagapi call. */
1104 types[index] = dag_linktype(p->fd);
1105
1106 {
1107 #endif
1108 switch((types[index] & 0x7f)) {
1109
1110 case TYPE_HDLC_POS:
1111 case TYPE_COLOR_HDLC_POS:
1112 case TYPE_DSM_COLOR_HDLC_POS:
1113 case TYPE_COLOR_HASH_POS:
1114
1115 if (p->dlt_list != NULL) {
1116 p->dlt_list[dlt_index++] = DLT_CHDLC;
1117 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1118 p->dlt_list[dlt_index++] = DLT_FRELAY;
1119 }
1120 if(!p->linktype)
1121 p->linktype = DLT_CHDLC;
1122 break;
1123
1124 case TYPE_ETH:
1125 case TYPE_COLOR_ETH:
1126 case TYPE_DSM_COLOR_ETH:
1127 case TYPE_COLOR_HASH_ETH:
1128 /*
1129 * This is (presumably) a real Ethernet capture; give it a
1130 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1131 * that an application can let you choose it, in case you're
1132 * capturing DOCSIS traffic that a Cisco Cable Modem
1133 * Termination System is putting out onto an Ethernet (it
1134 * doesn't put an Ethernet header onto the wire, it puts raw
1135 * DOCSIS frames out on the wire inside the low-level
1136 * Ethernet framing).
1137 */
1138 if (p->dlt_list != NULL) {
1139 p->dlt_list[dlt_index++] = DLT_EN10MB;
1140 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1141 }
1142 if(!p->linktype)
1143 p->linktype = DLT_EN10MB;
1144 break;
1145
1146 case TYPE_ATM:
1147 case TYPE_AAL5:
1148 case TYPE_MC_ATM:
1149 case TYPE_MC_AAL5:
1150 if (p->dlt_list != NULL) {
1151 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1152 p->dlt_list[dlt_index++] = DLT_SUNATM;
1153 }
1154 if(!p->linktype)
1155 p->linktype = DLT_ATM_RFC1483;
1156 break;
1157
1158 case TYPE_COLOR_MC_HDLC_POS:
1159 case TYPE_MC_HDLC:
1160 if (p->dlt_list != NULL) {
1161 p->dlt_list[dlt_index++] = DLT_CHDLC;
1162 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1163 p->dlt_list[dlt_index++] = DLT_FRELAY;
1164 p->dlt_list[dlt_index++] = DLT_MTP2;
1165 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1166 p->dlt_list[dlt_index++] = DLT_LAPD;
1167 }
1168 if(!p->linktype)
1169 p->linktype = DLT_CHDLC;
1170 break;
1171
1172 case TYPE_IPV4:
1173 case TYPE_IPV6:
1174 if(!p->linktype)
1175 p->linktype = DLT_RAW;
1176 break;
1177
1178 case TYPE_LEGACY:
1179 case TYPE_MC_RAW:
1180 case TYPE_MC_RAW_CHANNEL:
1181 case TYPE_IP_COUNTER:
1182 case TYPE_TCP_FLOW_COUNTER:
1183 case TYPE_INFINIBAND:
1184 case TYPE_RAW_LINK:
1185 case TYPE_INFINIBAND_LINK:
1186 default:
1187 /* Libpcap cannot deal with these types yet */
1188 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1189 break;
1190
1191 } /* switch */
1192 index++;
1193 }
1194
1195 p->dlt_list[dlt_index++] = DLT_ERF;
1196
1197 p->dlt_count = dlt_index;
1198
1199 if(!p->linktype)
1200 p->linktype = DLT_ERF;
1201
1202 return p->linktype;
1203 }