]> The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
1333a132ddf8aad2864fc8af4d30885f19c2337e
[libpcap] / pcap-pf.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 *
21 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 */
24
25 #ifndef lint
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)";
28 #endif
29
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
34 #include <sys/file.h>
35 #include <sys/ioctl.h>
36 #include <net/pfilt.h>
37
38 struct mbuf;
39 struct rtentry;
40 #include <net/if.h>
41
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>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <netdb.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include "pcap-int.h"
61
62 #include "gnuc.h"
63 #ifdef HAVE_OS_PROTO_H
64 #include "os-proto.h"
65 #endif
66
67 /*
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).
72 */
73 #define BUFSPACE (200 * 256)
74
75 int
76 pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
77 {
78 register u_char *p, *bp;
79 struct bpf_insn *fcode;
80 register int cc, n, buflen, inc;
81 register struct enstamp *sp;
82 #ifdef LBL_ALIGN
83 struct enstamp stamp;
84 #endif
85 #ifdef PCAP_FDDIPAD
86 register int pad;
87 #endif
88
89 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns;
90 again:
91 cc = pc->cc;
92 if (cc == 0) {
93 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
94 if (cc < 0) {
95 if (errno == EWOULDBLOCK)
96 return (0);
97 if (errno == EINVAL &&
98 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
99 /*
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.
104 */
105 (void)lseek(pc->fd, 0L, SEEK_SET);
106 goto again;
107 }
108 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
109 pcap_strerror(errno));
110 return (-1);
111 }
112 bp = pc->buffer + pc->offset;
113 } else
114 bp = pc->bp;
115 /*
116 * Loop through each packet.
117 */
118 n = 0;
119 #ifdef PCAP_FDDIPAD
120 if (pc->linktype == DLT_FDDI)
121 pad = pcap_fddipad;
122 else
123 pad = 0;
124 #endif
125 while (cc > 0) {
126 if (cc < sizeof(*sp)) {
127 snprintf(pc->errbuf, sizeof(pc->errbuf),
128 "pf short read (%d)", cc);
129 return (-1);
130 }
131 #ifdef LBL_ALIGN
132 if ((long)bp & 3) {
133 sp = &stamp;
134 memcpy((char *)sp, (char *)bp, sizeof(*sp));
135 } else
136 #endif
137 sp = (struct enstamp *)bp;
138 if (sp->ens_stamplen != sizeof(*sp)) {
139 snprintf(pc->errbuf, sizeof(pc->errbuf),
140 "pf short stamplen (%d)",
141 sp->ens_stamplen);
142 return (-1);
143 }
144
145 p = bp + sp->ens_stamplen;
146 buflen = sp->ens_count;
147 if (buflen > pc->snapshot)
148 buflen = pc->snapshot;
149
150 /* Calculate inc before possible pad update */
151 inc = ENALIGN(buflen + sp->ens_stamplen);
152 cc -= inc;
153 bp += inc;
154 #ifdef PCAP_FDDIPAD
155 p += pad;
156 buflen -= pad;
157 #endif
158 pc->md.TotPkts++;
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;
163
164 /*
165 * Short-circuit evaluation: if using BPF filter
166 * in kernel, no need to do it now.
167 */
168 if (fcode == NULL ||
169 bpf_filter(fcode, p, sp->ens_count, buflen)) {
170 struct pcap_pkthdr h;
171 pc->md.TotAccepted++;
172 h.ts = sp->ens_tstamp;
173 #ifdef PCAP_FDDIPAD
174 h.len = sp->ens_count - pad;
175 #else
176 h.len = sp->ens_count;
177 #endif
178 h.caplen = buflen;
179 (*callback)(user, &h, p);
180 if (++n >= cnt && cnt > 0) {
181 pc->cc = cc;
182 pc->bp = bp;
183 return (n);
184 }
185 }
186 }
187 pc->cc = 0;
188 return (n);
189 }
190
191 int
192 pcap_stats(pcap_t *p, struct pcap_stat *ps)
193 {
194
195 ps->ps_recv = p->md.TotAccepted;
196 ps->ps_drop = p->md.TotDrops;
197 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
198 return (0);
199 }
200
201 pcap_t *
202 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
203 {
204 pcap_t *p;
205 short enmode;
206 int backlog = -1; /* request the most */
207 struct enfilter Filter;
208 struct endevp devparams;
209
210 p = (pcap_t *)malloc(sizeof(*p));
211 if (p == NULL) {
212 snprintf(ebuf, PCAP_ERRBUF_SIZE,
213 "pcap_open_live: %s", pcap_strerror(errno));
214 return (0);
215 }
216 bzero((char *)p, sizeof(*p));
217 p->fd = pfopen(device, O_RDONLY);
218 if (p->fd < 0) {
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));
222 goto bad;
223 }
224 p->md.OrigMissed = -1;
225 enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
226 if (promisc)
227 enmode |= ENPROMISC;
228 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
229 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
230 pcap_strerror(errno));
231 goto bad;
232 }
233 #ifdef ENCOPYALL
234 /* Try to set COPYALL mode so that we see packets to ourself */
235 enmode = ENCOPYALL;
236 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
237 #endif
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));
242 goto bad;
243 }
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));
248 goto bad;
249 }
250 /* HACK: to compile prior to Ultrix 4.2 */
251 #ifndef ENDT_FDDI
252 #define ENDT_FDDI 4
253 #endif
254 switch (devparams.end_dev_type) {
255
256 case ENDT_10MB:
257 p->linktype = DLT_EN10MB;
258 p->offset = 2;
259 break;
260
261 case ENDT_FDDI:
262 p->linktype = DLT_FDDI;
263 break;
264
265 default:
266 /*
267 * XXX
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.
271 */
272 #ifdef notdef
273 warning(
274 "Packet filter data-link type %d unknown, assuming Ethernet",
275 devparams.end_dev_type);
276 #endif
277 p->linktype = DLT_EN10MB;
278 p->offset = 2;
279 break;
280 }
281 /* set truncation */
282 #ifdef PCAP_FDDIPAD
283 if (p->linktype == DLT_FDDI)
284 /* packetfilter includes the padding in the snapshot */
285 snaplen += pcap_fddipad;
286 #endif
287 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
288 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
289 pcap_strerror(errno));
290 goto bad;
291 }
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));
300 goto bad;
301 }
302
303 if (to_ms != 0) {
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));
310 goto bad;
311 }
312 }
313 p->bufsize = BUFSPACE;
314 p->buffer = (u_char*)malloc(p->bufsize + p->offset);
315
316 return (p);
317 bad:
318 free(p);
319 return (NULL);
320 }
321
322 int
323 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
324 {
325 /*
326 * See if BIOCSETF works. If it does, the kernel supports
327 * BPF-style filters, and we do not need to do post-filtering.
328 */
329 p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
330 if (p->md.use_bpf) {
331 struct bpf_version bv;
332
333 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
334 snprintf(p->errbuf, sizeof(p->errbuf),
335 "BIOCVERSION: %s", pcap_strerror(errno));
336 return (-1);
337 }
338 else if (bv.bv_major != BPF_MAJOR_VERSION ||
339 bv.bv_minor < BPF_MINOR_VERSION) {
340 fprintf(stderr,
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 */
345 p->md.use_bpf = 0;
346 }
347 } else
348 p->fcode = *fp;
349
350 /*XXX this goes in tcpdump*/
351 if (p->md.use_bpf)
352 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
353 else
354 fprintf(stderr, "tcpdump: Filtering in user process\n");
355 return (0);
356 }