1 /* $NetBSD: dlpisubs.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
4 * This code is derived from code formerly in pcap-dlpi.c, originally
5 * contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), University College
6 * London, and subsequently modified by Guy Harris (guy@alum.mit.edu),
7 * Mark Pizzolato <List-tcpdump-workers@subscriptions.pizzolato.net>,
8 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
12 * This file contains dlpi/libdlpi related common functions used
13 * by pcap-[dlpi,libdlpi].c.
16 #include <sys/cdefs.h>
17 __RCSID("$NetBSD: dlpisubs.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
24 #define DL_IPATM 0x12 /* ATM Classical IP interface */
27 #ifdef HAVE_SYS_BUFMOD_H
29 * Size of a bufmod chunk to pass upstream; that appears to be the
30 * biggest value to which you can set it, and setting it to that value
31 * (which is bigger than what appears to be the Solaris default of 8192)
32 * reduces the number of packet drops.
34 #define CHUNKSIZE 65536
37 * Size of the buffer to allocate for packet data we read; it must be
38 * large enough to hold a chunk.
40 #define PKTBUFSIZE CHUNKSIZE
42 #else /* HAVE_SYS_BUFMOD_H */
45 * Size of the buffer to allocate for packet data we read; this is
46 * what the value used to be - there's no particular reason why it
47 * should be tied to MAXDLBUF, but we'll leave it as this for now.
50 #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
54 #include <sys/types.h>
56 #ifdef HAVE_SYS_BUFMOD_H
57 #include <sys/bufmod.h>
60 #include <sys/stream.h>
77 #ifdef HAVE_SYS_BUFMOD_H
78 static void pcap_stream_err(const char *, int, char *);
82 * Get the packet statistics.
85 pcap_stats_dlpi(pcap_t
*p
, struct pcap_stat
*ps
)
87 struct pcap_dlpi
*pd
= p
->priv
;
90 * "ps_recv" counts packets handed to the filter, not packets
91 * that passed the filter. As filtering is done in userland,
92 * this would not include packets dropped because we ran out
93 * of buffer space; in order to make this more like other
94 * platforms (Linux 2.4 and later, BSDs with BPF), where the
95 * "packets received" count includes packets received but dropped
96 * due to running out of buffer space, and to keep from confusing
97 * applications that, for example, compute packet drop percentages,
98 * we also make it count packets dropped by "bufmod" (otherwise we
99 * might run the risk of the packet drop count being bigger than
100 * the received-packet count).
102 * "ps_drop" counts packets dropped by "bufmod" because of
103 * flow control requirements or resource exhaustion; it doesn't
104 * count packets dropped by the interface driver, or packets
105 * dropped upstream. As filtering is done in userland, it counts
106 * packets regardless of whether they would've passed the filter.
108 * These statistics don't include packets not yet read from
109 * the kernel by libpcap, but they may include packets not
110 * yet read from libpcap by the application.
115 * Add in the drop count, as per the above comment.
117 ps
->ps_recv
+= ps
->ps_drop
;
122 * Loop through the packets and call the callback for each packet.
123 * Return the number of packets read.
126 pcap_process_pkts(pcap_t
*p
, pcap_handler callback
, u_char
*user
,
127 int count
, u_char
*bufp
, int len
)
129 struct pcap_dlpi
*pd
= p
->priv
;
130 int n
, caplen
, origlen
;
132 struct pcap_pkthdr pkthdr
;
133 #ifdef HAVE_SYS_BUFMOD_H
140 /* Loop through packets */
144 #ifdef HAVE_SYS_BUFMOD_H
147 * Has "pcap_breakloop()" been called?
148 * If so, return immediately - if we haven't read any
149 * packets, clear the flag and return -2 to indicate
150 * that we were told to break out of the loop, otherwise
151 * leave the flag set, so that the *next* call will break
152 * out of the loop without having read any packets, and
153 * return the number of packets we've processed so far.
166 if ((long)bufp
& 3) {
168 memcpy(sbp
, bufp
, sizeof(*sbp
));
171 sbp
= (struct sb_hdr
*)bufp
;
172 pd
->stat
.ps_drop
= sbp
->sbh_drops
;
173 pk
= bufp
+ sizeof(*sbp
);
174 bufp
+= sbp
->sbh_totlen
;
175 origlen
= sbp
->sbh_origlen
;
176 caplen
= sbp
->sbh_msglen
;
179 caplen
= min(p
->snapshot
, len
);
184 if (bpf_filter(p
->fcode
.bf_insns
, pk
, origlen
, caplen
)) {
185 #ifdef HAVE_SYS_BUFMOD_H
186 pkthdr
.ts
.tv_sec
= sbp
->sbh_timestamp
.tv_sec
;
187 pkthdr
.ts
.tv_usec
= sbp
->sbh_timestamp
.tv_usec
;
189 (void) gettimeofday(&pkthdr
.ts
, NULL
);
191 pkthdr
.len
= origlen
;
192 pkthdr
.caplen
= caplen
;
193 /* Insure caplen does not exceed snapshot */
194 if (pkthdr
.caplen
> p
->snapshot
)
195 pkthdr
.caplen
= p
->snapshot
;
196 (*callback
)(user
, &pkthdr
, pk
);
197 if (++n
>= count
&& !PACKET_COUNT_IS_UNLIMITED(count
)) {
203 #ifdef HAVE_SYS_BUFMOD_H
211 * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
214 pcap_process_mactype(pcap_t
*p
, u_int mactype
)
222 p
->linktype
= DLT_EN10MB
;
225 * This is (presumably) a real Ethernet capture; give it a
226 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
227 * that an application can let you choose it, in case you're
228 * capturing DOCSIS traffic that a Cisco Cable Modem
229 * Termination System is putting out onto an Ethernet (it
230 * doesn't put an Ethernet header onto the wire, it puts raw
231 * DOCSIS frames out on the wire inside the low-level
234 p
->dlt_list
= (u_int
*)malloc(sizeof(u_int
) * 2);
236 * If that fails, just leave the list empty.
238 if (p
->dlt_list
!= NULL
) {
239 p
->dlt_list
[0] = DLT_EN10MB
;
240 p
->dlt_list
[1] = DLT_DOCSIS
;
246 p
->linktype
= DLT_FDDI
;
251 /* XXX - what about DL_TPB? Is that Token Bus? */
252 p
->linktype
= DLT_IEEE802
;
258 p
->linktype
= DLT_SUNATM
;
259 p
->offset
= 0; /* works for LANE and LLC encapsulation */
264 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown mactype %u",
272 #ifdef HAVE_SYS_BUFMOD_H
274 * Push and configure the buffer module. Returns -1 for error, otherwise 0.
277 pcap_conf_bufmod(pcap_t
*p
, int snaplen
)
280 bpf_u_int32 ss
, chunksize
;
282 /* Non-standard call to get the data nicely buffered. */
283 if (ioctl(p
->fd
, I_PUSH
, "bufmod") != 0) {
284 pcap_stream_err("I_PUSH bufmod", errno
, p
->errbuf
);
290 strioctl(p
->fd
, SBIOCSSNAP
, sizeof(ss
), (char *)&ss
) != 0) {
291 pcap_stream_err("SBIOCSSNAP", errno
, p
->errbuf
);
295 if (p
->opt
.immediate
) {
296 /* Set the timeout to zero, for immediate delivery. */
299 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
300 pcap_stream_err("SBIOCSTIME", errno
, p
->errbuf
);
304 /* Set up the bufmod timeout. */
305 if (p
->opt
.timeout
!= 0) {
306 to
.tv_sec
= p
->opt
.timeout
/ 1000;
307 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
308 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
309 pcap_stream_err("SBIOCSTIME", errno
, p
->errbuf
);
314 /* Set the chunk length. */
315 chunksize
= CHUNKSIZE
;
316 if (strioctl(p
->fd
, SBIOCSCHUNK
, sizeof(chunksize
), (char *)&chunksize
)
318 pcap_stream_err("SBIOCSCHUNKP", errno
, p
->errbuf
);
325 #endif /* HAVE_SYS_BUFMOD_H */
328 * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
331 pcap_alloc_databuf(pcap_t
*p
)
333 p
->bufsize
= PKTBUFSIZE
;
334 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
335 if (p
->buffer
== NULL
) {
336 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
344 * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
345 * length of returned data on success.
348 strioctl(int fd
, int cmd
, int len
, char *dp
)
357 if ((retv
= ioctl(fd
, I_STR
, &str
)) < 0)
363 #ifdef HAVE_SYS_BUFMOD_H
365 * Write stream error message to errbuf.
368 pcap_stream_err(const char *func
, int err
, char *errbuf
)
370 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", func
, pcap_strerror(err
));