SEN2008 · Week 5 · Network Layer Concepts (I): , internetworking, Internet Protocol (IP, IPv6)

IPv4 addressing, subnets and CIDR

Finding the network address, broadcast, host range and number of hosts from an IP address bit by bit; subnetting, VLSM and longest prefix match. A guaranteed calculation question on the midterm.

IPv4 addresssubnet maskprefix lengthCIDRnetwork addressbroadcast addressVLSMroute aggregationlongest prefix matchprivate address

What takes a packet to the right place on the Internet is the IP address. Routers look not at the whole address but at its prefix. That is why the midterm almost always has questions like “what is the network address of this address, what is the broadcast, how many hosts are there, and which interface does it go to according to this table?” They all rest on the same few bit operations. Someone who knows these rules solves these questions in minutes:

  1. Network address = IP AND mask. The host bits are zeroed.
  2. Broadcast = all host bits set to 1. That is the network address + block size - 1.
  3. Number of hosts = 2h−22^{h} - 2, where hh is the number of host bits.
  4. Block size = 256 - the value of the mask octet. Boundaries are always multiples of this number.
  5. In routing, the longest prefix wins (longest prefix match).

The IPv4 address and the mask

An IPv4 address is a 32-bit number. To make it readable, it is split into four 8-bit parts (octets) and written in decimal: 192.168.10.77. An address consists of two parts: the network part and the host part. Interfaces with the same network part are on the same subnet and reach each other without a router.

The subnet mask tells where the split is. In the mask, the network bits are 1 and the host bits are 0. The short notation is the prefix length: /26 means “the first 26 bits are the network”.

PrefixMaskHost bitsUsable hosts
/8255.0.0.02416777214
/16255.255.0.01665534
/20255.255.240.0124094
/24255.255.255.08254
/26255.255.255.192662
/27255.255.255.224530
/30255.255.255.25222

In every subnet two addresses cannot be given to hosts: the network address, whose host bits are all 0, and the broadcast address, whose host bits are all 1.

CIDR: classless addressing

In the past, addresses were divided into classes (A: /8, B: /16, C: /24). An organization that wanted 2000 hosts did not fit in a class C, so it got a class B, and 63 thousand addresses were wasted. CIDR (Classless Inter-Domain Routing) removed these fixed boundaries: the prefix can be any length, written as a.b.c.d/x. Today the classes are only a historical term. If “classful” appears on an exam, the old system is meant.

Network address, broadcast, host range

Quick method: call the octet whose mask is neither 255 nor 0 the interesting octet. The block size is 256−mask256 - \text{mask}. Find the multiple of the block size that contains that octet of the address.

Example 1: 192.168.10.77/26

mask        255.255.255.192      block size 256-192 = 64 (4th octet)
multiples of 64: 0, 64, 128, 192   77 → between 64 and 127
network     192.168.10.64
broadcast   192.168.10.127
hosts       192.168.10.65  ...  192.168.10.126
number of hosts 2^6 - 2 = 62

The same thing at the bit level: 77 = 01001101. The mask 192 = 11000000. AND: 01000000 = 64.

Example 2: 172.16.45.200/20

The interesting octet is the third: the /20 mask is 255.255.240.0, with block size 16.

multiples of 16: 0, 16, 32, 48     45 → between 32 and 47
network     172.16.32.0
broadcast   172.16.47.255         (3rd octet 47, 4th octet always 255)
hosts       172.16.32.1  ...  172.16.47.254
number of hosts 2^12 - 2 = 4094

PITFALL · the fourth octet. If the interesting octet is the third, the fourth octet of the network address is 0 and that of the broadcast is 255. Someone who writes 172.16.45.200 and says “network 172.16.32.200” forgot to zero the host bits.

Dividing into subnets (subnetting)

If you borrow nn of the host bits for the network part, you get 2n2^n equal subnets.

Example 3: split 200.10.20.0/24 into 4 equal subnets. 22=42^2 = 4, so borrow 2 bits: /26. The block size is 64.

SubnetNetwork addressHost rangeBroadcast
1200.10.20.0/26.1 - .62.63
2200.10.20.64/26.65 - .126.127
3200.10.20.128/26.129 - .190.191
4200.10.20.192/26.193 - .254.255

Example 3b: split 172.16.0.0/16 into /22 subnets. The question usually asks for three things: how many subnets, how many hosts in each, and which one is the n-th subnet.

borrowed bits  22 - 16 = 6       → 2^6 = 64 subnets
host bits      32 - 22 = 10      → 2^10 - 2 = 1022 hosts
block size     256 - 252 = 4     (in the 3rd octet)
subnet 1       172.16.0.0/22
subnet 2       172.16.4.0/22
subnet 3       172.16.8.0/22
subnet 64      172.16.252.0/22

