Fix bug of not explicitly binding to socket in send-linux#956
Merged
Conversation
… IP Packets (AF_INET sockets)
zakird
approved these changes
Nov 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It seems that since we're using raw IP sockets, we need to explicitly bind to the socket to ensure we use it. I don't claim to fully understand what's happening internally here, but I tested extensively and everything seems to work as expected.
The entry in
man socketforSO_BINDTODEVICEhere mentions it as the socket option for binding to a IP socket. What it doesn't say and what I couldn't find documented anywhere is why the prior approach of setting the interface index on the socket works with raw sockets and sometimes IP sockets, but not always. In conclusion, not sure why we need to also add this socket option when using IP sockets, but it does seem to fix the problem.Testing
Each case was run while
tcpdumpwas running to confirm the correct interface was selectedLinux (Ubuntu 24.04)
tcpdumpsudo tcpdump -i any -nn '(tcp and dst port 53 and host 1.1.1.1)' -qBase Case - Wireguard not running - Send TCP 1.1.1.1:53, interface not specified
Base Case - Wireguard not running - Send TCP 1.1.1.1:53, interface specified as loopback
gateway-mac, packet sent onloafter specifying (expected)Base Case - Wireguard not running - Send TCP 1.1.1.1:53, interface not specified,
--iplayerBase Case - Wireguard not running - Send TCP 1.1.1.1:53, interface specified as loopback,
iplayerWireguard running, AllowedIPs
0.0.0.0/0(all traffic thru Wireguard) - Send TCP 1.1.1.1:53, no interface specifiedwg0interface, expectedWireguard running, AllowedIPs
0.0.0.0/0(all traffic thru Wireguard) - Send TCP 1.1.1.1:53, Specifywg0wg0network, expected. Fixed error msg to inform users of using the--iplayerflag.Wireguard running, AllowedIPs
0.0.0.0/0(all traffic thru Wireguard) - Send TCP 1.1.1.1:53, no interface specified,--iplayerwg0interfaceWireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 1.1.1.1:53, Specifywg0,--iplayerwg0Wireguard running, AllowedIPs ``1.1.1.0/24` (only that subnet allowed thru Wireguard) - Send TCP 1.1.1.1:53, no interface specified
get_gateway: Unexpected hardware address length (0). If you are using a VPN, supply the --iplayer flag (and provide an interface via -i)Wireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 1.1.1.1:53, Specifywg0--iplayerWireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 1.1.1.1:53, no interface specified,--iplayerenp0s8(default interface). We do warn users this is occurring indebuglogs and I think it's the best we can do since we're bypassing the kernel's IP routing tables with our raw IP socket.Wireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 1.1.1.1:53, Specifywg0,--iplayerwg0Wireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 8.8.8.8:53 (not awg0allowed IP), no interface specified,--iplayerenp0s8(default interface). We do warn users this is occurring indebuglogs and I think it's the best we can do since we're bypassing the kernel's IP routing tables with our raw IP socket.Wireguard running, AllowedIPs
1.1.1.0/24(only that subnet allowed thru Wireguard) - Send TCP 8.8.8.8:53 (not awg0allowed IP), --interface wg0,--iplayerwg0but Wireguard has internal logic to prevent traffic to non-allowed IPs from going out on that interface.Related Issues
Resolves #955