Program for IP Forwarding Table Lookup

Learn via video courses
Topics Covered

Overview

There exists a routing table in the Unix operating system consisting of several tuples that contain the I.P. (Internet Protocol) address of the network, the subnet mask, the IP address of the gateway, and the name of the interface.

This information is required for forwarding packets to the exterior of the network for connecting to the internet. In this article, we shall have a look at how a computer system takes a sequence of decisions when packets need to be forwarded.

Introduction

There exist the following two different methods of delivering IP datagrams:

Routing

Involves finding and setting up routing tables.

Forwarding

Involves passing a packet from an input interface to an output interface.

The process of forwarding must be executed as quickly as possible. On routers, forwarding is done often with hardware support. On personal computer systems, it is performed in the kernel of the machine’s operating system.

Example of IP Forwarding Lookup Table

Let us have a look at a sample IP forwarding lookup table for a clearer understanding. Assume there exists a packet having source IP address “20.129.0.1”. Its routing table may look something like the following:

Network IP addressSubnet maskGateway IP addressInterface name
200.200.16.0255.255.248.0192.13.2.55eth4
20.128.0.0255.128.0.012.1.1.1eth1
200.200.200.0255.255.248.0192.13.2.55eth4
0.0.0.00.0.0.012.23.44.1eth9

We shall refer to this system as well as the table above in the article later.

Working of IP Forwarding Lookup Table

When a packet is sent to the kernel of the operating system in order to find the interface and gateway, the first thing that is done is a bitwise AND operation individually with each subnet mask entry in the table for finding the longest prefix match. After obtaining the desired bitwise AND operation result, it’s then compared with the IP addresses of the network.

The result now produced is the IP address corresponding to the gateway, as well as the name of the interface via which the packet may now go out. “00010100.10000001.00000000.00000001” is the binary representation of “20.129.0.1”. A bitwise AND operation is performed upon this value with the subnet mask of each entry in our table.

The subnet mask from entry 2 (20.128.0.0, 255.128.0.0, 12.1.1.1, eth1) yields the longest matching prefix for our packet. Therefore, our packet goes out from the interface eth1 and selects the gateway 12.1.1.1 for forwarding.

Implementation of IP Forwarding Lookup Table

Following is a C program that implements an IP forwarding lookup table

Conclusion

  • There exists a routing table In the Unix operating system consisting of several tuples that contain the I.P. (Internet Protocol) address of the network, the subnet mask, the IP address of the gateway, and the name of the interface.
  • There exist two different methods of delivering IP datagrams: Routing and Forwarding.
  • When a packet is sent to the kernel of the operating system in order to find the interface and gateway, the first thing that is done is a bitwise AND operation individually with each subnet mask entry in the table for finding the longest prefix match.