/
packet.go
305 lines (263 loc) · 8.86 KB
/
packet.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
// packet.go - Katzenpost server packet structure.
// Copyright (C) 2017 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package packet implements the Katzenpost server side packet structure.
package packet
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/katzenpost/core/constants"
"github.com/katzenpost/core/sphinx"
"github.com/katzenpost/core/sphinx/commands"
"github.com/katzenpost/core/utils"
)
var (
pktPool = sync.Pool{
New: func() interface{} {
return new(Packet)
},
}
rawPacketPool = sync.Pool{
New: func() interface{} {
b := make([]byte, constants.PacketLength)
return b
},
}
pktID uint64
)
type Packet struct {
Raw []byte
Payload []byte
// The parsed out routing commands.
NextNodeHop *commands.NextNodeHop
NodeDelay *commands.NodeDelay
Recipient *commands.Recipient
SurbReply *commands.SURBReply
ID uint64
Delay time.Duration
RecvAt time.Duration
DispatchAt time.Duration
MustForward bool
MustTerminate bool
}
// Set sets the Packet's internal components.
func (pkt *Packet) Set(payload []byte, cmds []commands.RoutingCommand) error {
pkt.Payload = payload
return pkt.splitCommands(cmds)
}
func (pkt *Packet) splitCommands(cmds []commands.RoutingCommand) error {
for _, v := range cmds {
switch cmd := v.(type) {
case *commands.NextNodeHop:
if pkt.NextNodeHop != nil {
return newRedundantError(cmd)
}
pkt.NextNodeHop = cmd
case *commands.NodeDelay:
if pkt.NodeDelay != nil {
return newRedundantError(cmd)
}
pkt.NodeDelay = cmd
case *commands.Recipient:
if pkt.Recipient != nil {
return newRedundantError(cmd)
}
pkt.Recipient = cmd
case *commands.SURBReply:
if pkt.SurbReply != nil {
return newRedundantError(cmd)
}
pkt.SurbReply = cmd
default:
return fmt.Errorf("unknown command type: %T", v)
}
}
return nil
}
// CmdsToString returns an abbreviated list of the packet's routing commands,
// suitable for debugging.
func (pkt *Packet) CmdsToString() string {
hasNextNodeHop := pkt.NextNodeHop != nil
hasNodeDelay := pkt.NodeDelay != nil
hasRecipient := pkt.Recipient != nil
hasSURBReply := pkt.SurbReply != nil
return fmt.Sprintf("NextNodeHop: %v NodeDelay: %v, Recipient: %v, SURBReply: %v", hasNextNodeHop, hasNodeDelay, hasRecipient, hasSURBReply)
}
// IsForward returns true iff the packet has routing commands indicating it is
// a forward packet destined for another hop.
func (pkt *Packet) IsForward() bool {
return pkt.NextNodeHop != nil && pkt.NodeDelay != nil && pkt.Recipient == nil && pkt.SurbReply == nil
}
// IsToUser returns true iff the packet has routing commands indicating it is
// a forward packet destined for a local user.
func (pkt *Packet) IsToUser() bool {
return pkt.NextNodeHop == nil && pkt.NodeDelay != nil && pkt.Recipient != nil && pkt.SurbReply == nil
}
// IsUnreliableToUser returns true iff the packet has routing commands
// indicating it is an unreliable forward packet destined for a local user.
func (pkt *Packet) IsUnreliableToUser() bool {
return pkt.NextNodeHop == nil && pkt.NodeDelay == nil && pkt.Recipient != nil && pkt.SurbReply == nil
}
// IsSURBReply returns true iff the packet has routing commands indicating it
// is a SURB Reply destined for a local user.
func (pkt *Packet) IsSURBReply() bool {
return pkt.NextNodeHop == nil && pkt.NodeDelay == nil && pkt.Recipient != nil && pkt.SurbReply != nil
}
// Dispose clears the packet structure and returns it to the allocation pool.
func (pkt *Packet) Dispose() {
// Note: Calling Dispose() should happen for the common code paths, but
// we rely on the GC just deallocating packets that happen to get leaked.
//
// In particular this will happen when connections get closed, since there
// is no special effort made to clean out the various queues.
// TODO/perf: Return the packet components to the various pools.
pkt.disposeRaw()
// Clear out the struct for reuse.
// pkt.raw = nil // Cleared by pkt.disposeRaw()
pkt.Payload = nil
pkt.NextNodeHop = nil
pkt.NodeDelay = nil
pkt.Recipient = nil
pkt.SurbReply = nil
pkt.ID = 0
pkt.Delay = 0
pkt.RecvAt = 0
pkt.DispatchAt = 0
pkt.MustForward = false
pkt.MustTerminate = false
// Return the packet struct to the pool.
pktPool.Put(pkt)
}
func (pkt *Packet) copyToRaw(b []byte) error {
if len(b) != constants.PacketLength {
// TODO: When we have actual large packets, handle them.
return fmt.Errorf("invalid Sphinx packet size: %v", len(b))
}
// The common case of standard packet sizes uses a pool allocator
// to store the raw packets.
pkt.Raw = rawPacketPool.Get().([]byte)
// Sanity check, just in case the pool allocator is doing something dumb.
if len(pkt.Raw) != len(b) {
panic("BUG: Pool allocated rawPkt has incorrect size")
}
// Copy the raw packet into pkt's buffer.
copy(pkt.Raw, b)
return nil
}
func (pkt *Packet) disposeRaw() {
if len(pkt.Raw) == constants.PacketLength {
utils.ExplicitBzero(pkt.Raw)
rawPacketPool.Put(pkt.Raw) // nolint: megacheck
}
pkt.Raw = nil
}
// New allocates a new Packet, with the specified raw payload.
func New(raw []byte) (*Packet, error) {
id := atomic.AddUint64(&pktID, 1)
return NewWithID(raw, id)
}
// NewWithID allocates a new Packet, with the specified raw payload and ID.
// Most callers should use New, this exists to support serializing packets
// to external memory.
func NewWithID(raw []byte, id uint64) (*Packet, error) {
v := pktPool.Get()
pkt := v.(*Packet)
pkt.ID = id
if err := pkt.copyToRaw(raw); err != nil {
pkt.Dispose()
return nil, err
}
return pkt, nil
}
func newRedundantError(cmd commands.RoutingCommand) error {
return fmt.Errorf("redundant command: %T", cmd)
}
func ParseForwardPacket(pkt *Packet) ([]byte, []byte, error) {
const (
flagsPadding = 0
flagsSURB = 1
reserved = 0
)
var hdrLength = constants.SphinxPlaintextHeaderLength + sphinx.SURBLength
// Sanity check the forward packet payload length.
if len(pkt.Payload) != constants.ForwardPayloadLength {
return nil, nil, fmt.Errorf("invalid payload length: %v", len(pkt.Payload))
}
// Parse the payload, which should be a valid BlockSphinxPlaintext.
b := pkt.Payload
if len(b) < hdrLength {
return nil, nil, fmt.Errorf("truncated message block")
}
if b[1] != reserved {
return nil, nil, fmt.Errorf("invalid message reserved: 0x%02x", b[1])
}
ct := b[hdrLength:]
var surb []byte
switch b[0] {
case flagsPadding:
case flagsSURB:
surb = b[constants.SphinxPlaintextHeaderLength:hdrLength]
default:
return nil, nil, fmt.Errorf("invalid message flags: 0x%02x", b[0])
}
if len(ct) != constants.UserForwardPayloadLength {
return nil, nil, fmt.Errorf("mis-sized user payload: %v", len(ct))
}
return ct, surb, nil
}
func NewPacketFromSURB(pkt *Packet, surb, payload []byte) (*Packet, error) {
if !pkt.IsToUser() {
return nil, fmt.Errorf("invalid commands to generate a SURB reply")
}
// Pad out payloads to the full packet size.
respPayload := make([]byte, constants.ForwardPayloadLength)
switch {
case len(payload) == 0:
case len(payload) > constants.ForwardPayloadLength:
return nil, fmt.Errorf("oversized response payload: %v", len(payload))
default:
copy(respPayload[:], payload)
}
// Build a response packet using a SURB.
//
// TODO/perf: This is a crypto operation that is paralleizable, and
// could be handled by the crypto worker(s), since those are allocated
// based on hardware acceleration considerations. However the forward
// packet processing doesn't constantly utilize the AES-NI units due
// to the non-AEZ components of a Sphinx Unwrap operation.
rawRespPkt, firstHop, err := sphinx.NewPacketFromSURB(surb, respPayload[:])
if err != nil {
return nil, err
}
// Build the command vector for the SURB-ACK
cmds := make([]commands.RoutingCommand, 0, 2)
nextHopCmd := new(commands.NextNodeHop)
copy(nextHopCmd.ID[:], firstHop[:])
cmds = append(cmds, nextHopCmd)
nodeDelayCmd := new(commands.NodeDelay)
nodeDelayCmd.Delay = pkt.NodeDelay.Delay
cmds = append(cmds, nodeDelayCmd)
// Assemble the response packet.
respPkt, _ := New(rawRespPkt)
_ = respPkt.Set(nil, cmds)
respPkt.RecvAt = pkt.RecvAt
respPkt.Delay = time.Duration(nodeDelayCmd.Delay) * time.Millisecond
respPkt.MustForward = true
// XXX: This should probably fudge the delay to account for processing
// time.
return respPkt, nil
}