BPF(4)

NAME
bpf - Berkeley Packet Filter

SYNOPSIS
pseudo-device bpfilter 16

DESCRIPTION
The Berkeley Packet Filter provides a raw interface to data link layers in a protocol independent fashion. All packets on the network, even those destined for other hosts, are accessible through this mechanism.

The packet filter appears as a character special device, /dev/bpf0, /dev/bpf1, etc. After opening one of these files, the file descriptor must be bound to a specific interface with the BIOSETIF ioctl. The interfaces can be bound to more than on file descriptor, and the filter underlying each descriptor will see an identical packet stream. The total number of open files is limited to the value given in the kernel configuration; the example given in the SYNOPSIS above sets the limit to 16.

On BSD systems, device cloning is not supported. Hence, a separate device file is required for each minor device. If a file is in use, the open will fail and errno will be set to EBUSY.

Associated with each open instance of a bpf file is a user-settable packet filter. Whenever a packet is received by an interface, all file descriptors listening on that interface apply their filter. Each descriptor that accepts the packet receives its own copy.

Reads from these files return the next group of packets that have matched the filter. To improve performance, the buffer passed to read must be the same size as the buffers used internally by bpf. This size is returned by the BIOCGBLEN ioctl (see below). Note that an individual packet larger than this size is necessarily truncated.

The packet filter will support any link level protocol that has fixed length headers. Currently, only Ethernet, SLIP and PPP drivers have been modified to interact with bpf.

Since packet data is in network byte order, applications should use the byteorder(3n) macros to extract multi-byte values.

A packet can be sent out on the network by writing to a bpf file descriptor. The writes are unbuffered, meaning only one packet can be processed per write. Currently, only writes to Ethernets and SLIP links are supported.

IOCTL CALLS
The ioctl command codes below are defined in <net/bpf.h>. All commands require these includes:
	#include <sys/types.h>
	#include <sys/time.h>
	#include <sys/ioctl.h>
	#include <net/bpf.h>
Additionally, BIOCGETIF and BIOCSETIF require <net/if.h>. In addition to FIONREAD and SIOCGIFADDR, the following commands may be applied to any open bpf file. The (third) argument to the ioctl should be a pointer to the type indicated.

BIOCGFLEN (u_int)
    Returns the maximum possible packet filter program length in units of struct bpf_insn (see BIOCSETF below). Attempting to set a filter longer than this length is an error.

BIOCGBLEN (u_int)
    Returns the required buffer length for reads on bpf files. A read call will result in EIO if it is passed a buffer that is not this size.

BIOCDEVP (struct bpf_devp)
    Returns the following structure containing device parameters for the network interface associated with the open file:
    struct bpf_devp {
    	u_short bdev_type;
    	u_short bdev_hdrlen;
    };

    The fields are:

    bdev_type
        The device type; the types are defined in <net/bpf.h>. Currently, only DLT_EN10MB and DLT_SLIP are supported.

    bdev_hdrlen
        The total header length in bytes (14 for Ethernet, 16 for SLIP).

BIOCPROMISC
    Forces the interface into promiscuous mode. All packets, not just those destined for the local host, are processed. Since more than one file can be listening on a given interface, a listener that opened its interface non-promiscuously may receive packets promiscuously. This problem can be remedied with an appropriate filter.

    The interface remains in promiscuous mode until all files listening promiscuously are closed.

BIOCFLUSH
    Flushes the buffer of incoming packets, and resets the statistics that are returned by BIOCGSTATS.

BIOCGETIF (struct ifreq)
    Returns the name of the hardware interface that file is listening on. The name is returned in the if_name field of ifr. All other fields are undefined.

BIOCSETIF (struct ifreq)
    Sets the hardware interface associate with the file. This command must be performed before any packets can be read. The device is indicated by name using the if_name field of the ifreq. Additionally, performs the actions of BIOCFLUSH.

BIOCSRTIMEOUT, BIOCGRTIMEOUT (struct timeval)
    Set or get the read timeout parameter. The timeval specifies the length of time to wait before timing out on a read request. This parameter is initialized to zero by open(2), indicating no timeout.

