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