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)
- The host/router network layer has three parts: the IP protocol (datagram format, addressing, packet handling), routing protocols (OSPF, BGP, or an SDN controller) that build the forwarding table, and ICMP (error reporting, router signaling).
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) |
| Field | Size | Meaning |
|---|---|---|
| Version | 4 bits | IP version number (4) |
| Header length | 4 bits | Header size, counted in 32-bit words. No options → 5 words = 20 bytes |
| Type of service | 8 bits | Diffserv (bits 0–5) + ECN (bits 6–7) |
| Total length | 16 bits | Whole datagram in bytes, header included. Max 64K bytes; typically 1500 or less |
| Identifier | 16 bits | Same value in every fragment of one datagram |
| Flags | 3 bits | Fragment flag: 1 = more fragments follow, 0 = last one |
| Fragment offset | 13 bits | Where this fragment's data starts, in units of 8 bytes |
| TTL | 8 bits | Remaining max hops, decremented at each router; dropped at 0 |
| Upper-layer protocol | 8 bits | Who gets the payload: TCP or UDP |
| Header checksum | 16 bits | Checksum over the header only; recomputed at each router (TTL changes) |
| Source / destination address | 32 bits each | IP addresses |
| Options | variable | e.g., timestamp, record route taken |
- Overhead: 20 bytes TCP + 20 bytes IP = 40 bytes + app-layer overhead (4-34).
Fragmentation and reassembly (4-35, 4-36)
- MTU (max transfer unit): largest link-level frame a link can carry. Different link types have different MTUs.
- A datagram too big for the next link is fragmented inside the network: one datagram becomes several datagrams, each with its own 20-byte IP header.
- Reassembled only at the destination, never at routers. The header's identifier, flag and offset fields tell the receiver which fragments belong together and in what order.
How to solve: fragmentation
- Data in the original = total length − 20.
- Max data per fragment = MTU − 20, then round down to a multiple of 8 (offset counts 8-byte units).
- Cut the data into full-size pieces; the last piece gets the remainder (it does not need to be a multiple of 8).
- 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
| Fragment | Data bytes | Length (data + 20) | ID | Flag | Offset |
|---|---|---|---|---|---|
| 1 | 1480 (bytes 0–1479) | 1500 | x | 1 | 0 |
| 2 | 1480 (bytes 1480–2959) | 1500 | x | 1 | 1480 / 8 = 185 |
| 3 | 1020 (bytes 2960–3979) | 1040 | x | 0 | 2960 / 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)
- IP address: 32-bit identifier for each host or router interface.
- Interface: the connection between a host/router and a physical link. Routers typically have several interfaces; a host has one or two (wired Ethernet, WiFi).
- Dotted decimal: each of the 4 bytes written in decimal.
223.1.1.1 = 11011111 00000001 00000001 00000001.
How to convert: dotted decimal ↔ binary
- 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. - 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)
- Subnet: device interfaces that can physically reach each other without passing through a router.
- An IP address has a subnet part (high-order bits, shared by everyone in the subnet) and a host part (remaining low-order bits).
- Recipe: detach each interface from its host or router. Each resulting island of isolated network is a subnet (4-40).
- Subnet mask /24: the high-order 24 bits are the subnet part, e.g., subnet 223.1.1.0/24 (4-40).
- Router-to-router links are subnets too: slide 4-41 has 6 subnets (223.1.1/24, 223.1.2/24, 223.1.3/24, 223.1.7/24, 223.1.8/24, 223.1.9/24).
CIDR (4-42)
- CIDR (Classless InterDomain Routing, "cider"): the subnet part can be any length. Format a.b.c.d/x, x = number of bits in the subnet part.
- Example:
200.23.16.0/23 = 11001000 00010111 0001000|0 00000000: first 23 bits are the subnet, last 9 are the host part. - Addresses in a /x =
232−x. /24 → 256, /23 → 512, /20 → 4096, /26 → 64, /28 → 16. - Range of a /x: lowest address = host bits all 0, highest = host bits all 1.
Hierarchical addressing and route aggregation (4-52 → 4-55)
- Route aggregation: Fly-By-Night-ISP owns Organizations 0–7 (200.23.16.0/23, 200.23.18.0/23, …, 200.23.30.0/23) and advertises one route: "send me anything beginning 200.23.16.0/20". ISPs-R-Us advertises 199.31.0.0/16 (4-52).
- More specific routes: Organization 1 (200.23.18.0/23) moves to ISPs-R-Us. ISPs-R-Us now advertises 199.31.0.0/16 or 200.23.18.0/23. Fly-By-Night keeps advertising the /20 (4-53, 4-54).
- A router that hears both sends 200.23.18.x to ISPs-R-Us: /23 is longer than /20, so it wins. This is why forwarding uses longest prefix match.
- ICANN allocates addresses through 5 regional registries and manages the DNS root. The last IPv4 chunk went to the RRs in 2011. NAT and IPv6 (128-bit) address the shortage (4-55).
How to solve: longest prefix match
- Write the destination in binary (at least through the longest prefix in the table).
- For each table entry, compare the prefix bits to the address bits from the left. All prefix bits equal → match.
- Among the matches, pick the longest prefix. No match → "otherwise" (default) entry.
Slide example (4-15)
| Prefix | CIDR | Link |
|---|---|---|
| 11001000 00010111 00010*** | 200.23.16.0/21 | 0 |
| 11001000 00010111 00011000 | 200.23.24.0/24 | 1 |
| 11001000 00010111 00011*** | 200.23.24.0/21 | 2 |
| otherwise | 3 |
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 range | Link 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 |
| otherwise | 3 |
- (a) Provide a forwarding table that has five entries, uses longest prefix matching, and forwards packets to the correct link interfaces.
- (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)
- Link 0: the first 10 bits are fixed (
11100000 00), the rest run 0…0 to 1…1 → prefix11100000 00(/10). - Link 1: first 16 bits fixed → prefix
11100000 01000000(/16). - Link 2's range is not one prefix. Use the short prefix
1110000(/7), which covers 11100000… through 11100001…, and let longer entries carve out the pieces that belong elsewhere. - Inside that /7, addresses starting
11100001 1are not in link 2's range (it ends at 11100001 0111…), so they need link 3: prefix11100001 1(/9).
| # | Prefix match | Length | Link interface |
|---|---|---|---|
| 1 | 11100000 00 | 10 | 0 |
| 2 | 11100000 01000000 | 16 | 1 |
| 3 | 1110000 | 7 | 2 |
| 4 | 11100001 1 | 9 | 3 |
| 5 | otherwise | – | 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 match | Interface |
|---|---|
| 1 | 0 |
| 10 | 1 |
| 111 | 2 |
| otherwise | 3 |
Prefix 1 catches 1xxxxxxx, but the longer prefixes 10 and 111 take their parts away. What is left for interface 0 is 110xxxxx.
| Interface | Addresses it really gets | Range | Count |
|---|---|---|---|
| 0 | 110xxxxx | 11000000 – 11011111 | 25 = 32 |
| 1 | 10xxxxxx | 10000000 – 10111111 | 26 = 64 |
| 2 | 111xxxxx | 11100000 – 11111111 | 25 = 32 |
| 3 | 0xxxxxxx | 00000000 – 01111111 | 27 = 128 |
Check: 32 + 64 + 32 + 128 = 256 = 28 ✓.
How to solve: splitting a block into subnets
- For each subnet, round the required count up to a power of 2: block size 2k → prefix length
32 − k. - Check the sizes fit: their sum must be ≤ the parent block (232−x).
- Place the largest block first, at the start of the parent. Then the next largest right after it, and so on.
- A block of size 2k must start at a multiple of 2k (in the last octet for small blocks).
- 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").
| Subnet | Prefix | Last octet in binary | Range |
|---|---|---|---|
| 1 | 223.1.17.0/26 | 00|xxxxxx | .0 – .63 |
| 2 | 223.1.17.128/25 | 1|xxxxxxx | .128 – .255 |
| 3 | 223.1.17.192/28 | 1100|xxxx | .192 – .207 |
Clean non-overlapping answer (largest first):
| Subnet | Size | Prefix | Last octet | Range |
|---|---|---|---|---|
| 2 (≥90) | 128 | 223.1.17.0/25 | 0|xxxxxxx | .0 – .127 |
| 1 (≥60) | 64 | 223.1.17.128/26 | 10|xxxxxx | .128 – .191 |
| 3 (≥12) | 16 | 223.1.17.192/28 | 1100|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.
• 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 = 294Check: 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 255Check: 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 → 37. 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 →