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