Dataplane – the part that implements the actual routing process, based on entries in the routing table.
This project implements a simple Layer 3 router capable of forwarding IPv4 packets, performing Longest Prefix Match (LPM), handling ARP (requests, replies, and caching), and generating ICMP messages.
It is based on the skeleton provided in Laboratory 4 of the Communication Protocols course.
When the router receives an IPv4 packet, it follows these steps:
-
Destination check
If the destination IP matches one of the router’s interfaces, the router does not forward the packet but replies with an ICMP Echo Reply. -
Checksum verification
The IP header checksum is recalculated. If it differs from the original, the packet is dropped (corrupted data). -
TTL (Time To Live)
- If
TTL <= 1: the packet is dropped and an ICMP Time Exceeded message is sent. - Otherwise,
TTLis decremented and the checksum is recalculated.
- If
-
Route lookup (LPM)
The functionget_best_route(IP_node *root, uint32_t IP)searches the Trie to find the most specific route (Longest Prefix Match).
If no valid route is found, an ICMP Destination Unreachable message is sent. -
Next-hop MAC resolution
After identifying the route, the router looks for the next hop’s MAC address in the ARP table before forwarding the packet.
Implemented using a binary Trie structure:
- Each node represents one bit of the IP address (
0or1). - Each node can store a pointer to a valid routing table entry.
- The
insert_ip_route()function inserts a route bit by bit according to its mask. - The
get_best_route()function traverses the Trie and returns the longest matching prefix for a given IP.
After determining the best route, the router must find the MAC address of the next hop:
-
If the MAC address is cached:
The packet is immediately sent. -
If the MAC address is missing:
- The packet is stored in a waiting queue.
- An ARP Request is sent for the next hop’s IP.
-
Upon receiving an ARP Reply:
- The router updates the ARP table.
- All queued packets waiting for that IP are transmitted.
- Packets without a resolved address remain in the queue.
Implemented in the send_icmp() function, which builds and sends ICMP messages in the following cases:
- TTL expired → ICMP Time Exceeded
- No valid route → ICMP Destination Unreachable
- Packet addressed to the router → ICMP Echo Reply
Steps to build and send an ICMP packet:
- Construct a new IP + ICMP packet based on the original.
- Set the source MAC to the router’s interface MAC.
- Set the destination MAC to the original sender’s MAC.
- Swap source/destination IPs.
- Set ICMP header fields (type, code, checksum).
- Recalculate the IP checksum.
- Send the packet on the same interface where it was received.
This project was developed for the Communications Protocols course, based on the Dataplane router assignment: