This article describes how to use iptbales to implement the powerful NAT Function under linux2.4. For more information about the syntax of iptables, see the article "using iptales to implement a firewall with excessive packet forwarding rate. What needs to be affirmed is that this article is definitely not a simple repetition of the NAT-HOWTO or the Chinese version, in the whole narrative process, the author is trying to express his understanding, his own thoughts in his own language.
I. Overview
1. What is NAT
In the traditional standard TCP/IP communication process, all routers only act as a man-in-the-middle, that is, storage forwarding. The Router does not modify the forwarded packets, more specifically, the vro will not modify the forwarded packets except for replacing the source MAC address with its own MAC address. Network Address Translation (NAT) is an operation that modifies the source IP Address, destination IP Address, source port, and destination port of a data packet for special purposes.
2. Why NAT?
Let's see under what circumstances we need to perform NAT.
Assume that an ISP provides Internet access services in the campus. To facilitate management, the IP addresses assigned by the ISP to the campus users are all pseudo IP addresses, but some users require that their WWW servers be set up to publish information, in this case, we can use NAT to provide such services. We can bind multiple valid IP addresses to the external network card of the firewall, and then forward packets sent to one of the IP addresses to a user's WWW server through NAT technology, then, the response packet of the internal WWW server is disguised as the package sent by the legal IP address.
For example, Internet cafes that use dial-up Internet access, because there is only one valid IP address, some means must be used to allow other machines to access the Internet. Generally, the proxy server is used, but the proxy server, in particular, the application layer proxy server can only support limited protocols. If a new service comes out after a while, you can only wait for the proxy server to support the upgraded version of the new application. If NAT is used to solve this problem,
NAT not only achieves high access speeds, but also supports any new services or applications seamlessly.
Another aspect of the application is redirection, that is, when a packet is received, the packet is not forwarded, but redirected to an application on the system. The most common application is to use it with squid as a transparent proxy. It caches http traffic and provides seamless access to the Internet.
3. NAT type
In the NAT-HOWTO of linux2.4, the author divides NAT into two types from the perspective of principle, that is, source NAT (SNAT) and destination NAT (DNAT), as the name suggests, SNAT is to change the source address of the forwarded data packet, and DNAT is to change the destination address of the forwarded data packet.
Ii. Principles
As we mentioned in the article "using iptales to implement a firewall with excessive packaging considerations", netfilter is a general architecture at the core of Linux and provides a series of "tables" (tables ), each table is composed of several "chains", and each chain can contain one or more rules. The default table is "filter ". However, when using NAT, the table we use is no longer a "filter", but a "nat" table, therefore, we must use the "-t nat" option to explicitly specify this. Because the default table is "filter", we do not need to explicitly specify "-t filter" when using the filter function ".
Like the filter table, the nat table also has three default "chains" (chains). These three chains are also rule containers. They are:
PREROUTING: You can define the rules for destination NAT here, because the router only checks the destination IP address of the data packet during routing, so in order to make the data packet can be correctly routed, we must perform destination NAT before routing;
POSTROUTING: You can define the source NAT rules here. The system will execute the rules in the chain after determining the route of the data packet.
OUTPUT: defines the destination NAT rule for locally generated packets.
Iii. Operation syntax
As mentioned above, when using the iptables NAT Function, we must use the "-t nat" display in each rule to use the nat table. Use the following options:
1. Operations on Rules
Add (append) A new rule to the end of A chain (-.
Insert a new rule (-I) at a location in the chain, usually at the beginning.
Replace a rule (-R) at a certain position in the chain ).
Delete a rule (-D) at a location in the chain ).
Delete the first rule (-D) in the chain ).
2. Specify the source address and Destination Address
Use -- source/-- src/-s to specify the source address (here/Represents or means, the same below), and use -- destination/-- dst/-s to specify the destination address. You can use the following four methods to specify an IP Address:
A. Use a complete domain name, such as "www.linuxaid.com.cn ";
B. Use an IP address, such as "192.168.1.1 ";
C. Use x. x/x. x to specify a network address, such as "192.168.1.0/255.255.255.0 ";
D. use x. x. x. x/x specifies a network address. For example, "192.168.1.0/24" indicates the number of valid digits of the subnet mask, which is usually used in UNIX environments.
The default subnet mask number is 32, that is, specifying 192.168.1.1 is equivalent to 192.168.1.1/32.
3. Specify Network Interfaces
You can use -- in-interface/-I or -- out-interface/-o to specify network interfaces. From the principle of NAT, we can see that for the PREROUTING chain, we can only use-I to specify the incoming network interface; For POSTROUTING and OUTPUT, we can only use the network interface specified by-o.
4. Specify the protocol and Port
You can use the -- protocol/-p option to specify the protocol. For udp and tcp Protocols, you can also specify the port using -- source-port/-- sport and -- destination-port/-- dport.
4. Preparations
1. Compile the kernel and select the following options during compilation. For details, see the article "using iptales to implement a firewall with excessive considerations:
Full NAT
MASQUERADE target support
REDIRECT target support
2. To use a NAT table, you must first load the relevant modules:
Modprobe ip_tables
Modprobe ip_nat_ftp
The iptable_nat module is automatically loaded at runtime.
V. Use instances
1. Source NAT (SNAT)
For example, change the source IP address of all data packets from 192.168.1.0/24 to 1.2.3.4:
Iptables-t nat-a postrouting-s 192.168.1.0/24-o eth0-j SNAT -- to 1.2.3.4
It should be noted that the system does not perform SNAT until the packets are sent out during routing and overconsideration.
There is a special case of SNAT which ip spoofing is called Masquerading. We recommend that you use it when using dial-up Internet access, or use it when the valid ip address is not fixed. For example
# Iptables-t nat-a postrouting-o ppp0-j MASQUERADE
We can see that there is no need to explicitly specify the source IP address and other information at this time.
2. Destination SNAT (DNAT)
For example, change the destination IP address of all data packets from 192.168.1.0/24 to 1.2.3.4:
Iptables-t nat-a prerouting-s 192.168.1.0/24-I eth1-j DNAT -- to 1.2.3.4
Note that the system performs DNAT before routing and excessive consideration.
There is a special situation of DNAT, that is, the so-called Redirection, which is equivalent to changing the destination IP address of the qualified data packet to the IP address of the network interface when the data packet enters the system. It is usually used to form a transparent proxy with squid configuration. Assuming that the listening port of squid is 3128, we can use the following statement to import data from 192.168.1.0/24, the packet destined for port 80 is redirected to the squid listener.
Port:
Iptables-t nat-a prerouting-I eth1-p tcp-s 192.168.1.0/24 -- dport 80
-J REDIRECT -- to-port 3128
6. Comprehensive examples
1. Use dialing to drive LAN Access
Small enterprises and Internet cafes use dial-up networks to access the Internet. Usually, proxies are used. However, considering costs and protocol support, we recommend that you use ip spoofing to enable Internet access in the area network.
After the kernel is successfully upgraded, install iptables and execute the following script:
# Load related modules
Modprobe ip_tables
Modprobe ip_nat_ftp
# Mask ip addresses
Iptables-t nat-a postrouting-o ppp0-j MASQUERADE
2. ip ing
Assume that an ISP provides Internet access to the campus. To facilitate management, the IP addresses assigned by the ISP to the campus users are all pseudo IP addresses, but some users require that their WWW servers be set up to publish information. We can bind multiple valid ip addresses to the external network card of the firewall, and then forward packets sent to one of the ip addresses to the internal WWW server of a user through ip ing, then, the response packet of the internal WWW server is disguised as the package sent by the legal IP address.
Let's assume the following scenarios:
The ip address assigned by this ISP to A www server is:
Pseudo ip: 192.168.1.100
Real ip: 202.110.123.100
The ip address assigned by this ISP to B's www server is:
Pseudo ip: 192.168.1.200
Real ip: 202.110.123.200
The IP addresses of the linux firewall are:
Intranet interface eth1: 192.168.1.1
Internet interface eth0: 202.110.123.1
Then, we bind the real ip addresses allocated to units A and B to the firewall's Internet interface and run the following command as root:
Ifconfig eth0 add 202.110.123.100 netmask 255.255.255.0
Ifconfig eth0 add 202.110.123.200 netmask 255.255.255.0
After the kernel is successfully upgraded, install iptables and execute the following script:
# Load related modules
Modprobe ip_tables
Modprobe ip_nat_ftp
First, perform the destination NAT (DNAT) on all packets received by the firewall with the destination ip address 202.110.123.100 and 202.110.123.200 ):
Iptables-a prerouting-I eth0-d 202.110.123.100-j DNAT -- to 192.168.1.100
Iptables-a prerouting-I eth0-d 202.110.123.200-j DNAT -- to 192.168.1.200
Next, perform source NAT (SNAT) on the packets received by the firewall with the source IP addresses 192.168.1.100 and 192.168.1.200 ):
Iptables-a postrouting-o eth0-s 192.168.1.100-j SNAT -- to 202.110.123.100
Iptables-a postrouting-o eth0-s 192.168.1.200-j SNAT -- to 202.110.123.200
In this way, all data packets whose destination ip addresses are 202.110.123.100 and 202.110.123.200 will be forwarded to 192.168.1.100 and 192.168.1.200 respectively; all data packets from 192.168.1.100 and 192.168.1.200 are disguised as ip ing from 202.110.123.100 and 202.110.123.200.