]> The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
From Yoann Vandoorselaere <[email protected]>: make the "device"
[libpcap] / pcap-pf.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 */
24
25 #ifndef lint
26 static const char rcsid[] =
27 "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.71 2002-12-22 02:36:50 guy Exp $ (LBL)";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/timeb.h>
37 #include <sys/socket.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <net/pfilt.h>
41
42 struct mbuf;
43 struct rtentry;
44 #include <net/if.h>
45
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/if_ether.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/udp.h>
52 #include <netinet/udp_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcpip.h>
55
56 #include <ctype.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "pcap-int.h"
65
66 #ifdef HAVE_OS_PROTO_H
67 #include "os-proto.h"
68 #endif
69
70 /*
71 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
72 * applications aren't going to need more than 200 bytes of packet header
73 * and the read shouldn't return more packets than packetfilter's internal
74 * queue limit (bounded at 256).
75 */
76 #define BUFSPACE (200 * 256)
77
78 int
79 pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
80 {
81 register u_char *p, *bp;
82 struct bpf_insn *fcode;
83 register int cc, n, buflen, inc;
84 register struct enstamp *sp;
85 #ifdef LBL_ALIGN
86 struct enstamp stamp;
87 #endif
88 #ifdef PCAP_FDDIPAD
89 register int pad;
90 #endif
91
92 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns;
93 again:
94 cc = pc->cc;
95 if (cc == 0) {
96 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
97 if (cc < 0) {
98 if (errno == EWOULDBLOCK)
99 return (0);
100 if (errno == EINVAL &&
101 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
102 /*
103 * Due to a kernel bug, after 2^31 bytes,
104 * the kernel file offset overflows and
105 * read fails with EINVAL. The lseek()
106 * to 0 will fix things.
107 */
108 (void)lseek(pc->fd, 0L, SEEK_SET);
109 goto again;
110 }
111 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
112 pcap_strerror(errno));
113 return (-1);
114 }
115 bp = pc->buffer + pc->offset;
116 } else
117 bp = pc->bp;
118 /*
119 * Loop through each packet.
120 */
121 n = 0;
122 #ifdef PCAP_FDDIPAD
123 if (pc->linktype == DLT_FDDI)
124 pad = pcap_fddipad;
125 else
126 pad = 0;
127 #endif
128 while (cc > 0) {
129 if (cc < sizeof(*sp)) {
130 snprintf(pc->errbuf, sizeof(pc->errbuf),
131 "pf short read (%d)", cc);
132 return (-1);
133 }
134 #ifdef LBL_ALIGN
135 if ((long)bp & 3) {
136 sp = &stamp;
137 memcpy((char *)sp, (char *)bp, sizeof(*sp));
138 } else
139 #endif
140 sp = (struct enstamp *)bp;
141 if (sp->ens_stamplen != sizeof(*sp)) {
142 snprintf(pc->errbuf, sizeof(pc->errbuf),
143 "pf short stamplen (%d)",
144 sp->ens_stamplen);
145 return (-1);
146 }
147
148 p = bp + sp->ens_stamplen;
149 buflen = sp->ens_count;
150 if (buflen > pc->snapshot)
151 buflen = pc->snapshot;
152
153 /* Calculate inc before possible pad update */
154 inc = ENALIGN(buflen + sp->ens_stamplen);
155 cc -= inc;
156 bp += inc;
157 #ifdef PCAP_FDDIPAD
158 p += pad;
159 buflen -= pad;
160 #endif
161 pc->md.TotPkts++;
162 pc->md.TotDrops += sp->ens_dropped;
163 pc->md.TotMissed = sp->ens_ifoverflows;
164 if (pc->md.OrigMissed < 0)
165 pc->md.OrigMissed = pc->md.TotMissed;
166
167 /*
168 * Short-circuit evaluation: if using BPF filter
169 * in kernel, no need to do it now.
170 */
171 if (fcode == NULL ||
172 bpf_filter(fcode, p, sp->ens_count, buflen)) {
173 struct pcap_pkthdr h;
174 pc->md.TotAccepted++;
175 h.ts = sp->ens_tstamp;
176 #ifdef PCAP_FDDIPAD
177 h.len = sp->ens_count - pad;
178 #else
179 h.len = sp->ens_count;
180 #endif
181 h.caplen = buflen;
182 (*callback)(user, &h, p);
183 if (++n >= cnt && cnt > 0) {
184 pc->cc = cc;
185 pc->bp = bp;
186 return (n);
187 }
188 }
189 }
190 pc->cc = 0;
191 return (n);
192 }
193
194 int
195 pcap_stats(pcap_t *p, struct pcap_stat *ps)
196 {
197
198 /*
199 * If packet filtering is being done in the kernel:
200 *
201 * "ps_recv" counts only packets that passed the filter.
202 * This does not include packets dropped because we
203 * ran out of buffer space. (XXX - perhaps it should,
204 * by adding "ps_drop" to "ps_recv", for compatibility
205 * with some other platforms. On the other hand, on
206 * some platforms "ps_recv" counts only packets that
207 * passed the filter, and on others it counts packets
208 * that didn't pass the filter....)
209 *
210 * "ps_drop" counts packets that passed the kernel filter
211 * (if any) but were dropped because the input queue was
212 * full.
213 *
214 * "ps_ifdrop" counts packets dropped by the network
215 * inteface (regardless of whether they would have passed
216 * the input filter, of course).
217 *
218 * If packet filtering is not being done in the kernel:
219 *
220 * "ps_recv" counts only packets that passed the filter.
221 *
222 * "ps_drop" counts packets that were dropped because the
223 * input queue was full, regardless of whether they passed
224 * the userland filter.
225 *
226 * "ps_ifdrop" counts packets dropped by the network
227 * inteface (regardless of whether they would have passed
228 * the input filter, of course).
229 *
230 * These statistics don't include packets not yet read from
231 * the kernel by libpcap, but they may include packets not
232 * yet read from libpcap by the application.
233 */
234 ps->ps_recv = p->md.TotAccepted;
235 ps->ps_drop = p->md.TotDrops;
236 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
237 return (0);
238 }
239
240 pcap_t *
241 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
242 char *ebuf)
243 {
244 pcap_t *p;
245 short enmode;
246 int backlog = -1; /* request the most */
247 struct enfilter Filter;
248 struct endevp devparams;
249
250 p = (pcap_t *)malloc(sizeof(*p));
251 if (p == NULL) {
252 snprintf(ebuf, PCAP_ERRBUF_SIZE,
253 "pcap_open_live: %s", pcap_strerror(errno));
254 return (0);
255 }
256 memset(p, 0, sizeof(*p));
257 p->fd = pfopen(device, O_RDONLY);
258 if (p->fd < 0) {
259 snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
260 your system may not be properly configured; see the packetfilter(4) man page\n",
261 device, pcap_strerror(errno));
262 goto bad;
263 }
264 p->md.OrigMissed = -1;
265 enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
266 if (promisc)
267 enmode |= ENPROMISC;
268 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
269 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
270 pcap_strerror(errno));
271 goto bad;
272 }
273 #ifdef ENCOPYALL
274 /* Try to set COPYALL mode so that we see packets to ourself */
275 enmode = ENCOPYALL;
276 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
277 #endif
278 /* set the backlog */
279 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
280 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
281 pcap_strerror(errno));
282 goto bad;
283 }
284 /* discover interface type */
285 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
286 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
287 pcap_strerror(errno));
288 goto bad;
289 }
290 /* HACK: to compile prior to Ultrix 4.2 */
291 #ifndef ENDT_FDDI
292 #define ENDT_FDDI 4
293 #endif
294 switch (devparams.end_dev_type) {
295
296 case ENDT_10MB:
297 p->linktype = DLT_EN10MB;
298 p->offset = 2;
299 break;
300
301 case ENDT_FDDI:
302 p->linktype = DLT_FDDI;
303 break;
304
305 #ifdef ENDT_SLIP
306 case ENDT_SLIP:
307 p->linktype = DLT_SLIP;
308 break;
309 #endif
310
311 #ifdef ENDT_PPP
312 case ENDT_PPP:
313 p->linktype = DLT_PPP;
314 break;
315 #endif
316
317 #ifdef ENDT_LOOPBACK
318 case ENDT_LOOPBACK:
319 /*
320 * It appears to use Ethernet framing, at least on
321 * Digital UNIX 4.0.
322 */
323 p->linktype = DLT_EN10MB;
324 p->offset = 2;
325 break;
326 #endif
327
328 #ifdef ENDT_TRN
329 case ENDT_TRN:
330 p->linktype = DLT_IEEE802;
331 break;
332 #endif
333
334 default:
335 /*
336 * XXX - what about ENDT_IEEE802? The pfilt.h header
337 * file calls this "IEEE 802 networks (non-Ethernet)",
338 * but that doesn't specify a specific link layer type;
339 * it could be 802.4, or 802.5 (except that 802.5 is
340 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
341 * DLT_IEEE802 was hijacked to mean Token Ring in various
342 * BSDs, and why we went along with that hijacking.
343 *
344 * XXX - what about ENDT_HDLC and ENDT_NULL?
345 * Presumably, as ENDT_OTHER is just "Miscellaneous
346 * framing", there's not much we can do, as that
347 * doesn't specify a particular type of header.
348 */
349 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u",
350 devparams.end_dev_type);
351 goto bad;
352 }
353 /* set truncation */
354 #ifdef PCAP_FDDIPAD
355 if (p->linktype == DLT_FDDI)
356 /* packetfilter includes the padding in the snapshot */
357 snaplen += pcap_fddipad;
358 #endif
359 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
360 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
361 pcap_strerror(errno));
362 goto bad;
363 }
364 p->snapshot = snaplen;
365 /* accept all packets */
366 memset(&Filter, 0, sizeof(Filter));
367 Filter.enf_Priority = 37; /* anything > 2 */
368 Filter.enf_FilterLen = 0; /* means "always true" */
369 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
370 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
371 pcap_strerror(errno));
372 goto bad;
373 }
374
375 if (to_ms != 0) {
376 struct timeval timeout;
377 timeout.tv_sec = to_ms / 1000;
378 timeout.tv_usec = (to_ms * 1000) % 1000000;
379 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
380 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
381 pcap_strerror(errno));
382 goto bad;
383 }
384 }
385 p->bufsize = BUFSPACE;
386 p->buffer = (u_char*)malloc(p->bufsize + p->offset);
387
388 return (p);
389 bad:
390 if (p->fd >= 0)
391 close(p->fd);
392 free(p);
393 return (NULL);
394 }
395
396 int
397 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
398 {
399 return (0);
400 }
401
402 int
403 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
404 {
405 /*
406 * See if BIOCSETF works. If it does, the kernel supports
407 * BPF-style filters, and we do not need to do post-filtering.
408 */
409 p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
410 if (p->md.use_bpf) {
411 struct bpf_version bv;
412
413 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
414 snprintf(p->errbuf, sizeof(p->errbuf),
415 "BIOCVERSION: %s", pcap_strerror(errno));
416 return (-1);
417 }
418 else if (bv.bv_major != BPF_MAJOR_VERSION ||
419 bv.bv_minor < BPF_MINOR_VERSION) {
420 fprintf(stderr,
421 "requires bpf language %d.%d or higher; kernel is %d.%d",
422 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
423 bv.bv_major, bv.bv_minor);
424 /* don't give up, just be inefficient */
425 p->md.use_bpf = 0;
426 }
427 } else {
428 if (install_bpf_program(p, fp) < 0)
429 return (-1);
430 }
431
432 /*XXX this goes in tcpdump*/
433 if (p->md.use_bpf)
434 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
435 else
436 fprintf(stderr, "tcpdump: Filtering in user process\n");
437 return (0);
438 }
439
440 int
441 pcap_set_datalink_platform(pcap_t *p, int dlt)
442 {
443 return (0);
444 }