When a host creates an IP packet, it must select some source address. Correct source address selection is a critical procedure, because it gives the receiver the information needed to deliver a reply. If the source is selected incorrectly, in the best case, the backward path may appear different to the forward one which is harmful for performance. In the worst case, when the addresses are administratively scoped, the reply may be lost entirely.
Linux-2.2 selects source addresses using the following algorithm:
bind(2)
syscall or supplying it to sendmsg(2)
via the ancillary data object
IP_PKTINFO
. In this case the kernel only checks the validity
of the address and never tries to ``improve'' an incorrect user choice,
generating an error instead.
1cm NB.
Never say ``Never''. The sysctl option ip_dynaddr
breaks
this axiom. It has been made deliberately with the purpose
of automatically reselecting the address on hosts with dynamic dial-out interfaces.
However, this hack must not be used on multihomed hosts
and especially on routers: it would break them.
src
parameter
to the ip route
command, sec.7, p..
scope
parameter to the ip route
command,
sec.7, p.).
link
or host
,
the algorithm fails and returns a zero source address.
lo
is always the first
in the search list, so that if an address with global scope (not 127.0.0.1!)
is configured on loopback, it is always preferred.