Wed Sep 26 21:58:27 2012 UTC ()
npf.conf(5): improve and explain grammar definition.


(rmind)
diff -r1.15 -r1.16 src/usr.sbin/npf/npfctl/npf.conf.5

cvs diff -r1.15 -r1.16 src/usr.sbin/npf/npfctl/npf.conf.5 (switch to unified diff)

--- src/usr.sbin/npf/npfctl/npf.conf.5 2012/08/13 01:18:31 1.15
+++ src/usr.sbin/npf/npfctl/npf.conf.5 2012/09/26 21:58:27 1.16
@@ -1,235 +1,255 @@ @@ -1,235 +1,255 @@
1.\" $NetBSD: npf.conf.5,v 1.15 2012/08/13 01:18:31 rmind Exp $ 1.\" $NetBSD: npf.conf.5,v 1.16 2012/09/26 21:58:27 rmind Exp $
2.\" 2.\"
3.\" Copyright (c) 2009-2012 The NetBSD Foundation, Inc. 3.\" Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
4.\" All rights reserved. 4.\" All rights reserved.
5.\" 5.\"
6.\" This material is based upon work partially supported by The 6.\" This material is based upon work partially supported by The
7.\" NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 7.\" NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
8.\" 8.\"
9.\" Redistribution and use in source and binary forms, with or without 9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions 10.\" modification, are permitted provided that the following conditions
11.\" are met: 11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright 12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer. 13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\" notice, this list of conditions and the following disclaimer in the 15.\" notice, this list of conditions and the following disclaimer in the
16.\" documentation and/or other materials provided with the distribution. 16.\" documentation and/or other materials provided with the distribution.
17.\" 17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE. 28.\" POSSIBILITY OF SUCH DAMAGE.
29.\" 29.\"
30.Dd August 12, 2012 30.Dd September 26, 2012
31.Dt NPF.CONF 5 31.Dt NPF.CONF 5
32.Os 32.Os
33.Sh NAME 33.Sh NAME
34.Nm npf.conf 34.Nm npf.conf
35.Nd NPF packet filter configuration file 35.Nd NPF packet filter configuration file
36.\" ----- 36.\" -----
37.Sh DESCRIPTION 37.Sh DESCRIPTION
38.Nm 38.Nm
39is the default configuration file for NPF packet filter. 39is the default configuration file for NPF packet filter.
40It can contain definitions, grouped rules, rule procedures, 40It can contain definitions, grouped rules, rule procedures,
41translation policies, and tables. 41translation policies, and tables.
42.Ss Definitions 42.Ss Definitions
43Definitions are general purpose keywords which can be used in the 43Definitions are general purpose keywords which can be used in the
44ruleset to make it more flexible and easier to manage. 44ruleset to make it more flexible and easier to manage.
45Most commonly, definitions are used to define one of the following: 45Most commonly, definitions are used to define one of the following:
46IP addresses, networks, ports, or interfaces. 46IP addresses, networks, ports, or interfaces.
47Definitions can contain multiple elements. 47Definitions can contain multiple elements.
48.Ss Groups 48.Ss Groups
49Having one huge ruleset for all interfaces or directions might be 49Having one huge ruleset for all interfaces or directions might be
50inefficient; therefore, NPF requires that all rules be defined within groups. 50inefficient; therefore, NPF requires that all rules be defined within groups.
51Groups can be thought of as higher level rules which have subrules. 51Groups can be thought of as higher level rules which have subrules.
52The main properties of a group are its interface and traffic direction. 52The main properties of a group are its interface and traffic direction.
53Packets matching group criteria are passed to the ruleset of that group. 53Packets matching group criteria are passed to the ruleset of that group.
54If a packet does not match any group, it is passed to the default group. 54If a packet does not match any group, it is passed to the default group.
55The default group must always be defined. 55The default group must always be defined.
56.Ss Rules 56.Ss Rules
57Rules, which are the main part of NPF configuration, describe the criteria 57Rules, which are the main part of NPF configuration, describe the criteria
58used to inspect and make decisions about packets. 58used to inspect and make decisions about packets.
59Currently, NPF supports filtering on the following criteria: interface, 59Currently, NPF supports filtering on the following criteria: interface,
60traffic direction, protocol, IP address or network, TCP/UDP port 60traffic direction, protocol, IP address or network, TCP/UDP port
61or range, TCP flags, and ICMP type/code. 61or range, TCP flags, and ICMP type/code.
62Supported actions are blocking or passing the packet. 62Supported actions are blocking or passing the packet.
63.Pp 63.Pp
64Each rule has a priority, which is set according to its order in the ruleset. 64Each rule has a priority, which is set according to its order in the ruleset.
65Rules defined first are accordingly inspected first. 65Rules defined first are accordingly inspected first.
66All rules in the group are inspected sequentially, and the last matching 66All rules in the group are inspected sequentially, and the last matching
67dictates the action to be taken. 67dictates the action to be taken.
68Rules, however, may be explicitly marked as final. 68Rules, however, may be explicitly marked as final.
69In such cases, processing stops after encountering the first matching rule 69In such cases, processing stops after encountering the first matching rule
70marked as final. 70marked as final.
71If there is no matching rule in the custom group, then rules in the default 71If there is no matching rule in the custom group, then rules in the default
72group will be inspected. 72group will be inspected.
73.Pp 73.Pp
74Stateful filtering is supported using the "stateful" keyword. 74Stateful filtering is supported using the "stateful" keyword.
75In such cases, state (a session) is created and any further packets 75In such cases, state (a session) is created and any further packets
76of the connection are tracked. 76of the connection are tracked.
77Packets in backwards stream, after having been confirmed to belong to 77Packets in backwards stream, after having been confirmed to belong to
78the same connection, are passed without ruleset inspection. 78the same connection, are passed without ruleset inspection.
79Rules may have associated rule procedures (described in a later section), 79Rules may have associated rule procedures (described in a later section),
80which are applied for all packets of a connection. 80which are applied for all packets of a connection.
81.Pp 81.Pp
82Definitions (prefixed with "$") and tables (specified by an ID within 82Definitions (prefixed with "$") and tables (specified by an ID within
83"\*[Lt]\*[Gt]" marks) can be used in the filter options of rules. 83"\*[Lt]\*[Gt]" marks) can be used in the filter options of rules.
84.Ss Rule procedures and normalisation 84.Ss Rule procedures and normalisation
85Rule procedures are provided to perform packet transformations and various 85Rule procedures are provided to perform packet transformations and various
86additional procedures on the packets. 86additional procedures on the packets.
87It should be noted that rule procedures are applied for the connections, 87It should be noted that rule procedures are applied for the connections,
88that is, both for packets which match the rule and for further packets 88that is, both for packets which match the rule and for further packets
89of the connection, which are passed without ruleset inspection. 89of the connection, which are passed without ruleset inspection.
90Currently, two facilities are supported: 90Currently, two facilities are supported:
91traffic normalisation and packet logging. 91traffic normalisation and packet logging.
92Packet normalisation has the following functionality: 92Packet normalisation has the following functionality:
93IP ID randomisation, IP_DF flag cleansing, TCP minimum TTL enforcement, 93IP ID randomisation, IP_DF flag cleansing, TCP minimum TTL enforcement,
94and maximum MSS enforcement ("MSS clamping"). 94and maximum MSS enforcement ("MSS clamping").
95If a matching rule is going to drop the packet, normalisation functions 95If a matching rule is going to drop the packet, normalisation functions
96are not performed. 96are not performed.
97Packet logging is performed both in packet passing and blocking cases. 97Packet logging is performed both in packet passing and blocking cases.
98Note that the logging interface has to be created manually, using 98Note that the logging interface has to be created manually, using
99.Xr ifconfig 8 99.Xr ifconfig 8
100routine, for example: 100routine, for example:
101.Pp 101.Pp
102ifconfig npflog0 create 102ifconfig npflog0 create
103.Ss Network address translation 103.Ss Network address translation
104Rules for address translation can be added. 104Rules for address translation can be added.
105Translation is performed on the specified interface, assigning the specified 105Translation is performed on the specified interface, assigning the specified
106address of said interface. 106address of said interface.
107Currently, three types of translation are supported: 107Currently, three types of translation are supported:
108Network Address Port Translation (NAPT) - a regular NAT, 108Network Address Port Translation (NAPT) - a regular NAT,
109also known as "outbound NAT"; 109also known as "outbound NAT";
110Port forwarding (redirection) - also known as "inbound NAT"; 110Port forwarding (redirection) - also known as "inbound NAT";
111Bi-directional NAT - a combination of inbound and outbound NAT. 111Bi-directional NAT - a combination of inbound and outbound NAT.
112.Pp 112.Pp
113Minimal filtering criteria on local network and destination are provided. 113Minimal filtering criteria on local network and destination are provided.
114Note that address translation implies routing, therefore IP forwarding 114Note that address translation implies routing, therefore IP forwarding
115is required to be enabled: 115is required to be enabled:
116net.inet.ip.forwarding = 1. 116net.inet.ip.forwarding = 1.
117See 117See
118.Xr sysctl 7 118.Xr sysctl 7
119for more details. 119for more details.
120.Ss Tables 120.Ss Tables
121Certain configurations might use very large sets of IP addresses or change 121Certain configurations might use very large sets of IP addresses or change
122sets frequently. 122sets frequently.
123Storing large IP sets in the configuration file or performing frequent 123Storing large IP sets in the configuration file or performing frequent
124reloads can have a significant performance cost. 124reloads can have a significant performance cost.
125.Pp 125.Pp
126In order to achieve high performance, NPF has tables. 126In order to achieve high performance, NPF has tables.
127NPF tables provide separate storage designed for large IP sets and frequent 127NPF tables provide separate storage designed for large IP sets and frequent
128updates without reloading the entire ruleset. 128updates without reloading the entire ruleset.
129Tables can be managed dynamically or loaded from a separate file, which 129Tables can be managed dynamically or loaded from a separate file, which
130is useful for large static tables. 130is useful for large static tables.
131There are two types of storage: "tree" (red-black tree is used) and 131There are two types of storage: "tree" (red-black tree is used) and
132"hash". 132"hash".
133.\" ----- 133.\" -----
134.Sh GRAMMAR 134.Sh GRAMMAR
 135The following is a non-formal BNF-like definition of the grammar.
 136The definition is simplified and is intended to be human readable,
 137therefore it does not strictly represent the full syntax, which
 138is more flexible.
