From 4082ba9e780320e3c6e31a21c7d7ad164c229b5b Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Wed, 24 Oct 2018 23:38:58 +0100 Subject: ping: do not bind to device when destination IP is on device Commit f455fee41c077d4b700a473b2f5b3487b8febc1d made ping to bind to device when a device argument was given. But that to does not work if destination IP on device that socket binds to. See below how to reproduce this issue. # ip addr show dev eth0 | grep 'inet ' inet 192.168.122.100/24 brd 192.168.122.255 scope global eth0 # ping -I eth0 192.168.122.100 PING 192.168.122.100 (192.168.122.100) from 192.168.122.100 eth0: 56(84) bytes of data. Easiest workaround is to cancel bind to device socket option when source and destination IPs are the same and device option is defined. Addresses: https://github.com/iputils/iputils/issues/104 Signed-off-by: Sami Kerola --- ping.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ping.c b/ping.c index 791a48e..965c854 100644 --- a/ping.c +++ b/ping.c @@ -652,6 +652,12 @@ int ping4_run(int argc, char **argv, struct addrinfo *ai, socket_st *sock) &source.sin_addr, sizeof(source.sin_addr))) break; } + if (ifa && !memcmp(&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, + &dst.sin_addr, sizeof(source.sin_addr))) { + enable_capability_raw(); + setsockopt(sock->fd, SOL_SOCKET, SO_BINDTODEVICE, "", 0); + disable_capability_raw(); + } freeifaddrs(ifa0); if (!ifa) error(0, 0, "Warning: source address might be selected on device other than: %s", device); -- cgit v1.2.1