natd - Network Address Translation daemon
(If you need NAT on a PPP link, ppp(8) provides the -nat option that gives most of the functionality, and uses the same libalias(3) library.)
The utility normally runs in the background as a daemon. It is passed raw IP packets as they travel into and out of the machine, and will possibly change these before re-injecting them back into the IP packet stream.
It changes all packets destined for another host so that their source IP address is that of the current machine. For each packet changed in this manner, an internal table entry is created to record this fact. The source port number is also changed to indicate the table entry applying to the packet. Packets that are received with a target IP of the current host are checked against this internal table. If an entry is found, it is used to determine the correct target IP address and port to place in the packet.
The following command line options are available:
If this option is not used, then such a packet will be altered using the rules in -target_address below, and the entry will be made in the internal translation table.
Arguments targetIP , aliasIP and remoteIP can be given as IP addresses or as hostnames. The targetPORT , aliasPORT and remotePORT ranges need not be the same numerically, but must have the same size. When targetPORT , aliasPORT or remotePORT specifies a singular value (not a range), it can be given as a service name that is searched for in the services(5) database.
For example, the argument
tcp inside1:telnet 6666
means that incoming TCP packets destined for port 6666 on this machine will be sent to the telnet port on the inside1 machine.
tcp inside2:2300-2399 3300-3399
will redirect incoming connections on ports 3300-3399 to host inside2, ports 2300-2399. The mapping is 1:1 meaning port 3300 maps to 2300, 3301 maps to 2301, etc.
If publicIP is not specified, then the default aliasing address is used. If remoteIP is specified, then only packets coming from/to remoteIP will match the rule.
redirect_address 10.0.0.8 0.0.0.0
The above command would redirect all incoming traffic to machine 10.0.0.8.
If several address aliases specify the same public address as follows
redirect_address 192.168.0.2 public_addr redirect_address 192.168.0.3 public_addr redirect_address 192.168.0.4 public_addr
the incoming traffic will be directed to the last translated local address (192.168.0.4), but outgoing traffic from the first two addresses will still be aliased to appear from the specified public_addr
tcp www1:http,www2:http,www3:http www:http
means that incoming HTTP requests for host www will be transparently redirected to one of the www1, www2 or www3, where a host is selected simply on a round-robin basis, without regard to load on the net.
All data passing out will be rewritten with a source address equal to address All data coming in will be checked to see if it matches any already-aliased outgoing connection. If it does, the packet is altered accordingly. If not, all -redirect_port -redirect_proto and -redirect_address assignments are checked and actioned. If no other action can be made and if -deny_incoming is not specified, the packet is delivered to the local machine using the rules specified in -target_address option below.
The target address may be set to 255.255.255.255 in which case all new incoming packets go to the alias address set by -alias_address or -interface
If this option is not used, or called with the argument 0.0.0.0 then all new incoming packets go to the address specified in the packet. This allows external machines to talk directly to internal machines if they can route packets to the machine in question.
The specified interface is usually the ``public'' (or ``external'' network interface.
alias_address 158.152.17.1
would specify an alias address of 158.152.17.1. Options that do not take an argument are specified with an argument of yes or no in the configuration file. For example, the line
log yes
is synonymous with -log
Options can be divided to several sections. Each section applies to own instance. This ability allows to configure one process for several NAT instances. The first instance that always exists is a "default" instance. Each another instance should begin with
instance instance_name
At the next should be placed a configuration option. Example:
# default instance
port 8668
alias_address 158.152.17.1
# second instance
instance dsl1
port 8888
alias_address 192.168.0.1
Trailing spaces and empty lines are ignored. A `#' sign will mark the rest of the line as a comment.
This can be useful in some transparent proxying situations when outgoing traffic is redirected to the local machine and is running on the internal interface (it usually runs on the external interface).
A maximum of count rules starting from the rule number basenumber will be used for punching firewall holes. The range will be cleared for all rules on startup. This option has no effect when the kernel is in security level 3, see init(8) for more information.
options IPFIREWALL options IPDIVERT
Refer to the handbook for detailed instructions on building a custom kernel.
gateway_enable=YES
in the /etc/rc.conf file or using the command
"sysctl net.inet.ip.forwarding=1"
Running is fairly straight forward. The line
natd -interface ed0
should suffice in most cases (substituting the correct interface name). Please check rc.conf5 on how to configure it to be started automatically during boot. Once is running, you must ensure that traffic is diverted to :
/sbin/ipfw -f flush /sbin/ipfw add divert natd all from any to any via ed0 /sbin/ipfw add pass all from any to any
The second line depends on your interface (change `ed0' as appropriate).
You should be aware of the fact that, with these firewall settings, everyone on your local network can fake his source-address using your host as gateway. If there are other hosts on your local network, you are strongly encouraged to create firewall rules that only allow traffic to and from trusted hosts.
If you specify real firewall rules, it is best to specify line 2 at the start of the script so that sees all packets before they are dropped by the firewall.
After translation by , packets re-enter the firewall at the rule number following the rule number that caused the diversion (not the next rule if there are several at the same number).
firewall_enable=YES
in /etc/rc.conf This tells the system startup scripts to run the /etc/rc.firewall script. If you do not wish to reboot now, just run this by hand from the console. NEVER run this from a remote session unless you put it into the background. If you do, you will lock yourself out after the flush takes place, and execution of /etc/rc.firewall will stop at this point - blocking all accesses permanently. Running the script in the background should be enough to prevent this disaster.
net 1.2.3.0/24 1.2.3.1 ------------------ sis0 (router) (1.2.3.4) net 10.0.0.0/24 sis1 ------------------- 10.0.0.2 (10.0.0.1) net 2.3.4.0/24 2.3.4.1 ------------------ sis2 (router) (2.3.4.5)
Default route is out via `sis0'
Interior machine (10.0.0.2) is accessible on TCP port 122 through both exterior IPs, and outgoing connections choose a path randomly between `sis0' and `sis2'
The way this works is that natd.conf builds two instances of the aliasing engine.
In addition to these instances' private divert(4) sockets, a third socket called the ``globalport'' is created; packets sent to via this one will be matched against all instances and translated if an existing entry is found, and unchanged if no entry is found. The following lines are placed into /etc/natd.conf
log deny_incoming verbose instance default interface sis0 port 1000 redirect_port tcp 10.0.0.2:122 122 instance sis2 interface sis2 port 2000 redirect_port tcp 10.0.0.2:122 122 globalport 3000
And the following ipfw(8) rules are used:
ipfw -f flush ipfw add allow ip from any to any via sis1 ipfw add skipto 1000 ip from any to any in via sis0 ipfw add skipto 2000 ip from any to any out via sis0 ipfw add skipto 3000 ip from any to any in via sis2 ipfw add skipto 4000 ip from any to any out via sis2 ipfw add 1000 count ip from any to any ipfw add divert 1000 ip from any to any ipfw add allow ip from any to any ipfw add 2000 count ip from any to any ipfw add divert 3000 ip from any to any ipfw add allow ip from 1.2.3.4 to any ipfw add skipto 5000 ip from 2.3.4.5 to any ipfw add prob .5 skipto 4000 ip from any to any ipfw add divert 1000 ip from any to any ipfw add allow ip from any to any ipfw add 3000 count ip from any to any ipfw add divert 2000 ip from any to any ipfw add allow ip from any to any ipfw add 4000 count ip from any to any ipfw add divert 2000 ip from any to any ipfw add 5000 fwd 2.3.4.1 ip from 2.3.4.5 to not 2.3.4.0/24 ipfw add allow ip from any to any
Here the packet from internal network to Internet goes out via `sis0' (rule number 2000) and gets catched by the globalport socket (3000). After that, either a match is found in a translation table of one of the two instances, or the packet is passed to one of the two other divert(4) ports (1000 or 2000), with equal probability. This ensures that load balancing is done on a per-flow basis (i.e., packets from a single TCP connection always flow through the same interface). Translated packets with source IP of a non-default interface (`sis2' ) are forwarded to the appropriate router on that interface.
An Archie Cobbs Aq [email protected] (divert sockets) An Charles Mott Aq [email protected] (packet aliasing) An Eivind Eklund Aq [email protected] (IRC support & misc additions) An Ari Suutari Aq [email protected] (natd) An Dru Nelson Aq [email protected] (early PPTP support) An Brian Somers Aq [email protected] (glue) An Ruslan Ermilov Aq [email protected] (natd, packet aliasing, glue) An Poul-Henning Kamp Aq [email protected] (multiple instances)
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |