L3 routing concept

L3 routing is done on layer 3  of the TCP/IP stack and layer 3 of the TCP/IP stack is called a network layer, on the other hand, the L2 routing is done on layer 2 of the TCP/IP stack and the layer 2 of the TCP/IP stack is called a data link layer

For L3 routing, only IP addresses were used to deliver the packet to the destination subnet, and  L3 is all about routing a packet from a source subnet to the destination subnet

Rendered by QuickLaTeX.com

As you can see from the above topology that we have five nodes and these nodes denote  L3 routers which are interconnected through links

A simple scenario what will a router P do if it receives a packet destined for router T?

Will the router P forwards the packet to Q or R?

How the router P will know that if it receives a packet that is destined for router  T?

How will router P will take this decision?

How would  P know that it has to forward the packet to destination T, or else it has to forward the packet to Q or R, which path will be better?

So, to accomplish this

We need to  feed rules in every L3 router in the network which defines the behavior of an L3 router to how to forward the packet destined to a remote destination

This set of rules are called “L3 routing information”

Without L3 routing information, routers that are more than one hop away cannot talk to each other

Therefore, if we can’t feed P with routing information then router P can’t talk to router T and S

Why?

Because router T and S are one hop away from router P whereas the machines which are directly connected can always talk to each other with the help of L2 routing without feeding  L3 routing information

A simple example

Suppose, router P receives a packet that is destined to the destination T, at this instant, we have fed L3 information rules in all the routers of the topology stating how to forward the packet

Rendered by QuickLaTeX.com

As you can see from the above topology that L3 information installed on each router of the topology

The L3 information carries two parts, the first part is about the ultimate destination of a packet and the second part is about the next immediate node in the topology

Rendered by QuickLaTeX.com

So, when router P receives a packet that is destined for the  router T then the router P knows that it has to forward the packet to the next-hop that is router Q,

At this instant, the router Q will look up its L3 routing information and will know that to forward its packet to remote destination T, it has to forward the packet to the next-hop S, and the router S will do the same thing that it will forward the packet to T according to its L3 routing information and the packet will eventually reach to its intended destination

Therefore, L3 routing information is fed in every router in the topology, so, that always routers work together to reach their intended destination

A practical example

Rendered by QuickLaTeX.com

The above topology shows 8 devices  L, M, N, O, P, Q, and 2  L3 routers  with MAC address  ML, MM, MN, MO, MP, MQ, and MR1, MR2

Also, it contains  subnet A with network id 11.1.1.0/24 and a subnet B with network id 12.1.1.0/24 then both the subnets are gated by L3 router

So, for a subnet A, L3 router R1 is a gateway router similarly for subnet B L3 router R2 is a gateway router

Suppose machine N which is present in subnet A, like to send a packet to machine P which is present in remote subnet B, on the other hand, in between the source machine  subnet A and destination machine subnet B internet is there

Here, the internet means a finite number of L3 routers

Every subnet in the above topology is gated by an L3 router

So, for the source subnet A, L3 router R1 is a gateway router similarly, for subnet B, L3 router R2 is a gateway router

Whenever a machine like to send data to a remote machine that is present in the remote subnet then  at that instance,

 first, the source machine N has to send the data to gateway router R1 to leave its local subnet

The process of sending of data from source machine N to R1 is accomplished by L2 routing only

Second,once, the data is received at the L3 router R1 then it will be sent to the next available L3 router

the process of sending data from L3 router R1 to L3 router R2 is accomplished by L3 routing only

third, L3 router R2 will have the intelligence to find the destination machine P

The process of sending the data from L3 router R2  to the machine P is accomplished by using l2 routing only

Ip addresses

As we are discussing L3 routing then at this instant, IP addresses(logical addressing) play a major role in L3 routing

At the network layer, the main protocol defined by TCP/IP is the internetworking protocol(IP) and it is the role of IP protocol to transport data in a packet

IPv4(IP  version 4) is a 32-bit address, IPv6(IP  version 6) is a 128-bit address and they are unique also universally defines the connection to the device

Here, unique means that each address defines a  connection to the internet, and on the other hand, no two devices will have the same IP address on the internet  

