xref: /freebsd/contrib/libpcap/pcap-pf.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/timeb.h>
32 #include <sys/socket.h>
33 #include <sys/file.h>
34 #include <sys/ioctl.h>
35 #include <net/pfilt.h>
36 
37 struct mbuf;
38 struct rtentry;
39 #include <net/if.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/if_ether.h>
45 #include <netinet/ip_var.h>
46 #include <netinet/udp.h>
47 #include <netinet/udp_var.h>
48 #include <netinet/tcp.h>
49 #include <netinet/tcpip.h>
50 
51 #include <errno.h>
52 #include <netdb.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 /*
59  * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
60  * native OS version, as we need various BPF ioctls from it.
61  */
62 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
63 #include <net/bpf.h>
64 
65 #include "pcap-int.h"
66 
67 #ifdef HAVE_OS_PROTO_H
68 #include "os-proto.h"
69 #endif
70 
71 /*
72  * FDDI packets are padded to make everything line up on a nice boundary.
73  */
74 #define       PCAP_FDDIPAD 3
75 
76 /*
77  * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
78  * Tru64 UNIX packetfilter devices.
79  */
80 struct pcap_pf {
81 	int	filtering_in_kernel; /* using kernel filter */
82 	u_long	TotPkts;	/* can't oflow for 79 hrs on ether */
83 	u_long	TotAccepted;	/* count accepted by filter */
84 	u_long	TotDrops;	/* count of dropped packets */
85 	long	TotMissed;	/* missed by i/f during this run */
86 	long	OrigMissed;	/* missed by i/f before this run */
87 };
88 
89 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
90 
91 /*
92  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
93  * applications aren't going to need more than 200 bytes of packet header
94  * and the read shouldn't return more packets than packetfilter's internal
95  * queue limit (bounded at 256).
96  */
97 #define BUFSPACE (200 * 256)
98 
99 static int
100 pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
101 {
102 	struct pcap_pf *pf = pc->priv;
103 	register u_char *p, *bp;
104 	register int cc, n, buflen, inc;
105 	register struct enstamp *sp;
106 	struct enstamp stamp;
107 	register u_int pad;
108 
109  again:
110 	cc = pc->cc;
111 	if (cc == 0) {
112 		cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
113 		if (cc < 0) {
114 			if (errno == EWOULDBLOCK)
115 				return (0);
116 			if (errno == EINVAL &&
117 			    lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
118 				/*
119 				 * Due to a kernel bug, after 2^31 bytes,
120 				 * the kernel file offset overflows and
121 				 * read fails with EINVAL. The lseek()
122 				 * to 0 will fix things.
123 				 */
124 				(void)lseek(pc->fd, 0L, SEEK_SET);
125 				goto again;
126 			}
127 			pcap_fmt_errmsg_for_errno(pc->errbuf,
128 			    sizeof(pc->errbuf), errno, "pf read");
129 			return (-1);
130 		}
131 		bp = (u_char *)pc->buffer + pc->offset;
132 	} else
133 		bp = pc->bp;
134 	/*
135 	 * Loop through each packet.
136 	 *
137 	 * This assumes that a single buffer of packets will have
138 	 * <= INT_MAX packets, so the packet count doesn't overflow.
139 	 */
140 	n = 0;
141 	pad = pc->fddipad;
142 	while (cc > 0) {
143 		/*
144 		 * Has "pcap_breakloop()" been called?
145 		 * If so, return immediately - if we haven't read any
146 		 * packets, clear the flag and return -2 to indicate
147 		 * that we were told to break out of the loop, otherwise
148 		 * leave the flag set, so that the *next* call will break
149 		 * out of the loop without having read any packets, and
150 		 * return the number of packets we've processed so far.
151 		 */
152 		if (pc->break_loop) {
153 			if (n == 0) {
154 				pc->break_loop = 0;
155 				return (-2);
156 			} else {
157 				pc->cc = cc;
158 				pc->bp = bp;
159 				return (n);
160 			}
161 		}
162 		if (cc < sizeof(*sp)) {
163 			snprintf(pc->errbuf, sizeof(pc->errbuf),
164 			    "pf short read (%d)", cc);
165 			return (-1);
166 		}
167 		if ((long)bp & 3) {
168 			sp = &stamp;
169 			memcpy((char *)sp, (char *)bp, sizeof(*sp));
170 		} else
171 			sp = (struct enstamp *)bp;
172 		if (sp->ens_stamplen != sizeof(*sp)) {
173 			snprintf(pc->errbuf, sizeof(pc->errbuf),
174 			    "pf short stamplen (%d)",
175 			    sp->ens_stamplen);
176 			return (-1);
177 		}
178 
179 		p = bp + sp->ens_stamplen;
180 		buflen = sp->ens_count;
181 		if (buflen > pc->snapshot)
182 			buflen = pc->snapshot;
183 
184 		/* Calculate inc before possible pad update */
185 		inc = ENALIGN(buflen + sp->ens_stamplen);
186 		cc -= inc;
187 		bp += inc;
188 		pf->TotPkts++;
189 		pf->TotDrops += sp->ens_dropped;
190 		pf->TotMissed = sp->ens_ifoverflows;
191 		if (pf->OrigMissed < 0)
192 			pf->OrigMissed = pf->TotMissed;
193 
194 		/*
195 		 * Short-circuit evaluation: if using BPF filter
196 		 * in kernel, no need to do it now - we already know
197 		 * the packet passed the filter.
198 		 *
199 		 * Note: the filter code was generated assuming
200 		 * that pc->fddipad was the amount of padding
201 		 * before the header, as that's what's required
202 		 * in the kernel, so we run the filter before
203 		 * skipping that padding.
204 		 */
205 		if (pf->filtering_in_kernel ||
206 		    pcap_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
207 			struct pcap_pkthdr h;
208 			pf->TotAccepted++;
209 			h.ts = sp->ens_tstamp;
210 			h.len = sp->ens_count - pad;
211 			p += pad;
212 			buflen -= pad;
213 			h.caplen = buflen;
214 			(*callback)(user, &h, p);
215 			if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
216 				pc->cc = cc;
217 				pc->bp = bp;
218 				return (n);
219 			}
220 		}
221 	}
222 	pc->cc = 0;
223 	return (n);
224 }
225 
226 static int
227 pcap_inject_pf(pcap_t *p, const void *buf, int size)
228 {
229 	int ret;
230 
231 	ret = write(p->fd, buf, size);
232 	if (ret == -1) {
233 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
234 		    errno, "send");
235 		return (-1);
236 	}
237 	return (ret);
238 }
239 
240 static int
241 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
242 {
243 	struct pcap_pf *pf = p->priv;
244 
245 	/*
246 	 * If packet filtering is being done in the kernel:
247 	 *
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....)
256 	 *
257 	 *	"ps_drop" counts packets that passed the kernel filter
258 	 *	(if any) but were dropped because the input queue was
259 	 *	full.
260 	 *
261 	 *	"ps_ifdrop" counts packets dropped by the network
262 	 *	interface (regardless of whether they would have passed
263 	 *	the input filter, of course).
264 	 *
265 	 * If packet filtering is not being done in the kernel:
266 	 *
267 	 *	"ps_recv" counts only packets that passed the filter.
268 	 *
269 	 *	"ps_drop" counts packets that were dropped because the
270 	 *	input queue was full, regardless of whether they passed
271 	 *	the userland filter.
272 	 *
273 	 *	"ps_ifdrop" counts packets dropped by the network
274 	 *	interface (regardless of whether they would have passed
275 	 *	the input filter, of course).
276 	 *
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.
280 	 */
281 	ps->ps_recv = pf->TotAccepted;
282 	ps->ps_drop = pf->TotDrops;
283 	ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
284 	return (0);
285 }
286 
287 /*
288  * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
289  * don't get DLT_DOCSIS defined.
290  */
291 #ifndef DLT_DOCSIS
292 #define DLT_DOCSIS	143
293 #endif
294 
295 static int
296 pcap_activate_pf(pcap_t *p)
297 {
298 	struct pcap_pf *pf = p->priv;
299 	short enmode;
300 	int backlog = -1;	/* request the most */
301 	struct enfilter Filter;
302 	struct endevp devparams;
303 	int err;
304 
305 	/*
306 	 * Initially try a read/write open (to allow the inject
307 	 * method to work).  If that fails due to permission
308 	 * issues, fall back to read-only.  This allows a
309 	 * non-root user to be granted specific access to pcap
310 	 * capabilities via file permissions.
311 	 *
312 	 * XXX - we should have an API that has a flag that
313 	 * controls whether to open read-only or read-write,
314 	 * so that denial of permission to send (or inability
315 	 * to send, if sending packets isn't supported on
316 	 * the device in question) can be indicated at open
317 	 * time.
318 	 *
319 	 * XXX - we assume here that "pfopen()" does not, in fact, modify
320 	 * its argument, even though it takes a "char *" rather than a
321 	 * "const char *" as its first argument.  That appears to be
322 	 * the case, at least on Digital UNIX 4.0.
323 	 *
324 	 * XXX - is there an error that means "no such device"?  Is
325 	 * there one that means "that device doesn't support pf"?
326 	 */
327 	p->fd = pfopen(p->opt.device, O_RDWR);
328 	if (p->fd == -1 && errno == EACCES)
329 		p->fd = pfopen(p->opt.device, O_RDONLY);
330 	if (p->fd < 0) {
331 		if (errno == EACCES) {
332 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
333 			    "pf open: %s: Permission denied\n"
334 "your system may not be properly configured; see the packetfilter(4) man page",
335 			    p->opt.device);
336 			err = PCAP_ERROR_PERM_DENIED;
337 		} else {
338 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
339 			    errno, "pf open: %s", p->opt.device);
340 			err = PCAP_ERROR;
341 		}
342 		goto bad;
343 	}
344 
345 	/*
346 	 * Turn a negative snapshot value (invalid), a snapshot value of
347 	 * 0 (unspecified), or a value bigger than the normal maximum
348 	 * value, into the maximum allowed value.
349 	 *
350 	 * If some application really *needs* a bigger snapshot
351 	 * length, we should just increase MAXIMUM_SNAPLEN.
352 	 */
353 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
354 		p->snapshot = MAXIMUM_SNAPLEN;
355 
356 	pf->OrigMissed = -1;
357 	enmode = ENTSTAMP|ENNONEXCL;
358 	if (!p->opt.immediate)
359 		enmode |= ENBATCH;
360 	if (p->opt.promisc)
361 		enmode |= ENPROMISC;
362 	if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
363 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
364 		    errno, "EIOCMBIS");
365 		err = PCAP_ERROR;
366 		goto bad;
367 	}
368 #ifdef	ENCOPYALL
369 	/* Try to set COPYALL mode so that we see packets to ourself */
370 	enmode = ENCOPYALL;
371 	(void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
372 #endif
373 	/* set the backlog */
374 	if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
375 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
376 		    errno, "EIOCSETW");
377 		err = PCAP_ERROR;
378 		goto bad;
379 	}
380 	/* discover interface type */
381 	if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
382 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
383 		    errno, "EIOCDEVP");
384 		err = PCAP_ERROR;
385 		goto bad;
386 	}
387 	/* HACK: to compile prior to Ultrix 4.2 */
388 #ifndef	ENDT_FDDI
389 #define	ENDT_FDDI	4
390 #endif
391 	switch (devparams.end_dev_type) {
392 
393 	case ENDT_10MB:
394 		p->linktype = DLT_EN10MB;
395 		p->offset = 2;
396 		/*
397 		 * This is (presumably) a real Ethernet capture; give it a
398 		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
399 		 * that an application can let you choose it, in case you're
400 		 * capturing DOCSIS traffic that a Cisco Cable Modem
401 		 * Termination System is putting out onto an Ethernet (it
402 		 * doesn't put an Ethernet header onto the wire, it puts raw
403 		 * DOCSIS frames out on the wire inside the low-level
404 		 * Ethernet framing).
405 		 */
406 		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
407 		/*
408 		 * If that fails, just leave the list empty.
409 		 */
410 		if (p->dlt_list != NULL) {
411 			p->dlt_list[0] = DLT_EN10MB;
412 			p->dlt_list[1] = DLT_DOCSIS;
413 			p->dlt_count = 2;
414 		}
415 		break;
416 
417 	case ENDT_FDDI:
418 		p->linktype = DLT_FDDI;
419 		break;
420 
421 #ifdef ENDT_SLIP
422 	case ENDT_SLIP:
423 		p->linktype = DLT_SLIP;
424 		break;
425 #endif
426 
427 #ifdef ENDT_PPP
428 	case ENDT_PPP:
429 		p->linktype = DLT_PPP;
430 		break;
431 #endif
432 
433 #ifdef ENDT_LOOPBACK
434 	case ENDT_LOOPBACK:
435 		/*
436 		 * It appears to use Ethernet framing, at least on
437 		 * Digital UNIX 4.0.
438 		 */
439 		p->linktype = DLT_EN10MB;
440 		p->offset = 2;
441 		break;
442 #endif
443 
444 #ifdef ENDT_TRN
445 	case ENDT_TRN:
446 		p->linktype = DLT_IEEE802;
447 		break;
448 #endif
449 
450 	default:
451 		/*
452 		 * XXX - what about ENDT_IEEE802?  The pfilt.h header
453 		 * file calls this "IEEE 802 networks (non-Ethernet)",
454 		 * but that doesn't specify a specific link layer type;
455 		 * it could be 802.4, or 802.5 (except that 802.5 is
456 		 * ENDT_TRN), or 802.6, or 802.11, or....  That's why
457 		 * DLT_IEEE802 was hijacked to mean Token Ring in various
458 		 * BSDs, and why we went along with that hijacking.
459 		 *
460 		 * XXX - what about ENDT_HDLC and ENDT_NULL?
461 		 * Presumably, as ENDT_OTHER is just "Miscellaneous
462 		 * framing", there's not much we can do, as that
463 		 * doesn't specify a particular type of header.
464 		 */
465 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
466 		    "unknown data-link type %u", devparams.end_dev_type);
467 		err = PCAP_ERROR;
468 		goto bad;
469 	}
470 	/* set truncation */
471 	if (p->linktype == DLT_FDDI) {
472 		p->fddipad = PCAP_FDDIPAD;
473 
474 		/* packetfilter includes the padding in the snapshot */
475 		p->snapshot += PCAP_FDDIPAD;
476 	} else
477 		p->fddipad = 0;
478 	if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
479 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
480 		    errno, "EIOCTRUNCATE");
481 		err = PCAP_ERROR;
482 		goto bad;
483 	}
484 	/* accept all packets */
485 	memset(&Filter, 0, sizeof(Filter));
486 	Filter.enf_Priority = 37;	/* anything > 2 */
487 	Filter.enf_FilterLen = 0;	/* means "always true" */
488 	if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
489 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
490 		    errno, "EIOCSETF");
491 		err = PCAP_ERROR;
492 		goto bad;
493 	}
494 
495 	if (p->opt.timeout != 0) {
496 		struct timeval timeout;
497 		timeout.tv_sec = p->opt.timeout / 1000;
498 		timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
499 		if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
500 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
501 			    errno, "EIOCSRTIMEOUT");
502 			err = PCAP_ERROR;
503 			goto bad;
504 		}
505 	}
506 
507 	p->bufsize = BUFSPACE;
508 	p->buffer = malloc(p->bufsize + p->offset);
509 	if (p->buffer == NULL) {
510 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
511 		    errno, "malloc");
512 		err = PCAP_ERROR;
513 		goto bad;
514 	}
515 
516 	/*
517 	 * "select()" and "poll()" work on packetfilter devices.
518 	 */
519 	p->selectable_fd = p->fd;
520 
521 	p->read_op = pcap_read_pf;
522 	p->inject_op = pcap_inject_pf;
523 	p->setfilter_op = pcap_setfilter_pf;
524 	p->setdirection_op = NULL;	/* Not implemented. */
525 	p->set_datalink_op = NULL;	/* can't change data link type */
526 	p->getnonblock_op = pcap_getnonblock_fd;
527 	p->setnonblock_op = pcap_setnonblock_fd;
528 	p->stats_op = pcap_stats_pf;
529 
530 	return (0);
531  bad:
532 	pcap_cleanup_live_common(p);
533 	return (err);
534 }
535 
536 pcap_t *
537 pcap_create_interface(const char *device _U_, char *ebuf)
538 {
539 	pcap_t *p;
540 
541 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_pf);
542 	if (p == NULL)
543 		return (NULL);
544 
545 	p->activate_op = pcap_activate_pf;
546 	return (p);
547 }
548 
549 /*
550  * XXX - is there an error from pfopen() that means "no such device"?
551  * Is there one that means "that device doesn't support pf"?
552  */
553 static int
554 can_be_bound(const char *name _U_)
555 {
556 	return (1);
557 }
558 
559 static int
560 get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
561 {
562 	/*
563 	 * Nothing we can do other than mark loopback devices as "the
564 	 * connected/disconnected status doesn't apply".
565 	 *
566 	 * XXX - is there a way to find out whether an adapter has
567 	 * something plugged into it?
568 	 */
569 	if (*flags & PCAP_IF_LOOPBACK) {
570 		/*
571 		 * Loopback devices aren't wireless, and "connected"/
572 		 * "disconnected" doesn't apply to them.
573 		 */
574 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
575 		return (0);
576 	}
577 	return (0);
578 }
579 
580 int
581 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
582 {
583 	return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
584 	    get_if_flags));
585 }
586 
587 static int
588 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
589 {
590 	struct pcap_pf *pf = p->priv;
591 	struct bpf_version bv;
592 
593 	/*
594 	 * See if BIOCVERSION works.  If not, we assume the kernel doesn't
595 	 * support BPF-style filters (it's not documented in the bpf(7)
596 	 * or packetfiler(7) man pages, but the code used to fail if
597 	 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
598 	 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
599 	 * there, at least).
600 	 */
601 	if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
602 		/*
603 		 * OK, we have the version of the BPF interpreter;
604 		 * is it the same major version as us, and the same
605 		 * or better minor version?
606 		 */
607 		if (bv.bv_major == BPF_MAJOR_VERSION &&
608 		    bv.bv_minor >= BPF_MINOR_VERSION) {
609 			/*
610 			 * Yes.  Try to install the filter.
611 			 */
612 			if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
613 				pcap_fmt_errmsg_for_errno(p->errbuf,
614 				    sizeof(p->errbuf), errno, "BIOCSETF");
615 				return (-1);
616 			}
617 
618 			/*
619 			 * OK, that succeeded.  We're doing filtering in
620 			 * the kernel.  (We assume we don't have a
621 			 * userland filter installed - that'd require
622 			 * a previous version check to have failed but
623 			 * this one to succeed.)
624 			 *
625 			 * XXX - this message should be supplied to the
626 			 * application as a warning of some sort,
627 			 * except that if it's a GUI application, it's
628 			 * not clear that it should be displayed in
629 			 * a window to annoy the user.
630 			 */
631 			fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
632 			pf->filtering_in_kernel = 1;
633 
634 			/*
635 			 * Discard any previously-received packets,
636 			 * as they might have passed whatever filter
637 			 * was formerly in effect, but might not pass
638 			 * this filter (BIOCSETF discards packets buffered
639 			 * in the kernel, so you can lose packets in any
640 			 * case).
641 			 */
642 			p->cc = 0;
643 			return (0);
644 		}
645 
646 		/*
647 		 * We can't use the kernel's BPF interpreter; don't give
648 		 * up, just log a message and be inefficient.
649 		 *
650 		 * XXX - this should really be supplied to the application
651 		 * as a warning of some sort.
652 		 */
653 		fprintf(stderr,
654 	    "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
655 		    BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
656 		    bv.bv_major, bv.bv_minor);
657 	}
658 
659 	/*
660 	 * We couldn't do filtering in the kernel; do it in userland.
661 	 */
662 	if (install_bpf_program(p, fp) < 0)
663 		return (-1);
664 
665 	/*
666 	 * XXX - this message should be supplied by the application as
667 	 * a warning of some sort.
668 	 */
669 	fprintf(stderr, "tcpdump: Filtering in user process\n");
670 	pf->filtering_in_kernel = 0;
671 	return (0);
672 }
673 
674 /*
675  * Libpcap version string.
676  */
677 const char *
678 pcap_lib_version(void)
679 {
680 	return (PCAP_VERSION_STRING);
681 }
682