117a35ce7SGleb Smirnoff.\" Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org> 217a35ce7SGleb Smirnoff.\" All rights reserved. 317a35ce7SGleb Smirnoff.\" 417a35ce7SGleb Smirnoff.\" Redistribution and use in source and binary forms, with or without 517a35ce7SGleb Smirnoff.\" modification, are permitted provided that the following conditions 617a35ce7SGleb Smirnoff.\" are met: 717a35ce7SGleb Smirnoff.\" 1. Redistributions of source code must retain the above copyright 817a35ce7SGleb Smirnoff.\" notice, this list of conditions and the following disclaimer. 917a35ce7SGleb Smirnoff.\" 2. Redistributions in binary form must reproduce the above copyright 1017a35ce7SGleb Smirnoff.\" notice, this list of conditions and the following disclaimer in the 1117a35ce7SGleb Smirnoff.\" documentation and/or other materials provided with the distribution. 1217a35ce7SGleb Smirnoff.\" 1317a35ce7SGleb Smirnoff.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1417a35ce7SGleb Smirnoff.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1517a35ce7SGleb Smirnoff.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1617a35ce7SGleb Smirnoff.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1717a35ce7SGleb Smirnoff.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1817a35ce7SGleb Smirnoff.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1917a35ce7SGleb Smirnoff.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2017a35ce7SGleb Smirnoff.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2117a35ce7SGleb Smirnoff.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2217a35ce7SGleb Smirnoff.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2317a35ce7SGleb Smirnoff.\" SUCH DAMAGE. 2417a35ce7SGleb Smirnoff.\" 2517a35ce7SGleb Smirnoff.\" $FreeBSD$ 2617a35ce7SGleb Smirnoff.\" 2721f97e93SGleb Smirnoff.Dd March 21, 2013 2817a35ce7SGleb Smirnoff.Dt NG_NAT 4 2917a35ce7SGleb Smirnoff.Os 3017a35ce7SGleb Smirnoff.Sh NAME 3117a35ce7SGleb Smirnoff.Nm ng_nat 32a4be0b3cSRuslan Ermilov.Nd "NAT netgraph node type" 3317a35ce7SGleb Smirnoff.Sh SYNOPSIS 3417a35ce7SGleb Smirnoff.In netgraph/ng_nat.h 3517a35ce7SGleb Smirnoff.Sh DESCRIPTION 3617a35ce7SGleb SmirnoffAn 3717a35ce7SGleb Smirnoff.Nm 3817a35ce7SGleb Smirnoffnode performs network address translation (NAT) of packets 3917a35ce7SGleb Smirnoffpassing through it. 40a4be0b3cSRuslan ErmilovA 41a4be0b3cSRuslan Ermilov.Nm nat 4217a35ce7SGleb Smirnoffnode uses 4317a35ce7SGleb Smirnoff.Xr libalias 3 4417a35ce7SGleb Smirnoffengine for packet aliasing. 4517a35ce7SGleb Smirnoff.Sh HOOKS 4617a35ce7SGleb SmirnoffThis node type has two hooks: 47c60bda17SJoel Dahl.Bl -tag -width ".Va out" 48a4be0b3cSRuslan Ermilov.It Va out 49a4be0b3cSRuslan ErmilovPackets received on this hook are considered outgoing and will be 50a4be0b3cSRuslan Ermilovmasqueraded to a configured address. 51a4be0b3cSRuslan Ermilov.It Va in 52a4be0b3cSRuslan ErmilovPackets coming on this hook are considered incoming and will be 5317a35ce7SGleb Smirnoffdealiased. 5417a35ce7SGleb Smirnoff.El 5517a35ce7SGleb Smirnoff.Sh CONTROL MESSAGES 5617a35ce7SGleb SmirnoffThis node type supports the generic control messages, plus the following: 57c60bda17SJoel Dahl.Bl -tag -width foo 58c60bda17SJoel Dahl.It Dv NGM_NAT_SET_IPADDR Pq Ic setaliasaddr 59a4be0b3cSRuslan ErmilovConfigure aliasing address for a node. 60a4be0b3cSRuslan ErmilovAfter both hooks have been connected and aliasing address was configured, 61a4be0b3cSRuslan Ermilova node is ready for aliasing operation. 62c60bda17SJoel Dahl.It Dv NGM_NAT_SET_MODE Pq Ic setmode 63e842c540SAlexander MotinSet node's operation mode using supplied 64e842c540SAlexander Motin.Vt "struct ng_nat_mode" . 65e842c540SAlexander Motin.Bd -literal 66e842c540SAlexander Motinstruct ng_nat_mode { 67e842c540SAlexander Motin uint32_t flags; 68e842c540SAlexander Motin uint32_t mask; 69e842c540SAlexander Motin}; 70e842c540SAlexander Motin/* Supported flags: */ 71e842c540SAlexander Motin#define NG_NAT_LOG 0x01 72e842c540SAlexander Motin#define NG_NAT_DENY_INCOMING 0x02 73e842c540SAlexander Motin#define NG_NAT_SAME_PORTS 0x04 74e842c540SAlexander Motin#define NG_NAT_UNREGISTERED_ONLY 0x10 75e842c540SAlexander Motin#define NG_NAT_RESET_ON_ADDR_CHANGE 0x20 76e842c540SAlexander Motin#define NG_NAT_PROXY_ONLY 0x40 77e842c540SAlexander Motin#define NG_NAT_REVERSE 0x80 78e842c540SAlexander Motin.Ed 79c60bda17SJoel Dahl.It Dv NGM_NAT_SET_TARGET Pq Ic settarget 80e842c540SAlexander MotinConfigure target address for a node. 81e842c540SAlexander MotinWhen an incoming packet not associated with any pre-existing aliasing 82e842c540SAlexander Motinlink arrives at the host machine, it will be sent to the specified address. 83c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_PORT Pq Ic redirectport 84fffba935SAlexander MotinRedirect incoming connections arriving to given port(s) to 85fffba935SAlexander Motinanother host and port(s). 86fffba935SAlexander MotinThe following 87fffba935SAlexander Motin.Vt "struct ng_nat_redirect_port" 88fffba935SAlexander Motinmust be supplied as argument. 89fffba935SAlexander Motin.Bd -literal 90fffba935SAlexander Motin#define NG_NAT_DESC_LENGTH 64 91fffba935SAlexander Motinstruct ng_nat_redirect_port { 92fffba935SAlexander Motin struct in_addr local_addr; 93fffba935SAlexander Motin struct in_addr alias_addr; 94fffba935SAlexander Motin struct in_addr remote_addr; 95fffba935SAlexander Motin uint16_t local_port; 96fffba935SAlexander Motin uint16_t alias_port; 97fffba935SAlexander Motin uint16_t remote_port; 98fffba935SAlexander Motin uint8_t proto; 99fffba935SAlexander Motin char description[NG_NAT_DESC_LENGTH]; 100fffba935SAlexander Motin}; 101fffba935SAlexander Motin.Ed 102fffba935SAlexander Motin.Pp 103fffba935SAlexander MotinRedirection is assigned an unique ID which is returned as 104fffba935SAlexander Motinresponse to this message, and 105fffba935SAlexander Motininformation about redirection added to 106fffba935SAlexander Motinlist of static redirects which later can be retrieved by 107fffba935SAlexander Motin.Dv NGM_NAT_LIST_REDIRECTS 108fffba935SAlexander Motinmessage. 109c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_ADDR Pq Ic redirectaddr 110fffba935SAlexander MotinRedirect traffic for public IP address to a machine on the 111fffba935SAlexander Motinlocal network. 112fffba935SAlexander MotinThis function is known as 113fffba935SAlexander Motin.Em static NAT . 114fffba935SAlexander MotinThe following 115fffba935SAlexander Motin.Vt "struct ng_nat_redirect_addr" 116fffba935SAlexander Motinmust be supplied as argument. 117fffba935SAlexander Motin.Bd -literal 118fffba935SAlexander Motinstruct ng_nat_redirect_addr { 119fffba935SAlexander Motin struct in_addr local_addr; 120fffba935SAlexander Motin struct in_addr alias_addr; 121fffba935SAlexander Motin char description[NG_NAT_DESC_LENGTH]; 122fffba935SAlexander Motin}; 123fffba935SAlexander Motin.Ed 124fffba935SAlexander Motin.Pp 125fffba935SAlexander MotinUnique ID for this redirection is returned as response to this message. 126c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_PROTO Pq Ic redirectproto 127fffba935SAlexander MotinRedirect incoming IP packets of protocol 128fffba935SAlexander Motin.Va proto 129fffba935SAlexander Motin(see 130fffba935SAlexander Motin.Xr protocols 5 ) 131fffba935SAlexander Motinto a machine on the local network. 132fffba935SAlexander MotinThe following 133fffba935SAlexander Motin.Vt "struct ng_nat_redirect_proto" 134fffba935SAlexander Motinmust be supplied as argument. 135fffba935SAlexander Motin.Bd -literal 136fffba935SAlexander Motinstruct ng_nat_redirect_proto { 137fffba935SAlexander Motin struct in_addr local_addr; 138fffba935SAlexander Motin struct in_addr alias_addr; 139fffba935SAlexander Motin struct in_addr remote_addr; 140fffba935SAlexander Motin uint8_t proto; 141fffba935SAlexander Motin char description[NG_NAT_DESC_LENGTH]; 142fffba935SAlexander Motin}; 143fffba935SAlexander Motin.Ed 144fffba935SAlexander Motin.Pp 145fffba935SAlexander MotinUnique ID for this redirection is returned as response to this message. 146c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_DYNAMIC Pq Ic redirectdynamic 147fffba935SAlexander MotinMark redirection with specified ID as dynamic, i.e., it will serve 148fffba935SAlexander Motinfor exactly one next connection and then will be automatically 149fffba935SAlexander Motindeleted from internal links table. 150fffba935SAlexander MotinOnly fully specified links can be made dynamic. 151fffba935SAlexander MotinThe redirection with this ID is also immediately deleted from 152fffba935SAlexander Motinuser-visible list of static redirects (available through 153fffba935SAlexander Motin.Dv NGM_NAT_LIST_REDIRECTS 154fffba935SAlexander Motinmessage). 155c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_DELETE Pq Ic redirectdelete 156fffba935SAlexander MotinDelete redirection with specified ID (currently active 157fffba935SAlexander Motinconnections are not affected). 158c60bda17SJoel Dahl.It Dv NGM_NAT_ADD_SERVER Pq Ic addserver 159fffba935SAlexander MotinAdd another server to a pool. 160fffba935SAlexander MotinThis is used to transparently offload network load on a single server 161fffba935SAlexander Motinand distribute the load across a pool of servers, also known as 162fffba935SAlexander Motin.Em LSNAT 163fffba935SAlexander Motin(RFC 2391). 164fffba935SAlexander MotinThe following 165fffba935SAlexander Motin.Vt "struct ng_nat_add_server" 166fffba935SAlexander Motinmust be supplied as argument. 167fffba935SAlexander Motin.Bd -literal 168fffba935SAlexander Motinstruct ng_nat_add_server { 169fffba935SAlexander Motin uint32_t id; 170fffba935SAlexander Motin struct in_addr addr; 171fffba935SAlexander Motin uint16_t port; 172fffba935SAlexander Motin}; 173fffba935SAlexander Motin.Ed 174fffba935SAlexander Motin.Pp 175fffba935SAlexander MotinFirst, the redirection is set up by 176fffba935SAlexander Motin.Dv NGM_NAT_REDIRECT_PORT 177fffba935SAlexander Motinor 178fffba935SAlexander Motin.Dv NGM_NAT_REDIRECT_ADDR . 179fffba935SAlexander MotinThen, ID of that redirection is used in multiple 180fffba935SAlexander Motin.Dv NGM_NAT_ADD_SERVER 181fffba935SAlexander Motinmessages to add necessary number of servers. 182fffba935SAlexander MotinFor redirections created by 183fffba935SAlexander Motin.Dv NGM_NAT_REDIRECT_ADDR , 184fffba935SAlexander Motinthe 185fffba935SAlexander Motin.Va port 186fffba935SAlexander Motinis ignored and could have any value. 187fffba935SAlexander MotinOriginal redirection's parameters 188fffba935SAlexander Motin.Va local_addr 189fffba935SAlexander Motinand 190fffba935SAlexander Motin.Va local_port 191fffba935SAlexander Motinare also ignored after 192fffba935SAlexander Motin.Dv NGM_NAT_ADD_SERVER 193fffba935SAlexander Motinwas used (they are effectively replaced by server pool). 194c60bda17SJoel Dahl.It Dv NGM_NAT_LIST_REDIRECTS Pq Ic listredirects 195fffba935SAlexander MotinReturn list of configured static redirects as 196fffba935SAlexander Motin.Vt "struct ng_nat_list_redirects" . 197fffba935SAlexander Motin.Bd -literal 198fffba935SAlexander Motinstruct ng_nat_listrdrs_entry { 199fffba935SAlexander Motin uint32_t id; /* Anything except zero */ 200fffba935SAlexander Motin struct in_addr local_addr; 201fffba935SAlexander Motin struct in_addr alias_addr; 202fffba935SAlexander Motin struct in_addr remote_addr; 203fffba935SAlexander Motin uint16_t local_port; 204fffba935SAlexander Motin uint16_t alias_port; 205fffba935SAlexander Motin uint16_t remote_port; 206fffba935SAlexander Motin uint16_t proto; /* Valid proto or NG_NAT_REDIRPROTO_ADDR */ 207fffba935SAlexander Motin uint16_t lsnat; /* LSNAT servers count */ 208fffba935SAlexander Motin char description[NG_NAT_DESC_LENGTH]; 209fffba935SAlexander Motin}; 210fffba935SAlexander Motinstruct ng_nat_list_redirects { 211fffba935SAlexander Motin uint32_t total_count; 212fffba935SAlexander Motin struct ng_nat_listrdrs_entry redirects[]; 213fffba935SAlexander Motin}; 214fffba935SAlexander Motin#define NG_NAT_REDIRPROTO_ADDR (IPPROTO_MAX + 3) 215fffba935SAlexander Motin.Ed 216fffba935SAlexander Motin.Pp 217fffba935SAlexander MotinEntries of the 218fffba935SAlexander Motin.Va redirects 219fffba935SAlexander Motinarray returned in the unified format for all redirect types. 220fffba935SAlexander MotinPorts are meaningful only if protocol is either TCP or UDP 221fffba935SAlexander Motinand 222fffba935SAlexander Motin.Em static NAT 223fffba935SAlexander Motinredirection (created by 224fffba935SAlexander Motin.Dv NGM_NAT_REDIRECT_ADDR ) 225fffba935SAlexander Motinis indicated by 226fffba935SAlexander Motin.Va proto 227fffba935SAlexander Motinset to 228fffba935SAlexander Motin.Dv NG_NAT_REDIRPROTO_ADDR . 229fffba935SAlexander MotinIf 230fffba935SAlexander Motin.Va lsnat 231fffba935SAlexander Motinservers counter is greater than zero, then 232fffba935SAlexander Motin.Va local_addr 233fffba935SAlexander Motinand 234fffba935SAlexander Motin.Va local_port 235fffba935SAlexander Motinare also meaningless. 236c60bda17SJoel Dahl.It Dv NGM_NAT_PROXY_RULE Pq Ic proxyrule 237fffba935SAlexander MotinSpecify a transparent proxying rule (string must be 238fffba935SAlexander Motinsupplied as argument). 239fffba935SAlexander MotinSee 240fffba935SAlexander Motin.Xr libalias 3 241fffba935SAlexander Motinfor details. 24221f97e93SGleb Smirnoff.It Dv NGM_NAT_LIBALIAS_INFO Pq Ic libaliasinfo 24321f97e93SGleb SmirnoffReturn internal statistics of 24421f97e93SGleb Smirnoff.Xr libalias 3 24521f97e93SGleb Smirnoffinstance as 24621f97e93SGleb Smirnoff.Vt "struct ng_nat_libalias_info" . 24721f97e93SGleb Smirnoff.Bd -literal 24821f97e93SGleb Smirnoffstruct ng_nat_libalias_info { 24921f97e93SGleb Smirnoff uint32_t icmpLinkCount; 25021f97e93SGleb Smirnoff uint32_t udpLinkCount; 25121f97e93SGleb Smirnoff uint32_t tcpLinkCount; 25221f97e93SGleb Smirnoff uint32_t sctpLinkCount; 25321f97e93SGleb Smirnoff uint32_t pptpLinkCount; 25421f97e93SGleb Smirnoff uint32_t protoLinkCount; 25521f97e93SGleb Smirnoff uint32_t fragmentIdLinkCount; 25621f97e93SGleb Smirnoff uint32_t fragmentPtrLinkCount; 25721f97e93SGleb Smirnoff uint32_t sockCount; 25821f97e93SGleb Smirnoff}; 25921f97e93SGleb Smirnoff.Ed 26021f97e93SGleb SmirnoffIn case of 26121f97e93SGleb Smirnoff.Nm 26221f97e93SGleb Smirnofffailed to retreive a certain counter 26321f97e93SGleb Smirnofffrom its 26421f97e93SGleb Smirnoff.Xr libalias 26521f97e93SGleb Smirnoffinstance, the corresponding field is returned as 26621f97e93SGleb Smirnoff.Va UINT32_MAX . 26717a35ce7SGleb Smirnoff.El 268fffba935SAlexander Motin.Pp 269fffba935SAlexander MotinIn all redirection messages 270fffba935SAlexander Motin.Va local_addr 271fffba935SAlexander Motinand 272fffba935SAlexander Motin.Va local_port 273fffba935SAlexander Motinmean address and port of target machine in the internal network, 274fffba935SAlexander Motinrespectively. 275fffba935SAlexander MotinIf 276fffba935SAlexander Motin.Va alias_addr 277fffba935SAlexander Motinis zero, then default aliasing address (set by 278fffba935SAlexander Motin.Dv NGM_NAT_SET_IPADDR ) 279fffba935SAlexander Motinis used. 280fffba935SAlexander MotinConnections can also be restricted to be accepted only 281fffba935SAlexander Motinfrom specific external machines by using non-zero 282fffba935SAlexander Motin.Va remote_addr 283fffba935SAlexander Motinand/or 284fffba935SAlexander Motin.Va remote_port . 285fffba935SAlexander MotinEach redirection assigned an ID which can be later used for 286fffba935SAlexander Motinredirection manipulation on individual basis (e.g., removal). 287fffba935SAlexander MotinThis ID guaranteed to be unique until the node shuts down 288fffba935SAlexander Motin(it will not be reused after deletion), and is returned to 289fffba935SAlexander Motinuser after making each new redirection or can be found in 290fffba935SAlexander Motinthe stored list of all redirections. 291fffba935SAlexander MotinThe 292fffba935SAlexander Motin.Va description 293fffba935SAlexander Motinpassed to and from node unchanged, together with ID providing 294fffba935SAlexander Motina way for several entities to concurrently manipulate 295fffba935SAlexander Motinredirections in automated way. 29617a35ce7SGleb Smirnoff.Sh SHUTDOWN 29717a35ce7SGleb SmirnoffThis node shuts down upon receipt of a 29817a35ce7SGleb Smirnoff.Dv NGM_SHUTDOWN 29917a35ce7SGleb Smirnoffcontrol message, or when both hooks are disconnected. 30017a35ce7SGleb Smirnoff.Sh EXAMPLES 301a4be0b3cSRuslan ErmilovIn the following example, the packets are injected into a 302a4be0b3cSRuslan Ermilov.Nm nat 303a4be0b3cSRuslan Ermilovnode using the 30417a35ce7SGleb Smirnoff.Xr ng_ipfw 4 30517a35ce7SGleb Smirnoffnode. 30617a35ce7SGleb Smirnoff.Bd -literal -offset indent 30717a35ce7SGleb Smirnoff# Create NAT node 30817a35ce7SGleb Smirnoffngctl mkpeer ipfw: nat 60 out 30917a35ce7SGleb Smirnoffngctl name ipfw:60 nat 31017a35ce7SGleb Smirnoffngctl connect ipfw: nat: 61 in 31117a35ce7SGleb Smirnoffngctl msg nat: setaliasaddr x.y.35.8 31217a35ce7SGleb Smirnoff 31317a35ce7SGleb Smirnoff# Divert traffic into NAT node 31417a35ce7SGleb Smirnoffipfw add 300 netgraph 61 all from any to any in via fxp0 31517a35ce7SGleb Smirnoffipfw add 400 netgraph 60 all from any to any out via fxp0 31617a35ce7SGleb Smirnoff 31717a35ce7SGleb Smirnoff# Let packets continue with after being (de)aliased 31817a35ce7SGleb Smirnoffsysctl net.inet.ip.fw.one_pass=0 31917a35ce7SGleb Smirnoff.Ed 32017a35ce7SGleb Smirnoff.Pp 321a4be0b3cSRuslan ErmilovThe 32217a35ce7SGleb Smirnoff.Nm 323a4be0b3cSRuslan Ermilovnode can be inserted right after the 324a4be0b3cSRuslan Ermilov.Xr ng_iface 4 325a4be0b3cSRuslan Ermilovnode in the graph. 326a4be0b3cSRuslan ErmilovIn the following example, we perform masquerading on a 32717a35ce7SGleb Smirnoffserial line with HDLC encapsulation. 32817a35ce7SGleb Smirnoff.Bd -literal -offset indent 32917a35ce7SGleb Smirnoff/usr/sbin/ngctl -f- <<-SEQ 33017a35ce7SGleb Smirnoff mkpeer cp0: cisco rawdata downstream 33117a35ce7SGleb Smirnoff name cp0:rawdata hdlc 33217a35ce7SGleb Smirnoff mkpeer hdlc: nat inet in 33317a35ce7SGleb Smirnoff name hdlc:inet nat 33417a35ce7SGleb Smirnoff mkpeer nat: iface out inet 33517a35ce7SGleb Smirnoff msg nat: setaliasaddr x.y.8.35 33617a35ce7SGleb SmirnoffSEQ 33717a35ce7SGleb Smirnoffifconfig ng0 x.y.8.35 x.y.8.1 33817a35ce7SGleb Smirnoff.Ed 33917a35ce7SGleb Smirnoff.Sh SEE ALSO 34017a35ce7SGleb Smirnoff.Xr libalias 3 , 34117a35ce7SGleb Smirnoff.Xr ng_ipfw 4 , 342a4be0b3cSRuslan Ermilov.Xr natd 8 , 34317a35ce7SGleb Smirnoff.Xr ngctl 8 34417a35ce7SGleb Smirnoff.Sh HISTORY 34517a35ce7SGleb SmirnoffThe 34617a35ce7SGleb Smirnoff.Nm 34717a35ce7SGleb Smirnoffnode type was implemented in 34817a35ce7SGleb Smirnoff.Fx 6.0 . 34917a35ce7SGleb Smirnoff.Sh AUTHORS 350*6c899950SBaptiste Daroussin.An Gleb Smirnoff Aq Mt glebius@FreeBSD.org 351