1 .\" Copyright (c) 1990 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that: (1) source code distributions
6 .\" retain the above copyright notice and this paragraph in its entirety, (2)
7 .\" distributions including binary code include the above copyright notice and
8 .\" this paragraph in its entirety in the documentation or other materials
9 .\" provided with the distribution, and (3) all advertising materials mentioning
10 .\" features or use of this software display the following acknowledgement:
11 .\" ``This product includes software developed by the University of California,
12 .\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
13 .\" the University nor the names of its contributors may be used to endorse
14 .\" or promote products derived from this software without specific prior
15 .\" written permission.
16 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17 .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 .\" This document is derived in part from the enet man page (enet.4)
21 .\" distributed with 4.3BSD Unix.
30 .Nd Berkeley Packet Filter
34 The Berkeley Packet Filter
35 provides a raw interface to data link layers in a protocol
37 All packets on the network, even those destined for other hosts,
38 are accessible through this mechanism.
40 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 be multiple listeners, and the filter
49 underlying each descriptor will see an identical packet stream.
50 The total number of open
51 files is limited to the value given in the kernel configuration; the
54 above sets the limit to 16.
56 A separate device file is required for each minor device.
57 If a file is in use, the open will fail and
62 Associated with each open instance of a
64 file is a user-settable packet filter.
65 Whenever a packet is received by an interface,
66 all file descriptors listening on that interface apply their filter.
67 Each descriptor that accepts the packet receives its own copy.
69 Reads from these files return the next group of packets
70 that have matched the filter.
71 To improve performance, the buffer passed to read must be
72 the same size as the buffers used internally by
74 This size is returned by the
76 ioctl (see below), and
79 Note that an individual packet larger than this size is necessarily
82 The packet filter will support any link level protocol that has fixed length
83 headers. Currently, only Ethernet,
87 drivers have been modified to interact with
90 Since packet data is in network byte order, applications should use the
92 macros to extract multi-byte values.
94 A packet can be sent out on the network by writing to a
96 file descriptor. The writes are unbuffered, meaning only one
97 packet can be processed per write.
98 Currently, only writes to Ethernets and
104 command codes below are defined in
109 #include <sys/types.h>
110 #include <sys/time.h>
111 #include <sys/ioctl.h>
128 the following commands may be applied to any open
131 The (third) argument to
133 should be a pointer to the type indicated.
135 .Bl -tag -width BIOCGRTIMEOUT
138 Returns the required buffer length for reads on
143 Sets the buffer length for reads on
145 files. The buffer must be set before the file is attached to an interface
148 If the requested buffer size cannot be accommodated, the closest
149 allowable size will be set and returned in the argument.
150 A read call will result in
152 if it is passed a buffer that is not this size.
155 Returns the type of the data link layer underlying the attached interface.
157 is returned if no interface has been specified.
158 The device types, prefixed with
163 Forces the interface into promiscuous mode.
164 All packets, not just those destined for the local host, are processed.
165 Since more than one file can be listening on a given interface,
166 a listener that opened its interface non-promiscuously may receive
167 packets promiscuously. This problem can be remedied with an
170 Flushes the buffer of incoming packets,
171 and resets the statistics that are returned by BIOCGSTATS.
173 .Pq Li "struct ifreq"
174 Returns the name of the hardware interface that the file is listening on.
175 The name is returned in the ifr_name field of
179 All other fields are undefined.
181 .Pq Li "struct ifreq"
182 Sets the hardware interface associate with the file. This
183 command must be performed before any packets can be read.
184 The device is indicated by name using the
189 Additionally, performs the actions of
193 .Pq Li "struct timeval"
194 Set or get the read timeout parameter.
196 specifies the length of time to wait before timing
197 out on a read request.
198 This parameter is initialized to zero by
200 indicating no timeout.
202 .Pq Li "struct bpf_stat"
203 Returns the following structure of packet statistics:
206 u_int bs_recv; /* number of packets received */
207 u_int bs_drop; /* number of packets dropped */
212 .Bl -hang -offset indent
214 the number of packets received by the descriptor since opened or reset
215 (including any buffered since the last read call);
218 the number of packets which were accepted by the filter but dropped by the
219 kernel because of buffer overflows
220 (i.e., the application's reads aren't keeping up with the packet traffic).
226 based on the truth value of the argument.
227 When immediate mode is enabled, reads return immediately upon packet
228 reception. Otherwise, a read will block until either the kernel buffer
229 becomes full or a timeout occurs.
230 This is useful for programs like
232 which must respond to messages in real time.
233 The default for a new file is off.
235 .Pq Li "struct bpf_program"
236 Sets the filter program used by the kernel to discard uninteresting
237 packets. An array of instructions and its length is passed in using
238 the following structure:
242 struct bpf_insn *bf_insns;
246 The filter program is pointed to by the
248 field while its length in units of
249 .Sq Li struct bpf_insn
254 .Dv BIOCFLUSH are performed.
257 for an explanation of the filter language.
259 .Pq Li "struct bpf_version"
260 Returns the major and minor version numbers of the filter language currently
261 recognized by the kernel. Before installing a filter, applications must check
262 that the current version is compatible with the running kernel. Version
263 numbers are compatible if the major numbers match and the application minor
264 is less than or equal to the kernel minor. The kernel version number is
265 returned in the following structure:
273 The current version numbers are given by
274 .Dv BPF_MAJOR_VERSION
276 .Dv BPF_MINOR_VERSION
279 An incompatible filter
280 may result in undefined behavior (most likely, an error returned by
282 or haphazard packet matching).
286 Set or get the status of the
289 Set to zero if the link level source address should be filled in automatically
290 by the the interface output routine. Set to one if the link level source
291 address will be written, as provided, to the wire. This flag is initialized
294 The following structure is prepended to each packet returned by
298 struct timeval bh_tstamp; /* time stamp */
299 u_long bh_caplen; /* length of captured portion */
300 u_long bh_datalen; /* original length of packet */
301 u_short bh_hdrlen; /* length of bpf header (this struct
302 plus alignment padding */
306 The fields, whose values are stored in host order, and are:
308 .Bl -tag -compact -width bh_datalen
310 The time at which the packet was processed by the packet filter.
312 The length of the captured portion of the packet. This is the minimum of
313 the truncation amount specified by the filter and the length of the packet.
315 The length of the packet off the wire.
316 This value is independent of the truncation amount specified by the filter.
320 header, which may not be equal to
321 .\" XXX - not really a function call
322 .Fn sizeof "struct bpf_hdr" .
327 field exists to account for
328 padding between the header and the link level protocol.
329 The purpose here is to guarantee proper alignment of the packet
330 data structures, which is required on alignment sensitive
331 architectures and improves performance on many other architectures.
332 The packet filter insures that the
334 and the network layer
335 header will be word aligned. Suitable precautions
336 must be taken when accessing the link layer protocol fields on alignment
337 restricted machines. (This isn't a problem on an Ethernet, since
338 the type field is a short falling on an even offset,
339 and the addresses are probably accessed in a bytewise fashion).
341 Additionally, individual packets are padded so that each starts
342 on a word boundary. This requires that an application
343 has some knowledge of how to get from packet to packet.
349 this process. It rounds up its argument
350 to the nearest word aligned value (where a word is
356 points to the start of a packet, this expression
357 will advance it to the next packet:
358 .Dl p = (char *)p + BPF_WORDALIGN(p->bh_hdrlen + p->bh_caplen)
360 For the alignment mechanisms to work properly, the
363 must itself be word aligned.
367 will always return an aligned buffer.
369 A filter program is an array of instructions, with all branches forwardly
370 directed, terminated by a
373 Each instruction performs some action on the pseudo-machine state,
374 which consists of an accumulator, index register, scratch memory store,
375 and implicit program counter.
377 The following structure defines the instruction format:
389 field is used in different ways by different instructions,
394 fields are used as offsets
395 by the branch instructions.
396 The opcodes are encoded in a semi-hierarchical fashion.
397 There are eight classes of instructions:
407 Various other mode and
408 operator bits are or'd into the class to give the actual instructions.
409 The classes and modes are defined in
412 Below are the semantics for each defined
415 We use the convention that A is the accumulator, X is the index register,
416 P[] packet data, and M[] scratch memory store.
417 P[i:n] gives the data at byte offset
420 interpreted as a word (n=4),
421 unsigned halfword (n=2), or unsigned byte (n=1).
422 M[i] gives the i'th word in the scratch memory store, which is only
423 addressed in word units. The memory store is indexed from 0 to
430 are the corresponding fields in the
431 instruction definition.
433 refers to the length of the packet.
435 .Bl -tag -width BPF_STXx -compact
437 These instructions copy a value into the accumulator. The type of the
438 source operand is specified by an
440 and can be a constant
442 packet data at a fixed offset
444 packet data at a variable offset
448 or a word in the scratch memory store
454 the data size must be specified as a word
460 The semantics of all the recognized
464 .Bl -tag -width "BPF_LD+BPF_W+BPF_IND" -compact
465 .It Li BPF_LD+BPF_W+BPF_ABS
467 .It Li BPF_LD+BPF_H+BPF_ABS
469 .It Li BPF_LD+BPF_B+BPF_ABS
471 .It Li BPF_LD+BPF_W+BPF_IND
473 .It Li BPF_LD+BPF_H+BPF_IND
475 .It Li BPF_LD+BPF_B+BPF_IND
477 .It Li BPF_LD+BPF_W+BPF_LEN
479 .It Li BPF_LD+BPF_IMM
481 .It Li BPF_LD+BPF_MEM
486 These instructions load a value into the index register. Note that
487 the addressing modes are more restrictive than those of the accumulator loads,
490 a hack for efficiently loading the IP header length.
492 .Bl -tag -width "BPF_LDX+BPF_W+BPF_MEM" -compact
493 .It Li BPF_LDX+BPF_W+BPF_IMM
495 .It Li BPF_LDX+BPF_W+BPF_MEM
497 .It Li BPF_LDX+BPF_W+BPF_LEN
499 .It Li BPF_LDX+BPF_B+BPF_MSH
504 This instruction stores the accumulator into the scratch memory.
505 We do not need an addressing mode since there is only one possibility
508 .Bl -tag -width "BPF_ST" -compact
514 This instruction stores the index register in the scratch memory store.
516 .Bl -tag -width "BPF_STX" -compact
522 The alu instructions perform operations between the accumulator and
523 index register or constant, and store the result back in the accumulator.
524 For binary operations, a source mode is required
531 .Bl -tag -width "BPF_ALU+BPF_MUL+BPF_K" -compact
532 .It Li BPF_ALU+BPF_ADD+BPF_K
534 .It Li BPF_ALU+BPF_SUB+BPF_K
536 .It Li BPF_ALU+BPF_MUL+BPF_K
538 .It Li BPF_ALU+BPF_DIV+BPF_K
540 .It Li BPF_ALU+BPF_AND+BPF_K
542 .It Li BPF_ALU+BPF_OR+BPF_K
544 .It Li BPF_ALU+BPF_LSH+BPF_K
546 .It Li BPF_ALU+BPF_RSH+BPF_K
548 .It Li BPF_ALU+BPF_ADD+BPF_X
550 .It Li BPF_ALU+BPF_SUB+BPF_X
552 .It Li BPF_ALU+BPF_MUL+BPF_X
554 .It Li BPF_ALU+BPF_DIV+BPF_X
556 .It Li BPF_ALU+BPF_AND+BPF_X
558 .It Li BPF_ALU+BPF_OR+BPF_X
560 .It Li BPF_ALU+BPF_LSH+BPF_X
562 .It Li BPF_ALU+BPF_RSH+BPF_X
564 .It Li BPF_ALU+BPF_NEG
569 The jump instructions alter flow of control. Conditional jumps
570 compare the accumulator against a constant
572 or the index register
574 If the result is true (or non-zero),
575 the true branch is taken, otherwise the false branch is taken.
576 Jump offsets are encoded in 8 bits so the longest jump is 256 instructions.
577 However, the jump always
579 opcode uses the 32 bit
581 field as the offset, allowing arbitrarily distant destinations.
582 All conditionals use unsigned comparison conventions.
584 .Bl -tag -width "BPF_JMP+BPF_KSET+BPF_X" -compact
585 .It Li BPF_JMP+BPF_JA
587 .It Li BPF_JMP+BPF_JGT+BPF_K
588 pc += (A > k) ? jt : jf
589 .It Li BPF_JMP+BPF_JGE+BPF_K
590 pc += (A >= k) ? jt : jf
591 .It Li BPF_JMP+BPF_JEQ+BPF_K
592 pc += (A == k) ? jt : jf
593 .It Li BPF_JMP+BPF_JSET+BPF_K
594 pc += (A & k) ? jt : jf
595 .It Li BPF_JMP+BPF_JGT+BPF_X
596 pc += (A > X) ? jt : jf
597 .It Li BPF_JMP+BPF_JGE+BPF_X
598 pc += (A >= X) ? jt : jf
599 .It Li BPF_JMP+BPF_JEQ+BPF_X
600 pc += (A == X) ? jt : jf
601 .It Li BPF_JMP+BPF_JSET+BPF_X
602 pc += (A & X) ? jt : jf
606 The return instructions terminate the filter program and specify the amount
607 of packet to accept (i.e., they return the truncation amount). A return
608 value of zero indicates that the packet should be ignored.
609 The return value is either a constant
614 .Bl -tag -width "BPF_RET+BPF_K" -compact
622 The miscellaneous category was created for anything that doesn't
623 fit into the above classes, and for any new instructions that might need to
624 be added. Currently, these are the register transfer instructions
625 that copy the index register to the accumulator or vice versa.
627 .Bl -tag -width "BPF_MISC+BPF_TAX" -compact
628 .It Li BPF_MISC+BPF_TAX
630 .It Li BPF_MISC+BPF_TXA
636 interface provides the following macros to facilitate
638 .Fn BPF_STMT opcode operand
640 .Fn BPF_JUMP opcode operand true_offset false_offset .
643 The following filter is taken from the Reverse ARP Daemon. It accepts
644 only Reverse ARP requests.
646 struct bpf_insn insns[] = {
647 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
648 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_REVARP, 0, 3),
649 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
650 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, REVARP_REQUEST, 0, 1),
651 BPF_STMT(BPF_RET+BPF_K, sizeof(struct ether_arp) +
652 sizeof(struct ether_header)),
653 BPF_STMT(BPF_RET+BPF_K, 0),
657 This filter accepts only IP packets between host 128.3.112.15 and
660 struct bpf_insn insns[] = {
661 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
662 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 8),
663 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
664 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
665 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
666 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
667 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
668 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
669 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
670 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
671 BPF_STMT(BPF_RET+BPF_K, 0),
675 Finally, this filter returns only TCP finger packets. We must parse
676 the IP header to reach the TCP header. The
679 checks that the IP fragment offset is 0 so we are sure
680 that we have a TCP header.
682 struct bpf_insn insns[] = {
683 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
684 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 10),
685 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
686 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_TCP, 0, 8),
687 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
688 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
689 BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
690 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
691 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
692 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
693 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
694 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
695 BPF_STMT(BPF_RET+BPF_K, 0),
705 .%T "An efficient, extensible, and portable network monitor"
708 .Bl -tag -compact -width /dev/bpfXXX
709 .It Pa /dev/bpf Ns Sy n
710 the packet filter device
713 The read buffer must be of a fixed size (returned by the
717 A file that does not request promiscuous mode may receive promiscuously
718 received packets as a side effect of another file requesting this
719 mode on the same hardware interface. This could be fixed in the kernel
720 with additional processing overhead. However, we favor the model where
721 all files must assume that the interface is promiscuous, and if
722 so desired, must utilize a filter to reject foreign packets.
724 Data link protocols with variable length headers are not currently supported.
727 The Enet packet filter was created in 1980 by Mike Accetta and
728 Rick Rashid at Carnegie-Mellon University. Jeffrey Mogul, at
729 Stanford, ported the code to BSD and continued its development from
730 1983 on. Since then, it has evolved into the Ultrix Packet Filter
742 of Lawrence Berkeley Laboratory, implemented BPF in
743 Summer 1990. Much of the design is due to