blob: 75524aa8702323546b529a52ed9b979d615e6991 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/third_party/quiche/src/quic/core/quic_dispatcher.h"
6
vasilvv872e7a32019-03-12 16:42:44 -07007#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05008#include <utility>
9
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "net/third_party/quiche/src/quic/core/chlo_extractor.h"
11#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
12#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
fayangccbab732019-05-13 10:11:25 -070013#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include "net/third_party/quiche/src/quic/core/quic_time_wait_list_manager.h"
15#include "net/third_party/quiche/src/quic/core/quic_types.h"
16#include "net/third_party/quiche/src/quic/core/quic_utils.h"
dschinazi76992e72019-07-17 20:40:40 -070017#include "net/third_party/quiche/src/quic/core/quic_versions.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
19#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
23#include "net/third_party/quiche/src/quic/platform/api/quic_stack_trace.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050024#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
dschinazi965ce092019-05-23 06:29:01 -070025#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050026
27namespace quic {
28
29typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket;
30typedef QuicBufferedPacketStore::BufferedPacketList BufferedPacketList;
31typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult;
32
33namespace {
34
35// An alarm that informs the QuicDispatcher to delete old sessions.
36class DeleteSessionsAlarm : public QuicAlarm::Delegate {
37 public:
38 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher)
39 : dispatcher_(dispatcher) {}
40 DeleteSessionsAlarm(const DeleteSessionsAlarm&) = delete;
41 DeleteSessionsAlarm& operator=(const DeleteSessionsAlarm&) = delete;
42
43 void OnAlarm() override { dispatcher_->DeleteSessions(); }
44
45 private:
46 // Not owned.
47 QuicDispatcher* dispatcher_;
48};
49
50// Collects packets serialized by a QuicPacketCreator in order
51// to be handed off to the time wait list manager.
52class PacketCollector : public QuicPacketCreator::DelegateInterface,
53 public QuicStreamFrameDataProducer {
54 public:
55 explicit PacketCollector(QuicBufferAllocator* allocator)
56 : send_buffer_(allocator) {}
57 ~PacketCollector() override = default;
58
59 // QuicPacketCreator::DelegateInterface methods:
60 void OnSerializedPacket(SerializedPacket* serialized_packet) override {
61 // Make a copy of the serialized packet to send later.
62 packets_.emplace_back(
63 new QuicEncryptedPacket(CopyBuffer(*serialized_packet),
64 serialized_packet->encrypted_length, true));
65 serialized_packet->encrypted_buffer = nullptr;
66 DeleteFrames(&(serialized_packet->retransmittable_frames));
67 serialized_packet->retransmittable_frames.clear();
68 }
69
70 char* GetPacketBuffer() override {
71 // Let QuicPacketCreator to serialize packets on stack buffer.
72 return nullptr;
73 }
74
dschinazi17d42422019-06-18 16:35:07 -070075 void OnUnrecoverableError(QuicErrorCode /*error*/,
76 const std::string& /*error_details*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050077
QUICHE teama6ef0a62019-03-07 20:34:33 -050078 // QuicStreamFrameDataProducer
dschinazi17d42422019-06-18 16:35:07 -070079 WriteStreamDataResult WriteStreamData(QuicStreamId /*id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -050080 QuicStreamOffset offset,
81 QuicByteCount data_length,
82 QuicDataWriter* writer) override {
83 if (send_buffer_.WriteStreamData(offset, data_length, writer)) {
84 return WRITE_SUCCESS;
85 }
86 return WRITE_FAILED;
87 }
dschinazi17d42422019-06-18 16:35:07 -070088 bool WriteCryptoData(EncryptionLevel /*level*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -050089 QuicStreamOffset offset,
90 QuicByteCount data_length,
91 QuicDataWriter* writer) override {
92 return send_buffer_.WriteStreamData(offset, data_length, writer);
93 }
94
95 std::vector<std::unique_ptr<QuicEncryptedPacket>>* packets() {
96 return &packets_;
97 }
98
99 private:
100 std::vector<std::unique_ptr<QuicEncryptedPacket>> packets_;
101 // This is only needed until the packets are encrypted. Once packets are
102 // encrypted, the stream data is no longer required.
103 QuicStreamSendBuffer send_buffer_;
104};
105
106// Helper for statelessly closing connections by generating the
107// correct termination packets and adding the connection to the time wait
108// list manager.
109class StatelessConnectionTerminator {
110 public:
dschinazi7b9278c2019-05-20 07:36:21 -0700111 StatelessConnectionTerminator(QuicConnectionId server_connection_id,
fayangf7c569c2019-05-07 11:56:51 -0700112 const ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 QuicConnectionHelperInterface* helper,
114 QuicTimeWaitListManager* time_wait_list_manager)
dschinazi7b9278c2019-05-20 07:36:21 -0700115 : server_connection_id_(server_connection_id),
fayangf7c569c2019-05-07 11:56:51 -0700116 framer_(ParsedQuicVersionVector{version},
117 /*unused*/ QuicTime::Zero(),
118 Perspective::IS_SERVER,
119 /*unused*/ kQuicDefaultConnectionIdLength),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120 collector_(helper->GetStreamSendBufferAllocator()),
dschinazi7b9278c2019-05-20 07:36:21 -0700121 creator_(server_connection_id, &framer_, &collector_),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500122 time_wait_list_manager_(time_wait_list_manager) {
fayangf7c569c2019-05-07 11:56:51 -0700123 framer_.set_data_producer(&collector_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 }
125
126 ~StatelessConnectionTerminator() {
127 // Clear framer's producer.
fayangf7c569c2019-05-07 11:56:51 -0700128 framer_.set_data_producer(nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 }
130
131 // Generates a packet containing a CONNECTION_CLOSE frame specifying
132 // |error_code| and |error_details| and add the connection to time wait.
133 void CloseConnection(QuicErrorCode error_code,
vasilvvc48c8712019-03-11 13:38:16 -0700134 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135 bool ietf_quic) {
fkastenholz591814c2019-09-06 12:11:46 -0700136 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(
137 framer_.transport_version(), error_code, error_details,
138 /*transport_close_frame_type=*/0);
fkastenholz72f509b2019-04-10 09:17:49 -0700139
QUICHE teama6ef0a62019-03-07 20:34:33 -0500140 if (!creator_.AddSavedFrame(QuicFrame(frame), NOT_RETRANSMISSION)) {
141 QUIC_BUG << "Unable to add frame to an empty packet";
142 delete frame;
143 return;
144 }
145 creator_.Flush();
146 DCHECK_EQ(1u, collector_.packets()->size());
147 time_wait_list_manager_->AddConnectionIdToTimeWait(
dschinazi7b9278c2019-05-20 07:36:21 -0700148 server_connection_id_, ietf_quic,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500149 QuicTimeWaitListManager::SEND_TERMINATION_PACKETS,
QUICHE team6987b4a2019-03-15 16:23:04 -0700150 quic::ENCRYPTION_INITIAL, collector_.packets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500151 }
152
QUICHE teama6ef0a62019-03-07 20:34:33 -0500153 private:
dschinazi7b9278c2019-05-20 07:36:21 -0700154 QuicConnectionId server_connection_id_;
fayangf7c569c2019-05-07 11:56:51 -0700155 QuicFramer framer_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156 // Set as the visitor of |creator_| to collect any generated packets.
157 PacketCollector collector_;
158 QuicPacketCreator creator_;
159 QuicTimeWaitListManager* time_wait_list_manager_;
160};
161
162// Class which extracts the ALPN from a CHLO packet.
163class ChloAlpnExtractor : public ChloExtractor::Delegate {
164 public:
dschinazi17d42422019-06-18 16:35:07 -0700165 void OnChlo(QuicTransportVersion /*version*/,
166 QuicConnectionId /*server_connection_id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500167 const CryptoHandshakeMessage& chlo) override {
168 QuicStringPiece alpn_value;
169 if (chlo.GetStringPiece(kALPN, &alpn_value)) {
vasilvvc48c8712019-03-11 13:38:16 -0700170 alpn_ = std::string(alpn_value);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171 }
172 }
173
vasilvvc48c8712019-03-11 13:38:16 -0700174 std::string&& ConsumeAlpn() { return std::move(alpn_); }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500175
176 private:
vasilvvc48c8712019-03-11 13:38:16 -0700177 std::string alpn_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500178};
179
QUICHE teama6ef0a62019-03-07 20:34:33 -0500180} // namespace
181
182QuicDispatcher::QuicDispatcher(
183 const QuicConfig* config,
184 const QuicCryptoServerConfig* crypto_config,
185 QuicVersionManager* version_manager,
186 std::unique_ptr<QuicConnectionHelperInterface> helper,
187 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper,
188 std::unique_ptr<QuicAlarmFactory> alarm_factory,
dschinazi8ff74822019-05-28 16:37:20 -0700189 uint8_t expected_server_connection_id_length)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500190 : config_(config),
191 crypto_config_(crypto_config),
192 compressed_certs_cache_(
193 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize),
194 helper_(std::move(helper)),
195 session_helper_(std::move(session_helper)),
196 alarm_factory_(std::move(alarm_factory)),
197 delete_sessions_alarm_(
198 alarm_factory_->CreateAlarm(new DeleteSessionsAlarm(this))),
199 buffered_packets_(this, helper_->GetClock(), alarm_factory_.get()),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500200 version_manager_(version_manager),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500201 last_error_(QUIC_NO_ERROR),
202 new_sessions_allowed_per_event_loop_(0u),
QUICHE team963d57e2019-03-21 10:58:47 -0700203 accept_new_connections_(true),
dschinazi7b9278c2019-05-20 07:36:21 -0700204 allow_short_initial_server_connection_ids_(false),
dschinazi8ff74822019-05-28 16:37:20 -0700205 expected_server_connection_id_length_(
206 expected_server_connection_id_length),
fayang91475c42019-06-19 08:04:26 -0700207 should_update_expected_server_connection_id_length_(false) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500208
209QuicDispatcher::~QuicDispatcher() {
210 session_map_.clear();
211 closed_session_list_.clear();
212}
213
214void QuicDispatcher::InitializeWithWriter(QuicPacketWriter* writer) {
215 DCHECK(writer_ == nullptr);
216 writer_.reset(writer);
217 time_wait_list_manager_.reset(CreateQuicTimeWaitListManager());
218}
219
220void QuicDispatcher::ProcessPacket(const QuicSocketAddress& self_address,
221 const QuicSocketAddress& peer_address,
222 const QuicReceivedPacket& packet) {
dschinazi965ce092019-05-23 06:29:01 -0700223 QUIC_DVLOG(2) << "Dispatcher received encrypted " << packet.length()
224 << " bytes:" << std::endl
225 << QuicTextUtils::HexDump(
226 QuicStringPiece(packet.data(), packet.length()));
fayang1ed1f762019-06-24 11:40:04 -0700227 ReceivedPacketInfo packet_info(self_address, peer_address, packet);
fayang1f123502019-05-14 08:05:16 -0700228 std::string detailed_error;
dschinazi48ac9192019-07-31 00:07:26 -0700229 QuicErrorCode error;
230 if (!GetQuicReloadableFlag(quic_use_parse_public_header)) {
231 error = QuicFramer::ProcessPacketDispatcher(
232 packet, expected_server_connection_id_length_, &packet_info.form,
233 &packet_info.version_flag, &packet_info.version_label,
234 &packet_info.destination_connection_id,
235 &packet_info.source_connection_id, &detailed_error);
236 } else {
237 QUIC_RELOADABLE_FLAG_COUNT(quic_use_parse_public_header);
238 bool retry_token_present;
239 QuicStringPiece retry_token;
240 error = QuicFramer::ParsePublicHeaderDispatcher(
241 packet, expected_server_connection_id_length_, &packet_info.form,
242 &packet_info.version_flag, &packet_info.use_length_prefix,
243 &packet_info.version_label, &packet_info.version,
244 &packet_info.destination_connection_id,
245 &packet_info.source_connection_id, &retry_token_present, &retry_token,
246 &detailed_error);
247 }
fayangccbab732019-05-13 10:11:25 -0700248 if (error != QUIC_NO_ERROR) {
249 // Packet has framing error.
250 SetLastError(error);
251 QUIC_DLOG(ERROR) << detailed_error;
252 return;
253 }
dschinazi243eabc2019-08-05 16:15:29 -0700254 if (!GetQuicReloadableFlag(quic_use_parse_public_header)) {
255 packet_info.version = ParseQuicVersionLabel(packet_info.version_label);
256 }
fayang1ed1f762019-06-24 11:40:04 -0700257 if (packet_info.destination_connection_id.length() !=
dschinazi8ff74822019-05-28 16:37:20 -0700258 expected_server_connection_id_length_ &&
259 !should_update_expected_server_connection_id_length_ &&
fayangccbab732019-05-13 10:11:25 -0700260 !QuicUtils::VariableLengthConnectionIdAllowedForVersion(
fayang1ed1f762019-06-24 11:40:04 -0700261 packet_info.version.transport_version)) {
fayangccbab732019-05-13 10:11:25 -0700262 SetLastError(QUIC_INVALID_PACKET_HEADER);
263 QUIC_DLOG(ERROR) << "Invalid Connection Id Length";
264 return;
265 }
dschinazib953d022019-08-01 18:05:58 -0700266
267 if (packet_info.version_flag && IsSupportedVersion(packet_info.version)) {
268 if (!QuicUtils::IsConnectionIdValidForVersion(
269 packet_info.destination_connection_id,
270 packet_info.version.transport_version)) {
271 SetLastError(QUIC_INVALID_PACKET_HEADER);
272 QUIC_DLOG(ERROR)
273 << "Invalid destination connection ID length for version";
274 return;
275 }
276 if (packet_info.version.SupportsClientConnectionIds() &&
277 !QuicUtils::IsConnectionIdValidForVersion(
278 packet_info.source_connection_id,
279 packet_info.version.transport_version)) {
280 SetLastError(QUIC_INVALID_PACKET_HEADER);
281 QUIC_DLOG(ERROR) << "Invalid source connection ID length for version";
282 return;
283 }
284 }
285
dschinazi8ff74822019-05-28 16:37:20 -0700286 if (should_update_expected_server_connection_id_length_) {
fayang1ed1f762019-06-24 11:40:04 -0700287 expected_server_connection_id_length_ =
288 packet_info.destination_connection_id.length();
fayangccbab732019-05-13 10:11:25 -0700289 }
fayang91475c42019-06-19 08:04:26 -0700290
fayang1ed1f762019-06-24 11:40:04 -0700291 if (MaybeDispatchPacket(packet_info)) {
fayang91475c42019-06-19 08:04:26 -0700292 // Packet has been dropped or successfully dispatched, stop processing.
fayangccbab732019-05-13 10:11:25 -0700293 return;
294 }
fayang1ed1f762019-06-24 11:40:04 -0700295 ProcessHeader(&packet_info);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500296}
297
dschinazi7b9278c2019-05-20 07:36:21 -0700298QuicConnectionId QuicDispatcher::MaybeReplaceServerConnectionId(
299 QuicConnectionId server_connection_id,
QUICHE teamc65d1d12019-03-19 20:58:04 -0700300 ParsedQuicVersion version) {
fayang91475c42019-06-19 08:04:26 -0700301 if (server_connection_id.length() == expected_server_connection_id_length_) {
dschinazi7b9278c2019-05-20 07:36:21 -0700302 return server_connection_id;
QUICHE teamc65d1d12019-03-19 20:58:04 -0700303 }
304 DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion(
305 version.transport_version));
dschinaziadc75072019-08-19 10:54:45 -0700306
QUICHE teamc65d1d12019-03-19 20:58:04 -0700307 QuicConnectionId new_connection_id =
wub662a3d62019-08-16 14:10:50 -0700308 GenerateNewServerConnectionId(version, server_connection_id);
fayang91475c42019-06-19 08:04:26 -0700309 DCHECK_EQ(expected_server_connection_id_length_, new_connection_id.length());
dschinazi28c1bf32019-08-19 11:54:46 -0700310
311 // Verify that GenerateNewServerConnectionId is deterministic.
312 DCHECK_EQ(new_connection_id,
313 GenerateNewServerConnectionId(version, server_connection_id));
314
dschinazi7b9278c2019-05-20 07:36:21 -0700315 QUIC_DLOG(INFO) << "Replacing incoming connection ID " << server_connection_id
QUICHE teamc65d1d12019-03-19 20:58:04 -0700316 << " with " << new_connection_id;
317 return new_connection_id;
318}
319
wub662a3d62019-08-16 14:10:50 -0700320QuicConnectionId QuicDispatcher::GenerateNewServerConnectionId(
321 ParsedQuicVersion /*version*/,
dschinaziadc75072019-08-19 10:54:45 -0700322 QuicConnectionId connection_id) const {
dschinaziadc75072019-08-19 10:54:45 -0700323 return QuicUtils::CreateReplacementConnectionId(connection_id);
wub662a3d62019-08-16 14:10:50 -0700324}
325
fayang91475c42019-06-19 08:04:26 -0700326bool QuicDispatcher::MaybeDispatchPacket(
fayang1ed1f762019-06-24 11:40:04 -0700327 const ReceivedPacketInfo& packet_info) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500328 // Port zero is only allowed for unidirectional UDP, so is disallowed by QUIC.
329 // Given that we can't even send a reply rejecting the packet, just drop the
330 // packet.
fayang1ed1f762019-06-24 11:40:04 -0700331 if (packet_info.peer_address.port() == 0) {
fayang91475c42019-06-19 08:04:26 -0700332 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500333 }
334
fayang1ed1f762019-06-24 11:40:04 -0700335 QuicConnectionId server_connection_id = packet_info.destination_connection_id;
QUICHE team8e2e4532019-03-14 14:37:56 -0700336
QUICHE team963d57e2019-03-21 10:58:47 -0700337 // The IETF spec requires the client to generate an initial server
338 // connection ID that is at least 64 bits long. After that initial
339 // connection ID, the dispatcher picks a new one of its expected length.
340 // Therefore we should never receive a connection ID that is smaller
341 // than 64 bits and smaller than what we expect.
dschinazi7b9278c2019-05-20 07:36:21 -0700342 if (server_connection_id.length() < kQuicMinimumInitialConnectionIdLength &&
fayang91475c42019-06-19 08:04:26 -0700343 server_connection_id.length() < expected_server_connection_id_length_ &&
dschinazi7b9278c2019-05-20 07:36:21 -0700344 !allow_short_initial_server_connection_ids_) {
fayang1ed1f762019-06-24 11:40:04 -0700345 DCHECK(packet_info.version_flag);
QUICHE team963d57e2019-03-21 10:58:47 -0700346 DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion(
fayang1ed1f762019-06-24 11:40:04 -0700347 packet_info.version.transport_version));
QUICHE team963d57e2019-03-21 10:58:47 -0700348 QUIC_DLOG(INFO) << "Packet with short destination connection ID "
dschinazi7b9278c2019-05-20 07:36:21 -0700349 << server_connection_id << " expected "
fayang91475c42019-06-19 08:04:26 -0700350 << static_cast<int>(expected_server_connection_id_length_);
dschinaziee07e472019-06-19 09:56:56 -0700351 if (!GetQuicReloadableFlag(quic_drop_invalid_small_initial_connection_id)) {
fayang17230ac2019-06-21 06:28:28 -0700352 // Add this connection_id to the time-wait state, to safely reject
353 // future packets.
354 QUIC_DLOG(INFO) << "Adding connection ID " << server_connection_id
355 << " to time-wait list.";
356 StatelesslyTerminateConnection(
fayang1ed1f762019-06-24 11:40:04 -0700357 server_connection_id, packet_info.form, packet_info.version_flag,
dschinazi48ac9192019-07-31 00:07:26 -0700358 packet_info.use_length_prefix, packet_info.version,
359 QUIC_HANDSHAKE_FAILED, "Reject connection",
fayang17230ac2019-06-21 06:28:28 -0700360 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
361
362 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(
363 server_connection_id));
364 time_wait_list_manager_->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700365 packet_info.self_address, packet_info.peer_address,
366 server_connection_id, packet_info.form, GetPerPacketContext());
fayang17230ac2019-06-21 06:28:28 -0700367
368 buffered_packets_.DiscardPackets(server_connection_id);
dschinaziee07e472019-06-19 09:56:56 -0700369 } else {
370 QUIC_RELOADABLE_FLAG_COUNT(quic_drop_invalid_small_initial_connection_id);
fayang17230ac2019-06-21 06:28:28 -0700371 // Drop the packet silently.
dschinaziee07e472019-06-19 09:56:56 -0700372 }
fayang91475c42019-06-19 08:04:26 -0700373 return true;
QUICHE team963d57e2019-03-21 10:58:47 -0700374 }
375
QUICHE teama6ef0a62019-03-07 20:34:33 -0500376 // Packets with connection IDs for active connections are processed
377 // immediately.
dschinazi7b9278c2019-05-20 07:36:21 -0700378 auto it = session_map_.find(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500379 if (it != session_map_.end()) {
dschinazi7b9278c2019-05-20 07:36:21 -0700380 DCHECK(!buffered_packets_.HasBufferedPackets(server_connection_id));
fayang1ed1f762019-06-24 11:40:04 -0700381 it->second->ProcessUdpPacket(packet_info.self_address,
382 packet_info.peer_address, packet_info.packet);
fayang91475c42019-06-19 08:04:26 -0700383 return true;
dschinazi5c030852019-07-11 15:45:53 -0700384 } else {
385 // We did not find the connection ID, check if we've replaced it.
386 QuicConnectionId replaced_connection_id = MaybeReplaceServerConnectionId(
387 server_connection_id, packet_info.version);
388 if (replaced_connection_id != server_connection_id) {
389 // Search for the replacement.
390 auto it2 = session_map_.find(replaced_connection_id);
391 if (it2 != session_map_.end()) {
392 DCHECK(!buffered_packets_.HasBufferedPackets(replaced_connection_id));
393 it2->second->ProcessUdpPacket(packet_info.self_address,
394 packet_info.peer_address,
395 packet_info.packet);
396 return true;
397 }
398 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500399 }
400
dschinazi7b9278c2019-05-20 07:36:21 -0700401 if (buffered_packets_.HasChloForConnection(server_connection_id)) {
fayang1ed1f762019-06-24 11:40:04 -0700402 BufferEarlyPacket(packet_info);
fayang91475c42019-06-19 08:04:26 -0700403 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500404 }
405
fayang1ed1f762019-06-24 11:40:04 -0700406 if (OnFailedToDispatchPacket(packet_info)) {
fayang91475c42019-06-19 08:04:26 -0700407 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500408 }
409
dschinazi7b9278c2019-05-20 07:36:21 -0700410 if (time_wait_list_manager_->IsConnectionIdInTimeWait(server_connection_id)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500411 // This connection ID is already in time-wait state.
412 time_wait_list_manager_->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700413 packet_info.self_address, packet_info.peer_address,
414 packet_info.destination_connection_id, packet_info.form,
415 GetPerPacketContext());
fayang91475c42019-06-19 08:04:26 -0700416 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500417 }
418
419 // The packet has an unknown connection ID.
420
421 // Unless the packet provides a version, assume that we can continue
422 // processing using our preferred version.
fayang1ed1f762019-06-24 11:40:04 -0700423 if (packet_info.version_flag) {
424 if (!IsSupportedVersion(packet_info.version)) {
425 if (ShouldCreateSessionForUnknownVersion(packet_info.version_label)) {
fayang91475c42019-06-19 08:04:26 -0700426 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500427 }
428 if (!crypto_config()->validate_chlo_size() ||
fayang1ed1f762019-06-24 11:40:04 -0700429 packet_info.packet.length() >= kMinPacketSizeForVersionNegotiation) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500430 // Since the version is not supported, send a version negotiation
431 // packet and stop processing the current packet.
fayang1ed1f762019-06-24 11:40:04 -0700432 QuicConnectionId client_connection_id =
433 packet_info.source_connection_id;
dschinazi30ab6db2019-08-13 14:43:32 -0700434 bool use_length_prefix = packet_info.use_length_prefix;
435 if (!GetQuicReloadableFlag(quic_use_length_prefix_from_packet_info)) {
436 use_length_prefix = packet_info.form != GOOGLE_QUIC_PACKET &&
437 !QuicVersionLabelUses4BitConnectionIdLength(
438 packet_info.version_label);
439 } else {
440 QUIC_RELOADABLE_FLAG_COUNT(quic_use_length_prefix_from_packet_info);
441 // TODO(dschinazi) remove the client-side workaround in
442 // QuicFramer::ParseServerVersionNegotiationProbeResponse
443 // when quic_use_length_prefix_from_packet_info is deprecated.
444 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500445 time_wait_list_manager()->SendVersionNegotiationPacket(
dschinazi346b7ce2019-06-05 01:38:18 -0700446 server_connection_id, client_connection_id,
dschinazi30ab6db2019-08-13 14:43:32 -0700447 packet_info.form != GOOGLE_QUIC_PACKET, use_length_prefix,
dschinazi48ac9192019-07-31 00:07:26 -0700448 GetSupportedVersions(), packet_info.self_address,
449 packet_info.peer_address, GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500450 }
fayang91475c42019-06-19 08:04:26 -0700451 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500452 }
fayangccbab732019-05-13 10:11:25 -0700453 }
nharper55fa6132019-05-07 19:37:21 -0700454
nharper55fa6132019-05-07 19:37:21 -0700455 return false;
456}
457
fayang1ed1f762019-06-24 11:40:04 -0700458void QuicDispatcher::ProcessHeader(ReceivedPacketInfo* packet_info) {
459 QuicConnectionId server_connection_id =
460 packet_info->destination_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500461 // Packet's connection ID is unknown. Apply the validity checks.
wub2a5670f2019-06-13 13:39:16 -0700462 // TODO(wub): Determine the fate completely in ValidityChecks, then call
463 // ProcessUnauthenticatedHeaderFate in one place.
fayang1ed1f762019-06-24 11:40:04 -0700464 QuicPacketFate fate = ValidityChecks(*packet_info);
fayang17230ac2019-06-21 06:28:28 -0700465 ChloAlpnExtractor alpn_extractor;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500466 switch (fate) {
dschinazi76992e72019-07-17 20:40:40 -0700467 case kFateProcess: {
fayang1ed1f762019-06-24 11:40:04 -0700468 if (packet_info->version.handshake_protocol == PROTOCOL_TLS1_3) {
fayang17230ac2019-06-21 06:28:28 -0700469 // TODO(nharper): Support buffering non-ClientHello packets when using
470 // TLS.
fayang1ed1f762019-06-24 11:40:04 -0700471 ProcessChlo(/*alpn=*/"", packet_info);
fayang17230ac2019-06-21 06:28:28 -0700472 break;
473 }
dschinazi76992e72019-07-17 20:40:40 -0700474 ParsedQuicVersionVector chlo_extractor_versions;
475 if (!GetQuicRestartFlag(
476 quic_dispatcher_hands_chlo_extractor_one_version)) {
477 chlo_extractor_versions = GetSupportedVersions();
478 } else {
479 QUIC_RESTART_FLAG_COUNT(
480 quic_dispatcher_hands_chlo_extractor_one_version);
481 chlo_extractor_versions = {packet_info->version};
482 // TODO(dschinazi) once we deprecate
483 // quic_dispatcher_hands_chlo_extractor_one_version, we should change
484 // ChloExtractor::Extract to only take one version.
485 }
fayang17230ac2019-06-21 06:28:28 -0700486 if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) &&
dschinazi76992e72019-07-17 20:40:40 -0700487 !ChloExtractor::Extract(packet_info->packet, chlo_extractor_versions,
fayang17230ac2019-06-21 06:28:28 -0700488 config_->create_session_tag_indicators(),
489 &alpn_extractor,
490 server_connection_id.length())) {
491 // Buffer non-CHLO packets.
fayang1ed1f762019-06-24 11:40:04 -0700492 BufferEarlyPacket(*packet_info);
fayang17230ac2019-06-21 06:28:28 -0700493 break;
494 }
fayang1ed1f762019-06-24 11:40:04 -0700495 ProcessChlo(alpn_extractor.ConsumeAlpn(), packet_info);
dschinazi76992e72019-07-17 20:40:40 -0700496 } break;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500497 case kFateTimeWait:
wub0a4b9c52019-05-28 13:18:58 -0700498 // Add this connection_id to the time-wait state, to safely reject
499 // future packets.
500 QUIC_DLOG(INFO) << "Adding connection ID " << server_connection_id
501 << " to time-wait list.";
502 QUIC_CODE_COUNT(quic_reject_fate_time_wait);
503 StatelesslyTerminateConnection(
fayang1ed1f762019-06-24 11:40:04 -0700504 server_connection_id, packet_info->form, packet_info->version_flag,
dschinazi48ac9192019-07-31 00:07:26 -0700505 packet_info->use_length_prefix, packet_info->version,
506 QUIC_HANDSHAKE_FAILED, "Reject connection",
wub0a4b9c52019-05-28 13:18:58 -0700507 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
508
dschinazi7b9278c2019-05-20 07:36:21 -0700509 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(
510 server_connection_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500511 time_wait_list_manager_->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700512 packet_info->self_address, packet_info->peer_address,
513 server_connection_id, packet_info->form, GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500514
dschinazi7b9278c2019-05-20 07:36:21 -0700515 buffered_packets_.DiscardPackets(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500516 break;
fayangd057e662019-07-10 13:29:41 -0700517 case kFateDrop:
518 break;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500519 }
520}
521
522QuicDispatcher::QuicPacketFate QuicDispatcher::ValidityChecks(
fayang1ed1f762019-06-24 11:40:04 -0700523 const ReceivedPacketInfo& packet_info) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500524 // To have all the checks work properly without tears, insert any new check
525 // into the framework of this method in the section for checks that return the
526 // check's fate value. The sections for checks must be ordered with the
527 // highest priority fate first.
528
QUICHE teama6ef0a62019-03-07 20:34:33 -0500529 // All packets within a connection sent by a client before receiving a
530 // response from the server are required to have the version negotiation flag
531 // set. Since this may be a client continuing a connection we lost track of
532 // via server restart, send a rejection to fast-fail the connection.
fayang1ed1f762019-06-24 11:40:04 -0700533 if (!packet_info.version_flag) {
dschinazi5b236be2019-08-19 14:55:22 -0700534 if (GetQuicReloadableFlag(quic_reply_to_old_android_conformance_test)) {
535 QUIC_RELOADABLE_FLAG_COUNT(quic_reply_to_old_android_conformance_test);
536 // The Android network conformance test contains a UDP test that sends a
537 // 12-byte packet with the following format:
538 // - 0x0c (public flags: 8-byte connection ID, 1-byte packet number)
539 // - randomized 8-byte connection ID
540 // - 0x01 (1-byte packet number)
541 // - 0x00 (private flags)
542 // - 0x07 (PING frame).
543 // That packet is invalid and we would normally drop it but in order to
544 // unblock this conformance testing we have the following workaround that
545 // will be removed once the fixed test is deployed.
546 // TODO(b/139691956) Remove this workaround once fixed test is deployed.
547 if (packet_info.packet.length() == 12 &&
548 packet_info.packet.data()[0] == 0x0c &&
549 packet_info.packet.data()[9] == 0x01 &&
550 packet_info.packet.data()[10] == 0x00 &&
551 packet_info.packet.data()[11] == 0x07) {
552 QUIC_DLOG(INFO) << "Received Android UDP network conformance test "
553 "packet with connection ID "
554 << packet_info.destination_connection_id;
555 // Respond with a public reset that the test will know how to parse
556 // then return kFateDrop to stop processing of this packet.
557 time_wait_list_manager()->SendPublicReset(
558 packet_info.self_address, packet_info.peer_address,
559 packet_info.destination_connection_id,
560 /*ietf_quic=*/false, GetPerPacketContext());
561 return kFateDrop;
562 }
563 }
564
QUICHE teama6ef0a62019-03-07 20:34:33 -0500565 QUIC_DLOG(INFO)
566 << "Packet without version arrived for unknown connection ID "
fayang1ed1f762019-06-24 11:40:04 -0700567 << packet_info.destination_connection_id;
fayangd057e662019-07-10 13:29:41 -0700568 if (GetQuicReloadableFlag(quic_reject_unprocessable_packets_statelessly)) {
569 QUIC_RELOADABLE_FLAG_COUNT(quic_reject_unprocessable_packets_statelessly);
570 MaybeResetPacketsWithNoVersion(packet_info);
571 return kFateDrop;
572 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500573 return kFateTimeWait;
574 }
575
fayang91475c42019-06-19 08:04:26 -0700576 // Let the connection parse and validate packet number.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500577 return kFateProcess;
578}
579
580void QuicDispatcher::CleanUpSession(SessionMap::iterator it,
581 QuicConnection* connection,
dschinazi17d42422019-06-18 16:35:07 -0700582 ConnectionCloseSource /*source*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500583 write_blocked_list_.erase(connection);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500584 QuicTimeWaitListManager::TimeWaitAction action =
585 QuicTimeWaitListManager::SEND_STATELESS_RESET;
586 if (connection->termination_packets() != nullptr &&
587 !connection->termination_packets()->empty()) {
588 action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS;
fayang51c23732019-06-24 06:59:55 -0700589 } else {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500590 if (!connection->IsHandshakeConfirmed()) {
fayangd4291e42019-05-30 10:31:21 -0700591 if (!VersionHasIetfInvariantHeader(connection->transport_version())) {
fayang1de67892019-04-19 05:59:45 -0700592 QUIC_CODE_COUNT(gquic_add_to_time_wait_list_with_handshake_failed);
593 } else {
594 QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_handshake_failed);
595 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500596 action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS;
597 // This serializes a connection close termination packet with error code
598 // QUIC_HANDSHAKE_FAILED and adds the connection to the time wait list.
599 StatelesslyTerminateConnection(
fayang1de67892019-04-19 05:59:45 -0700600 connection->connection_id(),
fayangd4291e42019-05-30 10:31:21 -0700601 VersionHasIetfInvariantHeader(connection->transport_version())
fayang1de67892019-04-19 05:59:45 -0700602 ? IETF_QUIC_LONG_HEADER_PACKET
603 : GOOGLE_QUIC_PACKET,
dschinazi48ac9192019-07-31 00:07:26 -0700604 /*version_flag=*/true,
605 connection->version().HasLengthPrefixedConnectionIds(),
606 connection->version(), QUIC_HANDSHAKE_FAILED,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500607 "Connection is closed by server before handshake confirmed",
608 // Although it is our intention to send termination packets, the
609 // |action| argument is not used by this call to
610 // StatelesslyTerminateConnection().
611 action);
612 session_map_.erase(it);
613 return;
614 }
615 QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_stateless_reset);
616 }
617 time_wait_list_manager_->AddConnectionIdToTimeWait(
fayangd4291e42019-05-30 10:31:21 -0700618 it->first, VersionHasIetfInvariantHeader(connection->transport_version()),
619 action, connection->encryption_level(),
620 connection->termination_packets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500621 session_map_.erase(it);
622}
623
624void QuicDispatcher::StopAcceptingNewConnections() {
625 accept_new_connections_ = false;
626}
627
628std::unique_ptr<QuicPerPacketContext> QuicDispatcher::GetPerPacketContext()
629 const {
630 return nullptr;
631}
632
633void QuicDispatcher::DeleteSessions() {
QUICHE teamaa1d6a82019-03-13 09:14:13 -0700634 if (!write_blocked_list_.empty()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500635 for (const std::unique_ptr<QuicSession>& session : closed_session_list_) {
636 if (write_blocked_list_.erase(session->connection()) != 0) {
637 QUIC_BUG << "QuicConnection was in WriteBlockedList before destruction";
638 }
639 }
640 }
641 closed_session_list_.clear();
642}
643
644void QuicDispatcher::OnCanWrite() {
645 // The socket is now writable.
646 writer_->SetWritable();
647
648 // Move every blocked writer in |write_blocked_list_| to a temporary list.
649 const size_t num_blocked_writers_before = write_blocked_list_.size();
650 WriteBlockedList temp_list;
651 temp_list.swap(write_blocked_list_);
652 DCHECK(write_blocked_list_.empty());
653
654 // Give each blocked writer a chance to write what they indended to write.
655 // If they are blocked again, they will call |OnWriteBlocked| to add
656 // themselves back into |write_blocked_list_|.
657 while (!temp_list.empty()) {
658 QuicBlockedWriterInterface* blocked_writer = temp_list.begin()->first;
659 temp_list.erase(temp_list.begin());
660 blocked_writer->OnBlockedWriterCanWrite();
661 }
662 const size_t num_blocked_writers_after = write_blocked_list_.size();
663 if (num_blocked_writers_after != 0) {
664 if (num_blocked_writers_before == num_blocked_writers_after) {
665 QUIC_CODE_COUNT(quic_zero_progress_on_can_write);
666 } else {
667 QUIC_CODE_COUNT(quic_blocked_again_on_can_write);
668 }
669 }
670}
671
672bool QuicDispatcher::HasPendingWrites() const {
673 return !write_blocked_list_.empty();
674}
675
676void QuicDispatcher::Shutdown() {
677 while (!session_map_.empty()) {
678 QuicSession* session = session_map_.begin()->second.get();
679 session->connection()->CloseConnection(
680 QUIC_PEER_GOING_AWAY, "Server shutdown imminent",
681 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET);
682 // Validate that the session removes itself from the session map on close.
683 DCHECK(session_map_.empty() ||
684 session_map_.begin()->second.get() != session);
685 }
686 DeleteSessions();
687}
688
dschinazi7b9278c2019-05-20 07:36:21 -0700689void QuicDispatcher::OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500690 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700691 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500692 ConnectionCloseSource source) {
dschinazi7b9278c2019-05-20 07:36:21 -0700693 auto it = session_map_.find(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500694 if (it == session_map_.end()) {
dschinazi7b9278c2019-05-20 07:36:21 -0700695 QUIC_BUG << "ConnectionId " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500696 << " does not exist in the session map. Error: "
697 << QuicErrorCodeToString(error);
698 QUIC_BUG << QuicStackTrace();
699 return;
700 }
701
702 QUIC_DLOG_IF(INFO, error != QUIC_NO_ERROR)
dschinazi7b9278c2019-05-20 07:36:21 -0700703 << "Closing connection (" << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500704 << ") due to error: " << QuicErrorCodeToString(error)
705 << ", with details: " << error_details;
706
707 QuicConnection* connection = it->second->connection();
708 if (ShouldDestroySessionAsynchronously()) {
709 // Set up alarm to fire immediately to bring destruction of this session
710 // out of current call stack.
711 if (closed_session_list_.empty()) {
712 delete_sessions_alarm_->Update(helper()->GetClock()->ApproximateNow(),
713 QuicTime::Delta::Zero());
714 }
715 closed_session_list_.push_back(std::move(it->second));
716 }
wub5f64ec42019-06-06 07:31:19 -0700717 CleanUpSession(it, connection, source);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500718}
719
720void QuicDispatcher::OnWriteBlocked(
721 QuicBlockedWriterInterface* blocked_writer) {
722 if (!blocked_writer->IsWriterBlocked()) {
723 // It is a programming error if this ever happens. When we are sure it is
724 // not happening, replace it with a DCHECK.
725 QUIC_BUG
726 << "Tried to add writer into blocked list when it shouldn't be added";
727 // Return without adding the connection to the blocked list, to avoid
728 // infinite loops in OnCanWrite.
729 return;
730 }
731
732 write_blocked_list_.insert(std::make_pair(blocked_writer, true));
733}
734
dschinazi17d42422019-06-18 16:35:07 -0700735void QuicDispatcher::OnRstStreamReceived(const QuicRstStreamFrame& /*frame*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500736
dschinazi17d42422019-06-18 16:35:07 -0700737void QuicDispatcher::OnStopSendingReceived(
738 const QuicStopSendingFrame& /*frame*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500739
740void QuicDispatcher::OnConnectionAddedToTimeWaitList(
dschinazi7b9278c2019-05-20 07:36:21 -0700741 QuicConnectionId server_connection_id) {
742 QUIC_DLOG(INFO) << "Connection " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500743 << " added to time wait list.";
744}
745
746void QuicDispatcher::StatelesslyTerminateConnection(
dschinazi7b9278c2019-05-20 07:36:21 -0700747 QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500748 PacketHeaderFormat format,
fayang1de67892019-04-19 05:59:45 -0700749 bool version_flag,
dschinazi48ac9192019-07-31 00:07:26 -0700750 bool use_length_prefix,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500751 ParsedQuicVersion version,
752 QuicErrorCode error_code,
vasilvvc48c8712019-03-11 13:38:16 -0700753 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500754 QuicTimeWaitListManager::TimeWaitAction action) {
fayang51c23732019-06-24 06:59:55 -0700755 if (format != IETF_QUIC_LONG_HEADER_PACKET && !version_flag) {
dschinazi7b9278c2019-05-20 07:36:21 -0700756 QUIC_DVLOG(1) << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500757 << " based on a non-ietf-long packet, action:" << action
758 << ", error_code:" << error_code
759 << ", error_details:" << error_details;
760 time_wait_list_manager_->AddConnectionIdToTimeWait(
dschinazi7b9278c2019-05-20 07:36:21 -0700761 server_connection_id, format != GOOGLE_QUIC_PACKET, action,
762 ENCRYPTION_INITIAL, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500763 return;
764 }
765
766 // If the version is known and supported by framer, send a connection close.
fayangccbab732019-05-13 10:11:25 -0700767 if (IsSupportedVersion(version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500768 QUIC_DVLOG(1)
dschinazi7b9278c2019-05-20 07:36:21 -0700769 << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500770 << " based on an ietf-long packet, which has a supported version:"
771 << version << ", error_code:" << error_code
772 << ", error_details:" << error_details;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500773
dschinazi7b9278c2019-05-20 07:36:21 -0700774 StatelessConnectionTerminator terminator(server_connection_id, version,
775 helper_.get(),
776 time_wait_list_manager_.get());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500777 // This also adds the connection to time wait list.
fayang1de67892019-04-19 05:59:45 -0700778 terminator.CloseConnection(error_code, error_details,
779 format != GOOGLE_QUIC_PACKET);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500780 return;
781 }
782
783 QUIC_DVLOG(1)
dschinazi7b9278c2019-05-20 07:36:21 -0700784 << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500785 << " based on an ietf-long packet, which has an unsupported version:"
786 << version << ", error_code:" << error_code
787 << ", error_details:" << error_details;
788 // Version is unknown or unsupported by framer, send a version negotiation
789 // with an empty version list, which can be understood by the client.
790 std::vector<std::unique_ptr<QuicEncryptedPacket>> termination_packets;
791 termination_packets.push_back(QuicFramer::BuildVersionNegotiationPacket(
dschinazib417d602019-05-29 13:08:45 -0700792 server_connection_id, EmptyQuicConnectionId(),
dschinazi48ac9192019-07-31 00:07:26 -0700793 /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, use_length_prefix,
dschinazi1ac22cc2019-06-25 11:47:50 -0700794 /*versions=*/{}));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500795 time_wait_list_manager()->AddConnectionIdToTimeWait(
dschinazi7b9278c2019-05-20 07:36:21 -0700796 server_connection_id, /*ietf_quic=*/format != GOOGLE_QUIC_PACKET,
QUICHE team6987b4a2019-03-15 16:23:04 -0700797 QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500798 &termination_packets);
799}
800
QUICHE teama6ef0a62019-03-07 20:34:33 -0500801bool QuicDispatcher::ShouldCreateSessionForUnknownVersion(
802 QuicVersionLabel /*version_label*/) {
803 return false;
804}
805
QUICHE teama6ef0a62019-03-07 20:34:33 -0500806void QuicDispatcher::OnExpiredPackets(
dschinazi7b9278c2019-05-20 07:36:21 -0700807 QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500808 BufferedPacketList early_arrived_packets) {
809 QUIC_CODE_COUNT(quic_reject_buffered_packets_expired);
810 StatelesslyTerminateConnection(
dschinazi7b9278c2019-05-20 07:36:21 -0700811 server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500812 early_arrived_packets.ietf_quic ? IETF_QUIC_LONG_HEADER_PACKET
813 : GOOGLE_QUIC_PACKET,
dschinazi48ac9192019-07-31 00:07:26 -0700814 /*version_flag=*/true,
815 early_arrived_packets.version.HasLengthPrefixedConnectionIds(),
816 early_arrived_packets.version, QUIC_HANDSHAKE_FAILED,
817 "Packets buffered for too long",
QUICHE teama6ef0a62019-03-07 20:34:33 -0500818 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
819}
820
821void QuicDispatcher::ProcessBufferedChlos(size_t max_connections_to_create) {
822 // Reset the counter before starting creating connections.
823 new_sessions_allowed_per_event_loop_ = max_connections_to_create;
824 for (; new_sessions_allowed_per_event_loop_ > 0;
825 --new_sessions_allowed_per_event_loop_) {
dschinazi7b9278c2019-05-20 07:36:21 -0700826 QuicConnectionId server_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500827 BufferedPacketList packet_list =
dschinazi7b9278c2019-05-20 07:36:21 -0700828 buffered_packets_.DeliverPacketsForNextConnection(
829 &server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500830 const std::list<BufferedPacket>& packets = packet_list.buffered_packets;
831 if (packets.empty()) {
832 return;
833 }
dschinazi7b9278c2019-05-20 07:36:21 -0700834 QuicConnectionId original_connection_id = server_connection_id;
835 server_connection_id = MaybeReplaceServerConnectionId(server_connection_id,
836 packet_list.version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500837 QuicSession* session =
dschinazi7b9278c2019-05-20 07:36:21 -0700838 CreateQuicSession(server_connection_id, packets.front().peer_address,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500839 packet_list.alpn, packet_list.version);
dschinazi7b9278c2019-05-20 07:36:21 -0700840 if (original_connection_id != server_connection_id) {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700841 session->connection()->AddIncomingConnectionId(original_connection_id);
dschinazi5c030852019-07-11 15:45:53 -0700842 session->connection()->InstallInitialCrypters(original_connection_id);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700843 }
dschinazi7b9278c2019-05-20 07:36:21 -0700844 QUIC_DLOG(INFO) << "Created new session for " << server_connection_id;
dschinazi5c030852019-07-11 15:45:53 -0700845
846 DCHECK(session_map_.find(server_connection_id) == session_map_.end())
847 << "Tried to add session map existing entry " << server_connection_id;
848
dschinazi7b9278c2019-05-20 07:36:21 -0700849 session_map_.insert(
850 std::make_pair(server_connection_id, QuicWrapUnique(session)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500851 DeliverPacketsToSession(packets, session);
852 }
853}
854
855bool QuicDispatcher::HasChlosBuffered() const {
856 return buffered_packets_.HasChlosBuffered();
857}
858
859bool QuicDispatcher::ShouldCreateOrBufferPacketForConnection(
fayang1ed1f762019-06-24 11:40:04 -0700860 const ReceivedPacketInfo& packet_info) {
dschinazi7b9278c2019-05-20 07:36:21 -0700861 QUIC_VLOG(1) << "Received packet from new connection "
fayang1ed1f762019-06-24 11:40:04 -0700862 << packet_info.destination_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500863 return true;
864}
865
866// Return true if there is any packet buffered in the store.
dschinazi7b9278c2019-05-20 07:36:21 -0700867bool QuicDispatcher::HasBufferedPackets(QuicConnectionId server_connection_id) {
868 return buffered_packets_.HasBufferedPackets(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500869}
870
dschinazi7b9278c2019-05-20 07:36:21 -0700871void QuicDispatcher::OnBufferPacketFailure(
872 EnqueuePacketResult result,
873 QuicConnectionId server_connection_id) {
874 QUIC_DLOG(INFO) << "Fail to buffer packet on connection "
875 << server_connection_id << " because of " << result;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500876}
877
QUICHE teama6ef0a62019-03-07 20:34:33 -0500878QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() {
879 return new QuicTimeWaitListManager(writer_.get(), this, helper_->GetClock(),
880 alarm_factory_.get());
881}
882
fayang1ed1f762019-06-24 11:40:04 -0700883void QuicDispatcher::BufferEarlyPacket(const ReceivedPacketInfo& packet_info) {
884 bool is_new_connection = !buffered_packets_.HasBufferedPackets(
885 packet_info.destination_connection_id);
886 if (is_new_connection &&
887 !ShouldCreateOrBufferPacketForConnection(packet_info)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500888 return;
889 }
890
891 EnqueuePacketResult rs = buffered_packets_.EnqueuePacket(
fayang1ed1f762019-06-24 11:40:04 -0700892 packet_info.destination_connection_id,
893 packet_info.form != GOOGLE_QUIC_PACKET, packet_info.packet,
894 packet_info.self_address, packet_info.peer_address, /*is_chlo=*/false,
895 /*alpn=*/"", packet_info.version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500896 if (rs != EnqueuePacketResult::SUCCESS) {
fayang1ed1f762019-06-24 11:40:04 -0700897 OnBufferPacketFailure(rs, packet_info.destination_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500898 }
899}
900
fayang1ed1f762019-06-24 11:40:04 -0700901void QuicDispatcher::ProcessChlo(const std::string& alpn,
902 ReceivedPacketInfo* packet_info) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500903 if (!accept_new_connections_) {
904 // Don't any create new connection.
905 QUIC_CODE_COUNT(quic_reject_stop_accepting_new_connections);
906 StatelesslyTerminateConnection(
fayang1ed1f762019-06-24 11:40:04 -0700907 packet_info->destination_connection_id, packet_info->form,
dschinazi48ac9192019-07-31 00:07:26 -0700908 /*version_flag=*/true, packet_info->use_length_prefix,
909 packet_info->version, QUIC_HANDSHAKE_FAILED,
fayang1ed1f762019-06-24 11:40:04 -0700910 "Stop accepting new connections",
QUICHE teama6ef0a62019-03-07 20:34:33 -0500911 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
912 // Time wait list will reject the packet correspondingly.
913 time_wait_list_manager()->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700914 packet_info->self_address, packet_info->peer_address,
915 packet_info->destination_connection_id, packet_info->form,
916 GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500917 return;
918 }
fayang1ed1f762019-06-24 11:40:04 -0700919 if (!buffered_packets_.HasBufferedPackets(
920 packet_info->destination_connection_id) &&
921 !ShouldCreateOrBufferPacketForConnection(*packet_info)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500922 return;
923 }
danzh88e3e052019-06-13 11:47:18 -0700924 if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500925 new_sessions_allowed_per_event_loop_ <= 0) {
926 // Can't create new session any more. Wait till next event loop.
fayang1ed1f762019-06-24 11:40:04 -0700927 QUIC_BUG_IF(buffered_packets_.HasChloForConnection(
928 packet_info->destination_connection_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500929 EnqueuePacketResult rs = buffered_packets_.EnqueuePacket(
fayang1ed1f762019-06-24 11:40:04 -0700930 packet_info->destination_connection_id,
931 packet_info->form != GOOGLE_QUIC_PACKET, packet_info->packet,
932 packet_info->self_address, packet_info->peer_address,
933 /*is_chlo=*/true, alpn, packet_info->version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500934 if (rs != EnqueuePacketResult::SUCCESS) {
fayang1ed1f762019-06-24 11:40:04 -0700935 OnBufferPacketFailure(rs, packet_info->destination_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500936 }
937 return;
938 }
QUICHE teamc65d1d12019-03-19 20:58:04 -0700939
fayang1ed1f762019-06-24 11:40:04 -0700940 QuicConnectionId original_connection_id =
941 packet_info->destination_connection_id;
942 packet_info->destination_connection_id = MaybeReplaceServerConnectionId(
943 original_connection_id, packet_info->version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500944 // Creates a new session and process all buffered packets for this connection.
dschinazi7b9278c2019-05-20 07:36:21 -0700945 QuicSession* session =
fayang1ed1f762019-06-24 11:40:04 -0700946 CreateQuicSession(packet_info->destination_connection_id,
947 packet_info->peer_address, alpn, packet_info->version);
948 if (original_connection_id != packet_info->destination_connection_id) {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700949 session->connection()->AddIncomingConnectionId(original_connection_id);
dschinazi5c030852019-07-11 15:45:53 -0700950 session->connection()->InstallInitialCrypters(original_connection_id);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700951 }
dschinazi7b9278c2019-05-20 07:36:21 -0700952 QUIC_DLOG(INFO) << "Created new session for "
fayang1ed1f762019-06-24 11:40:04 -0700953 << packet_info->destination_connection_id;
dschinazi5c030852019-07-11 15:45:53 -0700954
955 DCHECK(session_map_.find(packet_info->destination_connection_id) ==
956 session_map_.end())
957 << "Tried to add session map existing entry "
958 << packet_info->destination_connection_id;
959
fayang1ed1f762019-06-24 11:40:04 -0700960 session_map_.insert(std::make_pair(packet_info->destination_connection_id,
961 QuicWrapUnique(session)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500962 std::list<BufferedPacket> packets =
fayang1ed1f762019-06-24 11:40:04 -0700963 buffered_packets_.DeliverPackets(packet_info->destination_connection_id)
dschinazi7b9278c2019-05-20 07:36:21 -0700964 .buffered_packets;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500965 // Process CHLO at first.
fayang1ed1f762019-06-24 11:40:04 -0700966 session->ProcessUdpPacket(packet_info->self_address,
967 packet_info->peer_address, packet_info->packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500968 // Deliver queued-up packets in the same order as they arrived.
969 // Do this even when flag is off because there might be still some packets
970 // buffered in the store before flag is turned off.
971 DeliverPacketsToSession(packets, session);
972 --new_sessions_allowed_per_event_loop_;
973}
974
QUICHE teama6ef0a62019-03-07 20:34:33 -0500975bool QuicDispatcher::ShouldDestroySessionAsynchronously() {
976 return true;
977}
978
979void QuicDispatcher::SetLastError(QuicErrorCode error) {
980 last_error_ = error;
981}
982
fayang91475c42019-06-19 08:04:26 -0700983bool QuicDispatcher::OnFailedToDispatchPacket(
fayang1ed1f762019-06-24 11:40:04 -0700984 const ReceivedPacketInfo& /*packet_info*/) {
fayang91475c42019-06-19 08:04:26 -0700985 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500986}
987
QUICHE teama6ef0a62019-03-07 20:34:33 -0500988const QuicTransportVersionVector&
989QuicDispatcher::GetSupportedTransportVersions() {
990 return version_manager_->GetSupportedTransportVersions();
991}
992
993const ParsedQuicVersionVector& QuicDispatcher::GetSupportedVersions() {
994 return version_manager_->GetSupportedVersions();
995}
996
997void QuicDispatcher::DeliverPacketsToSession(
998 const std::list<BufferedPacket>& packets,
999 QuicSession* session) {
1000 for (const BufferedPacket& packet : packets) {
1001 session->ProcessUdpPacket(packet.self_address, packet.peer_address,
1002 *(packet.packet));
1003 }
1004}
1005
fayangccbab732019-05-13 10:11:25 -07001006bool QuicDispatcher::IsSupportedVersion(const ParsedQuicVersion version) {
fayangccbab732019-05-13 10:11:25 -07001007 for (const ParsedQuicVersion& supported_version :
1008 version_manager_->GetSupportedVersions()) {
1009 if (version == supported_version) {
1010 return true;
1011 }
1012 }
1013 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001014}
1015
fayangd057e662019-07-10 13:29:41 -07001016void QuicDispatcher::MaybeResetPacketsWithNoVersion(
1017 const ReceivedPacketInfo& packet_info) {
1018 DCHECK(!packet_info.version_flag);
1019 const size_t MinValidPacketLength =
1020 kPacketHeaderTypeSize + expected_server_connection_id_length_ +
1021 PACKET_1BYTE_PACKET_NUMBER + /*payload size=*/1 + /*tag size=*/12;
1022 if (packet_info.packet.length() < MinValidPacketLength) {
1023 // The packet size is too small.
1024 QUIC_CODE_COUNT(drop_too_small_packets);
1025 return;
1026 }
1027 // TODO(fayang): Consider rate limiting reset packets if reset packet size >
1028 // packet_length.
1029
1030 time_wait_list_manager()->SendPublicReset(
1031 packet_info.self_address, packet_info.peer_address,
1032 packet_info.destination_connection_id,
1033 packet_info.form != GOOGLE_QUIC_PACKET, GetPerPacketContext());
1034}
1035
QUICHE teama6ef0a62019-03-07 20:34:33 -05001036} // namespace quic