1 // Copyright (c) 2013 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_config.h"
9 #include "base/logging.h"
10 #include "net/quic/crypto/crypto_handshake_message.h"
11 #include "net/quic/crypto/crypto_protocol.h"
12 #include "net/quic/quic_flags.h"
13 #include "net/quic/quic_utils.h"
20 // Reads the value corresponding to |name_| from |msg| into |out|. If the
21 // |name_| is absent in |msg| and |presence| is set to OPTIONAL |out| is set
22 // to |default_value|.
23 QuicErrorCode
ReadUint32(const CryptoHandshakeMessage
& msg
,
25 QuicConfigPresence presence
,
28 string
* error_details
) {
29 DCHECK(error_details
!= nullptr);
30 QuicErrorCode error
= msg
.GetUint32(tag
, out
);
32 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
:
33 if (presence
== PRESENCE_REQUIRED
) {
34 *error_details
= "Missing " + QuicUtils::TagToString(tag
);
37 error
= QUIC_NO_ERROR
;
43 *error_details
= "Bad " + QuicUtils::TagToString(tag
);
50 QuicConfigValue::QuicConfigValue(QuicTag tag
,
51 QuicConfigPresence presence
)
55 QuicConfigValue::~QuicConfigValue() {}
57 QuicNegotiableValue::QuicNegotiableValue(QuicTag tag
,
58 QuicConfigPresence presence
)
59 : QuicConfigValue(tag
, presence
),
62 QuicNegotiableValue::~QuicNegotiableValue() {}
64 QuicNegotiableUint32::QuicNegotiableUint32(QuicTag tag
,
65 QuicConfigPresence presence
)
66 : QuicNegotiableValue(tag
, presence
),
70 QuicNegotiableUint32::~QuicNegotiableUint32() {}
72 void QuicNegotiableUint32::set(uint32 max
, uint32 default_value
) {
73 DCHECK_LE(default_value
, max
);
75 default_value_
= default_value
;
78 uint32
QuicNegotiableUint32::GetUint32() const {
80 return negotiated_value_
;
82 return default_value_
;
85 void QuicNegotiableUint32::ToHandshakeMessage(
86 CryptoHandshakeMessage
* out
) const {
88 out
->SetValue(tag_
, negotiated_value_
);
90 out
->SetValue(tag_
, max_value_
);
94 QuicErrorCode
QuicNegotiableUint32::ProcessPeerHello(
95 const CryptoHandshakeMessage
& peer_hello
,
97 string
* error_details
) {
98 DCHECK(!negotiated());
99 DCHECK(error_details
!= nullptr);
101 QuicErrorCode error
= ReadUint32(peer_hello
,
107 if (error
!= QUIC_NO_ERROR
) {
110 if (hello_type
== SERVER
&& value
> max_value_
) {
112 "Invalid value received for " + QuicUtils::TagToString(tag_
);
113 return QUIC_INVALID_NEGOTIATED_VALUE
;
116 set_negotiated(true);
117 negotiated_value_
= min(value
, max_value_
);
118 return QUIC_NO_ERROR
;
121 QuicNegotiableTag::QuicNegotiableTag(QuicTag tag
, QuicConfigPresence presence
)
122 : QuicNegotiableValue(tag
, presence
),
127 QuicNegotiableTag::~QuicNegotiableTag() {}
129 void QuicNegotiableTag::set(const QuicTagVector
& possible
,
130 QuicTag default_value
) {
131 DCHECK(ContainsQuicTag(possible
, default_value
));
132 possible_values_
= possible
;
133 default_value_
= default_value
;
136 QuicTag
QuicNegotiableTag::GetTag() const {
138 return negotiated_tag_
;
140 return default_value_
;
143 void QuicNegotiableTag::ToHandshakeMessage(CryptoHandshakeMessage
* out
) const {
145 // Because of the way we serialize and parse handshake messages we can
146 // serialize this as value and still parse it as a vector.
147 out
->SetValue(tag_
, negotiated_tag_
);
149 out
->SetVector(tag_
, possible_values_
);
153 QuicErrorCode
QuicNegotiableTag::ReadVector(
154 const CryptoHandshakeMessage
& msg
,
157 string
* error_details
) const {
158 DCHECK(error_details
!= nullptr);
159 QuicErrorCode error
= msg
.GetTaglist(tag_
, out
, out_length
);
161 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
:
162 if (presence_
== PRESENCE_REQUIRED
) {
163 *error_details
= "Missing " + QuicUtils::TagToString(tag_
);
166 error
= QUIC_NO_ERROR
;
168 *out
= &default_value_
;
173 *error_details
= "Bad " + QuicUtils::TagToString(tag_
);
179 QuicErrorCode
QuicNegotiableTag::ProcessPeerHello(
180 const CryptoHandshakeMessage
& peer_hello
,
181 HelloType hello_type
,
182 string
* error_details
) {
183 DCHECK(!negotiated());
184 DCHECK(error_details
!= nullptr);
185 const QuicTag
* received_tags
;
186 size_t received_tags_length
;
187 QuicErrorCode error
= ReadVector(peer_hello
, &received_tags
,
188 &received_tags_length
, error_details
);
189 if (error
!= QUIC_NO_ERROR
) {
193 if (hello_type
== SERVER
) {
194 if (received_tags_length
!= 1 ||
195 !ContainsQuicTag(possible_values_
, *received_tags
)) {
196 *error_details
= "Invalid " + QuicUtils::TagToString(tag_
);
197 return QUIC_INVALID_NEGOTIATED_VALUE
;
199 negotiated_tag_
= *received_tags
;
201 QuicTag negotiated_tag
;
202 if (!QuicUtils::FindMutualTag(possible_values_
,
204 received_tags_length
,
205 QuicUtils::LOCAL_PRIORITY
,
208 *error_details
= "Unsupported " + QuicUtils::TagToString(tag_
);
209 return QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
;
211 negotiated_tag_
= negotiated_tag
;
214 set_negotiated(true);
215 return QUIC_NO_ERROR
;
218 QuicFixedUint32::QuicFixedUint32(QuicTag tag
, QuicConfigPresence presence
)
219 : QuicConfigValue(tag
, presence
),
220 has_send_value_(false),
221 has_receive_value_(false) {
223 QuicFixedUint32::~QuicFixedUint32() {}
225 bool QuicFixedUint32::HasSendValue() const {
226 return has_send_value_
;
229 uint32
QuicFixedUint32::GetSendValue() const {
230 LOG_IF(DFATAL
, !has_send_value_
)
231 << "No send value to get for tag:" << QuicUtils::TagToString(tag_
);
235 void QuicFixedUint32::SetSendValue(uint32 value
) {
236 has_send_value_
= true;
240 bool QuicFixedUint32::HasReceivedValue() const {
241 return has_receive_value_
;
244 uint32
QuicFixedUint32::GetReceivedValue() const {
245 LOG_IF(DFATAL
, !has_receive_value_
)
246 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_
);
247 return receive_value_
;
250 void QuicFixedUint32::SetReceivedValue(uint32 value
) {
251 has_receive_value_
= true;
252 receive_value_
= value
;
255 void QuicFixedUint32::ToHandshakeMessage(CryptoHandshakeMessage
* out
) const {
256 if (has_send_value_
) {
257 out
->SetValue(tag_
, send_value_
);
261 QuicErrorCode
QuicFixedUint32::ProcessPeerHello(
262 const CryptoHandshakeMessage
& peer_hello
,
263 HelloType hello_type
,
264 string
* error_details
) {
265 DCHECK(error_details
!= nullptr);
266 QuicErrorCode error
= peer_hello
.GetUint32(tag_
, &receive_value_
);
268 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
:
269 if (presence_
== PRESENCE_OPTIONAL
) {
270 return QUIC_NO_ERROR
;
272 *error_details
= "Missing " + QuicUtils::TagToString(tag_
);
275 has_receive_value_
= true;
278 *error_details
= "Bad " + QuicUtils::TagToString(tag_
);
284 QuicFixedTag::QuicFixedTag(QuicTag name
,
285 QuicConfigPresence presence
)
286 : QuicConfigValue(name
, presence
),
287 has_send_value_(false),
288 has_receive_value_(false) {
291 QuicFixedTag::~QuicFixedTag() {}
293 bool QuicFixedTag::HasSendValue() const {
294 return has_send_value_
;
297 uint32
QuicFixedTag::GetSendValue() const {
298 LOG_IF(DFATAL
, !has_send_value_
)
299 << "No send value to get for tag:" << QuicUtils::TagToString(tag_
);
303 void QuicFixedTag::SetSendValue(uint32 value
) {
304 has_send_value_
= true;
308 bool QuicFixedTag::HasReceivedValue() const {
309 return has_receive_value_
;
312 uint32
QuicFixedTag::GetReceivedValue() const {
313 LOG_IF(DFATAL
, !has_receive_value_
)
314 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_
);
315 return receive_value_
;
318 void QuicFixedTag::SetReceivedValue(uint32 value
) {
319 has_receive_value_
= true;
320 receive_value_
= value
;
323 void QuicFixedTag::ToHandshakeMessage(CryptoHandshakeMessage
* out
) const {
324 if (has_send_value_
) {
325 out
->SetValue(tag_
, send_value_
);
329 QuicErrorCode
QuicFixedTag::ProcessPeerHello(
330 const CryptoHandshakeMessage
& peer_hello
,
331 HelloType hello_type
,
332 string
* error_details
) {
333 DCHECK(error_details
!= nullptr);
334 QuicErrorCode error
= peer_hello
.GetUint32(tag_
, &receive_value_
);
336 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
:
337 if (presence_
== PRESENCE_OPTIONAL
) {
338 return QUIC_NO_ERROR
;
340 *error_details
= "Missing " + QuicUtils::TagToString(tag_
);
343 has_receive_value_
= true;
346 *error_details
= "Bad " + QuicUtils::TagToString(tag_
);
352 QuicFixedTagVector::QuicFixedTagVector(QuicTag name
,
353 QuicConfigPresence presence
)
354 : QuicConfigValue(name
, presence
),
355 has_send_values_(false),
356 has_receive_values_(false) {
359 QuicFixedTagVector::~QuicFixedTagVector() {}
361 bool QuicFixedTagVector::HasSendValues() const {
362 return has_send_values_
;
365 QuicTagVector
QuicFixedTagVector::GetSendValues() const {
366 LOG_IF(DFATAL
, !has_send_values_
)
367 << "No send values to get for tag:" << QuicUtils::TagToString(tag_
);
371 void QuicFixedTagVector::SetSendValues(const QuicTagVector
& values
) {
372 has_send_values_
= true;
373 send_values_
= values
;
376 bool QuicFixedTagVector::HasReceivedValues() const {
377 return has_receive_values_
;
380 QuicTagVector
QuicFixedTagVector::GetReceivedValues() const {
381 LOG_IF(DFATAL
, !has_receive_values_
)
382 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_
);
383 return receive_values_
;
386 void QuicFixedTagVector::SetReceivedValues(const QuicTagVector
& values
) {
387 has_receive_values_
= true;
388 receive_values_
= values
;
391 void QuicFixedTagVector::ToHandshakeMessage(CryptoHandshakeMessage
* out
) const {
392 if (has_send_values_
) {
393 out
->SetVector(tag_
, send_values_
);
397 QuicErrorCode
QuicFixedTagVector::ProcessPeerHello(
398 const CryptoHandshakeMessage
& peer_hello
,
399 HelloType hello_type
,
400 string
* error_details
) {
401 DCHECK(error_details
!= nullptr);
402 const QuicTag
* received_tags
;
403 size_t received_tags_length
;
404 QuicErrorCode error
=
405 peer_hello
.GetTaglist(tag_
, &received_tags
, &received_tags_length
);
407 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
:
408 if (presence_
== PRESENCE_OPTIONAL
) {
409 return QUIC_NO_ERROR
;
411 *error_details
= "Missing " + QuicUtils::TagToString(tag_
);
414 DVLOG(1) << "Received Connection Option tags from receiver.";
415 has_receive_values_
= true;
416 for (size_t i
= 0; i
< received_tags_length
; ++i
) {
417 receive_values_
.push_back(received_tags
[i
]);
421 *error_details
= "Bad " + QuicUtils::TagToString(tag_
);
427 QuicConfig::QuicConfig()
428 : max_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
429 max_idle_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
430 max_undecryptable_packets_(0),
431 congestion_feedback_(kCGST
, PRESENCE_OPTIONAL
),
432 connection_options_(kCOPT
, PRESENCE_OPTIONAL
),
433 idle_connection_state_lifetime_seconds_(kICSL
, PRESENCE_REQUIRED
),
434 silent_close_(kSCLS
, PRESENCE_OPTIONAL
),
435 max_streams_per_connection_(kMSPC
, PRESENCE_REQUIRED
),
436 bytes_for_connection_id_(kTCID
, PRESENCE_OPTIONAL
),
437 initial_round_trip_time_us_(kIRTT
, PRESENCE_OPTIONAL
),
438 initial_stream_flow_control_window_bytes_(kSFCW
, PRESENCE_OPTIONAL
),
439 initial_session_flow_control_window_bytes_(kCFCW
, PRESENCE_OPTIONAL
),
440 socket_receive_buffer_(kSRBF
, PRESENCE_OPTIONAL
) {
444 QuicConfig::~QuicConfig() {}
446 void QuicConfig::SetConnectionOptionsToSend(
447 const QuicTagVector
& connection_options
) {
448 connection_options_
.SetSendValues(connection_options
);
451 bool QuicConfig::HasReceivedConnectionOptions() const {
452 return connection_options_
.HasReceivedValues();
455 QuicTagVector
QuicConfig::ReceivedConnectionOptions() const {
456 return connection_options_
.GetReceivedValues();
459 bool QuicConfig::HasSendConnectionOptions() const {
460 return connection_options_
.HasSendValues();
463 QuicTagVector
QuicConfig::SendConnectionOptions() const {
464 return connection_options_
.GetSendValues();
467 void QuicConfig::SetIdleConnectionStateLifetime(
468 QuicTime::Delta max_idle_connection_state_lifetime
,
469 QuicTime::Delta default_idle_conection_state_lifetime
) {
470 idle_connection_state_lifetime_seconds_
.set(
471 static_cast<uint32
>(max_idle_connection_state_lifetime
.ToSeconds()),
472 static_cast<uint32
>(default_idle_conection_state_lifetime
.ToSeconds()));
475 QuicTime::Delta
QuicConfig::IdleConnectionStateLifetime() const {
476 return QuicTime::Delta::FromSeconds(
477 idle_connection_state_lifetime_seconds_
.GetUint32());
480 void QuicConfig::SetSilentClose(bool silent_close
) {
481 silent_close_
.set(silent_close
? 1 : 0, silent_close
? 1 : 0);
484 bool QuicConfig::SilentClose() const {
485 return silent_close_
.GetUint32() > 0;
488 void QuicConfig::SetMaxStreamsPerConnection(size_t max_streams
,
489 size_t default_streams
) {
490 max_streams_per_connection_
.set(max_streams
, default_streams
);
493 uint32
QuicConfig::MaxStreamsPerConnection() const {
494 return max_streams_per_connection_
.GetUint32();
497 bool QuicConfig::HasSetBytesForConnectionIdToSend() const {
498 return bytes_for_connection_id_
.HasSendValue();
501 void QuicConfig::SetBytesForConnectionIdToSend(uint32 bytes
) {
502 bytes_for_connection_id_
.SetSendValue(bytes
);
505 bool QuicConfig::HasReceivedBytesForConnectionId() const {
506 return bytes_for_connection_id_
.HasReceivedValue();
509 uint32
QuicConfig::ReceivedBytesForConnectionId() const {
510 return bytes_for_connection_id_
.GetReceivedValue();
513 void QuicConfig::SetInitialRoundTripTimeUsToSend(uint32 rtt
) {
514 initial_round_trip_time_us_
.SetSendValue(rtt
);
517 bool QuicConfig::HasReceivedInitialRoundTripTimeUs() const {
518 return initial_round_trip_time_us_
.HasReceivedValue();
521 uint32
QuicConfig::ReceivedInitialRoundTripTimeUs() const {
522 return initial_round_trip_time_us_
.GetReceivedValue();
525 bool QuicConfig::HasInitialRoundTripTimeUsToSend() const {
526 return initial_round_trip_time_us_
.HasSendValue();
529 uint32
QuicConfig::GetInitialRoundTripTimeUsToSend() const {
530 return initial_round_trip_time_us_
.GetSendValue();
533 void QuicConfig::SetInitialStreamFlowControlWindowToSend(uint32 window_bytes
) {
534 if (window_bytes
< kMinimumFlowControlSendWindow
) {
535 LOG(DFATAL
) << "Initial stream flow control receive window ("
536 << window_bytes
<< ") cannot be set lower than default ("
537 << kMinimumFlowControlSendWindow
<< ").";
538 window_bytes
= kMinimumFlowControlSendWindow
;
540 initial_stream_flow_control_window_bytes_
.SetSendValue(window_bytes
);
543 uint32
QuicConfig::GetInitialStreamFlowControlWindowToSend() const {
544 return initial_stream_flow_control_window_bytes_
.GetSendValue();
547 bool QuicConfig::HasReceivedInitialStreamFlowControlWindowBytes() const {
548 return initial_stream_flow_control_window_bytes_
.HasReceivedValue();
551 uint32
QuicConfig::ReceivedInitialStreamFlowControlWindowBytes() const {
552 return initial_stream_flow_control_window_bytes_
.GetReceivedValue();
555 void QuicConfig::SetInitialSessionFlowControlWindowToSend(uint32 window_bytes
) {
556 if (window_bytes
< kMinimumFlowControlSendWindow
) {
557 LOG(DFATAL
) << "Initial session flow control receive window ("
558 << window_bytes
<< ") cannot be set lower than default ("
559 << kMinimumFlowControlSendWindow
<< ").";
560 window_bytes
= kMinimumFlowControlSendWindow
;
562 initial_session_flow_control_window_bytes_
.SetSendValue(window_bytes
);
565 uint32
QuicConfig::GetInitialSessionFlowControlWindowToSend() const {
566 return initial_session_flow_control_window_bytes_
.GetSendValue();
569 bool QuicConfig::HasReceivedInitialSessionFlowControlWindowBytes() const {
570 return initial_session_flow_control_window_bytes_
.HasReceivedValue();
573 uint32
QuicConfig::ReceivedInitialSessionFlowControlWindowBytes() const {
574 return initial_session_flow_control_window_bytes_
.GetReceivedValue();
577 void QuicConfig::SetSocketReceiveBufferToSend(uint32 tcp_receive_window
) {
578 socket_receive_buffer_
.SetSendValue(tcp_receive_window
);
581 uint32
QuicConfig::GetSocketReceiveBufferToSend() const {
582 return socket_receive_buffer_
.GetSendValue();
585 bool QuicConfig::HasReceivedSocketReceiveBuffer() const {
586 return socket_receive_buffer_
.HasReceivedValue();
589 uint32
QuicConfig::ReceivedSocketReceiveBuffer() const {
590 return socket_receive_buffer_
.GetReceivedValue();
593 bool QuicConfig::negotiated() const {
594 // TODO(ianswett): Add the negotiated parameters once and iterate over all
595 // of them in negotiated, ToHandshakeMessage, ProcessClientHello, and
596 // ProcessServerHello.
597 return congestion_feedback_
.negotiated() &&
598 idle_connection_state_lifetime_seconds_
.negotiated() &&
599 max_streams_per_connection_
.negotiated();
602 void QuicConfig::SetDefaults() {
603 QuicTagVector congestion_feedback
;
604 // TODO(alyssar) stop sending this once QUIC_VERSION_23 is sunset.
605 // This field was required until version 22 was removed but by the time
606 // QUIC_VERSION_23 is sunset, no users of QUIC_VERSION_24 should be expecting
608 congestion_feedback
.push_back(kQBIC
);
609 congestion_feedback_
.set(congestion_feedback
, kQBIC
);
610 idle_connection_state_lifetime_seconds_
.set(kMaximumIdleTimeoutSecs
,
611 kDefaultIdleTimeoutSecs
);
612 if (FLAGS_quic_allow_silent_close
) {
613 silent_close_
.set(1, 0);
615 silent_close_
.set(0, 0);
617 SetMaxStreamsPerConnection(kDefaultMaxStreamsPerConnection
,
618 kDefaultMaxStreamsPerConnection
);
619 max_time_before_crypto_handshake_
=
620 QuicTime::Delta::FromSeconds(kMaxTimeForCryptoHandshakeSecs
);
621 max_idle_time_before_crypto_handshake_
=
622 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
);
623 max_undecryptable_packets_
= kDefaultMaxUndecryptablePackets
;
625 SetInitialStreamFlowControlWindowToSend(kMinimumFlowControlSendWindow
);
626 SetInitialSessionFlowControlWindowToSend(kMinimumFlowControlSendWindow
);
629 void QuicConfig::ToHandshakeMessage(CryptoHandshakeMessage
* out
) const {
630 congestion_feedback_
.ToHandshakeMessage(out
);
631 idle_connection_state_lifetime_seconds_
.ToHandshakeMessage(out
);
632 silent_close_
.ToHandshakeMessage(out
);
633 max_streams_per_connection_
.ToHandshakeMessage(out
);
634 bytes_for_connection_id_
.ToHandshakeMessage(out
);
635 initial_round_trip_time_us_
.ToHandshakeMessage(out
);
636 initial_stream_flow_control_window_bytes_
.ToHandshakeMessage(out
);
637 initial_session_flow_control_window_bytes_
.ToHandshakeMessage(out
);
638 socket_receive_buffer_
.ToHandshakeMessage(out
);
639 connection_options_
.ToHandshakeMessage(out
);
642 QuicErrorCode
QuicConfig::ProcessPeerHello(
643 const CryptoHandshakeMessage
& peer_hello
,
644 HelloType hello_type
,
645 string
* error_details
) {
646 DCHECK(error_details
!= nullptr);
648 QuicErrorCode error
= QUIC_NO_ERROR
;
649 if (error
== QUIC_NO_ERROR
) {
650 error
= congestion_feedback_
.ProcessPeerHello(
651 peer_hello
, hello_type
, error_details
);
653 if (error
== QUIC_NO_ERROR
) {
654 error
= idle_connection_state_lifetime_seconds_
.ProcessPeerHello(
655 peer_hello
, hello_type
, error_details
);
657 if (error
== QUIC_NO_ERROR
) {
659 silent_close_
.ProcessPeerHello(peer_hello
, hello_type
, error_details
);
661 if (error
== QUIC_NO_ERROR
) {
662 error
= max_streams_per_connection_
.ProcessPeerHello(
663 peer_hello
, hello_type
, error_details
);
665 if (error
== QUIC_NO_ERROR
) {
666 error
= bytes_for_connection_id_
.ProcessPeerHello(
667 peer_hello
, hello_type
, error_details
);
669 if (error
== QUIC_NO_ERROR
) {
670 error
= initial_round_trip_time_us_
.ProcessPeerHello(
671 peer_hello
, hello_type
, error_details
);
673 if (error
== QUIC_NO_ERROR
) {
674 error
= initial_stream_flow_control_window_bytes_
.ProcessPeerHello(
675 peer_hello
, hello_type
, error_details
);
677 if (error
== QUIC_NO_ERROR
) {
678 error
= initial_session_flow_control_window_bytes_
.ProcessPeerHello(
679 peer_hello
, hello_type
, error_details
);
681 if (error
== QUIC_NO_ERROR
) {
682 error
= socket_receive_buffer_
.ProcessPeerHello(
683 peer_hello
, hello_type
, error_details
);
685 if (error
== QUIC_NO_ERROR
) {
686 error
= connection_options_
.ProcessPeerHello(
687 peer_hello
, hello_type
, error_details
);