Network Layer — IP Addressing, Subnetting, Routing
679 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Network Layer — IP Addressing, Subnetting, Routing ## 🎯 Learning Objectives - Calculate subnet masks, network addresses, and broadcast addresses - Apply CIDR notation and VLSM - Trace Dijkstra's algorithm for link-state routing - Compare distance vector (RIP) and link-state (OSPF) routing * * * ## 1. IP Addressin...

Network Layer — IP Addressing, Subnetting, Routing
🎯 Learning Objectives
- Calculate subnet masks, network addresses, and broadcast addresses
- Apply CIDR notation and VLSM
- Trace Dijkstra's algorithm for link-state routing
- Compare distance vector (RIP) and link-state (OSPF) routing
1. IP Addressing
1.1 IPv4 Address Structure
An IPv4 address is 32 bits, typically written in dotted decimal:
192.168.1.1pseudoBits: | 8 bits | 8 bits | 8 bits | 8 bits | 192 168 1 1 Network ────────────┤├──── Host ───────┤
1.2 Address Classes (Historical)
| Class | Start | Prefix | Network Bits | Host Bits | Addresses per Network |
|---|---|---|---|---|---|
| A | 0.0.0.0 - 127.255.255.255 | /8 | 8 | 24 | 16,777,214 |
| B | 128.0.0.0 - 191.255.255.255 | /16 | 16 | 16 | 65,534 |
| C | 192.0.0.0 - 223.255.255.255 | /24 | 24 | 8 | 254 |
1.3 CIDR (Classless Inter-Domain Routing)
CIDR notation:
192.168.1.0/24 means the first 24 bits are the network prefix.| CIDR | Subnet Mask | Total Addresses | Usable Hosts |
|---|---|---|---|
| /30 | 255.255.255.252 | 4 | 2 |
| /29 | 255.255.255.248 | 8 | 6 |
| /28 | 255.255.255.240 | 16 | 14 |
| /27 | 255.255.255.224 | 32 | 30 |
| /26 | 255.255.255.192 | 64 | 62 |
| /25 | 255.255.255.128 | 128 | 126 |
| /24 | 255.255.255.0 | 256 | 254 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
1.4 Subnetting Worked Example
Problem: You have network
192.168.1.0/24. Create 4 subnets with equal hosts.
Step 1: 4 subnets need ceil(log2(4)) = 2 extra bits → /26
Step 2: Subnet boundaries:| Subnet | Network | First Host | Last Host | Broadcast |
|---|---|---|---|---|
| 1 | 192.168.1.0/26 | .1 | .62 | .63 |
| 2 | 192.168.1.64/26 | .65 | .126 | .127 |
| 3 | 192.168.1.128/26 | .129 | .190 | .191 |
| 4 | 192.168.1.192/26 | .193 | .254 | .255 |
Step 3: Binary representation of subnet 1:
pseudoNetwork: 192.168.1.00 000000 → 192.168.1.0 Mask: 255.255.255.11 000000 → /26 First: 192.168.1.00 000001 → .1 Last: 192.168.1.00 111110 → .62 Broadcast: 192.168.1.00 111111 → .63
2. Routing Algorithms
2.1 Dijkstra's Algorithm (Link-State Routing)
Used by OSPF. Each router knows the full network topology and computes the shortest path.
(Diagram)
Dijkstra from A:
| Step | Visited | Dist(A) | Dist(B) | Dist(C) | Dist(D) | Dist(E) | Dist(F) |
|---|---|---|---|---|---|---|---|
| 0 | {A} | 0 | 2 (A) | 5 (A) | ∞ | ∞ | ∞ |
| 1 | {A,B} | 0 | 2* | 5 (A) | 3 (B) | 5 (B) | ∞ |
| 2 | {A,B,D} | 0 | 2 | 4 (D) | 3* | 5 (B) | 5 (D) |
| 3 | {A,B,D,C} | 0 | 2 | 4* | 3 | 5 (B) | 5 (D) |
| 4 | {A,B,D,C,E} | 0 | 2 | 4 | 3 | 5* | 5 (D) |
| 5 | {A,B,D,C,E,F} | 0 | 2 | 4 | 3 | 5 | 5* |
Routing table for A:
| Destination | Next Hop | Cost |
|---|---|---|
| B | B | 2 |
| C | B (via D) | 4 |
| D | B | 3 |
| E | B | 5 |
| F | B (via D) | 5 |
2.2 Distance Vector Routing (RIP)
Each router shares its distance vector (list of distances to all destinations) with neighbors only.
Bellman-Ford equation: dx(y)=minv{c(x,v)+dv(y)}
Example: Router B's DV evolves:
sqlInitial: B → A:2, C:∞, D:1, E:3, F:∞ After receiving from A: A→C:5, A→D:∞, A→E:∞, A→F:∞ B→C = min(∞, 2+5=7) = 7 via A
Problems: Count-to-infinity, routing loops. RIP uses hop count (max 15) and split horizon to mitigate.
2.3 OSPF vs RIP vs BGP
| Property | RIP (DV) | OSPF (LS) | BGP (Path Vector) |
|---|---|---|---|
| Algorithm | Bellman-Ford | Dijkstra | Path vector |
| Metric | Hop count | Cost (bandwidth) | Path attributes |
| Convergence | Slow | Fast (flooding) | Slow (policy) |
| Scope | Intra-AS | Intra-AS | Inter-AS |
| Max hops | 15 | No limit | No limit |
| Updates | Every 30s | Event-driven | Event-driven |
3. ICMP and ARP
3.1 ICMP (Internet Control Message Protocol)
Used for error reporting and diagnostics:
| Type | Code | Meaning |
|---|---|---|
| 0 | 0 | Echo Reply (ping reply) |
| 3 | 0 | Destination Network Unreachable |
| 3 | 1 | Destination Host Unreachable |
| 8 | 0 | Echo Request (ping) |
| 11 | 0 | TTL Expired (traceroute) |
3.2 ARP (Address Resolution Protocol)
Maps IP addresses to MAC addresses:
(Diagram)
4. 📝 Practice Questions
Q1: Given network 172.16.0.0/16, create 8 subnets. What is the subnet mask and address ranges?Answer: 8 subnets = 3 bits → /19. Subnet mask: 255.255.224.0. Each subnet has 8192 addresses (8190 usable).Subnets: 172.16.0.0/19, 172.16.32.0/19, 172.16.64.0/19, ..., 172.16.224.0/19 Q2: Apply Dijkstra's algorithm to find the shortest path from D to F in the graph above.Answer: From A's routing table above, D→F via D→C→F (cost 1+4=5) or D→F directly (cost 2). Direct path D→F (cost 2) is shortest. Q3: Why does RIP have a maximum hop count of 15?Answer: To prevent count-to-infinity loops. If a route's metric reaches 16 (infinity), it's considered unreachable. This bounds the convergence time of the distance vector algorithm. Q4: What problem does CIDR solve that classful addressing couldn't?Answer: CIDR allows flexible subnetting (VLSM) and aggregation (route summarization). Classful addressing wasted addresses (a Class C is too small for many, Class B too large). CIDR also reduces routing table size through supernetting. Q5: How does ARP work when the destination is on a different network?Answer: The host checks if the destination IP is on the same network (using its subnet mask). If not, it sends the packet to the default gateway (router). It uses ARP to find the gateway's MAC address, not the destination's.
5. 🔗 Cross-References
- Week 2 - Transport Layer: TCP segments travel inside IP packets
- Week 4 - Data Link: ARP bridges network and data link layers
- BSCS4021 (Advanced Algorithms): Dijkstra, Bellman-Ford, graph algorithms Join Discord PreviousTCP & UDPNextData Link & Physical