How do PCs tell what network connection to use to connect to the internet?

2

If your pc (lets assume windows) is connected via a wired LAN and has a USB dongle that supplies 3G internet, how does your pc know which connection to use when browsing the web? Does linux follow the same rules as windows?

rumz

Posted 2010-09-09T13:24:12.823

Reputation: 41

Answers

2

Any time multiple network connections are involved (on any OS), the routing table identifies which connection to use.

I'm going to explain the routing table using a Linux example, but Windows is effectively the same. The main difference is how it labels the interfaces.

Let's say you have 3 network connections that each are connected to different networks, like this:

eth0 -> 10.0.0.0/24
eth1 -> 172.16.0.0/24
eth2 -> 192.168.0.0/24

By default, as your system configures the network connections, it will add routes for each of these networks to your routing table. Here's an example routing table for a server with the connections shown above:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
172.16.0.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth2
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         10.0.0.1        0.0.0.0         UG    0      0        0 eth0

So, if you want to connect to one of those networks, your Linux system will decide which connection to use based on the destination network. If you want to connect to a server with the IP 10.0.0.233, it will use eth0. If you want to connect to 192.168.0.4, it will use eth2.

So, what about networks that aren't specifically listed in the routing table? Well, that's where the default route comes in. Notice the last line in that routing table:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
...
0.0.0.0         10.0.0.1        0.0.0.0         UG    0      0        0 eth0

The short version is that any traffic destined for a network that isn't specifically listed in the routing table will be sent through the default route. In the example above, that is eth0, sending specifically to the router at 10.0.0.1.

The longer version is that routing decisions are based on network prefix length. A given packet will follow the route with the most specific matching destination. Here is an example list of networks in CIDR notation that range from most to least specific:

10.100.4.0/24  <-- Most specific
10.100.0.0/16
10.0.0.0/8
0.0.0.0/0      <-- Least specific

So, given a destination IP of 10.100.6.231, the most specific matching range in the above list is 10.100.0.0/16, so the route for that network would be followed. If the destination is 144.92.12.24, the most specific matching range is 0.0.0.0/0 (which is the default route). Once the routing decision is made, then the system will send the traffic down the appropriate network connection.

This got a little long-winded, but I hope that it explains things. In general, the default route decides what connection internet traffic will go through. The default route is configured differently on Windows vs Linux, but ultimately everything works on the same underlying routing mechanism.

baumgart

Posted 2010-09-09T13:24:12.823

Reputation: 1 176

+1 - Great detail. I added to my answer that you have the definitive info for linux. – JNK – 2010-09-09T15:59:03.537

1

Windows using binding order.

This is basically an ordered list of connections to try. Windows will try the first connection, and if unsuccessful will go to the next, etc. For internet it will almost always use the first. The second connection may be used if you are on a LAN and a WAN, and the second NIC connects to a certain IP schema (192.168.4.X for example) that is not a part of the first NIC.

I don't know enough about Linux networking to address that, but some quick searching shows it may work similarly. I'll check some more and come back with an edit if I have a definitive answer.

EDIT:

baumgart's answer below is better for Linux...

JNK

Posted 2010-09-09T13:24:12.823

Reputation: 7 642

Centos and Ubuntu – rumz – 2010-09-09T13:43:15.680

@ roomy - added detail to the answer. – JNK – 2010-09-09T13:52:11.617

FYI, Linux bonding is not what you're looking for here. Bonding ties multiple interfaces together to provide redundancy to a single switch, but using multiple NICs on different networks does not work in bonding. – baumgart – 2010-09-09T15:27:57.190

@Baumgart - thanks for the clarification. Can you offer any info on the proper command? – JNK – 2010-09-09T15:34:47.023

Well, as my answer below says (in painfully horrendous detail), it's all about the routing table. Every distribution configures the network slightly differently, but they all set the default route somehow. Doing it manually at the command line uses the 'route' command, typically 'route add default gw 10.0.0.1' (or whatever IP is your default gateway). – baumgart – 2010-09-09T15:55:32.450