JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 7: Device and Network Interfaces     Oracle Solaris 11.1 Information Library
search filter icon
search icon

Document Information

Preface

Introduction

Device and Network Interfaces

1394(7D)

aac(7D)

adpu320(7D)

afe(7D)

agpgart_io(7I)

AH(7P)

ahci(7D)

allkmem(7D)

amd8111s(7D)

arcmsr(7D)

arn(7D)

ARP(7P)

arp(7P)

ast(7D)

asy(7D)

ata(7D)

atge(7D)

ath(7D)

atu(7D)

audio1575(7D)

audio(7D)

audio(7I)

audio810(7D)

audiocmi(7D)

audiocs(7D)

audioemu10k(7D)

audioens(7D)

audiohd(7D)

audioixp(7D)

audiols(7D)

audiop16x(7D)

audiopci(7D)

audiosolo(7D)

audiots(7D)

audiovia823x(7D)

av1394(7D)

balloon(7D)

bbc_beep(7D)

bcm_sata(7D)

bfe(7D)

bge(7D)

blkdev(7D)

bmc(7D)

bnx(7D)

bnxe(7D)

bpf(7D)

bscbus(7D)

bscv(7D)

bufmod(7M)

cdio(7I)

chxge(7D)

cmdk(7D)

connld(7M)

console(7D)

cpqary3(7D)

cpr(7)

cpuid(7D)

ctfs(7FS)

cxge(7D)

dad(7D)

daplt(7D)

dca(7D)

dcam1394(7D)

dcfs(7FS)

dev(7FS)

devchassis(7FS)

devfs(7FS)

devinfo(7D)

dkio(7I)

dlcosmk(7ipp)

dlpi(7P)

dm2s(7D)

dmfe(7D)

dnet(7D)

dr(7d)

drmach(7d)

dscpmk(7ipp)

dsp(7I)

dtrace(7D)

e1000(7D)

e1000g(7D)

ecpp(7D)

efb(7D)

ehci(7D)

eibnx(7D)

elxl(7D)

emlxs(7D)

eoib(7D)

eri(7D)

ESP(7P)

evb(7P)

fas(7D)

fasttrap(7D)

fbio(7I)

fbt(7D)

fcip(7D)

fcoe(7D)

fcoei(7D)

fcoet(7D)

fcp(7D)

fctl(7D)

fipe(7D)

firewire(7D)

flowacct(7ipp)

fp(7d)

FSS(7)

gld(7D)

glm(7D)

hci1394(7D)

hdio(7I)

heci(7D)

hermon(7D)

hid(7D)

hme(7D)

hsfs(7FS)

hubd(7D)

hwa1480_fw(7D)

hwahc(7D)

hwarc(7D)

hxge(7D)

i2bsc(7D)

i915(7d)

ib(7D)

ibcm(7D)

ibdm(7D)

ibdma(7D)

ibmf(7)

ibp(7D)

ibtl(7D)

icmp6(7P)

ICMP(7P)

icmp(7P)

iec61883(7I)

ieee1394(7D)

if(7P)

ifp(7D)

if_tcp(7P)

igb(7D)

igbvf(7D)

ii(7D)

imraid_sas(7D)

inet6(7P)

inet(7P)

ip6(7P)

IP(7P)

ip(7P)

ipgpc(7ipp)

ipmi(7D)

ipnat(7I)

ipnet(7D)

ipqos(7ipp)

iprb(7D)

ipsec(7P)

ipsecah(7P)

ipsecesp(7P)

ipw(7D)

iscsi(7D)

isdnio(7I)

iser(7D)

isp(7D)

iwh(7D)

iwi(7D)

iwk(7D)

iwp(7D)

ixgb(7d)

ixgbe(7D)

ixgbevf(7D)

kb(7M)

kdmouse(7D)

kmdb(7d)

kmem(7D)

kstat(7D)

ksyms(7D)

ldterm(7M)

llc1(7D)

llc2(7D)

lo0(7D)

lockstat(7D)

