-
Notifications
You must be signed in to change notification settings - Fork 72
/
packet_packer.go
358 lines (317 loc) · 11.6 KB
/
packet_packer.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
package quic
import (
"bytes"
"errors"
"fmt"
"github.com/lucas-clemente/quic-go/ackhandler"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
)
type packedPacket struct {
number protocol.PacketNumber
raw []byte
frames []wire.Frame
encryptionLevel protocol.EncryptionLevel
}
type packetPacker struct {
connectionID protocol.ConnectionID
perspective protocol.Perspective
version protocol.VersionNumber
cryptoSetup handshake.CryptoSetup
connectionParameters handshake.ConnectionParametersManager
streamFramer *streamFramer
controlFrames []wire.Frame
stopWaiting map[protocol.PathID]*wire.StopWaitingFrame
ackFrame map[protocol.PathID]*wire.AckFrame
}
func newPacketPacker(connectionID protocol.ConnectionID,
cryptoSetup handshake.CryptoSetup,
connectionParameters handshake.ConnectionParametersManager,
streamFramer *streamFramer,
perspective protocol.Perspective,
version protocol.VersionNumber,
) *packetPacker {
return &packetPacker{
cryptoSetup: cryptoSetup,
connectionID: connectionID,
connectionParameters: connectionParameters,
perspective: perspective,
version: version,
streamFramer: streamFramer,
stopWaiting: make(map[protocol.PathID]*wire.StopWaitingFrame),
ackFrame: make(map[protocol.PathID]*wire.AckFrame),
}
}
// PackConnectionClose packs a packet that ONLY contains a ConnectionCloseFrame
func (p *packetPacker) PackConnectionClose(ccf *wire.ConnectionCloseFrame, pth *path) (*packedPacket, error) {
frames := []wire.Frame{ccf}
encLevel, sealer := p.cryptoSetup.GetSealer()
ph := p.getPublicHeader(encLevel, pth)
raw, err := p.writeAndSealPacket(ph, frames, sealer, pth)
return &packedPacket{
number: ph.PacketNumber,
raw: raw,
frames: frames,
encryptionLevel: encLevel,
}, err
}
// PackPing packs a packet that ONLY contains a PingFrame
func (p *packetPacker) PackPing(pf *wire.PingFrame, pth *path) (*packedPacket, error) {
// Add the PingFrame in front of the controlFrames
pth.SetLeastUnacked(pth.sentPacketHandler.GetLeastUnacked())
p.controlFrames = append([]wire.Frame{pf}, p.controlFrames...)
return p.PackPacket(pth)
}
func (p *packetPacker) PackAckPacket(pth *path) (*packedPacket, error) {
if p.ackFrame[pth.pathID] == nil {
return nil, errors.New("packet packer BUG: no ack frame queued")
}
encLevel, sealer := p.cryptoSetup.GetSealer()
ph := p.getPublicHeader(encLevel, pth)
frames := []wire.Frame{p.ackFrame[pth.pathID]}
if p.stopWaiting[pth.pathID] != nil {
p.stopWaiting[pth.pathID].PacketNumber = ph.PacketNumber
p.stopWaiting[pth.pathID].PacketNumberLen = ph.PacketNumberLen
frames = append(frames, p.stopWaiting[pth.pathID])
p.stopWaiting[pth.pathID] = nil
}
p.ackFrame[pth.pathID] = nil
raw, err := p.writeAndSealPacket(ph, frames, sealer, pth)
return &packedPacket{
number: ph.PacketNumber,
raw: raw,
frames: frames,
encryptionLevel: encLevel,
}, err
}
// PackHandshakeRetransmission retransmits a handshake packet, that was sent with less than forward-secure encryption
func (p *packetPacker) PackHandshakeRetransmission(packet *ackhandler.Packet, pth *path) (*packedPacket, error) {
if packet.EncryptionLevel == protocol.EncryptionForwardSecure {
return nil, errors.New("PacketPacker BUG: forward-secure encrypted handshake packets don't need special treatment")
}
sealer, err := p.cryptoSetup.GetSealerWithEncryptionLevel(packet.EncryptionLevel)
if err != nil {
return nil, err
}
if p.stopWaiting[pth.pathID] == nil {
return nil, errors.New("PacketPacker BUG: Handshake retransmissions must contain a StopWaitingFrame")
}
ph := p.getPublicHeader(packet.EncryptionLevel, pth)
p.stopWaiting[pth.pathID].PacketNumber = ph.PacketNumber
p.stopWaiting[pth.pathID].PacketNumberLen = ph.PacketNumberLen
frames := append([]wire.Frame{p.stopWaiting[pth.pathID]}, packet.Frames...)
p.stopWaiting[pth.pathID] = nil
raw, err := p.writeAndSealPacket(ph, frames, sealer, pth)
return &packedPacket{
number: ph.PacketNumber,
raw: raw,
frames: frames,
encryptionLevel: packet.EncryptionLevel,
}, err
}
// PackPacket packs a new packet
// the other controlFrames are sent in the next packet, but might be queued and sent in the next packet if the packet would overflow MaxPacketSize otherwise
func (p *packetPacker) PackPacket(pth *path) (*packedPacket, error) {
if p.streamFramer.HasCryptoStreamFrame() {
return p.packCryptoPacket(pth)
}
encLevel, sealer := p.cryptoSetup.GetSealer()
publicHeader := p.getPublicHeader(encLevel, pth)
publicHeaderLength, err := publicHeader.GetLength(p.perspective)
if err != nil {
return nil, err
}
if p.stopWaiting[pth.pathID] != nil {
p.stopWaiting[pth.pathID].PacketNumber = publicHeader.PacketNumber
p.stopWaiting[pth.pathID].PacketNumberLen = publicHeader.PacketNumberLen
}
// TODO (QDC): rework this part with PING
var isPing bool
if len(p.controlFrames) > 0 {
_, isPing = p.controlFrames[0].(*wire.PingFrame)
}
var payloadFrames []wire.Frame
if isPing {
payloadFrames = []wire.Frame{p.controlFrames[0]}
// Remove the ping frame from the control frames
p.controlFrames = p.controlFrames[1:len(p.controlFrames)]
} else {
maxSize := protocol.MaxPacketSize - protocol.ByteCount(sealer.Overhead()) - publicHeaderLength
payloadFrames, err = p.composeNextPacket(maxSize, p.canSendData(encLevel), pth)
if err != nil {
return nil, err
}
}
// Check if we have enough frames to send
if len(payloadFrames) == 0 {
return nil, nil
}
// Don't send out packets that only contain a StopWaitingFrame
if len(payloadFrames) == 1 && p.stopWaiting[pth.pathID] != nil {
return nil, nil
}
p.stopWaiting[pth.pathID] = nil
p.ackFrame[pth.pathID] = nil
raw, err := p.writeAndSealPacket(publicHeader, payloadFrames, sealer, pth)
if err != nil {
return nil, err
}
return &packedPacket{
number: publicHeader.PacketNumber,
raw: raw,
frames: payloadFrames,
encryptionLevel: encLevel,
}, nil
}
func (p *packetPacker) packCryptoPacket(pth *path) (*packedPacket, error) {
encLevel, sealer := p.cryptoSetup.GetSealerForCryptoStream()
publicHeader := p.getPublicHeader(encLevel, pth)
publicHeaderLength, err := publicHeader.GetLength(p.perspective)
if err != nil {
return nil, err
}
maxLen := protocol.MaxPacketSize - protocol.ByteCount(sealer.Overhead()) - protocol.NonForwardSecurePacketSizeReduction - publicHeaderLength
frames := []wire.Frame{p.streamFramer.PopCryptoStreamFrame(maxLen)}
raw, err := p.writeAndSealPacket(publicHeader, frames, sealer, pth)
if err != nil {
return nil, err
}
return &packedPacket{
number: publicHeader.PacketNumber,
raw: raw,
frames: frames,
encryptionLevel: encLevel,
}, nil
}
func (p *packetPacker) composeNextPacket(
maxFrameSize protocol.ByteCount,
canSendStreamFrames bool,
pth *path,
) ([]wire.Frame, error) {
var payloadLength protocol.ByteCount
var payloadFrames []wire.Frame
// STOP_WAITING and ACK will always fit
if p.stopWaiting[pth.pathID] != nil {
payloadFrames = append(payloadFrames, p.stopWaiting[pth.pathID])
l, err := p.stopWaiting[pth.pathID].MinLength(p.version)
if err != nil {
return nil, err
}
payloadLength += l
}
if p.ackFrame[pth.pathID] != nil {
payloadFrames = append(payloadFrames, p.ackFrame[pth.pathID])
l, err := p.ackFrame[pth.pathID].MinLength(p.version)
if err != nil {
return nil, err
}
payloadLength += l
}
for len(p.controlFrames) > 0 {
frame := p.controlFrames[len(p.controlFrames)-1]
minLength, err := frame.MinLength(p.version)
if err != nil {
return nil, err
}
if payloadLength+minLength > maxFrameSize {
break
}
payloadFrames = append(payloadFrames, frame)
payloadLength += minLength
p.controlFrames = p.controlFrames[:len(p.controlFrames)-1]
}
if payloadLength > maxFrameSize {
return nil, fmt.Errorf("Packet Packer BUG: packet payload (%d) too large (%d)", payloadLength, maxFrameSize)
}
if !canSendStreamFrames {
return payloadFrames, nil
}
// temporarily increase the maxFrameSize by 2 bytes
// this leads to a properly sized packet in all cases, since we do all the packet length calculations with StreamFrames that have the DataLen set
// however, for the last StreamFrame in the packet, we can omit the DataLen, thus saving 2 bytes and yielding a packet of exactly the correct size
maxFrameSize += 2
fs := p.streamFramer.PopStreamFrames(maxFrameSize - payloadLength)
if len(fs) != 0 {
fs[len(fs)-1].DataLenPresent = false
}
// TODO: Simplify
for _, f := range fs {
payloadFrames = append(payloadFrames, f)
}
for b := p.streamFramer.PopBlockedFrame(); b != nil; b = p.streamFramer.PopBlockedFrame() {
p.controlFrames = append(p.controlFrames, b)
}
return payloadFrames, nil
}
func (p *packetPacker) QueueControlFrame(frame wire.Frame, pth *path) {
switch f := frame.(type) {
case *wire.StopWaitingFrame:
p.stopWaiting[pth.pathID] = f
case *wire.AckFrame:
p.ackFrame[pth.pathID] = f
default:
p.controlFrames = append(p.controlFrames, f)
}
}
func (p *packetPacker) getPublicHeader(encLevel protocol.EncryptionLevel, pth *path) *wire.PublicHeader {
pnum := pth.packetNumberGenerator.Peek()
packetNumberLen := protocol.GetPacketNumberLengthForPublicHeader(pnum, pth.leastUnacked)
publicHeader := &wire.PublicHeader{
ConnectionID: p.connectionID,
PacketNumber: pnum,
PacketNumberLen: packetNumberLen,
TruncateConnectionID: p.connectionParameters.TruncateConnectionID(),
}
if p.perspective == protocol.PerspectiveServer && encLevel == protocol.EncryptionSecure {
publicHeader.DiversificationNonce = p.cryptoSetup.DiversificationNonce()
}
if p.perspective == protocol.PerspectiveClient && encLevel != protocol.EncryptionForwardSecure {
publicHeader.VersionFlag = true
publicHeader.VersionNumber = p.version
}
// XXX (QDC): need a additional check because of tests
if pth.sess != nil && pth.sess.handshakeComplete && p.version >= protocol.VersionMP {
publicHeader.MultipathFlag = true
publicHeader.PathID = pth.pathID
// XXX (QDC): in case of doubt, never truncate the connection ID. This might change...
publicHeader.TruncateConnectionID = false
}
return publicHeader
}
func (p *packetPacker) writeAndSealPacket(
publicHeader *wire.PublicHeader,
payloadFrames []wire.Frame,
sealer handshake.Sealer,
pth *path,
) ([]byte, error) {
raw := getPacketBuffer()
buffer := bytes.NewBuffer(raw)
if err := publicHeader.Write(buffer, p.version, p.perspective); err != nil {
return nil, err
}
payloadStartIndex := buffer.Len()
for _, frame := range payloadFrames {
err := frame.Write(buffer, p.version)
if err != nil {
return nil, err
}
}
if protocol.ByteCount(buffer.Len()+sealer.Overhead()) > protocol.MaxPacketSize {
return nil, errors.New("PacketPacker BUG: packet too large")
}
raw = raw[0:buffer.Len()]
_ = sealer.Seal(raw[payloadStartIndex:payloadStartIndex], raw[payloadStartIndex:], publicHeader.PacketNumber, raw[:payloadStartIndex])
raw = raw[0 : buffer.Len()+sealer.Overhead()]
num := pth.packetNumberGenerator.Pop()
if num != publicHeader.PacketNumber {
return nil, errors.New("packetPacker BUG: Peeked and Popped packet numbers do not match")
}
return raw, nil
}
func (p *packetPacker) canSendData(encLevel protocol.EncryptionLevel) bool {
if p.perspective == protocol.PerspectiveClient {
return encLevel >= protocol.EncryptionSecure
}
return encLevel == protocol.EncryptionForwardSecure
}