← all topics

8 · IP datagram, fragmentation, addressing, subnets, CIDR, longest prefix match ★

Slides 4-32 → 4-42, 4-52 → 4-55 · HW5 P4, P5, P6 · Textbook 4.3

Where IP sits (4-33)

IPv4 datagram format (4-34)

|<-------------------------- 32 bits -------------------------->|
| ver (4) | hdr len (4) | type of service (8) |  total length (16)  |
|        16-bit identifier          | flags (3) | frag offset (13)  |
|  TTL (8)  | upper-layer proto (8) |     header checksum (16)      |
|                 32-bit source IP address                         |
|               32-bit destination IP address                      |
|                      options (if any)                            |
|        data (variable length, typically a TCP or UDP segment)    |
FieldSizeMeaning
Version4 bitsIP version number (4)
Header length4 bitsHeader size, counted in 32-bit words. No options → 5 words = 20 bytes
Type of service8 bitsDiffserv (bits 0–5) + ECN (bits 6–7)
Total length16 bitsWhole datagram in bytes, header included. Max 64K bytes; typically 1500 or less
Identifier16 bitsSame value in every fragment of one datagram
Flags3 bitsFragment flag: 1 = more fragments follow, 0 = last one
Fragment offset13 bitsWhere this fragment's data starts, in units of 8 bytes
TTL8 bitsRemaining max hops, decremented at each router; dropped at 0
Upper-layer protocol8 bitsWho gets the payload: TCP or UDP
Header checksum16 bitsChecksum over the header only; recomputed at each router (TTL changes)
Source / destination address32 bits eachIP addresses
Optionsvariablee.g., timestamp, record route taken

Fragmentation and reassembly (4-35, 4-36)

How to solve: fragmentation

  1. Data in the original = total length − 20.
  2. Max data per fragment = MTU − 20, then round down to a multiple of 8 (offset counts 8-byte units).
  3. Cut the data into full-size pieces; the last piece gets the remainder (it does not need to be a multiple of 8).
  4. Each fragment: length = data + 20; offset = (bytes of data before it) / 8; flag = 1 except the last, which is 0; ID = the original's ID.

Slide example (4-36): 4000-byte datagram, MTU 1500

data in original     = 4000 − 20 = 3980 bytes
max data / fragment  = 1500 − 20 = 1480   (1480 / 8 = 185, already a multiple of 8)
3980 = 1480 + 1480 + 1020
FragmentData bytesLength (data + 20)IDFlagOffset
11480 (bytes 0–1479)1500x10
21480 (bytes 1480–2959)1500x11480 / 8 = 185
31020 (bytes 2960–3979)1040x02960 / 8 = 370

Check: 1480 + 1480 + 1020 = 3980 ✓. Total bytes sent = 1500 + 1500 + 1040 = 4040 (40 extra bytes = two extra headers).

IP addresses and interfaces (4-37, 4-38)

How to convert: dotted decimal ↔ binary

  1. Decimal → binary, one octet at a time. Place values: 128 64 32 16 8 4 2 1. Go left to right: if the value is ≥ the place value, write 1 and subtract; else write 0. Always write 8 bits.
  2. Binary → decimal: add the place values of the 1 bits.
223: 223−128=95 →1 | 95−64=31 →1 | 31<32 →0 | 31−16=15 →1
     15−8=7 →1 | 7−4=3 →1 | 3−2=1 →1 | 1−1=0 →1        = 11011111
17:  0 0 0 1 0 0 0 1  (16 + 1)                          = 00010001
11001000 = 128+64+8 = 200        10010001 = 128+16+1 = 145

Subnets (4-39 → 4-41)

CIDR (4-42)

Hierarchical addressing and route aggregation (4-52 → 4-55)

How to solve: longest prefix match

  1. Write the destination in binary (at least through the longest prefix in the table).
  2. For each table entry, compare the prefix bits to the address bits from the left. All prefix bits equal → match.
  3. Among the matches, pick the longest prefix. No match → "otherwise" (default) entry.

Slide example (4-15)

