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" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 22 | #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] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 24 | #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] | 25 | |
| 26 | namespace quic { |
| 27 | |
| 28 | typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; |
| 29 | typedef QuicBufferedPacketStore::BufferedPacketList BufferedPacketList; |
| 30 | typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // An alarm that informs the QuicDispatcher to delete old sessions. |
| 35 | class DeleteSessionsAlarm : public QuicAlarm::Delegate { |
| 36 | public: |
| 37 | explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) |
| 38 | : dispatcher_(dispatcher) {} |
| 39 | DeleteSessionsAlarm(const DeleteSessionsAlarm&) = delete; |
| 40 | DeleteSessionsAlarm& operator=(const DeleteSessionsAlarm&) = delete; |
| 41 | |
| 42 | void OnAlarm() override { dispatcher_->DeleteSessions(); } |
| 43 | |
| 44 | private: |
| 45 | // Not owned. |
| 46 | QuicDispatcher* dispatcher_; |
| 47 | }; |
| 48 | |
| 49 | // Collects packets serialized by a QuicPacketCreator in order |
| 50 | // to be handed off to the time wait list manager. |
| 51 | class PacketCollector : public QuicPacketCreator::DelegateInterface, |
| 52 | public QuicStreamFrameDataProducer { |
| 53 | public: |
| 54 | explicit PacketCollector(QuicBufferAllocator* allocator) |
| 55 | : send_buffer_(allocator) {} |
| 56 | ~PacketCollector() override = default; |
| 57 | |
| 58 | // QuicPacketCreator::DelegateInterface methods: |
| 59 | void OnSerializedPacket(SerializedPacket* serialized_packet) override { |
| 60 | // Make a copy of the serialized packet to send later. |
| 61 | packets_.emplace_back( |
| 62 | new QuicEncryptedPacket(CopyBuffer(*serialized_packet), |
| 63 | serialized_packet->encrypted_length, true)); |
| 64 | serialized_packet->encrypted_buffer = nullptr; |
| 65 | DeleteFrames(&(serialized_packet->retransmittable_frames)); |
| 66 | serialized_packet->retransmittable_frames.clear(); |
| 67 | } |
| 68 | |
| 69 | char* GetPacketBuffer() override { |
| 70 | // Let QuicPacketCreator to serialize packets on stack buffer. |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 74 | void OnUnrecoverableError(QuicErrorCode /*error*/, |
| 75 | const std::string& /*error_details*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | // QuicStreamFrameDataProducer |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 78 | WriteStreamDataResult WriteStreamData(QuicStreamId /*id*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | QuicStreamOffset offset, |
| 80 | QuicByteCount data_length, |
| 81 | QuicDataWriter* writer) override { |
| 82 | if (send_buffer_.WriteStreamData(offset, data_length, writer)) { |
| 83 | return WRITE_SUCCESS; |
| 84 | } |
| 85 | return WRITE_FAILED; |
| 86 | } |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 87 | bool WriteCryptoData(EncryptionLevel /*level*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | QuicStreamOffset offset, |
| 89 | QuicByteCount data_length, |
| 90 | QuicDataWriter* writer) override { |
| 91 | return send_buffer_.WriteStreamData(offset, data_length, writer); |
| 92 | } |
| 93 | |
| 94 | std::vector<std::unique_ptr<QuicEncryptedPacket>>* packets() { |
| 95 | return &packets_; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | std::vector<std::unique_ptr<QuicEncryptedPacket>> packets_; |
| 100 | // This is only needed until the packets are encrypted. Once packets are |
| 101 | // encrypted, the stream data is no longer required. |
| 102 | QuicStreamSendBuffer send_buffer_; |
| 103 | }; |
| 104 | |
| 105 | // Helper for statelessly closing connections by generating the |
| 106 | // correct termination packets and adding the connection to the time wait |
| 107 | // list manager. |
| 108 | class StatelessConnectionTerminator { |
| 109 | public: |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 110 | StatelessConnectionTerminator(QuicConnectionId server_connection_id, |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 111 | const ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | QuicConnectionHelperInterface* helper, |
| 113 | QuicTimeWaitListManager* time_wait_list_manager) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 114 | : server_connection_id_(server_connection_id), |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 115 | framer_(ParsedQuicVersionVector{version}, |
| 116 | /*unused*/ QuicTime::Zero(), |
| 117 | Perspective::IS_SERVER, |
| 118 | /*unused*/ kQuicDefaultConnectionIdLength), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | collector_(helper->GetStreamSendBufferAllocator()), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 120 | creator_(server_connection_id, &framer_, &collector_), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | time_wait_list_manager_(time_wait_list_manager) { |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 122 | framer_.set_data_producer(&collector_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ~StatelessConnectionTerminator() { |
| 126 | // Clear framer's producer. |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 127 | framer_.set_data_producer(nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Generates a packet containing a CONNECTION_CLOSE frame specifying |
| 131 | // |error_code| and |error_details| and add the connection to time wait. |
| 132 | void CloseConnection(QuicErrorCode error_code, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 133 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | bool ietf_quic) { |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 135 | QuicConnectionCloseFrame* frame = |
| 136 | new QuicConnectionCloseFrame(error_code, error_details); |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 137 | if (VersionHasIetfQuicFrames(framer_.transport_version())) { |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 138 | frame->close_type = IETF_QUIC_TRANSPORT_CONNECTION_CLOSE; |
| 139 | } |
| 140 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | if (!creator_.AddSavedFrame(QuicFrame(frame), NOT_RETRANSMISSION)) { |
| 142 | QUIC_BUG << "Unable to add frame to an empty packet"; |
| 143 | delete frame; |
| 144 | return; |
| 145 | } |
| 146 | creator_.Flush(); |
| 147 | DCHECK_EQ(1u, collector_.packets()->size()); |
| 148 | time_wait_list_manager_->AddConnectionIdToTimeWait( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 149 | server_connection_id_, ietf_quic, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 150 | QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 151 | quic::ENCRYPTION_INITIAL, collector_.packets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | } |
| 153 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | private: |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 155 | QuicConnectionId server_connection_id_; |
fayang | f7c569c | 2019-05-07 11:56:51 -0700 | [diff] [blame] | 156 | QuicFramer framer_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 157 | // Set as the visitor of |creator_| to collect any generated packets. |
| 158 | PacketCollector collector_; |
| 159 | QuicPacketCreator creator_; |
| 160 | QuicTimeWaitListManager* time_wait_list_manager_; |
| 161 | }; |
| 162 | |
| 163 | // Class which extracts the ALPN from a CHLO packet. |
| 164 | class ChloAlpnExtractor : public ChloExtractor::Delegate { |
| 165 | public: |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 166 | void OnChlo(QuicTransportVersion /*version*/, |
| 167 | QuicConnectionId /*server_connection_id*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 168 | const CryptoHandshakeMessage& chlo) override { |
| 169 | QuicStringPiece alpn_value; |
| 170 | if (chlo.GetStringPiece(kALPN, &alpn_value)) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 171 | alpn_ = std::string(alpn_value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 175 | std::string&& ConsumeAlpn() { return std::move(alpn_); } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 176 | |
| 177 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 178 | std::string alpn_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 181 | } // namespace |
| 182 | |
| 183 | QuicDispatcher::QuicDispatcher( |
| 184 | const QuicConfig* config, |
| 185 | const QuicCryptoServerConfig* crypto_config, |
| 186 | QuicVersionManager* version_manager, |
| 187 | std::unique_ptr<QuicConnectionHelperInterface> helper, |
| 188 | std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |
| 189 | std::unique_ptr<QuicAlarmFactory> alarm_factory, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 190 | uint8_t expected_server_connection_id_length) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 191 | : config_(config), |
| 192 | crypto_config_(crypto_config), |
| 193 | compressed_certs_cache_( |
| 194 | QuicCompressedCertsCache::kQuicCompressedCertsCacheSize), |
| 195 | helper_(std::move(helper)), |
| 196 | session_helper_(std::move(session_helper)), |
| 197 | alarm_factory_(std::move(alarm_factory)), |
| 198 | delete_sessions_alarm_( |
| 199 | alarm_factory_->CreateAlarm(new DeleteSessionsAlarm(this))), |
| 200 | buffered_packets_(this, helper_->GetClock(), alarm_factory_.get()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | version_manager_(version_manager), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 202 | last_error_(QUIC_NO_ERROR), |
| 203 | new_sessions_allowed_per_event_loop_(0u), |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 204 | accept_new_connections_(true), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 205 | allow_short_initial_server_connection_ids_(false), |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 206 | expected_server_connection_id_length_( |
| 207 | expected_server_connection_id_length), |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 208 | should_update_expected_server_connection_id_length_(false) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 209 | |
| 210 | QuicDispatcher::~QuicDispatcher() { |
| 211 | session_map_.clear(); |
| 212 | closed_session_list_.clear(); |
| 213 | } |
| 214 | |
| 215 | void QuicDispatcher::InitializeWithWriter(QuicPacketWriter* writer) { |
| 216 | DCHECK(writer_ == nullptr); |
| 217 | writer_.reset(writer); |
| 218 | time_wait_list_manager_.reset(CreateQuicTimeWaitListManager()); |
| 219 | } |
| 220 | |
| 221 | void QuicDispatcher::ProcessPacket(const QuicSocketAddress& self_address, |
| 222 | const QuicSocketAddress& peer_address, |
| 223 | const QuicReceivedPacket& packet) { |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 224 | QUIC_DVLOG(2) << "Dispatcher received encrypted " << packet.length() |
| 225 | << " bytes:" << std::endl |
| 226 | << QuicTextUtils::HexDump( |
| 227 | QuicStringPiece(packet.data(), packet.length())); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 228 | ReceivedPacketInfo packet_info(self_address, peer_address, packet); |
fayang | 1f12350 | 2019-05-14 08:05:16 -0700 | [diff] [blame] | 229 | std::string detailed_error; |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 230 | const QuicErrorCode error = QuicFramer::ProcessPacketDispatcher( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 231 | packet, expected_server_connection_id_length_, &packet_info.form, |
| 232 | &packet_info.version_flag, &packet_info.version_label, |
| 233 | &packet_info.destination_connection_id, &packet_info.source_connection_id, |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 234 | &detailed_error); |
| 235 | if (error != QUIC_NO_ERROR) { |
| 236 | // Packet has framing error. |
| 237 | SetLastError(error); |
| 238 | QUIC_DLOG(ERROR) << detailed_error; |
| 239 | return; |
| 240 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 241 | packet_info.version = ParseQuicVersionLabel(packet_info.version_label); |
| 242 | if (packet_info.destination_connection_id.length() != |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 243 | expected_server_connection_id_length_ && |
| 244 | !should_update_expected_server_connection_id_length_ && |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 245 | !QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 246 | packet_info.version.transport_version)) { |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 247 | SetLastError(QUIC_INVALID_PACKET_HEADER); |
| 248 | QUIC_DLOG(ERROR) << "Invalid Connection Id Length"; |
| 249 | return; |
| 250 | } |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 251 | if (should_update_expected_server_connection_id_length_) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 252 | expected_server_connection_id_length_ = |
| 253 | packet_info.destination_connection_id.length(); |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 254 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 255 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 256 | if (MaybeDispatchPacket(packet_info)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 257 | // Packet has been dropped or successfully dispatched, stop processing. |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 258 | return; |
| 259 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 260 | ProcessHeader(&packet_info); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 261 | } |
| 262 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 263 | QuicConnectionId QuicDispatcher::MaybeReplaceServerConnectionId( |
| 264 | QuicConnectionId server_connection_id, |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 265 | ParsedQuicVersion version) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 266 | if (server_connection_id.length() == expected_server_connection_id_length_) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 267 | return server_connection_id; |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 268 | } |
| 269 | DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 270 | version.transport_version)); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 271 | auto it = connection_id_map_.find(server_connection_id); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 272 | if (it != connection_id_map_.end()) { |
| 273 | return it->second; |
| 274 | } |
| 275 | QuicConnectionId new_connection_id = |
| 276 | session_helper_->GenerateConnectionIdForReject(version.transport_version, |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 277 | server_connection_id); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 278 | DCHECK_EQ(expected_server_connection_id_length_, new_connection_id.length()); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 279 | connection_id_map_.insert( |
| 280 | std::make_pair(server_connection_id, new_connection_id)); |
| 281 | QUIC_DLOG(INFO) << "Replacing incoming connection ID " << server_connection_id |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 282 | << " with " << new_connection_id; |
| 283 | return new_connection_id; |
| 284 | } |
| 285 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 286 | bool QuicDispatcher::MaybeDispatchPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 287 | const ReceivedPacketInfo& packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 288 | // Port zero is only allowed for unidirectional UDP, so is disallowed by QUIC. |
| 289 | // Given that we can't even send a reply rejecting the packet, just drop the |
| 290 | // packet. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 291 | if (packet_info.peer_address.port() == 0) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 292 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 293 | } |
| 294 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 295 | QuicConnectionId server_connection_id = packet_info.destination_connection_id; |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 296 | |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 297 | // The IETF spec requires the client to generate an initial server |
| 298 | // connection ID that is at least 64 bits long. After that initial |
| 299 | // connection ID, the dispatcher picks a new one of its expected length. |
| 300 | // Therefore we should never receive a connection ID that is smaller |
| 301 | // than 64 bits and smaller than what we expect. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 302 | if (server_connection_id.length() < kQuicMinimumInitialConnectionIdLength && |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 303 | server_connection_id.length() < expected_server_connection_id_length_ && |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 304 | !allow_short_initial_server_connection_ids_) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 305 | DCHECK(packet_info.version_flag); |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 306 | DCHECK(QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 307 | packet_info.version.transport_version)); |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 308 | QUIC_DLOG(INFO) << "Packet with short destination connection ID " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 309 | << server_connection_id << " expected " |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 310 | << static_cast<int>(expected_server_connection_id_length_); |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 311 | if (!GetQuicReloadableFlag(quic_drop_invalid_small_initial_connection_id)) { |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 312 | // Add this connection_id to the time-wait state, to safely reject |
| 313 | // future packets. |
| 314 | QUIC_DLOG(INFO) << "Adding connection ID " << server_connection_id |
| 315 | << " to time-wait list."; |
| 316 | StatelesslyTerminateConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 317 | server_connection_id, packet_info.form, packet_info.version_flag, |
| 318 | packet_info.version, QUIC_HANDSHAKE_FAILED, "Reject connection", |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 319 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 320 | |
| 321 | DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait( |
| 322 | server_connection_id)); |
| 323 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 324 | packet_info.self_address, packet_info.peer_address, |
| 325 | server_connection_id, packet_info.form, GetPerPacketContext()); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 326 | |
| 327 | buffered_packets_.DiscardPackets(server_connection_id); |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 328 | } else { |
| 329 | QUIC_RELOADABLE_FLAG_COUNT(quic_drop_invalid_small_initial_connection_id); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 330 | // Drop the packet silently. |
dschinazi | ee07e47 | 2019-06-19 09:56:56 -0700 | [diff] [blame] | 331 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 332 | return true; |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 333 | } |
| 334 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 335 | // Packets with connection IDs for active connections are processed |
| 336 | // immediately. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 337 | auto it = session_map_.find(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 338 | if (it != session_map_.end()) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 339 | DCHECK(!buffered_packets_.HasBufferedPackets(server_connection_id)); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 340 | it->second->ProcessUdpPacket(packet_info.self_address, |
| 341 | packet_info.peer_address, packet_info.packet); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 342 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 343 | } |
| 344 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 345 | if (buffered_packets_.HasChloForConnection(server_connection_id)) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 346 | BufferEarlyPacket(packet_info); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 347 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 348 | } |
| 349 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 350 | if (OnFailedToDispatchPacket(packet_info)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 351 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 352 | } |
| 353 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 354 | if (time_wait_list_manager_->IsConnectionIdInTimeWait(server_connection_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 355 | // This connection ID is already in time-wait state. |
| 356 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 357 | packet_info.self_address, packet_info.peer_address, |
| 358 | packet_info.destination_connection_id, packet_info.form, |
| 359 | GetPerPacketContext()); |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 360 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // The packet has an unknown connection ID. |
| 364 | |
| 365 | // Unless the packet provides a version, assume that we can continue |
| 366 | // processing using our preferred version. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 367 | if (packet_info.version_flag) { |
| 368 | if (!IsSupportedVersion(packet_info.version)) { |
| 369 | if (ShouldCreateSessionForUnknownVersion(packet_info.version_label)) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 370 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 371 | } |
| 372 | if (!crypto_config()->validate_chlo_size() || |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 373 | packet_info.packet.length() >= kMinPacketSizeForVersionNegotiation) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 374 | // Since the version is not supported, send a version negotiation |
| 375 | // packet and stop processing the current packet. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 376 | QuicConnectionId client_connection_id = |
| 377 | packet_info.source_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 378 | time_wait_list_manager()->SendVersionNegotiationPacket( |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 379 | server_connection_id, client_connection_id, |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 380 | packet_info.form != GOOGLE_QUIC_PACKET, GetSupportedVersions(), |
| 381 | packet_info.self_address, packet_info.peer_address, |
dschinazi | b417d60 | 2019-05-29 13:08:45 -0700 | [diff] [blame] | 382 | GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 383 | } |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 384 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 385 | } |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 386 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 387 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 391 | void QuicDispatcher::ProcessHeader(ReceivedPacketInfo* packet_info) { |
| 392 | QuicConnectionId server_connection_id = |
| 393 | packet_info->destination_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 394 | // Packet's connection ID is unknown. Apply the validity checks. |
wub | 2a5670f | 2019-06-13 13:39:16 -0700 | [diff] [blame] | 395 | // TODO(wub): Determine the fate completely in ValidityChecks, then call |
| 396 | // ProcessUnauthenticatedHeaderFate in one place. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 397 | QuicPacketFate fate = ValidityChecks(*packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 398 | ChloAlpnExtractor alpn_extractor; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | switch (fate) { |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 400 | case kFateProcess: |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 401 | if (packet_info->version.handshake_protocol == PROTOCOL_TLS1_3) { |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 402 | // TODO(nharper): Support buffering non-ClientHello packets when using |
| 403 | // TLS. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 404 | ProcessChlo(/*alpn=*/"", packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 405 | break; |
| 406 | } |
| 407 | if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) && |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 408 | !ChloExtractor::Extract(packet_info->packet, GetSupportedVersions(), |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 409 | config_->create_session_tag_indicators(), |
| 410 | &alpn_extractor, |
| 411 | server_connection_id.length())) { |
| 412 | // Buffer non-CHLO packets. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 413 | BufferEarlyPacket(*packet_info); |
fayang | 17230ac | 2019-06-21 06:28:28 -0700 | [diff] [blame] | 414 | break; |
| 415 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 416 | ProcessChlo(alpn_extractor.ConsumeAlpn(), packet_info); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 417 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 418 | case kFateTimeWait: |
wub | 0a4b9c5 | 2019-05-28 13:18:58 -0700 | [diff] [blame] | 419 | // Add this connection_id to the time-wait state, to safely reject |
| 420 | // future packets. |
| 421 | QUIC_DLOG(INFO) << "Adding connection ID " << server_connection_id |
| 422 | << " to time-wait list."; |
| 423 | QUIC_CODE_COUNT(quic_reject_fate_time_wait); |
| 424 | StatelesslyTerminateConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 425 | server_connection_id, packet_info->form, packet_info->version_flag, |
| 426 | packet_info->version, QUIC_HANDSHAKE_FAILED, "Reject connection", |
wub | 0a4b9c5 | 2019-05-28 13:18:58 -0700 | [diff] [blame] | 427 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 428 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 429 | DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait( |
| 430 | server_connection_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 431 | time_wait_list_manager_->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 432 | packet_info->self_address, packet_info->peer_address, |
| 433 | server_connection_id, packet_info->form, GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 434 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 435 | buffered_packets_.DiscardPackets(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 436 | break; |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 437 | case kFateDrop: |
| 438 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | QuicDispatcher::QuicPacketFate QuicDispatcher::ValidityChecks( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 443 | const ReceivedPacketInfo& packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 444 | // To have all the checks work properly without tears, insert any new check |
| 445 | // into the framework of this method in the section for checks that return the |
| 446 | // check's fate value. The sections for checks must be ordered with the |
| 447 | // highest priority fate first. |
| 448 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 449 | // All packets within a connection sent by a client before receiving a |
| 450 | // response from the server are required to have the version negotiation flag |
| 451 | // set. Since this may be a client continuing a connection we lost track of |
| 452 | // via server restart, send a rejection to fast-fail the connection. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 453 | if (!packet_info.version_flag) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 454 | QUIC_DLOG(INFO) |
| 455 | << "Packet without version arrived for unknown connection ID " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 456 | << packet_info.destination_connection_id; |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 457 | if (GetQuicReloadableFlag(quic_reject_unprocessable_packets_statelessly)) { |
| 458 | QUIC_RELOADABLE_FLAG_COUNT(quic_reject_unprocessable_packets_statelessly); |
| 459 | MaybeResetPacketsWithNoVersion(packet_info); |
| 460 | return kFateDrop; |
| 461 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 462 | return kFateTimeWait; |
| 463 | } |
| 464 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 465 | // Let the connection parse and validate packet number. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | return kFateProcess; |
| 467 | } |
| 468 | |
| 469 | void QuicDispatcher::CleanUpSession(SessionMap::iterator it, |
| 470 | QuicConnection* connection, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 471 | ConnectionCloseSource /*source*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 472 | write_blocked_list_.erase(connection); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 473 | QuicTimeWaitListManager::TimeWaitAction action = |
| 474 | QuicTimeWaitListManager::SEND_STATELESS_RESET; |
| 475 | if (connection->termination_packets() != nullptr && |
| 476 | !connection->termination_packets()->empty()) { |
| 477 | action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS; |
fayang | 51c2373 | 2019-06-24 06:59:55 -0700 | [diff] [blame] | 478 | } else { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 479 | if (!connection->IsHandshakeConfirmed()) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 480 | if (!VersionHasIetfInvariantHeader(connection->transport_version())) { |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 481 | QUIC_CODE_COUNT(gquic_add_to_time_wait_list_with_handshake_failed); |
| 482 | } else { |
| 483 | QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_handshake_failed); |
| 484 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 485 | action = QuicTimeWaitListManager::SEND_TERMINATION_PACKETS; |
| 486 | // This serializes a connection close termination packet with error code |
| 487 | // QUIC_HANDSHAKE_FAILED and adds the connection to the time wait list. |
| 488 | StatelesslyTerminateConnection( |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 489 | connection->connection_id(), |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 490 | VersionHasIetfInvariantHeader(connection->transport_version()) |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 491 | ? IETF_QUIC_LONG_HEADER_PACKET |
| 492 | : GOOGLE_QUIC_PACKET, |
| 493 | /*version_flag=*/true, connection->version(), QUIC_HANDSHAKE_FAILED, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 494 | "Connection is closed by server before handshake confirmed", |
| 495 | // Although it is our intention to send termination packets, the |
| 496 | // |action| argument is not used by this call to |
| 497 | // StatelesslyTerminateConnection(). |
| 498 | action); |
| 499 | session_map_.erase(it); |
| 500 | return; |
| 501 | } |
| 502 | QUIC_CODE_COUNT(quic_v44_add_to_time_wait_list_with_stateless_reset); |
| 503 | } |
| 504 | time_wait_list_manager_->AddConnectionIdToTimeWait( |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 505 | it->first, VersionHasIetfInvariantHeader(connection->transport_version()), |
| 506 | action, connection->encryption_level(), |
| 507 | connection->termination_packets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 508 | session_map_.erase(it); |
| 509 | } |
| 510 | |
| 511 | void QuicDispatcher::StopAcceptingNewConnections() { |
| 512 | accept_new_connections_ = false; |
| 513 | } |
| 514 | |
| 515 | std::unique_ptr<QuicPerPacketContext> QuicDispatcher::GetPerPacketContext() |
| 516 | const { |
| 517 | return nullptr; |
| 518 | } |
| 519 | |
| 520 | void QuicDispatcher::DeleteSessions() { |
QUICHE team | aa1d6a8 | 2019-03-13 09:14:13 -0700 | [diff] [blame] | 521 | if (!write_blocked_list_.empty()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 522 | for (const std::unique_ptr<QuicSession>& session : closed_session_list_) { |
| 523 | if (write_blocked_list_.erase(session->connection()) != 0) { |
| 524 | QUIC_BUG << "QuicConnection was in WriteBlockedList before destruction"; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | closed_session_list_.clear(); |
| 529 | } |
| 530 | |
| 531 | void QuicDispatcher::OnCanWrite() { |
| 532 | // The socket is now writable. |
| 533 | writer_->SetWritable(); |
| 534 | |
| 535 | // Move every blocked writer in |write_blocked_list_| to a temporary list. |
| 536 | const size_t num_blocked_writers_before = write_blocked_list_.size(); |
| 537 | WriteBlockedList temp_list; |
| 538 | temp_list.swap(write_blocked_list_); |
| 539 | DCHECK(write_blocked_list_.empty()); |
| 540 | |
| 541 | // Give each blocked writer a chance to write what they indended to write. |
| 542 | // If they are blocked again, they will call |OnWriteBlocked| to add |
| 543 | // themselves back into |write_blocked_list_|. |
| 544 | while (!temp_list.empty()) { |
| 545 | QuicBlockedWriterInterface* blocked_writer = temp_list.begin()->first; |
| 546 | temp_list.erase(temp_list.begin()); |
| 547 | blocked_writer->OnBlockedWriterCanWrite(); |
| 548 | } |
| 549 | const size_t num_blocked_writers_after = write_blocked_list_.size(); |
| 550 | if (num_blocked_writers_after != 0) { |
| 551 | if (num_blocked_writers_before == num_blocked_writers_after) { |
| 552 | QUIC_CODE_COUNT(quic_zero_progress_on_can_write); |
| 553 | } else { |
| 554 | QUIC_CODE_COUNT(quic_blocked_again_on_can_write); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | bool QuicDispatcher::HasPendingWrites() const { |
| 560 | return !write_blocked_list_.empty(); |
| 561 | } |
| 562 | |
| 563 | void QuicDispatcher::Shutdown() { |
| 564 | while (!session_map_.empty()) { |
| 565 | QuicSession* session = session_map_.begin()->second.get(); |
| 566 | session->connection()->CloseConnection( |
| 567 | QUIC_PEER_GOING_AWAY, "Server shutdown imminent", |
| 568 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 569 | // Validate that the session removes itself from the session map on close. |
| 570 | DCHECK(session_map_.empty() || |
| 571 | session_map_.begin()->second.get() != session); |
| 572 | } |
| 573 | DeleteSessions(); |
| 574 | } |
| 575 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 576 | void QuicDispatcher::OnConnectionClosed(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 577 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 578 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 579 | ConnectionCloseSource source) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 580 | auto it = session_map_.find(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 581 | if (it == session_map_.end()) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 582 | QUIC_BUG << "ConnectionId " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 583 | << " does not exist in the session map. Error: " |
| 584 | << QuicErrorCodeToString(error); |
| 585 | QUIC_BUG << QuicStackTrace(); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | QUIC_DLOG_IF(INFO, error != QUIC_NO_ERROR) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 590 | << "Closing connection (" << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 591 | << ") due to error: " << QuicErrorCodeToString(error) |
| 592 | << ", with details: " << error_details; |
| 593 | |
| 594 | QuicConnection* connection = it->second->connection(); |
| 595 | if (ShouldDestroySessionAsynchronously()) { |
| 596 | // Set up alarm to fire immediately to bring destruction of this session |
| 597 | // out of current call stack. |
| 598 | if (closed_session_list_.empty()) { |
| 599 | delete_sessions_alarm_->Update(helper()->GetClock()->ApproximateNow(), |
| 600 | QuicTime::Delta::Zero()); |
| 601 | } |
| 602 | closed_session_list_.push_back(std::move(it->second)); |
| 603 | } |
wub | 5f64ec4 | 2019-06-06 07:31:19 -0700 | [diff] [blame] | 604 | CleanUpSession(it, connection, source); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void QuicDispatcher::OnWriteBlocked( |
| 608 | QuicBlockedWriterInterface* blocked_writer) { |
| 609 | if (!blocked_writer->IsWriterBlocked()) { |
| 610 | // It is a programming error if this ever happens. When we are sure it is |
| 611 | // not happening, replace it with a DCHECK. |
| 612 | QUIC_BUG |
| 613 | << "Tried to add writer into blocked list when it shouldn't be added"; |
| 614 | // Return without adding the connection to the blocked list, to avoid |
| 615 | // infinite loops in OnCanWrite. |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | write_blocked_list_.insert(std::make_pair(blocked_writer, true)); |
| 620 | } |
| 621 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 622 | void QuicDispatcher::OnRstStreamReceived(const QuicRstStreamFrame& /*frame*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 623 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 624 | void QuicDispatcher::OnStopSendingReceived( |
| 625 | const QuicStopSendingFrame& /*frame*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 626 | |
| 627 | void QuicDispatcher::OnConnectionAddedToTimeWaitList( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 628 | QuicConnectionId server_connection_id) { |
| 629 | QUIC_DLOG(INFO) << "Connection " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 630 | << " added to time wait list."; |
| 631 | } |
| 632 | |
| 633 | void QuicDispatcher::StatelesslyTerminateConnection( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 634 | QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 635 | PacketHeaderFormat format, |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 636 | bool version_flag, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 637 | ParsedQuicVersion version, |
| 638 | QuicErrorCode error_code, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 639 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 640 | QuicTimeWaitListManager::TimeWaitAction action) { |
fayang | 51c2373 | 2019-06-24 06:59:55 -0700 | [diff] [blame] | 641 | if (format != IETF_QUIC_LONG_HEADER_PACKET && !version_flag) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 642 | QUIC_DVLOG(1) << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 643 | << " based on a non-ietf-long packet, action:" << action |
| 644 | << ", error_code:" << error_code |
| 645 | << ", error_details:" << error_details; |
| 646 | time_wait_list_manager_->AddConnectionIdToTimeWait( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 647 | server_connection_id, format != GOOGLE_QUIC_PACKET, action, |
| 648 | ENCRYPTION_INITIAL, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 649 | return; |
| 650 | } |
| 651 | |
| 652 | // If the version is known and supported by framer, send a connection close. |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 653 | if (IsSupportedVersion(version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 654 | QUIC_DVLOG(1) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 655 | << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 656 | << " based on an ietf-long packet, which has a supported version:" |
| 657 | << version << ", error_code:" << error_code |
| 658 | << ", error_details:" << error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 659 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 660 | StatelessConnectionTerminator terminator(server_connection_id, version, |
| 661 | helper_.get(), |
| 662 | time_wait_list_manager_.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 663 | // This also adds the connection to time wait list. |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 664 | terminator.CloseConnection(error_code, error_details, |
| 665 | format != GOOGLE_QUIC_PACKET); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
| 669 | QUIC_DVLOG(1) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 670 | << "Statelessly terminating " << server_connection_id |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 671 | << " based on an ietf-long packet, which has an unsupported version:" |
| 672 | << version << ", error_code:" << error_code |
| 673 | << ", error_details:" << error_details; |
| 674 | // Version is unknown or unsupported by framer, send a version negotiation |
| 675 | // with an empty version list, which can be understood by the client. |
| 676 | std::vector<std::unique_ptr<QuicEncryptedPacket>> termination_packets; |
| 677 | termination_packets.push_back(QuicFramer::BuildVersionNegotiationPacket( |
dschinazi | b417d60 | 2019-05-29 13:08:45 -0700 | [diff] [blame] | 678 | server_connection_id, EmptyQuicConnectionId(), |
| 679 | /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, |
dschinazi | 1ac22cc | 2019-06-25 11:47:50 -0700 | [diff] [blame] | 680 | /*versions=*/{})); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 681 | time_wait_list_manager()->AddConnectionIdToTimeWait( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 682 | server_connection_id, /*ietf_quic=*/format != GOOGLE_QUIC_PACKET, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 683 | QuicTimeWaitListManager::SEND_TERMINATION_PACKETS, ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 684 | &termination_packets); |
| 685 | } |
| 686 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 687 | bool QuicDispatcher::ShouldCreateSessionForUnknownVersion( |
| 688 | QuicVersionLabel /*version_label*/) { |
| 689 | return false; |
| 690 | } |
| 691 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 692 | void QuicDispatcher::OnExpiredPackets( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 693 | QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 694 | BufferedPacketList early_arrived_packets) { |
| 695 | QUIC_CODE_COUNT(quic_reject_buffered_packets_expired); |
| 696 | StatelesslyTerminateConnection( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 697 | server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 698 | early_arrived_packets.ietf_quic ? IETF_QUIC_LONG_HEADER_PACKET |
| 699 | : GOOGLE_QUIC_PACKET, |
fayang | 1de6789 | 2019-04-19 05:59:45 -0700 | [diff] [blame] | 700 | /*version_flag=*/true, early_arrived_packets.version, |
| 701 | QUIC_HANDSHAKE_FAILED, "Packets buffered for too long", |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 702 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 703 | } |
| 704 | |
| 705 | void QuicDispatcher::ProcessBufferedChlos(size_t max_connections_to_create) { |
| 706 | // Reset the counter before starting creating connections. |
| 707 | new_sessions_allowed_per_event_loop_ = max_connections_to_create; |
| 708 | for (; new_sessions_allowed_per_event_loop_ > 0; |
| 709 | --new_sessions_allowed_per_event_loop_) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 710 | QuicConnectionId server_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 711 | BufferedPacketList packet_list = |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 712 | buffered_packets_.DeliverPacketsForNextConnection( |
| 713 | &server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 714 | const std::list<BufferedPacket>& packets = packet_list.buffered_packets; |
| 715 | if (packets.empty()) { |
| 716 | return; |
| 717 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 718 | QuicConnectionId original_connection_id = server_connection_id; |
| 719 | server_connection_id = MaybeReplaceServerConnectionId(server_connection_id, |
| 720 | packet_list.version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 721 | QuicSession* session = |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 722 | CreateQuicSession(server_connection_id, packets.front().peer_address, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 723 | packet_list.alpn, packet_list.version); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 724 | if (original_connection_id != server_connection_id) { |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 725 | session->connection()->AddIncomingConnectionId(original_connection_id); |
| 726 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 727 | QUIC_DLOG(INFO) << "Created new session for " << server_connection_id; |
| 728 | session_map_.insert( |
| 729 | std::make_pair(server_connection_id, QuicWrapUnique(session))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 730 | DeliverPacketsToSession(packets, session); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | bool QuicDispatcher::HasChlosBuffered() const { |
| 735 | return buffered_packets_.HasChlosBuffered(); |
| 736 | } |
| 737 | |
| 738 | bool QuicDispatcher::ShouldCreateOrBufferPacketForConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 739 | const ReceivedPacketInfo& packet_info) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 740 | QUIC_VLOG(1) << "Received packet from new connection " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 741 | << packet_info.destination_connection_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 742 | return true; |
| 743 | } |
| 744 | |
| 745 | // Return true if there is any packet buffered in the store. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 746 | bool QuicDispatcher::HasBufferedPackets(QuicConnectionId server_connection_id) { |
| 747 | return buffered_packets_.HasBufferedPackets(server_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 748 | } |
| 749 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 750 | void QuicDispatcher::OnBufferPacketFailure( |
| 751 | EnqueuePacketResult result, |
| 752 | QuicConnectionId server_connection_id) { |
| 753 | QUIC_DLOG(INFO) << "Fail to buffer packet on connection " |
| 754 | << server_connection_id << " because of " << result; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 755 | } |
| 756 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 757 | QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { |
| 758 | return new QuicTimeWaitListManager(writer_.get(), this, helper_->GetClock(), |
| 759 | alarm_factory_.get()); |
| 760 | } |
| 761 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 762 | void QuicDispatcher::BufferEarlyPacket(const ReceivedPacketInfo& packet_info) { |
| 763 | bool is_new_connection = !buffered_packets_.HasBufferedPackets( |
| 764 | packet_info.destination_connection_id); |
| 765 | if (is_new_connection && |
| 766 | !ShouldCreateOrBufferPacketForConnection(packet_info)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
| 770 | EnqueuePacketResult rs = buffered_packets_.EnqueuePacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 771 | packet_info.destination_connection_id, |
| 772 | packet_info.form != GOOGLE_QUIC_PACKET, packet_info.packet, |
| 773 | packet_info.self_address, packet_info.peer_address, /*is_chlo=*/false, |
| 774 | /*alpn=*/"", packet_info.version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 775 | if (rs != EnqueuePacketResult::SUCCESS) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 776 | OnBufferPacketFailure(rs, packet_info.destination_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 780 | void QuicDispatcher::ProcessChlo(const std::string& alpn, |
| 781 | ReceivedPacketInfo* packet_info) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 782 | if (!accept_new_connections_) { |
| 783 | // Don't any create new connection. |
| 784 | QUIC_CODE_COUNT(quic_reject_stop_accepting_new_connections); |
| 785 | StatelesslyTerminateConnection( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 786 | packet_info->destination_connection_id, packet_info->form, |
| 787 | /*version_flag=*/true, packet_info->version, QUIC_HANDSHAKE_FAILED, |
| 788 | "Stop accepting new connections", |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 789 | quic::QuicTimeWaitListManager::SEND_STATELESS_RESET); |
| 790 | // Time wait list will reject the packet correspondingly. |
| 791 | time_wait_list_manager()->ProcessPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 792 | packet_info->self_address, packet_info->peer_address, |
| 793 | packet_info->destination_connection_id, packet_info->form, |
| 794 | GetPerPacketContext()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 795 | return; |
| 796 | } |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 797 | if (!buffered_packets_.HasBufferedPackets( |
| 798 | packet_info->destination_connection_id) && |
| 799 | !ShouldCreateOrBufferPacketForConnection(*packet_info)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 800 | return; |
| 801 | } |
danzh | 88e3e05 | 2019-06-13 11:47:18 -0700 | [diff] [blame] | 802 | if (GetQuicFlag(FLAGS_quic_allow_chlo_buffering) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 803 | new_sessions_allowed_per_event_loop_ <= 0) { |
| 804 | // Can't create new session any more. Wait till next event loop. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 805 | QUIC_BUG_IF(buffered_packets_.HasChloForConnection( |
| 806 | packet_info->destination_connection_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 807 | EnqueuePacketResult rs = buffered_packets_.EnqueuePacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 808 | packet_info->destination_connection_id, |
| 809 | packet_info->form != GOOGLE_QUIC_PACKET, packet_info->packet, |
| 810 | packet_info->self_address, packet_info->peer_address, |
| 811 | /*is_chlo=*/true, alpn, packet_info->version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 812 | if (rs != EnqueuePacketResult::SUCCESS) { |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 813 | OnBufferPacketFailure(rs, packet_info->destination_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 814 | } |
| 815 | return; |
| 816 | } |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 817 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 818 | QuicConnectionId original_connection_id = |
| 819 | packet_info->destination_connection_id; |
| 820 | packet_info->destination_connection_id = MaybeReplaceServerConnectionId( |
| 821 | original_connection_id, packet_info->version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 822 | // Creates a new session and process all buffered packets for this connection. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 823 | QuicSession* session = |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 824 | CreateQuicSession(packet_info->destination_connection_id, |
| 825 | packet_info->peer_address, alpn, packet_info->version); |
| 826 | if (original_connection_id != packet_info->destination_connection_id) { |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 827 | session->connection()->AddIncomingConnectionId(original_connection_id); |
| 828 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 829 | QUIC_DLOG(INFO) << "Created new session for " |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 830 | << packet_info->destination_connection_id; |
| 831 | session_map_.insert(std::make_pair(packet_info->destination_connection_id, |
| 832 | QuicWrapUnique(session))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 833 | std::list<BufferedPacket> packets = |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 834 | buffered_packets_.DeliverPackets(packet_info->destination_connection_id) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 835 | .buffered_packets; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 836 | // Process CHLO at first. |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 837 | session->ProcessUdpPacket(packet_info->self_address, |
| 838 | packet_info->peer_address, packet_info->packet); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 839 | // Deliver queued-up packets in the same order as they arrived. |
| 840 | // Do this even when flag is off because there might be still some packets |
| 841 | // buffered in the store before flag is turned off. |
| 842 | DeliverPacketsToSession(packets, session); |
| 843 | --new_sessions_allowed_per_event_loop_; |
| 844 | } |
| 845 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 846 | bool QuicDispatcher::ShouldDestroySessionAsynchronously() { |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | void QuicDispatcher::SetLastError(QuicErrorCode error) { |
| 851 | last_error_ = error; |
| 852 | } |
| 853 | |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 854 | bool QuicDispatcher::OnFailedToDispatchPacket( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 855 | const ReceivedPacketInfo& /*packet_info*/) { |
fayang | 91475c4 | 2019-06-19 08:04:26 -0700 | [diff] [blame] | 856 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 857 | } |
| 858 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 859 | const QuicTransportVersionVector& |
| 860 | QuicDispatcher::GetSupportedTransportVersions() { |
| 861 | return version_manager_->GetSupportedTransportVersions(); |
| 862 | } |
| 863 | |
| 864 | const ParsedQuicVersionVector& QuicDispatcher::GetSupportedVersions() { |
| 865 | return version_manager_->GetSupportedVersions(); |
| 866 | } |
| 867 | |
| 868 | void QuicDispatcher::DeliverPacketsToSession( |
| 869 | const std::list<BufferedPacket>& packets, |
| 870 | QuicSession* session) { |
| 871 | for (const BufferedPacket& packet : packets) { |
| 872 | session->ProcessUdpPacket(packet.self_address, packet.peer_address, |
| 873 | *(packet.packet)); |
| 874 | } |
| 875 | } |
| 876 | |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 877 | bool QuicDispatcher::IsSupportedVersion(const ParsedQuicVersion version) { |
fayang | ccbab73 | 2019-05-13 10:11:25 -0700 | [diff] [blame] | 878 | for (const ParsedQuicVersion& supported_version : |
| 879 | version_manager_->GetSupportedVersions()) { |
| 880 | if (version == supported_version) { |
| 881 | return true; |
| 882 | } |
| 883 | } |
| 884 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 885 | } |
| 886 | |
fayang | d057e66 | 2019-07-10 13:29:41 -0700 | [diff] [blame] | 887 | void QuicDispatcher::MaybeResetPacketsWithNoVersion( |
| 888 | const ReceivedPacketInfo& packet_info) { |
| 889 | DCHECK(!packet_info.version_flag); |
| 890 | const size_t MinValidPacketLength = |
| 891 | kPacketHeaderTypeSize + expected_server_connection_id_length_ + |
| 892 | PACKET_1BYTE_PACKET_NUMBER + /*payload size=*/1 + /*tag size=*/12; |
| 893 | if (packet_info.packet.length() < MinValidPacketLength) { |
| 894 | // The packet size is too small. |
| 895 | QUIC_CODE_COUNT(drop_too_small_packets); |
| 896 | return; |
| 897 | } |
| 898 | // TODO(fayang): Consider rate limiting reset packets if reset packet size > |
| 899 | // packet_length. |
| 900 | |
| 901 | time_wait_list_manager()->SendPublicReset( |
| 902 | packet_info.self_address, packet_info.peer_address, |
| 903 | packet_info.destination_connection_id, |
| 904 | packet_info.form != GOOGLE_QUIC_PACKET, GetPerPacketContext()); |
| 905 | } |
| 906 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 907 | } // namespace quic |