$NetBSD: npf.conf.5,v 1.9.2.8 2013/02/11 21:49:47 riz Exp $

Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
All rights reserved.

This material is based upon work partially supported by The
NetBSD Foundation under a contract with Mindaugas Rasiukevicius.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

.Dd January 11, 2013 .Dt NPF.CONF 5 .Os .Sh NAME .Nm npf.conf .Nd NPF packet filter configuration file -----
.Sh DESCRIPTION .Nm is the default configuration file for the NPF packet filter.

p This manual page serves as a reference for editing .Nm . Please refer to the official NPF documentation for comprehensive and in-depth information.

p There are multiple structural elements .Nm may contain: .Cd variable and .Cd table definitions (with or without content), abstraction .Cd groups , packet filtering .Cd rules , .Cd map rules for address translation and .Cd procedure definitions to call on filtered packets. The minimal .Nm must contain a mandatory .Cd default group . .Sh SYNTAX .Ss Variables Variables are specified using the dollar ($) sign, which is used both in definitions and uses of a variable. Variables are defined by assigning a value to them as follows: d -literal $var1 = 10.0.0.1 .Ed

p A variable may also be defined as a set: d -literal $var2 = { 10.0.0.1, 10.0.0.2 } .Ed

p Common variable definitions are for IP addresses, networks, ports, and interfaces. .Ss Tables Tables are specified using a number between angle brackets \*[Lt] and \*[Gt]. The number used to specify a table should be between 0 and 15. The following is an example of table definition: d -literal table <1> type hash dynamic

p .Ed Currently, tables support two storage types: "hash" or "tree". They can also be "dynamic" or static i.e. loaded from the specified file.

p The file should contain a list of IP addresses and/or networks in the form of: d -literal 10.0.0.0/24 10.1.1.1 .Ed

p Tables of type "hash" can only contain IP addresses. .Ss Interfaces Interfaces can be specified as the values of the variables:

p d -literal $pub_if_list = { ifnet(wm0), ifnet(wm1) } .Ed

p In the context of filtering, an interface provides a list of its all IP addresses, including IPv4 and IPv6. Specific interface addresses can be selected by the family, e.g.: d -literal $pub_if4 = inet4(wm0) $pub_if6 = { inet6(wm0) } .Ed .Ss Groups Groups may have the following options: name, interface, and direction. They are defined in the following form:

