Digging into IPv4 Loopback Addressing

Anyone who has worked with IPv4 networking has learned about the loopback address. To explain, say your system cannot connect to the Internet. The problem could be any of the following:

  1. Your router isn’t forwarding your IP packets to the Internet.
  2. Your internal network isn’t forwarding your packets to the router.
  3. Your system isn’t transmitting packets to the network due to a problem with your systems network interface card (NIC).
  4. Your system’s network configuration is incorrect.

The loopback address provides a way to check item 3, verifying your NIC is physically capable of transmitting and receiving packets. The old-fashioned way is to use a loopback cable that routes the transmit line (TX) from the NIC back to the receive line (RX) on the NIC. However, today cards build in the ability to internally loopback the packet when the address 172.0.0.1 is detected. This negates the need for a special cable, but it does remove some components from the test, as the signal never leaves and enters the NIC. Still, it’s a pretty good test, as few networking problems are caused by failed transceiver components on a NIC.

I was curious about why 127.0.0.1 is the standard address? Would 127.0.0.2 work?

IP Addressing Background, Notation

First, it helps to have some background on IP addressing. IP addresses are 32-bit numbers, broken into 4 8-bit numbers, called octets. These octets are separated by a period in standard IPv4 notation, such as 192.168.2.3.

The subnet mask is used to identify which part of the IP address is the network ID, and which part is the system ID. If you bitwise AND the subnet mask with the IP address, those bits not zeroed are the network ID, and those that are zeroed are the system (computer) ID. For example, a class A address with an IP address of 1.2.3.4, with the subnet mask of 255.0.0.0, means the network ID is 1.x.y.z, and the system ID is x.2.3.4. The letter variables are used to indicate any number from 0 to 255.

Reserved IP Addresses

In any network, two IP addresses are reserved, one with the system ID bits all being 1, and the other with all of them being 0.

The system ID bits being all 1 indicates the message is a broadcast message, sent to and processed by every system on the network. Hence, 191.0.255.255 and 192.223.0.255, with subnet masks of 255.255.0.0 and 255.255.255.0 respectively, are both broadcast messages for their respective networks. No systems on those networks can have that address.

More interesting to me is the case where all system ID bits are 0. Per this study guide 1), this is reserved because it matches the system ID. For example, 192.223.1.0 is not a valid IP addresses, because when the subnet mask (255.255.255.0) is AND’d with the IP address, the result of 192.223.1.0 matches the network ID.

It seems to me this should be a valid IP address, as the network ID doesn’t consist of all 4 octets. My belief is a system ID of all 0s has been reserved to simplify the implementation; we can simply mask out the system ID, and treat the 32-bit result as a system ID, for comparison purposes, without having to exclude the system ID bits.

Private IP Addresses

A final note on IPv4 addressing. Some addresses are defined as “private”, or in network speak, nonroutable. A routable IP address is allowed to leave the network.

Say your on a system with an IP address of 192.168.2.5, and you try and load the website “google.com”, which today on my system, resolves to an IP address of 216.58.203.46. Google’s IP address is not on your home network of 192.168.2.x, so it gets forwarded to your gateway device, your router, which then sends it to “the cloud”, via your Internet Service Provider (ISP). Eventually, it’ll get routed to Google’s server, and the response will get routed back to you.

However, if you try and get to an address like 192.168.0.46, your router will not forward this address onto “the cloud”. It’s not on your network, but it’s a private, nonroutable IP address, and your router will simply ignore it.

This is done to prevent collisions between internal networks and publicly accessible networks. Internal networks should use these private IP addresses. Systems on the Internet are not allowed to use those private addresses, so there will not be a conflict with your internal networks and external networks.

IP Address Classes

The IP standard 2) specifies 5 classes for IP addresses. This may become obsolete as the industry moves to a classless IP system 3) or moves to IPv6, but today, this is still useful information.

This table shows the IP address ranges for the different classes, the default subnet mask, and the private (nonroutable) addresses within each class. This information comes from this book 4) and this website 5).

ClassLeading BitsFirst OctetDefault Subnet Mask# of Hosts/NetworkPrivate IP Range
Ab01 – 127255.0.0.0256x256x256 – 2 = 16,777,21410.x.y.z
Bb10128 – 191255.255.0.0256×256 – 2 = 65,534172.16.x.y – 172.31.x.y
Cb110192 – 223255.255.255.0254192.168.x.y
Db1110225 – 239
Eb1111240 – 247

A couple interesting things to note from this table:

  1. The leading bits provide an easy way for the addressing logic (be it in hardware or software) to determine the class of an IP.
  2. Not all available IP address are used. For example, 0.1.1.1 isn’t in any range, although the leading bits indicate it could be a class A address. Also, 254.0.0.1 seems to be a class E address based on the leading bits, but isn’t indicated has a valid IP, per 6). This site 7) indicates these are, at least technically, part of those classes, but the classless system described on this webpage 8) has them reserved for other purposes.

Questions Regarding the Loopback Address

With this understanding, these are the questions that came to my mind.

Why a class A Address for Loopback Tests?

Loopback addresses are for testing your local system. Why use a class A address, which eliminates 16,777,214 addresses (256x256x256 = 16,777,216 address, minus the 2 reserved addresses discussed above), when a class C address would eliminate only 254 addresses?

Why not use a Private IP Address for Loopback Testing?

Again, the loopback address is for testing a local system. It’s not supposed to get routed anywhere, so why not use a private address? We don’t want this to go beyond the system being tested, much less to another network.

I would argue that a loopback address of 223.0.0.1 would’ve been a better choice than 127.0.0.1. Maybe I’m missing something. If you know what, please let me know.

Do all Addresses in the 127.x.y.z Range Work?

I tried pinging the various loopback addresses on my home system running Ubuntu 16.04, LTS. What I wanted to understand is if non-standard loopback addresses work as expected. The table below shows the IPs I tested.

IP AddressResultComment
127.0.0.164 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 msThe standard loopback IP address.
127.255.255.255Do you want to ping broadcast? Then -bThe system is correctly treating this as a broadcast address.
127.255.255.25464 bytes from 127.255.255.254: icmp_seq=1 ttl=64 time=0.019 msA valid, though non-standard loopback IP address.
127.1.1.164 bytes from 127.1.1.1: icmp_seq=1 ttl=64 time=0.020 msA valid, though non-standard loopback IP address.
127.0.0.0Do you want to ping broadcast? Then -bAgain, the system is treating this as a broadcast address.

So yes, non-standard loopback addresses do work, not just the standard 127.0.0.1, with the exceptions of the reserved broadcast and network ID addresses. Granted I didn’t test all combinations of 127.x.y.z, but I believe other combinations will work as expected.

Conclusion

The point of all this is to dig a little deeper into the loopback addressing mechanism in IPv4. While most people know about 127.0.0.1, most don’t know other addresses work just as well. Personally, I find “127.1.1.1” easier to type than “127.0.0.1, so I might adopt this as my new, standard loopback address. Besides, it’s fun to be different, sometimes.

Keep in mind the broadcast address and network ID are always reserved IP addresses.

I’ve posed a few questions and suppositions in this discussion. If you have any answers or clarifications, I’d love to hear from you.

References

1, 4, 6.CompTIA Network+ Certification Study Guide, 6th edition, by Glen E. Clarke
2.https://tools.ietf.org/html/rfc791/
3.https://tools.ietf.org/html/rfc1519/
5, 7.https://en.wikipedia.org/wiki/Classful_network/
8.https://en.wikipedia.org/wiki/Reserved_IP_addresses/

Leave a Reply

Your email address will not be published. Required fields are marked *