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