-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathendpoint.go
400 lines (346 loc) · 10.7 KB
/
endpoint.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
package vmnet
import (
"log/slog"
"net"
"os"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/insomniacslk/dhcp/dhcpv4"
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/network/arp"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
type endpoint struct {
// conn is the set of connection each identifying one inbound/outbound
// channel.
conns syncmap[tcpip.Address, net.Conn]
arpTable syncmap[tcpip.Address, tcpip.LinkAddress]
// mtu (maximum transmission unit) is the maximum size of a packet.
mtu uint32
// addr is the MAC address of the endpoint.
addr tcpip.LinkAddress
subnet tcpip.Subnet
dispatcher stack.NetworkDispatcher
// wg keeps track of running goroutines.
wg sync.WaitGroup
// closed is a function to be called when the FD's peer (if any) closes
// its end of the communication pipe.
closed func(tcpip.Address, error)
writer *pcapgo.Writer
snapLen int
pool *bytePool
logger *slog.Logger
dhcpv4Handler *dhcpHandler
}
type gatewayEndpointOption struct {
MTU uint32
Address tcpip.LinkAddress
Subnet tcpip.Subnet
Writer *os.File
ClosedFunc func(tcpip.Address, error)
Pool *bytePool
Logger *slog.Logger
DHCPv4Handler *dhcpHandler
}
func newGatewayEndpoint(opts gatewayEndpointOption) (*endpoint, error) {
ep := &endpoint{
conns: newSyncmap[tcpip.Address, net.Conn](),
arpTable: newSyncmap[tcpip.Address, tcpip.LinkAddress](),
mtu: opts.MTU,
closed: opts.ClosedFunc,
addr: opts.Address,
subnet: opts.Subnet,
pool: opts.Pool,
logger: opts.Logger,
dhcpv4Handler: opts.DHCPv4Handler,
}
if opts.Writer != nil {
ep.writer = pcapgo.NewWriter(opts.Writer)
ep.snapLen = 65536
if err := ep.writer.WriteFileHeader(uint32(ep.snapLen), layers.LinkTypeEthernet); err != nil {
return nil, err
}
}
return ep, nil
}
func (e *endpoint) RegisterConn(ipAddr tcpip.Address, hwAddr tcpip.LinkAddress, conn net.Conn) {
e.conns.Store(ipAddr, conn)
e.arpTable.Store(ipAddr, hwAddr)
// Link endpoints are not savable. When transportation endpoints are
// saved, they stop sending outgoing packets and all incoming packets
// are rejected.
if e.dispatcher != nil {
e.wg.Add(1)
go func() {
e.dispatchLoop(ipAddr, conn)
e.wg.Done()
}()
}
}
// IsAttached implements stack.LinkEndpoint.IsAttached.
func (e *endpoint) IsAttached() bool {
return e.dispatcher != nil
}
// MTU implements stack.LinkEndpoint.MTU. It returns the value initialized
// during construction.
func (e *endpoint) MTU() uint32 {
return e.mtu
}
// Capabilities implements stack.LinkEndpoint.Capabilities.
func (e *endpoint) Capabilities() stack.LinkEndpointCapabilities {
return stack.CapabilityResolutionRequired
}
// MaxHeaderLength returns the maximum size of the link-layer header.
func (e *endpoint) MaxHeaderLength() uint16 {
return header.EthernetMinimumSize
}
// 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()
}
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType.
func (e *endpoint) ARPHardwareType() header.ARPHardwareType {
return header.ARPHardwareEther
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (e *endpoint) AddHeader(pkt stack.PacketBufferPtr) {
// 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,
})
}
// Attach launches the goroutine that reads packets from the file descriptor and
// dispatches them via the provided dispatcher. If one is already attached,
// then nothing happens.
//
// Attach implements stack.LinkEndpoint.Attach.
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) {
// nil means the NIC is being removed.
if dispatcher == nil && e.dispatcher != nil {
e.Wait()
e.dispatcher = nil
return
}
if dispatcher != nil && e.dispatcher == nil {
e.dispatcher = dispatcher
}
}
// dispatchLoop reads packets from the file descriptor in a loop and dispatches
// them to the network stack.
func (e *endpoint) dispatchLoop(ipAddr tcpip.Address, conn net.Conn) {
for {
cont, err := e.inboundDispatch(ipAddr, conn)
if err != nil || !cont {
e.conns.Delete(ipAddr)
if e.closed != nil {
e.closed(ipAddr, err)
}
return
}
}
}
// writePacket writes outbound packets to the connection. If it is not
// currently writable, the packet is dropped.
func (e *endpoint) writePacket(pkt stack.PacketBufferPtr) tcpip.Error {
data := pkt.ToView().AsSlice()
if e.writer != nil {
packetSize := pkt.Size()
e.writer.WritePacket(gopacket.CaptureInfo{
Timestamp: time.Now(),
CaptureLength: e.captureLength(packetSize),
Length: packetSize,
}, data)
}
conn, ok := e.conns.Load(pkt.EgressRoute.RemoteAddress)
if ok {
if _, err := conn.Write(data); err != nil {
e.logger.Warn("failed to write packet data in endpoint", err)
return &tcpip.ErrInvalidEndpointState{}
}
return nil
}
e.conns.Range(func(_ tcpip.Address, v net.Conn) bool {
v.Write(data)
return true
})
return nil
}
// WritePackets writes outbound packets to the underlying connection. If
// one is not currently writable, the packet is dropped.
//
// Being a batch API, each packet in pkts should have the following
// fields populated:
// - pkt.EgressRoute
// - pkt.NetworkProtocolNumber
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (written int, err tcpip.Error) {
for _, pkt := range pkts.AsSlice() {
if err := e.writePacket(pkt); err != nil {
break
}
written++
}
return written, err
}
func (e *endpoint) captureLength(packetSize int) int {
if packetSize < e.snapLen {
return packetSize
}
return e.snapLen
}
// dispatch reads one packet from the file descriptor and dispatches it.
func (e *endpoint) inboundDispatch(devAddr tcpip.Address, conn net.Conn) (bool, error) {
data := e.pool.getBytes()
defer e.pool.putBytes(data)
n, err := conn.Read(data)
if err != nil {
return false, err
}
if e.writer != nil {
e.writer.WritePacket(gopacket.CaptureInfo{
Timestamp: time.Now(),
CaptureLength: e.captureLength(n),
Length: n,
}, data[:n])
}
buf := buffer.MakeWithData(data[:n])
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buf,
})
defer pkt.DecRef()
return e.deliverOrConsumeNetworkPacket(pkt, conn)
}
func (e *endpoint) deliverOrConsumeNetworkPacket(
pkt stack.PacketBufferPtr,
conn net.Conn,
) (bool, error) {
hdr, ok := pkt.LinkHeader().Consume(int(e.MaxHeaderLength()))
if !ok {
return false, nil
}
ethHdr := header.Ethernet(hdr)
switch ethHdr.Type() {
case arp.ProtocolNumber:
return e.deliverOrConsumeARPPacket(ethHdr, pkt, conn)
case ipv4.ProtocolNumber:
return e.deliverOrConsumeIPv4Packet(ethHdr, pkt, conn)
default:
e.dispatcher.DeliverNetworkPacket(ethHdr.Type(), pkt)
return true, nil
}
}
func (e *endpoint) deliverOrConsumeARPPacket(
ethHdr header.Ethernet,
pkt stack.PacketBufferPtr,
conn net.Conn,
) (bool, error) {
data := pkt.ToView().AsSlice()
req := header.ARP(data[header.EthernetMinimumSize:])
if req.IsValid() && req.Op() == header.ARPRequest {
target := req.ProtocolAddressTarget()
linkAddr, ok := e.arpTable.Load(tcpip.AddrFromSlice(target))
if ok {
buf := make([]byte, header.EthernetMinimumSize+header.ARPSize)
eth := header.Ethernet(buf)
eth.Encode(&header.EthernetFields{
SrcAddr: linkAddr,
DstAddr: tcpip.LinkAddress(req.HardwareAddressSender()),
Type: header.ARPProtocolNumber,
})
res := header.ARP(buf[header.EthernetMinimumSize:])
res.SetIPv4OverEthernet()
res.SetOp(header.ARPReply)
copy(res.HardwareAddressSender(), linkAddr)
copy(res.ProtocolAddressSender(), req.ProtocolAddressTarget())
copy(res.HardwareAddressTarget(), req.HardwareAddressSender())
copy(res.ProtocolAddressTarget(), req.ProtocolAddressSender())
conn.Write(buf)
return true, nil
}
}
e.dispatcher.DeliverNetworkPacket(ethHdr.Type(), pkt)
return true, nil
}
func (e *endpoint) deliverOrConsumeIPv4Packet(
ethHdr header.Ethernet,
pkt stack.PacketBufferPtr,
conn net.Conn,
) (bool, error) {
data := pkt.ToView().AsSlice()
ipv4 := header.IPv4(data[header.EthernetMinimumSize:])
switch ipv4.TransportProtocol() {
case header.ICMPv4ProtocolNumber:
{
// Deliver packets to the created network stack.
if e.subnet.Contains(ipv4.DestinationAddress()) {
e.dispatcher.DeliverNetworkPacket(ethHdr.Type(), pkt)
return true, nil
}
// Only ICMPv4 echo request is emulated on the Go side.
//
// sudo privilege is required to use ICMP packets. So gvisor
// cannot use ICMPv4 packets as they are. Therefore, gvisor
// looks at the contents of the packets and converts them to
// echo requests using UDP. The result is converted to ICMPv4
// packet, which is then passed to the guest.
icmpv4 := header.ICMPv4(data[header.EthernetMinimumSize+header.IPv4MinimumSize:])
if icmpv4.Type() == header.ICMPv4Echo {
tmp := icmpv4.Payload()
payload := make([]byte, len(tmp))
copy(payload, tmp)
go pingv4(conn, pingPacket{
srcIP: ipv4.SourceAddress(),
dstIP: ipv4.DestinationAddress(),
srcMAC: ethHdr.SourceAddress(),
dstMAC: ethHdr.DestinationAddress(),
payload: payload,
ident: icmpv4.Ident(),
sequence: icmpv4.Sequence(),
})
return true, nil
}
}
case header.UDPProtocolNumber:
{
udpv4 := header.UDP(data[header.EthernetMinimumSize+header.IPv4MinimumSize:])
srcPort := udpv4.SourcePort()
dstPort := udpv4.DestinationPort()
// In order to ensure IP address allocation here, DHCP broadcast responses
// are made to the connection associated with each IP address.
if dstPort == 67 && srcPort == 68 {
msg, err := dhcpv4.FromBytes(udpv4.Payload())
if err != nil {
e.logger.Warn("failed to decode DHCPv4 packet data in endpoint", err)
return true, nil
}
go e.dhcpv4Handler.handleDHCPv4(conn, dhcpv4Packet{
srcIP: ipv4.SourceAddress(),
dstIP: ipv4.DestinationAddress(),
srcPort: srcPort,
dstPort: dstPort,
srcMAC: ethHdr.SourceAddress(),
dstMAC: ethHdr.DestinationAddress(),
msg: msg,
})
return true, nil
}
}
default:
}
e.dispatcher.DeliverNetworkPacket(ethHdr.Type(), pkt)
return true, nil
}
func (e *endpoint) ParseHeader(stack.PacketBufferPtr) bool { return true }