+static int
+dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
+{
+ struct pcap_dag *pd = p->priv;
+ unsigned int processed = 0;
+ unsigned int nonblocking = pd->dag_flags & DAGF_NONBLOCK;
+ unsigned int num_ext_hdr = 0;
+ unsigned int ticks_per_second;
+
+ /* Get the next bufferful of packets (if necessary). */
+ while (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size) {
+
+ /*
+ * Has "pcap_breakloop()" been called?
+ */
+ if (p->break_loop) {
+ /*
+ * Yes - clear the flag that indicates that
+ * it has, and return -2 to indicate that
+ * we were told to break out of the loop.
+ */
+ p->break_loop = 0;
+ return -2;
+ }
+
+ /* dag_advance_stream() will block (unless nonblock is called)
+ * until 64kB of data has accumulated.
+ * If to_ms is set, it will timeout before 64kB has accumulated.
+ * We wait for 64kB because processing a few packets at a time
+ * can cause problems at high packet rates (>200kpps) due
+ * to inefficiencies.
+ * This does mean if to_ms is not specified the capture may 'hang'
+ * for long periods if the data rate is extremely slow (<64kB/sec)
+ * If non-block is specified it will return immediately. The user
+ * is then responsible for efficiency.
+ */
+ if ( NULL == (pd->dag_mem_top = dag_advance_stream(p->fd, pd->dag_stream, &(pd->dag_mem_bottom))) ) {
+ return -1;
+ }
+
+ if (nonblocking && (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
+ {
+ /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
+ return 0;
+ }
+
+ if(!nonblocking &&
+ pd->dag_timeout &&
+ (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
+ {
+ /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
+ return 0;
+ }
+
+ }
+
+ /*
+ * Process the packets.
+ *
+ * This assumes that a single buffer of packets will have
+ * <= INT_MAX packets, so the packet count doesn't overflow.
+ */
+ while (pd->dag_mem_top - pd->dag_mem_bottom >= dag_record_size) {
+
+ unsigned short packet_len = 0;
+ int caplen = 0;
+ struct pcap_pkthdr pcap_header;
+
+ dag_record_t *header = (dag_record_t *)(pd->dag_mem_bottom);
+
+ u_char *dp = ((u_char *)header); /* + dag_record_size; */
+ unsigned short rlen;
+
+ /*
+ * Has "pcap_breakloop()" been called?
+ */
+ if (p->break_loop) {
+ /*
+ * Yes - clear the flag that indicates that
+ * it has, and return -2 to indicate that
+ * we were told to break out of the loop.
+ */
+ p->break_loop = 0;
+ return -2;
+ }
+
+ rlen = ntohs(header->rlen);
+ if (rlen < dag_record_size)
+ {
+ pcap_strlcpy(p->errbuf, "dag_read: record too small",
+ PCAP_ERRBUF_SIZE);
+ return -1;
+ }
+ pd->dag_mem_bottom += rlen;
+
+ /* Count lost packets. */
+ switch((header->type & 0x7f)) {
+ /* in these types the color value overwrites the lctr */
+ case ERF_TYPE_COLOR_HDLC_POS:
+ case ERF_TYPE_COLOR_ETH:
+ case ERF_TYPE_DSM_COLOR_HDLC_POS:
+ case ERF_TYPE_DSM_COLOR_ETH:
+ case ERF_TYPE_COLOR_MC_HDLC_POS:
+ case ERF_TYPE_COLOR_HASH_ETH:
+ case ERF_TYPE_COLOR_HASH_POS:
+ break;
+
+ default:
+ if ( (pd->drop_attr == kNullAttributeUuid) && (header->lctr) ) {
+ pd->stat.ps_drop += ntohs(header->lctr);
+ }
+ }
+
+ if ((header->type & 0x7f) == ERF_TYPE_PAD) {
+ continue;
+ }
+
+ num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
+
+ /* ERF encapsulation */
+ /* The Extensible Record Format is not dropped for this kind of encapsulation,
+ * and will be handled as a pseudo header by the decoding application.
+ * The information carried in the ERF header and in the optional subheader (if present)
+ * could be merged with the libpcap information, to offer a better decoding.
+ * The packet length is
+ * o the length of the packet on the link (header->wlen),
+ * o plus the length of the ERF header (dag_record_size), as the length of the
+ * pseudo header will be adjusted during the decoding,
+ * o plus the length of the optional subheader (if present).
+ *
+ * The capture length is header.rlen and the byte stuffing for alignment will be dropped
+ * if the capture length is greater than the packet length.
+ */
+ if (p->linktype == DLT_ERF) {
+ packet_len = ntohs(header->wlen) + dag_record_size;
+ caplen = rlen;
+ switch ((header->type & 0x7f)) {
+ case ERF_TYPE_MC_AAL5:
+ case ERF_TYPE_MC_ATM:
+ case ERF_TYPE_MC_HDLC:
+ case ERF_TYPE_MC_RAW_CHANNEL:
+ case ERF_TYPE_MC_RAW:
+ case ERF_TYPE_MC_AAL2:
+ case ERF_TYPE_COLOR_MC_HDLC_POS:
+ packet_len += 4; /* MC header */
+ break;
+
+ case ERF_TYPE_COLOR_HASH_ETH:
+ case ERF_TYPE_DSM_COLOR_ETH:
+ case ERF_TYPE_COLOR_ETH:
+ case ERF_TYPE_ETH:
+ packet_len += 2; /* ETH header */
+ break;
+ } /* switch type */
+
+ /* Include ERF extension headers */
+ packet_len += (8 * num_ext_hdr);
+
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ } else {
+ /* Other kind of encapsulation according to the header Type */
+
+ /* Skip over generic ERF header */
+ dp += dag_record_size;
+ /* Skip over extension headers */
+ dp += 8 * num_ext_hdr;
+
+ switch((header->type & 0x7f)) {
+ case ERF_TYPE_ATM:
+ case ERF_TYPE_AAL5:
+ if ((header->type & 0x7f) == ERF_TYPE_AAL5) {
+ packet_len = ntohs(header->wlen);
+ caplen = rlen - dag_record_size;
+ }
+ case ERF_TYPE_MC_ATM:
+ if ((header->type & 0x7f) == ERF_TYPE_MC_ATM) {
+ caplen = packet_len = ATM_CELL_SIZE;
+ dp+=4;
+ }
+ case ERF_TYPE_MC_AAL5:
+ if ((header->type & 0x7f) == ERF_TYPE_MC_AAL5) {
+ packet_len = ntohs(header->wlen);
+ caplen = rlen - dag_record_size - 4;
+ dp+=4;
+ }
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+
+ if ((header->type & 0x7f) == ERF_TYPE_ATM) {
+ caplen = packet_len = ATM_CELL_SIZE;
+ }
+ if (p->linktype == DLT_SUNATM) {
+ struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
+ unsigned long rawatm;
+
+ rawatm = ntohl(*((unsigned long *)dp));
+ sunatm->vci = htons((rawatm >> 4) & 0xffff);
+ sunatm->vpi = (rawatm >> 20) & 0x00ff;
+ sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
+ ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
+ ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
+ ((dp[ATM_HDR_SIZE] == 0xaa &&
+ dp[ATM_HDR_SIZE+1] == 0xaa &&
+ dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
+
+ } else if (p->linktype == DLT_ATM_RFC1483) {
+ packet_len -= ATM_HDR_SIZE;
+ caplen -= ATM_HDR_SIZE;
+ dp += ATM_HDR_SIZE;
+ } else
+ continue;
+ break;
+
+ case ERF_TYPE_COLOR_HASH_ETH:
+ case ERF_TYPE_DSM_COLOR_ETH:
+ case ERF_TYPE_COLOR_ETH:
+ case ERF_TYPE_ETH:
+ if ((p->linktype != DLT_EN10MB) &&
+ (p->linktype != DLT_DOCSIS))
+ continue;
+ packet_len = ntohs(header->wlen);
+ packet_len -= (pd->dag_fcs_bits >> 3);
+ caplen = rlen - dag_record_size - 2;
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ dp += 2;
+ break;
+
+ case ERF_TYPE_COLOR_HASH_POS:
+ case ERF_TYPE_DSM_COLOR_HDLC_POS:
+ case ERF_TYPE_COLOR_HDLC_POS:
+ case ERF_TYPE_HDLC_POS:
+ if ((p->linktype != DLT_CHDLC) &&
+ (p->linktype != DLT_PPP_SERIAL) &&
+ (p->linktype != DLT_FRELAY))
+ continue;
+ packet_len = ntohs(header->wlen);
+ packet_len -= (pd->dag_fcs_bits >> 3);
+ caplen = rlen - dag_record_size;
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ break;
+
+ case ERF_TYPE_COLOR_MC_HDLC_POS:
+ case ERF_TYPE_MC_HDLC:
+ if ((p->linktype != DLT_CHDLC) &&
+ (p->linktype != DLT_PPP_SERIAL) &&
+ (p->linktype != DLT_FRELAY) &&
+ (p->linktype != DLT_MTP2) &&
+ (p->linktype != DLT_MTP2_WITH_PHDR) &&
+ (p->linktype != DLT_LAPD))
+ continue;
+ packet_len = ntohs(header->wlen);
+ packet_len -= (pd->dag_fcs_bits >> 3);
+ caplen = rlen - dag_record_size - 4;
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ /* jump the MC_HDLC_HEADER */
+ dp += 4;
+#ifdef DLT_MTP2_WITH_PHDR
+ if (p->linktype == DLT_MTP2_WITH_PHDR) {
+ /* Add the MTP2 Pseudo Header */
+ caplen += MTP2_HDR_LEN;
+ packet_len += MTP2_HDR_LEN;
+
+ TempPkt[MTP2_SENT_OFFSET] = 0;
+ TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
+ *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
+ *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
+ memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
+ dp = TempPkt;
+ }
+#endif
+ break;
+
+ case ERF_TYPE_IPV4:
+ if ((p->linktype != DLT_RAW) &&
+ (p->linktype != DLT_IPV4))
+ continue;
+ packet_len = ntohs(header->wlen);
+ caplen = rlen - dag_record_size;
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ break;
+
+ case ERF_TYPE_IPV6:
+ if ((p->linktype != DLT_RAW) &&
+ (p->linktype != DLT_IPV6))
+ continue;
+ packet_len = ntohs(header->wlen);
+ caplen = rlen - dag_record_size;
+ /* Skip over extension headers */
+ caplen -= (8 * num_ext_hdr);
+ if (caplen > packet_len) {
+ caplen = packet_len;
+ }
+ break;
+
+ /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
+ case ERF_TYPE_MC_RAW:
+ case ERF_TYPE_MC_RAW_CHANNEL:
+ case ERF_TYPE_IP_COUNTER:
+ case ERF_TYPE_TCP_FLOW_COUNTER:
+ case ERF_TYPE_INFINIBAND:
+ case ERF_TYPE_RAW_LINK:
+ case ERF_TYPE_INFINIBAND_LINK:
+ default:
+ /* Unhandled ERF type.
+ * Ignore rather than generating error
+ */
+ continue;
+ } /* switch type */
+
+ } /* ERF encapsulation */
+
+ if (caplen > p->snapshot)
+ caplen = p->snapshot;
+
+ /* Run the packet filter if there is one. */
+ if ((p->fcode.bf_insns == NULL) || pcap_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
+
+ /* convert between timestamp formats */
+ register unsigned long long ts;
+
+ if (IS_BIGENDIAN()) {
+ ts = SWAPLL(header->ts);
+ } else {
+ ts = header->ts;
+ }
+
+ switch (p->opt.tstamp_precision) {
+ case PCAP_TSTAMP_PRECISION_NANO:
+ ticks_per_second = 1000000000;
+ break;
+ case PCAP_TSTAMP_PRECISION_MICRO:
+ default:
+ ticks_per_second = 1000000;
+ break;
+
+ }
+ pcap_header.ts.tv_sec = ts >> 32;
+ ts = (ts & 0xffffffffULL) * ticks_per_second;
+ ts += 0x80000000; /* rounding */
+ pcap_header.ts.tv_usec = ts >> 32;
+ if (pcap_header.ts.tv_usec >= ticks_per_second) {
+ pcap_header.ts.tv_usec -= ticks_per_second;
+ pcap_header.ts.tv_sec++;
+ }
+
+ /* Fill in our own header data */
+ pcap_header.caplen = caplen;
+ pcap_header.len = packet_len;
+
+ /* Count the packet. */
+ pd->stat.ps_recv++;
+
+ /* Call the user supplied callback function */
+ callback(user, &pcap_header, dp);
+
+ /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
+ processed++;
+ if (processed == cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
+ {
+ /* Reached the user-specified limit. */
+ return cnt;
+ }
+ }
+ }
+
+ return processed;
+}
+
+static int
+dag_inject(pcap_t *p, const void *buf _U_, int size _U_)
+{
+ pcap_strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
+ PCAP_ERRBUF_SIZE);
+ return (-1);