IP Addresses and Subnetting

This section will attempt to explain what an IP address is, how a network can be split into subnets, and how to choose the value of your subnet mask.

The IP Address

In an IP network (such as the Internet, or a typical office network), a node (such as your PC) can be identified on the network by a unique IP address. Note that the IP address is unique within the context of its own network.

Such an IP address is held as a 32 bit number (using the current IPv4 standards that are prevalent today). When expressed in decimal format, the IP address is made up of four blocks of figures, where each block represents 8 bits (an octet) and can therefore range from 0 to 255 (since 2 raised to the power of 8 is 256). This is called dotted decimal notation. If this doesn't make much sense or seems hazy, it might be worth reviewing your knowledge of binary. To determine your PC's IP address, open a Windows command prompt and type "ipconfig" (without the quotes) and hit return. (Please don't take me too literally... hitting your return key can be hazardous to your keyboard's health.) For example, if your IP address appears as 80.3.255.101, then what you are seeing is the decimal representation of the IP address:

01010000 00000011 11111111 01100101B

It's also worth bearing in mind that your machine may have two or even more IP addresses. For example, my PC has two Ethernet network cards: one plugs into my cable modem and is my gateway to the Internet, while the other connects my PC to my home network (which is actually just my main Athlon-based PC and an old Pentium II that I use for mucking about with).


Subnetting

The ipconfig command (ifconfig under Linux) will also reveal another 32 bit number called the subnet mask. This mask is used to split a network into groups of smaller 'networks', called subnets.

This topic is a bit (no pun intended) tricky to explain, so you will be forgiven for having to read this more than once...

Firstly, you need to understand that any given 32 bit IP address is actually composed of network bits and host bits. You can think of the IP address as being composed of a prefix and a suffix, where the prefix defines the IP address of the network, and the suffix uniquely identifies any given machine (aka 'host') within that network.

It is the subnet mask that defines how many bits of the IP address define the subnet that the machine belongs to, and how many bits of the IP address uniquely identify the machine itself on that subnet.

Okay? No? Time for an example then... Let's say your subnet mask is set to 255.255.255.0. This is a pretty typical value. In binary, this would be written as:

11111111 11111111 11111111 00000000

You can see that the first 24 bits are set to 1, and the remaining 8 are set to 0. There is a short-form notation for this, and it's called the netprefix notation or CIDR notation. When the first 24 bits are set to 1, as above, then this is called a "/24 netprefix".

In a /24 netprefix subnet, the first 24 bits of any IP address define the subnet, and the remaining 8 bits - called the host bits - uniquely identify the node itself on that subnet.

The way this works depends on something called a bitwise AND operation. This will be familiar to anyone who is well versed in maths, physics or computing. Basically, it works like this... You have two bits, which we shall call X and Y. If bit X is set to 1 AND bit Y is set to 1, then the resulting bit of our Boolean AND operation is also 1. If X and Y have any other values, then the result of the AND operation will be 0. Hence the output is only true (i.e. set to 1), when both of the inputs are also true. This is best shown with a truth table diagram:

AND gate
X Y X · Y
0 0 0
0 1 0
1 0 0
1 1 1

Now let's see how this applies to our IP address and subnet mask. We compare the two values bit-for-bit. If a given bit is 1 in the subnet mask, then the result of the bitwise AND operation with the equivalent bit in the IP address will only be 1 if the corresponding bit in the IP address is also 1.

For the purposes of setting up our netprefix /24 subnet, this means that computers within the same LAN will see each other so long as the first 24 bits match after a bitwise AND operation of the IP address with the subnet mask. This means that the first 24 bits of the IP address must be identical in all machines in the subnet. Only the last 8 bits (the host bits) may vary from machine to machine (and these indeed should be different from machine to machine in order to uniquely identify themselves on the network). If we convert the IP addresses back to decimal, this means that the first three groups of the IP address must be identical, while any figure is permitted in the last group.

For example, let's imagine we have a network, and the subnet mask of all machines on the network is set to 255.255.255.0 (netprefix /24). The IP addresses of four nodes on the network are as follows:

