]> The Tcpdump Group git mirrors - libpcap/blob - dlpisubs.c
Another change from Debian.
[libpcap] / dlpisubs.c
1 /*
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>.
7 */
8
9 /*
10 * This file contains dlpi/libdlpi related common functions used
11 * by pcap-[dlpi,libdlpi].c.
12 */
13 #ifndef lint
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)";
16 #endif
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #ifndef DL_IPATM
23 #define DL_IPATM 0x12 /* ATM Classical IP interface */
24 #endif
25
26 #ifdef HAVE_SYS_BUFMOD_H
27 /*
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.
32 */
33 #define CHUNKSIZE 65536
34
35 /*
36 * Size of the buffer to allocate for packet data we read; it must be
37 * large enough to hold a chunk.
38 */
39 #define PKTBUFSIZE CHUNKSIZE
40
41 #else /* HAVE_SYS_BUFMOD_H */
42
43 /*
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.
47 */
48 #define MAXDLBUF 8192
49 #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
50
51 #endif
52
53 #include <sys/types.h>
54 #include <sys/time.h>
55 #ifdef HAVE_SYS_BUFMOD_H
56 #include <sys/bufmod.h>
57 #endif
58 #include <sys/dlpi.h>
59 #include <sys/stream.h>
60
61 #include <errno.h>
62 #include <memory.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stropts.h>
67 #include <unistd.h>
68
69 #include "pcap-int.h"
70 #include "dlpisubs.h"
71
72 static void pcap_stream_err(const char *, int, char *);
73
74 /*
75 * Get the packet statistics.
76 */
77 int
78 pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)
79 {
80
81 /*
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).
93 *
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.
99 *
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.
103 */
104 *ps = p->md.stat;
105
106 /*
107 * Add in the drop count, as per the above comment.
108 */
109 ps->ps_recv += ps->ps_drop;
110 return (0);
111 }
112
113 /*
114 * Loop through the packets and call the callback for each packet.
115 * Return the number of packets read.
116 */
117 int
118 pcap_process_pkts(pcap_t *p, pcap_handler callback, u_char *user,
119 int count, u_char *bufp, int len)
120 {
121 int n, caplen, origlen;
122 u_char *ep, *pk;
123 struct pcap_pkthdr pkthdr;
124 #ifdef HAVE_SYS_BUFMOD_H
125 struct sb_hdr *sbp;
126 #ifdef LBL_ALIGN
127 struct sb_hdr sbhdr;
128 #endif
129 #endif
130
131 /* Loop through packets */
132 ep = bufp + len;
133 n = 0;
134
135 #ifdef HAVE_SYS_BUFMOD_H
136 while (bufp < ep) {
137 /*
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.
145 */
146 if (p->break_loop) {
147 if (n == 0) {
148 p->break_loop = 0;
149 return (-2);
150 } else {
151 p->bp = bufp;
152 p->cc = ep - bufp;
153 return (n);
154 }
155 }
156 #ifdef LBL_ALIGN
157 if ((long)bufp & 3) {
158 sbp = &sbhdr;
159 memcpy(sbp, bufp, sizeof(*sbp));
160 } else
161 #endif
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;
168 #else
169 origlen = len;
170 caplen = min(p->snapshot, len);
171 pk = bufp;
172 bufp += caplen;
173 #endif
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;
179 #else
180 (void) gettimeofday(&pkthdr.ts, NULL);
181 #endif
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) {
189 p->cc = ep - bufp;
190 p->bp = bufp;
191 return (n);
192 }
193 }
194 #ifdef HAVE_SYS_BUFMOD_H
195 }
196 #endif
197 p->cc = 0;
198 return (n);
199 }
200
201 /*
202 * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
203 */
204 int
205 pcap_process_mactype(pcap_t *p, u_int mactype)
206 {
207 int retv = 0;
208
209 switch (mactype) {
210
211 case DL_CSMACD:
212 case DL_ETHER:
213 p->linktype = DLT_EN10MB;
214 p->offset = 2;
215 /*
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
223 * Ethernet framing).
224 */
225 p->dlt_list = (u_int *)malloc(sizeof(u_int) * 2);
226 /*
227 * If that fails, just leave the list empty.
228 */
229 if (p->dlt_list != NULL) {
230 p->dlt_list[0] = DLT_EN10MB;
231 p->dlt_list[1] = DLT_DOCSIS;
232 p->dlt_count = 2;
233 }
234 break;
235
236 case DL_FDDI:
237 p->linktype = DLT_FDDI;
238 p->offset = 3;
239 break;
240
241 case DL_TPR:
242 /* XXX - what about DL_TPB? Is that Token Bus? */
243 p->linktype = DLT_IEEE802;
244 p->offset = 2;
245 break;
246
247 #ifdef HAVE_SOLARIS
248 case DL_IPATM:
249 p->linktype = DLT_SUNATM;
250 p->offset = 0; /* works for LANE and LLC encapsulation */
251 break;
252 #endif
253
254 default:
255 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown mactype %u",
256 mactype);
257 retv = -1;
258 }
259
260 return (retv);
261 }
262
263 #ifdef HAVE_SYS_BUFMOD_H
264 /*
265 * Push and configure the buffer module. Returns -1 for error, otherwise 0.
266 */
267 int
268 pcap_conf_bufmod(pcap_t *p, int snaplen, int timeout)
269 {
270 int retv = 0;
271
272 bpf_u_int32 ss, chunksize;
273
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);
277 retv = -1;
278 }
279
280 ss = snaplen;
281 if (ss > 0 &&
282 strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
283 pcap_stream_err("SBIOCSSNAP", errno, p->errbuf);
284 retv = -1;
285 }
286
287 /* Set up the bufmod timeout. */
288 if (timeout != 0) {
289 struct timeval to;
290
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);
295 retv = -1;
296 }
297 }
298
299 /* Set the chunk length. */
300 chunksize = CHUNKSIZE;
301 if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize)
302 != 0) {
303 pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf);
304 retv = -1;
305 }
306
307 return (retv);
308 }
309 #endif /* HAVE_SYS_BUFMOD_H */
310
311 /*
312 * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
313 */
314 int
315 pcap_alloc_databuf(pcap_t *p)
316 {
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);
321 return (-1);
322 }
323
324 return (0);
325 }
326
327 /*
328 * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
329 * length of returned data on success.
330 */
331 int
332 strioctl(int fd, int cmd, int len, char *dp)
333 {
334 struct strioctl str;
335 int retv;
336
337 str.ic_cmd = cmd;
338 str.ic_timout = -1;
339 str.ic_len = len;
340 str.ic_dp = dp;
341 if ((retv = ioctl(fd, I_STR, &str)) < 0)
342 return (retv);
343
344 return (str.ic_len);
345 }
346
347 /*
348 * Write stream error message to errbuf.
349 */
350 static void
351 pcap_stream_err(const char *func, int err, char *errbuf)
352 {
353 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", func, pcap_strerror(err));
354 }