Routing¶
The internet consists of smaller, interconnected network. How to send a packet from one device to another, if they are searated by several smaller networks?

Router is a network level device that sends packets between networks. It decides whether to send a packet to another device or broadcast it in its local network. Each router has a routing table that contains:
- a destination network address and mask
- a gateway
Example:
destination mask gateway 192.168.1.0
255.255.255.0
10.0.0.1
192.168.2.0
255.255.255.0
10.0.0.2
192.168.3.0
255.255.255.0
10.0.0.3
192.168.4.0
255.255.255.0
10.0.0.4
10.0.0.0
255.0.0.0
-
If a router receives a packet that must be sent to device 192.168.1.42/24
, the router derives the address of the network the device is in (based on the mask). For 192.168.1.42/24
the network address is 192.168.1.0/24
.
The routing table tells the router that messages to network 192.168.1.42/24
must be sent through gateway 10.0.0.1
. A gateway is just another device that the router is connected to (another router) that knows where to pass the message next, so that it is eventually delivered to 192.168.1.42/24
.
If a router receives a packet for the network it is in, then it broadcasts the packet to the network. This information is also in the routing table: the entry with gateway set to -.
On the basis of the routing table, what is the network address (and mask) of the router?
Remarks¶
- Selecting a route through the network is a multistage process. Every router on the way from the source to the destination makes a separate decision for every packet it receives. This means that if a message is split into multiple packets, each can be delivered via different routes.
- The source address (the address of the device sending the packet) is igunored when establishing a route.
Routing types¶
- static routing - the routing table is specified by the administrator
- dynamic routing – the routing table is specified by a routing protocolduring runtime; protocols include: RIP, OSPF, IS-IS, IGRP
- hardware routing
- software routing
Example¶

Routing tables:
R1¶
destination mask gateway 10.0.0.0
255.0.0.0
-
20.0.0.0
255.0.0.0
-
30.0.0.0
255.0.0.0
20.0.0.2
40.0.0.0
255.0.0.0
20.0.0.2
R2¶
destination mask gateway 10.0.0.0
255.0.0.0
20.0.0.1
20.0.0.0
255.0.0.0
-
30.0.0.0
255.0.0.0
-
40.0.0.0
255.0.0.0
30.0.0.2
R3¶
destination mask gateway 10.0.0.0
255.0.0.0
30.0.0.1
20.0.0.0
255.0.0.0
30.0.0.1
30.0.0.0
255.0.0.0
-
40.0.0.0
255.0.0.0
-
Establishing a route¶
Establishing a route for a packet sent from network 10.0.0.0/8
to device 40.0.0.2/8
:
Router R1 receives the packet and tries to determine where to send it next.
- For each entry in its routing table it performs the bitwise
AND
operation on the packet’s destination address and the mask in the routing table. For all entries in R1’s routing table this is the network address is40.0.0.2 AND 255.0.0.0 = 40.0.0.0
.- After each bitwise
AND
operation the router checks whether the result is equal to destination for this entry- So, after performing bitwise
AND
for the 4-th row, the rult
40.0.0.0
equals the destination40.0.0.0
, so the router sends the packet to gateway20.0.0.2
.Router R2 receives the packet from R1 and repeats the procedure using its own routing table. R2 sends the packet to gateway
30.0.0.2
.Router R3 receives the packet from R2 and repeats the procedure. It determines that the gateway for
40.0.0.0
is-
. It therefore broadcasts the packet to all the devices in network40.0.0.0
. Since the network contains the device40.0.0.2
, the packet reaches its ultimate destination.
Static routing in Linux¶
In order for a Linux computer to act as a router, it must have:
- a routing table
- turned on forwarding between logical interfaces
DON’T FORGET ABOUT FORWARDING
Routing table¶
ip route
:show routing table:
ip route
lubip route show
show routing table for interface
eth0
:ip route show dev eth0
add routing table entry to destination
192.168.55.0/24
with gateway192.168.1.254
for interfaceeth0
:ip route add 192.168.55.0/24 via 192.168.1.254 dev eth0
- add default gateway for
eth0
(so, gateway equal to-
): ip route add default via 192.168.1.254
- add default gateway for
remove an entry from the routing table for destination
192.168.55.0/24
for interfaceeth0
:ip route delete 192.168.55.0/24 dev eth0
Forwarding¶
- File switches forwarding on or off
/proc/sys/net/ipv4/ip_forward
: - if file contains
1
forwarding is turned on - if file contains
0
forwarding is turned off
- if file contains
- The contents of the file can be changed with the
sysctl
command: - show status:
sysctl net.ipv4.ip_forward
- turn on forwarding:
sysctl -w net.ipv4.ip_forward=1
- turn off forwarding:
sysctl -w net.ipv4.ip_forward=0
- show status:
- The contents of the file can also be changed manually
- show status:
cat /proc/sys/net/ipv4/ip_forward
- turn on forwarding:
echo 1 > /proc/sys/net/ipv4/ip_forward
- turn off forwarding:
echo 0 > /proc/sys/net/ipv4/ip_forward
- show status:
Turning on or off for every system start: /etc/sysctl.conf
CIDR¶
Question: how can address aggregation be used to make routing faster?