10.0.0.1
10.0.0.2
10.0.0.100
10.0.1.101

The first three machines exist in the same subnet, because the first 24 bits of the IP addresses match. However, the fourth is in a different subnet and cannot be seen by the other machines in the IP network.


Classful and Classless IP Addressing

In the early days of IP addressing, subnets were essentially divided into classes. This scheme was called classful IP addressing. Most important were the three classes A, B and C. Class A subnets are equivalent to /8 subnets. The subnet prefix always has bit 0 (the first bit) set to 0. 24 bits are used for host addressing, allowing approximately 16.8 million hosts per subnet. Because the first bit of the subnet prefix is fixed (it can only be 0), this means that in fact only 7 bits are available for determining the subnet. Hence class A networks can only be divided into 128 different subnets (with up to 16.8 million hosts per subnet).

Class B subnets are equivalent to /16 subnets. The subnet prefix always begins with the first two bits set to 10. 16 bits are used for host addressing, allowing approximately 65 thousand hosts per subnet.

Class C subnets are equivalent to /24 subnets. The subnet prefix always begins with bits 110 and only 8 bits are allocated to host addressing.

As the Internet rapidly grew, it became obvious that this classful addressing scheme was limiting and inflexible. Hence classless addressing became dominant. This is why today's IP addressing scheme makes use of subnet masks which can divide IP addresses into network bits and host bits at an arbitrary point.


Choosing your subnet mask

Why would you choose a subnet mask of 255.255.255.0, or any other value for that matter? Well, this depends on how many subnets you want in your network, and in turn, how many nodes you want in each subnet.

With a /24 netprefix, the first 24 bits are dedicated to identifying the subnet, while only 8 bits identify a node within the subnet. Since 2 raised to the power of 8 is 256, this gives us 256 nodes within a subnet. Actually, we can only have 254 nodes, since setting all the host bits to 1 is the broadcast address of the network, whereas setting all the host bits to 0 is the subnet IP address itself (i.e. the address of the subnet, not one of its nodes).

However, 24 bits identify the subnet, which means that we can have 16.8 million (2 raised to the power of 24) subnets in our network, each with a possible 254 nodes!

Similarly, let's say we use a netprefix of /16. This means that 16 bits identify the subnet, and the remaining 16 identify the nodes within each subnet. Hence we can have some 65 thousand subnets, each permitting a possible 65534 nodes. (Yes, this number is correct. Remember, it's 216 - 2.)

Time for an example...

What do you do if you want to create a network that will hold a maximum of 10 machines? First, you determine how many host bits are required. 2 to the power of 3 is 8, and 2 to the power of 4 is 16, so we know we need 4 bits to allow 10 nodes. Thus we reserve the last 4 bits of the 32 bit subnet mask for nodes, and we use the first 28 bits to identify the subnet itself. This is obviously a /28 subnet:

11111111 11111111 11111111 11110000

Converting to decimal, we must set our subnet mask to 255.255.255.240.

However, when straying from the easy-to-use netprefixes of /8, /16 and /24, assigning IP addresses to your nodes is a bit more tricky. Recall that for your 10 nodes to exist within the same network with a /28 netprefix, the first 28 bits of all their IP addresses must be the same. If we identify the lowest allowed IP address of a node on our network as 10.0.0.1, then the highest allowed IP address for a node on the same subnet is 10.0.0.15. Why? Well, let's compare the IP addresses in binary:

00001010 00000000 00000000 00000001 - Lowest allowed IP address
00001010 00000000 00000000 00001111 - Highest allowed IP address

If our IP address was 10.0.0.16, then in binary this would be:

00001010 00000000 00000000 00010000

In this case, the 28th bit would not be the same as the other machines in the network. The /28 netprefix does not allow this value. Since we initially decided that we only wanted 10 machines in our subnet, a more sensible range would be 10.0.0.1 to 10.0.0.10. Of course, this range has been decided by you as a network administrator, and is not a limitation imposed by your subnet mask.

The table below shows a list of common subnet values, together with the maximum number of hosts an individual subnet can support.

