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