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
43 utility provides a Network Address Translation facility for use
49 (If you need NAT on a PPP link,
53 option that gives most of the
55 functionality, and uses the same
61 utility normally runs in the background as a daemon.
62 It is passed raw IP packets as they travel into and out of the machine,
63 and will possibly change these before re-injecting them back into the
66 It changes all packets destined for another host so that their source
67 IP address is that of the current machine.
68 For each packet changed in this manner, an internal table entry is
69 created to record this fact.
70 The source port number is also changed to indicate the table entry
71 applying to the packet.
72 Packets that are received with a target IP of the current host are
73 checked against this internal table.
74 If an entry is found, it is used to determine the correct target IP
75 address and port to place in the packet.
77 The following command line options are available:
80 Log various aliasing statistics and information to the file
81 .Pa /var/log/alias.log .
82 This file is truncated each time
85 .It Fl deny_incoming | d
86 Do not pass incoming packets that have no
87 entry in the internal translation table.
89 If this option is not used, then such a packet will be altered
92 below, and the entry will be made in the internal translation table.
94 Log denied incoming packets via
98 .It Fl log_facility Ar facility_name
99 Use specified log facility when logging information via
103 is one of the keywords specified in
105 .It Fl use_sockets | s
108 in order to establish an FTP data or IRC DCC send connection.
109 This option uses more system resources, but guarantees successful
110 connections when port numbers conflict.
111 .It Fl same_ports | m
112 Try to keep the same port number when altering outgoing packets.
113 With this option, protocols such as RPC will have a better chance
115 If it is not possible to maintain the port number, it will be silently
116 changed as per normal.
121 Instead, stay attached to the controlling terminal and display all packet
122 alterations to the standard output.
123 This option should only be used for debugging purposes.
124 .It Fl unregistered_only | u
125 Only alter outgoing packets with an
128 According to RFC 1918, unregistered source addresses are 10.0.0.0/8,
129 172.16.0.0/12 and 192.168.0.0/16.
130 .It Fl redirect_port Ar proto Xo
131 .Ar targetIP Ns : Ns Xo
132 .Ar targetPORT Ns Op - Ns Ar targetPORT Xc
133 .Op Ar aliasIP Ns : Ns Xo
134 .Ar aliasPORT Ns Op - Ns Ar aliasPORT Xc
135 .Oo Ar remoteIP Ns Oo : Ns
136 .Ar remotePORT Ns Op - Ns Ar remotePORT
139 Redirect incoming connections arriving to given port(s) to another host
148 is the desired target IP address,
150 is the desired target port number or range,
152 is the requested port number or range, and
154 is the aliasing address.
159 can be used to specify the connection more accurately if necessary.
162 is not specified, it is assumed to be all ports.
165 .Ar targetIP , aliasIP
168 can be given as IP addresses or as hostnames.
170 .Ar targetPORT , aliasPORT
173 ranges need not be the same numerically, but must have the same size.
175 .Ar targetPORT , aliasPORT
178 specifies a singular value (not a range), it can be given as a service
179 name that is searched for in the
183 For example, the argument
185 .Dl Ar tcp inside1:telnet 6666
187 means that incoming TCP packets destined for port 6666 on this machine
188 will be sent to the telnet port on the inside1 machine.
190 .Dl Ar tcp inside2:2300-2399 3300-3399
192 will redirect incoming connections on ports 3300-3399 to host
193 inside2, ports 2300-2399.
194 The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc.
195 .It Fl redirect_proto Ar proto localIP Oo
196 .Ar publicIP Op Ar remoteIP
198 Redirect incoming IP packets of protocol
206 address and vice versa.
210 is not specified, then the default aliasing address is used.
213 is specified, then only packets coming from/to
216 .It Fl redirect_address Ar localIP publicIP
217 Redirect traffic for public IP address to a machine on the local
219 This function is known as
221 Normally static NAT is useful if your ISP has allocated a small block
222 of IP addresses to you, but it can even be used in the case of single
225 .Dl Ar redirect_address 10.0.0.8 0.0.0.0
227 The above command would redirect all incoming traffic
230 If several address aliases specify the same public address
232 .Bd -literal -offset indent
233 redirect_address 192.168.0.2 public_addr
234 redirect_address 192.168.0.3 public_addr
235 redirect_address 192.168.0.4 public_addr
238 the incoming traffic will be directed to the last
239 translated local address (192.168.0.4), but outgoing
240 traffic from the first two addresses will still be aliased
241 to appear from the specified
243 .It Fl redirect_port Ar proto Xo
244 .Ar targetIP Ns : Ns Xo
245 .Ar targetPORT Ns Oo , Ns
246 .Ar targetIP Ns : Ns Xo
247 .Ar targetPORT Ns Oo , Ns
252 .Op Ar aliasIP Ns : Ns Xo
256 .Op : Ns Ar remotePORT
259 .It Fl redirect_address Xo
260 .Ar localIP Ns Oo , Ns
261 .Ar localIP Ns Oo , Ns
270 are used to transparently offload network load on a single server and
271 distribute the load across a pool of servers.
272 This function is known as
275 For example, the argument
277 .Dl Ar tcp www1:http,www2:http,www3:http www:http
279 means that incoming HTTP requests for host www will be transparently
280 redirected to one of the www1, www2 or www3, where a host is selected
281 simply on a round-robin basis, without regard to load on the net.
289 will monitor the routing socket for alterations to the
292 If the interface's IP address is changed,
294 will dynamically alter its concept of the alias address.
295 .It Fl in_port | i Ar port
296 Read from and write to
300 treating all packets as
302 .It Fl out_port | o Ar port
303 Read from and write to
307 treating all packets as
309 .It Fl port | p Ar port
310 Read from and write to
314 distinguishing packets as
318 using the rules specified in
322 is not numeric, it is searched for in the
325 If this option is not specified, the divert port named
327 will be used as a default.
328 .It Fl alias_address | a Ar address
331 as the aliasing address.
334 option must be used (but not both),
337 option is not specified.
338 The specified address is usually the address assigned to the
344 will be rewritten with a source address equal to
348 will be checked to see if it matches any already-aliased outgoing
350 If it does, the packet is altered accordingly.
356 assignments are checked and actioned.
357 If no other action can be made and if
359 is not specified, the packet is delivered to the local machine
360 using the rules specified in
363 .It Fl t | target_address Ar address
364 Set the target address.
365 When an incoming packet not associated with any pre-existing link
366 arrives at the host machine, it will be sent to the specified
369 The target address may be set to
370 .Ar 255.255.255.255 ,
371 in which case all new incoming packets go to the alias address set by
376 If this option is not used, or called with the argument
378 then all new incoming packets go to the address specified in
380 This allows external machines to talk directly to internal machines if
381 they can route packets to the machine in question.
382 .It Fl interface | n Ar interface
385 to determine the aliasing address.
386 If there is a possibility that the IP address associated with
390 option should also be used.
391 If this option is not specified, the
402 .It Fl config | f Ar file
403 Read configuration from
407 should contain a list of options, one per line, in the same form
408 as the long form of the above command line options.
409 For example, the line
411 .Dl alias_address 158.152.17.1
413 would specify an alias address of 158.152.17.1.
414 Options that do not take an argument are specified with an argument of
418 in the configuration file.
419 For example, the line
426 Options can be divided to several sections.
427 Each section applies to own
430 This ability allows to configure one
432 process for several NAT instances.
433 The first instance that always exists is a "default" instance.
434 Each another instance should begin with
436 .Dl instance Ar instance_name
438 At the next should be placed a configuration option.
441 .Dl \&# default instance
443 .Dl alias_address 158.152.17.1
445 .Dl \&# second instance
448 .Dl alias_address 192.168.0.1
450 Trailing spaces and empty lines are ignored.
453 sign will mark the rest of the line as a comment.
454 .It Fl instance Ar instancename
455 This option switches command line options processing to configure instance
457 (creating it if necessary) till the next
459 option or end of command line.
460 It is easier to set up multiple instances in the configuration file
463 option rather than on a command line.
464 .It Fl globalport Ar port
465 Read from and write to
469 treating all packets as
471 This option is intended to be used with multiple instances:
472 packets received on this port are checked against
473 internal translation tables of every configured instance.
474 If an entry is found, packet is aliased according to that entry.
475 In no entry was found in any of the instances, packet is passed
476 unchanged, and no new entry will be created.
478 .Sx MULTIPLE INSTANCES
483 reverse the way it handles
487 packets, allowing it to operate on the
489 network interface rather than the
493 This can be useful in some transparent proxying situations
494 when outgoing traffic is redirected to the local machine
497 is running on the internal interface (it usually runs on the
502 to perform transparent proxying only.
503 Normal address translation is not performed.
505 .Op Ar type encode_ip_hdr | encode_tcp_stream
507 .Ar server a.b.c.d:yyyy
509 Enable transparent proxying.
510 Outgoing TCP packets with the given port going through this
511 host to any other host are redirected to the given server and port.
512 Optionally, the original target address can be encoded into the packet.
515 to put this information into the IP option field or
516 .Ar encode_tcp_stream
517 to inject the data into the beginning of the TCP stream.
519 .Ar basenumber Ns : Ns Ar count
527 based firewall for FTP/IRC DCC connections.
528 This is done dynamically by installing temporary firewall rules which
529 allow a particular connection (and only that connection) to go through
531 The rules are removed once the corresponding connection terminates.
535 rules starting from the rule number
537 will be used for punching firewall holes.
538 The range will be cleared for all rules on startup.
539 This option has no effect when the kernel is in security
542 for more information.
543 .It Fl skinny_port Ar port
544 This option allows you to specify the TCP port used for
545 the Skinny Station protocol.
546 Skinny is used by Cisco IP phones to communicate with
547 Cisco Call Managers to set up voice over IP calls.
548 By default, Skinny aliasing is not performed.
549 The typical port value for Skinny is 2000.
550 .It Fl log_ipfw_denied
551 Log when a packet cannot be re-injected because an
554 This is the default with
556 .It Fl pid_file | P Ar file
557 Specify an alternate file in which to store the process ID.
559 .Pa /var/run/natd.pid .
562 The following steps are necessary before attempting to run
566 Build a custom kernel with the following options:
567 .Bd -literal -offset indent
572 Refer to the handbook for detailed instructions on building a custom
575 Ensure that your machine is acting as a gateway.
576 This can be done by specifying the line
578 .Dl gateway_enable=YES
582 file or using the command
584 .Dl "sysctl net.inet.ip.forwarding=1"
589 option, make sure that your interface is already configured.
590 If, for example, you wish to specify
596 on that interface, you must make sure that you start
604 is fairly straight forward.
607 .Dl natd -interface ed0
609 should suffice in most cases (substituting the correct interface name).
612 on how to configure it to be started automatically during boot.
615 is running, you must ensure that traffic is diverted to
619 You will need to adjust the
622 If you are not interested in having a firewall, the
623 following lines will do:
624 .Bd -literal -offset indent
626 /sbin/ipfw add divert natd all from any to any via ed0
627 /sbin/ipfw add pass all from any to any
630 The second line depends on your interface (change
634 You should be aware of the fact that, with these firewall settings,
635 everyone on your local network can fake his source-address using your
637 If there are other hosts on your local network, you are strongly
638 encouraged to create firewall rules that only allow traffic to and
641 If you specify real firewall rules, it is best to specify line 2 at
642 the start of the script so that
644 sees all packets before they are dropped by the firewall.
648 packets re-enter the firewall at the rule number following the rule number
649 that caused the diversion (not the next rule if there are several at the
652 Enable your firewall by setting
654 .Dl firewall_enable=YES
658 This tells the system startup scripts to run the
661 If you do not wish to reboot now, just run this by hand from the console.
662 NEVER run this from a remote session unless you put it into the background.
663 If you do, you will lock yourself out after the flush takes place, and
666 will stop at this point - blocking all accesses permanently.
667 Running the script in the background should be enough to prevent this
670 .Sh MULTIPLE INSTANCES
671 It is not so uncommon to have a need of aliasing to several external IP
673 While this traditionally was achieved by running several
675 processes with independent configurations,
677 can have multiple aliasing instances in a single process,
678 also allowing them to be not so independent of each other.
679 For example, let us see a common task of load balancing two
680 channels to different providers on a machine with two external
683 (with IP 1.2.3.4) and
686 .Bd -literal -offset indent
688 1.2.3.1 ------------------ sis0
691 sis1 ------------------- 10.0.0.2
694 2.3.4.1 ------------------ sis2
698 Default route is out via
701 Interior machine (10.0.0.2) is accessible on TCP port 122 through
702 both exterior IPs, and outgoing connections choose a path randomly
708 The way this works is that
710 builds two instances of the aliasing engine.
712 In addition to these instances' private
714 sockets, a third socket called the
716 is created; packets sent to
718 via this one will be matched against all instances and translated
719 if an existing entry is found, and unchanged if no entry is found.
720 The following lines are placed into
722 .Bd -literal -offset indent
730 redirect_port tcp 10.0.0.2:122 122
735 redirect_port tcp 10.0.0.2:122 122
743 .Bd -literal -offset indent
746 ipfw add allow ip from any to any via sis1
748 ipfw add skipto 1000 ip from any to any in via sis0
749 ipfw add skipto 2000 ip from any to any out via sis0
750 ipfw add skipto 3000 ip from any to any in via sis2
751 ipfw add skipto 4000 ip from any to any out via sis2
753 ipfw add 1000 count ip from any to any
755 ipfw add divert 1000 ip from any to any
756 ipfw add allow ip from any to any
758 ipfw add 2000 count ip from any to any
760 ipfw add divert 3000 ip from any to any
762 ipfw add allow ip from 1.2.3.4 to any
763 ipfw add skipto 5000 ip from 2.3.4.5 to any
765 ipfw add prob .5 skipto 4000 ip from any to any
767 ipfw add divert 1000 ip from any to any
768 ipfw add allow ip from any to any
770 ipfw add 3000 count ip from any to any
772 ipfw add divert 2000 ip from any to any
773 ipfw add allow ip from any to any
775 ipfw add 4000 count ip from any to any
777 ipfw add divert 2000 ip from any to any
779 ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24
780 ipfw add allow ip from any to any
783 Here the packet from internal network to Internet goes out via
785 (rule number 2000) and gets catched by the
788 After that, either a match is found in a translation table
789 of one of the two instances, or the packet is passed to one
792 ports (1000 or 2000), with equal probability.
793 This ensures that load balancing is done on a per-flow basis
794 (i.e., packets from a single TCP connection always flow through the
796 Translated packets with source IP of a non-default interface
798 are forwarded to the appropriate router on that interface.
810 This program is the result of the efforts of many people at different
813 .An Archie Cobbs Aq archie@FreeBSD.org
815 .An Charles Mott Aq cm@linktel.net
817 .An Eivind Eklund Aq perhaps@yes.no
818 (IRC support & misc additions)
819 .An Ari Suutari Aq suutari@iki.fi
821 .An Dru Nelson Aq dnelson@redwoodsoft.com
823 .An Brian Somers Aq brian@awfulhak.org
825 .An Ruslan Ermilov Aq ru@FreeBSD.org
826 (natd, packet aliasing, glue)
827 .An Poul-Henning Kamp Aq phk@FreeBSD.org