From 68f0721c6a1aea6bb97320ec0cf6b8f7f723603d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Mar 2019 05:20:13 +0100 Subject: [PATCH] tun: import mobile particularities --- device/queueconstants_android.go | 16 ++++++++++++++ ...constants.go => queueconstants_default.go} | 4 ++-- device/queueconstants_ios.go | 18 +++++++++++++++ tun/tun_darwin.go | 12 +++++----- tun/tun_linux.go | 22 +++++++++++++++++++ 5 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 device/queueconstants_android.go rename device/{queueconstants.go => queueconstants_default.go} (90%) create mode 100644 device/queueconstants_ios.go diff --git a/device/queueconstants_android.go b/device/queueconstants_android.go new file mode 100644 index 0000000..8d051ad --- /dev/null +++ b/device/queueconstants_android.go @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. + */ + +package device + +/* Reduce memory consumption for Android */ + +const ( + QueueOutboundSize = 1024 + QueueInboundSize = 1024 + QueueHandshakeSize = 1024 + MaxSegmentSize = 2200 + PreallocatedBuffersPerPool = 4096 +) \ No newline at end of file diff --git a/device/queueconstants.go b/device/queueconstants_default.go similarity index 90% rename from device/queueconstants.go rename to device/queueconstants_default.go index 3e94b7f..cf86ba1 100644 --- a/device/queueconstants.go +++ b/device/queueconstants_default.go @@ -1,3 +1,5 @@ +// +build !android,!ios + /* SPDX-License-Identifier: MIT * * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. @@ -5,8 +7,6 @@ package device -/* Implementation specific constants */ - const ( QueueOutboundSize = 1024 QueueInboundSize = 1024 diff --git a/device/queueconstants_ios.go b/device/queueconstants_ios.go new file mode 100644 index 0000000..589b0aa --- /dev/null +++ b/device/queueconstants_ios.go @@ -0,0 +1,18 @@ +// +build ios + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. + */ + +package device + +/* Fit within memory limits for iOS's Network Extension API, which has stricter requirements */ + +const ( + QueueOutboundSize = 1024 + QueueInboundSize = 1024 + QueueHandshakeSize = 1024 + MaxSegmentSize = 1700 + PreallocatedBuffersPerPool = 1024 +) diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 600b156..3b39982 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -171,7 +171,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { tun := &NativeTun{ tunFile: file, events: make(chan TUNEvent, 10), - errors: make(chan error, 1), + errors: make(chan error, 5), } name, err := tun.Name() @@ -200,10 +200,12 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { go tun.routineRouteListener(tunIfindex) - err = tun.setMTU(mtu) - if err != nil { - tun.Close() - return nil, err + if mtu > 0 { + err = tun.setMTU(mtu) + if err != nil { + tun.Close() + return nil, err + } } return tun, nil diff --git a/tun/tun_linux.go b/tun/tun_linux.go index f808079..c352c1a 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -443,3 +443,25 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { return tun, nil } + +func CreateUnmonitoredTUNFromFD(tunFd int) (TUNDevice, string, error) { + file := os.NewFile(uintptr(tunFd), "/dev/tun") + tun := &NativeTun{ + tunFile: file, + fd: file.Fd(), + events: make(chan TUNEvent, 5), + errors: make(chan error, 5), + nopi: true, + } + var err error + tun.fdCancel, err = rwcancel.NewRWCancel(int(tun.fd)) + if err != nil { + return nil, "", err + } + name, err := tun.Name() + if err != nil { + tun.fdCancel.Cancel() + return nil, "", err + } + return tun, name, nil +}