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