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