OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/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_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" | |
18 | |
19 namespace net { | |
20 | |
21 using base::StringPiece; | |
22 using std::make_pair; | |
23 using std::find; | |
24 | |
25 class DeleteSessionsAlarm : public QuicAlarm::Delegate { | |
26 public: | |
27 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) | |
28 : dispatcher_(dispatcher) { | |
29 } | |
30 | |
31 QuicTime OnAlarm() override { | |
32 dispatcher_->DeleteSessions(); | |
33 return QuicTime::Zero(); | |
34 } | |
35 | |
36 private: | |
37 QuicDispatcher* dispatcher_; | |
38 }; | |
39 | |
40 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { | |
41 public: | |
42 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) | |
43 : dispatcher_(dispatcher), | |
44 connection_id_(0) {} | |
45 | |
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); | |
52 } | |
53 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override { | |
54 dispatcher_->OnUnauthenticatedHeader(header); | |
55 return false; | |
56 } | |
57 void OnError(QuicFramer* framer) override { | |
58 DVLOG(1) << QuicUtils::ErrorToString(framer->error()); | |
59 } | |
60 | |
61 bool OnProtocolVersionMismatch(QuicVersion /*received_version*/) override { | |
62 if (dispatcher_->time_wait_list_manager()->IsConnectionIdInTimeWait( | |
63 connection_id_)) { | |
64 // Keep processing after protocol mismatch - this will be dealt with by | |
65 // the TimeWaitListManager. | |
66 return true; | |
67 } else { | |
68 DLOG(DFATAL) << "Version mismatch, connection ID (" << connection_id_ | |
69 << ") not in time wait list."; | |
70 return false; | |
71 } | |
72 } | |
73 | |
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 { | |
78 DCHECK(false); | |
79 } | |
80 void OnVersionNegotiationPacket( | |
81 const QuicVersionNegotiationPacket& /*packet*/) override { | |
82 DCHECK(false); | |
83 } | |
84 void OnDecryptedPacket(EncryptionLevel level) override { DCHECK(false); } | |
85 bool OnPacketHeader(const QuicPacketHeader& /*header*/) override { | |
86 DCHECK(false); | |
87 return false; | |
88 } | |
89 void OnRevivedPacket() override { DCHECK(false); } | |
90 void OnFecProtectedPayload(StringPiece /*payload*/) override { | |
91 DCHECK(false); | |
92 } | |
93 bool OnStreamFrame(const QuicStreamFrame& /*frame*/) override { | |
94 DCHECK(false); | |
95 return false; | |
96 } | |
97 bool OnAckFrame(const QuicAckFrame& /*frame*/) override { | |
98 DCHECK(false); | |
99 return false; | |
100 } | |
101 bool OnStopWaitingFrame(const QuicStopWaitingFrame& /*frame*/) override { | |
102 DCHECK(false); | |
103 return false; | |
104 } | |
105 bool OnPingFrame(const QuicPingFrame& /*frame*/) override { | |
106 DCHECK(false); | |
107 return false; | |
108 } | |
109 bool OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) override { | |
110 DCHECK(false); | |
111 return false; | |
112 } | |
113 bool OnConnectionCloseFrame( | |
114 const QuicConnectionCloseFrame& /*frame*/) override { | |
115 DCHECK(false); | |
116 return false; | |
117 } | |
118 bool OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) override { | |
119 DCHECK(false); | |
120 return false; | |
121 } | |
122 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& /*frame*/) override { | |
123 DCHECK(false); | |
124 return false; | |
125 } | |
126 bool OnBlockedFrame(const QuicBlockedFrame& frame) override { | |
127 DCHECK(false); | |
128 return false; | |
129 } | |
130 void OnFecData(const QuicFecData& /*fec*/) override { DCHECK(false); } | |
131 void OnPacketComplete() override { DCHECK(false); } | |
132 | |
133 private: | |
134 QuicDispatcher* dispatcher_; | |
135 | |
136 // Latched in OnUnauthenticatedPublicHeader for use later. | |
137 QuicConnectionId connection_id_; | |
138 }; | |
139 | |
140 QuicPacketWriter* QuicDispatcher::DefaultPacketWriterFactory::Create( | |
141 QuicServerPacketWriter* writer, | |
142 QuicConnection* connection) { | |
143 return new QuicPerConnectionPacketWriter(writer, connection); | |
144 } | |
145 | |
146 QuicDispatcher::PacketWriterFactoryAdapter::PacketWriterFactoryAdapter( | |
147 QuicDispatcher* dispatcher) | |
148 : dispatcher_(dispatcher) {} | |
149 | |
150 QuicDispatcher::PacketWriterFactoryAdapter::~PacketWriterFactoryAdapter() {} | |
151 | |
152 QuicPacketWriter* QuicDispatcher::PacketWriterFactoryAdapter::Create( | |
153 QuicConnection* connection) const { | |
154 return dispatcher_->packet_writer_factory_->Create( | |
155 dispatcher_->writer_.get(), | |
156 connection); | |
157 } | |
158 | |
159 QuicDispatcher::QuicDispatcher(const QuicConfig& config, | |
160 const QuicCryptoServerConfig& crypto_config, | |
161 const QuicVersionVector& supported_versions, | |
162 PacketWriterFactory* packet_writer_factory, | |
163 QuicConnectionHelperInterface* helper) | |
164 : config_(config), | |
165 crypto_config_(crypto_config), | |
166 helper_(helper), | |
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()); | |
176 } | |
177 | |
178 QuicDispatcher::~QuicDispatcher() { | |
179 STLDeleteValues(&session_map_); | |
180 STLDeleteElements(&closed_session_list_); | |
181 } | |
182 | |
183 void QuicDispatcher::Initialize(QuicServerPacketWriter* writer) { | |
184 DCHECK(writer_ == nullptr); | |
185 writer_.reset(writer); | |
186 time_wait_list_manager_.reset(CreateQuicTimeWaitListManager()); | |
187 } | |
188 | |
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. | |
201 } | |
202 | |
203 bool QuicDispatcher::OnUnauthenticatedPublicHeader( | |
204 const QuicPacketPublicHeader& header) { | |
205 QuicSession* session = nullptr; | |
206 | |
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 | |
209 // it. | |
210 if (current_client_address_.port() == 0) { | |
211 return false; | |
212 } | |
213 | |
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) { | |
218 return false; | |
219 } | |
220 if (time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)) { | |
221 return HandlePacketForTimeWait(header); | |
222 } | |
223 | |
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_); | |
230 } | |
231 | |
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 | |
235 // packets. | |
236 | |
237 if (header.version_flag && | |
238 !framer_.IsSupportedVersion(header.versions.front())) { | |
239 // TODO(ianswett): Produce a no-version version negotiation packet. | |
240 return false; | |
241 } | |
242 | |
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, | |
247 nullptr); | |
248 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); | |
249 return HandlePacketForTimeWait(header); | |
250 } | |
251 DVLOG(1) << "Created new session for " << connection_id; | |
252 session_map_.insert(std::make_pair(connection_id, session)); | |
253 } else { | |
254 session = it->second; | |
255 } | |
256 | |
257 session->connection()->ProcessUdpPacket( | |
258 current_server_address_, current_client_address_, *current_packet_); | |
259 | |
260 // Do not parse the packet further. The session will process it completely. | |
261 return false; | |
262 } | |
263 | |
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, | |
271 *current_packet_); | |
272 } | |
273 | |
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); | |
283 } | |
284 | |
285 void QuicDispatcher::DeleteSessions() { | |
286 STLDeleteElements(&closed_session_list_); | |
287 } | |
288 | |
289 void QuicDispatcher::OnCanWrite() { | |
290 // We finished a write: the socket should not be blocked. | |
291 writer_->SetWritable(); | |
292 | |
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(); | |
300 } | |
301 } | |
302 | |
303 bool QuicDispatcher::HasPendingWrites() const { | |
304 return !write_blocked_list_.empty(); | |
305 } | |
306 | |
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); | |
313 } | |
314 DeleteSessions(); | |
315 } | |
316 | |
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(); | |
325 return; | |
326 } | |
327 DVLOG_IF(1, error != QUIC_NO_ERROR) << "Closing connection (" | |
328 << connection_id | |
329 << ") due to error: " | |
330 << QuicUtils::ErrorToString(error); | |
331 if (closed_session_list_.empty()) { | |
332 delete_sessions_alarm_->Set(helper_->GetClock()->ApproximateNow()); | |
333 } | |
334 closed_session_list_.push_back(it->second); | |
335 CleanUpSession(it); | |
336 } | |
337 | |
338 void QuicDispatcher::OnWriteBlocked( | |
339 QuicBlockedWriterInterface* blocked_writer) { | |
340 if (!writer_->IsWriteBlocked()) { | |
341 LOG(DFATAL) << | |
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. | |
345 return; | |
346 } | |
347 write_blocked_list_.insert(std::make_pair(blocked_writer, true)); | |
348 } | |
349 | |
350 void QuicDispatcher::OnConnectionAddedToTimeWaitList( | |
351 QuicConnectionId connection_id) { | |
352 DVLOG(1) << "Connection " << connection_id << " added to time wait list."; | |
353 } | |
354 | |
355 void QuicDispatcher::OnConnectionRemovedFromTimeWaitList( | |
356 QuicConnectionId connection_id) { | |
357 DVLOG(1) << "Connection " << connection_id << " removed from time wait list."; | |
358 } | |
359 | |
360 QuicSession* QuicDispatcher::CreateQuicSession( | |
361 QuicConnectionId connection_id, | |
362 const IPEndPoint& server_address, | |
363 const IPEndPoint& client_address) { | |
364 QuicServerSession* session = new QuicServerSession( | |
365 config_, | |
366 CreateQuicConnection(connection_id, server_address, client_address), | |
367 this); | |
368 session->InitializeSession(crypto_config_); | |
369 return session; | |
370 } | |
371 | |
372 QuicConnection* QuicDispatcher::CreateQuicConnection( | |
373 QuicConnectionId connection_id, | |
374 const IPEndPoint& server_address, | |
375 const IPEndPoint& client_address) { | |
376 return new QuicConnection(connection_id, | |
377 client_address, | |
378 helper_, | |
379 connection_writer_factory_, | |
380 /* owns_writer= */ true, | |
381 /* is_server= */ true, | |
382 crypto_config_.HasProofSource(), | |
383 supported_versions_); | |
384 } | |
385 | |
386 QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { | |
387 return new QuicTimeWaitListManager( | |
388 writer_.get(), this, helper_, supported_versions()); | |
389 } | |
390 | |
391 bool QuicDispatcher::HandlePacketForTimeWait( | |
392 const QuicPacketPublicHeader& header) { | |
393 if (header.reset_flag) { | |
394 // Public reset packets do not have sequence numbers, so ignore the packet. | |
395 return false; | |
396 } | |
397 | |
398 // Switch the framer to the correct version, so that the sequence number can | |
399 // be parsed correctly. | |
400 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromConnectionId( | |
401 header.connection_id)); | |
402 | |
403 // Continue parsing the packet to extract the sequence number. Then | |
404 // send it to the time wait manager in OnUnathenticatedHeader. | |
405 return true; | |
406 } | |
407 | |
408 } // namespace net | |
OLD | NEW |