forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.cc
More file actions
234 lines (206 loc) · 7.94 KB
/
Copy pathpacket.cc
File metadata and controls
234 lines (206 loc) · 7.94 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
222
223
224
225
226
227
228
229
230
231
232
233
234
#if HAVE_OPENSSL && HAVE_QUIC
#include "guard.h"
#ifndef OPENSSL_NO_QUIC
#include <crypto/crypto_util.h>
#include <ngtcp2/ngtcp2.h>
#include <ngtcp2/ngtcp2_crypto.h>
#include <node_sockaddr-inl.h>
#include <uv.h>
#include <cstring>
#include <string>
#include "cid.h"
#include "defs.h"
#include "endpoint.h"
#include "ncrypto.h"
#include "packet.h"
#include "tokens.h"
namespace node::quic {
namespace {
static constexpr size_t kRandlen = NGTCP2_MIN_STATELESS_RESET_RANDLEN * 5;
static constexpr size_t kMinStatelessResetLen = 41;
} // namespace
std::string PathDescriptor::ToString() const {
DebugIndentScope indent;
auto prefix = indent.Prefix();
std::string res = "{";
res += prefix + "version: " + std::to_string(version);
res += prefix + "dcid: " + dcid.ToString();
res += prefix + "scid: " + scid.ToString();
res += prefix + "local address: " + local_address.ToString();
res += prefix + "remote address: " + remote_address.ToString();
res += indent.Close();
return res;
}
// ============================================================================
// Packet
Packet::Packet(uint8_t* data,
size_t capacity,
Listener* listener,
const SocketAddress& destination)
: data_(data),
capacity_(capacity),
length_(capacity),
listener_(listener),
destination_(destination),
req_{} {}
std::string Packet::ToString() const {
std::string res = "Packet(";
#ifdef DEBUG
if (diagnostic_label_) {
res += diagnostic_label_;
res += ", ";
}
#endif
res += std::to_string(length_);
res += ")";
return res;
}
// ============================================================================
// Static factory methods
Packet::Ptr Packet::CreateRetryPacket(Endpoint& endpoint,
const PathDescriptor& path_descriptor,
const TokenSecret& token_secret) {
auto& random = CID::Factory::random();
CID cid = random.Generate();
RetryToken token(path_descriptor.version,
path_descriptor.remote_address,
cid,
path_descriptor.dcid,
token_secret);
if (!token) return Ptr();
const ngtcp2_vec& vec = token;
size_t pktlen =
vec.len + (2 * NGTCP2_MAX_CIDLEN) + path_descriptor.scid.length() + 8;
auto packet =
endpoint.CreatePacket(path_descriptor.remote_address, pktlen, "retry");
if (!packet) return packet;
ngtcp2_vec dest = *packet;
ssize_t nwrite = ngtcp2_crypto_write_retry(dest.base,
pktlen,
path_descriptor.version,
path_descriptor.scid,
cid,
path_descriptor.dcid,
vec.base,
vec.len);
if (nwrite <= 0) return Ptr();
packet->Truncate(static_cast<size_t>(nwrite));
return packet;
}
Packet::Ptr Packet::CreateConnectionClosePacket(
Endpoint& endpoint,
const SocketAddress& destination,
ngtcp2_conn* conn,
const QuicError& error) {
auto packet = endpoint.CreatePacket(
destination, kDefaultMaxPacketLength, "connection close");
if (!packet) return packet;
ngtcp2_vec vec = *packet;
ssize_t nwrite = ngtcp2_conn_write_connection_close(
conn, nullptr, nullptr, vec.base, vec.len, error, uv_hrtime());
if (nwrite < 0) return Ptr();
packet->Truncate(static_cast<size_t>(nwrite));
return packet;
}
Packet::Ptr Packet::CreateImmediateConnectionClosePacket(
Endpoint& endpoint,
const PathDescriptor& path_descriptor,
const QuicError& reason) {
auto packet = endpoint.CreatePacket(path_descriptor.remote_address,
kDefaultMaxPacketLength,
"immediate connection close (endpoint)");
if (!packet) return packet;
ngtcp2_vec vec = *packet;
// ngtcp2_crypto_write_connection_close expects dcid to be the
// client's SCID and scid to be the client's DCID (mirrored).
// PathDescriptor carries the incoming packet's CIDs as-is, so
// we swap here.
ssize_t nwrite = ngtcp2_crypto_write_connection_close(vec.base,
vec.len,
path_descriptor.version,
path_descriptor.scid,
path_descriptor.dcid,
reason.code(),
nullptr,
0);
if (nwrite <= 0) return Ptr();
packet->Truncate(static_cast<size_t>(nwrite));
return packet;
}
Packet::Ptr Packet::CreateStatelessResetPacket(
Endpoint& endpoint,
const PathDescriptor& path_descriptor,
const TokenSecret& token_secret,
size_t source_len) {
// Per the QUIC spec, a stateless reset token must be strictly smaller than
// the packet that triggered it.
size_t pktlen = source_len - 1;
if (pktlen < kMinStatelessResetLen) return Ptr();
StatelessResetToken token(token_secret, path_descriptor.dcid);
uint8_t random[kRandlen];
CHECK(ncrypto::CSPRNG(random, kRandlen));
auto packet = endpoint.CreatePacket(path_descriptor.remote_address,
kDefaultMaxPacketLength,
"stateless reset");
if (!packet) return packet;
ngtcp2_vec vec = *packet;
auto nwrite = ngtcp2_pkt_write_stateless_reset2(
vec.base, pktlen, token, random, kRandlen);
if (nwrite < static_cast<ssize_t>(kMinStatelessResetLen)) {
return Ptr();
}
packet->Truncate(static_cast<size_t>(nwrite));
return packet;
}
Packet::Ptr Packet::CreateVersionNegotiationPacket(
Endpoint& endpoint, const PathDescriptor& path_descriptor) {
const auto generateReservedVersion = [&] {
socklen_t addrlen = path_descriptor.remote_address.length();
uint32_t h = 0x811C9DC5u;
uint32_t ver = htonl(path_descriptor.version);
const uint8_t* p = path_descriptor.remote_address.raw();
const uint8_t* ep = p + addrlen;
for (; p != ep; ++p) {
h ^= *p;
h *= 0x01000193u;
}
p = reinterpret_cast<const uint8_t*>(&ver);
ep = p + sizeof(path_descriptor.version);
for (; p != ep; ++p) {
h ^= *p;
h *= 0x01000193u;
}
h &= 0xf0f0f0f0u;
h |= NGTCP2_RESERVED_VERSION_MASK;
return h;
};
uint32_t sv[3] = {
generateReservedVersion(), NGTCP2_PROTO_VER_MIN, NGTCP2_PROTO_VER_MAX};
size_t pktlen = path_descriptor.dcid.length() +
path_descriptor.scid.length() + (sizeof(sv)) + 7;
auto packet = endpoint.CreatePacket(path_descriptor.remote_address,
kDefaultMaxPacketLength,
"version negotiation");
if (!packet) return packet;
ngtcp2_vec vec = *packet;
// ngtcp2_pkt_write_version_negotiation expects dcid to be the
// client's SCID and scid to be the client's DCID (mirrored).
// PathDescriptor carries the incoming packet's CIDs as-is, so
// we swap here.
ssize_t nwrite =
ngtcp2_pkt_write_version_negotiation(vec.base,
pktlen,
0,
path_descriptor.scid,
path_descriptor.scid.length(),
path_descriptor.dcid,
path_descriptor.dcid.length(),
sv,
arraysize(sv));
if (nwrite <= 0) return Ptr();
packet->Truncate(static_cast<size_t>(nwrite));
return packet;
}
} // namespace node::quic
#endif // OPENSSL_NO_QUIC
#endif // HAVE_OPENSSL && HAVE_QUIC