Does Routing Affect a Socket with a Bound Source Address

Choosing socket output interface: SO_BINDTODEVICE vs bind before connect

Source address doesn't affect routing of a packet. The interface will be selected based on the destination address and host's router table. You can modify it with route command.

route default gw [gateway IP]

No matter which interface the socket is bound, the packet is to be routed based on route table. The interface to which the socket is bound will determine the source ip address.

Now, I run some tests binding to loopback interface and connecting the socket to other address in the Internet and in that case connect failed with errno 22 (EINVAL). However, in other tests with two interfaces (none was loopback), and connecting to a server in the Internet, no matter which interface I bound the socket to, the packet was sent out according to the routing table, in my case following the default rule. The sources address depended on the bind.

Packet with Strict Source Route IP Option not being sent

It turns out that accept_source_route=1 was not set in sysctl. Instead, deny_source_route=0 was set - this was the default setting in Raspbian Jessie Lite.



Related Topics



Leave a reply



Submit