Key Takeaway
Linux Routing System Introduction
Kernel Configuration
The Linux kernel manages packet forwarding through specific system parameters, primarily involving aglobal switch and interface-level settings.
Global Switch:net.ipv4.ip_forward
Temporary Enable
Interface-Level Control:net.ipv4.conf.$NIC.forwarding
Temporary Enable for a Specific Interface
Relationship Between Global and Interface-Level Settings
Packet forwarding is implemented by the Linux kernel and relies on the interplay between the global switch and interface-level settings:-
Global Switch Priority: When
net.ipv4.ip_forwardis set to0, packet forwarding isdisabledforall interfaces, regardless of interface-level settings. When set to1, thenet.ipv4.conf.$NIC.forwardingsetting determines whether forwarding is enabled for a specific interface. -
Interface-Level Control: When the global switch is enabled (
net.ipv4.ip_forward=1), administrators can usenet.ipv4.conf.$NIC.forwardingto control forwarding behavior for specific interfaces, providing greater flexibility. - Routing Table Dependency: The actual packet forwarding path is determined by the kernel’s routing table. Proper routing rules (e.g., configured using ip route) must be set to ensure packets are correctly forwarded from the source interface to the destination interface.
Practical Use Cases
Kubernetes Networking
In Kubernetes clusters, container networking often relies on the host for packet forwarding.CNI plugins (e.g., Flannel, Calico) require the host to enable net.ipv4.ip_forward to support inter-Pod communication. In some custom network environments, enabling net.ipv4.conf.$NIC.forwarding for specific interfaces may also be necessary to meet specific traffic control requirements.
Reference: Container Runtimes
Router or Gateway
A Linux host can be configured as a router or gateway to connect different network segments. This requires enabling packet forwarding and configuring appropriate routing tables and firewall rules (e.g., iptables or nftables) to ensure security and proper traffic management.Routing Table (RT)
Routing Tables in the Linux Kernel
-
Purpose of Routing Tables:
Routing tables store
routing rulesthat determine the transmission path of packets. Each routing table contains a set of routing entries specifying the destination address, next hop, interface, and other related information. -
Support for Multiple Routing Tables:
The Linux kernel supports up to
255routing tables, identified byrt_table_idvalues ranging from 0 to 255. These tables enable advanced routing strategies, such as policy-based routing based on source address, interface, or custom rules. -
Management Tools:
User-space tools like
ip route(from the iproute2 package) are used to manage and query routing tables. Table definitions are typically configured in/etc/iproute2/rt_tables. -
Kernel Implementation:
Routing tables are managed by the kernel’s networking subsystem (found in
net/ipv4/route.candnet/ipv6/route.c), and are closely related to the IP forwarding functionality implemented indevinet.c.
Discussion on Local, Main, Default, and Unspec Routing Tables

local (ID: 255), main (ID: 254), default (ID: 253), and unspec (ID: 0). These tables define different types of routing rules that determine how packets are transmitted across the network.-
Priority: The
localtable has the highest priority, followed bymain, and thendefault(as part of main). Theunspectable is rarely involved in actual routing. -
Configuration File: Routing table configurations are typically stored in
/etc/iproute2/rt_tables, where new table names and IDs can be defined.
local routing table
local routing table
- Definition: The local routing table is a special table used to store routing information directly related to the local host. It primarily handles packets for local interfaces, such as the loopback interface (e.g., 127.0.0.1) and broadcast addresses.
-
Purpose:
- Manages packets for local network interfaces.
- Includes routes for directly connected network addresses (e.g., 127.0.0.0/8 or IP addresses of local interfaces).
- These routes are typically auto-generated by the system and have the highest priority.
-
Characteristics:
- Cannot be overridden by general routing rules.
- Typically used for local communication, where packets do not leave the host.
-
Default ID is
255, as defined in/etc/iproute2/rt_tables.
-
Example:
This shows routes for the loopback interface and local IP addresses.
main routing table
main routing table
- Definition: The main routing table is the default primary routing table in Linux, containing most routing rules for general packet forwarding and transmission.
-
Purpose:
- Handles routing for non-local packets, such as those destined for external networks or the internet.
- Includes the default gateway and other manually or dynamically configured routes.
-
Characteristics:
-
Default ID is
254. - If no specific routing table is specified, the ip route command operates on the main table by default.
- Most network applications and services rely on this table for packet routing.
-
Default ID is
-
Example:
This indicates the
default gatewayand routes for thelocalnetwork.
default routing table
default routing table
- Definition: The default routing table is not a standalone table but a specific routing rule within the main table, commonly referred to as the default route.
-
Purpose:
- When a packet’s destination address does not match any specific route, the system forwards the packet to the default gateway.
- Typically used for connecting to external networks or the internet.
-
Characteristics:
-
Appears in the main table as default via
<gateway> dev <interface>. - It is the most common route for handling packets with unknown destinations.
-
Appears in the main table as default via
-
Example:
This indicates that all packets with unknown destinations are sent to 192.168.1.1
unspec routing table
unspec routing table
- Definition: The unspec routing table is a special table that is rarely used and typically not directly visible in standard Linux routing configurations. It serves as a placeholder or for internal system use, indicating an “unspecified” routing table.
-
Purpose:
- Often appears in specific routing protocols or policy routing scenarios, possibly for temporary or undefined routes.
- The system may use unspec to represent routes not yet assigned to a specific table.
-
Characteristics:
-
Default ID is
0. - Rarely manipulated directly by users and seldom used in practical applications.
- May be indirectly involved in ip rule or advanced routing configurations.
-
Default ID is
-
Example:
Typically returns no significant content, as the unspec table rarely contains actual routes in standard configurations.
Experiment 1 - Communicate between network namespaces with different subnet
Experimental Environment Setup
commands - Create our environment
commands - Create our environment
Let’s begin the experiment
1. Perform ping command from ns2 to ns1

