1.\" manual page [] for natd 1.4 2.\" $FreeBSD$ 3.Dd 15 April 1997 4.Os FreeBSD 5.Dt NATD 8 6.Sh NAME 7.Nm natd 8.Nd 9Network Address Translation Daemon 10.Sh SYNOPSIS 11.Nm 12.Op Fl ldsmvu 13.Op Fl dynamic 14.Op Fl i Ar inport 15.Op Fl o Ar outport 16.Op Fl p Ar port 17.Op Fl a Ar address 18.Op Fl n Ar interface 19.Op Fl f Ar configfile 20 21.Nm 22.Op Fl log 23.Op Fl deny_incoming 24.Op Fl log_denied 25.Op Fl use_sockets 26.Op Fl same_ports 27.Op Fl verbose 28.Op Fl log_facility Ar facility_name 29.Op Fl unregistered_only 30.Op Fl dynamic 31.Op Fl inport Ar inport 32.Op Fl outport Ar outport 33.Op Fl port Ar port 34.Op Fl alias_address Ar address 35.Op Fl interface Ar interface 36.Op Fl config Ar configfile 37.Op Fl redirect_port Ar linkspec 38.Op Fl redirect_address Ar localIP publicIP 39.Op Fl reverse 40.Op Fl proxy_only 41.Op Fl proxy_rule Ar proxyspec 42.Op Fl pptpalias Ar localIP 43 44.Sh DESCRIPTION 45This program provides a Network Address Translation facility for use 46with 47.Xr divert 4 48sockets under FreeBSD. It is intended for use with NICs - if you want 49to do NAT on a PPP link, use the -alias switch to 50.Xr ppp 8 . 51 52.Pp 53.Nm Natd 54normally runs in the background as a daemon. It is passed raw IP packets 55as they travel into and out of the machine, and will possibly change these 56before re-injecting them back into the IP packet stream. 57 58.Pp 59.Nm Natd 60changes all packets destined for another host so that their source 61IP number is that of the current machine. For each packet changed 62in this manner, an internal table entry is created to record this 63fact. The source port number is also changed to indicate the 64table entry applying to the packet. Packets that are received with 65a target IP of the current host are checked against this internal 66table. If an entry is found, it is used to determine the correct 67target IP number and port to place in the packet. 68 69.Pp 70The following command line options are available. 71.Bl -tag -width Fl 72 73.It Fl log | l 74Log various aliasing statistics and information to the file 75.Pa /var/log/alias.log . 76This file is truncated each time natd is started. 77 78.It Fl deny_incoming | d 79Reject packets destined for the current IP number that have no entry 80in the internal translation table. 81 82.It Fl log_denied 83Log denied incoming packets via syslog (see also log_facility) 84 85.It Fl log_facility Ar facility_name 86Use specified log facility when logging information via syslog. 87Facility names are as in 88.Xr syslog.conf 5 89 90.It Fl use_sockets | s 91Allocate a 92.Xr socket 2 93in order to establish an FTP data or IRC DCC send connection. This 94option uses more system resources, but guarantees successful connections 95when port numbers conflict. 96 97.It Fl same_ports | m 98Try to keep the same port number when altering outgoing packets. 99With this option, protocols such as RPC will have a better chance 100of working. If it is not possible to maintain the port number, it 101will be silently changed as per normal. 102 103.It Fl verbose | v 104Don't call 105.Xr fork 2 106or 107.Xr daemon 3 108on startup. Instead, stay attached to the controling terminal and 109display all packet alterations to the standard output. This option 110should only be used for debugging purposes. 111 112.It Fl unregistered_only | u 113Only alter outgoing packets with an unregistered source address. 114According to rfc 1918, unregistered source addresses are 10.0.0.0/8, 115172.16.0.0/12 and 192.168.0.0/16. 116 117.It Fl redirect_port Ar proto targetIP:targetPORT[-targetPORT] [aliasIP:]aliasPORT[-aliasPORT] [remoteIP[:remotePORT[-remotePORT]]] 118Redirect incoming connections arriving to given port(s) to another host 119and port(s). 120Proto is either tcp or udp, targetIP is the desired target IP 121number, targetPORT is the desired target PORT number or range, aliasPORT 122is the requested PORT number or range, and aliasIP is the aliasing address. 123RemoteIP and remotePORT can be used to specify the connection 124more accurately if necessary. 125The targetPORT range and aliasPORT range need not be the same numerically, 126but must have the same size. 127If remotePORT is not specified, it is assumed to be all ports. 128If remotePORT is specified, it must match the size of targetPORT, or be 0 129(all ports). 130For example, the argument 131 132.Dl Ar tcp inside1:telnet 6666 133 134means that incoming tcp packets destined for port 6666 on this machine will 135be sent to the telnet port on the inside1 machine. 136 137.Dl Ar tcp inside2:2300-2399 3300-3399 138 139will redirect incoming connections on ports 3300-3399 to host 140inside2, ports 2300-2399. 141The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc. 142 143.It Fl redirect_address Ar localIP publicIP 144Redirect traffic for public IP address to a machine on the local 145network. This function is known as "static NAT". Normally static NAT 146is useful if your ISP has allocated a small block of IP addresses to you, 147but it can even be used in the case of single address: 148 149 redirect_address 10.0.0.8 0.0.0.0 150 151The above command would redirect all incoming traffic 152to machine 10.0.0.8. 153 154If several address aliases specify the same public address 155as follows 156 157 redirect_address 192.168.0.2 public_addr 158 redirect_address 192.168.0.3 public_addr 159 redirect_address 192.168.0.4 public_addr 160 161the incoming traffic will be directed to the last 162translated local address (192.168.0.4), but outgoing 163traffic to the first two addresses will still be aliased 164to specified public address. 165 166.It Fl dynamic 167If the 168.Fl n 169or 170.Fl interface 171option is used, 172.Nm 173will monitor the routing socket for alterations to the 174.Ar interface 175passed. If the interfaces IP number is changed, 176.Nm 177will dynamically alter its concept of the alias address. 178 179.It Fl i | inport Ar inport 180Read from and write to 181.Ar inport , 182treating all packets as packets coming into the machine. 183 184.It Fl o | outport Ar outport 185Read from and write to 186.Ar outport , 187treating all packets as packets going out of the machine. 188 189.It Fl p | port Ar port 190Read from and write to 191.Ar port , 192distinguishing packets as incoming our outgoing using the rules specified in 193.Xr divert 4 . 194If 195.Ar port 196is not numeric, it is searched for in the 197.Pa /etc/services 198database using the 199.Xr getservbyname 3 200function. If this flag is not specified, the divert port named natd will 201be used as a default. An example entry in the 202.Pa /etc/services 203database would be: 204 205 natd 8668/divert # Network Address Translation socket 206 207Refer to 208.Xr services 5 209for further details. 210 211.It Fl a | alias_address Ar address 212Use 213.Ar address 214as the alias address. If this option is not specified, the 215.Fl n 216or 217.Fl interface 218option must be used. The specified address should be the address assigned 219to the public network interface. 220.Pp 221All data passing out through this addresses interface will be rewritten 222with a source address equal to 223.Ar address . 224All data arriving at the interface from outside will be checked to 225see if it matches any already-aliased outgoing connection. If it does, 226the packet is altered accordingly. If not, all 227.Fl redirect_port 228and 229.Fl redirect_address 230assignments are checked and actioned. If no other action can be made, 231and if 232.Fl deny_incoming 233is not specified, the packet is delivered to the local machine and port 234as specified in the packet. 235 236.It Fl n | interface Ar interface 237Use 238.Ar interface 239to determine the alias address. If there is a possibility that the 240IP number associated with 241.Ar interface 242may change, the 243.Fl dynamic 244flag should also be used. If this option is not specified, the 245.Fl a 246or 247.Fl alias_address 248flag must be used. 249.Pp 250The specified 251.Ar interface 252must be the public network interface. 253.It Fl f | config Ar configfile 254Read configuration from 255.Ar configfile . 256.Ar Configfile 257contains a list of options, one per line in the same form as the 258long form of the above command line flags. For example, the line 259 260 alias_address 158.152.17.1 261 262would specify an alias address of 158.152.17.1. Options that don't 263take an argument are specified with an option of 264.Ar yes 265or 266.Ar no 267in the configuration file. For example, the line 268 269 log yes 270 271is synonomous with 272.Fl log . 273.Pp 274Trailing spaces and empty lines are ignored. 275A 276.Ql \&# 277sign will mark the rest of the line as a comment. 278 279.It Fl reverse 280Reverse operation of natd. This can be useful in some 281transparent proxying situations when outgoing traffic 282is redirected to the local machine and natd is running on the 283incoming interface (it usually runs on the outgoing interface). 284 285.It Fl proxy_only 286Force natd to perform transparent proxying 287only. Normal address translation is not performed. 288 289.It Fl proxy_rule Ar [type encode_ip_hdr|encode_tcp_stream] port xxxx server a.b.c.d:yyyy 290Enable transparent proxying. Packets with the given port going through this 291host to any other host are redirected to the given server and port. 292Optionally, the original target address can be encoded into the packet. Use 293.Dq encode_ip_hdr 294to put this information into the IP option field or 295.Dq encode_tcp_stream 296to inject the data into the beginning of the TCP stream. 297 298.It Fl pptpalias Ar localIP 299Allow PPTP packets to go to the defined localIP address. PPTP is a VPN or secure 300IP tunneling technology being developed primarily by Microsoft. For its encrypted traffic, 301it uses an old IP encapsulation protocol called GRE (47). This 302natd option will translate any traffic of this protocol to a 303single, specified IP address. This would allow either one client or one server 304to be serviced with natd. If you are setting up a server, don't forget to allow the TCP traffic 305for the PPTP setup. For a client or server, you must allow GRE (protocol 47) if you have firewall lists active. 306 307.El 308 309.Sh RUNNING NATD 310The following steps are necessary before attempting to run 311.Nm natd : 312 313.Bl -enum 314.It 315Get FreeBSD version 2.2 or higher. Versions before this do not support 316.Xr divert 4 317sockets. 318 319.It 320Build a custom kernel with the following options: 321 322 options IPFIREWALL 323 options IPDIVERT 324 325Refer to the handbook for detailed instructions on building a custom 326kernel. 327 328.It 329Ensure that your machine is acting as a gateway. This can be done by 330specifying the line 331 332 gateway_enable=YES 333 334in 335.Pa /etc/rc.conf , 336or using the command 337 338 sysctl -w net.inet.ip.forwarding=1 339 340.It 341If you wish to use the 342.Fl n 343or 344.Fl interface 345flags, make sure that your interface is already configured. If, for 346example, you wish to specify tun0 as your 347.Ar interface , 348and you're using 349.Xr ppp 8 350on that interface, you must make sure that you start 351.Nm ppp 352prior to starting 353.Nm natd . 354 355.It 356Create an entry in 357.Pa /etc/services : 358 359 natd 8668/divert # Network Address Translation socket 360 361This gives a default for the 362.Fl p 363or 364.Fl port 365flag. 366 367.El 368.Pp 369Running 370.Nm 371is fairly straight forward. The line 372 373 natd -interface ed0 374 375should suffice in most cases (substituting the correct interface name). Once 376.Nm 377is running, you must ensure that traffic is diverted to natd: 378 379.Bl -enum 380.It 381You will need to adjust the 382.Pa /etc/rc.firewall 383script to taste. If you're not interested in having a firewall, the 384following lines will do: 385 386 /sbin/ipfw -f flush 387 /sbin/ipfw add divert natd all from any to any via ed0 388 /sbin/ipfw add pass all from any to any 389 390The second line depends on your interface (change ed0 as appropriate) 391and assumes that you've updated 392.Pa /etc/services 393with the natd entry as above. If you specify real firewall rules, it's 394best to specify line 2 at the start of the script so that 395.Nm 396sees all packets before they are dropped by the firewall. 397.Pp 398After translation by 399.Nm natd , 400packets re-enter the firewall at the rule number following the rule number 401that caused the diversion (not the next rule if there are several at the 402same number). 403 404.It 405Enable your firewall by setting 406 407 firewall_enable=YES 408 409in 410.Pa /etc/rc.conf . 411This tells the system startup scripts to run the 412.Pa /etc/rc.firewall 413script. If you don't wish to reboot now, just run this by hand from the 414console. NEVER run this from a virtual session unless you put it into 415the background. If you do, you'll lock yourself out after the flush 416takes place, and execution of 417.Pa /etc/rc.firewall 418will stop at this point - blocking all accesses permanently. Running 419the script in the background should be enough to prevent this disaster. 420 421.El 422 423.Sh SEE ALSO 424.Xr socket 2 , 425.Xr getservbyname 3 , 426.Xr divert 4 , 427.Xr services 5 , 428.Xr ipfw 8 429 430.Sh AUTHORS 431This program is the result of the efforts of many people at different 432times: 433 434.An Archie Cobbs Aq archie@whistle.com 435(divert sockets) 436.An Charles Mott Aq cmott@srv.net 437(packet aliasing) 438.An Eivind Eklund Aq perhaps@yes.no 439(IRC support & misc additions) 440.An Ari Suutari Aq suutari@iki.fi 441(natd) 442.An Dru Nelson Aq dnelson@redwoodsoft.com 443(PPTP support) 444.An Brian Somers Aq brian@awfulhak.org 445(glue) 446