Using Subnet Masks and IP addresses for routing


Basic Premise

Once you have selected an organization for your network you must set up the routers within your network to function properly.

The basic premise is that a subnet mask is used in conjunction with an IP address to determine the subnet in which a machine is located.  This is necessary because routing algorithms assume that ALL MACHINES IN A SUBNET ARE PHYSICALLY TOGETHER AND CAN BE ROUTED AS A UNIT.  So the problem for the router is to determine which network/subnet the destination IP is in and route the packet regardless of the host being referenced on the subnet.  Having to worry only about subnets and not about networks makes the process much simpler for the router.


Applying the Masks to Addresses to Resolve Routing Questions

When applying a subnet mask, one performs a BITWISE AND on the value of the destination address and the mask.

Consider the following example
 
 
IP first octet second octet third octet fourth octet
Subnet Mask 255.255.255.0 11111111 11111111 11111111 00000000
Destination IP 134.12.13.5 10000110 00001100 00001101 00000101
Result of AND 134.12.13.0 10000110 00001100 00001101 00000000

Notice the impact of putting a 1 in a position on the mask is to effectively pass that bit of the address through and the impact of a zero in a position on the mask is to block a bit and force a zero in that position.

In this example, the subnet mask indicates that the first three octets are to be examined and the last octet ignored.  What you find here is a typical subdivision of a class B address (134.12.0.0) into a subnet (13) and host (5), so the effect of the mask is to look at the network part of the address (including subnet), 134.12.13.0, and ignore the host, 5.

Other examples with a 255.255.255.0 mask:
 
Subnet Mask 255.255.255.0
Destination IP 137.155.2.12
Result of AND 137.155.2.0
 
Subnet Mask 255.255.255.0
Destination IP 137.155.45.23
Result of AND 137.155.45.0


Some Less Typical Approaches

Although these examples seems to imply that all bits in an octet of a mask must be the same, this is not true. With a subnet mask of 255.255.255.0, the class B address is roughly divided into 256 subnets each having 256 hosts. One could choose to have less subnets and more machines per subnet.  Consider an example with 8 subnets and 8192 machines per subnet
 
 
Subnet Mask 255.255.224.0 11111111 11111111 11100000 00000000
Destination IP 134.12.13.5 10000110 00001100 00001101 00000101
Result of AND 134.12.0.0 10000110 00001100 00000000 00000000

Note the first three bits of the third octet of the mask determine the subnet. Since there are three bits, 8 subnet addresses can be formed. Can you name them? (0, 32, 64, 96, 128, 160, 192, 224)  This particular address (134.12.13.0) resides in subnet 0 with a host number of 13.5.

Here is an example from the same class B address but with a different and more interesting/challenging subnet.
Subnet Mask 255.255.224.0
Destination IP 134.12.194.7
Result of AND 134.12.192.0
This machine is on subnet 192 with host number 2.7.



Try these examples and see if you get the correct answers!
 
 
Subnet Mask 255.255.128.0
Destination IP 105.134.195.5
Result of AND 105.134.128.0
 
 
Subnet Mask 255.255.128.0
Destination IP 105.134.127.5
Result of AND 105.134.0.0

The previous two are only differentiating whether the subnet number (third octet) is >=128


Here are 2 examples of a mask which has 512 subnets with 128 machines on each net.
 
Subnet Mask 255.255.255.128
Destination IP 105.134.195.2
Result of AND 105.134.195.0
 
Subnet Mask 255.255.255.128
Destination IP 105.134.195.130
Result of AND 105.134.195.128

The first example is a machine on subnet 195.0 with host 2.
The second example is a machine on subnet 195.128 with host 2.

This example is different than a typical approach, but practical nonetheless.  It illustrates how you could use you class B address to get more subnets and less machines on a net.  As we begin deploying more and more intelligent switches this may not be as much of a concern, but putting 128 machines on an ethernet segment versus 256 makes a huge difference in performance.