ns1 from ns2.-
On the
Hostside, listen for packets passing through thebr0interface. -
From
ns2, send a packet tons1:-
At this point, you will get
Network is unreachable. -
No packets pass through
br0on the Host side.
-
At this point, you will get
-
Use the command
$ ip rto check the current routing table inns2.
This routing rule can be broken down as follows:
This routing rule can be broken down as follows:
-
Destination Network:10.22.34.0/24
- Indicates the target network is the 10.22.34.0 subnet with a subnet mask of /24 (i.e., 255.255.255.0).
- This means the route applies to all IP addresses ranging from 10.22.34.0 to 10.22.34.255.
-
Device (dev): eth0
- Packets will be sent through the network interface eth0. This is typically an Ethernet interface, indicating the host’s local network interface.
-
Protocol (proto): kernel
- Indicates this route was automatically added by the kernel, usually generated by the system when configuring the network interface. Such routes typically relate to networks directly connected to the host.
-
Scope: link
- Means the route’s scope is link, which applies only to the local network directly connected to this network interface (communication within the same subnet).
- This implies packets are not forwarded to other subnets but communicated directly with devices on the same subnet via eth0.
-
Source Address (src): 10.22.34.5
- Indicates that when the host sends packets via this route, it will use 10.22.34.5 as the source IP address. This is usually the IP address assigned to the eth0 interface.
2. Monitor packet at br0 interface and try ping command again

-
Used the command
$ ip route add 10.22.33.0/24 dev eth0-
Command breakdown:
-
Destination Network:
10.22.33.0/24- Specifies the target network for this route: 10.22.33.0 with a subnet mask of /24 (255.255.255.0).
- This means the route applies to all IP addresses in the range 10.22.33.0 to 10.22.33.255.
-
Device:
dev eth0- Indicates that packets destined for the 10.22.33.0/24 network should be sent out through the
eth0network interface.
- Indicates that packets destined for the 10.22.33.0/24 network should be sent out through the
-
Destination Network:
-
Command breakdown:
-
Tried pinging
ns1again fromns2-
The ping still failed; communication with
ns1was not possible. -
On the host side, the
br0interface observed ARP request packets. We saw ARP request was asking who has 10.22.33.4 (it’s the IP address ofns1)
-
The ping still failed; communication with
3. Monitor packet at ns1’s eth0 interface and try ping command again

-
This time, we also monitor the
eth0interface inns1to see if it receives the ARP packets. -
Again, from
ns2, we initiate a ping tons1:-
We observe that
ns1’seth0interface has received the ARP request packets. -
However,
ns2still does not receive a reply.
-
We observe that
ns1 received the ARP request, why doesn’t it send a response?- This is because
ns1’s routing table has no entry for how to send packets tons2’s subnet (10.22.34.0), so it cannot forward the packet.
4. Update ns1’s Routing Table