Netprefix Subnet mask
(dotted decimal notation)
Maximum hosts
(host bits - 2)
/8255.0.0.016777214
/16255.255.0.065534
/17255.255.128.032766
/18255.255.192.016382
/19255.255.224.08190
/20255.255.240.04094
/21255.255.248.02046
/22255.255.252.01022
/23255.255.254.0510
/24255.255.255.0254
/25255.255.255.128126
/26255.255.255.19262
/27255.255.255.22430
/28255.255.255.24014
/29255.255.255.2486
/30255.255.255.2522

Getting an Internet IP Address

If you are connected to the Internet, chances are that you are connected via an Internet Service Provider (ISP). If so, then your ISP will allocate you an IP address when you connect. If you connect with a dial-up modem, then your IP address will be dynamic, meaning that you will be allocated a new IP address every time you connect to the Internet. Of course, it is possible that your ISP will allocate you the same IP address twice in a row.

Even if you are connected using a cable modem with an 'always-on' connection, chances are that you are still be assigned a dynamic IP address by your ISP. This is transparent to you, because when your IP address expires, a new one is allocated to you immediately by your ISP's DHCP server. This new IP address may well be the same one you had before. If you have a cable modem that plugs directly into an Ethernet card in your computer, and the TCP/IP properties of this connection are set within your software to 'obtain an IP address automatically', then you are indeed getting a dynamic IP address.


Recommended IP Ranges

If your IP network is not going to be directly connected to the Internet, then you can use any IP addresses and subnets you like for your nodes. However, for such networks, there are private address recommendations. Basically these recommendations state that there are ranges of IP addresses that are not used in the Internet. You are free to use the IP addresses for your private network(s):

The 'class' of network is a throwback to the times of classful IP addressing. The class represents the network size. Class A networks are the biggest standard class, since they use 24 bits for host addressing, giving 16.8 million possible nodes within a subnet.

Class B networks allow 16 bits for host addressing, allowing 65 534 different nodes on each subnet. (Yes, this figure is correct. Recall that with all host bits set to 0, the IP address refers to the network itself, and with all host bits set to 1, we are referring to the broadcast address.) Note that only 16 such networks are available according to the private addressing recommendations.

Finally we have class C networks, which permit 254 nodes per subnet. The private addressing recommendations allow for 256 such networks.


IP Version 6

The current IP addressing system (IP version 4) has proved to be very well designed and highly scalable. When TCP/IP first came about, there were very few computers and very few LANs. Now we're seeing a time where just about everyone has an Internet connection!

Unfortunately, no scheme can last forever and it would seem that IPv4 is facing a serious brick wall: 32 bit addressing only provides for some 4300 million unique addresses. While that's a lot of addresses, the time will soon arrive when this simply isn't enough! (In fact, due to the way IP addresses are allocated by network prefix, address space is much more limited than it would first appear.)

IP version 6 has been introduced to address (sorry, bad pun) some issues with the current IPv4 system. For example, IPv6 provides a mechanism to associate datagrams with a particular network path, thereby providing better performance for applications which require a constant and reliable data path, such as streaming audio and video.

However, the biggest issue that will be resolved with IPv6 is the current lack of address space. IPv6 uses 128 bit IP addresses. Like IPv4, these addresses are split into network prefix and host suffix. But unlike IPv4, IPv6 addresses also allow for hierarchical addressing.

Fortunately, to ease the migration to IPv6, existing IPv4 addresses have been mapped into IPv6 address space. This is done by setting the last 32 bits of the new 128-bit addresses to be the same as the existing IPv4 addresses, and then padding the leading 96 bits with zeros.

Writing out 128-bit addresses using dotted decimal notation would be tedious and require 16 decimal blocks. Thus a new colon hexadecimal notation has been conceived in which the IP address will be split into eight 16-bit groups. Each group will be represented using a 4 digit hexadecimal number. (If this doesn't make any sense, be sure to review the hexadecimal section.) E.g:

84BF:32C5:9450:0:0:12F8:FFFF:FFFF

Furthermore, zeroes can be omitted, so the above IPv6 address can be represented like this:

84BF:32C5:9450::12F8:FFFF:FFFF

What's next

The next section looks at the OSI 7 Layer Model.