1*b899bfd9Srmind.\" $NetBSD: libnpf.3,v 1.12 2020/05/30 14:16:56 rmind Exp $ 2e92a47d1Schristos.\" 3dadc88e3Srmind.\" Copyright (c) 2011-2019 The NetBSD Foundation, Inc. 4e92a47d1Schristos.\" All rights reserved. 5e92a47d1Schristos.\" 6e92a47d1Schristos.\" This material is based upon work partially supported by The 7e92a47d1Schristos.\" NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 8e92a47d1Schristos.\" 9e92a47d1Schristos.\" Redistribution and use in source and binary forms, with or without 10e92a47d1Schristos.\" modification, are permitted provided that the following conditions 11e92a47d1Schristos.\" are met: 12e92a47d1Schristos.\" 1. Redistributions of source code must retain the above copyright 13e92a47d1Schristos.\" notice, this list of conditions and the following disclaimer. 14e92a47d1Schristos.\" 2. Redistributions in binary form must reproduce the above copyright 15e92a47d1Schristos.\" notice, this list of conditions and the following disclaimer in the 16e92a47d1Schristos.\" documentation and/or other materials provided with the distribution. 17e92a47d1Schristos.\" 18e92a47d1Schristos.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19e92a47d1Schristos.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20e92a47d1Schristos.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21e92a47d1Schristos.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22e92a47d1Schristos.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23e92a47d1Schristos.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24e92a47d1Schristos.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25e92a47d1Schristos.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26e92a47d1Schristos.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27e92a47d1Schristos.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28e92a47d1Schristos.\" POSSIBILITY OF SUCH DAMAGE. 29e92a47d1Schristos.\" 30*b899bfd9Srmind.Dd May 30, 2020 31e92a47d1Schristos.Dt LIBNPF 3 32e92a47d1Schristos.Os 33e92a47d1Schristos.Sh NAME 34e92a47d1Schristos.Nm libnpf 35e92a47d1Schristos.Nd NPF packet filter library 36e92a47d1Schristos.Sh LIBRARY 37e92a47d1Schristos.Lb libnpf 38e92a47d1Schristos.Sh SYNOPSIS 39e92a47d1Schristos.In npf.h 40e92a47d1Schristos.\" --- 41e92a47d1Schristos.Ft nl_config_t * 42e92a47d1Schristos.Fn npf_config_create "void" 43e92a47d1Schristos.Ft int 440e1944daSrmind.Fn npf_config_submit "nl_config_t *ncf" "int fd" "npf_error_t *errinfo" 455e66d73dSrmind.Ft nl_config_t * 463d9a792dSrmind.Fn npf_config_retrieve "int fd" 47e92a47d1Schristos.Ft int 48e92a47d1Schristos.Fn npf_config_flush "int fd" 495e66d73dSrmind.Ft void 503d9a792dSrmind.Fn npf_config_export "nl_config_t *ncf" "size_t *len" 515e66d73dSrmind.Ft nl_config_t * 525e66d73dSrmind.Fn npf_config_import "const void *blob" "size_t len" 535e66d73dSrmind.Ft bool 545e66d73dSrmind.Fn npf_config_active_p "nl_config_t *ncf" 553d9a792dSrmind.Ft bool 563d9a792dSrmind.Fn npf_config_loaded_p "nl_config_t *ncf" 575e66d73dSrmind.Ft void 585e66d73dSrmind.Fn npf_config_destroy "nl_config_t *ncf" 59e92a47d1Schristos.\" --- 60e92a47d1Schristos.Ft nl_rule_t * 613d9a792dSrmind.Fn npf_rule_create "const char *name" "uint32_t attr" "const char *ifname" 62e92a47d1Schristos.Ft int 63e92a47d1Schristos.Fn npf_rule_setcode "nl_rule_t *rl" "int type" "const void *code" "size_t len" 64e92a47d1Schristos.Ft int 653d9a792dSrmind.Fn npf_rule_setkey "nl_rule_t *rl" "const void *key" "size_t len" 665e66d73dSrmind.Ft int 675e66d73dSrmind.Fn npf_rule_setinfo "nl_rule_t *rl" "const void *info" "size_t len" 68e92a47d1Schristos.Ft int 695e66d73dSrmind.Fn npf_rule_setprio "nl_rule_t *rl" "int pri" 70e92a47d1Schristos.Ft int 713d9a792dSrmind.Fn npf_rule_setproc "nl_rule_t *rl" "const char *name" 725e66d73dSrmind.Ft int 735e66d73dSrmind.Fn npf_rule_insert "nl_config_t *ncf" "nl_rule_t *parent" "nl_rule_t *rl" 743d9a792dSrmind.Ft bool 753d9a792dSrmind.Fn npf_rule_exists_p "nl_config_t *ncf" "const char *name" 765e66d73dSrmind.Ft void * 775e66d73dSrmind.Fn npf_rule_export "nl_rule_t *rl" "size_t *length" 78e92a47d1Schristos.Ft void 79e92a47d1Schristos.Fn npf_rule_destroy "nl_rule_t *rl" 80e92a47d1Schristos.\" --- 81e92a47d1Schristos.Ft nl_rproc_t * 823d9a792dSrmind.Fn npf_rproc_create "const char *name" 833d9a792dSrmind.Ft int 843d9a792dSrmind.Fn npf_rproc_extcall "nl_rproc_t *rp" "nl_ext_t *ext" 85e92a47d1Schristos.Ft bool 86e92a47d1Schristos.Fn npf_rproc_exists_p "nl_config_t *ncf" "const char *name" 87e92a47d1Schristos.Ft int 88e92a47d1Schristos.Fn npf_rproc_insert "nl_config_t *ncf" "nl_rproc_t *rp" 89e92a47d1Schristos.\" --- 90e92a47d1Schristos.Ft nl_nat_t * 913d9a792dSrmind.Fn npf_nat_create "int type" "unsigned flags" "const char *ifname" 923d9a792dSrmind.Ft int 933d9a792dSrmind.Fn npf_nat_setaddr "nl_nat_t *nt" "int af" "npf_addr_t *addr" \ 943d9a792dSrmind"npf_netmask_t mask" 953d9a792dSrmind.Ft int 963d9a792dSrmind.Fn npf_nat_setport "nl_nat_t *nt" "in_port_t port" 97e92a47d1Schristos.Ft int 98dadc88e3Srmind.Fn npf_nat_insert "nl_config_t *ncf" "nl_nat_t *nt" 99e92a47d1Schristos.\" --- 100e92a47d1Schristos.Ft nl_table_t * 101dadc88e3Srmind.Fn npf_table_create "const char *name" "unsigned id" "int type" 102e92a47d1Schristos.Ft int 103e92a47d1Schristos.Fn npf_table_add_entry "nl_table_t *tl" "int af" \ 1043d9a792dSrmind"const npf_addr_t *addr" "const npf_netmask_t mask" 105e92a47d1Schristos.Ft int 106e92a47d1Schristos.Fn npf_table_insert "nl_config_t *ncf" "nl_table_t *tl" 1070e1944daSrmind.Ft int 1080e1944daSrmind.Fn npf_table_replace "int fd" "nl_table_t *tl" "npf_error_t *errinfo" 109e92a47d1Schristos.Ft void 110e92a47d1Schristos.Fn npf_table_destroy "nl_table_t *tl" 11104cb50acSrmind.\" --- 11204cb50acSrmind.Ft int 11304cb50acSrmind.Fn npf_ruleset_add "int fd" "const char *name" "nl_rule_t *rl" "uint64_t *id" 11404cb50acSrmind.Ft int 11504cb50acSrmind.Fn npf_ruleset_remove "int fd" "const char *name" "uint64_t id" 11604cb50acSrmind.Ft int 11704cb50acSrmind.Fn npf_ruleset_remkey "int fd" "const char *name" "const void *key" "size_t len" 11804cb50acSrmind.Ft int 11904cb50acSrmind.Fn npf_ruleset_flush "int fd" "const char *name" 120e92a47d1Schristos.\" ----- 121e92a47d1Schristos.Sh DESCRIPTION 122e92a47d1SchristosThe 123e92a47d1Schristos.Nm 124e92a47d1Schristoslibrary provides an interface to create an NPF configuration having rules, 125e92a47d1Schristostables, procedures, or translation policies. 126e92a47d1SchristosThe configuration can be submitted to the kernel. 127e92a47d1Schristos.\" ----- 128e92a47d1Schristos.Sh FUNCTIONS 129e92a47d1Schristos.Ss Configuration 130e92a47d1Schristos.Bl -tag -width 4n 131e92a47d1Schristos.It Fn npf_config_create 132f24cbc0aSrmindCreate a new configuration object. 1335e66d73dSrmind.It Fn npf_config_submit "ncf" "fd" "errinfo" 134f24cbc0aSrmindSubmit the configuration object, specified by 135f24cbc0aSrmind.Fa ncf , 136e92a47d1Schristosto the kernel. 137f24cbc0aSrmindOn failure, the error information is written into the structure 138f24cbc0aSrmindspecified by 1395e66d73dSrmind.Fa errinfo . 1403d9a792dSrmind.It Fn npf_config_export "ncf" "len" 1413d9a792dSrmindSerialize the given configuration and return the binary object as 142f24cbc0aSrmindwell as its length in 1435e66d73dSrmind.Fa len 1445e66d73dSrmindparameter. 1455e66d73dSrmindThe binary object is dynamically allocated and should be destroyed using 1465e66d73dSrmind.Xr free 3 . 1475e66d73dSrmind.It Fn npf_config_import "blob" "len" 1485e66d73dSrmindRead the configuration from a binary object of the specified length, 149f24cbc0aSrmindunserialize, and return the configuration object. 1505e66d73dSrmind.It Fn npf_config_flush "fd" 1515e66d73dSrmindFlush the current configuration. 1523d9a792dSrmind.It Fn npf_config_retrieve "fd" 1535e66d73dSrmindRetrieve and return the loaded configuration from the kernel. 1545e66d73dSrmind.It Fn npf_config_active_p "ncf" 1553d9a792dSrmindIndicate whether the retrieved configuration is active i.e. packet 1563d9a792dSrmindfiltering is enabled (true if yes and false otherwise). 1573d9a792dSrmind.It Fn npf_config_loaded_p "ncf" 1583d9a792dSrmindIndicate whether the retrieved configuration is loaded i.e. has any 1593d9a792dSrmindrules (true if yes and false otherwise). 160e92a47d1Schristos.It Fn npf_config_destroy "ncf" 161f24cbc0aSrmindDestroy the configuration object, specified by 162e92a47d1Schristos.Fa ncf . 163e92a47d1Schristos.El 164e92a47d1Schristos.\" --- 165e92a47d1Schristos.Ss Rule interface 166e92a47d1Schristos.Bl -tag -width 4n 167e92a47d1Schristos.It Fn npf_rule_create "name" "attr" "ifname" 1683d9a792dSrmindCreate a rule with a given name, attributes and priority. 169f24cbc0aSrmindIf the name is specified, then it should be unique within the 170f24cbc0aSrmindconfiguration object. 171f24cbc0aSrmindOtherwise, the name can be 172e92a47d1Schristos.Dv NULL , 173f24cbc0aSrmindin which case the rule will have no identifier. 174e92a47d1SchristosThe following attributes, which can be ORed, are available: 175e92a47d1Schristos.Bl -tag -width indent 176e92a47d1Schristos.It Dv NPF_RULE_PASS 177f24cbc0aSrmindThe decision of this rule shall be "pass". 178e92a47d1SchristosIf this attribute is not 179f24cbc0aSrmindspecified, then "block" (drop the packet) is the default. 1805e66d73dSrmind.It Dv NPF_RULE_IN 181f24cbc0aSrmindMatch the incoming packets. 1825e66d73dSrmind.It Dv NPF_RULE_OUT 183f24cbc0aSrmindMatch the outgoing packets. 184e92a47d1Schristos.It Dv NPF_RULE_FINAL 185f24cbc0aSrmindIndicate that on rule match, further processing of the ruleset should 186f24cbc0aSrmindbe stopped and this rule should be applied instantly. 187e92a47d1Schristos.It Dv NPF_RULE_STATEFUL 188f24cbc0aSrmindCreate a state (session) on match, track the connection and pass the 189f24cbc0aSrmindbackwards stream (the returning packets) without the ruleset inspection. 190*b899bfd9SrmindThe state is uniquely identified by an n-tuple key. 191dadc88e3Srmind.It Dv NPF_RULE_GSTATEFUL 192*b899bfd9SrmindExclude the interface identifier from the state key (n-tuple). 193eed20ae1SwizThis makes the state global with respect to the network interfaces. 194*b899bfd9SrmindThe state is also picked for packets travelling in different direction 195*b899bfd9Srmindthan originally. 196e92a47d1Schristos.It Dv NPF_RULE_RETRST 197e92a47d1SchristosReturn TCP RST packet in a case of packet block. 198e92a47d1Schristos.It Dv NPF_RULE_RETICMP 199e92a47d1SchristosReturn ICMP destination unreachable in a case of packet block. 2005e66d73dSrmind.It Dv NPF_RULE_GROUP 2015e66d73dSrmindAllow this rule to have sub-rules. 202f24cbc0aSrmindIf this flag is used with the 2035e66d73dSrmind.Dv NPF_RULE_DYNAMIC 204f24cbc0aSrmindflag set, then it is a dynamic group. 205f24cbc0aSrmindThe sub-rules can be added dynamically to a dynamic group, also meaning 206f24cbc0aSrmindthat the sub-rules must have the 207f24cbc0aSrmind.Dv NPF_RULE_DYNAMIC 208f24cbc0aSrmindflag set. 2095e66d73dSrmindOtherwise rules must be added statically i.e. created with the configuration. 2105e66d73dSrmind.It Dv NPF_RULE_DYNAMIC 2115e66d73dSrmindIndicate that the rule is dynamic. 212f24cbc0aSrmindSuch rules can only be added to the dynamic groups. 213e92a47d1Schristos.El 214e92a47d1Schristos.Pp 215f24cbc0aSrmindThe interface is specified by the 216f24cbc0aSrmind.Fa ifname 217f24cbc0aSrmindstring. 218e92a47d1Schristos.Dv NULL 219e92a47d1Schristosindicates any interface. 220e92a47d1Schristos.\" --- 221e92a47d1Schristos.It Fn npf_rule_setcode "rl" "type" "code" "len" 222f24cbc0aSrmindAssign the code for the rule specified by 223f24cbc0aSrmind.Fa rl . 224f24cbc0aSrmindThe code is used to implement the filter criteria. 225f24cbc0aSrmindThe pointer to the binary code is specified by 226e92a47d1Schristos.Fa code , 227f24cbc0aSrmindthe size of the memory area by 228f24cbc0aSrmind.Fa len , 229f24cbc0aSrmindand the type of the code is specified by 230e92a47d1Schristos.Fa type . 231f24cbc0aSrmindCurrently, only the BPF byte-code is supported and the 2325e66d73dSrmind.Dv NPF_CODE_BPF 233f24cbc0aSrmindconstant should be passed. 234e92a47d1Schristos.\" --- 2353d9a792dSrmind.It Fn npf_rule_setkey "rl" "key" "len" 236e92a47d1SchristosAssign a key for the rule specified by 237e92a47d1Schristos.Fa rl . 238f24cbc0aSrmindThe binary key is specified by 239e92a47d1Schristos.Fa key , 240e92a47d1Schristosand its size by 241e92a47d1Schristos.Fa len . 242e92a47d1SchristosThe size shall not exceed 243e92a47d1Schristos.Dv NPF_RULE_MAXKEYLEN . 244f24cbc0aSrmindThe kernel does not check whether key is unique, therefore it is the 245f24cbc0aSrmindresponsibility of the caller. 246e92a47d1Schristos.\" --- 2475e66d73dSrmind.It Fn npf_rule_setinfo "rl" "info" "len" 248f24cbc0aSrmindAssociate an arbitrary information blob specified by 2495e66d73dSrmind.Fa info , 2505e66d73dSrmindand its size by 2515e66d73dSrmind.Fa len . 252f24cbc0aSrmindThis may be used for such purposes as the byte-code annotation. 253e92a47d1Schristos.\" --- 254e92a47d1Schristos.It Fn npf_rule_setprio "rl" "pri" 255e92a47d1SchristosSet priority to the rule. 256e92a47d1SchristosNegative priorities are invalid. 257e92a47d1Schristos.Pp 258f24cbc0aSrmindThe priority is the order of the rule in the ruleset. 259f24cbc0aSrmindThe lower value means first to process, the higher value - last to process. 260e92a47d1SchristosIf multiple rules are inserted with the same priority, 261f24cbc0aSrmindthen the order is unspecified. 262e92a47d1Schristos.Pp 263e92a47d1SchristosThe special constants 264e92a47d1Schristos.Dv NPF_PRI_FIRST 265e92a47d1Schristosand 266e92a47d1Schristos.Dv NPF_PRI_LAST 267e92a47d1Schristoscan be passed to indicate that the rule should be inserted into the 268e92a47d1Schristosbeginning or the end of the priority level 0 in the ruleset. 269e92a47d1SchristosAll rules inserted using these constants will have the priority 0 270e92a47d1Schristosassigned and will share this level in the ordered way. 2715e66d73dSrmind.\" --- 2723d9a792dSrmind.It Fn npf_rule_setproc "rl" "name" 273e92a47d1SchristosSet a procedure for the specified rule. 2745e66d73dSrmind.\" --- 2755e66d73dSrmind.It Fn npf_rule_insert "ncf" "parent" "rl" 276f24cbc0aSrmindInsert the rule into the set of the parent rule specified by 2775e66d73dSrmind.Fa parent . 278f24cbc0aSrmindIf the value of 2795e66d73dSrmind.Fa parent 2805e66d73dSrmindis 2815e66d73dSrmind.Dv NULL , 2825e66d73dSrmindthen insert into the main ruleset. 283*b899bfd9SrmindThe rule will be consumed (the relevant resourced will be freed) and it 284*b899bfd9Srmindmust not be referenced after insertion. 2855e66d73dSrmind.\" --- 2863d9a792dSrmind.It Fn npf_rule_exists_p "ncf" "name" 2873d9a792dSrmindCheck whether the rule with a given name is already in the configuration. 2883d9a792dSrmind.\" --- 2895e66d73dSrmind.It Fn npf_rule_export "rl" "length" 2905e66d73dSrmindSerialize the rule (including the byte-code), return a binary object 2915e66d73dSrmindand set its 2925e66d73dSrmind.Fa length . 2935e66d73dSrmindThe binary object is dynamically allocated and should be destroyed using 2945e66d73dSrmind.Xr free 3 . 2955e66d73dSrmind.\" --- 296e92a47d1Schristos.It Fn npf_rule_destroy "rl" 297f24cbc0aSrmindDestroy the given rule object. 298e92a47d1Schristos.El 299e92a47d1Schristos.\" ----- 300e92a47d1Schristos.Ss Rule procedure interface 301e92a47d1Schristos.Bl -tag -width 4n 302e92a47d1Schristos.It Fn npf_rproc_create "name" 303e92a47d1SchristosCreate a rule procedure with a given 304e92a47d1Schristos.Fa name . 305f24cbc0aSrmindThr name must be unique for each procedure. 306e92a47d1Schristos.It Fn npf_rproc_insert "ncf" "rp" 307f24cbc0aSrmindInsert the rule procedure into the specified configuration object. 30839013e66SrmindThe rule procedure must not be referenced after insertion. 309e92a47d1Schristos.El 310e92a47d1Schristos.\" ----- 311e92a47d1Schristos.Ss Translation interface 312e92a47d1Schristos.Bl -tag -width 4n 3133d9a792dSrmind.It Fn npf_nat_create "type" "flags" "ifname" 314f24cbc0aSrmindCreate a NAT policy of a specified type. 315e92a47d1SchristosThere are two types: 316e92a47d1Schristos.Bl -tag -width "NPF_NAT_PORTMAP " 317e92a47d1Schristos.It Dv NPF_NATIN 3185e66d73dSrmindInbound NAT policy (rewrite destination). 319e92a47d1Schristos.It Dv NPF_NATOUT 3205e66d73dSrmindOutbound NAT policy (rewrite source). 321e92a47d1Schristos.El 322e92a47d1Schristos.Pp 323e92a47d1SchristosA bi-directional NAT is obtained by combining two policies. 324e92a47d1SchristosThe following 325e92a47d1Schristos.Fa flags 326e92a47d1Schristosare supported: 327e92a47d1Schristos.Bl -tag -width "NPF_NAT_PORTMAP " 3285e66d73dSrmind.It Dv NPF_NAT_STATIC 329f24cbc0aSrmindPerform static (stateless) translation rather than dynamic (stateful). 330e92a47d1Schristos.It Dv NPF_NAT_PORTS 331f24cbc0aSrmindPerform the port translation. 332f24cbc0aSrmindIf this flag is not specified, then the port translation is not performed 333f24cbc0aSrmindand the 334e92a47d1Schristos.Fa port 335f24cbc0aSrmindparameter is ignored. 336e92a47d1Schristos.It Dv NPF_NAT_PORTMAP 337f24cbc0aSrmindCreate a port map and select a random port for translation. 338f24cbc0aSrmindIf enabled, then the value specified by the 339f24cbc0aSrmind.Fa port 340f24cbc0aSrmindparameter is ignored. 341f24cbc0aSrmindThis flag is effective only if the 342e92a47d1Schristos.Dv NPF_NAT_PORTS 343e92a47d1Schristosflag is set. 344e92a47d1Schristos.El 345e92a47d1Schristos.Pp 3463d9a792dSrmindThe network interface on which the policy will be applicable is specified by 3473d9a792dSrmind.Fa ifname . 3483d9a792dSrmind.\" --- 3493d9a792dSrmind.It Fn npf_nat_setaddr "nt" "af" "addr" "mask" 3503d9a792dSrmindSet the translation address, as specified by 351e92a47d1Schristos.Fa addr , 352e92a47d1Schristosand its family by 353e92a47d1Schristos.Fa af . 354f24cbc0aSrmindThe family must be either 355e92a47d1Schristos.Dv AF_INET 356e92a47d1Schristosfor IPv4 or 357e92a47d1Schristos.Dv AF_INET6 358e92a47d1Schristosfor IPv6 address. 3593d9a792dSrmindAdditionally, 3603d9a792dSrmind.Fa mask 3610e1944daSrmindmay be specified to indicate the translation network; 3620e1944daSrmindotherwise, it should be set to 3630e1944daSrmind.Dv NPF_NO_NETMASK . 36404cb50acSrmind.Pp 36504cb50acSrmindIn order to use the translation network, a custom algorithm may need to 36604cb50acSrmindbe specified using the 3673d9a792dSrmind.Fn npf_nat_setalgo 3683d9a792dSrmindfunction. 3693d9a792dSrmind.\" --- 3703d9a792dSrmind.It Fn npf_nat_setport "nt" "port" 3713d9a792dSrmindSet the translation port, specified by 3723d9a792dSrmind.Fa port . 3735e66d73dSrmind.\" --- 3745e66d73dSrmind.It Fn npf_nat_setalgo "nt" "algo" 3755e66d73dSrmindSet a particular NAT algorithm. 3763d9a792dSrmindCurrently, the following algorithms are supported with dynamic NAT: 3773d9a792dSrmind.Bl -tag -width "NPF_ALGO_IPHASH" 3783d9a792dSrmind.It Dv NPF_ALGO_IPHASH 3793d9a792dSrmindHash of the source and destination addresses. 3803d9a792dSrmind.It Dv NPF_ALGO_RR 3813d9a792dSrmindRound-robin for the translation addresses. 38204cb50acSrmind.It Dv NPF_ALGO_NETMAP 38304cb50acSrmindNetwork-to-network map as described below, but with state tracking. 38404cb50acSrmindIt is used when it is necessary to translate the ports. 3853d9a792dSrmind.El 3863d9a792dSrmind.Pp 3873d9a792dSrmindThe following are support with static NAT: 3883d9a792dSrmind.Bl -tag -width "NPF_ALGO_NETMAP" 3893d9a792dSrmind.It Dv NPF_ALGO_NETMAP 3903d9a792dSrmindNetwork-to-network map where the translation network prefix (address 3913d9a792dSrmindafter applying the mask) is bitwise OR-ed with the host part of the 3923d9a792dSrmindoriginal address (zero bits of the mask). 3933d9a792dSrmind.It Dv NPF_ALGO_NPT66 3943d9a792dSrmindIPv6-to-IPv6 Network Prefix Translation (NPTv6, defined in RFC 6296). 3953d9a792dSrmind.El 3965e66d73dSrmind.\" --- 397dadc88e3Srmind.It Fn npf_nat_insert "ncf" "nt" 398dadc88e3SrmindInsert the NAT policy, its rule, into the specified configuration. 39939013e66SrmindThe NAT rule must not be referenced after insertion. 400e92a47d1Schristos.El 401e92a47d1Schristos.\" ----- 402e92a47d1Schristos.Ss Table interface 403e92a47d1Schristos.Bl -tag -width 4n 404e92a47d1Schristos.It Fn npf_table_create "name" "index" "type" 405f24cbc0aSrmindCreate an NPF table of a specified type. 406f24cbc0aSrmindThe table is identified by the 407e92a47d1Schristos.Fa name 408e92a47d1Schristosand 409e92a47d1Schristos.Fa index , 410e92a47d1Schristoswhich should be in the range between 1 and 41139013e66Srmind.Dv NPF_MAX_TABLES . 412f24cbc0aSrmind.Pp 413f24cbc0aSrmindThe following types are supported: 4143d9a792dSrmind.Bl -tag -width "NPF_TABLE_IPSET" 4153d9a792dSrmind.It Dv NPF_TABLE_IPSET 4163d9a792dSrmindIndicates to use a regular associative array for storage of IP sets. 4173d9a792dSrmindCurrently implemented as a hashmap. 4183d9a792dSrmind.It Dv NPF_TABLE_LPM 4193d9a792dSrmindIndicates to the table can contain networks (as well as hosts) and the 4203d9a792dSrmindlongest prefix match should be performed on lookup. 4213d9a792dSrmind.It Dv NPF_TABLE_CONST 4223d9a792dSrmindIndicates that the table contents will be constant and the table can be 4233d9a792dSrmindconsidered immutable (no inserts/removes after load). 4243d9a792dSrmindIf such constraint is acceptable, this table type will provide the best 4253d9a792dSrmindperformance. 4263d9a792dSrmindIt is currently implemented as a perfect hash table, generated on table 4273d9a792dSrmindinsertion into the configuration. 428e92a47d1Schristos.El 4295e66d73dSrmind.\" --- 430e92a47d1Schristos.It Fn npf_table_add_entry "tl" "af" "addr" "mask" 431e92a47d1SchristosAdd an entry of IP address and mask, specified by 432e92a47d1Schristos.Fa addr 433e92a47d1Schristosand 434e92a47d1Schristos.Fa mask , 435e92a47d1Schristosto the table specified by 436e92a47d1Schristos.Fa tl . 437f24cbc0aSrmindThe family, specified by 438e92a47d1Schristos.Fa af , 439e92a47d1Schristosmust be either 440e92a47d1Schristos.Dv AF_INET 441e92a47d1Schristosfor IPv4 or 442e92a47d1Schristos.Dv AF_INET6 443e92a47d1Schristosfor IPv6 address. 4440e1944daSrmindIf there is no mask, then 4450e1944daSrmind.Fa mask 4460e1944daSrmindshould be set to 4470e1944daSrmind.Dv NPF_NO_NETMASK . 4480e1944daSrmind.\" --- 449e92a47d1Schristos.It Fn npf_table_insert "ncf" "tl" 450f24cbc0aSrmindAdd the table to the configuration object. 451f24cbc0aSrmindThis routine performs a check for duplicate table IDs. 45239013e66SrmindThe table must not be referenced after insertion. 4535e66d73dSrmind.\" --- 4540e1944daSrmind.It Fn npf_table_replace "fd" "tl" "errinfo" 4550e1944daSrmindSubmit the table object, specified by 4560e1944daSrmind.Fa tl , 4570e1944daSrmindto the kernel, to replace the existing table with the 4580e1944daSrmindcorresponding table name and ID. 4590e1944daSrmindOn failure, the error information is written into the structure 4600e1944daSrmindspecified by 4610e1944daSrmind.Fa errinfo . 4620e1944daSrmind.\" --- 463e92a47d1Schristos.It Fn npf_table_destroy "tl" 464e92a47d1SchristosDestroy the specified table. 465e92a47d1Schristos.El 466e92a47d1Schristos.\" ----- 46704cb50acSrmind.Ss Ruleset interface 46804cb50acSrmind.Bl -tag -width 4n 46904cb50acSrmind.It Fn npf_ruleset_add "fd" "name" "rl" "id" 47004cb50acSrmindAdd a given rule, specified by 47104cb50acSrmind.Fa rl , 47204cb50acSrmindinto the dynamic ruleset named 47304cb50acSrmind.Fa name . 47404cb50acSrmindOn success, return 0 and a unique rule ID in the 47504cb50acSrmind.Fa id 47604cb50acSrmindparameter. 47704cb50acSrmind.It Fn npf_ruleset_remove "fd" "name" "id" 47804cb50acSrmindRemove a rule from the dynamic ruleset, specified by 47904cb50acSrmind.Fa name . 48004cb50acSrmindThe rule is specified by its unique ID in the 48104cb50acSrmind.Fa id 48204cb50acSrmindparameter. 48304cb50acSrmind.It Fn npf_ruleset_remkey "fd" "name" "key" "len" 48404cb50acSrmindRemove a rule from the dynamic ruleset, specified by 48504cb50acSrmind.Fa name . 48604cb50acSrmindThe rule is specified by its key, in the 48704cb50acSrmind.Fa key 48804cb50acSrmindand 48904cb50acSrmind.Fa len 49004cb50acSrmindparameters. 49104cb50acSrmindThe key for the rule must have been set during its construction, using the 49204cb50acSrmind.Fn npf_rule_setkey 49304cb50acSrmindroutine. 49404cb50acSrmind.It Fn npf_ruleset_flush "fd" "name" 49504cb50acSrmindClear the dynamic ruleset, specified by 49604cb50acSrmind.Fa name , 49704cb50acSrmindby removing all its rules. 49804cb50acSrmind.El 49904cb50acSrmind.\" ----- 500e92a47d1Schristos.Sh SEE ALSO 501e92a47d1Schristos.Xr bpf 4 , 502c2b1c6ccSrmind.Xr npf 7 , 503e92a47d1Schristos.Xr npfctl 8 504e92a47d1Schristos.Sh HISTORY 505e92a47d1SchristosThe NPF library first appeared in 506e92a47d1Schristos.Nx 6.0 . 507