IP Addressing and subnets

First thing you should know is that the Internet Protocol (IP) uses logical addressing to transport a packet from it's source to destination. That's the principle on which the Internet works and can be easily compared with a classical postal service, where every letter needs a sender and a receiver on the envelope, with detailed information on area codes, city, street number, etc.

An IP address (referring to IPv4) consists of a string of 32 bits. For the end user, these 32 bits are organized in 4 bytes represented in a system called "dotted decimal". This means that the bytes are marked with points between, like 192.168.0.1.

I. Network Math

Before talking about addressing schemes, you should know the basic math involved in IP networking.

A) Binary to decimal conversion

To fully understand these kind of transformations, you should firstly know these values:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
(will refer to this as Table A.)

Now how do we transform binary to decimal?

Let's take the number 10100110 which is in binary (ones and zeros) and transform it to decimal.

We just take all values and form a table, starting with 2^0 (lowest value) from the right position of our binary number. (you'll do this only the first times, after some practice you'll do it mentally with no problems):

1 0 1 0 0 1 1 0
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

Now we count only the values with a one (1) above and calculate their sum.
The result is 2^7+2^5+2^2+2^1=(using Table A.)=128+32+4+2=166
So 10100110 (binary) = 166 (decimal).


B) Decimal to binary conversion

For this transformation you also need the values in Table A.

Let's take for example the number 240 and try to find out the binary equivalent.
The main idea behind this is to try to write the decimal number as a sum of numbers in Table A. (exponential powers of 2).
The easiest way is this:

Find the highest number that can be written as a power of 2 that is smaller or equal then our decimal number (from Table A).

256 is the highest, but its bigger than our number (240) so it's not good. Next is 128 which matches our criteria.

Subtract 128 from 240: 240 - 128 = 112
Note the number 128 down.

Now do the same for 112. Find the highest number in Table A smaller then 112. This value will be 64.
Subtract: 112 - 64 = 48.
Note the number 64 down.

Find the highest value smaller than 48 in Table A. It's 32.
Subtract: 48 - 32 = 16
Note 32 down.

Now 16 is a an exponential power of two, it's the highest value in Table A smaller or equal to 16 so we stop. Note 16 down too.

Put all numbers noted down:
128+64+32+16=240 our number. We managed to write our decimal number as a sum of exponentials powers of 2.

Now write all values from 2^0 to 2^7 (one byte long data). Write the decimal equivalent of our noted down numbers, and put ones (1) under them. Put zeros under the numbers we didn't noted down.

2^7(128) 2^6(64) 2^5(32) 2^4(16) 2^3 2^2 2^1 2^0
1 1 1 1 0 0 0 0

The number in ones and zeros you obtains is the binary equivalent of the decimal.
SO 240 (=2^7+2^6+2^5+2^4) = 11110000.

When working with IP addresses, we work with 4 bytes of data (32 bits).
This means that an IP address in binary (as a computer sees it) looks like this:

11000101101001010011101110101000.

If we make a binary dotted notation, it would look like this:

11000101.10100101.00111011.10101000


To transform an address from binary to decimal (or the opposite), we take each 8 bit group and work like in A) or B).

11000101=2^7+2^6+0+0+0+2^2+0+2^0=128+64+4+1=197.
So the first part of the dotted decimal would be 197.
10100101=2^7+0+2^5+0+0+2^2+0+2^0=128+32+4+1=165.
So the first two parts are 197.165.
00111011=(................................)=59.
10101000=(................................)=168.
And so we get the whole IP address in dotted decimal 197.165.59.168.

C) The ANDing

