Networking Basics: IP addresses, Masks, router addresses

Network Basics

Beneath the top coat

by Craig Miller

Networks are cool and useful, but sometimes we fly too high, and miss the basics of what is happening under the hood. This month we'll discuss:

The above are basics, but it will help you help someone else when you are asking for help, or just figuring out how to get to your router to update the configuration.

IP Addresses

If you have listened to me long enough, you know that there are two (2) types of IP addresses:

IPv4 is an address in the human readable form of four octets separated by periods e.g 192.168.175.13

But in reality, the computer sees this address as a 32 bit number: 3232280333

It does this because it can easily apply a mask to the number, which we'll cover later.

IPv6 has similar characteristics in human readable form of hextets separated by colons e.g. 2001:db8:8011:fd60:5d49:9499:419c:cc47

Again, the computer sees the address as a binary number, representing the above in decimal: 42540766450918422389299266845530704967

This is less useful for humans to understand, and this is why IPv6 is represented in hexidecimal (base 16). Each hexidecimal digit represents 4 bits in the address. So looking at the IPv6 address again, the first hextet of "2001" would be 0010 0000 0000 0001 in binary. Again, the computer represents the address in binary in order to apply a mask.

What is a Net Mask?

As mentioned earlier, an IP address (both IPv4 and IPv6) are stored as binary numbers in memory, which allows a mask to be applied to the number.

How does a mask work?

To illustrate how a mask works, let's use smaller numbers, that are represented as 8 bits. Take the hex number 52, which when represented in binary would be 0101 0010. And let us apply a mask of F0, which is 1111 0000 in binary. Using the logical AND function, where a 1 in the number and the mask will produce a 1, and everything else will be 0

0101 0010   52
1111 0000   F0
---------
0101 0000   50

Using a Net Mask

In the example above one could represent the mask as a /4, that is the first 4 bits (counting from the left) are 1s.

Since early 1990s, the creators of the internet knew the 4 billion size of IPv4 addressing was going to be a problem. In 1993, RFC 1519 Classless Inter-Domain Routing (CIDR):an Address Assignment and Aggregation Strategy was introduced. CIDR (pronounced cider) introduces the concept of a variable length network mask. You may have seen a network mask as 255.255.255.0, but in CIDR that would be represented as a /24, since the mask would be a continuous set of 1s for the first 24 bits, followed by 8 zeros.

The netmask determines the border between the network part of the address and the host part. In a common home network, a host address might be 192.168.175.13. If the network mask is /24, then the network part of the address will be 192.168.175.0 and the host part will be 13.

This is easy if the network mask falls on an 8 bit boundary, but CIDR introduced the concept of any length of mask. For example what would be the network and host, if the netmask is a /23? Let's look at the IPv4 address with the second half in binary

192.168.10101111.00001101 Address
255.255.11111110.00000000 /23 Mask

Now the network (in decimal) is 192.168.174.0 and the hosts are from 1-511 (512 is the broadcast address). As you can see changing the mask, can increase the number of host addresses, but it steals bits from the network portion of the address.

What about netmasks for IPv6?

Glad you asked. A problem with variable subnet masks (made possible by CIDR) is, that it is hard. If you don't believe me, how many IPv4 host addresses are in a /27?

In IPv6, one of the improvements is networks will always be /64. This means the first 64 bits are the network number, and the last 64 bits are the host number. Looking again at the example IPv6 address:

  Network         |   Host (or IID)
2001:db8:8011:fd60:5d49:9499:419c:cc47

While it is certainly possible to apply a longer mask in IPv6, many things will break, as RFC 7421 highlights. So it is better to keep the netmask on IPv6 as /64, you will have fewer problems.

What is a default route?

Now that we have a clearer understanding of addressing and netmasks, let's look into routing. One of the best parts of the internet is that your device (laptop, Pi, IoT) can get to any other device on the internet (well technically this is not true with the wide spread use of IPv4 NAT (Net Address Translation). But how do the packets from your device know how to get to their destination?

Routing tables

Your home router will have a routing table that tells it how to forward packets. The simple rule of a routing table is : Take the longest match route. Given the following route table:

# ip route
default via 192.168.118.1 dev wan 
192.168.175.0/24 dev lan4 scope link  src 192.168.175.2 
192.168.118.0/24 dev wan scope link  src 192.168.118.192 
192.168.243.0/24 dev br-lan scope link  src 192.168.243.1 

If the destination address is: 192.168.175.13, then using the netmasks of the routing table, you will see that the second line matches, and therefore the packet will be sent out the lan4 interface.

It is the same with IPv6, although you will notice that the netmask is not always /64. This is normal, because only end-station networks (where hosts are) need to be /64. (the line numbers have been added for clarification)

# ip -6 route
1   default from 2001:db8:8011:fd40::/64 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 512 
2   default from 2001:db8:8011:fd44::/62 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 512 
3   2001:db8:8011:fd00::/56 from 2001:db8:8011:fd40::/64 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 512 
4   2001:db8:8011:fd00::/56 from 2001:db8:8011:fd44::/62 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 512 
5   2001:db8:8011:fd00::/64 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 32 
6   2001:db8:8011:fd00::/62 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 32 
7   2001:db8:8011:fd04::/62 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 32 
8   2001:db8:8011:fd11::/64 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 32 
9   2001:db8:8011:fd60::/60 via fe80::58ef:68ff:fe0d:51b7 dev wan  metric 32 
10  fe80::/64 dev eth0  metric 256 
11  fe80::/64 dev br-lan  metric 256 
12  fe80::/64 dev wan  metric 256 
13  fe80::/64 dev lan4  metric 256 

Again with a destination address of 2001:db8:8011:fd60:5d49:9499:419c:cc47, the longest matching route is line 9, sending the packet out the wan interface.

The default route

As you can see in the previous routing tables, both IPv4 and IPv6 have default routes (sometimes more than 1). The default route means, the route of last resort. If a packet comes into the router, and doesn't match any of the specific routes, then use the default route, and let the upstream router decide what to do with the packet.

The default route on a host, will always be the address of your local router. In the above example for IPv4 the address of the local upstream router is 192.168.118.1. And for IPv6 the local router's address is fe80::58ef:68ff:fe0d:51b7.

But wait, the IPv6 address looks funny. It is a link-local address (which always start with fe80::). Without delving too deeply into IPv6, every interface will have a link-local address. This is the same for the router, it also has link-local addresses for all of its interfaces, and all of them start with fe80::.

Where does the default route come from?

How does your laptop, Pi, or IoT get a default route? It depends.

For IPv4, the default route is part of the DHCP (Dynamic Host Control Protocol) transaction (RFC 2132). This made sense at the time, because if you didn't get an IPv4 address from DHCP, then you wouldn't get to your local router (aka default route address) either.

For IPv6, another improvement is to get the router address from a Router Advertisement (RA). Unlike IPv4, IPv6 routers periodically send out RAs onto the network, advertising themselves as a the local router (which will become a default route on your host), in addition to other information about the local network (e.g. the network prefix, use DHCPv6 or SLAAC or both, DNS server addresses, etc).

Q & A

What is your laptop/device's IP address?

What is the router's IPv4 and IPv6 address on the Lab LAN?

Network Plumbing

We often take for granted, the underlying plumbing of IP networks. We just want to open a socket to some remote server, and be done with it. But as you can see there's a lot happening down in the basement of the network stack, and how nice that it all works pretty well.


Notes:


19 Sept 2023