I've been trying to independently verify the answer to this stackoverflow question about how the Linux kernel assigns a source address to IP packets when the source doesn't request a specific source address. One of the comments refers to this source which contains the following sentence:
lacking this hint, the kernel will choose the first address configured on the interface which falls in the same network as the destination address...
I think this sentence could be interpreted in two ways:
- The kernel picks the interface which sits on the same network as the destination address, then picks the first address assigned to that interface.
- Of the addresses assigned to the interface, the kernel picks the first one which sits on same network as the destination address.
I think the first one is the naïve (i.e. purely grammatical) interpretation of that sentence, but the second one is how I would have guessed the kernel chooses. My reasoning is this: Assume, for simplicity, there are no gateways/routers on this network. How could the destination send a response (e.g. TCP ACK) to the source address if the selected source address isn't on the same network (which is possible in the first interpretation)?
Which interpretation is correct?
- If it's the first, how does the destination route a response back to the source?
- Is this behaviour documented anywhere (ideally the Linux man pages)?
route add default dev wan0), then any network matches. While it still matches the 2nd case, it also matches the 1st case. So I'm not sure simplification will help for the actual problem.