Jump to content

IP forwarding algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Geertivp (talk | contribs) at 22:02, 21 January 2012 (Removing duplicate ELSE IF code and adding an example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The IP forwarding algorithm, commonly known as IP routing, is a specific implementation of routing for IP networks and gives a more directed approach in forwarding datagrams over a network. In order to achieve a successful transfer of data the algorithm uses a routing table to select a next-hop router as the next destination for a datagram. The IP address that is selected is known as the next-hop address.[1]

IP Routing algorithm

The IP Forwarding Algorithm states:

Given a destination IP address, D, and network prefix, N:

if (N matches a directly connected network address)

    Deliver datagram to D over that network link;

else if (The routing table contains a route for N)

    Send datagram to the next-hop address listed in the routing table;

else if (There exists a default route)

    Send datagram to the default route;

else

    Send a forwarding error message to the originator;

The first case is OSI level 2 routing; the next 2 cases are OSI level 3 routing.

When there is no route available an ICMP error message is sent to the originator of the packet, to inform that host that the packet could not delivered, and to avoid unnecessary retransmission and network congestion.

IP Routing types

OSI level 2

A systems' own subnet is immediately reachable over the primary network link. An ethernet arp and MAC addressing broadcasting technique will be used to send packets to the target system in this case (OSI level 2).

Sometimes private LAN routing rules are forgotten. This frequently leads to packet non-delivery. Especially if you have traffic of multiple subnets over the same network segment.

The following subnets cannot be routed, so they need to always be reacheable via a direct attached network device, via a NIC, a HUB, a switch, or a bridge.

Remark that there is no next-hop address, because those subnets are not routeable over the public internet.

route add -net 169.254.0.0 netmask 255.255.0.0 dev eth0
route add -net 192.168.0.0 netmask 255.255.0.0 dev eth0
route add -net 172.16.0.0  netmask 255.240.0.0 dev eth0
route add -net 10.0.0.0    netmask 255.0.0.0   dev eth0

OSI level 3

The next 2 route types are OSI level 3:

You can send all traffic for a subnet to a specific host:

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.96.1

To change the default route, the target router should already have been defined:

route add default gw 71.46.14.1

Routing flags

Most common Routing Flags
Flag Description
G Use Gateway (gateway filled in)
H Target is a Host (bit mask of 32 bits)
U Route is Up

Example

Example of a (small) routing table:

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         71.46.14.1      0.0.0.0         UG    0      0        0 ppp0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
71.46.14.1      0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
172.16.0.0      0.0.0.0         255.240.0.0     U     0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
192.168.1.0     192.168.96.1    255.255.255.0   UG    0      0        0 eth0
192.168.96.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

Network interfaces: eth0 is the network interface card; ppp0 is the PPPoE connection.

A default route is recognized by Destination 0.0.0.0 and Flag G.

A network router is identified by a network mask 255.255.255.255 and a Flag H.

See also

For more information you can consult the route man page on Unix systems.

References

  1. ^ Internetworking with TCP/IP: Principles, protocols, and architecture By Douglas Comer