PrefixCIDRLink
11001000 00010111 00010***200.23.16.0/210
11001000 00010111 00011000200.23.24.0/241
11001000 00010111 00011***200.23.24.0/212
otherwise3
200.23.22.161 = 11001000 00010111 00010110 10100001
  matches 00010***  (21 bits) only               → link 0
200.23.24.170 = 11001000 00010111 00011000 10101010
  matches 00011000 (24 bits) and 00011*** (21)   → longest is 24 → link 1

HW5 P4: the problem

Q (HW5 P4): A datagram network uses 32-bit host addresses. A router has four links, numbered 0 through 3, and packets are forwarded to the link interfaces as follows:

Destination address rangeLink interface
11100000 00000000 00000000 00000000
through
11100000 00111111 11111111 11111111
0
11100000 01000000 00000000 00000000
through
11100000 01000000 11111111 11111111
1
11100000 01000001 00000000 00000000
through
11100001 01111111 11111111 11111111
2
otherwise3
  1. (a) Provide a forwarding table that has five entries, uses longest prefix matching, and forwards packets to the correct link interfaces.
  2. (b) Describe how your forwarding table determines the link interface for datagrams with these destination addresses:
11001000 10010001 01010001 01010101   (200.145.81.85)
11100001 01000000 11000011 00111100   (225.64.195.60)
11100001 10000000 00010001 01110111   (225.128.17.119)

a) Building the table (official answer)

#Prefix matchLengthLink interface
111100000 00100
211100000 01000000161
3111000072
411100001 193
5otherwise–3

b) Lookups (official answer)

11001000 10010001 ...   first bits 1100 ≠ 1110 → no prefix matches
                        → entry 5 (otherwise) → link 3
11100001 01000000 ...   1110000 ✓ (7 bits)
                        11100001 1? 9th bit is 0 → ✗
                        11100000 ... ✗ (8th bit is 1)
                        → entry 3 → link 2
11100001 10000000 ...   1110000 ✓ (7 bits), 11100001 1 ✓ (9 bits)
                        → longest = 9 bits → entry 4 → link 3

HW5 P5: ranges from an 8-bit LPM table

Q (HW5 P5): A datagram network uses 8-bit host addresses. A router uses longest prefix matching and has the forwarding table below. For each of the four interfaces, give the associated range of destination host addresses and the number of addresses in the range.

Prefix matchInterface
10
101
1112
otherwise3

Prefix 1 catches 1xxxxxxx, but the longer prefixes 10 and 111 take their parts away. What is left for interface 0 is 110xxxxx.

InterfaceAddresses it really getsRangeCount
0110xxxxx11000000 – 1101111125 = 32
110xxxxxx10000000 – 1011111126 = 64
2111xxxxx11100000 – 1111111125 = 32
30xxxxxxx00000000 – 0111111127 = 128

Check: 32 + 64 + 32 + 128 = 256 = 28 ✓.

How to solve: splitting a block into subnets

  1. For each subnet, round the required count up to a power of 2: block size 2k → prefix length 32 − k.
  2. Check the sizes fit: their sum must be ≤ the parent block (232−x).
  3. Place the largest block first, at the start of the parent. Then the next largest right after it, and so on.
  4. A block of size 2k must start at a multiple of 2k (in the last octet for small blocks).
  5. Write each as a.b.c.d/x and list its first and last address to confirm nothing overlaps.

HW5 P6: 223.1.17.0/24 → subnets of ≥60, ≥90, ≥12

Q (HW5 P6): A router interconnects three subnets: Subnet 1, Subnet 2, and Subnet 3. All interfaces in these three subnets must have the prefix 223.1.17/24. Subnet 1 must support at least 60 interfaces, Subnet 2 at least 90, and Subnet 3 at least 12. Provide three network addresses (of the form a.b.c.d/x) that satisfy these constraints.

Subnet 1: 60 → 64  = 2⁶ → /26
Subnet 2: 90 → 128 = 2⁷ → /25
Subnet 3: 12 → 16  = 2⁴ → /28
total 64 + 128 + 16 = 208 ≤ 256 ✓

