xref: /freebsd/share/man/man4/ng_bpf.4 (revision 1cdc13928007b1bed491e36df3d19fd7d87b900c)
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.\"
3892a3e552SArchie Cobbs.Dd December 2, 1999
39bec35b9aSArchie Cobbs.Dt NG_BPF 4
4051291597SJulian Elischer.Os FreeBSD
4192a3e552SArchie Cobbs.Sh NAME
4292a3e552SArchie Cobbs.Nm ng_bpf
4392a3e552SArchie Cobbs.Nd Berkeley packet filter netgraph node type
4492a3e552SArchie Cobbs.Sh SYNOPSIS
4592a3e552SArchie Cobbs.Fd #include <net/bpf.h>
4692a3e552SArchie Cobbs.Fd #include <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
5492a3e552SArchie Cobbsnamed hooks.  With each hook is associated a
553136363fSRuslan Ermilov.Xr bpf 4
5692a3e552SArchie Cobbsfilter program which is applied to incoming data only, a destination hook
5792a3e552SArchie Cobbsfor matching packets, a destination hook for non-matching packets,
5892a3e552SArchie Cobbsand various statistics counters.
5992a3e552SArchie Cobbs.Pp
6092a3e552SArchie CobbsA
6151291597SJulian Elischer.Xr bpf 4
6292a3e552SArchie Cobbsprogram returns an unsigned integer, which is normally interpreted as
6392a3e552SArchie Cobbsthe length of the prefix of the packet to return.  In the context of this
6492a3e552SArchie Cobbsnode type, returning zero is considered a non-match, in which case the
6592a3e552SArchie Cobbsentire packet is delivered out the non-match destination hook.
6692a3e552SArchie CobbsReturning a value greater than zero causes the packet to be truncated
6792a3e552SArchie Cobbsto that length and delivered out the match destination hook.
6892a3e552SArchie CobbsEither or both destination hooks may be the empty string, or may
6992a3e552SArchie Cobbsnot exist, in which case the packet is dropped.
7092a3e552SArchie Cobbs.Pp
7192a3e552SArchie CobbsNew hooks are initially configured to drop all packets.
72e653127eSArchie CobbsA new filter program may be installed using the
73e653127eSArchie Cobbs.Dv NGM_BPF_SET_PROGRAM
7492a3e552SArchie Cobbscontrol message.
7592a3e552SArchie Cobbs.Sh HOOKS
7692a3e552SArchie CobbsThis node type supports any number of hooks having arbitrary names.
7792a3e552SArchie Cobbs.Sh CONTROL MESSAGES
7892a3e552SArchie CobbsThis node type supports the generic control messages, plus the following:
7992a3e552SArchie Cobbs.Bl -tag -width foo
80e653127eSArchie Cobbs.It Dv NGM_BPF_SET_PROGRAM
8192a3e552SArchie CobbsThis command sets the filter program that will be applied to incoming
8292a3e552SArchie Cobbsdata on a hook.  The following structure must be supplied as an argument:
8392a3e552SArchie Cobbs.Bd -literal -offset 4n
843cb991adSArchie Cobbsstruct ng_bpf_hookprog {
8592a3e552SArchie Cobbs  char            thisHook[NG_HOOKLEN+1];   /* name of hook */
8692a3e552SArchie Cobbs  char            ifMatch[NG_HOOKLEN+1];    /* match dest hook */
8792a3e552SArchie Cobbs  char            ifNotMatch[NG_HOOKLEN+1]; /* !match dest hook */
8892a3e552SArchie Cobbs  int32_t         bpf_prog_len;             /* #isns in program */
8992a3e552SArchie Cobbs  struct bpf_insn bpf_prog[0];              /* bpf program */
9092a3e552SArchie Cobbs};
9192a3e552SArchie Cobbs.Ed
9292a3e552SArchie Cobbs.Pp
9392a3e552SArchie CobbsThe hook to be updated is specified in
9492a3e552SArchie Cobbs.Dv thisHook .
9592a3e552SArchie CobbsThe BPF program is the sequence of instructions in the
9692a3e552SArchie Cobbs.Dv bpf_prog
9792a3e552SArchie Cobbsarray; there must be
9892a3e552SArchie Cobbs.Dv bpf_prog_len
9992a3e552SArchie Cobbsof them.
10092a3e552SArchie CobbsMatching and non-matching incoming packets are delivered out the hooks named
10192a3e552SArchie Cobbs.Dv ifMatch
10292a3e552SArchie Cobbsand
10392a3e552SArchie Cobbs.Dv ifNotMatch ,
10492a3e552SArchie Cobbsrespectively.  The program must be a valid
10551291597SJulian Elischer.Xr bpf 4
10692a3e552SArchie Cobbsprogram or else
10792a3e552SArchie Cobbs.Er EINVAL
10892a3e552SArchie Cobbsis returned.
109e653127eSArchie Cobbs.It Dv NGM_BPF_GET_PROGRAM
11051291597SJulian ElischerThis command takes an
11151291597SJulian Elischer.Tn ASCII
11251291597SJulian Elischerstring argument, the hook name, and returns the
11392a3e552SArchie Cobbscorresponding
1143cb991adSArchie Cobbs.Dv "struct ng_bpf_hookprog"
11592a3e552SArchie Cobbsas shown above.
11692a3e552SArchie Cobbs.It Dv NGM_BPF_GET_STATS
11751291597SJulian ElischerThis command takes an
11851291597SJulian Elischer.Tn ASCII
11951291597SJulian Elischerstring argument, the hook name, and returns the
12092a3e552SArchie Cobbsstatistics associated with the hook as a
12192a3e552SArchie Cobbs.Dv "struct ng_bpf_hookstat" .
12292a3e552SArchie Cobbs.It Dv NGM_BPF_CLR_STATS
12351291597SJulian ElischerThis command takes an
12451291597SJulian Elischer.Tn ASCII
12551291597SJulian Elischerstring argument, the hook name, and clears the
12692a3e552SArchie Cobbsstatistics associated with the hook.
12792a3e552SArchie Cobbs.It Dv NGM_BPF_GETCLR_STATS
12892a3e552SArchie CobbsThis command is identical to
12992a3e552SArchie Cobbs.Dv NGM_BPF_GET_STATS ,
13092a3e552SArchie Cobbsexcept that the statistics are also atomically cleared.
13192a3e552SArchie Cobbs.El
13292a3e552SArchie Cobbs.Sh SHUTDOWN
13392a3e552SArchie CobbsThis node shuts down upon receipt of a
13492a3e552SArchie Cobbs.Dv NGM_SHUTDOWN
13592a3e552SArchie Cobbscontrol message, or when all hooks have been disconnected.
13628f2bdd8SRuslan Ermilov.Sh EXAMPLES
13778c87228SArchie CobbsIt is possible to configure a node from the command line, using
1381cdc1392SMaxim Sobolev.Xr tcpdump 1
13978c87228SArchie Cobbsto generate raw BPF instructions which are then fed into an
14078c87228SArchie Cobbs.Xr awk 1
14178c87228SArchie Cobbsscript to create 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
15378c87228SArchie Cobbscat > /tmp/bpf.awk << xxENDxx
15478c87228SArchie Cobbs{
15578c87228SArchie Cobbs  if (!init) {
15678c87228SArchie Cobbs    printf "bpf_prog_len=%d bpf_prog=[", \\$1;
15778c87228SArchie Cobbs    init=1;
15878c87228SArchie Cobbs  } else {
15978c87228SArchie Cobbs    printf " { code=%d jt=%d jf=%d k=%d }", \\$1, \\$2, \\$3, \\$4;
16078c87228SArchie Cobbs  }
16178c87228SArchie Cobbs}
16278c87228SArchie CobbsEND {
16378c87228SArchie Cobbs  print " ]"
16478c87228SArchie Cobbs}
16578c87228SArchie CobbsxxENDxx
16678c87228SArchie Cobbs
16720d7cd24SArchie CobbsBPFPROG=`tcpdump -s 8192 -ddd ${PATTERN} | awk -f /tmp/bpf.awk`
16878c87228SArchie Cobbs
16930f6482cSArchie Cobbsngctl msg ${NODEPATH} setprogram { thisHook=\\"${INHOOK}\\" \\
17078c87228SArchie Cobbs  ifMatch=\\"${MATCHHOOK}\\" \\
17178c87228SArchie Cobbs  ifNotMatch=\\"${NOTMATCHHOOK}\\" \\
17278c87228SArchie Cobbs  ${BPFPROG} } }
17378c87228SArchie Cobbs.Ed
17492a3e552SArchie Cobbs.Sh BUGS
17592a3e552SArchie CobbsWhen built as a loadable kernel module, this module includes the file
17651291597SJulian Elischer.Pa net/bpf_filter.c .
17792a3e552SArchie CobbsAlthough loading the module should fail if
17851291597SJulian Elischer.Pa net/bpf_filter.c
17992a3e552SArchie Cobbsalready exists in the kernel, currently it does not, and the duplicate
18092a3e552SArchie Cobbscopies of the file do not interfere.
18192a3e552SArchie CobbsHowever, this may change in the future.
18251291597SJulian Elischer.Sh HISTORY
18351291597SJulian ElischerThe
18451291597SJulian Elischer.Nm
18551291597SJulian Elischernode type was implemented in
18651291597SJulian Elischer.Fx 4.0 .
18792a3e552SArchie Cobbs.Sh SEE ALSO
18892a3e552SArchie Cobbs.Xr netgraph 4 ,
18992a3e552SArchie Cobbs.Xr bpf 4 ,
190bec35b9aSArchie Cobbs.Xr ngctl 8
19151291597SJulian Elischer.Sh AUTHORS
192eddc45e7SJeroen Ruigrok van der Werven.An Archie Cobbs Aq archie@FreeBSD.org
193