On Sun, Oct 16, 2016 at 11:55:33PM +0200, Neels Hofmeyr wrote:
So normally when I send something, the routing table
(ip r show) determines
which local interface the remote side sees as sender.
Yes. Please note 'ip r show' will only show you 'table main'. Since
Linux 2.2.x (yes, ages ago) Linux supports and uses multiple routin
tables. See 'ip route show table all' to see all routes that exist in
the stack.
If you didn't bind a socket and send a packet, it works more or less
like this:
* The kernel performs a route lookup and finds an outbound route for the
destination. If the route contains the src parameter, the kernel
selects this IP address for the outbound packet.
* If no 'src' parameter is in the route, the kernel will choose the
first address configured on the interface which falls in the same
network as the destination address or the nexthop router.
See
http://linux-ip.net/html/routing-saddr-selection.html
If I connect() and bind() at the same time, would that
bypass the routing table
and the bind() address would be the sender?
yes, if you explicitly bind before using the socket, you will bypass the
regular automatic source address selection of the kernel.
And that's why we don't really need it,
because it doesn't make sense to send
from a different interface than indicated by the routing table?
well, it sometimes makes sense in more complex scenarios with
multi-homed machines, but I think we can ignore that until somebody
requests it.
Also, in a lot of cases it makes sense to store such configuration in
the routing table itself, rather than inside the applicatin config.
Chances are that other applications/services wanting to reach the same
destination address/network will have to choose the same source address.
So something like 'ip route add 8.8.8.8/32 via 192.168.100.1 src 1.2.3.4'
will teach the routing table to choose the local source address 1.2.3.4
whenever sending packets to 8.8.8.8 and route them via the gateway
192.168.100.1. This configuration is valid system-wide, for all
applications/sockets. Please note that 1.2.3.4 must be a valid local
address (i.e. one that has been added using 'ip addr add 1.2.3.4 ...'
before.
Regards,
Harald
--
- Harald Welte <laforge(a)gnumonks.org>
http://laforge.gnumonks.org/
============================================================================
"Privacy in residential applications is a desirable marketing option."
(ETSI EN 300 175-7 Ch. A6)