IP Addressing and subnets |
| Written by Michael D. | |
|
Learn about IP addressing and subnetting in a complete tutorial
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 broadcast addr 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.
written by ganesh ukey , September 02, 2007
plese tell me how i find ip address of my friend when i wiill online?
written by Mihai Dobos , September 02, 2007
Hello I don't see any reason for you to know a friend's IP address when he gets online, except to start a denial of service or so. If you want an answer, be more specific and give some reasons please. Best regards, Michael D.
written by Anita , October 10, 2007
Given a /29 with 30 networks, 6 host and the networks will be multiples of 8, what is the easiest way to calculate the 23rd network without having to write them all out by hand, and not use a subnet calculator? Also what is a fast way to find the 4th host on the 23rd network? Thank you.
written by Mihai Dobos , October 10, 2007
Hello Write all numbers in binary; For the Xth subnetwork, take the subnet bits and start counting (see 23 in binary and apply it in the subnetting part of the address). Take a look if you consider subnet "all zeros" usable or not (depends on a service). For the host part, do the same but consider only the last 3 host bits (see 4 in binary and apply it in the host part of your 23rd network to get the whole address). Try to do your homework by yourself, practice makes perfect :). Mihai D.
written by gaigoi113 , October 26, 2007
I taking CCNA class now and this tutor really help me understand the IP/Subnet addressing materials. Thanks
written by Lalit , October 28, 2007
Hello I couldn't understand, why first and last subnets are not usable. I can understand about the first and last host address in the subnet are not usable because it is loopback and brodcast addresses. can you please help me.
written by Mihai Dobos , October 28, 2007
Hello Lalit The first subnet represents the subnetwork address, resulted from the ANDing between an IP address in that range and the subnet mask. This identifies the subnetwork as a layer 3 address for the routers. The last subnet is the broadcast address; if you want to send a packet to all hosts in the subnetwork, you will send it to the broadcast address. Mihai D.
written by Patrick Morechild , November 14, 2007
Hi I?m taking a network class. We are working on subnetting. We were told to research ?why all ones and all zeros can?t be used in an subnetting address?. From what i understand, they are reserved. why I this? Can you recommend any cites that can elaborate on this. Thanks Patrick M
written by fazal khan , October 14, 2008
i am really interested in to have some information about networking world because now adays it is really important especially for information technology poeples.
written by dinaom , October 31, 2008
yes i wnt to know more like the detail how to calculate binary and decimal
written by htetnaing , November 19, 2008
I'm confused about the last exercise on this article. 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 broadcast addr 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. I've calculated for many times and the network address is 10.40.17.32. If we perform logical AND with the given ip address which is 10.40.17.255 (00001010.00101000.00010001.00010001) with /28 subnet mask which is 11111111.11111111.11111111.11110000 then I got 00001010.11111111.11111111.11110000 for network address or m i wrong or is there any other reason because it's a tricky question ?? I'm confused please help me out of this.
written by hishaam , December 20, 2008
hallo ir I don't get how culculation works please teach me more. I mean if my Ip addresss is 10.40.17.32 My question is whee does 10. CAME FROM, 40. CAME FROM 17.AND 32 PLS EXPLAIN THANK YOU IN ADVANCE
written by lismonkey , February 18, 2009
I've been reading through the information on the forum, and it has been very helpful. I don't want to sound mean or anything. I think many individuals on here need to work on their spelling and grammer far more than their conceptual knowledge of networking.
written by Mike Albury , February 26, 2009
lismonkey, Dont forget that there are some people whose official language is not English, so dont sound insulting.
written by DarkMoon , May 20, 2009
Variable Length Subnet Mask, should mention about it either^^. Cheer! Your article is marvelous.
written by vinh , July 13, 2009
Can somebody help me to explain how we can calculate : $ipcalc 192.168.1.1 - 192.168.1.30 deaggregate 192.168.1.1 - 192.168.1.30 192.168.1.1/32 192.168.1.2/31 192.168.1.4/30 192.168.1.8/29 192.168.1.16/29 192.168.1.24/30 192.168.1.28/31 192.168.1.30/32 I mean the algorithm to calculate it. Thank so much
written by Kedibone , July 21, 2009
Can you show me the subnet, broadcast and host range of the following : 1.207.19.24.0/27 2.209.42.21.0/29 3.142.31.0.0/21
written by Mee , November 21, 2009
You already show the subnet - /27 it's 255.255.255.224 = 255.255.255.11100000. 5 0's at the end of the subnet so there are 32 - 2 possble hosts since 2 to the power of 5 is 32 - the broadcast address and netword id. Add 32 to 0 to get the next subnet...so this is 207.19.24.32, next subnet is 207.19.24.64 and so on. The broadcast is the last address in the subnet so this would be 207.19.24.31. The last usable host therefore is 207.19.24.30 (handy to know that all broadcasts are odd numbers and all network IDs are even). So the last usable address is 207.19.24.30 and the 1st usable address is 207.19.24.1 (since 207.19.24.0 is the netword id) so the range is 207.19.24.1 - 207.19.24.30. I'm sure you can work out the rest yourself.
written by sulakshana , December 01, 2009
How we can select the network ID?
written by ashhh , January 21, 2010
hi, i am confused about distributing ip in subnets,suppose if i m subnetting 192.168.1.1/26,then block size will be of 64-2=62 host per subnet,then range in blocks will be 192.168.1.1 192.168.1.2 . . . 192.168.1.62------broadcast ip 192.168.1.63.....next block's network ip 192.168.1.64.....host ip is this configuratin is correct.plz suggest me regards ashhh
written by birju baxi , January 31, 2010
suppose you are the network manager for central university.A medium size university with 13,000 students.The university has 10 separate colleges(e.g. business,art,journalism),3 of which are relatively large(300 faculty and staff memebers,2000 students and 3 buildings) and 7 of which are relatively small(200 faculty and staff , 1000 stdents and 1 building).In addition there are another 2000 staff members who work in various administration department (e.g. library,maintenance,finance)spread over another 10 buildings.There are 4 resience halls that house a total of 2000 students.suppose university has the 128.100.XXX.XXX address reange on the Internet.How would you assign the IP address to the various subnets? How would you control the process bu which IP address are assigned to individual computers? you will have to make some assumptions to answer both questions so be sure to stable you assumptions.
written by abdu , February 07, 2010
i hope from you to help me to pass cisco exam 640-802 Do you need more help? Ask now!
|
|
| Last Updated ( Saturday, 23 June 2007 ) |