utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package tun
  20. import (
  21. std_errors "errors"
  22. "fmt"
  23. "net"
  24. "os/exec"
  25. "strconv"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  28. )
  29. var errUnsupported = std_errors.New("operation unsupported on this platform")
  30. // runNetworkConfigCommand execs a network config command, such as "ifconfig"
  31. // or "iptables". On platforms that support capabilities, the network config
  32. // capabilities of the current process is made available to the command
  33. // subprocess. Alternatively, "sudo" will be used when useSudo is true.
  34. func runNetworkConfigCommand(
  35. logger common.Logger,
  36. useSudo bool,
  37. commandName string, commandArgs ...string) error {
  38. // configureSubprocessCapabilities will set inheritable
  39. // capabilities on platforms which support that (Linux).
  40. // Specifically, CAP_NET_ADMIN will be transferred from
  41. // this process to the child command.
  42. err := configureNetworkConfigSubprocessCapabilities()
  43. if err != nil {
  44. return errors.Trace(err)
  45. }
  46. // TODO: use CommandContext to interrupt on server shutdown?
  47. // (the commands currently being issued shouldn't block...)
  48. if useSudo {
  49. commandArgs = append([]string{commandName}, commandArgs...)
  50. commandName = "sudo"
  51. }
  52. cmd := exec.Command(commandName, commandArgs...)
  53. output, err := cmd.CombinedOutput()
  54. logger.WithTraceFields(common.LogFields{
  55. "command": commandName,
  56. "args": commandArgs,
  57. "output": string(output),
  58. "error": err,
  59. }).Debug("exec")
  60. if err != nil {
  61. err := fmt.Errorf(
  62. "command %s %+v failed with %s", commandName, commandArgs, string(output))
  63. return errors.Trace(err)
  64. }
  65. return nil
  66. }
  67. func splitIPMask(IPAddressCIDR string) (string, string, error) {
  68. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  69. if err != nil {
  70. return "", "", errors.Trace(err)
  71. }
  72. var netmask string
  73. IPv4Mask := net.IP(IPNet.Mask).To4()
  74. if IPv4Mask != nil {
  75. netmask = fmt.Sprintf(
  76. "%d.%d.%d.%d", IPv4Mask[0], IPv4Mask[1], IPv4Mask[2], IPv4Mask[3])
  77. } else {
  78. netmask = IPNet.Mask.String()
  79. }
  80. return IP.String(), netmask, nil
  81. }
  82. func splitIPPrefixLen(IPAddressCIDR string) (string, string, error) {
  83. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  84. if err != nil {
  85. return "", "", errors.Trace(err)
  86. }
  87. prefixLen, _ := IPNet.Mask.Size()
  88. return IP.String(), strconv.Itoa(prefixLen), nil
  89. }
  90. func getMTU(configMTU int) int {
  91. if configMTU <= 0 {
  92. return DEFAULT_MTU
  93. } else if configMTU > 65536 {
  94. return 65536
  95. }
  96. return configMTU
  97. }