tun_linux.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. "path/filepath"
  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/tailscale/netlink"
  31. "golang.org/x/sys/unix"
  32. )
  33. const (
  34. DEFAULT_PUBLIC_INTERFACE_NAME = "eth0"
  35. )
  36. func IsSupported() bool {
  37. return true
  38. }
  39. func makeDeviceInboundBuffer(MTU int) []byte {
  40. return make([]byte, MTU)
  41. }
  42. func makeDeviceOutboundBuffer(MTU int) []byte {
  43. // On Linux, no outbound buffer is used
  44. return nil
  45. }
  46. // OpenTunDevice opens a file for performing device I/O with
  47. // either a specified tun device, or a new tun device (when
  48. // name is "").
  49. func OpenTunDevice(name string) (*os.File, string, error) {
  50. // Prevent fork between creating fd and setting CLOEXEC
  51. // TODO: is this still necessary with unix.Open?
  52. syscall.ForkLock.RLock()
  53. defer syscall.ForkLock.RUnlock()
  54. // Requires process to run as root or have CAP_NET_ADMIN
  55. // As explained in https://github.com/golang/go/issues/30426, the fd must
  56. // not be added to the Go poller before the following TUNSETIFF ioctl
  57. // call. This is achieved by using unix.Open -- which opens a raw fd --
  58. // instead of os.FileOpen, followed by the ioctl and finally os.NewFile
  59. // to add the fd to the Go poller.
  60. //
  61. // Set CLOEXEC so file descriptor not leaked to network config command
  62. // subprocesses.
  63. fileName := "/dev/net/tun"
  64. fd, err := unix.Open(fileName, os.O_RDWR|syscall.O_CLOEXEC, 0)
  65. if err != nil {
  66. return nil, "", errors.Trace(err)
  67. }
  68. // This code follows snippets in this thread:
  69. // https://groups.google.com/forum/#!msg/golang-nuts/x_c_pZ6p95c/8T0JBZLpTwAJ;
  70. // Definitions from <linux/if.h>, <linux/if_tun.h>
  71. // Note: using IFF_NO_PI, so packets have no size/flags header. This does mean
  72. // that if the MTU is changed after the tun device is initialized, packets could
  73. // be truncated when read.
  74. const (
  75. IFNAMSIZ = 16
  76. IF_REQ_PAD_SIZE = 40 - 18
  77. IFF_TUN = 0x0001
  78. IFF_NO_PI = 0x1000
  79. )
  80. var ifName [IFNAMSIZ]byte
  81. if name == "" {
  82. copy(ifName[:], []byte("tun%d"))
  83. } else {
  84. copy(ifName[:], []byte(name))
  85. }
  86. ifReq := struct {
  87. name [IFNAMSIZ]byte
  88. flags uint16
  89. pad [IF_REQ_PAD_SIZE]byte
  90. }{
  91. ifName,
  92. uint16(IFF_TUN | IFF_NO_PI),
  93. [IF_REQ_PAD_SIZE]byte{},
  94. }
  95. _, _, errno := syscall.Syscall(
  96. syscall.SYS_IOCTL,
  97. uintptr(fd),
  98. uintptr(syscall.TUNSETIFF),
  99. uintptr(unsafe.Pointer(&ifReq)))
  100. if errno != 0 {
  101. unix.Close(fd)
  102. return nil, "", errors.Trace(errno)
  103. }
  104. err = unix.SetNonblock(fd, true)
  105. if err != nil {
  106. unix.Close(fd)
  107. return nil, "", errors.Trace(err)
  108. }
  109. file := os.NewFile(uintptr(fd), fileName)
  110. deviceName := strings.Trim(string(ifReq.name[:]), "\x00")
  111. return file, deviceName, nil
  112. }
  113. func (device *Device) readTunPacket() (int, int, error) {
  114. // Assumes MTU passed to makeDeviceInboundBuffer is actual MTU and
  115. // so buffer is sufficiently large to always read a complete packet.
  116. n, err := device.deviceIO.Read(device.inboundBuffer)
  117. if err != nil {
  118. return 0, 0, errors.Trace(err)
  119. }
  120. return 0, n, nil
  121. }
  122. func (device *Device) writeTunPacket(packet []byte) error {
  123. // Doesn't need outboundBuffer since there's no header; write directly to device.
  124. _, err := device.deviceIO.Write(packet)
  125. if err != nil {
  126. return errors.Trace(err)
  127. }
  128. return nil
  129. }
  130. func resetNATTables(
  131. config *ServerConfig,
  132. IPAddress net.IP) error {
  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. // The netlink library does not expose the facilities for conclusively determining if
  139. // src-nat has been applied to an individual flow, so replacing the previous call to
  140. // the conntrack binary (see the comment above) with the code below is not a 1-to-1
  141. // replacement. Since no other non-SNAT flows for these IPs that might exist need to
  142. // be retained at the time resetNATTables is called, we're now skipping that check.
  143. var family netlink.InetFamily
  144. if IPAddress.To4() != nil {
  145. family = unix.AF_INET
  146. } else if IPAddress.To16() != nil {
  147. family = unix.AF_INET6
  148. } else {
  149. return errors.TraceNew("invalid IP address family")
  150. }
  151. filter := &netlink.ConntrackFilter{}
  152. _ = filter.AddIP(netlink.ConntrackOrigSrcIP, IPAddress)
  153. _, err := netlink.ConntrackDeleteFilter(netlink.ConntrackTable, family, filter)
  154. if err != nil {
  155. return errors.Trace(err)
  156. }
  157. return nil
  158. }
  159. func setSysctl(key, value string) error {
  160. err := os.WriteFile(
  161. filepath.Join("/proc/sys", strings.ReplaceAll(key, ".", "/")),
  162. []byte(value),
  163. 0o644,
  164. )
  165. if err != nil {
  166. return errors.Tracef("failed to write sysctl %s=%s: %w", key, value, err)
  167. }
  168. return nil
  169. }
  170. func configureServerInterface(
  171. config *ServerConfig,
  172. tunDeviceName string) error {
  173. // Set tun device network addresses and MTU
  174. link, err := netlink.LinkByName(tunDeviceName)
  175. if err != nil {
  176. return errors.Tracef("failed to get interface %s: %w", tunDeviceName, err)
  177. }
  178. _, ipv4Net, err := net.ParseCIDR(serverIPv4AddressCIDR)
  179. if err != nil {
  180. return errors.Tracef("failed to parse server IPv4 address: %s: %w", serverIPv4AddressCIDR, err)
  181. }
  182. ipv4Addr := &netlink.Addr{IPNet: ipv4Net}
  183. err = netlink.AddrAdd(link, ipv4Addr)
  184. if err != nil {
  185. return errors.Tracef("failed to add IPv4 address to interface: %s: %w", ipv4Net.String(), err)
  186. }
  187. err = netlink.LinkSetMTU(link, getMTU(config.MTU))
  188. if err != nil {
  189. return errors.Tracef("failed to set interface MTU: %d: %w", config.MTU, err)
  190. }
  191. err = netlink.LinkSetUp(link)
  192. if err != nil {
  193. return errors.Tracef("failed to set interface up: %w", err)
  194. }
  195. _, ipv6Net, err := net.ParseCIDR(serverIPv6AddressCIDR)
  196. if err != nil {
  197. err = errors.Tracef("failed to parse server IPv6 address: %s: %w", serverIPv4AddressCIDR, err)
  198. } else {
  199. ipv6Addr := &netlink.Addr{IPNet: ipv6Net}
  200. err = netlink.AddrAdd(link, ipv6Addr)
  201. if err != nil {
  202. err = errors.Tracef("failed to add IPv6 address to interface: %s: %w", ipv6Net.String(), err)
  203. }
  204. }
  205. if err != nil {
  206. if config.AllowNoIPv6NetworkConfiguration {
  207. config.Logger.WithTraceFields(
  208. common.LogFields{
  209. "error": err}).Warning(
  210. "assign IPv6 address failed")
  211. } else {
  212. return errors.Trace(err)
  213. }
  214. }
  215. egressInterface := config.EgressInterface
  216. if egressInterface == "" {
  217. egressInterface = DEFAULT_PUBLIC_INTERFACE_NAME
  218. }
  219. // NAT tun device to external interface
  220. // TODO: need only set forwarding for specific interfaces?
  221. err = setSysctl("net.ipv4.conf.all.forwarding", "1")
  222. if err != nil {
  223. return errors.Trace(err)
  224. }
  225. err = setSysctl("net.ipv6.conf.all.forwarding", "1")
  226. if err != nil {
  227. if config.AllowNoIPv6NetworkConfiguration {
  228. config.Logger.WithTraceFields(
  229. common.LogFields{
  230. "error": err}).Warning(
  231. "allow IPv6 forwarding failed")
  232. } else {
  233. return errors.Trace(err)
  234. }
  235. }
  236. // To avoid duplicates, first try to drop existing rule, then add
  237. for _, mode := range []string{"-D", "-A"} {
  238. err = common.RunNetworkConfigCommand(
  239. config.Logger,
  240. config.SudoNetworkConfigCommands,
  241. "iptables",
  242. "-t", "nat",
  243. mode, "POSTROUTING",
  244. "-s", privateSubnetIPv4.String(),
  245. "-o", egressInterface,
  246. "-j", "MASQUERADE")
  247. if mode != "-D" && err != nil {
  248. return errors.Trace(err)
  249. }
  250. err = common.RunNetworkConfigCommand(
  251. config.Logger,
  252. config.SudoNetworkConfigCommands,
  253. "ip6tables",
  254. "-t", "nat",
  255. mode, "POSTROUTING",
  256. "-s", privateSubnetIPv6.String(),
  257. "-o", egressInterface,
  258. "-j", "MASQUERADE")
  259. if mode != "-D" && err != nil {
  260. if config.AllowNoIPv6NetworkConfiguration {
  261. config.Logger.WithTraceFields(
  262. common.LogFields{
  263. "error": err}).Warning(
  264. "configure IPv6 masquerading failed")
  265. } else {
  266. return errors.Trace(err)
  267. }
  268. }
  269. }
  270. return nil
  271. }
  272. func configureClientInterface(
  273. config *ClientConfig,
  274. tunDeviceName string) error {
  275. // Set tun device network addresses and MTU
  276. link, err := netlink.LinkByName(tunDeviceName)
  277. if err != nil {
  278. return errors.Trace(fmt.Errorf("failed to get interface %s: %w", tunDeviceName, err))
  279. }
  280. _, ipv4Net, err := net.ParseCIDR(config.IPv4AddressCIDR)
  281. if err != nil {
  282. return errors.Trace(err)
  283. }
  284. ipv4Addr := &netlink.Addr{IPNet: ipv4Net}
  285. if err := netlink.AddrAdd(link, ipv4Addr); err != nil {
  286. return errors.Trace(err)
  287. }
  288. if err := netlink.LinkSetMTU(link, getMTU(config.MTU)); err != nil {
  289. return errors.Trace(err)
  290. }
  291. if err := netlink.LinkSetUp(link); err != nil {
  292. return errors.Trace(err)
  293. }
  294. _, ipv6Net, err := net.ParseCIDR(config.IPv6AddressCIDR)
  295. if err != nil {
  296. err = errors.Trace(err)
  297. } else {
  298. ipv6Addr := &netlink.Addr{IPNet: ipv6Net}
  299. err = netlink.AddrAdd(link, ipv6Addr)
  300. if err != nil {
  301. err = errors.Trace(err)
  302. }
  303. }
  304. if err != nil {
  305. if config.AllowNoIPv6NetworkConfiguration {
  306. config.Logger.WithTraceFields(
  307. common.LogFields{
  308. "error": err}).Warning(
  309. "assign IPv6 address failed")
  310. } else {
  311. return errors.Trace(err)
  312. }
  313. }
  314. // Set routing. Routes set here should automatically
  315. // drop when the tun device is removed.
  316. // TODO: appear to need explicit routing only for IPv6?
  317. for _, destination := range config.RouteDestinations {
  318. // Destination may be host (IP) or network (CIDR)
  319. IP := net.ParseIP(destination)
  320. if IP == nil {
  321. var err error
  322. IP, _, err = net.ParseCIDR(destination)
  323. if err != nil {
  324. return errors.Trace(err)
  325. }
  326. }
  327. if IP.To4() != nil {
  328. continue
  329. }
  330. // Note: use "replace" instead of "add" as route from
  331. // previous run (e.g., tun_test case) may not yet be cleared.
  332. link, err := netlink.LinkByName(tunDeviceName)
  333. if err != nil {
  334. err = errors.Trace(err)
  335. } else {
  336. _, destNet, parseErr := net.ParseCIDR(destination)
  337. if parseErr != nil {
  338. err = errors.Trace(err)
  339. } else {
  340. route := &netlink.Route{
  341. LinkIndex: link.Attrs().Index,
  342. Dst: destNet,
  343. Family: netlink.FAMILY_V6,
  344. }
  345. err = netlink.RouteReplace(route)
  346. if err != nil {
  347. err = errors.Trace(err)
  348. }
  349. }
  350. }
  351. if err != nil {
  352. if config.AllowNoIPv6NetworkConfiguration {
  353. config.Logger.WithTraceFields(
  354. common.LogFields{
  355. "error": err}).Warning("add IPv6 route failed")
  356. } else {
  357. return errors.Trace(err)
  358. }
  359. }
  360. }
  361. return nil
  362. }
  363. // BindToDevice binds a socket to the specified interface.
  364. func BindToDevice(fd int, deviceName string) error {
  365. err := syscall.BindToDevice(fd, deviceName)
  366. if err != nil {
  367. return errors.Trace(err)
  368. }
  369. return nil
  370. }
  371. func fixBindToDevice(logger common.Logger, useSudo bool, tunDeviceName string) error {
  372. // Fix the problem described here:
  373. // https://stackoverflow.com/questions/24011205/cant-perform-tcp-handshake-through-a-nat-between-two-nics-with-so-bindtodevice/
  374. //
  375. // > the linux kernel is configured on certain mainstream distributions
  376. // > (Ubuntu...) to act as a router and drop packets where the source
  377. // > address is suspect in order to prevent spoofing (search "rp_filter" on
  378. // > https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt and
  379. // > RFC3704)
  380. err := setSysctl("net.ipv4.conf.all.accept_local", "1")
  381. if err != nil {
  382. return errors.Trace(err)
  383. }
  384. err = setSysctl("net.ipv4.conf.all.rp_filter", "0")
  385. if err != nil {
  386. return errors.Trace(err)
  387. }
  388. err = setSysctl(fmt.Sprintf("net.ipv4.conf.%s.rp_filter", tunDeviceName), "0")
  389. if err != nil {
  390. return errors.Trace(err)
  391. }
  392. return nil
  393. }