Netfilter Framework(ipv4)
graph LR
%% 定义节点样式
classDef hook fill:#d9ead3,stroke:#6aa84f,stroke-width:2px;
classDef process fill:#cfe2f3,stroke:#3d85c6,stroke-width:2px;
classDef routing fill:#e6e6e6,stroke:#666666,stroke-width:2px;
%% 主流程节点
NetIn["Network Interface<br>(Input)"]:::process
PreRouting("PRE_ROUTING"):::hook
RoutingDec1["Routing Decision"]:::routing
LocalIn("LOCAL_IN"):::hook
HigherLayers["Higher Layers<br>Local Processes"]:::process
Forward("FORWARD"):::hook
LocalOut("LOCAL_OUT"):::hook
RoutingDec2["Routing Decision"]:::routing
PostRouting("POST_ROUTING"):::hook
NetOut["Network Interface<br>(Output)"]:::process
%% 连接关系
NetIn --> PreRouting
PreRouting --> RoutingDec1
RoutingDec1 --> LocalIn
RoutingDec1 --> Forward
LocalIn --> HigherLayers
HigherLayers --> LocalOut
LocalOut --> RoutingDec2
RoutingDec2 --> PostRouting
Forward --> PostRouting
PostRouting --> NetOut
%% 模拟原图箭头颜色
%% Receive packet (红色路径)
linkStyle 0 stroke:#ff0000,stroke-width:2px;
linkStyle 1 stroke:#ff0000,stroke-width:2px;
linkStyle 2 stroke:#ff0000,stroke-width:2px;
linkStyle 3 stroke:#ff0000,stroke-width:2px;
%% Transmit packet (蓝色路径)
linkStyle 4 stroke:#0000ff,stroke-width:2px;
linkStyle 5 stroke:#0000ff,stroke-width:2px;
linkStyle 6 stroke:#0000ff,stroke-width:2px;
linkStyle 7 stroke:#0000ff,stroke-width:2px;
linkStyle 8 stroke:#0000ff,stroke-width:2px;
linkStyle 9 stroke:#0000ff,stroke-width:2px;
Hook Structure
|
|
-
hook callback function
- It is a callback function
- Signature depends on kernel version.
1 2 3typedef unsigned int nf_hookfn(void *priv, struct sk_buff *skb, const struct nf_hook_state *state); -
callback response
1 2 3 4 5 6 7 8 9 10//netfilter.h /* Responses from hook functions. */ #define NF_DROP 0 #define NF_ACCEPT 1 #define NF_STOLEN 2 #define NF_QUEUE 3 #define NF_REPEAT 4 #define NF_STOP 5 /* Deprecated, for userspace nf_queue compatibility. */ #define NF_MAX_VERDICT NF_STOP
| Hook | Chain |
|---|---|
NF_DROP |
Drop the packet |
NF_ACCEPT |
Continue normal traversal |
NF_STOLEN |
Current hook function will take care of the packet. Don’t continue traversal |
NF_QUEUE |
Queue the packet (usually for user space handling) |
NF_REPEAT |
Call this hook again |
NF_STOP |
Terminate the hook chain processing without releasing sk_buff data |
- priority
- Callbacks of same hook point are called in ascending order of priority
|
|
Hook Position in Linux Network Stack
|
|
在 kernel v2.6.25之后 ,将原本仅用于 IPv4 的 NF_IP_XXX 枚举合并到了统一的 NF_INET_XXX 命名空间中,可做兼容处理
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25))
|
|
PREROUTING
| Hook | File | Function |
|---|---|---|
| NF_INET_PRE_ROUTING | net/ipv4/ip_input.c |
ip_rcv() |
- Works only for incoming packets.
- Packet is passed to this hook after the simple sanity checks.
- Before routing decision is made (forwarded or local in).
- This is the first hook through which incoming packet is passed through.
|
|
__netif_receive_skb_core() 会根据 skb 的 protocol 字段(ETH_P_IP)在 ptype_base 哈希表中查到 ip_packet_type,并将 pt_prev 指向它,然后通过pt_prev->func 调用 ip_rcv
|
|
INPUT
| Hook | File | Function |
|---|---|---|
| NF_INET_LOCAL_IN | net/ipv4/ip_input.c |
ip_local_deliver() |
- Works only for incoming packets.
- Triggered after the routing decision (i.e., once the kernel determines the packet is destined for the local host).
- Occurs before local processing (e.g., before
|
|
FORWARD
| Hook | File | Function |
|---|---|---|
NF_INET_FORWARD |
net/ipv4/ip_forward.c |
ip_forward() |
- Works only for incoming packet.
- According to routing decision, if the packet is sent to another interface then this hook is called.
|
|
OUTPUT
| Hook | File | Function |
|---|---|---|
NF_INET_LOCAL_OUT |
net/ipv4/ip_output.c |
__ip_local_out |
- Works only for outgoing packets.
- The packet is created locally (originating from the host itself).
- Routing decision is made after this hook is called.
|
|
|
|
POSTROUTING
| Hook | File | Function |
|---|---|---|
NF_INET_POST_ROUTING |
net/ipv4/ip_output.c |
ip_output() |
- Works only for outgoing packets.
- This is the last hook. After that, the packet is sent to the lower layers.
|
|
Register and Unregister
Netfilter Register Routine
|
|
|
|
|
|
|
|
|
|
Netfilter Unregister Routine
|
|
Register and Unregister user defined callback
linux-learn-demo-code/netfilter at main · charles-7777/linux-learn-demo-code