Basic rules for IP addressing

  • (192.016.4.48) there should not be zero in the lead of the number such as we have 016
  • (192.168.1.10.8) always an IP address should have at least four numbers, not more than that, here, in our example we have 8 as an extra number
  • (192.168.401.10) always in an IP address, each number has to be equal to 255 or less but here, in our example 401 is out of range number

Rendered by QuickLaTeX.com

The above figure shows that CIDR or mask  24 can also be written as 255.255.255.0

The IPV4 protocol has the concept of classful addressing where an IP address is divided into two parts

  1. Network  id
  2. Host id

Mask  (or) CIDR

Mask is a 32-bit number that will determine which part of the IP address is network id and host id

For example, mask 255.0.0.0 which can be represented in binary as (11111111.00000000.00000000.00000000)

The 11111111 part is a network id and

Why?

Because the mask has 8 bits which are 1’s that means 8 bits are network id and the remaining 24 will be host id

00000000.00000000.00000000 part is a host part

Another example: suppose we are having mask value as 255.255.255.0/24 and then suppose if we have ip address as 202.100.133.16 then what is the network id?

The network id for the IP address is 202.100.133

Why?

Because we have a mask that contains the first 24 bits as 1’s

CIDR(/8) in decimal notation 255.0.0.0 and  binary 11111111.00000000.00000000.00000000

CIDR(/16) in decimal notation 255.255.0.0 and  binary 11111111.11111111.00000000.00000000

CIDR(/24) in decimal notation 255.255.255.0 and binary 11111111.11111111.1111111.00000000

CIDR(/21) in decimal notation 255.255.248.0 and binary 11111111.11111111.11111000.00000000

Classless interdomain routing (CIDR) notion or slash notation (/n) where n can be 8,16 or 24

what is a loopback interface

The loopback interface is a software-based interface and it is not a physical interface based on hardware therefore, you can create as many as you like

The loopback interface will have a MAC address and it can be configured with an IP address

With a loopback interface, you can not  receive and send data to the other nodes in the topology  and it is used for device reachability in the network

Why do we need a loopback interface?

As you might know, that, only interfaces will have an IP address, not the machines, but there should be some way to identify the machines uniquely through p address  in the network

For this purpose, we create a special interface called loopback interface on the machine and assign an IP address to it

Demo of L3 routing with python

Rendered by QuickLaTeX.com

The above topology shows two host machines h1 and h2 with loopback addresses also three L3 routers with loopback addresses and four subnets with network id

# Mininet Python APIs
#network emulation
from mininet.net import Mininet
#Simple command-line interface to talk to nodes
from mininet.cli import CLI
#virtual Ethernet, link emulator 
from mininet.link import Link
if '__main__' == __name__:
    net = Mininet()
    # Add Hosts
    h1 = net.addHost('h1')
    h2 = net.addHost('h2')
    # Add L3 Routers
    r1 = net.addHost('r1')
    r2 = net.addHost('r2')
    r3 = net.addHost('r3')
    # Add links
    net.addLink(h1, r1 , intfName1 = 'h1-eth0', intfName2 = 'r1-eth0')
    net.addLink(r1, r2 , intfName1 = 'r1-eth1', intfName2 = 'r2-eth0')
    net.addLink(r2, r3 , intfName1 = 'r2-eth1', intfName2 = 'r3-eth0')
    net.addLink(r3, h2 , intfName1 = 'r3-eth1', intfName2 = 'h2-eth0')
    net.build()
    # Remove default IP addresses from host's interfaces 
    h1.cmd("ifconfig h1-eth0 0")
    h2.cmd("ifconfig h2-eth0 0")
    # Assign IP Address to Hosts as ususal
    h1.cmd("ifconfig h1-eth0 10.1.1.1 netmask 255.255.255.0")
    h2.cmd("ifconfig h2-eth0 13.1.1.2 netmask 255.255.255.0")
    # Default Route in hosts
    h1.cmd("ip route add default via 10.1.1.2 dev h1-eth0")
    h2.cmd("ip route add default via 13.1.1.1 dev h2-eth0")
    # config loop back addresses of hosts
    h1.cmd("ifconfig lo 122.1.1.4 netmask 255.255.255.255")
    h2.cmd("ifconfig lo 122.1.1.5 netmask 255.255.255.255")
    
    # config Router R1
    r1.cmd("ifconfig r1-eth0 0")
    r1.cmd("ifconfig r1-eth1 0")
    r1.cmd("ifconfig r1-eth0 10.1.1.2 netmask 255.255.255.0")
    r1.cmd("ifconfig r1-eth1 11.1.1.2 netmask 255.255.255.0")
    r1.cmd("ifconfig lo 122.1.1.1 netmask 255.255.255.255")
    
    # config Router R2
    r2.cmd("ifconfig r2-eth0 0")
    r2.cmd("ifconfig r2-eth1 0")
    r2.cmd("ifconfig r2-eth0 11.1.1.1 netmask 255.255.255.0")
    r2.cmd("ifconfig r2-eth1 12.1.1.1 netmask 255.255.255.0")
    r2.cmd("ifconfig lo 122.1.1.2 netmask 255.255.255.255")
    
    # config Router R3
    r3.cmd("ifconfig r3-eth0 0")
    r3.cmd("ifconfig r3-eth1 0")
    r3.cmd("ifconfig r3-eth0 12.1.1.2 netmask 255.255.255.0")
    r3.cmd("ifconfig r3-eth1 13.1.1.1 netmask 255.255.255.0")
    r3.cmd("ifconfig lo 122.1.1.3 netmask 255.255.255.255")
    #enable IP forwarding on Router
    r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
    #install L3 routes on the router R1 for all remote subnets
    r1.cmd("ip route add to 122.1.1.4/32 via 10.1.1.1 dev r1-eth0") # Route to h1
    r1.cmd("ip route add to 122.1.1.2/32 via 11.1.1.1 dev r1-eth1") # Route to r2
    r1.cmd("ip route add to 122.1.1.3/32 via 11.1.1.1 dev r1-eth1") # Route to r3
    r1.cmd("ip route add to 122.1.1.5/32 via 11.1.1.1 dev r1-eth1") # Route to h2
    r1.cmd("ip route add to 12.1.1.0/24  via 11.1.1.1 dev r1-eth1") # Route to subnet S3
    r1.cmd("ip route add to 13.1.1.0/24  via 11.1.1.1 dev r1-eth1") # Route to subnet S4
    #install L3 routes on the router R2 for all remote subnets
    r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
    r2.cmd("ip route add to 122.1.1.4/32 via 11.1.1.2 dev r2-eth0") # Route to h1
    r2.cmd("ip route add to 122.1.1.1/32 via 11.1.1.2 dev r2-eth0") # Route to r1
    r2.cmd("ip route add to 122.1.1.3/32 via 12.1.1.2 dev r2-eth1") # Route to r3
    r2.cmd("ip route add to 122.1.1.5/32 via 12.1.1.2 dev r2-eth1") # Route to h2
    r2.cmd("ip route add to 10.1.1.0/24  via 11.1.1.2 dev r2-eth0") # Route to subnet S1
    r2.cmd("ip route add to 13.1.1.0/24  via 12.1.1.2 dev r2-eth1") # Route to subnet S4
    #install L3 routes on the router R3 for all remote subnets
    r3.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
    r3.cmd("ip route add to 122.1.1.4/32 via 12.1.1.1 dev r3-eth0") # Route to h1
    r3.cmd("ip route add to 122.1.1.1/32 via 12.1.1.1 dev r3-eth0") # Route to r1
    r3.cmd("ip route add to 122.1.1.2/32 via 12.1.1.2 dev r3-eth0") # Route to r2
    r3.cmd("ip route add to 122.1.1.5/32 via 13.1.1.2 dev r3-eth1") # Route to h2
    r3.cmd("ip route add to 10.1.1.0/24  via 12.1.1.1 dev r3-eth0") # Route to subnet S1
    r3.cmd("ip route add to 11.1.1.0/24  via 12.1.1.1 dev r3-eth0") # Route to subnet S2
    #install L3 routes on the host h1 for all remote subnets
    h1.cmd("ip route add to 122.1.1.1/32 via 10.1.1.2 dev h1-eth0") # Route to r1
    h1.cmd("ip route add to 122.1.1.2/32 via 10.1.1.2 dev h1-eth0") # Route to r2
    h1.cmd("ip route add to 122.1.1.3/32 via 10.1.1.2 dev h1-eth0") # Route to r3
    h1.cmd("ip route add to 122.1.1.5/32 via 10.1.1.2 dev h1-eth0") # Route to h2
    h1.cmd("ip route add to 11.1.1.0/24  via 10.1.1.2 dev h1-eth0") # Route to subnet S2
    h1.cmd("ip route add to 12.1.1.0/24  via 10.1.1.2 dev h1-eth0") # Route to subnet S3
    h1.cmd("ip route add to 13.1.1.0/24  via 10.1.1.2 dev h1-eth0") # Route to subnet S4
    #install L3 routes on the host h2 for all remote subnets
    h2.cmd("ip route add to 122.1.1.1/32 via 13.1.1.1 dev h2-eth0") # Route to r1
    h2.cmd("ip route add to 122.1.1.2/32 via 13.1.1.1 dev h2-eth0") # Route to r2
    h2.cmd("ip route add to 122.1.1.3/32 via 13.1.1.1 dev h2-eth0") # Route to r3
    h2.cmd("ip route add to 122.1.1.4/32 via 13.1.1.1 dev h2-eth0") # Route to h2
    h2.cmd("ip route add to 11.1.1.0/24  via 13.1.1.1 dev h2-eth0") # Route to subnet S2
    h2.cmd("ip route add to 12.1.1.0/24  via 13.1.1.1 dev h2-eth0") # Route to subnet S3
    h2.cmd("ip route add to 10.1.1.0/24  via 13.1.1.1 dev h2-eth0") # Route to subnet S1
    
    # Start Mininet Cli prompt
    CLI(net)
    
    #stop emulator 
    net.stop()

