| OLD | NEW |
| (Empty) |
| 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/tools/quic/quic_dispatcher.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 | |
| 9 #include "base/debug/stack_trace.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "net/quic/quic_blocked_writer_interface.h" | |
| 13 #include "net/quic/quic_flags.h" | |
| 14 #include "net/quic/quic_utils.h" | |
| 15 #include "net/tools/epoll_server/epoll_server.h" | |
| 16 #include "net/tools/quic/quic_default_packet_writer.h" | |
| 17 #include "net/tools/quic/quic_epoll_connection_helper.h" | |
| 18 #include "net/tools/quic/quic_per_connection_packet_writer.h" | |
| 19 #include "net/tools/quic/quic_socket_utils.h" | |
| 20 #include "net/tools/quic/quic_time_wait_list_manager.h" | |
| 21 | |
| 22 namespace net { | |
| 23 | |
| 24 namespace tools { | |
| 25 | |
| 26 using base::StringPiece; | |
| 27 | |
| 28 class DeleteSessionsAlarm : public EpollAlarm { | |
| 29 public: | |
| 30 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) | |
| 31 : dispatcher_(dispatcher) { | |
| 32 } | |
| 33 | |
| 34 int64 OnAlarm() override { | |
| 35 EpollAlarm::OnAlarm(); | |
| 36 dispatcher_->DeleteSessions(); | |
| 37 return 0; | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 QuicDispatcher* dispatcher_; | |
| 42 }; | |
| 43 | |
| 44 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { | |
| 45 public: | |
| 46 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) | |
| 47 : dispatcher_(dispatcher), | |
| 48 connection_id_(0) {} | |
| 49 | |
| 50 // QuicFramerVisitorInterface implementation | |
| 51 void OnPacket() override {} | |
| 52 bool OnUnauthenticatedPublicHeader( | |
| 53 const QuicPacketPublicHeader& header) override { | |
| 54 connection_id_ = header.connection_id; | |
| 55 return dispatcher_->OnUnauthenticatedPublicHeader(header); | |
| 56 } | |
| 57 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override { | |
| 58 dispatcher_->OnUnauthenticatedHeader(header); | |
| 59 return false; | |
| 60 } | |
| 61 void OnError(QuicFramer* framer) override { | |
| 62 DVLOG(1) << QuicUtils::ErrorToString(framer->error()); | |
| 63 } | |
| 64 | |
| 65 bool OnProtocolVersionMismatch(QuicVersion /*received_version*/) override { | |
| 66 if (dispatcher_->time_wait_list_manager()->IsConnectionIdInTimeWait( | |
| 67 connection_id_)) { | |
| 68 // Keep processing after protocol mismatch - this will be dealt with by | |
| 69 // the TimeWaitListManager. | |
| 70 return true; | |
| 71 } else { | |
| 72 DLOG(DFATAL) << "Version mismatch, connection ID (" << connection_id_ | |
| 73 << ") not in time wait list."; | |
| 74 return false; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // The following methods should never get called because we always return | |
| 79 // false from OnUnauthenticatedHeader(). As a result, we never process the | |
| 80 // payload of the packet. | |
| 81 void OnPublicResetPacket(const QuicPublicResetPacket& /*packet*/) override { | |
| 82 DCHECK(false); | |
| 83 } | |
| 84 void OnVersionNegotiationPacket( | |
| 85 const QuicVersionNegotiationPacket& /*packet*/) override { | |
| 86 DCHECK(false); | |
| 87 } | |
| 88 void OnDecryptedPacket(EncryptionLevel level) override { DCHECK(false); } | |
| 89 bool OnPacketHeader(const QuicPacketHeader& /*header*/) override { | |
| 90 DCHECK(false); | |
| 91 return false; | |
| 92 } | |
| 93 void OnRevivedPacket() override { DCHECK(false); } | |
| 94 void OnFecProtectedPayload(StringPiece /*payload*/) override { | |
| 95 DCHECK(false); | |
| 96 } | |
| 97 bool OnStreamFrame(const QuicStreamFrame& /*frame*/) override { | |
| 98 DCHECK(false); | |
| 99 return false; | |
| 100 } | |
| 101 bool OnAckFrame(const QuicAckFrame& /*frame*/) override { | |
| 102 DCHECK(false); | |
| 103 return false; | |
| 104 } | |
| 105 bool OnStopWaitingFrame(const QuicStopWaitingFrame& /*frame*/) override { | |
| 106 DCHECK(false); | |
| 107 return false; | |
| 108 } | |
| 109 bool OnPingFrame(const QuicPingFrame& /*frame*/) override { | |
| 110 DCHECK(false); | |
| 111 return false; | |
| 112 } | |
| 113 bool OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) override { | |
| 114 DCHECK(false); | |
| 115 return false; | |
| 116 } | |
| 117 bool OnConnectionCloseFrame( | |
| 118 const QuicConnectionCloseFrame& /*frame*/) override { | |
| 119 DCHECK(false); | |
| 120 return false; | |
| 121 } | |
| 122 bool OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) override { | |
| 123 DCHECK(false); | |
| 124 return false; | |
| 125 } | |
| 126 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& /*frame*/) override { | |
| 127 DCHECK(false); | |
| 128 return false; | |
| 129 } | |
| 130 bool OnBlockedFrame(const QuicBlockedFrame& frame) override { | |
| 131 DCHECK(false); | |
| 132 return false; | |
| 133 } | |
| 134 void OnFecData(const QuicFecData& /*fec*/) override { DCHECK(false); } | |
| 135 void OnPacketComplete() override { DCHECK(false); } | |
| 136 | |
| 137 private: | |
| 138 QuicDispatcher* dispatcher_; | |
| 139 | |
| 140 // Latched in OnUnauthenticatedPublicHeader for use later. | |
| 141 QuicConnectionId connection_id_; | |
| 142 }; | |
| 143 | |
| 144 QuicPacketWriter* QuicDispatcher::DefaultPacketWriterFactory::Create( | |
| 145 QuicPacketWriter* writer, | |
| 146 QuicConnection* connection) { | |
| 147 return new QuicPerConnectionPacketWriter(writer, connection); | |
| 148 } | |
| 149 | |
| 150 QuicDispatcher::PacketWriterFactoryAdapter::PacketWriterFactoryAdapter( | |
| 151 QuicDispatcher* dispatcher) | |
| 152 : dispatcher_(dispatcher) {} | |
| 153 | |
| 154 QuicDispatcher::PacketWriterFactoryAdapter::~PacketWriterFactoryAdapter() {} | |
| 155 | |
| 156 QuicPacketWriter* QuicDispatcher::PacketWriterFactoryAdapter::Create( | |
| 157 QuicConnection* connection) const { | |
| 158 return dispatcher_->packet_writer_factory_->Create( | |
| 159 dispatcher_->writer_.get(), | |
| 160 connection); | |
| 161 } | |
| 162 | |
| 163 QuicDispatcher::QuicDispatcher(const QuicConfig& config, | |
| 164 const QuicCryptoServerConfig& crypto_config, | |
| 165 const QuicVersionVector& supported_versions, | |
| 166 PacketWriterFactory* packet_writer_factory, | |
| 167 EpollServer* epoll_server) | |
| 168 : config_(config), | |
| 169 crypto_config_(crypto_config), | |
| 170 delete_sessions_alarm_(new DeleteSessionsAlarm(this)), | |
| 171 epoll_server_(epoll_server), | |
| 172 helper_(new QuicEpollConnectionHelper(epoll_server_)), | |
| 173 packet_writer_factory_(packet_writer_factory), | |
| 174 connection_writer_factory_(this), | |
| 175 supported_versions_(supported_versions), | |
| 176 current_packet_(nullptr), | |
| 177 framer_(supported_versions, /*unused*/ QuicTime::Zero(), true), | |
| 178 framer_visitor_(new QuicFramerVisitor(this)) { | |
| 179 framer_.set_visitor(framer_visitor_.get()); | |
| 180 } | |
| 181 | |
| 182 QuicDispatcher::~QuicDispatcher() { | |
| 183 STLDeleteValues(&session_map_); | |
| 184 STLDeleteElements(&closed_session_list_); | |
| 185 } | |
| 186 | |
| 187 void QuicDispatcher::Initialize(int fd) { | |
| 188 DCHECK(writer_ == nullptr); | |
| 189 writer_.reset(CreateWriter(fd)); | |
| 190 time_wait_list_manager_.reset(CreateQuicTimeWaitListManager()); | |
| 191 } | |
| 192 | |
| 193 void QuicDispatcher::ProcessPacket(const IPEndPoint& server_address, | |
| 194 const IPEndPoint& client_address, | |
| 195 const QuicEncryptedPacket& packet) { | |
| 196 current_server_address_ = server_address; | |
| 197 current_client_address_ = client_address; | |
| 198 current_packet_ = &packet; | |
| 199 // ProcessPacket will cause the packet to be dispatched in | |
| 200 // OnUnauthenticatedPublicHeader, or sent to the time wait list manager | |
| 201 // in OnAuthenticatedHeader. | |
| 202 framer_.ProcessPacket(packet); | |
| 203 // TODO(rjshade): Return a status describing if/why a packet was dropped, | |
| 204 // and log somehow. Maybe expose as a varz. | |
| 205 } | |
| 206 | |
| 207 bool QuicDispatcher::OnUnauthenticatedPublicHeader( | |
| 208 const QuicPacketPublicHeader& header) { | |
| 209 QuicSession* session = nullptr; | |
| 210 | |
| 211 // Port zero is only allowed for unidirectional UDP, so is disallowed by QUIC. | |
| 212 // Given that we can't even send a reply rejecting the packet, just black hole | |
| 213 // it. | |
| 214 if (current_client_address_.port() == 0) { | |
| 215 return false; | |
| 216 } | |
| 217 | |
| 218 QuicConnectionId connection_id = header.connection_id; | |
| 219 SessionMap::iterator it = session_map_.find(connection_id); | |
| 220 if (it == session_map_.end()) { | |
| 221 if (header.reset_flag) { | |
| 222 return false; | |
| 223 } | |
| 224 if (time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)) { | |
| 225 return HandlePacketForTimeWait(header); | |
| 226 } | |
| 227 | |
| 228 // Ensure the packet has a version negotiation bit set before creating a new | |
| 229 // session for it. All initial packets for a new connection are required to | |
| 230 // have the flag set. Otherwise it may be a stray packet. | |
| 231 if (header.version_flag) { | |
| 232 session = CreateQuicSession(connection_id, current_server_address_, | |
| 233 current_client_address_); | |
| 234 } | |
| 235 | |
| 236 if (session == nullptr) { | |
| 237 DVLOG(1) << "Failed to create session for " << connection_id; | |
| 238 // Add this connection_id fo the time-wait state, to safely reject future | |
| 239 // packets. | |
| 240 | |
| 241 if (header.version_flag && | |
| 242 !framer_.IsSupportedVersion(header.versions.front())) { | |
| 243 // TODO(ianswett): Produce a no-version version negotiation packet. | |
| 244 return false; | |
| 245 } | |
| 246 | |
| 247 // Use the version in the packet if possible, otherwise assume the latest. | |
| 248 QuicVersion version = header.version_flag ? header.versions.front() : | |
| 249 supported_versions_.front(); | |
| 250 time_wait_list_manager_->AddConnectionIdToTimeWait(connection_id, version, | |
| 251 nullptr); | |
| 252 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); | |
| 253 return HandlePacketForTimeWait(header); | |
| 254 } | |
| 255 DVLOG(1) << "Created new session for " << connection_id; | |
| 256 session_map_.insert(std::make_pair(connection_id, session)); | |
| 257 } else { | |
| 258 session = it->second; | |
| 259 } | |
| 260 | |
| 261 session->connection()->ProcessUdpPacket( | |
| 262 current_server_address_, current_client_address_, *current_packet_); | |
| 263 | |
| 264 // Do not parse the packet further. The session will process it completely. | |
| 265 return false; | |
| 266 } | |
| 267 | |
| 268 void QuicDispatcher::OnUnauthenticatedHeader(const QuicPacketHeader& header) { | |
| 269 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait( | |
| 270 header.public_header.connection_id)); | |
| 271 time_wait_list_manager_->ProcessPacket(current_server_address_, | |
| 272 current_client_address_, | |
| 273 header.public_header.connection_id, | |
| 274 header.packet_sequence_number, | |
| 275 *current_packet_); | |
| 276 } | |
| 277 | |
| 278 void QuicDispatcher::CleanUpSession(SessionMap::iterator it) { | |
| 279 QuicConnection* connection = it->second->connection(); | |
| 280 QuicEncryptedPacket* connection_close_packet = | |
| 281 connection->ReleaseConnectionClosePacket(); | |
| 282 write_blocked_list_.erase(connection); | |
| 283 time_wait_list_manager_->AddConnectionIdToTimeWait(it->first, | |
| 284 connection->version(), | |
| 285 connection_close_packet); | |
| 286 session_map_.erase(it); | |
| 287 } | |
| 288 | |
| 289 void QuicDispatcher::DeleteSessions() { | |
| 290 STLDeleteElements(&closed_session_list_); | |
| 291 } | |
| 292 | |
| 293 void QuicDispatcher::OnCanWrite() { | |
| 294 // We got an EPOLLOUT: the socket should not be blocked. | |
| 295 writer_->SetWritable(); | |
| 296 | |
| 297 // Give all the blocked writers one chance to write, until we're blocked again | |
| 298 // or there's no work left. | |
| 299 while (!write_blocked_list_.empty() && !writer_->IsWriteBlocked()) { | |
| 300 QuicBlockedWriterInterface* blocked_writer = | |
| 301 write_blocked_list_.begin()->first; | |
| 302 write_blocked_list_.erase(write_blocked_list_.begin()); | |
| 303 blocked_writer->OnCanWrite(); | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 bool QuicDispatcher::HasPendingWrites() const { | |
| 308 return !write_blocked_list_.empty(); | |
| 309 } | |
| 310 | |
| 311 void QuicDispatcher::Shutdown() { | |
| 312 while (!session_map_.empty()) { | |
| 313 QuicSession* session = session_map_.begin()->second; | |
| 314 session->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | |
| 315 // Validate that the session removes itself from the session map on close. | |
| 316 DCHECK(session_map_.empty() || session_map_.begin()->second != session); | |
| 317 } | |
| 318 DeleteSessions(); | |
| 319 } | |
| 320 | |
| 321 void QuicDispatcher::OnConnectionClosed(QuicConnectionId connection_id, | |
| 322 QuicErrorCode error) { | |
| 323 SessionMap::iterator it = session_map_.find(connection_id); | |
| 324 if (it == session_map_.end()) { | |
| 325 LOG(DFATAL) << "ConnectionId " << connection_id | |
| 326 << " does not exist in the session map. " | |
| 327 << "Error: " << QuicUtils::ErrorToString(error); | |
| 328 LOG(DFATAL) << base::debug::StackTrace().ToString(); | |
| 329 return; | |
| 330 } | |
| 331 | |
| 332 DLOG_IF(INFO, error != QUIC_NO_ERROR) << "Closing connection (" | |
| 333 << connection_id | |
| 334 << ") due to error: " | |
| 335 << QuicUtils::ErrorToString(error); | |
| 336 | |
| 337 if (closed_session_list_.empty()) { | |
| 338 epoll_server_->RegisterAlarmApproximateDelta( | |
| 339 0, delete_sessions_alarm_.get()); | |
| 340 } | |
| 341 closed_session_list_.push_back(it->second); | |
| 342 CleanUpSession(it); | |
| 343 } | |
| 344 | |
| 345 void QuicDispatcher::OnWriteBlocked( | |
| 346 QuicBlockedWriterInterface* blocked_writer) { | |
| 347 if (!writer_->IsWriteBlocked()) { | |
| 348 LOG(DFATAL) << | |
| 349 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked."; | |
| 350 // Return without adding the connection to the blocked list, to avoid | |
| 351 // infinite loops in OnCanWrite. | |
| 352 return; | |
| 353 } | |
| 354 write_blocked_list_.insert(std::make_pair(blocked_writer, true)); | |
| 355 } | |
| 356 | |
| 357 void QuicDispatcher::OnConnectionAddedToTimeWaitList( | |
| 358 QuicConnectionId connection_id) { | |
| 359 DVLOG(1) << "Connection " << connection_id << " added to time wait list."; | |
| 360 } | |
| 361 | |
| 362 void QuicDispatcher::OnConnectionRemovedFromTimeWaitList( | |
| 363 QuicConnectionId connection_id) { | |
| 364 DVLOG(1) << "Connection " << connection_id << " removed from time wait list."; | |
| 365 } | |
| 366 | |
| 367 QuicPacketWriter* QuicDispatcher::CreateWriter(int fd) { | |
| 368 return new QuicDefaultPacketWriter(fd); | |
| 369 } | |
| 370 | |
| 371 QuicSession* QuicDispatcher::CreateQuicSession( | |
| 372 QuicConnectionId connection_id, | |
| 373 const IPEndPoint& server_address, | |
| 374 const IPEndPoint& client_address) { | |
| 375 QuicServerSession* session = new QuicServerSession( | |
| 376 config_, | |
| 377 CreateQuicConnection(connection_id, server_address, client_address), | |
| 378 this); | |
| 379 session->InitializeSession(crypto_config_); | |
| 380 return session; | |
| 381 } | |
| 382 | |
| 383 QuicConnection* QuicDispatcher::CreateQuicConnection( | |
| 384 QuicConnectionId connection_id, | |
| 385 const IPEndPoint& server_address, | |
| 386 const IPEndPoint& client_address) { | |
| 387 return new QuicConnection(connection_id, | |
| 388 client_address, | |
| 389 helper_.get(), | |
| 390 connection_writer_factory_, | |
| 391 /* owns_writer= */ true, | |
| 392 /* is_server= */ true, | |
| 393 crypto_config_.HasProofSource(), | |
| 394 supported_versions_); | |
| 395 } | |
| 396 | |
| 397 QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { | |
| 398 return new QuicTimeWaitListManager( | |
| 399 writer_.get(), this, epoll_server(), supported_versions()); | |
| 400 } | |
| 401 | |
| 402 bool QuicDispatcher::HandlePacketForTimeWait( | |
| 403 const QuicPacketPublicHeader& header) { | |
| 404 if (header.reset_flag) { | |
| 405 // Public reset packets do not have sequence numbers, so ignore the packet. | |
| 406 return false; | |
| 407 } | |
| 408 | |
| 409 // Switch the framer to the correct version, so that the sequence number can | |
| 410 // be parsed correctly. | |
| 411 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromConnectionId( | |
| 412 header.connection_id)); | |
| 413 | |
| 414 // Continue parsing the packet to extract the sequence number. Then | |
| 415 // send it to the time wait manager in OnUnathenticatedHeader. | |
| 416 return true; | |
| 417 } | |
| 418 | |
| 419 } // namespace tools | |
| 420 } // namespace net | |
| OLD | NEW |