1.6 Configure and verify IPv4 addressing and subnetting

Remember that hosts on the same LAN have the same subnet.  Anything behind the router has the same subnet.

If I have a large network, I should make one subnet for

  • Each LAN or VLAN.  We will learn about VLANs later – but for now, you can think of a VLAN like cutting your LAN into multiple segments.  This is done through configuration on the switch (logically) and not physically.
  • Each WAN
  • Each point-to-point serial connection

Once I figure out how many subnets I need, I should figure out how big each subnet must be.

Remember that when a computer wants to access another computer it first must ask itself: is this device on my local network or is it somewhere else?  If it is local, the computer users ARP to figure out the destination’s MAC address.  It sends the data to the destination MAC address; a switch (or multiple switches) on the network carries the data

What happens when the destination is not local?  Then the device must send the data to a router.  But how does it know which router to send it to?  And how does it know whether a device is local?

Every device has network settings, which include at least three items

  • IP address – this is the IP address of the device
  • Subnet mask – this tells the device how big its local network is; the local network is known as a subnet
  • Default gateway – this is another name for a router

The device uses its IP address and the subnet to figure out the range of IP addresses in its local network.  If the destination IP address of a packet is not in the local network, then it is sent to the default gateway.

This is going to be the hardest part of the book.  Learning the complicated math about subnets is difficult.

Networks are classified based on size.  The entire IP address space is from 0.0.0.0 to 255.255.255.255.  But you can’t have all of it.  A company can be assigned a portion of the space, called a network.  Networks can be divided into classes.

  • A Class A network contains 224 addresses.  Networks in the range of 1.0.0.0 to 126.0.0.0.0 are Class A networks.  So, a network with a range of 2.0.0.0 to 2.255.255.255 is a Class A network.

    These are used for unicast in large networks.
  • A Class B network contains 216 addresses.  Networks in the range of 128.0.0.0 to 191.0.0.0 are Class B networks.  So, a network with a range of 130.0.0.0 to 130.0.255.255 is a Class B network.

    These are used for unicast in medium networks.
  • A Class C network contains 28 addresses.  Networks in the range of 192.168.0.0 to 223.0.0.0 are Class C networks.  So, a network with a range of 200.0.0.0 to 200.0.0.255 is a Class C network.

    These are used for unicast in small networks.

We have two more classes of networks

  • Networks in the range of 224.0.0.0 to 239.0.0.0 are Class D networks.  They are used for multicast.
  • Networks in the range of 240.0.0.0 to 254.0.0.0 are Class E networks.  These are reserved for future use.

These networks do not have subnet masks.  They are strictly experimental, and most routers will not accept traffic from IP addresses in their ranges.  The use of a Class A, B, or C network is called Classful Subnetting.

A subnet mask looks like an IP address.  It is 32-bits long (each octet is 8-bits).  If I must go deep down into how computers work, I will.  Remember that computers are electrical.  They only think in terms of “on or off”, like an electrical switch.  So, a 1 is on, and a 0 is off.

8-bits makes up one byte.  A computer with 8-bits can only count to 255 in one operation.  If I make a table that is base-two (every entry is double the previous entry), I can combine these eight numbers to make any number from 0 to 255.  Look at the table below.  You can add these numbers up to make any number you think of between 0 and 255.

1286432168421

If you look at the 8-bits in a byte, each bit is assigned to one of these numbers.  If the bit is a one, or in “on’ position, then the number is added, and if the bit is a zero, or in the “off” position, then the number is ignored.

For example, the computer might represent the following number

1286432168421
11011001

The value of this byte is 128 + 64 + 16 + 8 + 1 = 217

Thus, we have two ways to write out this number, either as 217 or as 11011001

So what?  255.255.255.252 is an example of a subnet mask.  We could write it out as

11111111.11111111.11111111.11111100 if we wanted to.  We call this a binary number.  How did we get the binary number?

Well, the first three octets are “255”.  We get “255” from the table full of 1’s (11111111).

