Saturday, 20 August 2016

Basics of Routing and NAT



               A router is a Layer 3 device, which works with the IP address. Unlike switches each interface of a router is configured with IP Address. Every router has a routing table containing routes for any IP addresses. All routing tables have a default route.
                                     
                                                Fig.1

                  In fig.1 I have 2 networks named network1 and network2 connected with my router router1.Router1 is also connected with my ISP router Router2. Let us check the routing table for Router1.

 
Destination Address
Net Mask
Gateway
Interface
Metric
192.168.10.0
255.255.255.0
0.0.0.0
192.168.10.1
100
192.168.20.0
255.255.255.0
0.0.0.0
192.168.20.1
100
20.20.20.24
255.255.255.248
0.0.0.0
20.20.20.25
100
0.0.0.0
0.0.0.0
20.20.20.25
20.20.20.30
10

                                 The first line states that whatever the traffic destined to 192.168.10.0/24 network use the interface with IP 192.168.10.1. The 0.0.0.0 in the gateway field indicates that the network is local to Router1 and hence forwarding to another IP is not required. Second and third line states the same, as these networks are local to Router1. The last line indicates whatever the destination IP and whatever the subnet mask send it to Router2 through the interface configured with IP 20.20.20.30. This is called as the default route and every router connected with the internet will have at least one default route.
        Let us connect our Router1 with one more ISP, with Router3 as shown below diagram.


Let me add the new interface to the routing table.

Destination Address
Net Mask
Gateway
Interface
Metric
192.168.10.0
255.255.255.0
0.0.0.0
192.168.10.1
100
192.168.20.0
255.255.255.0
0.0.0.0
192.168.20.1
100
20.20.20.24
255.255.255.248
0.0.0.0
20.20.20.25
100
0.0.0.0
0.0.0.0
20.20.20.25
20.20.20.30
10
0.0.0.0
0.0.0.0
30.30.30.65
30.30.30.70
11

                  Now we have 2 default routes and there is a conflict in choosing the interface to deliver the internet traffic. If multiple routes exist to a given destination network, the metric is used to decide which route is to be taken. The route with the lowest metric is the preferred route. So in our case the internet traffic will flow through the interface 20.20.20.30.


Network Address Translation (NAT)


      In IP Address section we discusses that only public IP is used in the internet communication. In our home or office, we have multiple devices and it is not practical to have public IP for each device. We are using the Private IP range for the home office environments. So how are they communicate with the internet? 
The router translates these private IP with its ISP given public IP and keeps track of all these requests and send them back with the desired information. 


              As we discussed the packet is having the source IP, Destination IP, Source port and destination port etc. along with the data. So the Router will replace the local source IP which is not routable with its Public IP and stores the local IP and port information and forward the translated packet to the Internet. Once it get the response back from the  internet with the routers Public IP as the destination, the router checks the corresponding source and destination port information and as per that replaces the destination IP with the corresponding private IP. NAT is enabled by default in almost all home office routers. In enterprise level routers, you need to enable it manually.

      We were discussing about the outgoing traffic to the internet. What if I have a web server or a webcam in my LAN that I want to access from the internet? My router will receive the HTTP request but in order to reach the web server or webcam I need to configure Port forwarding in my router.


            I have given the IP 192.168.10.5 for my web server. So what I have to tell the router is, please forward whatever the incoming request you receive for HTTP (port 80) to the IP 192.168.10.5 and port 80. Once I configure port forwarding my router will know to forward the request to my web server.


       There are some applications like FTP that will send on one port and listen in different port. If my computer want to access a FTP server it sends the request on port 21. But the FTP server will respond back on port 20. So this will make the router confusing, as it expects response from port 21. So I am just telling the router that if I send any request on port 21, be ready to receive the response from port 20 as well as port 21. This is called port triggering.
 
   
Routing Protocols

                  Now I think you can imagine how many routers will be in the internet backbone. Manually updating the routing table of a big network is not practical. There is a mechanism called dynamic routing through which the routers talk each other and update the routing table itself as per the changes. We learned earlier that the metric value is considering to choose the best route in case of multiple routes. Earlier the metric value was calculated based on the hop count (number of routers passed to reach the destination)

                       The dynamic routing calculates the metric value based on the following.



1.   Maximum Transmission Unit (MTU): It determines the maximum size a frame can hold. In an Ethernet frame this is a maximum of 1500 bytes. But in the internet there are different ways like cable modem, telephone line etc. other than the Ethernet. Each technology have different protocols. So if an Ethernet frame enters in to a different device that having a lower MTU, it has to be chopped causing more traffic and delay.

2.   Bandwidth: The bandwidth of each interface connected with the router
3.   Cost: The cost of the carrier.
4.   Latency: The time take to get response from the route.
      Different routing protocols choose different ways to calculate the metric based on the above factors. Dynamic routing protocols are divided in to distance vector and link state protocols. Distance vector protocols mainly calculates the metric based on the hop count. Also it will send the entire routing table to its neighbors, even if a single change occurred. Link state protocol on the other hand check the link status and updates only the changes occurred in the network. So they are converging faster than distance vector protocols.
        All dynamic protocols are divide into Interior Gateway Protocol (IGP) and Exterior Gateway Protocol (EGP). A company like ISP have lot of routers they owns. These group of routers under one authority is called as an Autonomous System (AS). If they want to communicate outside the AS, they have to use EGP. Inside AS they are using IGP.


Routing Information Protocol (RIP)



 RIP is one of the oldest dynamic routing protocol. It is still used in smaller network. It is a distance vector, interior protocol.







As shown in the diagram I have 3 routers named R1, R2 and R3 connected with NW1, NW2 and NW3 respectively. Each router is RIP enabled and it updates the route periodically. Each router has the route to its connected network. After the first update R1 knows the route to NW2 (Through R2) and NW3 (Through R3). Similarly R2 and R3 have its neighboring networks. In the nest update R1 gets a new route to NW3 (through R2) and NW2 (Through R3). Now it has 2 routes to the same destination. In that case it checks the hop count to delete the long path. So in our case R1 deletes the path to NW2 through R3 as it has to pass 2 routers to reach NW2. As there is a fixed time interval to get the neighbor routing table, it takes more time to converge the routes. RipV1 can work with class full address only. RIPV2 can work with classless addresses and security built in to it. RIP has a maximum hop count of 15 and after that it is considered as unreachable network. That is why it is applicable only in smaller network.


Open Shortest Path First  Protocol(OSPF)


  OSPF is a link state interior protocol. Each router configured with OSPF advertise the link state to neighbor routers. Based on the link state information it creates a link-state database. Based on these database it calculates the best shortest path to the reachable network. OSPF will then create 3 tables to store the following information.

1. Neighbor Table: Contains all discovered neighbors with whom routing information     will be interchanged.
2. Topology Table: Contains all OSPF routers, best path and alternate path.
3. Routing Table: Contains current best routes to forward the traffic

  OSPF uses grouping of well-connected routers known as area. Area ID is used for this logical separation.


Border Gateway Protocol (BGP)

  BGP is a hybrid Exterior protocol. It is the primary protocol for the internet. As we discussed earlier Exterior protocols are used to interconnect the Autonomous Systems. Each As in the internet is have an ASID. BGP uses these ASIDs to get the corresponding AS.
 
 

No comments:

Post a Comment