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