Skip to content

Conversation

micolous
Copy link
Contributor

@micolous micolous commented Jun 12, 2021

This pull request fixes the send() function on loX (if_lo) and Darwin utunX (if_utun), by including the Loopback 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, because tuntaposx 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:

Without this change:

  • running Wireshark against a 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 of 0x001c.
  • an application on the far side of the utun device gets bad packets (because it's missing the DLT_NULL header)

Long term it looks like Apple is pushing userspace applications towards utun and away from third-party kernel extensions like tuntaposx. I'm working on another PR which adds utun support for TunTapInterface, 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.

@codecov
Copy link

codecov bot commented Jun 12, 2021

Codecov Report

Merging #3257 (f9656e3) into master (a6bf8dd) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #3257   +/-   ##
=======================================
  Coverage   52.38%   52.38%           
=======================================
  Files           9        9           
  Lines        1365     1365           
=======================================
  Hits          715      715           
  Misses        650      650           

@micolous micolous marked this pull request as ready for review June 12, 2021 04:52
@micolous
Copy link
Contributor Author

I'm still in the process of updating the comment per feedback on #3258.

However, I've found that FreeBSD has buggy handling of DLT_NULL for tun interfaces, and I'm still trying to get my head around all that.

@micolous micolous changed the title Darwin/macOS: fix send() on utun devices DRAFT: Darwin/macOS: fix send() on utun devices Jun 14, 2021
The Loopback work-around is only needed for tuntaposx (on Darwin).
@micolous micolous force-pushed the macos-utun-sendfix branch from 5bad379 to f9656e3 Compare June 14, 2021 04:25
@micolous micolous changed the title DRAFT: Darwin/macOS: fix send() on utun devices FreeBSD/macOS: fix send() on loopback and utun devices Jun 14, 2021
@micolous micolous changed the title FreeBSD/macOS: fix send() on loopback and utun devices BSD + macOS: fix send() on loopback and utun devices Jun 14, 2021
@micolous
Copy link
Contributor Author

After the tests finish, this is ready again.

Copy link
Member

@gpotter2 gpotter2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool PR, thanks !

@gpotter2 gpotter2 merged commit 6eef12d into secdev:master Jul 1, 2021
@gpotter2 gpotter2 added this to the 2.5.0 milestone Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants