1.\" -*- nroff -*- 2.\" 3.\" $NetBSD: bpf.4,v 1.39 2006/08/04 23:30:53 wiz 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 August 4, 2006 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 BIOSETIF 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 under 66BSD, can be set with 67.Dv BIOCSBLEN . 68Note that an individual packet larger than this size is necessarily 69truncated. 70.Pp 71The packet filter will support any link level protocol that has fixed length 72headers. 73Currently, only Ethernet, SLIP and PPP drivers have been 74modified to interact with 75.Nm . 76.Pp 77Since packet data is in network byte order, applications should use the 78.Xr byteorder 3 79macros to extract multi-byte values. 80.Pp 81A packet can be sent out on the network by writing to a 82.Nm 83file descriptor. 84The writes are unbuffered, meaning only one packet can be processed per write. 85Currently, only writes to Ethernets and SLIP links are supported. 86.Sh IOCTLS 87The 88.Xr ioctl 2 89command codes below are defined in 90.Aq Pa net/bpf.h . 91All 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] 97.Ed 98.Pp 99Additionally, 100.Dv BIOCGETIF 101and 102.Dv BIOCSETIF 103require 104.Pa \*[Lt]net/if.h\*[Gt] . 105.Pp 106The (third) argument to the 107.Xr ioctl 2 108should be a pointer to the type indicated. 109.Bl -tag -width indent -offset indent 110.It Dv "BIOCGBLEN (u_int)" 111Returns the required buffer length for reads on 112.Nm 113files. 114.It Dv "BIOCSBLEN (u_int)" 115Sets the buffer length for reads on 116.Nm 117files. 118The buffer must be set before the file is attached to an interface with 119.Dv BIOCSETIF . 120If the requested buffer size cannot be accommodated, the closest 121allowable size will be set and returned in the argument. 122A read call will result in 123.Er EINVAL 124if it is passed a buffer that is not this size. 125.It Dv BIOCGDLT (u_int) 126Returns the type of the data link layer underlying the attached interface. 127.Er EINVAL 128is returned if no interface has been specified. 129The device types, prefixed with 130.Dq DLT_ , 131are defined in 132.Aq Pa net/bpf.h . 133.It Dv BIOCGDLTLIST (struct bpf_dltlist) 134Returns an array of available type of the data link layer 135underlying the attached interface: 136.Bd -literal -offset indent 137struct bpf_dltlist { 138 u_int bfl_len; 139 u_int *bfl_list; 140}; 141.Ed 142.Pp 143The available type is returned to the array pointed to the 144.Va bfl_list 145field while its length in u_int is supplied to the 146.Va bfl_len 147field. 148.Er ENOMEM 149is returned if there is not enough buffer. 150The 151.Va bfl_len 152field is modified on return to indicate the actual length in u_int 153of the array returned. 154If 155.Va bfl_list 156is 157.Dv NULL , 158the 159.Va bfl_len 160field is returned to indicate the required length of an array in u_int. 161.It Dv BIOCSDLT (u_int) 162Change the type of the data link layer underlying the attached interface. 163.Er EINVAL 164is returned if no interface has been specified or the specified 165type is not available for the interface. 166.It Dv BIOCPROMISC 167Forces the interface into promiscuous mode. 168All packets, not just those destined for the local host, are processed. 169Since more than one file can be listening on a given interface, 170a listener that opened its interface non-promiscuously may receive 171packets promiscuously. 172This problem can be remedied with an appropriate filter. 173.Pp 174The interface remains in promiscuous mode until all files listening 175promiscuously are closed. 176.It Dv BIOCFLUSH 177Flushes the buffer of incoming packets, 178and resets the statistics that are returned by 179.Dv BIOCGSTATS . 180.It Dv BIOCGETIF (struct ifreq) 181Returns the name of the hardware interface that the file is listening on. 182The name is returned in the ifr_name field of 183.Fa ifr . 184All other fields are undefined. 185.It Dv BIOCSETIF (struct ifreq) 186Sets the hardware interface associate with the file. 187This command must be performed before any packets can be read. 188The device is indicated by name using the 189.Dv ifr_name 190field of the 191.Fa ifreq . 192Additionally, performs the actions of 193.Dv BIOCFLUSH . 194.It Dv BIOCSRTIMEOUT, BIOCGRTIMEOUT (struct timeval) 195Set or get the read timeout parameter. 196The 197.Fa timeval 198specifies the length of time to wait before timing 199out on a read request. 200This parameter is initialized to zero by 201.Xr open 2 , 202indicating no timeout. 203.It Dv BIOCGSTATS (struct bpf_stat) 204Returns the following structure of packet statistics: 205.Bd -literal -offset indent 206struct bpf_stat { 207 uint64_t bs_recv; 208 uint64_t bs_drop; 209 uint64_t bs_capt; 210 uint64_t bs_padding[13]; 211}; 212.Ed 213.Pp 214The fields are: 215.Bl -tag -width bs_recv -offset indent 216.It Va bs_recv 217the number of packets received by the descriptor since opened or reset 218(including any buffered since the last read call); 219.It Va bs_drop 220the number of packets which were accepted by the filter but dropped by the 221kernel because of buffer overflows 222(i.e., the application's reads aren't keeping up with the packet 223traffic); and 224.It Va bs_capt 225the number of packets accepted by the filter. 226.El 227.It Dv BIOCIMMEDIATE (u_int) 228Enable or disable 229.Dq immediate mode , 230based on the truth value of the argument. 231When immediate mode is enabled, reads return immediately upon packet 232reception. 233Otherwise, a read will block until either the kernel buffer 234becomes full or a timeout occurs. 235This is useful for programs like 236.Xr rarpd 8 , 237which must respond to messages in real time. 238The default for a new file is off. 239.It Dv BIOCSETF (struct bpf_program) 240Sets the filter program used by the kernel to discard uninteresting 241packets. 242An array of instructions and its length is passed in using the following structure: 243.Bd -literal -offset indent 244struct bpf_program { 245 u_int bf_len; 246 struct bpf_insn *bf_insns; 247}; 248.Ed 249.Pp 250The filter program is pointed to by the 251.Va bf_insns 252field while its length in units of 253.Sq struct bpf_insn 254is given by the 255.Va bf_len 256field. 257Also, the actions of 258.Dv BIOCFLUSH 259are performed. 260.Pp 261See section 262.Sy FILTER MACHINE 263for an explanation of the filter language. 264.It Dv BIOCVERSION (struct bpf_version) 265Returns the major and minor version numbers of the filter language currently 266recognized by the kernel. 267Before installing a filter, applications must check 268that the current version is compatible with the running kernel. 269Version numbers are compatible if the major numbers match and the 270application minor is less than or equal to the kernel minor. 271The kernel version number is returned in the following structure: 272.Bd -literal -offset indent 273struct bpf_version { 274 u_short bv_major; 275 u_short bv_minor; 276}; 277.Ed 278.Pp 279The current version numbers are given by 280.Dv BPF_MAJOR_VERSION 281and 282.Dv BPF_MINOR_VERSION 283from 284.Aq Pa net/bpf.h . 285An incompatible filter 286may result in undefined behavior (most likely, an error returned by 287.Xr ioctl 2 288or haphazard packet matching). 289.It Dv BIOCGHDRCMPLT BIOCSHDRCMPLT (u_int) 290Enable/disable or get the 291.Dq header complete 292flag status. 293If enabled, packets written to the bpf file descriptor will not have 294network layer headers rewritten in the interface output routine. 295By default, the flag is disabled (value is 0). 296.It Dv BIOCGSEESENT BIOCSSEESENT (u_int) 297Enable/disable or get the 298.Dq see sent 299flag status. 300If enabled, packets sent will be passed to the filter. 301By default, the flag is enabled (value is 1). 302.El 303.Sh STANDARD IOCTLS 304.Nm 305now supports several standard 306.Xr ioctl 2 Ns 's 307which allow the user to do async and/or non-blocking I/O to an open 308.I bpf 309file descriptor. 310.Bl -tag -width indent -offset indent 311.It Dv FIONREAD (int) 312Returns the number of bytes that are immediately available for reading. 313.It Dv SIOCGIFADDR (struct ifreq) 314Returns the address associated with the interface. 315.It Dv FIONBIO (int) 316Set or clear non-blocking I/O. 317If arg is non-zero, then doing a 318.Xr read 2 319when no data is available will return -1 and 320.Va errno 321will be set to 322.Er EAGAIN . 323If arg is zero, non-blocking I/O is disabled. 324Note: setting this 325overrides the timeout set by 326.Dv BIOCSRTIMEOUT . 327.It Dv FIOASYNC (int) 328Enable or disable async I/O. 329When enabled (arg is non-zero), the process or process group specified by 330.Dv FIOSETOWN 331will start receiving SIGIO's when packets 332arrive. 333Note that you must do an 334.Dv FIOSETOWN 335in order for this to take affect, as 336the system will not default this for you. 337The signal may be changed via 338.Dv BIOCSRSIG . 339.It Dv FIOSETOWN FIOGETOWN (int) 340Set or get the process or process group (if negative) that should receive SIGIO 341when packets are available. 342The signal may be changed using 343.Dv BIOCSRSIG 344(see above). 345.El 346.Sh BPF HEADER 347The following structure is prepended to each packet returned by 348.Xr read 2 : 349.Bd -literal -offset indent 350struct bpf_hdr { 351 struct timeval bh_tstamp; 352 uint32_t bh_caplen; 353 uint32_t bh_datalen; 354 uint16_t bh_hdrlen; 355}; 356.Ed 357.Pp 358The fields, whose values are stored in host order, and are: 359.Bl -tag -width bh_datalen -offset indent 360.It Va bh_tstamp 361The time at which the packet was processed by the packet filter. 362.It Va bh_caplen 363The length of the captured portion of the packet. 364This is the minimum of 365the truncation amount specified by the filter and the length of the packet. 366.It Va bh_datalen 367The length of the packet off the wire. 368This value is independent of the truncation amount specified by the filter. 369.It Va bh_hdrlen 370The length of the BPF header, which may not be equal to 371.Em sizeof(struct bpf_hdr) . 372.El 373.Pp 374The 375.Va bh_hdrlen 376field exists to account for 377padding between the header and the link level protocol. 378The purpose here is to guarantee proper alignment of the packet 379data structures, which is required on alignment sensitive 380architectures and improves performance on many other architectures. 381The packet filter ensures that the 382.Va bpf_hdr 383and the 384.Em network layer 385header will be word aligned. 386Suitable precautions must be taken when accessing the link layer 387protocol fields on alignment restricted machines. 388(This isn't a problem on an Ethernet, since 389the type field is a short falling on an even offset, 390and the addresses are probably accessed in a bytewise fashion). 391.Pp 392Additionally, individual packets are padded so that each starts 393on a word boundary. 394This requires that an application 395has some knowledge of how to get from packet to packet. 396The macro 397.Dv BPF_WORDALIGN 398is defined in 399.Aq Pa net/bpf.h 400to facilitate this process. 401It rounds up its argument 402to the nearest word aligned value (where a word is 403.Dv BPF_ALIGNMENT 404bytes wide). 405.Pp 406For example, if 407.Sq Va p 408points to the start of a packet, this expression 409will advance it to the next packet: 410.Pp 411.Dl p = (char *)p + BPF_WORDALIGN(p-\*[Gt]bh_hdrlen + p-\*[Gt]bh_caplen) 412.Pp 413For the alignment mechanisms to work properly, the 414buffer passed to 415.Xr read 2 416must itself be word aligned. 417.Xr malloc 3 418will always return an aligned buffer. 419.Sh FILTER MACHINE 420A filter program is an array of instructions, with all branches forwardly 421directed, terminated by a 422.Sy return 423instruction. 424Each instruction performs some action on the pseudo-machine state, 425which consists of an accumulator, index register, scratch memory store, 426and implicit program counter. 427.Pp 428The following structure defines the instruction format: 429.Bd -literal -offset indent 430struct bpf_insn { 431 uint16_t code; 432 u_char jt; 433 u_char jf; 434 int32_t k; 435}; 436.Ed 437.Pp 438The 439.Va k 440field is used in different ways by different instructions, 441and the 442.Va jt 443and 444.Va jf 445fields are used as offsets 446by the branch instructions. 447The opcodes are encoded in a semi-hierarchical fashion. 448There are eight classes of instructions: BPF_LD, BPF_LDX, BPF_ST, BPF_STX, 449BPF_ALU, BPF_JMP, BPF_RET, and BPF_MISC. 450Various other mode and 451operator bits are or'd into the class to give the actual instructions. 452The classes and modes are defined in 453.Aq Pa net/bpf.h . 454.Pp 455Below are the semantics for each defined BPF instruction. 456We use the convention that A is the accumulator, X is the index register, 457P[] packet data, and M[] scratch memory store. 458P[i:n] gives the data at byte offset 459.Dq i 460in the packet, 461interpreted as a word (n=4), 462unsigned halfword (n=2), or unsigned byte (n=1). 463M[i] gives the i'th word in the scratch memory store, which is only 464addressed in word units. 465The memory store is indexed from 0 to BPF_MEMWORDS-1. 466.Va k , 467.Va jt , 468and 469.Va jf 470are the corresponding fields in the 471instruction definition. 472.Dq len 473refers to the length of the packet. 474.Bl -tag -width indent -offset indent 475.It Sy BPF_LD 476These instructions copy a value into the accumulator. 477The type of the source operand is specified by an 478.Dq addressing mode 479and can be a constant 480.Sy ( BBPF_IMM ) , 481packet data at a fixed offset 482.Sy ( BPF_ABS ) , 483packet data at a variable offset 484.Sy ( BPF_IND ) , 485the packet length 486.Sy ( BPF_LEN ) , 487or a word in the scratch memory store 488.Sy ( BPF_MEM ) . 489For 490.Sy BPF_IND 491and 492.Sy BPF_ABS , 493the data size must be specified as a word 494.Sy ( BPF_W ) , 495halfword 496.Sy ( BPF_H ) , 497or byte 498.Sy ( BPF_B ) . 499The 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] 510.El 511.It Sy BPF_LDX 512These instructions load a value into the index register. 513Note that the addressing modes are more restricted than those of 514the accumulator loads, but they include 515.Sy BPF_MSH , 516a 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) 522.El 523.It Sy BPF_ST 524This instruction stores the accumulator into the scratch memory. 525We do not need an addressing mode since there is only one possibility 526for the destination. 527.Bl -column "BPF_ST" "M[k] \*[Lt]- A" -offset indent 528.It Sy BPF_ST Ta M[k] \*[Lt]- A 529.El 530.It Sy BPF_STX 531This 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 534.El 535.It Sy BPF_ALU 536The alu instructions perform operations between the accumulator and 537index register or constant, and store the result back in the accumulator. 538For binary operations, a source mode is required 539.Sy ( BPF_K 540or 541.Sy BPF_X ) . 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 560.El 561.It Sy BPF_JMP 562The jump instructions alter flow of control. 563Conditional jumps compare the accumulator against a constant 564.Sy ( BPF_K ) 565or the index register 566.Sy ( BPF_X ) . 567If the result is true (or non-zero), 568the true branch is taken, otherwise the false branch is taken. 569Jump offsets are encoded in 8 bits so the longest jump is 256 instructions. 570However, the jump always 571.Sy ( BPF_JA ) 572opcode uses the 32 bit 573.Va k 574field as the offset, allowing arbitrarily distant destinations. 575All 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" 586.El 587.It Sy BPF_RET 588The return instructions terminate the filter program and specify the amount 589of packet to accept (i.e., they return the truncation amount). 590A return value of zero indicates that the packet should be ignored. 591The return value is either a constant 592.Sy ( BPF_K ) 593or the accumulator 594.Sy ( BPF_A ) . 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 598.El 599.It Sy BPF_MISC 600The miscellaneous category was created for anything that doesn't 601fit into the above classes, and for any new instructions that might need to 602be added. 603Currently, these are the register transfer instructions 604that 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 608.El 609.El 610.Pp 611The BPF interface provides the following macros to facilitate 612array initializers: 613.Bd -unfilled -offset indent 614.Sy BPF_STMT No (opcode, operand) 615.Sy BPF_JUMP No (opcode, operand, true_offset, false_offset) 616.Ed 617.Sh SYSCTLS 618The following sysctls are available when 619.Nm 620is enabled: 621.Pp 622.Bl -tag -width "XnetXbpfXmaxbufsizeXX" 623.It Li net.bpf.maxbufsize 624Sets the maximum buffer size available for 625.Nm 626peers. 627.It Li net.bpf.stats 628Shows 629.Nm 630statistics. 631They can be retrieved with the 632.Xr netstat 1 633utility. 634.It Li net.bpf.peers 635Shows the current 636.Nm 637peers. 638This is only available to the super user and can also be retrieved with the 639.Xr netstat 1 640utility. 641.El 642.Sh FILES 643.Pa /dev/bpf 644.Sh EXAMPLES 645The following filter is taken from the Reverse ARP Daemon. 646It accepts only Reverse ARP requests. 647.Bd -literal -offset indent 648struct 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), 656}; 657.Ed 658.Pp 659This filter accepts only IP packets between host 128.3.112.15 and 660128.3.112.35. 661.Bd -literal -offset indent 662struct 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), 674}; 675.Ed 676.Pp 677Finally, this filter returns only TCP finger packets. 678We must parse the IP header to reach the TCP header. 679The 680.Sy BPF_JSET 681instruction checks that the IP fragment offset is 0 so we are sure 682that we have a TCP header. 683.Bd -literal -offset indent 684struct 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), 698}; 699.Ed 700.Sh SEE ALSO 701.Xr ioctl 2 , 702.Xr read 2 , 703.Xr select 2 , 704.Xr signal 3 , 705.Xr tcpdump 8 706.Rs 707.%T "The BSD Packet Filter: A New Architecture for User-level Packet Capture" 708.%A S. McCanne 709.%A V. Jacobson 710.%J Proceedings of the 1993 Winter USENIX 711.%C Technical Conference, San Diego, CA 712.Re 713.Sh HISTORY 714The Enet packet filter was created in 1980 by Mike Accetta and 715Rick Rashid at Carnegie-Mellon University. 716Jeffrey Mogul, at Stanford, ported the code to BSD and continued 717its development from 1983 on. 718Since then, it has evolved into the ULTRIX Packet Filter 719at DEC, a STREAMS NIT module under SunOS 4.1, and BPF. 720.Sh AUTHORS 721Steven McCanne, of Lawrence Berkeley Laboratory, implemented BPF in 722Summer 1990. 723The design was in collaboration with Van Jacobson, 724also of Lawrence Berkeley Laboratory. 725.Sh BUGS 726The read buffer must be of a fixed size (returned by the 727.Dv BIOCGBLEN 728ioctl). 729.Pp 730A file that does not request promiscuous mode may receive promiscuously 731received packets as a side effect of another file requesting this 732mode on the same hardware interface. 733This could be fixed in the kernel with additional processing overhead. 734However, we favor the model where 735all files must assume that the interface is promiscuous, and if 736so desired, must use a filter to reject foreign packets. 737.Pp 738Data link protocols with variable length headers are not currently supported. 739.Pp 740Under SunOS, if a BPF application reads more than 2^31 bytes of 741data, read will fail in 742.Er EINVAL . 743You can either fix the bug in SunOS, 744or lseek to 0 when read fails for this reason. 745.Pp 746.Dq Immediate mode 747and the 748.Dq read timeout 749are misguided features. 750This functionality can be emulated with non-blocking mode and 751.Xr select 2 . 752