The OSI Model and the Network Layer

The Network Layer (Layer 3 of the OSI model) is responsible for logical addressing (e.g., IP addresses) and routing data packets between different networks. This is where your simple 4-byte addresses come into play.

Goal: To wrap the Link Layer frame (Part 64) with a header that includes a Source Address and a Destination Address.

Defining the Packet Structure

Our simple network packet (often called a datagram) needs to sit inside the Link Layer frame and contain the necessary addressing information.

Field Size (Bytes) Purpose
Protocol ID 1 Identifies the payload (e.g., 01H for ICMP, 06H for TCP, etc.).
Packet Length 2 The total size of the entire datagram.
Source Address 4 The 32-bit IP address of the sender.
Destination Address 4 The 32-bit IP address of the final destination.
Payload Variable The data from the transport layer (e.g., a message).
Header Checksum 2 Simple error checking for the header fields.

The Routing Table

A Routing Table is a data structure the OS kernel uses to decide where to send a packet. When a packet is destined for a machine on a different network, the OS must send it to the Gateway (router).

Generic Routing Table Structure:

Address Block Mask Gateway Address
192.168.1.0 255.255.255.0 0.0.0.0 (Local Network)
0.0.0.0 0.0.0.0 192.168.1.1 (Default Gateway)

The Routing Logic (Address Comparison)

Before sending a packet, the OS must perform a logical AND comparison to determine if the destination is local or remote.

The Steps:

  1. Load Addresses: Load the Destination Address into registers (e.g., DEHL′) and the **Local Subnet Mask** into other registers (e.g., BC′).
  2. Comparison: Perform a 32-bit bitwise `AND′ on the Destination Address and the Subnet Mask.
  3. Result Check: If the result matches the Local Network Address, the packet can be sent directly via the Link Layer. Otherwise, the packet must be routed to the Default Gateway address found in the routing table.

Example Check:

    ; Assume 32-bit addresses are loaded into registers
    CALL AND_32_BIT       ; Perform Destination AND Mask
    
    ; Compare result against local network address
    LD   HL, LOCAL_NET_ADDR
    CALL CMP_32_BIT       ; Compare the 32-bit result with the local subnet base
    
    JP   Z, SEND_LOCAL     ; If Zero flag is set, it's local.
    CALL SEND_GATEWAY     ; Otherwise, send to the default router.