]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - share/man/man4/ng_bpf.4
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / share / man / man4 / ng_bpf.4
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 May 30, 2007
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 sys/types.h
46 .In net/bpf.h
47 .In netgraph.h
48 .In netgraph/ng_bpf.h
49 .Sh DESCRIPTION
50 The
51 .Nm bpf
52 node type allows Berkeley Packet Filter (see
53 .Xr bpf 4 )
54 filters to be applied to data travelling through a Netgraph network.
55 Each node allows an arbitrary number of connections to arbitrarily
56 named hooks.
57 With each hook is associated a
58 .Xr bpf 4
59 filter program which is applied to incoming data only, a destination hook
60 for matching packets, a destination hook for non-matching packets,
61 and various statistics counters.
62 .Pp
63 A
64 .Xr bpf 4
65 program returns an unsigned integer, which is normally interpreted as
66 the length of the prefix of the packet to return.
67 In the context of this
68 node type, returning zero is considered a non-match, in which case the
69 entire packet is delivered out the non-match destination hook.
70 Returning a value greater than zero causes the packet to be truncated
71 to that length and delivered out the match destination hook.
72 Either or both destination hooks may be the empty string, or may
73 not exist, in which case the packet is dropped.
74 .Pp
75 New hooks are initially configured to drop all packets.
76 A new filter program may be installed using the
77 .Dv NGM_BPF_SET_PROGRAM
78 control message.
79 .Sh HOOKS
80 This node type supports any number of hooks having arbitrary names.
81 .Sh CONTROL MESSAGES
82 This node type supports the generic control messages, plus the following:
83 .Bl -tag -width foo
84 .It Dv NGM_BPF_SET_PROGRAM
85 This command sets the filter program that will be applied to incoming
86 data on a hook.
87 The following structure must be supplied as an argument:
88 .Bd -literal -offset 4n
89 struct ng_bpf_hookprog {
90   char            thisHook[NG_HOOKSIZ];     /* name of hook */
91   char            ifMatch[NG_HOOKSIZ];      /* match dest hook */
92   char            ifNotMatch[NG_HOOKSIZ];   /* !match dest hook */
93   int32_t         bpf_prog_len;             /* #isns in program */
94   struct bpf_insn bpf_prog[];               /* bpf program */
95 };
96 .Ed
97 .Pp
98 The hook to be updated is specified in
99 .Dv thisHook .
100 The BPF program is the sequence of instructions in the
101 .Dv bpf_prog
102 array; there must be
103 .Dv bpf_prog_len
104 of them.
105 Matching and non-matching incoming packets are delivered out the hooks named
106 .Dv ifMatch
107 and
108 .Dv ifNotMatch ,
109 respectively.
110 The program must be a valid
111 .Xr bpf 4
112 program or else
113 .Er EINVAL
114 is returned.
115 .It Dv NGM_BPF_GET_PROGRAM
116 This command takes an
117 .Tn ASCII
118 string argument, the hook name, and returns the
119 corresponding
120 .Dv "struct ng_bpf_hookprog"
121 as shown above.
122 .It Dv NGM_BPF_GET_STATS
123 This command takes an
124 .Tn ASCII
125 string argument, the hook name, and returns the
126 statistics associated with the hook as a
127 .Dv "struct ng_bpf_hookstat" .
128 .It Dv NGM_BPF_CLR_STATS
129 This command takes an
130 .Tn ASCII
131 string argument, the hook name, and clears the
132 statistics associated with the hook.
133 .It Dv NGM_BPF_GETCLR_STATS
134 This command is identical to
135 .Dv NGM_BPF_GET_STATS ,
136 except that the statistics are also atomically cleared.
137 .El
138 .Sh SHUTDOWN
139 This node shuts down upon receipt of a
140 .Dv NGM_SHUTDOWN
141 control message, or when all hooks have been disconnected.
142 .Sh EXAMPLES
143 It is possible to configure a node from the command line, using
144 .Xr tcpdump 1
145 to generate raw BPF instructions which are then fed into an
146 .Xr awk 1
147 script to create the ASCII form of a
148 .Dv NGM_BPF_SET_PROGRAM
149 control message, as demonstrated here:
150 .Bd -literal -offset 4n
151 #!/bin/sh
152
153 PATTERN="tcp dst port 80"
154 NODEPATH="my_node:"
155 INHOOK="hook1"
156 MATCHHOOK="hook2"
157 NOTMATCHHOOK="hook3"
158
159 BPFPROG=$( tcpdump -s 8192 -ddd ${PATTERN} | \\
160            ( read len ; \\
161              echo -n "bpf_prog_len=$len" ; \\
162              echo -n "bpf_prog=[" ; \\
163              while read code jt jf k ; do \\
164                  echo -n " { code=$code jt=$jt jf=$jf k=$k }" ; \\
165              done ; \\
166              echo " ]" ) )
167
168 ngctl msg ${NODEPATH} setprogram { thisHook=\\"${INHOOK}\\" \\
169   ifMatch=\\"${MATCHHOOK}\\" \\
170   ifNotMatch=\\"${NOTMATCHHOOK}\\" \\
171   ${BPFPROG} }
172 .Ed
173 .Sh SEE ALSO
174 .Xr bpf 4 ,
175 .Xr netgraph 4 ,
176 .Xr ngctl 8
177 .Sh HISTORY
178 The
179 .Nm
180 node type was implemented in
181 .Fx 4.0 .
182 .Sh AUTHORS
183 .An Archie Cobbs Aq archie@FreeBSD.org
184 .Sh BUGS
185 When built as a loadable kernel module, this module includes the file
186 .Pa net/bpf_filter.c .
187 Although loading the module should fail if
188 .Pa net/bpf_filter.c
189 already exists in the kernel, currently it does not, and the duplicate
190 copies of the file do not interfere.
191 However, this may change in the future.