lofi(7D)

lofs(7FS)

log(7D)

lsc(7D)

marvell88sx(7D)

mc-opl(7D)

mcxe(7D)

md(7D)

mediator(7D)

mega_sas(7D)

mem(7D)

mga(7D)

mhd(7i)

mixer(7I)

mpt(7D)

mpt_sas(7D)

mr_sas(7D)

msglog(7D)

mt(7D)

mtio(7I)

mwl(7D)

mxfe(7D)

myri10ge(7D)

n2cp(7d)

n2rng(7d)

nca(7d)

ncp(7D)

ngdr(7d)

ngdrmach(7d)

nge(7D)

npe(7D)

ntwdt(7D)

ntxn(7D)

null(7D)

nulldriver(7D)

nv_sata(7D)

nxge(7D)

objfs(7FS)

oce(7D)

ohci(7D)

openprom(7D)

oplkmdrv(7D)

oplmsu(7D)

oplpanel(7D)

packet(7P)

pcan(7D)

pcata(7D)

pcfs(7FS)

pcic(7D)

pcicmu(7D)

pcie_pci(7D)

pckt(7M)

pcmcia(7D)

pcn(7D)

pcser(7D)

pcwl(7D)

pf_key(7P)

pfmod(7M)

PF_PACKET(7P)

physmem(7D)

pipemod(7M)

pm(7D)

poll(7d)

prnio(7I)

profile(7D)

ptem(7M)

ptm(7D)

pts(7D)

pty(7D)

qfe(7d)

qlc(7D)

qlcnic(7D)

qlge(7D)

quotactl(7I)

radeon(7d)

ral(7D)

ramdisk(7D)

random(7D)

RARP(7P)

rarp(7P)

rge(7D)

route(7P)

routing(7P)

rtls(7D)

rtw(7D)

rum(7D)

rwd(7D)

rwn(7D)

sad(7D)

sata(7D)

scfd(7D)

scsa1394(7D)

scsa2usb(7D)

scsi_vhci(7D)

SCTP(7P)

sctp(7P)

scu(7D)

sd(7D)

sda(7D)

SDC(7)

sdcard(7D)

sdhost(7D)

sdp(7D)

sdt(7D)

se(7D)

se_hdlc(7D)

ses(7D)

sesio(7I)

sf(7D)

sfe(7D)

sgen(7D)

sharefs(7FS)

si3124(7D)

sip(7P)

slp(7P)

smbfs(7FS)

smbios(7D)

smbus(7D)

smp(7D)

snca(7d)

socal(7D)

sockio(7I)

sol_ofs(7D)

sol_ucma(7D)

sol_umad(7D)

sol_uverbs(7D)

sppptun(7M)

srpt(7D)

ssd(7D)

st(7D)

streamio(7I)

su(7D)

sv(7D)

sxge(7D)

sysmsg(7D)

systrace(7D)

TCP(7P)

tcp(7P)

termio(7I)

termiox(7I)

ticlts(7D)

ticots(7D)

ticotsord(7D)

timod(7M)

tirdwr(7M)

tmpfs(7FS)

todopl(7D)

tokenmt(7ipp)

tsalarm(7D)

tswtclmt(7ipp)

ttcompat(7M)

tty(7D)

ttymux(7D)

tzmon(7d)

uata(7D)

uath(7D)

udfs(7FS)

UDP(7P)

udp(7P)

ufs(7FS)

ugen(7D)

uhci(7D)

ural(7D)

urandom(7D)

urtw(7D)

usb(7D)

usba(7D)

usb_ac(7D)

usb_ah(7M)

usb_as(7D)

usbecm(7D)

usbftdi(7D)

usb_ia(7D)

usbkbm(7M)

usb_mid(7D)

usbms(7M)

usbprn(7D)

usbsacm(7D)

usbser_edge(7D)

usbsksp(7D)

usbsprl(7D)

usbvc(7D)

usbwcm(7M)

uscsi(7I)

usmp(7I)

uvfs(7FS)

