The exact procedure depends on the type of networks, the topology, and the equipment. I will attempt to describe the process with regard to most Ethernet networks.
Terms:
- MAC Address: Like a Social Security Number. It doesn't change as you move
- IP Address: Like an address, when you move (over long distances), it changes.
- TCP Packet: Data with TCP Port information (sometimes referred to as a TCP Segment)
- IP Datagram: Data with IP information
- Ethernet Frame: Data with MAC information
The IP Address is divided into two parts, the network and the node. The subnet you configure on your computer or router determines what network an IP address is on. You need to configure an interface with an IP Address (and subnet) to route to it.
Depending on your router, there are several things that might happen when it receives a packet:
Home Router (NAT Gateway)
- Packet comes in on Router
- Router extracts IP address from IP Datagram
- Router checks destination, 3. If the address is not the current router, it usually drops the packet (read below if its more than a NAT gateway)
- Router extracts port number from the packet
5 Router checks forwarding tables to see if that port is associated with an internal IP Address
- If yes: Delivers it (see below)
- Otherwise: Drop
"Real Router"
- Packet comes in on Router
- Router extracts IP address from IP Datagram
- Router checks to see if it is a part of destination IP network
- If yes: delivers it (see below)
- Otherwise, check the TTL (also from the IP Datagram) to see if it should be dropped or signalled as undeliverable.
- If still deliverable, check routing table for network destination, forward it to next router if known.
- Otherwise, forward it to the default gateway, drop the packet, or send it an ICMP response that its undeliverable. (depends on configuration)
Delivery (Ethernet)
- Router checks to see if IP address is in its ARP table (IP address to MAC addresses).
- If not, send an ARP request to locate the MAC address.
- Once an ARP response is recieved, send the packet to that MAC address.
The ARP request is a broadcast frame, so every computer sees the request. If there is no response, it may be silently dropped or responded to. The router only sends one frame for the broadcast (if its also bridge, it may send it out on each interface the bridge is on). To send broadcast frames, there is a special address called the Broadcast Address. On Ethernet networks, the address is FF:FF:FF:FF:FF:FF (all 1s in binary).
Bridges (including switches) recognise frames directed to FF:FF:FF:FF:FF:FF as a broadcast, and transmit it on every port. Some bridges (like managed switches), keep track of ARP requests themselves, so that they do not need to broadcast and simply reply with what's in their ARP cache. Firewalls can be configured to block these broadcasts, but it may have decremental effects to the IP network (the sides of the firewall can no longer talk to each other without a router).
Getting from the router to the node depends on the hardware (usually a bridge, a switch, or a hub)
Bridge
A bridge takes things input from one port, and sends it to one (or more) ports. Technically, switches are bridges, but a Firewall or Wireless Access Point are also bridges.
Switch
A switch remembers which port has which MAC address. (Usually, it'll learn it from the ARP response). The switch will send the frame (which contains a packet), to the destination port. In the rare instances that the switch doesn't know the MAC address, it behaves like a Hub and sends the information to every address.
Hub
A hub will not remember which port has MAC address. A hub will always send the frame to every port. There are a lot of problems (like collisions) associated with hubs.
Delivery (again)
Finally, the frame at this point will continue until on hubs and switches until it reaches its destination or is dropped. Things like STP exist to prevent it from being forwarded forever.