# How does the kernel decide which route (of 2) to use?

## numerodix

Suppose you are connected to two networks on two interfaces. The networks are configured identically.

You get something like this:

```
$ /sbin/route  -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.0.0     0.0.0.0         255.255.255.0   U     2000   0        0 wlan0

127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

0.0.0.0         192.168.0.1     0.0.0.0         UG    2000   0        0 wlan0
```

So now a packet destined outside 192.168.{0,1} must travel either via 192.168.1.1 or 192.168.0.1, but the choice is arbitrary. It might turn out that one of them is a dead end, that past the gateway there's a network partition. Maybe that's why you're on two networks to begin with. So how does the kernel decide which gateway to use?

----------

## Chaosite

When doing complex things with routing, you should use the iproute2 tools.

$ /sbin/ip route

10.0.0.0/24 dev eth2  proto kernel  scope link  src 10.0.0.127 

127.0.0.0/8 dev lo  scope link 

default via 10.0.0.254 dev eth2

And the, suprising enough, answer is: the kernel uses the first route which is available to it. You can do more complex stuff than that, if you desire, though - there are several advanced routing HOWTOs on the net.

----------

## Monkeh

Uh, that's what the metric is for.

----------

## Chaosite

 *Monkeh wrote:*   

> Uh, that's what the metric is for.

 

... Really? What is the metric for, anyway?

manpage isn't very helpful with "set the metric field in the routing table (used by routing  daemons) to M.".

----------

## numerodix

manpage also says:

```
Metric The ’distance’ to the target (usually counted in hops). It is not  used

              by recent kernels, but may be needed by routing daemons.
```

@Chaosite

What makes iproute2 more worthwhile? Am I not getting the same info from "ip route" as I am from "route"?

----------

## jdmulloy

So I'm thinking the question is basically, if both private networks have a gateway to say the public internet and one of them is down is the kernel smart enough to work around it?

----------

## aidanjt

It'll go by shortest (or only) route first.  The kernel maintains different kinds of tables on the network state and make routing decisions based upon that.

----------

## numerodix

 *AidanJT wrote:*   

> It'll go by shortest (or only) route first.  The kernel maintains different kinds of tables on the network state and make routing decisions based upon that.

 

But before any traffic has been sent, it doesn't have any cached information. So what happens? Does it try one first, then the other if the first doesn't work?

----------

## aidanjt

 *numerodix wrote:*   

> But before any traffic has been sent, it doesn't have any cached information. So what happens? Does it try one first, then the other if the first doesn't work?

 

Normally it sends out a RIP request first to update it's own tables, if it can't do that, then it falls back on the send'n'pray method and maintains routing tables by its sweet lonesome.  But normally the route for a given network only has one interface path, unless we're talking about backbone routing of course.

----------

## bunder

shouldn't there be only one default gateway?   :Confused: 

----------

## aidanjt

 *bunder wrote:*   

> shouldn't there be only one default gateway?  

 

Nope, you can have multiple routes to the tubes.

----------

## Chaosite

 *numerodix wrote:*   

> @Chaosite
> 
> What makes iproute2 more worthwhile? Am I not getting the same info from "ip route" as I am from "route"?

 

http://lartc.org/howto/lartc.iproute2.html

Here's how to add a multipath route (google this term) using iproute2:

```
ip route add default nexthop via 10.0.0.1 dev eth0 nexthop via 10.1.0.1 dev wlan0
```

You can add weight to it too. Note that if your failures cause the interface to go down, you can just have 2 default routes, and once the interface goes down one of them will disappear. It's up to you to put it back once the interface goes back again.

----------

## desultory

Moved from Off the Wall to Networking & Security.

----------

