]>
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.54 1999-10-07 23:46:40 mcr Exp $ (LBL)";
30 #include <sys/types.h>
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <net/pfilt.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/if_ether.h>
49 #include <netinet/ip_var.h>
50 #include <netinet/udp.h>
51 #include <netinet/udp_var.h>
52 #include <netinet/tcp.h>
53 #include <netinet/tcpip.h>
66 #ifdef HAVE_OS_PROTO_H
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).
76 #define BUFSPACE (200 * 256)
79 pcap_read(pcap_t
*pc
, int cnt
, pcap_handler callback
, u_char
*user
)
81 register u_char
*p
, *bp
;
82 struct bpf_insn
*fcode
;
83 register int cc
, n
, buflen
, inc
;
84 register struct enstamp
*sp
;
92 fcode
= pc
->md
.use_bpf
? NULL
: pc
->fcode
.bf_insns
;
96 cc
= read(pc
->fd
, (char *)pc
->buffer
+ pc
->offset
, pc
->bufsize
);
98 if (errno
== EWOULDBLOCK
)
100 if (errno
== EINVAL
&&
101 lseek(pc
->fd
, 0L, SEEK_CUR
) + pc
->bufsize
< 0) {
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.
108 (void)lseek(pc
->fd
, 0L, SEEK_SET
);
111 sprintf(pc
->errbuf
, "pf read: %s",
112 pcap_strerror(errno
));
115 bp
= pc
->buffer
+ pc
->offset
;
119 * Loop through each packet.
123 if (pc
->linktype
== DLT_FDDI
)
129 if (cc
< sizeof(*sp
)) {
130 sprintf(pc
->errbuf
, "pf short read (%d)", cc
);
136 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
139 sp
= (struct enstamp
*)bp
;
140 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
141 sprintf(pc
->errbuf
, "pf short stamplen (%d)",
146 p
= bp
+ sp
->ens_stamplen
;
147 buflen
= sp
->ens_count
;
148 if (buflen
> pc
->snapshot
)
149 buflen
= pc
->snapshot
;
151 /* Calculate inc before possible pad update */
152 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
160 pc
->md
.TotDrops
+= sp
->ens_dropped
;
161 pc
->md
.TotMissed
= sp
->ens_ifoverflows
;
162 if (pc
->md
.OrigMissed
< 0)
163 pc
->md
.OrigMissed
= pc
->md
.TotMissed
;
166 * Short-circuit evaluation: if using BPF filter
167 * in kernel, no need to do it now.
170 bpf_filter(fcode
, p
, sp
->ens_count
, buflen
)) {
171 struct pcap_pkthdr h
;
172 pc
->md
.TotAccepted
++;
173 h
.ts
= sp
->ens_tstamp
;
175 h
.len
= sp
->ens_count
- pad
;
177 h
.len
= sp
->ens_count
;
180 (*callback
)(user
, &h
, p
);
181 if (++n
>= cnt
&& cnt
> 0) {
193 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
196 ps
->ps_recv
= p
->md
.TotAccepted
;
197 ps
->ps_drop
= p
->md
.TotDrops
;
198 ps
->ps_ifdrop
= p
->md
.TotMissed
- p
->md
.OrigMissed
;
203 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
207 int backlog
= -1; /* request the most */
208 struct enfilter Filter
;
209 struct endevp devparams
;
211 p
= (pcap_t
*)malloc(sizeof(*p
));
213 sprintf(ebuf
, "pcap_open_live: %s", pcap_strerror(errno
));
216 bzero((char *)p
, sizeof(*p
));
217 p
->fd
= pfopen(device
, O_RDONLY
);
219 sprintf(ebuf
, "pf open: %s: %s\n\
220 your system may not be properly configured; see \"man packetfilter(4)\"\n",
221 device
, pcap_strerror(errno
));
224 p
->md
.OrigMissed
= -1;
225 enmode
= ENTSTAMP
|ENBATCH
|ENNONEXCL
;
228 if (ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
) < 0) {
229 sprintf(ebuf
, "EIOCMBIS: %s", pcap_strerror(errno
));
233 /* Try to set COPYALL mode so that we see packets to ourself */
235 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
237 /* set the backlog */
238 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
239 sprintf(ebuf
, "EIOCSETW: %s", pcap_strerror(errno
));
242 /* discover interface type */
243 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
244 sprintf(ebuf
, "EIOCDEVP: %s", pcap_strerror(errno
));
247 /* HACK: to compile prior to Ultrix 4.2 */
251 switch (devparams
.end_dev_type
) {
254 p
->linktype
= DLT_EN10MB
;
259 p
->linktype
= DLT_FDDI
;
265 * Currently, the Ultrix packet filter supports only
266 * Ethernet and FDDI. Eventually, support for SLIP and PPP
267 * (and possibly others: T1?) should be added.
271 "Packet filter data-link type %d unknown, assuming Ethernet",
272 devparams
.end_dev_type
);
274 p
->linktype
= DLT_EN10MB
;
280 if (p
->linktype
== DLT_FDDI
)
281 /* packetfilter includes the padding in the snapshot */
282 snaplen
+= pcap_fddipad
;
284 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&snaplen
) < 0) {
285 sprintf(ebuf
, "EIOCTRUNCATE: %s", pcap_strerror(errno
));
288 p
->snapshot
= snaplen
;
289 /* accept all packets */
290 bzero((char *)&Filter
, sizeof(Filter
));
291 Filter
.enf_Priority
= 37; /* anything > 2 */
292 Filter
.enf_FilterLen
= 0; /* means "always true" */
293 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
294 sprintf(ebuf
, "EIOCSETF: %s", pcap_strerror(errno
));
299 struct timeval timeout
;
300 timeout
.tv_sec
= to_ms
/ 1000;
301 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
302 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
303 sprintf(ebuf
, "EIOCSRTIMEOUT: %s",
304 pcap_strerror(errno
));
308 p
->bufsize
= BUFSPACE
;
309 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
318 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
321 * See if BIOCSETF works. If it does, the kernel supports
322 * BPF-style filters, and we do not need to do post-filtering.
324 p
->md
.use_bpf
= (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) >= 0);
326 struct bpf_version bv
;
328 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
329 sprintf(p
->errbuf
, "BIOCVERSION: %s",
330 pcap_strerror(errno
));
333 else if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
334 bv
.bv_minor
< BPF_MINOR_VERSION
) {
336 "requires bpf language %d.%d or higher; kernel is %d.%d",
337 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
338 bv
.bv_major
, bv
.bv_minor
);
339 /* don't give up, just be inefficient */
345 /*XXX this goes in tcpdump*/
347 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
349 fprintf(stderr
, "tcpdump: Filtering in user process\n");