From: [email-masked] (Charles Hedrick)

Let's see if I can come up with a general explanation of how to use
"route" under Linux.  I'll give some "cookbook" examples at the end.
For better or worse, this is going to be slightly different than for
other variants of Unix.  That's because as of 0.99pl15, Linux does not
create a route automatically when you enable an interface.  On the
other hand, it does supply an additional option that I haven't seen
elsewhere: the ability to specify an interface name instead of a
gateway.

WARNING: this presentation assumes that you are using at least
0.99pl15, libc 4.5.19 (4.5.8 will *not* give the same results), and
the current version of route.  (If there's any question, you can get a
copy of route from athos.rutgers.edu in /pub/linux.)

I suggest a three-stage strategy for doing route:

1) enable all your interfaces, using ifconfig, dip, or whatever.  Make
sure that you specify the correct netmask for them, if your network is
subnetted.

2) set up the network route for each interface.  

2a) For an Ethernet, this is the net or subnet to which it is
attached, e.g.

   route add 128.6.0.0 dev eth0

or if we set the subnet mask to 255.255.255.0 in the ifconfig (as we
really should)

   route add 128.6.157.0 dev eth0

2b) For a point to point link, it is best to think about what traffic
you want to send over the link.  If many cases it will be the default.
In that case you'll do

   route add default dev sl0

In other cases, you may want to send all traffic for a specific network
(e.g. the network to which the machine at the other end is attached).
In that case use that network, e.g.

   route add 128.6.4.0 dev sl0

This assumes that net 128.6 is subnetted, with a netmask of 255.255.255.0.
If you haven't mentioned any subnet masks, then you'd want just the main
network number, i.e.

   route add 128.6.0.0 dev sl0

In the most general case, e.g. if you've got more than one interface,
and want to use routed or gated to adjust routes dynamically, you may
want a fixed route only for the specific host at the other end of the
link.  E.g.

   route add 128.6.4.54 dev sl0

3) Now create routes for any destinations not already mentioned.  If
you are running routed or gated, that will handle things for you.
Otherwise, most likely you just need a default route, if you haven't
already defined one.

3a) For Ethernet, a default route should point to a router on your
Ethernet, e.g.

   route add default gw 128.6.157.1

3b) For a point to point line, a default route can simply point to the
line itself.  Since there's only one thing at the other end, there's
no need to specify a gateway.  E.g.

   route add default dev sl0

3c) If you have more than one interface, and want to route specific
networks to specific interfaces, you can use several route statements
like those in (3a) or (3b), each one mentioning a single network or
host that you want to go a certain way.  Examples:

Ethernet:

   route add 128.6.5.0 128.6.157.2

point to point:

   route add 128.6.26.0 dev sl0

In the case of Ethernet, you have to send traffic to a specific router
on the Ethernet.  In the case of a point to point line, you just send
it to the line.  (The assumption is that the system at the other end
of the line is a router. The line only goes one place.)

This will work correctly if you've defined the correct netmask for all
of your interfaces using ifconfig, and if all subnets on your network
have the same netmask.  This is normally true.  If not, you'll need to
give more attention to the netmasks.

ABOUT SUBNET MASKS

When you do something like
   
   route add 128.6.0.0 dev sl0

this will send all traffic for network 128.6.0.0 out the slip line.
But what is network 128.6.0.0?  Is it every address starting with
128.6, or just addresses starting with 128.6.0?  I.e. what "subnet
mask" is used?  (Actually, it's considered bad form to use subnet 0,
so there would not be a subnet called 128.6.0.)  The answer is that
the Linux kernel will make some attempt to guess what you mean, but
you may need to specify it.  If the interface you're referring to has
a subnet mask defined by ifconfig, and the destination matches the
interface's address, it will use that subnet mask.  Otherwise it will
use the major network.  That needs some examples:

Let's suppose that eth0 has address 128.6.157.5 and netmask
255.255.255.0.  That would have been set up using

   ifconfig eth0 128.6.157.5 netmask 255.255.255.0

That netmask means that the network is subnetted, so that anything
starting with 128.6.157 is considered to be on the subnet.  Now
suppose you say

   route add 128.6.2.0 gw 128.6.157.2

This will send subnet 128.6.2 to gateway 128.6.157.2.  Since the
destination has the same major address (128.6) as the gateway, Linux
uses the netmask for that interface, i.e. 255.255.255.0.  This isn't a
perfect heuristic.  It assumes that all subnets on your network have
the same netmask.  But it's right for most places.  If it isn't for
you, you can specify the subnet mask explicitly:

   route add 128.6.2.0 gw 128.6.157.2 netmask 255.255.255.128.

If you use a destination on a different major net, Linux has no
way of knowing the subnet.  So it will assume that the whole 
network is meant.  E.g.

   route add 18.0.0.0 gw 128.6.157.2

This says to send all of net 18 to gateway 128.6.157.2.  Since we
don't know anything about the netmask for net 18, we simply assume the
whole network is meant.  Again, you could specify an explicit subnet
mask if necessary.

COOKBOOK EXAMPLES

I will give specific instructions for the two most common cases: a
single Ethernet and a single SLIP line.  If you have more than one
interface, you're probably going to need to understand what's going
on.

For a single Ethernet, the simplest setup is

  ifconfig eth0 128.6.157.3 netmask 255.255.255.0
  route add 128.6.157.0 dev eth0
  route add default gw 128.6.157.1

Of course you use your own addresses and mask:

     128.6.157.3 is assumed to be the address of your system
     255.255.255.0 is assumed to be the correct subnet mask for your network
     128.6.157.0 is your local subnet (i.e. your address and'ed with your mask)
     128.6.157.1 is asssumed to be the default router for your network

Most of this information would be obtained from your network
administrator.

For a single SLIP connection, the simplest setup is

  use dip to enable the line.  This does the equivalent of ifconfig.
  route add default dev sl0

You don't really need to worry about setting up the subnet for SLIP if
that's your only interface.  In more complicated setups, you'll need
to use ifconfig on sl0 to set the netmask.