]> The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
Add a "setfilter" function pointer to the pcap_t structure, which
[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.76 2003-07-25 04:42:04 guy Exp $ (LBL)";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/timeb.h>
37 #include <sys/socket.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <net/pfilt.h>
41
42 struct mbuf;
43 struct rtentry;
44 #include <net/if.h>
45
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>
55
56 #include <ctype.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 /*
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.
67 */
68 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
69 #include <net/bpf.h>
70
71 #include "pcap-int.h"
72
73 #ifdef HAVE_OS_PROTO_H
74 #include "os-proto.h"
75 #endif
76
77 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
78
79 /*
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).
84 */
85 #define BUFSPACE (200 * 256)
86
87 int
88 pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
89 {
90 register u_char *p, *bp;
91 struct bpf_insn *fcode;
92 register int cc, n, buflen, inc;
93 register struct enstamp *sp;
94 #ifdef LBL_ALIGN
95 struct enstamp stamp;
96 #endif
97 #ifdef PCAP_FDDIPAD
98 register int pad;
99 #endif
100
101 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns;
102 again:
103 cc = pc->cc;
104 if (cc == 0) {
105 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
106 if (cc < 0) {
107 if (errno == EWOULDBLOCK)
108 return (0);
109 if (errno == EINVAL &&
110 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
111 /*
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.
116 */
117 (void)lseek(pc->fd, 0L, SEEK_SET);
118 goto again;
119 }
120 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
121 pcap_strerror(errno));
122 return (-1);
123 }
124 bp = pc->buffer + pc->offset;
125 } else
126 bp = pc->bp;
127 /*
128 * Loop through each packet.
129 */
130 n = 0;
131 #ifdef PCAP_FDDIPAD
132 if (pc->linktype == DLT_FDDI)
133 pad = pcap_fddipad;
134 else
135 pad = 0;
136 #endif
137 while (cc > 0) {
138 if (cc < sizeof(*sp)) {
139 snprintf(pc->errbuf, sizeof(pc->errbuf),
140 "pf short read (%d)", cc);
141 return (-1);
142 }
143 #ifdef LBL_ALIGN
144 if ((long)bp & 3) {
145 sp = &stamp;
146 memcpy((char *)sp, (char *)bp, sizeof(*sp));
147 } else
148 #endif
149 sp = (struct enstamp *)bp;
150 if (sp->ens_stamplen != sizeof(*sp)) {
151 snprintf(pc->errbuf, sizeof(pc->errbuf),
152 "pf short stamplen (%d)",
153 sp->ens_stamplen);
154 return (-1);
155 }
156
157 p = bp + sp->ens_stamplen;
158 buflen = sp->ens_count;
159 if (buflen > pc->snapshot)
160 buflen = pc->snapshot;
161
162 /* Calculate inc before possible pad update */
163 inc = ENALIGN(buflen + sp->ens_stamplen);
164 cc -= inc;
165 bp += inc;
166 #ifdef PCAP_FDDIPAD
167 p += pad;
168 buflen -= pad;
169 #endif
170 pc->md.TotPkts++;
171 pc->md.TotDrops += sp->ens_dropped;
172 pc->md.TotMissed = sp->ens_ifoverflows;
173 if (pc->md.OrigMissed < 0)
174 pc->md.OrigMissed = pc->md.TotMissed;
175
176 /*
177 * Short-circuit evaluation: if using BPF filter
178 * in kernel, no need to do it now.
179 */
180 if (fcode == NULL ||
181 bpf_filter(fcode, p, sp->ens_count, buflen)) {
182 struct pcap_pkthdr h;
183 pc->md.TotAccepted++;
184 h.ts = sp->ens_tstamp;
185 #ifdef PCAP_FDDIPAD
186 h.len = sp->ens_count - pad;
187 #else
188 h.len = sp->ens_count;
189 #endif
190 h.caplen = buflen;
191 (*callback)(user, &h, p);
192 if (++n >= cnt && cnt > 0) {
193 pc->cc = cc;
194 pc->bp = bp;
195 return (n);
196 }
197 }
198 }
199 pc->cc = 0;
200 return (n);
201 }
202
203 static int
204 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
205 {
206
207 /*
208 * If packet filtering is being done in the kernel:
209 *
210 * "ps_recv" counts only packets that passed the filter.
211 * This does not include packets dropped because we
212 * ran out of buffer space. (XXX - perhaps it should,
213 * by adding "ps_drop" to "ps_recv", for compatibility
214 * with some other platforms. On the other hand, on
215 * some platforms "ps_recv" counts only packets that
216 * passed the filter, and on others it counts packets
217 * that didn't pass the filter....)
218 *
219 * "ps_drop" counts packets that passed the kernel filter
220 * (if any) but were dropped because the input queue was
221 * full.
222 *
223 * "ps_ifdrop" counts packets dropped by the network
224 * inteface (regardless of whether they would have passed
225 * the input filter, of course).
226 *
227 * If packet filtering is not being done in the kernel:
228 *
229 * "ps_recv" counts only packets that passed the filter.
230 *
231 * "ps_drop" counts packets that were dropped because the
232 * input queue was full, regardless of whether they passed
233 * the userland filter.
234 *
235 * "ps_ifdrop" counts packets dropped by the network
236 * inteface (regardless of whether they would have passed
237 * the input filter, of course).
238 *
239 * These statistics don't include packets not yet read from
240 * the kernel by libpcap, but they may include packets not
241 * yet read from libpcap by the application.
242 */
243 ps->ps_recv = p->md.TotAccepted;
244 ps->ps_drop = p->md.TotDrops;
245 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
246 return (0);
247 }
248
249 static void
250 pcap_close_pf(pcap_t *p)
251 {
252 if (p->buffer != NULL)
253 free(p->buffer);
254 if (p->fd >= 0)
255 close(p->fd);
256 }
257
258 pcap_t *
259 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
260 char *ebuf)
261 {
262 pcap_t *p;
263 short enmode;
264 int backlog = -1; /* request the most */
265 struct enfilter Filter;
266 struct endevp devparams;
267
268 p = (pcap_t *)malloc(sizeof(*p));
269 if (p == NULL) {
270 snprintf(ebuf, PCAP_ERRBUF_SIZE,
271 "pcap_open_live: %s", pcap_strerror(errno));
272 return (0);
273 }
274 memset(p, 0, sizeof(*p));
275
276 /*
277 * XXX - we assume here that "pfopen()" does not, in fact, modify
278 * its argument, even though it takes a "char *" rather than a
279 * "const char *" as its first argument. That appears to be
280 * the case, at least on Digital UNIX 4.0.
281 */
282 p->fd = pfopen(device, O_RDONLY);
283 if (p->fd < 0) {
284 snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
285 your system may not be properly configured; see the packetfilter(4) man page\n",
286 device, pcap_strerror(errno));
287 goto bad;
288 }
289 p->md.OrigMissed = -1;
290 enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
291 if (promisc)
292 enmode |= ENPROMISC;
293 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
294 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
295 pcap_strerror(errno));
296 goto bad;
297 }
298 #ifdef ENCOPYALL
299 /* Try to set COPYALL mode so that we see packets to ourself */
300 enmode = ENCOPYALL;
301 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
302 #endif
303 /* set the backlog */
304 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
305 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
306 pcap_strerror(errno));
307 goto bad;
308 }
309 /* discover interface type */
310 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
311 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
312 pcap_strerror(errno));
313 goto bad;
314 }
315 /* HACK: to compile prior to Ultrix 4.2 */
316 #ifndef ENDT_FDDI
317 #define ENDT_FDDI 4
318 #endif
319 switch (devparams.end_dev_type) {
320
321 case ENDT_10MB:
322 p->linktype = DLT_EN10MB;
323 p->offset = 2;
324 break;
325
326 case ENDT_FDDI:
327 p->linktype = DLT_FDDI;
328 break;
329
330 #ifdef ENDT_SLIP
331 case ENDT_SLIP:
332 p->linktype = DLT_SLIP;
333 break;
334 #endif
335
336 #ifdef ENDT_PPP
337 case ENDT_PPP:
338 p->linktype = DLT_PPP;
339 break;
340 #endif
341
342 #ifdef ENDT_LOOPBACK
343 case ENDT_LOOPBACK:
344 /*
345 * It appears to use Ethernet framing, at least on
346 * Digital UNIX 4.0.
347 */
348 p->linktype = DLT_EN10MB;
349 p->offset = 2;
350 break;
351 #endif
352
353 #ifdef ENDT_TRN
354 case ENDT_TRN:
355 p->linktype = DLT_IEEE802;
356 break;
357 #endif
358
359 default:
360 /*
361 * XXX - what about ENDT_IEEE802? The pfilt.h header
362 * file calls this "IEEE 802 networks (non-Ethernet)",
363 * but that doesn't specify a specific link layer type;
364 * it could be 802.4, or 802.5 (except that 802.5 is
365 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
366 * DLT_IEEE802 was hijacked to mean Token Ring in various
367 * BSDs, and why we went along with that hijacking.
368 *
369 * XXX - what about ENDT_HDLC and ENDT_NULL?
370 * Presumably, as ENDT_OTHER is just "Miscellaneous
371 * framing", there's not much we can do, as that
372 * doesn't specify a particular type of header.
373 */
374 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u",
375 devparams.end_dev_type);
376 goto bad;
377 }
378 /* set truncation */
379 #ifdef PCAP_FDDIPAD
380 if (p->linktype == DLT_FDDI)
381 /* packetfilter includes the padding in the snapshot */
382 snaplen += pcap_fddipad;
383 #endif
384 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
385 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
386 pcap_strerror(errno));
387 goto bad;
388 }
389 p->snapshot = snaplen;
390 /* accept all packets */
391 memset(&Filter, 0, sizeof(Filter));
392 Filter.enf_Priority = 37; /* anything > 2 */
393 Filter.enf_FilterLen = 0; /* means "always true" */
394 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
395 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
396 pcap_strerror(errno));
397 goto bad;
398 }
399
400 if (to_ms != 0) {
401 struct timeval timeout;
402 timeout.tv_sec = to_ms / 1000;
403 timeout.tv_usec = (to_ms * 1000) % 1000000;
404 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
405 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
406 pcap_strerror(errno));
407 goto bad;
408 }
409 }
410
411 p->bufsize = BUFSPACE;
412 p->buffer = (u_char*)malloc(p->bufsize + p->offset);
413 if (p->buffer == NULL) {
414 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
415 goto bad;
416 }
417
418 p->setfilter_op = pcap_setfilter_pf;
419 p->stats_op = pcap_stats_pf;
420 p->close_op = pcap_close_pf;
421
422 return (p);
423 bad:
424 if (p->fd >= 0)
425 close(p->fd);
426 free(p);
427 return (NULL);
428 }
429
430 int
431 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
432 {
433 return (0);
434 }
435
436 static int
437 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
438 {
439 /*
440 * See if BIOCSETF works. If it does, the kernel supports
441 * BPF-style filters, and we do not need to do post-filtering.
442 */
443 p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
444 if (p->md.use_bpf) {
445 struct bpf_version bv;
446
447 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
448 snprintf(p->errbuf, sizeof(p->errbuf),
449 "BIOCVERSION: %s", pcap_strerror(errno));
450 return (-1);
451 }
452 else if (bv.bv_major != BPF_MAJOR_VERSION ||
453 bv.bv_minor < BPF_MINOR_VERSION) {
454 fprintf(stderr,
455 "requires bpf language %d.%d or higher; kernel is %d.%d",
456 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
457 bv.bv_major, bv.bv_minor);
458 /* don't give up, just be inefficient */
459 p->md.use_bpf = 0;
460 }
461 } else {
462 if (install_bpf_program(p, fp) < 0)
463 return (-1);
464 }
465
466 /*XXX this goes in tcpdump*/
467 if (p->md.use_bpf)
468 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
469 else
470 fprintf(stderr, "tcpdump: Filtering in user process\n");
471 return (0);
472 }
473
474 int
475 pcap_set_datalink_platform(pcap_t *p, int dlt)
476 {
477 return (0);
478 }