Chapter 3

3. Subnetting

Learn about subnets and how to do subnet arithmetic!

Subsections of 3. Subnetting

1. IPv4

Lesson Content

So we know that network hosts have a unique address they can be found at. These addresses are known as IP addresses. An IPv4 address looks something like this:

204.23.124.23

This address actually contains two parts, the network portion that tells us know network it’s on and the host portion that tells us which host on that network it is. For this course we will mostly be discussing IPv4 addresses, which are what you commonly will see when referring to IP addresses.

An IP address is separated into octets by the periods. So there are 4 octets in an IPv4 address. If you know a bit of computer science, an octet is 8 bits and 8 bits actually equal 1 byte, so we also refer to an IPv4 address as having 4 bytes. We use bits frequently when dealing with subnets and IP addresses.

You can view your IP address with the ifconfig -a command:

pete@icebox:~$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr 1d:3a:32:24:4d:ce  
          inet addr:192.168.1.129  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fd60::21c:29ff:fe63:5cdc/64 Scope:Link

As you can see my IPv4 address is: 192.168.1.129

Exercise

Find your IP address with ifconfig.

Quiz Question

# How many bytes are in an IPv4 address? > An IPv4 address is 32 bits. An IP Address is shown as 4 decimal numbers representing 4 bytes: d.d.d.d where d = decimal number (0 - 255). High order bits are the network identifier and lower order bits are the host identifier. 1. [ ] 32 2. [ ] 16 3. [ ] 8 4. [x] 4

2. Subnets

Lesson Content

How can I tell if I’m on the same network as Patty? Well we can just look at the subnet short for subnetwork. A subnet is a group of hosts with IP addresses that are similar in a certain way. These hosts usually are in a proximate location from each other and you can easily send data to and from hosts on the same subnet. Think about it as sending mail in the same zip code, it’s a lot easier than sending mail to a different state.

For example, all hosts with an IP address that starts with 123.45.67 would be on the same subnet. My host has an IP of 123.45.67.8 and Patty’s has an IP of 123.45.67.9. The common numbers are my network prefix and the 8 and 9 are our hosts, therefore my network is the same as Patty’s. A subnet is divided into a network prefix, such as 123.45.67.0 and a subnet mask.

Subnet Masks

Subnet masks determine what part of your IP address is the network portion and what part is the host portion.

A typical subnet mask can look something like this:

255.255.255.0

The 255 portion is actually our mask. To make this a little easier to understand, remember how we refer to each octet as 8 bits? In computer science a bit is denoted by a 0 or a 1 in binary form. When binary numbers are used, 1 means on and 0 means off. So what does 8 0’s or 1’s equal?

Punch into Google “binary to decimal calculator” and convert 11111111 into a decimal form. What do you get? 255! So an octet ranges from 0 to 255. So if we had a subnet mask of 255.255.255.0, and an IP address of 192.168.1.0, how many hosts are on that subnet? We’ll find out the answer to that in our subnet math lesson.

Also when we talk about our subnet, we commonly denote it by the network prefix followed by the subnet mask:

123.234.0.0/255.255.0.0

Why?

Why on earth do we make subnets? Subnetting is used to segment networks and control the flow of traffic within that network. So a host on one subnet can’t interact with another host on a different subnet.

But wait a minute, what if I want to connect to other hosts like yahoo.com? Then you need to connect subnets together. To connect subnets you just need to find the hosts that are connected to more than one subnet. For example, if my host at 192.168.1.129 is connected to a local network of 192.168.1.129/24 it can reach any hosts on that network. To reach hosts on the rest of the Internet, it needs to communicate through the router. Traditionally, on most networks with a subnet mask of 255.255.255.0, the router is usually at address 1 of the subnet, so 192.168.1.1. Now that router will have a port that connects it to another subnet (more in the Routing course). Certain IP addresses (private networks) are not visible to the internet, and we have things like NAT in place (more on this later).

