tun_darwin.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. //go:build !ios && !e2e_testing
  2. package overlay
  3. import (
  4. "errors"
  5. "fmt"
  6. "log/slog"
  7. "math/bits"
  8. "net/netip"
  9. "os"
  10. "sync"
  11. "sync/atomic"
  12. "syscall"
  13. "unsafe"
  14. "github.com/gaissmai/bart"
  15. "github.com/slackhq/nebula/config"
  16. "github.com/slackhq/nebula/overlay/tio"
  17. "github.com/slackhq/nebula/routing"
  18. "github.com/slackhq/nebula/util"
  19. netroute "golang.org/x/net/route"
  20. "golang.org/x/sys/unix"
  21. )
  22. type tun struct {
  23. f *os.File
  24. Device string
  25. vpnNetworks []netip.Prefix
  26. DefaultMTU int
  27. Routes atomic.Pointer[[]Route]
  28. routeTree atomic.Pointer[bart.Table[routing.Gateways]]
  29. linkAddr atomic.Pointer[netroute.LinkAddr]
  30. // hostOwned means the fd arrived from the OS, which has already configured addressing, mtu
  31. // and routes for it. NEPacketTunnelProvider on darwin does this.
  32. hostOwned bool
  33. l *slog.Logger
  34. }
  35. type ifReq struct {
  36. Name [unix.IFNAMSIZ]byte
  37. Flags uint16
  38. pad [8]byte
  39. }
  40. const (
  41. _SIOCAIFADDR_IN6 = 2155899162
  42. _UTUN_OPT_IFNAME = 2
  43. _IN6_IFF_NODAD = 0x0020
  44. _IN6_IFF_SECURED = 0x0400
  45. utunControlName = "com.apple.net.utun_control"
  46. )
  47. type ifreqMTU struct {
  48. Name [16]byte
  49. MTU int32
  50. pad [8]byte
  51. }
  52. type addrLifetime struct {
  53. Expire float64
  54. Preferred float64
  55. Vltime uint32
  56. Pltime uint32
  57. }
  58. type ifreqAlias4 struct {
  59. Name [unix.IFNAMSIZ]byte
  60. Addr unix.RawSockaddrInet4
  61. DstAddr unix.RawSockaddrInet4
  62. MaskAddr unix.RawSockaddrInet4
  63. }
  64. type ifreqAlias6 struct {
  65. Name [unix.IFNAMSIZ]byte
  66. Addr unix.RawSockaddrInet6
  67. DstAddr unix.RawSockaddrInet6
  68. PrefixMask unix.RawSockaddrInet6
  69. Flags uint32
  70. Lifetime addrLifetime
  71. }
  72. // utunIndex is the utun tun.dev asks for, or -1 for whichever is free. ok is false for a name that isn't utunN, which
  73. // also gets whichever is free
  74. func utunIndex(name string) (index int, ok bool) {
  75. if name == "" || name == "utun" {
  76. return -1, true
  77. }
  78. if _, err := fmt.Sscanf(name, "utun%d", &index); err != nil || index < 0 {
  79. return -1, false
  80. }
  81. return index, true
  82. }
  83. // tunDevNeedsRestart is true when tun.dev asks for a particular utun that isn't the running one. Anything else takes
  84. // whichever is free, and the one we have will do
  85. func tunDevNeedsRestart(name, running string) bool {
  86. index, _ := utunIndex(name)
  87. return index >= 0 && fmt.Sprintf("utun%d", index) != running
  88. }
  89. func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
  90. ifIndex, ok := utunIndex(c.GetString("tun.dev", ""))
  91. if !ok {
  92. // NOTE: we don't make this error so we don't break existing
  93. // configs that set a name before it was used.
  94. l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
  95. }
  96. fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, unix.AF_SYS_CONTROL)
  97. if err != nil {
  98. return nil, fmt.Errorf("system socket: %v", err)
  99. }
  100. var ctlInfo = &unix.CtlInfo{}
  101. copy(ctlInfo.Name[:], utunControlName)
  102. err = unix.IoctlCtlInfo(fd, ctlInfo)
  103. if err != nil {
  104. return nil, fmt.Errorf("CTLIOCGINFO: %v", err)
  105. }
  106. err = unix.Connect(fd, &unix.SockaddrCtl{
  107. ID: ctlInfo.Id,
  108. Unit: uint32(ifIndex) + 1,
  109. })
  110. if err != nil {
  111. return nil, fmt.Errorf("SYS_CONNECT: %v", err)
  112. }
  113. name, err := unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
  114. if err != nil {
  115. return nil, fmt.Errorf("failed to retrieve tun name: %w", err)
  116. }
  117. err = unix.SetNonblock(fd, true)
  118. if err != nil {
  119. return nil, fmt.Errorf("SetNonblock: %v", err)
  120. }
  121. t := &tun{
  122. f: os.NewFile(uintptr(fd), ""),
  123. Device: name,
  124. vpnNetworks: vpnNetworks,
  125. DefaultMTU: getMTU(l, c),
  126. l: l,
  127. }
  128. err = t.reload(c, true, nil)
  129. if err != nil {
  130. return nil, err
  131. }
  132. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  133. r.RestartRequiredIfChanged("tun.mtu")
  134. if tunDevNeedsRestart(c.GetString("tun.dev", ""), t.Device) {
  135. r.RestartRequiredIfChanged("tun.dev")
  136. }
  137. err := t.reload(c, false, r)
  138. if err != nil {
  139. util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
  140. r.Failed()
  141. }
  142. })
  143. return t, nil
  144. }
  145. func (t *tun) deviceBytes() (o [16]byte) {
  146. for i, c := range t.Device {
  147. o[i] = byte(c)
  148. }
  149. return
  150. }
  151. // newTunFromFd adopts a utun the host already created and configured, which is how a darwin
  152. // network extension is handed its device. Everything about moving packets is shared with newTun,
  153. // only the setup differs: the host owns addressing and routing here.
  154. func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
  155. if err := unix.SetNonblock(deviceFd, true); err != nil {
  156. // We own the fd from the moment it is handed to us
  157. _ = unix.Close(deviceFd)
  158. return nil, fmt.Errorf("failed to set the tun fd to non-blocking mode: %w", err)
  159. }
  160. file := os.NewFile(uintptr(deviceFd), "/dev/tun")
  161. t := &tun{
  162. f: file,
  163. Device: utunNameFromFd(deviceFd),
  164. vpnNetworks: vpnNetworks,
  165. DefaultMTU: getMTU(l, c),
  166. hostOwned: true,
  167. l: l,
  168. }
  169. if err := t.reload(c, true, nil); err != nil {
  170. _ = file.Close()
  171. return nil, err
  172. }
  173. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  174. if err := t.reload(c, false, r); err != nil {
  175. util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
  176. r.Failed()
  177. }
  178. })
  179. return t, nil
  180. }
  181. // utunNameFromFd asks the socket what interface it is, for logs. A blank name is not worth
  182. // failing a tunnel over, so an error just leaves it empty.
  183. func utunNameFromFd(fd int) string {
  184. name, err := unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
  185. if err != nil {
  186. return ""
  187. }
  188. return name
  189. }
  190. func (t *tun) Close() error {
  191. if t.f != nil {
  192. return t.f.Close()
  193. }
  194. return nil
  195. }
  196. func (t *tun) Activate() error {
  197. // The host handed us a configured device. Its addresses, mtu and routes come from the network
  198. // settings it applied, and a sandboxed extension cannot change them anyway.
  199. if t.hostOwned {
  200. return nil
  201. }
  202. devName := t.deviceBytes()
  203. s, err := unix.Socket(
  204. unix.AF_INET,
  205. unix.SOCK_DGRAM,
  206. unix.IPPROTO_IP,
  207. )
  208. if err != nil {
  209. return err
  210. }
  211. defer unix.Close(s)
  212. fd := uintptr(s)
  213. // Set the MTU on the device
  214. ifm := ifreqMTU{Name: devName, MTU: int32(t.DefaultMTU)}
  215. if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
  216. return fmt.Errorf("failed to set tun mtu: %v", err)
  217. }
  218. // Get the device flags
  219. ifrf := ifReq{Name: devName}
  220. if err = ioctl(fd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
  221. return fmt.Errorf("failed to get tun flags: %s", err)
  222. }
  223. linkAddr, err := getLinkAddr(t.Device)
  224. if err != nil {
  225. return err
  226. }
  227. if linkAddr == nil {
  228. return fmt.Errorf("unable to discover link_addr for tun interface")
  229. }
  230. t.linkAddr.Store(linkAddr)
  231. for _, network := range t.vpnNetworks {
  232. if network.Addr().Is4() {
  233. err = t.activate4(network)
  234. if err != nil {
  235. return err
  236. }
  237. } else {
  238. err = t.activate6(network)
  239. if err != nil {
  240. return err
  241. }
  242. }
  243. }
  244. // Run the interface
  245. ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
  246. if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
  247. return fmt.Errorf("failed to run tun device: %s", err)
  248. }
  249. // Unsafe path routes
  250. return t.addRoutes(false, nil)
  251. }
  252. // activate4 assigns our vpn address as a /32 and routes the vpn network to the tun explicitly.
  253. func (t *tun) activate4(network netip.Prefix) error {
  254. s, err := unix.Socket(
  255. unix.AF_INET,
  256. unix.SOCK_DGRAM,
  257. unix.IPPROTO_IP,
  258. )
  259. if err != nil {
  260. return err
  261. }
  262. defer unix.Close(s)
  263. ifr := ifreqAlias4{
  264. Name: t.deviceBytes(),
  265. Addr: unix.RawSockaddrInet4{
  266. Len: unix.SizeofSockaddrInet4,
  267. Family: unix.AF_INET,
  268. Addr: network.Addr().As4(),
  269. },
  270. DstAddr: unix.RawSockaddrInet4{
  271. Len: unix.SizeofSockaddrInet4,
  272. Family: unix.AF_INET,
  273. Addr: network.Addr().As4(),
  274. },
  275. MaskAddr: unix.RawSockaddrInet4{
  276. Len: unix.SizeofSockaddrInet4,
  277. Family: unix.AF_INET,
  278. Addr: [4]byte{255, 255, 255, 255}, //always make the address a /32
  279. },
  280. }
  281. // On Darwin, VPNs don't edit the routing table directly.
  282. // A network extension hands its routes to the system, and configd installs them.
  283. // configd replaces any route already holding the same prefix, including ours.
  284. // As an example, Tailscale with an exit node treats any interface prefix wider than one address as a LAN.
  285. // With LAN access off, it routes each LAN into Tailscale's utun.
  286. // With LAN access on, it excludes each LAN from its tunnel.
  287. // configd routes an excluded prefix like its own routes would, ignoring ours, so it goes out the default route.
  288. // Either way our route is replaced, and traffic for the vpn network no longer reaches nebula.
  289. // A single address isn't a LAN to Tailscale, so our route is left alone.
  290. // We route the vpn network to the tun explicitly instead.
  291. if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&ifr))); err != nil {
  292. return fmt.Errorf("failed to set tun v4 address: %s", err)
  293. }
  294. if err := addRoute(network, t.linkAddr.Load()); err != nil {
  295. return fmt.Errorf("failed to add route for %v: %w", network.Masked(), err)
  296. }
  297. return nil
  298. }
  299. // activate6 is activate4 for IPv6
  300. func (t *tun) activate6(network netip.Prefix) error {
  301. s, err := unix.Socket(
  302. unix.AF_INET6,
  303. unix.SOCK_DGRAM,
  304. unix.IPPROTO_IP,
  305. )
  306. if err != nil {
  307. return err
  308. }
  309. defer unix.Close(s)
  310. ifr := ifreqAlias6{
  311. Name: t.deviceBytes(),
  312. Addr: unix.RawSockaddrInet6{
  313. Len: unix.SizeofSockaddrInet6,
  314. Family: unix.AF_INET6,
  315. Addr: network.Addr().As16(),
  316. },
  317. PrefixMask: unix.RawSockaddrInet6{
  318. Len: unix.SizeofSockaddrInet6,
  319. Family: unix.AF_INET6,
  320. // Always set the netmask to be a /128. See activate4 for why we do this.
  321. Addr: [16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  322. },
  323. Lifetime: addrLifetime{
  324. // never expires
  325. Vltime: 0xffffffff,
  326. Pltime: 0xffffffff,
  327. },
  328. Flags: _IN6_IFF_NODAD, // No DAD, a utun has no neighbors.
  329. }
  330. if err := ioctl(uintptr(s), _SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&ifr))); err != nil {
  331. return fmt.Errorf("failed to set tun address: %s", err)
  332. }
  333. if err := addRoute(network, t.linkAddr.Load()); err != nil {
  334. return fmt.Errorf("failed to add route for %v: %w", network.Masked(), err)
  335. }
  336. return nil
  337. }
  338. func (t *tun) reload(c *config.C, initial bool, r *config.Reload) error {
  339. change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
  340. if err != nil {
  341. return err
  342. }
  343. if !initial && !change {
  344. return nil
  345. }
  346. routeTree, err := makeRouteTree(t.l, routes, false)
  347. if err != nil {
  348. return err
  349. }
  350. // Teach nebula how to handle the routes before establishing them in the system table
  351. oldRoutes := t.Routes.Swap(&routes)
  352. t.routeTree.Store(routeTree)
  353. if !initial {
  354. // Remove first, if the system removes a wanted route hopefully it will be re-added next
  355. err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes), r)
  356. if err != nil {
  357. util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
  358. r.Failed()
  359. }
  360. // Ensure any routes we actually want are installed
  361. err = t.addRoutes(true, r)
  362. if err != nil {
  363. // Catch any stray logs
  364. util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
  365. r.Failed()
  366. }
  367. }
  368. return nil
  369. }
  370. func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
  371. r, ok := t.routeTree.Load().Lookup(ip)
  372. if ok {
  373. return r
  374. }
  375. return routing.Gateways{}
  376. }
  377. func fetchRoutes() ([]netroute.Message, error) {
  378. rib, err := netroute.FetchRIB(unix.AF_UNSPEC, unix.NET_RT_DUMP, 0)
  379. if err != nil {
  380. return nil, err
  381. }
  382. return netroute.ParseRIB(unix.NET_RT_DUMP, rib)
  383. }
  384. // routeInterfaceIn returns the index of the interface prefix is routed through, or 0 if msgs has no route for exactly
  385. // that prefix. Routes scoped to an interface are skipped, they don't stop us adding ours.
  386. func routeInterfaceIn(msgs []netroute.Message, prefix netip.Prefix) int {
  387. for _, msg := range msgs {
  388. m, ok := msg.(*netroute.RouteMessage)
  389. if !ok || len(m.Addrs) <= unix.RTAX_NETMASK || m.Flags&unix.RTF_IFSCOPE != 0 {
  390. continue
  391. }
  392. if p, ok := routePrefix(m.Addrs[unix.RTAX_DST], m.Addrs[unix.RTAX_NETMASK]); ok && p == prefix.Masked() {
  393. return m.Index
  394. }
  395. }
  396. return 0
  397. }
  398. // routePrefix returns the prefix a route covers. Host routes have no netmask.
  399. func routePrefix(dst, mask netroute.Addr) (netip.Prefix, bool) {
  400. switch d := dst.(type) {
  401. case *netroute.Inet4Addr:
  402. bits := 32
  403. if m, ok := mask.(*netroute.Inet4Addr); ok {
  404. bits = maskBits(m.IP[:])
  405. }
  406. return netip.PrefixFrom(netip.AddrFrom4(d.IP), bits), true
  407. case *netroute.Inet6Addr:
  408. bits := 128
  409. if m, ok := mask.(*netroute.Inet6Addr); ok {
  410. bits = maskBits(m.IP[:])
  411. }
  412. return netip.PrefixFrom(netip.AddrFrom16(d.IP), bits), true
  413. }
  414. return netip.Prefix{}, false
  415. }
  416. func maskBits(mask []byte) int {
  417. n := 0
  418. for _, b := range mask {
  419. n += bits.OnesCount8(b)
  420. }
  421. return n
  422. }
  423. // Get the LinkAddr for the interface of the given name
  424. // Is there an easier way to fetch this when we create the interface?
  425. // Maybe SIOCGIFINDEX? but this doesn't appear to exist in the darwin headers.
  426. func getLinkAddr(name string) (*netroute.LinkAddr, error) {
  427. rib, err := netroute.FetchRIB(unix.AF_UNSPEC, unix.NET_RT_IFLIST, 0)
  428. if err != nil {
  429. return nil, err
  430. }
  431. msgs, err := netroute.ParseRIB(unix.NET_RT_IFLIST, rib)
  432. if err != nil {
  433. return nil, err
  434. }
  435. for _, m := range msgs {
  436. switch m := m.(type) {
  437. case *netroute.InterfaceMessage:
  438. if m.Name == name {
  439. sa, ok := m.Addrs[unix.RTAX_IFP].(*netroute.LinkAddr)
  440. if ok {
  441. return sa, nil
  442. }
  443. }
  444. }
  445. }
  446. return nil, nil
  447. }
  448. func (t *tun) addRoutes(logErrors bool, rl *config.Reload) error {
  449. // The route tree is still ours, the system routing table is not
  450. if t.hostOwned {
  451. return nil
  452. }
  453. // Activate hasn't run yet, it will install the routes
  454. linkAddr := t.linkAddr.Load()
  455. if linkAddr == nil {
  456. return nil
  457. }
  458. routes := *t.Routes.Load()
  459. systemRoutes := sync.OnceValues(fetchRoutes)
  460. for _, r := range routes {
  461. if len(r.Via) == 0 || !r.Install {
  462. // We don't allow route MTUs so only install routes with a via
  463. continue
  464. }
  465. err := addRoute(r.Cidr, linkAddr)
  466. if err != nil {
  467. if errors.Is(err, unix.EEXIST) {
  468. // Every route we added already exists on a reload, only warn about someone else's
  469. msgs, err := systemRoutes()
  470. if err != nil {
  471. t.l.Warn("unable to add unsafe_route, a route already exists but its interface could not be determined", "route", r.Cidr, "error", err)
  472. } else if routeInterfaceIn(msgs, r.Cidr) != linkAddr.Index {
  473. t.l.Warn("unable to add unsafe_route, the destination is already routed through another interface", "route", r.Cidr)
  474. }
  475. } else {
  476. retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
  477. if logErrors {
  478. retErr.Log(t.l)
  479. rl.Failed()
  480. } else {
  481. return retErr
  482. }
  483. }
  484. } else {
  485. t.l.Info("Added route", "route", r)
  486. // A cidr listed twice would otherwise find our own route missing from an older fetch
  487. systemRoutes = sync.OnceValues(fetchRoutes)
  488. }
  489. }
  490. return nil
  491. }
  492. func (t *tun) removeRoutes(routes []Route, rl *config.Reload) error {
  493. linkAddr := t.linkAddr.Load()
  494. if t.hostOwned || linkAddr == nil {
  495. return nil
  496. }
  497. for _, r := range routes {
  498. if len(r.Via) == 0 || !r.Install {
  499. // addRoutes never installs these
  500. continue
  501. }
  502. err := delRoute(r.Cidr, linkAddr)
  503. if errors.Is(err, unix.ESRCH) {
  504. // Already gone, which is what we wanted
  505. t.l.Info("Route was already removed", "route", r)
  506. } else if err != nil {
  507. t.l.Error("Failed to remove route", "error", err, "route", r)
  508. rl.Failed()
  509. } else {
  510. t.l.Info("Removed route", "route", r)
  511. }
  512. }
  513. return nil
  514. }
  515. func addRoute(prefix netip.Prefix, gateway netroute.Addr) error {
  516. sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
  517. if err != nil {
  518. return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
  519. }
  520. defer unix.Close(sock)
  521. route := &netroute.RouteMessage{
  522. Version: unix.RTM_VERSION,
  523. Type: unix.RTM_ADD,
  524. Flags: unix.RTF_UP,
  525. Seq: 1,
  526. }
  527. if prefix.Addr().Is4() {
  528. route.Addrs = []netroute.Addr{
  529. unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
  530. unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
  531. unix.RTAX_GATEWAY: gateway,
  532. }
  533. } else {
  534. route.Addrs = []netroute.Addr{
  535. unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
  536. unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
  537. unix.RTAX_GATEWAY: gateway,
  538. }
  539. }
  540. data, err := route.Marshal()
  541. if err != nil {
  542. return fmt.Errorf("failed to create route.RouteMessage: %w", err)
  543. }
  544. _, err = unix.Write(sock, data[:])
  545. if err != nil {
  546. return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
  547. }
  548. return nil
  549. }
  550. func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
  551. sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
  552. if err != nil {
  553. return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
  554. }
  555. defer unix.Close(sock)
  556. route := netroute.RouteMessage{
  557. Version: unix.RTM_VERSION,
  558. Type: unix.RTM_DELETE,
  559. Seq: 1,
  560. }
  561. if prefix.Addr().Is4() {
  562. route.Addrs = []netroute.Addr{
  563. unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
  564. unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
  565. unix.RTAX_GATEWAY: gateway,
  566. }
  567. } else {
  568. route.Addrs = []netroute.Addr{
  569. unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
  570. unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
  571. unix.RTAX_GATEWAY: gateway,
  572. }
  573. }
  574. data, err := route.Marshal()
  575. if err != nil {
  576. return fmt.Errorf("failed to create route.RouteMessage: %w", err)
  577. }
  578. _, err = unix.Write(sock, data[:])
  579. if err != nil {
  580. return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
  581. }
  582. return nil
  583. }
  584. // tunWritev and tunReadv are linkname'd to x/sys/unix's libc-routed writev/readv stubs so the
  585. // calls go through libSystem's pinned trampoline. A raw syscall.Syscall(SYS_WRITEV/SYS_READV, ...)
  586. // on darwin/arm64 emits an SVC #0x80 trap (see $GOROOT/src/syscall/asm_darwin_arm64.s), the path
  587. // Apple keeps warning they will eventually disallow. We pull the low-level stubs instead of calling
  588. // unix.Writev/unix.Readv because those take [][]byte and rebuild the []Iovec every call, which
  589. // heap-allocates the header; linkname'ing the stubs lets us hand them our own stack-allocated
  590. // iovecs. See golang/go#78049.
  591. //go:linkname tunWritev golang.org/x/sys/unix.writev
  592. //go:noescape
  593. func tunWritev(fd int, iovecs []unix.Iovec) (n int, err error)
  594. //go:linkname tunReadv golang.org/x/sys/unix.readv
  595. //go:noescape
  596. func tunReadv(fd int, iovecs []unix.Iovec) (n int, err error)
  597. // Read pulls one IP packet off the utun device, scattering the 4 byte protocol header away from
  598. // the packet so the payload lands directly in to.
  599. func (t *tun) Read(to []byte) (int, error) {
  600. var head [4]byte
  601. rc, err := t.f.SyscallConn()
  602. if err != nil {
  603. return 0, err
  604. }
  605. var n int
  606. var callErr error
  607. err = rc.Read(func(fd uintptr) bool {
  608. iovecs := []unix.Iovec{
  609. {Base: &head[0], Len: 4},
  610. {Base: &to[0], Len: uint64(len(to))},
  611. }
  612. n, callErr = tunReadv(int(fd), iovecs)
  613. if errno, ok := callErr.(syscall.Errno); ok && errno.Temporary() {
  614. return false
  615. }
  616. return true
  617. })
  618. if err != nil {
  619. return 0, err
  620. }
  621. if callErr != nil {
  622. return 0, callErr
  623. }
  624. if n < 4 {
  625. return 0, nil
  626. }
  627. return n - 4, nil
  628. }
  629. // Write pushes one IP packet onto the utun device. Safe for concurrent use:
  630. // the AF prefix and iovecs are per-call stack state, and the fd write itself
  631. // serializes on the runtime's fd mutex (see the Queue contract in tio.go).
  632. func (t *tun) Write(from []byte) (int, error) {
  633. if len(from) == 0 {
  634. return 0, syscall.EIO
  635. }
  636. ipVer := from[0] >> 4
  637. var head [4]byte
  638. switch ipVer {
  639. case 4:
  640. head[3] = syscall.AF_INET
  641. case 6:
  642. head[3] = syscall.AF_INET6
  643. default:
  644. return 0, fmt.Errorf("unable to determine IP version from packet")
  645. }
  646. // Grab rc as a local so the compiler can devirtualize the call and keep the closure on the stack.
  647. rc, err := t.f.SyscallConn()
  648. if err != nil {
  649. return 0, err
  650. }
  651. var n int
  652. var callErr error
  653. err = rc.Write(func(fd uintptr) bool {
  654. iovecs := []unix.Iovec{
  655. {Base: &head[0], Len: 4},
  656. {Base: &from[0], Len: uint64(len(from))},
  657. }
  658. n, callErr = tunWritev(int(fd), iovecs)
  659. // Type-assert to syscall.Errno so the EAGAIN/EWOULDBLOCK/EINTR check doesn't box the errno
  660. // constants into error interfaces on every call.
  661. if errno, ok := callErr.(syscall.Errno); ok && errno.Temporary() {
  662. return false
  663. }
  664. return true
  665. })
  666. if err != nil {
  667. return 0, err
  668. }
  669. if callErr != nil {
  670. return 0, callErr
  671. }
  672. return n - 4, nil
  673. }
  674. func (t *tun) Networks() []netip.Prefix {
  675. return t.vpnNetworks
  676. }
  677. func (t *tun) Name() string {
  678. return t.Device
  679. }
  680. func (t *tun) Queues(int) ([]tio.Queue, error) {
  681. return []tio.Queue{tio.NewSingleQueue(t, defaultBatchBufSize)}, nil
  682. }