IP Routing - how does it work again? [closed]
Can someone explain how IP routing works to me?
E.g. if I ping 123.123.123.123, how does each router know which way to go as shown in a traceroute
dump.
Solution 1:
This explanation will be simplified let me know if you want more details. It's not completely accurate but it should give you a a good explanation.
It basically bubbles up. When you do ping x. If your computer doesn't have address x in it's routing table it goes to it's gateway (if you look at your routing table it will be the one that has destination default or 0.0.0.0
) then it will look at it's routing table to see if it has a route that matches this address or IP range if for example if it had 123.123.123.0/24
as a route it would send it to what ever IP address it has associated with that route. If it doesn't it will do the same as your computer, send it to the default route.
For your computer for example, this will end when your ISP receives this and it will send it to what ever peer it knows has a route for this address (or is likely too, they share routing tables at that level). Then the reverse process happens, it goes down towards more specific ranges until it reaches the computer in question.
- your compter(no match, send to default route)
- your router (no match, send to default route) ->
- ....
- router X (match found for
123.0.0.0/8
with associated address 10.10.10.1) - router at 10.10.10.1 (match found for
123.123.0.0/16
with associated address 123.123.0.1) - router at 123.123.0.1 (match found for
123.123.123.0/24
with associated address 123.123.123.1) - router at 123.123.123.1 is directly connected to that computer(or thru a switch) and it sends it the packet because it knows it's network card mac address.
The commands route
and traceroute
(tracert
for windows) might help make this clearer. On unix these commands have a -n
option that make them show the address and not the DNS name of the particular address.
using route, my default route is show do be:
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
the IFACE here is important because it tells the computer what network card is connected to this router which is at 192.168.1.1.
My computer will send all packets with unknown location through the eth0
network card.
Solution 2:
Each router gets an IP packet with a destination address. It will pick a next hop, based on the most specific route that matches the destination.
If we start from your computer, it will have two (there may be more, but "two" is the typical case) routes installed. One is for the LAN it's on, the other is for 0.0.0.0/0 ("the internet", usually called the default route). If the machine you ping is not local to the LAN, the packet will be sent to the default gateway.
Then, each intermediate router, until the router that has the destination LAN attached, will do the same thing. Look up the most specific route to the destination, then send teh packet on.
The intermediate routers will, most probably, have learned of the best route using one or more routing protocols (BGP, OSPF or similar), both to lessen the administrative burden and to provide a measure of fault tolerance.