192a3e552SArchie Cobbs.\" Copyright (c) 1999 Whistle Communications, Inc. 292a3e552SArchie Cobbs.\" All rights reserved. 392a3e552SArchie Cobbs.\" 492a3e552SArchie Cobbs.\" Subject to the following obligations and disclaimer of warranty, use and 592a3e552SArchie Cobbs.\" redistribution of this software, in source or object code forms, with or 692a3e552SArchie Cobbs.\" without modifications are expressly permitted by Whistle Communications; 792a3e552SArchie Cobbs.\" provided, however, that: 892a3e552SArchie Cobbs.\" 1. Any and all reproductions of the source or object code must include the 992a3e552SArchie Cobbs.\" copyright notice above and the following disclaimer of warranties; and 1092a3e552SArchie Cobbs.\" 2. No rights are granted, in any manner or form, to use Whistle 1192a3e552SArchie Cobbs.\" Communications, Inc. trademarks, including the mark "WHISTLE 1292a3e552SArchie Cobbs.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1392a3e552SArchie Cobbs.\" such appears in the above copyright notice or in the software. 1492a3e552SArchie Cobbs.\" 1592a3e552SArchie Cobbs.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 1692a3e552SArchie Cobbs.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 1792a3e552SArchie Cobbs.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 1892a3e552SArchie Cobbs.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 1992a3e552SArchie Cobbs.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2092a3e552SArchie Cobbs.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2192a3e552SArchie Cobbs.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2292a3e552SArchie Cobbs.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2392a3e552SArchie Cobbs.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2492a3e552SArchie Cobbs.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2592a3e552SArchie Cobbs.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 2692a3e552SArchie Cobbs.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 2792a3e552SArchie Cobbs.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 2892a3e552SArchie Cobbs.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2992a3e552SArchie Cobbs.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3092a3e552SArchie Cobbs.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3192a3e552SArchie Cobbs.\" OF SUCH DAMAGE. 3292a3e552SArchie Cobbs.\" 33eddc45e7SJeroen Ruigrok van der Werven.\" Author: Archie Cobbs <archie@FreeBSD.org> 3492a3e552SArchie Cobbs.\" 3592a3e552SArchie Cobbs.\" $FreeBSD$ 3692a3e552SArchie Cobbs.\" $Whistle: ng_bpf.8,v 1.2 1999/12/03 01:57:12 archie Exp $ 3792a3e552SArchie Cobbs.\" 38*c60bda17SJoel Dahl.Dd November 13, 2012 39bec35b9aSArchie Cobbs.Dt NG_BPF 4 403d45e180SRuslan Ermilov.Os 4192a3e552SArchie Cobbs.Sh NAME 4292a3e552SArchie Cobbs.Nm ng_bpf 4392a3e552SArchie Cobbs.Nd Berkeley packet filter netgraph node type 4492a3e552SArchie Cobbs.Sh SYNOPSIS 45fbad9e2dSRuslan Ermilov.In sys/types.h 4632eef9aeSRuslan Ermilov.In net/bpf.h 47fbad9e2dSRuslan Ermilov.In netgraph.h 4832eef9aeSRuslan Ermilov.In netgraph/ng_bpf.h 4992a3e552SArchie Cobbs.Sh DESCRIPTION 5092a3e552SArchie CobbsThe 5192a3e552SArchie Cobbs.Nm bpf 5292a3e552SArchie Cobbsnode type allows Berkeley Packet Filter (see 5351291597SJulian Elischer.Xr bpf 4 ) 5492a3e552SArchie Cobbsfilters to be applied to data travelling through a Netgraph network. 5592a3e552SArchie CobbsEach node allows an arbitrary number of connections to arbitrarily 565203edcdSRuslan Ermilovnamed hooks. 575203edcdSRuslan ErmilovWith each hook is associated a 583136363fSRuslan Ermilov.Xr bpf 4 5992a3e552SArchie Cobbsfilter program which is applied to incoming data only, a destination hook 6092a3e552SArchie Cobbsfor matching packets, a destination hook for non-matching packets, 6192a3e552SArchie Cobbsand various statistics counters. 6292a3e552SArchie Cobbs.Pp 6392a3e552SArchie CobbsA 6451291597SJulian Elischer.Xr bpf 4 6592a3e552SArchie Cobbsprogram returns an unsigned integer, which is normally interpreted as 665203edcdSRuslan Ermilovthe length of the prefix of the packet to return. 675203edcdSRuslan ErmilovIn the context of this 6892a3e552SArchie Cobbsnode type, returning zero is considered a non-match, in which case the 6992a3e552SArchie Cobbsentire packet is delivered out the non-match destination hook. 7092a3e552SArchie CobbsReturning a value greater than zero causes the packet to be truncated 7192a3e552SArchie Cobbsto that length and delivered out the match destination hook. 7292a3e552SArchie CobbsEither or both destination hooks may be the empty string, or may 7392a3e552SArchie Cobbsnot exist, in which case the packet is dropped. 7492a3e552SArchie Cobbs.Pp 7592a3e552SArchie CobbsNew hooks are initially configured to drop all packets. 76e653127eSArchie CobbsA new filter program may be installed using the 77e653127eSArchie Cobbs.Dv NGM_BPF_SET_PROGRAM 7892a3e552SArchie Cobbscontrol message. 7992a3e552SArchie Cobbs.Sh HOOKS 8092a3e552SArchie CobbsThis node type supports any number of hooks having arbitrary names. 8192a3e552SArchie Cobbs.Sh CONTROL MESSAGES 8292a3e552SArchie CobbsThis node type supports the generic control messages, plus the following: 8392a3e552SArchie Cobbs.Bl -tag -width foo 84*c60bda17SJoel Dahl.It Dv NGM_BPF_SET_PROGRAM Pq Ic setprogram 8592a3e552SArchie CobbsThis command sets the filter program that will be applied to incoming 865203edcdSRuslan Ermilovdata on a hook. 875203edcdSRuslan ErmilovThe following structure must be supplied as an argument: 8892a3e552SArchie Cobbs.Bd -literal -offset 4n 893cb991adSArchie Cobbsstruct ng_bpf_hookprog { 9089624a34SHartmut Brandt char thisHook[NG_HOOKSIZ]; /* name of hook */ 9189624a34SHartmut Brandt char ifMatch[NG_HOOKSIZ]; /* match dest hook */ 9289624a34SHartmut Brandt char ifNotMatch[NG_HOOKSIZ]; /* !match dest hook */ 93*c60bda17SJoel Dahl int32_t bpf_prog_len; /* #insns in program */ 94c1374276SGleb Smirnoff struct bpf_insn bpf_prog[]; /* bpf program */ 9592a3e552SArchie Cobbs}; 9692a3e552SArchie Cobbs.Ed 9792a3e552SArchie Cobbs.Pp 9892a3e552SArchie CobbsThe hook to be updated is specified in 9992a3e552SArchie Cobbs.Dv thisHook . 10092a3e552SArchie CobbsThe BPF program is the sequence of instructions in the 10192a3e552SArchie Cobbs.Dv bpf_prog 10292a3e552SArchie Cobbsarray; there must be 10392a3e552SArchie Cobbs.Dv bpf_prog_len 10492a3e552SArchie Cobbsof them. 10592a3e552SArchie CobbsMatching and non-matching incoming packets are delivered out the hooks named 10692a3e552SArchie Cobbs.Dv ifMatch 10792a3e552SArchie Cobbsand 10892a3e552SArchie Cobbs.Dv ifNotMatch , 1095203edcdSRuslan Ermilovrespectively. 1105203edcdSRuslan ErmilovThe program must be a valid 11151291597SJulian Elischer.Xr bpf 4 11292a3e552SArchie Cobbsprogram or else 11392a3e552SArchie Cobbs.Er EINVAL 11492a3e552SArchie Cobbsis returned. 115*c60bda17SJoel Dahl.It Dv NGM_BPF_GET_PROGRAM Pq Ic getprogram 11651291597SJulian ElischerThis command takes an 11751291597SJulian Elischer.Tn ASCII 11851291597SJulian Elischerstring argument, the hook name, and returns the 11992a3e552SArchie Cobbscorresponding 1203cb991adSArchie Cobbs.Dv "struct ng_bpf_hookprog" 12192a3e552SArchie Cobbsas shown above. 122*c60bda17SJoel Dahl.It Dv NGM_BPF_GET_STATS Pq Ic getstats 12351291597SJulian ElischerThis command takes an 12451291597SJulian Elischer.Tn ASCII 12551291597SJulian Elischerstring argument, the hook name, and returns the 12692a3e552SArchie Cobbsstatistics associated with the hook as a 12792a3e552SArchie Cobbs.Dv "struct ng_bpf_hookstat" . 128*c60bda17SJoel Dahl.It Dv NGM_BPF_CLR_STATS Pq Ic clrstats 12951291597SJulian ElischerThis command takes an 13051291597SJulian Elischer.Tn ASCII 13151291597SJulian Elischerstring argument, the hook name, and clears the 13292a3e552SArchie Cobbsstatistics associated with the hook. 133*c60bda17SJoel Dahl.It Dv NGM_BPF_GETCLR_STATS Pq Ic getclrstats 13492a3e552SArchie CobbsThis command is identical to 13592a3e552SArchie Cobbs.Dv NGM_BPF_GET_STATS , 13692a3e552SArchie Cobbsexcept that the statistics are also atomically cleared. 13792a3e552SArchie Cobbs.El 13892a3e552SArchie Cobbs.Sh SHUTDOWN 13992a3e552SArchie CobbsThis node shuts down upon receipt of a 14092a3e552SArchie Cobbs.Dv NGM_SHUTDOWN 14192a3e552SArchie Cobbscontrol message, or when all hooks have been disconnected. 14228f2bdd8SRuslan Ermilov.Sh EXAMPLES 14378c87228SArchie CobbsIt is possible to configure a node from the command line, using 1441cdc1392SMaxim Sobolev.Xr tcpdump 1 14578c87228SArchie Cobbsto generate raw BPF instructions which are then fed into an 14678c87228SArchie Cobbs.Xr awk 1 14778c87228SArchie Cobbsscript to create the ASCII form of a 14878c87228SArchie Cobbs.Dv NGM_BPF_SET_PROGRAM 14978c87228SArchie Cobbscontrol message, as demonstrated here: 15078c87228SArchie Cobbs.Bd -literal -offset 4n 15178c87228SArchie Cobbs#!/bin/sh 15278c87228SArchie Cobbs 15378c87228SArchie CobbsPATTERN="tcp dst port 80" 15430f6482cSArchie CobbsNODEPATH="my_node:" 15578c87228SArchie CobbsINHOOK="hook1" 15678c87228SArchie CobbsMATCHHOOK="hook2" 15778c87228SArchie CobbsNOTMATCHHOOK="hook3" 15878c87228SArchie Cobbs 1597e7c1896SGiorgos KeramidasBPFPROG=$( tcpdump -s 8192 -ddd ${PATTERN} | \\ 1607e7c1896SGiorgos Keramidas ( read len ; \\ 1617e7c1896SGiorgos Keramidas echo -n "bpf_prog_len=$len" ; \\ 1627e7c1896SGiorgos Keramidas echo -n "bpf_prog=[" ; \\ 1637e7c1896SGiorgos Keramidas while read code jt jf k ; do \\ 1647e7c1896SGiorgos Keramidas echo -n " { code=$code jt=$jt jf=$jf k=$k }" ; \\ 1657e7c1896SGiorgos Keramidas done ; \\ 1667e7c1896SGiorgos Keramidas echo " ]" ) ) 16778c87228SArchie Cobbs 16830f6482cSArchie Cobbsngctl msg ${NODEPATH} setprogram { thisHook=\\"${INHOOK}\\" \\ 16978c87228SArchie Cobbs ifMatch=\\"${MATCHHOOK}\\" \\ 17078c87228SArchie Cobbs ifNotMatch=\\"${NOTMATCHHOOK}\\" \\ 171c1374276SGleb Smirnoff ${BPFPROG} } 17278c87228SArchie Cobbs.Ed 1739cbda590SRuslan Ermilov.Sh SEE ALSO 1749cbda590SRuslan Ermilov.Xr bpf 4 , 1759cbda590SRuslan Ermilov.Xr netgraph 4 , 1769cbda590SRuslan Ermilov.Xr ngctl 8 1779cbda590SRuslan Ermilov.Sh HISTORY 1789cbda590SRuslan ErmilovThe 1799cbda590SRuslan Ermilov.Nm 1809cbda590SRuslan Ermilovnode type was implemented in 1819cbda590SRuslan Ermilov.Fx 4.0 . 1829cbda590SRuslan Ermilov.Sh AUTHORS 1839cbda590SRuslan Ermilov.An Archie Cobbs Aq archie@FreeBSD.org 18492a3e552SArchie Cobbs.Sh BUGS 18592a3e552SArchie CobbsWhen built as a loadable kernel module, this module includes the file 18651291597SJulian Elischer.Pa net/bpf_filter.c . 18792a3e552SArchie CobbsAlthough loading the module should fail if 18851291597SJulian Elischer.Pa net/bpf_filter.c 18992a3e552SArchie Cobbsalready exists in the kernel, currently it does not, and the duplicate 19092a3e552SArchie Cobbscopies of the file do not interfere. 19192a3e552SArchie CobbsHowever, this may change in the future. 192