BSD + macOS: fix send() on loopback and utun devices #3257
Merged
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.
This pull request fixes the
send()
function onloX
(if_lo
) and DarwinutunX
(if_utun
), by including theLoopback
header unless on Darwin/macOS with a device that looks like tuntaposx's TUN device.My previous change (#2584) of mine operated under the assumption that a
DLT_NULL
(Loopback
) device doesn't need any headers on BSD, becausetuntaposx
on Darwin didn't work with it. At the time, I hadn't tested this on BSD, but the true story seems to be a bit more complicated:bpf
couldn't send packets toif_lo
interfaces due to a kernel bug.if_lo
needs theLoopback
header.if_tuntap
is broken due to a kernel bug, but should need theLoopback
header once that's fixed.if_lo
andif_utun
need theLoopback
header.tun
interface needs no headers (per Migrate tuntap into own module, add ICMPEcho_am and bugfixing #2584), because they seem to interpretNULL
as "no headers".Without this change:
utun
has missing headers, and the first 4 bytes of the packet are swapped between network and host byte order, and reports an "Unknown" packet type of0x001c
.utun
device gets bad packets (because it's missing theDLT_NULL
header)Long term it looks like Apple is pushing userspace applications towards
utun
and away from third-party kernel extensions liketuntaposx
. I'm working on another PR which addsutun
support forTunTapInterface
, but that's not quite ready yet (#3258).Tests: I've added a test to
bpf.uts
which sends an ICMP ping packet to localhost on IPv4, and tries to sniff for that packet being sent and a response from the kernel. This seems to work on FreeBSD 13.0-RELEASE and macOS 11.4.