A Deep Dive Into IP Addresses
IPv4 vs IPv6, Binary vs Dots, Network Prefixes vs Host Identifiers, Subnetting and Netmasks, CDIR
IPv4 Addresses
IPv4 addresses are normally expressed in dot-notation xxx.xxx.xxx.xxx
where xxx
is a value from 0 to 255. But another way to express them is as a 4-tuple of octets, which is an 8-bit segment since 2⁸=256. Here is the same IPv4 address in both dot-notation and 4-tuple octet.
172.217.6.3610101100 11011001 00000110 00100100
IPv6 Addresses
IPv6 addresses are normally expressed in colon-notation as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
where xxxx
is a hexadecimal value. We can express them as an 8-tuple of 16-bit segments. Here is the same IPv6 address as an 8-tuple of 16 bits.
2001:0db8:0012:0001:3c5e:7354:0000:5db10010000000000001 0000110110111000 0000000000010010 0000000000000010
0011110001011110 0111001101010100 0000000000000000 0101110110110001
Note there are only 2³² possible IPv4 addresses but 2¹²⁸ possible IPv6 addresses. There is then a one-to-one but not onto mapping from IPv4 to IPv6 addresses. The Internet is (gradually) transitioning to IPv6 addresses so it can support more networked devices, without having to resort to NAT-ing.
Network Prefix and Host Identifier
The Internet is not a single large network, but rather a collection of networks. One of these networks may be a college campus network; another of these networks may be a metro-wide ISP’s network, and they connect to each other through internet exchange points.
IPv4 addresses are comprised of two parts. The first part is the network prefix, which identifies the network the address belongs to. The second part is the host identifier, which identifies the host within that network.
Where the network prefix ends and where the host identifier begins depends on the class of the IPv4 address.
IPv4 Address Classification
There are 5 classes of IPv4 addresses, labeled A through E. The class of the IP address is determined by the first 4 bits.
- Class A — IP addresses are in this class if their first bit is a
0.
In dot-notation, this is the range0.0.0.0
to127.255.255.255
. The first 8 bits represent the network prefix and the rest represents the host identifier. For example,127.42.13.69
has network prefix127
and host identifier42.13.69
. - Class B — IP addresses are in this class if their first two bits are
10
. In dot-notation, this is the range128.0.0.0
to191.255.255.255
. The first 16 bits represent the network prefix and the rest represent the host identifier. For example,129.42.13.69
has network prefix129.42
and host identifier13.69
. - Class C — IP addresses are in this class if their first three bits are
110
. In dot-notation, this is the range192.0.0.0
to223.255.255.255
. The first 24 bits represent the network prefix and the rest represent the host identifier. For example,196.13.42.69
has network prefix196.13.42
and host identifier69
. - Class D — IP addresses are in this class if their first four bits are
1110
. In dot-notation, this is the range224.0.0.0
to239.255.255.255
. These addresses are used for multi-casting protocols (ie. when a single packet can be sent to multiple hosts in one action) - Class E — IP addresses are in this class if their first four bits are
1111
. In dot-notation, this the range240.0.0.0
to255.255.255.255
. These addresses are reserved for future and experimental use.
Reserved IP Addresses
Some IPv4 addresses are reserved for specific uses, namely loopback IPs and Private IPs.
Loopback IPs
The IPv4 address range 127.0.0.0
to 127.255.255.255
is reserved for looping back, which is when a host sends a network request to itself. Sometimes we want a program on a host to connect back to itself for debugging or development purposes.
Private IPs
The IP ranges 10.0.0.0 — 10.xxx.xxx.xxx
, 172.16.0.0 — 172.31.xxx.xxx
, and 192.168.0.0 — 192.168.xxx.xxx
are designated private network addresses, meaning they can be assigned to computers which must go through the Network Address Translation (NAT) protocol to connect to the Internet. It’s private IPs that make it possible for over 8 billion devices to connect with only about 4 billion IPv4 address (2³² =~ 4 billion).
Subnets
A subnetwork or subnet is basically a smaller network within a larger network. The process of partitioning a network into subnets is called subnetting. Each computer on the same subnet can communicate directly with each other but not directly with computers on a different subnet. This is usually done for security or performance reasons.
As stated earlier, IPv4 addresses are comprised by a network prefix and host identifier. Subnetting is done by partitioning the IP address into three parts, a network prefix, a subnet number, and host identifier. The specification of where these numbers start and end in the IP address is done through a net mask.
Netmasks
A netmask is used to describe which segments of the IP address are the network prefix and host identifier. Namely, the netmask is a 4-tuple of octets, that specify which segment of the IP address as part of the network prefix by using a 1
for every position that corresponds to a network prefix, and 0
otherwise. In other words, we get the network prefix through bit-wise multiplication of the IP address and the netmask.
For example, the default netmask for Class A IP addresses would be 11111111 00000000 00000000 00000000
as binary or 255.0.0.0
in dot-notation.
The default netmask for Class B IP addresses would be 11111111 11111111 00000000 00000000
as binary or 255.255.0.0
in dot-notation.
The default netmask for Class C IP addresses would be 11111111 11111111 11111111 00000000
as binary
or 255.255.255.0
in dot-notation.
Note that netmasks look like an IP addresses but they aren’t. They are used to supplement IP addresses by describing which portion is the network prefix and which section is the host identifier.
Subnetting through Subnet Masks
Subnetting can be done by adding more 1's
to the default netmask of the IP addresses in a network, to determine their subnet number. IPs with the same subnet number belong to the same subnet.
For example, suppose a network has the IP addresses 192.168.5.0-192.168.5.255
and we want to subnet this network. Because 192.168.6.xxx
are class C IP addresses, its default netmask is 11111111 11111111 11111111 00000000
. To subnet, we add an additional 1
on the left side to get 11111111 11111111 11111111 10000000
, which we will call the subnet mask. We can now determine the subnet number of any given IP address in this range.
For example, 192.168.5.130
in binary is 11000000 10101000 00000101 10000010
. If we bit-wise multiply with the subnet mask, we get 11000000 10101000 00000101 10000000
or 192.168.5.128
as the subnet number and 00000000 00000000 00000000 00000010
or 0.0.0.2
as the host number.
Using this computation, we would have divided all the IP addresses in the range 192.168.5.0-192.168.5.255
into two sets, based on their subnet number. The range 192.168.5.1-192.168.5.127
will belong in the same subnet and the range 192.168.5.129-192.168.5.255
will belong in the other subnet.
Classless Inter-Domain Routing (CIDR)
An alternative to applying subnets netmasks to IP addresses is to use Class Inter-Domain Routing (CIDR) notation, which looks like xxx.xxx.xxx.xxx/y
where xxx
is a number from 1-255
and y
is a number from 1-24
that is used to specify the number of bits which make up the network prefix and subnet number.
For example, 192.168.0.15
with netmask 255.255.255.0
is equivalent to 192.168.0.15/24
. They both mean that the first 24 bits of 192.168.0.15
are the networking portion.
Supernetting Through CIDR
While subnetting is the partitioning of a network into subnets, supernetting is the merging of networks into a single network. This can be done with CIDR.
For example, suppose we want to merge the networks with IP address range 192.168.0.xxx
with 192.168.1.xxx
. We can reference their both their IP addresses as 192.168.0.xxx/23.
This is because every IP address in those ranges will have the same network prefix.
For example, the IP addresses 192.168.0.123
and 192.168.1.42
, based on their default netmasks, would belong to different networks, 192.168.0
and 192.168.1
. But with the subnet specified by /23
the first 23 bytes of their IP addresses are the same
192.168.0.123
11000000 10101000 0000000 | 0 01111011192.168.1.42
11000000 10101000 0000000 | 1 00101010
allowing them to belong to the same network. We’ve basically merged the networks 192.168.0.XXX
and 192.168.1.XXX
together, forming a supernet.