The third octet of the n-th subnet is (n−1)×4(n - 1) \times 4. Don’t forget to count from zero: subnet 3 starts at 8, not at 12.

VLSM: subnets of different sizes

In a real network, departments have different needs. VLSM (Variable Length Subnet Mask) gives each department the prefix that fits its need. Rule: start with the largest need, and make each subnet start at a multiple of its own block size.

Example 4: with 10.0.0.0/24, three LANs of 100, 50 and 20 hosts and a router link of 2 hosts.

100 hosts → 2^7-2=126 is enough → /25 → 10.0.0.0/25    (.1 - .126,   bc .127)
 50 hosts → 2^6-2=62  is enough → /26 → 10.0.0.128/26  (.129 - .190, bc .191)
 20 hosts → 2^5-2=30  is enough → /27 → 10.0.0.192/27  (.193 - .222, bc .223)
  2 hosts → 2^2-2=2   is enough → /30 → 10.0.0.224/30  (.225 - .226, bc .227)
first free address: 10.0.0.228

TIP · why do we start from the largest? If you start with a small one, a large block has to leave a gap to align itself to a multiple of its own size. Going from large to small, the blocks line up one after another with no gaps.

Route aggregation

The second gain of CIDR: advertising neighbouring networks with a single route. This is called route aggregation or supernetting. Router tables shrink.

Example 5: 198.51.96.0/24, .97.0/24, .98.0/24, .99.0/24 in one route.

 96 = 01100000
 97 = 01100001
 98 = 01100010
 99 = 01100011
      ^^^^^^      common first 6 bits
prefix = 16 + 6 = 22   →   198.51.96.0/22

Aggregation works cleanly only when the blocks are aligned and adjacent. You can’t aggregate 97 and 100 into a single /22.

Longest prefix match

Each row of a router’s forwarding table contains a prefix and an output interface. A destination address can match more than one row. The router chooses the longest prefix, because it is the more specific information.

11.20.0.0/16    → 0
11.20.64.0/18   → 1      (3rd octet 64 to 127)
11.20.72.0/21   → 2      (3rd octet 72 to 79)
0.0.0.0/0       → 3      (default)

11.20.75.9   → /16, /18, /21 match → longest /21 → 2
11.20.100.1  → /16, /18 match      → /18       → 1
11.20.70.3   → /16, /18 (not 72-79)→ /18       → 1
11.21.0.1    → only default        → 3

PITFALL · not the first match. It is wrong to read the table from top to bottom and pick the first row that matches. Find all the matches and look at the prefix length.

Private addresses and special-purpose blocks

RFC 1918 private addresses are not routed on the Internet. They are used in home and company networks, and are translated to a public address by NAT when leaving (next page).

BlockRangeNote
10.0.0.0/810.0.0.0 - 10.255.255.255large organizations
172.16.0.0/12172.16.0.0 - 172.31.255.255172.32 is outside
192.168.0.0/16192.168.0.0 - 192.168.255.255home routers
127.0.0.0/8loopback127.0.0.1 is yourself
169.254.0.0/16link-localwhen DHCP doesn’t answer

Where do addresses come from? A host usually gets its address through DHCP (Dynamic Host Configuration Protocol): the four steps discover, offer, request, ACK. An organization gets its address block from its ISP, and ISPs get theirs from regional registries (under ICANN).

Common mistakes

Wrong ideaCorrect
/27 → 32 hosts30, the network and broadcast addresses are subtracted
The /21 mask is 255.255.240.0It is 255.255.248.0; 240 is /20
Keeping the fourth octet in the network addressThe octets after the interesting octet become 0
Taking the start of the next block as the broadcastBroadcast = next network address - 1
In LPM, the first matching row in the tableThe longest prefix wins
172.32.x.x is a private address172.16/12 covers only 16 to 31
Starting VLSM with a small subnetAllocate from large to small, and alignment is not broken
Two addresses with different third octets are on different networksWith a /23 or shorter mask they can be on the same network

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

How many usable host addresses does a /27 subnet provide?

Show answer
30

The host part is 32−27=532 - 27 = 5 bits. There are 25=322^5 = 32 addresses. The first is the network address and the last is the broadcast; these two cannot be given to hosts: 32−2=3032 - 2 = 30.

Question 2easy

Write the prefix length /21 as a dotted-decimal subnet mask.

Show answer
255.255.248.0

21 ones: the first two octets are 16 bits (255.255), and 5 bits of 1 remain in the third octet: 11111000 = 128+64+32+16+8 = 248. The fourth octet is 0. The result is 255.255.248.0.

Question 3medium

What is the network address of the host 10.45.130.77/19?

Show answer
10.45.128.0

The /19 mask is 255.255.224.0. The interesting octet is the third, and the block size is 256−224=32256 - 224 = 32. The multiple of 32 that contains 130 is 128 (the block from 128 to 159). The fourth octet is zeroed: 10.45.128.0. The broadcast is 10.45.159.255.

