From 68f0721c6a1aea6bb97320ec0cf6b8f7f723603d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Mar 2019 05:20:13 +0100 Subject: tun: import mobile particularities --- device/queueconstants.go | 16 ---------------- device/queueconstants_android.go | 16 ++++++++++++++++ device/queueconstants_default.go | 16 ++++++++++++++++ device/queueconstants_ios.go | 18 ++++++++++++++++++ tun/tun_darwin.go | 12 +++++++----- tun/tun_linux.go | 22 ++++++++++++++++++++++ 6 files changed, 79 insertions(+), 21 deletions(-) delete mode 100644 device/queueconstants.go create mode 100644 device/queueconstants_android.go create mode 100644 device/queueconstants_default.go create mode 100644 device/queueconstants_ios.go diff --git a/device/queueconstants.go b/device/queueconstants.go deleted file mode 100644 index 3e94b7f..0000000 --- a/device/queueconstants.go +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. - */ - -package device - -/* Implementation specific constants */ - -const ( - QueueOutboundSize = 1024 - QueueInboundSize = 1024 - QueueHandshakeSize = 1024 - MaxSegmentSize = (1 << 16) - 1 // largest possible UDP datagram - PreallocatedBuffersPerPool = 0 // Disable and allow for infinite memory growth -) 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_default.go b/device/queueconstants_default.go new file mode 100644 index 0000000..cf86ba1 --- /dev/null +++ b/device/queueconstants_default.go @@ -0,0 +1,16 @@ +// +build !android,!ios + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. + */ + +package device + +const ( + QueueOutboundSize = 1024 + QueueInboundSize = 1024 + QueueHandshakeSize = 1024 + MaxSegmentSize = (1 << 16) - 1 // largest possible UDP datagram + PreallocatedBuffersPerPool = 0 // Disable and allow for infinite memory growth +) 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 +} -- cgit v1.2.3