]>
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
[] _U_
=
27 "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.97 2008-04-14 20:40:58 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 register int cc
, n
, buflen
, inc
;
92 register struct enstamp
*sp
;
103 cc
= read(pc
->fd
, (char *)pc
->buffer
+ pc
->offset
, pc
->bufsize
);
105 if (errno
== EWOULDBLOCK
)
107 if (errno
== EINVAL
&&
108 lseek(pc
->fd
, 0L, SEEK_CUR
) + pc
->bufsize
< 0) {
110 * Due to a kernel bug, after 2^31 bytes,
111 * the kernel file offset overflows and
112 * read fails with EINVAL. The lseek()
113 * to 0 will fix things.
115 (void)lseek(pc
->fd
, 0L, SEEK_SET
);
118 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
), "pf read: %s",
119 pcap_strerror(errno
));
122 bp
= pc
->buffer
+ pc
->offset
;
126 * Loop through each packet.
134 * Has "pcap_breakloop()" been called?
135 * If so, return immediately - if we haven't read any
136 * packets, clear the flag and return -2 to indicate
137 * that we were told to break out of the loop, otherwise
138 * leave the flag set, so that the *next* call will break
139 * out of the loop without having read any packets, and
140 * return the number of packets we've processed so far.
142 if (pc
->break_loop
) {
152 if (cc
< sizeof(*sp
)) {
153 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
154 "pf short read (%d)", cc
);
160 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
163 sp
= (struct enstamp
*)bp
;
164 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
165 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
166 "pf short stamplen (%d)",
171 p
= bp
+ sp
->ens_stamplen
;
172 buflen
= sp
->ens_count
;
173 if (buflen
> pc
->snapshot
)
174 buflen
= pc
->snapshot
;
176 /* Calculate inc before possible pad update */
177 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
181 pc
->md
.TotDrops
+= sp
->ens_dropped
;
182 pc
->md
.TotMissed
= sp
->ens_ifoverflows
;
183 if (pc
->md
.OrigMissed
< 0)
184 pc
->md
.OrigMissed
= pc
->md
.TotMissed
;
187 * Short-circuit evaluation: if using BPF filter
188 * in kernel, no need to do it now - we already know
189 * the packet passed the filter.
192 * Note: the filter code was generated assuming
193 * that pc->fddipad was the amount of padding
194 * before the header, as that's what's required
195 * in the kernel, so we run the filter before
196 * skipping that padding.
199 if (pc
->md
.use_bpf
||
200 bpf_filter(pc
->fcode
.bf_insns
, 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
;
214 (*callback
)(user
, &h
, p
);
215 if (++n
>= cnt
&& cnt
> 0) {
227 pcap_inject_pf(pcap_t
*p
, const void *buf
, size_t size
)
231 ret
= write(p
->fd
, buf
, size
);
233 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
234 pcap_strerror(errno
));
241 pcap_stats_pf(pcap_t
*p
, struct pcap_stat
*ps
)
245 * If packet filtering is being done in the kernel:
247 * "ps_recv" counts only packets that passed the filter.
248 * This does not include packets dropped because we
249 * ran out of buffer space. (XXX - perhaps it should,
250 * by adding "ps_drop" to "ps_recv", for compatibility
251 * with some other platforms. On the other hand, on
252 * some platforms "ps_recv" counts only packets that
253 * passed the filter, and on others it counts packets
254 * that didn't pass the filter....)
256 * "ps_drop" counts packets that passed the kernel filter
257 * (if any) but were dropped because the input queue was
260 * "ps_ifdrop" counts packets dropped by the network
261 * inteface (regardless of whether they would have passed
262 * the input filter, of course).
264 * If packet filtering is not being done in the kernel:
266 * "ps_recv" counts only packets that passed the filter.
268 * "ps_drop" counts packets that were dropped because the
269 * input queue was full, regardless of whether they passed
270 * the userland filter.
272 * "ps_ifdrop" counts packets dropped by the network
273 * inteface (regardless of whether they would have passed
274 * the input filter, of course).
276 * These statistics don't include packets not yet read from
277 * the kernel by libpcap, but they may include packets not
278 * yet read from libpcap by the application.
280 ps
->ps_recv
= p
->md
.TotAccepted
;
281 ps
->ps_drop
= p
->md
.TotDrops
;
282 ps
->ps_ifdrop
= p
->md
.TotMissed
- p
->md
.OrigMissed
;
287 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
288 * don't get DLT_DOCSIS defined.
291 #define DLT_DOCSIS 143
295 pcap_activate_pf(pcap_t
*p
)
298 int backlog
= -1; /* request the most */
299 struct enfilter Filter
;
300 struct endevp devparams
;
303 * Initially try a read/write open (to allow the inject
304 * method to work). If that fails due to permission
305 * issues, fall back to read-only. This allows a
306 * non-root user to be granted specific access to pcap
307 * capabilities via file permissions.
309 * XXX - we should have an API that has a flag that
310 * controls whether to open read-only or read-write,
311 * so that denial of permission to send (or inability
312 * to send, if sending packets isn't supported on
313 * the device in question) can be indicated at open
316 * XXX - we assume here that "pfopen()" does not, in fact, modify
317 * its argument, even though it takes a "char *" rather than a
318 * "const char *" as its first argument. That appears to be
319 * the case, at least on Digital UNIX 4.0.
321 p
->fd
= pfopen(p
->opt
.source
, O_RDWR
);
322 if (p
->fd
== -1 && errno
== EACCES
)
323 p
->fd
= pfopen(p
->opt
.source
, O_RDONLY
);
325 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "pf open: %s: %s\n\
326 your system may not be properly configured; see the packetfilter(4) man page\n",
327 p
->opt
.source
, pcap_strerror(errno
));
330 p
->md
.OrigMissed
= -1;
331 enmode
= ENTSTAMP
|ENBATCH
|ENNONEXCL
;
334 if (ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
) < 0) {
335 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCMBIS: %s",
336 pcap_strerror(errno
));
340 /* Try to set COPYALL mode so that we see packets to ourself */
342 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
344 /* set the backlog */
345 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
346 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSETW: %s",
347 pcap_strerror(errno
));
350 /* discover interface type */
351 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
352 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCDEVP: %s",
353 pcap_strerror(errno
));
356 /* HACK: to compile prior to Ultrix 4.2 */
360 switch (devparams
.end_dev_type
) {
363 p
->linktype
= DLT_EN10MB
;
366 * This is (presumably) a real Ethernet capture; give it a
367 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
368 * that an application can let you choose it, in case you're
369 * capturing DOCSIS traffic that a Cisco Cable Modem
370 * Termination System is putting out onto an Ethernet (it
371 * doesn't put an Ethernet header onto the wire, it puts raw
372 * DOCSIS frames out on the wire inside the low-level
375 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
377 * If that fails, just leave the list empty.
379 if (p
->dlt_list
!= NULL
) {
380 p
->dlt_list
[0] = DLT_EN10MB
;
381 p
->dlt_list
[1] = DLT_DOCSIS
;
387 p
->linktype
= DLT_FDDI
;
392 p
->linktype
= DLT_SLIP
;
398 p
->linktype
= DLT_PPP
;
405 * It appears to use Ethernet framing, at least on
408 p
->linktype
= DLT_EN10MB
;
415 p
->linktype
= DLT_IEEE802
;
421 * XXX - what about ENDT_IEEE802? The pfilt.h header
422 * file calls this "IEEE 802 networks (non-Ethernet)",
423 * but that doesn't specify a specific link layer type;
424 * it could be 802.4, or 802.5 (except that 802.5 is
425 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
426 * DLT_IEEE802 was hijacked to mean Token Ring in various
427 * BSDs, and why we went along with that hijacking.
429 * XXX - what about ENDT_HDLC and ENDT_NULL?
430 * Presumably, as ENDT_OTHER is just "Miscellaneous
431 * framing", there's not much we can do, as that
432 * doesn't specify a particular type of header.
434 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
435 "unknown data-link type %u", devparams
.end_dev_type
);
440 if (p
->linktype
== DLT_FDDI
) {
441 p
->fddipad
= PCAP_FDDIPAD
;
443 /* packetfilter includes the padding in the snapshot */
444 p
->snapshot
+= PCAP_FDDIPAD
;
448 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&p
->snapshot
) < 0) {
449 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCTRUNCATE: %s",
450 pcap_strerror(errno
));
453 /* accept all packets */
454 memset(&Filter
, 0, sizeof(Filter
));
455 Filter
.enf_Priority
= 37; /* anything > 2 */
456 Filter
.enf_FilterLen
= 0; /* means "always true" */
457 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
458 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSETF: %s",
459 pcap_strerror(errno
));
463 if (p
->md
.timeout
!= 0) {
464 struct timeval timeout
;
465 timeout
.tv_sec
= p
->md
.timeout
/ 1000;
466 timeout
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
467 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
468 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSRTIMEOUT: %s",
469 pcap_strerror(errno
));
474 p
->bufsize
= BUFSPACE
;
475 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
476 if (p
->buffer
== NULL
) {
477 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
482 * "select()" and "poll()" work on packetfilter devices.
484 p
->selectable_fd
= p
->fd
;
486 p
->read_op
= pcap_read_pf
;
487 p
->inject_op
= pcap_inject_pf
;
488 p
->setfilter_op
= pcap_setfilter_pf
;
489 p
->setdirection_op
= NULL
; /* Not implemented. */
490 p
->set_datalink_op
= NULL
; /* can't change data link type */
491 p
->getnonblock_op
= pcap_getnonblock_fd
;
492 p
->setnonblock_op
= pcap_setnonblock_fd
;
493 p
->stats_op
= pcap_stats_pf
;
497 pcap_cleanup_live_common(p
);
502 pcap_create(const char *device
, char *ebuf
)
506 p
= pcap_create_common(device
, ebuf
);
510 p
->activate_op
= pcap_activate_pf
;
515 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
521 pcap_setfilter_pf(pcap_t
*p
, struct bpf_program
*fp
)
523 struct bpf_version bv
;
526 * See if BIOCVERSION works. If not, we assume the kernel doesn't
527 * support BPF-style filters (it's not documented in the bpf(7)
528 * or packetfiler(7) man pages, but the code used to fail if
529 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
530 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
533 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) >= 0) {
535 * OK, we have the version of the BPF interpreter;
536 * is it the same major version as us, and the same
537 * or better minor version?
539 if (bv
.bv_major
== BPF_MAJOR_VERSION
&&
540 bv
.bv_minor
>= BPF_MINOR_VERSION
) {
542 * Yes. Try to install the filter.
544 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
545 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
546 "BIOCSETF: %s", pcap_strerror(errno
));
551 * OK, that succeeded. We're doing filtering in
552 * the kernel. (We assume we don't have a
553 * userland filter installed - that'd require
554 * a previous version check to have failed but
555 * this one to succeed.)
557 * XXX - this message should be supplied to the
558 * application as a warning of some sort,
559 * except that if it's a GUI application, it's
560 * not clear that it should be displayed in
561 * a window to annoy the user.
563 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
567 * Discard any previously-received packets,
568 * as they might have passed whatever filter
569 * was formerly in effect, but might not pass
570 * this filter (BIOCSETF discards packets buffered
571 * in the kernel, so you can lose packets in any
579 * We can't use the kernel's BPF interpreter; don't give
580 * up, just log a message and be inefficient.
582 * XXX - this should really be supplied to the application
583 * as a warning of some sort.
586 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
587 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
588 bv
.bv_major
, bv
.bv_minor
);
592 * We couldn't do filtering in the kernel; do it in userland.
594 if (install_bpf_program(p
, fp
) < 0)
598 * XXX - this message should be supplied by the application as
599 * a warning of some sort.
601 fprintf(stderr
, "tcpdump: Filtering in user process\n");