tun_linux.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. ipv4Addr, err := netlink.ParseAddr(serverIPv4AddressCIDR)
  179. if err != nil {
  180. return errors.Tracef("failed to parse server IPv4 address: %s: %w", serverIPv4AddressCIDR, err)
  181. }
  182. err = netlink.AddrAdd(link, ipv4Addr)
  183. if err != nil {
  184. return errors.Tracef("failed to add IPv4 address to interface: %s: %w", ipv4Addr.String(), err)
  185. }
  186. err = netlink.LinkSetMTU(link, getMTU(config.MTU))
  187. if err != nil {
  188. return errors.Tracef("failed to set interface MTU: %d: %w", config.MTU, err)
  189. }
  190. err = netlink.LinkSetUp(link)
  191. if err != nil {
  192. return errors.Tracef("failed to set interface up: %w", err)
  193. }
  194. ipv6Addr, err := netlink.ParseAddr(serverIPv6AddressCIDR)
  195. if err != nil {
  196. err = errors.Tracef("failed to parse server IPv6 address: %s: %w", serverIPv6AddressCIDR, err)
  197. } else {
  198. err = netlink.AddrAdd(link, ipv6Addr)
  199. if err != nil {
  200. err = errors.Tracef("failed to add IPv6 address to interface: %s: %w", ipv6Addr.String(), err)
  201. }
  202. }
  203. if err != nil {
  204. if config.AllowNoIPv6NetworkConfiguration {
  205. config.Logger.WithTraceFields(
  206. common.LogFields{
  207. "error": err}).Warning(
  208. "assign IPv6 address failed")
  209. } else {
  210. return errors.Trace(err)
  211. }
  212. }
  213. egressInterface := config.EgressInterface
  214. if egressInterface == "" {
  215. egressInterface = DEFAULT_PUBLIC_INTERFACE_NAME
  216. }
  217. // NAT tun device to external interface
  218. // TODO: need only set forwarding for specific interfaces?
  219. err = setSysctl("net.ipv4.conf.all.forwarding", "1")
  220. if err != nil {
  221. return errors.Trace(err)
  222. }
  223. err = setSysctl("net.ipv6.conf.all.forwarding", "1")
  224. if err != nil {
  225. if config.AllowNoIPv6NetworkConfiguration {
  226. config.Logger.WithTraceFields(
  227. common.LogFields{
  228. "error": err}).Warning(
  229. "allow IPv6 forwarding failed")
  230. } else {
  231. return errors.Trace(err)
  232. }
  233. }
  234. // To avoid duplicates, first try to drop existing rule, then add
  235. for _, mode := range []string{"-D", "-A"} {
  236. err = common.RunNetworkConfigCommand(
  237. config.Logger,
  238. config.SudoNetworkConfigCommands,
  239. "iptables",
  240. "-t", "nat",
  241. mode, "POSTROUTING",
  242. "-s", privateSubnetIPv4.String(),
  243. "-o", egressInterface,
  244. "-j", "MASQUERADE")
  245. if mode != "-D" && err != nil {
  246. return errors.Trace(err)
  247. }
  248. err = common.RunNetworkConfigCommand(
  249. config.Logger,
  250. config.SudoNetworkConfigCommands,
  251. "ip6tables",
  252. "-t", "nat",
  253. mode, "POSTROUTING",
  254. "-s", privateSubnetIPv6.String(),
  255. "-o", egressInterface,
  256. "-j", "MASQUERADE")
  257. if mode != "-D" && err != nil {
  258. if config.AllowNoIPv6NetworkConfiguration {
  259. config.Logger.WithTraceFields(
  260. common.LogFields{
  261. "error": err}).Warning(
  262. "configure IPv6 masquerading failed")
  263. } else {
  264. return errors.Trace(err)
  265. }
  266. }
  267. }
  268. return nil
  269. }
  270. func configureClientInterface(
  271. config *ClientConfig,
  272. tunDeviceName string) error {
  273. // Set tun device network addresses and MTU
  274. link, err := netlink.LinkByName(tunDeviceName)
  275. if err != nil {
  276. return errors.Tracef("failed to get interface %s: %w", tunDeviceName, err)
  277. }
  278. ipv4Addr, err := netlink.ParseAddr(config.IPv4AddressCIDR)
  279. if err != nil {
  280. return errors.Trace(err)
  281. }
  282. err = netlink.AddrAdd(link, ipv4Addr)
  283. if err != nil {
  284. return errors.Trace(err)
  285. }
  286. err = netlink.LinkSetMTU(link, getMTU(config.MTU))
  287. if err != nil {
  288. return errors.Trace(err)
  289. }
  290. err = netlink.LinkSetUp(link)
  291. if err != nil {
  292. return errors.Trace(err)
  293. }
  294. ipv6Addr, err := netlink.ParseAddr(config.IPv6AddressCIDR)
  295. if err != nil {
  296. err = errors.Trace(err)
  297. } else {
  298. err = netlink.AddrAdd(link, ipv6Addr)
  299. if err != nil {
  300. err = errors.Trace(err)
  301. }
  302. }
  303. if err != nil {
  304. if config.AllowNoIPv6NetworkConfiguration {
  305. config.Logger.WithTraceFields(
  306. common.LogFields{
  307. "error": err}).Warning(
  308. "assign IPv6 address failed")
  309. } else {
  310. return errors.Trace(err)
  311. }
  312. }
  313. // Set routing. Routes set here should automatically
  314. // drop when the tun device is removed.
  315. // TODO: appear to need explicit routing only for IPv6?
  316. for _, destination := range config.RouteDestinations {
  317. // Destination may be host (IP) or network (CIDR)
  318. IP := net.ParseIP(destination)
  319. if IP == nil {
  320. var err error
  321. IP, _, err = net.ParseCIDR(destination)
  322. if err != nil {
  323. return errors.Trace(err)
  324. }
  325. }
  326. if IP.To4() != nil {
  327. continue
  328. }
  329. // Note: use "replace" instead of "add" as route from
  330. // previous run (e.g., tun_test case) may not yet be cleared.
  331. link, err := netlink.LinkByName(tunDeviceName)
  332. if err != nil {
  333. err = errors.Trace(err)
  334. } else {
  335. _, destNet, parseErr := net.ParseCIDR(destination)
  336. if parseErr != nil {
  337. err = errors.Trace(err)
  338. } else {
  339. route := &netlink.Route{
  340. LinkIndex: link.Attrs().Index,
  341. Dst: destNet,
  342. Family: netlink.FAMILY_V6,
  343. }
  344. err = netlink.RouteReplace(route)
  345. if err != nil {
  346. err = errors.Trace(err)
  347. }
  348. }
  349. }
  350. if err != nil {
  351. if config.AllowNoIPv6NetworkConfiguration {
  352. config.Logger.WithTraceFields(
  353. common.LogFields{
  354. "error": err}).Warning("add IPv6 route failed")
  355. } else {
  356. return errors.Trace(err)
  357. }
  358. }
  359. }
  360. return nil
  361. }
  362. // BindToDevice binds a socket to the specified interface.
  363. func BindToDevice(fd int, deviceName string) error {
  364. err := syscall.BindToDevice(fd, deviceName)
  365. if err != nil {
  366. return errors.Trace(err)
  367. }
  368. return nil
  369. }
  370. func fixBindToDevice(logger common.Logger, useSudo bool, tunDeviceName string) error {
  371. // Fix the problem described here:
  372. // https://stackoverflow.com/questions/24011205/cant-perform-tcp-handshake-through-a-nat-between-two-nics-with-so-bindtodevice/
  373. //
  374. // > the linux kernel is configured on certain mainstream distributions
  375. // > (Ubuntu...) to act as a router and drop packets where the source
  376. // > address is suspect in order to prevent spoofing (search "rp_filter" on
  377. // > https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt and
  378. // > RFC3704)
  379. err := setSysctl("net.ipv4.conf.all.accept_local", "1")
  380. if err != nil {
  381. return errors.Trace(err)
  382. }
  383. err = setSysctl("net.ipv4.conf.all.rp_filter", "0")
  384. if err != nil {
  385. return errors.Trace(err)
  386. }
  387. err = setSysctl(fmt.Sprintf("net.ipv4.conf.%s.rp_filter", tunDeviceName), "0")
  388. if err != nil {
  389. return errors.Trace(err)
  390. }
  391. return nil
  392. }