xref: /netbsd-src/external/bsd/libpcap/dist/dlpisubs.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: dlpisubs.c,v 1.1.1.4 2013/12/31 16:57:23 christos Exp $	*/
2 
3 /*
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>.
9  */
10 
11 /*
12  * This file contains dlpi/libdlpi related common functions used
13  * by pcap-[dlpi,libdlpi].c.
14  */
15 #ifndef lint
16 static const char rcsid[] _U_ =
17 	"@(#) Header: /tcpdump/master/libpcap/dlpisubs.c,v 1.3 2008-12-02 16:40:19 guy Exp  (LBL)";
18 #endif
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #ifndef DL_IPATM
25 #define DL_IPATM	0x12	/* ATM Classical IP interface */
26 #endif
27 
28 #ifdef HAVE_SYS_BUFMOD_H
29 	/*
30 	 * Size of a bufmod chunk to pass upstream; that appears to be the
31 	 * biggest value to which you can set it, and setting it to that value
32 	 * (which is bigger than what appears to be the Solaris default of 8192)
33 	 * reduces the number of packet drops.
34 	 */
35 #define	CHUNKSIZE	65536
36 
37 	/*
38 	 * Size of the buffer to allocate for packet data we read; it must be
39 	 * large enough to hold a chunk.
40 	 */
41 #define	PKTBUFSIZE	CHUNKSIZE
42 
43 #else /* HAVE_SYS_BUFMOD_H */
44 
45 	/*
46 	 * Size of the buffer to allocate for packet data we read; this is
47 	 * what the value used to be - there's no particular reason why it
48 	 * should be tied to MAXDLBUF, but we'll leave it as this for now.
49 	 */
50 #define	MAXDLBUF	8192
51 #define	PKTBUFSIZE	(MAXDLBUF * sizeof(bpf_u_int32))
52 
53 #endif
54 
55 #include <sys/types.h>
56 #include <sys/time.h>
57 #ifdef HAVE_SYS_BUFMOD_H
58 #include <sys/bufmod.h>
59 #endif
60 #include <sys/dlpi.h>
61 #include <sys/stream.h>
62 
63 #include <errno.h>
64 #include <memory.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <stropts.h>
69 #include <unistd.h>
70 
71 #ifdef HAVE_LIBDLPI
72 #include <libdlpi.h>
73 #endif
74 
75 #include "pcap-int.h"
76 #include "dlpisubs.h"
77 
78 #ifdef HAVE_SYS_BUFMOD_H
79 static void pcap_stream_err(const char *, int, char *);
80 #endif
81 
82 /*
83  * Get the packet statistics.
84  */
85 int
86 pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)
87 {
88 	struct pcap_dlpi *pd = p->priv;
89 
90 	/*
91 	 * "ps_recv" counts packets handed to the filter, not packets
92 	 * that passed the filter.  As filtering is done in userland,
93 	 * this would not include packets dropped because we ran out
94 	 * of buffer space; in order to make this more like other
95 	 * platforms (Linux 2.4 and later, BSDs with BPF), where the
96 	 * "packets received" count includes packets received but dropped
97 	 * due to running out of buffer space, and to keep from confusing
98 	 * applications that, for example, compute packet drop percentages,
99 	 * we also make it count packets dropped by "bufmod" (otherwise we
100 	 * might run the risk of the packet drop count being bigger than
101 	 * the received-packet count).
102 	 *
103 	 * "ps_drop" counts packets dropped by "bufmod" because of
104 	 * flow control requirements or resource exhaustion; it doesn't
105 	 * count packets dropped by the interface driver, or packets
106 	 * dropped upstream.  As filtering is done in userland, it counts
107 	 * packets regardless of whether they would've passed the filter.
108 	 *
109 	 * These statistics don't include packets not yet read from
110 	 * the kernel by libpcap, but they may include packets not
111 	 * yet read from libpcap by the application.
112 	 */
113 	*ps = pd->stat;
114 
115 	/*
116 	 * Add in the drop count, as per the above comment.
117 	 */
118 	ps->ps_recv += ps->ps_drop;
119 	return (0);
120 }
121 
122 /*
123  * Loop through the packets and call the callback for each packet.
124  * Return the number of packets read.
125  */
126 int
127 pcap_process_pkts(pcap_t *p, pcap_handler callback, u_char *user,
128 	int count, u_char *bufp, int len)
129 {
130 	struct pcap_dlpi *pd = p->priv;
131 	int n, caplen, origlen;
132 	u_char *ep, *pk;
133 	struct pcap_pkthdr pkthdr;
134 #ifdef HAVE_SYS_BUFMOD_H
135 	struct sb_hdr *sbp;
136 #ifdef LBL_ALIGN
137 	struct sb_hdr sbhdr;
138 #endif
139 #endif
140 
141 	/* Loop through packets */
142 	ep = bufp + len;
143 	n = 0;
144 
145 #ifdef HAVE_SYS_BUFMOD_H
146 	while (bufp < ep) {
147 		/*
148 		 * Has "pcap_breakloop()" been called?
149 		 * If so, return immediately - if we haven't read any
150 		 * packets, clear the flag and return -2 to indicate
151 		 * that we were told to break out of the loop, otherwise
152 		 * leave the flag set, so that the *next* call will break
153 		 * out of the loop without having read any packets, and
154 		 * return the number of packets we've processed so far.
155 		 */
156 		if (p->break_loop) {
157 			if (n == 0) {
158 				p->break_loop = 0;
159 				return (-2);
160 			} else {
161 				p->bp = bufp;
162 				p->cc = ep - bufp;
163 				return (n);
164 			}
165 		}
166 #ifdef LBL_ALIGN
167 		if ((long)bufp & 3) {
168 			sbp = &sbhdr;
169 			memcpy(sbp, bufp, sizeof(*sbp));
170 		} else
171 #endif
172 			sbp = (struct sb_hdr *)bufp;
173 		pd->stat.ps_drop = sbp->sbh_drops;
174 		pk = bufp + sizeof(*sbp);
175 		bufp += sbp->sbh_totlen;
176 		origlen = sbp->sbh_origlen;
177 		caplen = sbp->sbh_msglen;
178 #else
179 		origlen = len;
180 		caplen = min(p->snapshot, len);
181 		pk = bufp;
182 		bufp += caplen;
183 #endif
184 		++pd->stat.ps_recv;
185 		if (bpf_filter(p->fcode.bf_insns, pk, origlen, caplen)) {
186 #ifdef HAVE_SYS_BUFMOD_H
187 			pkthdr.ts.tv_sec = sbp->sbh_timestamp.tv_sec;
188 			pkthdr.ts.tv_usec = sbp->sbh_timestamp.tv_usec;
189 #else
190 			(void) gettimeofday(&pkthdr.ts, NULL);
191 #endif
192 			pkthdr.len = origlen;
193 			pkthdr.caplen = caplen;
194 			/* Insure caplen does not exceed snapshot */
195 			if (pkthdr.caplen > p->snapshot)
196 				pkthdr.caplen = p->snapshot;
197 			(*callback)(user, &pkthdr, pk);
198 			if (++n >= count && count >= 0) {
199 				p->cc = ep - bufp;
200 				p->bp = bufp;
201 				return (n);
202 			}
203 		}
204 #ifdef HAVE_SYS_BUFMOD_H
205 	}
206 #endif
207 	p->cc = 0;
208 	return (n);
209 }
210 
211 /*
212  * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
213  */
214 int
215 pcap_process_mactype(pcap_t *p, u_int mactype)
216 {
217 	int retv = 0;
218 
219 	switch (mactype) {
220 
221 	case DL_CSMACD:
222 	case DL_ETHER:
223 		p->linktype = DLT_EN10MB;
224 		p->offset = 2;
225 		/*
226 		 * This is (presumably) a real Ethernet capture; give it a
227 		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
228 		 * that an application can let you choose it, in case you're
229 		 * capturing DOCSIS traffic that a Cisco Cable Modem
230 		 * Termination System is putting out onto an Ethernet (it
231 		 * doesn't put an Ethernet header onto the wire, it puts raw
232 		 * DOCSIS frames out on the wire inside the low-level
233 		 * Ethernet framing).
234 		 */
235 		p->dlt_list = (u_int *)malloc(sizeof(u_int) * 2);
236 		/*
237 		 * If that fails, just leave the list empty.
238 		 */
239 		if (p->dlt_list != NULL) {
240 			p->dlt_list[0] = DLT_EN10MB;
241 			p->dlt_list[1] = DLT_DOCSIS;
242 			p->dlt_count = 2;
243 		}
244 		break;
245 
246 	case DL_FDDI:
247 		p->linktype = DLT_FDDI;
248 		p->offset = 3;
249 		break;
250 
251 	case DL_TPR:
252 		/* XXX - what about DL_TPB?  Is that Token Bus?  */
253 		p->linktype = DLT_IEEE802;
254 		p->offset = 2;
255 		break;
256 
257 #ifdef HAVE_SOLARIS
258 	case DL_IPATM:
259 		p->linktype = DLT_SUNATM;
260 		p->offset = 0;  /* works for LANE and LLC encapsulation */
261 		break;
262 #endif
263 
264 	default:
265 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown mactype %u",
266 		    mactype);
267 		retv = -1;
268 	}
269 
270 	return (retv);
271 }
272 
273 #ifdef HAVE_SYS_BUFMOD_H
274 /*
275  * Push and configure the buffer module. Returns -1 for error, otherwise 0.
276  */
277 int
278 pcap_conf_bufmod(pcap_t *p, int snaplen)
279 {
280 	struct timeval to;
281 	bpf_u_int32 ss, chunksize;
282 
283 	/* Non-standard call to get the data nicely buffered. */
284 	if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
285 		pcap_stream_err("I_PUSH bufmod", errno, p->errbuf);
286 		return (-1);
287 	}
288 
289 	ss = snaplen;
290 	if (ss > 0 &&
291 	    strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
292 		pcap_stream_err("SBIOCSSNAP", errno, p->errbuf);
293 		return (-1);
294 	}
295 
296 	if (p->opt.immediate) {
297 		/* Set the timeout to zero, for immediate delivery. */
298 		to.tv_sec = 0;
299 		to.tv_usec = 0;
300 		if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
301 			pcap_stream_err("SBIOCSTIME", errno, p->errbuf);
302 			return (-1);
303 		}
304 	} else {
305 		/* Set up the bufmod timeout. */
306 		if (p->opt.timeout != 0) {
307 			to.tv_sec = p->opt.timeout / 1000;
308 			to.tv_usec = (p->opt.timeout * 1000) % 1000000;
309 			if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
310 				pcap_stream_err("SBIOCSTIME", errno, p->errbuf);
311 				return (-1);
312 			}
313 		}
314 
315 		/* Set the chunk length. */
316 		chunksize = CHUNKSIZE;
317 		if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize)
318 		    != 0) {
319 			pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf);
320 			return (-1);
321 		}
322 	}
323 
324 	return (0);
325 }
326 #endif /* HAVE_SYS_BUFMOD_H */
327 
328 /*
329  * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
330  */
331 int
332 pcap_alloc_databuf(pcap_t *p)
333 {
334 	p->bufsize = PKTBUFSIZE;
335 	p->buffer = (u_char *)malloc(p->bufsize + p->offset);
336 	if (p->buffer == NULL) {
337 		strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
338 		return (-1);
339 	}
340 
341 	return (0);
342 }
343 
344 /*
345  * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
346  * length of returned data on success.
347  */
348 int
349 strioctl(int fd, int cmd, int len, char *dp)
350 {
351 	struct strioctl str;
352 	int retv;
353 
354 	str.ic_cmd = cmd;
355 	str.ic_timout = -1;
356 	str.ic_len = len;
357 	str.ic_dp = dp;
358 	if ((retv = ioctl(fd, I_STR, &str)) < 0)
359 		return (retv);
360 
361 	return (str.ic_len);
362 }
363 
364 #ifdef HAVE_SYS_BUFMOD_H
365 /*
366  * Write stream error message to errbuf.
367  */
368 static void
369 pcap_stream_err(const char *func, int err, char *errbuf)
370 {
371 	snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", func, pcap_strerror(err));
372 }
373 #endif
374