xref: /freebsd/sbin/natd/natd.8 (revision dce6e6518b85561495cff38a3074a69d29d58a55)
1.\" $FreeBSD$
2.Dd February 28, 2003
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 log_denied
32.Op Fl log_facility Ar facility_name
33.Op Fl punch_fw Ar firewall_range
34.Op Fl log_ipfw_denied
35.Ek
36.Sh DESCRIPTION
37The
38.Nm
39utility provides a Network Address Translation facility for use
40with
41.Xr divert 4
42sockets under
43.Fx .
44.Pp
45(If you need NAT on a PPP link,
46.Xr ppp 8
47provides the
48.Fl nat
49option that gives most of the
50.Nm
51functionality, and uses the same
52.Xr libalias 3
53library.)
54.Pp
55The
56.Nm
57utility normally runs in the background as a daemon.
58It is passed raw IP packets as they travel into and out of the machine,
59and will possibly change these before re-injecting them back into the
60IP packet stream.
61.Pp
62It changes all packets destined for another host so that their source
63IP number is that of the current machine.
64For each packet changed in this manner, an internal table entry is
65created to record this fact.
66The source port number is also changed to indicate the table entry
67applying to the packet.
68Packets that are received with a target IP of the current host are
69checked against this internal table.
70If an entry is found, it is used to determine the correct target IP
71number and port to place in the packet.
72.Pp
73The following command line options are available:
74.Bl -tag -width Fl
75.It Fl log | l
76Log various aliasing statistics and information to the file
77.Pa /var/log/alias.log .
78This file is truncated each time
79.Nm
80is started.
81.It Fl deny_incoming | d
82Do not pass incoming packets that have no
83entry in the internal translation table.
84.Pp
85If this option is not used, then such a packet will be altered
86using the rules in
87.Fl target_address
88below, and the entry will be made in the internal translation table.
89.It Fl log_denied
90Log denied incoming packets via
91.Xr syslog 3
92(see also
93.Fl log_facility ) .
94.It Fl log_facility Ar facility_name
95Use specified log facility when logging information via
96.Xr syslog 3 .
97Argument
98.Ar facility_name
99is one of the keywords specified in
100.Xr syslog.conf 5 .
101.It Fl use_sockets | s
102Allocate a
103.Xr socket 2
104in order to establish an FTP data or IRC DCC send connection.
105This option uses more system resources, but guarantees successful
106connections when port numbers conflict.
107.It Fl same_ports | m
108Try to keep the same port number when altering outgoing packets.
109With this option, protocols such as RPC will have a better chance
110of working.
111If it is not possible to maintain the port number, it will be silently
112changed as per normal.
113.It Fl verbose | v
114Do not call
115.Xr daemon 3
116on startup.
117Instead, stay attached to the controlling terminal and display all packet
118alterations to the standard output.
119This option should only be used for debugging purposes.
120.It Fl unregistered_only | u
121Only alter outgoing packets with an
122.Em unregistered
123source address.
124According to RFC 1918, unregistered source addresses are 10.0.0.0/8,
125172.16.0.0/12 and 192.168.0.0/16.
126.It Fl redirect_port Ar proto Xo
127.Ar targetIP Ns : Ns Xo
128.Ar targetPORT Ns Op - Ns Ar targetPORT Xc
129.Op Ar aliasIP Ns : Ns Xo
130.Ar aliasPORT Ns Op - Ns Ar aliasPORT Xc
131.Oo Ar remoteIP Ns Oo : Ns
132.Ar remotePORT Ns Op - Ns Ar remotePORT
133.Oc Oc
134.Xc
135Redirect incoming connections arriving to given port(s) to another host
136and port(s).
137Argument
138.Ar proto
139is either
140.Ar tcp
141or
142.Ar udp ,
143.Ar targetIP
144is the desired target IP number,
145.Ar targetPORT
146is the desired target port number or range,
147.Ar aliasPORT
148is the requested port number or range, and
149.Ar aliasIP
150is the aliasing address.
151Arguments
152.Ar remoteIP
153and
154.Ar remotePORT
155can be used to specify the connection more accurately if necessary.
156The
157.Ar targetPORT
158range and
159.Ar aliasPORT
160range need not be the same numerically, but must have the same size.
161If
162.Ar remotePORT
163is not specified, it is assumed to be all ports.
164If
165.Ar remotePORT
166is specified, it must match the size of
167.Ar targetPORT ,
168or be 0 (all ports).
169For example, the argument
170.Pp
171.Dl Ar tcp inside1:telnet 6666
172.Pp
173means that incoming TCP packets destined for port 6666 on this machine
174will be sent to the telnet port on the inside1 machine.
175.Pp
176.Dl Ar tcp inside2:2300-2399 3300-3399
177.Pp
178will redirect incoming connections on ports 3300-3399 to host
179inside2, ports 2300-2399.
180The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc.
181.It Fl redirect_proto Ar proto localIP Oo
182.Ar publicIP Op Ar remoteIP
183.Oc
184Redirect incoming IP packets of protocol
185.Ar proto
186(see
187.Xr protocols 5 )
188destined for
189.Ar publicIP
190address to a
191.Ar localIP
192address and vice versa.
193.Pp
194If
195.Ar publicIP
196is not specified, then the default aliasing address is used.
197If
198.Ar remoteIP
199is specified, then only packets coming from/to
200.Ar remoteIP
201will match the rule.
202.It Fl redirect_address Ar localIP publicIP
203Redirect traffic for public IP address to a machine on the local
204network.
205This function is known as
206.Em static NAT .
207Normally static NAT is useful if your ISP has allocated a small block
208of IP addresses to you, but it can even be used in the case of single
209address:
210.Pp
211.Dl Ar redirect_address 10.0.0.8 0.0.0.0
212.Pp
213The above command would redirect all incoming traffic
214to machine 10.0.0.8.
215.Pp
216If several address aliases specify the same public address
217as follows
218.Bd -literal -offset indent
219.Ar redirect_address 192.168.0.2 public_addr
220.Ar redirect_address 192.168.0.3 public_addr
221.Ar redirect_address 192.168.0.4 public_addr
222.Ed
223.Pp
224the incoming traffic will be directed to the last
225translated local address (192.168.0.4), but outgoing
226traffic from the first two addresses will still be aliased
227to appear from the specified
228.Ar public_addr .
229.It Fl redirect_port Ar proto Xo
230.Ar targetIP Ns : Ns Xo
231.Ar targetPORT Ns Oo , Ns
232.Ar targetIP Ns : Ns Xo
233.Ar targetPORT Ns Oo , Ns
234.Ar ...\&
235.Oc Oc
236.Xc
237.Xc
238.Op Ar aliasIP Ns : Ns Xo
239.Ar aliasPORT
240.Xc
241.Oo Ar remoteIP Ns
242.Op : Ns Ar remotePORT
243.Oc
244.Xc
245.It Fl redirect_address Xo
246.Ar localIP Ns Oo , Ns
247.Ar localIP Ns Oo , Ns
248.Ar ...\&
249.Oc Oc
250.Ar publicIP
251.Xc
252These forms of
253.Fl redirect_port
254and
255.Fl redirect_address
256are used to transparently offload network load on a single server and
257distribute the load across a pool of servers.
258This function is known as
259.Em LSNAT
260(RFC 2391).
261For example, the argument
262.Pp
263.Dl Ar tcp www1:http,www2:http,www3:http www:http
264.Pp
265means that incoming HTTP requests for host www will be transparently
266redirected to one of the www1, www2 or www3, where a host is selected
267simply on a round-robin basis, without regard to load on the net.
268.It Fl dynamic
269If the
270.Fl n
271or
272.Fl interface
273option is used,
274.Nm
275will monitor the routing socket for alterations to the
276.Ar interface
277passed.
278If the interface's IP number is changed,
279.Nm
280will dynamically alter its concept of the alias address.
281.It Fl in_port | i Ar port
282Read from and write to
283.Xr divert 4
284port
285.Ar port ,
286treating all packets as
287.Dq incoming .
288.It Fl out_port | o Ar port
289Read from and write to
290.Xr divert 4
291port
292.Ar port ,
293treating all packets as
294.Dq outgoing .
295.It Fl port | p Ar port
296Read from and write to
297.Xr divert 4
298port
299.Ar port ,
300distinguishing packets as
301.Dq incoming
302or
303.Dq outgoing
304using the rules specified in
305.Xr divert 4 .
306If
307.Ar port
308is not numeric, it is searched for in the
309.Xr services 5
310database.
311If this option is not specified, the divert port named
312.Ar natd
313will be used as a default.
314.It Fl alias_address | a Ar address
315Use
316.Ar address
317as the aliasing address.
318Either this or the
319.Fl interface
320option must be used (but not both),
321if the
322.Fl proxy_only
323option is not specified.
324The specified address is usually the address assigned to the
325.Dq public
326network interface.
327.Pp
328All data passing
329.Em out
330will be rewritten with a source address equal to
331.Ar address .
332All data coming
333.Em in
334will be checked to see if it matches any already-aliased outgoing
335connection.
336If it does, the packet is altered accordingly.
337If not, all
338.Fl redirect_port ,
339.Fl redirect_proto
340and
341.Fl redirect_address
342assignments are checked and actioned.
343If no other action can be made and if
344.Fl deny_incoming
345is not specified, the packet is delivered to the local machine
346using the rules specified in
347.Fl target_address
348option below.
349.It Fl t | target_address Ar address
350Set the target address.
351When an incoming packet not associated with any pre-existing link
352arrives at the host machine, it will be sent to the specified
353.Ar address .
354.Pp
355The target address may be set to
356.Ar 255.255.255.255 ,
357in which case all new incoming packets go to the alias address set by
358.Fl alias_address
359or
360.Fl interface .
361.Pp
362If this option is not used, or called with the argument
363.Ar 0.0.0.0 ,
364then all new incoming packets go to the address specified in
365the packet.
366This allows external machines to talk directly to internal machines if
367they can route packets to the machine in question.
368.It Fl interface | n Ar interface
369Use
370.Ar interface
371to determine the aliasing address.
372If there is a possibility that the IP number associated with
373.Ar interface
374may change, the
375.Fl dynamic
376option should also be used.
377If this option is not specified, the
378.Fl alias_address
379option must be used.
380.Pp
381The specified
382.Ar interface
383is usually the
384.Dq public
385(or
386.Dq external )
387network interface.
388.It Fl config | f Ar file
389Read configuration from
390.Ar file .
391A
392.Ar file
393should contain a list of options, one per line, in the same form
394as the long form of the above command line options.
395For example, the line
396.Pp
397.Dl alias_address 158.152.17.1
398.Pp
399would specify an alias address of 158.152.17.1.
400Options that do not take an argument are specified with an argument of
401.Ar yes
402or
403.Ar no
404in the configuration file.
405For example, the line
406.Pp
407.Dl log yes
408.Pp
409is synonymous with
410.Fl log .
411.Pp
412Trailing spaces and empty lines are ignored.
413A
414.Ql \&#
415sign will mark the rest of the line as a comment.
416.It Fl reverse
417This option makes
418.Nm
419reverse the way it handles
420.Dq incoming
421and
422.Dq outgoing
423packets, allowing it to operate on the
424.Dq internal
425network interface rather than the
426.Dq external
427one.
428.Pp
429This can be useful in some transparent proxying situations
430when outgoing traffic is redirected to the local machine
431and
432.Nm
433is running on the internal interface (it usually runs on the
434external interface).
435.It Fl proxy_only
436Force
437.Nm
438to perform transparent proxying only.
439Normal address translation is not performed.
440.It Fl proxy_rule Xo
441.Op Ar type encode_ip_hdr | encode_tcp_stream
442.Ar port xxxx
443.Ar server a.b.c.d:yyyy
444.Xc
445Enable transparent proxying.
446Outgoing TCP packets with the given port going through this
447host to any other host are redirected to the given server and port.
448Optionally, the original target address can be encoded into the packet.
449Use
450.Ar encode_ip_hdr
451to put this information into the IP option field or
452.Ar encode_tcp_stream
453to inject the data into the beginning of the TCP stream.
454.It Fl punch_fw Xo
455.Ar basenumber Ns : Ns Ar count
456.Xc
457This option directs
458.Nm
459to
460.Dq punch holes
461in an
462.Xr ipfirewall 4
463based firewall for FTP/IRC DCC connections.
464This is done dynamically by installing temporary firewall rules which
465allow a particular connection (and only that connection) to go through
466the firewall.
467The rules are removed once the corresponding connection terminates.
468.Pp
469A maximum of
470.Ar count
471rules starting from the rule number
472.Ar basenumber
473will be used for punching firewall holes.
474The range will be cleared for all rules on startup.
475.It Fl log_ipfw_denied
476Log when a packet cannot be re-injected because an
477.Xr ipfw 8
478rule blocks it.
479This is the default with
480.Fl verbose .
481.El
482.Sh RUNNING NATD
483The following steps are necessary before attempting to run
484.Nm :
485.Bl -enum
486.It
487Build a custom kernel with the following options:
488.Bd -literal -offset indent
489options IPFIREWALL
490options IPDIVERT
491.Ed
492.Pp
493Refer to the handbook for detailed instructions on building a custom
494kernel.
495.It
496Ensure that your machine is acting as a gateway.
497This can be done by specifying the line
498.Pp
499.Dl gateway_enable=YES
500.Pp
501in the
502.Pa /etc/rc.conf
503file or using the command
504.Pp
505.Dl "sysctl net.inet.ip.forwarding=1"
506.Pp
507.It
508If you use the
509.Fl interface
510option, make sure that your interface is already configured.
511If, for example, you wish to specify
512.Ql tun0
513as your
514.Ar interface ,
515and you are using
516.Xr ppp 8
517on that interface, you must make sure that you start
518.Nm ppp
519prior to starting
520.Nm .
521.El
522.Pp
523Running
524.Nm
525is fairly straight forward.
526The line
527.Pp
528.Dl natd -interface ed0
529.Pp
530should suffice in most cases (substituting the correct interface name).
531Please check
532.Xr rc.conf 5
533on how to configure it to be started automatically during boot.
534Once
535.Nm
536is running, you must ensure that traffic is diverted to
537.Nm :
538.Bl -enum
539.It
540You will need to adjust the
541.Pa /etc/rc.firewall
542script to taste.
543If you are not interested in having a firewall, the
544following lines will do:
545.Bd -literal -offset indent
546/sbin/ipfw -f flush
547/sbin/ipfw add divert natd all from any to any via ed0
548/sbin/ipfw add pass all from any to any
549.Ed
550.Pp
551The second line depends on your interface (change
552.Ql ed0
553as appropriate).
554.Pp
555You should be aware of the fact that, with these firewall settings,
556everyone on your local network can fake his source-address using your
557host as gateway.
558If there are other hosts on your local network, you are strongly
559encouraged to create firewall rules that only allow traffic to and
560from trusted hosts.
561.Pp
562If you specify real firewall rules, it is best to specify line 2 at
563the start of the script so that
564.Nm
565sees all packets before they are dropped by the firewall.
566.Pp
567After translation by
568.Nm ,
569packets re-enter the firewall at the rule number following the rule number
570that caused the diversion (not the next rule if there are several at the
571same number).
572.It
573Enable your firewall by setting
574.Pp
575.Dl firewall_enable=YES
576.Pp
577in
578.Pa /etc/rc.conf .
579This tells the system startup scripts to run the
580.Pa /etc/rc.firewall
581script.
582If you do not wish to reboot now, just run this by hand from the console.
583NEVER run this from a remote session unless you put it into the background.
584If you do, you will lock yourself out after the flush takes place, and
585execution of
586.Pa /etc/rc.firewall
587will stop at this point - blocking all accesses permanently.
588Running the script in the background should be enough to prevent this
589disaster.
590.El
591.Sh SEE ALSO
592.Xr libalias 3 ,
593.Xr divert 4 ,
594.Xr protocols 5 ,
595.Xr rc.conf 5 ,
596.Xr services 5 ,
597.Xr syslog.conf 5 ,
598.Xr ipfw 8 ,
599.Xr ppp 8
600.Sh AUTHORS
601This program is the result of the efforts of many people at different
602times:
603.Pp
604.An Archie Cobbs Aq archie@FreeBSD.org
605(divert sockets)
606.An Charles Mott Aq cm@linktel.net
607(packet aliasing)
608.An Eivind Eklund Aq perhaps@yes.no
609(IRC support & misc additions)
610.An Ari Suutari Aq suutari@iki.fi
611(natd)
612.An Dru Nelson Aq dnelson@redwoodsoft.com
613(early PPTP support)
614.An Brian Somers Aq brian@awfulhak.org
615(glue)
616.An Ruslan Ermilov Aq ru@FreeBSD.org
617(natd, packet aliasing, glue)
618