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