Module 0208: IPTABLES, the basics

Tak Auyeung, Ph.D.

January 9, 2010

1 About this module

2 What is iptables

Iptables is a component of the Linux kernel (when it is compiled in, as in most distributions) that controls the flow of network packets.

3 What do you do with iptables?

“iptables” is a versatile mechanism that is suitable for many purposes. For example, it can be used as a software “firewall” to stop unwanted traffic from reaching applications. However, it can also be used to implement network address transition (NAT) and other useful networking mechanism.

4 Iptable rules

We’ll start with a construct that is neither at the top level nor the bottom level. A “rule” is essentially a conditional action. This means that a rule specifies a condition. If the condition is met, then the action takes place.

In the context of “iptables”, a rule may specify something like “if a connection is attempted from 10.0.2.200 to port 80 of 192.168.3.13, drop it”.

5 Iptable matches

A “match” is essentiall the condition of a rule. Iptables supports a variety of matches. Here is an example:

This is merely a small sample of all the possible matches of iptables.

6 Iptables targets

A target of a rule is the “action” of the rule, it specifies what to do when the condition (match) of a rule is met.

The following is a list of common targets:

7 Iptable chains

A chain consists one or more rules. Rules of chain are evaluated sequentially until one matches (the condition is met), then the associated target executes.

An exception is when a rule specifies no target, but rather a --jump action. This causes the the rule to jump to another chain (usually user defined) and start evaluating rules in that chain. Rules in the chain that is --jumped into can specify a RETURN target to continue evaluation in the original chain.

8 Iptables table

Each table is a container of chains. There are four tables: filter, nat, raw and mangle. Of these, the filter table is the easiest to understand. The filter table chains chains of rules that specifies what goes through and what gets blocked, mostly using ACCEPT and REJECT targets.

The filter table has two important chains. The INPUT chain is responsible to control the filtering of incoming packets, whereas the OUTPUT chain is responsible to control the filtering of outgoing packets.

The other three tables are a little more complicated, and they are out of the scope of this module.

9 More advanced iptables

As mentioned earlier, iptables is capable of specifying complex network “tricks”. For instance, iptables can be used to implement network address translation. However, it can also be used to perform surgical operations, like altering the TTL (time-to-live) field of packets. It can also set the TOS (type-of-service) field of an IP packet header.