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