Fundamentals of TCP/IP
Keywords: IP addresses, subnets, subnet masks, network address, broadcast address, network classes, classfull subnetting, classless subnetting, reserved subnets, routing, default route.
The format of 192.168.0.1 (4 octets) is the most common way to represent an IP address. Usually, network masks are represented in this format too: 255.255.255.0. This however is not the actual representation of what a computer, network interface or kernel works with. These work using bits!
IP Addresses in Bits
So, how should these decimal octets be translated to something a computer or network interface understands? Basically, an IP address uses 32 bits (4 bytes). Everything is derived from these 32 bits, the IP address as well as the subnet mask, network classes and thus classfull and classless subnets.
To calculate from a decimal IP address, use the following table:
Notice that the bit position is indicative for the power you set 2 to.
To convert the IP address 192.168.0.1 binary form, here’s how to:
For each octet, mark the decimal values you want to use to get to the total value in the octet. Using 192, we need 128 and 64, and we are done:
Notice we use a 1 to mark the field, you can mark the fields that are empty with a 0. Putting these ‘marks’ together, it reads 11000000 (which happens to be the binary equivalent of 192).
The second octet is a little more difficult. Since it’s bigger then 128, we need the first bit marked 1:
Now having a total of 128, we can’t add 64 because the result (128+64 = 192) is too large (192 > 168). The third bit however ‘fits’:
And the grand total is now 160. Mathematics say we need another bit with decimal value 8, which is of course the 5th bit:
And we can fill out the rest with zero's. The grand total is now
168. Same exercise goes for the third octet (0) and the fourth (1).
HINT: If you remember the powers of two, it saves you headaches.
Network/Subnet, or Address Mask
The address mask is calculated in the same way:
In binary form this is: 11111111.11111111.11111111.00000000
This binary value indicates the number of bits that is used for 1) the network, 2) the subnet, 3) the host. 1 & 2 practically can be merged, but we will not, to emphasize to you the difference between network and subnet addressing. You may understand later on in this document, where we describe networks vs. subnets.
Every bit in the binary form of the IP address with this mask should be preserved if the mask has a ‘1’ on the corresponding bit position.
||Decimal Octets||Binary Octets|
|First Host Address||192.168.0.1||11000000.10101000.00000000.00000001|
|Last Host Address||192.168.0.254||11000000.10101000.00000000.11111110|
Notice here that the network address is the minimal value possible using the preserved bits, and that the broadcast address
is the maximal value possible using the preserved bits. The first and
last host address available in this range are the minimum+1 address and
the maximum-1 address, respectively.
In the old days, some genius decided that all the available IP addresses should be divided into different categories, named classes. This resulted in Class A, B, C, D and E networks. The meaning of these networks intentionally was: Use Class C for the smallest networks, Class B for larger networks, and Class A for enterprise networks. There are additional classes: Class D is for multicast, and Class E has been reserved for (possibly) future use. The classes are defined as follows:
|Class||First Octet in Binary||Decimal Values||Network Mask|
Notice that these classes are defined by the highest-priority bits in the first octet. (High priority bits are on the left, as they are the most distinctive, just like the number 9854).
Calculating the number of available IP addresses in each network is quite easy:
For a Class A network, as the first 8 bits are used to form the ‘network part’ of the IP address, this leaves 24 bits to form the ‘host part’. 2^24 therefore is the number of IP addresses in this network. Two of those are reserved, the network address, and the broadcast address. So the maximum number of hosts in this network is 2^24 -2 (16.8 million).
Subnets – sub-networks may be a more appropriate term – are subs of a network (class) that is divided into smaller parts.
Given a Class A IP address the network mask is 255.0.0.0. This however is way too large for many organizations except Internet Service Providers. The maximum number of hosts in this network is 16.8 million, which is way too many for a single broadcast domain, and only 128 companies would have such a range available.
|class||Number of networks
||number of hosts per network
||number of possible 'network owners'
||128 - 6
||16384 - 19
||2097152 - 772
Using networks, and networks alone, only 2.112.867 companies would be allowed to connect to the internet, which of course is way too few. This calls the need for sub-networks. The concept of sub-networks (subnets), allow an organization that has a network to divide the network into smaller parts. These companies are typically Internet Service Providers or very large corporations. The ISP in this case is the better example. It connects clients to the internet and each of these clients will need a subnet or IP address. Most often only business customers can get additional IP addresses, and consumers get just one IP address.
So, a business wants to connect to the internet, and having 5 servers that need to be connected to the internet, it asks for additional IP addresses for it’s ISP. The ISP has Class A network 126.96.36.199/8, and needs to give the customer 5 IP addresses…
Using bits in the address mask of an IP address, you can only have subnets sized by powers of 2. So the ISP counts how many hosts bits someone would need to have 5 host addresses, and 2 IP addresses (network / broadcast). Is 1 bit enough? 1 bit can have 2 IP addresses, and leaves no room for hosts. Is 2 bits enough? 2 bits would allow for 4 IP addresses, and thus 2 hosts. Is 3 bits enough? 3 bits would allow for 8 IP addresses, and thus 6 hosts. Bingo!
The ISP decides that, of the 32 bits in any IP address in the Class A 188.8.131.52/8 network, the last 3 bits will be used as ‘host bits’. As the first 8 bits are the network bits, and 3 bits are used to form the host part of the address, this leaves 32 – 11 (21) bits to form the subnet part of the address. Altogether, 32 – 3 bits (29) are used to form the mask, so the network mask will be /29.
Classfull and Classless Subnets
The above example shows the concept of Classless subnets. While the ISP has a network, the customer gets a classless subnet. It no longer restrains us to the use of entire classes, but allows for smaller subnets to be used within a network. It also allows us to mix up terms in 'network bits' and 'subnet bits', because really there are only classfull and classless subnets. Summarizing what we have read so far, an IP address exists of a number of subnet bits (whether a classfull or classless subnet), and a number of host bits.
Using the Class A network 10.0.0.0/8 for our network, having numerous network devices, we want to devide these devices into different subnets. We decide to place the servers and clients into different subnets, so that we can place a firewall in between and protect our servers from these clients. Since we have a limited amount of servers, we will use 10.0.0.0/25 for our server network, and 10.0.1.0/24 for our clients.
Because these are two different subnets, we need a router to connect
from one network to the other and vice-versa. This will also be our
firewall. We use a linux machine with two network interfaces,
connecting one interface to the server LAN (suppose eth0), and one
interface to the client LAN (suppose eth1). We'll configure eth0 to
hold IP address 10.0.0.1/25, and eth1 to hold IP address 10.0.1.1/24.
To start routing, enable forwarding (echo 1 >
/proc/sys/net/ipv4/ip_forward should suffice). Optionally, stop your
firewall if you haven't already. Configuring your firewall is a
seperate topic, see the related articles or 'Additional Reading' on the
bottom of this page.
How does this matter?
If at your home you have a 192.168.1.0/24 network, and your gateway is 192.168.1.1, try and configure one of your boxes with a /25 address prefix (255.255.255.128 netmask). You will not be able to ping your gateway, because you belong to another subnet (you're using a different address mask). The address mask does matter.
In the (probably very rare) case you have a large network at home,
and need to use more then 254 host address, try using the 10.10.3.0/23
subnet. This mask indicates the first 23 bits are subnet bits, and the
minimal value (remember: the network address) of 10.10.3.0 in binary,
with the first 23 bits preserved, is (in decimal octets) 10.10.2.0. So
you'll actually be using the 10.10.2.0/23.
We test this stuff on our own machines, really we do. But you may run into problems, if you do, come to #fedora on irc.freenode.net