xref: /freebsd/sbin/natd/natd.8 (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1.\" manual page [] for natd 1.4
2.\" $FreeBSD$
3.Dd 15 April 1997
4.Os FreeBSD
5.Dt NATD 8
6.Sh NAME
7.Nm natd
8.Nd
9Network Address Translation Daemon
10.Sh SYNOPSIS
11.Nm
12.Op Fl ldsmvu
13.Op Fl dynamic
14.Op Fl i Ar inport
15.Op Fl o Ar outport
16.Op Fl p Ar port
17.Op Fl a Ar address
18.Op Fl n Ar interface
19.Op Fl f Ar configfile
20
21.Nm
22.Op Fl log
23.Op Fl deny_incoming
24.Op Fl log_denied
25.Op Fl use_sockets
26.Op Fl same_ports
27.Op Fl verbose
28.Op Fl log_facility Ar facility_name
29.Op Fl unregistered_only
30.Op Fl dynamic
31.Op Fl inport Ar inport
32.Op Fl outport Ar outport
33.Op Fl port Ar port
34.Op Fl alias_address Ar address
35.Op Fl interface Ar interface
36.Op Fl config Ar configfile
37.Op Fl redirect_port Ar linkspec
38.Op Fl redirect_address Ar linkspec
39.Op Fl reverse
40.Op Fl proxy_only
41.Op Fl proxy_rule Ar proxyspec
42.Op Fl pptpalias Ar localIP
43
44.Sh DESCRIPTION
45This program provides a Network Address Translation facility for use
46with
47.Xr divert 4
48sockets under FreeBSD.  It is intended for use with NICs - if you want
49to do NAT on a PPP link, use the -nat switch to
50.Xr ppp 8 .
51
52.Pp
53.Nm Natd
54normally runs in the background as a daemon.  It is passed raw IP packets
55as they travel into and out of the machine, and will possibly change these
56before re-injecting them back into the IP packet stream.
57
58.Pp
59.Nm Natd
60changes all packets destined for another host so that their source
61IP number is that of the current machine.  For each packet changed
62in this manner, an internal table entry is created to record this
63fact.  The source port number is also changed to indicate the
64table entry applying to the packet.  Packets that are received with
65a target IP of the current host are checked against this internal
66table.  If an entry is found, it is used to determine the correct
67target IP number and port to place in the packet.
68
69.Pp
70The following command line options are available.
71.Bl -tag -width Fl
72
73.It Fl log | l
74Log various aliasing statistics and information to the file
75.Pa /var/log/alias.log .
76This file is truncated each time natd is started.
77
78.It Fl deny_incoming | d
79Reject packets destined for the current IP number that have no entry
80in the internal translation table.
81
82.It Fl log_denied
83Log denied incoming packets via syslog (see also log_facility)
84
85.It Fl log_facility Ar facility_name
86Use specified log facility when logging information via syslog.
87Facility names are as in
88.Xr syslog.conf 5
89
90.It Fl use_sockets | s
91Allocate a
92.Xr socket 2
93in order to establish an FTP data or IRC DCC send connection.  This
94option uses more system resources, but guarantees successful connections
95when port numbers conflict.
96
97.It Fl same_ports | m
98Try to keep the same port number when altering outgoing packets.
99With this option, protocols such as RPC will have a better chance
100of working.  If it is not possible to maintain the port number, it
101will be silently changed as per normal.
102
103.It Fl verbose | v
104Don't call
105.Xr fork 2
106or
107.Xr daemon 3
108on startup.  Instead, stay attached to the controling terminal and
109display all packet alterations to the standard output.  This option
110should only be used for debugging purposes.
111
112.It Fl unregistered_only | u
113Only alter outgoing packets with an unregistered source address.
114According to rfc 1918, unregistered source addresses are 10.0.0.0/8,
115172.16.0.0/12 and 192.168.0.0/16.
116
117.It Fl redirect_port Ar proto targetIP:targetPORT[-targetPORT] [aliasIP:]aliasPORT[-aliasPORT] [remoteIP[:remotePORT[-remotePORT]]]
118Redirect incoming connections arriving to given port(s) to another host
119and port(s).
120Proto is either tcp or udp, targetIP is the desired target IP
121number, targetPORT is the desired target PORT number or range, aliasPORT
122is the requested PORT number or range, and aliasIP is the aliasing address.
123RemoteIP and remotePORT can be used to specify the connection
124more accurately if necessary.
125The targetPORT range and aliasPORT range need not be the same numerically,
126but must have the same size.
127If remotePORT is not specified, it is assumed to be all ports.
128If remotePORT is specified, it must match the size of targetPORT, or be 0
129(all ports).
130For example, the argument
131
132.Dl Ar tcp inside1:telnet 6666
133
134means that incoming tcp packets destined for port 6666 on this machine will
135be sent to the telnet port on the inside1 machine.
136
137.Dl Ar tcp inside2:2300-2399 3300-3399
138
139will redirect incoming connections on ports 3300-3399 to host
140inside2, ports 2300-2399.
141The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc.
142
143.It Fl redirect_address Ar localIP publicIP
144Redirect traffic for public IP address to a machine on the local
145network.
146This function is known as "static NAT". Normally static NAT
147is useful if your ISP has allocated a small block of IP addresses to you,
148but it can even be used in the case of single address:
149
150  redirect_address 10.0.0.8 0.0.0.0
151
152The above command would redirect all incoming traffic
153to machine 10.0.0.8.
154
155If several address aliases specify the same public address
156as follows
157
158  redirect_address 192.168.0.2 public_addr
159  redirect_address 192.168.0.3 public_addr
160  redirect_address 192.168.0.4 public_addr
161
162the incoming traffic will be directed to the last
163translated local address (192.168.0.4), but outgoing
164traffic to the first two addresses will still be aliased
165to specified public address.
166.It Fl redirect_port Ar proto Xo
167.Ar targetIP Ns : Ns Xo
168.Ar targetPORT Ns Oo , Ns
169.Ar targetIP Ns : Ns Xo
170.Ar targetPORT Ns Oo , Ns
171.Ar ...
172.Oc Oc
173.Xc
174.Xc
175.Op Ar aliasIP Ns : Ns Xo
176.Ar aliasPORT
177.Xc
178.Oo Ar remoteIP Ns
179.Op : Ns Ar remotePORT
180.Oc
181.Xc
182.It Fl redirect_address Xo
183.Ar localIP Ns Oo , Ns
184.Ar localIP Ns Oo , Ns
185.Ar ...
186.Oc Oc
187.Ar publicIP
188.Xc
189These forms of
190.Fl redirect_port
191and
192.Fl redirect_address
193are used to transparently offload network load on a single server and
194distribute the load across a pool of servers.
195This function is known as
196.Em LSNAT
197(RFC 2391).
198For example, the argument
199.Pp
200.Dl Ar tcp www1:http,www2:http,www3:http www:http
201.Pp
202means that incoming HTTP requests for host www will be transparently
203redirected to one of the www1, www2 or www3, where a host is selected
204simply on a round-robin basis, without regard to load on the net.
205.It Fl dynamic
206If the
207.Fl n
208or
209.Fl interface
210option is used,
211.Nm
212will monitor the routing socket for alterations to the
213.Ar interface
214passed.  If the interfaces IP number is changed,
215.Nm
216will dynamically alter its concept of the alias address.
217
218.It Fl i | inport Ar inport
219Read from and write to
220.Ar inport ,
221treating all packets as packets coming into the machine.
222
223.It Fl o | outport Ar outport
224Read from and write to
225.Ar outport ,
226treating all packets as packets going out of the machine.
227
228.It Fl p | port Ar port
229Read from and write to
230.Ar port ,
231distinguishing packets as incoming our outgoing using the rules specified in
232.Xr divert 4 .
233If
234.Ar port
235is not numeric, it is searched for in the
236.Pa /etc/services
237database using the
238.Xr getservbyname 3
239function.  If this flag is not specified, the divert port named natd will
240be used as a default.  An example entry in the
241.Pa /etc/services
242database would be:
243
244  natd   8668/divert  # Network Address Translation socket
245
246Refer to
247.Xr services 5
248for further details.
249
250.It Fl a | alias_address Ar address
251Use
252.Ar address
253as the alias address.  If this option is not specified, the
254.Fl n
255or
256.Fl interface
257option must be used.  The specified address should be the address assigned
258to the public network interface.
259.Pp
260All data passing out through this addresses interface will be rewritten
261with a source address equal to
262.Ar address .
263All data arriving at the interface from outside will be checked to
264see if it matches any already-aliased outgoing connection.  If it does,
265the packet is altered accordingly.  If not, all
266.Fl redirect_port
267and
268.Fl redirect_address
269assignments are checked and actioned.  If no other action can be made,
270and if
271.Fl deny_incoming
272is not specified, the packet is delivered to the local machine and port
273as specified in the packet.
274
275.It Fl n | interface Ar interface
276Use
277.Ar interface
278to determine the alias address.  If there is a possibility that the
279IP number associated with
280.Ar interface
281may change, the
282.Fl dynamic
283flag should also be used.  If this option is not specified, the
284.Fl a
285or
286.Fl alias_address
287flag must be used.
288.Pp
289The specified
290.Ar interface
291must be the public network interface.
292.It Fl f | config Ar configfile
293Read configuration from
294.Ar configfile .
295.Ar Configfile
296contains a list of options, one per line in the same form as the
297long form of the above command line flags.  For example, the line
298
299  alias_address 158.152.17.1
300
301would specify an alias address of 158.152.17.1.  Options that don't
302take an argument are specified with an option of
303.Ar yes
304or
305.Ar no
306in the configuration file.  For example, the line
307
308  log yes
309
310is synonomous with
311.Fl log .
312.Pp
313Trailing spaces and empty lines are ignored.
314A
315.Ql \&#
316sign will mark the rest of the line as a comment.
317
318.It Fl reverse
319Reverse operation of natd.
320This can be useful in some
321transparent proxying situations when outgoing traffic
322is redirected to the local machine and natd is running on the
323incoming interface (it usually runs on the outgoing interface).
324
325.It Fl proxy_only
326Force natd to perform transparent proxying
327only.
328Normal address translation is not performed.
329
330.It Fl proxy_rule Ar [type encode_ip_hdr|encode_tcp_stream] port xxxx server a.b.c.d:yyyy
331Enable transparent proxying.
332Packets with the given port going through this
333host to any other host are redirected to the given server and port.
334Optionally, the original target address can be encoded into the packet.
335Use
336.Dq encode_ip_hdr
337to put this information into the IP option field or
338.Dq encode_tcp_stream
339to inject the data into the beginning of the TCP stream.
340
341.It Fl pptpalias Ar localIP
342Allow PPTP packets to go to the defined localIP address.
343PPTP is a VPN or secure
344IP tunneling technology being developed primarily by Microsoft.
345For its encrypted traffic,
346it uses an old IP encapsulation protocol called GRE (47).
347This natd option will translate any traffic of this protocol to a
348single, specified IP address.
349This would allow either one client or one server
350to be serviced with natd.
351If you are setting up a server, don't forget to allow the TCP traffic
352for the PPTP setup.
353For a client or server,
354you must allow GRE (protocol 47) if you have firewall lists active.
355
356.El
357
358.Sh RUNNING NATD
359The following steps are necessary before attempting to run
360.Nm natd :
361
362.Bl -enum
363.It
364Get FreeBSD version 2.2 or higher.  Versions before this do not support
365.Xr divert 4
366sockets.
367
368.It
369Build a custom kernel with the following options:
370
371  options IPFIREWALL
372  options IPDIVERT
373
374Refer to the handbook for detailed instructions on building a custom
375kernel.
376
377.It
378Ensure that your machine is acting as a gateway.  This can be done by
379specifying the line
380
381  gateway_enable=YES
382
383in
384.Pa /etc/rc.conf ,
385or using the command
386
387  sysctl -w net.inet.ip.forwarding=1
388
389.It
390If you wish to use the
391.Fl n
392or
393.Fl interface
394flags, make sure that your interface is already configured.  If, for
395example, you wish to specify tun0 as your
396.Ar interface ,
397and you're using
398.Xr ppp 8
399on that interface, you must make sure that you start
400.Nm ppp
401prior to starting
402.Nm natd .
403
404.It
405Create an entry in
406.Pa /etc/services :
407
408  natd          8668/divert  # Network Address Translation socket
409
410This gives a default for the
411.Fl p
412or
413.Fl port
414flag.
415
416.El
417.Pp
418Running
419.Nm
420is fairly straight forward.  The line
421
422  natd -interface ed0
423
424should suffice in most cases (substituting the correct interface name).  Once
425.Nm
426is running, you must ensure that traffic is diverted to natd:
427
428.Bl -enum
429.It
430You will need to adjust the
431.Pa /etc/rc.firewall
432script to taste.  If you're not interested in having a firewall, the
433following lines will do:
434
435  /sbin/ipfw -f flush
436  /sbin/ipfw add divert natd all from any to any via ed0
437  /sbin/ipfw add pass all from any to any
438
439The second line depends on your interface (change ed0 as appropriate)
440and assumes that you've updated
441.Pa /etc/services
442with the natd entry as above.  If you specify real firewall rules, it's
443best to specify line 2 at the start of the script so that
444.Nm
445sees all packets before they are dropped by the firewall.
446.Pp
447After translation by
448.Nm natd ,
449packets re-enter the firewall at the rule number following the rule number
450that caused the diversion (not the next rule if there are several at the
451same number).
452
453.It
454Enable your firewall by setting
455
456  firewall_enable=YES
457
458in
459.Pa /etc/rc.conf .
460This tells the system startup scripts to run the
461.Pa /etc/rc.firewall
462script.  If you don't wish to reboot now, just run this by hand from the
463console.  NEVER run this from a virtual session unless you put it into
464the background.  If you do, you'll lock yourself out after the flush
465takes place, and execution of
466.Pa /etc/rc.firewall
467will stop at this point - blocking all accesses permanently.  Running
468the script in the background should be enough to prevent this disaster.
469
470.El
471
472.Sh SEE ALSO
473.Xr socket 2 ,
474.Xr getservbyname 3 ,
475.Xr divert 4 ,
476.Xr services 5 ,
477.Xr ipfw 8
478
479.Sh AUTHORS
480This program is the result of the efforts of many people at different
481times:
482
483.An Archie Cobbs Aq archie@whistle.com
484(divert sockets)
485.An Charles Mott Aq cmott@scientech.com
486(packet aliasing)
487.An Eivind Eklund Aq perhaps@yes.no
488(IRC support & misc additions)
489.An Ari Suutari Aq suutari@iki.fi
490(natd)
491.An Dru Nelson Aq dnelson@redwoodsoft.com
492(PPTP support)
493.An Brian Somers Aq brian@awfulhak.org
494(glue)
495