135.Bd -literal 139.Bd -literal
136line = ( def | table | map | group | rproc ) 140; Syntax of a single line. Lines can be separated by LF (\n) or
 141; a semicolon. Comments start with a hash (#) character.
137 142
138var = $\*[Lt]name\*[Gt] 143syntax = var-def | table-def | map | group | rproc | comment
139iface = ( \*[Lt]interface\*[Gt] | var ) 144
140def = ( var "=" "{ "\*[Lt]value_1\*[Gt]", "\*[Lt]value_2\*[Gt]", ... }" | "\*[Lt]value\*[Gt]" ) 145; Variable definition. Names can be alpha-numeric, including "_" character.
141 146
142table = "table" \*[Lt]tid\*[Gt] "type" ( "hash" | "tree" ) 147var-name = "$" . string
143 ( "dynamic" | "file" \*[Lt]path\*[Gt] ) 148interface = interface-name | var-name
144 149var-def = var "=" ( var-value | "{" value *[ "," value ] "}" )
145map-di = ( "->" | "<-" | "<->" ) 150
146map-type = ( "static" | "dynamic" ) 151; Table definition. Table ID shall be numeric. Path is in the double quotes.
147map = "map" iface map-type \*[Lt]seg1\*[Gt] map-di \*[Lt]seg2\*[Gt] [ "pass" filt-opts ] 152
148 153table-id = \*[Lt]tid\*[Gt]
149rproc = "procedure" \*[Lt]name\*[Gt] procs 154table-def = "table" table-id "type" ( "hash" | "tree" )
150procs = "{" op1 \*[Lt]newline\*[Gt], op2 \*[Lt]newline\*[Gt], ... "}" 155 ( "dynamic" | "file" path )
151op = ( "log" iface | "normalise" "(" norm-opt1 "," norm-opt2 ... ")" ) 156
152norm-opt = [ "random-id" | "min-ttl" \*[Lt]num\*[Gt] | "max-mss" \*[Lt]num\*[Gt] | "no-df" ] 157; Mapping for address translation.
153 158
154group = "group" "(" ( "default" | group-opts ) ")" ruleset 159map = "map" interface ( "static" | "dynamic" )
155group-opts = [ name \*[Lt]name\*[Gt] "," ] "interface" iface [ "," ( "in" | "out" ) ] 160 net-seg ( "->" | "<-" | "<->" ) net-seg
156 161 [ "pass" filt-opts ]
157ruleset = "{" rule1 \*[Lt]newline\*[Gt], rule2 \*[Lt]newline\*[Gt], ... "}" 162
158 163; Rule procedure definition. The name should be in the double quotes.
159rule = ( "block" block-opts | "pass" ) [ "stateful" ] [ "in" | out" ] [ "final" ] 164;
160 [ "on" iface ] [ "family" fam-opt ] [ "proto" \*[Lt]protocol\*[Gt] [ proto-opts ] ] 165; Each call can have its own options in a form of key-value pairs.
161 ( "all" | filt-opts ) [ "apply" rproc ] } 166; Both key and values may be strings (either in double quotes or not)
162 167; and numbers, depending on the extension.
163fam-opt = [ "inet" | "inet6" ] 168
164block-opts = [ "return-rst" | "return-icmp" | "return" ] 169proc = "procedure" proc-name "{" *( proc-call [ new-line ] ) "}"
165filt-addr = iface | var | \*[Lt]addr/mask\*[Gt] | \*[Lt]tid\*[Gt] 170proc-opts = key " " val [ "," proc-opts ]
166port-opts = [ "port" ( \*[Lt]port-num\*[Gt] | \*[Lt]port-from\*[Gt] "-" \*[Lt]port-to\*[Gt] | var ) ] 171proc-call = call-name ":" proc-opts new-line
167filt-opts = [ "from" filt-addr [ port-opts ] ] [ "to" filt-addr [ port-opts ] ] 172
168proto-opts = [ "flags" \*[Lt]tcp_flags\*[Gt] | "icmp-type" \*[Lt]type\*[Gt] "code" \*[Lt]code\*[Gt] ] 173; Group definition and the ruleset.
 174
 175group = "group" "(" ( "default" | group-opts ) ")" "{" ruleset "}"
 176group-opts = [ "name" string ] [ "interface" interface ] [ "in" | "out" ]
 177ruleset = [ rule new-line ] [ ruleset ]
 178
 179rule = ( "block" [ block-opts ] | "pass" ) [ "stateful" ]
 180 [ "in" | out" ] [ "final" ] [ "on" iface ]
 181 [ "family" fam-opt ] [ "proto" protocol [ proto-opts ] ]
 182 ( "all" | filt-opts ) [ "apply" proc-name ]
 183
 184block-opts = "return-rst" | "return-icmp" | "return"
 185fam-opt = "inet" | "inet6"
 186proto-opts = "flags" tcp-flags [ "/" tcp-flag-mask ] |
 187 "icmp-type" type [ "code" icmp-code ]
 188
 189addr-mask = addr [ "/" mask ]
 190filt-opts = "from" filt-addr [ port-opts ] "to" filt-addr [ port-opts ]
 191filt-addr = [ interface | var-name | addr-mask | table-id | "any" ]
 192filt-port = "port" ( port-num | port-from "-" port-to | var-name )
169.Ed 193.Ed
170.\" ----- 194.\" -----
171.Sh FILES 195.Sh FILES
172.Bl -tag -width /dev/npf.conf -compact 196.Bl -tag -width /dev/npf.conf -compact
173.It Pa /dev/npf 197.It Pa /dev/npf
174control device 198control device
175.It Pa /etc/npf.conf 199.It Pa /etc/npf.conf
176default configuration file 200default configuration file
177.El 201.El
178.\" ----- 202.\" -----
179.Sh EXAMPLES 203.Sh EXAMPLES
180.Bd -literal 204.Bd -literal
181$ext_if = "wm0" 205$ext_if = "wm0"
182$int_if = "wm1" 206$int_if = "wm1"
183 207
184table <1> type hash file "/etc/npf_blacklist" 208table <1> type hash file "/etc/npf_blacklist"
185table <2> type tree dynamic 209table <2> type tree dynamic
186 210
187$services_tcp = { http, https, smtp, domain, 6000, 9022 } 211$services_tcp = { http, https, smtp, domain, 6000, 9022 }
188$services_udp = { domain, ntp, 6000 } 212$services_udp = { domain, ntp, 6000 }
189$localnet = { 10.1.1.0/24 } 213$localnet = { 10.1.1.0/24 }
190 214
191# Note: if $ext_if has multiple IP address (e.g. IPv6 as well), 215# Note: if $ext_if has multiple IP address (e.g. IPv6 as well),
192# then the translation address has to be specified explicitly. 216# then the translation address has to be specified explicitly.
193map $ext_if dynamic 10.1.1.0/24 -> $ext_if 217map $ext_if dynamic 10.1.1.0/24 -> $ext_if
194map $ext_if dynamic 10.1.1.2 port 22 <- $ext_if 9022 218map $ext_if dynamic 10.1.1.2 port 22 <- $ext_if 9022
195 219
196procedure "log" { 220procedure "log" {
197 log: npflog0 221 log: npflog0
198} 222}
199 223
200procedure "rid" { 
201 normalise: "random-id" 
202} 
203 
204group (name "external", interface $ext_if) { 224group (name "external", interface $ext_if) {
205 pass stateful out final from $ext_if apply "rid" 225 pass stateful out final from $ext_if
206 226
207 block in final from \*[Lt]1\*[Gt] 227 block in final from \*[Lt]1\*[Gt]
208 pass stateful in final family inet proto tcp to $ext_if port ssh apply "log" 228 pass stateful in final family inet proto tcp to $ext_if port ssh apply "log"
209 pass stateful in final proto tcp to $ext_if port $services_tcp 229 pass stateful in final proto tcp to $ext_if port $services_tcp
210 pass stateful in final proto udp to $ext_if port $services_udp 230 pass stateful in final proto udp to $ext_if port $services_udp
211 pass stateful in final proto tcp to $ext_if port 49151-65535 # Passive FTP 231 pass stateful in final proto tcp to $ext_if port 49151-65535 # Passive FTP
212 pass stateful in final proto udp to $ext_if port 33434-33600 # Traceroute 232 pass stateful in final proto udp to $ext_if port 33434-33600 # Traceroute
213} 233}
214 234
215group (name "internal", interface $int_if) { 235group (name "internal", interface $int_if) {
216 block in all 236 block in all
217 pass in final from \*[Lt]2\*[Gt] 237 pass in final from \*[Lt]2\*[Gt]
218 pass out final all 238 pass out final all
219} 239}
220 240
221group (default) { 241group (default) {
222 pass final on lo0 all 242 pass final on lo0 all
223 block all 243 block all
224} 244}
225.Ed 245.Ed
226.\" ----- 246.\" -----
227.Sh SEE ALSO 247.Sh SEE ALSO
228.Xr npfctl 8 , 248.Xr npfctl 8 ,
229.Xr npf_ncode 9 249.Xr npf_ncode 9
230.Sh HISTORY 250.Sh HISTORY
231NPF first appeared in 251NPF first appeared in
232.Nx 6.0 . 252.Nx 6.0 .
233.Sh AUTHORS 253.Sh AUTHORS
234NPF was designed and implemented by 254NPF was designed and implemented by
235.An Mindaugas Rasiukevicius . 255.An Mindaugas Rasiukevicius .