1.\" $FreeBSD$ 2.Dd June 23, 2008 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 instance Ar instancename 32.Op Fl globalport Ar port 33.Op Fl log_denied 34.Op Fl log_facility Ar facility_name 35.Op Fl punch_fw Ar firewall_range 36.Op Fl skinny_port Ar port 37.Op Fl log_ipfw_denied 38.Op Fl pid_file | P Ar pidfile 39.Op Fl exit_delay | P Ar ms 40.Ek 41.Sh DESCRIPTION 42The 43.Nm 44utility provides a Network Address Translation facility for use 45with 46.Xr divert 4 47sockets under 48.Fx . 49.Pp 50(If you need NAT on a PPP link, 51.Xr ppp 8 52provides the 53.Fl nat 54option that gives most of the 55.Nm 56functionality, and uses the same 57.Xr libalias 3 58library.) 59.Pp 60The 61.Nm 62utility normally runs in the background as a daemon. 63It is passed raw IP packets as they travel into and out of the machine, 64and will possibly change these before re-injecting them back into the 65IP packet stream. 66.Pp 67It changes all packets destined for another host so that their source 68IP address is that of the current machine. 69For each packet changed in this manner, an internal table entry is 70created to record this fact. 71The source port number is also changed to indicate the table entry 72applying to the packet. 73Packets that are received with a target IP of the current host are 74checked against this internal table. 75If an entry is found, it is used to determine the correct target IP 76address and port to place in the packet. 77.Pp 78The following command line options are available: 79.Bl -tag -width Fl 80.It Fl log | l 81Log various aliasing statistics and information to the file 82.Pa /var/log/alias.log . 83This file is truncated each time 84.Nm 85is started. 86.It Fl deny_incoming | d 87Do not pass incoming packets that have no 88entry in the internal translation table. 89.Pp 90If this option is not used, then such a packet will be altered 91using the rules in 92.Fl target_address 93below, and the entry will be made in the internal translation table. 94.It Fl log_denied 95Log denied incoming packets via 96.Xr syslog 3 97(see also 98.Fl log_facility ) . 99.It Fl log_facility Ar facility_name 100Use specified log facility when logging information via 101.Xr syslog 3 . 102Argument 103.Ar facility_name 104is one of the keywords specified in 105.Xr syslog.conf 5 . 106.It Fl use_sockets | s 107Allocate a 108.Xr socket 2 109in order to establish an FTP data or IRC DCC send connection. 110This option uses more system resources, but guarantees successful 111connections when port numbers conflict. 112.It Fl same_ports | m 113Try to keep the same port number when altering outgoing packets. 114With this option, protocols such as RPC will have a better chance 115of working. 116If it is not possible to maintain the port number, it will be silently 117changed as per normal. 118.It Fl verbose | v 119Do not call 120.Xr daemon 3 121on startup. 122Instead, stay attached to the controlling terminal and display all packet 123alterations to the standard output. 124This option should only be used for debugging purposes. 125.It Fl unregistered_only | u 126Only alter outgoing packets with an 127.Em unregistered 128source address. 129According to RFC 1918, unregistered source addresses are 10.0.0.0/8, 130172.16.0.0/12 and 192.168.0.0/16. 131.It Fl redirect_port Ar proto Xo 132.Ar targetIP Ns : Ns Xo 133.Ar targetPORT Ns Oo - Ns Ar targetPORT Oc Xc 134.Oo Ar aliasIP Ns : Oc Ns Xo 135.Ar aliasPORT Ns Oo - Ns Ar aliasPORT Oc Xc 136.Oo Ar remoteIP Ns Oo : Ns 137.Ar remotePORT Ns Op - Ns Ar remotePORT 138.Oc Oc 139.Xc 140Redirect incoming connections arriving to given port(s) to another host 141and port(s). 142Argument 143.Ar proto 144is either 145.Ar tcp 146or 147.Ar udp , 148.Ar targetIP 149is the desired target IP address, 150.Ar targetPORT 151is the desired target port number or range, 152.Ar aliasPORT 153is the requested port number or range, and 154.Ar aliasIP 155is the aliasing address. 156Arguments 157.Ar remoteIP 158and 159.Ar remotePORT 160can be used to specify the connection more accurately if necessary. 161If 162.Ar remotePORT 163is not specified, it is assumed to be all ports. 164.Pp 165Arguments 166.Ar targetIP , aliasIP 167and 168.Ar remoteIP 169can be given as IP addresses or as hostnames. 170The 171.Ar targetPORT , aliasPORT 172and 173.Ar remotePORT 174ranges need not be the same numerically, but must have the same size. 175When 176.Ar targetPORT , aliasPORT 177or 178.Ar remotePORT 179specifies a singular value (not a range), it can be given as a service 180name that is searched for in the 181.Xr services 5 182database. 183.Pp 184For example, the argument 185.Pp 186.Dl Ar tcp inside1:telnet 6666 187.Pp 188means that incoming TCP packets destined for port 6666 on this machine 189will be sent to the telnet port on the inside1 machine. 190.Pp 191.Dl Ar tcp inside2:2300-2399 3300-3399 192.Pp 193will redirect incoming connections on ports 3300-3399 to host 194inside2, ports 2300-2399. 195The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc. 196.It Fl redirect_proto Ar proto localIP Oo 197.Ar publicIP Op Ar remoteIP 198.Oc 199Redirect incoming IP packets of protocol 200.Ar proto 201(see 202.Xr protocols 5 ) 203destined for 204.Ar publicIP 205address to a 206.Ar localIP 207address and vice versa. 208.Pp 209If 210.Ar publicIP 211is not specified, then the default aliasing address is used. 212If 213.Ar remoteIP 214is specified, then only packets coming from/to 215.Ar remoteIP 216will match the rule. 217.It Fl redirect_address Ar localIP publicIP 218Redirect traffic for public IP address to a machine on the local 219network. 220This function is known as 221.Em static NAT . 222Normally static NAT is useful if your ISP has allocated a small block 223of IP addresses to you, but it can even be used in the case of single 224address: 225.Pp 226.Dl Ar redirect_address 10.0.0.8 0.0.0.0 227.Pp 228The above command would redirect all incoming traffic 229to machine 10.0.0.8. 230.Pp 231If several address aliases specify the same public address 232as follows 233.Bd -literal -offset indent 234redirect_address 192.168.0.2 public_addr 235redirect_address 192.168.0.3 public_addr 236redirect_address 192.168.0.4 public_addr 237.Ed 238.Pp 239the incoming traffic will be directed to the last 240translated local address (192.168.0.4), but outgoing 241traffic from the first two addresses will still be aliased 242to appear from the specified 243.Ar public_addr . 244.It Fl redirect_port Ar proto Xo 245.Ar targetIP Ns : Ns Xo 246.Ar targetPORT Ns Oo , Ns 247.Ar targetIP Ns : Ns Xo 248.Ar targetPORT Ns Oo , Ns 249.Ar ...\& 250.Oc Xc Oc Xc 251.Oo Ar aliasIP Ns : Oc Ns Xo 252.Ar aliasPORT 253.Xc 254.Oo Ar remoteIP Ns 255.Op : Ns Ar remotePORT 256.Oc 257.Xc 258.It Fl redirect_address Xo 259.Ar localIP Ns Oo , Ns 260.Ar localIP Ns Oo , Ns 261.Ar ...\& 262.Oc Oc 263.Ar publicIP 264.Xc 265These forms of 266.Fl redirect_port 267and 268.Fl redirect_address 269are used to transparently offload network load on a single server and 270distribute the load across a pool of servers. 271This function is known as 272.Em LSNAT 273(RFC 2391). 274For example, the argument 275.Pp 276.Dl Ar tcp www1:http,www2:http,www3:http www:http 277.Pp 278means that incoming HTTP requests for host www will be transparently 279redirected to one of the www1, www2 or www3, where a host is selected 280simply on a round-robin basis, without regard to load on the net. 281.It Fl dynamic 282If the 283.Fl n 284or 285.Fl interface 286option is used, 287.Nm 288will monitor the routing socket for alterations to the 289.Ar interface 290passed. 291If the interface's IP address is changed, 292.Nm 293will dynamically alter its concept of the alias address. 294.It Fl in_port | i Ar port 295Read from and write to 296.Xr divert 4 297port 298.Ar port , 299treating all packets as 300.Dq incoming . 301.It Fl out_port | o Ar port 302Read from and write to 303.Xr divert 4 304port 305.Ar port , 306treating all packets as 307.Dq outgoing . 308.It Fl port | p Ar port 309Read from and write to 310.Xr divert 4 311port 312.Ar port , 313distinguishing packets as 314.Dq incoming 315or 316.Dq outgoing 317using the rules specified in 318.Xr divert 4 . 319If 320.Ar port 321is not numeric, it is searched for in the 322.Xr services 5 323database. 324If this option is not specified, the divert port named 325.Ar natd 326will be used as a default. 327.It Fl alias_address | a Ar address 328Use 329.Ar address 330as the aliasing address. 331Either this or the 332.Fl interface 333option must be used (but not both), 334if the 335.Fl proxy_only 336option is not specified. 337The specified address is usually the address assigned to the 338.Dq public 339network interface. 340.Pp 341All data passing 342.Em out 343will be rewritten with a source address equal to 344.Ar address . 345All data coming 346.Em in 347will be checked to see if it matches any already-aliased outgoing 348connection. 349If it does, the packet is altered accordingly. 350If not, all 351.Fl redirect_port , 352.Fl redirect_proto 353and 354.Fl redirect_address 355assignments are checked and actioned. 356If no other action can be made and if 357.Fl deny_incoming 358is not specified, the packet is delivered to the local machine 359using the rules specified in 360.Fl target_address 361option below. 362.It Fl t | target_address Ar address 363Set the target address. 364When an incoming packet not associated with any pre-existing link 365arrives at the host machine, it will be sent to the specified 366.Ar address . 367.Pp 368The target address may be set to 369.Ar 255.255.255.255 , 370in which case all new incoming packets go to the alias address set by 371.Fl alias_address 372or 373.Fl interface . 374.Pp 375If this option is not used, or called with the argument 376.Ar 0.0.0.0 , 377then all new incoming packets go to the address specified in 378the packet. 379This allows external machines to talk directly to internal machines if 380they can route packets to the machine in question. 381.It Fl interface | n Ar interface 382Use 383.Ar interface 384to determine the aliasing address. 385If there is a possibility that the IP address associated with 386.Ar interface 387may change, the 388.Fl dynamic 389option should also be used. 390If this option is not specified, the 391.Fl alias_address 392option must be used. 393.Pp 394The specified 395.Ar interface 396is usually the 397.Dq public 398(or 399.Dq external ) 400network interface. 401.It Fl config | f Ar file 402Read configuration from 403.Ar file . 404A 405.Ar file 406should contain a list of options, one per line, in the same form 407as the long form of the above command line options. 408For example, the line 409.Pp 410.Dl alias_address 158.152.17.1 411.Pp 412would specify an alias address of 158.152.17.1. 413Options that do not take an argument are specified with an argument of 414.Ar yes 415or 416.Ar no 417in the configuration file. 418For example, the line 419.Pp 420.Dl log yes 421.Pp 422is synonymous with 423.Fl log . 424.Pp 425Options can be divided to several sections. 426Each section applies to own 427.Nm 428instance. 429This ability allows to configure one 430.Nm 431process for several NAT instances. 432The first instance that always exists is a "default" instance. 433Each another instance should begin with 434.Pp 435.Dl instance Ar instance_name 436.Pp 437At the next should be placed a configuration option. 438Example: 439.Pp 440.Dl \&# default instance 441.Dl port 8668 442.Dl alias_address 158.152.17.1 443.Pp 444.Dl \&# second instance 445.Dl instance dsl1 446.Dl port 8888 447.Dl alias_address 192.168.0.1 448.Pp 449Trailing spaces and empty lines are ignored. 450A 451.Ql \&# 452sign will mark the rest of the line as a comment. 453.It Fl instance Ar instancename 454This option switches command line options processing to configure instance 455.Ar instancename 456(creating it if necessary) till the next 457.Fl instance 458option or end of command line. 459It is easier to set up multiple instances in the configuration file 460specified with the 461.Fl config 462option rather than on a command line. 463.It Fl globalport Ar port 464Read from and write to 465.Xr divert 4 466port 467.Ar port , 468treating all packets as 469.Dq outgoing . 470This option is intended to be used with multiple instances: 471packets received on this port are checked against 472internal translation tables of every configured instance. 473If an entry is found, packet is aliased according to that entry. 474If no entry was found in any of the instances, packet is passed 475unchanged, and no new entry will be created. 476See the section 477.Sx MULTIPLE INSTANCES 478for more details. 479.It Fl reverse 480This option makes 481.Nm 482reverse the way it handles 483.Dq incoming 484and 485.Dq outgoing 486packets, allowing it to operate on the 487.Dq internal 488network interface rather than the 489.Dq external 490one. 491.Pp 492This can be useful in some transparent proxying situations 493when outgoing traffic is redirected to the local machine 494and 495.Nm 496is running on the internal interface (it usually runs on the 497external interface). 498.It Fl proxy_only 499Force 500.Nm 501to perform transparent proxying only. 502Normal address translation is not performed. 503.It Fl proxy_rule Xo 504.Op Ar type encode_ip_hdr | encode_tcp_stream 505.Ar port xxxx 506.Ar server a.b.c.d:yyyy 507.Xc 508Enable transparent proxying. 509Outgoing TCP packets with the given port going through this 510host to any other host are redirected to the given server and port. 511Optionally, the original target address can be encoded into the packet. 512Use 513.Ar encode_ip_hdr 514to put this information into the IP option field or 515.Ar encode_tcp_stream 516to inject the data into the beginning of the TCP stream. 517.It Fl punch_fw Xo 518.Ar basenumber Ns : Ns Ar count 519.Xc 520This option directs 521.Nm 522to 523.Dq punch holes 524in an 525.Xr ipfirewall 4 526based firewall for FTP/IRC DCC connections. 527This is done dynamically by installing temporary firewall rules which 528allow a particular connection (and only that connection) to go through 529the firewall. 530The rules are removed once the corresponding connection terminates. 531.Pp 532A maximum of 533.Ar count 534rules starting from the rule number 535.Ar basenumber 536will be used for punching firewall holes. 537The range will be cleared for all rules on startup. 538This option has no effect when the kernel is in security 539level 3, see 540.Xr init 8 541for more information. 542.It Fl skinny_port Ar port 543This option allows you to specify the TCP port used for 544the Skinny Station protocol. 545Skinny is used by Cisco IP phones to communicate with 546Cisco Call Managers to set up voice over IP calls. 547By default, Skinny aliasing is not performed. 548The typical port value for Skinny is 2000. 549.It Fl log_ipfw_denied 550Log when a packet cannot be re-injected because an 551.Xr ipfw 8 552rule blocks it. 553This is the default with 554.Fl verbose . 555.It Fl pid_file | P Ar file 556Specify an alternate file in which to store the process ID. 557The default is 558.Pa /var/run/natd.pid . 559.It Fl exit_delay Ar ms 560Specify delay in ms before daemon exit after signal. 561The default is 562.Pa 10000 . 563.El 564.Sh RUNNING NATD 565The following steps are necessary before attempting to run 566.Nm : 567.Bl -enum 568.It 569Build a custom kernel with the following options: 570.Bd -literal -offset indent 571options IPFIREWALL 572options IPDIVERT 573.Ed 574.Pp 575Refer to the handbook for detailed instructions on building a custom 576kernel. 577.It 578Ensure that your machine is acting as a gateway. 579This can be done by specifying the line 580.Pp 581.Dl gateway_enable=YES 582.Pp 583in the 584.Pa /etc/rc.conf 585file or using the command 586.Pp 587.Dl "sysctl net.inet.ip.forwarding=1" 588.Pp 589.It 590If you use the 591.Fl interface 592option, make sure that your interface is already configured. 593If, for example, you wish to specify 594.Ql tun0 595as your 596.Ar interface , 597and you are using 598.Xr ppp 8 599on that interface, you must make sure that you start 600.Nm ppp 601prior to starting 602.Nm . 603.El 604.Pp 605Running 606.Nm 607is fairly straight forward. 608The line 609.Pp 610.Dl natd -interface ed0 611.Pp 612should suffice in most cases (substituting the correct interface name). 613Please check 614.Xr rc.conf 5 615on how to configure it to be started automatically during boot. 616Once 617.Nm 618is running, you must ensure that traffic is diverted to 619.Nm : 620.Bl -enum 621.It 622You will need to adjust the 623.Pa /etc/rc.firewall 624script to taste. 625If you are not interested in having a firewall, the 626following lines will do: 627.Bd -literal -offset indent 628/sbin/ipfw -f flush 629/sbin/ipfw add divert natd all from any to any via ed0 630/sbin/ipfw add pass all from any to any 631.Ed 632.Pp 633The second line depends on your interface (change 634.Ql ed0 635as appropriate). 636.Pp 637You should be aware of the fact that, with these firewall settings, 638everyone on your local network can fake his source-address using your 639host as gateway. 640If there are other hosts on your local network, you are strongly 641encouraged to create firewall rules that only allow traffic to and 642from trusted hosts. 643.Pp 644If you specify real firewall rules, it is best to specify line 2 at 645the start of the script so that 646.Nm 647sees all packets before they are dropped by the firewall. 648.Pp 649After translation by 650.Nm , 651packets re-enter the firewall at the rule number following the rule number 652that caused the diversion (not the next rule if there are several at the 653same number). 654.It 655Enable your firewall by setting 656.Pp 657.Dl firewall_enable=YES 658.Pp 659in 660.Pa /etc/rc.conf . 661This tells the system startup scripts to run the 662.Pa /etc/rc.firewall 663script. 664If you do not wish to reboot now, just run this by hand from the console. 665NEVER run this from a remote session unless you put it into the background. 666If you do, you will lock yourself out after the flush takes place, and 667execution of 668.Pa /etc/rc.firewall 669will stop at this point - blocking all accesses permanently. 670Running the script in the background should be enough to prevent this 671disaster. 672.El 673.Sh MULTIPLE INSTANCES 674It is not so uncommon to have a need of aliasing to several external IP 675addresses. 676While this traditionally was achieved by running several 677.Nm 678processes with independent configurations, 679.Nm 680can have multiple aliasing instances in a single process, 681also allowing them to be not so independent of each other. 682For example, let us see a common task of load balancing two 683channels to different providers on a machine with two external 684interfaces 685.Ql sis0 686(with IP 1.2.3.4) and 687.Ql sis2 688(with IP 2.3.4.5): 689.Bd -literal -offset indent 690 net 1.2.3.0/24 6911.2.3.1 ------------------ sis0 692(router) (1.2.3.4) 693 net 10.0.0.0/24 694 sis1 ------------------- 10.0.0.2 695 (10.0.0.1) 696 net 2.3.4.0/24 6972.3.4.1 ------------------ sis2 698(router) (2.3.4.5) 699.Ed 700.Pp 701Default route is out via 702.Ql sis0 . 703.Pp 704Interior machine (10.0.0.2) is accessible on TCP port 122 through 705both exterior IPs, and outgoing connections choose a path randomly 706between 707.Ql sis0 708and 709.Ql sis2 . 710.Pp 711The way this works is that 712.Pa natd.conf 713builds two instances of the aliasing engine. 714.Pp 715In addition to these instances' private 716.Xr divert 4 717sockets, a third socket called the 718.Dq globalport 719is created; packets sent to 720.Nm 721via this one will be matched against all instances and translated 722if an existing entry is found, and unchanged if no entry is found. 723The following lines are placed into 724.Pa /etc/natd.conf : 725.Bd -literal -offset indent 726log 727deny_incoming 728verbose 729 730instance default 731interface sis0 732port 1000 733redirect_port tcp 10.0.0.2:122 122 734 735instance sis2 736interface sis2 737port 2000 738redirect_port tcp 10.0.0.2:122 122 739 740globalport 3000 741.Ed 742.Pp 743And the following 744.Xr ipfw 8 745rules are used: 746.Bd -literal -offset indent 747ipfw -f flush 748 749ipfw add allow ip from any to any via sis1 750 751ipfw add skipto 1000 ip from any to any in via sis0 752ipfw add skipto 2000 ip from any to any out via sis0 753ipfw add skipto 3000 ip from any to any in via sis2 754ipfw add skipto 4000 ip from any to any out via sis2 755 756ipfw add 1000 count ip from any to any 757 758ipfw add divert 1000 ip from any to any 759ipfw add allow ip from any to any 760 761ipfw add 2000 count ip from any to any 762 763ipfw add divert 3000 ip from any to any 764 765ipfw add allow ip from 1.2.3.4 to any 766ipfw add skipto 5000 ip from 2.3.4.5 to any 767 768ipfw add prob .5 skipto 4000 ip from any to any 769 770ipfw add divert 1000 ip from any to any 771ipfw add allow ip from any to any 772 773ipfw add 3000 count ip from any to any 774 775ipfw add divert 2000 ip from any to any 776ipfw add allow ip from any to any 777 778ipfw add 4000 count ip from any to any 779 780ipfw add divert 2000 ip from any to any 781 782ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24 783ipfw add allow ip from any to any 784.Ed 785.Pp 786Here the packet from internal network to Internet goes out via 787.Ql sis0 788(rule number 2000) and gets caught by the 789.Ic globalport 790socket (3000). 791After that, either a match is found in a translation table 792of one of the two instances, or the packet is passed to one 793of the two other 794.Xr divert 4 795ports (1000 or 2000), with equal probability. 796This ensures that load balancing is done on a per-flow basis 797(i.e., packets from a single TCP connection always flow through the 798same interface). 799Translated packets with source IP of a non-default interface 800.Pq Ql sis2 801are forwarded to the appropriate router on that interface. 802.Sh SEE ALSO 803.Xr libalias 3 , 804.Xr divert 4 , 805.Xr protocols 5 , 806.Xr rc.conf 5 , 807.Xr services 5 , 808.Xr syslog.conf 5 , 809.Xr init 8 , 810.Xr ipfw 8 , 811.Xr ppp 8 812.Sh AUTHORS 813This program is the result of the efforts of many people at different 814times: 815.Pp 816.An Archie Cobbs Aq archie@FreeBSD.org 817(divert sockets) 818.An Charles Mott Aq cm@linktel.net 819(packet aliasing) 820.An Eivind Eklund Aq perhaps@yes.no 821(IRC support & misc additions) 822.An Ari Suutari Aq suutari@iki.fi 823(natd) 824.An Dru Nelson Aq dnelson@redwoodsoft.com 825(early PPTP support) 826.An Brian Somers Aq brian@awfulhak.org 827(glue) 828.An Ruslan Ermilov Aq ru@FreeBSD.org 829(natd, packet aliasing, glue) 830.An Poul-Henning Kamp Aq phk@FreeBSD.org 831(multiple instances) 832