xref: /freebsd/sbin/natd/natd.8 (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
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 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
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 Oc
251.Xc
252.Xc
253.Op Ar aliasIP Ns : Ns Xo
254.Ar aliasPORT
255.Xc
256.Oo Ar remoteIP Ns
257.Op : Ns Ar remotePORT
258.Oc
259.Xc
260.It Fl redirect_address Xo
261.Ar localIP Ns Oo , Ns
262.Ar localIP Ns Oo , Ns
263.Ar ...\&
264.Oc Oc
265.Ar publicIP
266.Xc
267These forms of
268.Fl redirect_port
269and
270.Fl redirect_address
271are used to transparently offload network load on a single server and
272distribute the load across a pool of servers.
273This function is known as
274.Em LSNAT
275(RFC 2391).
276For example, the argument
277.Pp
278.Dl Ar tcp www1:http,www2:http,www3:http www:http
279.Pp
280means that incoming HTTP requests for host www will be transparently
281redirected to one of the www1, www2 or www3, where a host is selected
282simply on a round-robin basis, without regard to load on the net.
283.It Fl dynamic
284If the
285.Fl n
286or
287.Fl interface
288option is used,
289.Nm
290will monitor the routing socket for alterations to the
291.Ar interface
292passed.
293If the interface's IP address is changed,
294.Nm
295will dynamically alter its concept of the alias address.
296.It Fl in_port | i Ar port
297Read from and write to
298.Xr divert 4
299port
300.Ar port ,
301treating all packets as
302.Dq incoming .
303.It Fl out_port | o Ar port
304Read from and write to
305.Xr divert 4
306port
307.Ar port ,
308treating all packets as
309.Dq outgoing .
310.It Fl port | p Ar port
311Read from and write to
312.Xr divert 4
313port
314.Ar port ,
315distinguishing packets as
316.Dq incoming
317or
318.Dq outgoing
319using the rules specified in
320.Xr divert 4 .
321If
322.Ar port
323is not numeric, it is searched for in the
324.Xr services 5
325database.
326If this option is not specified, the divert port named
327.Ar natd
328will be used as a default.
329.It Fl alias_address | a Ar address
330Use
331.Ar address
332as the aliasing address.
333Either this or the
334.Fl interface
335option must be used (but not both),
336if the
337.Fl proxy_only
338option is not specified.
339The specified address is usually the address assigned to the
340.Dq public
341network interface.
342.Pp
343All data passing
344.Em out
345will be rewritten with a source address equal to
346.Ar address .
347All data coming
348.Em in
349will be checked to see if it matches any already-aliased outgoing
350connection.
351If it does, the packet is altered accordingly.
352If not, all
353.Fl redirect_port ,
354.Fl redirect_proto
355and
356.Fl redirect_address
357assignments are checked and actioned.
358If no other action can be made and if
359.Fl deny_incoming
360is not specified, the packet is delivered to the local machine
361using the rules specified in
362.Fl target_address
363option below.
364.It Fl t | target_address Ar address
365Set the target address.
366When an incoming packet not associated with any pre-existing link
367arrives at the host machine, it will be sent to the specified
368.Ar address .
369.Pp
370The target address may be set to
371.Ar 255.255.255.255 ,
372in which case all new incoming packets go to the alias address set by
373.Fl alias_address
374or
375.Fl interface .
376.Pp
377If this option is not used, or called with the argument
378.Ar 0.0.0.0 ,
379then all new incoming packets go to the address specified in
380the packet.
381This allows external machines to talk directly to internal machines if
382they can route packets to the machine in question.
383.It Fl interface | n Ar interface
384Use
385.Ar interface
386to determine the aliasing address.
387If there is a possibility that the IP address associated with
388.Ar interface
389may change, the
390.Fl dynamic
391option should also be used.
392If this option is not specified, the
393.Fl alias_address
394option must be used.
395.Pp
396The specified
397.Ar interface
398is usually the
399.Dq public
400(or
401.Dq external )
402network interface.
403.It Fl config | f Ar file
404Read configuration from
405.Ar file .
406A
407.Ar file
408should contain a list of options, one per line, in the same form
409as the long form of the above command line options.
410For example, the line
411.Pp
412.Dl alias_address 158.152.17.1
413.Pp
414would specify an alias address of 158.152.17.1.
415Options that do not take an argument are specified with an argument of
416.Ar yes
417or
418.Ar no
419in the configuration file.
420For example, the line
421.Pp
422.Dl log yes
423.Pp
424is synonymous with
425.Fl log .
426.Pp
427Options can be divided to several sections.
428Each section applies to own
429.Nm
430instance.
431This ability allows to configure one
432.Nm
433process for several NAT instances.
434The first instance that always exists is a "default" instance.
435Each another instance should begin with
436.Pp
437.Dl instance Ar instance_name
438.Pp
439At the next should be placed a configuration option.
440Example:
441.Pp
442.Dl \&# default instance
443.Dl port 8668
444.Dl alias_address 158.152.17.1
445.Pp
446.Dl \&# second instance
447.Dl instance dsl1
448.Dl port 8888
449.Dl alias_address 192.168.0.1
450.Pp
451Trailing spaces and empty lines are ignored.
452A
453.Ql \&#
454sign will mark the rest of the line as a comment.
455.It Fl instance Ar instancename
456This option switches command line options processing to configure instance
457.Ar instancename
458(creating it if necessary) till the next
459.Fl instance
460option or end of command line.
461It is easier to set up multiple instances in the configuration file
462specified with the
463.Fl config
464option rather than on a command line.
465.It Fl globalport Ar port
466Read from and write to
467.Xr divert 4
468port
469.Ar port ,
470treating all packets as
471.Dq outgoing .
472This option is intended to be used with multiple instances:
473packets received on this port are checked against
474internal translation tables of every configured instance.
475If an entry is found, packet is aliased according to that entry.
476In no entry was found in any of the instances, packet is passed
477unchanged, and no new entry will be created.
478See the section
479.Sx MULTIPLE INSTANCES
480for more details.
481.It Fl reverse
482This option makes
483.Nm
484reverse the way it handles
485.Dq incoming
486and
487.Dq outgoing
488packets, allowing it to operate on the
489.Dq internal
490network interface rather than the
491.Dq external
492one.
493.Pp
494This can be useful in some transparent proxying situations
495when outgoing traffic is redirected to the local machine
496and
497.Nm
498is running on the internal interface (it usually runs on the
499external interface).
500.It Fl proxy_only
501Force
502.Nm
503to perform transparent proxying only.
504Normal address translation is not performed.
505.It Fl proxy_rule Xo
506.Op Ar type encode_ip_hdr | encode_tcp_stream
507.Ar port xxxx
508.Ar server a.b.c.d:yyyy
509.Xc
510Enable transparent proxying.
511Outgoing TCP packets with the given port going through this
512host to any other host are redirected to the given server and port.
513Optionally, the original target address can be encoded into the packet.
514Use
515.Ar encode_ip_hdr
516to put this information into the IP option field or
517.Ar encode_tcp_stream
518to inject the data into the beginning of the TCP stream.
519.It Fl punch_fw Xo
520.Ar basenumber Ns : Ns Ar count
521.Xc
522This option directs
523.Nm
524to
525.Dq punch holes
526in an
527.Xr ipfirewall 4
528based firewall for FTP/IRC DCC connections.
529This is done dynamically by installing temporary firewall rules which
530allow a particular connection (and only that connection) to go through
531the firewall.
532The rules are removed once the corresponding connection terminates.
533.Pp
534A maximum of
535.Ar count
536rules starting from the rule number
537.Ar basenumber
538will be used for punching firewall holes.
539The range will be cleared for all rules on startup.
540This option has no effect when the kernel is in security
541level 3, see
542.Xr init 8
543for more information.
544.It Fl skinny_port Ar port
545This option allows you to specify the TCP port used for
546the Skinny Station protocol.
547Skinny is used by Cisco IP phones to communicate with
548Cisco Call Managers to set up voice over IP calls.
549By default, Skinny aliasing is not performed.
550The typical port value for Skinny is 2000.
551.It Fl log_ipfw_denied
552Log when a packet cannot be re-injected because an
553.Xr ipfw 8
554rule blocks it.
555This is the default with
556.Fl verbose .
557.It Fl pid_file | P Ar file
558Specify an alternate file in which to store the process ID.
559The default is
560.Pa /var/run/natd.pid .
561.It Fl exit_delay Ar ms
562Specify delay in ms before daemon exit after signal.
563The default is
564.Pa 10000 .
565.El
566.Sh RUNNING NATD
567The following steps are necessary before attempting to run
568.Nm :
569.Bl -enum
570.It
571Build a custom kernel with the following options:
572.Bd -literal -offset indent
573options IPFIREWALL
574options IPDIVERT
575.Ed
576.Pp
577Refer to the handbook for detailed instructions on building a custom
578kernel.
579.It
580Ensure that your machine is acting as a gateway.
581This can be done by specifying the line
582.Pp
583.Dl gateway_enable=YES
584.Pp
585in the
586.Pa /etc/rc.conf
587file or using the command
588.Pp
589.Dl "sysctl net.inet.ip.forwarding=1"
590.Pp
591.It
592If you use the
593.Fl interface
594option, make sure that your interface is already configured.
595If, for example, you wish to specify
596.Ql tun0
597as your
598.Ar interface ,
599and you are using
600.Xr ppp 8
601on that interface, you must make sure that you start
602.Nm ppp
603prior to starting
604.Nm .
605.El
606.Pp
607Running
608.Nm
609is fairly straight forward.
610The line
611.Pp
612.Dl natd -interface ed0
613.Pp
614should suffice in most cases (substituting the correct interface name).
615Please check
616.Xr rc.conf 5
617on how to configure it to be started automatically during boot.
618Once
619.Nm
620is running, you must ensure that traffic is diverted to
621.Nm :
622.Bl -enum
623.It
624You will need to adjust the
625.Pa /etc/rc.firewall
626script to taste.
627If you are not interested in having a firewall, the
628following lines will do:
629.Bd -literal -offset indent
630/sbin/ipfw -f flush
631/sbin/ipfw add divert natd all from any to any via ed0
632/sbin/ipfw add pass all from any to any
633.Ed
634.Pp
635The second line depends on your interface (change
636.Ql ed0
637as appropriate).
638.Pp
639You should be aware of the fact that, with these firewall settings,
640everyone on your local network can fake his source-address using your
641host as gateway.
642If there are other hosts on your local network, you are strongly
643encouraged to create firewall rules that only allow traffic to and
644from trusted hosts.
645.Pp
646If you specify real firewall rules, it is best to specify line 2 at
647the start of the script so that
648.Nm
649sees all packets before they are dropped by the firewall.
650.Pp
651After translation by
652.Nm ,
653packets re-enter the firewall at the rule number following the rule number
654that caused the diversion (not the next rule if there are several at the
655same number).
656.It
657Enable your firewall by setting
658.Pp
659.Dl firewall_enable=YES
660.Pp
661in
662.Pa /etc/rc.conf .
663This tells the system startup scripts to run the
664.Pa /etc/rc.firewall
665script.
666If you do not wish to reboot now, just run this by hand from the console.
667NEVER run this from a remote session unless you put it into the background.
668If you do, you will lock yourself out after the flush takes place, and
669execution of
670.Pa /etc/rc.firewall
671will stop at this point - blocking all accesses permanently.
672Running the script in the background should be enough to prevent this
673disaster.
674.El
675.Sh MULTIPLE INSTANCES
676It is not so uncommon to have a need of aliasing to several external IP
677addresses.
678While this traditionally was achieved by running several
679.Nm
680processes with independent configurations,
681.Nm
682can have multiple aliasing instances in a single process,
683also allowing them to be not so independent of each other.
684For example, let us see a common task of load balancing two
685channels to different providers on a machine with two external
686interfaces
687.Ql sis0
688(with IP 1.2.3.4) and
689.Ql sis2
690(with IP 2.3.4.5):
691.Bd -literal -offset indent
692          net 1.2.3.0/24
6931.2.3.1 ------------------ sis0
694(router)                (1.2.3.4)
695                                         net 10.0.0.0/24
696                                  sis1 ------------------- 10.0.0.2
697                               (10.0.0.1)
698          net 2.3.4.0/24
6992.3.4.1 ------------------ sis2
700(router)                (2.3.4.5)
701.Ed
702.Pp
703Default route is out via
704.Ql sis0 .
705.Pp
706Interior machine (10.0.0.2) is accessible on TCP port 122 through
707both exterior IPs, and outgoing connections choose a path randomly
708between
709.Ql sis0
710and
711.Ql sis2 .
712.Pp
713The way this works is that
714.Pa natd.conf
715builds two instances of the aliasing engine.
716.Pp
717In addition to these instances' private
718.Xr divert 4
719sockets, a third socket called the
720.Dq globalport
721is created; packets sent to
722.Nm
723via this one will be matched against all instances and translated
724if an existing entry is found, and unchanged if no entry is found.
725The following lines are placed into
726.Pa /etc/natd.conf :
727.Bd -literal -offset indent
728log
729deny_incoming
730verbose
731
732instance default
733interface sis0
734port 1000
735redirect_port tcp 10.0.0.2:122 122
736
737instance sis2
738interface sis2
739port 2000
740redirect_port tcp 10.0.0.2:122 122
741
742globalport 3000
743.Ed
744.Pp
745And the following
746.Xr ipfw 8
747rules are used:
748.Bd -literal -offset indent
749ipfw -f flush
750
751ipfw add      allow ip from any to any via sis1
752
753ipfw add      skipto 1000 ip from any to any in via sis0
754ipfw add      skipto 2000 ip from any to any out via sis0
755ipfw add      skipto 3000 ip from any to any in via sis2
756ipfw add      skipto 4000 ip from any to any out via sis2
757
758ipfw add 1000 count ip from any to any
759
760ipfw add      divert 1000 ip from any to any
761ipfw add      allow ip from any to any
762
763ipfw add 2000 count ip from any to any
764
765ipfw add      divert 3000 ip from any to any
766
767ipfw add      allow ip from 1.2.3.4 to any
768ipfw add      skipto 5000 ip from 2.3.4.5 to any
769
770ipfw add      prob .5 skipto 4000 ip from any to any
771
772ipfw add      divert 1000 ip from any to any
773ipfw add      allow ip from any to any
774
775ipfw add 3000 count ip from any to any
776
777ipfw add      divert 2000 ip from any to any
778ipfw add      allow ip from any to any
779
780ipfw add 4000 count ip from any to any
781
782ipfw add      divert 2000 ip from any to any
783
784ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24
785ipfw add      allow ip from any to any
786.Ed
787.Pp
788Here the packet from internal network to Internet goes out via
789.Ql sis0
790(rule number 2000) and gets catched by the
791.Ic globalport
792socket (3000).
793After that, either a match is found in a translation table
794of one of the two instances, or the packet is passed to one
795of the two other
796.Xr divert 4
797ports (1000 or 2000), with equal probability.
798This ensures that load balancing is done on a per-flow basis
799(i.e., packets from a single TCP connection always flow through the
800same interface).
801Translated packets with source IP of a non-default interface
802.Pq Ql sis2
803are forwarded to the appropriate router on that interface.
804.Sh SEE ALSO
805.Xr libalias 3 ,
806.Xr divert 4 ,
807.Xr protocols 5 ,
808.Xr rc.conf 5 ,
809.Xr services 5 ,
810.Xr syslog.conf 5 ,
811.Xr init 8 ,
812.Xr ipfw 8 ,
813.Xr ppp 8
814.Sh AUTHORS
815This program is the result of the efforts of many people at different
816times:
817.Pp
818.An Archie Cobbs Aq archie@FreeBSD.org
819(divert sockets)
820.An Charles Mott Aq cm@linktel.net
821(packet aliasing)
822.An Eivind Eklund Aq perhaps@yes.no
823(IRC support & misc additions)
824.An Ari Suutari Aq suutari@iki.fi
825(natd)
826.An Dru Nelson Aq dnelson@redwoodsoft.com
827(early PPTP support)
828.An Brian Somers Aq brian@awfulhak.org
829(glue)
830.An Ruslan Ermilov Aq ru@FreeBSD.org
831(natd, packet aliasing, glue)
832.An Poul-Henning Kamp Aq phk@FreeBSD.org
833(multiple instances)
834