-
Notifications
You must be signed in to change notification settings - Fork 14
/
fdbased.go
477 lines (413 loc) · 14.1 KB
/
fdbased.go
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
// Copyright (c) 2022 RethinkDNS and its authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package netstack provides the implemention of data-link layer endpoints
// backed by boundary-preserving file descriptors (e.g., TUN devices,
// seqpacket/datagram sockets).
//
// Adopted from: github.com/google/gvisor/blob/f33d034/pkg/tcpip/link/fdbased/endpoint.go
// since fdbased isn't built when building for android (it is only built for linux).
package netstack
import (
"fmt"
"sync/atomic"
"time"
"github.com/celzero/firestack/intra/core"
"github.com/celzero/firestack/intra/log"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
var _ stack.InjectableLinkEndpoint = (*endpoint)(nil)
var _ stack.LinkEndpoint = (*endpoint)(nil)
var _ stack.LinkEndpoint = (*sniff)(nil)
var _ Swapper = (*sniff)(nil)
const invalidfd int = -1
type Swapper interface {
// Swap closes existing FDs; uses new fd and mtu.
Swap(fd, mtu int) error
}
type SeamlessEndpoint interface {
stack.LinkEndpoint
Swapper
}
// linkDispatcher reads packets from the link FD and dispatches them to the
// NetworkDispatcher.
type linkDispatcher interface {
stop()
dispatch() (bool, tcpip.Error)
}
type endpoint struct {
sync.RWMutex
// fds is the set of file descriptors each identifying one inbound/outbound
// channel. The endpoint will dispatch from all inbound channels as well as
// hash outbound packets to specific channels based on the packet hash.
fds *core.Volatile[int] // int
// mtu (maximum transmission unit) is the maximum size of a packet.
mtu atomic.Uint32
// hdrSize specifies the link-layer header size. If set to 0, no header
// is added/removed; otherwise an ethernet header is used.
hdrSize int
// addr is the address of the endpoint.
addr tcpip.LinkAddress
// caps holds the endpoint capabilities.
caps stack.LinkEndpointCapabilities
// dispatches packets from the link FD (tun device)
// to the network stack.
inboundDispatcher linkDispatcher
// the nic this endpoint is attached to.
dispatcher stack.NetworkDispatcher
// wg keeps track of running goroutines.
wg sync.WaitGroup
// maxSyscallHeaderBytes has the same meaning as
// Options.MaxSyscallHeaderBytes.
maxSyscallHeaderBytes uintptr
// writevMaxIovs is the maximum number of iovecs that may be passed to
// rawfile.NonBlockingWriteIovec, as possibly limited by
// maxSyscallHeaderBytes. (No analogous limit is defined for
// rawfile.NonBlockingSendMMsg, since in that case the maximum number of
// iovecs also depends on the number of mmsghdrs. Instead, if sendBatch
// encounters a packet whose iovec count is limited by
// maxSyscallHeaderBytes, it falls back to writing the packet using writev
// via WritePacket.)
writevMaxIovs int
}
// Options specify the details about the fd-based endpoint to be created.
type Options struct {
// FDs is a set of FDs used to read/write packets.
FDs []int
// MTU is the mtu to use for this endpoint.
MTU uint32
// EthernetHeader if true, indicates that the endpoint should read/write
// ethernet frames instead of IP packets.
EthernetHeader bool
// Address is the link address for this endpoint. Only used if
// EthernetHeader is true.
Address tcpip.LinkAddress
// SaveRestore if true, indicates that this NIC capability set should
// include CapabilitySaveRestore
SaveRestore bool
// DisconnectOk if true, indicates that this NIC capability set should
// include CapabilityDisconnectOk.
DisconnectOk bool
// TXChecksumOffload if true, indicates that this endpoints capability
// set should include CapabilityTXChecksumOffload.
TXChecksumOffload bool
// RXChecksumOffload if true, indicates that this endpoints capability
// set should include CapabilityRXChecksumOffload.
RXChecksumOffload bool
// If MaxSyscallHeaderBytes is non-zero, it is the maximum number of bytes
// of struct iovec, msghdr, and mmsghdr that may be passed by each host
// system call.
MaxSyscallHeaderBytes int
}
// New creates a new fd-based endpoint.
//
// Makes fd non-blocking, but does not take ownership of fd, which must remain
// open for the lifetime of the returned endpoint (until after the endpoint has
// stopped being using and Wait returns).
func NewFdbasedInjectableEndpoint(opts *Options) (SeamlessEndpoint, error) {
caps := stack.LinkEndpointCapabilities(0)
if opts.RXChecksumOffload {
caps |= stack.CapabilityRXChecksumOffload
}
if opts.TXChecksumOffload {
caps |= stack.CapabilityTXChecksumOffload
}
hdrSize := 0
if opts.EthernetHeader {
hdrSize = header.EthernetMinimumSize
caps |= stack.CapabilityResolutionRequired
}
if opts.SaveRestore {
caps |= stack.CapabilitySaveRestore
}
if opts.DisconnectOk {
caps |= stack.CapabilityDisconnectOk
}
if len(opts.FDs) == 0 {
return nil, fmt.Errorf("opts.FD is empty, at least one FD must be specified")
}
if opts.MaxSyscallHeaderBytes < 0 {
return nil, fmt.Errorf("opts.MaxSyscallHeaderBytes is negative")
}
e := &endpoint{
mtu: atomic.Uint32{},
fds: core.NewVolatile[int](invalidfd),
caps: caps,
addr: opts.Address,
hdrSize: hdrSize,
// MaxSyscallHeaderBytes remains unused
maxSyscallHeaderBytes: uintptr(opts.MaxSyscallHeaderBytes),
writevMaxIovs: rawfile.MaxIovs,
}
if e.maxSyscallHeaderBytes != 0 {
if max := int(e.maxSyscallHeaderBytes / rawfile.SizeofIovec); max < e.writevMaxIovs {
e.writevMaxIovs = max
}
}
// Create per channel dispatchers; usually only one.
if len(opts.FDs) != 1 {
return nil, fmt.Errorf("len(opts.FDs) = %d, expected 1", len(opts.FDs))
}
if err := e.Swap(opts.FDs[0], int(opts.MTU)); err != nil {
return nil, err
}
return e, nil
}
func createInboundDispatcher(e *endpoint, fd int) (linkDispatcher, error) {
// By default use the readv() dispatcher as it works with all kinds of
// FDs (tap/tun/unix domain sockets and af_packet).
d, err := newReadVDispatcher(fd, e)
if err != nil {
return nil, fmt.Errorf("newReadVDispatcher(%d, %+v) = %v", fd, e, err)
}
return d, nil
}
// Implements Swapper.
func (e *endpoint) Swap(fd, mtu int) (err error) {
var prev linkDispatcher
var prevfd int
defer func() {
// TODO: should we let the previous dispatcher stop on EOF?
// From prelim experiments, it seems prevfd never EOFs?
if prev != nil {
log.I("ns: tun(%d => %d): Swap: stopping previous dispatcher", prevfd, fd)
go func() {
time.Sleep(2 * time.Second) // some arbitrary delay
prev.stop()
// avoid e.Wait(), it blocks until ALL dispatchers stop, not just prev
}()
}
}()
if err = unix.SetNonblock(fd, true); err != nil {
return fmt.Errorf("unix.SetNonblock(%v) failed: %v", fd, err)
}
e.mtu.Store(uint32(mtu))
// commence WritePackets() on fd
prevfd = e.fds.Swap(fd)
e.Lock()
defer e.Unlock()
prev = e.inboundDispatcher
e.inboundDispatcher, err = createInboundDispatcher(e, fd)
if err != nil {
return fmt.Errorf("createInboundDispatcher(...) = %v", err)
}
if e.dispatcher != nil { // attached?
go e.dispatchLoop(e.inboundDispatcher)
}
return nil
}
// Attach launches the goroutine that reads packets from the file descriptor and
// dispatches them via the provided dispatcher.
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) {
e.Lock()
defer e.Unlock()
rx := e.inboundDispatcher
// Attach is called when the NIC is being created and then enabled.
// stack.CreateNIC -> nic.newNIC -> ep.Attach
// nil means the NIC is being removed.
if dispatcher == nil && e.dispatcher != nil {
if rx != nil {
rx.stop()
e.Wait()
}
e.dispatcher = nil
return
}
if dispatcher != nil && e.dispatcher == nil {
e.dispatcher = dispatcher
go e.dispatchLoop(rx)
return
}
}
// IsAttached implements stack.LinkEndpoint.IsAttached.
func (e *endpoint) IsAttached() bool {
e.RLock()
defer e.RUnlock()
return e.dispatcher != nil
}
// MTU implements stack.LinkEndpoint.MTU. It returns the value initialized
// during construction.
func (e *endpoint) MTU() uint32 {
return e.mtu.Load()
}
// Capabilities implements stack.LinkEndpoint.Capabilities.
func (e *endpoint) Capabilities() stack.LinkEndpointCapabilities {
return e.caps
}
// MaxHeaderLength returns the maximum size of the link-layer header.
func (e *endpoint) MaxHeaderLength() uint16 {
return uint16(e.hdrSize)
}
// LinkAddress returns the link address of this endpoint.
func (e *endpoint) LinkAddress() tcpip.LinkAddress {
return e.addr
}
// Wait implements stack.LinkEndpoint.Wait. It waits for the endpoint to stop
// reading from its FD.
func (e *endpoint) Wait() {
e.wg.Wait()
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (e *endpoint) AddHeader(pkt *stack.PacketBuffer) {
if e.hdrSize > 0 && pkt != nil {
// Add ethernet header if needed.
eth := header.Ethernet(pkt.LinkHeader().Push(header.EthernetMinimumSize))
eth.Encode(&header.EthernetFields{
SrcAddr: pkt.EgressRoute.LocalLinkAddress,
DstAddr: pkt.EgressRoute.RemoteLinkAddress,
Type: pkt.NetworkProtocolNumber,
})
}
}
func (e *endpoint) parseHeader(pkt *stack.PacketBuffer) bool {
if pkt == nil {
return false
}
_, ok := pkt.LinkHeader().Consume(e.hdrSize)
return ok
}
// ParseHeader implements stack.LinkEndpoint.ParseHeader.
func (e *endpoint) ParseHeader(pkt *stack.PacketBuffer) bool {
if pkt == nil {
return false
}
if e.hdrSize > 0 {
return e.parseHeader(pkt)
}
return true
}
func (e *endpoint) logPacketIfNeeded(dir sniffer.Direction, pkt *stack.PacketBuffer) {
if pkt == nil {
return
}
protocol := pkt.NetworkProtocolNumber
if sniffer.LogPackets.Load() == 1 {
sniffer.LogPacket("rdnspcap", dir, protocol, pkt)
}
}
// fd returns the file descriptor associated with the endpoint.
func (e *endpoint) fd() int {
if fd := e.fds.Load(); fd > 0 {
return fd
}
return invalidfd
}
// writePackets writes outbound packets to the file descriptor. If it is not
// currently writable, the packet is dropped.
// Way more simplified than og impl, ref: github.com/google/gvisor/issues/7125
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
// Preallocate to avoid repeated reallocation as we append to batch.
// batchSz is 47 because when SWGSO is in use then a single 65KB TCP
// segment can get split into 46 segments of 1420 bytes and a single 216
// byte segment.
const batchSz = 47
fd := e.fd() // may have been closed
if fd == invalidfd { // unlikely; panic instead?
log.E("ns: tun(-1): WritePackets (to tun): fd invalid")
return 0, &tcpip.ErrNoSuchFile{}
}
batch := make([]unix.Iovec, 0, batchSz)
packets, written := 0, 0
total := pkts.Len()
for _, pkt := range pkts.AsSlice() {
e.logPacketIfNeeded(sniffer.DirectionSend, pkt)
views := pkt.AsSlices()
numIovecs := len(views)
if len(batch)+numIovecs > rawfile.MaxIovs {
// writes in to fd, up to len(batch) not cap(batch)
if err := rawfile.NonBlockingWriteIovec(fd, batch); err != nil {
log.W("ns: tun(%d): WritePackets (to tun): err(%v), sent(%d)/total(%d)", fd, err, written, total)
return written, err
}
// mark processed packets as written
written += packets
// truncate batch
batch = batch[:0]
// reset processed packets count
packets = 0
}
for _, v := range views {
batch = rawfile.AppendIovecFromBytes(batch, v, rawfile.MaxIovs)
}
packets += 1
}
if len(batch) > 0 {
if err := rawfile.NonBlockingWriteIovec(fd, batch); err != nil {
log.W("ns: tun(%d): WritePackets (to tun): err(%v), sent(%d)/total(%d)", fd, err, packets, total)
return written, err
}
written += packets
}
log.VV("ns: tun(%d): WritePackets (to tun): written(%d)/total(%d)", fd, written, total)
return written, nil
}
// dispatchLoop reads packets from the file descriptor in a loop and dispatches
// them to the network stack. Must be run as a goroutine.
func (e *endpoint) dispatchLoop(inbound linkDispatcher) tcpip.Error {
e.wg.Add(1)
defer e.wg.Done()
fd := e.fd()
if inbound == nil {
log.W("ns: tun(%d): dispatchLoop: inbound nil", fd)
return &tcpip.ErrUnknownDevice{}
}
for {
cont, err := inbound.dispatch()
if err != nil || !cont {
log.I("ns: tun(%d): dispatchLoop: exit; err(%v)", fd, err)
return err
}
}
}
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType.
func (e *endpoint) ARPHardwareType() header.ARPHardwareType {
if e.hdrSize > 0 {
return header.ARPHardwareEther
}
return header.ARPHardwareNone
}
// InjectInbound ingresses a netstack-inbound packet.
func (e *endpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
log.VV("ns: inject-inbound (from tun) %d", protocol)
d := e.dispatcher // TODO: read lock?
if d != nil && pkt != nil {
e.logPacketIfNeeded(sniffer.DirectionRecv, pkt)
d.DeliverNetworkPacket(protocol, pkt)
} else {
log.W("ns: inject-inbound (from tun) %d pkt?(%t) dropped: endpoint not attached", protocol, pkt != nil)
}
}
// Unused: InjectOutobund implements stack.InjectableEndpoint.InjectOutbound.
// InjectOutbound egresses a tun-inbound packet.
func (e *endpoint) InjectOutbound(dest tcpip.Address, packet *buffer.View) tcpip.Error {
fd := e.fd()
log.VV("ns: tun(%d): inject-outbound (to tun) to dst(%v)", fd, dest)
// TODO: e.logPacketIfNeeded(sniffer.DirectionSend, packet)
return rawfile.NonBlockingWrite(fd, packet.AsSlice())
}