The third (and last, don't worry) math mechanism you should know is something called logical ANDing. It's part of Boolean Algebra.
It sounds complicated, but it's a very simple rule. Just keep the following three combinations in mind:

1 AND 1 = 1
1 AND 0 = 0
0 AND 0 = 0.

This will get handy when trying to calculate the address of a network.


II. Addressing standards: IP classes

The first thing you should know is that an IP address is made up from two logical parts: the network and the host part.
As an analogy, the network part is like the street name is postal service and the host part is like house numbers.

When a postman delivers a letter, it's hard to see where's the number let's say 32 in all the city. It's rather impossible and will lead to failure in delivery. Instead, the postman finds the street and then goes to the house number on that specific street.
This is true in networking, too. The routers find network addresses based on the network bits in the IP address and send the packet to that "street" to a specific host. It's easier and more efficient this way.

So how many bits are for network and how many bits for host? These are categorized in some IP classes:

Class A: Includes all the IP addresses from 1.0.0.0 to 126.255.255.255. The first 8 bits are Network bits and the rest of 24 are host bits. (a network mask of 255.0.0.0)

Class B: Includes all the IP addresses from 128.0.0.0 to 191.255.255.255. The first 16 bits are Network bits and the rest of 16 are host bits. (a network mask of 255.255.0.0)

Class C: Includes all the IP addresses from 192.0.0.0 to 223.255.255.255. The first 24 bits are Network bits and the rest of 8 are host bits. (a network mask of 255.255.255.0)

These are the usable spaces.

There are several special classes:

Class D: 224.0.0.0 - 239.255.255.255 - Reserved for multicasting

Class E: 240.0.0.0 - 254.255.255.255 - Reserved for testing and experimental purposes.


The range 127.0.0.0 - 127.255.255.255 is reserved for loopback addresses for devices.


We also have some private IP addresses (NON routable on the Internet - this means any ISP will drop you packets if you want to get on the Internet with a private address). They are used for private networking (closed LANs), using Network Address Translation to get on the Internet (sharing a public address).
These are:

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255



The network mask actually marks the network and host bits. If we take an IP address (in binary) like


0000 1010.1100 1101.1010 0100.1011 1111
1111 1111.0000 0000.0000 0000.0000 0000 and a network mask in binary (255.0.0.0 - Class A).


We see that 255.0.0.0 means 11111111 and 24 zeros. This means that the first bits set to 1 (8 bits) mark network bits in the IP above. That's why any IP with a mask address of 255.0.0.0 has the first 8 bits as a network part of the address.

This binary representation leads to another notation of the network mask called CIDR (Classless Interdomain Routing). Using this notation we write only how many network bits we have in the IP address. Like 10.12.40.0/8 - this means we have a network mask of 255.0.0.0 = 8 network bits.

160.17.40.0 /16 = a network mask of 255.255.0.0 = 16 network bits and so on.

Routers use this to find out network address to forward packets. But how does a router finds a network address? By using the logical ANDing presented earlier.

Let's say we have the IP address of 10.40.17.4/8

The network address of this address is the result of ANDing the ip address in binary and the network mask in binary. /8 means we have 8 network bits = 255.0.0.0

NNNN NNNN HHHH HHHH HHHH HHHH HHHH HHHH (N= Network bit/ H= Host bit)
0000 1010.0010 1000.0001 0001.0000 0100 = 10.40.17.4
1111 1111.0000 0000.0000 0000.0000 0000 = 255.0.0.0
---------------------------------------- We compare using ANDing bit by bit
0000 1010.0000 0000.0000 0000.0000 0000 = 10.0.0.0 = the network address

Now if we take another IP address in Class A with the default network mask of /8 we'll see something strange:

0000 1010.0000 0001.0000 0010.0000 0100 = 10.1.2.4
1111 1111.0000 0000.0000 0000.0000 0000 = 255.0.0.0
---------------------------------------
0000 1010.0000 0000.0000 0000.0000 0000 = 10.0.0.0 network address

The network addresses for these two IPs are the same. Why? because by using only first 8 bits for network, only the 10 (first 8 bits in the IP) counts in the logical ANDing.
Now if a router has a route to 10.40.17.4/8 and a route to 10.1.2.4/8 and a packet arrives for the IP address of 10.40.17.4/8, the router cannot find it's network address properly. It finds it as duplicate, thinks that both routes are working to that host and does a load balancing with undesired effects, loosing all the data.

This is why we use subnetting.

III. Subnetting IPv4 networks


Subnetting means "borrowing" host bits and transforming them in subnetwork bits. We divide large networks in smaller logical subnets.
Let's take the example above. We have 10.40.17.4 and 10.1.2.4. We see that the first difference occurs in the second octet of th IP address (40 != 1). So if we use a mask of /16 instead of /8, it should do the trick:

0000 1010.0010 1000.0001 0001.0000 0100 = 10.40.17.4
1111 1111.1111 1111.0000 0000.0000 0000 = 255.255.0.0
--------------------------------------- Logical ANDing
0000 1010.0010 1000.0000 0000.0000 0000 = 10.40.0.0 = the network address

The network address (using the same principle) for 10.1.2.4/16 would be 10.1.0.0, creating a different path. A router can now tell where is the first host and where is the second.

Now instead of having network bits and host bits, we have the following:
NNNN NNNN SSSS SSSS HHHH HHHH HHHH HHHH
0000 1010.0010 1000.0001 0001.0000 0100 = 10.40.17.4
(N= Network bit/ S=Subnetwork bit/ H= Host bit)
We just "borrowed" 8 bits from the host to form a subnetwork.

Now let's do a simple exercise:
We have the IP address 10.40.17.4. Let's find the appropriate subnet mask for it to have a minimum of 255 usable hosts per subnet and a maximum number of subnets.

The address 10.40.17.4 is a Class A IP address. This means that we start with the first 8 bits as Network bits. If we need a minimum of 255 hosts, it's a good idea to start from this point.

Remember Table A?

To have 255 hosts, we need to find the power of smallest power of 2 bigger then 255. This will be 2^8 (=256), meaning that we need 8 host bits. (a subnet of 1111 1111.1111 1111.1111 1111.0000 0000 - 8 zeros to mark the host bits). Well, this is a big mistake, because we are talking about usable hosts. Why?
Because we cannot use the subnet address:
0000 1010.0010 1000.0001 0001.0000 0100 = 10.40.17.4
1111 1111.1111 1111.1111 1111.0000 0000 = 255.255.255.0
---------------------------------------
0000 1010.0010 1000.0001 0001.0000 0000 = 10.40.17.0 - subnet address

and we cannot use the subnet's broadcast address (the address to which if i send a package all hosts in the subnet accept it). The broadcast address is the subnet address with ones (1) in the host field.

Ex: 0000 1010.0010 1000.0001 0001.1111 1111 = 10.40.17.255

So we'll need to use 9 host bits to have at least 255 usable addresses (in fact we'll have 510 usable hosts, but this is the minimum in our requirements and includes 255).
NNNN NNNN SSSS SSSS SSSS SSSH HHHH HHHH
0000 1010.0010 1000.0001 0001.0000 0100 = 10.40.17.4
1111 1111.1111 1111.1111 1110.0000 0000 =
255.255.254.0
---------------------------------------
Subnet Mask 0000 1010.0010 1000.0001 0000.0000 0000 = 10.40.16.0 = network address
0000 1010.0010 1000.0001 0001.1111 1111 = 10.40.17.255 = broadcast address

