3 .\" $NetBSD: bpf.4,v 1.40 2009/04/10 13:37:38 joerg Exp $
5 .\" Copyright (c) 1990, 1991, 1992, 1993, 1994
6 .\" The Regents of the University of California. All rights reserved.
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.
24 .\" This document is derived in part from the enet man page (enet.4)
25 .\" distributed with 4.3BSD Unix.
32 .Nd Berkeley Packet Filter raw network interface
34 .Cd "pseudo-device bpfilter"
36 The Berkeley Packet Filter
37 provides a raw interface to data link layers in a protocol
39 All packets on the network, even those destined for other hosts,
40 are accessible through this mechanism.
42 The packet filter appears as a character special device,
44 After opening the device, the file descriptor must be bound to a
45 specific network interface with the
48 A given interface can be shared by multiple listeners, and the filter
49 underlying each descriptor will see an identical packet stream.
51 Associated with each open instance of a
53 file is a user-settable packet filter.
54 Whenever a packet is received by an interface,
55 all file descriptors listening on that interface apply their filter.
56 Each descriptor that accepts the packet receives its own copy.
58 Reads from these files return the next group of packets
59 that have matched the filter.
60 To improve performance, the buffer passed to read must be
61 the same size as the buffers used internally by
63 This size is returned by the
65 ioctl (see below), and under
68 Note that an individual packet larger than this size is necessarily
71 The packet filter will support any link level protocol that has fixed length
73 Currently, only Ethernet, SLIP and PPP drivers have been
74 modified to interact with
77 Since packet data is in network byte order, applications should use the
79 macros to extract multi-byte values.
81 A packet can be sent out on the network by writing to a
84 The writes are unbuffered, meaning only one packet can be processed per write.
85 Currently, only writes to Ethernets and SLIP links are supported.
89 command codes below are defined in
91 All commands require these includes:
92 .Bd -literal -offset indent
93 #include \*[Lt]sys/types.h\*[Gt]
94 #include \*[Lt]sys/time.h\*[Gt]
95 #include \*[Lt]sys/ioctl.h\*[Gt]
96 #include \*[Lt]net/bpf.h\*[Gt]
104 .Pa \*[Lt]net/if.h\*[Gt] .
106 The (third) argument to the
108 should be a pointer to the type indicated.
109 .Bl -tag -width indent -offset indent
110 .It Dv "BIOCGBLEN (u_int)"
111 Returns the required buffer length for reads on
114 .It Dv "BIOCSBLEN (u_int)"
115 Sets the buffer length for reads on
118 The buffer must be set before the file is attached to an interface with
120 If the requested buffer size cannot be accommodated, the closest
121 allowable size will be set and returned in the argument.
122 A read call will result in
124 if it is passed a buffer that is not this size.
125 .It Dv BIOCGDLT (u_int)
126 Returns the type of the data link layer underlying the attached interface.
128 is returned if no interface has been specified.
129 The device types, prefixed with
133 .It Dv BIOCGDLTLIST (struct bpf_dltlist)
134 Returns an array of available type of the data link layer
135 underlying the attached interface:
136 .Bd -literal -offset indent
143 The available type is returned to the array pointed to the
145 field while its length in u_int is supplied to the
149 is returned if there is not enough buffer.
152 field is modified on return to indicate the actual length in u_int
153 of the array returned.
160 field is returned to indicate the required length of an array in u_int.
161 .It Dv BIOCSDLT (u_int)
162 Change the type of the data link layer underlying the attached interface.
164 is returned if no interface has been specified or the specified
165 type is not available for the interface.
167 Forces the interface into promiscuous mode.
168 All packets, not just those destined for the local host, are processed.
169 Since more than one file can be listening on a given interface,
170 a listener that opened its interface non-promiscuously may receive
171 packets promiscuously.
172 This problem can be remedied with an appropriate filter.
174 The interface remains in promiscuous mode until all files listening
175 promiscuously are closed.
177 Flushes the buffer of incoming packets,
178 and resets the statistics that are returned by
180 .It Dv BIOCGETIF (struct ifreq)
181 Returns the name of the hardware interface that the file is listening on.
182 The name is returned in the ifr_name field of
184 All other fields are undefined.
185 .It Dv BIOCSETIF (struct ifreq)
186 Sets the hardware interface associate with the file.
187 This command must be performed before any packets can be read.
188 The device is indicated by name using the
192 Additionally, performs the actions of
194 .It Dv BIOCSRTIMEOUT, BIOCGRTIMEOUT (struct timeval)
195 Set or get the read timeout parameter.
198 specifies the length of time to wait before timing
199 out on a read request.
200 This parameter is initialized to zero by
202 indicating no timeout.
203 .It Dv BIOCGSTATS (struct bpf_stat)
204 Returns the following structure of packet statistics:
205 .Bd -literal -offset indent
210 uint64_t bs_padding[13];
215 .Bl -tag -width bs_recv -offset indent
217 the number of packets received by the descriptor since opened or reset
218 (including any buffered since the last read call);
220 the number of packets which were accepted by the filter but dropped by the
221 kernel because of buffer overflows
222 (i.e., the application's reads aren't keeping up with the packet
225 the number of packets accepted by the filter.
227 .It Dv BIOCIMMEDIATE (u_int)
230 based on the truth value of the argument.
231 When immediate mode is enabled, reads return immediately upon packet
233 Otherwise, a read will block until either the kernel buffer
234 becomes full or a timeout occurs.
235 This is useful for programs like
237 which must respond to messages in real time.
238 The default for a new file is off.
239 .It Dv BIOCSETF (struct bpf_program)
240 Sets the filter program used by the kernel to discard uninteresting
242 An array of instructions and its length is passed in using the following structure:
243 .Bd -literal -offset indent
246 struct bpf_insn *bf_insns;
250 The filter program is pointed to by the
252 field while its length in units of
263 for an explanation of the filter language.
264 .It Dv BIOCVERSION (struct bpf_version)
265 Returns the major and minor version numbers of the filter language currently
266 recognized by the kernel.
267 Before installing a filter, applications must check
268 that the current version is compatible with the running kernel.
269 Version numbers are compatible if the major numbers match and the
270 application minor is less than or equal to the kernel minor.
271 The kernel version number is returned in the following structure:
272 .Bd -literal -offset indent
279 The current version numbers are given by
280 .Dv BPF_MAJOR_VERSION
282 .Dv BPF_MINOR_VERSION
285 An incompatible filter
286 may result in undefined behavior (most likely, an error returned by
288 or haphazard packet matching).
289 .It Dv BIOCGHDRCMPLT BIOCSHDRCMPLT (u_int)
290 Enable/disable or get the
293 If enabled, packets written to the bpf file descriptor will not have
294 network layer headers rewritten in the interface output routine.
295 By default, the flag is disabled (value is 0).
296 .It Dv BIOCGSEESENT BIOCSSEESENT (u_int)
297 Enable/disable or get the
300 If enabled, packets sent will be passed to the filter.
301 By default, the flag is enabled (value is 1).
305 now supports several standard
307 which allow the user to do async and/or non-blocking I/O to an open
310 .Bl -tag -width indent -offset indent
311 .It Dv FIONREAD (int)
312 Returns the number of bytes that are immediately available for reading.
313 .It Dv SIOCGIFADDR (struct ifreq)
314 Returns the address associated with the interface.
316 Set or clear non-blocking I/O.
317 If arg is non-zero, then doing a
319 when no data is available will return -1 and
323 If arg is zero, non-blocking I/O is disabled.
325 overrides the timeout set by
327 .It Dv FIOASYNC (int)
328 Enable or disable async I/O.
329 When enabled (arg is non-zero), the process or process group specified by
331 will start receiving SIGIO's when packets
333 Note that you must do an
335 in order for this to take effect, as
336 the system will not default this for you.
337 The signal may be changed via
339 .It Dv FIOSETOWN FIOGETOWN (int)
340 Set or get the process or process group (if negative) that should receive SIGIO
341 when packets are available.
342 The signal may be changed using
347 The following structure is prepended to each packet returned by
349 .Bd -literal -offset indent
351 struct timeval bh_tstamp;
358 The fields, whose values are stored in host order, and are:
359 .Bl -tag -width bh_datalen -offset indent
361 The time at which the packet was processed by the packet filter.
363 The length of the captured portion of the packet.
364 This is the minimum of
365 the truncation amount specified by the filter and the length of the packet.
367 The length of the packet off the wire.
368 This value is independent of the truncation amount specified by the filter.
370 The length of the BPF header, which may not be equal to
371 .Em sizeof(struct bpf_hdr) .
376 field exists to account for
377 padding between the header and the link level protocol.
378 The purpose here is to guarantee proper alignment of the packet
379 data structures, which is required on alignment sensitive
380 architectures and improves performance on many other architectures.
381 The packet filter ensures that the
385 header will be word aligned.
386 Suitable precautions must be taken when accessing the link layer
387 protocol fields on alignment restricted machines.
388 (This isn't a problem on an Ethernet, since
389 the type field is a short falling on an even offset,
390 and the addresses are probably accessed in a bytewise fashion).
392 Additionally, individual packets are padded so that each starts
394 This requires that an application
395 has some knowledge of how to get from packet to packet.
400 to facilitate this process.
401 It rounds up its argument
402 to the nearest word aligned value (where a word is
408 points to the start of a packet, this expression
409 will advance it to the next packet:
411 .Dl p = (char *)p + BPF_WORDALIGN(p-\*[Gt]bh_hdrlen + p-\*[Gt]bh_caplen)
413 For the alignment mechanisms to work properly, the
416 must itself be word aligned.
418 will always return an aligned buffer.
420 A filter program is an array of instructions, with all branches forwardly
421 directed, terminated by a
424 Each instruction performs some action on the pseudo-machine state,
425 which consists of an accumulator, index register, scratch memory store,
426 and implicit program counter.
428 The following structure defines the instruction format:
429 .Bd -literal -offset indent
440 field is used in different ways by different instructions,
445 fields are used as offsets
446 by the branch instructions.
447 The opcodes are encoded in a semi-hierarchical fashion.
448 There are eight classes of instructions: BPF_LD, BPF_LDX, BPF_ST, BPF_STX,
449 BPF_ALU, BPF_JMP, BPF_RET, and BPF_MISC.
450 Various other mode and
451 operator bits are or'd into the class to give the actual instructions.
452 The classes and modes are defined in
455 Below are the semantics for each defined BPF instruction.
456 We use the convention that A is the accumulator, X is the index register,
457 P[] packet data, and M[] scratch memory store.
458 P[i:n] gives the data at byte offset
461 interpreted as a word (n=4),
462 unsigned halfword (n=2), or unsigned byte (n=1).
463 M[i] gives the i'th word in the scratch memory store, which is only
464 addressed in word units.
465 The memory store is indexed from 0 to BPF_MEMWORDS-1.
470 are the corresponding fields in the
471 instruction definition.
473 refers to the length of the packet.
474 .Bl -tag -width indent -offset indent
476 These instructions copy a value into the accumulator.
477 The type of the source operand is specified by an
479 and can be a constant
481 packet data at a fixed offset
483 packet data at a variable offset
487 or a word in the scratch memory store
493 the data size must be specified as a word
499 The semantics of all the recognized BPF_LD instructions follow.
500 .Bl -column "BPF_LD_BPF_W_BPF_ABS" "A \*[Lt]- P[k:4]" -offset indent
501 .It Sy BPF_LD+BPF_W+BPF_ABS Ta A \*[Lt]- P[k:4]
502 .It Sy BPF_LD+BPF_H+BPF_ABS Ta A \*[Lt]- P[k:2]
503 .It Sy BPF_LD+BPF_B+BPF_ABS Ta A \*[Lt]- P[k:1]
504 .It Sy BPF_LD+BPF_W+BPF_IND Ta A \*[Lt]- P[X+k:4]
505 .It Sy BPF_LD+BPF_H+BPF_IND Ta A \*[Lt]- P[X+k:2]
506 .It Sy BPF_LD+BPF_B+BPF_IND Ta A \*[Lt]- P[X+k:1]
507 .It Sy BPF_LD+BPF_W+BPF_LEN Ta A \*[Lt]- len
508 .It Sy BPF_LD+BPF_IMM Ta A \*[Lt]- k
509 .It Sy BPF_LD+BPF_MEM Ta A \*[Lt]- M[k]
512 These instructions load a value into the index register.
513 Note that the addressing modes are more restricted than those of
514 the accumulator loads, but they include
516 a hack for efficiently loading the IP header length.
517 .Bl -column "BPF_LDX_BPF_W_BPF_IMM" "X \*[Lt]- k" -offset indent
518 .It Sy BPF_LDX+BPF_W+BPF_IMM Ta X \*[Lt]- k
519 .It Sy BPF_LDX+BPF_W+BPF_MEM Ta X \*[Lt]- M[k]
520 .It Sy BPF_LDX+BPF_W+BPF_LEN Ta X \*[Lt]- len
521 .It Sy BPF_LDX+BPF_B+BPF_MSH Ta X \*[Lt]- 4*(P[k:1]\*[Am]0xf)
524 This instruction stores the accumulator into the scratch memory.
525 We do not need an addressing mode since there is only one possibility
527 .Bl -column "BPF_ST" "M[k] \*[Lt]- A" -offset indent
528 .It Sy BPF_ST Ta M[k] \*[Lt]- A
531 This instruction stores the index register in the scratch memory store.
532 .Bl -column "BPF_STX" "M[k] \*[Lt]- X" -offset indent
533 .It Sy BPF_STX Ta M[k] \*[Lt]- X
536 The alu instructions perform operations between the accumulator and
537 index register or constant, and store the result back in the accumulator.
538 For binary operations, a source mode is required
542 .Bl -column "BPF_ALU_BPF_ADD_BPF_K" "A \*[Lt]- A + k" -offset indent
543 .It Sy BPF_ALU+BPF_ADD+BPF_K Ta A \*[Lt]- A + k
544 .It Sy BPF_ALU+BPF_SUB+BPF_K Ta A \*[Lt]- A - k
545 .It Sy BPF_ALU+BPF_MUL+BPF_K Ta A \*[Lt]- A * k
546 .It Sy BPF_ALU+BPF_DIV+BPF_K Ta A \*[Lt]- A / k
547 .It Sy BPF_ALU+BPF_AND+BPF_K Ta A \*[Lt]- A \*[Am] k
548 .It Sy BPF_ALU+BPF_OR+BPF_K Ta A \*[Lt]- A | k
549 .It Sy BPF_ALU+BPF_LSH+BPF_K Ta A \*[Lt]- A \*[Lt]\*[Lt] k
550 .It Sy BPF_ALU+BPF_RSH+BPF_K Ta A \*[Lt]- A \*[Gt]\*[Gt] k
551 .It Sy BPF_ALU+BPF_ADD+BPF_X Ta A \*[Lt]- A + X
552 .It Sy BPF_ALU+BPF_SUB+BPF_X Ta A \*[Lt]- A - X
553 .It Sy BPF_ALU+BPF_MUL+BPF_X Ta A \*[Lt]- A * X
554 .It Sy BPF_ALU+BPF_DIV+BPF_X Ta A \*[Lt]- A / X
555 .It Sy BPF_ALU+BPF_AND+BPF_X Ta A \*[Lt]- A \*[Am] X
556 .It Sy BPF_ALU+BPF_OR+BPF_X Ta A \*[Lt]- A | X
557 .It Sy BPF_ALU+BPF_LSH+BPF_X Ta A \*[Lt]- A \*[Lt]\*[Lt] X
558 .It Sy BPF_ALU+BPF_RSH+BPF_X Ta A \*[Lt]- A \*[Gt]\*[Gt] X
559 .It Sy BPF_ALU+BPF_NEG Ta A \*[Lt]- -A
562 The jump instructions alter flow of control.
563 Conditional jumps compare the accumulator against a constant
565 or the index register
567 If the result is true (or non-zero),
568 the true branch is taken, otherwise the false branch is taken.
569 Jump offsets are encoded in 8 bits so the longest jump is 256 instructions.
570 However, the jump always
572 opcode uses the 32 bit
574 field as the offset, allowing arbitrarily distant destinations.
575 All conditionals use unsigned comparison conventions.
576 .Bl -column "BPF_JMP+BPF_JGE+BPF_K" "pc += (A \*[Ge] k) ? jt : jf" -offset indent
577 .It Sy BPF_JMP+BPF_JA Ta pc += k
578 .It Sy BPF_JMP+BPF_JGT+BPF_K Ta "pc += (A \*[Gt] k) ? jt : jf"
579 .It Sy BPF_JMP+BPF_JGE+BPF_K Ta "pc += (A \*[Ge] k) ? jt : jf"
580 .It Sy BPF_JMP+BPF_JEQ+BPF_K Ta "pc += (A == k) ? jt : jf"
581 .It Sy BPF_JMP+BPF_JSET+BPF_K Ta "pc += (A \*[Am] k) ? jt : jf"
582 .It Sy BPF_JMP+BPF_JGT+BPF_X Ta "pc += (A \*[Gt] X) ? jt : jf"
583 .It Sy BPF_JMP+BPF_JGE+BPF_X Ta "pc += (A \*[Ge] X) ? jt : jf"
584 .It Sy BPF_JMP+BPF_JEQ+BPF_X Ta "pc += (A == X) ? jt : jf"
585 .It Sy BPF_JMP+BPF_JSET+BPF_X Ta "pc += (A \*[Am] X) ? jt : jf"
588 The return instructions terminate the filter program and specify the amount
589 of packet to accept (i.e., they return the truncation amount).
590 A return value of zero indicates that the packet should be ignored.
591 The return value is either a constant
595 .Bl -column "BPF_RET+BPF_A" "accept A bytes" -offset indent
596 .It Sy BPF_RET+BPF_A Ta accept A bytes
597 .It Sy BPF_RET+BPF_K Ta accept k bytes
600 The miscellaneous category was created for anything that doesn't
601 fit into the above classes, and for any new instructions that might need to
603 Currently, these are the register transfer instructions
604 that copy the index register to the accumulator or vice versa.
605 .Bl -column "BPF_MISC+BPF_TAX" "X \*[Lt]- A" -offset indent
606 .It Sy BPF_MISC+BPF_TAX Ta X \*[Lt]- A
607 .It Sy BPF_MISC+BPF_TXA Ta A \*[Lt]- X
611 The BPF interface provides the following macros to facilitate
613 .Bd -unfilled -offset indent
614 .Sy BPF_STMT No (opcode, operand)
615 .Sy BPF_JUMP No (opcode, operand, true_offset, false_offset)
618 The following sysctls are available when
622 .Bl -tag -width "XnetXbpfXmaxbufsizeXX"
623 .It Li net.bpf.maxbufsize
624 Sets the maximum buffer size available for
631 They can be retrieved with the
638 This is only available to the super user and can also be retrieved with the
645 The following filter is taken from the Reverse ARP Daemon.
646 It accepts only Reverse ARP requests.
647 .Bd -literal -offset indent
648 struct bpf_insn insns[] = {
649 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
650 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3),
651 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
652 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, REVARP_REQUEST, 0, 1),
653 BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) +
654 sizeof(struct ether_header)),
655 BPF_STMT(BPF_RET+BPF_K, 0),
659 This filter accepts only IP packets between host 128.3.112.15 and
661 .Bd -literal -offset indent
662 struct bpf_insn insns[] = {
663 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
664 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 8),
665 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
666 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
667 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
668 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
669 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
670 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
671 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
672 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
673 BPF_STMT(BPF_RET+BPF_K, 0),
677 Finally, this filter returns only TCP finger packets.
678 We must parse the IP header to reach the TCP header.
681 instruction checks that the IP fragment offset is 0 so we are sure
682 that we have a TCP header.
683 .Bd -literal -offset indent
684 struct bpf_insn insns[] = {
685 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
686 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 10),
687 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
688 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_TCP, 0, 8),
689 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
690 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
691 BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
692 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
693 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
694 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
695 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
696 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
697 BPF_STMT(BPF_RET+BPF_K, 0),
707 .%T "The BSD Packet Filter: A New Architecture for User-level Packet Capture"
710 .%J Proceedings of the 1993 Winter USENIX
711 .%C Technical Conference, San Diego, CA
714 The Enet packet filter was created in 1980 by Mike Accetta and
715 Rick Rashid at Carnegie-Mellon University.
716 Jeffrey Mogul, at Stanford, ported the code to BSD and continued
717 its development from 1983 on.
718 Since then, it has evolved into the ULTRIX Packet Filter
719 at DEC, a STREAMS NIT module under SunOS 4.1, and BPF.
721 Steven McCanne, of Lawrence Berkeley Laboratory, implemented BPF in
723 The design was in collaboration with Van Jacobson,
724 also of Lawrence Berkeley Laboratory.
726 The read buffer must be of a fixed size (returned by the
730 A file that does not request promiscuous mode may receive promiscuously
731 received packets as a side effect of another file requesting this
732 mode on the same hardware interface.
733 This could be fixed in the kernel with additional processing overhead.
734 However, we favor the model where
735 all files must assume that the interface is promiscuous, and if
736 so desired, must use a filter to reject foreign packets.
738 Data link protocols with variable length headers are not currently supported.
740 Under SunOS, if a BPF application reads more than 2^31 bytes of
741 data, read will fail in
743 You can either fix the bug in SunOS,
744 or lseek to 0 when read fails for this reason.
749 are misguided features.
750 This functionality can be emulated with non-blocking mode and