go-ping / ping

ICMP Ping library for Go
MIT License
1.33k stars 345 forks source link

Fix unprivileged packet matching on Linux #147

Closed CHTJonas closed 3 years ago

CHTJonas commented 3 years ago

62f79f1f4f36fc821eebb131548270d4d8b68653 changed the matching of ICMP IDs so that it happened in both privileged an unprivileged modes. I played around and tested this on macOS and Windows but not on Linux which, it seems, behaves differently.

This introduces a platform-dependant matching function using build constraints. Fixes #146.

CHTJonas commented 3 years ago

Looks like CI is failing because we're using the latest version of goreleaser which is trying to build for darwin_arm64 (Apple Silicon) but we're using Go version 1.15 which doesn't support that OS/arch combo. I'll open another PR to bump the Go version.

jraby commented 3 years ago

I was trying to figure out why go-ping hung in unpriviliged / datagram mode and ended up here.

It seems to reason why message ID are not comparable / ignored on linux is because the kernel uses the socket's source port as the ID for outgoing message. The datagram socket is created with a 0 port which tells the kernel to choose a free port. The port number is then used to demultiplex incoming icmp replies and send them to the corresponding socket.

Anyways, lots of words to say that ignoring the ID in this case is quite correct.


Ref: https://lwn.net/Articles/443051/

[pid 3322448] socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP) = 3
[pid 3322448] bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
[pid 3322448] fcntl(3, F_GETFL)         = 0x2 (flags O_RDWR)
[pid 3322448] fcntl(3, F_DUPFD_CLOEXEC, 0) = 7
[pid 3322448] fcntl(7, F_GETFL)         = 0x2 (flags O_RDWR)
[pid 3322448] fcntl(7, F_SETFL, O_RDWR|O_NONBLOCK) = 0
[pid 3322448] getsockname(7, {sa_family=AF_INET, sin_port=htons(34), sin_addr=inet_addr("0.0.0.0")}, [112->16]) = 0

----

21:35:36.244849 wlan0 Out IP 172.16.1.156 > 172.217.13.196: ICMP echo request, id 34, seq 0, length 24
21:35:36.256708 wlan0 In  IP 172.217.13.196 > 172.16.1.156: ICMP echo reply, id 34, seq 0, length 24
CHTJonas commented 3 years ago

@jraby thanks for the confirmation I wasn't barking up the wrong tree - good detective work! Closing as this has been merged into master in 5f9dd90.