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.\" 27fffba935SAlexander Motin.Dd March 1, 2008 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: 47*c60bda17SJoel 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: 57*c60bda17SJoel Dahl.Bl -tag -width foo 58*c60bda17SJoel 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. 62*c60bda17SJoel 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 79*c60bda17SJoel 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. 83*c60bda17SJoel 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. 109*c60bda17SJoel 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. 126*c60bda17SJoel 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. 146*c60bda17SJoel 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). 155*c60bda17SJoel Dahl.It Dv NGM_NAT_REDIRECT_DELETE Pq Ic redirectdelete 156fffba935SAlexander MotinDelete redirection with specified ID (currently active 157fffba935SAlexander Motinconnections are not affected). 158*c60bda17SJoel 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). 194*c60bda17SJoel 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. 236*c60bda17SJoel 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. 24217a35ce7SGleb Smirnoff.El 243fffba935SAlexander Motin.Pp 244fffba935SAlexander MotinIn all redirection messages 245fffba935SAlexander Motin.Va local_addr 246fffba935SAlexander Motinand 247fffba935SAlexander Motin.Va local_port 248fffba935SAlexander Motinmean address and port of target machine in the internal network, 249fffba935SAlexander Motinrespectively. 250fffba935SAlexander MotinIf 251fffba935SAlexander Motin.Va alias_addr 252fffba935SAlexander Motinis zero, then default aliasing address (set by 253fffba935SAlexander Motin.Dv NGM_NAT_SET_IPADDR ) 254fffba935SAlexander Motinis used. 255fffba935SAlexander MotinConnections can also be restricted to be accepted only 256fffba935SAlexander Motinfrom specific external machines by using non-zero 257fffba935SAlexander Motin.Va remote_addr 258fffba935SAlexander Motinand/or 259fffba935SAlexander Motin.Va remote_port . 260fffba935SAlexander MotinEach redirection assigned an ID which can be later used for 261fffba935SAlexander Motinredirection manipulation on individual basis (e.g., removal). 262fffba935SAlexander MotinThis ID guaranteed to be unique until the node shuts down 263fffba935SAlexander Motin(it will not be reused after deletion), and is returned to 264fffba935SAlexander Motinuser after making each new redirection or can be found in 265fffba935SAlexander Motinthe stored list of all redirections. 266fffba935SAlexander MotinThe 267fffba935SAlexander Motin.Va description 268fffba935SAlexander Motinpassed to and from node unchanged, together with ID providing 269fffba935SAlexander Motina way for several entities to concurrently manipulate 270fffba935SAlexander Motinredirections in automated way. 27117a35ce7SGleb Smirnoff.Sh SHUTDOWN 27217a35ce7SGleb SmirnoffThis node shuts down upon receipt of a 27317a35ce7SGleb Smirnoff.Dv NGM_SHUTDOWN 27417a35ce7SGleb Smirnoffcontrol message, or when both hooks are disconnected. 27517a35ce7SGleb Smirnoff.Sh EXAMPLES 276a4be0b3cSRuslan ErmilovIn the following example, the packets are injected into a 277a4be0b3cSRuslan Ermilov.Nm nat 278a4be0b3cSRuslan Ermilovnode using the 27917a35ce7SGleb Smirnoff.Xr ng_ipfw 4 28017a35ce7SGleb Smirnoffnode. 28117a35ce7SGleb Smirnoff.Bd -literal -offset indent 28217a35ce7SGleb Smirnoff# Create NAT node 28317a35ce7SGleb Smirnoffngctl mkpeer ipfw: nat 60 out 28417a35ce7SGleb Smirnoffngctl name ipfw:60 nat 28517a35ce7SGleb Smirnoffngctl connect ipfw: nat: 61 in 28617a35ce7SGleb Smirnoffngctl msg nat: setaliasaddr x.y.35.8 28717a35ce7SGleb Smirnoff 28817a35ce7SGleb Smirnoff# Divert traffic into NAT node 28917a35ce7SGleb Smirnoffipfw add 300 netgraph 61 all from any to any in via fxp0 29017a35ce7SGleb Smirnoffipfw add 400 netgraph 60 all from any to any out via fxp0 29117a35ce7SGleb Smirnoff 29217a35ce7SGleb Smirnoff# Let packets continue with after being (de)aliased 29317a35ce7SGleb Smirnoffsysctl net.inet.ip.fw.one_pass=0 29417a35ce7SGleb Smirnoff.Ed 29517a35ce7SGleb Smirnoff.Pp 296a4be0b3cSRuslan ErmilovThe 29717a35ce7SGleb Smirnoff.Nm 298a4be0b3cSRuslan Ermilovnode can be inserted right after the 299a4be0b3cSRuslan Ermilov.Xr ng_iface 4 300a4be0b3cSRuslan Ermilovnode in the graph. 301a4be0b3cSRuslan ErmilovIn the following example, we perform masquerading on a 30217a35ce7SGleb Smirnoffserial line with HDLC encapsulation. 30317a35ce7SGleb Smirnoff.Bd -literal -offset indent 30417a35ce7SGleb Smirnoff/usr/sbin/ngctl -f- <<-SEQ 30517a35ce7SGleb Smirnoff mkpeer cp0: cisco rawdata downstream 30617a35ce7SGleb Smirnoff name cp0:rawdata hdlc 30717a35ce7SGleb Smirnoff mkpeer hdlc: nat inet in 30817a35ce7SGleb Smirnoff name hdlc:inet nat 30917a35ce7SGleb Smirnoff mkpeer nat: iface out inet 31017a35ce7SGleb Smirnoff msg nat: setaliasaddr x.y.8.35 31117a35ce7SGleb SmirnoffSEQ 31217a35ce7SGleb Smirnoffifconfig ng0 x.y.8.35 x.y.8.1 31317a35ce7SGleb Smirnoff.Ed 31417a35ce7SGleb Smirnoff.Sh SEE ALSO 31517a35ce7SGleb Smirnoff.Xr libalias 3 , 31617a35ce7SGleb Smirnoff.Xr ng_ipfw 4 , 317a4be0b3cSRuslan Ermilov.Xr natd 8 , 31817a35ce7SGleb Smirnoff.Xr ngctl 8 31917a35ce7SGleb Smirnoff.Sh HISTORY 32017a35ce7SGleb SmirnoffThe 32117a35ce7SGleb Smirnoff.Nm 32217a35ce7SGleb Smirnoffnode type was implemented in 32317a35ce7SGleb Smirnoff.Fx 6.0 . 32417a35ce7SGleb Smirnoff.Sh AUTHORS 32517a35ce7SGleb Smirnoff.An Gleb Smirnoff Aq glebius@FreeBSD.org 326