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