Exercise

Use ifconfig to view your subnet mask.

Quiz Question

# True or false, a subnet consists of a subnet mask and network prefix. > Subnet masks (IPv4) and prefixes (IPv6) identify the range of IP addresses that make up a subnet, or group of IP addresses on the same network. For example, a subnet can be used to identify all the machines in a building, department, geographic location, or on the same local area network (LAN). 1. [ ] False 2. [x] True

3. Subnet Math

Lesson Content

Ok, we know that subnet masks are important to figure out how many hosts we can have on our subnet. So how many hosts would that be?

Let’s say I have an IP address of 192.168.1.0 and a subnet mask of 255.255.255.0, now let’s line up these numbers in binary form. For now use an online calculator to convert these values from decimal to binary.

192.168.1.165  = 11000000.10101000.00000001.10100101
255.255.255.0  = 11111111.11111111.11111111.00000000

The IP address is masked by our subnet mask, when you see a 1, it is masked and we pretend like we don’t see it. So the only possible hosts we can have are from the 00000000 region. Remember 11111111 in binary form equals 255, we also account 0 as a host number, so there are 256 possible options. However, it may look like we have 256 possible options, but we actually subtract 2 hosts because we have to account for the broadcast address and the subnet address, leaving us with 254 possible hosts on our subnet. So we know that we can have hosts with IP addresses ranging from 192.168.1.1 - 192.168.1.254.

Exercise

No exercises for this lesson.

Quiz Question

# What is the binary equivalent of 255? > - Divide 255 by 2. Use the integer quotient obtained in this step as the dividend for the next step. Repeat the process until the quotient becomes 0. Write the remainder from bottom to top i.e. in the reverse chronological order. This will give the binary equivalent of 255. > - | Devidend | Remainder | |:-----------:|:---------:| | 255/2 = 127 | 1 | | 127/2 = 63 | 1 | | 63/2 = 31 | 1 | | 31/2 = 15 | 1 | | 15/2 = 7 | 1 | | 7/2 = 3 | 1 | | 3/2 = 1 | 1 | | 1/2 = 0 | 1 | 1. [ ] 00000000 2. [ ] 11101101 3. [ ] 01010101 4. [x] 11111111

4. Subnetting Cheats

Lesson Content

I hate to have to add this section, in the real world you would most likely never have to do subnet math by hand, however if you were getting interviewed on this, you’ll have to know how to convert to and from binary form for subnetting. Luckily there are some arithmetic cheats you can memorize.

First memorize your base-2 calculations, just do it:

  • 2^1 = 2
  • 2^2 = 4
  • 2^3 = 8
  • 2^4 = 16
  • 2^5 = 32
  • 2^6 = 64
  • 2^7 = 128
  • 2^8 = 256
  • 2^9 = 512
  • 2^10 = 1024
  • 2^11 = 2048
  • 2^12 = 4096

Decimal to Binary Chart

1   1  1  1  1 1 1 1
128 64 32 16 8 4 2 1

There are lots of reasons why the following chart looks the way it does, if you’re curious how it works there are lots of resources online.

Ok, got these memorized? Let’s do a quick decimal to binary conversion:

Convert 192.168.23.43 to Binary

Remember: 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1

Let’s walk through converting the first octet to binary and you’ll understand how the rest works.

  1. Can you subtract 192 - 128? Yes, so the first bit is 1
  2. 192 - 128 = 64, the next number in the chart is 64, can you subtract 64 - 64? Yes, so the second bit is 1
  3. We’ve run out of numbers to subtract from, so our binary form of 192 is 11000000

Convert Binary 11000000 to Decimal

For binary to decimal conversion you add up the numbers that have a 1, so:

128 + 64 + 0 + 0 + 0 + 0 + 0 + 0 = 192!

Exercise

Look at your IP address and subnet mask and see how many hosts you can have on your subnet.

