If I have two routers, how do computers choose which to use?

2

We have two ISPs at home, and I've connected a different router to each and both routers into our LAN, one large ethernet switch that everything connects to. One router has the IP 192.168.1.1 and connects through Time Warner Cable, and the other has 10.0.1.1 and connects through Verizon. Both are DHCP servers as well.

For whatever reason, anything with DHCP enabled seems to take a DHCP lease from 192.168.1.1, thus accepting 192.168.1.1 as the router and connecting to the Internet through TWC. I don't know if that's because that router just always "wins" a race condition or if there's some actual priority. How do computers choose which router to take the DHCP lease from?

I don't have enterprise-level hardware. The router on TWC is an Apple AirPort Extreme, and the one on Verizon is the Actiontec they gave us. AFAIK I can't set up either to host one DHCP server for both routers. I've got a Mac server that can serve DHCP, but I can't rely on it. And when I Google for solutions involving multiple routers, I get people who think that router = wireless access point.

sudo

Posted 2016-07-16T21:59:15.487

Reputation: 549

1Keep in mind that a router will probably distribute DHCP addresses even when its internet is down. (It may come back any second!) You may want a multi-WAN Router that is transparent to all the devices behind it. – DasKrümelmonster – 2016-07-16T22:12:26.243

What is the Actiontec connected to? Ethernet? DSL line? Cable? Fiber? (I'm guessing the AirPort is connected via Ethernet to the cable modem?) – user1686 – 2016-07-16T22:12:34.033

just read your title not your whole post.. I think your computer has a default gateway specified.. Gateway=router. And it might show in your computer's routing table (your computer has a routing table, and has the potential to be a whole router, but by default doesn't take traffic on input and forward it on, so is not a router.. but still has a routing table). And the routing table decides where to route traffic that originates on that computer. – barlop – 2016-07-16T22:15:31.323

@grawity The Actiontec is connected to the Verizon modem via ethernet. AirPort is connected to the TWC modem via ethernet. – sudo – 2016-07-16T22:23:17.860

@barlop Unless I'm misunderstanding, the routing table seems to be set after the computer chooses a DHCP server (however it does that). – sudo – 2016-07-16T22:24:44.680

1@sudo: Ok, so if you got a multi-WAN router, you'd connect it via ethernet to both modems directly, without using the current ones. – user1686 – 2016-07-16T22:26:06.013

Hmm. I guess that would be ideal then. – sudo – 2016-07-16T22:31:27.307

1It does not make sense to have two DHCP servers on the same LAN. Yes, there is a race condition. Does it really make sense to have two ISPs on the same LAN at all? Once they are behind a switch, you kind of lose any benefit of having them both, unless your switch has special rules for routing specific traffic to one ISP or the other. In which case, the switch should be repkaced with a 3rd router that acts as the DHCP server for the LAN. ISPs are separate networks, a router is used to join multiple networks together. Switches are for joining segments of the same network together. – Remy Lebeau – 2016-07-16T22:33:09.920

Answers

2

The first DHCP reply to arrive usually wins, there's not much more to it. (This means that running two DHCP servers within the same segment can get quite messy.)

In your case, you would need a single router that was connected to both WAN connections, instead of two. (If you can't find a dedicated one, some people use an old PC with pfSense as a multi-WAN router.) That is, you would have something like this:

                ┌───────────┐
TWC ──<cable>── │cable modem│ ──<ethernet>───┐
                └───────────┘              ┌──────┐                ┌─LAN──┐
                                           │router│ ──<ethernet>── │switch│
                  ┌─────────┐              └──────┘                └──────┘
Verizon ──<???>── │ ? modem │ ──<ethernet>───┘
                  └─────────┘

The router then would have WAN IP addresses assigned directly from both ISPs. (Though that's not a strict requirement – it would survive even with a 192.168 address on the WAN interface, as long as each interface was on a different subnet. But, that's messy.)

Depending on the router, it could choose ISPs based on device, ping latency, time of day, etc.

user1686

Posted 2016-07-16T21:59:15.487

Reputation: 283 655

Thanks! That's what I was afraid of. I'm not willing to dive into the dual-WAN router yet, but maybe I will later. For now, I'm going to try using the Mac DHCP server and see how well that works. It lets me specify multiple address ranges from different routers. – sudo – 2016-07-16T22:27:54.883

Wow, the OS X DHCP server is really limited. The only way to do it is to create a bunch of tiny ranges, alternating between the routers, as it seems to assign clients to fill the ranges in order. (I no longer have two ISPs anyway.) – sudo – 2017-09-08T00:37:48.697

You most likely can install an alternative (e.g. ISC DHCPd) via macports. But that's irrelevant – DHCP still only specifies a single gateway address; you need the gateway itself to be smart about where to send the traffic. – user1686 – 2017-09-08T07:26:50.023

My original goal was to be less smart about it and just assign clients evenly between the routers, with one ISP per router. – sudo – 2017-09-08T17:12:09.667

3

The DHCP protocol specification allows for multiple DHCP servers, even serving different DHCP address pools (ranges), on the same LAN. However, the spec leaves it up to the client implementation to decide which lease offer to accept if it has received multiple offers because of the presence of multiple servers.

It's likely that some DHCP client implementations just take the first offer they get, making it a race.

However, I know that Apple's DHCP client, used in macOS (formerly OS X, Mac OS X), iOS, tvOS, and watchOS, waits a certain brief amount of time to gather offers, and then selects the one containing the most DHCP Message Options. The trick here is that corporate DHCP servers usually have more configuation information they're trying to push to clients, such as WINS servers, LDAP servers, etc. (as compared to consumer home gateway routers’ DHCP servers, which add relatively few Message Options). So by taking the offer with the most DHCP Message Options, it's more likely to be taking the real offer from the official corporate server, and less likely to get derailed by some doofus plugging in a misconfigured wireless router to the office LAN.

I haven't looked at the docs or sources for other common DHCP clients, so I don't know what other BSDs, Android or other various flavors of Linux, or Windows do.

tl;dr: It's up to each DHCP client to pick which offer it accepts. Some use smart heuristics but others may be dumb and just take the first thing they get.

Spiff

Posted 2016-07-16T21:59:15.487

Reputation: 84 656

Thanks, this is good to know (I read the whole thing). I've been in a house where someone somehow plugged in a router that started fooling about half the devices. I guess they were equal in terms of our heuristics, mostly Macs running OS X. – sudo – 2016-07-16T22:45:04.487

2

It is a race condition, and whichever router responds with an IP address to the device, wins its connection. If you set both of your routers to use the same subnet, then you could potentially have the failover that you're looking for. I may be wrong on how it's fully set up though. What you can do is set the two routers on your network to the same IP scheme, (ie: 192.168.0.1 and 192.168.0.2). It would probably be best to disable DHCP on one of them too. Then add routing tables on your computers so that both are in there, but have the internet you want to use more have a LOWER metric value. Lower metric values are used more often than higher ones. If your primary router goes down, it should automatically switch over. Been a while since I had to do anything like that without a smart switch or such, so I could be wrong.

Blerg

Posted 2016-07-16T21:59:15.487

Reputation: 1 094

Unfortunately, I don't have the option of editing routing tables on computers. This is a home network where people with iPhones and such are hopping on and off the wireless APs, and guests bring their own laptops. – sudo – 2016-07-16T22:30:09.863