uwb(7D)

uwba(7D)

virtualkm(7D)

visual_io(7I)

vni(7d)

vr(7D)

vt(7I)

vuid2ps2(7M)

vuid3ps2(7M)

vuidm3p(7M)

vuidm4p(7M)

vuidm5p(7M)

vuidmice(7M)

vxge(7D)

wpi(7D)

wscons(7D)

wusb_ca(7D)

wusb_df(7D)

xge(7D)

xhci(7D)

yge(7D)

zcons(7D)

zero(7D)

zfs(7FS)

zs(7D)

zsh(7D)

zyd(7D)

bpf

- Berkeley Packet Filter raw network interface

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/bpf. After opening the device, the file descriptor must be bound to a specific network interface with the BIOSETIF ioctl. A specific interface can be shared by multiple listeners, and the filter underlying each descriptor sees an identical packet stream.

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 , and under BSD, can be set with BIOCSBLEN. An individual packet larger than this size is necessarily truncated.

The packet filter supports 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(3SOCKET) 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 that only one packet can be processed per write. Currently, only writes to Ethernets and SLIP links are supported.

ioctls

The ioctl(2) command codes in this section are defined in <net/bfp.h>. All commands require these includes:

#include <sys/types.h>
#include <sys/time.h>
#include <sys/time.h>
#include <net/bpf.h>

Additionally, BIOCGETIF and BIOCSETIF require <net/if.h>.

The third argument to the ioctl(2)should be a pointer to the type indicated.

BIOCGBLEN (u_int)

Returns the required buffer length for reads on bpf files.

BIOCSBLEN (u_int)

Sets the buffer length for reads on bpf files. The buffer must be set before the file is attached to an interface with BIOCSETIF. If the requested buffer size cannot be accommodated, the closest allowable size is set and returned in the argument. A read call results in EINVAL if it is passed a buffer that is not this size.

BIOCGDLT (u_int)

Returns the type of the data link layer underlying the attached interface. EINVAL is returned if no interface has been specified. The device types, prefixed with DLT_, are defined in <net/bpf.h>.

BIOCGDLTLIST (struct bpf_dltlist)

Returns an array of available type of the data link layer underlying the attached interface:

struct bpf_dltlist {
  u_int bfl_len;
  u_int *bfl_list;
};

The available type is returned to the array pointed to the bfl_list field while its length in u_int is supplied to the bfl_len field. NOMEM is returned if there is not enough buffer. The bfl_len field is modified on return to indicate the actual length in u_int of the array returned. If bfl_list is NULL, the bfl_len field is returned to indicate the required length of an array in u_int.

BIOCSDLT (u_int)

Change the type of the data link layer underlying the attached interface. EINVAL is returned if no interface has been specified or the specified type is not available for the interface.

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 can 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.

BIOCGETLIF (struct lifreq)

Returns the name of the hardware interface that the file is listening on. The name is returned in the lifr_name field of lifreq. If the hardware interface is part of a non-global zone, lifr_zoneid is set to the zone ID of the hardware interface. All other fields are undefined.

BIOCSETLIF (struct lifreq)

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 lifr_name field of the lifreq. Additionally, performs the actions of BIOCFLUSH. If lifr_zoneid field in lifreq is non-zero, the hardware interface to be associated with the file is part of a non-global zone and not the running zone.

BIOCGETIF (struct ifreq)

Returns the name of the hardware interface that the file is listening on. The name is returned in the ifr_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 ifr_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 {
    uint64_t bs_recv;
    uint64_t bs_drop;
    uint64_t bs_capt;
    uint64_t bs_padding[13];
};

The fields are:

bs_recv

