How does a router on the Internet know where to send a packet to next to get it towards its destination IP address? [closed]

I've been doing small-time freelance IT work (among other things) for about the last decade now. I've set-up or rebuilt more local network arrangements than I can care to count, and more recently I've been studying up on some of the big gaps that remained in my networking knowledge. All-in-all, while I most definitely wouldn't call myself an "expert" I think that at this point I have a decent grasp of how things work inside a network perimeter, and a fair understanding of most networking fundamentals (the layers of the OSI stack, TCP/IP, DNS, MAC addressing & ARP, higher-level protocols, etc.)

However, the other day something occurred to me: "How would I explain to someone how routing a packet on the Internet actually works?" And I realized I'd have trouble answering that question.

Now, I do have at least some understanding of what, for example, Border Gateway Protocol is & does, or what a routing table is, or what Internet infrastructure peering points are. But I'll admit that I don't really understand how those things and others actually determine what a router will actually do with a packet to move it towards its destination IP address.

So, how does a router on the Internet that receives a packet decide which router to forward it to next? And how does that router then know the best router it should forward the packet to? As I said, I have some understanding of elements & concepts related to the Internet routing infrastructure, but I'm unclear about how everything fits together to actually allow a given router to make a good routing "decision" about where to send a packet that comes to it to do its part in correctly moving the packet towards its final destination.

(Note: I've looked at any of number of resources to try to gain a better understanding of this. But most things I've come upon thus far have either (1) elided over the technical topic of how a router on the Internet knows where to send a packet for the next hop, or (2) gotten so bogged down in technical details & jargon about protocols and such that it's been hard to gain a picture of how everything comes together at a router's decision point. And I thought this would be as good a place as any to get an actual technical answer to that question that might also be comprehensible to a non-expert in networking.)


Solution 1:

As you already mentioned BGP is you basic answer.

I'll try to summarize this, but the answer can get pretty long and complicated.

IP addresses are distributed by ARIN + few other similar international organizations which are tasked w/ distributing and keeping track of IP address space.

An ISP's border routers (where ever they might be) then advertise that they are the the destination for said IP address space, and they can also advertise the best path to take to them.

There are also various algorithms, and settings and etc that a Network Engineer can use to configure a router to take specific routes to get to a destination. For example avoiding a known bad carrier, or more complicated scenarios.

But again, what you're looking for is BGP and it's many options.

Some links for further reading: http://searchtelecom.techtarget.com/feature/BGP-essentials-The-protocol-that-makes-the-Internet-work http://www.cisco.com/c/en/us/support/docs/ip/border-gateway-protocol-bgp/13753-25.html

Solution 2:

Routers use routing protocols to distribute information about the location of each network on the Internet among themselves. Each router that participates in the routing protocol builds up a routing table (other routers mostly use pre-configured default routes). Each routing table entry essentially says "To reach destinations within prefix P, send through interface I to next-hop address A" (unless the destination is on a directly-connected network, in which case there's no next-hop address).

When a router wants to send to a destination, it looks for the entry with the longest prefix that matches it, and then follows its direction to forward the packet. Each router along the path uses this same mechanism, and the packet eventually reaches its destination.

Actual implementations make use of optimizations, such as caches and hardware features that automatically match addresses against routing tables, but the above is the basic algorithm.

Solution 3:

A router has a routing table filled with information about where to send packets. If it's a simple home router, it probably has two entries; local network (e.g. 192.168.1.0/24) is directly connected on LAN interface and everything else is sent via the default route on the WAN interface to the gateway address (next hop), which is a router managed by the ISP.

The routing table can be filled manually if they are as simple as this example, or by running some routing protocol like BGP or OSPF, that talks to the peers and computes the contents of the routing table.