IP Address Assignment and NAT

Published on: Oct. 26, 2024

Overview

Routers are devices that combine the functions of a Layer 2 switch and a Layer 3 router. They enable multiple devices to access the internet through a single global IP address. Each router typically has two types of IP addresses: a private IP and a public IP.

Methods for Assigning IP Addresses

Network Address Translation (NAT)

One key feature of routers is Network Address Translation (NAT). NAT enables a router to maintain a NAT table, which records both inbound and outbound traffic. This table allows the router to keep track of which internal IP addresses and ports are associated with which external IP addresses and ports.

How NAT Works

Here’s how NAT works in practice:

  1. When a device, such as a PC, wants to access a website, it sends a request to the router. For example, if your PC’s IP is 192.168.0.10 and you want to visit Google's website with an IP of 5.5.5.5, your router will need to manage this request.
  2. The router assigns an external port number (e.g., 20000) and records an entry in the NAT table:
    • Local IP: 192.168.0.10
    • Local Port: 3000
    • Remote IP: 5.5.5.5
    • Remote Port: 80
    • External Port: 20000
  3. The router modifies the outbound IP packet as follows:
    • Source IP: Public IP of the router (e.g., 3.3.3.3)
    • Source Port: 20000
    • Destination IP: 5.5.5.5
    • Destination Port: 80
  4. When the server (Google) responds, it sends a packet back to the router with:
    • Source IP: 5.5.5.5
    • Source Port: 80
    • Destination IP: Public IP of the router (e.g., 3.3.3.3)
    • Destination Port: 20000
  5. The router looks up the NAT table, finds the matching entry, and translates the packet’s destination back to the internal IP address and port:
    • Source IP: 5.5.5.5
    • Source Port: 80
    • Destination IP: 192.168.0.10
    • Destination Port: 3000

By doing this, the router ensures that the incoming packet is correctly routed back to the originating device within the local network. This process helps manage internal and external traffic and also adds a layer of security by controlling which devices can communicate with external networks.

Back to Blog