tun_linux.go 11 KB

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