bindToDevice.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // +build android linux
  2. /*
  3. * Copyright (c) 2014, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package psiphon
  21. import (
  22. "errors"
  23. "fmt"
  24. "net"
  25. "syscall"
  26. "time"
  27. )
  28. // bindToDevice sends a file descriptor a service which will bind the socket to
  29. // a device so that it doesn't route through a VPN interface. This is used for
  30. // TCP tunnel connections made while the VPN is active and for UDP DNS requests
  31. // sent as part of establishing those TCP connections.
  32. // On Android, where this facility is used, the underlying implementation uses
  33. // setsockopt(SO_BINDTODEVICE). This socket options requires root, which is
  34. // why this is delegated to a remote service.
  35. func bindToDevice(socketFd int, config *DialConfig) error {
  36. addr, err := net.ResolveUnixAddr("unix", config.BindToDeviceServiceAddress)
  37. if err != nil {
  38. return ContextError(err)
  39. }
  40. conn, err := net.DialUnix("unix", nil, addr)
  41. if err != nil {
  42. return ContextError(err)
  43. }
  44. defer conn.Close()
  45. // Set request timeouts, using the ConnectTimeout from the overall Dial
  46. conn.SetReadDeadline(time.Now().Add(config.ConnectTimeout))
  47. conn.SetWriteDeadline(time.Now().Add(config.ConnectTimeout))
  48. // The 0 byte payload for the write is a dummy message. The important
  49. // payload is the file descriptor.
  50. // The response is also a single byte. 0 is success, and any other
  51. // byte value is an error code.
  52. msg := []byte{byte(0)}
  53. rights := syscall.UnixRights(socketFd)
  54. bytesWritten, ooBytesWritten, err := conn.WriteMsgUnix(msg, rights, nil)
  55. if err != nil {
  56. return ContextError(err)
  57. }
  58. if bytesWritten != len(msg) || ooBytesWritten != len(rights) {
  59. return ContextError(errors.New("bindToDevice write request failed"))
  60. }
  61. bytesRead, err := conn.Read(msg)
  62. if err != nil {
  63. return ContextError(err)
  64. }
  65. if bytesRead != len(msg) || msg[0] != 0 {
  66. return ContextError(fmt.Errorf("bindToDevice read response failed: %d", int(msg[0])))
  67. }
  68. return nil
  69. }