/usr/share/doc/iptables/html
NameSizeModeActions
NAT-HOWTO-1.html11520644editdlrm
NAT-HOWTO-2.html43460644editdlrm
NAT-HOWTO-3.html14440644editdlrm
NAT-HOWTO-4.html44000644editdlrm
NAT-HOWTO-5.html51390644editdlrm
NAT-HOWTO-6.html83970644editdlrm
NAT-HOWTO-7.html13240644editdlrm
NAT-HOWTO-8.html11360644editdlrm
NAT-HOWTO-9.html20910644editdlrm
NAT-HOWTO-10.html25250644editdlrm
NAT-HOWTO-11.html9100644editdlrm
NAT-HOWTO.html26660644editdlrm
netfilter-extensions-HOWTO-1.html21020644editdlrm
netfilter-extensions-HOWTO-2.html78070644editdlrm
netfilter-extensions-HOWTO-3.html289690644editdlrm
netfilter-extensions-HOWTO-4.html125480644editdlrm
netfilter-extensions-HOWTO-5.html70410644editdlrm
netfilter-extensions-HOWTO-6.html112420644editdlrm
netfilter-extensions-HOWTO-7.html21880644editdlrm
netfilter-extensions-HOWTO-8.html10210644editdlrm
netfilter-extensions-HOWTO-9.html22220644editdlrm
netfilter-extensions-HOWTO.html72670644editdlrm
netfilter-hacking-HOWTO-1.html74740644editdlrm
netfilter-hacking-HOWTO-2.html17180644editdlrm
netfilter-hacking-HOWTO-3.html80380644editdlrm
netfilter-hacking-HOWTO-4.html533380644editdlrm
netfilter-hacking-HOWTO-5.html10560644editdlrm
netfilter-hacking-HOWTO-6.html30280644editdlrm
netfilter-hacking-HOWTO-7.html92880644editdlrm
netfilter-hacking-HOWTO-8.html52340644editdlrm
netfilter-hacking-HOWTO-9.html8350644editdlrm
netfilter-hacking-HOWTO.html38110644editdlrm
packet-filtering-HOWTO-1.html18910644editdlrm
packet-filtering-HOWTO-2.html15930644editdlrm
packet-filtering-HOWTO-3.html57750644editdlrm
packet-filtering-HOWTO-4.html21890644editdlrm
packet-filtering-HOWTO-5.html16440644editdlrm
packet-filtering-HOWTO-6.html35050644editdlrm
packet-filtering-HOWTO-7.html356160644editdlrm
packet-filtering-HOWTO-8.html14570644editdlrm
packet-filtering-HOWTO-9.html23330644editdlrm
packet-filtering-HOWTO-10.html25750644editdlrm
packet-filtering-HOWTO-11.html37420644editdlrm
packet-filtering-HOWTO.html28310644editdlrm
Edit: /usr/share/doc/iptables/html/NAT-HOWTO-5.html (5139B)
Linux 2.4 NAT HOWTO: Controlling What To NAT Next Previous Contents

5. Controlling What To NAT

You need to create NAT rules which tell the kernel what connections to change, and how to change them. To do this, we use the very versatile iptables tool, and tell it to alter the NAT table by specifying the `-t nat' option.

The table of NAT rules contains three lists called `chains': each rule is examined in order until one matches. The two chains are called PREROUTING (for Destination NAT, as packets first come in), and POSTROUTING (for Source NAT, as packets leave). The third (OUTPUT) will be ignored here.

The following diagram would illustrate it quite well if I had any artistic talent:

      _____                                     _____
     /     \                                   /     \
   PREROUTING -->[Routing ]----------------->POSTROUTING----->
     \D-NAT/     [Decision]                    \S-NAT/
                     |                            ^
                     |                            |
                     |                            |
                     |                            |
                     |                            |
                     |                            |
                     |                            |
                     --------> Local Process ------

At each of the points above, when a packet passes we look up what connection it is associated with. If it's a new connection, we look up the corresponding chain in the NAT table to see what to do with it. The answer it gives will apply to all future packets on that connection.

5.1 Simple Selection using iptables

iptables takes a number of standard options as listed below. All the double-dash options can be abbreviated, as long as iptables can still tell them apart from the other possible options. If your kernel has iptables support as a module, you'll need to load the ip_tables.o module first: `insmod ip_tables'.

The most important option here is the table selection option, `-t'. For all NAT operations, you will want to use `-t nat' for the NAT table. The second most important option to use is `-A' to append a new rule at the end of the chain (e.g. `-A POSTROUTING'), or `-I' to insert one at the beginning (e.g. `-I PREROUTING').

You can specify the source (`-s' or `--source') and destination (`-d' or `--destination') of the packets you want to NAT. These options can be followed by a single IP address (e.g. 192.168.1.1), a name (e.g. www.gnumonks.org), or a network address (e.g. 192.168.1.0/24 or 192.168.1.0/255.255.255.0).

You can specify the incoming (`-i' or `--in-interface') or outgoing (`-o' or `--out-interface') interface to match, but which you can specify depends on which chain you are putting the rule into: at PREROUTING you can only select incoming interface, and at POSTROUTING you can only select outgoing interface. If you use the wrong one, iptables will give an error.

5.2 Finer Points Of Selecting What Packets To Mangle

I said above that you can specify a source and destination address. If you omit the source address option, then any source address will do. If you omit the destination address option, then any destination address will do.

You can also indicate a specific protocol (`-p' or `--protocol'), such as TCP or UDP; only packets of this protocol will match the rule. The main reason for doing this is that specifying a protocol of tcp or udp then allows extra options: specifically the `--source-port' and `--destination-port' options (abbreviated as `--sport' and `--dport').

These options allow you to specify that only packets with a certain source and destination port will match the rule. This is useful for redirecting web requests (TCP port 80 or 8080) and leaving other packets alone.

These options must follow the `-p' option (which has a side-effect of loading the shared library extension for that protocol). You can use port numbers, or a name from the /etc/services file.

All the different qualities you can select a packet by are detailed in painful detail in the manual page (man iptables).


Next Previous Contents