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.\" $Whistle: ng_bpf.8,v 1.2 1999/12/03 01:57:12 archie Exp $ 3592a3e552SArchie Cobbs.\" 36*81a93548SHans Petter Selasky.Dd September 20, 2020 37bec35b9aSArchie Cobbs.Dt NG_BPF 4 383d45e180SRuslan Ermilov.Os 3992a3e552SArchie Cobbs.Sh NAME 4092a3e552SArchie Cobbs.Nm ng_bpf 4192a3e552SArchie Cobbs.Nd Berkeley packet filter netgraph node type 4292a3e552SArchie Cobbs.Sh SYNOPSIS 43fbad9e2dSRuslan Ermilov.In sys/types.h 4432eef9aeSRuslan Ermilov.In net/bpf.h 45fbad9e2dSRuslan Ermilov.In netgraph.h 4632eef9aeSRuslan Ermilov.In netgraph/ng_bpf.h 4792a3e552SArchie Cobbs.Sh DESCRIPTION 4892a3e552SArchie CobbsThe 4992a3e552SArchie Cobbs.Nm bpf 5092a3e552SArchie Cobbsnode type allows Berkeley Packet Filter (see 5151291597SJulian Elischer.Xr bpf 4 ) 5292a3e552SArchie Cobbsfilters to be applied to data travelling through a Netgraph network. 5392a3e552SArchie CobbsEach node allows an arbitrary number of connections to arbitrarily 545203edcdSRuslan Ermilovnamed hooks. 555203edcdSRuslan ErmilovWith each hook is associated a 563136363fSRuslan Ermilov.Xr bpf 4 5792a3e552SArchie Cobbsfilter program which is applied to incoming data only, a destination hook 5892a3e552SArchie Cobbsfor matching packets, a destination hook for non-matching packets, 5992a3e552SArchie Cobbsand various statistics counters. 6092a3e552SArchie Cobbs.Pp 6192a3e552SArchie CobbsA 6251291597SJulian Elischer.Xr bpf 4 6392a3e552SArchie Cobbsprogram returns an unsigned integer, which is normally interpreted as 645203edcdSRuslan Ermilovthe length of the prefix of the packet to return. 655203edcdSRuslan ErmilovIn the context of this 6692a3e552SArchie Cobbsnode type, returning zero is considered a non-match, in which case the 6792a3e552SArchie Cobbsentire packet is delivered out the non-match destination hook. 6892a3e552SArchie CobbsReturning a value greater than zero causes the packet to be truncated 6992a3e552SArchie Cobbsto that length and delivered out the match destination hook. 7092a3e552SArchie CobbsEither or both destination hooks may be the empty string, or may 7192a3e552SArchie Cobbsnot exist, in which case the packet is dropped. 7292a3e552SArchie Cobbs.Pp 7392a3e552SArchie CobbsNew hooks are initially configured to drop all packets. 74e653127eSArchie CobbsA new filter program may be installed using the 75e653127eSArchie Cobbs.Dv NGM_BPF_SET_PROGRAM 7692a3e552SArchie Cobbscontrol message. 7792a3e552SArchie Cobbs.Sh HOOKS 7892a3e552SArchie CobbsThis node type supports any number of hooks having arbitrary names. 7992a3e552SArchie Cobbs.Sh CONTROL MESSAGES 8092a3e552SArchie CobbsThis node type supports the generic control messages, plus the following: 8192a3e552SArchie Cobbs.Bl -tag -width foo 82c60bda17SJoel Dahl.It Dv NGM_BPF_SET_PROGRAM Pq Ic setprogram 8392a3e552SArchie CobbsThis command sets the filter program that will be applied to incoming 845203edcdSRuslan Ermilovdata on a hook. 855203edcdSRuslan ErmilovThe following structure must be supplied as an argument: 8692a3e552SArchie Cobbs.Bd -literal -offset 4n 873cb991adSArchie Cobbsstruct ng_bpf_hookprog { 8889624a34SHartmut Brandt char thisHook[NG_HOOKSIZ]; /* name of hook */ 8989624a34SHartmut Brandt char ifMatch[NG_HOOKSIZ]; /* match dest hook */ 9089624a34SHartmut Brandt char ifNotMatch[NG_HOOKSIZ]; /* !match dest hook */ 91c60bda17SJoel Dahl int32_t bpf_prog_len; /* #insns in program */ 92c1374276SGleb Smirnoff struct bpf_insn bpf_prog[]; /* bpf program */ 9392a3e552SArchie Cobbs}; 9492a3e552SArchie Cobbs.Ed 9592a3e552SArchie Cobbs.Pp 9692a3e552SArchie CobbsThe hook to be updated is specified in 9792a3e552SArchie Cobbs.Dv thisHook . 9892a3e552SArchie CobbsThe BPF program is the sequence of instructions in the 9992a3e552SArchie Cobbs.Dv bpf_prog 10092a3e552SArchie Cobbsarray; there must be 10192a3e552SArchie Cobbs.Dv bpf_prog_len 10292a3e552SArchie Cobbsof them. 10392a3e552SArchie CobbsMatching and non-matching incoming packets are delivered out the hooks named 10492a3e552SArchie Cobbs.Dv ifMatch 10592a3e552SArchie Cobbsand 10692a3e552SArchie Cobbs.Dv ifNotMatch , 1075203edcdSRuslan Ermilovrespectively. 1085203edcdSRuslan ErmilovThe program must be a valid 10951291597SJulian Elischer.Xr bpf 4 11092a3e552SArchie Cobbsprogram or else 11192a3e552SArchie Cobbs.Er EINVAL 11292a3e552SArchie Cobbsis returned. 113c60bda17SJoel Dahl.It Dv NGM_BPF_GET_PROGRAM Pq Ic getprogram 114*81a93548SHans Petter SelaskyThis command takes an ASCII 11551291597SJulian Elischerstring argument, the hook name, and returns the 11692a3e552SArchie Cobbscorresponding 1173cb991adSArchie Cobbs.Dv "struct ng_bpf_hookprog" 11892a3e552SArchie Cobbsas shown above. 119c60bda17SJoel Dahl.It Dv NGM_BPF_GET_STATS Pq Ic getstats 120*81a93548SHans Petter SelaskyThis command takes an ASCII 12151291597SJulian Elischerstring argument, the hook name, and returns the 12292a3e552SArchie Cobbsstatistics associated with the hook as a 12392a3e552SArchie Cobbs.Dv "struct ng_bpf_hookstat" . 124c60bda17SJoel Dahl.It Dv NGM_BPF_CLR_STATS Pq Ic clrstats 125*81a93548SHans Petter SelaskyThis command takes an ASCII 12651291597SJulian Elischerstring argument, the hook name, and clears the 12792a3e552SArchie Cobbsstatistics associated with the hook. 128c60bda17SJoel Dahl.It Dv NGM_BPF_GETCLR_STATS Pq Ic getclrstats 12992a3e552SArchie CobbsThis command is identical to 13092a3e552SArchie Cobbs.Dv NGM_BPF_GET_STATS , 13192a3e552SArchie Cobbsexcept that the statistics are also atomically cleared. 13292a3e552SArchie Cobbs.El 13392a3e552SArchie Cobbs.Sh SHUTDOWN 13492a3e552SArchie CobbsThis node shuts down upon receipt of a 13592a3e552SArchie Cobbs.Dv NGM_SHUTDOWN 13692a3e552SArchie Cobbscontrol message, or when all hooks have been disconnected. 13728f2bdd8SRuslan Ermilov.Sh EXAMPLES 13878c87228SArchie CobbsIt is possible to configure a node from the command line, using 1391cdc1392SMaxim Sobolev.Xr tcpdump 1 140*81a93548SHans Petter Selaskyto generate raw BPF instructions which are then transformed 141*81a93548SHans Petter Selaskyinto the ASCII form of a 14278c87228SArchie Cobbs.Dv NGM_BPF_SET_PROGRAM 14378c87228SArchie Cobbscontrol message, as demonstrated here: 14478c87228SArchie Cobbs.Bd -literal -offset 4n 14578c87228SArchie Cobbs#!/bin/sh 14678c87228SArchie Cobbs 14778c87228SArchie CobbsPATTERN="tcp dst port 80" 14830f6482cSArchie CobbsNODEPATH="my_node:" 14978c87228SArchie CobbsINHOOK="hook1" 15078c87228SArchie CobbsMATCHHOOK="hook2" 15178c87228SArchie CobbsNOTMATCHHOOK="hook3" 15278c87228SArchie Cobbs 153bc044625SEugene GrosbeinBPFPROG=$( tcpdump -s 8192 -p -ddd ${PATTERN} | \\ 1547e7c1896SGiorgos Keramidas ( read len ; \\ 1557e7c1896SGiorgos Keramidas echo -n "bpf_prog_len=$len " ; \\ 1567e7c1896SGiorgos Keramidas echo -n "bpf_prog=[" ; \\ 1577e7c1896SGiorgos Keramidas while read code jt jf k ; do \\ 1587e7c1896SGiorgos Keramidas echo -n " { code=$code jt=$jt jf=$jf k=$k }" ; \\ 1597e7c1896SGiorgos Keramidas done ; \\ 1607e7c1896SGiorgos Keramidas echo " ]" ) ) 16178c87228SArchie Cobbs 16230f6482cSArchie Cobbsngctl msg ${NODEPATH} setprogram { thisHook=\\"${INHOOK}\\" \\ 16378c87228SArchie Cobbs ifMatch=\\"${MATCHHOOK}\\" \\ 16478c87228SArchie Cobbs ifNotMatch=\\"${NOTMATCHHOOK}\\" \\ 165c1374276SGleb Smirnoff ${BPFPROG} } 16678c87228SArchie Cobbs.Ed 167*81a93548SHans Petter Selasky.Pp 168*81a93548SHans Petter SelaskyBased on the previous example, it is possible to prevent a jail (or a VM) 169*81a93548SHans Petter Selaskyfrom spoofing by allowing only traffic that has the expected ethernet and 170*81a93548SHans Petter SelaskyIP addresses: 171*81a93548SHans Petter Selasky.Bd -literal -offset 4n 172*81a93548SHans Petter Selasky#!/bin/sh 173*81a93548SHans Petter Selasky 174*81a93548SHans Petter SelaskyNODEPATH="my_node:" 175*81a93548SHans Petter SelaskyJAIL_MAC="0a:00:de:ad:be:ef" 176*81a93548SHans Petter SelaskyJAIL_IP="128.66.1.42" 177*81a93548SHans Petter SelaskyJAIL_HOOK="jail" 178*81a93548SHans Petter SelaskyHOST_HOOK="host" 179*81a93548SHans Petter SelaskyDEBUG_HOOK="nomatch" 180*81a93548SHans Petter Selasky 181*81a93548SHans Petter Selaskybpf_prog() { 182*81a93548SHans Petter Selasky local PATTERN=$1 183*81a93548SHans Petter Selasky 184*81a93548SHans Petter Selasky tcpdump -s 8192 -p -ddd ${PATTERN} | ( 185*81a93548SHans Petter Selasky read len 186*81a93548SHans Petter Selasky echo -n "bpf_prog_len=$len " 187*81a93548SHans Petter Selasky echo -n "bpf_prog=[" 188*81a93548SHans Petter Selasky while read code jt jf k ; do 189*81a93548SHans Petter Selasky echo -n " { code=$code jt=$jt jf=$jf k=$k }" 190*81a93548SHans Petter Selasky done 191*81a93548SHans Petter Selasky echo " ]" 192*81a93548SHans Petter Selasky ) 193*81a93548SHans Petter Selasky} 194*81a93548SHans Petter Selasky 195*81a93548SHans Petter Selasky# Prevent jail from spoofing (filter packets coming from jail) 196*81a93548SHans Petter Selaskyngctl msg ${NODEPATH} setprogram { \\ 197*81a93548SHans Petter Selasky thisHook=\\"${JAIL_HOOK}\\" \\ 198*81a93548SHans Petter Selasky ifMatch=\\"${HOST_HOOK}\\" \\ 199*81a93548SHans Petter Selasky ifNotMatch=\\"${DEBUG_HOOK}\\" \\ 200*81a93548SHans Petter Selasky $(bpf_prog "ether src ${JAIL_MAC} && src ${JAIL_IP}") \\ 201*81a93548SHans Petter Selasky} 202*81a93548SHans Petter Selasky 203*81a93548SHans Petter Selasky# Prevent jail from receiving spoofed packets (filter packets 204*81a93548SHans Petter Selasky# coming from host) 205*81a93548SHans Petter Selaskyngctl msg ${NODEPATH} setprogram { \\ 206*81a93548SHans Petter Selasky thisHook=\\"${HOST_HOOK}\\" \\ 207*81a93548SHans Petter Selasky ifMatch=\\"${JAIL_HOOK}\\" \\ 208*81a93548SHans Petter Selasky ifNotMatch=\\"${DEBUG_HOOK}\\" \\ 209*81a93548SHans Petter Selasky $(bpf_prog "ether dst ${JAIL_MAC} && dst ${JAIL_IP}") \\ 210*81a93548SHans Petter Selasky} 211*81a93548SHans Petter Selasky.Ed 2129cbda590SRuslan Ermilov.Sh SEE ALSO 2139cbda590SRuslan Ermilov.Xr bpf 4 , 2149cbda590SRuslan Ermilov.Xr netgraph 4 , 2159cbda590SRuslan Ermilov.Xr ngctl 8 2169cbda590SRuslan Ermilov.Sh HISTORY 2179cbda590SRuslan ErmilovThe 2189cbda590SRuslan Ermilov.Nm 2199cbda590SRuslan Ermilovnode type was implemented in 2209cbda590SRuslan Ermilov.Fx 4.0 . 2219cbda590SRuslan Ermilov.Sh AUTHORS 2226c899950SBaptiste Daroussin.An Archie Cobbs Aq Mt archie@FreeBSD.org 22392a3e552SArchie Cobbs.Sh BUGS 22492a3e552SArchie CobbsWhen built as a loadable kernel module, this module includes the file 22551291597SJulian Elischer.Pa net/bpf_filter.c . 22692a3e552SArchie CobbsAlthough loading the module should fail if 22751291597SJulian Elischer.Pa net/bpf_filter.c 22892a3e552SArchie Cobbsalready exists in the kernel, currently it does not, and the duplicate 22992a3e552SArchie Cobbscopies of the file do not interfere. 23092a3e552SArchie CobbsHowever, this may change in the future. 231