xref: /netbsd-src/share/man/man4/bpf.4 (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1.\" -*- nroff -*-
2.\"
3.\"	$NetBSD: bpf.4,v 1.61 2018/06/26 06:47:57 msaitoh Exp $
4.\"
5.\" Copyright (c) 1990, 1991, 1992, 1993, 1994
6.\"	The Regents of the University of California.  All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that: (1) source code distributions
10.\" retain the above copyright notice and this paragraph in its entirety, (2)
11.\" distributions including binary code include the above copyright notice and
12.\" this paragraph in its entirety in the documentation or other materials
13.\" provided with the distribution, and (3) all advertising materials mentioning
14.\" features or use of this software display the following acknowledgement:
15.\" ``This product includes software developed by the University of California,
16.\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17.\" the University nor the names of its contributors may be used to endorse
18.\" or promote products derived from this software without specific prior
19.\" written permission.
20.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23.\"
24.\" This document is derived in part from the enet man page (enet.4)
25.\" distributed with 4.3BSD Unix.
26.\"
27.Dd June 22, 2018
28.Dt BPF 4
29.Os
30.Sh NAME
31.Nm bpf
32.Nd Berkeley Packet Filter raw network interface
33.Sh SYNOPSIS
34.Cd "pseudo-device bpfilter"
35.Sh DESCRIPTION
36The Berkeley Packet Filter
37provides a raw interface to data link layers in a protocol
38independent fashion.
39All packets on the network, even those destined for other hosts,
40are accessible through this mechanism.
41.Pp
42The packet filter appears as a character special device,
43.Pa /dev/bpf .
44After opening the device, the file descriptor must be bound to a
45specific network interface with the
46.Dv BIOCSETIF
47ioctl.
48A given interface can be shared by multiple listeners, and the filter
49underlying each descriptor will see an identical packet stream.
50.Pp
51Associated with each open instance of a
52.Nm
53file is a user-settable packet filter.
54Whenever a packet is received by an interface,
55all file descriptors listening on that interface apply their filter.
56Each descriptor that accepts the packet receives its own copy.
57.Pp
58Reads from these files return the next group of packets
59that have matched the filter.
60To improve performance, the buffer passed to read must be
61the same size as the buffers used internally by
62.Nm .
63This size is returned by the
64.Dv BIOCGBLEN
65ioctl (see below), and can be set with
66.Dv BIOCSBLEN .
67Note that an individual packet larger than this size is necessarily
68truncated.
69.Pp
70Since packet data is in network byte order, applications should use the
71.Xr byteorder 3
72macros to extract multi-byte values.
73.Pp
74A packet can be sent out on the network by writing to a
75.Nm
76file descriptor.
77The writes are unbuffered, meaning only one packet can be processed per write.
78Currently, only writes to Ethernets and SLIP links are supported.
79.Sh IOCTLS
80The
81.Xr ioctl 2
82command codes below are defined in
83.In net/bpf.h .
84All commands require these includes:
85.Bd -literal -offset indent
86#include <sys/types.h>
87#include <sys/time.h>
88#include <sys/ioctl.h>
89#include <net/bpf.h>
90.Ed
91.Pp
92Additionally,
93.Dv BIOCGETIF
94and
95.Dv BIOCSETIF
96require
97.Pa <net/if.h> .
98.Pp
99The (third) argument to the
100.Xr ioctl 2
101should be a pointer to the type indicated.
102.Bl -tag -width indent -offset indent
103.It Dv BIOCGBLEN ( u_int )
104Returns the required buffer length for reads on
105.Nm
106files.
107.It Dv BIOCSBLEN ( u_int )
108Sets the buffer length for reads on
109.Nm
110files.
111The buffer must be set before the file is attached to an interface with
112.Dv BIOCSETIF .
113If the requested buffer size cannot be accommodated, the closest
114allowable size will be set and returned in the argument.
115A read call will result in
116.Er EINVAL
117if it is passed a buffer that is not this size.
118.It Dv BIOCGDLT ( u_int )
119Returns the type of the data link layer underlying the attached interface.
120.Er EINVAL
121is returned if no interface has been specified.
122The device types, prefixed with
123.Dq DLT_ ,
124are defined in
125.In net/bpf.h .
126.It Dv BIOCGDLTLIST ( struct bpf_dltlist )
127Returns an array of the available types of the data link layer
128underlying the attached interface:
129.Bd -literal -offset indent
130struct bpf_dltlist {
131	u_int bfl_len;
132	u_int *bfl_list;
133};
134.Ed
135.Pp
136The available types are returned in the array pointed to by the
137.Va bfl_list
138field while their length in u_int is supplied to the
139.Va bfl_len
140field.
141.Er ENOMEM
142is returned if there is not enough buffer space and
143.Er EFAULT
144is returned if a bad address is encountered.
145The
146.Va bfl_len
147field is modified on return to indicate the actual length in u_int
148of the array returned.
149If
150.Va bfl_list
151is
152.Dv NULL ,
153the
154.Va bfl_len
155field is set to indicate the required length of an array in u_int.
156.It Dv BIOCSDLT ( u_int )
157Changes the type of the data link layer underlying the attached interface.
158.Er EINVAL
159is returned if no interface has been specified or the specified
160type is not available for the interface.
161.It Dv BIOCPROMISC
162Forces the interface into promiscuous mode.
163All packets, not just those destined for the local host, are processed.
164Since more than one file can be listening on a given interface,
165a listener that opened its interface non-promiscuously may receive
166packets promiscuously.
167This problem can be remedied with an appropriate filter.
168.Pp
169The interface remains in promiscuous mode until all files listening
170promiscuously are closed.
171.It Dv BIOCFLUSH
172Flushes the buffer of incoming packets,
173and resets the statistics that are returned by
174.Dv BIOCGSTATS .
175.It Dv BIOCGETIF ( struct ifreq )
176Returns the name of the hardware interface that the file is listening on.
177The name is returned in the ifr_name field of
178.Fa ifr .
179All other fields are undefined.
180.It Dv BIOCSETIF ( struct ifreq )
181Sets the hardware interface associated with the file.
182This command must be performed before any packets can be read.
183The device is indicated by name using the
184.Dv ifr_name
185field of the
186.Fa ifreq .
187Additionally, performs the actions of
188.Dv BIOCFLUSH .
189.It Dv BIOCSRTIMEOUT , BIOCGRTIMEOUT ( struct timeval )
190Sets or gets the read timeout parameter.
191The
192.Fa timeval
193specifies the length of time to wait before timing
194out on a read request.
195This parameter is initialized to zero by
196.Xr open 2 ,
197indicating no timeout.
198.It Dv BIOCGSTATS ( struct bpf_stat )
199Returns the following structure of packet statistics:
200.Bd -literal -offset indent
201struct bpf_stat {
202	uint64_t bs_recv;
203	uint64_t bs_drop;
204	uint64_t bs_capt;
205	uint64_t bs_padding[13];
206};
207.Ed
208.Pp
209The fields are:
210.Bl -tag -width bs_recv -offset indent
211.It Va bs_recv
212the number of packets received by the descriptor since opened or reset
213(including any buffered since the last read call);
214.It Va bs_drop
215the number of packets which were accepted by the filter but dropped by the
216kernel because of buffer overflows
217(i.e., the application's reads aren't keeping up with the packet
218traffic); and
219.It Va bs_capt
220the number of packets accepted by the filter.
221.El
222.It Dv BIOCIMMEDIATE ( u_int )
223Enables or disables
224.Dq immediate mode ,
225based on the truth value of the argument.
226When immediate mode is enabled, reads return immediately upon packet
227reception.
228Otherwise, a read will block until either the kernel buffer
229becomes full or a timeout occurs.
230This is useful for programs like
231.Xr rarpd 8 ,
232which must respond to messages in real time.
233The default for a new file is off.
234.It Dv BIOCSETF ( struct bpf_program )
235Sets the filter program used by the kernel to discard uninteresting
236packets.
237An array of instructions and its length are passed in using the following structure:
238.Bd -literal -offset indent
239struct bpf_program {
240	u_int bf_len;
241	struct bpf_insn *bf_insns;
242};
243.Ed
244.Pp
245The filter program is pointed to by the
246.Va bf_insns
247field while its length in units of
248.Sq struct bpf_insn
249is given by the
250.Va bf_len
251field.
252Also, the actions of
253.Dv BIOCFLUSH
254are performed.
255.Pp
256See section
257.Sy FILTER MACHINE
258for an explanation of the filter language.
259.It Dv BIOCVERSION ( struct bpf_version )
260Returns the major and minor version numbers of the filter language currently
261recognized by the kernel.
262Before installing a filter, applications must check
263that the current version is compatible with the running kernel.
264Version numbers are compatible if the major numbers match and the
265application minor is less than or equal to the kernel minor.
266The kernel version number is returned in the following structure:
267.Bd -literal -offset indent
268struct bpf_version {
269	u_short bv_major;
270	u_short bv_minor;
271};
272.Ed
273.Pp
274The current version numbers are given by
275.Dv BPF_MAJOR_VERSION
276and
277.Dv BPF_MINOR_VERSION
278from
279.In net/bpf.h .
280An incompatible filter
281may result in undefined behavior (most likely, an error returned by
282.Xr ioctl 2
283or haphazard packet matching).
284.It Dv BIOCSRSIG , BIOCGRSIG ( u_int )
285Sets or gets the receive signal.
286This signal will be sent to the process or process group specified by
287.Dv FIOSETOWN .
288It defaults to
289.Dv SIGIO .
290.It Dv BIOCGHDRCMPLT , BIOCSHDRCMPLT ( u_int )
291Sets or gets the status of the
292.Dq header complete
293flag.
294Set to zero if the link level source address should be filled in
295automatically by the interface output routine.
296Set to one if the link level source address will be written,
297as provided, to the wire.
298This flag is initialized to zero by default.
299.It Dv BIOCGSEESENT , BIOCSSEESENT ( u_int )
300These commands are obsolete but left for compatibility.
301Use
302.Dv BIOCSDIRECTION
303and
304.Dv BIOCGDIRECTION
305instead.
306Set or get the flag determining whether locally generated packets on the
307interface should be returned by BPF.
308Set to zero to see only incoming packets on the interface.
309Set to one to see packets originating locally and remotely on the interface.
310This flag is initialized to one by default.
311.It Dv BIOCSDIRECTION
312.It Dv BIOCGDIRECTION
313.Pq Li u_int
314Set or get the setting determining whether incoming, outgoing, or all packets
315on the interface should be returned by BPF.
316Set to
317.Dv BPF_D_IN
318to see only incoming packets on the interface.
319Set to
320.Dv BPF_D_INOUT
321to see packets originating locally and remotely on the interface.
322Set to
323.Dv BPF_D_OUT
324to see only outgoing packets on the interface.
325This setting is initialized to
326.Dv BPF_D_INOUT
327by default.
328.It Dv BIOCFEEDBACK , BIOCSFEEDBACK , BIOCGFEEDBACK ( u_int )
329Set (or get)
330.Dq packet feedback mode .
331This allows injected packets to be fed back as input to the interface when
332output via the interface is successful.
333The first name is meant for
334.Fx
335compatibility, the two others follow the Get/Set convention.
336.\"When
337.\".Dv BPF_D_INOUT
338.\"direction is set, injected
339Injected
340outgoing packets are not returned by BPF to avoid
341duplication.
342This flag is initialized to zero by default.
343.El
344.Sh STANDARD IOCTLS
345.Nm
346now supports several standard
347.Xr ioctl 2 Ns 's
348which allow the user to do async and/or non-blocking I/O to an open
349.Nm bpf
350file descriptor.
351.Bl -tag -width indent -offset indent
352.It Dv FIONREAD ( int )
353Returns the number of bytes that are immediately available for reading.
354.It Dv FIONBIO ( int )
355Set or clear non-blocking I/O.
356If arg is non-zero, then doing a
357.Xr read 2
358when no data is available will return -1 and
359.Va errno
360will be set to
361.Er EAGAIN .
362If arg is zero, non-blocking I/O is disabled.
363Note: setting this
364overrides the timeout set by
365.Dv BIOCSRTIMEOUT .
366.It Dv FIOASYNC ( int )
367Enable or disable async I/O.
368When enabled (arg is non-zero), the process or process group specified by
369.Dv FIOSETOWN
370will start receiving SIGIO's when packets
371arrive.
372Note that you must do an
373.Dv FIOSETOWN
374in order for this to take effect, as
375the system will not default this for you.
376The signal may be changed via
377.Dv BIOCSRSIG .
378.It Dv FIOSETOWN , FIOGETOWN ( int )
379Set or get the process or process group (if negative) that should receive SIGIO
380when packets are available.
381The signal may be changed using
382.Dv BIOCSRSIG
383(see above).
384.El
385.Sh BPF HEADER
386The following structure is prepended to each packet returned by
387.Xr read 2 :
388.Bd -literal -offset indent
389struct bpf_hdr {
390	struct bpf_timeval bh_tstamp;
391	uint32_t bh_caplen;
392	uint32_t bh_datalen;
393	uint16_t bh_hdrlen;
394};
395.Ed
396.Pp
397The fields, whose values are stored in host order, are:
398.Bl -tag -width bh_datalen -offset indent
399.It Va bh_tstamp
400The time at which the packet was processed by the packet filter.
401This structure differs from the standard
402.Vt struct timeval
403in that both members are of type
404.Vt long .
405.It Va bh_caplen
406The length of the captured portion of the packet.
407This is the minimum of
408the truncation amount specified by the filter and the length of the packet.
409.It Va bh_datalen
410The length of the packet off the wire.
411This value is independent of the truncation amount specified by the filter.
412.It Va bh_hdrlen
413The length of the BPF header, which may not be equal to
414.Em sizeof(struct bpf_hdr) .
415.El
416.Pp
417The
418.Va bh_hdrlen
419field exists to account for
420padding between the header and the link level protocol.
421The purpose here is to guarantee proper alignment of the packet
422data structures, which is required on alignment sensitive
423architectures and improves performance on many other architectures.
424The packet filter ensures that the
425.Va bpf_hdr
426and the
427.Em network layer
428header will be word aligned.
429Suitable precautions must be taken when accessing the link layer
430protocol fields on alignment restricted machines.
431(This isn't a problem on an Ethernet, since
432the type field is a short falling on an even offset,
433and the addresses are probably accessed in a bytewise fashion).
434.Pp
435Additionally, individual packets are padded so that each starts
436on a word boundary.
437This requires that an application
438has some knowledge of how to get from packet to packet.
439The macro
440.Dv BPF_WORDALIGN
441is defined in
442.In net/bpf.h
443to facilitate this process.
444It rounds up its argument
445to the nearest word aligned value (where a word is
446.Dv BPF_ALIGNMENT
447bytes wide).
448.Pp
449For example, if
450.Sq Va p
451points to the start of a packet, this expression
452will advance it to the next packet:
453.Pp
454.Dl p = (char *)p + BPF_WORDALIGN(p->bh_hdrlen + p->bh_caplen)
455.Pp
456For the alignment mechanisms to work properly, the
457buffer passed to
458.Xr read 2
459must itself be word aligned.
460.Xr malloc 3
461will always return an aligned buffer.
462.Sh FILTER MACHINE
463A filter program is an array of instructions, with all branches forwardly
464directed, terminated by a
465.Sy return
466instruction.
467Each instruction performs some action on the pseudo-machine state,
468which consists of an accumulator, index register, scratch memory store,
469and implicit program counter.
470.Pp
471The following structure defines the instruction format:
472.Bd -literal -offset indent
473struct bpf_insn {
474	uint16_t code;
475	u_char 	jt;
476	u_char 	jf;
477	uint32_t k;
478};
479.Ed
480.Pp
481The
482.Va k
483field is used in different ways by different instructions,
484and the
485.Va jt
486and
487.Va jf
488fields are used as offsets
489by the branch instructions.
490The opcodes are encoded in a semi-hierarchical fashion.
491There are eight classes of instructions: BPF_LD, BPF_LDX, BPF_ST, BPF_STX,
492BPF_ALU, BPF_JMP, BPF_RET, and BPF_MISC.
493Various other mode and
494operator bits are or'd into the class to give the actual instructions.
495The classes and modes are defined in
496.In net/bpf.h .
497.Pp
498Below are the semantics for each defined BPF instruction.
499We use the convention that A is the accumulator, X is the index register,
500P[] packet data, and M[] scratch memory store.
501P[i:n] gives the data at byte offset
502.Dq i
503in the packet,
504interpreted as a word (n=4),
505unsigned halfword (n=2), or unsigned byte (n=1).
506M[i] gives the i'th word in the scratch memory store, which is only
507addressed in word units.
508The memory store is indexed from 0 to BPF_MEMWORDS-1.
509.Va k ,
510.Va jt ,
511and
512.Va jf
513are the corresponding fields in the
514instruction definition.
515.Dq len
516refers to the length of the packet.
517.Bl -tag -width indent -offset indent
518.It Sy BPF_LD
519These instructions copy a value into the accumulator.
520The type of the source operand is specified by an
521.Dq addressing mode
522and can be a constant
523.Sy ( BPF_IMM ) ,
524packet data at a fixed offset
525.Sy ( BPF_ABS ) ,
526packet data at a variable offset
527.Sy ( BPF_IND ) ,
528the packet length
529.Sy ( BPF_LEN ) ,
530or a word in the scratch memory store
531.Sy ( BPF_MEM ) .
532For
533.Sy BPF_IND
534and
535.Sy BPF_ABS ,
536the data size must be specified as a word
537.Sy ( BPF_W ) ,
538halfword
539.Sy ( BPF_H ) ,
540or byte
541.Sy ( BPF_B ) .
542Arithmetic overflow when calculating a variable offset terminates
543the filter program and the packet is ignored.
544The semantics of all the recognized BPF_LD instructions follow.
545.Bl -column "BPF_LD_BPF_W_BPF_ABS" "A <- P[k:4]" -offset indent
546.It Sy BPF_LD+BPF_W+BPF_ABS Ta A <- P[k:4]
547.It Sy BPF_LD+BPF_H+BPF_ABS Ta A <- P[k:2]
548.It Sy BPF_LD+BPF_B+BPF_ABS Ta A <- P[k:1]
549.It Sy BPF_LD+BPF_W+BPF_IND Ta A <- P[X+k:4]
550.It Sy BPF_LD+BPF_H+BPF_IND Ta A <- P[X+k:2]
551.It Sy BPF_LD+BPF_B+BPF_IND Ta A <- P[X+k:1]
552.It Sy BPF_LD+BPF_W+BPF_LEN Ta A <- len
553.It Sy BPF_LD+BPF_IMM Ta A <- k
554.It Sy BPF_LD+BPF_MEM Ta A <- M[k]
555.El
556.It Sy BPF_LDX
557These instructions load a value into the index register.
558Note that the addressing modes are more restricted than those of
559the accumulator loads, but they include
560.Sy BPF_MSH ,
561a hack for efficiently loading the IP header length.
562.Bl -column "BPF_LDX_BPF_W_BPF_MEM" "X <- k" -offset indent
563.It Sy BPF_LDX+BPF_W+BPF_IMM Ta X <- k
564.It Sy BPF_LDX+BPF_W+BPF_MEM Ta X <- M[k]
565.It Sy BPF_LDX+BPF_W+BPF_LEN Ta X <- len
566.It Sy BPF_LDX+BPF_B+BPF_MSH Ta X <- 4*(P[k:1]&0xf)
567.El
568.It Sy BPF_ST
569This instruction stores the accumulator into the scratch memory.
570We do not need an addressing mode since there is only one possibility
571for the destination.
572.Bl -column "BPF_ST" "M[k] <- A" -offset indent
573.It Sy BPF_ST Ta M[k] <- A
574.El
575.It Sy BPF_STX
576This instruction stores the index register in the scratch memory store.
577.Bl -column "BPF_STX" "M[k] <- X" -offset indent
578.It Sy BPF_STX Ta M[k] <- X
579.El
580.It Sy BPF_ALU
581The alu instructions perform operations between the accumulator and
582index register or constant, and store the result back in the accumulator.
583For binary operations, a source mode is required
584.Sy ( BPF_K
585or
586.Sy BPF_X ) .
587.Bl -column "BPF_ALU_BPF_ADD_BPF_K" "A <- A + k" -offset indent
588.It Sy BPF_ALU+BPF_ADD+BPF_K Ta A <- A + k
589.It Sy BPF_ALU+BPF_SUB+BPF_K Ta A <- A - k
590.It Sy BPF_ALU+BPF_MUL+BPF_K Ta A <- A * k
591.It Sy BPF_ALU+BPF_DIV+BPF_K Ta A <- A / k
592.It Sy BPF_ALU+BPF_AND+BPF_K Ta A <- A & k
593.It Sy BPF_ALU+BPF_OR+BPF_K Ta A <- A | k
594.It Sy BPF_ALU+BPF_LSH+BPF_K Ta A <- A << k
595.It Sy BPF_ALU+BPF_RSH+BPF_K Ta A <- A >> k
596.It Sy BPF_ALU+BPF_ADD+BPF_X Ta A <- A + X
597.It Sy BPF_ALU+BPF_SUB+BPF_X Ta A <- A - X
598.It Sy BPF_ALU+BPF_MUL+BPF_X Ta A <- A * X
599.It Sy BPF_ALU+BPF_DIV+BPF_X Ta A <- A / X
600.It Sy BPF_ALU+BPF_AND+BPF_X Ta A <- A & X
601.It Sy BPF_ALU+BPF_OR+BPF_X Ta A <- A | X
602.It Sy BPF_ALU+BPF_LSH+BPF_X Ta A <- A << X
603.It Sy BPF_ALU+BPF_RSH+BPF_X Ta A <- A >> X
604.It Sy BPF_ALU+BPF_NEG Ta A <- -A
605.El
606.It Sy BPF_JMP
607The jump instructions alter flow of control.
608Conditional jumps compare the accumulator against a constant
609.Sy ( BPF_K )
610or the index register
611.Sy ( BPF_X ) .
612If the result is true (or non-zero),
613the true branch is taken, otherwise the false branch is taken.
614Jump offsets are encoded in 8 bits so the longest jump is 256 instructions.
615However, the jump always
616.Sy ( BPF_JA )
617opcode uses the 32 bit
618.Va k
619field as the offset, allowing arbitrarily distant destinations.
620All conditionals use unsigned comparison conventions.
621.Bl -column "BPF_JMP+BPF_JSET+BPF_K" "pc += (A \*[Ge] k) ? jt : jf" -offset indent
622.It Sy BPF_JMP+BPF_JA Ta pc += k
623.It Sy BPF_JMP+BPF_JGT+BPF_K Ta "pc += (A > k) ? jt : jf"
624.It Sy BPF_JMP+BPF_JGE+BPF_K Ta "pc += (A \*[Ge] k) ? jt : jf"
625.It Sy BPF_JMP+BPF_JEQ+BPF_K Ta "pc += (A == k) ? jt : jf"
626.It Sy BPF_JMP+BPF_JSET+BPF_K Ta "pc += (A & k) ? jt : jf"
627.It Sy BPF_JMP+BPF_JGT+BPF_X Ta "pc += (A > X) ? jt : jf"
628.It Sy BPF_JMP+BPF_JGE+BPF_X Ta "pc += (A \*[Ge] X) ? jt : jf"
629.It Sy BPF_JMP+BPF_JEQ+BPF_X Ta "pc += (A == X) ? jt : jf"
630.It Sy BPF_JMP+BPF_JSET+BPF_X Ta "pc += (A & X) ? jt : jf"
631.El
632.It Sy BPF_RET
633The return instructions terminate the filter program and specify the amount
634of packet to accept (i.e., they return the truncation amount).
635A return value of zero indicates that the packet should be ignored.
636The return value is either a constant
637.Sy ( BPF_K )
638or the accumulator
639.Sy ( BPF_A ) .
640.Bl -column "BPF_RET+BPF_A" "accept A bytes" -offset indent
641.It Sy BPF_RET+BPF_A Ta accept A bytes
642.It Sy BPF_RET+BPF_K Ta accept k bytes
643.El
644.It Sy BPF_MISC
645The miscellaneous category was created for anything that doesn't
646fit into the above classes, and for any new instructions that might need to
647be added.
648Currently, these are the register transfer instructions
649that copy the index register to the accumulator or vice versa.
650.Bl -column "BPF_MISC+BPF_TAX" "X <- A" -offset indent
651.It Sy BPF_MISC+BPF_TAX Ta X <- A
652.It Sy BPF_MISC+BPF_TXA Ta A <- X
653.El
654.Pp
655Also, two instructions to call a "coprocessor" if initialized by the kernel
656component.
657There is no coprocessor by default.
658.Bl -column "BPF_MISC+BPF_COPX" "A <- funcs[X](...)" -offset indent
659.It Sy BPF_MISC+BPF_COP Ta A <- funcs[k](..)
660.It Sy BPF_MISC+BPF_COPX Ta A <- funcs[X](..)
661.El
662.Pp
663If the coprocessor is not set or the function index is out of range, these
664instructions will abort the program and return zero.
665.El
666.Pp
667The BPF interface provides the following macros to facilitate
668array initializers:
669.Bd -unfilled -offset indent
670.Fn BPF_STMT opcode operand
671.Fn BPF_JUMP opcode operand true_offset false_offset
672.Ed
673.Sh SYSCTLS
674The following sysctls are available when
675.Nm
676is enabled:
677.Bl -tag -width "XnetXbpfXmaxbufsizeXX"
678.It Li net.bpf.maxbufsize
679Sets the maximum buffer size available for
680.Nm
681peers.
682.It Li net.bpf.stats
683Shows
684.Nm
685statistics.
686They can be retrieved with the
687.Xr netstat 1
688utility.
689.It Li net.bpf.peers
690Shows the current
691.Nm
692peers.
693This is only available to the super user and can also be retrieved with the
694.Xr netstat 1
695utility.
696.El
697.Pp
698On architectures with
699.Xr bpfjit 4
700support, the additional sysctl is available:
701.Bl -tag -width "XnetXbpfXjitXX"
702.It Li net.bpf.jit
703Toggle
704.Sy Just-In-Time
705compilation of new filter programs.
706In order to enable Just-In-Time compilation,
707the bpfjit kernel module must be loaded.
708Changing a value of this sysctl doesn't affect
709existing filter programs.
710.El
711.Sh FILES
712.Pa /dev/bpf
713.Sh EXAMPLES
714The following filter is taken from the Reverse ARP Daemon.
715It accepts only Reverse ARP requests.
716.Bd -literal -offset indent
717struct bpf_insn insns[] = {
718	BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
719	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3),
720	BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
721	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, REVARP_REQUEST, 0, 1),
722	BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) +
723	    sizeof(struct ether_header)),
724	BPF_STMT(BPF_RET+BPF_K, 0),
725};
726.Ed
727.Pp
728This filter accepts only IP packets between host 128.3.112.15 and
729128.3.112.35.
730.Bd -literal -offset indent
731struct bpf_insn insns[] = {
732	BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
733	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 8),
734	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
735	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
736	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
737	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
738	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
739	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
740	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
741	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
742	BPF_STMT(BPF_RET+BPF_K, 0),
743};
744.Ed
745.Pp
746Finally, this filter returns only TCP finger packets.
747We must parse the IP header to reach the TCP header.
748The
749.Sy BPF_JSET
750instruction checks that the IP fragment offset is 0 so we are sure
751that we have a TCP header.
752.Bd -literal -offset indent
753struct bpf_insn insns[] = {
754	BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
755	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 10),
756	BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
757	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_TCP, 0, 8),
758	BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
759	BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
760	BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
761	BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
762	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
763	BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
764	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
765	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
766	BPF_STMT(BPF_RET+BPF_K, 0),
767};
768.Ed
769.Sh SEE ALSO
770.Xr ioctl 2 ,
771.Xr read 2 ,
772.Xr select 2 ,
773.Xr signal 3 ,
774.Xr bpfjit 4 ,
775.Xr tcpdump 8
776.Rs
777.%T "The BSD Packet Filter: A New Architecture for User-level Packet Capture"
778.%A S. McCanne
779.%A V. Jacobson
780.%J Proceedings of the 1993 Winter USENIX
781.%C Technical Conference, San Diego, CA
782.Re
783.Sh HISTORY
784The Enet packet filter was created in 1980 by Mike Accetta and
785Rick Rashid at Carnegie-Mellon University.
786Jeffrey Mogul, at Stanford, ported the code to BSD and continued
787its development from 1983 on.
788Since then, it has evolved into the ULTRIX Packet Filter
789at DEC, a STREAMS NIT module under SunOS 4.1, and BPF.
790.Sh AUTHORS
791.An -nosplit
792.An Steven McCanne ,
793of Lawrence Berkeley Laboratory, implemented BPF in Summer 1990.
794The design was in collaboration with
795.An Van Jacobson ,
796also of Lawrence Berkeley Laboratory.
797.Sh BUGS
798The read buffer must be of a fixed size (returned by the
799.Dv BIOCGBLEN
800ioctl).
801.Pp
802A file that does not request promiscuous mode may receive promiscuously
803received packets as a side effect of another file requesting this
804mode on the same hardware interface.
805This could be fixed in the kernel with additional processing overhead.
806However, we favor the model where
807all files must assume that the interface is promiscuous, and if
808so desired, must use a filter to reject foreign packets.
809.Pp
810Under SunOS, if a BPF application reads more than 2^31 bytes of
811data, read will fail in
812.Er EINVAL .
813You can either fix the bug in SunOS,
814or lseek to 0 when read fails for this reason.
815.Pp
816.Dq Immediate mode
817and the
818.Dq read timeout
819are misguided features.
820This functionality can be emulated with non-blocking mode and
821.Xr select 2 .
822