1

If a socket isn't bound to a specific local IP address, how exactly does the TCP/IP stack select a source IP address to fill in the packet's IP header? An example explanation would be great.

Xin
  • 69
  • 1
  • 9
  • 1
    Does this help? https://stackoverflow.com/questions/5755265/how-to-set-source-address-when-sending-using-and-udp-socket. It's not an exact match, but similar. – Kevin Boone Sep 18 '20 at 12:41
  • The source address selection is described here. http://linux-ip.net/html/routing-saddr-selection.html – Xin Sep 18 '20 at 16:54
  • I've voted for reopening. The claimed duplicate does not really answer this question, i.e. the most it says is that the kernel somehow "looks around for interfaces and selects a source address" without describing this selection process in any detail. This does not match the question for *"how exactly ... select the source IP address"*. Any more details are only in additional links, not in the answers. – Steffen Ullrich Sep 18 '20 at 17:27
  • FWIW I agree. I said the link I posted was similar, not a duplicate. – Kevin Boone Sep 18 '20 at 20:05

1 Answers1

2

If no explicit source IP is given then one is determined on TCP connect. Based on the routing information to the target the kernel picks the outgoing network interface and then assigns the IP address of this interface as source IP. If the interface has multiple IP addresses configured the first one is used, although the internal order of the IP addresses might not be obvious.

With UDP, ICMP etc the situation is similar.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • "If the interface has multiple IP addresses configured the first one is used, although the internal order of the IP addresses might not be obvious." Is there any way to determine the internal order? – Ian Pilcher Aug 09 '23 at 15:54