1286432168421
11111111

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

The fourth octet is 252.  We get “252” from the table with six 1’s (11111100).

1286432168421
11111100

128 + 64 + 32 + 16 + 8 + 4 = 252

We could also call 255.255.255.252 a /30 subnet mask, because it has 30 “1’s” in it.  Note that you’ll never see a subnet mask like 255.255.255.217.  Why not?

If you wrote 217 in binary, it would look like 128 + 64 + 16 + 8 + 1 = 217, or 11011001.  The whole subnet mask would be 11111111.11111111.11111111.11011001. 

The 1’s in a subnet mask always appear on the left and the 0’s always appear on the right.  Thus, the only numbers that can fit into a subnet mask are 0, 128, 192, 224, 240, 248, 252, 254, and 255.

Many network engineers like to refer to a subnet mask as a “/30” or “/28” or “slash whatever number it is”, instead of saying the entire name.

But how do I use a subnet mask?  If my IP address is 192.168.0.29 and my subnet mask is /28, how big is my network?  What IP address does it start on and where does it end?  We can figure it out

  • /28 is my subnet.  We can write it out as 255.255.255.240.  We can also write it out as 11111111.11111111.11111111.11110000
  • 192.168.0.29 is my IP address.  We can write it out as 11000000.10101000.00000000.00011101
  • We use the subnet mask to “mask” the IP address. 

    Masking is a special kind of addition. 

    For each place, if there is a zero in either the subnet or the IP address, then the result is zero.
    For each place, if there is a one in both the subnet and the IP address, the result is one.

    11111111.11111111.11111111.11110000 (subnet)
    11000000.10101000.00000000.00011101 (IP address)
    ————————————————–
    11000000.10101000.00000000.00010000 (result)
  • We convert the result back to the decimal: 192.168.0.16
    This gives us our network name.
  • So, my network starts at 192.168.0.16
  • Where does it end?  How big is my network?  I know that my subnet is a /28, and since there are 32 total bits in an IP address, I can subtract to get 4 bits. 

    32 bits total – 28 bits in the subnet = 4 bits in the network.

    Remember that, 32 – subnet bits = network size

    There are 4 bits left for my network. 

    If I count the 4 bits from the end of the table, I get 8 + 4 + 2 + 1 = 15.  So, my network has 15 more IP addresses. 

    It starts at 192.168.0.16 and it ends at 192.168.0.31
  • In any network, the starting IP address is known as the subnet.  It can’t be assigned to a device.  Although my network is called 192.168.0.16, 192.168.0.16 is not a usable IP.
  • The first usable IP address is 192.168.0.17
  • The last IP address in a network is the broadcast address for the network.  Remember broadcast IP addressees?    If a device wants to send a message to all the devices within its network, it sends it to the broadcast address.  In this case, it is 192.168.0.31.  I can’t assign this IP address to a device either.
  • Thus, the last usable IP address is 192.168.0.30
  • Notice that this is a Class C network.  The whole range is 192.168.0.0 to 192.168.0.255.  That is a range of 256 addresses.  Our subnet is 16 IP addresses wide.  Therefore 256 / 16 = 16. 

    If our company was assigned this entire Class C network, we could create up to 16 subnets that each contain 16 IP addresses.
  • If this was a Class B network, it would have a range of 65,536 addresses.  Therefore 65536 / 16 = 4096.  We can create up to 4096 subnets that are 16 IP addresses wide, in a Class B network.

    Of course, we can create subnets that are smaller or larger than 16 IP addresses, but that would change the number of subnets we could create.
  • In summary
    • My IP address is 192.168.0.29
    • My subnet mask is 255.255.255.240
    • The subnet name is 192.168.0.16
    • The first useable IP address is 192.168.0.17
    • The last useable IP address is 192.168.0.30
    • The broadcast IP address is 192.168.0.31

