Skip to content

This project implements the dataplane of a software router in C, designed to operate at Layer 3 of the OSI model. The router acts as a core network device, efficiently processing and forwarding IPv4 traffic between different network interfaces.

Notifications You must be signed in to change notification settings

GeorgeGrasu/Simple-Router-Implementation

Repository files navigation

Router Implementation

This project implements the dataplane of a simple router in C. It handles IPv4 forwarding, ARP address resolution (with caching), and ICMP control messages.

Features

  • IPv4 Packet Forwarding:

    • Validates IP checksums.
    • Decrements TTL and recalculates checksums.
    • Performs Longest Prefix Match (LPM) on the routing table to find the next hop.
    • Forwards packets to the appropriate interface or next hop.
  • Dynamic ARP:

    • Maintains an ARP cache (static size of 100 entries) to map IP addresses to MAC addresses.
    • Sends ARP Requests (who-has) for next-hop IPs not in the cache.
    • Queues packets waiting for ARP resolution.
    • Processes ARP Replies (is-at) to update the cache and release queued packets.
  • ICMP Handling:

    • Responds to Echo Requests (Ping) destined for the router interface with Echo Replies.
    • Sends Time Exceeded (Type 11) messages if a packet's TTL drops to 0 or 1.
    • Sends Destination Unreachable (Type 3) messages if no route is found for a packet.

Prerequisites

This project requires a Linux environment (or WSL) with the following tools:

  • gcc and make
  • python3
  • mininet
  • scapy (Python library)
  • tshark (Wireshark command-line tool)
  • ethtool

Automatic Installation

A helper script is provided to install the necessary dependencies on Debian/Ubuntu-based systems (including WSL):

./install_deps.sh

Building and Running

Build

To compile the router, simply run:

make

This ensures the router binary is created.

Running Tests

The project comes with a comprehensive test suite using Mininet. To run the tests:

./checker/checker.sh

This script will start the Mininet topology, run the tests defined in checker/check.py, and output the score.

Implementation Details

Routing Table Lookup (get_best_route)

The current implementation uses a linear search through the routing table (rtable) to find the entry with the longest network mask (mask) that matches the destination IP. While simple, this is O(N) per packet.

ARP Handling (process_arp_packet)

  • Replies: When an ARP reply is received, the cache is updated (renew), and the queue of pending packets is checked. Any packet waiting for this IP is retrieved, its Ethernet header is populated with the new MAC address, and it is sent out.
  • Requests: When an ARP request is received for one of the router's own IPs, it immediately replies with its MAC address.

ICMP Logic (router.c)

  • Echo Reply: Handled by swapping source/destination IPs in the IP header and changing the ICMP type to REPLY.
  • Error Messages: New packets are generated for Time Exceeded and Destination Unreachable, encapsulating the original IP header (standard ICMP behavior).

Queueing

A simple queue (queue.h) is used to store packets (pkt struct) that are waiting for an ARP reply. The queue is checked every time a new ARP entry is added to the cache.

File Structure

  • router.c - Main logic for the router dataplane.
  • lib/ - Helper libraries for data structures (queue, list) and parsing.
  • include/ - Header files for protocols and structures.
  • checker/ - Test infrastructure (Mininet topology and scripts).

About

This project implements the dataplane of a software router in C, designed to operate at Layer 3 of the OSI model. The router acts as a core network device, efficiently processing and forwarding IPv4 traffic between different network interfaces.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published