1.\" $FreeBSD$ 2.Dd June 27, 2000 3.Dt NATD 8 4.Os 5.Sh NAME 6.Nm natd 7.Nd Network Address Translation daemon 8.Sh SYNOPSIS 9.Nm 10.Bk -words 11.Op Fl unregistered_only | u 12.Op Fl log | l 13.Op Fl proxy_only 14.Op Fl reverse 15.Op Fl deny_incoming | d 16.Op Fl use_sockets | s 17.Op Fl same_ports | m 18.Op Fl verbose | v 19.Op Fl dynamic 20.Op Fl in_port | i Ar port 21.Op Fl out_port | o Ar port 22.Op Fl port | p Ar port 23.Op Fl alias_address | a Ar address 24.Op Fl target_address | t Ar address 25.Op Fl interface | n Ar interface 26.Op Fl proxy_rule Ar proxyspec 27.Op Fl redirect_port Ar linkspec 28.Op Fl redirect_proto Ar linkspec 29.Op Fl redirect_address Ar linkspec 30.Op Fl config | f Ar configfile 31.Op Fl log_denied 32.Op Fl log_facility Ar facility_name 33.Op Fl punch_fw Ar firewall_range 34.Op Fl log_ipfw_denied 35.Ek 36.Sh DESCRIPTION 37The 38.Nm 39utility provides a Network Address Translation facility for use 40with 41.Xr divert 4 42sockets under 43.Fx . 44It is intended for use with NICs - if you want to do NAT on a PPP link, 45use the 46.Fl nat 47switch to 48.Xr ppp 8 . 49.Pp 50The 51.Nm 52utility normally runs in the background as a daemon. 53It is passed raw IP packets as they travel into and out of the machine, 54and will possibly change these before re-injecting them back into the 55IP packet stream. 56.Pp 57It changes all packets destined for another host so that their source 58IP number is that of the current machine. 59For each packet changed in this manner, an internal table entry is 60created to record this fact. 61The source port number is also changed to indicate the table entry 62applying to the packet. 63Packets that are received with a target IP of the current host are 64checked against this internal table. 65If an entry is found, it is used to determine the correct target IP 66number and port to place in the packet. 67.Pp 68The following command line options are available: 69.Bl -tag -width Fl 70.It Fl log | l 71Log various aliasing statistics and information to the file 72.Pa /var/log/alias.log . 73This file is truncated each time 74.Nm 75is started. 76.It Fl deny_incoming | d 77Do not pass incoming packets that have no 78entry in the internal translation table. 79.Pp 80If this option is not used, then such a packet will be altered 81using the rules in 82.Fl target_address 83below, and the entry will be made in the internal translation table. 84.It Fl log_denied 85Log denied incoming packets via 86.Xr syslog 3 87(see also 88.Fl log_facility ) . 89.It Fl log_facility Ar facility_name 90Use specified log facility when logging information via 91.Xr syslog 3 . 92Argument 93.Ar facility_name 94is one of the keywords specified in 95.Xr syslog.conf 5 . 96.It Fl use_sockets | s 97Allocate a 98.Xr socket 2 99in order to establish an FTP data or IRC DCC send connection. 100This option uses more system resources, but guarantees successful 101connections when port numbers conflict. 102.It Fl same_ports | m 103Try to keep the same port number when altering outgoing packets. 104With this option, protocols such as RPC will have a better chance 105of working. 106If it is not possible to maintain the port number, it will be silently 107changed as per normal. 108.It Fl verbose | v 109Do not call 110.Xr daemon 3 111on startup. 112Instead, stay attached to the controlling terminal and display all packet 113alterations to the standard output. 114This option should only be used for debugging purposes. 115.It Fl unregistered_only | u 116Only alter outgoing packets with an 117.Em unregistered 118source address. 119According to RFC 1918, unregistered source addresses are 10.0.0.0/8, 120172.16.0.0/12 and 192.168.0.0/16. 121.It Fl redirect_port Ar proto Xo 122.Ar targetIP Ns : Ns Xo 123.Ar targetPORT Ns Op - Ns Ar targetPORT Xc 124.Op Ar aliasIP Ns : Ns Xo 125.Ar aliasPORT Ns Op - Ns Ar aliasPORT Xc 126.Oo Ar remoteIP Ns Oo : Ns 127.Ar remotePORT Ns Op - Ns Ar remotePORT 128.Oc Oc 129.Xc 130Redirect incoming connections arriving to given port(s) to another host 131and port(s). 132Argument 133.Ar proto 134is either 135.Ar tcp 136or 137.Ar udp , 138.Ar targetIP 139is the desired target IP number, 140.Ar targetPORT 141is the desired target port number or range, 142.Ar aliasPORT 143is the requested port number or range, and 144.Ar aliasIP 145is the aliasing address. 146Arguments 147.Ar remoteIP 148and 149.Ar remotePORT 150can be used to specify the connection more accurately if necessary. 151The 152.Ar targetPORT 153range and 154.Ar aliasPORT 155range need not be the same numerically, but must have the same size. 156If 157.Ar remotePORT 158is not specified, it is assumed to be all ports. 159If 160.Ar remotePORT 161is specified, it must match the size of 162.Ar targetPORT , 163or be 0 (all ports). 164For example, the argument 165.Pp 166.Dl Ar tcp inside1:telnet 6666 167.Pp 168means that incoming TCP packets destined for port 6666 on this machine 169will be sent to the telnet port on the inside1 machine. 170.Pp 171.Dl Ar tcp inside2:2300-2399 3300-3399 172.Pp 173will redirect incoming connections on ports 3300-3399 to host 174inside2, ports 2300-2399. 175The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc. 176.It Fl redirect_proto Ar proto localIP Oo 177.Ar publicIP Op Ar remoteIP 178.Oc 179Redirect incoming IP packets of protocol 180.Ar proto 181(see 182.Xr protocols 5 ) 183destined for 184.Ar publicIP 185address to a 186.Ar localIP 187address and vice versa. 188.Pp 189If 190.Ar publicIP 191is not specified, then the default aliasing address is used. 192If 193.Ar remoteIP 194is specified, then only packets coming from/to 195.Ar remoteIP 196will match the rule. 197.It Fl redirect_address Ar localIP publicIP 198Redirect traffic for public IP address to a machine on the local 199network. 200This function is known as 201.Em static NAT . 202Normally static NAT is useful if your ISP has allocated a small block 203of IP addresses to you, but it can even be used in the case of single 204address: 205.Pp 206.Dl Ar redirect_address 10.0.0.8 0.0.0.0 207.Pp 208The above command would redirect all incoming traffic 209to machine 10.0.0.8. 210.Pp 211If several address aliases specify the same public address 212as follows 213.Bd -literal -offset indent 214.Ar redirect_address 192.168.0.2 public_addr 215.Ar redirect_address 192.168.0.3 public_addr 216.Ar redirect_address 192.168.0.4 public_addr 217.Ed 218.Pp 219the incoming traffic will be directed to the last 220translated local address (192.168.0.4), but outgoing 221traffic from the first two addresses will still be aliased 222to appear from the specified 223.Ar public_addr . 224.It Fl redirect_port Ar proto Xo 225.Ar targetIP Ns : Ns Xo 226.Ar targetPORT Ns Oo , Ns 227.Ar targetIP Ns : Ns Xo 228.Ar targetPORT Ns Oo , Ns 229.Ar ...\& 230.Oc Oc 231.Xc 232.Xc 233.Op Ar aliasIP Ns : Ns Xo 234.Ar aliasPORT 235.Xc 236.Oo Ar remoteIP Ns 237.Op : Ns Ar remotePORT 238.Oc 239.Xc 240.It Fl redirect_address Xo 241.Ar localIP Ns Oo , Ns 242.Ar localIP Ns Oo , Ns 243.Ar ...\& 244.Oc Oc 245.Ar publicIP 246.Xc 247These forms of 248.Fl redirect_port 249and 250.Fl redirect_address 251are used to transparently offload network load on a single server and 252distribute the load across a pool of servers. 253This function is known as 254.Em LSNAT 255(RFC 2391). 256For example, the argument 257.Pp 258.Dl Ar tcp www1:http,www2:http,www3:http www:http 259.Pp 260means that incoming HTTP requests for host www will be transparently 261redirected to one of the www1, www2 or www3, where a host is selected 262simply on a round-robin basis, without regard to load on the net. 263.It Fl dynamic 264If the 265.Fl n 266or 267.Fl interface 268option is used, 269.Nm 270will monitor the routing socket for alterations to the 271.Ar interface 272passed. 273If the interface's IP number is changed, 274.Nm 275will dynamically alter its concept of the alias address. 276.It Fl in_port | i Ar port 277Read from and write to 278.Xr divert 4 279port 280.Ar port , 281treating all packets as 282.Dq incoming . 283.It Fl out_port | o Ar port 284Read from and write to 285.Xr divert 4 286port 287.Ar port , 288treating all packets as 289.Dq outgoing . 290.It Fl port | p Ar port 291Read from and write to 292.Xr divert 4 293port 294.Ar port , 295distinguishing packets as 296.Dq incoming 297or 298.Dq outgoing 299using the rules specified in 300.Xr divert 4 . 301If 302.Ar port 303is not numeric, it is searched for in the 304.Xr services 5 305database. 306If this option is not specified, the divert port named 307.Ar natd 308will be used as a default. 309.It Fl alias_address | a Ar address 310Use 311.Ar address 312as the aliasing address. 313If this option is not specified, the 314.Fl interface 315option must be used. 316The specified address is usually the address assigned to the 317.Dq public 318network interface. 319.Pp 320All data passing 321.Em out 322will be rewritten with a source address equal to 323.Ar address . 324All data coming 325.Em in 326will be checked to see if it matches any already-aliased outgoing 327connection. 328If it does, the packet is altered accordingly. 329If not, all 330.Fl redirect_port , 331.Fl redirect_proto 332and 333.Fl redirect_address 334assignments are checked and actioned. 335If no other action can be made and if 336.Fl deny_incoming 337is not specified, the packet is delivered to the local machine 338using the rules specified in 339.Fl target_address 340option below. 341.It Fl t | target_address Ar address 342Set the target address. 343When an incoming packet not associated with any pre-existing link 344arrives at the host machine, it will be sent to the specified 345.Ar address . 346.Pp 347The target address may be set to 348.Ar 255.255.255.255 , 349in which case all new incoming packets go to the alias address set by 350.Fl alias_address 351or 352.Fl interface . 353.Pp 354If this option is not used, or called with the argument 355.Ar 0.0.0.0 , 356then all new incoming packets go to the address specified in 357the packet. 358This allows external machines to talk directly to internal machines if 359they can route packets to the machine in question. 360.It Fl interface | n Ar interface 361Use 362.Ar interface 363to determine the aliasing address. 364If there is a possibility that the IP number associated with 365.Ar interface 366may change, the 367.Fl dynamic 368option should also be used. 369If this option is not specified, the 370.Fl alias_address 371option must be used. 372.Pp 373The specified 374.Ar interface 375is usually the 376.Dq public 377(or 378.Dq external ) 379network interface. 380.It Fl config | f Ar file 381Read configuration from 382.Ar file . 383A 384.Ar file 385should contain a list of options, one per line, in the same form 386as the long form of the above command line options. 387For example, the line 388.Pp 389.Dl alias_address 158.152.17.1 390.Pp 391would specify an alias address of 158.152.17.1. 392Options that do not take an argument are specified with an argument of 393.Ar yes 394or 395.Ar no 396in the configuration file. 397For example, the line 398.Pp 399.Dl log yes 400.Pp 401is synonymous with 402.Fl log . 403.Pp 404Trailing spaces and empty lines are ignored. 405A 406.Ql \&# 407sign will mark the rest of the line as a comment. 408.It Fl reverse 409This option makes 410.Nm 411reverse the way it handles 412.Dq incoming 413and 414.Dq outgoing 415packets, allowing it to operate on the 416.Dq internal 417network interface rather than the 418.Dq external 419one. 420.Pp 421This can be useful in some transparent proxying situations 422when outgoing traffic is redirected to the local machine 423and 424.Nm 425is running on the internal interface (it usually runs on the 426external interface). 427.It Fl proxy_only 428Force 429.Nm 430to perform transparent proxying only. 431Normal address translation is not performed. 432.It Fl proxy_rule Xo 433.Op Ar type encode_ip_hdr | encode_tcp_stream 434.Ar port xxxx 435.Ar server a.b.c.d:yyyy 436.Xc 437Enable transparent proxying. 438Outgoing TCP packets with the given port going through this 439host to any other host are redirected to the given server and port. 440Optionally, the original target address can be encoded into the packet. 441Use 442.Ar encode_ip_hdr 443to put this information into the IP option field or 444.Ar encode_tcp_stream 445to inject the data into the beginning of the TCP stream. 446.It Fl punch_fw Xo 447.Ar basenumber Ns : Ns Ar count 448.Xc 449This option directs 450.Nm 451to 452.Dq punch holes 453in an 454.Xr ipfirewall 4 455based firewall for FTP/IRC DCC connections. 456This is done dynamically by installing temporary firewall rules which 457allow a particular connection (and only that connection) to go through 458the firewall. 459The rules are removed once the corresponding connection terminates. 460.Pp 461A maximum of 462.Ar count 463rules starting from the rule number 464.Ar basenumber 465will be used for punching firewall holes. 466The range will be cleared for all rules on startup. 467.It Fl log_ipfw_denied 468Log when a packet cannot be re-injected because an 469.Xr ipfw 8 470rule blocks it. 471This is the default with 472.Fl verbose . 473.El 474.Sh RUNNING NATD 475The following steps are necessary before attempting to run 476.Nm : 477.Bl -enum 478.It 479Build a custom kernel with the following options: 480.Bd -literal -offset indent 481options IPFIREWALL 482options IPDIVERT 483.Ed 484.Pp 485Refer to the handbook for detailed instructions on building a custom 486kernel. 487.It 488Ensure that your machine is acting as a gateway. 489This can be done by specifying the line 490.Pp 491.Dl gateway_enable=YES 492.Pp 493in the 494.Pa /etc/rc.conf 495file or using the command 496.Pp 497.Dl "sysctl net.inet.ip.forwarding=1" 498.Pp 499.It 500If you use the 501.Fl interface 502option, make sure that your interface is already configured. 503If, for example, you wish to specify 504.Ql tun0 505as your 506.Ar interface , 507and you are using 508.Xr ppp 8 509on that interface, you must make sure that you start 510.Nm ppp 511prior to starting 512.Nm . 513.El 514.Pp 515Running 516.Nm 517is fairly straight forward. 518The line 519.Pp 520.Dl natd -interface ed0 521.Pp 522should suffice in most cases (substituting the correct interface name). 523Please check 524.Xr rc.conf 5 525on how to configure it to be started automatically during boot. 526Once 527.Nm 528is running, you must ensure that traffic is diverted to 529.Nm : 530.Bl -enum 531.It 532You will need to adjust the 533.Pa /etc/rc.firewall 534script to taste. 535If you are not interested in having a firewall, the 536following lines will do: 537.Bd -literal -offset indent 538/sbin/ipfw -f flush 539/sbin/ipfw add divert natd all from any to any via ed0 540/sbin/ipfw add pass all from any to any 541.Ed 542.Pp 543The second line depends on your interface (change 544.Ql ed0 545as appropriate). 546.Pp 547You should be aware of the fact that, with these firewall settings, 548everyone on your local network can fake his source-address using your 549host as gateway. 550If there are other hosts on your local network, you are strongly 551encouraged to create firewall rules that only allow traffic to and 552from trusted hosts. 553.Pp 554If you specify real firewall rules, it is best to specify line 2 at 555the start of the script so that 556.Nm 557sees all packets before they are dropped by the firewall. 558.Pp 559After translation by 560.Nm , 561packets re-enter the firewall at the rule number following the rule number 562that caused the diversion (not the next rule if there are several at the 563same number). 564.It 565Enable your firewall by setting 566.Pp 567.Dl firewall_enable=YES 568.Pp 569in 570.Pa /etc/rc.conf . 571This tells the system startup scripts to run the 572.Pa /etc/rc.firewall 573script. 574If you do not wish to reboot now, just run this by hand from the console. 575NEVER run this from a remote session unless you put it into the background. 576If you do, you will lock yourself out after the flush takes place, and 577execution of 578.Pa /etc/rc.firewall 579will stop at this point - blocking all accesses permanently. 580Running the script in the background should be enough to prevent this 581disaster. 582.El 583.Sh SEE ALSO 584.Xr divert 4 , 585.Xr protocols 5 , 586.Xr rc.conf 5 , 587.Xr services 5 , 588.Xr syslog.conf 5 , 589.Xr ipfw 8 , 590.Xr ppp 8 591.Sh AUTHORS 592This program is the result of the efforts of many people at different 593times: 594.Pp 595.An Archie Cobbs Aq archie@FreeBSD.org 596(divert sockets) 597.An Charles Mott Aq cmott@scientech.com 598(packet aliasing) 599.An Eivind Eklund Aq perhaps@yes.no 600(IRC support & misc additions) 601.An Ari Suutari Aq suutari@iki.fi 602(natd) 603.An Dru Nelson Aq dnelson@redwoodsoft.com 604(early PPTP support) 605.An Brian Somers Aq brian@awfulhak.org 606(glue) 607.An Ruslan Ermilov Aq ru@FreeBSD.org 608(natd, packet aliasing, glue) 609