Thu Dec 7 00:22:06 2017 UTC ()
libnpf(3): improve the wording, fix and expand some sections.


(rmind)
diff -r1.4 -r1.5 src/lib/libnpf/libnpf.3

cvs diff -r1.4 -r1.5 src/lib/libnpf/libnpf.3 (expand / switch to unified diff)

--- src/lib/libnpf/libnpf.3 2016/12/27 21:25:12 1.4
+++ src/lib/libnpf/libnpf.3 2017/12/07 00:22:06 1.5
@@ -1,43 +1,43 @@ @@ -1,43 +1,43 @@
1.\" $NetBSD: libnpf.3,v 1.4 2016/12/27 21:25:12 wiz Exp $ 1.\" $NetBSD: libnpf.3,v 1.5 2017/12/07 00:22:06 rmind Exp $
2.\" 2.\"
3.\" Copyright (c) 2011-2015 The NetBSD Foundation, Inc. 3.\" Copyright (c) 2011-2017 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 April 19, 2015 30.Dd December 7, 2017
31.Dt LIBNPF 3 31.Dt LIBNPF 3
32.Os 32.Os
33.Sh NAME 33.Sh NAME
34.Nm libnpf 34.Nm libnpf
35.Nd NPF packet filter library 35.Nd NPF packet filter library
36.Sh LIBRARY 36.Sh LIBRARY
37.Lb libnpf 37.Lb libnpf
38.Sh SYNOPSIS 38.Sh SYNOPSIS
39.In npf.h 39.In npf.h
40.\" --- 40.\" ---
41.Ft nl_config_t * 41.Ft nl_config_t *
42.Fn npf_config_create "void" 42.Fn npf_config_create "void"
43.Ft int 43.Ft int
@@ -100,275 +100,293 @@ @@ -100,275 +100,293 @@
100.Fn npf_table_destroy "nl_table_t *tl" 100.Fn npf_table_destroy "nl_table_t *tl"
101.\" ----- 101.\" -----
102.Sh DESCRIPTION 102.Sh DESCRIPTION
103The 103The
104.Nm 104.Nm
105library provides an interface to create an NPF configuration having rules, 105library provides an interface to create an NPF configuration having rules,
106tables, procedures, or translation policies. 106tables, procedures, or translation policies.
107The configuration can be submitted to the kernel. 107The configuration can be submitted to the kernel.
108.\" ----- 108.\" -----
109.Sh FUNCTIONS 109.Sh FUNCTIONS
110.Ss Configuration 110.Ss Configuration
111.Bl -tag -width 4n 111.Bl -tag -width 4n
112.It Fn npf_config_create 112.It Fn npf_config_create
113Create a configuration. 113Create a new configuration object.
114.It Fn npf_config_submit "ncf" "fd" "errinfo" 114.It Fn npf_config_submit "ncf" "fd" "errinfo"
115Submit configuration 115Submit the configuration object, specified by
116.Fa ncf 116.Fa ncf ,
117to the kernel. 117to the kernel.
118On error, the the description is written into the structure specified by 118On failure, the error information is written into the structure
 119specified by
119.Fa errinfo . 120.Fa errinfo .
120.It Fn npf_config_export "fd" "len" 121.It Fn npf_config_export "fd" "len"
121Serialize the given configuration and return binary object and its 122Serialize the current configuration and return the binary object as
122length in 123well as its length in
123.Fa len 124.Fa len
124parameter. 125parameter.
125The binary object is dynamically allocated and should be destroyed using 126The binary object is dynamically allocated and should be destroyed using
126.Xr free 3 . 127.Xr free 3 .
127.It Fn npf_config_import "blob" "len" 128.It Fn npf_config_import "blob" "len"
128Read the configuration from a binary object of the specified length, 129Read the configuration from a binary object of the specified length,
129unserialize, construct and return the configuration object. 130unserialize, and return the configuration object.
130.It Fn npf_config_flush "fd" 131.It Fn npf_config_flush "fd"
131Flush the current configuration. 132Flush the current configuration.
132.It Fn npf_config_retrieve "fd" "active" "loaded" 133.It Fn npf_config_retrieve "fd" "active" "loaded"
133Retrieve and return the loaded configuration from the kernel. 134Retrieve and return the loaded configuration from the kernel.
134.It Fn npf_config_active_p "ncf" 135.It Fn npf_config_active_p "ncf"
135Indicate whether the retrieved configuration is active (true if yes 136Indicate whether the retrieved configuration is active (true if yes
136and false otherwise). 137and false otherwise).
137.It Fn npf_config_destroy "ncf" 138.It Fn npf_config_destroy "ncf"
138Destroy the configuration 139Destroy the configuration object, specified by
139.Fa ncf . 140.Fa ncf .
140.El 141.El
141.\" --- 142.\" ---
142.Ss Rule interface 143.Ss Rule interface
143.Bl -tag -width 4n 144.Bl -tag -width 4n
144.It Fn npf_rule_create "name" "attr" "ifname" 145.It Fn npf_rule_create "name" "attr" "ifname"
145Create a rule with a given name, attribute and priorty. 146Create a rule with a given name, attribute and priorty.
146Name can be 147If the name is specified, then it should be unique within the
 148configuration object.
 149Otherwise, the name can be