Note the difference between the third part of the network address and broadcast address. The usable IP addresses in our case are from 10.40.16.1 to 10.40.17.254.

So how many subnets can we use?

We have 15 subnet bits = 2^15 subnets (this is pretty large).
The first subnet (with all subnet bits set to zeros) is called Subnet Zero and it's usable by enabling a service on routers. When resolving a subnet exercise, keep in mind that subnet zero is usually used but not always. The requirement of the exercise should state if we use IP subnet zero or not.
We also have a broadcast subnet that is represented by all ones (1) in the subnet bits. This subnet is not usable.


Now let's see a kind of exercise that could trick you.

Let's say we use our IP address of 10.40.17.17 with a subnet mask of /28. What is the broadcast address and what is the usable IP range in this case?


NNNN NNNN SSSS SSSS SSSS SSSS SSSS HHHH
0000 1010.0010 1000.0001 0001.0001 0001 = 10.40.17.17
1111 1111.1111 1111.1111 1111.1111 0000 = 255.255.255.240 Subnet Mask (/28)
---------------------------------------
0000 1010.0010 1000.0001 0001.0001 0000 = 10.40.17.16
network addr 0000 1010.0010 1000.0001 0001.0001 1111 = 10.40.17.31


Note that both network address and broadcast address for our case are not typical (.0 and .255).
The usable IP range is 10.40.17.17 to 10.40.17.30.

Source: http://www.tutorial5.com/content/view/89/79/
broadcast addr

0 comentarios: