IPv4 addressing
In this chapter, we’ll focus on Layer 3: the Network layer. This is the layer where most routing decisions happen, so it’s a major focus for many network administrators and engineers.
This is also the layer where routers operate. Before getting into how IP routing works, you need a solid understanding of IPv4 addressing and network boundaries. In this chapter, we’ll discuss IP version 4 (IPv4) only. Later in the book, we’ll also cover IP version 6 (IPv6).
IPv4 addressing
In Chapter 1.4 The Layers of the TCP/IP model, we discussed that every device that communicates on a TCP/IP-based network must have an Internet Protocol (IP) address. This is a logical address assigned by a network administrator to a NIC card, and it’s associated with the NIC’s MAC address. An IP address does two important jobs:
- It identifies a host on a network.
- It indicates which IP network that host belongs to.
IP networks have boundaries. Within a single IP network, there’s a range of IP addresses that can be assigned to hosts. A key skill for a network administrator is knowing how to determine that range for any given network.
The construct that defines the network boundary is the subnet mask. The subnet mask tells you where an IP network begins and ends. By inspecting the subnet mask, you can also determine the size of the network - in other words, how many IP addresses this network can accommodate.
Understanding binary
Before we discuss the structure of an IPv4 address, we need to understand binary and why it matters for IP addressing.
Binary (also called the base-2 numeral system) is a numbering system that uses only ones and zeroes (1s and 0s). Computers interpret data using binary, and networking devices like routers and switches do the same. Humans work comfortably with words and decimal numbers, but computers and networking devices ultimately process information as 1s and 0s.
In computing:
- 1 means on
- 0 means off
At the physical layer, those 1s and 0s are represented as signals:
- On copper Ethernet, a 1 might be represented by (roughly) 1 volt, and a 0 by 0 volts.
- On fiber optic cable, a 1 is the presence of a light signal, and a 0 is the absence of that light.
When 1s and 0s arrive in a particular sequence, devices interpret that sequence as data. It’s similar to how we combine letters into words and words into sentences - except computers do it with bits.
(Image 120)
Image Title - Binary digits compared with the word “CAFE”
With that in mind, to understand the value of an IP address and determine what network it belongs to, you need to be able to convert an IP address from dotted decimal format into binary.
(Image 121)
Image Title - IP Address 192.168.1.2 in Dotted Decimal and Binary
The structure of an IPv4 address
In the image above, you can see the IP address 192.168.1.2 in dotted decimal notation, and below it you can see its binary representation. Each binary digit is called a bit. As discussed earlier:
- 1 bits are “on”
- 0 bits are “off”
An IPv4 address contains 32 bits total. Those 32 bits are split into 4 portions called octets. They’re called octets because each portion contains 8 bits.
In dotted decimal notation:
- Each octet is separated by a dot (.)
- Each octet is shown as a decimal number
Each octet can have a decimal value from 0 to 255.
A natural next question is: how do you convert a decimal number into binary? Let’s walk through the process.
Converting from decimal to binary
To convert from decimal to binary, you need to understand the positional values within an octet. Positional values are the numeric values assigned to each bit position.
This is similar to place value in decimal numbers (1s place, 10s place, 100s place). For example, in the number 254:
(Image 122)
Image Title - Positional Values of the whole number 254
In decimal, the digits represent different place values (hundreds, tens, ones). Binary works the same way, but the values increase by powers of 2.
For an 8-bit octet, the positional values are:
(Image 123)
Image Title - Positional Values of an Octet
In binary, each position can contain only:
- 1 (meaning we “use” that positional value)
- 0 (meaning we don’t)
Let’s convert the first octet of 192.168.1.2, which is 192, into binary.
(Image 124)
Image Title - Binary sequence for 192
To decide whether a position gets a 1 or a 0, you repeatedly ask: Is the remaining number greater than or equal to this positional value?
Starting with the first positional value of 128:
- Is 192 greater than or equal to 128?
- If the answer is “yes”, (which it is), then we must place a 1 in the 128 column.
- Then, we’ll subtract the positional value 128 from 192 (192 - 128) and we end up with 64.
Let’s move on to the next positional value (64) and ask a similar question using the number we just subtracted:
- Is 64 greater than or equal to 64?
- Here, the answer is still “yes”, so then we will place a 1 in the 64 column.
- Then, we will subtract the value 64 from the difference in the previous step, which was also 64 (64 - 64) and we end up with 0.
Let’s also do the second octet (168) together.
Starting with the first positional value of 128:
- Is 168 greater than or equal to 128?
- Yes, so we must place a 1 in the 128 column.
- Then, we’ll subtract the positional value 128 from 168 (168 - 128) and we end up with 40.
Moving on to the second positional value (64):
- Is 40 greater than or equal to 64?
- No, so we must place a 0 in the 64 column.
- Whenever there is a “0” in the positional value, we do not subtract, we simply move on to the next positional value.
Third positional value, 32:
- Is 40 greater than or equal to 32?
- Yes, so we must place a 1 in the 32 column.
- Then, we’ll subtract the positional value 32 from 40 (40 - 32) and we end up with 8.
Fourth positional value, 16:
- Is 8 greater than or equal to 16?
- No, so we must place a 0 in the 16 column.
- Whenever there is a “0” in the positional value, we do not subtract, we simply move on to the next positional value.
Fifth positional value, 8:
- Is 8 greater than or equal to 8?
- Yes, so we must place a 1 in the 8 column.
- Then, we’ll subtract the positional value 8 from 8 (8 - 8) and we end up with 0.
At this point, we’re already at 0, so the remaining positional values will be 0. The binary representation of 168 then becomes:
10101000
Hopefully you can now see how to convert decimal numbers to binary digits within an octet.
Converting from binary to decimal
To convert from binary to decimal, the simplest method is to add up the positional values that have a 1.
- Wherever you see a positional value with a 1, add that value.
- Ignore any positional values that contain a 0.
The total is the decimal value of the octet.
(Image 125)
Image Title - Adding up all the positional values within an Octet
As you can see, converting from binary to decimal is much simpler than converting from decimal to binary.
For practice, attempt to convert the following IP addresses into binary:
- 10.1.1.255
- 172.32.1.4
- 192.168.8.64
In the next chapter, we’ll look at how the bits within a subnet mask determine what network an IP address belongs to.