7 .Nd Network Address Translation daemon
11 .Op Fl unregistered_only | u
15 .Op Fl deny_incoming | d
16 .Op Fl use_sockets | s
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
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
44 utility provides a Network Address Translation facility for use
50 (If you need NAT on a PPP link,
54 option that gives most of the
56 functionality, and uses the same
62 utility normally runs in the background as a daemon.
63 It is passed raw IP packets as they travel into and out of the machine,
64 and will possibly change these before re-injecting them back into the
67 It changes all packets destined for another host so that their source
68 IP address is that of the current machine.
69 For each packet changed in this manner, an internal table entry is
70 created to record this fact.
71 The source port number is also changed to indicate the table entry
72 applying to the packet.
73 Packets that are received with a target IP of the current host are
74 checked against this internal table.
75 If an entry is found, it is used to determine the correct target IP
76 address and port to place in the packet.
78 The following command line options are available:
81 Log various aliasing statistics and information to the file
82 .Pa /var/log/alias.log .
83 This file is truncated each time
86 .It Fl deny_incoming | d
87 Do not pass incoming packets that have no
88 entry in the internal translation table.
90 If this option is not used, then such a packet will be altered
93 below, and the entry will be made in the internal translation table.
95 Log denied incoming packets via
99 .It Fl log_facility Ar facility_name
100 Use specified log facility when logging information via
104 is one of the keywords specified in
106 .It Fl use_sockets | s
109 in order to establish an FTP data or IRC DCC send connection.
110 This option uses more system resources, but guarantees successful
111 connections when port numbers conflict.
112 .It Fl same_ports | m
113 Try to keep the same port number when altering outgoing packets.
114 With this option, protocols such as RPC will have a better chance
116 If it is not possible to maintain the port number, it will be silently
117 changed as per normal.
122 Instead, stay attached to the controlling terminal and display all packet
123 alterations to the standard output.
124 This option should only be used for debugging purposes.
125 .It Fl unregistered_only | u
126 Only alter outgoing packets with an
129 According to RFC 1918, unregistered source addresses are 10.0.0.0/8,
130 172.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 Op - Ns Ar targetPORT Xc
134 .Op Ar aliasIP Ns : Ns Xo
135 .Ar aliasPORT Ns Op - Ns Ar aliasPORT Xc
136 .Oo Ar remoteIP Ns Oo : Ns
137 .Ar remotePORT Ns Op - Ns Ar remotePORT
140 Redirect incoming connections arriving to given port(s) to another host
149 is the desired target IP address,
151 is the desired target port number or range,
153 is the requested port number or range, and
155 is the aliasing address.
160 can be used to specify the connection more accurately if necessary.
163 is not specified, it is assumed to be all ports.
166 .Ar targetIP , aliasIP
169 can be given as IP addresses or as hostnames.
171 .Ar targetPORT , aliasPORT
174 ranges need not be the same numerically, but must have the same size.
176 .Ar targetPORT , aliasPORT
179 specifies a singular value (not a range), it can be given as a service
180 name that is searched for in the
184 For example, the argument
186 .Dl Ar tcp inside1:telnet 6666
188 means that incoming TCP packets destined for port 6666 on this machine
189 will be sent to the telnet port on the inside1 machine.
191 .Dl Ar tcp inside2:2300-2399 3300-3399
193 will redirect incoming connections on ports 3300-3399 to host
194 inside2, ports 2300-2399.
195 The 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
199 Redirect incoming IP packets of protocol
207 address and vice versa.
211 is not specified, then the default aliasing address is used.
214 is specified, then only packets coming from/to
217 .It Fl redirect_address Ar localIP publicIP
218 Redirect traffic for public IP address to a machine on the local
220 This function is known as
222 Normally static NAT is useful if your ISP has allocated a small block
223 of IP addresses to you, but it can even be used in the case of single
226 .Dl Ar redirect_address 10.0.0.8 0.0.0.0
228 The above command would redirect all incoming traffic
231 If several address aliases specify the same public address
233 .Bd -literal -offset indent
234 redirect_address 192.168.0.2 public_addr
235 redirect_address 192.168.0.3 public_addr
236 redirect_address 192.168.0.4 public_addr
239 the incoming traffic will be directed to the last
240 translated local address (192.168.0.4), but outgoing
241 traffic from the first two addresses will still be aliased
242 to appear from the specified
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
253 .Op Ar aliasIP Ns : Ns Xo
257 .Op : Ns Ar remotePORT
260 .It Fl redirect_address Xo
261 .Ar localIP Ns Oo , Ns
262 .Ar localIP Ns Oo , Ns
271 are used to transparently offload network load on a single server and
272 distribute the load across a pool of servers.
273 This function is known as
276 For example, the argument
278 .Dl Ar tcp www1:http,www2:http,www3:http www:http
280 means that incoming HTTP requests for host www will be transparently
281 redirected to one of the www1, www2 or www3, where a host is selected
282 simply on a round-robin basis, without regard to load on the net.
290 will monitor the routing socket for alterations to the
293 If the interface's IP address is changed,
295 will dynamically alter its concept of the alias address.
296 .It Fl in_port | i Ar port
297 Read from and write to
301 treating all packets as
303 .It Fl out_port | o Ar port
304 Read from and write to
308 treating all packets as
310 .It Fl port | p Ar port
311 Read from and write to
315 distinguishing packets as
319 using the rules specified in
323 is not numeric, it is searched for in the
326 If this option is not specified, the divert port named
328 will be used as a default.
329 .It Fl alias_address | a Ar address
332 as the aliasing address.
335 option must be used (but not both),
338 option is not specified.
339 The specified address is usually the address assigned to the
345 will be rewritten with a source address equal to
349 will be checked to see if it matches any already-aliased outgoing
351 If it does, the packet is altered accordingly.
357 assignments are checked and actioned.
358 If no other action can be made and if
360 is not specified, the packet is delivered to the local machine
361 using the rules specified in
364 .It Fl t | target_address Ar address
365 Set the target address.
366 When an incoming packet not associated with any pre-existing link
367 arrives at the host machine, it will be sent to the specified
370 The target address may be set to
371 .Ar 255.255.255.255 ,
372 in which case all new incoming packets go to the alias address set by
377 If this option is not used, or called with the argument
379 then all new incoming packets go to the address specified in
381 This allows external machines to talk directly to internal machines if
382 they can route packets to the machine in question.
383 .It Fl interface | n Ar interface
386 to determine the aliasing address.
387 If there is a possibility that the IP address associated with
391 option should also be used.
392 If this option is not specified, the
403 .It Fl config | f Ar file
404 Read configuration from
408 should contain a list of options, one per line, in the same form
409 as the long form of the above command line options.
410 For example, the line
412 .Dl alias_address 158.152.17.1
414 would specify an alias address of 158.152.17.1.
415 Options that do not take an argument are specified with an argument of
419 in the configuration file.
420 For example, the line
427 Options can be divided to several sections.
428 Each section applies to own
431 This ability allows to configure one
433 process for several NAT instances.
434 The first instance that always exists is a "default" instance.
435 Each another instance should begin with
437 .Dl instance Ar instance_name
439 At the next should be placed a configuration option.
442 .Dl \&# default instance
444 .Dl alias_address 158.152.17.1
446 .Dl \&# second instance
449 .Dl alias_address 192.168.0.1
451 Trailing spaces and empty lines are ignored.
454 sign will mark the rest of the line as a comment.
455 .It Fl instance Ar instancename
456 This option switches command line options processing to configure instance
458 (creating it if necessary) till the next
460 option or end of command line.
461 It is easier to set up multiple instances in the configuration file
464 option rather than on a command line.
465 .It Fl globalport Ar port
466 Read from and write to
470 treating all packets as
472 This option is intended to be used with multiple instances:
473 packets received on this port are checked against
474 internal translation tables of every configured instance.
475 If an entry is found, packet is aliased according to that entry.
476 If no entry was found in any of the instances, packet is passed
477 unchanged, and no new entry will be created.
479 .Sx MULTIPLE INSTANCES
484 reverse the way it handles
488 packets, allowing it to operate on the
490 network interface rather than the
494 This can be useful in some transparent proxying situations
495 when outgoing traffic is redirected to the local machine
498 is running on the internal interface (it usually runs on the
503 to perform transparent proxying only.
504 Normal address translation is not performed.
506 .Op Ar type encode_ip_hdr | encode_tcp_stream
508 .Ar server a.b.c.d:yyyy
510 Enable transparent proxying.
511 Outgoing TCP packets with the given port going through this
512 host to any other host are redirected to the given server and port.
513 Optionally, the original target address can be encoded into the packet.
516 to put this information into the IP option field or
517 .Ar encode_tcp_stream
518 to inject the data into the beginning of the TCP stream.
520 .Ar basenumber Ns : Ns Ar count
528 based firewall for FTP/IRC DCC connections.
529 This is done dynamically by installing temporary firewall rules which
530 allow a particular connection (and only that connection) to go through
532 The rules are removed once the corresponding connection terminates.
536 rules starting from the rule number
538 will be used for punching firewall holes.
539 The range will be cleared for all rules on startup.
540 This option has no effect when the kernel is in security
543 for more information.
544 .It Fl skinny_port Ar port
545 This option allows you to specify the TCP port used for
546 the Skinny Station protocol.
547 Skinny is used by Cisco IP phones to communicate with
548 Cisco Call Managers to set up voice over IP calls.
549 By default, Skinny aliasing is not performed.
550 The typical port value for Skinny is 2000.
551 .It Fl log_ipfw_denied
552 Log when a packet cannot be re-injected because an
555 This is the default with
557 .It Fl pid_file | P Ar file
558 Specify an alternate file in which to store the process ID.
560 .Pa /var/run/natd.pid .
561 .It Fl exit_delay Ar ms
562 Specify delay in ms before daemon exit after signal.
567 The following steps are necessary before attempting to run
571 Build a custom kernel with the following options:
572 .Bd -literal -offset indent
577 Refer to the handbook for detailed instructions on building a custom
580 Ensure that your machine is acting as a gateway.
581 This can be done by specifying the line
583 .Dl gateway_enable=YES
587 file or using the command
589 .Dl "sysctl net.inet.ip.forwarding=1"
594 option, make sure that your interface is already configured.
595 If, for example, you wish to specify
601 on that interface, you must make sure that you start
609 is fairly straight forward.
612 .Dl natd -interface ed0
614 should suffice in most cases (substituting the correct interface name).
617 on how to configure it to be started automatically during boot.
620 is running, you must ensure that traffic is diverted to
624 You will need to adjust the
627 If you are not interested in having a firewall, the
628 following lines will do:
629 .Bd -literal -offset indent
631 /sbin/ipfw add divert natd all from any to any via ed0
632 /sbin/ipfw add pass all from any to any
635 The second line depends on your interface (change
639 You should be aware of the fact that, with these firewall settings,
640 everyone on your local network can fake his source-address using your
642 If there are other hosts on your local network, you are strongly
643 encouraged to create firewall rules that only allow traffic to and
646 If you specify real firewall rules, it is best to specify line 2 at
647 the start of the script so that
649 sees all packets before they are dropped by the firewall.
653 packets re-enter the firewall at the rule number following the rule number
654 that caused the diversion (not the next rule if there are several at the
657 Enable your firewall by setting
659 .Dl firewall_enable=YES
663 This tells the system startup scripts to run the
666 If you do not wish to reboot now, just run this by hand from the console.
667 NEVER run this from a remote session unless you put it into the background.
668 If you do, you will lock yourself out after the flush takes place, and
671 will stop at this point - blocking all accesses permanently.
672 Running the script in the background should be enough to prevent this
675 .Sh MULTIPLE INSTANCES
676 It is not so uncommon to have a need of aliasing to several external IP
678 While this traditionally was achieved by running several
680 processes with independent configurations,
682 can have multiple aliasing instances in a single process,
683 also allowing them to be not so independent of each other.
684 For example, let us see a common task of load balancing two
685 channels to different providers on a machine with two external
688 (with IP 1.2.3.4) and
691 .Bd -literal -offset indent
693 1.2.3.1 ------------------ sis0
696 sis1 ------------------- 10.0.0.2
699 2.3.4.1 ------------------ sis2
703 Default route is out via
706 Interior machine (10.0.0.2) is accessible on TCP port 122 through
707 both exterior IPs, and outgoing connections choose a path randomly
713 The way this works is that
715 builds two instances of the aliasing engine.
717 In addition to these instances' private
719 sockets, a third socket called the
721 is created; packets sent to
723 via this one will be matched against all instances and translated
724 if an existing entry is found, and unchanged if no entry is found.
725 The following lines are placed into
727 .Bd -literal -offset indent
735 redirect_port tcp 10.0.0.2:122 122
740 redirect_port tcp 10.0.0.2:122 122
748 .Bd -literal -offset indent
751 ipfw add allow ip from any to any via sis1
753 ipfw add skipto 1000 ip from any to any in via sis0
754 ipfw add skipto 2000 ip from any to any out via sis0
755 ipfw add skipto 3000 ip from any to any in via sis2
756 ipfw add skipto 4000 ip from any to any out via sis2
758 ipfw add 1000 count ip from any to any
760 ipfw add divert 1000 ip from any to any
761 ipfw add allow ip from any to any
763 ipfw add 2000 count ip from any to any
765 ipfw add divert 3000 ip from any to any
767 ipfw add allow ip from 1.2.3.4 to any
768 ipfw add skipto 5000 ip from 2.3.4.5 to any
770 ipfw add prob .5 skipto 4000 ip from any to any
772 ipfw add divert 1000 ip from any to any
773 ipfw add allow ip from any to any
775 ipfw add 3000 count ip from any to any
777 ipfw add divert 2000 ip from any to any
778 ipfw add allow ip from any to any
780 ipfw add 4000 count ip from any to any
782 ipfw add divert 2000 ip from any to any
784 ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24
785 ipfw add allow ip from any to any
788 Here the packet from internal network to Internet goes out via
790 (rule number 2000) and gets caught by the
793 After that, either a match is found in a translation table
794 of one of the two instances, or the packet is passed to one
797 ports (1000 or 2000), with equal probability.
798 This ensures that load balancing is done on a per-flow basis
799 (i.e., packets from a single TCP connection always flow through the
801 Translated packets with source IP of a non-default interface
803 are forwarded to the appropriate router on that interface.
815 This program is the result of the efforts of many people at different
818 .An Archie Cobbs Aq archie@FreeBSD.org
820 .An Charles Mott Aq cm@linktel.net
822 .An Eivind Eklund Aq perhaps@yes.no
823 (IRC support & misc additions)
824 .An Ari Suutari Aq suutari@iki.fi
826 .An Dru Nelson Aq dnelson@redwoodsoft.com
828 .An Brian Somers Aq brian@awfulhak.org
830 .An Ruslan Ermilov Aq ru@FreeBSD.org
831 (natd, packet aliasing, glue)
832 .An Poul-Henning Kamp Aq phk@FreeBSD.org