When bpf is accessed via libpcap with the select system call with a timeout set if a less than full buffer of packets received on the interface (and passed to bpf.c) they will never be returned to libpcap even on a timeout. OpenBSD has a partial fix for this (it gets the first packet of 9 up and leaves the other 8) which I have corrected, reported to OpenBSD and ported to FreeBSD. As a side note one of the OpenBSD people is working on a better bpf implementation and would be interested in help by someone knowledgable in the FreeBSD VM system to assist porting his code when finished to FreeBSD. How-To-Repeat: To reproduce: 1) Install argus-1.8.1.tar.gz and argus-1.8.1.patches from ftp.andrew.cmu.edu/pub/argus 2) Install the test.dif patch below to report Total packets received from libpcap to show the problem. 3) start argus_bpf as below and supply it a less than buffer full of packets (I used tcpreplay from www.anzen.com/research/nidsbench on another machine playing back a tcpdump file: tcpreplay -ixl0 -r1 ip.small.tcpdump). 4) Examine output as below (unpatched then patched). Unpatched bpf.c (all 9 packets still in bpf.c buffer, none get to libpcap): demob# bin/argus_bpf -i xl0 -P0 -w argus.log ^C 9 packets recv'd by filter 0 packets dropped by kernel demob# bin/ra -r argus.log -n Tue 10/17 12:34:19 man 1.168.192.0 255.255.255.0 INT Tue 10/17 12:34:33 man pkts 9 drops 0 flows 0 CLO bpf.c and bpfdesc.h patched as below and kernel recompiled and installed: demob# bin/argus_bpf -ixl0 -P0 -w argus.log Total packets: 1 Total packets: 2 Total packets: 3 Total packets: 4 Total packets: 5 Total packets: 6 Total packets: 7 Total packets: 8 Total packets: 9 ^C 9 packets recv'd by filter 0 packets dropped by kernel demob# bin/ra -r argus.log -n Tue 10/17 09:50:50 man 1.168.192.0 255.255.255.0 INT Tue 10/17 09:51:16 M tcp 130.71.240.184.2197 |> 142.58.12.12.80 RST Tue 10/17 09:51:23 man pkts 9 drops 0 flows 1 CLO demob# Apply in argus-1.8.1/ directory. *** server/cons_ether.c.orig Tue Oct 17 01:45:10 2000 --- server/cons_ether.c Tue Oct 17 01:45:59 2000 *************** *** 95,100 **** --- 95,101 ---- if (p && caplen) { totalPktsRcv++; + printf("Total packets: %d\n", totalPktsRcv); globaltvp.tv_sec = h->ts.tv_sec; globaltvp.tv_usec = h->ts.tv_usec;
Responsible Changed From-To: freebsd-bugs->brian Over to bpf maintainer.
Responsible Changed From-To: brian->jdp John has asked for this :)
I have verified that this patch installs correctly into FreeBSD 4.3's kernel. This patch aids timely reporting of results from iplog version 2.2.3. Guy Helmer, Ph.D.
On this subject, a month or two ago I was in communication with Guy Harris / John Polstra on the subject of this patch (I know Guy Harris knows more about Unix kernals than I do and I expect Mr Polstra does too). He reported a couple of issues with the patch (an off by one test and the belief that the timeout won't work under some single packet conditions) that I haven't yet had any time to poke at (assuming I know enough to understand what I'm poking at of course :-) ). Mr Polstra indicated he was thinking about how the patch should work and hopefully will supply a better one. Peter Van Epp / Operations and Technical Support Simon Fraser University, Burnaby, B.C. Canada > > I have verified that this patch installs correctly into FreeBSD 4.3's > kernel. This patch aids timely reporting of results from iplog version > 2.2.3. > > Guy Helmer, Ph.D. > >
This patch also solves problems with using poll/select, but simply marks the fact that a select or poll has been performed on the BPF device. If a packet then arrives for the BPF device, the poll/select succeeds. It changes the BPF semantics to always allow a poll to succeed when any packets are received. This patch also solves the problem with using BPF devices in the case of userland threading, because the thread library's poll doesn't timeout when a read timeout has been set on the BPF device. --- bpf.c.ORIG Mon Aug 27 13:25:57 2001 +++ bpf.c Fri Nov 9 10:47:22 2001 @@ -444,7 +444,8 @@ (d)->bd_hlen = (d)->bd_slen; \ (d)->bd_sbuf = (d)->bd_fbuf; \ (d)->bd_slen = 0; \ - (d)->bd_fbuf = 0; + (d)->bd_fbuf = 0; \ + (d)->bd_poll = 0; /* * bpfread - read next chunk of packets from buffers */ @@ -472,7 +473,7 @@ * have arrived to fill the store buffer. */ while (d->bd_hbuf == 0) { - if (d->bd_immediate && d->bd_slen != 0) { + if ((d->bd_immediate || d->bd_poll) && d->bd_slen != 0) { /* * A packet(s) either arrived since the previous * read or arrived while we were asleep. @@ -559,6 +560,7 @@ pgsigio(d->bd_sigio, d->bd_sig, 0); #if BSD >= 199103 + /* revents |= events & (POLLIN | POLLRDNORM); ??? */ selwakeup(&d->bd_sel); /* XXX */ d->bd_sel.si_pid = 0; @@ -1057,10 +1059,12 @@ s = splimp(); if (events & (POLLIN | POLLRDNORM)) { - if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) + if (d->bd_hlen != 0 || d->bd_poll || (d->bd_immediate && d->bd_slen != 0)) revents |= events & (POLLIN | POLLRDNORM); - else + else { + d->bd_poll = 1; selrecord(p, &d->bd_sel); + } } splx(s); return (revents); @@ -1199,7 +1203,7 @@ bpf_wakeup(d); curlen = 0; } - else if (d->bd_immediate) + else if (d->bd_immediate || d->bd_poll) /* * Immediate mode is set. A packet arrived so any * reads should be woken up. --- bpfdesc.h.ORIG Mon Aug 27 13:26:06 2001 +++ bpfdesc.h Fri Nov 9 10:47:25 2001 @@ -76,6 +76,7 @@ u_char bd_promisc; /* true if listening promiscuously */ u_char bd_state; /* idle, waiting, or timed out */ u_char bd_immediate; /* true to return on packet arrival */ + u_char bd_poll; /* poll waiting for data */ int bd_hdrcmplt; /* false to fill in src lladdr automatically */ int bd_seesent; /* true if bpf should see sent packets */ int bd_async; /* non-zero if packet reception should generate signal */
This patch also solves a problem with using the BPF in a threaded environment. The problem is described in bin/31649. Can this patch please be commited? The current behaviour of the system is very strange now... Edwin -- Edwin Groothuis | Personal website: http://www.MavEtJu.org edwin@mavetju.org | Interested in MUDs? Visit Fatal Dimensions: ------------------+ http://www.FatalDimensions.org/
State Changed From-To: open->closed Fixed in sys/net/bpf.c revision 1.59.2.8 and sys/net/bpfdesc.h revision 1.14.2.2.