Official answer: 223.1.17.0/26, 223.1.17.128/25, 223.1.17.192/28 ("other solutions are also fine as long as they satisfy the requirement").

SubnetPrefixLast octet in binaryRange
1223.1.17.0/2600|xxxxxx.0 – .63
2223.1.17.128/251|xxxxxxx.128 – .255
3223.1.17.192/281100|xxxx.192 – .207
Heads up: the official blocks overlap. 223.1.17.192/28 (.192–.207) sits inside 223.1.17.128/25 (.128–.255). A router would still forward correctly, since the /28 is longer and wins for .192–.207, and subnet 2 keeps 128 − 16 = 112 ≥ 90 addresses. But the three blocks are not disjoint. If you are asked for non-overlapping subnets, use the largest-first layout below.

Clean non-overlapping answer (largest first):

SubnetSizePrefixLast octetRange
2 (≥90)128223.1.17.0/250|xxxxxxx.0 – .127
1 (≥60)64223.1.17.128/2610|xxxxxx.128 – .191
3 (≥12)16223.1.17.192/281100|xxxx.192 – .207

Each block starts where the previous one ended + 1, and each start is a multiple of its size (0 of 128, 128 of 64, 192 of 16). No overlap; .208–.255 is left free.

Traps
• Total length includes the 20-byte header; data = length − 20. Every fragment gets its own header.
• Offset is in 8-byte units: divide the byte position by 8. And fragment data (except the last) must be a multiple of 8: round MTU − 20 down.
• Last fragment: flag 0. All others: flag 1. All share the same ID.
• Reassembly happens only at the destination.
• Addresses in /x = 232−x, not 2x.
• LPM: a short prefix like 1 does not get all of 1xxxxxxx. Subtract what longer prefixes take.
• Subnetting: round each size up to a power of 2, place largest first, start each block at a multiple of its size, and list ranges to prove no overlap.
• Always write 8 bits per octet when converting (17 = 00010001, not 10001).

Quick check

1. How many addresses in a /23? A /20? A /29?
/23: 2^(32−23) = 2^9  = 512
/20: 2^(32−20) = 2^12 = 4096
/29: 2^(32−29) = 2^3  = 8
2. What is a subnet? Interfaces that can physically reach each other without passing through a router. They share the high-order (subnet) bits of their addresses.
3. Fragment a 3000-byte datagram (ID = 777) for a link with MTU 1200.
data = 3000 − 20 = 2980
MTU − 20 = 1180 → not a multiple of 8 (1180/8 = 147.5)
round down: 8 × 147 = 1176 bytes of data per full fragment
2980 − 1176 − 1176 = 628 left for the last fragment

frag  data  length  ID   flag  offset
1     1176  1196    777  1     0
2     1176  1196    777  1     1176/8 = 147
3      628   648    777  0     2352/8 = 294
Check: 1176 + 1176 + 628 = 2980 ✓.
4. Fragment a 2400-byte datagram for MTU 700.
data = 2380;  700 − 20 = 680 = 8 × 85 ✓ (already a multiple of 8)
2380 = 680 + 680 + 680 + 340

frag  data  length  flag  offset
1     680   700     1     0
2     680   700     1     85
3     680   700     1     170
4     340   360     0     255
Check: offset 255 × 8 = 2040 = 3 × 680 ✓.
5. LPM: table 10.0.0.0/8 → A, 10.1.0.0/16 → B, 10.1.128.0/17 → C, otherwise → D. Where do 10.1.200.5, 10.1.100.1, 10.2.3.4, 11.0.0.1 go?
/17 means: first two octets 10.1, then 1st bit of 3rd octet = 1 (third octet ≥ 128)
10.1.200.5:  200 = 11001000, first bit 1 → matches /8, /16, /17 → longest /17 → C
10.1.100.1:  100 = 01100100, first bit 0 → /17 ✗; /8, /16 ✓ → B
10.2.3.4:    second octet 2 ≠ 1 → /16 ✗, /17 ✗; /8 ✓ → A
11.0.0.1:    first octet 11 ≠ 10 → nothing → D
6. LPM: 192.168.4.0/22 → 0, 192.168.6.0/23 → 1, 192.168.7.128/25 → 2, otherwise → 3. Where do 192.168.7.200, 192.168.5.9, 192.168.6.77, 192.168.8.1 go?
3rd octet bits:  4 = 000001|00  → /22 covers 3rd octet 000001xx = 4–7
                 6 = 0000011|0  → /23 covers 0000011x = 6–7
                 /25 = 192.168.7 + 4th octet first bit 1 = 7.128–7.255
