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
, /*unused*/ QuicTime::Zero(), true),
174 framer_visitor_(new QuicFramerVisitor(this)) {
175 framer_
.set_visitor(framer_visitor_
.get());
178 QuicDispatcher::~QuicDispatcher() {
179 STLDeleteValues(&session_map_
);
180 STLDeleteElements(&closed_session_list_
);
183 void QuicDispatcher::Initialize(QuicServerPacketWriter
* writer
) {
184 DCHECK(writer_
== nullptr);
185 writer_
.reset(writer
);
186 time_wait_list_manager_
.reset(CreateQuicTimeWaitListManager());
189 void QuicDispatcher::ProcessPacket(const IPEndPoint
& server_address
,
190 const IPEndPoint
& client_address
,
191 const QuicEncryptedPacket
& packet
) {
192 current_server_address_
= server_address
;
193 current_client_address_
= client_address
;
194 current_packet_
= &packet
;
195 // ProcessPacket will cause the packet to be dispatched in
196 // OnUnauthenticatedPublicHeader, or sent to the time wait list manager
197 // in OnAuthenticatedHeader.
198 framer_
.ProcessPacket(packet
);
199 // TODO(rjshade): Return a status describing if/why a packet was dropped,
200 // and log somehow. Maybe expose as a varz.
203 bool QuicDispatcher::OnUnauthenticatedPublicHeader(
204 const QuicPacketPublicHeader
& header
) {
205 QuicSession
* session
= nullptr;
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 QuicConnectionId connection_id
= header
.connection_id
;
215 SessionMap::iterator it
= session_map_
.find(connection_id
);
216 if (it
== session_map_
.end()) {
217 if (header
.reset_flag
) {
220 if (time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
)) {
221 return HandlePacketForTimeWait(header
);
224 // Ensure the packet has a version negotiation bit set before creating a new
225 // session for it. All initial packets for a new connection are required to
226 // have the flag set. Otherwise it may be a stray packet.
227 if (header
.version_flag
) {
228 session
= CreateQuicSession(connection_id
, current_server_address_
,
229 current_client_address_
);
232 if (session
== nullptr) {
233 DVLOG(1) << "Failed to create session for " << connection_id
;
234 // Add this connection_id fo the time-wait state, to safely reject future
237 if (header
.version_flag
&&
238 !framer_
.IsSupportedVersion(header
.versions
.front())) {
239 // TODO(ianswett): Produce a no-version version negotiation packet.
243 // Use the version in the packet if possible, otherwise assume the latest.
244 QuicVersion version
= header
.version_flag
? header
.versions
.front() :
245 supported_versions_
.front();
246 time_wait_list_manager_
->AddConnectionIdToTimeWait(connection_id
, version
,
248 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
));
249 return HandlePacketForTimeWait(header
);
251 DVLOG(1) << "Created new session for " << connection_id
;
252 session_map_
.insert(std::make_pair(connection_id
, session
));
254 session
= it
->second
;
257 session
->connection()->ProcessUdpPacket(
258 current_server_address_
, current_client_address_
, *current_packet_
);
260 // Do not parse the packet further. The session will process it completely.
264 void QuicDispatcher::OnUnauthenticatedHeader(const QuicPacketHeader
& header
) {
265 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(
266 header
.public_header
.connection_id
));
267 time_wait_list_manager_
->ProcessPacket(current_server_address_
,
268 current_client_address_
,
269 header
.public_header
.connection_id
,
270 header
.packet_sequence_number
,
274 void QuicDispatcher::CleanUpSession(SessionMap::iterator it
) {
275 QuicConnection
* connection
= it
->second
->connection();
276 QuicEncryptedPacket
* connection_close_packet
=
277 connection
->ReleaseConnectionClosePacket();
278 write_blocked_list_
.erase(connection
);
279 time_wait_list_manager_
->AddConnectionIdToTimeWait(it
->first
,
280 connection
->version(),
281 connection_close_packet
);
282 session_map_
.erase(it
);
285 void QuicDispatcher::DeleteSessions() {
286 STLDeleteElements(&closed_session_list_
);
289 void QuicDispatcher::OnCanWrite() {
290 // We finished a write: the socket should not be blocked.
291 writer_
->SetWritable();
293 // Give all the blocked writers one chance to write, until we're blocked again
294 // or there's no work left.
295 while (!write_blocked_list_
.empty() && !writer_
->IsWriteBlocked()) {
296 QuicBlockedWriterInterface
* blocked_writer
=
297 write_blocked_list_
.begin()->first
;
298 write_blocked_list_
.erase(write_blocked_list_
.begin());
299 blocked_writer
->OnCanWrite();
303 bool QuicDispatcher::HasPendingWrites() const {
304 return !write_blocked_list_
.empty();
307 void QuicDispatcher::Shutdown() {
308 while (!session_map_
.empty()) {
309 QuicSession
* session
= session_map_
.begin()->second
;
310 session
->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY
);
311 // Validate that the session removes itself from the session map on close.
312 DCHECK(session_map_
.empty() || session_map_
.begin()->second
!= session
);
317 void QuicDispatcher::OnConnectionClosed(QuicConnectionId connection_id
,
318 QuicErrorCode error
) {
319 SessionMap::iterator it
= session_map_
.find(connection_id
);
320 if (it
== session_map_
.end()) {
321 LOG(DFATAL
) << "ConnectionId " << connection_id
322 << " does not exist in the session map. "
323 << "Error: " << QuicUtils::ErrorToString(error
);
324 LOG(DFATAL
) << base::debug::StackTrace().ToString();
327 DVLOG_IF(1, error
!= QUIC_NO_ERROR
) << "Closing connection ("
329 << ") due to error: "
330 << QuicUtils::ErrorToString(error
);
331 if (closed_session_list_
.empty()) {
332 delete_sessions_alarm_
->Set(helper_
->GetClock()->ApproximateNow());
334 closed_session_list_
.push_back(it
->second
);
338 void QuicDispatcher::OnWriteBlocked(
339 QuicBlockedWriterInterface
* blocked_writer
) {
340 if (!writer_
->IsWriteBlocked()) {
342 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked.";
343 // Return without adding the connection to the blocked list, to avoid
344 // infinite loops in OnCanWrite.
347 write_blocked_list_
.insert(std::make_pair(blocked_writer
, true));
350 void QuicDispatcher::OnConnectionAddedToTimeWaitList(
351 QuicConnectionId connection_id
) {
352 DVLOG(1) << "Connection " << connection_id
<< " added to time wait list.";
355 void QuicDispatcher::OnConnectionRemovedFromTimeWaitList(
356 QuicConnectionId connection_id
) {
357 DVLOG(1) << "Connection " << connection_id
<< " removed from time wait list.";
360 QuicSession
* QuicDispatcher::CreateQuicSession(
361 QuicConnectionId connection_id
,
362 const IPEndPoint
& server_address
,
363 const IPEndPoint
& client_address
) {
364 // The QuicSession takes ownership of |connection| below.
365 QuicConnection
* connection
= new QuicConnection(
366 connection_id
, client_address
, helper_
, connection_writer_factory_
,
367 /* owns_writer= */ true, /* is_server= */ true,
368 crypto_config_
.HasProofSource(), supported_versions_
);
370 QuicServerSession
* session
= new QuicServerSession(config_
, connection
, this);
371 session
->InitializeSession(crypto_config_
);
375 QuicTimeWaitListManager
* QuicDispatcher::CreateQuicTimeWaitListManager() {
376 return new QuicTimeWaitListManager(
377 writer_
.get(), this, helper_
, supported_versions());
380 bool QuicDispatcher::HandlePacketForTimeWait(
381 const QuicPacketPublicHeader
& header
) {
382 if (header
.reset_flag
) {
383 // Public reset packets do not have sequence numbers, so ignore the packet.
387 // Switch the framer to the correct version, so that the sequence number can
388 // be parsed correctly.
389 framer_
.set_version(time_wait_list_manager_
->GetQuicVersionFromConnectionId(
390 header
.connection_id
));
392 // Continue parsing the packet to extract the sequence number. Then
393 // send it to the time wait manager in OnUnathenticatedHeader.