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