%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#ecf0f1', 'noteTextColor': '#2C3E50', 'noteBkgColor': '#f39c12', 'textColor': '#2C3E50', 'fontSize': '16px'}}}%%
graph LR
Source[Source Device<br/>192.168.1.10]
R1[Router 1<br/>Check destination IP]
R2[Router 2<br/>Lookup routing table]
R3[Router 3<br/>Forward to next hop]
Dest[Destination<br/>10.20.30.40]
Source -->|Packet with<br/>dest IP| R1
R1 -->|Route lookup| R2
R2 -->|Next hop| R3
R3 -->|Deliver| Dest
style Source fill:#E67E22,stroke:#2C3E50,stroke-width:3px,color:#fff
style Dest fill:#16A085,stroke:#2C3E50,stroke-width:3px,color:#fff
style R1 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style R2 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style R3 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
682 Routing Basics: What is Routing?
682.1 Learning Objectives
By the end of this section, you will be able to:
- Understand Routing: Explain how routing implements packet switching
- Describe Router Function: Understand how routers make forwarding decisions
- Differentiate Routing vs Forwarding: Distinguish between building tables and using them
Think of routing like a GPS for data packets.
When you send a message from your IoT sensor to a cloud server, that message doesn’t magically appear at its destination. It travels through multiple networks, hopping from router to router, until it reaches the right place. Routing is the process of deciding which path each packet should take.
Real-World Analogy: Postal Delivery
Imagine sending a letter from London to Sydney: 1. Your local post office doesn’t deliver directly to Sydney 2. The letter goes to a regional hub, then national hub, then international hub, then Australia, then Sydney, then the final address 3. At each step, someone decides “where does this letter go next?”
Routers do exactly this for data packets: - Each router looks at the destination address - Checks its “routing table” (like a list of directions) - Forwards the packet to the next hop toward the destination
Key Concepts You’ll Learn:
| Term | What It Means | Analogy |
|---|---|---|
| Router | Device that forwards packets between networks | Post office sorting center |
| Routing Table | List of destinations and next hops | Postal code directory |
| Next Hop | The immediate next router to send to | “Send to Manchester hub” |
| TTL (Time-To-Live) | Limits how many hops a packet can make | “Return to sender after 30 days” |
Why Routing Matters for IoT:
- Mesh networks: Your sensor might be 5 hops away from the gateway - each hop needs routing
- Reliability: Good routing finds alternate paths when links fail
- Efficiency: Smart routing saves battery on your constrained devices
- Scalability: Proper routing lets you add thousands of sensors without manual configuration
Routing is like passing notes in class, but way smarter!
682.1.1 The Sensor Squad Adventure: The Great Message Relay
The Sensor Squad lives in a big school building with lots of rooms, and they need to send important messages to Principal Cloud, whose office is on the other side of the building. But here’s the tricky part - the hallways are like a maze, and there are many different ways to get there!
Sammy the Temperature Sensor has urgent news: “The science lab is getting too hot! We need to tell Principal Cloud right away!” But Sammy can’t run all the way to the principal’s office - sensors have to stay where they are. So Sammy hands the message to Relay Rita the Router, who lives in the hallway.
“Don’t worry, Sammy!” says Rita. “I know exactly who to pass this message to.” Rita looks at her special notebook called a routing table. It says: “Messages for Principal Cloud - give to Rocky Router in the next hallway.” Rita doesn’t know the whole path to the principal’s office, but she knows the next step - that’s how routing works!
Rocky Router receives the message and checks HIS notebook. “Ah, for Principal Cloud, I send messages to Rachel Router near the gym.” Rocky passes it along. Then Rachel checks HER notebook and sees she can deliver directly to Principal Cloud’s office! The message hops from router to router - each one only needs to know the next step, not the whole journey.
Meanwhile, Lux the Light Sensor in the art room also needs to send a message. But oh no! The hallway Rocky Router uses is blocked by a spilled bucket of paint! Rocky’s routing table has a backup plan: “If main path blocked, use the library route instead.” The message takes a different path but still arrives safely.
682.1.2 Key Words for Kids
| Word | What It Means |
|---|---|
| Router | A helper device that passes messages along to the next stop on the way to the destination - like students passing notes! |
| Routing Table | A router’s special notebook that says “messages for THIS place go THAT direction” |
| Hop | One step in the journey - when a message moves from one router to the next router |
| TTL (Time-To-Live) | A countdown number on each message that prevents it from traveling forever in circles |
682.1.3 Try This at Home!
Play the Message Relay Game!
- Get 4-6 family members or friends to stand in different rooms or corners
- Each person is a “router” - give each router a piece of paper with simple rules like:
- “If message is for Kitchen, pass to the person in the hallway”
- “If message is for Bedroom, pass to the person in the living room”
- Write a message addressed to someone not right next to you
- Pass the message following only the rules - no direct delivery allowed!
- Count how many “hops” it takes to arrive
Deep Dives: - TTL and Loop Prevention - Preventing infinite loops - Routing Tables - Table structure and route types - RPL Fundamentals - IoT-specific routing protocol
Architecture: - WSN Routing - Sensor network routing - Ad-hoc Networks - Dynamic routing strategies
682.2 Prerequisites
Before diving into this chapter, you should be familiar with:
- Networking Basics: Understanding fundamental networking concepts including IP addressing, packet switching, and the TCP/IP stack
- Layered Network Models: Knowledge of the OSI and TCP/IP models helps you understand where routing operates (Network Layer)
682.3 Why Routing Matters for IoT
Routing enables IoT devices to communicate beyond local networks. Whether connecting sensors to cloud platforms or building multi-hop mesh networks, routing is fundamental. Understanding how packets find their way across complex networks is essential for designing reliable, scalable IoT systems.
Core Concept: Routing is hop-by-hop decision making - each router examines only the destination IP address, consults its routing table to find the best next hop, and forwards the packet one step closer to the destination without knowing the complete path.
Why It Matters: Your IoT sensor does not need to know how to reach the cloud - it only needs to know its default gateway. Routers handle path selection, failover when links break, and load balancing across multiple paths. This separation lets sensors stay simple while networks stay resilient.
Key Takeaway: For IoT deployments, use RPL (Routing Protocol for Low-Power and Lossy Networks) instead of enterprise protocols like OSPF - RPL uses 83% less energy, tolerates lossy wireless links, and fits in constrained device memory through its tree-based DODAG topology.
Misconception: “Routing protocols like OSPF route every single packet through the network.”
Reality: Routing protocols build routing tables. The actual packet forwarding uses these pre-computed tables.
The Misconception in Numbers:
If OSPF routed every packet individually:
Smart building with 100 sensors sending every 60 seconds:
- 100 sensors x 1,440 packets/day = 144,000 packets/day
- If OSPF runs SPF algorithm for each packet at 2ms:
- CPU time: 144,000 x 2ms = 288 seconds/day = 4.8 minutes/day just routing
- ESP32 gateway (160 MHz CPU) would spend 20% of CPU just on routing!
The Reality:
Routing protocols work in two phases:
Phase 1: Control Plane (Routing Protocol) - Topology change detected: Link goes down - OSPF floods Link State Advertisements (LSAs): 50ms - All routers run Dijkstra SPF algorithm: 2ms - Routing tables updated: 10ms - Total: 62ms (happens only on topology change!)
Phase 2: Data Plane (Packet Forwarding) - Lookup destination IP in routing table: 5 microseconds - Find matching route: 10 microseconds - Forward out interface: 15 microseconds - Total: 30 microseconds per packet (20,000x faster than SPF!)
Remember: Routing is the process (building tables). Forwarding is the action (moving packets using tables). Routing protocols enable forwarding, but don’t forward packets themselves!
682.4 What is Routing?
Think of routing exactly like GPS navigation for your car:
When you drive from home to a friend’s house across town, your GPS doesn’t give you one giant map showing every possible road in the world. Instead, it:
- Knows your current location (where you are now)
- Knows your destination (where you want to go)
- Gives you the next turn (“Turn right in 500 feet”)
- Recalculates if you miss a turn or if there’s traffic ahead
Routing works the SAME way for data packets:
GPS Navigation:
Your Car -> Turn right on Main St -> Go straight 2 blocks ->
Turn left on Oak Ave -> Destination reached
Packet Routing:
Your IoT Sensor -> Router 1 -> Router 2 -> Router 3 ->
Cloud Server (destination)
At each intersection (router), you only need to know: - Where you’re trying to go (destination IP address) - Which road to take next (next hop router)
You DON’T need to know: - Every single street in the entire city (complete network map) - Exactly how many turns until the end (total path) - What other cars are doing (other packets’ routes)
Key Insight: Hop-by-Hop Decisions
Just like GPS gives you ONE turn at a time, each router makes ONE decision at a time.
682.4.1 Definition
Routing is the process that implements packet switching.
How it works: 1. Packet arrives at router 2. Router examines destination IP address in packet header 3. Router consults routing table to determine next hop 4. Router forwards packet toward destination
Layer: Routing is a Network Layer (Internet Layer) process using IPv4 and IPv6 addresses.
682.4.2 Routing vs Forwarding
| Term | Definition | Example |
|---|---|---|
| Routing | Process of learning network paths and building routing tables | Running OSPF protocol to exchange network information |
| Forwarding | Process of moving packets from input to output interface | Looking up destination IP and sending packet out correct port |
Think of it like: - Routing = Learning all possible roads (building a map) - Forwarding = Using the map to choose the next turn
682.5 Routers: The Packet Directors
682.5.1 What is a Router?
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#ecf0f1', 'noteTextColor': '#2C3E50', 'noteBkgColor': '#f39c12', 'textColor': '#2C3E50', 'fontSize': '16px'}}}%%
graph TB
subgraph Router["Router Components"]
CPU[CPU<br/>Routing Decisions]
RT[Routing Table<br/>Network Paths]
Mem[Memory<br/>Packet Buffer]
IF1[Interface 1<br/>Ethernet]
IF2[Interface 2<br/>Wi-Fi]
IF3[Interface 3<br/>Cellular]
end
N1[Network 1<br/>192.168.1.0/24]
N2[Network 2<br/>10.0.0.0/8]
N3[Network 3<br/>Internet]
N1 <--> IF1
N2 <--> IF2
N3 <--> IF3
CPU --- RT
CPU --- Mem
CPU --- IF1
CPU --- IF2
CPU --- IF3
style CPU fill:#E67E22,stroke:#2C3E50,stroke-width:3px,color:#fff
style RT fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
style Mem fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
style IF1 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style IF2 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style IF3 fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style N1 fill:#ecf0f1,stroke:#2C3E50,stroke-width:2px,color:#2C3E50
style N2 fill:#ecf0f1,stroke:#2C3E50,stroke-width:2px,color:#2C3E50
style N3 fill:#ecf0f1,stroke:#2C3E50,stroke-width:2px,color:#2C3E50
This variant shows the same router concept using a real-world traffic intersection analogy to help beginners understand how routers direct packets.
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#16A085', 'tertiaryColor': '#E8F6F3', 'fontSize': '12px'}}}%%
flowchart TB
subgraph ROUTER["Router = Traffic Intersection"]
SIGN["Road Signs<br/>(Routing Table)<br/>North -> Highway 1<br/>East -> Local Road<br/>South -> Downtown"]
COP["Traffic Controller<br/>(CPU)<br/>Reads destination,<br/>directs cars"]
end
CAR1["Car to City A<br/>(Packet to 10.0.0.0)"]
CAR2["Car to City B<br/>(Packet to 172.16.0.0)"]
CAR3["Car to Unknown<br/>(No route)"]
HWY["Highway 1<br/>(Interface eth0)"]
LOCAL["Local Road<br/>(Interface wlan0)"]
DROP["Dead End<br/>(Packet Dropped)"]
CAR1 --> ROUTER
CAR2 --> ROUTER
CAR3 --> ROUTER
ROUTER -->|"Match: North"| HWY
ROUTER -->|"Match: East"| LOCAL
ROUTER -->|"No Match"| DROP
style SIGN fill:#16A085,stroke:#2C3E50,color:#fff
style COP fill:#E67E22,stroke:#2C3E50,color:#fff
style CAR1 fill:#2C3E50,stroke:#16A085,color:#fff
style CAR2 fill:#2C3E50,stroke:#16A085,color:#fff
style CAR3 fill:#7F8C8D,stroke:#2C3E50,color:#fff
style HWY fill:#16A085,stroke:#2C3E50,color:#fff
style LOCAL fill:#16A085,stroke:#2C3E50,color:#fff
style DROP fill:#E74C3C,stroke:#2C3E50,color:#fff
Key Insight: Just like road signs don’t physically move cars, routing tables don’t move packets - they just tell the CPU which interface to send them out.
Definition: A router is a specialized computing device with: - Multiple network interfaces (Ethernet, Wi-Fi, cellular) - Routing software (makes forwarding decisions) - Routing table (database of network paths)
Important: While routers are usually dedicated devices, any device with the necessary hardware and software can perform routing: - Linux servers - Raspberry Pi - IoT gateways - Even ESP32 (with limitations)
682.5.2 Router Operation at Network Layer
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#ecf0f1', 'noteTextColor': '#2C3E50', 'noteBkgColor': '#f39c12', 'textColor': '#2C3E50', 'fontSize': '16px'}}}%%
flowchart LR
IN[Incoming<br/>Interface<br/>eth0]
RECV[Receive<br/>Packet]
EXAM[Examine<br/>Destination IP]
LOOKUP[Lookup<br/>Routing Table]
DECIDE[Select<br/>Output Interface]
FWD[Forward<br/>Packet]
OUT[Outgoing<br/>Interface<br/>eth1]
IN --> RECV
RECV --> EXAM
EXAM --> LOOKUP
LOOKUP --> DECIDE
DECIDE --> FWD
FWD --> OUT
style IN fill:#E67E22,stroke:#2C3E50,stroke-width:3px,color:#fff
style OUT fill:#16A085,stroke:#2C3E50,stroke-width:3px,color:#fff
style RECV fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style EXAM fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style LOOKUP fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style DECIDE fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style FWD fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
At the Network Layer, each packet is: 1. Received from one interface 2. Examined for destination IP address 3. Matched against routing table 4. Forwarded out appropriate interface
Each router makes independent forwarding decisions for each packet.
682.6 Router Forwarding Decisions
682.6.1 The Decision Process
When a router receives a packet, it makes one of three decisions:
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#ecf0f1', 'noteTextColor': '#2C3E50', 'noteBkgColor': '#f39c12', 'textColor': '#2C3E50', 'fontSize': '16px'}}}%%
flowchart TD
START[Packet Arrives]
CHECK{Specific Route<br/>in Table?}
DEFAULT{Default Route<br/>Configured?}
FWD_SPEC[Forward to<br/>Next Hop]
FWD_DEF[Forward to<br/>Default Gateway]
DROP[Drop Packet]
START --> CHECK
CHECK -->|Yes| FWD_SPEC
CHECK -->|No| DEFAULT
DEFAULT -->|Yes| FWD_DEF
DEFAULT -->|No| DROP
style START fill:#E67E22,stroke:#2C3E50,stroke-width:3px,color:#fff
style FWD_SPEC fill:#16A085,stroke:#2C3E50,stroke-width:3px,color:#fff
style FWD_DEF fill:#16A085,stroke:#2C3E50,stroke-width:3px,color:#fff
style DROP fill:#95a5a6,stroke:#2C3E50,stroke-width:3px,color:#fff
style CHECK fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style DEFAULT fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
682.6.2 Decision Flowchart Details
1. Specific Route Match - Routing table contains route to destination network - Forward packet to specified “next hop” router - Use specified outbound interface
2. Default Route - No specific route found - Default route (0.0.0.0/0) is configured - Forward to default gateway - Hope next router has more specific route
3. Drop Packet - No specific route found - No default route configured - Drop packet (discard) - Optionally send ICMP “Destination Unreachable” error back to source
682.6.3 IoT Example: Smart Home Gateway
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#ecf0f1', 'noteTextColor': '#2C3E50', 'noteBkgColor': '#f39c12', 'textColor': '#2C3E50', 'fontSize': '16px'}}}%%
graph TB
subgraph Home["Smart Home Network 192.168.1.0/24"]
Sensor1[Temperature<br/>Sensor]
Sensor2[Motion<br/>Sensor]
Light[Smart<br/>Light]
end
Gateway[IoT Gateway<br/>Routing Table:<br/>Local -> Direct<br/>Cloud -> ISP]
subgraph Internet["Internet"]
ISP[ISP Router]
Cloud[Cloud Server<br/>203.0.113.50]
end
Sensor1 --> Gateway
Sensor2 --> Gateway
Light --> Gateway
Gateway <--> ISP
ISP <--> Cloud
style Sensor1 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
style Sensor2 fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
style Light fill:#E67E22,stroke:#2C3E50,stroke-width:2px,color:#fff
style Gateway fill:#16A085,stroke:#2C3E50,stroke-width:3px,color:#fff
style ISP fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
style Cloud fill:#2C3E50,stroke:#16A085,stroke-width:2px,color:#fff
682.7 Summary
- Routing is the process of building tables that enable packet forwarding
- Forwarding uses those tables to move packets from input to output interfaces
- Routers examine destination IP addresses and make independent forwarding decisions
- Each router knows only its next hop, not the complete path
- Three possible decisions: specific route, default route, or drop
682.8 What’s Next
Continue to TTL and Loop Prevention to understand how networks prevent packets from circulating forever when routing tables have errors.