Line 1 to 7: import python API’s for network creation and L3 routing in Mininet

Line 9: if ‘__main__’ == __name__:  is a python function where the execution of python program starts

Line 11: net =mininet () creates a network instance, where the net instance is called by Mininet () constructor and the net is variable

Line 14 to 15: as you know, we are creating a network topology which is mentioned in the above figure so, adding two hosts h1 and h2

Line 18 to 20: adding three L3 routers to the topology which we are about to create

Line 23 to 26: adding four link’s (from h1 to r1, r1 to r2, r2 to r3, and, r3 to h2) with their respective interfaces

Line 28: at this instant, we have finished adding all the physical entities to the topology so, build the network

From here, we will configure all the devices which are present in the topology

Line 31 to 32: first, remove the default interfaces from the host interfaces h1 and h2 which were given by mininet

Line 35 to 36: second, configure the IP address of our choice on the interfaces of host h1 and h2

Line 39 to 40: at this instant, we will install default routes in host h1 and h2 and also in the routing table

Line 43 to 44: configure the loopback address to the host machines h1 and h2

Line 47 to 48: remove the default IP address from the L3 router R1 interface

Line 49 to 51: configure the IP address of your choice and loopback address

Line 54 to 65: similarly configure the L3 routers R2 and R3

Line 68: This command enables the host to work as an L3 router forwarding packets b/w two networks

Note: Here, the net is not a variable which we assign net =mininet() at the beginning of the script so, don’t change it

To disable IP forwarding on Linux machines use 0

Echo  0>/proc/sys/net/ipv4/IP-forward

To enable IP forwarding use 1 instead of 0

Line 70 to 76: install the routes in the routing table to tell the device how to reach the loopback address of every other device in the topology

Line 75 to 76: tells, how to reach a remote subnet

Similarly tells the remaining device R2, R3, h1, and h2

Finally, compile the script and run basic Mininet commands that were available in case if you don’t know, how to compile a script in Mininet and the basic commands of mininet then you check out the blog mentioned below  

For the First blog click here —->

For the second blog click here —->

For Basic Linux commands here —->

You can view the routing table of any device such as for host h1 use the below command

h1 route -n

Similarly, for router r2

r1 route -n

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.