Table of Contents

Ubuntu - IPTables - About IP Tables

iptables diagram

A diagram showing the order of iptables traversal.

                                   netfilter hooks

                                  +-----------> local +-----------+
                                  |             process           |
                                  |                               |
                                  |                               |
                                  |                               |
                                  |                               v
  MANGLE            +-------------+--------+               +----------------------+    RAW
  FILTER            |                      |               |                      |    conntrack
  SECURITY          |        input         |               |     output           |    MANGLE
  SNAT              |                      |               |                      |    DNAT
                    +------+---------------+               +-------+--------------+    routing
                           ^                                       |
                           |                                       |                   FILTER
                           |                                       |                   SECURITY
                           |            +---------------------+    |         +-------------+
     +-----------+                      |                     |    +-------> |             |
+--> |pre routing+----  route    -----> |      forward        |              |post routing +---->
     |           |      lookup          |                     +------------> |             |
     +-----------+                      +---------------------+              +-------------+
     
     RAW                                       MANGLE                         MANGLE
     conntrack                                 FILTER                         SNAT
     MANGLE                                    SECURITY
     DNAT
     routing

NOTE:

  • This is actually the netfilter hooks, as iptables uses netfilter.

The flow:

  • Incoming packets destined for the local system: PREROUTING → INPUT.
  • Incoming packets destined to another host: PREROUTING → FORWARD → POSTROUTING.
  • Locally generated packets: OUTPUT → POSTROUTING.

iptables Tables

iptables tables represent distinct sets of rules, organized by area of concern, for evaluating packets.

TableDescription
FilterThe filter table filters packets.
Used to make decisions about whether to let a packet continue to its intended destination or to deny its request.
This table provides the bulk of functionality that people think of when discussing firewalls.
NATImplements network address translation rules.
As packets enter the network stack, rules in this table will determine whether and how to modify the source or destination addresses of the packet in order to impact the way that the packet and any response traffic are routed.
This is often used to route packets to networks when direct access is not possible.
MangleUsed to alter the IP headers of the packet in various ways.
For instance, you can adjust the TTL (Time to Live) value of a packet, either lengthening or shortening the number of valid network hops the packet can sustain.
Other IP headers can be altered in similar ways.
This table can also place an internal kernel “mark” on the packet for further processing in other tables and by other networking tools.
This mark does not touch the actual packet, but adds the mark to the kernels representation of the packet.
RawThe iptables firewall is stateful, meaning that packets are evaluated in regards to their relation to previous packets.
The connection tracking features built on top of the netfilter framework allow iptables to view packets as part of an ongoing connection or session instead of as a stream of discrete, unrelated packets.
The connection tracking logic is usually applied very soon after the packet hits the network interface.
The raw table has a very narrowly defined function - Its only purpose is to provide a mechanism for marking packets in order to opt-out of connection tracking.
SecurityUsed to set internal SELinux security context marks on packets, which will affect how SELinux or other systems that can interpret SELinux security contexts handle the packets.
These marks can be applied on a per-packet or per-connection basis.

References

https://docs.kernel.org/networking/skbuff.html