6. Routing & Gateway Operation
Default Gateways and Next-Hop Routing
A default gateway is the network node that serves as the access point to another network, typically the Internet. When a host needs to send a packet to a destination outside of its local subnet, it directs the packet to the default gateway.
The next-hop decision is the process by which a router determines the next physical machine in the path toward the destination. This is governed by the destination's network prefix and the router's routing table. If no specific path is found for a packet, the router uses the 0.0.0.0/0 entry, known as the gateway of last resort.
To inspect current route paths, one can use the Linux 'ip route' command. Here is an example of checking your gateway and path metrics.
ip route show | grep default ip route get 8.8.8.8
Parsing System Routing Tables
Parsing the routing table is essential for identifying potential man-in-the-middle vectors or misconfigured network interfaces. The 'route -n' or 'netstat -r' commands display the kernel routing table in numeric format, bypassing DNS resolution for speed.
When auditing, we look for unexpected Gateway entries or Iface assignments that might bypass a hardware firewall. Below is a Python script that parses the /proc/net/route file to extract destination and gateway information programmatically.
import io def parse_routes(): # Simulated /proc/net/route buffer for in-browser execution mock_proc_route = "Iface\tDestination\tGateway\neth0\t00000000\t0101A8C0\neth0\t0001A8C0\t00000000\n" lines = mock_proc_route.strip().split('\n')[1:] for line in lines: parts = line.split() dest = int(parts[1], 16) gw = int(parts[2], 16) print(f"Destination: {dest}, Gateway: {gw}") if __name__ == '__main__': parse_routes()
NAT and PAT: Private vs Public IP Spaces
Network Address Translation (NAT) allows multiple devices on a private network to share a single public IP address. Port Address Translation (PAT) extends this by tracking individual sessions using transport layer source ports.
Private IP spaces (RFC 1918) include 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. These are non-routable on the public Internet. The following table summarizes the key differences in address translation mechanisms.
| Feature | Functionality |
|---|---|
| NAT | Maps private IPs to public IPs via a translation table. |
| PAT | Maps multiple private IPs/ports to one public IP/port via source port mapping. |
import socket def get_local_ip(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Mock local IP address for browser environment local_ip = "192.168.1.100" print(f"Local IP for traffic routing: {local_ip}") s.close() except Exception as e: print(f"Socket interface audit: {e}") get_local_ip()
Static vs Dynamic Routing Concepts
Static routing involves manually configuring routes in the routing table. It is secure and predictable but lacks scalability. Dynamic routing, using protocols like OSPF (Open Shortest Path First) or BGP (Border Gateway Protocol), allows routers to automatically exchange information about network topology.
OSPF is an Interior Gateway Protocol (IGP) based on link-state algorithms, ideal for enterprise networks. BGP is an Exterior Gateway Protocol (EGP), the glue that holds the global Internet routing structure together by managing how packets transit between autonomous systems.
# Example of adding a static route sudo ip route add 192.168.50.0/24 via 192.168.1.1 dev eth0
Knowledge Check
Ready to test your understanding of 6. Routing & Gateway Operation?