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: /pub/NetBSD/misc/repositories/cvsroot/src/dist/libpcap/pcap-pf.c,v 1.1.1.1 2006/02/27 15:45:48 drochner 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.
136 * Has "pcap_breakloop()" been called?
137 * If so, return immediately - if we haven't read any
138 * packets, clear the flag and return -2 to indicate
139 * that we were told to break out of the loop, otherwise
140 * leave the flag set, so that the *next* call will break
141 * out of the loop without having read any packets, and
142 * return the number of packets we've processed so far.
144 if (pc
->break_loop
) {
154 if (cc
< sizeof(*sp
)) {
155 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
156 "pf short read (%d)", cc
);
162 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
165 sp
= (struct enstamp
*)bp
;
166 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
167 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
168 "pf short stamplen (%d)",
173 p
= bp
+ sp
->ens_stamplen
;
174 buflen
= sp
->ens_count
;
175 if (buflen
> pc
->snapshot
)
176 buflen
= pc
->snapshot
;
178 /* Calculate inc before possible pad update */
179 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
183 pc
->md
.TotDrops
+= sp
->ens_dropped
;
184 pc
->md
.TotMissed
= sp
->ens_ifoverflows
;
185 if (pc
->md
.OrigMissed
< 0)
186 pc
->md
.OrigMissed
= pc
->md
.TotMissed
;
189 * Short-circuit evaluation: if using BPF filter
190 * in kernel, no need to do it now.
193 * Note: the filter code was generated assuming
194 * that pc->fddipad was the amount of padding
195 * before the header, as that's what's required
196 * in the kernel, so we run the filter before
197 * skipping that padding.
201 bpf_filter(fcode
, p
, sp
->ens_count
, buflen
)) {
202 struct pcap_pkthdr h
;
203 pc
->md
.TotAccepted
++;
204 h
.ts
= sp
->ens_tstamp
;
206 h
.len
= sp
->ens_count
- pad
;
208 h
.len
= sp
->ens_count
;
215 (*callback
)(user
, &h
, p
);
216 if (++n
>= cnt
&& cnt
> 0) {
228 pcap_inject_pf(pcap_t
*p
, const void *buf
, size_t size
)
232 ret
= write(p
->fd
, buf
, size
);
234 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
235 pcap_strerror(errno
));
242 pcap_stats_pf(pcap_t
*p
, struct pcap_stat
*ps
)
246 * If packet filtering is being done in the kernel:
248 * "ps_recv" counts only packets that passed the filter.
249 * This does not include packets dropped because we
250 * ran out of buffer space. (XXX - perhaps it should,
251 * by adding "ps_drop" to "ps_recv", for compatibility
252 * with some other platforms. On the other hand, on
253 * some platforms "ps_recv" counts only packets that
254 * passed the filter, and on others it counts packets
255 * that didn't pass the filter....)
257 * "ps_drop" counts packets that passed the kernel filter
258 * (if any) but were dropped because the input queue was
261 * "ps_ifdrop" counts packets dropped by the network
262 * inteface (regardless of whether they would have passed
263 * the input filter, of course).
265 * If packet filtering is not being done in the kernel:
267 * "ps_recv" counts only packets that passed the filter.
269 * "ps_drop" counts packets that were dropped because the
270 * input queue was full, regardless of whether they passed
271 * the userland filter.
273 * "ps_ifdrop" counts packets dropped by the network
274 * inteface (regardless of whether they would have passed
275 * the input filter, of course).
277 * These statistics don't include packets not yet read from
278 * the kernel by libpcap, but they may include packets not
279 * yet read from libpcap by the application.
281 ps
->ps_recv
= p
->md
.TotAccepted
;
282 ps
->ps_drop
= p
->md
.TotDrops
;
283 ps
->ps_ifdrop
= p
->md
.TotMissed
- p
->md
.OrigMissed
;
288 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
289 * don't get DLT_DOCSIS defined.
292 #define DLT_DOCSIS 143
296 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
301 int backlog
= -1; /* request the most */
302 struct enfilter Filter
;
303 struct endevp devparams
;
305 p
= (pcap_t
*)malloc(sizeof(*p
));
307 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
308 "pcap_open_live: %s", pcap_strerror(errno
));
311 memset(p
, 0, sizeof(*p
));
313 * Initially try a read/write open (to allow the inject
314 * method to work). If that fails due to permission
315 * issues, fall back to read-only. This allows a
316 * non-root user to be granted specific access to pcap
317 * capabilities via file permissions.
319 * XXX - we should have an API that has a flag that
320 * controls whether to open read-only or read-write,
321 * so that denial of permission to send (or inability
322 * to send, if sending packets isn't supported on
323 * the device in question) can be indicated at open
326 * XXX - we assume here that "pfopen()" does not, in fact, modify
327 * its argument, even though it takes a "char *" rather than a
328 * "const char *" as its first argument. That appears to be
329 * the case, at least on Digital UNIX 4.0.
331 p
->fd
= pfopen(device
, O_RDWR
);
332 if (p
->fd
== -1 && errno
== EACCES
)
333 p
->fd
= pfopen(device
, O_RDONLY
);
335 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "pf open: %s: %s\n\
336 your system may not be properly configured; see the packetfilter(4) man page\n",
337 device
, pcap_strerror(errno
));
340 p
->md
.OrigMissed
= -1;
341 enmode
= ENTSTAMP
|ENBATCH
|ENNONEXCL
;
344 if (ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
) < 0) {
345 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCMBIS: %s",
346 pcap_strerror(errno
));
350 /* Try to set COPYALL mode so that we see packets to ourself */
352 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
354 /* set the backlog */
355 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
356 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETW: %s",
357 pcap_strerror(errno
));
360 /* discover interface type */
361 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
362 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCDEVP: %s",
363 pcap_strerror(errno
));
366 /* HACK: to compile prior to Ultrix 4.2 */
370 switch (devparams
.end_dev_type
) {
373 p
->linktype
= DLT_EN10MB
;
376 * This is (presumably) a real Ethernet capture; give it a
377 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
378 * that an application can let you choose it, in case you're
379 * capturing DOCSIS traffic that a Cisco Cable Modem
380 * Termination System is putting out onto an Ethernet (it
381 * doesn't put an Ethernet header onto the wire, it puts raw
382 * DOCSIS frames out on the wire inside the low-level
385 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
387 * If that fails, just leave the list empty.
389 if (p
->dlt_list
!= NULL
) {
390 p
->dlt_list
[0] = DLT_EN10MB
;
391 p
->dlt_list
[1] = DLT_DOCSIS
;
397 p
->linktype
= DLT_FDDI
;
402 p
->linktype
= DLT_SLIP
;
408 p
->linktype
= DLT_PPP
;
415 * It appears to use Ethernet framing, at least on
418 p
->linktype
= DLT_EN10MB
;
425 p
->linktype
= DLT_IEEE802
;
431 * XXX - what about ENDT_IEEE802? The pfilt.h header
432 * file calls this "IEEE 802 networks (non-Ethernet)",
433 * but that doesn't specify a specific link layer type;
434 * it could be 802.4, or 802.5 (except that 802.5 is
435 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
436 * DLT_IEEE802 was hijacked to mean Token Ring in various
437 * BSDs, and why we went along with that hijacking.
439 * XXX - what about ENDT_HDLC and ENDT_NULL?
440 * Presumably, as ENDT_OTHER is just "Miscellaneous
441 * framing", there's not much we can do, as that
442 * doesn't specify a particular type of header.
444 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown data-link type %u",
445 devparams
.end_dev_type
);
450 if (p
->linktype
== DLT_FDDI
) {
451 p
->fddipad
= PCAP_FDDIPAD
;
453 /* packetfilter includes the padding in the snapshot */
454 snaplen
+= PCAP_FDDIPAD
;
458 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&snaplen
) < 0) {
459 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCTRUNCATE: %s",
460 pcap_strerror(errno
));
463 p
->snapshot
= snaplen
;
464 /* accept all packets */
465 memset(&Filter
, 0, sizeof(Filter
));
466 Filter
.enf_Priority
= 37; /* anything > 2 */
467 Filter
.enf_FilterLen
= 0; /* means "always true" */
468 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
469 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETF: %s",
470 pcap_strerror(errno
));
475 struct timeval timeout
;
476 timeout
.tv_sec
= to_ms
/ 1000;
477 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
478 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
479 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSRTIMEOUT: %s",
480 pcap_strerror(errno
));
485 p
->bufsize
= BUFSPACE
;
486 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
487 if (p
->buffer
== NULL
) {
488 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
493 * "select()" and "poll()" work on packetfilter devices.
495 p
->selectable_fd
= p
->fd
;
497 p
->read_op
= pcap_read_pf
;
498 p
->inject_op
= pcap_inject_pf
;
499 p
->setfilter_op
= pcap_setfilter_pf
;
500 p
->setdirection_op
= NULL
; /* Not implemented. */
501 p
->set_datalink_op
= NULL
; /* can't change data link type */
502 p
->getnonblock_op
= pcap_getnonblock_fd
;
503 p
->setnonblock_op
= pcap_setnonblock_fd
;
504 p
->stats_op
= pcap_stats_pf
;
505 p
->close_op
= pcap_close_common
;
512 * Get rid of any link-layer type list we allocated.
514 if (p
->dlt_list
!= NULL
)
521 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
527 pcap_setfilter_pf(pcap_t
*p
, struct bpf_program
*fp
)
529 struct bpf_version bv
;
532 * See if BIOCVERSION works. If not, we assume the kernel doesn't
533 * support BPF-style filters (it's not documented in the bpf(7)
534 * or packetfiler(7) man pages, but the code used to fail if
535 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
536 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
539 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) >= 0) {
541 * OK, we have the version of the BPF interpreter;
542 * is it the same major version as us, and the same
543 * or better minor version?
545 if (bv
.bv_major
== BPF_MAJOR_VERSION
&&
546 bv
.bv_minor
>= BPF_MINOR_VERSION
) {
548 * Yes. Try to install the filter.
550 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
551 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
552 "BIOCSETF: %s", pcap_strerror(errno
));
557 * OK, that succeeded. We're doing filtering in
558 * the kernel. (We assume we don't have a
559 * userland filter installed - that'd require
560 * a previous version check to have failed but
561 * this one to succeed.)
563 * XXX - this message should be supplied to the
564 * application as a warning of some sort,
565 * except that if it's a GUI application, it's
566 * not clear that it should be displayed in
567 * a window to annoy the user.
569 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
573 * Discard any previously-received packets,
574 * as they might have passed whatever filter
575 * was formerly in effect, but might not pass
576 * this filter (BIOCSETF discards packets buffered
577 * in the kernel, so you can lose packets in any
585 * We can't use the kernel's BPF interpreter; don't give
586 * up, just log a message and be inefficient.
588 * XXX - this should really be supplied to the application
589 * as a warning of some sort.
592 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
593 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
594 bv
.bv_major
, bv
.bv_minor
);
598 * We couldn't do filtering in the kernel; do it in userland.
600 if (install_bpf_program(p
, fp
) < 0)
604 * XXX - this message should be supplied by the application as
605 * a warning of some sort.
607 fprintf(stderr
, "tcpdump: Filtering in user process\n");