]>
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.3 2008-12-02 16:40:19 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 #ifdef HAVE_SYS_BUFMOD_H
73 static void pcap_stream_err(const char *, int, char *);
77 * Get the packet statistics.
80 pcap_stats_dlpi(pcap_t
*p
, struct pcap_stat
*ps
)
84 * "ps_recv" counts packets handed to the filter, not packets
85 * that passed the filter. As filtering is done in userland,
86 * this would not include packets dropped because we ran out
87 * of buffer space; in order to make this more like other
88 * platforms (Linux 2.4 and later, BSDs with BPF), where the
89 * "packets received" count includes packets received but dropped
90 * due to running out of buffer space, and to keep from confusing
91 * applications that, for example, compute packet drop percentages,
92 * we also make it count packets dropped by "bufmod" (otherwise we
93 * might run the risk of the packet drop count being bigger than
94 * the received-packet count).
96 * "ps_drop" counts packets dropped by "bufmod" because of
97 * flow control requirements or resource exhaustion; it doesn't
98 * count packets dropped by the interface driver, or packets
99 * dropped upstream. As filtering is done in userland, it counts
100 * packets regardless of whether they would've passed the filter.
102 * These statistics don't include packets not yet read from
103 * the kernel by libpcap, but they may include packets not
104 * yet read from libpcap by the application.
109 * Add in the drop count, as per the above comment.
111 ps
->ps_recv
+= ps
->ps_drop
;
116 * Loop through the packets and call the callback for each packet.
117 * Return the number of packets read.
120 pcap_process_pkts(pcap_t
*p
, pcap_handler callback
, u_char
*user
,
121 int count
, u_char
*bufp
, int len
)
123 int n
, caplen
, origlen
;
125 struct pcap_pkthdr pkthdr
;
126 #ifdef HAVE_SYS_BUFMOD_H
133 /* Loop through packets */
137 #ifdef HAVE_SYS_BUFMOD_H
140 * Has "pcap_breakloop()" been called?
141 * If so, return immediately - if we haven't read any
142 * packets, clear the flag and return -2 to indicate
143 * that we were told to break out of the loop, otherwise
144 * leave the flag set, so that the *next* call will break
145 * out of the loop without having read any packets, and
146 * return the number of packets we've processed so far.
159 if ((long)bufp
& 3) {
161 memcpy(sbp
, bufp
, sizeof(*sbp
));
164 sbp
= (struct sb_hdr
*)bufp
;
165 p
->md
.stat
.ps_drop
= sbp
->sbh_drops
;
166 pk
= bufp
+ sizeof(*sbp
);
167 bufp
+= sbp
->sbh_totlen
;
168 origlen
= sbp
->sbh_origlen
;
169 caplen
= sbp
->sbh_msglen
;
172 caplen
= min(p
->snapshot
, len
);
176 ++p
->md
.stat
.ps_recv
;
177 if (bpf_filter(p
->fcode
.bf_insns
, pk
, origlen
, caplen
)) {
178 #ifdef HAVE_SYS_BUFMOD_H
179 pkthdr
.ts
.tv_sec
= sbp
->sbh_timestamp
.tv_sec
;
180 pkthdr
.ts
.tv_usec
= sbp
->sbh_timestamp
.tv_usec
;
182 (void) gettimeofday(&pkthdr
.ts
, NULL
);
184 pkthdr
.len
= origlen
;
185 pkthdr
.caplen
= caplen
;
186 /* Insure caplen does not exceed snapshot */
187 if (pkthdr
.caplen
> p
->snapshot
)
188 pkthdr
.caplen
= p
->snapshot
;
189 (*callback
)(user
, &pkthdr
, pk
);
190 if (++n
>= count
&& count
>= 0) {
196 #ifdef HAVE_SYS_BUFMOD_H
204 * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
207 pcap_process_mactype(pcap_t
*p
, u_int mactype
)
215 p
->linktype
= DLT_EN10MB
;
218 * This is (presumably) a real Ethernet capture; give it a
219 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
220 * that an application can let you choose it, in case you're
221 * capturing DOCSIS traffic that a Cisco Cable Modem
222 * Termination System is putting out onto an Ethernet (it
223 * doesn't put an Ethernet header onto the wire, it puts raw
224 * DOCSIS frames out on the wire inside the low-level
227 p
->dlt_list
= (u_int
*)malloc(sizeof(u_int
) * 2);
229 * If that fails, just leave the list empty.
231 if (p
->dlt_list
!= NULL
) {
232 p
->dlt_list
[0] = DLT_EN10MB
;
233 p
->dlt_list
[1] = DLT_DOCSIS
;
239 p
->linktype
= DLT_FDDI
;
244 /* XXX - what about DL_TPB? Is that Token Bus? */
245 p
->linktype
= DLT_IEEE802
;
251 p
->linktype
= DLT_SUNATM
;
252 p
->offset
= 0; /* works for LANE and LLC encapsulation */
257 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown mactype %u",
265 #ifdef HAVE_SYS_BUFMOD_H
267 * Push and configure the buffer module. Returns -1 for error, otherwise 0.
270 pcap_conf_bufmod(pcap_t
*p
, int snaplen
, int timeout
)
274 bpf_u_int32 ss
, chunksize
;
276 /* Non-standard call to get the data nicely buffered. */
277 if (ioctl(p
->fd
, I_PUSH
, "bufmod") != 0) {
278 pcap_stream_err("I_PUSH bufmod", errno
, p
->errbuf
);
284 strioctl(p
->fd
, SBIOCSSNAP
, sizeof(ss
), (char *)&ss
) != 0) {
285 pcap_stream_err("SBIOCSSNAP", errno
, p
->errbuf
);
289 /* Set up the bufmod timeout. */
293 to
.tv_sec
= timeout
/ 1000;
294 to
.tv_usec
= (timeout
* 1000) % 1000000;
295 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
296 pcap_stream_err("SBIOCSTIME", errno
, p
->errbuf
);
301 /* Set the chunk length. */
302 chunksize
= CHUNKSIZE
;
303 if (strioctl(p
->fd
, SBIOCSCHUNK
, sizeof(chunksize
), (char *)&chunksize
)
305 pcap_stream_err("SBIOCSCHUNKP", errno
, p
->errbuf
);
311 #endif /* HAVE_SYS_BUFMOD_H */
314 * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
317 pcap_alloc_databuf(pcap_t
*p
)
319 p
->bufsize
= PKTBUFSIZE
;
320 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
321 if (p
->buffer
== NULL
) {
322 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
330 * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
331 * length of returned data on success.
334 strioctl(int fd
, int cmd
, int len
, char *dp
)
343 if ((retv
= ioctl(fd
, I_STR
, &str
)) < 0)
349 #ifdef HAVE_SYS_BUFMOD_H
351 * Write stream error message to errbuf.
354 pcap_stream_err(const char *func
, int err
, char *errbuf
)
356 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", func
, pcap_strerror(err
));