Saturday, June 19, 2010

subnet tutorial

Subnetting
Subneting is the procedure to create logical sub-networks of a given network using bits assigned to the host address space. Obviusly, subnetting therefore, reduces the address space for hosts as each subnet also needs to reserve addresses for network and broadcast.

Lets take the example of a class C network address. A class C network address is in the form of n.n.n.h, implying that the first three octets of the 32 bit IP address is used to refer to the network and the last 8 bit octet is used to represent the host. therefore, a class C network can address 256 no of hosts.

Subnet Mask


Subnet mask is a 32 bit number, which helps to identify the network portion in an IP address. This is done by bit anding the subnet mask with the IP address. The default subnet mask for a class C address is 255.255.255.0, this mask implies that no subnets have been created for the IP address, and the entire host address space ( 256) is under one network.

Creation of subnet masks other than default subnet masks for class C nework:-

x x x x x x x x denotes 8 bits of the host address space.

1 0 0 0 0 0 0 0 -> 128

1 1 0 0 0 0 0 0 -> 192

1 1 1 0 0 0 0 0 -> 224

1 1 1 1 0 0 0 0 -> 240

1 1 1 1 1 0 0 0 -> 248

1 1 1 1 1 1 0 0 -> 252

1 1 1 1 1 1 1 0 -> 254

Notice that, there is no zero between the ones, this is one rule of subnet masking. It is quite simple for class C addresses, but for class A and B address, it may get a bit unwieldy as the number of bits in the host portion are more.If you keep this rule of continous ones as above, it should be easy enough to list out the subnets for class A and B as well.

The last subnet mask is not used as it leaves just one bit for defining hosts and none for broadcast address and leaves you with many subnets without any hosts.

Finding out the network address from an IP address.


IP address 10.205.15.20
Subnet mask : 255.255.255.192

Bit AND result : 10.205.15.0

So, host number 20 is part of the 0 subnet and its the 20th host in the 0 subnet.

Another example:

IP address : 10.205.15.130
Subnet mask : 255.255.255.192
Bit AND result : 10.205.15.128
so, it is the 2nd host in 128 subnet

Finding out the subnets in an network, given the subnet mask


Suppose, the network given is 10.205.15.34
and subnet mask is 255.255.255.192

1. The number of subnets is 2^x (not applicable to 128 mask) where x is the number of masked bits. masked bits are the bits which are 1 in the host portion

The no of masked bits in 192 mask is 2. so, there are 4 subnets. Out of these 4 subnets , one subnet is the 0 subnet (00) and one is the 1 subnet (11). Earlier these two subnets were not considered as subnets, but from a new RFC released in 1995, 0 and 1 subnet is also allowed.


so the subnets are :

0 0 0 0 0 0 0 0 -->0

0 1 0 0 0 0 0 0 --> 64

1 0 0 0 0 0 0 0 --> 128

1 1 0 0 0 0 0 0 --> 192

Since the no of possible subnets using the 192 mask is given above, we can write out the network, first host, last host and the broadcast address of these subnets.

subnet 0

first host-1
last host- 62
broadcast-63
No of hosts=62
subnet 64


first host-65
last host- 126
broadcast-127
No of hosts=126-65=62
Subnet 128


first host-129
last host- 190
broadcast-191
No of hosts=190-129=62
Subnet 192

first host-193
last host 254
broadcast 255

No of hosts=254-193=62

So, total no of hosts in these 4 subnets is 62X4=248

had we used the default subnet mask of 255.255.255.0, we would have had 254 hosts. so we loose six addresses for defining the network and broadcast address for the four subnets.

Finding the no of subnets for a given mask (easier method)



for example you want to find out the number of subnets in 252 subnet mask

1 1 1 1 1 1 0 0 -> 252

subtract the subnet mask from 256 and add the result to itself till you reach the subnet mask.

256-252=4
so the subnets are 4, 8,12,16,20,24,28,so on upto 252.

No of hosts per subnet is 2^ (no of unmasked bits)-2
so, in this case it is 2^2-2=2

CIDR notation


classless internet routing domain represents IP address and subnet mask in a new notation:-

10.205.15.30/24 implies that 24 bits are masked, so this is a class C IP address, with no subnets (255.255.255.0). 255 is 8 nos of 1. so, 3x8=24

10.205.15.30/25 implies one bit of the host is masked so the subnet mask is 255.255.255.128

10.205.15.30/26 denotes 255.255.255.192 and so on.