WWW

How the Internet Works, Chapter 7
The Internet Layer: How Routers Use IP Addresses to Direct Internet Traffic

posted in: How the Internet Works | 0

Routers send traffic from one network to another. Every local network that is part of the internet has at least one router. There are also routers that are not part of a local network, typically those which handle longer internet trips.

The router is responsible for opening (or de-encapsulating) a packet, looking at a packet’s IP address, determining where to send the packet based on its IP address, re-encapsulating the packet using the frame protocol of the destination node (usually some form of Ethernet, but not always), and forwarding the frame.

Each router has a routing table, which is a list of IP addresses that it can send to. When a router receives a packet, it searches its routing table to determine the closest match to the packet’s IP address, and forwards it there.

To determine the closest match, the router goes from the specific to the general, using this logic:

  1. If the routing prefix (see How Routers Evaluate IP Addresses below) on a table entry is the same as the IP address’s, then the destination node is local to that router. Send the packet there.
  2. Otherwise, send the packet to the closest match to the IP address in the routing table. The closest match uses a “longest match wins” principle: the more numbers matching at the beginning of the destination IP address and an IP address in the table, the closer the match.
  3. If there are no IP addresses in the routing table that even partially match, send the packet to the default gateway. This is the IP address of a router that handles trips outside the local area.
  4. If the default gateway is a core router, it doesn’t have default gateways of its own. Core routers’ routing tables contain top-level IP addresses and work exclusively with step 2.

How Routers Evaluate IP Addresses

An IP address has two logical parts: the routing prefix (the first group), which identifies a network, and the host identifier (the second group), which identifies a node on that network. The more bits that are used for the routing prefix, the fewer can be used for individual host identifiers. So the larger the routing prefix, the smaller the network.

For example, one of the Charter Communications networks has all the numbers from 24.158.0.0 to 24.158.255.255. So, its routing prefix is 24.158, and there are 65,534 possible host identifiers. (Not 65,536, because the highest and lowest available addresses are reserved for the router’s IP address and the broadcast address, respectively. The broadcast address is used to send to every node on the network, typically to discover resources.)

A typical small business office network uses the first three bytes for the routing prefix, and so has 254 numbers that can be used for host identifiers.

To distinguish the routing prefix from the host identifier, the router uses a subnet mask, also known as a netmask. The netmask uses the same format as an ordinary IP address, with bits set to 1 for the routing prefix, and set to 0 for the host identifier. Therefore, in the network in the above example, the subnet mask is 255.255.255.0. It follows that the logical AND of the subnet mask and any IP address on the network will be the routing prefix. It further follows that the logical AND of the one’s complement (a one’s complement of a number is the number with all its bits reversed) of the subnet mask (0.0.0.255, in our case) will be the host identifier.

Breaking Down an IP Address: an Example

Let’s look at an example of how this works. Suppose we have a 254-node network. The netmask will be 255.255.255.0. Suppose further that one of the nodes on this network has the IP address 169.254.190.93. The routing prefix would be 169.254.190, and the host identifier would be 93.

Now, suppose our router receives a packet with the destination address 169.254.190.93. The router will first apply the subnet mask:

Decimal 169 254 190 93
Binary 10101001 11111110 10111110 01011101
Netmask 11111111 11111111 11111111 00000000
Binary AND Netmask 10101001 11111110 10111110 00000000

The bottom row of this table in decimal is 169.254.190.0, or the router’s IP address. So, the router knows that the destination IP address is in its own network.

To get the destination node’s host identifier, the router ANDs the one’s complement of the subnet mask with the full IP address:

Decimal 169 254 190 93
Binary 10101001 11111110 10111110 01011101
One’s c. of Netmask 00000000 00000000 00000000 11111111
Binary AND One’s c. 00000000 00000000 00000000 01011101

The result in decimal is 0.0.0.93, or the destination’s host identifier. The router then finds the MAC address of the host (there are various ways to do this, depending on the actual network configuration) and sends the packet to it.

Limitations of the Internet Layer

The Internet layer determines the destination for data and sends it there. However, it does not ensure that the data is sent intact. Also, a single node can have many different applications that use the internet to send and receive data (for example, a browser and an email application), and the internet layer doesn’t do anything to distinguish between these.

Data reliability and application-level communication are the responsibility of the Transport Layer, which we begin discussing in the next article.