Repository navigation
Expand file tree
/
Copy pathtailscale.go
More file actions
721 lines (644 loc) · 16.1 KB
/
Copy pathtailscale.go
File metadata and controls
721 lines (644 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// A Go c-archive of the tsnet package. See tailscale.h for details.
package main
//#include "errno.h"
import "C"
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"os"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
"tailscale.com/hostinfo"
"tailscale.com/ipn"
"tailscale.com/tsnet"
"tailscale.com/types/logger"
)
func main() {}
// servers tracks all the allocated *tsnet.Server objects.
var servers struct {
mu sync.Mutex
next C.int
m map[C.int]*server
}
type server struct {
s *tsnet.Server
lastErr string
started bool
}
func getServer(sd C.int) *server {
servers.mu.Lock()
defer servers.mu.Unlock()
return servers.m[sd]
}
// listeners tracks all the tsnet_listener objects allocated via tsnet_listen.
var listeners struct {
mu sync.Mutex
m map[C.int]*listener
}
type listener struct {
s *server
ln net.Listener
fd int // go side fd of socketpair sent to C
mu sync.Mutex
// m maps the fd number C holds for an accepted connection (not our
// sender-side number; recvmsg installs a new one) to its remote IP.
m map[C.int]string
}
// listenAddrLen is the size of the fixed-size, NUL-padded record that
// carries each accepted connection's remote IP over the listener
// socketpair. Fixed size so TsnetAccept frames the stream with
// exact-size reads; 45 bytes covers the longest textual IP.
const listenAddrLen = 64
// conns tracks all the pipe(2)s allocated via tsnet_dial.
var conns struct {
mu sync.Mutex
m map[C.int]*conn // keyed by the FD given to C (w)
}
type conn struct {
s *tsnet.Server
c net.Conn
r *os.File // r is the local socket to the C client
}
func (s *server) recErr(err error) C.int {
if err == nil {
s.lastErr = ""
return 0
}
s.lastErr = err.Error()
return -1
}
//export TsnetNewServer
func TsnetNewServer() C.int {
servers.mu.Lock()
defer servers.mu.Unlock()
if servers.m == nil {
servers.m = map[C.int]*server{}
hostinfo.SetApp("libtailscale")
}
if servers.next == 0 {
servers.next = 42<<16 + 1
}
sd := servers.next
servers.next++
s := &server{s: &tsnet.Server{}}
servers.m[sd] = s
return (C.int)(sd)
}
//export TsnetStart
func TsnetStart(sd C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
err := s.s.Start()
if err == nil {
s.started = true
}
return s.recErr(err)
}
//export TsnetUp
func TsnetUp(sd C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
_, err := s.s.Up(context.Background()) // cancellation is via TsnetClose
if err == nil {
s.started = true
}
return s.recErr(err)
}
//export TsnetClose
func TsnetClose(sd C.int) C.int {
servers.mu.Lock()
s := servers.m[sd]
if s != nil {
delete(servers.m, sd)
}
servers.mu.Unlock()
if s == nil {
return C.EBADF
}
// TODO: cancel Up
// TODO: close related listeners / conns.
if !s.started {
// Server was never started, nothing to close.
return 0
}
if err := s.s.Close(); err != nil {
s.s.Logf("tailscale_close: failed with %v", err)
return -1
}
return 0
}
//export TsnetGetIps
func TsnetGetIps(sd C.int, buf *C.char, buflen C.size_t) C.int {
if buf == nil {
panic("errmsg passed nil buf")
} else if buflen == 0 {
panic("errmsg passed buflen of 0")
}
servers.mu.Lock()
s := servers.m[sd]
servers.mu.Unlock()
out := unsafe.Slice((*byte)(unsafe.Pointer(buf)), buflen)
if s == nil {
out[0] = '\x00'
return C.EBADF
}
ip4, ip6 := s.s.TailscaleIPs()
joined := strings.Join([]string{ip4.String(), ip6.String()}, ",")
n := copy(out, joined)
if n >= len(out) {
out[len(out)-1] = '\x00' // always NUL-terminate
return C.ERANGE
}
out[n] = '\x00'
return 0
}
//export TsnetErrmsg
func TsnetErrmsg(sd C.int, buf *C.char, buflen C.size_t) C.int {
if buf == nil {
panic("errmsg passed nil buf")
} else if buflen == 0 {
panic("errmsg passed buflen of 0")
}
servers.mu.Lock()
s := servers.m[sd]
servers.mu.Unlock()
out := unsafe.Slice((*byte)(unsafe.Pointer(buf)), buflen)
if s == nil {
out[0] = '\x00'
return C.EBADF
}
n := copy(out, s.lastErr)
if n >= len(out) {
out[len(out)-1] = '\x00' // always NUL-terminate
return C.ERANGE
}
out[n] = '\x00'
return 0
}
//export TsnetListen
func TsnetListen(sd C.int, network, addr *C.char, listenerOut *C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
ln, err := s.s.Listen(C.GoString(network), C.GoString(addr))
if err != nil {
return s.recErr(err)
}
s.started = true
// The tailscale_listener we return to C is one side of a socketpair(2).
// We do this so we can proactively call ln.Accept in a goroutine and
// feed an fd for the connection through the listener. This lets C use
// epoll on the tailscale_listener to know if it should call
// tailscale_accept, which avoids a blocking call on the far side.
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
if err != nil {
return s.recErr(err)
}
sp := fds[1]
fdC := C.int(fds[0])
listeners.mu.Lock()
if listeners.m == nil {
listeners.m = map[C.int]*listener{}
}
listener := &listener{s: s, ln: ln, fd: sp, m: map[C.int]string{}}
listeners.m[fdC] = listener
listeners.mu.Unlock()
cleanup := func() {
// If fdC is closed on the C side, then we end up calling
// into cleanup twice. Be careful to avoid syscall.Close
// twice as the FD may have been reallocated.
listeners.mu.Lock()
if tsLn, ok := listeners.m[fdC]; ok && tsLn.ln == ln {
delete(listeners.m, fdC)
syscall.Close(sp)
}
listeners.mu.Unlock()
ln.Close()
}
go func() {
// fdC is never written to, so trying to read from sp blocks
// until fdC is closed. We use this as a signal that C is
// done with the listener, and we can tear it down.
//
// TODO: would using os.NewFile avoid a locked up thread?
var buf [256]byte
syscall.Read(sp, buf[:])
cleanup()
}()
go func() {
defer cleanup()
for {
netConn, err := ln.Accept()
if err != nil {
return
}
var connFd C.int
if err := newConn(s, netConn, &connFd); err != nil {
if s.s.Logf != nil {
s.s.Logf("libtailscale.accept: newConn: %v", err)
}
netConn.Close()
continue
}
ip := extractIP(netConn.RemoteAddr().String())
if len(ip) >= listenAddrLen {
if s.s.Logf != nil {
s.s.Logf("libtailscale.accept: remote address %q does not fit in a %d-byte record", ip, listenAddrLen)
}
netConn.Close()
syscall.Close(int(connFd))
continue
}
var addrRec [listenAddrLen]byte
copy(addrRec[:], ip)
rights := syscall.UnixRights(int(connFd))
err = syscall.Sendmsg(sp, addrRec[:], rights, nil, 0)
if err != nil {
// a failed sendmsg delivered nothing (sp being closed is
// handled by the read goroutine above)
if s.s.Logf != nil {
s.s.Logf("libtailscale.accept: sendmsg failed: %v", err)
}
netConn.Close()
}
syscall.Close(int(connFd)) // now owned by recvmsg
}
}()
*listenerOut = fdC
return 0
}
//export TsnetAccept
func TsnetAccept(listenerFd C.int, connOut *C.int) C.int {
listeners.mu.Lock()
ln := listeners.m[listenerFd]
listeners.mu.Unlock()
if ln == nil {
return C.EBADF
}
// One record per connection: the fd via SCM_RIGHTS plus its remote
// IP. The fd parsed below is the number the kernel installed, i.e.
// the number C will hold.
//
// The socketpair is a stream socket, so records are framed here:
// each read asks for the remaining bytes of the head record, with
// MSG_WAITALL. The FIFO never serves a later record's bytes first,
// so each accept consumes exactly one record, even concurrently or
// after EINTR.
data := make([]byte, listenAddrLen)
cbuf := make([]byte, unix.CmsgLen(int(unsafe.Sizeof((C.int)(0)))))
var n, oobn int
for n < len(data) {
buf := data[n:]
if n > 0 {
// the cmsg rode with the record's first byte; only the tail remains
cbuf = nil
}
nn, on, _, _, err := syscall.Recvmsg(int(listenerFd), buf, cbuf, syscall.MSG_WAITALL)
n += nn
if nn > 0 {
oobn = on
}
if err == syscall.EINTR {
continue // partial bytes stay record-aligned, keep draining
}
if err != nil {
return ln.s.recErr(err)
}
if nn == 0 {
// EOF mid-record: the listener was closed on the C side.
return ln.s.recErr(fmt.Errorf("libtailscale: listener closed mid-record: got %d of %d bytes", n, len(data)))
}
}
scms, err := syscall.ParseSocketControlMessage(cbuf[:oobn])
if err != nil {
return ln.s.recErr(err)
}
if len(scms) != 1 {
return ln.s.recErr(fmt.Errorf("libtailscale: got %d control messages, want 1", len(scms)))
}
fds, err := syscall.ParseUnixRights(&scms[0])
if err != nil {
return ln.s.recErr(err)
}
if len(fds) != 1 {
return ln.s.recErr(fmt.Errorf("libtailscale: got %d FDs, want 1", len(fds)))
}
fd := C.int(fds[0])
// the entry must exist before C can learn this fd number
addrLen := bytes.IndexByte(data, 0)
if addrLen < 0 {
addrLen = listenAddrLen
}
ln.mu.Lock()
ln.m[fd] = string(data[:addrLen])
ln.mu.Unlock()
*connOut = fd
return 0
}
func newConn(s *server, netConn net.Conn, connOut *C.int) error {
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
if err != nil {
return err
}
r := os.NewFile(uintptr(fds[1]), "socketpair-r")
c := &conn{s: s.s, c: netConn, r: r}
fdC := C.int(fds[0])
conns.mu.Lock()
if conns.m == nil {
conns.m = make(map[C.int]*conn)
}
conns.m[fdC] = c
conns.mu.Unlock()
connCleanup := func() {
var inCleanup bool
conns.mu.Lock()
if tsConn, ok := conns.m[fdC]; ok && tsConn.c == netConn {
delete(conns.m, fdC)
inCleanup = true
}
conns.mu.Unlock()
if !inCleanup {
return
}
r.Close()
netConn.Close()
}
// the Shutdowns below must precede connCleanup: r.Fd() after Close
// can return a reused fd number. Wait for both copy directions.
var copies sync.WaitGroup
copies.Add(2)
go func() {
defer copies.Done()
var b [1 << 16]byte
io.CopyBuffer(r, netConn, b[:])
syscall.Shutdown(int(r.Fd()), syscall.SHUT_WR)
if cr, ok := netConn.(interface{ CloseRead() error }); ok {
cr.CloseRead()
}
}()
go func() {
defer copies.Done()
var b [1 << 16]byte
io.CopyBuffer(netConn, r, b[:])
syscall.Shutdown(int(r.Fd()), syscall.SHUT_RD)
if cw, ok := netConn.(interface{ CloseWrite() error }); ok {
cw.CloseWrite()
}
}()
go func() {
copies.Wait()
connCleanup()
}()
*connOut = fdC
return nil
}
//export TsnetGetRemoteAddr
func TsnetGetRemoteAddr(listener C.int, conn C.int, buf *C.char, buflen C.size_t) C.int {
if buf == nil {
panic("errmsg passed nil buf")
} else if buflen == 0 {
panic("errmsg passed buflen of 0")
}
out := unsafe.Slice((*byte)(unsafe.Pointer(buf)), buflen)
listeners.mu.Lock()
defer listeners.mu.Unlock()
l := listeners.m[listener]
if l == nil {
out[0] = '\x00'
return C.EBADF
}
l.mu.Lock()
defer l.mu.Unlock()
ip, ok := l.m[conn]
if !ok {
// set errmsg too: EBADF with an empty message is not debuggable
l.s.lastErr = fmt.Sprintf("libtailscale: getremoteaddr: no remote address recorded for conn %d on listener %d", conn, listener)
out[0] = '\x00'
return C.EBADF
}
n := copy(out, ip)
if n >= len(out) {
out[len(out)-1] = '\x00' // always NUL-terminate
return C.ERANGE
}
out[n] = '\x00'
return 0
}
// Strips the port from connection IPs
func extractIP(ipWithPort string) string {
re := regexp.MustCompile(`(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\[([0-9a-fA-F:]+)\]`)
match := re.FindString(ipWithPort)
return match
}
//export TsnetDial
func TsnetDial(sd C.int, network, addr *C.char, connOut *C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
netConn, err := s.s.Dial(context.Background(), C.GoString(network), C.GoString(addr))
if err != nil {
return s.recErr(err)
}
s.started = true
if err := newConn(s, netConn, connOut); err != nil {
return s.recErr(err)
}
return 0
}
//export TsnetSetDir
func TsnetSetDir(sd C.int, str *C.char) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
s.s.Dir = C.GoString(str)
return 0
}
//export TsnetSetHostname
func TsnetSetHostname(sd C.int, str *C.char) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
s.s.Hostname = C.GoString(str)
return 0
}
//export TsnetSetAuthKey
func TsnetSetAuthKey(sd C.int, str *C.char) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
s.s.AuthKey = C.GoString(str)
return 0
}
//export TsnetSetControlURL
func TsnetSetControlURL(sd C.int, str *C.char) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
s.s.ControlURL = C.GoString(str)
return 0
}
//export TsnetSetEphemeral
func TsnetSetEphemeral(sd C.int, e int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
if e == 0 {
s.s.Ephemeral = false
} else {
s.s.Ephemeral = true
}
return 0
}
//export TsnetSetLogFD
func TsnetSetLogFD(sd, fd C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
if fd == -1 {
s.s.Logf = logger.Discard
return 0
}
f := os.NewFile(uintptr(fd), "logfd")
s.s.Logf = func(format string, args ...any) {
fmt.Fprintf(f, format, args...)
fmt.Fprintf(f, "\n")
}
return 0
}
//export TsnetLoopback
func TsnetLoopback(sd C.int, addrOut *C.char, addrLen C.size_t, proxyOut *C.char, localOut *C.char) C.int {
// Panic here to ensure we always leave the out values NUL-terminated.
if addrOut == nil {
panic("loopback_api passed nil addr_out")
} else if addrLen == 0 {
panic("loopback_api passed addrlen of 0")
} else if proxyOut == nil {
panic("loopback_api passed nil proxy_cred_out")
} else if localOut == nil {
panic("loopback_api passed nil local_api_cred_out")
}
// Start out NUL-termianted to cover error conditions.
*addrOut = '\x00'
*localOut = '\x00'
*proxyOut = '\x00'
s := getServer(sd)
if s == nil {
return C.EBADF
}
addr, proxyCred, localAPICred, err := s.s.Loopback()
if err != nil {
return s.recErr(err)
}
if len(proxyCred) != 32 {
return s.recErr(fmt.Errorf("libtailscale: len(proxyCred)=%d, want 32", len(proxyCred)))
}
if len(localAPICred) != 32 {
return s.recErr(fmt.Errorf("libtailscale: len(localAPICred)=%d, want 32", len(localAPICred)))
}
out := unsafe.Slice((*byte)(unsafe.Pointer(addrOut)), addrLen)
n := copy(out, addr)
if n >= len(out) {
out[len(out)-1] = '\x00' // always NUL-terminate
return C.ERANGE
}
out[n] = '\x00'
// proxyOut and localOut are non-nil and 33 bytes long because
// they are defined in C as char cred_out[static 33].
out = unsafe.Slice((*byte)(unsafe.Pointer(proxyOut)), 33)
copy(out, proxyCred)
out[32] = '\x00'
out = unsafe.Slice((*byte)(unsafe.Pointer(localOut)), 33)
copy(out, localAPICred)
out[32] = '\x00'
return 0
}
//export TsnetStatusJSON
func TsnetStatusJSON(sd C.int, jsonOut **C.char) C.int {
if jsonOut == nil {
panic("status_json passed nil json_out")
}
*jsonOut = nil
s := getServer(sd)
if s == nil {
return C.EBADF
}
// LocalClient rides tsnet's in-memory LocalAPI listener — unlike
// Loopback()'s TCP listener it cannot be reclaimed by the OS while
// the process is suspended (iOS), so status reads keep working on
// long-lived nodes.
lc, err := s.s.LocalClient()
if err != nil {
return s.recErr(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
st, err := lc.Status(ctx)
if err != nil {
return s.recErr(err)
}
b, err := json.Marshal(st)
if err != nil {
return s.recErr(err)
}
*jsonOut = C.CString(string(b))
return 0
}
//export TsnetEnableFunnelToLocalhostPlaintextHttp1
func TsnetEnableFunnelToLocalhostPlaintextHttp1(sd C.int, localhostPort C.int) C.int {
s := getServer(sd)
if s == nil {
return C.EBADF
}
ctx := context.Background()
lc, err := s.s.LocalClient()
if err != nil {
return s.recErr(err)
}
st, err := lc.StatusWithoutPeers(ctx)
if err != nil {
return s.recErr(err)
}
domain := st.CertDomains[0]
hp := ipn.HostPort(net.JoinHostPort(domain, strconv.Itoa(443)))
tcpForward := fmt.Sprintf("127.0.0.1:%d", localhostPort)
sc := &ipn.ServeConfig{
TCP: map[uint16]*ipn.TCPPortHandler{
443: {
TCPForward: tcpForward,
TerminateTLS: domain,
},
},
AllowFunnel: map[ipn.HostPort]bool{
hp: true,
},
}
lc.SetServeConfig(ctx, sc)
if !sc.AllowFunnel[hp] {
return s.recErr(fmt.Errorf("libtailscale: failed to enable funnel"))
}
return 0
}