xref: /freebsd/sbin/natd/natd.8 (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1.Dd October 5, 2016
2.Dt NATD 8
3.Os
4.Sh NAME
5.Nm natd
6.Nd Network Address Translation daemon
7.Sh SYNOPSIS
8.Nm
9.Bk -words
10.Op Fl unregistered_only | u
11.Op Fl log | l
12.Op Fl proxy_only
13.Op Fl reverse
14.Op Fl deny_incoming | d
15.Op Fl use_sockets | s
16.Op Fl same_ports | m
17.Op Fl verbose | v
18.Op Fl dynamic
19.Op Fl in_port | i Ar port
20.Op Fl out_port | o Ar port
21.Op Fl port | p Ar port
22.Op Fl alias_address | a Ar address
23.Op Fl target_address | t Ar address
24.Op Fl interface | n Ar interface
25.Op Fl proxy_rule Ar proxyspec
26.Op Fl redirect_port Ar linkspec
27.Op Fl redirect_proto Ar linkspec
28.Op Fl redirect_address Ar linkspec
29.Op Fl config | f Ar configfile
30.Op Fl instance Ar instancename
31.Op Fl globalport Ar port
32.Op Fl log_denied
33.Op Fl log_facility Ar facility_name
34.Op Fl punch_fw Ar firewall_range
35.Op Fl skinny_port Ar port
36.Op Fl log_ipfw_denied
37.Op Fl pid_file | P Ar pidfile
38.Op Fl exit_delay | P Ar ms
39.Ek
40.Sh DESCRIPTION
41The
42.Nm
43utility provides a Network Address Translation facility for use
44with
45.Xr divert 4
46sockets under
47.Fx .
48.Pp
49(If you need NAT on a PPP link,
50.Xr ppp 8
51provides the
52.Fl nat
53option that gives most of the
54.Nm
55functionality, and uses the same
56.Xr libalias 3
57library.)
58.Pp
59The
60.Nm
61utility normally runs in the background as a daemon.
62It is passed raw IP packets as they travel into and out of the machine,
63and will possibly change these before re-injecting them back into the
64IP packet stream.
65.Pp
66It changes all packets destined for another host so that their source
67IP address is that of the current machine.
68For each packet changed in this manner, an internal table entry is
69created to record this fact.
70The source port number is also changed to indicate the table entry
71applying to the packet.
72Packets that are received with a target IP of the current host are
73checked against this internal table.
74If an entry is found, it is used to determine the correct target IP
75address and port to place in the packet.
76.Pp
77The following command line options are available:
78.Bl -tag -width Fl
79.It Fl log | l
80Log various aliasing statistics and information to the file
81.Pa /var/log/alias.log .
82This file is truncated each time
83.Nm
84is started.
85.It Fl deny_incoming | d
86Do not pass incoming packets that have no
87entry in the internal translation table.
88.Pp
89If this option is not used, then such a packet will be altered
90using the rules in
91.Fl target_address
92below, and the entry will be made in the internal translation table.
93.It Fl log_denied
94Log denied incoming packets via
95.Xr syslog 3
96(see also
97.Fl log_facility ) .
98.It Fl log_facility Ar facility_name
99Use specified log facility when logging information via
100.Xr syslog 3 .
101Argument
102.Ar facility_name
103is one of the keywords specified in
104.Xr syslog.conf 5 .
105.It Fl use_sockets | s
106Allocate a
107.Xr socket 2
108in order to establish an FTP data or IRC DCC send connection.
109This option uses more system resources, but guarantees successful
110connections when port numbers conflict.
111.It Fl same_ports | m
112Try to keep the same port number when altering outgoing packets.
113With this option, protocols such as RPC will have a better chance
114of working.
115If it is not possible to maintain the port number, it will be silently
116changed as per normal.
117.It Fl verbose | v
118Do not call
119.Xr daemon 3
120on startup.
121Instead, stay attached to the controlling terminal and display all packet
122alterations to the standard output.
123This option should only be used for debugging purposes.
124.It Fl unregistered_only | u
125Only alter outgoing packets with an
126.Em unregistered
127source address.
128According to RFC 1918, unregistered source addresses are 10.0.0.0/8,
129172.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 Oo - Ns Ar targetPORT Oc Xc
133.Oo Ar aliasIP Ns : Oc Ns Xo
134.Ar aliasPORT Ns Oo - Ns Ar aliasPORT Oc Xc
135.Oo Ar remoteIP Ns Oo : Ns
136.Ar remotePORT Ns Op - Ns Ar remotePORT
137.Oc Oc
138.Xc
139Redirect incoming connections arriving to given port(s) to another host
140and port(s).
141Argument
142.Ar proto
143is either
144.Ar tcp
145or
146.Ar udp ,
147.Ar targetIP
148is the desired target IP address,
149.Ar targetPORT
150is the desired target port number or range,
151.Ar aliasPORT
152is the requested port number or range, and
153.Ar aliasIP
154is the aliasing address.
155Arguments
156.Ar remoteIP
157and
158.Ar remotePORT
159can be used to specify the connection more accurately if necessary.
160If
161.Ar remotePORT
162is not specified, it is assumed to be all ports.
163.Pp
164Arguments
165.Ar targetIP , aliasIP
166and
167.Ar remoteIP
168can be given as IP addresses or as hostnames.
169The
170.Ar targetPORT , aliasPORT
171and
172.Ar remotePORT
173ranges need not be the same numerically, but must have the same size.
174When
175.Ar targetPORT , aliasPORT
176or
177.Ar remotePORT
178specifies a singular value (not a range), it can be given as a service
179name that is searched for in the
180.Xr services 5
181database.
182.Pp
183For example, the argument
184.Pp
185.Dl Ar tcp inside1:telnet 6666
186.Pp
187means that incoming TCP packets destined for port 6666 on this machine
188will be sent to the telnet port on the inside1 machine.
189.Pp
190.Dl Ar tcp inside2:2300-2399 3300-3399
191.Pp
192will redirect incoming connections on ports 3300-3399 to host
193inside2, ports 2300-2399.
194The 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
197.Oc
198Redirect incoming IP packets of protocol
199.Ar proto
200(see
201.Xr protocols 5 )
202destined for
203.Ar publicIP
204address to a
205.Ar localIP
206address and vice versa.
207.Pp
208If
209.Ar publicIP
210is not specified, then the default aliasing address is used.
211If
212.Ar remoteIP
213is specified, then only packets coming from/to
214.Ar remoteIP
215will match the rule.
216.It Fl redirect_address Ar localIP publicIP
217Redirect traffic for public IP address to a machine on the local
218network.
219This function is known as
220.Em static NAT .
221Normally static NAT is useful if your ISP has allocated a small block
222of IP addresses to you, but it can even be used in the case of single
223address:
224.Pp
225.Dl Ar redirect_address 10.0.0.8 0.0.0.0
226.Pp
227The above command would redirect all incoming traffic
228to machine 10.0.0.8.
229.Pp
230If several address aliases specify the same public address
231as follows
232.Bd -literal -offset indent
233redirect_address 192.168.0.2 public_addr
234redirect_address 192.168.0.3 public_addr
235redirect_address 192.168.0.4 public_addr
236.Ed
237.Pp
238the incoming traffic will be directed to the last
239translated local address (192.168.0.4), but outgoing
240traffic from the first two addresses will still be aliased
241to appear from the specified
242.Ar public_addr .
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
248.Ar ...\&
249.Oc Xc Oc Xc
250.Oo Ar aliasIP Ns : Oc Ns Xo
251.Ar aliasPORT
252.Xc
253.Oo Ar remoteIP Ns
254.Op : Ns Ar remotePORT
255.Oc
256.Xc
257.It Fl redirect_address Xo
258.Ar localIP Ns Oo , Ns
259.Ar localIP Ns Oo , Ns
260.Ar ...\&
261.Oc Oc
262.Ar publicIP
263.Xc
264These forms of
265.Fl redirect_port
266and
267.Fl redirect_address
268are used to transparently offload network load on a single server and
269distribute the load across a pool of servers.
270This function is known as
271.Em LSNAT
272(RFC 2391).
273For example, the argument
274.Pp
275.Dl Ar tcp www1:http,www2:http,www3:http www:http
276.Pp
277means that incoming HTTP requests for host www will be transparently
278redirected to one of the www1, www2 or www3, where a host is selected
279simply on a round-robin basis, without regard to load on the net.
280.It Fl dynamic
281If the
282.Fl n
283or
284.Fl interface
285option is used,
286.Nm
287will monitor the routing socket for alterations to the
288.Ar interface
289passed.
290If the interface's IP address is changed,
291.Nm
292will dynamically alter its concept of the alias address.
293.It Fl in_port | i Ar port
294Read from and write to
295.Xr divert 4
296port
297.Ar port ,
298treating all packets as
299.Dq incoming .
300.It Fl out_port | o Ar port
301Read from and write to
302.Xr divert 4
303port
304.Ar port ,
305treating all packets as
306.Dq outgoing .
307.It Fl port | p Ar port
308Read from and write to
309.Xr divert 4
310port
311.Ar port ,
312distinguishing packets as
313.Dq incoming
314or
315.Dq outgoing
316using the rules specified in
317.Xr divert 4 .
318If
319.Ar port
320is not numeric, it is searched for in the
321.Xr services 5
322database.
323If this option is not specified, the divert port named
324.Ar natd
325will be used as a default.
326.It Fl alias_address | a Ar address
327Use
328.Ar address
329as the aliasing address.
330Either this or the
331.Fl interface
332option must be used (but not both),
333if the
334.Fl proxy_only
335option is not specified.
336The specified address is usually the address assigned to the
337.Dq public
338network interface.
339.Pp
340All data passing
341.Em out
342will be rewritten with a source address equal to
343.Ar address .
344All data coming
345.Em in
346will be checked to see if it matches any already-aliased outgoing
347connection.
348If it does, the packet is altered accordingly.
349If not, all
350.Fl redirect_port ,
351.Fl redirect_proto
352and
353.Fl redirect_address
354assignments are checked and actioned.
355If no other action can be made and if
356.Fl deny_incoming
357is not specified, the packet is delivered to the local machine
358using the rules specified in
359.Fl target_address
360option below.
361.It Fl t | target_address Ar address
362Set the target address.
363When an incoming packet not associated with any pre-existing link
364arrives at the host machine, it will be sent to the specified
365.Ar address .
366.Pp
367The target address may be set to
368.Ar 255.255.255.255 ,
369in which case all new incoming packets go to the alias address set by
370.Fl alias_address
371or
372.Fl interface .
373.Pp
374If this option is not used, or called with the argument
375.Ar 0.0.0.0 ,
376then all new incoming packets go to the address specified in
377the packet.
378This allows external machines to talk directly to internal machines if
379they can route packets to the machine in question.
380.It Fl interface | n Ar interface
381Use
382.Ar interface
383to determine the aliasing address.
384If there is a possibility that the IP address associated with
385.Ar interface
386may change, the
387.Fl dynamic
388option should also be used.
389If this option is not specified, the
390.Fl alias_address
391option must be used.
392.Pp
393The specified
394.Ar interface
395is usually the
396.Dq public
397(or
398.Dq external )
399network interface.
400.It Fl config | f Ar file
401Read configuration from
402.Ar file .
403A
404.Ar file
405should contain a list of options, one per line, in the same form
406as the long form of the above command line options.
407For example, the line
408.Pp
409.Dl alias_address 158.152.17.1
410.Pp
411would specify an alias address of 158.152.17.1.
412Options that do not take an argument are specified with an argument of
413.Ar yes
414or
415.Ar no
416in the configuration file.
417For example, the line
418.Pp
419.Dl log yes
420.Pp
421is synonymous with
422.Fl log .
423.Pp
424Options can be divided to several sections.
425Each section applies to own
426.Nm
427instance.
428This ability allows the configuration of one
429.Nm
430process for several NAT instances.
431The first instance that always exists is a "default" instance.
432Each another instance should begin with
433.Pp
434.Dl instance Ar instance_name
435.Pp
436At the next should be placed a configuration option.
437Example:
438.Pp
439.Dl \&# default instance
440.Dl port 8668
441.Dl alias_address 158.152.17.1
442.Pp
443.Dl \&# second instance
444.Dl instance dsl1
445.Dl port 8888
446.Dl alias_address 192.168.0.1
447.Pp
448Trailing spaces and empty lines are ignored.
449A
450.Ql \&#
451sign will mark the rest of the line as a comment.
452.It Fl instance Ar instancename
453This option switches command line options processing to configure instance
454.Ar instancename
455(creating it if necessary) till the next
456.Fl instance
457option or end of command line.
458It is easier to set up multiple instances in the configuration file
459specified with the
460.Fl config
461option rather than on a command line.
462.It Fl globalport Ar port
463Read from and write to
464.Xr divert 4
465port
466.Ar port ,
467treating all packets as
468.Dq outgoing .
469This option is intended to be used with multiple instances:
470packets received on this port are checked against
471internal translation tables of every configured instance.
472If an entry is found, packet is aliased according to that entry.
473If no entry was found in any of the instances, packet is passed
474unchanged, and no new entry will be created.
475See the section
476.Sx MULTIPLE INSTANCES
477for more details.
478.It Fl reverse
479This option makes
480.Nm
481reverse the way it handles
482.Dq incoming
483and
484.Dq outgoing
485packets, allowing it to operate on the
486.Dq internal
487network interface rather than the
488.Dq external
489one.
490.Pp
491This can be useful in some transparent proxying situations
492when outgoing traffic is redirected to the local machine
493and
494.Nm
495is running on the internal interface (it usually runs on the
496external interface).
497.It Fl proxy_only
498Force
499.Nm
500to perform transparent proxying only.
501Normal address translation is not performed.
502.It Fl proxy_rule Xo
503.Op Ar type encode_ip_hdr | encode_tcp_stream
504.Ar port xxxx
505.Ar server a.b.c.d:yyyy
506.Xc
507Enable transparent proxying.
508Outgoing TCP packets with the given port going through this
509host to any other host are redirected to the given server and port.
510Optionally, the original target address can be encoded into the packet.
511Use
512.Ar encode_ip_hdr
513to put this information into the IP option field or
514.Ar encode_tcp_stream
515to inject the data into the beginning of the TCP stream.
516.It Fl punch_fw Xo
517.Ar basenumber Ns : Ns Ar count
518.Xc
519This option directs
520.Nm
521to
522.Dq punch holes
523in an
524.Xr ipfirewall 4
525based firewall for FTP/IRC DCC connections.
526This is done dynamically by installing temporary firewall rules which
527allow a particular connection (and only that connection) to go through
528the firewall.
529The rules are removed once the corresponding connection terminates.
530.Pp
531A maximum of
532.Ar count
533rules starting from the rule number
534.Ar basenumber
535will be used for punching firewall holes.
536The range will be cleared for all rules on startup.
537This option has no effect when the kernel is in security
538level 3, see
539.Xr init 8
540for more information.
541.It Fl skinny_port Ar port
542This option allows you to specify the TCP port used for
543the Skinny Station protocol.
544Skinny is used by Cisco IP phones to communicate with
545Cisco Call Managers to set up voice over IP calls.
546By default, Skinny aliasing is not performed.
547The typical port value for Skinny is 2000.
548.It Fl log_ipfw_denied
549Log when a packet cannot be re-injected because an
550.Xr ipfw 8
551rule blocks it.
552This is the default with
553.Fl verbose .
554.It Fl pid_file | P Ar file
555Specify an alternate file in which to store the process ID.
556The default is
557.Pa /var/run/natd.pid .
558.It Fl exit_delay Ar ms
559Specify delay in ms before daemon exit after signal.
560The default is
561.Pa 10000 .
562.El
563.Sh RUNNING NATD
564The following steps are necessary before attempting to run
565.Nm :
566.Bl -enum
567.It
568Build a custom kernel with the following options:
569.Bd -literal -offset indent
570options IPFIREWALL
571options IPDIVERT
572.Ed
573.Pp
574Refer to the handbook for detailed instructions on building a custom
575kernel.
576.It
577Ensure that your machine is acting as a gateway.
578This can be done by specifying the line
579.Pp
580.Dl gateway_enable=YES
581.Pp
582in the
583.Pa /etc/rc.conf
584file or using the command
585.Pp
586.Dl "sysctl net.inet.ip.forwarding=1"
587.It
588If you use the
589.Fl interface
590option, make sure that your interface is already configured.
591If, for example, you wish to specify
592.Ql tun0
593as your
594.Ar interface ,
595and you are using
596.Xr ppp 8
597on that interface, you must make sure that you start
598.Nm ppp
599prior to starting
600.Nm .
601.El
602.Pp
603Running
604.Nm
605is fairly straight forward.
606The line
607.Pp
608.Dl natd -interface ed0
609.Pp
610should suffice in most cases (substituting the correct interface name).
611Please check
612.Xr rc.conf 5
613on how to configure it to be started automatically during boot.
614Once
615.Nm
616is running, you must ensure that traffic is diverted to
617.Nm :
618.Bl -enum
619.It
620You will need to adjust the
621.Pa /etc/rc.firewall
622script to taste.
623If you are not interested in having a firewall, the
624following lines will do:
625.Bd -literal -offset indent
626/sbin/ipfw -f flush
627/sbin/ipfw add divert natd all from any to any via ed0
628/sbin/ipfw add pass all from any to any
629.Ed
630.Pp
631The second line depends on your interface (change
632.Ql ed0
633as appropriate).
634.Pp
635You should be aware of the fact that, with these firewall settings,
636everyone on your local network can fake his source-address using your
637host as gateway.
638If there are other hosts on your local network, you are strongly
639encouraged to create firewall rules that only allow traffic to and
640from trusted hosts.
641.Pp
642If you specify real firewall rules, it is best to specify line 2 at
643the start of the script so that
644.Nm
645sees all packets before they are dropped by the firewall.
646.Pp
647After translation by
648.Nm ,
649packets re-enter the firewall at the rule number following the rule number
650that caused the diversion (not the next rule if there are several at the
651same number).
652.It
653Enable your firewall by setting
654.Pp
655.Dl firewall_enable=YES
656.Pp
657in
658.Pa /etc/rc.conf .
659This tells the system startup scripts to run the
660.Pa /etc/rc.firewall
661script.
662If you do not wish to reboot now, just run this by hand from the console.
663NEVER run this from a remote session unless you put it into the background.
664If you do, you will lock yourself out after the flush takes place, and
665execution of
666.Pa /etc/rc.firewall
667will stop at this point - blocking all accesses permanently.
668Running the script in the background should be enough to prevent this
669disaster.
670.El
671.Sh MULTIPLE INSTANCES
672It is not so uncommon to have a need of aliasing to several external IP
673addresses.
674While this traditionally was achieved by running several
675.Nm
676processes with independent configurations,
677.Nm
678can have multiple aliasing instances in a single process,
679also allowing them to be not so independent of each other.
680For example, let us see a common task of load balancing two
681channels to different providers on a machine with two external
682interfaces
683.Ql sis0
684(with IP 1.2.3.4) and
685.Ql sis2
686(with IP 2.3.4.5):
687.Bd -literal -offset indent
688          net 1.2.3.0/24
6891.2.3.1 ------------------ sis0
690(router)                (1.2.3.4)
691                                         net 10.0.0.0/24
692                                  sis1 ------------------- 10.0.0.2
693                               (10.0.0.1)
694          net 2.3.4.0/24
6952.3.4.1 ------------------ sis2
696(router)                (2.3.4.5)
697.Ed
698.Pp
699Default route is out via
700.Ql sis0 .
701.Pp
702Interior machine (10.0.0.2) is accessible on TCP port 122 through
703both exterior IPs, and outgoing connections choose a path randomly
704between
705.Ql sis0
706and
707.Ql sis2 .
708.Pp
709The way this works is that
710.Pa natd.conf
711builds two instances of the aliasing engine.
712.Pp
713In addition to these instances' private
714.Xr divert 4
715sockets, a third socket called the
716.Dq globalport
717is created; packets sent to
718.Nm
719via this one will be matched against all instances and translated
720if an existing entry is found, and unchanged if no entry is found.
721The following lines are placed into
722.Pa /etc/natd.conf :
723.Bd -literal -offset indent
724log
725deny_incoming
726verbose
727
728instance default
729interface sis0
730port 1000
731redirect_port tcp 10.0.0.2:122 122
732
733instance sis2
734interface sis2
735port 2000
736redirect_port tcp 10.0.0.2:122 122
737
738globalport 3000
739.Ed
740.Pp
741And the following
742.Xr ipfw 8
743rules are used:
744.Bd -literal -offset indent
745ipfw -f flush
746
747ipfw add      allow ip from any to any via sis1
748
749ipfw add      skipto 1000 ip from any to any in via sis0
750ipfw add      skipto 2000 ip from any to any out via sis0
751ipfw add      skipto 3000 ip from any to any in via sis2
752ipfw add      skipto 4000 ip from any to any out via sis2
753
754ipfw add 1000 count ip from any to any
755
756ipfw add      divert 1000 ip from any to any
757ipfw add      allow ip from any to any
758
759ipfw add 2000 count ip from any to any
760
761ipfw add      divert 3000 ip from any to any
762
763ipfw add      allow ip from 1.2.3.4 to any
764ipfw add      skipto 5000 ip from 2.3.4.5 to any
765
766ipfw add      prob .5 skipto 4000 ip from any to any
767
768ipfw add      divert 1000 ip from any to any
769ipfw add      allow ip from any to any
770
771ipfw add 3000 count ip from any to any
772
773ipfw add      divert 2000 ip from any to any
774ipfw add      allow ip from any to any
775
776ipfw add 4000 count ip from any to any
777
778ipfw add      divert 2000 ip from any to any
779
780ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24
781ipfw add      allow ip from any to any
782.Ed
783.Pp
784Here the packet from internal network to Internet goes out via
785.Ql sis0
786(rule number 2000) and gets caught by the
787.Ic globalport
788socket (3000).
789After that, either a match is found in a translation table
790of one of the two instances, or the packet is passed to one
791of the two other
792.Xr divert 4
793ports (1000 or 2000), with equal probability.
794This ensures that load balancing is done on a per-flow basis
795(i.e., packets from a single TCP connection always flow through the
796same interface).
797Translated packets with source IP of a non-default interface
798.Pq Ql sis2
799are forwarded to the appropriate router on that interface.
800.Sh SEE ALSO
801.Xr libalias 3 ,
802.Xr divert 4 ,
803.Xr protocols 5 ,
804.Xr rc.conf 5 ,
805.Xr services 5 ,
806.Xr syslog.conf 5 ,
807.Xr init 8 ,
808.Xr ipfw 8 ,
809.Xr ppp 8
810.Sh HISTORY
811The
812.Nm
813utility appeared in
814.Fx 3.0 .
815.Sh AUTHORS
816This program is the result of the efforts of many people at different
817times:
818.Pp
819.An Archie Cobbs Aq Mt archie@FreeBSD.org
820(divert sockets)
821.An Charles Mott Aq Mt cm@linktel.net
822(packet aliasing)
823.An Eivind Eklund Aq Mt perhaps@yes.no
824(IRC support & misc additions)
825.An Ari Suutari Aq Mt suutari@iki.fi
826(natd)
827.An Dru Nelson Aq Mt dnelson@redwoodsoft.com
828(early PPTP support)
829.An Brian Somers Aq Mt brian@awfulhak.org
830(glue)
831.An Ruslan Ermilov Aq Mt ru@FreeBSD.org
832(natd, packet aliasing, glue)
833.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
834(multiple instances)
835