]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
1333a132ddf8aad2864fc8af4d30885f19c2337e
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.57 2000-07-01 03:35:08 assar 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>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/if_ether.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcpip.h>
63 #ifdef HAVE_OS_PROTO_H
68 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
69 * applications aren't going to need more than 200 bytes of packet header
70 * and the read shouldn't return more packets than packetfilter's internal
71 * queue limit (bounded at 256).
73 #define BUFSPACE (200 * 256)
76 pcap_read(pcap_t
*pc
, int cnt
, pcap_handler callback
, u_char
*user
)
78 register u_char
*p
, *bp
;
79 struct bpf_insn
*fcode
;
80 register int cc
, n
, buflen
, inc
;
81 register struct enstamp
*sp
;
89 fcode
= pc
->md
.use_bpf
? NULL
: pc
->fcode
.bf_insns
;
93 cc
= read(pc
->fd
, (char *)pc
->buffer
+ pc
->offset
, pc
->bufsize
);
95 if (errno
== EWOULDBLOCK
)
97 if (errno
== EINVAL
&&
98 lseek(pc
->fd
, 0L, SEEK_CUR
) + pc
->bufsize
< 0) {
100 * Due to a kernel bug, after 2^31 bytes,
101 * the kernel file offset overflows and
102 * read fails with EINVAL. The lseek()
103 * to 0 will fix things.
105 (void)lseek(pc
->fd
, 0L, SEEK_SET
);
108 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
), "pf read: %s",
109 pcap_strerror(errno
));
112 bp
= pc
->buffer
+ pc
->offset
;
116 * Loop through each packet.
120 if (pc
->linktype
== DLT_FDDI
)
126 if (cc
< sizeof(*sp
)) {
127 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
128 "pf short read (%d)", cc
);
134 memcpy((char *)sp
, (char *)bp
, sizeof(*sp
));
137 sp
= (struct enstamp
*)bp
;
138 if (sp
->ens_stamplen
!= sizeof(*sp
)) {
139 snprintf(pc
->errbuf
, sizeof(pc
->errbuf
),
140 "pf short stamplen (%d)",
145 p
= bp
+ sp
->ens_stamplen
;
146 buflen
= sp
->ens_count
;
147 if (buflen
> pc
->snapshot
)
148 buflen
= pc
->snapshot
;
150 /* Calculate inc before possible pad update */
151 inc
= ENALIGN(buflen
+ sp
->ens_stamplen
);
159 pc
->md
.TotDrops
+= sp
->ens_dropped
;
160 pc
->md
.TotMissed
= sp
->ens_ifoverflows
;
161 if (pc
->md
.OrigMissed
< 0)
162 pc
->md
.OrigMissed
= pc
->md
.TotMissed
;
165 * Short-circuit evaluation: if using BPF filter
166 * in kernel, no need to do it now.
169 bpf_filter(fcode
, p
, sp
->ens_count
, buflen
)) {
170 struct pcap_pkthdr h
;
171 pc
->md
.TotAccepted
++;
172 h
.ts
= sp
->ens_tstamp
;
174 h
.len
= sp
->ens_count
- pad
;
176 h
.len
= sp
->ens_count
;
179 (*callback
)(user
, &h
, p
);
180 if (++n
>= cnt
&& cnt
> 0) {
192 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
195 ps
->ps_recv
= p
->md
.TotAccepted
;
196 ps
->ps_drop
= p
->md
.TotDrops
;
197 ps
->ps_ifdrop
= p
->md
.TotMissed
- p
->md
.OrigMissed
;
202 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
206 int backlog
= -1; /* request the most */
207 struct enfilter Filter
;
208 struct endevp devparams
;
210 p
= (pcap_t
*)malloc(sizeof(*p
));
212 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
213 "pcap_open_live: %s", pcap_strerror(errno
));
216 bzero((char *)p
, sizeof(*p
));
217 p
->fd
= pfopen(device
, O_RDONLY
);
219 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "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 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCMBIS: %s",
230 pcap_strerror(errno
));
234 /* Try to set COPYALL mode so that we see packets to ourself */
236 (void)ioctl(p
->fd
, EIOCMBIS
, (caddr_t
)&enmode
);/* OK if this fails */
238 /* set the backlog */
239 if (ioctl(p
->fd
, EIOCSETW
, (caddr_t
)&backlog
) < 0) {
240 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETW: %s",
241 pcap_strerror(errno
));
244 /* discover interface type */
245 if (ioctl(p
->fd
, EIOCDEVP
, (caddr_t
)&devparams
) < 0) {
246 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCDEVP: %s",
247 pcap_strerror(errno
));
250 /* HACK: to compile prior to Ultrix 4.2 */
254 switch (devparams
.end_dev_type
) {
257 p
->linktype
= DLT_EN10MB
;
262 p
->linktype
= DLT_FDDI
;
268 * Currently, the Ultrix packet filter supports only
269 * Ethernet and FDDI. Eventually, support for SLIP and PPP
270 * (and possibly others: T1?) should be added.
274 "Packet filter data-link type %d unknown, assuming Ethernet",
275 devparams
.end_dev_type
);
277 p
->linktype
= DLT_EN10MB
;
283 if (p
->linktype
== DLT_FDDI
)
284 /* packetfilter includes the padding in the snapshot */
285 snaplen
+= pcap_fddipad
;
287 if (ioctl(p
->fd
, EIOCTRUNCATE
, (caddr_t
)&snaplen
) < 0) {
288 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCTRUNCATE: %s",
289 pcap_strerror(errno
));
292 p
->snapshot
= snaplen
;
293 /* accept all packets */
294 bzero((char *)&Filter
, sizeof(Filter
));
295 Filter
.enf_Priority
= 37; /* anything > 2 */
296 Filter
.enf_FilterLen
= 0; /* means "always true" */
297 if (ioctl(p
->fd
, EIOCSETF
, (caddr_t
)&Filter
) < 0) {
298 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSETF: %s",
299 pcap_strerror(errno
));
304 struct timeval timeout
;
305 timeout
.tv_sec
= to_ms
/ 1000;
306 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
307 if (ioctl(p
->fd
, EIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
308 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "EIOCSRTIMEOUT: %s",
309 pcap_strerror(errno
));
313 p
->bufsize
= BUFSPACE
;
314 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
323 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
326 * See if BIOCSETF works. If it does, the kernel supports
327 * BPF-style filters, and we do not need to do post-filtering.
329 p
->md
.use_bpf
= (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) >= 0);
331 struct bpf_version bv
;
333 if (ioctl(p
->fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
334 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
335 "BIOCVERSION: %s", pcap_strerror(errno
));
338 else if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
339 bv
.bv_minor
< BPF_MINOR_VERSION
) {
341 "requires bpf language %d.%d or higher; kernel is %d.%d",
342 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
,
343 bv
.bv_major
, bv
.bv_minor
);
344 /* don't give up, just be inefficient */
350 /*XXX this goes in tcpdump*/
352 fprintf(stderr
, "tcpdump: Using kernel BPF filter\n");
354 fprintf(stderr
, "tcpdump: Filtering in user process\n");