1 // Copyright 2014 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.
5 #include "net/quic/quic_dispatcher.h"
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_connection_helper.h"
14 #include "net/quic/quic_flags.h"
15 #include "net/quic/quic_per_connection_packet_writer.h"
16 #include "net/quic/quic_time_wait_list_manager.h"
17 #include "net/quic/quic_utils.h"
21 using base::StringPiece
;
25 class DeleteSessionsAlarm
: public QuicAlarm::Delegate
{
27 explicit DeleteSessionsAlarm(QuicDispatcher
* dispatcher
)
28 : dispatcher_(dispatcher
) {
31 QuicTime
OnAlarm() override
{
32 dispatcher_
->DeleteSessions();
33 return QuicTime::Zero();
37 QuicDispatcher
* dispatcher_
;
40 class QuicDispatcher::QuicFramerVisitor
: public QuicFramerVisitorInterface
{
42 explicit QuicFramerVisitor(QuicDispatcher
* dispatcher
)
43 : dispatcher_(dispatcher
),
46 // QuicFramerVisitorInterface implementation
47 void OnPacket() override
{}
48 bool OnUnauthenticatedPublicHeader(
49 const QuicPacketPublicHeader
& header
) override
{
50 connection_id_
= header
.connection_id
;
51 return dispatcher_
->OnUnauthenticatedPublicHeader(header
);
53 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
{
54 dispatcher_
->OnUnauthenticatedHeader(header
);
57 void OnError(QuicFramer
* framer
) override
{
58 DVLOG(1) << QuicUtils::ErrorToString(framer
->error());
61 bool OnProtocolVersionMismatch(QuicVersion
/*received_version*/) override
{
62 if (dispatcher_
->time_wait_list_manager()->IsConnectionIdInTimeWait(
64 // Keep processing after protocol mismatch - this will be dealt with by
65 // the TimeWaitListManager.
68 DLOG(DFATAL
) << "Version mismatch, connection ID (" << connection_id_
69 << ") not in time wait list.";
74 // The following methods should never get called because we always return
75 // false from OnUnauthenticatedHeader(). As a result, we never process the
76 // payload of the packet.
77 void OnPublicResetPacket(const QuicPublicResetPacket
& /*packet*/) override
{
80 void OnVersionNegotiationPacket(
81 const QuicVersionNegotiationPacket
& /*packet*/) override
{
84 void OnDecryptedPacket(EncryptionLevel level
) override
{ DCHECK(false); }
85 bool OnPacketHeader(const QuicPacketHeader
& /*header*/) override
{
89 void OnRevivedPacket() override
{ DCHECK(false); }
90 void OnFecProtectedPayload(StringPiece
/*payload*/) override
{
93 bool OnStreamFrame(const QuicStreamFrame
& /*frame*/) override
{
97 bool OnAckFrame(const QuicAckFrame
& /*frame*/) override
{
101 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& /*frame*/) override
{
105 bool OnPingFrame(const QuicPingFrame
& /*frame*/) override
{
109 bool OnRstStreamFrame(const QuicRstStreamFrame
& /*frame*/) override
{
113 bool OnConnectionCloseFrame(
114 const QuicConnectionCloseFrame
& /*frame*/) override
{
118 bool OnGoAwayFrame(const QuicGoAwayFrame
& /*frame*/) override
{
122 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& /*frame*/) override
{
126 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
{
130 void OnFecData(const QuicFecData
& /*fec*/) override
{ DCHECK(false); }
131 void OnPacketComplete() override
{ DCHECK(false); }
134 QuicDispatcher
* dispatcher_
;
136 // Latched in OnUnauthenticatedPublicHeader for use later.
137 QuicConnectionId connection_id_
;
140 QuicPacketWriter
* QuicDispatcher::DefaultPacketWriterFactory::Create(
141 QuicServerPacketWriter
* writer
,
142 QuicConnection
* connection
) {
143 return new QuicPerConnectionPacketWriter(writer
, connection
);
146 QuicDispatcher::PacketWriterFactoryAdapter::PacketWriterFactoryAdapter(
147 QuicDispatcher
* dispatcher
)
148 : dispatcher_(dispatcher
) {}
150 QuicDispatcher::PacketWriterFactoryAdapter::~PacketWriterFactoryAdapter() {}
152 QuicPacketWriter
* QuicDispatcher::PacketWriterFactoryAdapter::Create(
153 QuicConnection
* connection
) const {
154 return dispatcher_
->packet_writer_factory_
->Create(
155 dispatcher_
->writer_
.get(),
159 QuicDispatcher::QuicDispatcher(const QuicConfig
& config
,
160 const QuicCryptoServerConfig
& crypto_config
,
161 const QuicVersionVector
& supported_versions
,
162 PacketWriterFactory
* packet_writer_factory
,
163 QuicConnectionHelperInterface
* helper
)
165 crypto_config_(crypto_config
),
167 delete_sessions_alarm_(
168 helper_
->CreateAlarm(new DeleteSessionsAlarm(this))),
169 packet_writer_factory_(packet_writer_factory
),
170 connection_writer_factory_(this),
171 supported_versions_(supported_versions
),
172 current_packet_(nullptr),
173 framer_(supported_versions
,
174 /*unused*/ QuicTime::Zero(),
175 Perspective::IS_SERVER
),
176 framer_visitor_(new QuicFramerVisitor(this)) {
177 framer_
.set_visitor(framer_visitor_
.get());
180 QuicDispatcher::~QuicDispatcher() {
181 STLDeleteValues(&session_map_
);
182 STLDeleteElements(&closed_session_list_
);
185 void QuicDispatcher::Initialize(QuicServerPacketWriter
* writer
) {
186 DCHECK(writer_
== nullptr);
187 writer_
.reset(writer
);
188 time_wait_list_manager_
.reset(CreateQuicTimeWaitListManager());
191 void QuicDispatcher::ProcessPacket(const IPEndPoint
& server_address
,
192 const IPEndPoint
& client_address
,
193 const QuicEncryptedPacket
& packet
) {
194 current_server_address_
= server_address
;
195 current_client_address_
= client_address
;
196 current_packet_
= &packet
;
197 // ProcessPacket will cause the packet to be dispatched in
198 // OnUnauthenticatedPublicHeader, or sent to the time wait list manager
199 // in OnAuthenticatedHeader.
200 framer_
.ProcessPacket(packet
);
201 // TODO(rjshade): Return a status describing if/why a packet was dropped,
202 // and log somehow. Maybe expose as a varz.
205 bool QuicDispatcher::OnUnauthenticatedPublicHeader(
206 const QuicPacketPublicHeader
& header
) {
207 // Port zero is only allowed for unidirectional UDP, so is disallowed by QUIC.
208 // Given that we can't even send a reply rejecting the packet, just black hole
210 if (current_client_address_
.port() == 0) {
214 // The session that we have identified as the one to which this packet
216 QuicSession
* session
= nullptr;
217 QuicConnectionId connection_id
= header
.connection_id
;
218 SessionMap::iterator it
= session_map_
.find(connection_id
);
219 if (it
== session_map_
.end()) {
220 if (time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
)) {
221 return HandlePacketForTimeWait(header
);
224 // The packet has an unknown connection ID.
225 // If the packet is a public reset, there is nothing we must do or can do.
226 if (header
.reset_flag
) {
230 // All packets within a connection sent by a client before receiving a
231 // response from the server are required to have the version negotiation
232 // flag set. Since this may be a client continuing a connection we lost
233 // track of via server restart, send a rejection to fast-fail the
235 if (!header
.version_flag
) {
236 DVLOG(1) << "Packet without version arrived for unknown connection ID "
238 // Add this connection_id fo the time-wait state, to safely reject future
240 QuicVersion version
= supported_versions_
.front();
241 time_wait_list_manager_
->AddConnectionIdToTimeWait(connection_id
, version
,
243 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
));
244 return HandlePacketForTimeWait(header
);
247 session
= AdditionalValidityChecksThenCreateSession(header
, connection_id
);
248 if (session
== nullptr) {
252 session
= it
->second
;
255 session
->connection()->ProcessUdpPacket(
256 current_server_address_
, current_client_address_
, *current_packet_
);
258 // Do not parse the packet further. The session methods called above have
259 // processed it completely.
263 QuicSession
* QuicDispatcher::AdditionalValidityChecksThenCreateSession(
264 const QuicPacketPublicHeader
& header
,
265 QuicConnectionId connection_id
) {
266 QuicSession
* session
= CreateQuicSession(
267 connection_id
, current_server_address_
, current_client_address_
);
269 if (session
== nullptr) {
270 DVLOG(1) << "Failed to create session for " << connection_id
;
272 if (!framer_
.IsSupportedVersion(header
.versions
.front())) {
273 // TODO(ianswett): Produce packet saying "no supported version".
277 // Add this connection_id to the time-wait state, to safely reject future
279 QuicVersion version
= header
.versions
.front();
280 time_wait_list_manager_
->AddConnectionIdToTimeWait(connection_id
, version
,
282 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
));
283 HandlePacketForTimeWait(header
);
288 DVLOG(1) << "Created new session for connection ID " << connection_id
;
289 session_map_
.insert(std::make_pair(connection_id
, session
));
294 void QuicDispatcher::OnUnauthenticatedHeader(const QuicPacketHeader
& header
) {
295 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(
296 header
.public_header
.connection_id
));
297 time_wait_list_manager_
->ProcessPacket(current_server_address_
,
298 current_client_address_
,
299 header
.public_header
.connection_id
,
300 header
.packet_sequence_number
,
304 void QuicDispatcher::CleanUpSession(SessionMap::iterator it
) {
305 QuicConnection
* connection
= it
->second
->connection();
306 QuicEncryptedPacket
* connection_close_packet
=
307 connection
->ReleaseConnectionClosePacket();
308 write_blocked_list_
.erase(connection
);
309 time_wait_list_manager_
->AddConnectionIdToTimeWait(it
->first
,
310 connection
->version(),
311 connection_close_packet
);
312 session_map_
.erase(it
);
315 void QuicDispatcher::DeleteSessions() {
316 STLDeleteElements(&closed_session_list_
);
319 void QuicDispatcher::OnCanWrite() {
320 // We finished a write: the socket should not be blocked.
321 writer_
->SetWritable();
323 // Give all the blocked writers one chance to write, until we're blocked again
324 // or there's no work left.
325 while (!write_blocked_list_
.empty() && !writer_
->IsWriteBlocked()) {
326 QuicBlockedWriterInterface
* blocked_writer
=
327 write_blocked_list_
.begin()->first
;
328 write_blocked_list_
.erase(write_blocked_list_
.begin());
329 blocked_writer
->OnCanWrite();
333 bool QuicDispatcher::HasPendingWrites() const {
334 return !write_blocked_list_
.empty();
337 void QuicDispatcher::Shutdown() {
338 while (!session_map_
.empty()) {
339 QuicSession
* session
= session_map_
.begin()->second
;
340 session
->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY
);
341 // Validate that the session removes itself from the session map on close.
342 DCHECK(session_map_
.empty() || session_map_
.begin()->second
!= session
);
347 void QuicDispatcher::OnConnectionClosed(QuicConnectionId connection_id
,
348 QuicErrorCode error
) {
349 SessionMap::iterator it
= session_map_
.find(connection_id
);
350 if (it
== session_map_
.end()) {
351 LOG(DFATAL
) << "ConnectionId " << connection_id
352 << " does not exist in the session map. "
353 << "Error: " << QuicUtils::ErrorToString(error
);
354 LOG(DFATAL
) << base::debug::StackTrace().ToString();
357 DVLOG_IF(1, error
!= QUIC_NO_ERROR
) << "Closing connection ("
359 << ") due to error: "
360 << QuicUtils::ErrorToString(error
);
361 if (closed_session_list_
.empty()) {
362 delete_sessions_alarm_
->Set(helper_
->GetClock()->ApproximateNow());
364 closed_session_list_
.push_back(it
->second
);
368 void QuicDispatcher::OnWriteBlocked(
369 QuicBlockedWriterInterface
* blocked_writer
) {
370 if (!writer_
->IsWriteBlocked()) {
372 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked.";
373 // Return without adding the connection to the blocked list, to avoid
374 // infinite loops in OnCanWrite.
377 write_blocked_list_
.insert(std::make_pair(blocked_writer
, true));
380 void QuicDispatcher::OnConnectionAddedToTimeWaitList(
381 QuicConnectionId connection_id
) {
382 DVLOG(1) << "Connection " << connection_id
<< " added to time wait list.";
385 void QuicDispatcher::OnConnectionRemovedFromTimeWaitList(
386 QuicConnectionId connection_id
) {
387 DVLOG(1) << "Connection " << connection_id
<< " removed from time wait list.";
390 QuicSession
* QuicDispatcher::CreateQuicSession(
391 QuicConnectionId connection_id
,
392 const IPEndPoint
& server_address
,
393 const IPEndPoint
& client_address
) {
394 // The QuicSession takes ownership of |connection| below.
395 QuicConnection
* connection
= new QuicConnection(
396 connection_id
, client_address
, helper_
, connection_writer_factory_
,
397 /* owns_writer= */ true, Perspective::IS_SERVER
,
398 crypto_config_
.HasProofSource(), supported_versions_
);
400 QuicServerSession
* session
= new QuicServerSession(config_
, connection
, this);
401 session
->InitializeSession(crypto_config_
);
405 QuicTimeWaitListManager
* QuicDispatcher::CreateQuicTimeWaitListManager() {
406 return new QuicTimeWaitListManager(
407 writer_
.get(), this, helper_
, supported_versions());
410 bool QuicDispatcher::HandlePacketForTimeWait(
411 const QuicPacketPublicHeader
& header
) {
412 if (header
.reset_flag
) {
413 // Public reset packets do not have sequence numbers, so ignore the packet.
417 // Switch the framer to the correct version, so that the sequence number can
418 // be parsed correctly.
419 framer_
.set_version(time_wait_list_manager_
->GetQuicVersionFromConnectionId(
420 header
.connection_id
));
422 // Continue parsing the packet to extract the sequence number. Then
423 // send it to the time wait manager in OnUnathenticatedHeader.