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