QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 7 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #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" |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #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" |
dschinazi | 76992e7 | 2019-07-17 20:40:40 -0700 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | |
| 27 | namespace quic { |
| 28 | |
| 29 | typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; |
| 30 | typedef QuicBufferedPacketStore::BufferedPacketList BufferedPacketList; |
| 31 | typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // An alarm that informs the QuicDispatcher to delete old sessions. |
| 36 | class 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. |
| 52 | class 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 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 75 | void OnUnrecoverableError(QuicErrorCode /*error*/, |
| 76 | const std::string& /*error_details*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | // QuicStreamFrameDataProducer |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 79 | WriteStreamDataResult WriteStreamData(QuicStreamId /*id*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 80 | 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 | } |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 88 | bool WriteCryptoData(EncryptionLevel /*level*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | 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. |
| 109 | class StatelessConnectionTerminator { |
| 110 | public: |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 111 | StatelessConnectionTerminator(QuicConnectionId server_connection_id, |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 112 | const ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | QuicConnectionHelperInterface* helper, |
| 114 | QuicTimeWaitListManager* time_wait_list_manager) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 115 | : server_connection_id_(server_connection_id), |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 116 | framer_(ParsedQuicVersionVector{version}, |
| 117 | /*unused*/ QuicTime::Zero(), |
| 118 | Perspective::IS_SERVER, |
| 119 | /*unused*/ kQuicDefaultConnectionIdLength), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 120 | collector_(helper->GetStreamSendBufferAllocator()), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 121 | creator_(server_connection_id, &framer_, &collector_), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | time_wait_list_manager_(time_wait_list_manager) { |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 123 | framer_.set_data_producer(&collector_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ~StatelessConnectionTerminator() { |
| 127 | // Clear framer's producer. |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 128 | framer_.set_data_producer(nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | } |
| 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, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 134 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | bool ietf_quic) { |
fkastenholz | 591814c | 2019-09-06 12:11:46 -0700 | [diff] [blame^] | 136 | QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame( |
| 137 | framer_.transport_version(), error_code, error_details, |
| 138 | /*transport_close_frame_type=*/0); |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 139 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 140 | 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( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 148 | server_connection_id_, ietf_quic, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 150 | quic::ENCRYPTION_INITIAL, collector_.packets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 151 | } |
| 152 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 153 | private: |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 154 | QuicConnectionId server_connection_id_; |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 155 | QuicFramer framer_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | // 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. |
| 163 | class ChloAlpnExtractor : public ChloExtractor::Delegate { |
| 164 | public: |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 165 | void OnChlo(QuicTransportVersion /*version*/, |
| 166 | QuicConnectionId /*server_connection_id*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 167 | const CryptoHandshakeMessage& chlo) override { |
| 168 | QuicStringPiece alpn_value; |
| 169 | if (chlo.GetStringPiece(kALPN, &alpn_value)) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 170 | alpn_ = std::string(alpn_value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 174 | std::string&& ConsumeAlpn() { return std::move(alpn_); } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 175 | |
| 176 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 177 | std::string alpn_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | }; |
| 179 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 180 | } // namespace |
| 181 | |
| 182 | QuicDispatcher::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, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 189 | uint8_t expected_server_connection_id_length) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 190 | : 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 200 | version_manager_(version_manager), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | last_error_(QUIC_NO_ERROR), |
| 202 | new_sessions_allowed_per_event_loop_(0u), |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 203 | accept_new_connections_(true), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 204 | allow_short_initial_server_connection_ids_(false), |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 205 | expected_server_connection_id_length_( |
| 206 | expected_server_connection_id_length), |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 207 | should_update_expected_server_connection_id_length_(false) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 208 | |
| 209 | QuicDispatcher::~QuicDispatcher() { |
| 210 | session_map_.clear(); |
| 211 | closed_session_list_.clear(); |
| 212 | } |
| 213 | |
| 214 | void QuicDispatcher::InitializeWithWriter(QuicPacketWriter* writer) { |
| 215 | DCHECK(writer_ == nullptr); |
| 216 | writer_.reset(writer); |
| 217 | time_wait_list_manager_.reset(CreateQuicTimeWaitListManager()); |
| 218 | } |
| 219 | |
| 220 | void QuicDispatcher::ProcessPacket(const QuicSocketAddress& self_address, |
| 221 | const QuicSocketAddress& peer_address, |
| 222 | const QuicReceivedPacket& packet) { |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 223 | QUIC_DVLOG(2) << "Dispatcher received encrypted " << packet.length() |
| 224 | << " bytes:" << std::endl |
| 225 | << QuicTextUtils::HexDump( |
| 226 | QuicStringPiece(packet.data(), packet.length())); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 227 | ReceivedPacketInfo packet_info(self_address, peer_address, packet); |
fayang | 1f12350 | 2019-05-14 08:05:16 -0700 | [diff] [blame] | 228 | std::string detailed_error; |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 229 | 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 | } |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 248 | if (error != QUIC_NO_ERROR) { |
| 249 | // Packet has framing error. |
| 250 | SetLastError(error); |
| 251 | QUIC_DLOG(ERROR) << detailed_error; |
| 252 | return; |
| 253 | } |
dschinazi | 243eabc | 2019-08-05 16:15:29 -0700 | [diff] [blame] | 254 | if (!GetQuicReloadableFlag(quic_use_parse_public_header)) { |
| 255 | packet_info.version = ParseQuicVersionLabel(packet_info.version_label); |
| 256 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 257 | if (packet_info.destination_connection_id.length() != |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 258 | expected_server_connection_id_length_ && |
| 259 | !should_update_expected_server_connection_id_length_ && |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 260 | !QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 261 | packet_info.version.transport_version)) { |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 262 | SetLastError(QUIC_INVALID_PACKET_HEADER); |
| 263 | QUIC_DLOG(ERROR) << "Invalid Connection Id Length"; |
| 264 | return; |
| 265 | } |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 266 | |
| 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 | |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 286 | if (should_update_expected_server_connection_id_length_) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 287 | expected_server_connection_id_length_ = |
| 288 | packet_info.destination_connection_id.length(); |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 289 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 290 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 291 | if (MaybeDispatchPacket(packet_info)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 292 | // Packet has been dropped or successfully dispatched, stop processing. |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 295 | ProcessHeader(&packet_info); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 296 | } |
| 297 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 298 | QuicConnectionId QuicDispatcher::MaybeReplaceServerConnectionId( |
| 299 | QuicConnectionId server_connection_id, |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 300 | ParsedQuicVersion version) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 301 | if (server_connection_id.length() == expected_server_connection_id_length_) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 302 | return server_connection_id; |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 303 | } |
| 304 | DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 305 | version.transport_version)); |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 306 | |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 307 | QuicConnectionId new_connection_id = |
wub | 662a3d6 | 2019-08-16 14:10:50 -0700 | [diff] [blame] | 308 | GenerateNewServerConnectionId(version, server_connection_id); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 309 | DCHECK_EQ(expected_server_connection_id_length_, new_connection_id.length()); |
dschinazi | 28c1bf3 | 2019-08-19 11:54:46 -0700 | [diff] [blame] | 310 | |
| 311 | // Verify that GenerateNewServerConnectionId is deterministic. |
| 312 | DCHECK_EQ(new_connection_id, |
| 313 | GenerateNewServerConnectionId(version, server_connection_id)); |
| 314 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 315 | QUIC_DLOG(INFO) << "Replacing incoming connection ID " << server_connection_id |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 316 | << " with " << new_connection_id; |
| 317 | return new_connection_id; |
| 318 | } |
| 319 | |
wub | 662a3d6 | 2019-08-16 14:10:50 -0700 | [diff] [blame] | 320 | QuicConnectionId QuicDispatcher::GenerateNewServerConnectionId( |
| 321 | ParsedQuicVersion /*version*/, |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 322 | QuicConnectionId connection_id) const { |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 323 | return QuicUtils::CreateReplacementConnectionId(connection_id); |
wub | 662a3d6 | 2019-08-16 14:10:50 -0700 | [diff] [blame] | 324 | } |
| 325 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 326 | bool QuicDispatcher::MaybeDispatchPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 327 | const ReceivedPacketInfo& packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 328 | // 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. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 331 | if (packet_info.peer_address.port() == 0) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 332 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 333 | } |
| 334 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 335 | QuicConnectionId server_connection_id = packet_info.destination_connection_id; |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 336 | |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 337 | // 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. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 342 | if (server_connection_id.length() < kQuicMinimumInitialConnectionIdLength && |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 343 | server_connection_id.length() < expected_server_connection_id_length_ && |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 344 | !allow_short_initial_server_connection_ids_) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 345 | DCHECK(packet_info.version_flag); |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 346 | DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 347 | packet_info.version.transport_version)); |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 348 | QUIC_DLOG(INFO) << "Packet with short destination connection ID " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 349 | << server_connection_id << " expected " |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 350 | << static_cast<int>(expected_server_connection_id_length_); |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 351 | if (!GetQuicReloadableFlag(quic_drop_invalid_small_initial_connection_id)) { |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 352 | // 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( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 357 | server_connection_id, packet_info.form, packet_info.version_flag, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 358 | packet_info.use_length_prefix, packet_info.version, |
| 359 | QUIC_HANDSHAKE_FAILED, "Reject connection", |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 360 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 361 | |
| 362 | DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait( |
| 363 | server_connection_id)); |
| 364 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 365 | packet_info.self_address, packet_info.peer_address, |
| 366 | server_connection_id, packet_info.form, GetPerPacketContext()); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 367 | |
| 368 | buffered_packets_.DiscardPackets(server_connection_id); |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 369 | } else { |
| 370 | QUIC_RELOADABLE_FLAG_COUNT(quic_drop_invalid_small_initial_connection_id); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 371 | // Drop the packet silently. |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 372 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 373 | return true; |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 374 | } |
| 375 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 376 | // Packets with connection IDs for active connections are processed |
| 377 | // immediately. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 378 | auto it = session_map_.find(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 379 | if (it != session_map_.end()) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 380 | DCHECK(!buffered_packets_.HasBufferedPackets(server_connection_id)); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 381 | it->second->ProcessUdpPacket(packet_info.self_address, |
| 382 | packet_info.peer_address, packet_info.packet); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 383 | return true; |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 384 | } 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | } |
| 400 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 401 | if (buffered_packets_.HasChloForConnection(server_connection_id)) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 402 | BufferEarlyPacket(packet_info); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 403 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 404 | } |
| 405 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 406 | if (OnFailedToDispatchPacket(packet_info)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 407 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 408 | } |
| 409 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 410 | if (time_wait_list_manager_->IsConnectionIdInTimeWait(server_connection_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 411 | // This connection ID is already in time-wait state. |
| 412 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 413 | packet_info.self_address, packet_info.peer_address, |
| 414 | packet_info.destination_connection_id, packet_info.form, |
| 415 | GetPerPacketContext()); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 416 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 417 | } |
| 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. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 423 | if (packet_info.version_flag) { |
| 424 | if (!IsSupportedVersion(packet_info.version)) { |
| 425 | if (ShouldCreateSessionForUnknownVersion(packet_info.version_label)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 426 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 427 | } |
| 428 | if (!crypto_config()->validate_chlo_size() || |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 429 | packet_info.packet.length() >= kMinPacketSizeForVersionNegotiation) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 430 | // Since the version is not supported, send a version negotiation |
| 431 | // packet and stop processing the current packet. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 432 | QuicConnectionId client_connection_id = |
| 433 | packet_info.source_connection_id; |
dschinazi | 30ab6db | 2019-08-13 14:43:32 -0700 | [diff] [blame] | 434 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 445 | time_wait_list_manager()->SendVersionNegotiationPacket( |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 446 | server_connection_id, client_connection_id, |
dschinazi | 30ab6db | 2019-08-13 14:43:32 -0700 | [diff] [blame] | 447 | packet_info.form != GOOGLE_QUIC_PACKET, use_length_prefix, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 448 | GetSupportedVersions(), packet_info.self_address, |
| 449 | packet_info.peer_address, GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 450 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 451 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 452 | } |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 453 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 454 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 458 | void QuicDispatcher::ProcessHeader(ReceivedPacketInfo* packet_info) { |
| 459 | QuicConnectionId server_connection_id = |
| 460 | packet_info->destination_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 461 | // Packet's connection ID is unknown. Apply the validity checks. |
wub | 2a5670f | 2019-06-13 13:39:16 -0700 | [diff] [blame] | 462 | // TODO(wub): Determine the fate completely in ValidityChecks, then call |
| 463 | // ProcessUnauthenticatedHeaderFate in one place. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 464 | QuicPacketFate fate = ValidityChecks(*packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 465 | ChloAlpnExtractor alpn_extractor; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | switch (fate) { |
dschinazi | 76992e7 | 2019-07-17 20:40:40 -0700 | [diff] [blame] | 467 | case kFateProcess: { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 468 | if (packet_info->version.handshake_protocol == PROTOCOL_TLS1_3) { |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 469 | // TODO(nharper): Support buffering non-ClientHello packets when using |
| 470 | // TLS. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 471 | ProcessChlo(/*alpn=*/"", packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 472 | break; |
| 473 | } |
dschinazi | 76992e7 | 2019-07-17 20:40:40 -0700 | [diff] [blame] | 474 | 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 | } |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 486 | if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) && |
dschinazi | 76992e7 | 2019-07-17 20:40:40 -0700 | [diff] [blame] | 487 | !ChloExtractor::Extract(packet_info->packet, chlo_extractor_versions, |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 488 | config_->create_session_tag_indicators(), |
| 489 | &alpn_extractor, |
| 490 | server_connection_id.length())) { |
| 491 | // Buffer non-CHLO packets. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 492 | BufferEarlyPacket(*packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 493 | break; |
| 494 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 495 | ProcessChlo(alpn_extractor.ConsumeAlpn(), packet_info); |
dschinazi | 76992e7 | 2019-07-17 20:40:40 -0700 | [diff] [blame] | 496 | } break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 497 | case kFateTimeWait: |
wub | 0a4b9c5 | 2019-05-28 13:18:58 -0700 | [diff] [blame] | 498 | // 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( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 504 | server_connection_id, packet_info->form, packet_info->version_flag, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 505 | packet_info->use_length_prefix, packet_info->version, |
| 506 | QUIC_HANDSHAKE_FAILED, "Reject connection", |
wub | 0a4b9c5 | 2019-05-28 13:18:58 -0700 | [diff] [blame] | 507 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 508 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 509 | DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait( |
| 510 | server_connection_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 511 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 512 | packet_info->self_address, packet_info->peer_address, |
| 513 | server_connection_id, packet_info->form, GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 514 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 515 | buffered_packets_.DiscardPackets(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 516 | break; |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 517 | case kFateDrop: |
| 518 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | QuicDispatcher::QuicPacketFate QuicDispatcher::ValidityChecks( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 523 | const ReceivedPacketInfo& packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 524 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 529 | // 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. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 533 | if (!packet_info.version_flag) { |
dschinazi | 5b236be | 2019-08-19 14:55:22 -0700 | [diff] [blame] | 534 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 565 | QUIC_DLOG(INFO) |
| 566 | << "Packet without version arrived for unknown connection ID " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 567 | << packet_info.destination_connection_id; |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 568 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 573 | return kFateTimeWait; |
| 574 | } |
| 575 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 576 | // Let the connection parse and validate packet number. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 577 | return kFateProcess; |
| 578 | } |
| 579 | |
| 580 | void QuicDispatcher::CleanUpSession(SessionMap::iterator it, |
| 581 | QuicConnection* connection, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 582 | ConnectionCloseSource /*source*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 583 | write_blocked_list_.erase(connection); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 584 | 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; |
fayang | 51c2373 | 2019-06-24 06:59:55 -0700 | [diff] [blame] | 589 | } else { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 590 | if (!connection->IsHandshakeConfirmed()) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 591 | if (!VersionHasIetfInvariantHeader(connection->transport_version())) { |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 592 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 596 | 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( |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 600 | connection->connection_id(), |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 601 | VersionHasIetfInvariantHeader(connection->transport_version()) |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 602 | ? IETF_QUIC_LONG_HEADER_PACKET |
| 603 | : GOOGLE_QUIC_PACKET, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 604 | /*version_flag=*/true, |
| 605 | connection->version().HasLengthPrefixedConnectionIds(), |
| 606 | connection->version(), QUIC_HANDSHAKE_FAILED, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 607 | "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( |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 618 | it->first, VersionHasIetfInvariantHeader(connection->transport_version()), |
| 619 | action, connection->encryption_level(), |
| 620 | connection->termination_packets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 621 | session_map_.erase(it); |
| 622 | } |
| 623 | |
| 624 | void QuicDispatcher::StopAcceptingNewConnections() { |
| 625 | accept_new_connections_ = false; |
| 626 | } |
| 627 | |
| 628 | std::unique_ptr<QuicPerPacketContext> QuicDispatcher::GetPerPacketContext() |
| 629 | const { |
| 630 | return nullptr; |
| 631 | } |
| 632 | |
| 633 | void QuicDispatcher::DeleteSessions() { |
QUICHE team | aa1d6a8 | 2019-03-13 09:14:13 -0700 | [diff] [blame] | 634 | if (!write_blocked_list_.empty()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 635 | 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 | |
| 644 | void 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 | |
| 672 | bool QuicDispatcher::HasPendingWrites() const { |
| 673 | return !write_blocked_list_.empty(); |
| 674 | } |
| 675 | |
| 676 | void 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 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 689 | void QuicDispatcher::OnConnectionClosed(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 690 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 691 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 692 | ConnectionCloseSource source) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 693 | auto it = session_map_.find(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 694 | if (it == session_map_.end()) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 695 | QUIC_BUG << "ConnectionId " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 696 | << " 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) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 703 | << "Closing connection (" << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 704 | << ") 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 | } |
wub | 5f64ec4 | 2019-06-06 07:31:19 -0700 | [diff] [blame] | 717 | CleanUpSession(it, connection, source); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void 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 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 735 | void QuicDispatcher::OnRstStreamReceived(const QuicRstStreamFrame& /*frame*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 736 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 737 | void QuicDispatcher::OnStopSendingReceived( |
| 738 | const QuicStopSendingFrame& /*frame*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 739 | |
| 740 | void QuicDispatcher::OnConnectionAddedToTimeWaitList( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 741 | QuicConnectionId server_connection_id) { |
| 742 | QUIC_DLOG(INFO) << "Connection " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 743 | << " added to time wait list."; |
| 744 | } |
| 745 | |
| 746 | void QuicDispatcher::StatelesslyTerminateConnection( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 747 | QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 748 | PacketHeaderFormat format, |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 749 | bool version_flag, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 750 | bool use_length_prefix, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 751 | ParsedQuicVersion version, |
| 752 | QuicErrorCode error_code, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 753 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 754 | QuicTimeWaitListManager::TimeWaitAction action) { |
fayang | 51c2373 | 2019-06-24 06:59:55 -0700 | [diff] [blame] | 755 | if (format != IETF_QUIC_LONG_HEADER_PACKET && !version_flag) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 756 | QUIC_DVLOG(1) << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 757 | << " 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( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 761 | server_connection_id, format != GOOGLE_QUIC_PACKET, action, |
| 762 | ENCRYPTION_INITIAL, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 763 | return; |
| 764 | } |
| 765 | |
| 766 | // If the version is known and supported by framer, send a connection close. |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 767 | if (IsSupportedVersion(version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 768 | QUIC_DVLOG(1) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 769 | << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 770 | << " based on an ietf-long packet, which has a supported version:" |
| 771 | << version << ", error_code:" << error_code |
| 772 | << ", error_details:" << error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 773 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 774 | StatelessConnectionTerminator terminator(server_connection_id, version, |
| 775 | helper_.get(), |
| 776 | time_wait_list_manager_.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 777 | // This also adds the connection to time wait list. |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 778 | terminator.CloseConnection(error_code, error_details, |
| 779 | format != GOOGLE_QUIC_PACKET); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 780 | return; |
| 781 | } |
| 782 | |
| 783 | QUIC_DVLOG(1) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 784 | << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 785 | << " 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( |
dschinazi | b417d60 | 2019-05-29 13:08:45 -0700 | [diff] [blame] | 792 | server_connection_id, EmptyQuicConnectionId(), |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 793 | /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, use_length_prefix, |
dschinazi | 1ac22cc | 2019-06-25 11:47:50 -0700 | [diff] [blame] | 794 | /*versions=*/{})); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 795 | time_wait_list_manager()->AddConnectionIdToTimeWait( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 796 | server_connection_id, /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 797 | QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 798 | &termination_packets); |
| 799 | } |
| 800 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 801 | bool QuicDispatcher::ShouldCreateSessionForUnknownVersion( |
| 802 | QuicVersionLabel /*version_label*/) { |
| 803 | return false; |
| 804 | } |
| 805 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 806 | void QuicDispatcher::OnExpiredPackets( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 807 | QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 808 | BufferedPacketList early_arrived_packets) { |
| 809 | QUIC_CODE_COUNT(quic_reject_buffered_packets_expired); |
| 810 | StatelesslyTerminateConnection( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 811 | server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 812 | early_arrived_packets.ietf_quic ? IETF_QUIC_LONG_HEADER_PACKET |
| 813 | : GOOGLE_QUIC_PACKET, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 814 | /*version_flag=*/true, |
| 815 | early_arrived_packets.version.HasLengthPrefixedConnectionIds(), |
| 816 | early_arrived_packets.version, QUIC_HANDSHAKE_FAILED, |
| 817 | "Packets buffered for too long", |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 818 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 819 | } |
| 820 | |
| 821 | void 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_) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 826 | QuicConnectionId server_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 827 | BufferedPacketList packet_list = |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 828 | buffered_packets_.DeliverPacketsForNextConnection( |
| 829 | &server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 830 | const std::list<BufferedPacket>& packets = packet_list.buffered_packets; |
| 831 | if (packets.empty()) { |
| 832 | return; |
| 833 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 834 | QuicConnectionId original_connection_id = server_connection_id; |
| 835 | server_connection_id = MaybeReplaceServerConnectionId(server_connection_id, |
| 836 | packet_list.version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 837 | QuicSession* session = |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 838 | CreateQuicSession(server_connection_id, packets.front().peer_address, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 839 | packet_list.alpn, packet_list.version); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 840 | if (original_connection_id != server_connection_id) { |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 841 | session->connection()->AddIncomingConnectionId(original_connection_id); |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 842 | session->connection()->InstallInitialCrypters(original_connection_id); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 843 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 844 | QUIC_DLOG(INFO) << "Created new session for " << server_connection_id; |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 845 | |
| 846 | DCHECK(session_map_.find(server_connection_id) == session_map_.end()) |
| 847 | << "Tried to add session map existing entry " << server_connection_id; |
| 848 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 849 | session_map_.insert( |
| 850 | std::make_pair(server_connection_id, QuicWrapUnique(session))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 851 | DeliverPacketsToSession(packets, session); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | bool QuicDispatcher::HasChlosBuffered() const { |
| 856 | return buffered_packets_.HasChlosBuffered(); |
| 857 | } |
| 858 | |
| 859 | bool QuicDispatcher::ShouldCreateOrBufferPacketForConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 860 | const ReceivedPacketInfo& packet_info) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 861 | QUIC_VLOG(1) << "Received packet from new connection " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 862 | << packet_info.destination_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 863 | return true; |
| 864 | } |
| 865 | |
| 866 | // Return true if there is any packet buffered in the store. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 867 | bool QuicDispatcher::HasBufferedPackets(QuicConnectionId server_connection_id) { |
| 868 | return buffered_packets_.HasBufferedPackets(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 869 | } |
| 870 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 871 | void 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 876 | } |
| 877 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 878 | QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { |
| 879 | return new QuicTimeWaitListManager(writer_.get(), this, helper_->GetClock(), |
| 880 | alarm_factory_.get()); |
| 881 | } |
| 882 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 883 | void 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 888 | return; |
| 889 | } |
| 890 | |
| 891 | EnqueuePacketResult rs = buffered_packets_.EnqueuePacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 892 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 896 | if (rs != EnqueuePacketResult::SUCCESS) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 897 | OnBufferPacketFailure(rs, packet_info.destination_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 901 | void QuicDispatcher::ProcessChlo(const std::string& alpn, |
| 902 | ReceivedPacketInfo* packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 903 | if (!accept_new_connections_) { |
| 904 | // Don't any create new connection. |
| 905 | QUIC_CODE_COUNT(quic_reject_stop_accepting_new_connections); |
| 906 | StatelesslyTerminateConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 907 | packet_info->destination_connection_id, packet_info->form, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 908 | /*version_flag=*/true, packet_info->use_length_prefix, |
| 909 | packet_info->version, QUIC_HANDSHAKE_FAILED, |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 910 | "Stop accepting new connections", |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 911 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 912 | // Time wait list will reject the packet correspondingly. |
| 913 | time_wait_list_manager()->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 914 | packet_info->self_address, packet_info->peer_address, |
| 915 | packet_info->destination_connection_id, packet_info->form, |
| 916 | GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 917 | return; |
| 918 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 919 | if (!buffered_packets_.HasBufferedPackets( |
| 920 | packet_info->destination_connection_id) && |
| 921 | !ShouldCreateOrBufferPacketForConnection(*packet_info)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 922 | return; |
| 923 | } |
danzh | 88e3e05 | 2019-06-13 11:47:18 -0700 | [diff] [blame] | 924 | if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 925 | new_sessions_allowed_per_event_loop_ <= 0) { |
| 926 | // Can't create new session any more. Wait till next event loop. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 927 | QUIC_BUG_IF(buffered_packets_.HasChloForConnection( |
| 928 | packet_info->destination_connection_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 929 | EnqueuePacketResult rs = buffered_packets_.EnqueuePacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 930 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 934 | if (rs != EnqueuePacketResult::SUCCESS) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 935 | OnBufferPacketFailure(rs, packet_info->destination_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 936 | } |
| 937 | return; |
| 938 | } |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 939 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 940 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 944 | // Creates a new session and process all buffered packets for this connection. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 945 | QuicSession* session = |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 946 | 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 team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 949 | session->connection()->AddIncomingConnectionId(original_connection_id); |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 950 | session->connection()->InstallInitialCrypters(original_connection_id); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 951 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 952 | QUIC_DLOG(INFO) << "Created new session for " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 953 | << packet_info->destination_connection_id; |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 954 | |
| 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 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 960 | session_map_.insert(std::make_pair(packet_info->destination_connection_id, |
| 961 | QuicWrapUnique(session))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 962 | std::list<BufferedPacket> packets = |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 963 | buffered_packets_.DeliverPackets(packet_info->destination_connection_id) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 964 | .buffered_packets; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 965 | // Process CHLO at first. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 966 | session->ProcessUdpPacket(packet_info->self_address, |
| 967 | packet_info->peer_address, packet_info->packet); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 968 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 975 | bool QuicDispatcher::ShouldDestroySessionAsynchronously() { |
| 976 | return true; |
| 977 | } |
| 978 | |
| 979 | void QuicDispatcher::SetLastError(QuicErrorCode error) { |
| 980 | last_error_ = error; |
| 981 | } |
| 982 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 983 | bool QuicDispatcher::OnFailedToDispatchPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 984 | const ReceivedPacketInfo& /*packet_info*/) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 985 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 986 | } |
| 987 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 988 | const QuicTransportVersionVector& |
| 989 | QuicDispatcher::GetSupportedTransportVersions() { |
| 990 | return version_manager_->GetSupportedTransportVersions(); |
| 991 | } |
| 992 | |
| 993 | const ParsedQuicVersionVector& QuicDispatcher::GetSupportedVersions() { |
| 994 | return version_manager_->GetSupportedVersions(); |
| 995 | } |
| 996 | |
| 997 | void 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 | |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 1006 | bool QuicDispatcher::IsSupportedVersion(const ParsedQuicVersion version) { |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 1007 | for (const ParsedQuicVersion& supported_version : |
| 1008 | version_manager_->GetSupportedVersions()) { |
| 1009 | if (version == supported_version) { |
| 1010 | return true; |
| 1011 | } |
| 1012 | } |
| 1013 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 1016 | void 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1036 | } // namespace quic |