On paid plans, when a Tissue Mast check fails, the probe that saw it fail traces the path to the target. It does that in its own process, over an ICMP socket that doesn't need root. We've pulled the code out into a Rust crate called hoptrace and open-sourced it under MIT or Apache 2.0.
"Your site is down" starts a conversation, and the next question is always where. Your server, your host's network, or something in between. mtr answers that well. It sends packets with a rising TTL, each router that drops one sends back a Time Exceeded, and after a few passes you have every hop with its loss and latency. When one of our probes sees a check fail, we want that table for the address it couldn't reach, taken at the moment it couldn't reach it.
We could have shelled out to mtr. On Linux, mtr 0.93 and later can trace without privilege, using the same kind of socket this crate does. But that's a binary to install on every vantage, a child process for every failing check, and a text report to parse back into numbers. On macOS mtr needs root. We wanted a function call that returns a list of hops.
let trace = hoptrace::Tracer::new().trace("1.1.1.1".parse()?)?;
for hop in &trace.hops {
match (hop.addr, hop.avg) {
(Some(addr), Some(avg)) => println!("{:>2} {addr} {:.1}% loss {avg:?}", hop.ttl, hop.loss()),
_ => println!("{:>2} ???", hop.ttl),
}
}
It's SOCK_DGRAM with IPPROTO_ICMP, usually called a ping socket. macOS lets any process open one. Linux does once the process's group is in net.ipv4.ping_group_range, which most distributions set. It sends only echo requests, from the host's own address, so there's nothing to spoof with it. You can still set the TTL, and a trace needs nothing else.
Sending works the same on both systems. Reading the answers doesn't, and most of the work was finding out how each kernel behaves.
macOS hands every ICMP message to the socket through recvfrom, Time Exceeded included, with the IPv4 header still on the front. The Time Exceeded quotes the start of the request that expired, and the request's sequence number says which TTL it went out with. But every ICMP socket on the machine gets every one of those messages, so two traces running at once each see the other's routers. Each trace picks a random identifier and sequence base and keeps only messages that quote both. RFC 792 asks a router to quote just the first 8 bytes of the request. Most quote more, and when the quote reaches the trace's random token, that has to match too.
Linux gives a ping socket only its own echo replies, and the kernel swaps your identifier for one of its own. Time Exceeded never shows up on the normal queue. You set IP_RECVERR, and the errors land on the socket's error queue, which you read with recvmsg(MSG_ERRQUEUE). What comes back is the router's quote of your request, with a sock_extended_err in the control data and the router's address packed in right after that struct as the "offender". socket2 doesn't wrap any of this, so it's about a hundred lines of libc.
There's a second Linux catch. With IP_RECVERR on, each ICMP error also leaves an error pending on the socket, and the next sendto returns that error instead of sending. Answers arriving while a pass is still sending hit it in a few traces out of every few hundred in the test that provokes it. A failed send clears the pending error, so hoptrace retries once, and counts a request that still won't go as lost.
The Linux side is tested against three network namespaces, a client, a router and a target, so the error queue sees real Time Exceeded and Unreachable messages over IPv4 and IPv6. The macOS side has been run over IPv4 on real paths. IPv6 there has only been run to loopback.
Routers and targets rate-limit the ICMP they send. Until the target answers, every TTL past it reaches the target, so thirty requests fired at once would lose answers to the target's limit rather than to the path. The requests in a pass go out 5 ms apart, and once a pass has found the target, later passes stop there.
With equal-cost multipath, one TTL can land on a router in one pass and on the target in the next. The target's own reply wins, so the hop it answered at is the end of the path.
If the target never answers, the trace drops the silent hops after the last one that did. They say nothing about where the path goes. If a router says Unreachable, the path ends there instead.
Each answer is timed as soon as it's read, from a poll(2) wakeup, in user space. A loaded host adds its scheduling delay to the numbers. Loopback comes out at about 0.1 ms on an idle Mac.
$ cargo run --example report -- -c 5 1.1.1.1 # addresses changed
HOST: 1.1.1.1 (1.1.1.1)
Loss% Snt Avg Best Wrst
1.|-- 192.168.1.1 0.0% 5 3.0 2.6 3.4
2.|-- 198.51.100.17 0.0% 5 12.8 11.8 13.4
3.|-- 198.51.100.130 0.0% 5 14.0 12.1 18.8
...
8.|-- 1.1.1.1 0.0% 5 16.1 12.8 21.0
The kernel's default for net.ipv4.ping_group_range is 1 0, an empty range. systemd 243 and later sets it to every group, so most current distributions work out of the box, but slim container images and older systems often don't. When the kernel refuses, trace returns Error::NotPermitted, and its message names the sysctl. hoptrace::permitted() asks the same question without tracing anything. The first line below opens it until the next boot, the second keeps it open:
sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"
echo 'net.ipv4.ping_group_range = 0 2147483647' | sudo tee /etc/sysctl.d/60-ping.conf
If you can install a binary, mtr does more. It can probe with TCP and UDP, which gets past firewalls that drop echo requests, and it looks up names and AS numbers. trippy does all of that too, and with a raw socket it handles ECMP paths properly. It also has a library engine and an unprivileged mode, on macOS only and without the ECMP strategies. It's a much bigger dependency. If you only want to know whether one host answers and how fast, surge-ping is a good async ping on the same kind of socket.
hoptrace sends only ICMP echo, does no lookups and has no live view. It doesn't build for FreeBSD or Windows, which don't have the socket. It's 0.1, it blocks (run it on a blocking thread from async code), it needs Rust 1.70, and its only dependencies are socket2 and libc. Bug reports and pull requests go to github.com/tissue-systems/hoptrace.