tun_linux.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. "fmt"
  22. "net"
  23. "os"
  24. "strconv"
  25. "strings"
  26. "syscall"
  27. "unsafe"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  30. "github.com/syndtr/gocapability/capability"
  31. )
  32. const (
  33. DEFAULT_PUBLIC_INTERFACE_NAME = "eth0"
  34. )
  35. func IsSupported() bool {
  36. return true
  37. }
  38. func makeDeviceInboundBuffer(MTU int) []byte {
  39. return make([]byte, MTU)
  40. }
  41. func makeDeviceOutboundBuffer(MTU int) []byte {
  42. // On Linux, no outbound buffer is used
  43. return nil
  44. }
  45. // OpenTunDevice opens a file for performing device I/O with
  46. // either a specified tun device, or a new tun device (when
  47. // name is "").
  48. func OpenTunDevice(name string) (*os.File, string, error) {
  49. // Prevent fork between creating fd and setting CLOEXEC
  50. syscall.ForkLock.RLock()
  51. defer syscall.ForkLock.RUnlock()
  52. // Requires process to run as root or have CAP_NET_ADMIN
  53. // This code follows snippets in this thread:
  54. // https://groups.google.com/forum/#!msg/golang-nuts/x_c_pZ6p95c/8T0JBZLpTwAJ
  55. file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
  56. if err != nil {
  57. return nil, "", errors.Trace(err)
  58. }
  59. // Set CLOEXEC so file descriptor not leaked to network config command subprocesses
  60. syscall.CloseOnExec(int(file.Fd()))
  61. // Definitions from <linux/if.h>, <linux/if_tun.h>
  62. // Note: using IFF_NO_PI, so packets have no size/flags header. This does mean
  63. // that if the MTU is changed after the tun device is initialized, packets could
  64. // be truncated when read.
  65. const (
  66. IFNAMSIZ = 16
  67. IF_REQ_PAD_SIZE = 40 - 18
  68. IFF_TUN = 0x0001
  69. IFF_NO_PI = 0x1000
  70. )
  71. var ifName [IFNAMSIZ]byte
  72. if name == "" {
  73. copy(ifName[:], []byte("tun%d"))
  74. } else {
  75. copy(ifName[:], []byte(name))
  76. }
  77. ifReq := struct {
  78. name [IFNAMSIZ]byte
  79. flags uint16
  80. pad [IF_REQ_PAD_SIZE]byte
  81. }{
  82. ifName,
  83. uint16(IFF_TUN | IFF_NO_PI),
  84. [IF_REQ_PAD_SIZE]byte{},
  85. }
  86. _, _, errno := syscall.Syscall(
  87. syscall.SYS_IOCTL,
  88. file.Fd(),
  89. uintptr(syscall.TUNSETIFF),
  90. uintptr(unsafe.Pointer(&ifReq)))
  91. if errno != 0 {
  92. file.Close()
  93. return nil, "", errors.Trace(errno)
  94. }
  95. deviceName := strings.Trim(string(ifReq.name[:]), "\x00")
  96. return file, deviceName, nil
  97. }
  98. func (device *Device) readTunPacket() (int, int, error) {
  99. // Assumes MTU passed to makeDeviceInboundBuffer is actual MTU and
  100. // so buffer is sufficiently large to always read a complete packet.
  101. n, err := device.deviceIO.Read(device.inboundBuffer)
  102. if err != nil {
  103. return 0, 0, errors.Trace(err)
  104. }
  105. return 0, n, nil
  106. }
  107. func (device *Device) writeTunPacket(packet []byte) error {
  108. // Doesn't need outboundBuffer since there's no header; write directly to device.
  109. _, err := device.deviceIO.Write(packet)
  110. if err != nil {
  111. return errors.Trace(err)
  112. }
  113. return nil
  114. }
  115. func configureNetworkConfigSubprocessCapabilities() error {
  116. // If this process has CAP_NET_ADMIN, make it available to be inherited
  117. // be child processes via ambient mechanism described here:
  118. // https://github.com/torvalds/linux/commit/58319057b7847667f0c9585b9de0e8932b0fdb08
  119. //
  120. // The ambient mechanism is available in Linux kernel 4.3 and later.
  121. // When using capabilities, this process should have CAP_NET_ADMIN in order
  122. // to create tun devices. And the subprocess operations such as using "ifconfig"
  123. // and "iptables" for network config require the same CAP_NET_ADMIN capability.
  124. cap, err := capability.NewPid(0)
  125. if err != nil {
  126. return errors.Trace(err)
  127. }
  128. if cap.Get(capability.EFFECTIVE, capability.CAP_NET_ADMIN) {
  129. cap.Set(capability.INHERITABLE|capability.AMBIENT, capability.CAP_NET_ADMIN)
  130. err = cap.Apply(capability.AMBIENT)
  131. if err != nil {
  132. return errors.Trace(err)
  133. }
  134. }
  135. return nil
  136. }
  137. func resetNATTables(
  138. config *ServerConfig,
  139. IPAddress net.IP) error {
  140. // Uses the "conntrack" command, which is often not installed by default.
  141. // conntrack --delete -src-nat --orig-src <address> will clear NAT tables of existing
  142. // connections, making it less likely that traffic for a previous client using the
  143. // specified address will be forwarded to a new client using this address. This is in
  144. // the already unlikely event that there's still in-flight traffic when the address is
  145. // recycled.
  146. err := runNetworkConfigCommand(
  147. config.Logger,
  148. config.SudoNetworkConfigCommands,
  149. "conntrack",
  150. "--delete",
  151. "--src-nat",
  152. "--orig-src",
  153. IPAddress.String())
  154. if err != nil {
  155. // conntrack exits with this error message when there are no flows
  156. // to delete, which is not a failure condition.
  157. if strings.Contains(err.Error(), "0 flow entries have been deleted") {
  158. return nil
  159. }
  160. return errors.Trace(err)
  161. }
  162. return nil
  163. }
  164. func configureServerInterface(
  165. config *ServerConfig,
  166. tunDeviceName string) error {
  167. // Set tun device network addresses and MTU
  168. IPv4Address, IPv4Netmask, err := splitIPMask(serverIPv4AddressCIDR)
  169. if err != nil {
  170. return errors.Trace(err)
  171. }
  172. err = runNetworkConfigCommand(
  173. config.Logger,
  174. config.SudoNetworkConfigCommands,
  175. "ifconfig",
  176. tunDeviceName,
  177. IPv4Address, "netmask", IPv4Netmask,
  178. "mtu", strconv.Itoa(getMTU(config.MTU)),
  179. "up")
  180. if err != nil {
  181. return errors.Trace(err)
  182. }
  183. err = runNetworkConfigCommand(
  184. config.Logger,
  185. config.SudoNetworkConfigCommands,
  186. "ifconfig",
  187. tunDeviceName,
  188. "add", serverIPv6AddressCIDR)
  189. if err != nil {
  190. if config.AllowNoIPv6NetworkConfiguration {
  191. config.Logger.WithTraceFields(
  192. common.LogFields{
  193. "error": err}).Warning(
  194. "assign IPv6 address failed")
  195. } else {
  196. return errors.Trace(err)
  197. }
  198. }
  199. egressInterface := config.EgressInterface
  200. if egressInterface == "" {
  201. egressInterface = DEFAULT_PUBLIC_INTERFACE_NAME
  202. }
  203. // NAT tun device to external interface
  204. // TODO: need only set forwarding for specific interfaces?
  205. err = runNetworkConfigCommand(
  206. config.Logger,
  207. config.SudoNetworkConfigCommands,
  208. "sysctl",
  209. "net.ipv4.conf.all.forwarding=1")
  210. if err != nil {
  211. return errors.Trace(err)
  212. }
  213. err = runNetworkConfigCommand(
  214. config.Logger,
  215. config.SudoNetworkConfigCommands,
  216. "sysctl",
  217. "net.ipv6.conf.all.forwarding=1")
  218. if err != nil {
  219. if config.AllowNoIPv6NetworkConfiguration {
  220. config.Logger.WithTraceFields(
  221. common.LogFields{
  222. "error": err}).Warning(
  223. "allow IPv6 forwarding failed")
  224. } else {
  225. return errors.Trace(err)
  226. }
  227. }
  228. // To avoid duplicates, first try to drop existing rule, then add
  229. for _, mode := range []string{"-D", "-A"} {
  230. err = runNetworkConfigCommand(
  231. config.Logger,
  232. config.SudoNetworkConfigCommands,
  233. "iptables",
  234. "-t", "nat",
  235. mode, "POSTROUTING",
  236. "-s", privateSubnetIPv4.String(),
  237. "-o", egressInterface,
  238. "-j", "MASQUERADE")
  239. if mode != "-D" && err != nil {
  240. return errors.Trace(err)
  241. }
  242. err = runNetworkConfigCommand(
  243. config.Logger,
  244. config.SudoNetworkConfigCommands,
  245. "ip6tables",
  246. "-t", "nat",
  247. mode, "POSTROUTING",
  248. "-s", privateSubnetIPv6.String(),
  249. "-o", egressInterface,
  250. "-j", "MASQUERADE")
  251. if mode != "-D" && err != nil {
  252. if config.AllowNoIPv6NetworkConfiguration {
  253. config.Logger.WithTraceFields(
  254. common.LogFields{
  255. "error": err}).Warning(
  256. "configure IPv6 masquerading failed")
  257. } else {
  258. return errors.Trace(err)
  259. }
  260. }
  261. }
  262. return nil
  263. }
  264. func configureClientInterface(
  265. config *ClientConfig,
  266. tunDeviceName string) error {
  267. // Set tun device network addresses and MTU
  268. IPv4Address, IPv4Netmask, err := splitIPMask(config.IPv4AddressCIDR)
  269. if err != nil {
  270. return errors.Trace(err)
  271. }
  272. err = runNetworkConfigCommand(
  273. config.Logger,
  274. config.SudoNetworkConfigCommands,
  275. "ifconfig",
  276. tunDeviceName,
  277. IPv4Address,
  278. "netmask", IPv4Netmask,
  279. "mtu", strconv.Itoa(getMTU(config.MTU)),
  280. "up")
  281. if err != nil {
  282. return errors.Trace(err)
  283. }
  284. err = runNetworkConfigCommand(
  285. config.Logger,
  286. config.SudoNetworkConfigCommands,
  287. "ifconfig",
  288. tunDeviceName,
  289. "add", config.IPv6AddressCIDR)
  290. if err != nil {
  291. if config.AllowNoIPv6NetworkConfiguration {
  292. config.Logger.WithTraceFields(
  293. common.LogFields{
  294. "error": err}).Warning(
  295. "assign IPv6 address failed")
  296. } else {
  297. return errors.Trace(err)
  298. }
  299. }
  300. // Set routing. Routes set here should automatically
  301. // drop when the tun device is removed.
  302. // TODO: appear to need explicit routing only for IPv6?
  303. for _, destination := range config.RouteDestinations {
  304. // Destination may be host (IP) or network (CIDR)
  305. IP := net.ParseIP(destination)
  306. if IP == nil {
  307. var err error
  308. IP, _, err = net.ParseCIDR(destination)
  309. if err != nil {
  310. return errors.Trace(err)
  311. }
  312. }
  313. if IP.To4() != nil {
  314. continue
  315. }
  316. // Note: use "replace" instead of "add" as route from
  317. // previous run (e.g., tun_test case) may not yet be cleared.
  318. err = runNetworkConfigCommand(
  319. config.Logger,
  320. config.SudoNetworkConfigCommands,
  321. "ip",
  322. "-6",
  323. "route", "replace",
  324. destination,
  325. "dev", tunDeviceName)
  326. if err != nil {
  327. if config.AllowNoIPv6NetworkConfiguration {
  328. config.Logger.WithTraceFields(
  329. common.LogFields{
  330. "error": err}).Warning("add IPv6 route failed")
  331. } else {
  332. return errors.Trace(err)
  333. }
  334. }
  335. }
  336. return nil
  337. }
  338. // BindToDevice binds a socket to the specified interface.
  339. func BindToDevice(fd int, deviceName string) error {
  340. err := syscall.BindToDevice(fd, deviceName)
  341. if err != nil {
  342. return errors.Trace(err)
  343. }
  344. return nil
  345. }
  346. func fixBindToDevice(logger common.Logger, useSudo bool, tunDeviceName string) error {
  347. // Fix the problem described here:
  348. // https://stackoverflow.com/questions/24011205/cant-perform-tcp-handshake-through-a-nat-between-two-nics-with-so-bindtodevice/
  349. err := runNetworkConfigCommand(
  350. logger,
  351. useSudo,
  352. "sysctl",
  353. "net.ipv4.conf.all.accept_local=1")
  354. if err != nil {
  355. return errors.Trace(err)
  356. }
  357. err = runNetworkConfigCommand(
  358. logger,
  359. useSudo,
  360. "sysctl",
  361. "net.ipv4.conf.all.rp_filter=0")
  362. if err != nil {
  363. return errors.Trace(err)
  364. }
  365. err = runNetworkConfigCommand(
  366. logger,
  367. useSudo,
  368. "sysctl",
  369. fmt.Sprintf("net.ipv4.conf.%s.rp_filter=0", tunDeviceName))
  370. if err != nil {
  371. return errors.Trace(err)
  372. }
  373. return nil
  374. }