]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
Move platform-dependent pcap_t data out of the pcap_t structure.
[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 /* Amount of data to collect in Bytes before calling callbacks.
687 * Important for efficiency, but can introduce latency
688 * at low packet rates if to_ms not set!
689 */
690 mindata = 65536;
691
692 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
693 * Recommend 10-100ms. Calls will time out even if no data arrived.
694 */
695 maxwait.tv_sec = handle->opt.timeout/1000;
696 maxwait.tv_usec = (handle->opt.timeout%1000) * 1000;
697
698 if (dag_set_stream_poll(handle->fd, handlep->dag_stream,
699 mindata, &maxwait, &poll) < 0) {
700 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
701 goto faildetach;
702 }
703
704 #else
705 if((handlep->dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
706 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
707 goto failclose;
708 }
709
710 #endif /* HAVE_DAG_STREAMS_API */
711
712 /* XXX Not calling dag_configure() to set slen; this is unsafe in
713 * multi-stream environments as the gpp config is global.
714 * Once the firmware provides 'per-stream slen' this can be supported
715 * again via the Config API without side-effects */
716 #if 0
717 /* set the card snap length to the specified snaplen parameter */
718 /* This is a really bad idea, as different cards have different
719 * valid slen ranges. Should fix in Config API. */
720 if (handle->snapshot == 0 || handle->snapshot > MAX_DAG_SNAPLEN) {
721 handle->snapshot = MAX_DAG_SNAPLEN;
722 } else if (snaplen < MIN_DAG_SNAPLEN) {
723 handle->snapshot = MIN_DAG_SNAPLEN;
724 }
725 /* snap len has to be a multiple of 4 */
726 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
727
728 if(dag_configure(handle->fd, conf) < 0) {
729 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
730 goto faildetach;
731 }
732 #endif
733
734 #ifdef HAVE_DAG_STREAMS_API
735 if(dag_start_stream(handle->fd, handlep->dag_stream) < 0) {
736 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
737 goto faildetach;
738 }
739 #else
740 if(dag_start(handle->fd) < 0) {
741 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
742 goto failclose;
743 }
744 #endif /* HAVE_DAG_STREAMS_API */
745
746 /*
747 * Important! You have to ensure bottom is properly
748 * initialized to zero on startup, it won't give you
749 * a compiler warning if you make this mistake!
750 */
751 handlep->dag_mem_bottom = 0;
752 handlep->dag_mem_top = 0;
753
754 /*
755 * Find out how many FCS bits we should strip.
756 * First, query the card to see if it strips the FCS.
757 */
758 daginf = dag_info(handle->fd);
759 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) {
760 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
761 handlep->dag_fcs_bits = 0;
762
763 /* Note that no FCS will be supplied. */
764 handle->linktype_ext = LT_FCS_DATALINK_EXT(0);
765 } else {
766 /*
767 * Start out assuming it's 32 bits.
768 */
769 handlep->dag_fcs_bits = 32;
770
771 /* Allow an environment variable to override. */
772 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
773 if ((n = atoi(s)) == 0 || n == 16 || n == 32) {
774 handlep->dag_fcs_bits = n;
775 } else {
776 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
777 "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
778 goto failstop;
779 }
780 }
781
782 /*
783 * Did the user request that they not be stripped?
784 */
785 if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) {
786 /* Yes. Note the number of bytes that will be
787 supplied. */
788 handle->linktype_ext = LT_FCS_DATALINK_EXT(handlep->dag_fcs_bits/16);
789
790 /* And don't strip them. */
791 handlep->dag_fcs_bits = 0;
792 }
793 }
794
795 handlep->dag_timeout = handle->opt.timeout;
796
797 handle->linktype = -1;
798 if (dag_get_datalink(handle) < 0)
799 goto failstop;
800
801 handle->bufsize = 0;
802
803 if (new_pcap_dag(handle) < 0) {
804 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
805 goto failstop;
806 }
807
808 /*
809 * "select()" and "poll()" don't work on DAG device descriptors.
810 */
811 handle->selectable_fd = -1;
812
813 if (newDev != NULL) {
814 free((char *)newDev);
815 }
816
817 handle->read_op = dag_read;
818 handle->inject_op = dag_inject;
819 handle->setfilter_op = dag_setfilter;
820 handle->setdirection_op = NULL; /* Not implemented.*/
821 handle->set_datalink_op = dag_set_datalink;
822 handle->getnonblock_op = pcap_getnonblock_fd;
823 handle->setnonblock_op = dag_setnonblock;
824 handle->stats_op = dag_stats;
825 handle->cleanup_op = dag_platform_cleanup;
826 handlep->stat.ps_drop = 0;
827 handlep->stat.ps_recv = 0;
828 handlep->stat.ps_ifdrop = 0;
829 return 0;
830
831 #ifdef HAVE_DAG_STREAMS_API
832 failstop:
833 if (dag_stop_stream(handle->fd, handlep->dag_stream) < 0) {
834 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
835 }
836
837 faildetach:
838 if (dag_detach_stream(handle->fd, handlep->dag_stream) < 0)
839 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
840 #else
841 failstop:
842 if (dag_stop(handle->fd) < 0)
843 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
844 #endif /* HAVE_DAG_STREAMS_API */
845
846 failclose:
847 if (dag_close(handle->fd) < 0)
848 fprintf(stderr,"dag_close: %s\n", strerror(errno));
849 delete_pcap_dag(handle);
850
851 fail:
852 pcap_cleanup_live_common(handle);
853 if (newDev != NULL) {
854 free((char *)newDev);
855 }
856
857 return PCAP_ERROR;
858 }
859
860 pcap_t *dag_create(const char *device, char *ebuf, int *is_ours)
861 {
862 const char *cp;
863 char *cpend;
864 long devnum;
865 pcap_t *p;
866
867 /* Does this look like a DAG device? */
868 cp = strrchr(device, '/');
869 if (cp == NULL)
870 cp = device;
871 /* Does it begin with "dag"? */
872 if (strncmp(cp, "dag", 3) != 0) {
873 /* Nope, doesn't begin with "dag" */
874 *is_ours = 0;
875 return NULL;
876 }
877 /* Yes - is "dag" followed by a number from 0 to MAXDAG? */
878 cp += 3;
879 devnum = strtol(cp, &cpend, 10);
880 if (cpend == cp || *cpend != '\0') {
881 /* Not followed by a number. */
882 *is_ours = 0;
883 return NULL;
884 }
885 if (devnum < 0 || devnum > MAXDAG) {
886 /* Followed by a non-valid number. */
887 *is_ours = 0;
888 return NULL;
889 }
890
891 /* OK, it's probably ours. */
892 *is_ours = 1;
893
894 p = pcap_create_common(device, ebuf, sizeof (struct pcap_dag));
895 if (p == NULL)
896 return NULL;
897
898 p->activate_op = dag_activate;
899 return p;
900 }
901
902 static int
903 dag_stats(pcap_t *p, struct pcap_stat *ps) {
904 struct pcap_dag *pd = p->private;
905
906 /* This needs to be filled out correctly. Hopefully a dagapi call will
907 provide all necessary information.
908 */
909 /*pd->stat.ps_recv = 0;*/
910 /*pd->stat.ps_drop = 0;*/
911
912 *ps = pd->stat;
913
914 return 0;
915 }
916
917 /*
918 * Previously we just generated a list of all possible names and let
919 * pcap_add_if() attempt to open each one, but with streams this adds up
920 * to 81 possibilities which is inefficient.
921 *
922 * Since we know more about the devices we can prune the tree here.
923 * pcap_add_if() will still retest each device but the total number of
924 * open attempts will still be much less than the naive approach.
925 */
926 int
927 dag_findalldevs(pcap_if_t **devlistp, char *errbuf)
928 {
929 char name[12]; /* XXX - pick a size */
930 int ret = 0;
931 int c;
932 char dagname[DAGNAME_BUFSIZE];
933 int dagstream;
934 int dagfd;
935
936 /* Try all the DAGs 0-MAXDAG */
937 for (c = 0; c <= MAXDAG; c++) {
938 snprintf(name, 12, "dag%d", c);
939 if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream))
940 {
941 return -1;
942 }
943 if ( (dagfd = dag_open(dagname)) >= 0 ) {
944 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
945 /*
946 * Failure.
947 */
948 ret = -1;
949 }
950 #ifdef HAVE_DAG_STREAMS_API
951 {
952 int stream, rxstreams;
953 rxstreams = dag_rx_get_stream_count(dagfd);
954 for(stream=0;stream<DAG_STREAM_MAX;stream+=2) {
955 if (0 == dag_attach_stream(dagfd, stream, 0, 0)) {
956 dag_detach_stream(dagfd, stream);
957
958 snprintf(name, 10, "dag%d:%d", c, stream);
959 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
960 /*
961 * Failure.
962 */
963 ret = -1;
964 }
965
966 rxstreams--;
967 if(rxstreams <= 0) {
968 break;
969 }
970 }
971 }
972 }
973 #endif /* HAVE_DAG_STREAMS_API */
974 dag_close(dagfd);
975 }
976
977 }
978 return (ret);
979 }
980
981 /*
982 * Installs the given bpf filter program in the given pcap structure. There is
983 * no attempt to store the filter in kernel memory as that is not supported
984 * with DAG cards.
985 */
986 static int
987 dag_setfilter(pcap_t *p, struct bpf_program *fp)
988 {
989 if (!p)
990 return -1;
991 if (!fp) {
992 strncpy(p->errbuf, "setfilter: No filter specified",
993 sizeof(p->errbuf));
994 return -1;
995 }
996
997 /* Make our private copy of the filter */
998
999 if (install_bpf_program(p, fp) < 0)
1000 return -1;
1001
1002 return (0);
1003 }
1004
1005 static int
1006 dag_set_datalink(pcap_t *p, int dlt)
1007 {
1008 p->linktype = dlt;
1009
1010 return (0);
1011 }
1012
1013 static int
1014 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1015 {
1016 struct pcap_dag *pd = p->private;
1017
1018 /*
1019 * Set non-blocking mode on the FD.
1020 * XXX - is that necessary? If not, don't bother calling it,
1021 * and have a "dag_getnonblock()" function that looks at
1022 * "pd->dag_offset_flags".
1023 */
1024 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
1025 return (-1);
1026 #ifdef HAVE_DAG_STREAMS_API
1027 {
1028 uint32_t mindata;
1029 struct timeval maxwait;
1030 struct timeval poll;
1031
1032 if (dag_get_stream_poll(p->fd, pd->dag_stream,
1033 &mindata, &maxwait, &poll) < 0) {
1034 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
1035 return -1;
1036 }
1037
1038 /* Amount of data to collect in Bytes before calling callbacks.
1039 * Important for efficiency, but can introduce latency
1040 * at low packet rates if to_ms not set!
1041 */
1042 if(nonblock)
1043 mindata = 0;
1044 else
1045 mindata = 65536;
1046
1047 if (dag_set_stream_poll(p->fd, pd->dag_stream,
1048 mindata, &maxwait, &poll) < 0) {
1049 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
1050 return -1;
1051 }
1052 }
1053 #endif /* HAVE_DAG_STREAMS_API */
1054 if (nonblock) {
1055 pd->dag_offset_flags |= DAGF_NONBLOCK;
1056 } else {
1057 pd->dag_offset_flags &= ~DAGF_NONBLOCK;
1058 }
1059 return (0);
1060 }
1061
1062 static int
1063 dag_get_datalink(pcap_t *p)
1064 {
1065 struct pcap_dag *pd = p->private;
1066 int index=0, dlt_index=0;
1067 uint8_t types[255];
1068
1069 memset(types, 0, 255);
1070
1071 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
1072 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
1073 return (-1);
1074 }
1075
1076 p->linktype = 0;
1077
1078 #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES
1079 /* Get list of possible ERF types for this card */
1080 if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) {
1081 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno));
1082 return (-1);
1083 }
1084
1085 while (types[index]) {
1086
1087 #elif defined HAVE_DAG_GET_ERF_TYPES
1088 /* Get list of possible ERF types for this card */
1089 if (dag_get_erf_types(p->fd, types, 255) < 0) {
1090 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
1091 return (-1);
1092 }
1093
1094 while (types[index]) {
1095 #else
1096 /* Check the type through a dagapi call. */
1097 types[index] = dag_linktype(p->fd);
1098
1099 {
1100 #endif
1101 switch((types[index] & 0x7f)) {
1102
1103 case TYPE_HDLC_POS:
1104 case TYPE_COLOR_HDLC_POS:
1105 case TYPE_DSM_COLOR_HDLC_POS:
1106 case TYPE_COLOR_HASH_POS:
1107
1108 if (p->dlt_list != NULL) {
1109 p->dlt_list[dlt_index++] = DLT_CHDLC;
1110 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1111 p->dlt_list[dlt_index++] = DLT_FRELAY;
1112 }
1113 if(!p->linktype)
1114 p->linktype = DLT_CHDLC;
1115 break;
1116
1117 case TYPE_ETH:
1118 case TYPE_COLOR_ETH:
1119 case TYPE_DSM_COLOR_ETH:
1120 case TYPE_COLOR_HASH_ETH:
1121 /*
1122 * This is (presumably) a real Ethernet capture; give it a
1123 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1124 * that an application can let you choose it, in case you're
1125 * capturing DOCSIS traffic that a Cisco Cable Modem
1126 * Termination System is putting out onto an Ethernet (it
1127 * doesn't put an Ethernet header onto the wire, it puts raw
1128 * DOCSIS frames out on the wire inside the low-level
1129 * Ethernet framing).
1130 */
1131 if (p->dlt_list != NULL) {
1132 p->dlt_list[dlt_index++] = DLT_EN10MB;
1133 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1134 }
1135 if(!p->linktype)
1136 p->linktype = DLT_EN10MB;
1137 break;
1138
1139 case TYPE_ATM:
1140 case TYPE_AAL5:
1141 case TYPE_MC_ATM:
1142 case TYPE_MC_AAL5:
1143 if (p->dlt_list != NULL) {
1144 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1145 p->dlt_list[dlt_index++] = DLT_SUNATM;
1146 }
1147 if(!p->linktype)
1148 p->linktype = DLT_ATM_RFC1483;
1149 break;
1150
1151 case TYPE_COLOR_MC_HDLC_POS:
1152 case TYPE_MC_HDLC:
1153 if (p->dlt_list != NULL) {
1154 p->dlt_list[dlt_index++] = DLT_CHDLC;
1155 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1156 p->dlt_list[dlt_index++] = DLT_FRELAY;
1157 p->dlt_list[dlt_index++] = DLT_MTP2;
1158 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1159 p->dlt_list[dlt_index++] = DLT_LAPD;
1160 }
1161 if(!p->linktype)
1162 p->linktype = DLT_CHDLC;
1163 break;
1164
1165 case TYPE_IPV4:
1166 case TYPE_IPV6:
1167 if(!p->linktype)
1168 p->linktype = DLT_RAW;
1169 break;
1170
1171 case TYPE_LEGACY:
1172 case TYPE_MC_RAW:
1173 case TYPE_MC_RAW_CHANNEL:
1174 case TYPE_IP_COUNTER:
1175 case TYPE_TCP_FLOW_COUNTER:
1176 case TYPE_INFINIBAND:
1177 case TYPE_RAW_LINK:
1178 case TYPE_INFINIBAND_LINK:
1179 default:
1180 /* Libpcap cannot deal with these types yet */
1181 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1182 break;
1183
1184 } /* switch */
1185 index++;
1186 }
1187
1188 p->dlt_list[dlt_index++] = DLT_ERF;
1189
1190 p->dlt_count = dlt_index;
1191
1192 if(!p->linktype)
1193 p->linktype = DLT_ERF;
1194
1195 return p->linktype;
1196 }