PREROUTING
- order
- 1
- table
- raw
- job
- Mark packets to skip connection tracking
A lesson to click through: a map to follow, a table to look things up in, cards to test yourself.
Build an interactive lesson: a map the reader follows and drives, a table to look things up in, and cards to test themselves.
Paths through a diagram, move lines that let the reader drive a packet, a typed data block shown again as a board, flashcards, line rules for a kicker, an opening line and takeaways.
view: hooks as board by chain to view: hooks as board by table, and the lanes become the four tables.Every packet passes the same few checkpoints in the kernel. Press a journey under the map and watch one travel, or drive one yourself with the buttons.
The five checkpoints are called chains, and each one consults a few tables of rules in a set order. iptables and nftables rules are entries in those tables, so knowing the route tells you where a rule has to go. There are three journeys:
The arrows beside the journeys walk a packet one table at a time. To drive one yourself, use the buttons under the map: start a packet, choose where it is bound, and see what it meets. The trail under the buttons is its route, and Reset sends the next packet.
A packet meets either INPUT or FORWARD, never both. That one fact settles most "why is my rule not matching?" questions.
Each table has one job. Not every chain consults every table, and the order within a chain is fixed. Sort or filter the list, or switch it to cards.
| chain | order | table | job |
|---|---|---|---|
| PREROUTING | 1 | raw | Mark packets to skip connection tracking |
| PREROUTING | 2 | mangle | Change packet headers (TTL and marks) |
| PREROUTING | 3 | nat | Change the destination (DNAT and port forwarding) |
| INPUT | 1 | mangle | Change headers of packets for this machine |
| INPUT | 2 | filter | Accept or drop packets for this machine |
| FORWARD | 1 | mangle | Change headers of packets passing through |
| FORWARD | 2 | filter | Accept or drop packets passing through |
| OUTPUT | 1 | raw | Mark packets this machine sends to skip tracking |
| OUTPUT | 2 | mangle | Change headers of packets this machine sends |
| OUTPUT | 3 | nat | Change the destination of packets this machine sends |
| OUTPUT | 4 | filter | Accept or drop packets this machine sends |
| POSTROUTING | 1 | mangle | Last change to headers before leaving |
| POSTROUTING | 2 | nat | Change the source (SNAT and masquerade) |
The same rows as a board, one lane per chain:
PREROUTING 3
PREROUTING
PREROUTING
PREROUTING
INPUT 2
INPUT
INPUT
FORWARD 2
FORWARD
FORWARD
OUTPUT 4
OUTPUT
OUTPUT
OUTPUT
OUTPUT
POSTROUTING 2
POSTROUTING
POSTROUTING
To block traffic to a service on this machine, the rule belongs in INPUT, table filter. To block traffic a router passes on, it belongs in FORWARD.
flow block. Each node has a place (at 150,70), and each group draws a chain around its tables. The three path lines make the journey buttons, and the move lines make the buttons that drive a packet.chain:choice(…) lists the five chains. The line view: hooks as board by chain shows the same rows again as a board.flashcards block, one question :: answer per line.course for the small capitals above the title, lead for the opening line and => for a takeaway.view: hooks as board by chain to view: hooks as board by table. The lanes become raw, mangle, nat and filter.Which table changes the source address? :: nat, in POSTROUTING. The deck grows by one.=> Port forwarding is DNAT in PREROUTING. under the table. It becomes a takeaway like the others.Next: Flashcards studies a whole deck day by day; Step-by-step explainer tells a process down the page.
~~~ selfscribe
# Lesson: Build an interactive lesson: a map the reader follows and drives, a table to look things up in, and cards to test themselves.
# You learn: paths through a diagram, move lines that let the reader drive a packet, a typed data block shown again as a board, flashcards, line rules for a kicker, an opening line and takeaways.
# Try this: press a journey under the map and watch its packet travel, then step along it a table at a time with the arrows.
# Try this: change `view: hooks as board by chain` to `view: hooks as board by table`, and the lanes become the four tables.
# summary: A lesson to click through: a map to follow, a table to look things up in, cards to test yourself.
# category: study
# tags: interactive, advanced, school
# uses: diagram, animation, quiz, table
# The data rulebook brings Markdown with it, and adds data blocks and views.
use data
set title Interactive lesson
set style sans wide
set heading-font Space Grotesk
# ---- The lesson's colour, with its shade for dark pages ---------------------------------
palette
lesson #0b7285 dark #66d9e8
# ---- ```flow ... ```: the map, drawn by the flow plugin ---------------------------------
# Its `path` lines are the journeys to follow; its `move` lines are the buttons that drive a packet.
block ``` flow ... ``` -> flow verbatim
# ---- ```flashcards ... ```: "question :: answer" lines become cards to turn over ---------
block ``` flashcards ... ``` -> flashcards verbatim
# ---- "course …" above the title, "lead …" under it ---------------------------------------
line course _ -> div.course { font: 700 0.74em/1 var(--doc-font-ui); letter-spacing: 0.16em; text-transform: uppercase; color: var(--lesson); margin-bottom: -2.6em } as nf-course
line lead _ -> p.lead { font-size: 1.15em; line-height: 1.5; opacity: 0.8; max-width: 46em; margin-top: -0.3em } as nf-lead
# ---- "=> text": a takeaway, the one thing to remember from a section -------------------
line => _ -> p.takeaway { border-left: 3px solid var(--lesson); padding: 0.6em 1em; border-radius: 0 8px 8px 0; background: color-mix(in srgb, var(--lesson) 9%, transparent) } as nf-takeaway
~~~
course Linux networking · Lesson 3 of 6
# How Linux moves a packet
lead Every packet passes the same few checkpoints in the kernel. Press a journey under the map and watch one travel, or drive one yourself with the buttons.
```flow
set direction=LR
set start=netin
node netin "Arrives" :start at 0,70
group prerouting "PREROUTING" {
node raw_pre "raw" at 150,70
node mangle_pre "mangle" at 290,70
node nat_pre "nat" at 430,70
}
node route "For this machine?" :decision at 590,58
group forward "FORWARD" {
node mangle_fwd "mangle" at 800,70
node filter_fwd "filter" at 940,70
}
group postrouting "POSTROUTING" {
node mangle_post "mangle" at 1120,70
node nat_post "nat" at 1260,70
}
node netout "Leaves" :end at 1410,70
group input "INPUT" {
node mangle_in "mangle" at 602,220
node filter_in "filter" at 602,310
}
node proc "A program here" :service at 760,490
node dropped "Dropped" color=red at 860,310
group output "OUTPUT" {
node raw_out "raw" at 1120,490
node mangle_out "mangle" at 1120,400
node nat_out "nat" at 1120,310
node filter_out "filter" at 1120,220
}
netin -> raw_pre
raw_pre -> mangle_pre -> nat_pre
nat_pre -> route
route -> mangle_fwd "no"
mangle_fwd -> filter_fwd
filter_fwd -> mangle_post
mangle_post -> nat_post
nat_post -> netout
route -> mangle_in "yes"
mangle_in -> filter_in
filter_in -> proc
proc -> raw_out
raw_out -> mangle_out -> nat_out -> filter_out
filter_out -> mangle_post
filter_in -> dropped dash=dashed color=red
filter_fwd -> dropped dash=dashed color=red
filter_out -> dropped dash=dashed color=red
path incoming "For this machine": netin raw_pre mangle_pre nat_pre route mangle_in filter_in proc color=green
path forwarded "Passing through": netin raw_pre mangle_pre nat_pre route mangle_fwd filter_fwd mangle_post nat_post netout color=orange
path outgoing "Sent from here": proc raw_out mangle_out nat_out filter_out mangle_post nat_post netout color=purple
move "arrives": netin raw_pre mangle_pre nat_pre
move "passes": nat_pre route
move "for this machine": route mangle_in filter_in
move "for another machine": route mangle_fwd filter_fwd
move "accepted": filter_in proc
move "dropped by a rule": filter_in dropped
move "accepted": filter_fwd mangle_post nat_post
move "dropped by a rule": filter_fwd dropped
move "the program replies": proc raw_out mangle_out nat_out filter_out
move "accepted": filter_out mangle_post nat_post
move "dropped by a rule": filter_out dropped
move "leaves": nat_post netout
```
The five checkpoints are called **chains**, and each one consults a few **tables** of rules in a set order. `iptables` and `nftables` rules are entries in those tables, so knowing the route tells you where a rule has to go. There are three journeys:
- **For this machine:** PREROUTING, then the routing decision says yes, then INPUT, then the program that listens.
- **Passing through,** when the machine is a router: PREROUTING, the decision says no, FORWARD, POSTROUTING.
- **Sent from here:** OUTPUT, then POSTROUTING. It never sees PREROUTING or INPUT.
The arrows beside the journeys walk a packet one table at a time. To drive one yourself, use the buttons under the map: start a packet, choose where it is bound, and see what it meets. The trail under the buttons is its route, and Reset sends the next packet.
=> A packet meets **either** INPUT **or** FORWARD, never both. That one fact settles most "why is my rule not matching?" questions.
## Which table, in which chain
Each table has one job. Not every chain consults every table, and the order within a chain is fixed. Sort or filter the list, or switch it to cards.
```data hooks as table
chain:choice(PREROUTING|INPUT|FORWARD|OUTPUT|POSTROUTING), order:number, table:choice(raw|mangle|nat|filter), job
PREROUTING, 1, raw, Mark packets to skip connection tracking
PREROUTING, 2, mangle, Change packet headers (TTL and marks)
PREROUTING, 3, nat, Change the destination (DNAT and port forwarding)
INPUT, 1, mangle, Change headers of packets for this machine
INPUT, 2, filter, Accept or drop packets for this machine
FORWARD, 1, mangle, Change headers of packets passing through
FORWARD, 2, filter, Accept or drop packets passing through
OUTPUT, 1, raw, Mark packets this machine sends to skip tracking
OUTPUT, 2, mangle, Change headers of packets this machine sends
OUTPUT, 3, nat, Change the destination of packets this machine sends
OUTPUT, 4, filter, Accept or drop packets this machine sends
POSTROUTING, 1, mangle, Last change to headers before leaving
POSTROUTING, 2, nat, Change the source (SNAT and masquerade)
```
The same rows as a board, one lane per chain:
view: hooks as board by chain
=> To block traffic to a service on this machine, the rule belongs in **INPUT**, table **filter**. To block traffic a router passes on, it belongs in **FORWARD**.
## Check yourself
```flashcards
# Netfilter chains
A web server on this machine gets no traffic. Which chain and table do you look at first? :: INPUT, the filter table
You forward port 8080 on the router to a machine inside. Which chain changes the destination? :: PREROUTING, the nat table
Machines behind the router reach the internet with the router's address. Which chain does that? :: POSTROUTING, the nat table (masquerade)
Does a packet that is being forwarded pass through INPUT? :: No. It meets FORWARD instead
Which two chains does a packet sent by a local program pass through? :: OUTPUT, then POSTROUTING
```
---
## How it's made
- **The map** is one `flow` block. Each `node` has a place (`at 150,70`), and each `group` draws a chain around its tables. The three `path` lines make the journey buttons, and the `move` lines make the buttons that drive a packet.
- **The table** is a data block with typed columns: `chain:choice(…)` lists the five chains. The line `view: hooks as board by chain` shows the same rows again as a board.
- **The cards** at the end are a `flashcards` block, one `question :: answer` per line.
- **Three rules of its own** do the rest: `course` for the small capitals above the title, `lead` for the opening line and `=>` for a takeaway.
## Try it yourself
1. Under the map, drive a packet: arrives, passes, for another machine, dropped by a rule. The trail ends at Dropped.
2. Change `view: hooks as board by chain` to `view: hooks as board by table`. The lanes become raw, mangle, nat and filter.
3. Add a card to the deck: `Which table changes the source address? :: nat, in POSTROUTING`. The deck grows by one.
4. Write a line `=> Port forwarding is DNAT in PREROUTING.` under the table. It becomes a takeaway like the others.
Next: *Flashcards* studies a whole deck day by day; *Step-by-step explainer* tells a process down the page.