Subnet Masking and Routing

Subnet Masking

Recall the meanings of the bits in Class A, B, and C addresses:

Class Network Address Prefix Host Address
A First 8 bits 0 Last 24 bits
B First 16 bits 10 Last 16 bits
C First 24 bits 110 Last 8 bits

The network address is part that is assigned to the organization by the IANA. It is fixed for the organization. The host address is the part that can be assigned by the organization. This is the variable part, and can assigned any combination of 0s and 1s by the organization.

A subnet mask is a way of partitioning the organization's network into subnets. A subnet mask can be an important piece of information for a router or switch to perform its job efficiently.

Example:

Suppose you want to partition a Class C address into subnets, each having a subnet address of 27 bits. A subnet mask is used by a router to learn which of the bits in an address refer to the subnet and which bits refer to the host. Suppose the Class C address is 205.34.15.69, which in binary is 11001101.00100010.00001111.01000101 The subnet mask, constructed in binary, consists of a 1 marking each bit of the subnet address and a 0 marking each bit of the host address. In this case the subnet mask consists of 27 1-bits followed by 5 0-bits:

  Binary Dotted Decimal
Address 11001101.00100010.00001111.01000101 205.34.15.69
Binary Subnet Mask 11111111.11111111.11111111.11100000 255.255.255.224
Network Address 11001101.00100010.00001111.01000000 205.34.15.64

How many possible hosts addresses are there on the subnet ? Answer: 25 = 32.

Example:

The Class B address 131.5.252.19 is part of a subnet where the first 23 bits form the subnet address and the last 9 bits form the host address. What is the subnet mask? Answer:

  Binary Dotted Decimal
Address 10000011.00000101.11111100.00010011 131.5.252.19
Subnet Mask 11111111.11111111.11111110.00000000 255.255.254.0
Subnet Address 10000011.00000101.11111100.00000000 131.5.252.0

How many host addresses are there on the subnet? Answer: 29 = 512.

What is the range of host addresses for this subnet. Ans: The bits in the address that correspond to a 1 in the subnet mask are fixed, but the bits in the address that correspond to a 0 in the subnet mask are free to vary. The smallest possible value is to make all the bits that are free to vary 0. The largest value is to make all the bits that are free to vary 1. Hence the smallest value is
10000011.00000101.11111100.00000000 = 131.5.252.0
and the largest value is 10000011.00000101.11111101.11111111 = 131.5.253.255

The Bitwise And Operation

If the bit 0 represents False and the bit 1 represents True, then recall this definition of the And operation:

Input 1 Input 2 Result
0 0 0
0 1 0
1 0 0
1 1 1

The bitwise And operation on two binary values is computed by performing an And operation on corresponding bits in parallel:

11001001
10011011
========
10001001

Here is how it can be broken down:

Column
1 2 3 4 5 6 7 8
Input 1
1 1 0 1 1 0 1 1
Input 2
1 0 0 1 1 0 1 1
Result
1 0 0 0 1 0 0 1

Notice that in Examples 1 and 2, the network address equals the bitwise and of the host address with the subnet mask.

Routing Tables

A router contains an internal table called a routing table that tells the router how to forward Level 3 packets. In Example 3, we discuss a simplified routing table that contains the fields Subnet Mask, Subnet Address Address, and Next Hop Address. After this example, we list some of the other fields that are included in an actual routing table. Example 3:

Suppose that these are the entries in an example routing table.

Subnet Mask Network Address Next Hop Address
255.255.255.240 192.17.7.208 192.12.7.15
255.255.255.240 192.17.7.144 192.12.7.67
255.255.255.0 192.17.7.0 192.12.7.251
0.0.0.0 0.0.0.0 192.17.7.1

Suppose the router receives an IP packet with the destination address 192.17.7.164. Here is how the next hop address is determined.

Step 1. Compute the bitwise and of the destination address and the subnet mask. If the result is equal to network address in that row of the routing table, a match is found and the packet is forwarded to the next hop address in that row. If no match is found, the process is continued with the next row in the routing table.

  Dotted Decimal Binary
Destination Address192.17.7.164 11000000.00010001.00000111.10100100
Subnet Mask 255.255.255.240 11111111.11111111.11111111.11110000
Result 192.17.7.160 11000000.00010001.00000111.10100000
Network Address192.17.7.144 11000000.00010001.00000111.10010000

The result and the network address do not match, so continue to Step 2 where we repeat the same procedure with the second row of the routing table. Step 2.
  Dotted Decimal Binary
Destination Address192.17.7.164 11000000.00010001.00000111.10100100
Subnet Mask 255.255.255.240 11111111.11111111.11111111.11110000
Result 192.17.7.160 11000000.00010001.00000111.10100000
Network Address192.17.7.208 11000000.00010001.00000111.11010000

Again there is no match, so we continue to Step 3.

Step 3.
  Dotted Decimal Binary
Destination Address192.17.7.164 11000000.00010001.00000111.10100100
Subnet Mask 255.255.255.0 11111111.11111111.11111111.00000000
Result 192.17.7.0 11000000.00010001.00000111.00000000
Network Address192.17.7.0 11000000.00010001.00000111.00000000

Now the result and the network address match, so the packet is forwarded to the next hop address 192.12.7.251.

Notice that we will always have a match for the last row of the above routing table. Why?

Other fields in a more realistic routing table include

A static routing table is a table that does not change, for example, a table that is created by the network administrator. A dynamic routing table is a table that is changed by a routing algorithm, discussed in the next section.

Routing Algorithms

Three of the most popular routing algorithms are the following.

The ARP Protocol

When a packet finally arrives at its final router hop, the packet must complete its final journey to the host via Ethernet or some other Level 2 protocol. How can the router discover the MAC address of the host when only the destination IP address is containted in the packet? The answer is the ARP protocol. The router sends out an ARP query packet which contains this information:

Query MAC Sender Address IP Sender Address                                   IP Target Address

The target host recognizes its IP address in the ARP packet, fills in its MAC Address, and changes the first field to Answer, and sends the ARP packet back:

Answer MAC Sender Address IP Sender Address MAC Target Address IP Target Address

The router receives the answer, then encapsulates the original IP packet as an Ethernet frame, and forwards it to the target host.