ns1 environment, we add ns2’s subnet (10.22.34.0) to the routing table.After adding it, we immediately observe that the br0 interface on the host receives ICMP Reply packets from ns1, which are then successfully delivered to ns2.Take a deeper look at how Linux handles ARP packets
In the previous experiment, there was a question as follows: At that time, our answer was: “This is becausens1’s routing table had no entry for how to send packets to ns2’s subnet (10.22.34.0), so it naturally couldn’t forward the packet.”
But wait — let’s rethink the layers to which ARP and the routing table belong.
ARP is used to resolve IP addresses to MAC addresses and operates at the Data Link layer (Layer 2). In theory, ARP does not need Layer 3 (IP layer) routing information.
Since they operate at different layers, why is ARP still influenced by the routing table? Let’s look into the code to uncover the truth!
Implementation of ARP handling code in the Linux kernel
The following code discussion is based on Linux Kernel version6.16
Introduction to rp_filter (Reverse Path Filter)
Introduction to rp_filter (Reverse Path Filter)
-
Definition: rp_filter is a mechanism that performs reverse path checking to validate incoming packets against the system’s
routing tablerules. If a packet’s source IP address does not match the expected routing path, the system discards the packet. -
Purpose:
- Prevents spoofed IP packets from entering the system, particularly to mitigate DDoS attacks or network spoofing.
- Ensures that the source IP of a packet corresponds to the routing path defined in the system’s routing table.
- Enhances network security, especially in multi-interface or multi-network environments.
/proc/sys/net/ipv4/conf/<interface>/rp_filter or /etc/sysctl.conf. This parameter can be set individually for each network interface or globally (/proc/sys/net/ipv4/conf/all/rp_filter).Possible Values:-
0: Disables reverse path filtering; the system does not check the source path of packets. -
1: Enables strict mode. The system checks if the source IP belongs to the receiving interface and if that interface is the best path for sending response packets (based on the routing table). -
2: Enables loose mode. The system only checks if the source IP exists in the routing table (not requiring it to be the best path).
-
Strict Mode (rp_filter=1):
- When a packet arrives at an interface, the system checks the packet’s source IP address.
- The system simulates the routing of a response packet to verify if it would be sent out via the same interface.
- If the source IP does not match the best path for the receiving interface, the packet is discarded.
- Suitable for single-path networks or environments requiring high security.
-
Loose Mode (rp_filter=2):
- Only checks if the source IP exists in the system’s routing table, without requiring the response packet to use the same interface.
- Suitable for asymmetric routing environments, such as multi-path or load-balanced scenarios.
-
Disabled (rp_filter=0):
- No reverse path checks are performed, allowing all packets to pass.
- May be used in specific network configurations but offers lower security.
Experiment Environment Review
Network Configuration:-
ns1:
- Interface: eth0, IP: 10.22.33.4/24 (subnet 10.22.33.0/24).
-
Initial routing table:
- Missing route to 10.22.34.0/24.
-
rp_filter=2(loose mode).
-
ns2:
- Interface: eth0, IP: 10.22.34.5/24 (subnet 10.22.34.0/24).
-
br0:
- IP: 10.22.33.1/24 (subnet 10.22.33.0/24).
- Bridges ns1 and ns2 to provide Layer 2 communication.
-
ns2(10.22.34.5) pingsns1(10.22.33.4), sending an ARP request to resolve 10.22.33.4’s MAC address. -
ns1receives the ARP request (sip = 10.22.34.5, tip = 10.22.33.4) but does not respond because its routing table lacks an entry for 10.22.34.0/24. -
After adding the route on
ns1usingip route add 10.22.34.0/24 dev eth0,ns1replies to the ARP request, and the ping succeeds.
Kernel Code analysis
Mermaid flowchart in ns1
Mermaid flowchart in ns1
[net/ipv4/arp.c] function arp_process → function ip_route_input_noref
[net/ipv4/arp.c] function arp_process → function ip_route_input_noref
[net/ipv4/route.c] function ip_route_input_noref → function ip_route_input_rcu
[net/ipv4/route.c] function ip_route_input_noref → function ip_route_input_rcu
[net/ipv4/route.c] function ip_route_input_rcu → function ip_route_input_slow
[net/ipv4/route.c] function ip_route_input_rcu → function ip_route_input_slow
[net/ipv4/route.c] function ip_route_input_slow → function fib_validate_source_reason
[net/ipv4/route.c] function ip_route_input_slow → function fib_validate_source_reason
[net/ip_fib.h] function fib_validate_source_reason → function fib_validate_source
[net/ip_fib.h] function fib_validate_source_reason → function fib_validate_source
[net/ipv4/fib_frontend.c] function fib_validate_source → function __fib_validate_source
[net/ipv4/fib_frontend.c] function fib_validate_source → function __fib_validate_source
[net/ipv4/fib_frontend.c] function __fib_validate_source → function fib_lookup → block last_resort → block e_rpf → return -SKB_DROP_REASON_IP_RPFILTER
[net/ipv4/fib_frontend.c] function __fib_validate_source → function fib_lookup → block last_resort → block e_rpf → return -SKB_DROP_REASON_IP_RPFILTER
Code Summary
In Linux 6.16, the__fib_validate_source function performs the following steps:
-
Flow Setup: Sets
src(sip, 10.22.33.x) as the lookup target. -
Routing Lookup:
fib_lookupchecks whethersrcis reachable via the routing table. -
Type Check: Ensures that the route is of type
RTN_UNICASTor the allowedRTN_LOCAL. - Device Match: Verifies whether the route uses the receiving interface (eth0).
-
Fallback Handling: If
fib_lookupfails andrp_filteris non-zero (in my case, it’s 2), it returnsSKB_DROP_REASON_IP_RPFILTER.
ns1 lacks the 10.22.34.0/24 route, fib_lookup fails, and __fib_validate_source returns -SKB_DROP_REASON_IP_RPFILTER, preventing the ARP reply.
After the route is added, fib_lookup succeeds and returns 0, allowing ns1’s ARP to send out the ARP Reply packet.
Conclusion
In this article, we have drawn the following conclusions:- Gained a basic understanding of the Linux routing system.
- Successfully pinged between different subnets.
- Learned that when processing ARP packets, the Linux kernel still consults the routing table.