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