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 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
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
251 .Oo Ar aliasIP Ns : Oc Ns Xo
255 .Op : Ns Ar remotePORT
258 .It Fl redirect_address Xo
259 .Ar localIP Ns Oo , Ns
260 .Ar localIP Ns Oo , Ns
269 are used to transparently offload network load on a single server and
270 distribute the load across a pool of servers.
271 This function is known as
274 For example, the argument
276 .Dl Ar tcp www1:http,www2:http,www3:http www:http
278 means that incoming HTTP requests for host www will be transparently
279 redirected to one of the www1, www2 or www3, where a host is selected
280 simply on a round-robin basis, without regard to load on the net.
288 will monitor the routing socket for alterations to the
291 If the interface's IP address is changed,
293 will dynamically alter its concept of the alias address.
294 .It Fl in_port | i Ar port
295 Read from and write to
299 treating all packets as
301 .It Fl out_port | o Ar port
302 Read from and write to
306 treating all packets as
308 .It Fl port | p Ar port
309 Read from and write to
313 distinguishing packets as
317 using the rules specified in
321 is not numeric, it is searched for in the
324 If this option is not specified, the divert port named
326 will be used as a default.
327 .It Fl alias_address | a Ar address
330 as the aliasing address.
333 option must be used (but not both),
336 option is not specified.
337 The specified address is usually the address assigned to the
343 will be rewritten with a source address equal to
347 will be checked to see if it matches any already-aliased outgoing
349 If it does, the packet is altered accordingly.
355 assignments are checked and actioned.
356 If no other action can be made and if
358 is not specified, the packet is delivered to the local machine
359 using the rules specified in
362 .It Fl t | target_address Ar address
363 Set the target address.
364 When an incoming packet not associated with any pre-existing link
365 arrives at the host machine, it will be sent to the specified
368 The target address may be set to
369 .Ar 255.255.255.255 ,
370 in which case all new incoming packets go to the alias address set by
375 If this option is not used, or called with the argument
377 then all new incoming packets go to the address specified in
379 This allows external machines to talk directly to internal machines if
380 they can route packets to the machine in question.
381 .It Fl interface | n Ar interface
384 to determine the aliasing address.
385 If there is a possibility that the IP address associated with
389 option should also be used.
390 If this option is not specified, the
401 .It Fl config | f Ar file
402 Read configuration from
406 should contain a list of options, one per line, in the same form
407 as the long form of the above command line options.
408 For example, the line
410 .Dl alias_address 158.152.17.1
412 would specify an alias address of 158.152.17.1.
413 Options that do not take an argument are specified with an argument of
417 in the configuration file.
418 For example, the line
425 Options can be divided to several sections.
426 Each section applies to own
429 This ability allows the configuration of one
431 process for several NAT instances.
432 The first instance that always exists is a "default" instance.
433 Each another instance should begin with
435 .Dl instance Ar instance_name
437 At the next should be placed a configuration option.
440 .Dl \&# default instance
442 .Dl alias_address 158.152.17.1
444 .Dl \&# second instance
447 .Dl alias_address 192.168.0.1
449 Trailing spaces and empty lines are ignored.
452 sign will mark the rest of the line as a comment.
453 .It Fl instance Ar instancename
454 This option switches command line options processing to configure instance
456 (creating it if necessary) till the next
458 option or end of command line.
459 It is easier to set up multiple instances in the configuration file
462 option rather than on a command line.
463 .It Fl globalport Ar port
464 Read from and write to
468 treating all packets as
470 This option is intended to be used with multiple instances:
471 packets received on this port are checked against
472 internal translation tables of every configured instance.
473 If an entry is found, packet is aliased according to that entry.
474 If no entry was found in any of the instances, packet is passed
475 unchanged, and no new entry will be created.
477 .Sx MULTIPLE INSTANCES
482 reverse the way it handles
486 packets, allowing it to operate on the
488 network interface rather than the
492 This can be useful in some transparent proxying situations
493 when outgoing traffic is redirected to the local machine
496 is running on the internal interface (it usually runs on the
501 to perform transparent proxying only.
502 Normal address translation is not performed.
504 .Op Ar type encode_ip_hdr | encode_tcp_stream
506 .Ar server a.b.c.d:yyyy
508 Enable transparent proxying.
509 Outgoing TCP packets with the given port going through this
510 host to any other host are redirected to the given server and port.
511 Optionally, the original target address can be encoded into the packet.
514 to put this information into the IP option field or
515 .Ar encode_tcp_stream
516 to inject the data into the beginning of the TCP stream.
518 .Ar basenumber Ns : Ns Ar count
526 based firewall for FTP/IRC DCC connections.
527 This is done dynamically by installing temporary firewall rules which
528 allow a particular connection (and only that connection) to go through
530 The rules are removed once the corresponding connection terminates.
534 rules starting from the rule number
536 will be used for punching firewall holes.
537 The range will be cleared for all rules on startup.
538 This option has no effect when the kernel is in security
541 for more information.
542 .It Fl skinny_port Ar port
543 This option allows you to specify the TCP port used for
544 the Skinny Station protocol.
545 Skinny is used by Cisco IP phones to communicate with
546 Cisco Call Managers to set up voice over IP calls.
547 By default, Skinny aliasing is not performed.
548 The typical port value for Skinny is 2000.
549 .It Fl log_ipfw_denied
550 Log when a packet cannot be re-injected because an
553 This is the default with
555 .It Fl pid_file | P Ar file
556 Specify an alternate file in which to store the process ID.
558 .Pa /var/run/natd.pid .
559 .It Fl exit_delay Ar ms
560 Specify delay in ms before daemon exit after signal.
565 The following steps are necessary before attempting to run
569 Build a custom kernel with the following options:
570 .Bd -literal -offset indent
575 Refer to the handbook for detailed instructions on building a custom
578 Ensure that your machine is acting as a gateway.
579 This can be done by specifying the line
581 .Dl gateway_enable=YES
585 file or using the command
587 .Dl "sysctl net.inet.ip.forwarding=1"
592 option, make sure that your interface is already configured.
593 If, for example, you wish to specify
599 on that interface, you must make sure that you start
607 is fairly straight forward.
610 .Dl natd -interface ed0
612 should suffice in most cases (substituting the correct interface name).
615 on how to configure it to be started automatically during boot.
618 is running, you must ensure that traffic is diverted to
622 You will need to adjust the
625 If you are not interested in having a firewall, the
626 following lines will do:
627 .Bd -literal -offset indent
629 /sbin/ipfw add divert natd all from any to any via ed0
630 /sbin/ipfw add pass all from any to any
633 The second line depends on your interface (change
637 You should be aware of the fact that, with these firewall settings,
638 everyone on your local network can fake his source-address using your
640 If there are other hosts on your local network, you are strongly
641 encouraged to create firewall rules that only allow traffic to and
644 If you specify real firewall rules, it is best to specify line 2 at
645 the start of the script so that
647 sees all packets before they are dropped by the firewall.
651 packets re-enter the firewall at the rule number following the rule number
652 that caused the diversion (not the next rule if there are several at the
655 Enable your firewall by setting
657 .Dl firewall_enable=YES
661 This tells the system startup scripts to run the
664 If you do not wish to reboot now, just run this by hand from the console.
665 NEVER run this from a remote session unless you put it into the background.
666 If you do, you will lock yourself out after the flush takes place, and
669 will stop at this point - blocking all accesses permanently.
670 Running the script in the background should be enough to prevent this
673 .Sh MULTIPLE INSTANCES
674 It is not so uncommon to have a need of aliasing to several external IP
676 While this traditionally was achieved by running several
678 processes with independent configurations,
680 can have multiple aliasing instances in a single process,
681 also allowing them to be not so independent of each other.
682 For example, let us see a common task of load balancing two
683 channels to different providers on a machine with two external
686 (with IP 1.2.3.4) and
689 .Bd -literal -offset indent
691 1.2.3.1 ------------------ sis0
694 sis1 ------------------- 10.0.0.2
697 2.3.4.1 ------------------ sis2
701 Default route is out via
704 Interior machine (10.0.0.2) is accessible on TCP port 122 through
705 both exterior IPs, and outgoing connections choose a path randomly
711 The way this works is that
713 builds two instances of the aliasing engine.
715 In addition to these instances' private
717 sockets, a third socket called the
719 is created; packets sent to
721 via this one will be matched against all instances and translated
722 if an existing entry is found, and unchanged if no entry is found.
723 The following lines are placed into
725 .Bd -literal -offset indent
733 redirect_port tcp 10.0.0.2:122 122
738 redirect_port tcp 10.0.0.2:122 122
746 .Bd -literal -offset indent
749 ipfw add allow ip from any to any via sis1
751 ipfw add skipto 1000 ip from any to any in via sis0
752 ipfw add skipto 2000 ip from any to any out via sis0
753 ipfw add skipto 3000 ip from any to any in via sis2
754 ipfw add skipto 4000 ip from any to any out via sis2
756 ipfw add 1000 count ip from any to any
758 ipfw add divert 1000 ip from any to any
759 ipfw add allow ip from any to any
761 ipfw add 2000 count ip from any to any
763 ipfw add divert 3000 ip from any to any
765 ipfw add allow ip from 1.2.3.4 to any
766 ipfw add skipto 5000 ip from 2.3.4.5 to any
768 ipfw add prob .5 skipto 4000 ip from any to any
770 ipfw add divert 1000 ip from any to any
771 ipfw add allow ip from any to any
773 ipfw add 3000 count ip from any to any
775 ipfw add divert 2000 ip from any to any
776 ipfw add allow ip from any to any
778 ipfw add 4000 count ip from any to any
780 ipfw add divert 2000 ip from any to any
782 ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24
783 ipfw add allow ip from any to any
786 Here the packet from internal network to Internet goes out via
788 (rule number 2000) and gets caught by the
791 After that, either a match is found in a translation table
792 of one of the two instances, or the packet is passed to one
795 ports (1000 or 2000), with equal probability.
796 This ensures that load balancing is done on a per-flow basis
797 (i.e., packets from a single TCP connection always flow through the
799 Translated packets with source IP of a non-default interface
801 are forwarded to the appropriate router on that interface.
818 This program is the result of the efforts of many people at different
821 .An Archie Cobbs Aq Mt archie@FreeBSD.org
823 .An Charles Mott Aq Mt cm@linktel.net
825 .An Eivind Eklund Aq Mt perhaps@yes.no
826 (IRC support & misc additions)
827 .An Ari Suutari Aq Mt suutari@iki.fi
829 .An Dru Nelson Aq Mt dnelson@redwoodsoft.com
831 .An Brian Somers Aq Mt brian@awfulhak.org
833 .An Ruslan Ermilov Aq Mt ru@FreeBSD.org
834 (natd, packet aliasing, glue)
835 .An Poul-Henning Kamp Aq Mt phk@FreeBSD.org