]>
The Tcpdump Group git mirrors - libpcap/blob - dlpisubs.c
2 * This code is derived from code formerly in pcap-dlpi.c, originally
3 * contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), University College
4 * London, and subsequently modified by Guy Harris (guy@alum.mit.edu),
5 * Mark Pizzolato <List-tcpdump-workers@subscriptions.pizzolato.net>,
6 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
10 * This file contains dlpi/libdlpi related common functions used
11 * by pcap-[dlpi,libdlpi].c.
14 static const char rcsid
[] _U_
=
15 "@(#) $Header: /tcpdump/master/libpcap/dlpisubs.c,v 1.1.2.3 2008-12-02 16:44:30 guy Exp $ (LBL)";
23 #define DL_IPATM 0x12 /* ATM Classical IP interface */
26 #ifdef HAVE_SYS_BUFMOD_H
28 * Size of a bufmod chunk to pass upstream; that appears to be the
29 * biggest value to which you can set it, and setting it to that value
30 * (which is bigger than what appears to be the Solaris default of 8192)
31 * reduces the number of packet drops.
33 #define CHUNKSIZE 65536
36 * Size of the buffer to allocate for packet data we read; it must be
37 * large enough to hold a chunk.
39 #define PKTBUFSIZE CHUNKSIZE
41 #else /* HAVE_SYS_BUFMOD_H */
44 * Size of the buffer to allocate for packet data we read; this is
45 * what the value used to be - there's no particular reason why it
46 * should be tied to MAXDLBUF, but we'll leave it as this for now.
49 #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
53 #include <sys/types.h>
55 #ifdef HAVE_SYS_BUFMOD_H
56 #include <sys/bufmod.h>
59 #include <sys/stream.h>
72 static void pcap_stream_err(const char *, int, char *);
75 * Get the packet statistics.
78 pcap_stats_dlpi(pcap_t
*p
, struct pcap_stat
*ps
)
82 * "ps_recv" counts packets handed to the filter, not packets
83 * that passed the filter. As filtering is done in userland,
84 * this would not include packets dropped because we ran out
85 * of buffer space; in order to make this more like other
86 * platforms (Linux 2.4 and later, BSDs with BPF), where the
87 * "packets received" count includes packets received but dropped
88 * due to running out of buffer space, and to keep from confusing
89 * applications that, for example, compute packet drop percentages,
90 * we also make it count packets dropped by "bufmod" (otherwise we
91 * might run the risk of the packet drop count being bigger than
92 * the received-packet count).
94 * "ps_drop" counts packets dropped by "bufmod" because of
95 * flow control requirements or resource exhaustion; it doesn't
96 * count packets dropped by the interface driver, or packets
97 * dropped upstream. As filtering is done in userland, it counts
98 * packets regardless of whether they would've passed the filter.
100 * These statistics don't include packets not yet read from
101 * the kernel by libpcap, but they may include packets not
102 * yet read from libpcap by the application.
107 * Add in the drop count, as per the above comment.
109 ps
->ps_recv
+= ps
->ps_drop
;
114 * Loop through the packets and call the callback for each packet.
115 * Return the number of packets read.
118 pcap_process_pkts(pcap_t
*p
, pcap_handler callback
, u_char
*user
,
119 int count
, u_char
*bufp
, int len
)
121 int n
, caplen
, origlen
;
123 struct pcap_pkthdr pkthdr
;
124 #ifdef HAVE_SYS_BUFMOD_H
131 /* Loop through packets */
135 #ifdef HAVE_SYS_BUFMOD_H
138 * Has "pcap_breakloop()" been called?
139 * If so, return immediately - if we haven't read any
140 * packets, clear the flag and return -2 to indicate
141 * that we were told to break out of the loop, otherwise
142 * leave the flag set, so that the *next* call will break
143 * out of the loop without having read any packets, and
144 * return the number of packets we've processed so far.
157 if ((long)bufp
& 3) {
159 memcpy(sbp
, bufp
, sizeof(*sbp
));
162 sbp
= (struct sb_hdr
*)bufp
;
163 p
->md
.stat
.ps_drop
= sbp
->sbh_drops
;
164 pk
= bufp
+ sizeof(*sbp
);
165 bufp
+= sbp
->sbh_totlen
;
166 origlen
= sbp
->sbh_origlen
;
167 caplen
= sbp
->sbh_msglen
;
170 caplen
= min(p
->snapshot
, len
);
174 ++p
->md
.stat
.ps_recv
;
175 if (bpf_filter(p
->fcode
.bf_insns
, pk
, origlen
, caplen
)) {
176 #ifdef HAVE_SYS_BUFMOD_H
177 pkthdr
.ts
.tv_sec
= sbp
->sbh_timestamp
.tv_sec
;
178 pkthdr
.ts
.tv_usec
= sbp
->sbh_timestamp
.tv_usec
;
180 (void) gettimeofday(&pkthdr
.ts
, NULL
);
182 pkthdr
.len
= origlen
;
183 pkthdr
.caplen
= caplen
;
184 /* Insure caplen does not exceed snapshot */
185 if (pkthdr
.caplen
> p
->snapshot
)
186 pkthdr
.caplen
= p
->snapshot
;
187 (*callback
)(user
, &pkthdr
, pk
);
188 if (++n
>= count
&& count
>= 0) {
194 #ifdef HAVE_SYS_BUFMOD_H
202 * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
205 pcap_process_mactype(pcap_t
*p
, u_int mactype
)
213 p
->linktype
= DLT_EN10MB
;
216 * This is (presumably) a real Ethernet capture; give it a
217 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
218 * that an application can let you choose it, in case you're
219 * capturing DOCSIS traffic that a Cisco Cable Modem
220 * Termination System is putting out onto an Ethernet (it
221 * doesn't put an Ethernet header onto the wire, it puts raw
222 * DOCSIS frames out on the wire inside the low-level
225 p
->dlt_list
= (u_int
*)malloc(sizeof(u_int
) * 2);
227 * If that fails, just leave the list empty.
229 if (p
->dlt_list
!= NULL
) {
230 p
->dlt_list
[0] = DLT_EN10MB
;
231 p
->dlt_list
[1] = DLT_DOCSIS
;
237 p
->linktype
= DLT_FDDI
;
242 /* XXX - what about DL_TPB? Is that Token Bus? */
243 p
->linktype
= DLT_IEEE802
;
249 p
->linktype
= DLT_SUNATM
;
250 p
->offset
= 0; /* works for LANE and LLC encapsulation */
255 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown mactype %u",
263 #ifdef HAVE_SYS_BUFMOD_H
265 * Push and configure the buffer module. Returns -1 for error, otherwise 0.
268 pcap_conf_bufmod(pcap_t
*p
, int snaplen
, int timeout
)
272 bpf_u_int32 ss
, chunksize
;
274 /* Non-standard call to get the data nicely buffered. */
275 if (ioctl(p
->fd
, I_PUSH
, "bufmod") != 0) {
276 pcap_stream_err("I_PUSH bufmod", errno
, p
->errbuf
);
282 strioctl(p
->fd
, SBIOCSSNAP
, sizeof(ss
), (char *)&ss
) != 0) {
283 pcap_stream_err("SBIOCSSNAP", errno
, p
->errbuf
);
287 /* Set up the bufmod timeout. */
291 to
.tv_sec
= timeout
/ 1000;
292 to
.tv_usec
= (timeout
* 1000) % 1000000;
293 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
294 pcap_stream_err("SBIOCSTIME", errno
, p
->errbuf
);
299 /* Set the chunk length. */
300 chunksize
= CHUNKSIZE
;
301 if (strioctl(p
->fd
, SBIOCSCHUNK
, sizeof(chunksize
), (char *)&chunksize
)
303 pcap_stream_err("SBIOCSCHUNKP", errno
, p
->errbuf
);
309 #endif /* HAVE_SYS_BUFMOD_H */
312 * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
315 pcap_alloc_databuf(pcap_t
*p
)
317 p
->bufsize
= PKTBUFSIZE
;
318 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
319 if (p
->buffer
== NULL
) {
320 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
328 * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
329 * length of returned data on success.
332 strioctl(int fd
, int cmd
, int len
, char *dp
)
341 if ((retv
= ioctl(fd
, I_STR
, &str
)) < 0)
348 * Write stream error message to errbuf.
351 pcap_stream_err(const char *func
, int err
, char *errbuf
)
353 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", func
, pcap_strerror(err
));