]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
26 static const char rcsid
[] =
27 "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.79 2003-11-04 07:05:36 guy Exp $ (LBL)";
34 #include <sys/types.h>
36 #include <sys/timeb.h>
37 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <net/pfilt.h>
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>
65 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
66 * native OS version, as we need various BPF ioctls from it.
68 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
73 #ifdef HAVE_OS_PROTO_H
77 static int pcap_setfilter_pf(pcap_t
*, struct bpf_program
*);
80 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
81 * applications aren't going to need more than 200 bytes of packet header
82 * and the read shouldn't return more packets than packetfilter's internal
83 * queue limit (bounded at 256).
85 #define BUFSPACE (200 * 256)
88 pcap_read_pf(pcap_t
*pc
, int cnt
, pcap_handler callback
, u_char
*user
)
90 register u_char
*p
, *bp
;
91 struct bpf_insn
*fcode
;
92 register int cc
, n
, buflen
, inc
;
93 register struct enstamp
*sp
;
101 fcode
= pc
->md
.use_bpf
? NULL
: pc
->fcode
.bf_insns
;
105 cc
= read(pc
->fd
, (char *)pc
->buffer
+ pc
->offset
, pc
->bufsize
);
107 if (errno
== EWOULDBLOCK
)
109 if (errno
== EINVAL
&&
110 lseek(pc
->fd
, 0L, SEEK_CUR
) + pc
->bufsize
< 0) {
112 * Due to a kernel bug, after 2^31 bytes,
113 * the kernel file offset overflows and
114 * read fails with EINVAL. The lseek()
115 * to 0 will fix things.
117 (void)lseek(pc
->fd
, 0L, SEEK_SET
);
120 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
), "pf read: %s",
121 pcap_strerror(errno
));
124 bp
= pc
->buffer
+ pc
->offset
;
128 * Loop through each packet.
132 if (pc
->linktype
== DLT_FDDI
)
139 * Has "pcap_breakloop()" been called?
140 * If so, return immediately - if we haven't read any
141 * packets, clear the flag and return -2 to indicate
142 * that we were told to break out of the loop, otherwise
143 * leave the flag set, so that the *next* call will break
144 * out of the loop without having read any packets, and
145 * return the number of packets we've processed so far.
147 if (pc
->break_loop
) {
157 if (cc
< sizeof(*sp
)) {
158 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
159 "pf short read (%d)", cc
);
165 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
168 sp
= (struct enstamp
*)bp
;
169 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
170 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
171 "pf short stamplen (%d)",
176 p
= bp
+ sp
->ens_stamplen
;
177 buflen
= sp
->ens_count
;
178 if (buflen
> pc
->snapshot
)
179 buflen
= pc
->snapshot
;
181 /* Calculate inc before possible pad update */
182 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
190 pc
->md
.TotDrops
+= sp
->ens_dropped
;
191 pc
->md
.TotMissed
= sp
->ens_ifoverflows
;
192 if (pc
->md
.OrigMissed
< 0)
193 pc
->md
.OrigMissed
= pc
->md
.TotMissed
;
196 * Short-circuit evaluation: if using BPF filter
197 * in kernel, no need to do it now.
200 bpf_filter(fcode
, p
, sp
->ens_count
, buflen
)) {
201 struct pcap_pkthdr h
;
202 pc
->md
.TotAccepted
++;
203 h
.ts
= sp
->ens_tstamp
;
205 h
.len
= sp
->ens_count
- pad
;
207 h
.len
= sp
->ens_count
;
210 (*callback
)(user
, &h
, p
);
211 if (++n
>= cnt
&& cnt
> 0) {
223 pcap_stats_pf(pcap_t
*p
, struct pcap_stat
*ps
)
227 * If packet filtering is being done in the kernel:
229 * "ps_recv" counts only packets that passed the filter.
230 * This does not include packets dropped because we
231 * ran out of buffer space. (XXX - perhaps it should,
232 * by adding "ps_drop" to "ps_recv", for compatibility
233 * with some other platforms. On the other hand, on
234 * some platforms "ps_recv" counts only packets that
235 * passed the filter, and on others it counts packets
236 * that didn't pass the filter....)
238 * "ps_drop" counts packets that passed the kernel filter
239 * (if any) but were dropped because the input queue was
242 * "ps_ifdrop" counts packets dropped by the network
243 * inteface (regardless of whether they would have passed
244 * the input filter, of course).
246 * If packet filtering is not being done in the kernel:
248 * "ps_recv" counts only packets that passed the filter.
250 * "ps_drop" counts packets that were dropped because the
251 * input queue was full, regardless of whether they passed
252 * the userland filter.
254 * "ps_ifdrop" counts packets dropped by the network
255 * inteface (regardless of whether they would have passed
256 * the input filter, of course).
258 * These statistics don't include packets not yet read from
259 * the kernel by libpcap, but they may include packets not
260 * yet read from libpcap by the application.
262 ps
->ps_recv
= p
->md
.TotAccepted
;
263 ps
->ps_drop
= p
->md
.TotDrops
;
264 ps
->ps_ifdrop
= p
->md
.TotMissed
- p
->md
.OrigMissed
;
269 pcap_close_pf(pcap_t
*p
)
271 if (p
->buffer
!= NULL
)
278 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
283 int backlog
= -1; /* request the most */
284 struct enfilter Filter
;
285 struct endevp devparams
;
287 p
= (pcap_t
*)malloc(sizeof(*p
));
289 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
290 "pcap_open_live: %s", pcap_strerror(errno
));
293 memset(p
, 0, sizeof(*p
));
296 * XXX - we assume here that "pfopen()" does not, in fact, modify
297 * its argument, even though it takes a "char *" rather than a
298 * "const char *" as its first argument. That appears to be
299 * the case, at least on Digital UNIX 4.0.
301 p
->fd
= pfopen(device
, O_RDONLY
);
303 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "pf open: %s: %s\n\
304 your system may not be properly configured; see the packetfilter(4) man page\n",
305 device
, pcap_strerror(errno
));
308 p
->md
.OrigMissed
= -1;
309 enmode
= ENTSTAMP
|ENBATCH
|ENNONEXCL
;
312 if (ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
) < 0) {
313 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCMBIS: %s",
314 pcap_strerror(errno
));
318 /* Try to set COPYALL mode so that we see packets to ourself */
320 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
322 /* set the backlog */
323 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
324 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETW: %s",
325 pcap_strerror(errno
));
328 /* discover interface type */
329 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
330 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCDEVP: %s",
331 pcap_strerror(errno
));
334 /* HACK: to compile prior to Ultrix 4.2 */
338 switch (devparams
.end_dev_type
) {
341 p
->linktype
= DLT_EN10MB
;
346 p
->linktype
= DLT_FDDI
;
351 p
->linktype
= DLT_SLIP
;
357 p
->linktype
= DLT_PPP
;
364 * It appears to use Ethernet framing, at least on
367 p
->linktype
= DLT_EN10MB
;
374 p
->linktype
= DLT_IEEE802
;
380 * XXX - what about ENDT_IEEE802? The pfilt.h header
381 * file calls this "IEEE 802 networks (non-Ethernet)",
382 * but that doesn't specify a specific link layer type;
383 * it could be 802.4, or 802.5 (except that 802.5 is
384 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
385 * DLT_IEEE802 was hijacked to mean Token Ring in various
386 * BSDs, and why we went along with that hijacking.
388 * XXX - what about ENDT_HDLC and ENDT_NULL?
389 * Presumably, as ENDT_OTHER is just "Miscellaneous
390 * framing", there's not much we can do, as that
391 * doesn't specify a particular type of header.
393 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown data-link type %u",
394 devparams
.end_dev_type
);
399 if (p
->linktype
== DLT_FDDI
)
400 /* packetfilter includes the padding in the snapshot */
401 snaplen
+= pcap_fddipad
;
403 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&snaplen
) < 0) {
404 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCTRUNCATE: %s",
405 pcap_strerror(errno
));
408 p
->snapshot
= snaplen
;
409 /* accept all packets */
410 memset(&Filter
, 0, sizeof(Filter
));
411 Filter
.enf_Priority
= 37; /* anything > 2 */
412 Filter
.enf_FilterLen
= 0; /* means "always true" */
413 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
414 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETF: %s",
415 pcap_strerror(errno
));
420 struct timeval timeout
;
421 timeout
.tv_sec
= to_ms
/ 1000;
422 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
423 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
424 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSRTIMEOUT: %s",
425 pcap_strerror(errno
));
430 p
->bufsize
= BUFSPACE
;
431 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
432 if (p
->buffer
== NULL
) {
433 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
437 p
->read_op
= pcap_read_pf
;
438 p
->setfilter_op
= pcap_setfilter_pf
;
439 p
->set_datalink_op
= NULL
; /* can't change data link type */
440 p
->stats_op
= pcap_stats_pf
;
441 p
->close_op
= pcap_close_pf
;
452 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
458 pcap_setfilter_pf(pcap_t
*p
, struct bpf_program
*fp
)
461 * See if BIOCSETF works. If it does, the kernel supports
462 * BPF-style filters, and we do not need to do post-filtering.
464 p
->md
.use_bpf
= (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) >= 0);
466 struct bpf_version bv
;
468 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
469 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
470 "BIOCVERSION: %s", pcap_strerror(errno
));
473 else if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
474 bv
.bv_minor
< BPF_MINOR_VERSION
) {
476 "requires bpf language %d.%d or higher; kernel is %d.%d",
477 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
478 bv
.bv_major
, bv
.bv_minor
);
479 /* don't give up, just be inefficient */
483 if (install_bpf_program(p
, fp
) < 0)
487 /*XXX this goes in tcpdump*/
489 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
491 fprintf(stderr
, "tcpdump: Filtering in user process\n");