How is the loopback device implemented?

7,342

It's entirely handled through the routing tables.

It's pretty easy to try out in a virtual machine, if you want to have fun. I don't take responsibility for anything that might happen on the machine where you try this.

First let's change the netmask of lo to 255.255.0.0:

sudo ip addr del 127.0.0.1/8 dev lo; sudo ip addr add 127.0.0.1/16 dev lo

Now let's look at our lo:

$ ip -4 addr show dev lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    inet 127.0.0.1/16 scope host lo

Where would packets to 127.1.1.1 go?

$ sudo ip route get 127.1.1.1
127.1.1.1 via 172.16.22.2 dev eth0  src 172.16.22.130 
    cache  mtu 1500 advmss 1460 hoplimit 64

We start pinging in another terminal:

$ ping 127.1.1.1

Let's monitor ICMP traffic on eth0:

$ sudo tcpdump -i eth0 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
07:28:42.060077 IP 172.16.22.130 > 127.1.1.1: ICMP echo request, id 5665, seq 4, length 64
07:28:43.059920 IP 172.16.22.130 > 127.1.1.1: ICMP echo request, id 5665, seq 5, length 64

I'll leave you with the exercise of changing the netmask of lo on a remote machine to 255.255.0.0 and adding the IP address 127.1.1.1 to their network interface. You can see ICMP replies coming back. Note that routers might not play nicely.

Share:
7,342

Related videos on Youtube

Skyo Livede
Author by

Skyo Livede

Updated on September 18, 2022

Comments

  • Skyo Livede
    Skyo Livede over 1 year

    The loopback networking interface

    is a virtual network device implemented entirely in software.

    But how is this implementation actually accomplished and how do you manipulate it?

    As far as I know, IPs are marked as local in the local routing table:

    ip route show table local  
    

    Sending to an IP marked as local will trigger the loopback device. But is this detection done by purely by routing tables or also by some other kernel operations?

    Edit: My ultimate goal is to manipulate the loop-device configuration, so that sending datagrams among my interfaces (two wlan adapters) on my laptop will result in real traffic / datagrams in the network and not in local loops. Interface 1 -> WLAN -> Interface 2 and NOT: Interface 1 -> LOOP -> Interface 2