Question 4medium

What is the broadcast address of the subnet containing 172.20.99.14/22?

Show answer
172.20.99.255

The /22 mask is 255.255.252.0, with a block size of 4 in the third octet. The block that contains 99 is 96 to 99. The network address is 172.20.96.0. The broadcast is the last address of the block: 172.20.99.255. The number of usable hosts is 210−2=10222^{10} - 2 = 1022.

Question 5easy

A company owns 192.0.2.0/24 and divides it into equal /28 subnets. How many subnets are created?

Show answer
16

4 bits were borrowed from the prefix: 28−24=428 - 24 = 4. The number of subnets is 24=162^4 = 16. Each carries 24−2=142^4 - 2 = 14 hosts.

Question 6medium

Which address is a valid (assignable) host address in 192.168.5.64/27?

  1. 192.168.5.64
  2. 192.168.5.95
  3. 192.168.5.90
  4. 192.168.5.96
Show answer
C) 192.168.5.90

The /27 block size is 32. The subnet runs from 192.168.5.64 to 192.168.5.95. 64 is the network address, 95 is the broadcast, and 96 is the network address of the next subnet. The host range is .65 to .94, so .90 is valid.

Question 7medium

A router has the forwarding table below. To which interface does it forward a datagram destined to 135.46.57.14?

135.46.56.0/22   eth0
135.46.60.0/22   eth1
135.46.56.0/21   eth2
0.0.0.0/0        eth3
  1. eth0
  2. eth1
  3. eth2
  4. eth3
Show answer
A) eth0

Find the matching entries. The range of 135.46.56.0/22 is 56 to 59, and 57 is inside it: it matches. The range of 135.46.56.0/21 is 56 to 63: that matches too. 60.0/22 (60 to 63) does not match. The default matches everything. The longest prefix wins: /22 > /21, so the answer is eth0.

Question 8medium

An ISP wants to advertise the four networks 57.6.104.0/24, 57.6.105.0/24, 57.6.106.0/24 and 57.6.107.0/24 as a single route. What is the aggregated prefix?

Show answer
57.6.104.0/22

Convert the third octets to binary: 104 = 01101000, 105 = 01101001, 106 = 01101010, 107 = 01101011. The common first 6 bits are 011010. The common prefix is 16 + 6 = 22 bits. Zero everything outside the common bits: 57.6.104.0/22.

Question 9medium

A department needs 500 hosts in a single subnet. What is the longest prefix (smallest subnet) that fits?

Show answer
/23

Look for the smallest hh such that 2h−2≥5002^h - 2 \ge 500. h=8h = 8: 254, not enough. h=9h = 9: 510, enough. The prefix is 32−9=2332 - 9 = 23.

Question 10medium

Give the first and last usable host addresses of the subnet containing 192.168.200.130/25.

Show answer
192.168.200.129 - 192.168.200.254

The /25 mask is 255.255.255.128, and the block size is 128. 130 is in the block from 128 to 255. The network address is .128 and the broadcast is .255. The first host is .129 and the last host is .254.

Question 11hard

Are the hosts 10.1.5.10/23 and 10.1.4.250/23 on the same subnet?

Show answer
Yes, both are in 10.1.4.0/23

The /23 mask is 255.255.254.0, with a block size of 2 in the third octet. 5 and 4 are in the same block (4 to 5). The network address of both is 10.1.4.0. They are on the same subnet and communicate without needing a router. Don't say "different networks" just because the third octet differs.

Question 12hard

Which of the following is a private (RFC 1918) IPv4 address?

  1. 172.32.10.5
  2. 192.169.1.1
  3. 172.20.10.5
  4. 11.0.0.1
Show answer
C) 172.20.10.5

The private blocks are 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31), and 192.168.0.0/16. 172.32 is outside this range, and 192.169 is not 192.168. 172.20 is inside 172.16/12.

Verification: All subnet answers were computed with node by converting to 32-bit integers and using AND/OR mask operations (verify/SEN2008/05.mjs).

Review card

IPv4 = 32 bit, 4 octets. /p = first p bits network, remaining 32-p bits host number of hosts = 2^(32-p) - 2 (/24: 254 /26: 62 /27: 30 /30: 2) mask octets: 128 192 224 240 248 252 254 255 block size = 256 - (mask in the interesting octet) /26 → 64 /20 → 16 (3rd octet) network address = IP AND mask; broadcast = network OR (NOT mask) first host = network+1, last host = broadcast-1 borrow n bits → 2^n subnets VLSM: start with the largest need, boundaries are multiples of the block size aggregation: convert to binary, count the common prefix bits LPM: among matches the LONGEST prefix wins, 0.0.0.0/0 is the last resort private: 10/8, 172.16/12 (16-31), 192.168/16