Number of packets received by the descriptor since opened or reset (including any buffered since the last read call.

bs_drop

Number of packets which were accepted by the filter but dropped by the kernel because of buffer overflows, that is, the application's reads aren't keeping up with the packet traffic.

bs_capt

Number of packets accepted by the filter.

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 blocks until either the kernel buffer becomes full or a timeout occurs. This is useful for programs like rarpd(1M), 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 {
    u_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. The actions of BIOCFLUSH are also performed.

See the FILTER MACHINE section of this manual page for an explanation of the filter language.

BIOCVERSION (struct bpf_version)

Returns the major and minor version numbers of the filter language currently recognized by the kernel. Before installing a filter, applications must check that the current version is compatible with the running kernel. Version numbers are compatible if the major numbers match and the application minor is less than or equal to the kernel minor. The kernel version number is returned in the following structure:

struct bpf_version {
   u_short bv_major;
   u_short bv_minor;
 };

The current version numbers are given by BPF_MAJOR_VERSION and BPF_MINOR_VERSION from <net/bpf.h>.

An incompatible filter can result in undefined behavior, most likely, an error returned by ioctl(2) or haphazard packet matching.

BIOCGHDRCMPLT BIOCSHDRCMPLT (u_int)

Enable/disable or get the header complete flag status. If enabled, packets written to the bpf file descriptor does not have network layer headers rewritten in the interface output routine. By default, the flag is disabled (value is 0).

BIOCGSEESENT BIOCSSEESENT (u_int)

Enable/disable or get the see sent flag status. If enabled, packets sent is passed to the filter. By default, the flag is enabled (value is 1).

Standard Ioctls

bpf supports several standard ioctl(2)'s that allow the user to do async or non-blocking I/O to an open file descriptor.

FIONREAD (int)

Returns the number of bytes that are immediately available for reading.

SIOCGIFADDR (struct ifreq)

Returns the address associated with the interface.

FIONBIO (int)

Set or clear non-blocking I/O. If arg is non-zero, then doing a read(2) when no data is available returns -1 and errno is set to EAGAIN. If arg is zero, non-blocking I/O is disabled. Setting this overrides the timeout set by BIOCSRTIMEOUT.

FIOASYNC (int)

Enable or disable async I/O. When enabled (arg is non-zero), the process or process group specified by FIOSETOWN starts receiving SIGIOs when packets arrive. You must do an FIOSETOWN for this to take effect, as the system does not default this for you. The signal can be changed using BIOCSRSIG.

FIOSETOWN FIOGETOWN (int)

Set or get the process or process group (if negative) that should receive SIGIO when packets are available. The signal can be changed using BIOCSRSIG.

bpf Header

The following structure is prepended to each packet returned by read(2):

struct bpf_hdr {
    struct timeval bh_tstamp;
     uint32_t bh_caplen;
     uint32_t bh_datalen;
     uint16_t 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 cannot be equal to sizeof (struct bpf_hdr).

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 alignment sensitive architectures and improves performance on many other architectures. The packet filter ensures that the bpf_hdr and the network layer header is word aligned. Suitable precautions must be taken when accessing the link layer protocol fields on alignment restricted machines. This is not 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 advances 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(3C) always returns an aligned buffer.

Filter Machine

A filter program is an array of instructions, with all branches forwardly directed, terminated by a return instruction. Each instruction performs some action on the pseudo-machine state, which consists of an accumulator, index register, scratch memory store, and implicit program counter.

The following structure defines the instruction format:

struct bpf_insn {
   uint16_t code;
   u_char  jt;
   u_char  jf;
   int32_t k;
};

The k field is used in different ways by different instructions, and the jt and jf fields are used as offsets by the branch instructions. The opcodes are encoded in a semi-hierarchical fashion. There are eight classes of instructions: BPF_LD, BPF_LDX, BPF_ST, BPF_STX, BPF_ALU, BPF_JMP, BPF_RET, and BPF_MISC. Various other mode and operator bits are or'd into the class to give the actual instructions. The classes and modes are defined in <net/bpf.h>.

Below are the semantics for each defined BPF instruction. We use the convention that A is the accumulator, X is the index register, P[] packet data, and M[] scratch memory store. P[i:n] gives the data at byte offset i in the packet, interpreted as a word (n=4), unsigned halfword (n=2), or unsigned byte (n=1). M[i] gives the i'th word in the scratch memory store, which is only addressed in word units. The memory store is indexed from 0 to BPF_MEMWORDS-1.k, jt, and jf are the corresponding fields in the instruction definition. len refers to the length of the packet.

BPF_LD

These instructions copy a value into the accumulator. The type of the source operand is specified by an addressing mode and can be a constant (BBPF_IMM), packet data at a fixed offset (BPF_ABS), packet data at a variable offset (BPF_IND), the packet length (BPF_LEN), or a word in the scratch memory store (BPF_MEM). For BPF_IND and BPF_ABS, the data size must be specified as a word (BPF_W), halfword (BPF_H), or byte (BPF_B). The semantics of all the recognized BPF_LD instructions follow.

BPF_LD+BPF_W+BPF_ABS A <- P[k:4]
BPF_LD+BPF_H+BPF_ABS A <- P[k:2]
BPF_LD+BPF_B+BPF_ABS A <- P[k:1]
BPF_LD+BPF_W+BPF_IND A <- P[X+k:4]
BPF_LD+BPF_H+BPF_IND A <- P[X+k:2]
BPF_LD+BPF_B+BPF_IND A <- P[X+k:1]
BPF_LD+BPF_W+BPF_LEN A <- len
BPF_LD+BPF_IMM A <- k
BPF_LD+BPF_MEM A <- M[k]
BPF_LDX

These instructions load a value into the index register. The addressing modes are more restricted than those of the accumulator loads, but they include BPF_MSH, a hack for efficiently loading the IP header length.

BPF_LDX+BPF_W+BPF_IMM X <- k
BPF_LDX+BPF_W+BPF_MEM X <- M[k]
BPF_LDX+BPF_W+BPF_LEN X <- len
BPF_LDX+BPF_B+BPF_MSH X <- 4*(P[k:1]&0xf)
BPF_ST

This instruction stores the accumulator into the scratch memory. We do not need an addressing mode since there is only one possibility for the destination.

BPF_ST M[k] <- A
BPF_ALU

The alu instructions perform operations between the accumulator and index register or constant, and store the result back in the accumulator. For binary operations, a source mode is required (BPF_K or BPF_X).

BPF_ALU+BPF_ADD+BPF_K A <- A + k
BPF_ALU+BPF_SUB+BPF_K A <- A - k
BPF_ALU+BPF_MUL+BPF_K A <- A * k
BPF_ALU+BPF_DIV+BPF_K A <- A / k
BPF_ALU+BPF_AND+BPF_K A <- A & k
BPF_ALU+BPF_OR+BPF_K A <- A | k
BPF_ALU+BPF_LSH+BPF_K A <- A << k
BPF_ALU+BPF_RSH+BPF_K A <- A >> k
BPF_ALU+BPF_ADD+BPF_X A <- A + X
BPF_ALU+BPF_SUB+BPF_X A <- A - X
BPF_ALU+BPF_MUL+BPF_X A <- A * X
BPF_ALU+BPF_DIV+BPF_X A <- A / X
BPF_ALU+BPF_AND+BPF_X A <- A & X
BPF_ALU+BPF_OR+BPF_X A <- A | X
BPF_ALU+BPF_LSH+BPF_X A <- A << X
BPF_ALU+BPF_RSH+BPF_X A <- A >> X
BPF_ALU+BPF_NEG A <- -A
BPF_JMP

The jump instructions alter flow of control. Conditional jumps compare the accumulator against a constant (BPF_K) or the index register (BPF_X). If the result is true (or non-zero), the true branch is taken, otherwise the false branch is taken. Jump offsets are encoded in 8 bits so the longest jump is 256 instructions. However, the jump always (BPF_JA) opcode uses the 32 bit k field as the offset, allowing arbitrarily distant destinations. All condition also use unsigned comparison conventions.

BPF_JMP+BPF_JA  pc += k
BPF_JMP+BPF_JGT+BPF_K  pc += (A > k) ? jt : jf
BPF_JMP+BPF_JGE+BPF_K  pc += (A >= k) ? jt : jf
BPF_JMP+BPF_JEQ+BPF_K  pc += (A == k) ? jt : jf
BPF_JMP+BPF_JSET+BPF_K  pc += (A & k) ? jt : jf
BPF_JMP+BPF_JGT+BPF_X  pc += (A > X) ? jt : jf
BPF_JMP+BPF_JGE+BPF_X  pc += (A >= X) ? jt : jf
BPF_JMP+BPF_JEQ+BPF_X  pc += (A == X) ? jt : jf
BPF_JMP+BPF_JSET+BPF_X  pc += (A & X) ? jt : jf
BPF_RET

The return instructions terminate the filter program and specify the amount of packet to accept, that is, they return the truncation amount. A return value of zero indicates that the packet should be ignored. The return value is either a constant (BPF_K) or the accumulator (BPF_A).

BPF_RET+BPF_A accept A bytes
BPF_RET+BPF_K accept k bytes
BPF_MISC

The miscellaneous category was created for anything that does not fit into the other classes in this section, and for any new instructions that might need to be added. Currently, these are the register transfer instructions that copy the index register to the accumulator or vice versa.

BPF_MISC+BPF_TAX X <- A
BPF_MISC+BPF_TXA A <- X

The BPF interface provides the following macros to facilitate array initializers:

BPF_STMT (opcode, operand)
BPF_JUMP (opcode, operand, true_offset, false_offset)

Sysctls

The following sysctls are available when bpf is enabled:

net.bpf.maxbufsize

Sets the maximum buffer size available for bpf peers.

net.bpf.stats

Shows bpf statistics. They can be retrieved with the netstat(1M) utility.

net.bpf.peers

Shows the current bpf peers. This is only available to the super user and can also be retrieved with the netstat(1M) utility.

Files

/dev/bpf

Examples

Example 1 Using bfp to Accept Only Reverse ARP Requests

The following example shows a filter taken from the Reverse ARP Daemon. It accepts only Reverse ARP requests.

struct bpf_insn insns[] = {
           BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3),
           BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, REVARP_REQUEST, 0, 1),
           BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) +
               sizeof(struct ether_header)),
           BPF_STMT(BPF_RET+BPF_K, 0),
};