The default gateway can be any IP address in the range of 192.168.0.17 to 192.168.0.30.  Typically, an administrator will give the gateway either the first usable IP address or the last usable IP address, but he doesn’t have to. 

The gateway is the IP address the router has facing the internal network.  It’s the IP address a device will contact when it wants to send traffic out the local network.

By the way, I can write the IP address as 192.168.0.17 or as 192.168.000.017.  The extra zeroes at the beginning of each octet don’t matter. 

The opposite of Classful Subnetting is Classless Subnetting.  How does it work?

If my network is 192.168.0.0 to 192.168.0.255, I have 256 IP addresses.  I can break it down into one network of 256 addresses, or I can break it down into 2 networks of 128 addresses each, or 4 networks of 64 addresses each, or 8 networks of 32 addresses each, etc..  If my network was a Class A or Class B network, I could break it down into even more subnets and/or have even more IP addresses per subnet.

Look at the following table.  We have a choice of seven subnet masks

Subnet MaskNumber of IPs per SubnetNumber of Subnets
/242541
/251262
/26624
/27308
/281416
/29632
/30264

There is no /31 or /32 subnet because we need at least three IP addresses in a subnet – the network name, the useable IP, and the broadcast address.  A /31 subnet would be two IP addresses wide (meaning there would be a network and a broadcast address and no useable IP addresses), and a /32 subnet would be one IP address wide.

We could choose to break down our network into subnets of any size based on our requirements.  We ask ourselves what the largest required subnet is and go from there.  This is known as Fixed Length Subnetting.

What if I need subnets of different lengths?  What if my network range is 192.168.0.0 to 192.168.0.255 (Class C) and I need

  • A subnet with 100 IP addresses for computers
  • A subnet with 20 IP addresses for servers
  • A subnet with 20 IP addresses for network equipment
  • A subnet with 10 IP addresses for surveillance cameras

The smallest subnet that could accommodate 100 IP addresses is the /25, with 128 usable IP addresses per subnet.  But I can only make two /25 subnets out of the network that I have, and I need four subnets.

Introducing the Variable Length Subnet Mask.  If we don’t follow the traditional rules, we can do the following

  • Create a /25 subnet (192.168.0.0 to 192.168.0.127) for the subnet that requires 100 IP addresses.  This subnet has 126 usable IP addresses.
  • Create a /27 subnet (192.168.0.128 to 192.168.0.159) for the subnet that requires 20 IP addresses.  This subnet has 30 usable IP addresses.
  • Create a /27 subnet (192.168.0.160 to 192.168.0.191) for the second subnet that requires 20 IP addresses.  This subnet has 30 usable IP addresses.
  • Create a /28 subnet (192.168.0.192 to 192.168.0.207) for the subnet that requires 10 IP addresses.  This subnet has 14 usable IP addresses.
  • I still have the address space 192.168.0.208 to 192.168.0.255 left over, which is 48 IP addresses.  I can create a /27 and /28 subnet out of it.  Or I can create three /28 subnets out of it.  Or some other combination depending on my needs.

We write the subnets I created as follows

  • 192.168.0.0/25
  • 192.168.0.128/27
  • 192.168.0.160/27
  • 192.168.0.192/28

This notation is called Classless Inter-Domain Routing, or CIDR.  We are writing the name of the network followed by the subnet size (as the number of bits).

If you want to remember an easy formula for the number of usable IP addresses in a subnet, it is 2n-2, where n is the size of the subnet.

For example, if the subnet is /27, then the subnet size is 32 – 27 = 5

2n -2 = 25 -2 = 30 (I have 30 usable IP addresses in a /27 subnet)

In summary, for subnetting

  • I should choose a class of network (A, B, or C) if I can (if it hasn’t been assigned to me)
  • I figure out how many subnets I need – one for each LAN, VLAN, and WAN
  • For each subnet I calculate the size based on the number of devices inside it.  How many usable IP addresses do I need in each subnet?
  • I divide my network into subnets, taking care that I have enough subnets and that they each have an adequate number of IP addresses. 
  • If I don’t have enough IP addresses, I can try variable length subnetting or choose a larger class of network.