192.168.7.200: 7 in 4–7 ✓, in 6–7 ✓, 200 ≥ 128 ✓ → /25 → 2
192.168.5.9:   5 in 4–7 ✓, not 6–7 → /22 → 0
192.168.6.77:  6 in 4–7 ✓, in 6–7 ✓, not 7.x → /23 → 1
192.168.8.1:   8 = 00001000, not 000001xx → otherwise → 3
7. 8-bit addresses, LPM table: 0 → A, 01 → B, 011 → C, otherwise → D. Range and count for each.
A: 0 minus 01 = 00xxxxxx  00000000–00111111  64
B: 01 minus 011 = 010xxxxx 01000000–01011111  32
C: 011xxxxx               01100000–01111111  32
D: 1xxxxxxx               10000000–11111111  128
total 64 + 32 + 32 + 128 = 256 ✓
8. Split 10.5.8.0/24 into subnets for ≥100, ≥50 and ≥20 interfaces, with no overlap.
100 → 128 = 2^7 → /25     50 → 64 = 2^6 → /26     20 → 32 = 2^5 → /27
128 + 64 + 32 = 224 ≤ 256 ✓   largest first:
10.5.8.0/25     .0   – .127    (4th octet 0xxxxxxx)
10.5.8.128/26   .128 – .191    (10xxxxxx)
10.5.8.192/27   .192 – .223    (110xxxxx)
Each starts right after the previous one ends and at a multiple of its size, so no overlap.
9. Split 130.10.0.0/16 into 4 equal subnets. 4 = 22 → borrow 2 bits → /18, each 214 = 16384 addresses. The 3rd octet's top 2 bits go 00, 01, 10, 11:
130.10.0.0/18     130.10.0.0   – 130.10.63.255
130.10.64.0/18    130.10.64.0  – 130.10.127.255
130.10.128.0/18   130.10.128.0 – 130.10.191.255
130.10.192.0/18   130.10.192.0 – 130.10.255.255
10. What range does 172.16.40.0/21 cover? Is 172.16.48.1 in it?
/21 → 3rd octet keeps top 5 bits: 40 = 00101|000
host bits 000 → 111 in 3rd octet: 40 → 47
range 172.16.40.0 – 172.16.47.255  (2^11 = 2048 addresses)
48 = 00110|000 → top 5 bits 00110 ≠ 00101 → not in it
11. Fly-By-Night advertises 200.23.16.0/20; ISPs-R-Us advertises 200.23.18.0/23. Where does a packet to 200.23.19.4 go? To 200.23.20.4?
19 = 0001001|1 → in 200.23.18.0/23 (18–19) and in the /20 (16–31)
   → longest /23 → ISPs-R-Us
20 = 0001010|0 → not in 18–19, only the /20 → Fly-By-Night
12. Convert 172.16.45.130 to binary, and 11000000 10101000 00000001 11111110 to dotted decimal.
172 = 128+32+8+4 = 10101100    16 = 00010000
45  = 32+8+4+1   = 00101101    130 = 128+2 = 10000010
→ 10101100 00010000 00101101 10000010
11000000 = 192, 10101000 = 168, 00000001 = 1, 11111110 = 254
→ 192.168.1.254
13. Header length field = 5 and total length = 1500. How many bytes of data? Header = 5 × 4 = 20 bytes. Data = 1500 − 20 = 1480.
14. What does TTL do, and where are fragments reassembled? TTL = remaining hops; each router decrements it and drops the datagram at 0. Fragments are reassembled only at the destination host.

← prev · all topics · next →