Quiz Question

# What is the binary conversion of 123? > - Divide 123 by 2. Use the integer quotient obtained in this step as the dividend for the next step. Repeat the process until the quotient becomes 0. Write the remainder from bottom to top i.e. in the reverse chronological order. This will give the binary equivalent of 123. > - | Dividend | Remainder | |:----------:|:---------:| | 123/2 = 61 | 1 | | 61/2 = 30 | 1 | | 30/2 = 15 | 0 | | 15/2 = 7 | 1 | | 7/2 = 3 | 1 | | 3/2 = 1 | 1 | | 1/2 = 0 | 1 | Quiz Answer 1. [ ] 0000000 2. [ ] 1110110 3. [ ] 0101010 4. [x] 1111011

5. CIDR

Lesson Content

CIDR (classless inter-domain routing) is used to represent a subnet mask in a more compact way. You may see subnets notated in CIDR notation, where a subnet such as the 10.42.3.0/255.255.255.0 is written as 10.42.3.0/24 which just means it includes both the subnet prefix and the subnet mask.

Remember an IP address consists of 4 bytes or 32 bits, CIDR indicates the amount of bits used as the network prefix. So 123.12.24.0/23 means that the first 23 bits are used. Well what does that mean? How many hosts is that?

A simple trick is to subtract the total of bits an IP address can have (32) from the CIDR address (23), so that leaves 9 bits, 2^9 = 512, but we have to remove 2 addresses (subnet address and broadcast address) so we have 510 usable hosts.

Exercise

No exercises for this lesson.

Quiz Question

No questions move along!

6. NAT

Lesson Content

We’ve brought up NAT (network address translation) before but didn’t touch upon it, when we are working on our network, does that mean that the Internet can see our IP address? Not quite.

NAT makes a device like our router act as an intermediary between the Internet and private network. So only a single, unique IP address is required to represent an entire group of computers.

Think of NAT is like a receptionist in a large office, if someone wants to contact you, they only know the number to the whole office, the receptionist would then have to look for your extension number and forward the call to you.

How does it work?

A simple case would look like this:

  1. Patty wants to connect to www.google.com, so her machine sends this request through the router
  2. The router takes that request and opens its own connection to google.com, then it sends Patty’s request once it makes a connection
  3. The router is the intermediary between Patty and www.google.com. Google doesn’t know about Patty instead all it can see is the router.

NAT and packet routing in general can get pretty ugly, but we won’t dive into the specifics.

Exercise

No exercises for this lesson.

Quiz Question

# What is used to represent a single private address to the Internet? >NAT stands for network address translation. It's a way to map multiple private addresses inside a local network to a public IP address before transferring the information onto the internet. Organizations that want multiple devices to employ a single IP address use NAT, as do most home routers. 1. [ ] Port 2. [ ] MAC 3. [ ] IP 4. [x] NAT

7. IPv6

Lesson Content

We’ve heard the term IPv6 here and there, but what is it? Every device that connects to the Internet gets it’s own IP address, well that happens to be a finite number that we are soon approaching in this digital age. IPv6 was created to allow us to connect more hosts to the Internet, it comes with more IP improvements however, it’s adoption is quite slow. It isn’t meant to replace IPv4, they are meant to complement each other. The two IP protocols are very similar and if you know IPv4 you’ll understand IPv6, the major difference is the way the address is written. Here is what a typical IPv6 address looks like:

2dde:1235:1256:3:200:f8ed:fe23:59cf

Exercise

Check ifconfig to see if you have an IPv6 address listed.

Quiz Question

# What IP address is used to help increase the number of hosts that can connect to the Internet? > IPv6 is the newest version of internet protocol formulated by the IETF, which helps identify and local endpoint systems on a computer network and route online traffic while addressing the problem of IPv4 address depletion due to prolonged internet use worldwide. 1. [ ] IPv4 2. [x] IPv6