147.Dv NULL , 150.Dv NULL ,
148in which case rule has no unique identifier. 151in which case the rule will have no identifier.
149Otherwise, rules shall not have duplicate names. 
150The following attributes, which can be ORed, are available: 152The following attributes, which can be ORed, are available:
151.Bl -tag -width indent 153.Bl -tag -width indent
152.It Dv NPF_RULE_PASS 154.It Dv NPF_RULE_PASS
153Decision of this rule is "pass". 155The decision of this rule shall be "pass".
154If this attribute is not 156If this attribute is not
155specified, then packet "block" (drop) is the default. 157specified, then "block" (drop the packet) is the default.
156.It Dv NPF_RULE_IN 158.It Dv NPF_RULE_IN
157Match incoming packets. 159Match the incoming packets.
158.It Dv NPF_RULE_OUT 160.It Dv NPF_RULE_OUT
159Match outgoing packets. 161Match the outgoing packets.
160.It Dv NPF_RULE_FINAL 162.It Dv NPF_RULE_FINAL
161Indicates that on rule match, further processing of the 163Indicate that on rule match, further processing of the ruleset should
162ruleset should be stopped and this rule applied instantly. 164be stopped and this rule should be applied instantly.
163.It Dv NPF_RULE_STATEFUL 165.It Dv NPF_RULE_STATEFUL
164Create a state (session) on match, track the connection and 166Create a state (session) on match, track the connection and pass the
165therefore pass the backwards stream without inspection. 167backwards stream (the returning packets) without the ruleset inspection.
166The state is uniquely identified by a 5-tuple (source and destination 168The state is uniquely identified by a 5-tuple (source and destination
167IP addresses, port numbers and an interface identifier). 169IP addresses, port numbers and an interface identifier).
168.It Dv NPF_RULE_MULTIENDS 170.It Dv NPF_RULE_MULTIENDS
169Exclude the interface from the state identifier. 171Exclude the interface identifier from the state key i.e. use a 4-tuple.
170.It Dv NPF_RULE_RETRST 172.It Dv NPF_RULE_RETRST
171Return TCP RST packet in a case of packet block. 173Return TCP RST packet in a case of packet block.
172.It Dv NPF_RULE_RETICMP 174.It Dv NPF_RULE_RETICMP
173Return ICMP destination unreachable in a case of packet block. 175Return ICMP destination unreachable in a case of packet block.
174.It Dv NPF_RULE_GROUP 176.It Dv NPF_RULE_GROUP
175Allow this rule to have sub-rules. 177Allow this rule to have sub-rules.
176If used with 178If this flag is used with the
177.Dv NPF_RULE_DYNAMIC 179.Dv NPF_RULE_DYNAMIC
178flag set, the can be added dynamically. 180flag set, then it is a dynamic group.
 181The sub-rules can be added dynamically to a dynamic group, also meaning
 182that the sub-rules must have the
 183.Dv NPF_RULE_DYNAMIC
 184flag set.
179Otherwise rules must be added statically i.e. created with the configuration. 185Otherwise rules must be added statically i.e. created with the configuration.
180.It Dv NPF_RULE_DYNAMIC 186.It Dv NPF_RULE_DYNAMIC
181Indicate that the rule is dynamic. 187Indicate that the rule is dynamic.
 188Such rules can only be added to the dynamic groups.