Example 2 Using bfp to Accept IP Packets

The following example shows filter that accepts only IP packets between host 128.3.112.15 and 128.3.112.35.

struct bpf_insn insns[] = {
           BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 8),
           BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
           BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
           BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
           BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
           BPF_STMT(BPF_RET+BPF_K, 0),
};

Example 3 Using bfp to Return Only TCP Finger Packets

The following example shows a filter that returns only TCP finger packets. The IP header must be parsed to reach the TCP header. The BPF_JSET instruction checks that the IP fragment offset is 0 so we are sure that we have a TCP header.

struct bpf_insn insns[] = {
           BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 10),
           BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_TCP, 0, 8),
           BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
           BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
           BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
           BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
           BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
           BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
           BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
           BPF_STMT(BPF_RET+BPF_K, 0),
};

Attributes

See attributes(5) for a description of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Architecture
Sparc, x86
Interface Stability
Committed

See Also

netstat(1M), rarpd(1M), lseek(2), ioctl(2), open(2), read(2), malloc(3C), select(3C), byteorder(3SOCKET), signal(3C), attributes(5)

S. McCanne and V. Jacobson, The BSD Packet Filter: A New Architecture for User-level Packet Capture, Proceedings of the 1993 Winter USENIX.

Bugs

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

A file that does not request promiscuous mode can receive promiscuous 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 use a filter to reject foreign packets.

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

Under SunOS, if a BPF application reads more than 2^31 bytes of data, read fails in EINVALsignal(3C). You can either fix the bug in SunOS, or lseek(2) to 0 when read fails for this reason.

Immediate mode and the read timeout are misguided features. This functionality can be emulated with non-blocking mode and select(3C).