1 /* $NetBSD: pcap-pf.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * packet filter subroutines for tcpdump
24 * Extraction/creation by Jeffrey Mogul, DECWRL
27 #include <sys/cdefs.h>
28 __RCSID("$NetBSD: pcap-pf.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
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
78 * FDDI packets are padded to make everything line up on a nice boundary.
80 #define PCAP_FDDIPAD 3
83 * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
84 * Tru64 UNIX packetfilter devices.
87 int filtering_in_kernel
; /* using kernel filter */
88 u_long TotPkts
; /* can't oflow for 79 hrs on ether */
89 u_long TotAccepted
; /* count accepted by filter */
90 u_long TotDrops
; /* count of dropped packets */
91 long TotMissed
; /* missed by i/f during this run */
92 long OrigMissed
; /* missed by i/f before this run */
95 static int pcap_setfilter_pf(pcap_t
*, struct bpf_program
*);
98 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
99 * applications aren't going to need more than 200 bytes of packet header
100 * and the read shouldn't return more packets than packetfilter's internal
101 * queue limit (bounded at 256).
103 #define BUFSPACE (200 * 256)
106 pcap_read_pf(pcap_t
*pc
, int cnt
, pcap_handler callback
, u_char
*user
)
108 struct pcap_pf
*pf
= pc
->priv
;
109 register u_char
*p
, *bp
;
110 register int cc
, n
, buflen
, inc
;
111 register struct enstamp
*sp
;
113 struct enstamp stamp
;
120 cc
= read(pc
->fd
, (char *)pc
->buffer
+ pc
->offset
, pc
->bufsize
);
122 if (errno
== EWOULDBLOCK
)
124 if (errno
== EINVAL
&&
125 lseek(pc
->fd
, 0L, SEEK_CUR
) + pc
->bufsize
< 0) {
127 * Due to a kernel bug, after 2^31 bytes,
128 * the kernel file offset overflows and
129 * read fails with EINVAL. The lseek()
130 * to 0 will fix things.
132 (void)lseek(pc
->fd
, 0L, SEEK_SET
);
135 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
), "pf read: %s",
136 pcap_strerror(errno
));
139 bp
= pc
->buffer
+ pc
->offset
;
143 * Loop through each packet.
149 * Has "pcap_breakloop()" been called?
150 * If so, return immediately - if we haven't read any
151 * packets, clear the flag and return -2 to indicate
152 * that we were told to break out of the loop, otherwise
153 * leave the flag set, so that the *next* call will break
154 * out of the loop without having read any packets, and
155 * return the number of packets we've processed so far.
157 if (pc
->break_loop
) {
167 if (cc
< sizeof(*sp
)) {
168 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
169 "pf short read (%d)", cc
);
175 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
178 sp
= (struct enstamp
*)bp
;
179 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
180 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
181 "pf short stamplen (%d)",
186 p
= bp
+ sp
->ens_stamplen
;
187 buflen
= sp
->ens_count
;
188 if (buflen
> pc
->snapshot
)
189 buflen
= pc
->snapshot
;
191 /* Calculate inc before possible pad update */
192 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
196 pf
->TotDrops
+= sp
->ens_dropped
;
197 pf
->TotMissed
= sp
->ens_ifoverflows
;
198 if (pf
->OrigMissed
< 0)
199 pf
->OrigMissed
= pf
->TotMissed
;
202 * Short-circuit evaluation: if using BPF filter
203 * in kernel, no need to do it now - we already know
204 * the packet passed the filter.
206 * Note: the filter code was generated assuming
207 * that pc->fddipad was the amount of padding
208 * before the header, as that's what's required
209 * in the kernel, so we run the filter before
210 * skipping that padding.
212 if (pf
->filtering_in_kernel
||
213 bpf_filter(pc
->fcode
.bf_insns
, p
, sp
->ens_count
, buflen
)) {
214 struct pcap_pkthdr h
;
216 h
.ts
= sp
->ens_tstamp
;
217 h
.len
= sp
->ens_count
- pad
;
221 (*callback
)(user
, &h
, p
);
222 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
234 pcap_inject_pf(pcap_t
*p
, const void *buf
, size_t size
)
238 ret
= write(p
->fd
, buf
, size
);
240 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
241 pcap_strerror(errno
));
248 pcap_stats_pf(pcap_t
*p
, struct pcap_stat
*ps
)
250 struct pcap_pf
*pf
= p
->priv
;
253 * If packet filtering is being done in the kernel:
255 * "ps_recv" counts only packets that passed the filter.
256 * This does not include packets dropped because we
257 * ran out of buffer space. (XXX - perhaps it should,
258 * by adding "ps_drop" to "ps_recv", for compatibility
259 * with some other platforms. On the other hand, on
260 * some platforms "ps_recv" counts only packets that
261 * passed the filter, and on others it counts packets
262 * that didn't pass the filter....)
264 * "ps_drop" counts packets that passed the kernel filter
265 * (if any) but were dropped because the input queue was
268 * "ps_ifdrop" counts packets dropped by the network
269 * inteface (regardless of whether they would have passed
270 * the input filter, of course).
272 * If packet filtering is not being done in the kernel:
274 * "ps_recv" counts only packets that passed the filter.
276 * "ps_drop" counts packets that were dropped because the
277 * input queue was full, regardless of whether they passed
278 * the userland filter.
280 * "ps_ifdrop" counts packets dropped by the network
281 * inteface (regardless of whether they would have passed
282 * the input filter, of course).
284 * These statistics don't include packets not yet read from
285 * the kernel by libpcap, but they may include packets not
286 * yet read from libpcap by the application.
288 ps
->ps_recv
= pf
->TotAccepted
;
289 ps
->ps_drop
= pf
->TotDrops
;
290 ps
->ps_ifdrop
= pf
->TotMissed
- pf
->OrigMissed
;
295 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
296 * don't get DLT_DOCSIS defined.
299 #define DLT_DOCSIS 143
303 pcap_activate_pf(pcap_t
*p
)
305 struct pcap_pf
*pf
= p
->priv
;
307 int backlog
= -1; /* request the most */
308 struct enfilter Filter
;
309 struct endevp devparams
;
312 * Initially try a read/write open (to allow the inject
313 * method to work). If that fails due to permission
314 * issues, fall back to read-only. This allows a
315 * non-root user to be granted specific access to pcap
316 * capabilities via file permissions.
318 * XXX - we should have an API that has a flag that
319 * controls whether to open read-only or read-write,
320 * so that denial of permission to send (or inability
321 * to send, if sending packets isn't supported on
322 * the device in question) can be indicated at open
325 * XXX - we assume here that "pfopen()" does not, in fact, modify
326 * its argument, even though it takes a "char *" rather than a
327 * "const char *" as its first argument. That appears to be
328 * the case, at least on Digital UNIX 4.0.
330 p
->fd
= pfopen(p
->opt
.source
, O_RDWR
);
331 if (p
->fd
== -1 && errno
== EACCES
)
332 p
->fd
= pfopen(p
->opt
.source
, O_RDONLY
);
334 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "pf open: %s: %s\n\
335 your system may not be properly configured; see the packetfilter(4) man page\n",
336 p
->opt
.source
, pcap_strerror(errno
));
340 enmode
= ENTSTAMP
|ENNONEXCL
;
341 if (!p
->opt
.immediate
)
345 if (ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
) < 0) {
346 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCMBIS: %s",
347 pcap_strerror(errno
));
351 /* Try to set COPYALL mode so that we see packets to ourself */
353 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
355 /* set the backlog */
356 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
357 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSETW: %s",
358 pcap_strerror(errno
));
361 /* discover interface type */
362 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
363 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCDEVP: %s",
364 pcap_strerror(errno
));
367 /* HACK: to compile prior to Ultrix 4.2 */
371 switch (devparams
.end_dev_type
) {
374 p
->linktype
= DLT_EN10MB
;
377 * This is (presumably) a real Ethernet capture; give it a
378 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
379 * that an application can let you choose it, in case you're
380 * capturing DOCSIS traffic that a Cisco Cable Modem
381 * Termination System is putting out onto an Ethernet (it
382 * doesn't put an Ethernet header onto the wire, it puts raw
383 * DOCSIS frames out on the wire inside the low-level
386 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
388 * If that fails, just leave the list empty.
390 if (p
->dlt_list
!= NULL
) {
391 p
->dlt_list
[0] = DLT_EN10MB
;
392 p
->dlt_list
[1] = DLT_DOCSIS
;
398 p
->linktype
= DLT_FDDI
;
403 p
->linktype
= DLT_SLIP
;
409 p
->linktype
= DLT_PPP
;
416 * It appears to use Ethernet framing, at least on
419 p
->linktype
= DLT_EN10MB
;
426 p
->linktype
= DLT_IEEE802
;
432 * XXX - what about ENDT_IEEE802? The pfilt.h header
433 * file calls this "IEEE 802 networks (non-Ethernet)",
434 * but that doesn't specify a specific link layer type;
435 * it could be 802.4, or 802.5 (except that 802.5 is
436 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
437 * DLT_IEEE802 was hijacked to mean Token Ring in various
438 * BSDs, and why we went along with that hijacking.
440 * XXX - what about ENDT_HDLC and ENDT_NULL?
441 * Presumably, as ENDT_OTHER is just "Miscellaneous
442 * framing", there's not much we can do, as that
443 * doesn't specify a particular type of header.
445 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
446 "unknown data-link type %u", devparams
.end_dev_type
);
450 if (p
->linktype
== DLT_FDDI
) {
451 p
->fddipad
= PCAP_FDDIPAD
;
453 /* packetfilter includes the padding in the snapshot */
454 p
->snapshot
+= PCAP_FDDIPAD
;
457 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&p
->snapshot
) < 0) {
458 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCTRUNCATE: %s",
459 pcap_strerror(errno
));
462 /* accept all packets */
463 memset(&Filter
, 0, sizeof(Filter
));
464 Filter
.enf_Priority
= 37; /* anything > 2 */
465 Filter
.enf_FilterLen
= 0; /* means "always true" */
466 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
467 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSETF: %s",
468 pcap_strerror(errno
));
472 if (p
->opt
.timeout
!= 0) {
473 struct timeval timeout
;
474 timeout
.tv_sec
= p
->opt
.timeout
/ 1000;
475 timeout
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
476 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
477 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "EIOCSRTIMEOUT: %s",
478 pcap_strerror(errno
));
483 p
->bufsize
= BUFSPACE
;
484 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
485 if (p
->buffer
== NULL
) {
486 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
491 * "select()" and "poll()" work on packetfilter devices.
493 p
->selectable_fd
= p
->fd
;
495 p
->read_op
= pcap_read_pf
;
496 p
->inject_op
= pcap_inject_pf
;
497 p
->setfilter_op
= pcap_setfilter_pf
;
498 p
->setdirection_op
= NULL
; /* Not implemented. */
499 p
->set_datalink_op
= NULL
; /* can't change data link type */
500 p
->getnonblock_op
= pcap_getnonblock_fd
;
501 p
->setnonblock_op
= pcap_setnonblock_fd
;
502 p
->stats_op
= pcap_stats_pf
;
506 pcap_cleanup_live_common(p
);
511 pcap_create_interface(const char *device
, char *ebuf
)
515 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_pf
));
519 p
->activate_op
= pcap_activate_pf
;
524 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
530 pcap_setfilter_pf(pcap_t
*p
, struct bpf_program
*fp
)
532 struct pcap_pf
*pf
= p
->priv
;
533 struct bpf_version bv
;
536 * See if BIOCVERSION works. If not, we assume the kernel doesn't
537 * support BPF-style filters (it's not documented in the bpf(7)
538 * or packetfiler(7) man pages, but the code used to fail if
539 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
540 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
543 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) >= 0) {
545 * OK, we have the version of the BPF interpreter;
546 * is it the same major version as us, and the same
547 * or better minor version?
549 if (bv
.bv_major
== BPF_MAJOR_VERSION
&&
550 bv
.bv_minor
>= BPF_MINOR_VERSION
) {
552 * Yes. Try to install the filter.
554 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
555 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
556 "BIOCSETF: %s", pcap_strerror(errno
));
561 * OK, that succeeded. We're doing filtering in
562 * the kernel. (We assume we don't have a
563 * userland filter installed - that'd require
564 * a previous version check to have failed but
565 * this one to succeed.)
567 * XXX - this message should be supplied to the
568 * application as a warning of some sort,
569 * except that if it's a GUI application, it's
570 * not clear that it should be displayed in
571 * a window to annoy the user.
573 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
574 pf
->filtering_in_kernel
= 1;
577 * Discard any previously-received packets,
578 * as they might have passed whatever filter
579 * was formerly in effect, but might not pass
580 * this filter (BIOCSETF discards packets buffered
581 * in the kernel, so you can lose packets in any
589 * We can't use the kernel's BPF interpreter; don't give
590 * up, just log a message and be inefficient.
592 * XXX - this should really be supplied to the application
593 * as a warning of some sort.
596 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
597 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
598 bv
.bv_major
, bv
.bv_minor
);
602 * We couldn't do filtering in the kernel; do it in userland.
604 if (install_bpf_program(p
, fp
) < 0)
608 * XXX - this message should be supplied by the application as
609 * a warning of some sort.
611 fprintf(stderr
, "tcpdump: Filtering in user process\n");
612 pf
->filtering_in_kernel
= 0;