182.El 189.El
183.Pp 190.Pp
184Interface is specified by 191The interface is specified by the
185.Fa ifname , 192.Fa ifname
186which is a string. 193string.
187.Dv NULL 194.Dv NULL
188indicates any interface. 195indicates any interface.
189.\" --- 196.\" ---
190.It Fn npf_rule_setcode "rl" "type" "code" "len" 197.It Fn npf_rule_setcode "rl" "type" "code" "len"
191Assign compiled code for the rule specified by 198Assign the code for the rule specified by
192.Fa rl , 199.Fa rl .
193used for filter criteria. 200The code is used to implement the filter criteria.
194Pointer to the binary code is specified by 201The pointer to the binary code is specified by
195.Fa code , 202.Fa code ,
196and size of the memory area by 203the size of the memory area by
197.Fa len . 204.Fa len ,
198Type of the code is specified by 205and the type of the code is specified by
199.Fa type . 206.Fa type .
200Currently, only BPF byte-code is supported and 207Currently, only the BPF byte-code is supported and the
201.Dv NPF_CODE_BPF 208.Dv NPF_CODE_BPF
202should be passed. 209constant should be passed.
203.\" --- 210.\" ---
204.It Fn npf_rule_setkey "rl" "type" "key" "len" 211.It Fn npf_rule_setkey "rl" "type" "key" "len"
205Assign a key for the rule specified by 212Assign a key for the rule specified by
206.Fa rl . 213.Fa rl .
207Binary key is specified by 214The binary key is specified by
208.Fa key , 215.Fa key ,
209and its size by 216and its size by
210.Fa len . 217.Fa len .
211The size shall not exceed 218The size shall not exceed
212.Dv NPF_RULE_MAXKEYLEN . 219.Dv NPF_RULE_MAXKEYLEN .
213The kernel does not validate the key is unique, it is the responsibility 220The kernel does not check whether key is unique, therefore it is the
214of the caller. 221responsibility of the caller.
215.\" --- 222.\" ---
216.It Fn npf_rule_setinfo "rl" "info" "len" 223.It Fn npf_rule_setinfo "rl" "info" "len"
217Associate arbitrary information blob specified by 224Associate an arbitrary information blob specified by
218.Fa info , 225.Fa info ,
219and its size by 226and its size by
220.Fa len . 227.Fa len .
221This may be used for such purposes as byte-code annotation. 228This may be used for such purposes as the byte-code annotation.
222.\" --- 229.\" ---
223.It Fn npf_rule_setprio "rl" "pri" 230.It Fn npf_rule_setprio "rl" "pri"
224Set priority to the rule. 231Set priority to the rule.
225Negative priorities are invalid. 232Negative priorities are invalid.
226.Pp 233.Pp
227Priority is the order of the rule in the ruleset. 234The priority is the order of the rule in the ruleset.
228Lower value means first to process, higher value - last to process. 235The lower value means first to process, the higher value - last to process.
229If multiple rules are inserted with the same priority, 236If multiple rules are inserted with the same priority,
230the order is unspecified. 237then the order is unspecified.
231.Pp 238.Pp
232The special constants 239The special constants
233.Dv NPF_PRI_FIRST 240.Dv NPF_PRI_FIRST
234and 241and
235.Dv NPF_PRI_LAST 242.Dv NPF_PRI_LAST
236can be passed to indicate that the rule should be inserted into the 243can be passed to indicate that the rule should be inserted into the
237beginning or the end of the priority level 0 in the ruleset. 244beginning or the end of the priority level 0 in the ruleset.
238All rules inserted using these constants will have the priority 0 245All rules inserted using these constants will have the priority 0
239assigned and will share this level in the ordered way. 246assigned and will share this level in the ordered way.
240.\" --- 247.\" ---
241.It Fn npf_rule_setproc "ncf" "rl" "name" 248.It Fn npf_rule_setproc "ncf" "rl" "name"
242Set a procedure for the specified rule. 249Set a procedure for the specified rule.
243.\" --- 250.\" ---
244.It Fn npf_rule_insert "ncf" "parent" "rl" 251.It Fn npf_rule_insert "ncf" "parent" "rl"
245Insert the rule into the set of parent rule specified by 252Insert the rule into the set of the parent rule specified by
246.Fa parent . 253.Fa parent .
247If value of 254If the value of
248.Fa parent 255.Fa parent
249is 256is
250.Dv NULL , 257.Dv NULL ,
251then insert into the main ruleset. 258then insert into the main ruleset.
252.\" --- 259.\" ---
253.It Fn npf_rule_export "rl" "length" 260.It Fn npf_rule_export "rl" "length"
254Serialize the rule (including the byte-code), return a binary object 261Serialize the rule (including the byte-code), return a binary object
255and set its 262and set its
256.Fa length . 263.Fa length .
257The binary object is dynamically allocated and should be destroyed using 264The binary object is dynamically allocated and should be destroyed using
258.Xr free 3 . 265.Xr free 3 .
259.\" --- 266.\" ---
260.It Fn npf_rule_destroy "rl" 267.It Fn npf_rule_destroy "rl"
261Destroy the given rule. 268Destroy the given rule object.
262.El 269.El
263.\" ----- 270.\" -----
264.Ss Rule procedure interface 271.Ss Rule procedure interface
265.Bl -tag -width 4n 272.Bl -tag -width 4n
266.It Fn npf_rproc_create "name" 273.It Fn npf_rproc_create "name"
267Create a rule procedure with a given 274Create a rule procedure with a given
268.Fa name . 275.Fa name .
269Name must be unique for each procedure. 276Thr name must be unique for each procedure.
270.It Fn npf_rproc_insert "ncf" "rp" 277.It Fn npf_rproc_insert "ncf" "rp"
271Insert rule procedure into the specified configuration. 278Insert the rule procedure into the specified configuration object.
272.El 279.El
273.\" ----- 280.\" -----
274.Ss Translation interface 281.Ss Translation interface
275.Bl -tag -width 4n 282.Bl -tag -width 4n
276.It Fn npf_nat_create "type" "flags" "ifname" "addr" "af" "port" 283.It Fn npf_nat_create "type" "flags" "ifname" "addr" "af" "port"
277Create a NAT translation policy of a specified type. 284Create a NAT policy of a specified type.
278There are two types: 285There are two types:
279.Bl -tag -width "NPF_NAT_PORTMAP " 286.Bl -tag -width "NPF_NAT_PORTMAP "
280.It Dv NPF_NATIN 287.It Dv NPF_NATIN
281Inbound NAT policy (rewrite destination). 288Inbound NAT policy (rewrite destination).
282.It Dv NPF_NATOUT 289.It Dv NPF_NATOUT
283Outbound NAT policy (rewrite source). 290Outbound NAT policy (rewrite source).
284.El 291.El
285.Pp 292.Pp
286A bi-directional NAT is obtained by combining two policies. 293A bi-directional NAT is obtained by combining two policies.
287The following 294The following
288.Fa flags 295.Fa flags
289are supported: 296are supported:
290.Bl -tag -width "NPF_NAT_PORTMAP " 297.Bl -tag -width "NPF_NAT_PORTMAP "
291.It Dv NPF_NAT_STATIC 298.It Dv NPF_NAT_STATIC
292Perform static (stateless) NAT rather than dynamic (stateful). 299Perform static (stateless) translation rather than dynamic (stateful).
293.It Dv NPF_NAT_PORTS 300.It Dv NPF_NAT_PORTS
294Indicates to perform port translation. 301Perform the port translation.
295Otherwise, port translation is not performed and 302If this flag is not specified, then the port translation is not performed
 303and the
