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