blob: f1fbb32247055742cf0aa0012a2944b2a66008fd [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"
dmcardlecf0bfcf2019-12-13 08:08:21 -080024#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
25#include "net/third_party/quiche/src/common/platform/api/quiche_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
fayange3f2f7b2019-09-19 17:01:57 -070035// Minimal INITIAL packet length sent by clients is 1200.
36const QuicPacketLength kMinClientInitialPacketLength = 1200;
37
QUICHE teama6ef0a62019-03-07 20:34:33 -050038// An alarm that informs the QuicDispatcher to delete old sessions.
39class DeleteSessionsAlarm : public QuicAlarm::Delegate {
40 public:
41 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher)
42 : dispatcher_(dispatcher) {}
43 DeleteSessionsAlarm(const DeleteSessionsAlarm&) = delete;
44 DeleteSessionsAlarm& operator=(const DeleteSessionsAlarm&) = delete;
45
46 void OnAlarm() override { dispatcher_->DeleteSessions(); }
47
48 private:
49 // Not owned.
50 QuicDispatcher* dispatcher_;
51};
52
53// Collects packets serialized by a QuicPacketCreator in order
54// to be handed off to the time wait list manager.
55class PacketCollector : public QuicPacketCreator::DelegateInterface,
56 public QuicStreamFrameDataProducer {
57 public:
58 explicit PacketCollector(QuicBufferAllocator* allocator)
59 : send_buffer_(allocator) {}
60 ~PacketCollector() override = default;
61
62 // QuicPacketCreator::DelegateInterface methods:
63 void OnSerializedPacket(SerializedPacket* serialized_packet) override {
64 // Make a copy of the serialized packet to send later.
65 packets_.emplace_back(
66 new QuicEncryptedPacket(CopyBuffer(*serialized_packet),
67 serialized_packet->encrypted_length, true));
68 serialized_packet->encrypted_buffer = nullptr;
69 DeleteFrames(&(serialized_packet->retransmittable_frames));
70 serialized_packet->retransmittable_frames.clear();
71 }
72
73 char* GetPacketBuffer() override {
74 // Let QuicPacketCreator to serialize packets on stack buffer.
75 return nullptr;
76 }
77
dschinazi17d42422019-06-18 16:35:07 -070078 void OnUnrecoverableError(QuicErrorCode /*error*/,
79 const std::string& /*error_details*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050080
fayangcad11792019-09-16 13:11:44 -070081 bool ShouldGeneratePacket(HasRetransmittableData /*retransmittable*/,
82 IsHandshake /*handshake*/) override {
83 DCHECK(false);
84 return true;
85 }
86
87 const QuicFrames MaybeBundleAckOpportunistically() override {
88 DCHECK(false);
89 return {};
90 }
91
QUICHE teama6ef0a62019-03-07 20:34:33 -050092 // QuicStreamFrameDataProducer
dschinazi17d42422019-06-18 16:35:07 -070093 WriteStreamDataResult WriteStreamData(QuicStreamId /*id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -050094 QuicStreamOffset offset,
95 QuicByteCount data_length,
96 QuicDataWriter* writer) override {
97 if (send_buffer_.WriteStreamData(offset, data_length, writer)) {
98 return WRITE_SUCCESS;
99 }
100 return WRITE_FAILED;
101 }
dschinazi17d42422019-06-18 16:35:07 -0700102 bool WriteCryptoData(EncryptionLevel /*level*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500103 QuicStreamOffset offset,
104 QuicByteCount data_length,
105 QuicDataWriter* writer) override {
106 return send_buffer_.WriteStreamData(offset, data_length, writer);
107 }
108
109 std::vector<std::unique_ptr<QuicEncryptedPacket>>* packets() {
110 return &packets_;
111 }
112
113 private:
114 std::vector<std::unique_ptr<QuicEncryptedPacket>> packets_;
115 // This is only needed until the packets are encrypted. Once packets are
116 // encrypted, the stream data is no longer required.
117 QuicStreamSendBuffer send_buffer_;
118};
119
120// Helper for statelessly closing connections by generating the
121// correct termination packets and adding the connection to the time wait
122// list manager.
123class StatelessConnectionTerminator {
124 public:
dschinazi7b9278c2019-05-20 07:36:21 -0700125 StatelessConnectionTerminator(QuicConnectionId server_connection_id,
fayangf7c569c2019-05-07 11:56:51 -0700126 const ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500127 QuicConnectionHelperInterface* helper,
128 QuicTimeWaitListManager* time_wait_list_manager)
dschinazi7b9278c2019-05-20 07:36:21 -0700129 : server_connection_id_(server_connection_id),
fayangf7c569c2019-05-07 11:56:51 -0700130 framer_(ParsedQuicVersionVector{version},
131 /*unused*/ QuicTime::Zero(),
132 Perspective::IS_SERVER,
133 /*unused*/ kQuicDefaultConnectionIdLength),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134 collector_(helper->GetStreamSendBufferAllocator()),
dschinazi7b9278c2019-05-20 07:36:21 -0700135 creator_(server_connection_id, &framer_, &collector_),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500136 time_wait_list_manager_(time_wait_list_manager) {
fayangf7c569c2019-05-07 11:56:51 -0700137 framer_.set_data_producer(&collector_);
nharperba307612019-11-11 16:56:17 -0800138 framer_.SetInitialObfuscators(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500139 }
140
141 ~StatelessConnectionTerminator() {
142 // Clear framer's producer.
fayangf7c569c2019-05-07 11:56:51 -0700143 framer_.set_data_producer(nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144 }
145
fayange3f2f7b2019-09-19 17:01:57 -0700146 // Serializes a packet containing CONNECTION_CLOSE frame and send it (without
147 // adding connection to the time wait).
148 void StatelesslyCloseConnection(const QuicSocketAddress& self_address,
149 const QuicSocketAddress& peer_address,
150 QuicErrorCode error_code,
151 const std::string& error_details) {
rch9d76c2d2019-12-20 12:19:48 -0800152 // TODO(rch): Remove this method when quic_drop_small_initial_packets is
153 // deprecated.
154 DCHECK(!GetQuicReloadableFlag(quic_drop_small_initial_packets));
fayange3f2f7b2019-09-19 17:01:57 -0700155 SerializeConnectionClosePacket(error_code, error_details);
156
157 for (const auto& packet : *collector_.packets()) {
158 time_wait_list_manager_->SendPacket(self_address, peer_address, *packet);
159 }
160 }
161
QUICHE teama6ef0a62019-03-07 20:34:33 -0500162 // Generates a packet containing a CONNECTION_CLOSE frame specifying
163 // |error_code| and |error_details| and add the connection to time wait.
164 void CloseConnection(QuicErrorCode error_code,
vasilvvc48c8712019-03-11 13:38:16 -0700165 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166 bool ietf_quic) {
fayange3f2f7b2019-09-19 17:01:57 -0700167 SerializeConnectionClosePacket(error_code, error_details);
168
169 time_wait_list_manager_->AddConnectionIdToTimeWait(
170 server_connection_id_, ietf_quic,
171 QuicTimeWaitListManager::SEND_TERMINATION_PACKETS,
172 quic::ENCRYPTION_INITIAL, collector_.packets());
173 }
174
175 private:
176 void SerializeConnectionClosePacket(QuicErrorCode error_code,
177 const std::string& error_details) {
fkastenholz591814c2019-09-06 12:11:46 -0700178 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(
179 framer_.transport_version(), error_code, error_details,
180 /*transport_close_frame_type=*/0);
fkastenholz72f509b2019-04-10 09:17:49 -0700181
renjietangb63005e2019-11-19 23:08:53 -0800182 if (!creator_.AddFrame(QuicFrame(frame), NOT_RETRANSMISSION)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183 QUIC_BUG << "Unable to add frame to an empty packet";
184 delete frame;
185 return;
186 }
fayang62b637b2019-09-16 08:40:49 -0700187 creator_.FlushCurrentPacket();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500188 DCHECK_EQ(1u, collector_.packets()->size());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500189 }
190
dschinazi7b9278c2019-05-20 07:36:21 -0700191 QuicConnectionId server_connection_id_;
fayangf7c569c2019-05-07 11:56:51 -0700192 QuicFramer framer_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193 // Set as the visitor of |creator_| to collect any generated packets.
194 PacketCollector collector_;
195 QuicPacketCreator creator_;
196 QuicTimeWaitListManager* time_wait_list_manager_;
197};
198
199// Class which extracts the ALPN from a CHLO packet.
200class ChloAlpnExtractor : public ChloExtractor::Delegate {
201 public:
dschinazi17d42422019-06-18 16:35:07 -0700202 void OnChlo(QuicTransportVersion /*version*/,
203 QuicConnectionId /*server_connection_id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500204 const CryptoHandshakeMessage& chlo) override {
dmcardlecf0bfcf2019-12-13 08:08:21 -0800205 quiche::QuicheStringPiece alpn_value;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500206 if (chlo.GetStringPiece(kALPN, &alpn_value)) {
vasilvvc48c8712019-03-11 13:38:16 -0700207 alpn_ = std::string(alpn_value);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500208 }
209 }
210
vasilvvc48c8712019-03-11 13:38:16 -0700211 std::string&& ConsumeAlpn() { return std::move(alpn_); }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500212
213 private:
vasilvvc48c8712019-03-11 13:38:16 -0700214 std::string alpn_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500215};
216
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217} // namespace
218
219QuicDispatcher::QuicDispatcher(
220 const QuicConfig* config,
221 const QuicCryptoServerConfig* crypto_config,
222 QuicVersionManager* version_manager,
223 std::unique_ptr<QuicConnectionHelperInterface> helper,
224 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper,
225 std::unique_ptr<QuicAlarmFactory> alarm_factory,
dschinazi8ff74822019-05-28 16:37:20 -0700226 uint8_t expected_server_connection_id_length)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500227 : config_(config),
228 crypto_config_(crypto_config),
229 compressed_certs_cache_(
230 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize),
231 helper_(std::move(helper)),
232 session_helper_(std::move(session_helper)),
233 alarm_factory_(std::move(alarm_factory)),
234 delete_sessions_alarm_(
235 alarm_factory_->CreateAlarm(new DeleteSessionsAlarm(this))),
236 buffered_packets_(this, helper_->GetClock(), alarm_factory_.get()),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500237 version_manager_(version_manager),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500238 last_error_(QUIC_NO_ERROR),
239 new_sessions_allowed_per_event_loop_(0u),
QUICHE team963d57e2019-03-21 10:58:47 -0700240 accept_new_connections_(true),
dschinazi7b9278c2019-05-20 07:36:21 -0700241 allow_short_initial_server_connection_ids_(false),
dschinazi8ff74822019-05-28 16:37:20 -0700242 expected_server_connection_id_length_(
243 expected_server_connection_id_length),
dschinazic5589bd2019-09-12 14:50:11 -0700244 should_update_expected_server_connection_id_length_(false) {
dschinazi4101ead2019-12-06 15:26:07 -0800245 QUIC_BUG_IF(GetSupportedVersions().empty())
246 << "Trying to create dispatcher without any supported versions";
dschinazic5589bd2019-09-12 14:50:11 -0700247 QUIC_DLOG(INFO) << "Created QuicDispatcher with versions: "
248 << ParsedQuicVersionVectorToString(GetSupportedVersions());
249}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500250
251QuicDispatcher::~QuicDispatcher() {
252 session_map_.clear();
253 closed_session_list_.clear();
254}
255
256void QuicDispatcher::InitializeWithWriter(QuicPacketWriter* writer) {
257 DCHECK(writer_ == nullptr);
258 writer_.reset(writer);
259 time_wait_list_manager_.reset(CreateQuicTimeWaitListManager());
260}
261
262void QuicDispatcher::ProcessPacket(const QuicSocketAddress& self_address,
263 const QuicSocketAddress& peer_address,
264 const QuicReceivedPacket& packet) {
dschinazi965ce092019-05-23 06:29:01 -0700265 QUIC_DVLOG(2) << "Dispatcher received encrypted " << packet.length()
266 << " bytes:" << std::endl
dmcardlecf0bfcf2019-12-13 08:08:21 -0800267 << quiche::QuicheTextUtils::HexDump(quiche::QuicheStringPiece(
268 packet.data(), packet.length()));
fayang1ed1f762019-06-24 11:40:04 -0700269 ReceivedPacketInfo packet_info(self_address, peer_address, packet);
fayang1f123502019-05-14 08:05:16 -0700270 std::string detailed_error;
dschinazibf0413d2019-10-07 14:19:53 -0700271 bool retry_token_present;
dmcardlecf0bfcf2019-12-13 08:08:21 -0800272 quiche::QuicheStringPiece retry_token;
dschinazibf0413d2019-10-07 14:19:53 -0700273 const QuicErrorCode error = QuicFramer::ParsePublicHeaderDispatcher(
274 packet, expected_server_connection_id_length_, &packet_info.form,
275 &packet_info.long_packet_type, &packet_info.version_flag,
276 &packet_info.use_length_prefix, &packet_info.version_label,
277 &packet_info.version, &packet_info.destination_connection_id,
278 &packet_info.source_connection_id, &retry_token_present, &retry_token,
279 &detailed_error);
fayangccbab732019-05-13 10:11:25 -0700280 if (error != QUIC_NO_ERROR) {
281 // Packet has framing error.
282 SetLastError(error);
283 QUIC_DLOG(ERROR) << detailed_error;
284 return;
285 }
fayang1ed1f762019-06-24 11:40:04 -0700286 if (packet_info.destination_connection_id.length() !=
dschinazi8ff74822019-05-28 16:37:20 -0700287 expected_server_connection_id_length_ &&
288 !should_update_expected_server_connection_id_length_ &&
dschinazi97da52b2020-01-13 15:44:43 -0800289 packet_info.version.IsKnown() &&
290 !packet_info.version.AllowsVariableLengthConnectionIds()) {
fayangccbab732019-05-13 10:11:25 -0700291 SetLastError(QUIC_INVALID_PACKET_HEADER);
292 QUIC_DLOG(ERROR) << "Invalid Connection Id Length";
293 return;
294 }
dschinazib953d022019-08-01 18:05:58 -0700295
296 if (packet_info.version_flag && IsSupportedVersion(packet_info.version)) {
297 if (!QuicUtils::IsConnectionIdValidForVersion(
298 packet_info.destination_connection_id,
299 packet_info.version.transport_version)) {
300 SetLastError(QUIC_INVALID_PACKET_HEADER);
301 QUIC_DLOG(ERROR)
302 << "Invalid destination connection ID length for version";
303 return;
304 }
305 if (packet_info.version.SupportsClientConnectionIds() &&
306 !QuicUtils::IsConnectionIdValidForVersion(
307 packet_info.source_connection_id,
308 packet_info.version.transport_version)) {
309 SetLastError(QUIC_INVALID_PACKET_HEADER);
310 QUIC_DLOG(ERROR) << "Invalid source connection ID length for version";
311 return;
312 }
313 }
314
dschinazi8ff74822019-05-28 16:37:20 -0700315 if (should_update_expected_server_connection_id_length_) {
fayang1ed1f762019-06-24 11:40:04 -0700316 expected_server_connection_id_length_ =
317 packet_info.destination_connection_id.length();
fayangccbab732019-05-13 10:11:25 -0700318 }
fayang91475c42019-06-19 08:04:26 -0700319
fayang1ed1f762019-06-24 11:40:04 -0700320 if (MaybeDispatchPacket(packet_info)) {
fayang91475c42019-06-19 08:04:26 -0700321 // Packet has been dropped or successfully dispatched, stop processing.
fayangccbab732019-05-13 10:11:25 -0700322 return;
323 }
fayang1ed1f762019-06-24 11:40:04 -0700324 ProcessHeader(&packet_info);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500325}
326
dschinazi7b9278c2019-05-20 07:36:21 -0700327QuicConnectionId QuicDispatcher::MaybeReplaceServerConnectionId(
328 QuicConnectionId server_connection_id,
QUICHE teamc65d1d12019-03-19 20:58:04 -0700329 ParsedQuicVersion version) {
fayang91475c42019-06-19 08:04:26 -0700330 if (server_connection_id.length() == expected_server_connection_id_length_) {
dschinazi7b9278c2019-05-20 07:36:21 -0700331 return server_connection_id;
QUICHE teamc65d1d12019-03-19 20:58:04 -0700332 }
dschinazi97da52b2020-01-13 15:44:43 -0800333 DCHECK(version.AllowsVariableLengthConnectionIds());
dschinaziadc75072019-08-19 10:54:45 -0700334
QUICHE teamc65d1d12019-03-19 20:58:04 -0700335 QuicConnectionId new_connection_id =
wub662a3d62019-08-16 14:10:50 -0700336 GenerateNewServerConnectionId(version, server_connection_id);
fayang91475c42019-06-19 08:04:26 -0700337 DCHECK_EQ(expected_server_connection_id_length_, new_connection_id.length());
dschinazi28c1bf32019-08-19 11:54:46 -0700338
339 // Verify that GenerateNewServerConnectionId is deterministic.
340 DCHECK_EQ(new_connection_id,
341 GenerateNewServerConnectionId(version, server_connection_id));
342
dschinazi7b9278c2019-05-20 07:36:21 -0700343 QUIC_DLOG(INFO) << "Replacing incoming connection ID " << server_connection_id
QUICHE teamc65d1d12019-03-19 20:58:04 -0700344 << " with " << new_connection_id;
345 return new_connection_id;
346}
347
wub662a3d62019-08-16 14:10:50 -0700348QuicConnectionId QuicDispatcher::GenerateNewServerConnectionId(
349 ParsedQuicVersion /*version*/,
dschinaziadc75072019-08-19 10:54:45 -0700350 QuicConnectionId connection_id) const {
dschinaziadc75072019-08-19 10:54:45 -0700351 return QuicUtils::CreateReplacementConnectionId(connection_id);
wub662a3d62019-08-16 14:10:50 -0700352}
353
fayang91475c42019-06-19 08:04:26 -0700354bool QuicDispatcher::MaybeDispatchPacket(
fayang1ed1f762019-06-24 11:40:04 -0700355 const ReceivedPacketInfo& packet_info) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500356 // Port zero is only allowed for unidirectional UDP, so is disallowed by QUIC.
357 // Given that we can't even send a reply rejecting the packet, just drop the
358 // packet.
fayang1ed1f762019-06-24 11:40:04 -0700359 if (packet_info.peer_address.port() == 0) {
fayang91475c42019-06-19 08:04:26 -0700360 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500361 }
362
fayang1ed1f762019-06-24 11:40:04 -0700363 QuicConnectionId server_connection_id = packet_info.destination_connection_id;
QUICHE team8e2e4532019-03-14 14:37:56 -0700364
QUICHE team963d57e2019-03-21 10:58:47 -0700365 // The IETF spec requires the client to generate an initial server
366 // connection ID that is at least 64 bits long. After that initial
367 // connection ID, the dispatcher picks a new one of its expected length.
368 // Therefore we should never receive a connection ID that is smaller
369 // than 64 bits and smaller than what we expect.
dschinazi7b9278c2019-05-20 07:36:21 -0700370 if (server_connection_id.length() < kQuicMinimumInitialConnectionIdLength &&
fayang91475c42019-06-19 08:04:26 -0700371 server_connection_id.length() < expected_server_connection_id_length_ &&
dschinazi7b9278c2019-05-20 07:36:21 -0700372 !allow_short_initial_server_connection_ids_) {
fayang1ed1f762019-06-24 11:40:04 -0700373 DCHECK(packet_info.version_flag);
dschinazi97da52b2020-01-13 15:44:43 -0800374 DCHECK(packet_info.version.AllowsVariableLengthConnectionIds());
QUICHE team963d57e2019-03-21 10:58:47 -0700375 QUIC_DLOG(INFO) << "Packet with short destination connection ID "
dschinazi7b9278c2019-05-20 07:36:21 -0700376 << server_connection_id << " expected "
fayang91475c42019-06-19 08:04:26 -0700377 << static_cast<int>(expected_server_connection_id_length_);
dschinazi95025bf2019-09-06 15:08:47 -0700378 // Drop the packet silently.
379 QUIC_CODE_COUNT(quic_dropped_invalid_small_initial_connection_id);
fayang91475c42019-06-19 08:04:26 -0700380 return true;
QUICHE team963d57e2019-03-21 10:58:47 -0700381 }
382
QUICHE teama6ef0a62019-03-07 20:34:33 -0500383 // Packets with connection IDs for active connections are processed
384 // immediately.
dschinazi7b9278c2019-05-20 07:36:21 -0700385 auto it = session_map_.find(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500386 if (it != session_map_.end()) {
dschinazi7b9278c2019-05-20 07:36:21 -0700387 DCHECK(!buffered_packets_.HasBufferedPackets(server_connection_id));
fayang1ed1f762019-06-24 11:40:04 -0700388 it->second->ProcessUdpPacket(packet_info.self_address,
389 packet_info.peer_address, packet_info.packet);
fayang91475c42019-06-19 08:04:26 -0700390 return true;
dschinazicc5a54c2019-09-24 08:30:14 -0700391 } else if (packet_info.version.transport_version !=
392 QUIC_VERSION_UNSUPPORTED) {
dschinazi5c030852019-07-11 15:45:53 -0700393 // We did not find the connection ID, check if we've replaced it.
dschinazicc5a54c2019-09-24 08:30:14 -0700394 // This is only performed for supported versions because packets with
395 // unsupported versions can flow through this function in order to send
396 // a version negotiation packet, but we know that their connection ID
397 // did not get replaced since that is performed on connection creation,
398 // and that only happens for known verions.
dschinazi5c030852019-07-11 15:45:53 -0700399 QuicConnectionId replaced_connection_id = MaybeReplaceServerConnectionId(
400 server_connection_id, packet_info.version);
401 if (replaced_connection_id != server_connection_id) {
402 // Search for the replacement.
403 auto it2 = session_map_.find(replaced_connection_id);
404 if (it2 != session_map_.end()) {
405 DCHECK(!buffered_packets_.HasBufferedPackets(replaced_connection_id));
406 it2->second->ProcessUdpPacket(packet_info.self_address,
407 packet_info.peer_address,
408 packet_info.packet);
409 return true;
410 }
411 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500412 }
413
dschinazi7b9278c2019-05-20 07:36:21 -0700414 if (buffered_packets_.HasChloForConnection(server_connection_id)) {
fayang1ed1f762019-06-24 11:40:04 -0700415 BufferEarlyPacket(packet_info);
fayang91475c42019-06-19 08:04:26 -0700416 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500417 }
418
fayang1ed1f762019-06-24 11:40:04 -0700419 if (OnFailedToDispatchPacket(packet_info)) {
fayang91475c42019-06-19 08:04:26 -0700420 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500421 }
422
dschinazi7b9278c2019-05-20 07:36:21 -0700423 if (time_wait_list_manager_->IsConnectionIdInTimeWait(server_connection_id)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500424 // This connection ID is already in time-wait state.
425 time_wait_list_manager_->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700426 packet_info.self_address, packet_info.peer_address,
427 packet_info.destination_connection_id, packet_info.form,
428 GetPerPacketContext());
fayang91475c42019-06-19 08:04:26 -0700429 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500430 }
431
432 // The packet has an unknown connection ID.
433
434 // Unless the packet provides a version, assume that we can continue
435 // processing using our preferred version.
fayang1ed1f762019-06-24 11:40:04 -0700436 if (packet_info.version_flag) {
437 if (!IsSupportedVersion(packet_info.version)) {
438 if (ShouldCreateSessionForUnknownVersion(packet_info.version_label)) {
fayang91475c42019-06-19 08:04:26 -0700439 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500440 }
441 if (!crypto_config()->validate_chlo_size() ||
fayang1ed1f762019-06-24 11:40:04 -0700442 packet_info.packet.length() >= kMinPacketSizeForVersionNegotiation) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500443 // Since the version is not supported, send a version negotiation
444 // packet and stop processing the current packet.
fayang1ed1f762019-06-24 11:40:04 -0700445 QuicConnectionId client_connection_id =
446 packet_info.source_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500447 time_wait_list_manager()->SendVersionNegotiationPacket(
dschinazi346b7ce2019-06-05 01:38:18 -0700448 server_connection_id, client_connection_id,
dschinazibf6a7832019-10-08 18:03:52 -0700449 packet_info.form != GOOGLE_QUIC_PACKET,
450 packet_info.use_length_prefix, GetSupportedVersions(),
451 packet_info.self_address, packet_info.peer_address,
452 GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500453 }
fayang91475c42019-06-19 08:04:26 -0700454 return true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500455 }
fayange3f2f7b2019-09-19 17:01:57 -0700456
fayang12512a82019-11-04 11:34:39 -0800457 if (crypto_config()->validate_chlo_size() &&
fayange3f2f7b2019-09-19 17:01:57 -0700458 packet_info.form == IETF_QUIC_LONG_HEADER_PACKET &&
459 packet_info.long_packet_type == INITIAL &&
460 packet_info.packet.length() < kMinClientInitialPacketLength) {
rch9d76c2d2019-12-20 12:19:48 -0800461 if (GetQuicReloadableFlag(quic_drop_small_initial_packets)) {
462 QUIC_DVLOG(1) << "Dropping initial packet which is too short, length: "
463 << packet_info.packet.length();
464 QUIC_CODE_COUNT(quic_drop_small_initial_packets);
465 QUIC_RELOADABLE_FLAG_COUNT(quic_drop_small_initial_packets);
466 return true;
467 }
fayange3f2f7b2019-09-19 17:01:57 -0700468 StatelessConnectionTerminator terminator(
469 packet_info.destination_connection_id, packet_info.version,
470 helper_.get(), time_wait_list_manager_.get());
471 QUIC_DVLOG(1) << "Initial packet too small: "
472 << packet_info.packet.length();
473 terminator.StatelesslyCloseConnection(
474 packet_info.self_address, packet_info.peer_address,
475 IETF_QUIC_PROTOCOL_VIOLATION, "Initial packet too small");
476 return true;
477 }
fayangccbab732019-05-13 10:11:25 -0700478 }
nharper55fa6132019-05-07 19:37:21 -0700479
nharper55fa6132019-05-07 19:37:21 -0700480 return false;
481}
482
fayang1ed1f762019-06-24 11:40:04 -0700483void QuicDispatcher::ProcessHeader(ReceivedPacketInfo* packet_info) {
484 QuicConnectionId server_connection_id =
485 packet_info->destination_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500486 // Packet's connection ID is unknown. Apply the validity checks.
wub2a5670f2019-06-13 13:39:16 -0700487 // TODO(wub): Determine the fate completely in ValidityChecks, then call
488 // ProcessUnauthenticatedHeaderFate in one place.
fayang1ed1f762019-06-24 11:40:04 -0700489 QuicPacketFate fate = ValidityChecks(*packet_info);
fayang17230ac2019-06-21 06:28:28 -0700490 ChloAlpnExtractor alpn_extractor;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500491 switch (fate) {
dschinazi76992e72019-07-17 20:40:40 -0700492 case kFateProcess: {
fayang1ed1f762019-06-24 11:40:04 -0700493 if (packet_info->version.handshake_protocol == PROTOCOL_TLS1_3) {
fayang17230ac2019-06-21 06:28:28 -0700494 // TODO(nharper): Support buffering non-ClientHello packets when using
495 // TLS.
fayang1ed1f762019-06-24 11:40:04 -0700496 ProcessChlo(/*alpn=*/"", packet_info);
fayang17230ac2019-06-21 06:28:28 -0700497 break;
498 }
499 if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) &&
dschinazi4fd8cb12019-09-09 16:31:06 -0700500 !ChloExtractor::Extract(packet_info->packet, packet_info->version,
fayang17230ac2019-06-21 06:28:28 -0700501 config_->create_session_tag_indicators(),
502 &alpn_extractor,
503 server_connection_id.length())) {
504 // Buffer non-CHLO packets.
fayang1ed1f762019-06-24 11:40:04 -0700505 BufferEarlyPacket(*packet_info);
fayang17230ac2019-06-21 06:28:28 -0700506 break;
507 }
fayang1ed1f762019-06-24 11:40:04 -0700508 ProcessChlo(alpn_extractor.ConsumeAlpn(), packet_info);
dschinazi76992e72019-07-17 20:40:40 -0700509 } break;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500510 case kFateTimeWait:
wub0a4b9c52019-05-28 13:18:58 -0700511 // Add this connection_id to the time-wait state, to safely reject
512 // future packets.
513 QUIC_DLOG(INFO) << "Adding connection ID " << server_connection_id
514 << " to time-wait list.";
515 QUIC_CODE_COUNT(quic_reject_fate_time_wait);
516 StatelesslyTerminateConnection(
fayang1ed1f762019-06-24 11:40:04 -0700517 server_connection_id, packet_info->form, packet_info->version_flag,
dschinazi48ac9192019-07-31 00:07:26 -0700518 packet_info->use_length_prefix, packet_info->version,
519 QUIC_HANDSHAKE_FAILED, "Reject connection",
wub0a4b9c52019-05-28 13:18:58 -0700520 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
521
dschinazi7b9278c2019-05-20 07:36:21 -0700522 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(
523 server_connection_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500524 time_wait_list_manager_->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700525 packet_info->self_address, packet_info->peer_address,
526 server_connection_id, packet_info->form, GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500527
dschinazi7b9278c2019-05-20 07:36:21 -0700528 buffered_packets_.DiscardPackets(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500529 break;
fayangd057e662019-07-10 13:29:41 -0700530 case kFateDrop:
531 break;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500532 }
533}
534
535QuicDispatcher::QuicPacketFate QuicDispatcher::ValidityChecks(
fayang1ed1f762019-06-24 11:40:04 -0700536 const ReceivedPacketInfo& packet_info) {
fayang1ed1f762019-06-24 11:40:04 -0700537 if (!packet_info.version_flag) {
dschinazi5b236be2019-08-19 14:55:22 -0700538 // The Android network conformance test contains a UDP test that sends a
539 // 12-byte packet with the following format:
540 // - 0x0c (public flags: 8-byte connection ID, 1-byte packet number)
541 // - randomized 8-byte connection ID
542 // - 0x01 (1-byte packet number)
543 // - 0x00 (private flags)
544 // - 0x07 (PING frame).
545 // That packet is invalid and we would normally drop it but in order to
546 // unblock this conformance testing we have the following workaround that
547 // will be removed once the fixed test is deployed.
548 // TODO(b/139691956) Remove this workaround once fixed test is deployed.
549 if (packet_info.packet.length() == 12 &&
550 packet_info.packet.data()[0] == 0x0c &&
551 packet_info.packet.data()[9] == 0x01 &&
552 packet_info.packet.data()[10] == 0x00 &&
553 packet_info.packet.data()[11] == 0x07) {
554 QUIC_DLOG(INFO) << "Received Android UDP network conformance test "
555 "packet with connection ID "
556 << packet_info.destination_connection_id;
557 // Respond with a public reset that the test will know how to parse
558 // then return kFateDrop to stop processing of this packet.
559 time_wait_list_manager()->SendPublicReset(
560 packet_info.self_address, packet_info.peer_address,
561 packet_info.destination_connection_id,
562 /*ietf_quic=*/false, GetPerPacketContext());
563 return kFateDrop;
564 }
dschinazi5b236be2019-08-19 14:55:22 -0700565
QUICHE teama6ef0a62019-03-07 20:34:33 -0500566 QUIC_DLOG(INFO)
567 << "Packet without version arrived for unknown connection ID "
fayang1ed1f762019-06-24 11:40:04 -0700568 << packet_info.destination_connection_id;
fayang9d6231c2019-12-04 07:10:13 -0800569 MaybeResetPacketsWithNoVersion(packet_info);
570 return kFateDrop;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500571 }
572
fayang91475c42019-06-19 08:04:26 -0700573 // Let the connection parse and validate packet number.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500574 return kFateProcess;
575}
576
577void QuicDispatcher::CleanUpSession(SessionMap::iterator it,
578 QuicConnection* connection,
dschinazi17d42422019-06-18 16:35:07 -0700579 ConnectionCloseSource /*source*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500580 write_blocked_list_.erase(connection);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500581 QuicTimeWaitListManager::TimeWaitAction action =
582 QuicTimeWaitListManager::SEND_STATELESS_RESET;
583 if (connection->termination_packets() != nullptr &&
584 !connection->termination_packets()->empty()) {
585 action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS;
fayang51c23732019-06-24 06:59:55 -0700586 } else {
fayangfa3b1d62019-11-18 08:02:13 -0800587 if (!connection->IsHandshakeComplete()) {
fayangd4291e42019-05-30 10:31:21 -0700588 if (!VersionHasIetfInvariantHeader(connection->transport_version())) {
fayang1de67892019-04-19 05:59:45 -0700589 QUIC_CODE_COUNT(gquic_add_to_time_wait_list_with_handshake_failed);
590 } else {
591 QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_handshake_failed);
592 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500593 action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS;
594 // This serializes a connection close termination packet with error code
595 // QUIC_HANDSHAKE_FAILED and adds the connection to the time wait list.
596 StatelesslyTerminateConnection(
fayang1de67892019-04-19 05:59:45 -0700597 connection->connection_id(),
fayangd4291e42019-05-30 10:31:21 -0700598 VersionHasIetfInvariantHeader(connection->transport_version())
fayang1de67892019-04-19 05:59:45 -0700599 ? IETF_QUIC_LONG_HEADER_PACKET
600 : GOOGLE_QUIC_PACKET,
dschinazi48ac9192019-07-31 00:07:26 -0700601 /*version_flag=*/true,
602 connection->version().HasLengthPrefixedConnectionIds(),
603 connection->version(), QUIC_HANDSHAKE_FAILED,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500604 "Connection is closed by server before handshake confirmed",
605 // Although it is our intention to send termination packets, the
606 // |action| argument is not used by this call to
607 // StatelesslyTerminateConnection().
608 action);
609 session_map_.erase(it);
610 return;
611 }
612 QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_stateless_reset);
613 }
614 time_wait_list_manager_->AddConnectionIdToTimeWait(
fayangd4291e42019-05-30 10:31:21 -0700615 it->first, VersionHasIetfInvariantHeader(connection->transport_version()),
616 action, connection->encryption_level(),
617 connection->termination_packets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500618 session_map_.erase(it);
619}
620
621void QuicDispatcher::StopAcceptingNewConnections() {
622 accept_new_connections_ = false;
623}
624
625std::unique_ptr<QuicPerPacketContext> QuicDispatcher::GetPerPacketContext()
626 const {
627 return nullptr;
628}
629
630void QuicDispatcher::DeleteSessions() {
QUICHE teamaa1d6a82019-03-13 09:14:13 -0700631 if (!write_blocked_list_.empty()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500632 for (const std::unique_ptr<QuicSession>& session : closed_session_list_) {
633 if (write_blocked_list_.erase(session->connection()) != 0) {
634 QUIC_BUG << "QuicConnection was in WriteBlockedList before destruction";
635 }
636 }
637 }
638 closed_session_list_.clear();
639}
640
641void QuicDispatcher::OnCanWrite() {
642 // The socket is now writable.
643 writer_->SetWritable();
644
645 // Move every blocked writer in |write_blocked_list_| to a temporary list.
646 const size_t num_blocked_writers_before = write_blocked_list_.size();
647 WriteBlockedList temp_list;
648 temp_list.swap(write_blocked_list_);
649 DCHECK(write_blocked_list_.empty());
650
651 // Give each blocked writer a chance to write what they indended to write.
652 // If they are blocked again, they will call |OnWriteBlocked| to add
653 // themselves back into |write_blocked_list_|.
654 while (!temp_list.empty()) {
655 QuicBlockedWriterInterface* blocked_writer = temp_list.begin()->first;
656 temp_list.erase(temp_list.begin());
657 blocked_writer->OnBlockedWriterCanWrite();
658 }
659 const size_t num_blocked_writers_after = write_blocked_list_.size();
660 if (num_blocked_writers_after != 0) {
661 if (num_blocked_writers_before == num_blocked_writers_after) {
662 QUIC_CODE_COUNT(quic_zero_progress_on_can_write);
663 } else {
664 QUIC_CODE_COUNT(quic_blocked_again_on_can_write);
665 }
666 }
667}
668
669bool QuicDispatcher::HasPendingWrites() const {
670 return !write_blocked_list_.empty();
671}
672
673void QuicDispatcher::Shutdown() {
674 while (!session_map_.empty()) {
675 QuicSession* session = session_map_.begin()->second.get();
676 session->connection()->CloseConnection(
677 QUIC_PEER_GOING_AWAY, "Server shutdown imminent",
678 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET);
679 // Validate that the session removes itself from the session map on close.
680 DCHECK(session_map_.empty() ||
681 session_map_.begin()->second.get() != session);
682 }
683 DeleteSessions();
684}
685
dschinazi7b9278c2019-05-20 07:36:21 -0700686void QuicDispatcher::OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500687 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700688 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500689 ConnectionCloseSource source) {
dschinazi7b9278c2019-05-20 07:36:21 -0700690 auto it = session_map_.find(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500691 if (it == session_map_.end()) {
dschinazi7b9278c2019-05-20 07:36:21 -0700692 QUIC_BUG << "ConnectionId " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500693 << " does not exist in the session map. Error: "
694 << QuicErrorCodeToString(error);
695 QUIC_BUG << QuicStackTrace();
696 return;
697 }
698
699 QUIC_DLOG_IF(INFO, error != QUIC_NO_ERROR)
dschinazi7b9278c2019-05-20 07:36:21 -0700700 << "Closing connection (" << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500701 << ") due to error: " << QuicErrorCodeToString(error)
702 << ", with details: " << error_details;
703
704 QuicConnection* connection = it->second->connection();
705 if (ShouldDestroySessionAsynchronously()) {
706 // Set up alarm to fire immediately to bring destruction of this session
707 // out of current call stack.
708 if (closed_session_list_.empty()) {
709 delete_sessions_alarm_->Update(helper()->GetClock()->ApproximateNow(),
710 QuicTime::Delta::Zero());
711 }
712 closed_session_list_.push_back(std::move(it->second));
713 }
wub5f64ec42019-06-06 07:31:19 -0700714 CleanUpSession(it, connection, source);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500715}
716
717void QuicDispatcher::OnWriteBlocked(
718 QuicBlockedWriterInterface* blocked_writer) {
719 if (!blocked_writer->IsWriterBlocked()) {
720 // It is a programming error if this ever happens. When we are sure it is
721 // not happening, replace it with a DCHECK.
722 QUIC_BUG
723 << "Tried to add writer into blocked list when it shouldn't be added";
724 // Return without adding the connection to the blocked list, to avoid
725 // infinite loops in OnCanWrite.
726 return;
727 }
728
729 write_blocked_list_.insert(std::make_pair(blocked_writer, true));
730}
731
dschinazi17d42422019-06-18 16:35:07 -0700732void QuicDispatcher::OnRstStreamReceived(const QuicRstStreamFrame& /*frame*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500733
dschinazi17d42422019-06-18 16:35:07 -0700734void QuicDispatcher::OnStopSendingReceived(
735 const QuicStopSendingFrame& /*frame*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500736
737void QuicDispatcher::OnConnectionAddedToTimeWaitList(
dschinazi7b9278c2019-05-20 07:36:21 -0700738 QuicConnectionId server_connection_id) {
739 QUIC_DLOG(INFO) << "Connection " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500740 << " added to time wait list.";
741}
742
743void QuicDispatcher::StatelesslyTerminateConnection(
dschinazi7b9278c2019-05-20 07:36:21 -0700744 QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500745 PacketHeaderFormat format,
fayang1de67892019-04-19 05:59:45 -0700746 bool version_flag,
dschinazi48ac9192019-07-31 00:07:26 -0700747 bool use_length_prefix,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500748 ParsedQuicVersion version,
749 QuicErrorCode error_code,
vasilvvc48c8712019-03-11 13:38:16 -0700750 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500751 QuicTimeWaitListManager::TimeWaitAction action) {
fayang51c23732019-06-24 06:59:55 -0700752 if (format != IETF_QUIC_LONG_HEADER_PACKET && !version_flag) {
dschinazi7b9278c2019-05-20 07:36:21 -0700753 QUIC_DVLOG(1) << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500754 << " based on a non-ietf-long packet, action:" << action
755 << ", error_code:" << error_code
756 << ", error_details:" << error_details;
757 time_wait_list_manager_->AddConnectionIdToTimeWait(
dschinazi7b9278c2019-05-20 07:36:21 -0700758 server_connection_id, format != GOOGLE_QUIC_PACKET, action,
759 ENCRYPTION_INITIAL, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500760 return;
761 }
762
763 // If the version is known and supported by framer, send a connection close.
fayangccbab732019-05-13 10:11:25 -0700764 if (IsSupportedVersion(version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500765 QUIC_DVLOG(1)
dschinazi7b9278c2019-05-20 07:36:21 -0700766 << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500767 << " based on an ietf-long packet, which has a supported version:"
768 << version << ", error_code:" << error_code
769 << ", error_details:" << error_details;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500770
dschinazi7b9278c2019-05-20 07:36:21 -0700771 StatelessConnectionTerminator terminator(server_connection_id, version,
772 helper_.get(),
773 time_wait_list_manager_.get());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500774 // This also adds the connection to time wait list.
fayang1de67892019-04-19 05:59:45 -0700775 terminator.CloseConnection(error_code, error_details,
776 format != GOOGLE_QUIC_PACKET);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500777 return;
778 }
779
780 QUIC_DVLOG(1)
dschinazi7b9278c2019-05-20 07:36:21 -0700781 << "Statelessly terminating " << server_connection_id
QUICHE teama6ef0a62019-03-07 20:34:33 -0500782 << " based on an ietf-long packet, which has an unsupported version:"
783 << version << ", error_code:" << error_code
784 << ", error_details:" << error_details;
785 // Version is unknown or unsupported by framer, send a version negotiation
786 // with an empty version list, which can be understood by the client.
787 std::vector<std::unique_ptr<QuicEncryptedPacket>> termination_packets;
788 termination_packets.push_back(QuicFramer::BuildVersionNegotiationPacket(
dschinazib417d602019-05-29 13:08:45 -0700789 server_connection_id, EmptyQuicConnectionId(),
dschinazi48ac9192019-07-31 00:07:26 -0700790 /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, use_length_prefix,
dschinazi1ac22cc2019-06-25 11:47:50 -0700791 /*versions=*/{}));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500792 time_wait_list_manager()->AddConnectionIdToTimeWait(
dschinazi7b9278c2019-05-20 07:36:21 -0700793 server_connection_id, /*ietf_quic=*/format != GOOGLE_QUIC_PACKET,
QUICHE team6987b4a2019-03-15 16:23:04 -0700794 QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500795 &termination_packets);
796}
797
QUICHE teama6ef0a62019-03-07 20:34:33 -0500798bool QuicDispatcher::ShouldCreateSessionForUnknownVersion(
799 QuicVersionLabel /*version_label*/) {
800 return false;
801}
802
QUICHE teama6ef0a62019-03-07 20:34:33 -0500803void QuicDispatcher::OnExpiredPackets(
dschinazi7b9278c2019-05-20 07:36:21 -0700804 QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500805 BufferedPacketList early_arrived_packets) {
806 QUIC_CODE_COUNT(quic_reject_buffered_packets_expired);
807 StatelesslyTerminateConnection(
dschinazi7b9278c2019-05-20 07:36:21 -0700808 server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500809 early_arrived_packets.ietf_quic ? IETF_QUIC_LONG_HEADER_PACKET
810 : GOOGLE_QUIC_PACKET,
dschinazi48ac9192019-07-31 00:07:26 -0700811 /*version_flag=*/true,
812 early_arrived_packets.version.HasLengthPrefixedConnectionIds(),
813 early_arrived_packets.version, QUIC_HANDSHAKE_FAILED,
814 "Packets buffered for too long",
QUICHE teama6ef0a62019-03-07 20:34:33 -0500815 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
816}
817
818void QuicDispatcher::ProcessBufferedChlos(size_t max_connections_to_create) {
819 // Reset the counter before starting creating connections.
820 new_sessions_allowed_per_event_loop_ = max_connections_to_create;
821 for (; new_sessions_allowed_per_event_loop_ > 0;
822 --new_sessions_allowed_per_event_loop_) {
dschinazi7b9278c2019-05-20 07:36:21 -0700823 QuicConnectionId server_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500824 BufferedPacketList packet_list =
dschinazi7b9278c2019-05-20 07:36:21 -0700825 buffered_packets_.DeliverPacketsForNextConnection(
826 &server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500827 const std::list<BufferedPacket>& packets = packet_list.buffered_packets;
828 if (packets.empty()) {
829 return;
830 }
dschinazi7b9278c2019-05-20 07:36:21 -0700831 QuicConnectionId original_connection_id = server_connection_id;
832 server_connection_id = MaybeReplaceServerConnectionId(server_connection_id,
833 packet_list.version);
wub89490e02019-12-12 12:45:58 -0800834 std::unique_ptr<QuicSession> session =
dschinazi7b9278c2019-05-20 07:36:21 -0700835 CreateQuicSession(server_connection_id, packets.front().peer_address,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500836 packet_list.alpn, packet_list.version);
dschinazi7b9278c2019-05-20 07:36:21 -0700837 if (original_connection_id != server_connection_id) {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700838 session->connection()->AddIncomingConnectionId(original_connection_id);
dschinazi5c030852019-07-11 15:45:53 -0700839 session->connection()->InstallInitialCrypters(original_connection_id);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700840 }
dschinazi7b9278c2019-05-20 07:36:21 -0700841 QUIC_DLOG(INFO) << "Created new session for " << server_connection_id;
dschinazi5c030852019-07-11 15:45:53 -0700842
wub89490e02019-12-12 12:45:58 -0800843 auto insertion_result = session_map_.insert(
844 std::make_pair(server_connection_id, std::move(session)));
845 QUIC_BUG_IF(!insertion_result.second)
846 << "Tried to add a session to session_map with existing connection id: "
847 << server_connection_id;
848 DeliverPacketsToSession(packets, insertion_result.first->second.get());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500849 }
850}
851
852bool QuicDispatcher::HasChlosBuffered() const {
853 return buffered_packets_.HasChlosBuffered();
854}
855
856bool QuicDispatcher::ShouldCreateOrBufferPacketForConnection(
fayang1ed1f762019-06-24 11:40:04 -0700857 const ReceivedPacketInfo& packet_info) {
dschinazi7b9278c2019-05-20 07:36:21 -0700858 QUIC_VLOG(1) << "Received packet from new connection "
fayang1ed1f762019-06-24 11:40:04 -0700859 << packet_info.destination_connection_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500860 return true;
861}
862
863// Return true if there is any packet buffered in the store.
dschinazi7b9278c2019-05-20 07:36:21 -0700864bool QuicDispatcher::HasBufferedPackets(QuicConnectionId server_connection_id) {
865 return buffered_packets_.HasBufferedPackets(server_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500866}
867
dschinazi7b9278c2019-05-20 07:36:21 -0700868void QuicDispatcher::OnBufferPacketFailure(
869 EnqueuePacketResult result,
870 QuicConnectionId server_connection_id) {
871 QUIC_DLOG(INFO) << "Fail to buffer packet on connection "
872 << server_connection_id << " because of " << result;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500873}
874
QUICHE teama6ef0a62019-03-07 20:34:33 -0500875QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() {
876 return new QuicTimeWaitListManager(writer_.get(), this, helper_->GetClock(),
877 alarm_factory_.get());
878}
879
fayang1ed1f762019-06-24 11:40:04 -0700880void QuicDispatcher::BufferEarlyPacket(const ReceivedPacketInfo& packet_info) {
881 bool is_new_connection = !buffered_packets_.HasBufferedPackets(
882 packet_info.destination_connection_id);
883 if (is_new_connection &&
884 !ShouldCreateOrBufferPacketForConnection(packet_info)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500885 return;
886 }
887
888 EnqueuePacketResult rs = buffered_packets_.EnqueuePacket(
fayang1ed1f762019-06-24 11:40:04 -0700889 packet_info.destination_connection_id,
890 packet_info.form != GOOGLE_QUIC_PACKET, packet_info.packet,
891 packet_info.self_address, packet_info.peer_address, /*is_chlo=*/false,
892 /*alpn=*/"", packet_info.version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500893 if (rs != EnqueuePacketResult::SUCCESS) {
fayang1ed1f762019-06-24 11:40:04 -0700894 OnBufferPacketFailure(rs, packet_info.destination_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500895 }
896}
897
fayang1ed1f762019-06-24 11:40:04 -0700898void QuicDispatcher::ProcessChlo(const std::string& alpn,
899 ReceivedPacketInfo* packet_info) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500900 if (!accept_new_connections_) {
901 // Don't any create new connection.
902 QUIC_CODE_COUNT(quic_reject_stop_accepting_new_connections);
903 StatelesslyTerminateConnection(
fayang1ed1f762019-06-24 11:40:04 -0700904 packet_info->destination_connection_id, packet_info->form,
dschinazi48ac9192019-07-31 00:07:26 -0700905 /*version_flag=*/true, packet_info->use_length_prefix,
906 packet_info->version, QUIC_HANDSHAKE_FAILED,
fayang1ed1f762019-06-24 11:40:04 -0700907 "Stop accepting new connections",
QUICHE teama6ef0a62019-03-07 20:34:33 -0500908 quic::QuicTimeWaitListManager::SEND_STATELESS_RESET);
909 // Time wait list will reject the packet correspondingly.
910 time_wait_list_manager()->ProcessPacket(
fayang1ed1f762019-06-24 11:40:04 -0700911 packet_info->self_address, packet_info->peer_address,
912 packet_info->destination_connection_id, packet_info->form,
913 GetPerPacketContext());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500914 return;
915 }
fayang1ed1f762019-06-24 11:40:04 -0700916 if (!buffered_packets_.HasBufferedPackets(
917 packet_info->destination_connection_id) &&
918 !ShouldCreateOrBufferPacketForConnection(*packet_info)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500919 return;
920 }
danzh88e3e052019-06-13 11:47:18 -0700921 if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500922 new_sessions_allowed_per_event_loop_ <= 0) {
923 // Can't create new session any more. Wait till next event loop.
fayang1ed1f762019-06-24 11:40:04 -0700924 QUIC_BUG_IF(buffered_packets_.HasChloForConnection(
925 packet_info->destination_connection_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500926 EnqueuePacketResult rs = buffered_packets_.EnqueuePacket(
fayang1ed1f762019-06-24 11:40:04 -0700927 packet_info->destination_connection_id,
928 packet_info->form != GOOGLE_QUIC_PACKET, packet_info->packet,
929 packet_info->self_address, packet_info->peer_address,
930 /*is_chlo=*/true, alpn, packet_info->version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500931 if (rs != EnqueuePacketResult::SUCCESS) {
fayang1ed1f762019-06-24 11:40:04 -0700932 OnBufferPacketFailure(rs, packet_info->destination_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500933 }
934 return;
935 }
QUICHE teamc65d1d12019-03-19 20:58:04 -0700936
fayang1ed1f762019-06-24 11:40:04 -0700937 QuicConnectionId original_connection_id =
938 packet_info->destination_connection_id;
939 packet_info->destination_connection_id = MaybeReplaceServerConnectionId(
940 original_connection_id, packet_info->version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500941 // Creates a new session and process all buffered packets for this connection.
wub89490e02019-12-12 12:45:58 -0800942 std::unique_ptr<QuicSession> session =
fayang1ed1f762019-06-24 11:40:04 -0700943 CreateQuicSession(packet_info->destination_connection_id,
944 packet_info->peer_address, alpn, packet_info->version);
945 if (original_connection_id != packet_info->destination_connection_id) {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700946 session->connection()->AddIncomingConnectionId(original_connection_id);
dschinazi5c030852019-07-11 15:45:53 -0700947 session->connection()->InstallInitialCrypters(original_connection_id);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700948 }
dschinazi7b9278c2019-05-20 07:36:21 -0700949 QUIC_DLOG(INFO) << "Created new session for "
fayang1ed1f762019-06-24 11:40:04 -0700950 << packet_info->destination_connection_id;
dschinazi5c030852019-07-11 15:45:53 -0700951
wub89490e02019-12-12 12:45:58 -0800952 auto insertion_result = session_map_.insert(std::make_pair(
953 packet_info->destination_connection_id, std::move(session)));
954 QUIC_BUG_IF(!insertion_result.second)
955 << "Tried to add a session to session_map with existing connection id: "
dschinazi5c030852019-07-11 15:45:53 -0700956 << packet_info->destination_connection_id;
wub89490e02019-12-12 12:45:58 -0800957 QuicSession* session_ptr = insertion_result.first->second.get();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500958 std::list<BufferedPacket> packets =
fayang1ed1f762019-06-24 11:40:04 -0700959 buffered_packets_.DeliverPackets(packet_info->destination_connection_id)
dschinazi7b9278c2019-05-20 07:36:21 -0700960 .buffered_packets;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500961 // Process CHLO at first.
wub89490e02019-12-12 12:45:58 -0800962 session_ptr->ProcessUdpPacket(packet_info->self_address,
963 packet_info->peer_address, packet_info->packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500964 // Deliver queued-up packets in the same order as they arrived.
965 // Do this even when flag is off because there might be still some packets
966 // buffered in the store before flag is turned off.
wub89490e02019-12-12 12:45:58 -0800967 DeliverPacketsToSession(packets, session_ptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500968 --new_sessions_allowed_per_event_loop_;
969}
970
QUICHE teama6ef0a62019-03-07 20:34:33 -0500971bool QuicDispatcher::ShouldDestroySessionAsynchronously() {
972 return true;
973}
974
975void QuicDispatcher::SetLastError(QuicErrorCode error) {
976 last_error_ = error;
977}
978
fayang91475c42019-06-19 08:04:26 -0700979bool QuicDispatcher::OnFailedToDispatchPacket(
fayang1ed1f762019-06-24 11:40:04 -0700980 const ReceivedPacketInfo& /*packet_info*/) {
fayang91475c42019-06-19 08:04:26 -0700981 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500982}
983
QUICHE teama6ef0a62019-03-07 20:34:33 -0500984const QuicTransportVersionVector&
985QuicDispatcher::GetSupportedTransportVersions() {
986 return version_manager_->GetSupportedTransportVersions();
987}
988
989const ParsedQuicVersionVector& QuicDispatcher::GetSupportedVersions() {
990 return version_manager_->GetSupportedVersions();
991}
992
993void QuicDispatcher::DeliverPacketsToSession(
994 const std::list<BufferedPacket>& packets,
995 QuicSession* session) {
996 for (const BufferedPacket& packet : packets) {
997 session->ProcessUdpPacket(packet.self_address, packet.peer_address,
998 *(packet.packet));
999 }
1000}
1001
fayangccbab732019-05-13 10:11:25 -07001002bool QuicDispatcher::IsSupportedVersion(const ParsedQuicVersion version) {
fayangccbab732019-05-13 10:11:25 -07001003 for (const ParsedQuicVersion& supported_version :
1004 version_manager_->GetSupportedVersions()) {
1005 if (version == supported_version) {
1006 return true;
1007 }
1008 }
1009 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001010}
1011
fayangd057e662019-07-10 13:29:41 -07001012void QuicDispatcher::MaybeResetPacketsWithNoVersion(
1013 const ReceivedPacketInfo& packet_info) {
1014 DCHECK(!packet_info.version_flag);
1015 const size_t MinValidPacketLength =
1016 kPacketHeaderTypeSize + expected_server_connection_id_length_ +
1017 PACKET_1BYTE_PACKET_NUMBER + /*payload size=*/1 + /*tag size=*/12;
1018 if (packet_info.packet.length() < MinValidPacketLength) {
1019 // The packet size is too small.
1020 QUIC_CODE_COUNT(drop_too_small_packets);
1021 return;
1022 }
1023 // TODO(fayang): Consider rate limiting reset packets if reset packet size >
1024 // packet_length.
1025
1026 time_wait_list_manager()->SendPublicReset(
1027 packet_info.self_address, packet_info.peer_address,
1028 packet_info.destination_connection_id,
1029 packet_info.form != GOOGLE_QUIC_PACKET, GetPerPacketContext());
1030}
1031
QUICHE teama6ef0a62019-03-07 20:34:33 -05001032} // namespace quic