Find the names of the network interfaces by their IPs

Linux School Tech
3 min readJun 28, 2024

--

What is /proc/net/fib_trie file?

The /proc/net/fib_trie file in the Linux operating system provides information about the Forwarding Information Base (FIB) in the form of a prefix tree (Trie). FIB stands for Forwarding Information Base and its function is to store information about the best route to reach any IP address on the network.

A prefix tree is a type of tree data structure that is optimized for storing and searching IP addresses efficiently. In this structure, each node in the tree represents an IP prefix, and its sub-branches represent more specific IP addresses.

The /proc/net/fib_trie file provides the following information about each entry in the FIB:

  1. IP prefix: The IP address and its prefix length (e.g., 192.168.1.0/24).
  2. Route type: The type of routing (e.g., unicast, multicast).
  3. Metric: A number that indicates the preference for using this route.
  4. Interface: The network interface that is used to send packets to this IP address.
  5. Flags: Flags that provide additional information about the route.

To view the contents of the /proc/net/fib_trie file, you can use the cat command:

cat /proc/net/fib_trie

Sample Output:

Main:
+-- 0.0.0.0/0 3 0 5
|-- 0.0.0.0
/0 universe UNICAST
/0 universe UNICAST
+-- 127.0.0.0/8 2 0 2
+-- 127.0.0.0/31 1 0 0
|-- 127.0.0.0
/8 host LOCAL
|-- 127.0.0.1
/32 host LOCAL
|-- 127.255.255.255
/32 link BROADCAST
+-- 192.168.1.0/24 2 0 2
+-- 192.168.1.0/30 2 0 2
|-- 192.168.1.0
/24 link UNICAST
/24 link UNICAST
|-- 192.168.1.3
/32 host LOCAL
|-- 192.168.1.255
/32 link BROADCAST
Local:
+-- 0.0.0.0/0 3 0 5
|-- 0.0.0.0
/0 universe UNICAST
/0 universe UNICAST
+-- 127.0.0.0/8 2 0 2
+-- 127.0.0.0/31 1 0 0
|-- 127.0.0.0
/8 host LOCAL
|-- 127.0.0.1
/32 host LOCAL
|-- 127.255.255.255
/32 link BROADCAST
+-- 192.168.1.0/24 2 0 2
+-- 192.168.1.0/30 2 0 2
|-- 192.168.1.0
/24 link UNICAST
/24 link UNICAST
|-- 192.168.1.3
/32 host LOCAL
|-- 192.168.1.255
/32 link BROADCAST

Extracting IP Addresses From /proc/net/fib_trie file In Linux

In this video, we dive deep into a Bash script designed to extract IP addresses from the Linux kernel’s Forwarding Information Base (FIB) Trie and map them to their respective network interfaces.

We’ll walk you through each line of the script, explaining how it reads from /proc/net/fib_trie, processes the data with awk, and uses associative arrays in Bash to create a comprehensive mapping of IP addresses to their device names.

What is a Prefix Tree (Trie)?

A prefix tree (trie) is a type of tree data structure that is optimized for storing and searching IP addresses efficiently. In this structure, each node in the tree represents an IP prefix, and its subtrees represent more specific IP addresses.

Advantages of using prefix trees

  1. Fast searching: Searching for IP addresses in a prefix tree is much faster than searching in a simple list or table.
  2. Efficient storage: Prefix trees can store IP addresses more efficiently than lists or tables, because they only store unique prefixes.
  3. Easy updates: Updating information about IP addresses in a prefix tree is easy, because only the nodes corresponding to the changed IP addresses need to be changed.

Applications of prefix trees in Linux

  1. Forwarding Information Base (FIB): The file /proc/net/fib_trie provides information about the FIB in the form of a prefix tree. The FIB is responsible for storing information about the best route to reach each IP address on the network.
  2. IP routing table: The Linux kernel uses prefix trees to store information about the IP routing table.
  3. Packet filtering: Prefix trees can be used to filter IP packets based on the source or destination IP address. 4) DNS: Some DNS servers use prefix trees to store information about domain names and their corresponding IP addresses.

Read More

My YouTube Channel

More shell script videos and linux tutorials on my YouTube Channel.

--

--