Where is routing table stored internally in the Linux kernel?

24,691

The route or the ip utility get their information from a pseudo filesystem called procfs. It is normally mounted under /proc. There is a file called /proc/net/route, where you can see the kernel's IP routing table. You can print the routing table with cat instead, but the route utility formats the output human readable, because the IP adresses are stored in hex.

That file is not just a normal file. It is always generated at exactly the moment when opening it with an attempt to read, as all files in the proc filesystem.

I you are interessted how that file is written, then you need to look at the kernel sources: That function outputs the routeing table. You see at line 2510, the header of the routing table is printed. The routing table appears to be mostly in the struct fib_info that is defined in the header file ip_fib.h, line 98.

Share:
24,691

Related videos on Youtube

syntagma
Author by

syntagma

Updated on September 18, 2022

Comments

  • syntagma
    syntagma over 1 year

    When I execute route -n, from where exactly (from which structs) is the information displayed retrieved?

    I tried executing strace route -n but I didn't help me finding the right place it's stored.

    • Sreeraj
      Sreeraj about 9 years
    • Bandrami
      Bandrami about 9 years
      It's stored in memory. Are you looking for the memory address it's at?
    • syntagma
      syntagma about 9 years
      @Bandrami I would like to see what struct is responsible for holding the information.
    • David Tonhofer
      David Tonhofer over 5 years
      For anyone looking for the file on disk, it's either built from whatever the DHCP server gives out, or from what can be found in /etc/sysconfig/network-scripts/ifcfg-${DEV} in simple cases and/or from what can be found in /etc/sysconfig/network-scripts/route-${DEV} in more convoluted cases (case of Red Hat / CentOS 7). The latter file contains the output of ip -r. See Add a Static Route on CentOS.