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