From 3f281b0517762d41064aefb3f40eff8b08678208 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 12:06:35 -0400 Subject: [PATCH 01/14] Fixes Comment Typo --- params_unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params_unix.go b/params_unix.go index 4a926c7..e0e613a 100644 --- a/params_unix.go +++ b/params_unix.go @@ -8,7 +8,7 @@ package water type PlatformSpecificParams struct { // Name is the name to be set for the interface to be created. This overrides // the default name assigned by OS such as tap0 or tun0. A zero-value of this - // field, i.e. an emapty string, indicates that the default name should be + // field, i.e. an empty string, indicates that the default name should be // used. Name string } From 80f6655041cc951fc0122903daf08b5faeeb68dd Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 15:31:28 -0400 Subject: [PATCH 02/14] Removes Deprecated "New" Functions --- if.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/if.go b/if.go index 2113288..2131379 100644 --- a/if.go +++ b/if.go @@ -1,6 +1,8 @@ package water -import "io" +import ( + "io" +) // Interface is a TUN/TAP interface. type Interface struct { @@ -49,28 +51,6 @@ func New(config Config) (ifce *Interface, err error) { return newDev(config) } -// NewTAP creates a new TAP interface whose name is ifName. If ifName is empty, a -// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed -// 16 bytes. TAP interfaces are not supported on darwin. -// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds. -// -// Note: this function is deprecated and will be removed from the library. -// Please use New() instead. -func NewTAP(ifName string) (ifce *Interface, err error) { - return newTAP(ifName) -} - -// NewTUN creates a new TUN interface whose name is ifName. If ifName is empty, a -// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed -// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds. -// -// Note: this function is deprecated and will be removed from the library. -// Please use New() instead. -// 16 bytes. Setting interface name is NOT supported on darwin. -func NewTUN(ifName string) (ifce *Interface, err error) { - return newTUN(ifName) -} - // IsTUN returns true if ifce is a TUN interface. func (ifce *Interface) IsTUN() bool { return !ifce.isTAP From 70591d249921d075889cc49aaef072987e6b354a Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 12:25:48 -0400 Subject: [PATCH 03/14] Simplifies Platform Specific Interface Creation The interface `Config` object is now passed directly into each creation function in order to more easily use additional options. In addition, the linux and darwin parameters were split to better capture different options for each platform. --- if.go | 12 +++++++++++- if_unix.go | 16 ---------------- if_windows.go | 12 ------------ params_darwin.go | 13 +++++++++++++ params_unix.go => params_linux.go | 3 +-- syscalls_darwin.go | 10 +++++----- syscalls_linux.go | 8 ++++---- syscalls_other.go | 4 ++-- syscalls_windows.go | 8 ++------ 9 files changed, 38 insertions(+), 48 deletions(-) delete mode 100644 if_unix.go delete mode 100644 if_windows.go create mode 100644 params_darwin.go rename params_unix.go => params_linux.go (83%) diff --git a/if.go b/if.go index 2131379..af0adf6 100644 --- a/if.go +++ b/if.go @@ -2,6 +2,7 @@ package water import ( "io" + "errors" ) // Interface is a TUN/TAP interface. @@ -48,7 +49,14 @@ func New(config Config) (ifce *Interface, err error) { if zeroConfig == config { config = defaultConfig() } - return newDev(config) + switch config.DeviceType { + case TUN: + return newTUN(config) + case TAP: + return newTAP(config) + default: + return nil, errors.New("unknown device type") + } } // IsTUN returns true if ifce is a TUN interface. @@ -65,3 +73,5 @@ func (ifce *Interface) IsTAP() bool { func (ifce *Interface) Name() string { return ifce.name } + + diff --git a/if_unix.go b/if_unix.go deleted file mode 100644 index f2de759..0000000 --- a/if_unix.go +++ /dev/null @@ -1,16 +0,0 @@ -// +build linux darwin - -package water - -import "errors" - -func newDev(config Config) (ifce *Interface, err error) { - switch config.DeviceType { - case TUN: - return newTUN(config.Name) - case TAP: - return newTAP(config.Name) - default: - return nil, errors.New("unknown device type") - } -} diff --git a/if_windows.go b/if_windows.go deleted file mode 100644 index 3ef1f8e..0000000 --- a/if_windows.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build windows - -package water - -import "errors" - -func newDev(config Config) (ifce *Interface, err error) { - if config.DeviceType != TAP && config.DeviceType != TUN { - return nil, errors.New("unknown device type") - } - return openDev(config) -} diff --git a/params_darwin.go b/params_darwin.go new file mode 100644 index 0000000..13c6b18 --- /dev/null +++ b/params_darwin.go @@ -0,0 +1,13 @@ + +package water + +// PlatformSpecificParams defines parameters in Config that are specific to +// macOS. A zero-value of such type is valid, yielding an interface +// with OS defined name. +// Currently it is not possible to set the interface name in macOS. +type PlatformSpecificParams struct { +} + +func defaultPlatformSpecificParams() PlatformSpecificParams { + return PlatformSpecificParams{} +} diff --git a/params_unix.go b/params_linux.go similarity index 83% rename from params_unix.go rename to params_linux.go index e0e613a..9449f31 100644 --- a/params_unix.go +++ b/params_linux.go @@ -1,9 +1,8 @@ -// +build linux darwin package water // PlatformSpecificParams defines parameters in Config that are specific to -// Linux and macOS. A zero-value of such type is valid, yielding an interface +// Linux. A zero-value of such type is valid, yielding an interface // with OS defined name. type PlatformSpecificParams struct { // Name is the name to be set for the interface to be created. This overrides diff --git a/syscalls_darwin.go b/syscalls_darwin.go index 6a473f8..c998557 100644 --- a/syscalls_darwin.go +++ b/syscalls_darwin.go @@ -61,7 +61,7 @@ type sockaddrCtl struct { var sockaddrCtlSize uintptr = 32 -func newTUN(string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { var fd int // Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ... // @@ -122,6 +122,10 @@ func newTUN(string) (ifce *Interface, err error) { }, nil } +func newTAP(config Config) (ifce *Interface, err error) { + return nil, errors.New("tap interface not implemented on this platform") +} + // tunReadCloser is a hack to work around the first 4 bytes "packet // information" because there doesn't seem to be an IFF_NO_PI for darwin. type tunReadCloser struct { @@ -189,7 +193,3 @@ func (t *tunReadCloser) Close() error { return t.f.Close() } - -func newTAP(ifName string) (ifce *Interface, err error) { - return nil, errors.New("tap interface not implemented on this platform") -} diff --git a/syscalls_linux.go b/syscalls_linux.go index e1ba63f..420a645 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -21,12 +21,12 @@ type ifReq struct { pad [0x28 - 0x10 - 2]byte } -func newTAP(ifName string) (ifce *Interface, err error) { +func newTAP(config Config) (ifce *Interface, err error) { file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } - name, err := createInterface(file.Fd(), ifName, cIFF_TAP|cIFF_NO_PI) + name, err := createInterface(file.Fd(), config.Name, cIFF_TAP|cIFF_NO_PI) if err != nil { return nil, err } @@ -34,12 +34,12 @@ func newTAP(ifName string) (ifce *Interface, err error) { return } -func newTUN(ifName string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } - name, err := createInterface(file.Fd(), ifName, cIFF_TUN|cIFF_NO_PI) + name, err := createInterface(file.Fd(), config.Name, cIFF_TUN|cIFF_NO_PI) if err != nil { return nil, err } diff --git a/syscalls_other.go b/syscalls_other.go index ab3b9e5..ed49244 100644 --- a/syscalls_other.go +++ b/syscalls_other.go @@ -4,10 +4,10 @@ package water import "errors" -func newTAP(ifName string) (ifce *Interface, err error) { +func newTAP(config Config) (ifce *Interface, err error) { return nil, errors.New("tap interface not implemented on this platform") } -func newTUN(ifName string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { return nil, errors.New("tap interface not implemented on this platform") } diff --git a/syscalls_windows.go b/syscalls_windows.go index 5dc4013..a06c572 100644 --- a/syscalls_windows.go +++ b/syscalls_windows.go @@ -289,14 +289,10 @@ func openDev(config Config) (ifce *Interface, err error) { return nil, errIfceNameNotFound } -func newTAP(ifName string) (ifce *Interface, err error) { - config := defaultConfig() - config.DeviceType = TAP +func newTAP(config Config) (ifce *Interface, err error) { return openDev(config) } -func newTUN(ifName string) (ifce *Interface, err error) { - config := defaultConfig() - config.DeviceType = TUN +func newTUN(config Config) (ifce *Interface, err error) { return openDev(config) } From ad8a32dbd3b774702edf46e5b0f774ebd6380082 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 15:49:13 -0400 Subject: [PATCH 04/14] linux: Adds Persistent Devs + Setting Owner/Group --- params_linux.go | 16 +++++++++++- syscalls_linux.go | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/params_linux.go b/params_linux.go index 9449f31..cff2f16 100644 --- a/params_linux.go +++ b/params_linux.go @@ -10,8 +10,22 @@ type PlatformSpecificParams struct { // field, i.e. an empty string, indicates that the default name should be // used. Name string + + // Enable or disable persistence mode for the interface device. + Persist bool + + // ID of the user which will be granted ownership of the device. + // The default value of -1 specifies that any user may use the device. + Owner int + + // ID of the group which will be granted access to the device. + // The default value of -1 specifies that any group may use the device. + Group int } func defaultPlatformSpecificParams() PlatformSpecificParams { - return PlatformSpecificParams{} + return PlatformSpecificParams{ + Owner: -1, + Group: -1, + } } diff --git a/syscalls_linux.go b/syscalls_linux.go index 420a645..a2e75f9 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -21,6 +21,14 @@ type ifReq struct { pad [0x28 - 0x10 - 2]byte } +func ioctl(fd uintptr, request int, argp uintptr) error { + _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(request), argp) + if errno != 0 { + return os.NewSyscallError("ioctl", errno) + } + return nil +} + func newTAP(config Config) (ifce *Interface, err error) { file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { @@ -30,6 +38,26 @@ func newTAP(config Config) (ifce *Interface, err error) { if err != nil { return nil, err } + + // Set Device Owner + if config.Owner >= 0 { + if err = ioctl(file.Fd(), syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { + return + } + } + + // Set Device Group + if config.Group >= 0 { + if err = ioctl(file.Fd(), syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { + return + } + } + + // Set/Clear Persist Device Flag + if err = setPersistence(file.Fd(), config.Persist); err != nil { + return + } + ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: name} return } @@ -43,6 +71,26 @@ func newTUN(config Config) (ifce *Interface, err error) { if err != nil { return nil, err } + + // Set Device Owner + if config.Owner >= 0 { + if err = ioctl(file.Fd(), syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { + return + } + } + + // Set Device Group + if config.Group >= 0 { + if err = ioctl(file.Fd(), syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { + return + } + } + + // Set/Clear Persist Device Flag + if err = setPersistence(file.Fd(), config.Persist); err != nil { + return + } + ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: name} return } @@ -51,11 +99,20 @@ func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName str var req ifReq req.Flags = flags copy(req.Name[:], ifName) - _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req))) - if errno != 0 { - err = errno + + err = ioctl(fd, syscall.TUNSETIFF, uintptr(unsafe.Pointer(&req))) + if err != nil { return } + createdIFName = strings.Trim(string(req.Name[:]), "\x00") return } + +func setPersistence(fd uintptr, enabled bool) error { + value := 0; + if enabled { + value = 1 + } + return ioctl(fd, syscall.TUNSETPERSIST, uintptr(value)) +} From 341e40fcee222316f54555a2060e4fa38777361d Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 15:54:27 -0400 Subject: [PATCH 05/14] Adds Build Tag Comments for Consistency --- params_darwin.go | 1 + params_linux.go | 1 + 2 files changed, 2 insertions(+) diff --git a/params_darwin.go b/params_darwin.go index 13c6b18..bba950e 100644 --- a/params_darwin.go +++ b/params_darwin.go @@ -1,3 +1,4 @@ +// +build darwin package water diff --git a/params_linux.go b/params_linux.go index cff2f16..8f69d0a 100644 --- a/params_linux.go +++ b/params_linux.go @@ -1,3 +1,4 @@ +// +build linux package water From 80efdb69919407412ee56a5e3abb0a4b6499657a Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 16:06:34 -0400 Subject: [PATCH 06/14] Adjusts Tests with Changes --- ipv4_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipv4_test.go b/ipv4_test.go index 3a675b2..ec6d89b 100644 --- a/ipv4_test.go +++ b/ipv4_test.go @@ -30,7 +30,7 @@ func TestBroadcast(t *testing.T) { brd = net.IPv4(10, 0, 42, 255) ) - ifce, err := NewTAP("test") + ifce, err := New(Config{DeviceType: TAP}) if err != nil { t.Fatalf("creating TAP error: %v\n", err) } From c59a2fb87d39ed930f880a38955ddbf5212ec1a6 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 16:22:27 -0400 Subject: [PATCH 07/14] Removes Redundant Type Conversion --- syscalls_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syscalls_linux.go b/syscalls_linux.go index a2e75f9..203c973 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -22,7 +22,7 @@ type ifReq struct { } func ioctl(fd uintptr, request int, argp uintptr) error { - _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(request), argp) + _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(request), argp) if errno != 0 { return os.NewSyscallError("ioctl", errno) } From 497d14e4272f1604526cf4afd3a62a24df5c7aca Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 16:38:42 -0400 Subject: [PATCH 08/14] linux: Rewords Documentation for Owner/Group Param --- params_linux.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/params_linux.go b/params_linux.go index 8f69d0a..a0533f7 100644 --- a/params_linux.go +++ b/params_linux.go @@ -16,11 +16,13 @@ type PlatformSpecificParams struct { Persist bool // ID of the user which will be granted ownership of the device. - // The default value of -1 specifies that any user may use the device. + // If set to a negative value, the owner value will not be changed. + // By default, Linux sets the owner to -1, which allows any user. Owner int // ID of the group which will be granted access to the device. - // The default value of -1 specifies that any group may use the device. + // If set to a negative value, the group value will not be changed. + // By default, Linux sets the group to -1, which allows any group. Group int } From 3c9a2e9525b7b7ffae22b2a0f6b8fe7aa5744e75 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 09:03:22 -0400 Subject: [PATCH 09/14] Updates to README with macOS Caveats --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d723413..b53f1db 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,11 @@ You'd see the ICMP packets printed out: 2017/03/20 21:17:40 Packet Received: 45 00 00 54 e9 1d 00 00 40 01 7d 6c 0a 01 00 0a 0a 01 00 14 08 00 ee 04 21 15 00 00 58 d0 a9 64 00 08 fb a5 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 ``` +#### Caveats + +1. Only Point-to-Point user TUN devices are supported. TAP devices are *not* supported natively by macOS. +2. Custom interface names are not supported by macOS. Interface names are automatically generated serially, using the `utun<#>` naming convention. + ### TAP on Windows: To use it with windows, you will need to install a [tap driver](https://github.com/OpenVPN/tap-windows6), or [OpenVPN client](https://github.com/OpenVPN/openvpn) for windows. From f9df1e79f2dcc7fcd3334f38117960d459593cf6 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 09:43:05 -0400 Subject: [PATCH 10/14] Gofmt Cleanup --- if.go | 4 +--- syscalls_linux.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/if.go b/if.go index af0adf6..9f90485 100644 --- a/if.go +++ b/if.go @@ -1,8 +1,8 @@ package water import ( - "io" "errors" + "io" ) // Interface is a TUN/TAP interface. @@ -73,5 +73,3 @@ func (ifce *Interface) IsTAP() bool { func (ifce *Interface) Name() string { return ifce.name } - - diff --git a/syscalls_linux.go b/syscalls_linux.go index 203c973..c894389 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -110,7 +110,7 @@ func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName str } func setPersistence(fd uintptr, enabled bool) error { - value := 0; + value := 0 if enabled { value = 1 } From 6a94337a59c9bbca9cc5ca436dc78e1a1badb0eb Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 10:01:22 -0400 Subject: [PATCH 11/14] Additional Cleanup (DRY) --- syscalls_linux.go | 59 ++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/syscalls_linux.go b/syscalls_linux.go index c894389..4d97fd4 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -39,23 +39,8 @@ func newTAP(config Config) (ifce *Interface, err error) { return nil, err } - // Set Device Owner - if config.Owner >= 0 { - if err = ioctl(file.Fd(), syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { - return - } - } - - // Set Device Group - if config.Group >= 0 { - if err = ioctl(file.Fd(), syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { - return - } - } - - // Set/Clear Persist Device Flag - if err = setPersistence(file.Fd(), config.Persist); err != nil { - return + if err = setDeviceOptions(file.Fd(), config); err != nil { + return nil, err } ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: name} @@ -72,23 +57,8 @@ func newTUN(config Config) (ifce *Interface, err error) { return nil, err } - // Set Device Owner - if config.Owner >= 0 { - if err = ioctl(file.Fd(), syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { - return - } - } - - // Set Device Group - if config.Group >= 0 { - if err = ioctl(file.Fd(), syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { - return - } - } - - // Set/Clear Persist Device Flag - if err = setPersistence(file.Fd(), config.Persist); err != nil { - return + if err = setDeviceOptions(file.Fd(), config); err != nil { + return nil, err } ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: name} @@ -109,10 +79,27 @@ func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName str return } -func setPersistence(fd uintptr, enabled bool) error { +func setDeviceOptions(fd uintptr, config Config) (err error) { + + // Set Device Owner + if config.Owner >= 0 { + if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { + return + } + } + + // Set Device Group + if config.Group >= 0 { + if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { + return + } + } + + // Set/Clear Persist Device Flag value := 0 - if enabled { + if config.Persist { value = 1 } return ioctl(fd, syscall.TUNSETPERSIST, uintptr(value)) + } From 771a72e2eaaef8de4b08ac811e58ee21a96d6213 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 14:13:55 -0400 Subject: [PATCH 12/14] Adds Arroyo Networks to Contributors File --- CONTRIBUTORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 95bdcbd..3a84f44 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -3,4 +3,5 @@ Harshal Sheth KOJIMA Takanori Sean Purser-Haskell daregod -Lucus Lee \ No newline at end of file +Lucus Lee +Arroyo Networks, LLC \ No newline at end of file From 15d1b3ddbb407e4edc69db10047bb330787d18fc Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 14:27:12 -0400 Subject: [PATCH 13/14] linux: Re-adds NewTAP/NewTUN Functions --- if_linux.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 if_linux.go diff --git a/if_linux.go b/if_linux.go new file mode 100644 index 0000000..7752dc0 --- /dev/null +++ b/if_linux.go @@ -0,0 +1,32 @@ +// +build linux + +package water + +import ( + "fmt" +) + +// NewTAP creates a new TAP interface whose name is ifName. If ifName is empty, a +// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed +// 16 bytes. TAP interfaces are not supported on darwin. +// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds. +// +// Deprecated: This function may be removed in the future. Please use New() instead. +func NewTAP(ifName string) (ifce *Interface, err error) { + fmt.Println("Deprecated: NewTAP(..) may be removed in the future. Please use New() instead.") + config := Config{DeviceType: TAP} + config.Name = ifName + return newTAP(config) +} + +// NewTUN creates a new TUN interface whose name is ifName. If ifName is empty, a +// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed +// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds. +// +// Deprecated: This function will be removed in the future. Please use New() instead. +func NewTUN(ifName string) (ifce *Interface, err error) { + fmt.Println("Deprecated: NewTUN(..) may be removed in the future. Please use New() instead.") + config := Config{DeviceType: TUN} + config.Name = ifName + return newTUN(config) +} From 937f6ba95528d35c570c101ffa5a2d6b90f4e3fa Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 14:58:32 -0400 Subject: [PATCH 14/14] linux: Refactors Owner/Group to DevicePermissions --- params_linux.go | 30 +++++++++++++++++------------- syscalls_linux.go | 14 +++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/params_linux.go b/params_linux.go index a0533f7..41203f6 100644 --- a/params_linux.go +++ b/params_linux.go @@ -2,6 +2,18 @@ package water +type DevicePermissions struct { + // ID of the user which will be granted ownership of the device. + // If set to a negative value, the owner value will not be changed. + // By default, Linux sets the owner to -1, which allows any user. + Owner uint + + // ID of the group which will be granted access to the device. + // If set to a negative value, the group value will not be changed. + // By default, Linux sets the group to -1, which allows any group. + Group uint +} + // PlatformSpecificParams defines parameters in Config that are specific to // Linux. A zero-value of such type is valid, yielding an interface // with OS defined name. @@ -15,20 +27,12 @@ type PlatformSpecificParams struct { // Enable or disable persistence mode for the interface device. Persist bool - // ID of the user which will be granted ownership of the device. - // If set to a negative value, the owner value will not be changed. - // By default, Linux sets the owner to -1, which allows any user. - Owner int - - // ID of the group which will be granted access to the device. - // If set to a negative value, the group value will not be changed. - // By default, Linux sets the group to -1, which allows any group. - Group int + // Owner and Group permissions for the device. + // A zero-value of this field, i.e. nil, indicates that no changes to owner + // or group will be made. + Permissions *DevicePermissions } func defaultPlatformSpecificParams() PlatformSpecificParams { - return PlatformSpecificParams{ - Owner: -1, - Group: -1, - } + return PlatformSpecificParams{} } diff --git a/syscalls_linux.go b/syscalls_linux.go index 4d97fd4..50a945c 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -81,16 +81,16 @@ func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName str func setDeviceOptions(fd uintptr, config Config) (err error) { - // Set Device Owner - if config.Owner >= 0 { - if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { + // Device Permissions + if config.Permissions != nil { + + // Set Owner + if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Permissions.Owner)); err != nil { return } - } - // Set Device Group - if config.Group >= 0 { - if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { + // Set Group + if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Permissions.Group)); err != nil { return } }