1.\" Copyright (c) 1999 Whistle Communications, Inc. 2.\" All rights reserved. 3.\" 4.\" Subject to the following obligations and disclaimer of warranty, use and 5.\" redistribution of this software, in source or object code forms, with or 6.\" without modifications are expressly permitted by Whistle Communications; 7.\" provided, however, that: 8.\" 1. Any and all reproductions of the source or object code must include the 9.\" copyright notice above and the following disclaimer of warranties; and 10.\" 2. No rights are granted, in any manner or form, to use Whistle 11.\" Communications, Inc. trademarks, including the mark "WHISTLE 12.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as 13.\" such appears in the above copyright notice or in the software. 14.\" 15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 31.\" OF SUCH DAMAGE. 32.\" 33.\" Author: Archie Cobbs <archie@FreeBSD.org> 34.\" 35.\" $FreeBSD$ 36.\" $Whistle: ng_bpf.8,v 1.2 1999/12/03 01:57:12 archie Exp $ 37.\" 38.Dd December 2, 1999 39.Dt NG_BPF 4 40.Os 41.Sh NAME 42.Nm ng_bpf 43.Nd Berkeley packet filter netgraph node type 44.Sh SYNOPSIS 45.In net/bpf.h 46.In netgraph/ng_bpf.h 47.Sh DESCRIPTION 48The 49.Nm bpf 50node type allows Berkeley Packet Filter (see 51.Xr bpf 4 ) 52filters to be applied to data travelling through a Netgraph network. 53Each node allows an arbitrary number of connections to arbitrarily 54named hooks. 55With each hook is associated a 56.Xr bpf 4 57filter program which is applied to incoming data only, a destination hook 58for matching packets, a destination hook for non-matching packets, 59and various statistics counters. 60.Pp 61A 62.Xr bpf 4 63program returns an unsigned integer, which is normally interpreted as 64the length of the prefix of the packet to return. 65In the context of this 66node type, returning zero is considered a non-match, in which case the 67entire packet is delivered out the non-match destination hook. 68Returning a value greater than zero causes the packet to be truncated 69to that length and delivered out the match destination hook. 70Either or both destination hooks may be the empty string, or may 71not exist, in which case the packet is dropped. 72.Pp 73New hooks are initially configured to drop all packets. 74A new filter program may be installed using the 75.Dv NGM_BPF_SET_PROGRAM 76control message. 77.Sh HOOKS 78This node type supports any number of hooks having arbitrary names. 79.Sh CONTROL MESSAGES 80This node type supports the generic control messages, plus the following: 81.Bl -tag -width foo 82.It Dv NGM_BPF_SET_PROGRAM 83This command sets the filter program that will be applied to incoming 84data on a hook. 85The following structure must be supplied as an argument: 86.Bd -literal -offset 4n 87struct ng_bpf_hookprog { 88 char thisHook[NG_HOOKSIZ]; /* name of hook */ 89 char ifMatch[NG_HOOKSIZ]; /* match dest hook */ 90 char ifNotMatch[NG_HOOKSIZ]; /* !match dest hook */ 91 int32_t bpf_prog_len; /* #isns in program */ 92 struct bpf_insn bpf_prog[0]; /* bpf program */ 93}; 94.Ed 95.Pp 96The hook to be updated is specified in 97.Dv thisHook . 98The BPF program is the sequence of instructions in the 99.Dv bpf_prog 100array; there must be 101.Dv bpf_prog_len 102of them. 103Matching and non-matching incoming packets are delivered out the hooks named 104.Dv ifMatch 105and 106.Dv ifNotMatch , 107respectively. 108The program must be a valid 109.Xr bpf 4 110program or else 111.Er EINVAL 112is returned. 113.It Dv NGM_BPF_GET_PROGRAM 114This command takes an 115.Tn ASCII 116string argument, the hook name, and returns the 117corresponding 118.Dv "struct ng_bpf_hookprog" 119as shown above. 120.It Dv NGM_BPF_GET_STATS 121This command takes an 122.Tn ASCII 123string argument, the hook name, and returns the 124statistics associated with the hook as a 125.Dv "struct ng_bpf_hookstat" . 126.It Dv NGM_BPF_CLR_STATS 127This command takes an 128.Tn ASCII 129string argument, the hook name, and clears the 130statistics associated with the hook. 131.It Dv NGM_BPF_GETCLR_STATS 132This command is identical to 133.Dv NGM_BPF_GET_STATS , 134except that the statistics are also atomically cleared. 135.El 136.Sh SHUTDOWN 137This node shuts down upon receipt of a 138.Dv NGM_SHUTDOWN 139control message, or when all hooks have been disconnected. 140.Sh EXAMPLES 141It is possible to configure a node from the command line, using 142.Xr tcpdump 1 143to generate raw BPF instructions which are then fed into an 144.Xr awk 1 145script to create the ASCII form of a 146.Dv NGM_BPF_SET_PROGRAM 147control message, as demonstrated here: 148.Bd -literal -offset 4n 149#!/bin/sh 150 151PATTERN="tcp dst port 80" 152NODEPATH="my_node:" 153INHOOK="hook1" 154MATCHHOOK="hook2" 155NOTMATCHHOOK="hook3" 156 157cat > /tmp/bpf.awk << xxENDxx 158{ 159 if (!init) { 160 printf "bpf_prog_len=%d bpf_prog=[", \\$1; 161 init=1; 162 } else { 163 printf " { code=%d jt=%d jf=%d k=%d }", \\$1, \\$2, \\$3, \\$4; 164 } 165} 166END { 167 print " ]" 168} 169xxENDxx 170 171BPFPROG=`tcpdump -s 8192 -ddd ${PATTERN} | awk -f /tmp/bpf.awk` 172 173ngctl msg ${NODEPATH} setprogram { thisHook=\\"${INHOOK}\\" \\ 174 ifMatch=\\"${MATCHHOOK}\\" \\ 175 ifNotMatch=\\"${NOTMATCHHOOK}\\" \\ 176 ${BPFPROG} } } 177.Ed 178.Sh BUGS 179When built as a loadable kernel module, this module includes the file 180.Pa net/bpf_filter.c . 181Although loading the module should fail if 182.Pa net/bpf_filter.c 183already exists in the kernel, currently it does not, and the duplicate 184copies of the file do not interfere. 185However, this may change in the future. 186.Sh HISTORY 187The 188.Nm 189node type was implemented in 190.Fx 4.0 . 191.Sh SEE ALSO 192.Xr bpf 4 , 193.Xr netgraph 4 , 194.Xr ngctl 8 195.Sh AUTHORS 196.An Archie Cobbs Aq archie@FreeBSD.org 197