Going back to my three classes of networks

  • A Class A network contains 224 addresses.  Networks in the range of 1.0.0.0 to 126.0.0.0.0 are Class A networks.  So, a network with a range of 2.0.0.0 to 2.255.255.255 is a Class A network.
  • A Class B network contains 216 addresses.  Networks in the range of 128.0.0.0 to 191.0.0.0 are Class B networks.  So, a network with a range of 130.0.0.0 to 130.0.255.255 is a Class B network.
  • A Class C network contains 28 addresses.  Networks in the range of 192.168.0.0 to 223.0.0.0 are Class C networks.  So, a network with a range of 200.0.0.0 to 200.0.0.255 is a Class C network.

Think about the IP address in a Class A network.  If my network has a range of 2.0.0.0 to 2.255.255.255, that means the first octet doesn’t change.  It’s always “2” or “002”.  We call the first octet the “network” portion.  The remaining portion can be changed (assigned to hosts) and is called the “host” portion.  That means that a Class A network has 8 bits in the network portion and 24 bits in the host portion.

Now apply the same logic to a Class B network.  If my network has a range of 130.0.0.0 to 130.0.255.255, that means the first two octets don’t change.  It’s always “130.0” or “130.000”.  That means that a Class B network has 16 bits in the network portion and 16 bits in the host portion.

Now apply the same logic to a Class C network.  If my network has a range of 200.0.0.0 to 200.0.0.255, that means the first three octets don’t change.  It’s always “200.0.0” or “200.000.000”.  That means that a Class C network has 8 bits in the network portion and 24 bits in the host portion.

So an IP address has two portions

Network PortionHost Portion

When we apply a subnet mask, our IP address has three portions

NetworkSubnetHost

For example, if our network is a Class B, 130.0.0.0 to 130.0.255.25, and our subnet is a /27, there are 27 bits in the subnet mask. 

Now remember that the first two octets – or 16 bits – are fixed in a Class B network. 
That leaves 27 – 16 = 11 bits for the subnet.
Subnet– Network Bits = Subnet Bits
That leaves 32 – 27 = 5 bits for the host. 
32 Bits – Subnet = Host Bits

NetworkSubnetHost
11111111.1111111111111111.11100000

Or in other words, 2h-2 = number of usable IP’s in the subnet, where h is the number of host bits.  For example, 25 – 2 = 30 usable IP addresses in my example.

And, 2s = number of subnets, where s is the number of subnet bits.  For example, 211 = 2048 /27 subnets in my Class B example.

If our network is a Class C, 200.0.0.0 to 200.0.0.255, and our subnet is a /27, there are 27 bits in the subnet mask. 

Now remember that the first two octets – or 24 bits – are fixed in a Class C network. 
That leaves 27 – 24 = 3 bits for the subnet.
Subnet– Network Bits = Subnet Bits
That leaves 32 – 27 = 5 bits for the host. 
32 Bits – Subnet = Host Bits

NetworkSubnetHost
11111111.11111111.1111111111100000

Or in other words, 2H-2 = number of usable IP’s in the subnet, where H is the number of host bits.  For example, 2S – 2 = 30 usable IP addresses in my example.

And, 2s = number of subnets, where S is the number of subnet bits.  For example, 2S = 8 /27 subnets in my Class C example.

Notice that if the network changed, the number of IP’s per subnet doesn’t change, but the number of subnets per network does.

In summary, we can calculate the number of subnets and IP addresses by

  • Finding the number of network bits from the network type (8 for a Class A, 16 for a Class B, and 24 for a Class C)
  • Finding the number of subnet bits (S) from 32 – subnet size
  • 2H-2 is the number of usable IP addresses, where H is the number of host bits
  • 2s = number of subnets, where S is the number of subnet bits
  • Network Bits + Subnet Bits = Network Prefix Length