296.Fa port 304.Fa port
297is ignored. 305parameter is ignored.
298.It Dv NPF_NAT_PORTMAP 306.It Dv NPF_NAT_PORTMAP
299Effective only if 307Create a port map and select a random port for translation.
 308If enabled, then the value specified by the
 309.Fa port
 310parameter is ignored.
 311This flag is effective only if the
300.Dv NPF_NAT_PORTS 312.Dv NPF_NAT_PORTS
301flag is set. 313flag is set.
302Indicates to create a port map and select a random port for translation. 
303Otherwise, port is translated to the value specified by 
304.Fa port 
305is used. 
306.El 314.El
307.Pp 315.Pp
308Translation address is specified by 316The translation address is specified by
309.Fa addr , 317.Fa addr ,
310and its family by 318and its family by
311.Fa af . 319.Fa af .
312Family must be either 320The family must be either
313.Dv AF_INET 321.Dv AF_INET
314for IPv4 or 322for IPv4 or
315.Dv AF_INET6 323.Dv AF_INET6
316for IPv6 address. 324for IPv6 address.
317.\" --- 325.\" ---
318.It Fn npf_nat_setalgo "nt" "algo" 326.It Fn npf_nat_setalgo "nt" "algo"
319Set a particular NAT algorithm. 327Set a particular NAT algorithm.
320Currently, only 328Currently, only
321.Dv NPF_ALGO_NPT66 329.Dv NPF_ALGO_NPT66
322algorithm is supported for NPTv6 (RFC 6296). 330algorithm is supported for NPTv6 (RFC 6296).
323.\" --- 331.\" ---
324.It Fn npf_nat_insert "ncf" "nt" "pri" 332.It Fn npf_nat_insert "ncf" "nt" "pri"
325Insert NAT policy, its rule, into the specified configuration. 333Insert NAT policy, its rule, into the specified configuration.
326.El 334.El
327.\" ----- 335.\" -----
328.Ss Table interface 336.Ss Table interface
329.Bl -tag -width 4n 337.Bl -tag -width 4n
330.It Fn npf_table_create "name" "index" "type" 338.It Fn npf_table_create "name" "index" "type"
331Create NPF table of specified type. 339Create an NPF table of a specified type.
332The following types are supported: 340The table is identified by the
333.Bl -tag -width "NPF_TABLE_TREE " 
334.It Dv NPF_TABLE_HASH 
335Indicates to use hash table for storage. 
336.It Dv NPF_TABLE_TREE 
337Indicates to use red-black tree for storage. 
338Table is identified by the 
339.Fa name 341.Fa name
340and 342and
341.Fa index , 343.Fa index ,
342which should be in the range between 1 and 344which should be in the range between 1 and
343.Dv NPF_MAX_TABLE_ID . 345.Dv NPF_MAX_TABLE_ID .
 346.Pp
 347The following types are supported:
 348.Bl -tag -width "NPF_TABLE_HASH"
 349.It Dv NPF_TABLE_HASH
 350Indicates to use a hash table for storage.
 351.It Dv NPF_TABLE_TREE
 352Indicates to use a tree for storage, supporting the longest
 353prefix match.
 354.It Dv NPF_TABLE_CDB
 355Indicates to use constant database for storage, typically using
 356a perfect hash table.
 357In such case, the database produced by
 358.Xr cdbw 3
 359should be set using the
 360.Fn npf_table_setdata
 361function.
