183676de9SDavid Malone.\" Copyright (c) 2004 FreeBSD Inc. 283676de9SDavid Malone.\" All rights reserved. 383676de9SDavid Malone.\" 483676de9SDavid Malone.\" Redistribution and use in source and binary forms, with or without 583676de9SDavid Malone.\" modification, are permitted provided that the following conditions 683676de9SDavid Malone.\" are met: 783676de9SDavid Malone.\" 1. Redistributions of source code must retain the above copyright 883676de9SDavid Malone.\" notice, this list of conditions and the following disclaimer. 983676de9SDavid Malone.\" 2. Redistributions in binary form must reproduce the above copyright 1083676de9SDavid Malone.\" notice, this list of conditions and the following disclaimer in the 1183676de9SDavid Malone.\" documentation and/or other materials provided with the distribution. 1283676de9SDavid Malone.\" 1383676de9SDavid Malone.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1483676de9SDavid Malone.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1583676de9SDavid Malone.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1683676de9SDavid Malone.\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE 1783676de9SDavid Malone.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1883676de9SDavid Malone.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1983676de9SDavid Malone.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2083676de9SDavid Malone.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2183676de9SDavid Malone.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2283676de9SDavid Malone.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2383676de9SDavid Malone.\" SUCH DAMAGE. 2483676de9SDavid Malone.\" 256c74ff0eSAlexander V. Chernikov.Dd May 11, 2012 2683676de9SDavid Malone.Dt BPF 9 2783676de9SDavid Malone.Os 2883676de9SDavid Malone.\" 2983676de9SDavid Malone.Sh NAME 3083676de9SDavid Malone.Nm bpf 3183676de9SDavid Malone.Nd "Berkeley Packet Filter" 3283676de9SDavid Malone.\" 3383676de9SDavid Malone.Sh SYNOPSIS 3483676de9SDavid Malone.In net/bpf.h 3583676de9SDavid Malone.\" 3683676de9SDavid Malone.Ft void 3783676de9SDavid Malone.Fn bpfattach "struct ifnet *ifp" "u_int dlt" "u_int hdrlen" 3883676de9SDavid Malone.Ft void 39457432e8SRuslan Ermilov.Fo bpfattach2 40457432e8SRuslan Ermilov.Fa "struct ifnet *ifp" "u_int dlt" "u_int hdrlen" "struct bpf_if **driverp" 41457432e8SRuslan Ermilov.Fc 4283676de9SDavid Malone.Ft void 4383676de9SDavid Malone.Fn bpfdetach "struct ifnet *ifp" 4483676de9SDavid Malone.Ft void 4583676de9SDavid Malone.Fn bpf_tap "struct ifnet *ifp" "u_char *pkt" "u_int *pktlen" 4683676de9SDavid Malone.Ft void 4783676de9SDavid Malone.Fn bpf_mtap "struct ifnet *ifp" "struct mbuf *m" 4883676de9SDavid Malone.Ft void 494291a598SGleb Smirnoff.Fn bpf_mtap2 "struct bpf_if *bp" "void *data" "u_int dlen" "struct mbuf *m" 5083676de9SDavid Malone.Ft u_int 51457432e8SRuslan Ermilov.Fo bpf_filter 52ea0e76a2SRuslan Ermilov.Fa "const struct bpf_insn *pc " "u_char *pkt" "u_int wirelen" "u_int buflen" 53457432e8SRuslan Ermilov.Fc 5483676de9SDavid Malone.Ft int 5583676de9SDavid Malone.Fn bpf_validate "const struct bpf_insn *fcode" "int flen" 5683676de9SDavid Malone.\" 5783676de9SDavid Malone.Sh DESCRIPTION 5883676de9SDavid MaloneThe Berkeley Packet Filter provides a raw interface, 5983676de9SDavid Malonethat is protocol independent, 6083676de9SDavid Maloneto data link layers. 6183676de9SDavid MaloneIt allows all packets on the network, 6283676de9SDavid Maloneeven those destined for other hosts, 6383676de9SDavid Maloneto be passed from a network interface to user programs. 6483676de9SDavid MaloneEach program may specify a filter, 65457432e8SRuslan Ermilovin the form of a 66457432e8SRuslan Ermilov.Nm 67457432e8SRuslan Ermilovfilter machine program. 68457432e8SRuslan ErmilovThe 6983676de9SDavid Malone.Xr bpf 4 70571dba6eSHiten Pandyamanual page 7183676de9SDavid Malonedescribes the interface used by user programs. 72571dba6eSHiten PandyaThis manual page describes the functions used by interfaces to pass packets to 7383676de9SDavid Malone.Nm 7483676de9SDavid Maloneand the functions for testing and running 7583676de9SDavid Malone.Nm 7683676de9SDavid Malonefilter machine programs. 7783676de9SDavid Malone.Pp 78457432e8SRuslan ErmilovThe 7983676de9SDavid Malone.Fn bpfattach 80457432e8SRuslan Ermilovfunction 8183676de9SDavid Maloneattaches a network interface to 8283676de9SDavid Malone.Nm . 83457432e8SRuslan ErmilovThe 84457432e8SRuslan Ermilov.Fa ifp 85457432e8SRuslan Ermilovargument 8683676de9SDavid Maloneis a pointer to the structure that defines the interface to be 8783676de9SDavid Maloneattached to an interface. 88457432e8SRuslan ErmilovThe 89457432e8SRuslan Ermilov.Fa dlt 90457432e8SRuslan Ermilovargument 9183676de9SDavid Maloneis the data link-layer type: 92457432e8SRuslan Ermilov.Dv DLT_NULL 93457432e8SRuslan Ermilov(no link-layer encapsulation), 94457432e8SRuslan Ermilov.Dv DLT_EN10MB 95457432e8SRuslan Ermilov(Ethernet), 96457432e8SRuslan Ermilov.Dv DLT_IEEE802_11 97457432e8SRuslan Ermilov(802.11 wireless networks), 9883676de9SDavid Maloneetc. 9983676de9SDavid MaloneThe rest of the link layer types can be found in 100457432e8SRuslan Ermilov.In net/bpf.h . 101457432e8SRuslan ErmilovThe 102457432e8SRuslan Ermilov.Fa hdrlen 103457432e8SRuslan Ermilovargument 10483676de9SDavid Maloneis the fixed size of the link header; 10583676de9SDavid Malonevariable length headers are not yet supported. 10683676de9SDavid MaloneThe 10783676de9SDavid Malone.Nm 10883676de9SDavid Malonesystem will hold a pointer to 109457432e8SRuslan Ermilov.Fa ifp->if_bpf . 110457432e8SRuslan ErmilovThis variable will set to a 111457432e8SRuslan Ermilov.Pf non- Dv NULL 112457432e8SRuslan Ermilovvalue when 11383676de9SDavid Malone.Nm 11483676de9SDavid Malonerequires packets from this interface to be tapped using the functions below. 11583676de9SDavid Malone.Pp 116457432e8SRuslan ErmilovThe 11783676de9SDavid Malone.Fn bpfattach2 118457432e8SRuslan Ermilovfunction 119457432e8SRuslan Ermilovallows multiple 120457432e8SRuslan Ermilov.Nm 121457432e8SRuslan Ermilovinstances to be attached to a single interface, 12283676de9SDavid Maloneby registering an explicit 123457432e8SRuslan Ermilov.Fa if_bpf 12483676de9SDavid Malonerather than using 125457432e8SRuslan Ermilov.Fa ifp->if_bpf . 12683676de9SDavid MaloneIt is then possible to run 12783676de9SDavid Malone.Xr tcpdump 1 12883676de9SDavid Maloneon the interface for any data link-layer types attached. 12983676de9SDavid Malone.Pp 130457432e8SRuslan ErmilovThe 13183676de9SDavid Malone.Fn bpfdetach 132457432e8SRuslan Ermilovfunction detaches a 13383676de9SDavid Malone.Nm 13483676de9SDavid Maloneinstance from an interface, 13583676de9SDavid Malonespecified by 136457432e8SRuslan Ermilov.Fa ifp . 137457432e8SRuslan ErmilovThe 13883676de9SDavid Malone.Fn bpfdetach 139457432e8SRuslan Ermilovfunction 14083676de9SDavid Maloneshould be called once for each 141457432e8SRuslan Ermilov.Nm 14283676de9SDavid Maloneinstance attached. 14383676de9SDavid Malone.Pp 144457432e8SRuslan ErmilovThe 14583676de9SDavid Malone.Fn bpf_tap 146457432e8SRuslan Ermilovfunction 14783676de9SDavid Maloneis used by an interface to pass the packet to 14883676de9SDavid Malone.Nm . 14983676de9SDavid MaloneThe packet data (including link-header), 15083676de9SDavid Malonepointed to by 151457432e8SRuslan Ermilov.Fa pkt , 15283676de9SDavid Maloneis of length 153457432e8SRuslan Ermilov.Fa pktlen , 15483676de9SDavid Malonewhich must be a contiguous buffer. 155457432e8SRuslan ErmilovThe 156457432e8SRuslan Ermilov.Fa ifp 157457432e8SRuslan Ermilovargument 15883676de9SDavid Maloneis a pointer to the structure that defines the interface to be tapped. 15983676de9SDavid MaloneThe packet is parsed by each processes filter, 16083676de9SDavid Maloneand if accepted, 16183676de9SDavid Maloneit is buffered for the process to read. 16283676de9SDavid Malone.Pp 163457432e8SRuslan ErmilovThe 16483676de9SDavid Malone.Fn bpf_mtap 165457432e8SRuslan Ermilovfunction is like 16683676de9SDavid Malone.Fn bpf_tap 167457432e8SRuslan Ermilovexcept that it is used to tap packets that are in an 168457432e8SRuslan Ermilov.Vt mbuf 169457432e8SRuslan Ermilovchain, 170457432e8SRuslan Ermilov.Fa m . 171457432e8SRuslan ErmilovThe 172457432e8SRuslan Ermilov.Fa ifp 173457432e8SRuslan Ermilovargument 17483676de9SDavid Maloneis a pointer to the structure that defines the interface to be tapped. 17583676de9SDavid MaloneLike 17683676de9SDavid Malone.Fn bpf_tap , 17783676de9SDavid Malone.Fn bpf_mtap 17883676de9SDavid Malonerequires a link-header for whatever data link layer type is specified. 17983676de9SDavid MaloneNote that 18083676de9SDavid Malone.Nm 181457432e8SRuslan Ermilovonly reads from the 182457432e8SRuslan Ermilov.Vt mbuf 183457432e8SRuslan Ermilovchain, 18483676de9SDavid Maloneit does not free it or keep a pointer to it. 185457432e8SRuslan ErmilovThis means that an 186457432e8SRuslan Ermilov.Vt mbuf 187457432e8SRuslan Ermilovcontaining the link-header 18883676de9SDavid Malonecan be prepended to the chain if necessary. 18983676de9SDavid MaloneA cleaner interface to achieve this is provided by 19083676de9SDavid Malone.Fn bpf_mtap2 . 19183676de9SDavid Malone.Pp 192457432e8SRuslan ErmilovThe 19383676de9SDavid Malone.Fn bpf_mtap2 194457432e8SRuslan Ermilovfunction 19583676de9SDavid Maloneallows the user to pass a link-header 196457432e8SRuslan Ermilov.Fa data , 19783676de9SDavid Maloneof length 198457432e8SRuslan Ermilov.Fa dlen , 199457432e8SRuslan Ermilovindependent of the 200457432e8SRuslan Ermilov.Vt mbuf 201457432e8SRuslan Ermilov.Fa m , 20283676de9SDavid Malonecontaining the packet. 20383676de9SDavid MaloneThis simplifies the passing of some link-headers. 20483676de9SDavid Malone.Pp 205457432e8SRuslan ErmilovThe 20683676de9SDavid Malone.Fn bpf_filter 207457432e8SRuslan Ermilovfunction 20883676de9SDavid Maloneexecutes the filter program starting at 209457432e8SRuslan Ermilov.Fa pc 21083676de9SDavid Maloneon the packet 211457432e8SRuslan Ermilov.Fa pkt . 212457432e8SRuslan ErmilovThe 213457432e8SRuslan Ermilov.Fa wirelen 214457432e8SRuslan Ermilovargument 21583676de9SDavid Maloneis the length of the original packet and 216457432e8SRuslan Ermilov.Fa buflen 21783676de9SDavid Maloneis the amount of data present. 218ea0e76a2SRuslan ErmilovThe 219ea0e76a2SRuslan Ermilov.Fa buflen 220ea0e76a2SRuslan Ermilovvalue of 0 is special; it indicates that the 221ea0e76a2SRuslan Ermilov.Fa pkt 222ea0e76a2SRuslan Ermilovis actually a pointer to an mbuf chain 223ea0e76a2SRuslan Ermilov.Pq Vt "struct mbuf *" . 22483676de9SDavid Malone.Pp 225457432e8SRuslan ErmilovThe 22683676de9SDavid Malone.Fn bpf_validate 227457432e8SRuslan Ermilovfunction 22883676de9SDavid Malonechecks that the filter code 229457432e8SRuslan Ermilov.Fa fcode , 23083676de9SDavid Maloneof length 231457432e8SRuslan Ermilov.Fa flen , 23283676de9SDavid Maloneis valid. 23383676de9SDavid Malone.\" 23483676de9SDavid Malone.Sh RETURN VALUES 235457432e8SRuslan ErmilovThe 23683676de9SDavid Malone.Fn bpf_filter 237457432e8SRuslan Ermilovfunction returns \-1 238457432e8SRuslan Ermilov(cast to an unsigned integer) 23983676de9SDavid Maloneif there is no filter. 24083676de9SDavid MaloneOtherwise, it returns the result of the filter program. 24183676de9SDavid Malone.Pp 242457432e8SRuslan ErmilovThe 24383676de9SDavid Malone.Fn bpf_validate 244457432e8SRuslan Ermilovfunction 24583676de9SDavid Malonereturns 0 when the program is not a valid filter program. 24683676de9SDavid Malone.\" 2476c74ff0eSAlexander V. Chernikov.Sh EVENT HANDLERS 2486c74ff0eSAlexander V. Chernikov.Nm 2496c74ff0eSAlexander V. Chernikovinvokes 2506c74ff0eSAlexander V. Chernikov.Fa bpf_track 2516c74ff0eSAlexander V. Chernikov.Xr EVENTHANDLER 9 2526c74ff0eSAlexander V. Chernikovevent each time listener attaches to or detaches from an interface. 2536c74ff0eSAlexander V. ChernikovPointer to 2546c74ff0eSAlexander V. Chernikov.Pq Vt "struct ifnet *" 2556c74ff0eSAlexander V. Chernikovis passed as the first argument, interface 2566c74ff0eSAlexander V. Chernikov.Fa dlt 257*7851d429SEdward Tomasz Napieralafollows. 258*7851d429SEdward Tomasz NapieralaLast argument indicates listener is attached (1) or detached (0). 2596c74ff0eSAlexander V. ChernikovNote that handler is invoked with 2606c74ff0eSAlexander V. Chernikov.Nm 2616c74ff0eSAlexander V. Chernikovglobal lock held, which implies restriction on sleeping and calling 2626c74ff0eSAlexander V. Chernikov.Nm 2636c74ff0eSAlexander V. Chernikovsubsystem inside 2646c74ff0eSAlexander V. Chernikov.Xr EVENTHANDLER 9 2656c74ff0eSAlexander V. Chernikovdispatcher. 2666c74ff0eSAlexander V. ChernikovNote that handler is not called for write-only listeners. 2676c74ff0eSAlexander V. Chernikov.\" 26883676de9SDavid Malone.Sh SEE ALSO 26983676de9SDavid Malone.Xr tcpdump 1 , 2706c74ff0eSAlexander V. Chernikov.Xr bpf 4 , 2716c74ff0eSAlexander V. Chernikov.Xr EVENTHANDLER 9 27283676de9SDavid Malone.\" 27383676de9SDavid Malone.Sh HISTORY 27483676de9SDavid MaloneThe Enet packet filter was created in 1980 by Mike Accetta and 27583676de9SDavid MaloneRick Rashid at Carnegie-Mellon University. 27683676de9SDavid MaloneJeffrey Mogul, 27783676de9SDavid Maloneat Stanford, 27883676de9SDavid Maloneported the code to 27983676de9SDavid Malone.Bx 28083676de9SDavid Maloneand continued its development from 1983 on. 28183676de9SDavid MaloneSince then, 28283676de9SDavid Maloneit has evolved into the Ultrix Packet Filter at 28383676de9SDavid Malone.Tn DEC , 28483676de9SDavid Malonea 28583676de9SDavid Malone.Tn STREAMS 28683676de9SDavid Malone.Tn NIT 28783676de9SDavid Malonemodule under 288457432e8SRuslan Ermilov.Tn SunOS 289457432e8SRuslan Ermilov4.1, and 29083676de9SDavid Malone.Tn BPF . 29183676de9SDavid Malone.\" 29283676de9SDavid Malone.Sh AUTHORS 29383676de9SDavid Malone.An -nosplit 29483676de9SDavid Malone.An Steven McCanne , 29583676de9SDavid Maloneof Lawrence Berkeley Laboratory, implemented BPF in Summer 1990. 29683676de9SDavid MaloneMuch of the design is due to 29783676de9SDavid Malone.An Van Jacobson . 298457432e8SRuslan ErmilovThis manpage was written by 29983676de9SDavid Malone.An Orla McGann . 300