]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
From Stephen Donnelly:
[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.3 2007-11-09 00:57:01 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 /* Count lost packets. */
319 switch((header->type & 0x7f)) {
320 /* in these types the color value overwrites the lctr */
321 case TYPE_COLOR_HDLC_POS:
322 case TYPE_COLOR_ETH:
323 case TYPE_DSM_COLOR_HDLC_POS:
324 case TYPE_DSM_COLOR_ETH:
325 case TYPE_COLOR_MC_HDLC_POS:
326 case TYPE_COLOR_HASH_ETH:
327 case TYPE_COLOR_HASH_POS:
328 break;
329
330 default:
331 if (header->lctr) {
332 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
333 p->md.stat.ps_drop = UINT_MAX;
334 } else {
335 p->md.stat.ps_drop += ntohs(header->lctr);
336 }
337 }
338 }
339
340 if ((header->type & 0x7f) == TYPE_PAD) {
341 continue;
342 }
343
344 num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
345
346 /* ERF encapsulation */
347 /* The Extensible Record Format is not dropped for this kind of encapsulation,
348 * and will be handled as a pseudo header by the decoding application.
349 * The information carried in the ERF header and in the optional subheader (if present)
350 * could be merged with the libpcap information, to offer a better decoding.
351 * The packet length is
352 * o the length of the packet on the link (header->wlen),
353 * o plus the length of the ERF header (dag_record_size), as the length of the
354 * pseudo header will be adjusted during the decoding,
355 * o plus the length of the optional subheader (if present).
356 *
357 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
358 * if the capture length is greater than the packet length.
359 */
360 if (p->linktype == DLT_ERF) {
361 packet_len = ntohs(header->wlen) + dag_record_size;
362 caplen = rlen;
363 switch ((header->type & 0x7f)) {
364 case TYPE_MC_AAL5:
365 case TYPE_MC_ATM:
366 case TYPE_MC_HDLC:
367 packet_len += 4; /* MC header */
368 break;
369
370 case TYPE_COLOR_HASH_ETH:
371 case TYPE_DSM_COLOR_ETH:
372 case TYPE_COLOR_ETH:
373 case TYPE_ETH:
374 packet_len += 2; /* ETH header */
375 break;
376 } /* switch type */
377
378 /* Include ERF extension headers */
379 packet_len += (8 * num_ext_hdr);
380
381 if (caplen > packet_len) {
382 caplen = packet_len;
383 }
384 } else {
385 /* Other kind of encapsulation according to the header Type */
386
387 /* Skip over generic ERF header */
388 dp += dag_record_size;
389 /* Skip over extension headers */
390 dp += 8 * num_ext_hdr;
391
392 switch((header->type & 0x7f)) {
393 case TYPE_ATM:
394 case TYPE_AAL5:
395 if (header->type == TYPE_AAL5) {
396 packet_len = ntohs(header->wlen);
397 caplen = rlen - dag_record_size;
398 }
399 case TYPE_MC_ATM:
400 if (header->type == TYPE_MC_ATM) {
401 caplen = packet_len = ATM_CELL_SIZE;
402 dp+=4;
403 }
404 case TYPE_MC_AAL5:
405 if (header->type == TYPE_MC_AAL5) {
406 packet_len = ntohs(header->wlen);
407 caplen = rlen - dag_record_size - 4;
408 dp+=4;
409 }
410 if (header->type == TYPE_ATM) {
411 caplen = packet_len = ATM_CELL_SIZE;
412 }
413 if (p->linktype == DLT_SUNATM) {
414 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
415 unsigned long rawatm;
416
417 rawatm = ntohl(*((unsigned long *)dp));
418 sunatm->vci = htons((rawatm >> 4) & 0xffff);
419 sunatm->vpi = (rawatm >> 20) & 0x00ff;
420 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
421 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
422 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
423 ((dp[ATM_HDR_SIZE] == 0xaa &&
424 dp[ATM_HDR_SIZE+1] == 0xaa &&
425 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
426
427 } else {
428 packet_len -= ATM_HDR_SIZE;
429 caplen -= ATM_HDR_SIZE;
430 dp += ATM_HDR_SIZE;
431 }
432 break;
433
434 case TYPE_COLOR_HASH_ETH:
435 case TYPE_DSM_COLOR_ETH:
436 case TYPE_COLOR_ETH:
437 case TYPE_ETH:
438 packet_len = ntohs(header->wlen);
439 packet_len -= (p->md.dag_fcs_bits >> 3);
440 caplen = rlen - dag_record_size - 2;
441 if (caplen > packet_len) {
442 caplen = packet_len;
443 }
444 dp += 2;
445 break;
446
447 case TYPE_COLOR_HASH_POS:
448 case TYPE_DSM_COLOR_HDLC_POS:
449 case TYPE_COLOR_HDLC_POS:
450 case TYPE_HDLC_POS:
451 packet_len = ntohs(header->wlen);
452 packet_len -= (p->md.dag_fcs_bits >> 3);
453 caplen = rlen - dag_record_size;
454 if (caplen > packet_len) {
455 caplen = packet_len;
456 }
457 break;
458
459 case TYPE_COLOR_MC_HDLC_POS:
460 case TYPE_MC_HDLC:
461 packet_len = ntohs(header->wlen);
462 packet_len -= (p->md.dag_fcs_bits >> 3);
463 caplen = rlen - dag_record_size - 4;
464 if (caplen > packet_len) {
465 caplen = packet_len;
466 }
467 /* jump the MC_HDLC_HEADER */
468 dp += 4;
469 #ifdef DLT_MTP2_WITH_PHDR
470 if (p->linktype == DLT_MTP2_WITH_PHDR) {
471 /* Add the MTP2 Pseudo Header */
472 caplen += MTP2_HDR_LEN;
473 packet_len += MTP2_HDR_LEN;
474
475 TempPkt[MTP2_SENT_OFFSET] = 0;
476 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
477 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
478 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
479 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
480 dp = TempPkt;
481 }
482 #endif
483 break;
484
485 case TYPE_IPV4:
486 packet_len = ntohs(header->wlen);
487 caplen = rlen - dag_record_size;
488 if (caplen > packet_len) {
489 caplen = packet_len;
490 }
491 break;
492
493 default:
494 /* Unhandled ERF type.
495 * Ignore rather than generating error
496 */
497 continue;
498 } /* switch type */
499
500 /* Skip over extension headers */
501 caplen -= (8 * num_ext_hdr);
502
503 } /* ERF encapsulation */
504
505 if (caplen > p->snapshot)
506 caplen = p->snapshot;
507
508 /* Run the packet filter if there is one. */
509 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
510
511 /* convert between timestamp formats */
512 register unsigned long long ts;
513
514 if (IS_BIGENDIAN()) {
515 ts = SWAPLL(header->ts);
516 } else {
517 ts = header->ts;
518 }
519
520 pcap_header.ts.tv_sec = ts >> 32;
521 ts = (ts & 0xffffffffULL) * 1000000;
522 ts += 0x80000000; /* rounding */
523 pcap_header.ts.tv_usec = ts >> 32;
524 if (pcap_header.ts.tv_usec >= 1000000) {
525 pcap_header.ts.tv_usec -= 1000000;
526 pcap_header.ts.tv_sec++;
527 }
528
529 /* Fill in our own header data */
530 pcap_header.caplen = caplen;
531 pcap_header.len = packet_len;
532
533 /* Count the packet. */
534 p->md.stat.ps_recv++;
535
536 /* Call the user supplied callback function */
537 callback(user, &pcap_header, dp);
538
539 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
540 processed++;
541 if (processed == cnt)
542 {
543 /* Reached the user-specified limit. */
544 return cnt;
545 }
546 }
547 }
548
549 return processed;
550 }
551
552 static int
553 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
554 {
555 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
556 PCAP_ERRBUF_SIZE);
557 return (-1);
558 }
559
560 /*
561 * Get a handle for a live capture from the given DAG device. Passing a NULL
562 * device will result in a failure. The promisc flag is ignored because DAG
563 * cards are always promiscuous. The to_ms parameter is also ignored as it is
564 * not supported in hardware.
565 *
566 * snaplen is now also ignored, until we get per-stream slen support. Set
567 * slen with approprite DAG tool BEFORE pcap_open_live().
568 *
569 * See also pcap(3).
570 */
571 pcap_t *
572 dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
573 {
574 #if 0
575 char conf[30]; /* dag configure string */
576 #endif
577 pcap_t *handle;
578 char *s;
579 int n;
580 daginf_t* daginf;
581 char * newDev = NULL;
582 #ifdef HAVE_DAG_STREAMS_API
583 uint32_t mindata;
584 struct timeval maxwait;
585 struct timeval poll;
586 #endif
587
588 if (device == NULL) {
589 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
590 return NULL;
591 }
592 /* Allocate a handle for this session. */
593
594 handle = malloc(sizeof(*handle));
595 if (handle == NULL) {
596 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
597 return NULL;
598 }
599
600 /* Initialize some components of the pcap structure. */
601
602 memset(handle, 0, sizeof(*handle));
603
604 #ifdef HAVE_DAG_STREAMS_API
605 newDev = (char *)malloc(strlen(device) + 16);
606 if (newDev == NULL) {
607 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
608 goto fail;
609 }
610
611 /* Parse input name to get dag device and stream number if provided */
612 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
613 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
614 goto fail;
615 }
616 device = newDev;
617
618 if (handle->md.dag_stream%2) {
619 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
620 goto fail;
621 }
622 #else
623 if (strncmp(device, "/dev/", 5) != 0) {
624 newDev = (char *)malloc(strlen(device) + 5);
625 if (newDev == NULL) {
626 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
627 goto fail;
628 }
629 strcpy(newDev, "/dev/");
630 strcat(newDev, device);
631 device = newDev;
632 }
633 #endif /* HAVE_DAG_STREAMS_API */
634
635 /* setup device parameters */
636 if((handle->fd = dag_open((char *)device)) < 0) {
637 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
638 goto fail;
639 }
640
641 #ifdef HAVE_DAG_STREAMS_API
642 /* Open requested stream. Can fail if already locked or on error */
643 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
644 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
645 goto failclose;
646 }
647
648 /* Set up default poll parameters for stream
649 * Can be overridden by pcap_set_nonblock()
650 */
651 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
652 &mindata, &maxwait, &poll) < 0) {
653 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
654 goto faildetach;
655 }
656
657 /* Amount of data to collect in Bytes before calling callbacks.
658 * Important for efficiency, but can introduce latency
659 * at low packet rates if to_ms not set!
660 */
661 mindata = 65536;
662
663 /* Obey to_ms if supplied. This is a good idea!
664 * Recommend 10-100ms. Calls will time out even if no data arrived.
665 */
666 maxwait.tv_sec = to_ms/1000;
667 maxwait.tv_usec = (to_ms%1000) * 1000;
668
669 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
670 mindata, &maxwait, &poll) < 0) {
671 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
672 goto faildetach;
673 }
674
675 #else
676 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
677 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
678 goto failclose;
679 }
680
681 #endif /* HAVE_DAG_STREAMS_API */
682
683 /* XXX Not calling dag_configure() to set slen; this is unsafe in
684 * multi-stream environments as the gpp config is global.
685 * Once the firmware provides 'per-stream slen' this can be supported
686 * again via the Config API without side-effects */
687 #if 0
688 /* set the card snap length to the specified snaplen parameter */
689 /* This is a really bad idea, as different cards have different
690 * valid slen ranges. Should fix in Config API. */
691 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
692 snaplen = MAX_DAG_SNAPLEN;
693 } else if (snaplen < MIN_DAG_SNAPLEN) {
694 snaplen = MIN_DAG_SNAPLEN;
695 }
696 /* snap len has to be a multiple of 4 */
697 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
698
699 if(dag_configure(handle->fd, conf) < 0) {
700 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
701 goto faildetach;
702 }
703 #endif
704
705 #ifdef HAVE_DAG_STREAMS_API
706 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
707 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
708 goto faildetach;
709 }
710 #else
711 if(dag_start(handle->fd) < 0) {
712 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
713 goto failclose;
714 }
715 #endif /* HAVE_DAG_STREAMS_API */
716
717 /*
718 * Important! You have to ensure bottom is properly
719 * initialized to zero on startup, it won't give you
720 * a compiler warning if you make this mistake!
721 */
722 handle->md.dag_mem_bottom = 0;
723 handle->md.dag_mem_top = 0;
724
725 /*
726 * Find out how many FCS bits we should strip.
727 * First, query the card to see if it strips the FCS.
728 */
729 daginf = dag_info(handle->fd);
730 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) {
731 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
732 handle->md.dag_fcs_bits = 0;
733
734 /* Note that no FCS will be supplied. */
735 handle->linktype_ext = LT_FCS_DATALINK_EXT(0);
736 } else {
737 /*
738 * Start out assuming it's 32 bits.
739 */
740 handle->md.dag_fcs_bits = 32;
741
742 /* Allow an environment variable to override. */
743 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
744 if ((n = atoi(s)) == 0 || n == 16 || n == 32) {
745 handle->md.dag_fcs_bits = n;
746 } else {
747 snprintf(ebuf, PCAP_ERRBUF_SIZE,
748 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
749 goto failstop;
750 }
751 }
752
753 /*
754 * Did the user request that they not be stripped?
755 */
756 if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) {
757 /* Yes. Note the number of bytes that will be
758 supplied. */
759 handle->linktype_ext = LT_FCS_DATALINK_EXT(handle->md.dag_fcs_bits/16);
760
761 /* And don't strip them. */
762 handle->md.dag_fcs_bits = 0;
763 }
764 }
765
766 handle->snapshot = snaplen;
767 handle->md.dag_timeout = to_ms;
768
769 handle->linktype = -1;
770 if (dag_get_datalink(handle) < 0) {
771 strcpy(ebuf, handle->errbuf);
772 goto failstop;
773 }
774
775 handle->bufsize = 0;
776
777 if (new_pcap_dag(handle) < 0) {
778 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
779 goto failstop;
780 }
781
782 /*
783 * "select()" and "poll()" don't work on DAG device descriptors.
784 */
785 handle->selectable_fd = -1;
786
787 if (newDev != NULL) {
788 free((char *)newDev);
789 }
790
791 handle->read_op = dag_read;
792 handle->inject_op = dag_inject;
793 handle->setfilter_op = dag_setfilter;
794 handle->setdirection_op = NULL; /* Not implemented.*/
795 handle->set_datalink_op = dag_set_datalink;
796 handle->getnonblock_op = pcap_getnonblock_fd;
797 handle->setnonblock_op = dag_setnonblock;
798 handle->stats_op = dag_stats;
799 handle->close_op = dag_platform_close;
800 handle->md.stat.ps_drop = 0;
801 handle->md.stat.ps_recv = 0;
802 return handle;
803
804 #ifdef HAVE_DAG_STREAMS_API
805 failstop:
806 if (handle != NULL) {
807 if (dag_stop_stream(handle->fd, handle->md.dag_stream) < 0)
808 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
809 }
810
811 faildetach:
812 if (handle != NULL) {
813 if (dag_detach_stream(handle->fd, handle->md.dag_stream) < 0)
814 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
815 }
816 #else
817 failstop:
818 if (handle != NULL) {
819 if (dag_stop(p->fd) < 0)
820 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
821 }
822 #endif /* HAVE_DAG_STREAMS_API */
823
824 failclose:
825 if (handle != NULL) {
826 if (dag_close(handle->fd) < 0)
827 fprintf(stderr,"dag_close: %s\n", strerror(errno));
828 }
829 if (handle != NULL)
830 delete_pcap_dag(handle);
831
832 fail:
833 if (newDev != NULL) {
834 free((char *)newDev);
835 }
836 if (handle != NULL) {
837 /*
838 * Get rid of any link-layer type list we allocated.
839 */
840 if (handle->dlt_list != NULL) {
841 free(handle->dlt_list);
842 }
843 free(handle);
844 }
845
846 return NULL;
847 }
848
849 static int
850 dag_stats(pcap_t *p, struct pcap_stat *ps) {
851 /* This needs to be filled out correctly. Hopefully a dagapi call will
852 provide all necessary information.
853 */
854 /*p->md.stat.ps_recv = 0;*/
855 /*p->md.stat.ps_drop = 0;*/
856
857 *ps = p->md.stat;
858
859 return 0;
860 }
861
862 /*
863 * Previously we just generated a list of all possible names and let
864 * pcap_add_if() attempt to open each one, but with streams this adds up
865 * to 81 possibilities which is inefficient.
866 *
867 * Since we know more about the devices we can prune the tree here.
868 * pcap_add_if() will still retest each device but the total number of
869 * open attempts will still be much less than the naive approach.
870 */
871 int
872 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
873 {
874 char name[12]; /* XXX - pick a size */
875 int ret = 0;
876 int c;
877 char dagname[DAGNAME_BUFSIZE];
878 int dagstream;
879 int dagfd;
880
881 /* Try all the DAGs 0-9 */
882 for (c = 0; c < 9; c++) {
883 snprintf(name, 12, "dag%d", c);
884 if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
885 {
886 return -1;
887 }
888 if ( (dagfd = dag_open(dagname)) >= 0 ) {
889 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
890 /*
891 * Failure.
892 */
893 ret = -1;
894 }
895 #ifdef HAVE_DAG_STREAMS_API
896 {
897 int stream, rxstreams;
898 rxstreams = dag_rx_get_stream_count(dagfd);
899 for(stream=0;stream<16;stream+=2) {
900 if (0 == dag_attach_stream(dagfd, stream, 0, 0)) {
901 dag_detach_stream(dagfd, stream);
902
903 snprintf(name, 10, "dag%d:%d", c, stream);
904 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
905 /*
906 * Failure.
907 */
908 ret = -1;
909 }
910 }
911 }
912 }
913 #endif /* HAVE_DAG_STREAMS_API */
914 dag_close(dagfd);
915 }
916
917 }
918 return (ret);
919 }
920
921 /*
922 * Installs the given bpf filter program in the given pcap structure. There is
923 * no attempt to store the filter in kernel memory as that is not supported
924 * with DAG cards.
925 */
926 static int
927 dag_setfilter(pcap_t *p, struct bpf_program *fp)
928 {
929 if (!p)
930 return -1;
931 if (!fp) {
932 strncpy(p->errbuf, "setfilter: No filter specified",
933 sizeof(p->errbuf));
934 return -1;
935 }
936
937 /* Make our private copy of the filter */
938
939 if (install_bpf_program(p, fp) < 0)
940 return -1;
941
942 p->md.use_bpf = 0;
943
944 return (0);
945 }
946
947 static int
948 dag_set_datalink(pcap_t *p, int dlt)
949 {
950 p->linktype = dlt;
951
952 return (0);
953 }
954
955 static int
956 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
957 {
958 /*
959 * Set non-blocking mode on the FD.
960 * XXX - is that necessary? If not, don't bother calling it,
961 * and have a "dag_getnonblock()" function that looks at
962 * "p->md.dag_offset_flags".
963 */
964 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
965 return (-1);
966 #ifdef HAVE_DAG_STREAMS_API
967 {
968 uint32_t mindata;
969 struct timeval maxwait;
970 struct timeval poll;
971
972 if (dag_get_stream_poll(p->fd, p->md.dag_stream,
973 &mindata, &maxwait, &poll) < 0) {
974 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
975 return -1;
976 }
977
978 /* Amount of data to collect in Bytes before calling callbacks.
979 * Important for efficiency, but can introduce latency
980 * at low packet rates if to_ms not set!
981 */
982 if(nonblock)
983 mindata = 0;
984 else
985 mindata = 65536;
986
987 if (dag_set_stream_poll(p->fd, p->md.dag_stream,
988 mindata, &maxwait, &poll) < 0) {
989 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
990 return -1;
991 }
992 }
993 #endif /* HAVE_DAG_STREAMS_API */
994 if (nonblock) {
995 p->md.dag_offset_flags |= DAGF_NONBLOCK;
996 } else {
997 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
998 }
999 return (0);
1000 }
1001
1002 static int
1003 dag_get_datalink(pcap_t *p)
1004 {
1005 int index=0, dlt_index=0;
1006 uint8_t types[255];
1007
1008 memset(types, 0, 255);
1009
1010 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
1011 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
1012 return (-1);
1013 }
1014
1015 p->linktype = 0;
1016
1017 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1018 /* Get list of possible ERF types for this card */
1019 if (dag_get_stream_erf_types(p->fd, p->md.dag_stream, types, 255) < 0) {
1020 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno));
1021 return (-1);
1022 }
1023
1024 while (types[index]) {
1025
1026 #elif defined HAVE_DAG_GET_ERF_TYPES
1027 /* Get list of possible ERF types for this card */
1028 if (dag_get_erf_types(p->fd, types, 255) < 0) {
1029 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
1030 return (-1);
1031 }
1032
1033 while (types[index]) {
1034 #else
1035 /* Check the type through a dagapi call. */
1036 types[index] = dag_linktype(p->fd);
1037
1038 {
1039 #endif
1040 switch((types[index] & 0x7f)) {
1041
1042 case TYPE_HDLC_POS:
1043 case TYPE_COLOR_HDLC_POS:
1044 case TYPE_DSM_COLOR_HDLC_POS:
1045 case TYPE_COLOR_HASH_POS:
1046
1047 if (p->dlt_list != NULL) {
1048 p->dlt_list[dlt_index++] = DLT_CHDLC;
1049 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1050 p->dlt_list[dlt_index++] = DLT_FRELAY;
1051 }
1052 if(!p->linktype)
1053 p->linktype = DLT_CHDLC;
1054 break;
1055
1056 case TYPE_ETH:
1057 case TYPE_COLOR_ETH:
1058 case TYPE_DSM_COLOR_ETH:
1059 case TYPE_COLOR_HASH_ETH:
1060 /*
1061 * This is (presumably) a real Ethernet capture; give it a
1062 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1063 * that an application can let you choose it, in case you're
1064 * capturing DOCSIS traffic that a Cisco Cable Modem
1065 * Termination System is putting out onto an Ethernet (it
1066 * doesn't put an Ethernet header onto the wire, it puts raw
1067 * DOCSIS frames out on the wire inside the low-level
1068 * Ethernet framing).
1069 */
1070 if (p->dlt_list != NULL) {
1071 p->dlt_list[dlt_index++] = DLT_EN10MB;
1072 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1073 }
1074 if(!p->linktype)
1075 p->linktype = DLT_EN10MB;
1076 break;
1077
1078 case TYPE_ATM:
1079 case TYPE_AAL5:
1080 case TYPE_MC_ATM:
1081 case TYPE_MC_AAL5:
1082 if (p->dlt_list != NULL) {
1083 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1084 p->dlt_list[dlt_index++] = DLT_SUNATM;
1085 }
1086 if(!p->linktype)
1087 p->linktype = DLT_ATM_RFC1483;
1088 break;
1089
1090 case TYPE_COLOR_MC_HDLC_POS:
1091 case TYPE_MC_HDLC:
1092 if (p->dlt_list != NULL) {
1093 p->dlt_list[dlt_index++] = DLT_CHDLC;
1094 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1095 p->dlt_list[dlt_index++] = DLT_FRELAY;
1096 p->dlt_list[dlt_index++] = DLT_MTP2;
1097 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1098 p->dlt_list[dlt_index++] = DLT_LAPD;
1099 }
1100 if(!p->linktype)
1101 p->linktype = DLT_CHDLC;
1102 break;
1103
1104 case TYPE_IPV4:
1105 if(!p->linktype)
1106 p->linktype = DLT_RAW;
1107 break;
1108
1109 case TYPE_LEGACY:
1110 case TYPE_MC_RAW:
1111 case TYPE_MC_RAW_CHANNEL:
1112 case TYPE_IP_COUNTER:
1113 case TYPE_TCP_FLOW_COUNTER:
1114 case TYPE_INFINIBAND:
1115 case TYPE_IPV6:
1116 default:
1117 /* Libpcap cannot deal with these types yet */
1118 /* Add no DLTs, but still covered by DLT_ERF */
1119 break;
1120
1121 } /* switch */
1122 index++;
1123 }
1124
1125 p->dlt_list[dlt_index++] = DLT_ERF;
1126
1127 p->dlt_count = dlt_index;
1128
1129 if(!p->linktype)
1130 p->linktype = DLT_ERF;
1131
1132 return p->linktype;
1133 }