blob: 85e6da67e70551c65319b077222128b7f1406c5d [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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// A server side dispatcher which dispatches a given client's data to their
6// stream.
7
8#ifndef QUICHE_QUIC_CORE_QUIC_DISPATCHER_H_
9#define QUICHE_QUIC_CORE_QUIC_DISPATCHER_H_
10
11#include <memory>
12#include <vector>
13
14#include "base/macros.h"
15#include "net/third_party/quiche/src/quic/core/crypto/quic_compressed_certs_cache.h"
16#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
17#include "net/third_party/quiche/src/quic/core/quic_blocked_writer_interface.h"
18#include "net/third_party/quiche/src/quic/core/quic_buffered_packet_store.h"
19#include "net/third_party/quiche/src/quic/core/quic_connection.h"
20#include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h"
21#include "net/third_party/quiche/src/quic/core/quic_packets.h"
22#include "net/third_party/quiche/src/quic/core/quic_process_packet_interface.h"
23#include "net/third_party/quiche/src/quic/core/quic_session.h"
24#include "net/third_party/quiche/src/quic/core/quic_time_wait_list_manager.h"
25#include "net/third_party/quiche/src/quic/core/quic_version_manager.h"
26#include "net/third_party/quiche/src/quic/core/stateless_rejector.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_string.h"
30
31namespace quic {
32namespace test {
33class QuicDispatcherPeer;
34} // namespace test
35
36class QuicConfig;
37class QuicCryptoServerConfig;
38
39class QuicDispatcher : public QuicTimeWaitListManager::Visitor,
40 public ProcessPacketInterface,
41 public QuicFramerVisitorInterface,
42 public QuicBufferedPacketStore::VisitorInterface {
43 public:
44 // Ideally we'd have a linked_hash_set: the boolean is unused.
45 typedef QuicLinkedHashMap<QuicBlockedWriterInterface*, bool> WriteBlockedList;
46
47 QuicDispatcher(const QuicConfig* config,
48 const QuicCryptoServerConfig* crypto_config,
49 QuicVersionManager* version_manager,
50 std::unique_ptr<QuicConnectionHelperInterface> helper,
51 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper,
52 std::unique_ptr<QuicAlarmFactory> alarm_factory,
53 uint8_t expected_connection_id_length);
54 QuicDispatcher(const QuicDispatcher&) = delete;
55 QuicDispatcher& operator=(const QuicDispatcher&) = delete;
56
57 ~QuicDispatcher() override;
58
59 // Takes ownership of |writer|.
60 void InitializeWithWriter(QuicPacketWriter* writer);
61
62 // Process the incoming packet by creating a new session, passing it to
63 // an existing session, or passing it to the time wait list.
64 void ProcessPacket(const QuicSocketAddress& self_address,
65 const QuicSocketAddress& peer_address,
66 const QuicReceivedPacket& packet) override;
67
68 // Called when the socket becomes writable to allow queued writes to happen.
69 virtual void OnCanWrite();
70
71 // Returns true if there's anything in the blocked writer list.
72 virtual bool HasPendingWrites() const;
73
74 // Sends ConnectionClose frames to all connected clients.
75 void Shutdown();
76
77 // QuicSession::Visitor interface implementation (via inheritance of
78 // QuicTimeWaitListManager::Visitor):
79 // Ensure that the closed connection is cleaned up asynchronously.
80 void OnConnectionClosed(QuicConnectionId connection_id,
81 QuicErrorCode error,
82 const QuicString& error_details,
83 ConnectionCloseSource source) override;
84
85 // QuicSession::Visitor interface implementation (via inheritance of
86 // QuicTimeWaitListManager::Visitor):
87 // Queues the blocked writer for later resumption.
88 void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) override;
89
90 // QuicSession::Visitor interface implementation (via inheritance of
91 // QuicTimeWaitListManager::Visitor):
92 // Collects reset error code received on streams.
93 void OnRstStreamReceived(const QuicRstStreamFrame& frame) override;
94
95 // QuicSession::Visitor interface implementation (via inheritance of
96 // QuicTimeWaitListManager::Visitor):
97 // Collects reset error code received on streams.
98 void OnStopSendingReceived(const QuicStopSendingFrame& frame) override;
99
100 // QuicTimeWaitListManager::Visitor interface implementation
101 // Called whenever the time wait list manager adds a new connection to the
102 // time-wait list.
103 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id) override;
104
105 using SessionMap = QuicUnorderedMap<QuicConnectionId,
106 std::unique_ptr<QuicSession>,
107 QuicConnectionIdHash>;
108
109 const SessionMap& session_map() const { return session_map_; }
110
111 // Deletes all sessions on the closed session list and clears the list.
112 virtual void DeleteSessions();
113
114 // The largest packet number we expect to receive with a connection
115 // ID for a connection that is not established yet. The current design will
116 // send a handshake and then up to 50 or so data packets, and then it may
117 // resend the handshake packet up to 10 times. (Retransmitted packets are
118 // sent with unique packet numbers.)
119 static const uint64_t kMaxReasonableInitialPacketNumber = 100;
120 static_assert(kMaxReasonableInitialPacketNumber >=
121 kInitialCongestionWindow + 10,
122 "kMaxReasonableInitialPacketNumber is unreasonably small "
123 "relative to kInitialCongestionWindow.");
124
125 // QuicFramerVisitorInterface implementation. Not expected to be called
126 // outside of this class.
127 void OnPacket() override;
128 // Called when the public header has been parsed.
129 bool OnUnauthenticatedPublicHeader(const QuicPacketHeader& header) override;
130 // Called when the private header has been parsed of a data packet that is
131 // destined for the time wait manager.
132 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override;
133 void OnError(QuicFramer* framer) override;
134 bool OnProtocolVersionMismatch(ParsedQuicVersion received_version,
135 PacketHeaderFormat form) override;
136
137 // The following methods should never get called because
138 // OnUnauthenticatedPublicHeader() or OnUnauthenticatedHeader() (whichever
139 // was called last), will return false and prevent a subsequent invocation
140 // of these methods. Thus, the payload of the packet is never processed in
141 // the dispatcher.
142 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override;
143 void OnVersionNegotiationPacket(
144 const QuicVersionNegotiationPacket& packet) override;
145 void OnDecryptedPacket(EncryptionLevel level) override;
146 bool OnPacketHeader(const QuicPacketHeader& header) override;
147 void OnCoalescedPacket(const QuicEncryptedPacket& packet) override;
148 bool OnStreamFrame(const QuicStreamFrame& frame) override;
149 bool OnCryptoFrame(const QuicCryptoFrame& frame) override;
150 bool OnAckFrameStart(QuicPacketNumber largest_acked,
151 QuicTime::Delta ack_delay_time) override;
152 bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) override;
153 bool OnAckTimestamp(QuicPacketNumber packet_number,
154 QuicTime timestamp) override;
155 bool OnAckFrameEnd(QuicPacketNumber start) override;
156 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override;
157 bool OnPaddingFrame(const QuicPaddingFrame& frame) override;
158 bool OnPingFrame(const QuicPingFrame& frame) override;
159 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override;
160 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override;
161 bool OnApplicationCloseFrame(const QuicApplicationCloseFrame& frame) override;
162 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
163 bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) override;
164 bool OnPathResponseFrame(const QuicPathResponseFrame& frame) override;
165 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override;
166 bool OnMaxStreamIdFrame(const QuicMaxStreamIdFrame& frame) override;
167 bool OnStreamIdBlockedFrame(const QuicStreamIdBlockedFrame& frame) override;
168 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
169 bool OnBlockedFrame(const QuicBlockedFrame& frame) override;
170 bool OnNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame) override;
171 bool OnRetireConnectionIdFrame(
172 const QuicRetireConnectionIdFrame& frame) override;
173 bool OnNewTokenFrame(const QuicNewTokenFrame& frame) override;
174 bool OnMessageFrame(const QuicMessageFrame& frame) override;
175 void OnPacketComplete() override;
176 bool IsValidStatelessResetToken(QuicUint128 token) const override;
177 void OnAuthenticatedIetfStatelessResetPacket(
178 const QuicIetfStatelessResetPacket& packet) override;
179
180 // QuicBufferedPacketStore::VisitorInterface implementation.
181 void OnExpiredPackets(QuicConnectionId connection_id,
182 QuicBufferedPacketStore::BufferedPacketList
183 early_arrived_packets) override;
184
185 // Create connections for previously buffered CHLOs as many as allowed.
186 virtual void ProcessBufferedChlos(size_t max_connections_to_create);
187
188 // Return true if there is CHLO buffered.
189 virtual bool HasChlosBuffered() const;
190
191 protected:
192 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
193 const QuicSocketAddress& peer_address,
194 QuicStringPiece alpn,
195 const ParsedQuicVersion& version) = 0;
196
197 // Called when a connection is rejected statelessly.
198 virtual void OnConnectionRejectedStatelessly();
199
200 // Called when a connection is closed statelessly.
201 virtual void OnConnectionClosedStatelessly(QuicErrorCode error);
202
203 // Returns true if cheap stateless rejection should be attempted.
204 virtual bool ShouldAttemptCheapStatelessRejection();
205
206 // Values to be returned by ValidityChecks() to indicate what should be done
207 // with a packet. Fates with greater values are considered to be higher
208 // priority, in that if one validity check indicates a lower-valued fate and
209 // another validity check indicates a higher-valued fate, the higher-valued
210 // fate should be obeyed.
211 enum QuicPacketFate {
212 // Process the packet normally, which is usually to establish a connection.
213 kFateProcess,
214 // Put the connection ID into time-wait state and send a public reset.
215 kFateTimeWait,
216 // Buffer the packet.
217 kFateBuffer,
218 // Drop the packet (ignore and give no response).
219 kFateDrop,
220 };
221
222 // This method is called by OnUnauthenticatedHeader on packets not associated
223 // with a known connection ID. It applies validity checks and returns a
224 // QuicPacketFate to tell what should be done with the packet.
225 virtual QuicPacketFate ValidityChecks(const QuicPacketHeader& header);
226
227 // Create and return the time wait list manager for this dispatcher, which
228 // will be owned by the dispatcher as time_wait_list_manager_
229 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
230
231 // Called when |connection_id| doesn't have an open connection yet, to buffer
232 // |current_packet_| until it can be delivered to the connection.
233 void BufferEarlyPacket(QuicConnectionId connection_id,
234 bool ietf_quic,
235 ParsedQuicVersion version);
236
237 // Called when |current_packet_| is a CHLO packet. Creates a new connection
238 // and delivers any buffered packets for that connection id.
239 void ProcessChlo(PacketHeaderFormat form, ParsedQuicVersion version);
240
241 // Returns the actual client address of the current packet.
242 // This function should only be called once per packet at the very beginning
243 // of ProcessPacket(), its result is saved to |current_client_address_|, which
244 // is guaranteed to be valid even in the stateless rejector's callback(i.e.
245 // OnStatelessRejectorProcessDone).
246 // By default, this function returns |current_peer_address_|, subclasses have
247 // the option to override this function to return a different address.
248 virtual const QuicSocketAddress GetClientAddress() const;
249
250 // Return true if dispatcher wants to destroy session outside of
251 // OnConnectionClosed() call stack.
252 virtual bool ShouldDestroySessionAsynchronously();
253
254 QuicTimeWaitListManager* time_wait_list_manager() {
255 return time_wait_list_manager_.get();
256 }
257
258 const QuicTransportVersionVector& GetSupportedTransportVersions();
259
260 const ParsedQuicVersionVector& GetSupportedVersions();
261
262 QuicConnectionId current_connection_id() const {
263 return current_connection_id_;
264 }
265 const QuicSocketAddress& current_self_address() const {
266 return current_self_address_;
267 }
268 const QuicSocketAddress& current_peer_address() const {
269 return current_peer_address_;
270 }
271 const QuicSocketAddress& current_client_address() const {
272 return current_client_address_;
273 }
274 const QuicReceivedPacket& current_packet() const { return *current_packet_; }
275
276 const QuicConfig& config() const { return *config_; }
277
278 const QuicCryptoServerConfig* crypto_config() const { return crypto_config_; }
279
280 QuicCompressedCertsCache* compressed_certs_cache() {
281 return &compressed_certs_cache_;
282 }
283
284 QuicConnectionHelperInterface* helper() { return helper_.get(); }
285
286 QuicCryptoServerStream::Helper* session_helper() {
287 return session_helper_.get();
288 }
289
290 QuicAlarmFactory* alarm_factory() { return alarm_factory_.get(); }
291
292 QuicPacketWriter* writer() { return writer_.get(); }
293
294 // Returns true if a session should be created for a connection with an
295 // unknown version identified by |version_label|.
296 virtual bool ShouldCreateSessionForUnknownVersion(
297 QuicVersionLabel version_label);
298
299 void SetLastError(QuicErrorCode error);
300
301 // Called when the public header has been parsed and the session has been
302 // looked up, and the session was not found in the active list of sessions.
303 // Returns false if processing should stop after this call.
304 virtual bool OnUnauthenticatedUnknownPublicHeader(
305 const QuicPacketHeader& header);
306
307 // Called when a new connection starts to be handled by this dispatcher.
308 // Either this connection is created or its packets is buffered while waiting
309 // for CHLO. Returns true if a new connection should be created or its packets
310 // should be buffered, false otherwise.
311 virtual bool ShouldCreateOrBufferPacketForConnection(
312 QuicConnectionId connection_id,
313 bool ietf_quic);
314
315 bool HasBufferedPackets(QuicConnectionId connection_id);
316
317 // Called when BufferEarlyPacket() fail to buffer the packet.
318 virtual void OnBufferPacketFailure(
319 QuicBufferedPacketStore::EnqueuePacketResult result,
320 QuicConnectionId connection_id);
321
322 // Removes the session from the session map and write blocked list, and adds
323 // the ConnectionId to the time-wait list. If |session_closed_statelessly| is
324 // true, any future packets for the ConnectionId will be black-holed.
325 virtual void CleanUpSession(SessionMap::iterator it,
326 QuicConnection* connection,
327 bool session_closed_statelessly,
328 ConnectionCloseSource source);
329
330 void StopAcceptingNewConnections();
331
332 // Called to terminate a connection statelessly. Depending on |format|, either
333 // 1) send connection close with |error_code| and |error_details| and add
334 // connection to time wait list or 2) directly add connection to time wait
335 // list with |action|.
336 void StatelesslyTerminateConnection(
337 QuicConnectionId connection_id,
338 PacketHeaderFormat format,
339 ParsedQuicVersion version,
340 QuicErrorCode error_code,
341 const QuicString& error_details,
342 QuicTimeWaitListManager::TimeWaitAction action);
343
344 // Save/Restore per packet context. Used by async stateless rejector.
345 virtual std::unique_ptr<QuicPerPacketContext> GetPerPacketContext() const;
346 virtual void RestorePerPacketContext(
347 std::unique_ptr<QuicPerPacketContext> /*context*/) {}
348
349 // Skip validating that the public flags are set to legal values.
350 void DisableFlagValidation();
351
352 private:
353 friend class test::QuicDispatcherPeer;
354 friend class StatelessRejectorProcessDoneCallback;
355
356 typedef QuicUnorderedSet<QuicConnectionId, QuicConnectionIdHash>
357 QuicConnectionIdSet;
358
359 // Attempts to reject the connection statelessly, if stateless rejects are
360 // possible and if the current packet contains a CHLO message. Determines a
361 // fate which describes what subsequent processing should be performed on the
362 // packets, like ValidityChecks, and invokes ProcessUnauthenticatedHeaderFate.
363 void MaybeRejectStatelessly(QuicConnectionId connection_id,
364 PacketHeaderFormat form,
365 ParsedQuicVersion version);
366
367 // Deliver |packets| to |session| for further processing.
368 void DeliverPacketsToSession(
369 const std::list<QuicBufferedPacketStore::BufferedPacket>& packets,
370 QuicSession* session);
371
372 // Perform the appropriate actions on the current packet based on |fate| -
373 // either process, buffer, or drop it.
374 void ProcessUnauthenticatedHeaderFate(QuicPacketFate fate,
375 QuicConnectionId connection_id,
376 PacketHeaderFormat form,
377 ParsedQuicVersion version);
378
379 // Invoked when StatelessRejector::Process completes. |first_version| is the
380 // version of the packet which initiated the stateless reject.
381 // WARNING: This function can be called when a async proof returns, i.e. not
382 // from a stack traceable to ProcessPacket().
383 // TODO(fayang): maybe consider not using callback when there is no crypto
384 // involved.
385 void OnStatelessRejectorProcessDone(
386 std::unique_ptr<StatelessRejector> rejector,
387 const QuicSocketAddress& current_client_address,
388 const QuicSocketAddress& current_peer_address,
389 const QuicSocketAddress& current_self_address,
390 std::unique_ptr<QuicReceivedPacket> current_packet,
391 ParsedQuicVersion first_version,
392 PacketHeaderFormat current_packet_format);
393
394 // Examine the state of the rejector and decide what to do with the current
395 // packet.
396 void ProcessStatelessRejectorState(
397 std::unique_ptr<StatelessRejector> rejector,
398 QuicTransportVersion first_version,
399 PacketHeaderFormat form);
400
401 void set_new_sessions_allowed_per_event_loop(
402 int16_t new_sessions_allowed_per_event_loop) {
403 new_sessions_allowed_per_event_loop_ = new_sessions_allowed_per_event_loop;
404 }
405
406 const QuicConfig* config_;
407
408 const QuicCryptoServerConfig* crypto_config_;
409
410 // The cache for most recently compressed certs.
411 QuicCompressedCertsCache compressed_certs_cache_;
412
413 // The list of connections waiting to write.
414 WriteBlockedList write_blocked_list_;
415
416 SessionMap session_map_;
417
418 // Entity that manages connection_ids in time wait state.
419 std::unique_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
420
421 // The list of closed but not-yet-deleted sessions.
422 std::vector<std::unique_ptr<QuicSession>> closed_session_list_;
423
424 // The helper used for all connections.
425 std::unique_ptr<QuicConnectionHelperInterface> helper_;
426
427 // The helper used for all sessions.
428 std::unique_ptr<QuicCryptoServerStream::Helper> session_helper_;
429
430 // Creates alarms.
431 std::unique_ptr<QuicAlarmFactory> alarm_factory_;
432
433 // An alarm which deletes closed sessions.
434 std::unique_ptr<QuicAlarm> delete_sessions_alarm_;
435
436 // The writer to write to the socket with.
437 std::unique_ptr<QuicPacketWriter> writer_;
438
439 // Packets which are buffered until a connection can be created to handle
440 // them.
441 QuicBufferedPacketStore buffered_packets_;
442
443 // Set of connection IDs for which asynchronous CHLO processing is in
444 // progress, making it necessary to buffer any other packets which arrive on
445 // that connection until CHLO processing is complete.
446 QuicConnectionIdSet temporarily_buffered_connections_;
447
448 // Information about the packet currently being handled.
449
450 // Used for stateless rejector to generate and validate source address token.
451 QuicSocketAddress current_client_address_;
452 QuicSocketAddress current_peer_address_;
453 QuicSocketAddress current_self_address_;
454 const QuicReceivedPacket* current_packet_;
455 // If |current_packet_| is a CHLO packet, the extracted alpn.
456 QuicString current_alpn_;
457 QuicConnectionId current_connection_id_;
458
459 // Used to get the supported versions based on flag. Does not own.
460 QuicVersionManager* version_manager_;
461
462 QuicFramer framer_;
463
464 // The last error set by SetLastError(), which is called by
465 // framer_visitor_->OnError().
466 QuicErrorCode last_error_;
467
468 // A backward counter of how many new sessions can be create within current
469 // event loop. When reaches 0, it means can't create sessions for now.
470 int16_t new_sessions_allowed_per_event_loop_;
471
472 // True if this dispatcher is not draining.
473 bool accept_new_connections_;
474};
475
476} // namespace quic
477
478#endif // QUICHE_QUIC_CORE_QUIC_DISPATCHER_H_