-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrs_chunking.go
More file actions
221 lines (186 loc) · 7.5 KB
/
rs_chunking.go
File metadata and controls
221 lines (186 loc) · 7.5 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
package nebula
import (
"fmt"
"net/netip"
"github.com/klauspost/reedsolomon"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/udp"
)
const (
// DefaultChunkPayloadSize is the maximum payload per chunk, chosen to fit
// within the IPv6 minimum MTU of 1280 bytes after IP (40) + UDP (8) headers
// plus the Nebula header (16) and chunk header (8).
// 1280 - 40 - 8 - 16 - 8 = 1208, rounded down to 1200 for safety.
DefaultChunkPayloadSize = 1200
// DefaultDataShards is the number of data shards (k) for RS encoding.
DefaultDataShards = 5
// DefaultParityShards is the number of parity shards (m) for RS encoding.
DefaultParityShards = 3
// ChunkingThreshold is the message size above which RS chunking is applied.
// Messages at or below this size are sent as single packets (backward compatible).
ChunkingThreshold = DefaultChunkPayloadSize
)
// rsEncode splits a handshake message into RS-coded chunks. Each chunk includes
// the Nebula header (with HandshakeIXPSK0Chunked subtype) and a ChunkHeader.
//
// Parameters:
// - msg: the complete Noise handshake message (including the 16-byte Nebula header)
// - handshakeID: the InitiatorIndex for session disambiguation
// - noiseMsgNum: 0 for message 1 (initiator), 1 for message 2 (responder)
// - parityShards: number of parity shards (m)
// - chunkPayloadSize: max payload bytes per chunk
//
// The number of data shards is automatically calculated from the payload size
// and chunk payload size: k = ceil(payloadLen / chunkPayloadSize).
//
// Returns a slice of complete UDP packets (each containing Nebula header + chunk header + chunk data).
func rsEncode(msg []byte, handshakeID uint32, noiseMsgNum uint8, parityShards, chunkPayloadSize int) ([][]byte, error) {
if len(msg) <= header.Len {
return nil, fmt.Errorf("message too short to contain Nebula header")
}
// Extract the original Nebula header fields for re-encoding
var origHeader header.H
if err := origHeader.Parse(msg); err != nil {
return nil, fmt.Errorf("failed to parse Nebula header: %w", err)
}
// The payload after the Nebula header is what we RS-encode.
// Prepend a 4-byte length prefix so the decoder can strip RS padding.
rawPayload := msg[header.Len:]
payload := make([]byte, 4+len(rawPayload))
payload[0] = byte(len(rawPayload) >> 24)
payload[1] = byte(len(rawPayload) >> 16)
payload[2] = byte(len(rawPayload) >> 8)
payload[3] = byte(len(rawPayload))
copy(payload[4:], rawPayload)
// Auto-calculate data shards so each shard fits within chunkPayloadSize
dataShards := (len(payload) + chunkPayloadSize - 1) / chunkPayloadSize
if dataShards < 1 {
dataShards = 1
}
totalShards := dataShards + parityShards
if totalShards > 255 {
return nil, fmt.Errorf("too many total shards: %d (max 255)", totalShards)
}
enc, err := reedsolomon.New(dataShards, parityShards)
if err != nil {
return nil, fmt.Errorf("failed to create RS encoder: %w", err)
}
// Calculate shard size: divide payload into dataShards equal-sized pieces.
shardSize := (len(payload) + dataShards - 1) / dataShards
// Create data shards by splitting the payload
shards := make([][]byte, totalShards)
for i := 0; i < dataShards; i++ {
start := i * shardSize
end := start + shardSize
shard := make([]byte, shardSize)
if start < len(payload) {
if end > len(payload) {
end = len(payload)
}
copy(shard, payload[start:end])
// Remaining bytes are already zero (padding)
}
shards[i] = shard
}
// Allocate parity shards
for i := dataShards; i < totalShards; i++ {
shards[i] = make([]byte, shardSize)
}
// Generate parity
if err := enc.Encode(shards); err != nil {
return nil, fmt.Errorf("RS encode failed: %w", err)
}
// Build complete UDP packets: Nebula header + chunk header + shard data
packets := make([][]byte, totalShards)
for i := 0; i < totalShards; i++ {
pkt := make([]byte, header.Len+header.ChunkHeaderLen+len(shards[i]))
// Encode Nebula header with chunked subtype, preserving RemoteIndex and MessageCounter
header.Encode(pkt[:header.Len], header.Version, header.Handshake, header.HandshakeIXPSK0Chunked,
origHeader.RemoteIndex, origHeader.MessageCounter)
// Encode chunk header
header.EncodeChunkHeader(pkt[header.Len:header.Len+header.ChunkHeaderLen],
handshakeID, noiseMsgNum, uint8(i), uint8(totalShards), uint8(dataShards))
// Copy shard data
copy(pkt[header.Len+header.ChunkHeaderLen:], shards[i])
packets[i] = pkt
}
return packets, nil
}
// rsDecode reconstructs a handshake message from RS-coded chunks.
//
// Parameters:
// - shards: slice indexed by chunk_idx. nil entries are missing chunks.
// - dataShards: number of data shards (k)
// - totalShards: total number of shards (k + m)
//
// Returns the reconstructed payload (without Nebula header).
func rsDecode(shards [][]byte, dataShards, totalShards int) ([]byte, error) {
parityShards := totalShards - dataShards
enc, err := reedsolomon.New(dataShards, parityShards)
if err != nil {
return nil, fmt.Errorf("failed to create RS decoder: %w", err)
}
if err := enc.Reconstruct(shards); err != nil {
return nil, fmt.Errorf("RS reconstruction failed: %w", err)
}
// Verify reconstruction
ok, err := enc.Verify(shards)
if err != nil {
return nil, fmt.Errorf("RS verification failed: %w", err)
}
if !ok {
return nil, fmt.Errorf("RS verification: shards are not consistent")
}
// Concatenate data shards to get the length-prefixed payload
var prefixed []byte
for i := 0; i < dataShards; i++ {
prefixed = append(prefixed, shards[i]...)
}
// Extract original length from 4-byte prefix and strip RS padding
if len(prefixed) < 4 {
return nil, fmt.Errorf("reconstructed payload too short for length prefix")
}
origLen := int(prefixed[0])<<24 | int(prefixed[1])<<16 | int(prefixed[2])<<8 | int(prefixed[3])
prefixed = prefixed[4:]
if origLen > len(prefixed) {
return nil, fmt.Errorf("original length %d exceeds reconstructed data %d", origLen, len(prefixed))
}
return prefixed[:origLen], nil
}
// needsChunking returns true if the handshake message is too large for a single UDP packet.
func needsChunking(msg []byte) bool {
return len(msg) > header.Len+ChunkingThreshold
}
// sendHandshakeChunked RS-encodes and sends a handshake message as multiple chunks.
func sendHandshakeChunked(l *logrus.Logger, outside udp.Conn, msg []byte, handshakeID uint32, noiseMsgNum uint8, addr netip.AddrPort) error {
chunks, err := rsEncode(msg, handshakeID, noiseMsgNum, DefaultParityShards, DefaultChunkPayloadSize)
if err != nil {
return fmt.Errorf("RS encode failed: %w", err)
}
for i, chunk := range chunks {
if err := outside.WriteTo(chunk, addr); err != nil {
l.WithField("chunkIdx", i).WithField("addr", addr).WithError(err).
Error("Failed to send handshake chunk")
// Continue sending remaining chunks - RS coding means some loss is OK
}
}
return nil
}
// sendHandshakeChunkedVia RS-encodes and sends a handshake message as multiple chunks through a relay.
func sendHandshakeChunkedVia(f *Interface, via *HostInfo, relay *Relay, msg []byte, handshakeID uint32, noiseMsgNum uint8) error {
chunks, err := rsEncode(msg, handshakeID, noiseMsgNum, DefaultParityShards, DefaultChunkPayloadSize)
if err != nil {
return fmt.Errorf("RS encode failed: %w", err)
}
for i, chunk := range chunks {
nb := make([]byte, 12)
out := make([]byte, mtu)
f.SendVia(via, relay, chunk, nb, out, false)
if f.l.Level >= logrus.DebugLevel {
f.l.WithField("chunkIdx", i).WithField("relay", via.vpnAddrs[0]).
Debug("Sent handshake chunk via relay")
}
}
return nil
}