From da7e7e0a3e6f93a4555737c8b3cb19ce60dfa54f Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 12 May 2017 14:07:24 -0400 Subject: [PATCH 1/2] darwin: Detects IP Ver. for NULL Header on Write Adds the ability for both IPv4 and IPv6 to be written to the TUN device. --- syscalls_darwin.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/syscalls_darwin.go b/syscalls_darwin.go index df2b285..3bdbc50 100644 --- a/syscalls_darwin.go +++ b/syscalls_darwin.go @@ -159,7 +159,16 @@ func (t *tunReadCloser) Write(from []byte) (int, error) { } t.wBuf = t.wBuf[:len(from)+4] - t.wBuf[3] = 2 // Family: IP (2) + // Determine the IP Family for the NULL L2 Header + ipVer := from[0] >> 4 + if ipVer == 4 { + t.wBuf[3] = syscall.AF_INET + } else if ipVer == 6 { + t.wBuf[3] = syscall.AF_INET6 + } else { + return 0, errors.New("Unable to determine IP version from packet.") + } + copy(t.wBuf[4:], from) n, err := t.f.Write(t.wBuf) From 4112fb9cd77a6eb2738f97287f99a23809d5c85d Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 12:00:06 -0400 Subject: [PATCH 2/2] darwin: Adds Check Empty Writes to TUN --- syscalls_darwin.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/syscalls_darwin.go b/syscalls_darwin.go index 3bdbc50..6a473f8 100644 --- a/syscalls_darwin.go +++ b/syscalls_darwin.go @@ -151,6 +151,11 @@ func (t *tunReadCloser) Read(to []byte) (int, error) { } func (t *tunReadCloser) Write(from []byte) (int, error) { + + if len(from) == 0 { + return 0, syscall.EIO + } + t.wMu.Lock() defer t.wMu.Unlock()