2014-02-03 06:21:52 +04:00
|
|
|
.\" $NetBSD: npf.3,v 1.15 2014/02/03 02:21:52 rmind Exp $
|
2011-02-02 05:20:24 +03:00
|
|
|
.\"
|
2013-02-09 07:35:31 +04:00
|
|
|
.\" Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
|
2011-02-02 05:20:24 +03:00
|
|
|
.\" 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.
|
|
|
|
.\"
|
2014-02-03 06:21:52 +04:00
|
|
|
.Dd February 2, 2014
|
2011-02-02 05:20:24 +03:00
|
|
|
.Dt NPF 3
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm npf
|
|
|
|
.Nd NPF packet filter library
|
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libnpf
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.In npf.h
|
|
|
|
.\" ---
|
|
|
|
.Ft nl_config_t *
|
|
|
|
.Fn npf_config_create "void"
|
|
|
|
.Ft int
|
|
|
|
.Fn npf_config_submit "nl_config_t *ncf" "int fd"
|
|
|
|
.Ft void
|
|
|
|
.Fn npf_config_destroy "nl_config_t *ncf"
|
2012-01-15 04:49:47 +04:00
|
|
|
.Ft int
|
|
|
|
.Fn npf_config_flush "int fd"
|
2011-02-02 05:20:24 +03:00
|
|
|
.\" ---
|
|
|
|
.Ft nl_rule_t *
|
2013-11-08 04:38:26 +04:00
|
|
|
.Fn npf_rule_create "char *name" "uint32_t attr" "const char *ifname"
|
2011-02-02 05:20:24 +03:00
|
|
|
.Ft int
|
2013-02-09 07:35:31 +04:00
|
|
|
.Fn npf_rule_setcode "nl_rule_t *rl" "int type" "const void *code" "size_t len"
|
|
|
|
.Ft int
|
|
|
|
.Fn npf_rule_setkey "nl_rule_t *rl" "int type" "const void *code" "size_t len"
|
2011-02-02 05:20:24 +03:00
|
|
|
.Ft bool
|
|
|
|
.Fn npf_rule_exists_p "nl_config_t *ncf" "const char *name"
|
|
|
|
.Ft int
|
2014-01-08 14:14:21 +04:00
|
|
|
.Fn npf_rule_insert "nl_config_t *ncf" "nl_rule_t *parent" "nl_rule_t *rl"
|
2013-02-09 07:35:31 +04:00
|
|
|
.Ft int
|
|
|
|
.Fn npf_rule_setprio "nl_rule_t *rl" "pri_t pri"
|
2011-02-02 05:20:24 +03:00
|
|
|
.Ft int
|
|
|
|
.Fn npf_rule_setproc "nl_config_t *ncf" "nl_rule_t *rl" "const char *name"
|
|
|
|
.Ft void
|
|
|
|
.Fn npf_rule_destroy "nl_rule_t *rl"
|
|
|
|
.\" ---
|
|
|
|
.Ft nl_rproc_t *
|
|
|
|
.Fn npf_rproc_create "char *name"
|
|
|
|
.Ft bool
|
|
|
|
.Fn npf_rproc_exists_p "nl_config_t *ncf" "const char *name"
|
|
|
|
.Ft int
|
|
|
|
.Fn npf_rproc_insert "nl_config_t *ncf" "nl_rproc_t *rp"
|
|
|
|
.\" ---
|
|
|
|
.Ft nl_nat_t *
|
2013-11-08 04:38:26 +04:00
|
|
|
.Fn npf_nat_create "int type" "u_int flags" "const char *ifname" \
|
2011-02-02 05:20:24 +03:00
|
|
|
"npf_addr_t *addr" "int af" "in_port_t port"
|
|
|
|
.Ft int
|
|
|
|
.Fn npf_nat_insert "nl_config_t *ncf" "nl_nat_t *nt" "pri_t pri"
|
|
|
|
.\" ---
|
|
|
|
.Ft nl_table_t *
|
2013-11-12 10:06:21 +04:00
|
|
|
.Fn npf_table_create "const char *name" "u_int id" "int type"
|
2011-02-02 05:20:24 +03:00
|
|
|
.Ft int
|
2012-12-24 04:35:56 +04:00
|
|
|
.Fn npf_table_add_entry "nl_table_t *tl" "int af" \
|
2012-12-24 01:01:03 +04:00
|
|
|
"in_addr_t addr" "in_addr_t mask"
|
2011-02-02 05:20:24 +03:00
|
|
|
.Ft int
|
|
|
|
.Fn npf_table_insert "nl_config_t *ncf" "nl_table_t *tl"
|
|
|
|
.Ft void
|
|
|
|
.Fn npf_table_destroy "nl_table_t *tl"
|
|
|
|
.\" -----
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
library provides an interface to create an NPF configuration having rules,
|
|
|
|
tables, procedures, or translation policies.
|
|
|
|
The configuration can be submitted to the kernel.
|
|
|
|
.\" -----
|
2011-03-22 10:28:41 +03:00
|
|
|
.Sh FUNCTIONS
|
|
|
|
.Ss Configuration
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width 4n
|
|
|
|
.It Fn npf_config_create
|
|
|
|
Create a configuration.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_config_submit "ncf" "fd"
|
|
|
|
Submit configuration
|
|
|
|
.Fa ncf
|
|
|
|
to the kernel.
|
|
|
|
.It Fn npf_config_destroy "ncf"
|
|
|
|
Destroy the configuration
|
|
|
|
.Fa ncf .
|
2012-01-15 04:49:47 +04:00
|
|
|
.It Fn npf_config_flush "fd"
|
|
|
|
Flush the current configuration.
|
2011-02-02 05:20:24 +03:00
|
|
|
.El
|
|
|
|
.\" ---
|
2011-03-22 10:28:41 +03:00
|
|
|
.Ss Rule interface
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width 4n
|
2013-11-08 04:38:26 +04:00
|
|
|
.It Fn npf_rule_create "name" "attr" "ifname"
|
2011-02-02 12:05:01 +03:00
|
|
|
Create a rule with a given name, attribute and priorty.
|
|
|
|
Name can be
|
|
|
|
.Dv NULL ,
|
|
|
|
in which case rule has no unique identifier.
|
|
|
|
Otherwise, rules shall not have duplicate names.
|
|
|
|
The following attributes, which can be ORed, are available:
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width indent
|
|
|
|
.It Dv NPF_RULE_PASS
|
2011-02-02 12:05:01 +03:00
|
|
|
Decision of this rule is "pass".
|
|
|
|
If this attribute is not
|
2011-02-02 05:20:24 +03:00
|
|
|
specified, then packet "block" (drop) is the default.
|
|
|
|
.It Dv NPF_RULE_FINAL
|
|
|
|
Indicates that on rule match, further processing of the
|
|
|
|
ruleset should be stopped and this rule applied instantly.
|
2012-07-02 03:21:06 +04:00
|
|
|
.It Dv NPF_RULE_STATEFUL
|
2011-02-02 05:20:24 +03:00
|
|
|
Create a state (session) on match, track the connection and
|
|
|
|
therefore pass the backwards stream without inspection.
|
|
|
|
.It Dv NPF_RULE_RETRST
|
|
|
|
Return TCP RST packet in a case of packet block.
|
|
|
|
.It Dv NPF_RULE_RETICMP
|
|
|
|
Return ICMP destination unreachable in a case of packet block.
|
|
|
|
.It Dv NPF_RULE_IN
|
|
|
|
Rule may match only if incoming packet.
|
|
|
|
.It Dv NPF_RULE_OUT
|
|
|
|
Rule may match only if outgoing packet.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
Interface is specified by
|
2013-11-08 04:38:26 +04:00
|
|
|
.Fa ifname ,
|
|
|
|
which is a string.
|
2013-11-08 17:17:45 +04:00
|
|
|
.Dv NULL
|
|
|
|
indicates any interface.
|
2011-02-02 05:20:24 +03:00
|
|
|
.\" ---
|
2013-02-09 07:35:31 +04:00
|
|
|
.It Fn npf_rule_setcode "rl" "type" "code" "len"
|
2011-02-02 05:20:24 +03:00
|
|
|
Assign compiled code for the rule specified by
|
|
|
|
.Fa rl ,
|
|
|
|
used for filter criteria.
|
|
|
|
Pointer to the binary code is specified by
|
|
|
|
.Fa code ,
|
|
|
|
and size of the memory area by
|
2013-02-09 07:35:31 +04:00
|
|
|
.Fa len .
|
2011-02-02 05:20:24 +03:00
|
|
|
Type of the code is specified by
|
|
|
|
.Fa type .
|
|
|
|
Currently, only n-code is supported and
|
2013-02-09 07:35:31 +04:00
|
|
|
.Dv NPF_CODE_NC
|
2011-02-02 05:20:24 +03:00
|
|
|
should be passed.
|
|
|
|
.\" ---
|
2013-02-09 07:35:31 +04:00
|
|
|
.It Fn npf_rule_setkey "rl" "type" "key" "len"
|
|
|
|
Assign a key for the rule specified by
|
|
|
|
.Fa rl .
|
|
|
|
Binary key is specified by
|
|
|
|
.Fa key ,
|
|
|
|
and its size by
|
|
|
|
.Fa len .
|
|
|
|
The size shall not exceed
|
|
|
|
.Dv NPF_RULE_MAXKEYLEN .
|
|
|
|
.\" ---
|
|
|
|
.It Fn npf_rule_insert "ncf" "parent" "rl"
|
2011-02-02 05:20:24 +03:00
|
|
|
Insert the rule into the set of parent rule specified by
|
|
|
|
.Fa parent .
|
|
|
|
If value of
|
|
|
|
.Fa parent
|
2011-02-02 12:05:01 +03:00
|
|
|
is
|
|
|
|
.Dv NULL ,
|
2011-02-02 05:20:24 +03:00
|
|
|
then insert into the main ruleset.
|
2013-02-09 07:35:31 +04:00
|
|
|
.\" ---
|
|
|
|
.It Fn npf_rule_setprio "rl" "pri"
|
|
|
|
Set priority to the rule.
|
|
|
|
Negative priorities are invalid.
|
2011-02-02 05:20:24 +03:00
|
|
|
.Pp
|
2011-02-02 12:05:01 +03:00
|
|
|
Priority is the order of the rule in the ruleset.
|
|
|
|
Lower value means first to process, higher value - last to process.
|
2013-02-09 07:35:31 +04:00
|
|
|
If multiple rules are inserted with the same priority,
|
|
|
|
the order is unspecified.
|
|
|
|
.Pp
|
|
|
|
The special constants
|
|
|
|
.Dv NPF_PRI_FIRST
|
|
|
|
and
|
|
|
|
.Dv NPF_PRI_LAST
|
|
|
|
can be passed to indicate that the rule should be inserted into the
|
|
|
|
beginning or the end of the priority level 0 in the ruleset.
|
|
|
|
All rules inserted using these constants will have the priority 0
|
|
|
|
assigned and will share this level in the ordered way.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_rule_setproc "ncf" "rl" "name"
|
2013-02-09 07:35:31 +04:00
|
|
|
Set a procedure for the specified rule.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_rule_destroy "rl"
|
2011-02-02 05:20:24 +03:00
|
|
|
Destroy the given rule.
|
|
|
|
.El
|
|
|
|
.\" -----
|
2011-03-22 10:28:41 +03:00
|
|
|
.Ss Rule procedure interface
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width 4n
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_rproc_create "name"
|
2011-02-02 05:20:24 +03:00
|
|
|
Create a rule procedure with a given
|
|
|
|
.Fa name .
|
|
|
|
Name must be unique for each procedure.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_rproc_insert "ncf" "rp"
|
2011-02-02 05:20:24 +03:00
|
|
|
Insert rule procedure into the specified configuration.
|
|
|
|
.El
|
|
|
|
.\" -----
|
2011-03-22 10:28:41 +03:00
|
|
|
.Ss Translation interface
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width 4n
|
2013-11-08 04:38:26 +04:00
|
|
|
.It Fn npf_nat_create "type" "flags" "ifname" "addr" "af" "port"
|
2011-02-02 05:20:24 +03:00
|
|
|
Create a NAT translation policy of a specified type.
|
|
|
|
There are two types:
|
2011-03-22 10:28:41 +03:00
|
|
|
.Bl -tag -width "NPF_NAT_PORTMAP "
|
2011-02-02 05:20:24 +03:00
|
|
|
.It Dv NPF_NATIN
|
|
|
|
Inbound NAT policy.
|
|
|
|
.It Dv NPF_NATOUT
|
|
|
|
Outbound NAT policy.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
A bi-directional NAT is obtained by combining two policies.
|
2011-03-22 10:28:41 +03:00
|
|
|
The following
|
|
|
|
.Fa flags
|
|
|
|
are supported:
|
|
|
|
.Bl -tag -width "NPF_NAT_PORTMAP "
|
2011-02-02 05:20:24 +03:00
|
|
|
.It Dv NPF_NAT_PORTS
|
|
|
|
Indicates to perform port translation.
|
|
|
|
Otherwise, port translation is not performed and
|
|
|
|
.Fa port
|
|
|
|
is ignored.
|
|
|
|
.It Dv NPF_NAT_PORTMAP
|
|
|
|
Effective only if
|
|
|
|
.Dv NPF_NAT_PORTS
|
|
|
|
flag is set.
|
|
|
|
Indicates to create a port map and select a random port for translation.
|
|
|
|
Otherwise, port is translated to the value specified by
|
|
|
|
.Fa port
|
|
|
|
is used.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
Translation address is specified by
|
|
|
|
.Fa addr ,
|
|
|
|
and its family by
|
2012-12-24 04:35:56 +04:00
|
|
|
.Fa af .
|
2011-02-02 05:20:24 +03:00
|
|
|
Family must be either
|
|
|
|
.Dv AF_INET
|
|
|
|
for IPv4 or
|
|
|
|
.Dv AF_INET6
|
|
|
|
for IPv6 address.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_nat_insert "ncf" "nt" "pri"
|
2011-02-02 05:20:24 +03:00
|
|
|
Insert NAT policy, its rule, into the specified configuration.
|
|
|
|
.El
|
|
|
|
.\" -----
|
2011-03-22 10:28:41 +03:00
|
|
|
.Ss Table interface
|
2011-02-02 05:20:24 +03:00
|
|
|
.Bl -tag -width 4n
|
2013-11-12 04:46:34 +04:00
|
|
|
.It Fn npf_table_create "name" "index" "type"
|
2011-02-02 05:20:24 +03:00
|
|
|
Create NPF table of specified type.
|
|
|
|
The following types are supported:
|
2012-01-15 04:49:47 +04:00
|
|
|
.Bl -tag -width "NPF_TABLE_TREE "
|
2011-02-02 05:20:24 +03:00
|
|
|
.It Dv NPF_TABLE_HASH
|
|
|
|
Indicates to use hash table for storage.
|
2012-01-15 04:49:47 +04:00
|
|
|
.It Dv NPF_TABLE_TREE
|
2011-02-02 05:20:24 +03:00
|
|
|
Indicates to use red-black tree for storage.
|
2013-11-12 04:46:34 +04:00
|
|
|
Table is identified by the
|
|
|
|
.Fa name
|
|
|
|
and
|
2011-02-02 05:20:24 +03:00
|
|
|
.Fa index ,
|
|
|
|
which should be in the range between 1 and
|
|
|
|
.Dv NPF_MAX_TABLE_ID .
|
|
|
|
.El
|
2012-12-24 01:01:03 +04:00
|
|
|
.It Fn npf_table_add_entry "tl" "af" "addr" "mask"
|
|
|
|
Add an entry of IP address and mask, specified by
|
2011-02-02 05:20:24 +03:00
|
|
|
.Fa addr
|
|
|
|
and
|
|
|
|
.Fa mask ,
|
|
|
|
to the table specified by
|
|
|
|
.Fa tl .
|
2012-12-24 01:01:03 +04:00
|
|
|
Family, specified by
|
2012-12-24 04:35:56 +04:00
|
|
|
.Fa af ,
|
2012-12-24 01:01:03 +04:00
|
|
|
must be either
|
|
|
|
.Dv AF_INET
|
|
|
|
for IPv4 or
|
|
|
|
.Dv AF_INET6
|
|
|
|
for IPv6 address.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_table_insert "ncf" "tl"
|
2011-02-02 05:20:24 +03:00
|
|
|
Insert table into set of configuration.
|
|
|
|
Routine performs a check for duplicate table ID.
|
2011-03-22 10:28:41 +03:00
|
|
|
.It Fn npf_table_destroy "tl"
|
2011-02-02 05:20:24 +03:00
|
|
|
Destroy the specified table.
|
|
|
|
.El
|
|
|
|
.\" -----
|
|
|
|
.Sh SEE ALSO
|
2013-09-19 21:29:06 +04:00
|
|
|
.Xr bpf 4 ,
|
|
|
|
.Xr npfctl 8
|
2011-02-02 05:20:24 +03:00
|
|
|
.Sh HISTORY
|
2011-02-02 12:05:01 +03:00
|
|
|
The NPF library first appeared in
|
2011-02-02 05:20:24 +03:00
|
|
|
.Nx 6.0 .
|