BIOCGSTATS (struct bpf_stat)
    Returns the following structure of packet statistics:
    struct bpf_stat {
    	u_int bs_recv;
    	u_int bs_drop;
    };

    The fields are:

    bs_recv
        the number of packets received by the descriptor since opened or reset (including any buffered since the last read call); and

    bs_drop
        the number of packets which were accepted by the filter but dropped by the kernel because of buffer overflows (i.e., the application's reads aren't keeping up with the packet traffic).

BIOCIMMEDIATE (u_int)
    Enable or disable ``immediate mode'', based on the truth value of the argument. When immediate mode is enabled, reads return immediately upon packet reception. Otherwise, a read will block until either the kernel buffer becomes full or a timeout occurs. This is useful for programs like rarpd(8c), which must respond to messages in real time. The default for a new file is off.

BIOCSETF (struct bpf_program)
    Sets the filter program used by the kernel to discard uninteresting packets. An array of instructions and its length is passed in using the following structure:
    struct bpf_program {
    	int bf_len;
    	struct bpf_insn *bf_insns;
    };

    The filter program is pointed to by the bf_insns field while its length in units of `struct bpf_insn' is given by the bf_len field. Also, the actions of BIOCFLUSH are performed.

    For an explanation of the filtering machine, see the technical report ``The Berkeley Packet Filter'', distributed with bpf and tcpdump(1).

BPF HEADER
The following structure is prepended to each packet returned by read(2):
struct bpf_hdr {
	struct timeval bh_tstamp;
	u_long bh_caplen;
	u_long bh_datalen;
	u_short bh_hdrlen;
};

The fields, whose values are stored in host order, and are:

bh_tstamp
    The time at which the packet was processed by the packet filter.

bh_caplen
    The length of the captured portion of the packet. This is the minimum of the truncation amount specified by the filter and the length of the packet.

bh_datalen
    The length of the packet off the wire. This value is independent of the truncation amount specified by the filter.

bh_hdrlen
    The length of the BPF header, which may not be equal to sizeof(struct bpf_hdr). This is explained below.

The bh_hdrlen field exists to account for padding between the header and the link level protocol. The purpose here is to guarantee proper alignment of the packet data structures, which is required on certain RISC architectures and improves performance on many other architectures. The packet filter insures that the bpf_hdr and the network layer header will be word aligned. Suitable precautions must be taken when accessing the link layer protocol fields on alignment restricted machines. (This isn't a problem on an Ethernet, since the type field is a short falling on an even offset, and the addresses are probably accessed in a bytewise fashion).

Additionally, individual packets are padded so that each starts on a word boundary. This requires that an application has some knowledge of how to get from packet to packet. The macro BPF_WORDALIGN is defined in <net/bpf.h> to facilitate this process. It rounds up its argument to the nearest word aligned value (where a word is BPF_ALIGNMENT bytes wide).

For example, if `p' points to the start of a packet, this expression will advance it to the next packet:

    p = (char *)p + BPF_WORDALIGN(p->bh_hdrlen + p->bh_caplen)

For the alignment mechanisms to work properly, the buffer passed to read(2) must itself be word aligned. malloc(3) will always return an aligned buffer.

SEE ALSO
tcpdump(1), intro(4)

FILES
/dev/bpf0, /dev/bpf1, ...

BUGS
The read buffer must be of a fixed size (returned by the BIOCGBLEN ioctl).

A file that does not request promiscuous mode may receive promiscuously received packets as a side effect of another file requesting this mode on the same hardware interface. This could be fixed in the kernel with additional processing overhead. However, we favor the model where all files must assume that the interface is promiscuous, and if so desired, must utilize a filter to reject foreign packets.

Data link protocols with variable length headers are not currently supported.

Due to a SunOS bug, if a BPF application reads more than 2^31 bytes of data, read will fail in EINVAL. You can either fix your kernel, or lseek to 0 when read fails for this reason.

HISTORY

The Enet packet filter was created in 1980 by Mike Accetta and Rick Rashid at Carnegie-Mellon University. Jeffrey Mogul, at Stanford, ported the code to BSD and continued its development from 1983 on. Since then, it has evolved into the Ultrix Packet Filter at DEC, a STREAMS NIT module under SunOS 4.1, and BPF. Steven McCanne, of Lawrence Berkeley Laboratory, implemented BPF in Summer 1990. Much of the design is due to Van Jacobson.