344.El 362.El
345.\" --- 363.\" ---
346.It Fn npf_table_add_entry "tl" "af" "addr" "mask" 364.It Fn npf_table_add_entry "tl" "af" "addr" "mask"
347Add an entry of IP address and mask, specified by 365Add an entry of IP address and mask, specified by
348.Fa addr 366.Fa addr
349and 367and
350.Fa mask , 368.Fa mask ,
351to the table specified by 369to the table specified by
352.Fa tl . 370.Fa tl .
353Family, specified by 371The family, specified by
354.Fa af , 372.Fa af ,
355must be either 373must be either
356.Dv AF_INET 374.Dv AF_INET
357for IPv4 or 375for IPv4 or
358.Dv AF_INET6 376.Dv AF_INET6
359for IPv6 address. 377for IPv6 address.
360.It Fn npf_table_insert "ncf" "tl" 378.It Fn npf_table_insert "ncf" "tl"
361Insert table into set of configuration. 379Add the table to the configuration object.
362Routine performs a check for duplicate table ID. 380This routine performs a check for duplicate table IDs.
363.\" --- 381.\" ---
364.It Fn npf_table_destroy "tl" 382.It Fn npf_table_destroy "tl"
365Destroy the specified table. 383Destroy the specified table.
366.El 384.El
367.\" ----- 385.\" -----
368.Sh SEE ALSO 386.Sh SEE ALSO
369.Xr bpf 4 , 387.Xr bpf 4 ,
370.Xr npf 7 , 388.Xr npf 7 ,
371.Xr npfctl 8 389.Xr npfctl 8
372.Sh HISTORY 390.Sh HISTORY
373The NPF library first appeared in 391The NPF library first appeared in
374.Nx 6.0 . 392.Nx 6.0 .