p d -literal group (name "my_group", interface wm0, in) { # List of rules } .Ed .Ss Rules With a rule statement NPF is instructed to .Cd pass or .Cd block a packet depending on packet header information, transit direction and interface it arrives on, either immediately upon match (keyword .Cd final ) or using the last match. The rule can also instruct NPF to create an entry in the state table when passing the packet, to notify the sender when blocking it, and to apply a procedure to the packet (e.g. "log") in either case.

p A "fully-featured" rule would for example be: d -literal pass stateful in final family inet proto tcp flags S/SA \\ from $source port $sport to $dest port $dport apply "someproc" .Ed

p Any protocol in

a /etc/protocols can be specified. Further packet specification at present is limited to protocol TCP understanding flags, TCP and UDP understanding source and destination ports, and ICMP and IPv6-ICMP understanding icmp-type.

p Fragments are not selectable since NPF always reassembles packets before further processing. .Ss Map Network Address Translation (NAT) is expressed in a form of segment mapping. At present, only dynamic translation is supported. The following mapping types are available:

p l -tag -width <-> -compact t Pa -> outbound NAT (translation of the source) t Pa <- inbound NAT (translation of the destination) t Pa <-> bi-directional NAT (combination of inbound and outbound NAT) .El

p The following would translate the source to the IP address specified by the $pub_ip for the packets on the interface $ext_if. d -literal map $ext_if dynamic 10.1.1.0/24 -> $pub_ip .Ed

p Translations are implicitly filtered by limiting the operation to the network segments specified, that is, translation would be performed only on packets originating from 10.1.1.0/24 network. Explicit filter criteria can be specified using "pass <criteria>" as an additional option of the mapping. .Ss Procedures A rule procedure is defined as a collection of extension calls (it may have none). Every extension call has a name and a list of options in the form of key-value pairs. Depending on the call, the key might represent the argument and the value might be optional. For example: d -literal procedure "someproc" { log: npflog0 normalise: "random-id", "min-ttl" 64 } .Ed

p In this case, the procedure calls the logging and normalisation modules. .Ss Misc Text after a hash

q Sq # character is considered a comment. The backslash

q Sq \e character at the end of a line marks a continuation line, i.e., the next line is considered an extension of the present line. .Sh GRAMMAR The following is a non-formal BNF-like definition of the grammar. The definition is simplified and is intended to be human readable, therefore it does not strictly represent the full syntax, which is more flexible. d -literal ; Syntax of a single line. Lines can be separated by LF (\\n) or ; a semicolon. Comments start with a hash (#) character. syntax = var-def | table-def | map | group | rproc | comment ; Variable definition. Names can be alpha-numeric, including "_" character. var-name = "$" . string interface = interface-name | var-name var-def = var "=" ( var-value | "{" value *[ "," value ] "}" ) ; Table definition. Table ID shall be numeric. Path is in the double quotes. table-id = \*[Lt]tid\*[Gt] table-def = "table" table-id "type" ( "hash" | "tree" ) ( "dynamic" | "file" path ) ; Mapping for address translation. map = "map" interface ( "static" | "dynamic" ) net-seg ( "->" | "<-" | "<->" ) net-seg [ "pass" filt-opts ] ; Rule procedure definition. The name should be in the double quotes. ; ; Each call can have its own options in a form of key-value pairs. ; Both key and values may be strings (either in double quotes or not) ; and numbers, depending on the extension. proc = "procedure" proc-name "{" *( proc-call [ new-line ] ) "}" proc-opts = key " " val [ "," proc-opts ] proc-call = call-name ":" proc-opts new-line ; Group definition and the ruleset. group = "group" "(" ( "default" | group-opts ) ")" "{" ruleset "}" group-opts = [ "name" string ] [ "interface" interface ] [ "in" | "out" ] ruleset = [ rule new-line ] [ ruleset ] rule = ( "block" [ block-opts ] | "pass" ) [ "stateful" ] [ "in" | out" ] [ "final" ] [ "on" iface ] [ "family" fam-opt ] [ "proto" protocol [ proto-opts ] ] ( "all" | filt-opts ) [ "apply" proc-name ] block-opts = "return-rst" | "return-icmp" | "return" fam-opt = "inet" | "inet6" proto-opts = "flags" tcp-flags [ "/" tcp-flag-mask ] | "icmp-type" type [ "code" icmp-code ] addr-mask = addr [ "/" mask ] filt-opts = "from" filt-addr [ port-opts ] "to" filt-addr [ port-opts ] filt-addr = [ interface | var-name | addr-mask | table-id | "any" ] filt-port = "port" ( port-num | port-from "-" port-to | var-name ) .Ed -----
.Sh FILES l -tag -width /usr/share/examples/npf -compact t Pa /dev/npf control device t Pa /etc/npf.conf default configuration file t Pa /usr/share/examples/npf directory containing further examples .El -----
.Sh EXAMPLES d -literal $ext_if = ifnet(wm0) $int_if = ifnet(wm1) table <1> type hash file "/etc/npf_blacklist" table <2> type tree dynamic $services_tcp = { http, https, smtp, domain, 6000, 9022 } $services_udp = { domain, ntp, 6000 } $localnet = { 10.1.1.0/24 } # Note: if $ext_if has multiple IP address (e.g. IPv6 as well), # then the translation address has to be specified explicitly. map $ext_if dynamic 10.1.1.0/24 -> $ext_if map $ext_if dynamic 10.1.1.2 port 22 <- $ext_if port 9022 procedure "log" { # Note: npf_ext_log kernel module should be loaded, if not built-in. # Also, the interface created, e.g.: ifconfig npflog0 create log: npflog0 } group (name "external", interface $ext_if) { pass stateful out final all block in final from \*[Lt]1\*[Gt] pass stateful in final family inet proto tcp to $ext_if port ssh apply "log" pass stateful in final proto tcp to $ext_if port $services_tcp pass stateful in final proto udp to $ext_if port $services_udp pass stateful in final proto tcp to $ext_if port 49151-65535 # Passive FTP pass stateful in final proto udp to $ext_if port 33434-33600 # Traceroute } group (name "internal", interface $int_if) { block in all pass in final from \*[Lt]2\*[Gt] pass out final all } group (default) { pass final on lo0 all block all } .Ed -----
.Sh SEE ALSO .Xr npfctl 8 , .Xr npf_ncode 9 .Sh HISTORY NPF first appeared in .Nx 6.0 . .Sh AUTHORS NPF was designed and implemented by .An Mindaugas Rasiukevicius .